/*
* top - a top users display for Unix
*
* SYNOPSIS: Any Sun running SunOS 5.x (Solaris 2.x)
*
* DESCRIPTION:
* This is the machine-dependent module for SunOS 5.x (Solaris 2).
* There is some support for MP architectures.
* This makes top work on the following systems:
* SunOS 5.0 (not tested)
* SunOS 5.1
* SunOS 5.2
* SunOS 5.3
*
* Tested on a SPARCclassic with SunOS 5.1, using gcc-2.3.3, and
* SPARCsystem 600 with SunOS 5.2, using Sun C
*
* Note that this same file is used for SunOS 5.4, but only with
* SOLARIS24 defined. Use the file "m_sunos54.c" to make that happen
* automagically.
*
* LIBS: -lelf -lkvm -lkstat
*
* CFLAGS: -DHAVE_GETOPT -DORDER
*
*
* AUTHORS: Torsten Kasch <torsten@techfak.uni-bielefeld.de>
* Robert Boucher <boucher@sofkin.ca>
* CONTRIBUTORS: Marc Cohen <marc@aai.com>
* Charles Hedrick <hedrick@geneva.rutgers.edu>
* William L. Jones <jones@chpc>
* Petri Kutvonen <kutvonen@cs.helsinki.fi>
* Casper Dik <casper@fwi.uva.nl>
* Tim Pugh <tpugh@oce.orst.edu>
*/
#define _KMEMUSER
#include "top.h"
#include "machine.h"
#include "utils.h"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
#include <nlist.h>
#include <string.h>
#include <kvm.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/signal.h>
#include <sys/fault.h>
#include <sys/sysinfo.h>
#include <sys/sysmacros.h>
#include <sys/syscall.h>
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/procfs.h>
#include <sys/vm.h>
#include <sys/var.h>
#include <sys/cpuvar.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/priocntl.h>
#include <sys/tspriocntl.h>
#include <sys/processor.h>
#include <vm/anon.h>
#include <math.h>
#define USE_KSTAT
#ifdef USE_KSTAT
#include <kstat.h>
#endif
#define UNIX "/dev/ksyms"
#define KMEM "/dev/kmem"
#define PROCFS "/proc"
#define CPUSTATES 5
#ifndef PRIO_MIN
#define PRIO_MIN -20
#endif
#ifndef PRIO_MAX
#define PRIO_MAX 20
#endif
#ifndef FSCALE
#define FSHIFT 8 /* bits to right of fixed binary point */
#define FSCALE (1<<FSHIFT)
#endif /* FSCALE */
#define loaddouble(la) ((double)(la) / FSCALE)
#define dbl_align(x) (((unsigned long)(x)+(sizeof(double)-1)) & \
~(sizeof(double)-1))
#ifdef SOLARIS24
/*
* snarfed from <sys/procfs.h>:
* The following percent numbers are 16-bit binary
* fractions [0 .. 1] with the binary point to the
* right of the high-order bit (one == 0x8000)
*/
#define percent_cpu(pp) (((double)pp->pr_pctcpu)/0x8000*100)
#define weighted_cpu(pp) (*(double *)dbl_align(pp->pr_filler))
#else
#define percent_cpu(pp) (*(double *)dbl_align(&pp->pr_filler[0]))
#define weighted_cpu(pp) (*(double *)dbl_align(&pp->pr_filler[2]))
#endif
/* definitions for indices in the nlist array */
#define X_AVENRUN 0
#define X_MPID 1
#define X_CPU 2
#define X_V 3
#define X_NPROC 4
#define X_ANONINFO 5
#define X_FREEMEM 6
#define X_MAXMEM 7
#define X_AVAILRMEM 8
#define X_SWAPFS_MINFREE 9
#define X_NCPUS 10
static struct nlist nlst[] =
{
{"avenrun"}, /* 0 */
{"mpid"}, /* 1 */
{"cpu"}, /* 2 */
{"v"}, /* 3 */
{"nproc"}, /* 4 */
{"anoninfo"}, /* 5 */
{"freemem"}, /* 6 */
{"maxmem"}, /* 7 */
{"availrmem"}, /* 8 */
{"swapfs_minfree"}, /* 9 */
{"ncpus"}, /* 10 */
{0}
};
static unsigned long avenrun_offset;
static unsigned long mpid_offset;
#ifdef USE_KSTAT
static kstat_t **cpu_ks;
static cpu_stat_t *cpu_stat;
#else
static unsigned long *cpu_offset;
#endif
static unsigned long nproc_offset;
static unsigned long freemem_offset;
static unsigned long maxmem_offset;
static unsigned long availrmem_offset;
static unsigned long swapfs_minfree_offset;
static unsigned long anoninfo_offset;
/* get_process_info passes back a handle. This is what it looks like: */
struct handle
{
struct prpsinfo **next_proc;/* points to next valid proc pointer */
int remaining; /* number of pointers remaining */
};
/*
* Structure for keeping track of CPU times from last time around
* the program. We keep these things in a hash table, which is
* recreated at every cycle.
*/
struct oldproc
{
pid_t oldpid;
double oldtime;
double oldpct;
};
int oldprocs; /* size of table */
#define HASH(x) ((x << 1) % oldprocs)
/*
* GCC assumes that all doubles are aligned. Unfortunately it
* doesn't round up the structure size to be a multiple of 8.
* Thus we'll get a coredump when going through array. The
* following is a size rounded up to 8.
*/
#define PRPSINFOSIZE dbl_align(sizeof(struct prpsinfo))
/*
* These definitions control the format of the per-process area
*/
static char header[] =
" PID X PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND";
/* 0123456 -- field to fill in starts at header+6 */
#define UNAME_START 6
#define Proc_format \
"%5d %-8.8s %3d %4d %5s %5s %-5s %6s %5.2f%% %5.2f%% %s"
/* process state names for the "STATE" column of the display */
/* the extra nulls in the string "run" are for adding a slash and
the processor number when needed */
char *state_abbrev[] =
{"", "sleep", "run", "zombie", "stop", "start", "cpu", "swap"};
int process_states[8];
char *procstatenames[] =
{
"", " sleeping, ", " running, ", " zombie, ", " stopped, ",
" starting, ", " on cpu, ", " swapped, ",
NULL
};
int cpu_states[CPUSTATES];
char *cpustatenames[] =
{"idle", "user", "kernel", "iowait", "swap", NULL};
#define CPUSTATE_IOWAIT 3
#define CPUSTATE_SWAP 4
/* these are for detailing the memory statistics */
int memory_stats[5];
char *memorynames[] =
{"K real, ", "K active, ", "K free, ", "K swap, ", "K free swap", NULL};
/* these are names given to allowed sorting orders -- first is default */
char *ordernames[] =
{"cpu", "size", "res", "time", NULL};
/* forward definitions for comparison functions */
int compare_cpu();
int compare_size();
int compare_res();
int compare_time();
int (*proc_compares[])() = {
compare_cpu,
compare_size,
compare_res,
compare_time,
NULL };
kvm_t *kd;
static DIR *procdir;
static int nproc;
static int ncpus;
/* these are for keeping track of the proc array */
static int bytes;
static struct prpsinfo *pbase;
static struct prpsinfo **pref;
static struct oldproc *oldbase;
/* pagetok function is really a pointer to an appropriate function */
static int pageshift;
static int (*p_pagetok) ();
#define pagetok(size) ((*p_pagetok)(size))
/* useful externals */
extern int errno;
extern char *sys_errlist[];
extern char *myname;
extern int check_nlist ();
extern int gettimeofday ();
extern int getkval ();
extern void perror ();
extern void getptable ();
extern void quit ();
extern int nlist ();
int pagetok_none(int size)
{
return(size);
}
int pagetok_left(int size)
{
return(size << pageshift);
}
int pagetok_right(int size)
{
return(size >> pageshift);
}
int
machine_init (struct statics *statics)
{
static struct var v;
struct oldproc *op, *endbase;
int i;
int offset;
processor_info_t pi;
/* perform the kvm_open */
kd = kvm_open (NULL, NULL, NULL, O_RDONLY, "top");
/* turn off super user privs */
seteuid(getuid());
/* fill in the statics information */
statics->procstate_names = procstatenames;
statics->cpustate_names = cpustatenames;
statics->memory_names = memorynames;
statics->order_names = ordernames;
/* test kvm_open return value */
if (kd == NULL)
{
perror ("kvm_open");
return (-1);
}
if (kvm_nlist (kd, nlst) < 0)
{
perror ("kvm_nlist");
return (-1);
}
if (check_nlist (nlst) != 0)
return (-1);
/* NPROC Tuning parameter for max number of processes */
(void) getkval (nlst[X_V].n_value, &v, sizeof (struct var), nlst[X_V].n_name);
np
没有合适的资源?快使用搜索试试~ 我知道了~
linux top命令源代码
5星 · 超过95%的资源 需积分: 42 204 下载量 129 浏览量
2010-08-13
17:09:51
上传
评论
收藏 204KB GZ 举报
温馨提示
共100个文件
c:49个
man:23个
h:12个
linux top命令源码 top命令相当windows的任务管理器,查看资源使用状况。
资源推荐
资源详情
资源评论
收起资源包目录
linux top命令源代码 (100个子文件)
ADVERTISEMENT 784B
sigconv.awk 1KB
m_sunos5.c 28KB
m_next32.c 28KB
m_decosf1.c 23KB
m_netbsd10.c 23KB
display.c 22KB
m_sunos4.c 22KB
m_freebsd20.c 21KB
top.c 21KB
m_ftx.c 21KB
m_sunos4mp.c 19KB
m_hpux7.c 19KB
m_ultrix4.c 19KB
m_hpux10.c 19KB
m_hpux9.c 19KB
m_hpux8.c 18KB
m_svr4.c 18KB
m_ncr3000.c 18KB
m_irix5.c 18KB
m_bsd43.c 18KB
m_osmp41a.c 17KB
m_dynix32.c 17KB
m_aix32.c 17KB
m_dynix.c 17KB
m_netbsd08.c 17KB
m_aux3.c 17KB
m_aix41.c 17KB
m_386bsd.c 17KB
m_bsd386.c 16KB
m_dcosx.c 16KB
m_mtxinu.c 16KB
m_umax.c 16KB
m_utek.c 16KB
m_sco.c 15KB
m_bsdos2.c 15KB
m_bsd44.c 15KB
m_bsd44a.c 15KB
m_svr42.c 15KB
m_convex.c 14KB
m_linux.c 14KB
m_dgux.c 12KB
commands.c 10KB
utils.c 10KB
screen.c 9KB
username.c 5KB
getopt.c 2KB
m_next40.c 1KB
m_sunos54.c 1KB
version.c 520B
prime.c 458B
Changes 24KB
Configure 13KB
DISCLAIMER 1KB
FAQ 10KB
getans 2KB
m_next_task.h 3KB
top.local.H 2KB
loadavg.h 1KB
machine.h 1KB
screen.h 991B
os.h 780B
top.h 721B
layout.h 621B
utils.h 522B
display.h 135B
boolean.h 125B
patchlevel.h 21B
INSTALL 7KB
install 928B
m-template 5KB
m_next32.man 4KB
m_sunos4.man 1KB
m_umax.man 1024B
m_dgux.man 977B
m_freebsd20.man 677B
m_aux3.man 567B
m_hpux9.man 463B
m_sco.man 433B
m_sunos5.man 366B
m_ncr3000.man 345B
m_svr4.man 328B
m_dynix.man 319B
m_convex.man 319B
m_ftx.man 301B
m_linux.man 221B
m_bsdos2.man 202B
m_386bsd.man 194B
m_aix41.man 181B
m_bsd44a.man 150B
m_bsd44.man 138B
m_netbsd08.man 129B
m_svr42.man 118B
m_dcosx.man 64B
metatop 1KB
Porting 7KB
README 7KB
top.X 10KB
Makefile.X 4KB
Make.desc.X 673B
共 100 条
- 1
uglboy
- 粉丝: 11
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页