/*
* 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
* SunOS 5.4
* SunOS 5.5
* SunOS 5.6
* SunOS 5.7
*
* Tested on a SPARCclassic with SunOS 5.1, using gcc-2.3.3, and
* SPARCsystem 600 with SunOS 5.2, using Sun C
*
* LIBS: -lelf -lkvm -lkstat
*
* CFLAGS: -DHAVE_GETOPT -DORDER -DHAVE_STRERROR
*
*
* 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.dik@sun.com>
* Tim Pugh <tpugh@oce.orst.edu>
*/
#define _KMEMUSER
#if (OSREV == 551)
#undef OSREV
#define OSREV 55
#endif
#define USE_NEW_PROC
#if defined(USE_NEW_PROC) && OSREV >= 56
#define _STRUCTURED_PROC 1
#define prpsinfo psinfo
#include <sys/procfs.h>
#define pr_fill pr_nlwp
/* These require an ANSI C compiler "Reisser cpp" doesn't like this */
#define pr_state pr_lwp.pr_state
#define pr_oldpri pr_lwp.pr_oldpri
#define pr_nice pr_lwp.pr_nice
#define pr_pri pr_lwp.pr_pri
#define pr_onpro pr_lwp.pr_onpro
#define ZOMBIE(p) ((p)->pr_nlwp == 0)
#define SIZE_K(p) ((p)->pr_size)
#define RSS_K(p) ((p)->pr_rssize)
#else
#undef USE_NEW_PROC
#define ZOMBIE(p) ((p)->pr_zomb)
#define SIZE_K(p) ((p)->pr_bysize/1024)
#define RSS_K(p) ((p)->pr_byrssize/1024)
#define pr_onpro pr_filler[5]
#endif
#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 <sys/swap.h>
#include <vm/anon.h>
#include <math.h>
#if OSREV >= 53
#define USE_KSTAT
#endif
#ifdef USE_KSTAT
#include <kstat.h>
/*
* Some kstats are fixed at 32 bits, these will be specified as ui32; some
* are "natural" size (32 bit on 32 bit Solaris, 64 on 64 bit Solaris
* we'll make those unsigned long)
* Older Solaris doesn't define KSTAT_DATA_UINT32, those are always 32 bit.
*/
# ifndef KSTAT_DATA_UINT32
# define ui32 ul
# endif
#endif
#ifdef SC_AINFO
#undef USE_ANONINFO /* Use swapctl() instead */
#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))
#if (OSREV >= 54)
/*
* 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_V 0
#define X_MPID 1
#define X_ANONINFO 2
#define X_MAXMEM 3
#define X_SWAPFS_MINFREE 4
#define X_FREEMEM 5
#define X_AVAILRMEM 6
#define X_AVENRUN 7
#define X_CPU 8
#define X_NPROC 9
#define X_NCPUS 10
static struct nlist nlst[] =
{
{"v"}, /* 0 */ /* replaced by dynamic allocation */
{"mpid"}, /* 1 */
#if OSREV >= 56
/* this structure really has some extra fields, but the first three match */
{"k_anoninfo"}, /* 2 */
#else
{"anoninfo"}, /* 2 */
#endif
{"maxmem"}, /* 3 */ /* use sysconf */
{"swapfs_minfree"}, /* 4 */ /* used only w/ USE_ANONINFO */
{"freemem"}, /* 5 */ /* available from kstat >= 2.5 */
{"availrmem"}, /* 6 */ /* available from kstat >= 2.5 */
{"avenrun"}, /* 7 */ /* available from kstat */
{"cpu"}, /* 8 */ /* available from kstat */
{"nproc"}, /* 9 */ /* available from kstat */
{"ncpus"}, /* 10 */ /* available from kstat */
{0}
};
static unsigned long avenrun_offset;
static unsigned long mpid_offset;
#ifdef USE_KSTAT
#define NO_NPROC
static kstat_ctl_t *kc = NULL;
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;
static void reallocproc(int n);
static int maxprocs;
/* 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
*/
#if OSREV >= 58
static char header[] =
" PID X LWP PRI NICE SIZE RES STATE TIME CPU COMMAND";
/* 0123456 -- field to fill in starts at header+6 */
#define UNAME_START 7
#define Proc_format \
"%6d %-8.8s %3d %3d %4d %5s %5s %-6s %6s %5.2f%% %s"
#else
static char header[] =
" PID X LWP PRI NICE SIZE RES STATE TIME CPU COMMAND";
/* 0123456 -- field to fill in starts at header+6 */
#define UNAME_START 6
#define Proc_format \
"%5d %-8.8s %3d %3d %4d %5s %5s %-6s %6s %5.2f%% %s"
#endif
/* process state names for the "STATE" column of the display */
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 in use, ", "K swap free", 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,
没有合适的资源?快使用搜索试试~ 我知道了~
top指令源代码
5星 · 超过95%的资源 需积分: 14 35 下载量 154 浏览量
2008-06-25
11:04:15
上传
评论
收藏 239KB GZ 举报
温馨提示
共114个文件
c:57个
man:30个
h:12个
top3.5beta13 源代码
资源推荐
资源详情
资源评论
收起资源包目录
top指令源代码 (114个子文件)
sigconv.awk 1KB
m_sunos5.c 36KB
m_svr5.c 34KB
m_svr42MP.c 33KB
m_next32.c 28KB
m_irixsgi.c 26KB
m_netbsd132.c 26KB
m_decosf1.c 25KB
m_freebsd.c 23KB
m_netbsd10.c 23KB
display.c 22KB
m_sunos4mp.c 22KB
m_sunos4.c 22KB
top.c 22KB
m_sco5.c 21KB
m_ultrix4.c 21KB
m_ftx.c 21KB
m_macosx.c 20KB
m_linux.c 20KB
m_hpux9.c 19KB
m_hpux7.c 19KB
m_irix62.c 19KB
m_mtxinu.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_aix43.c 17KB
m_netbsd08.c 17KB
m_aux3.c 17KB
m_aix41.c 17KB
m_hpux10.c 17KB
m_386bsd.c 17KB
m_bsd386.c 16KB
m_dcosx.c 16KB
m_umax.c 16KB
m_utek.c 16KB
m_sco.c 16KB
m_bsdos2.c 15KB
m_bsd44.c 15KB
m_bsd44a.c 15KB
m_svr42.c 15KB
m_convex.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_irix62_64.c 531B
version.c 520B
prime.c 477B
Changes 29KB
Configure 15KB
FAQ 11KB
getans 2KB
m_next_task.h 3KB
top.local.H 2KB
loadavg.h 1KB
machine.h 1KB
os.h 1005B
screen.h 991B
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_sunos5.man 2KB
m_sunos4mp.man 1KB
m_sunos4.man 1KB
m_macosx.man 1KB
m_umax.man 1024B
m_dgux.man 977B
m_hpux10.man 797B
m_freebsd.man 766B
m_decosf1.man 745B
m_aux3.man 567B
m_hpux9.man 463B
m_sco.man 433B
m_linux.man 355B
m_ncr3000.man 345B
m_svr4.man 328B
m_dynix.man 319B
m_convex.man 319B
m_sco5.man 315B
m_ftx.man 301B
m_bsdos2.man 202B
m_svr42MP.man 199B
m_386bsd.man 194B
共 114 条
- 1
- 2
资源评论
- lemoner20032014-03-20很不错,这个代码相当于一个小项目。
- JuneIdiot2013-12-18很好,适合学习系统状态获取.
- cxjchen2013-04-23代码可以看,很详细,主要是看一下原理
「已注销」
- 粉丝: 3
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功