2 * top - a top users display for Unix
4 * THIS IS A TEMPLATE FILE FOR A MACHINE DEPENDENT (m_...c) FILE
6 * SYNOPSIS: one line description of machine this module works with
9 * Detailed description of this machine dependent module.
10 * It can be multiple lines, but a blank comment line (one with only an
11 * asterisk) is considered to end it. Place here a complete list of
12 * the machines and OS versions that this module works on.
14 * LIBS: list of special libraries to include at link step (REMOVE THIS LINE IF NOT NEEDED)
16 * AUTHOR: your name and <your@internet.address>
24 * These definitions control the format of the per-process area
27 static char header[] =
28 " PID X PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND";
29 /* 0123456 -- field to fill in starts at header+6 */
33 "%5d %-8.8s %3d %4d%6dK %4dK %-5s%4d:%02d %5.2f%% %5.2f%% %.14s"
35 /* these are for detailing the process states */
37 int process_states[?];
38 char *procstatenames[] = {
39 "", " sleeping, ", " ABANDONED, ", " running, ", " starting, ",
40 " zombie, ", " stopped, ",
44 /* these are for detailing the cpu states */
47 char *cpustatenames[] = {
48 "user", "nice", "system", "idle",
52 /* these are for detailing the memory statistics */
55 char *memorynames[] = {
56 "K available, ", "K in use, ", "K free, ", "K locked", NULL
59 /* useful externals */
61 extern char *sys_errlist[];
69 struct statics *statics;
75 char *format_header(uname_field)
77 register char *uname_field;
82 ptr = header + UNAME_START;
83 while (*uname_field != '\0')
85 *ptr++ = *uname_field++;
93 struct system_info *si;
98 static struct handle handle;
100 caddr_t get_process_info(si, sel, compare)
102 struct system_info *si;
103 struct process_select *sel;
107 return((caddr_t)&handle);
110 char fmt[128]; /* static area where result is built */
112 /* define what weighted cpu is. */
113 #define weighted_cpu(pct, pp) ((pp)->p_time == 0 ? 0.0 : \
114 ((pct) / (1.0 - exp((pp)->p_time * logcpu))))
116 char *format_next_process(handle, get_userid)
119 char *(*get_userid)();
126 * getkval(offset, ptr, size, refstr) - get a value out of the kernel.
127 * "offset" is the byte offset into the kernel for the desired value,
128 * "ptr" points to a buffer into which the value is retrieved,
129 * "size" is the size of the buffer (and the object to retrieve),
130 * "refstr" is a reference string used when printing error meessages,
131 * if "refstr" starts with a '!', then a failure on read will not
132 * be fatal (this may seem like a silly way to do things, but I
133 * really didn't want the overhead of another argument).
137 getkval(offset, ptr, size, refstr)
139 unsigned long offset;
145 if (kvm_read(kd, offset, ptr, size) != size)
153 fprintf(stderr, "top: kvm_read for %s: %s\n",
154 refstr, sys_errlist[errno]);
161 /* comparison routine for qsort */
162 /* NOTE: this is specific to the BSD proc structure, but it should
163 give you a good place to start. */
166 * proc_compare - comparison function for "qsort"
167 * Compares the resource consumption of two processes using five
168 * distinct keys. The keys (in descending order of importance) are:
169 * percent cpu, cpu ticks, state, resident set size, total virtual
170 * memory usage. The process states are ordered as follows (from least
171 * to most important): WAIT, zombie, sleep, stop, start, run. The
172 * array declaration below maps a process state index into a number
173 * that reflects this ordering.
176 static unsigned char sorted_state[] =
180 1, /* ABANDONED (WAIT) */
187 proc_compare(pp1, pp2)
193 register struct proc *p1;
194 register struct proc *p2;
196 register pctcpu lresult;
198 /* remove one level of indirection */
202 /* compare percent cpu (pctcpu) */
203 if ((lresult = p2->p_pctcpu - p1->p_pctcpu) == 0)
205 /* use cpticks to break the tie */
206 if ((result = p2->p_cpticks - p1->p_cpticks) == 0)
208 /* use process state to break the tie */
209 if ((result = sorted_state[p2->p_stat] -
210 sorted_state[p1->p_stat]) == 0)
212 /* use priority to break the tie */
213 if ((result = p2->p_pri - p1->p_pri) == 0)
215 /* use resident set size (rssize) to break the tie */
216 if ((result = p2->p_rssize - p1->p_rssize) == 0)
218 /* use total memory to break the tie */
219 result = PROCSIZE(p2) - PROCSIZE(p1);
227 result = lresult < 0 ? -1 : 1;
238 /* returns uid of owner of process pid */