m_linux.c revision 1.1 1 1.1 christos /*
2 1.1 christos * Copyright (c) 1984 through 2008, William LeFebvre
3 1.1 christos * All rights reserved.
4 1.1 christos *
5 1.1 christos * Redistribution and use in source and binary forms, with or without
6 1.1 christos * modification, are permitted provided that the following conditions are met:
7 1.1 christos *
8 1.1 christos * * Redistributions of source code must retain the above copyright
9 1.1 christos * notice, this list of conditions and the following disclaimer.
10 1.1 christos *
11 1.1 christos * * Redistributions in binary form must reproduce the above
12 1.1 christos * copyright notice, this list of conditions and the following disclaimer
13 1.1 christos * in the documentation and/or other materials provided with the
14 1.1 christos * distribution.
15 1.1 christos *
16 1.1 christos * * Neither the name of William LeFebvre nor the names of other
17 1.1 christos * contributors may be used to endorse or promote products derived from
18 1.1 christos * this software without specific prior written permission.
19 1.1 christos *
20 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 1.1 christos * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 1.1 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 1.1 christos * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 1.1 christos * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 christos */
32 1.1 christos
33 1.1 christos /*
34 1.1 christos * top - a top users display for Unix
35 1.1 christos *
36 1.1 christos * SYNOPSIS: Linux 1.2.x, 1.3.x, 2.x, using the /proc filesystem
37 1.1 christos *
38 1.1 christos * DESCRIPTION:
39 1.1 christos * This is the machine-dependent module for Linux 1.2.x, 1.3.x or 2.x.
40 1.1 christos *
41 1.1 christos * LIBS:
42 1.1 christos *
43 1.1 christos * CFLAGS: -DHAVE_GETOPT -DHAVE_STRERROR -DORDER
44 1.1 christos *
45 1.1 christos * TERMCAP: -lcurses
46 1.1 christos *
47 1.1 christos * AUTHOR: Richard Henderson <rth (at) tamu.edu>
48 1.1 christos * Order support added by Alexey Klimkin <kad (at) klon.tme.mcst.ru>
49 1.1 christos * Ported to 2.4 by William LeFebvre
50 1.1 christos * Additions for 2.6 by William LeFebvre
51 1.1 christos */
52 1.1 christos
53 1.1 christos #include "config.h"
54 1.1 christos
55 1.1 christos #include <sys/types.h>
56 1.1 christos #include <time.h>
57 1.1 christos #include <stdio.h>
58 1.1 christos #include <fcntl.h>
59 1.1 christos #include <unistd.h>
60 1.1 christos #include <stdlib.h>
61 1.1 christos #include <errno.h>
62 1.1 christos #include <dirent.h>
63 1.1 christos #include <string.h>
64 1.1 christos #include <math.h>
65 1.1 christos #include <ctype.h>
66 1.1 christos #include <sys/time.h>
67 1.1 christos #include <sys/stat.h>
68 1.1 christos #include <sys/vfs.h>
69 1.1 christos
70 1.1 christos #include <sys/param.h> /* for HZ */
71 1.1 christos #include <asm/page.h> /* for PAGE_SHIFT */
72 1.1 christos
73 1.1 christos #if 0
74 1.1 christos #include <linux/proc_fs.h> /* for PROC_SUPER_MAGIC */
75 1.1 christos #else
76 1.1 christos #define PROC_SUPER_MAGIC 0x9fa0
77 1.1 christos #endif
78 1.1 christos
79 1.1 christos #include "top.h"
80 1.1 christos #include "hash.h"
81 1.1 christos #include "machine.h"
82 1.1 christos #include "utils.h"
83 1.1 christos #include "username.h"
84 1.1 christos
85 1.1 christos #define PROCFS "/proc"
86 1.1 christos extern char *myname;
87 1.1 christos
88 1.1 christos /*=PROCESS INFORMATION==================================================*/
89 1.1 christos
90 1.1 christos struct top_proc
91 1.1 christos {
92 1.1 christos pid_t pid;
93 1.1 christos uid_t uid;
94 1.1 christos char *name;
95 1.1 christos int pri, nice, threads;
96 1.1 christos unsigned long size, rss, shared; /* in k */
97 1.1 christos int state;
98 1.1 christos unsigned long time;
99 1.1 christos unsigned long start_time;
100 1.1 christos double pcpu;
101 1.1 christos struct top_proc *next;
102 1.1 christos };
103 1.1 christos
104 1.1 christos
105 1.1 christos /*=STATE IDENT STRINGS==================================================*/
106 1.1 christos
107 1.1 christos #define NPROCSTATES 7
108 1.1 christos static char *state_abbrev[NPROCSTATES+1] =
109 1.1 christos {
110 1.1 christos "", "run", "sleep", "disk", "zomb", "stop", "swap",
111 1.1 christos NULL
112 1.1 christos };
113 1.1 christos
114 1.1 christos static char *procstatenames[NPROCSTATES+1] =
115 1.1 christos {
116 1.1 christos "", " running, ", " sleeping, ", " uninterruptable, ",
117 1.1 christos " zombie, ", " stopped, ", " swapping, ",
118 1.1 christos NULL
119 1.1 christos };
120 1.1 christos
121 1.1 christos #define NCPUSTATES 5
122 1.1 christos static char *cpustatenames[NCPUSTATES+1] =
123 1.1 christos {
124 1.1 christos "user", "nice", "system", "idle", "iowait",
125 1.1 christos NULL
126 1.1 christos };
127 1.1 christos static int show_iowait = 0;
128 1.1 christos
129 1.1 christos #define KERNELCTXT 0
130 1.1 christos #define KERNELFLT 1
131 1.1 christos #define KERNELINTR 2
132 1.1 christos #define KERNELNEWPROC 3
133 1.1 christos #define NKERNELSTATS 4
134 1.1 christos static char *kernelnames[NKERNELSTATS+1] =
135 1.1 christos {
136 1.1 christos " ctxsw, ", " flt, ", " intr, ", " newproc",
137 1.1 christos NULL
138 1.1 christos };
139 1.1 christos
140 1.1 christos #define MEMUSED 0
141 1.1 christos #define MEMFREE 1
142 1.1 christos #define MEMSHARED 2
143 1.1 christos #define MEMBUFFERS 3
144 1.1 christos #define MEMCACHED 4
145 1.1 christos #define NMEMSTATS 5
146 1.1 christos static char *memorynames[NMEMSTATS+1] =
147 1.1 christos {
148 1.1 christos "K used, ", "K free, ", "K shared, ", "K buffers, ", "K cached",
149 1.1 christos NULL
150 1.1 christos };
151 1.1 christos
152 1.1 christos #define SWAPUSED 0
153 1.1 christos #define SWAPFREE 1
154 1.1 christos #define SWAPCACHED 2
155 1.1 christos #define NSWAPSTATS 3
156 1.1 christos static char *swapnames[NSWAPSTATS+1] =
157 1.1 christos {
158 1.1 christos "K used, ", "K free, ", "K cached",
159 1.1 christos NULL
160 1.1 christos };
161 1.1 christos
162 1.1 christos static char fmt_header[] =
163 1.1 christos " PID X THR PRI NICE SIZE RES STATE TIME CPU COMMAND";
164 1.1 christos
165 1.1 christos static char proc_header_thr[] =
166 1.1 christos " PID %-9s THR PRI NICE SIZE RES SHR STATE TIME CPU COMMAND";
167 1.1 christos
168 1.1 christos static char proc_header_nothr[] =
169 1.1 christos " PID %-9s PRI NICE SIZE RES SHR STATE TIME CPU COMMAND";
170 1.1 christos
171 1.1 christos /* these are names given to allowed sorting orders -- first is default */
172 1.1 christos char *ordernames[] =
173 1.1 christos {"cpu", "size", "res", "time", "command", NULL};
174 1.1 christos
175 1.1 christos /* forward definitions for comparison functions */
176 1.1 christos int compare_cpu();
177 1.1 christos int compare_size();
178 1.1 christos int compare_res();
179 1.1 christos int compare_time();
180 1.1 christos int compare_cmd();
181 1.1 christos
182 1.1 christos int (*proc_compares[])() = {
183 1.1 christos compare_cpu,
184 1.1 christos compare_size,
185 1.1 christos compare_res,
186 1.1 christos compare_time,
187 1.1 christos compare_cmd,
188 1.1 christos NULL };
189 1.1 christos
190 1.1 christos /*=SYSTEM STATE INFO====================================================*/
191 1.1 christos
192 1.1 christos /* these are for calculating cpu state percentages */
193 1.1 christos
194 1.1 christos static long cp_time[NCPUSTATES];
195 1.1 christos static long cp_old[NCPUSTATES];
196 1.1 christos static long cp_diff[NCPUSTATES];
197 1.1 christos
198 1.1 christos /* for calculating the exponential average */
199 1.1 christos
200 1.1 christos static struct timeval lasttime = { 0, 0 };
201 1.1 christos static struct timeval timediff = { 0, 0 };
202 1.1 christos static long elapsed_msecs;
203 1.1 christos
204 1.1 christos /* these are for keeping track of processes and tasks */
205 1.1 christos
206 1.1 christos #define HASH_SIZE (1003)
207 1.1 christos #define INITIAL_ACTIVE_SIZE (256)
208 1.1 christos #define PROCBLOCK_SIZE (32)
209 1.1 christos static hash_table *ptable;
210 1.1 christos static hash_table *tasktable;
211 1.1 christos static struct top_proc **pactive;
212 1.1 christos static struct top_proc **nextactive;
213 1.1 christos static unsigned int activesize = 0;
214 1.1 christos static time_t boottime = -1;
215 1.1 christos static int have_task = 0;
216 1.1 christos
217 1.1 christos /* these are counters that need to be track */
218 1.1 christos static unsigned long last_ctxt = 0;
219 1.1 christos static unsigned long last_intr = 0;
220 1.1 christos static unsigned long last_newproc = 0;
221 1.1 christos static unsigned long last_flt = 0;
222 1.1 christos
223 1.1 christos /* these are for passing data back to the machine independant portion */
224 1.1 christos
225 1.1 christos static int cpu_states[NCPUSTATES];
226 1.1 christos static int process_states[NPROCSTATES];
227 1.1 christos static int kernel_stats[NKERNELSTATS];
228 1.1 christos static long memory_stats[NMEMSTATS];
229 1.1 christos static long swap_stats[NSWAPSTATS];
230 1.1 christos
231 1.1 christos /* useful macros */
232 1.1 christos #define bytetok(x) (((x) + 512) >> 10)
233 1.1 christos #define pagetok(x) ((x) << (PAGE_SHIFT - 10))
234 1.1 christos #define HASH(x) (((x) * 1686629713U) % HASH_SIZE)
235 1.1 christos
236 1.1 christos /* calculate a per-second rate using milliseconds */
237 1.1 christos #define per_second(n, msec) (((n) * 1000) / (msec))
238 1.1 christos
239 1.1 christos /*======================================================================*/
240 1.1 christos
241 1.1 christos static inline char *
242 1.1 christos skip_ws(const char *p)
243 1.1 christos {
244 1.1 christos while (isspace(*p)) p++;
245 1.1 christos return (char *)p;
246 1.1 christos }
247 1.1 christos
248 1.1 christos static inline char *
249 1.1 christos skip_token(const char *p)
250 1.1 christos {
251 1.1 christos while (isspace(*p)) p++;
252 1.1 christos while (*p && !isspace(*p)) p++;
253 1.1 christos return (char *)p;
254 1.1 christos }
255 1.1 christos
256 1.1 christos static void
257 1.1 christos xfrm_cmdline(char *p, int len)
258 1.1 christos {
259 1.1 christos while (--len > 0)
260 1.1 christos {
261 1.1 christos if (*p == '\0')
262 1.1 christos {
263 1.1 christos *p = ' ';
264 1.1 christos }
265 1.1 christos p++;
266 1.1 christos }
267 1.1 christos }
268 1.1 christos
269 1.1 christos static void
270 1.1 christos update_procname(struct top_proc *proc, char *cmd)
271 1.1 christos
272 1.1 christos {
273 1.1 christos printable(cmd);
274 1.1 christos
275 1.1 christos if (proc->name == NULL)
276 1.1 christos {
277 1.1 christos proc->name = strdup(cmd);
278 1.1 christos }
279 1.1 christos else if (strcmp(proc->name, cmd) != 0)
280 1.1 christos {
281 1.1 christos free(proc->name);
282 1.1 christos proc->name = strdup(cmd);
283 1.1 christos }
284 1.1 christos }
285 1.1 christos
286 1.1 christos /*
287 1.1 christos * Process structures are allocated and freed as needed. Here we
288 1.1 christos * keep big pools of them, adding more pool as needed. When a
289 1.1 christos * top_proc structure is freed, it is added to a freelist and reused.
290 1.1 christos */
291 1.1 christos
292 1.1 christos static struct top_proc *freelist = NULL;
293 1.1 christos static struct top_proc *procblock = NULL;
294 1.1 christos static struct top_proc *procmax = NULL;
295 1.1 christos
296 1.1 christos static struct top_proc *
297 1.1 christos new_proc()
298 1.1 christos {
299 1.1 christos struct top_proc *p;
300 1.1 christos
301 1.1 christos if (freelist)
302 1.1 christos {
303 1.1 christos p = freelist;
304 1.1 christos freelist = freelist->next;
305 1.1 christos }
306 1.1 christos else if (procblock)
307 1.1 christos {
308 1.1 christos p = procblock;
309 1.1 christos if (++procblock >= procmax)
310 1.1 christos {
311 1.1 christos procblock = NULL;
312 1.1 christos }
313 1.1 christos }
314 1.1 christos else
315 1.1 christos {
316 1.1 christos p = procblock = (struct top_proc *)calloc(PROCBLOCK_SIZE,
317 1.1 christos sizeof(struct top_proc));
318 1.1 christos procmax = procblock++ + PROCBLOCK_SIZE;
319 1.1 christos }
320 1.1 christos
321 1.1 christos /* initialization */
322 1.1 christos if (p->name != NULL)
323 1.1 christos {
324 1.1 christos free(p->name);
325 1.1 christos p->name = NULL;
326 1.1 christos }
327 1.1 christos
328 1.1 christos return p;
329 1.1 christos }
330 1.1 christos
331 1.1 christos static void
332 1.1 christos free_proc(struct top_proc *proc)
333 1.1 christos {
334 1.1 christos proc->next = freelist;
335 1.1 christos freelist = proc;
336 1.1 christos }
337 1.1 christos
338 1.1 christos
339 1.1 christos int
340 1.1 christos machine_init(struct statics *statics)
341 1.1 christos
342 1.1 christos {
343 1.1 christos /* make sure the proc filesystem is mounted */
344 1.1 christos {
345 1.1 christos struct statfs sb;
346 1.1 christos if (statfs(PROCFS, &sb) < 0 || sb.f_type != PROC_SUPER_MAGIC)
347 1.1 christos {
348 1.1 christos fprintf(stderr, "%s: proc filesystem not mounted on " PROCFS "\n",
349 1.1 christos myname);
350 1.1 christos return -1;
351 1.1 christos }
352 1.1 christos }
353 1.1 christos
354 1.1 christos /* chdir to the proc filesystem to make things easier */
355 1.1 christos chdir(PROCFS);
356 1.1 christos
357 1.1 christos /* a few preliminary checks */
358 1.1 christos {
359 1.1 christos int fd;
360 1.1 christos char buff[128];
361 1.1 christos char *p;
362 1.1 christos int cnt = 0;
363 1.1 christos unsigned long uptime;
364 1.1 christos struct timeval tv;
365 1.1 christos struct stat st;
366 1.1 christos
367 1.1 christos /* get a boottime */
368 1.1 christos if ((fd = open("uptime", 0)) != -1)
369 1.1 christos {
370 1.1 christos if (read(fd, buff, sizeof(buff)) > 0)
371 1.1 christos {
372 1.1 christos uptime = strtoul(buff, &p, 10);
373 1.1 christos gettimeofday(&tv, 0);
374 1.1 christos boottime = tv.tv_sec - uptime;
375 1.1 christos }
376 1.1 christos close(fd);
377 1.1 christos }
378 1.1 christos
379 1.1 christos /* see how many states we get from stat */
380 1.1 christos if ((fd = open("stat", 0)) != -1)
381 1.1 christos {
382 1.1 christos if (read(fd, buff, sizeof(buff)) > 0)
383 1.1 christos {
384 1.1 christos if ((p = strchr(buff, '\n')) != NULL)
385 1.1 christos {
386 1.1 christos *p = '\0';
387 1.1 christos p = buff;
388 1.1 christos while (*p != '\0')
389 1.1 christos {
390 1.1 christos if (*p++ == ' ')
391 1.1 christos {
392 1.1 christos cnt++;
393 1.1 christos }
394 1.1 christos }
395 1.1 christos }
396 1.1 christos }
397 1.1 christos
398 1.1 christos close(fd);
399 1.1 christos }
400 1.1 christos if (cnt > 5)
401 1.1 christos {
402 1.1 christos /* we have iowait */
403 1.1 christos show_iowait = 1;
404 1.1 christos }
405 1.1 christos
406 1.1 christos /* see if we have task subdirs */
407 1.1 christos if (stat("self/task", &st) != -1 && S_ISDIR(st.st_mode))
408 1.1 christos {
409 1.1 christos dprintf("we have task directories\n");
410 1.1 christos have_task = 1;
411 1.1 christos }
412 1.1 christos }
413 1.1 christos
414 1.1 christos /* if we aren't showing iowait, then we have to tweak cpustatenames */
415 1.1 christos if (!show_iowait)
416 1.1 christos {
417 1.1 christos cpustatenames[4] = NULL;
418 1.1 christos }
419 1.1 christos
420 1.1 christos /* fill in the statics information */
421 1.1 christos statics->procstate_names = procstatenames;
422 1.1 christos statics->cpustate_names = cpustatenames;
423 1.1 christos statics->kernel_names = kernelnames;
424 1.1 christos statics->memory_names = memorynames;
425 1.1 christos statics->swap_names = swapnames;
426 1.1 christos statics->order_names = ordernames;
427 1.1 christos statics->boottime = boottime;
428 1.1 christos statics->flags.fullcmds = 1;
429 1.1 christos statics->flags.warmup = 1;
430 1.1 christos statics->flags.threads = 1;
431 1.1 christos
432 1.1 christos /* allocate needed space */
433 1.1 christos pactive = (struct top_proc **)malloc(sizeof(struct top_proc *) * INITIAL_ACTIVE_SIZE);
434 1.1 christos activesize = INITIAL_ACTIVE_SIZE;
435 1.1 christos
436 1.1 christos /* create process and task hashes */
437 1.1 christos ptable = hash_create(HASH_SIZE);
438 1.1 christos tasktable = hash_create(HASH_SIZE);
439 1.1 christos
440 1.1 christos /* all done! */
441 1.1 christos return 0;
442 1.1 christos }
443 1.1 christos
444 1.1 christos
445 1.1 christos void
446 1.1 christos get_system_info(struct system_info *info)
447 1.1 christos
448 1.1 christos {
449 1.1 christos char buffer[4096+1];
450 1.1 christos int fd, len;
451 1.1 christos char *p;
452 1.1 christos struct timeval thistime;
453 1.1 christos unsigned long intr = 0;
454 1.1 christos unsigned long ctxt = 0;
455 1.1 christos unsigned long newproc = 0;
456 1.1 christos unsigned long flt = 0;
457 1.1 christos
458 1.1 christos /* timestamp and time difference */
459 1.1 christos gettimeofday(&thistime, 0);
460 1.1 christos timersub(&thistime, &lasttime, &timediff);
461 1.1 christos elapsed_msecs = timediff.tv_sec * 1000 + timediff.tv_usec / 1000;
462 1.1 christos lasttime = thistime;
463 1.1 christos
464 1.1 christos /* get load averages */
465 1.1 christos if ((fd = open("loadavg", O_RDONLY)) != -1)
466 1.1 christos {
467 1.1 christos if ((len = read(fd, buffer, sizeof(buffer)-1)) > 0)
468 1.1 christos {
469 1.1 christos buffer[len] = '\0';
470 1.1 christos info->load_avg[0] = strtod(buffer, &p);
471 1.1 christos info->load_avg[1] = strtod(p, &p);
472 1.1 christos info->load_avg[2] = strtod(p, &p);
473 1.1 christos p = skip_token(p); /* skip running/tasks */
474 1.1 christos p = skip_ws(p);
475 1.1 christos if (*p)
476 1.1 christos {
477 1.1 christos info->last_pid = atoi(p);
478 1.1 christos }
479 1.1 christos else
480 1.1 christos {
481 1.1 christos info->last_pid = -1;
482 1.1 christos }
483 1.1 christos }
484 1.1 christos close(fd);
485 1.1 christos }
486 1.1 christos
487 1.1 christos /* get the cpu time info */
488 1.1 christos if ((fd = open("stat", O_RDONLY)) != -1)
489 1.1 christos {
490 1.1 christos if ((len = read(fd, buffer, sizeof(buffer)-1)) > 0)
491 1.1 christos {
492 1.1 christos buffer[len] = '\0';
493 1.1 christos p = skip_token(buffer); /* "cpu" */
494 1.1 christos cp_time[0] = strtoul(p, &p, 0);
495 1.1 christos cp_time[1] = strtoul(p, &p, 0);
496 1.1 christos cp_time[2] = strtoul(p, &p, 0);
497 1.1 christos cp_time[3] = strtoul(p, &p, 0);
498 1.1 christos if (show_iowait)
499 1.1 christos {
500 1.1 christos cp_time[4] = strtoul(p, &p, 0);
501 1.1 christos }
502 1.1 christos
503 1.1 christos /* convert cp_time counts to percentages */
504 1.1 christos percentages(NCPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
505 1.1 christos
506 1.1 christos /* get the rest of it */
507 1.1 christos p = strchr(p, '\n');
508 1.1 christos while (p != NULL)
509 1.1 christos {
510 1.1 christos p++;
511 1.1 christos if (strncmp(p, "intr ", 5) == 0)
512 1.1 christos {
513 1.1 christos p = skip_token(p);
514 1.1 christos intr = strtoul(p, &p, 10);
515 1.1 christos }
516 1.1 christos else if (strncmp(p, "ctxt ", 5) == 0)
517 1.1 christos {
518 1.1 christos p = skip_token(p);
519 1.1 christos ctxt = strtoul(p, &p, 10);
520 1.1 christos }
521 1.1 christos else if (strncmp(p, "processes ", 10) == 0)
522 1.1 christos {
523 1.1 christos p = skip_token(p);
524 1.1 christos newproc = strtoul(p, &p, 10);
525 1.1 christos }
526 1.1 christos
527 1.1 christos p = strchr(p, '\n');
528 1.1 christos }
529 1.1 christos
530 1.1 christos kernel_stats[KERNELINTR] = per_second(intr - last_intr, elapsed_msecs);
531 1.1 christos kernel_stats[KERNELCTXT] = per_second(ctxt - last_ctxt, elapsed_msecs);
532 1.1 christos kernel_stats[KERNELNEWPROC] = per_second(newproc - last_newproc, elapsed_msecs);
533 1.1 christos last_intr = intr;
534 1.1 christos last_ctxt = ctxt;
535 1.1 christos last_newproc = newproc;
536 1.1 christos }
537 1.1 christos close(fd);
538 1.1 christos }
539 1.1 christos
540 1.1 christos /* get system wide memory usage */
541 1.1 christos if ((fd = open("meminfo", O_RDONLY)) != -1)
542 1.1 christos {
543 1.1 christos char *p;
544 1.1 christos int mem = 0;
545 1.1 christos int swap = 0;
546 1.1 christos unsigned long memtotal = 0;
547 1.1 christos unsigned long memfree = 0;
548 1.1 christos unsigned long swaptotal = 0;
549 1.1 christos
550 1.1 christos if ((len = read(fd, buffer, sizeof(buffer)-1)) > 0)
551 1.1 christos {
552 1.1 christos buffer[len] = '\0';
553 1.1 christos p = buffer-1;
554 1.1 christos
555 1.1 christos /* iterate thru the lines */
556 1.1 christos while (p != NULL)
557 1.1 christos {
558 1.1 christos p++;
559 1.1 christos if (p[0] == ' ' || p[0] == '\t')
560 1.1 christos {
561 1.1 christos /* skip */
562 1.1 christos }
563 1.1 christos else if (strncmp(p, "Mem:", 4) == 0)
564 1.1 christos {
565 1.1 christos p = skip_token(p); /* "Mem:" */
566 1.1 christos p = skip_token(p); /* total memory */
567 1.1 christos memory_stats[MEMUSED] = strtoul(p, &p, 10);
568 1.1 christos memory_stats[MEMFREE] = strtoul(p, &p, 10);
569 1.1 christos memory_stats[MEMSHARED] = strtoul(p, &p, 10);
570 1.1 christos memory_stats[MEMBUFFERS] = strtoul(p, &p, 10);
571 1.1 christos memory_stats[MEMCACHED] = strtoul(p, &p, 10);
572 1.1 christos memory_stats[MEMUSED] = bytetok(memory_stats[MEMUSED]);
573 1.1 christos memory_stats[MEMFREE] = bytetok(memory_stats[MEMFREE]);
574 1.1 christos memory_stats[MEMSHARED] = bytetok(memory_stats[MEMSHARED]);
575 1.1 christos memory_stats[MEMBUFFERS] = bytetok(memory_stats[MEMBUFFERS]);
576 1.1 christos memory_stats[MEMCACHED] = bytetok(memory_stats[MEMCACHED]);
577 1.1 christos mem = 1;
578 1.1 christos }
579 1.1 christos else if (strncmp(p, "Swap:", 5) == 0)
580 1.1 christos {
581 1.1 christos p = skip_token(p); /* "Swap:" */
582 1.1 christos p = skip_token(p); /* total swap */
583 1.1 christos swap_stats[SWAPUSED] = strtoul(p, &p, 10);
584 1.1 christos swap_stats[SWAPFREE] = strtoul(p, &p, 10);
585 1.1 christos swap_stats[SWAPUSED] = bytetok(swap_stats[SWAPUSED]);
586 1.1 christos swap_stats[SWAPFREE] = bytetok(swap_stats[SWAPFREE]);
587 1.1 christos swap = 1;
588 1.1 christos }
589 1.1 christos else if (!mem && strncmp(p, "MemTotal:", 9) == 0)
590 1.1 christos {
591 1.1 christos p = skip_token(p);
592 1.1 christos memtotal = strtoul(p, &p, 10);
593 1.1 christos }
594 1.1 christos else if (!mem && memtotal > 0 && strncmp(p, "MemFree:", 8) == 0)
595 1.1 christos {
596 1.1 christos p = skip_token(p);
597 1.1 christos memfree = strtoul(p, &p, 10);
598 1.1 christos memory_stats[MEMUSED] = memtotal - memfree;
599 1.1 christos memory_stats[MEMFREE] = memfree;
600 1.1 christos }
601 1.1 christos else if (!mem && strncmp(p, "MemShared:", 10) == 0)
602 1.1 christos {
603 1.1 christos p = skip_token(p);
604 1.1 christos memory_stats[MEMSHARED] = strtoul(p, &p, 10);
605 1.1 christos }
606 1.1 christos else if (!mem && strncmp(p, "Buffers:", 8) == 0)
607 1.1 christos {
608 1.1 christos p = skip_token(p);
609 1.1 christos memory_stats[MEMBUFFERS] = strtoul(p, &p, 10);
610 1.1 christos }
611 1.1 christos else if (!mem && strncmp(p, "Cached:", 7) == 0)
612 1.1 christos {
613 1.1 christos p = skip_token(p);
614 1.1 christos memory_stats[MEMCACHED] = strtoul(p, &p, 10);
615 1.1 christos }
616 1.1 christos else if (!swap && strncmp(p, "SwapTotal:", 10) == 0)
617 1.1 christos {
618 1.1 christos p = skip_token(p);
619 1.1 christos swaptotal = strtoul(p, &p, 10);
620 1.1 christos }
621 1.1 christos else if (!swap && swaptotal > 0 && strncmp(p, "SwapFree:", 9) == 0)
622 1.1 christos {
623 1.1 christos p = skip_token(p);
624 1.1 christos memfree = strtoul(p, &p, 10);
625 1.1 christos swap_stats[SWAPUSED] = swaptotal - memfree;
626 1.1 christos swap_stats[SWAPFREE] = memfree;
627 1.1 christos }
628 1.1 christos else if (!mem && strncmp(p, "SwapCached:", 11) == 0)
629 1.1 christos {
630 1.1 christos p = skip_token(p);
631 1.1 christos swap_stats[SWAPCACHED] = strtoul(p, &p, 10);
632 1.1 christos }
633 1.1 christos
634 1.1 christos /* move to the next line */
635 1.1 christos p = strchr(p, '\n');
636 1.1 christos }
637 1.1 christos }
638 1.1 christos close(fd);
639 1.1 christos }
640 1.1 christos
641 1.1 christos /* get vm related stuff */
642 1.1 christos if ((fd = open("vmstat", O_RDONLY)) != -1)
643 1.1 christos {
644 1.1 christos char *p;
645 1.1 christos
646 1.1 christos if ((len = read(fd, buffer, sizeof(buffer)-1)) > 0)
647 1.1 christos {
648 1.1 christos buffer[len] = '\0';
649 1.1 christos p = buffer;
650 1.1 christos
651 1.1 christos /* iterate thru the lines */
652 1.1 christos while (p != NULL)
653 1.1 christos {
654 1.1 christos if (strncmp(p, "pgmajfault ", 11) == 0)
655 1.1 christos {
656 1.1 christos p = skip_token(p);
657 1.1 christos flt = strtoul(p, &p, 10);
658 1.1 christos kernel_stats[KERNELFLT] = per_second(flt - last_flt, elapsed_msecs);
659 1.1 christos last_flt = flt;
660 1.1 christos break;
661 1.1 christos }
662 1.1 christos
663 1.1 christos /* move to the next line */
664 1.1 christos p = strchr(p, '\n');
665 1.1 christos p++;
666 1.1 christos }
667 1.1 christos }
668 1.1 christos close(fd);
669 1.1 christos }
670 1.1 christos
671 1.1 christos /* set arrays and strings */
672 1.1 christos info->cpustates = cpu_states;
673 1.1 christos info->memory = memory_stats;
674 1.1 christos info->swap = swap_stats;
675 1.1 christos info->kernel = kernel_stats;
676 1.1 christos }
677 1.1 christos
678 1.1 christos static void
679 1.1 christos read_one_proc_stat(pid_t pid, pid_t taskpid, struct top_proc *proc, struct process_select *sel)
680 1.1 christos {
681 1.1 christos char buffer[4096], *p, *q;
682 1.1 christos int fd, len;
683 1.1 christos int fullcmd;
684 1.1 christos
685 1.1 christos dprintf("reading proc %d - %d\n", pid, taskpid);
686 1.1 christos
687 1.1 christos /* if anything goes wrong, we return with proc->state == 0 */
688 1.1 christos proc->state = 0;
689 1.1 christos
690 1.1 christos /* full cmd handling */
691 1.1 christos fullcmd = sel->fullcmd;
692 1.1 christos if (fullcmd)
693 1.1 christos {
694 1.1 christos if (taskpid == -1)
695 1.1 christos {
696 1.1 christos sprintf(buffer, "%d/cmdline", pid);
697 1.1 christos }
698 1.1 christos else
699 1.1 christos {
700 1.1 christos sprintf(buffer, "%d/task/%d/cmdline", pid, taskpid);
701 1.1 christos }
702 1.1 christos if ((fd = open(buffer, O_RDONLY)) != -1)
703 1.1 christos {
704 1.1 christos /* read command line data */
705 1.1 christos /* (theres no sense in reading more than we can fit) */
706 1.1 christos if ((len = read(fd, buffer, MAX_COLS)) > 1)
707 1.1 christos {
708 1.1 christos buffer[len] = '\0';
709 1.1 christos xfrm_cmdline(buffer, len);
710 1.1 christos update_procname(proc, buffer);
711 1.1 christos }
712 1.1 christos else
713 1.1 christos {
714 1.1 christos fullcmd = 0;
715 1.1 christos }
716 1.1 christos close(fd);
717 1.1 christos }
718 1.1 christos else
719 1.1 christos {
720 1.1 christos fullcmd = 0;
721 1.1 christos }
722 1.1 christos }
723 1.1 christos
724 1.1 christos /* grab the shared memory size */
725 1.1 christos sprintf(buffer, "%d/statm", pid);
726 1.1 christos fd = open(buffer, O_RDONLY);
727 1.1 christos len = read(fd, buffer, sizeof(buffer)-1);
728 1.1 christos close(fd);
729 1.1 christos buffer[len] = '\0';
730 1.1 christos p = buffer;
731 1.1 christos p = skip_token(p); /* skip size */
732 1.1 christos p = skip_token(p); /* skip resident */
733 1.1 christos proc->shared = pagetok(strtoul(p, &p, 10));
734 1.1 christos
735 1.1 christos /* grab the proc stat info in one go */
736 1.1 christos if (taskpid == -1)
737 1.1 christos {
738 1.1 christos sprintf(buffer, "%d/stat", pid);
739 1.1 christos }
740 1.1 christos else
741 1.1 christos {
742 1.1 christos sprintf(buffer, "%d/task/%d/stat", pid, taskpid);
743 1.1 christos }
744 1.1 christos
745 1.1 christos fd = open(buffer, O_RDONLY);
746 1.1 christos len = read(fd, buffer, sizeof(buffer)-1);
747 1.1 christos close(fd);
748 1.1 christos
749 1.1 christos buffer[len] = '\0';
750 1.1 christos
751 1.1 christos proc->uid = (uid_t)proc_owner((int)pid);
752 1.1 christos
753 1.1 christos /* parse out the status */
754 1.1 christos
755 1.1 christos /* skip pid and locate command, which is in parentheses */
756 1.1 christos if ((p = strchr(buffer, '(')) == NULL)
757 1.1 christos {
758 1.1 christos return;
759 1.1 christos }
760 1.1 christos if ((q = strrchr(++p, ')')) == NULL)
761 1.1 christos {
762 1.1 christos return;
763 1.1 christos }
764 1.1 christos
765 1.1 christos /* set the procname */
766 1.1 christos *q = '\0';
767 1.1 christos if (!fullcmd)
768 1.1 christos {
769 1.1 christos update_procname(proc, p);
770 1.1 christos }
771 1.1 christos
772 1.1 christos /* scan the rest of the line */
773 1.1 christos p = q+1;
774 1.1 christos p = skip_ws(p);
775 1.1 christos switch (*p++) /* state */
776 1.1 christos {
777 1.1 christos case 'R': proc->state = 1; break;
778 1.1 christos case 'S': proc->state = 2; break;
779 1.1 christos case 'D': proc->state = 3; break;
780 1.1 christos case 'Z': proc->state = 4; break;
781 1.1 christos case 'T': proc->state = 5; break;
782 1.1 christos case 'W': proc->state = 6; break;
783 1.1 christos case '\0': return;
784 1.1 christos }
785 1.1 christos
786 1.1 christos p = skip_token(p); /* skip ppid */
787 1.1 christos p = skip_token(p); /* skip pgrp */
788 1.1 christos p = skip_token(p); /* skip session */
789 1.1 christos p = skip_token(p); /* skip tty */
790 1.1 christos p = skip_token(p); /* skip tty pgrp */
791 1.1 christos p = skip_token(p); /* skip flags */
792 1.1 christos p = skip_token(p); /* skip min flt */
793 1.1 christos p = skip_token(p); /* skip cmin flt */
794 1.1 christos p = skip_token(p); /* skip maj flt */
795 1.1 christos p = skip_token(p); /* skip cmaj flt */
796 1.1 christos
797 1.1 christos proc->time = strtoul(p, &p, 10); /* utime */
798 1.1 christos proc->time += strtoul(p, &p, 10); /* stime */
799 1.1 christos
800 1.1 christos p = skip_token(p); /* skip cutime */
801 1.1 christos p = skip_token(p); /* skip cstime */
802 1.1 christos
803 1.1 christos proc->pri = strtol(p, &p, 10); /* priority */
804 1.1 christos proc->nice = strtol(p, &p, 10); /* nice */
805 1.1 christos proc->threads = strtol(p, &p, 10); /* threads */
806 1.1 christos
807 1.1 christos p = skip_token(p); /* skip it_real_val */
808 1.1 christos proc->start_time = strtoul(p, &p, 10); /* start_time */
809 1.1 christos
810 1.1 christos proc->size = bytetok(strtoul(p, &p, 10)); /* vsize */
811 1.1 christos proc->rss = pagetok(strtoul(p, &p, 10)); /* rss */
812 1.1 christos
813 1.1 christos #if 0
814 1.1 christos /* for the record, here are the rest of the fields */
815 1.1 christos p = skip_token(p); /* skip rlim */
816 1.1 christos p = skip_token(p); /* skip start_code */
817 1.1 christos p = skip_token(p); /* skip end_code */
818 1.1 christos p = skip_token(p); /* skip start_stack */
819 1.1 christos p = skip_token(p); /* skip sp */
820 1.1 christos p = skip_token(p); /* skip pc */
821 1.1 christos p = skip_token(p); /* skip signal */
822 1.1 christos p = skip_token(p); /* skip sigblocked */
823 1.1 christos p = skip_token(p); /* skip sigignore */
824 1.1 christos p = skip_token(p); /* skip sigcatch */
825 1.1 christos p = skip_token(p); /* skip wchan */
826 1.1 christos #endif
827 1.1 christos
828 1.1 christos }
829 1.1 christos
830 1.1 christos static int show_usernames;
831 1.1 christos static int show_threads;
832 1.1 christos
833 1.1 christos
834 1.1 christos caddr_t
835 1.1 christos get_process_info(struct system_info *si,
836 1.1 christos struct process_select *sel,
837 1.1 christos int compare_index)
838 1.1 christos {
839 1.1 christos struct top_proc *proc;
840 1.1 christos struct top_proc *taskproc;
841 1.1 christos pid_t pid;
842 1.1 christos pid_t taskpid;
843 1.1 christos unsigned long now;
844 1.1 christos unsigned long elapsed;
845 1.1 christos hash_item_pid *hi;
846 1.1 christos hash_pos pos;
847 1.1 christos
848 1.1 christos /* round current time to a second */
849 1.1 christos now = (unsigned long)lasttime.tv_sec;
850 1.1 christos if (lasttime.tv_usec >= 500000)
851 1.1 christos {
852 1.1 christos now++;
853 1.1 christos }
854 1.1 christos
855 1.1 christos /* calculate number of ticks since our last check */
856 1.1 christos elapsed = timediff.tv_sec * HZ + (timediff.tv_usec * HZ) / 1000000;
857 1.1 christos if (elapsed <= 0)
858 1.1 christos {
859 1.1 christos elapsed = 1;
860 1.1 christos }
861 1.1 christos dprintf("get_process_info: elapsed %d ticks\n", elapsed);
862 1.1 christos
863 1.1 christos /* mark all hash table entries as not seen */
864 1.1 christos hi = hash_first_pid(ptable, &pos);
865 1.1 christos while (hi != NULL)
866 1.1 christos {
867 1.1 christos ((struct top_proc *)(hi->value))->state = 0;
868 1.1 christos hi = hash_next_pid(&pos);
869 1.1 christos }
870 1.1 christos /* mark all hash table entries as not seen */
871 1.1 christos hi = hash_first_pid(tasktable, &pos);
872 1.1 christos while (hi != NULL)
873 1.1 christos {
874 1.1 christos ((struct top_proc *)(hi->value))->state = 0;
875 1.1 christos hi = hash_next_pid(&pos);
876 1.1 christos }
877 1.1 christos
878 1.1 christos /* read the process information */
879 1.1 christos {
880 1.1 christos DIR *dir = opendir(".");
881 1.1 christos DIR *taskdir;
882 1.1 christos struct dirent *ent;
883 1.1 christos struct dirent *taskent;
884 1.1 christos int total_procs = 0;
885 1.1 christos struct top_proc **active;
886 1.1 christos hash_item_pid *hi;
887 1.1 christos hash_pos pos;
888 1.1 christos char buffer[64];
889 1.1 christos
890 1.1 christos int show_idle = sel->idle;
891 1.1 christos int show_uid = sel->uid != -1;
892 1.1 christos char *show_command = sel->command;
893 1.1 christos
894 1.1 christos show_usernames = sel->usernames;
895 1.1 christos show_threads = sel->threads && have_task;
896 1.1 christos
897 1.1 christos memset(process_states, 0, sizeof(process_states));
898 1.1 christos
899 1.1 christos taskdir = NULL;
900 1.1 christos taskent = NULL;
901 1.1 christos taskpid = -1;
902 1.1 christos
903 1.1 christos while ((ent = readdir(dir)) != NULL)
904 1.1 christos {
905 1.1 christos unsigned long otime;
906 1.1 christos
907 1.1 christos if (!isdigit(ent->d_name[0]))
908 1.1 christos continue;
909 1.1 christos
910 1.1 christos pid = atoi(ent->d_name);
911 1.1 christos
912 1.1 christos /* look up hash table entry */
913 1.1 christos proc = hash_lookup_pid(ptable, pid);
914 1.1 christos
915 1.1 christos /* if we came up empty, create a new entry */
916 1.1 christos if (proc == NULL)
917 1.1 christos {
918 1.1 christos proc = new_proc();
919 1.1 christos proc->pid = pid;
920 1.1 christos proc->time = 0;
921 1.1 christos hash_add_pid(ptable, pid, (void *)proc);
922 1.1 christos }
923 1.1 christos
924 1.1 christos /* remember the previous cpu time */
925 1.1 christos otime = proc->time;
926 1.1 christos
927 1.1 christos /* get current data */
928 1.1 christos read_one_proc_stat(pid, -1, proc, sel);
929 1.1 christos
930 1.1 christos /* continue on if this isn't really a process */
931 1.1 christos if (proc->state == 0)
932 1.1 christos continue;
933 1.1 christos
934 1.1 christos /* reset linked list (for threads) */
935 1.1 christos proc->next = NULL;
936 1.1 christos
937 1.1 christos /* accumulate process state data */
938 1.1 christos total_procs++;
939 1.1 christos process_states[proc->state]++;
940 1.1 christos
941 1.1 christos /* calculate pcpu */
942 1.1 christos if ((proc->pcpu = (proc->time - otime) / (double)elapsed) < 0.0001)
943 1.1 christos {
944 1.1 christos proc->pcpu = 0;
945 1.1 christos }
946 1.1 christos
947 1.1 christos /* if we have task subdirs and this process has more than
948 1.1 christos one thread, collect data on each thread */
949 1.1 christos if (have_task && proc->threads > 1)
950 1.1 christos {
951 1.1 christos snprintf(buffer, sizeof(buffer), "%d/task", pid);
952 1.1 christos if ((taskdir = opendir(buffer)) != NULL)
953 1.1 christos {
954 1.1 christos while ((taskent = readdir(taskdir)) != NULL)
955 1.1 christos {
956 1.1 christos if (!isdigit(taskent->d_name[0]))
957 1.1 christos continue;
958 1.1 christos
959 1.1 christos /* lookup entry in tasktable */
960 1.1 christos taskpid = atoi(taskent->d_name);
961 1.1 christos taskproc = hash_lookup_pid(tasktable, taskpid);
962 1.1 christos
963 1.1 christos /* if we came up empty, create a new entry */
964 1.1 christos if (taskproc == NULL)
965 1.1 christos {
966 1.1 christos taskproc = new_proc();
967 1.1 christos taskproc->pid = taskpid;
968 1.1 christos taskproc->time = 0;
969 1.1 christos hash_add_pid(tasktable, taskpid, (void *)taskproc);
970 1.1 christos }
971 1.1 christos
972 1.1 christos /* remember the previous cpu time */
973 1.1 christos otime = taskproc->time;
974 1.1 christos
975 1.1 christos /* get current data */
976 1.1 christos read_one_proc_stat(pid, taskpid, taskproc, sel);
977 1.1 christos
978 1.1 christos /* ignore if it isnt real */
979 1.1 christos if (taskproc->state == 0)
980 1.1 christos continue;
981 1.1 christos
982 1.1 christos /* when showing threads, add this to the accumulated
983 1.1 christos process state data, but remember that the first
984 1.1 christos thread is already accounted for */
985 1.1 christos if (show_threads && pid != taskpid)
986 1.1 christos {
987 1.1 christos total_procs++;
988 1.1 christos process_states[taskproc->state]++;
989 1.1 christos }
990 1.1 christos
991 1.1 christos /* calculate pcpu */
992 1.1 christos if ((taskproc->pcpu = (taskproc->time - otime) /
993 1.1 christos (double)elapsed) < 0.0)
994 1.1 christos {
995 1.1 christos taskproc->pcpu = 0;
996 1.1 christos }
997 1.1 christos
998 1.1 christos /* link this in to the proc's list */
999 1.1 christos taskproc->next = proc->next;
1000 1.1 christos proc->next = taskproc;
1001 1.1 christos }
1002 1.1 christos closedir(taskdir);
1003 1.1 christos }
1004 1.1 christos }
1005 1.1 christos }
1006 1.1 christos closedir(dir);
1007 1.1 christos
1008 1.1 christos /* make sure we have enough slots for the active procs */
1009 1.1 christos if (activesize < total_procs)
1010 1.1 christos {
1011 1.1 christos pactive = (struct top_proc **)realloc(pactive,
1012 1.1 christos sizeof(struct top_proc *) * total_procs);
1013 1.1 christos activesize = total_procs;
1014 1.1 christos }
1015 1.1 christos
1016 1.1 christos /* set up the active procs and flush dead entries */
1017 1.1 christos active = pactive;
1018 1.1 christos hi = hash_first_pid(ptable, &pos);
1019 1.1 christos while (hi != NULL)
1020 1.1 christos {
1021 1.1 christos proc = (struct top_proc *)(hi->value);
1022 1.1 christos if (proc->state == 0)
1023 1.1 christos {
1024 1.1 christos /* dead entry */
1025 1.1 christos hash_remove_pos_pid(&pos);
1026 1.1 christos free_proc(proc);
1027 1.1 christos }
1028 1.1 christos else
1029 1.1 christos {
1030 1.1 christos /* check to see if it qualifies as active */
1031 1.1 christos if ((show_idle || proc->state == 1 || proc->pcpu) &&
1032 1.1 christos (!show_uid || proc->uid == sel->uid) &&
1033 1.1 christos (show_command == NULL ||
1034 1.1 christos strstr(proc->name, show_command) != NULL))
1035 1.1 christos {
1036 1.1 christos /* are we showing threads and does this proc have any? */
1037 1.1 christos if (show_threads && proc->threads > 1 && proc->next != NULL)
1038 1.1 christos {
1039 1.1 christos /* then add just the thread info -- the main process
1040 1.1 christos info is included in the list */
1041 1.1 christos proc = proc->next;
1042 1.1 christos while (proc != NULL)
1043 1.1 christos {
1044 1.1 christos *active++ = proc;
1045 1.1 christos proc = proc->next;
1046 1.1 christos }
1047 1.1 christos }
1048 1.1 christos else
1049 1.1 christos {
1050 1.1 christos /* add the process */
1051 1.1 christos *active++ = proc;
1052 1.1 christos }
1053 1.1 christos }
1054 1.1 christos }
1055 1.1 christos
1056 1.1 christos hi = hash_next_pid(&pos);
1057 1.1 christos }
1058 1.1 christos
1059 1.1 christos si->p_active = active - pactive;
1060 1.1 christos si->p_total = total_procs;
1061 1.1 christos si->procstates = process_states;
1062 1.1 christos }
1063 1.1 christos
1064 1.1 christos /* if requested, sort the "active" procs */
1065 1.1 christos if (si->p_active)
1066 1.1 christos qsort(pactive, si->p_active, sizeof(struct top_proc *),
1067 1.1 christos proc_compares[compare_index]);
1068 1.1 christos
1069 1.1 christos /* don't even pretend that the return value thing here isn't bogus */
1070 1.1 christos nextactive = pactive;
1071 1.1 christos return (caddr_t)0;
1072 1.1 christos }
1073 1.1 christos
1074 1.1 christos
1075 1.1 christos char *
1076 1.1 christos format_header(char *uname_field)
1077 1.1 christos
1078 1.1 christos {
1079 1.1 christos int uname_len = strlen(uname_field);
1080 1.1 christos if (uname_len > 8)
1081 1.1 christos uname_len = 8;
1082 1.1 christos
1083 1.1 christos memcpy(strchr(fmt_header, 'X'), uname_field, uname_len);
1084 1.1 christos
1085 1.1 christos return fmt_header;
1086 1.1 christos }
1087 1.1 christos
1088 1.1 christos static char p_header[MAX_COLS];
1089 1.1 christos
1090 1.1 christos char *
1091 1.1 christos format_process_header(struct process_select *sel, caddr_t handle, int count)
1092 1.1 christos
1093 1.1 christos {
1094 1.1 christos char *h;
1095 1.1 christos
1096 1.1 christos h = sel->threads ? proc_header_nothr : proc_header_thr;
1097 1.1 christos
1098 1.1 christos snprintf(p_header, MAX_COLS, h, sel->usernames ? "USERNAME" : "UID");
1099 1.1 christos
1100 1.1 christos return p_header;
1101 1.1 christos }
1102 1.1 christos
1103 1.1 christos
1104 1.1 christos char *
1105 1.1 christos format_next_process(caddr_t handle, char *(*get_userid)(int))
1106 1.1 christos
1107 1.1 christos {
1108 1.1 christos static char fmt[MAX_COLS]; /* static area where result is built */
1109 1.1 christos struct top_proc *p = *nextactive++;
1110 1.1 christos char *userbuf;
1111 1.1 christos
1112 1.1 christos userbuf = show_usernames ? username(p->uid) : itoa_w(p->uid, 7);
1113 1.1 christos
1114 1.1 christos if (show_threads)
1115 1.1 christos {
1116 1.1 christos snprintf(fmt, sizeof(fmt),
1117 1.1 christos "%5d %-8.8s %3d %4d %5s %5s %5s %-5s %6s %5s%% %s",
1118 1.1 christos p->pid,
1119 1.1 christos userbuf,
1120 1.1 christos p->pri < -99 ? -99 : p->pri,
1121 1.1 christos p->nice,
1122 1.1 christos format_k(p->size),
1123 1.1 christos format_k(p->rss),
1124 1.1 christos format_k(p->shared),
1125 1.1 christos state_abbrev[p->state],
1126 1.1 christos format_time(p->time / HZ),
1127 1.1 christos format_percent(p->pcpu * 100.0),
1128 1.1 christos p->name);
1129 1.1 christos }
1130 1.1 christos else
1131 1.1 christos {
1132 1.1 christos snprintf(fmt, sizeof(fmt),
1133 1.1 christos "%5d %-8.8s %4d %3d %4d %5s %5s %5s %-5s %6s %5s%% %s",
1134 1.1 christos p->pid,
1135 1.1 christos userbuf,
1136 1.1 christos p->threads <= 9999 ? p->threads : 9999,
1137 1.1 christos p->pri < -99 ? -99 : p->pri,
1138 1.1 christos p->nice,
1139 1.1 christos format_k(p->size),
1140 1.1 christos format_k(p->rss),
1141 1.1 christos format_k(p->shared),
1142 1.1 christos state_abbrev[p->state],
1143 1.1 christos format_time(p->time / HZ),
1144 1.1 christos format_percent(p->pcpu * 100.0),
1145 1.1 christos p->name);
1146 1.1 christos }
1147 1.1 christos
1148 1.1 christos /* return the result */
1149 1.1 christos return (fmt);
1150 1.1 christos }
1151 1.1 christos
1152 1.1 christos /* comparison routines for qsort */
1153 1.1 christos
1154 1.1 christos /*
1155 1.1 christos * There are currently four possible comparison routines. main selects
1156 1.1 christos * one of these by indexing in to the array proc_compares.
1157 1.1 christos *
1158 1.1 christos * Possible keys are defined as macros below. Currently these keys are
1159 1.1 christos * defined: percent cpu, cpu ticks, process state, resident set size,
1160 1.1 christos * total virtual memory usage. The process states are ordered as follows
1161 1.1 christos * (from least to most important): WAIT, zombie, sleep, stop, start, run.
1162 1.1 christos * The array declaration below maps a process state index into a number
1163 1.1 christos * that reflects this ordering.
1164 1.1 christos */
1165 1.1 christos
1166 1.1 christos /* First, the possible comparison keys. These are defined in such a way
1167 1.1 christos that they can be merely listed in the source code to define the actual
1168 1.1 christos desired ordering.
1169 1.1 christos */
1170 1.1 christos
1171 1.1 christos #define ORDERKEY_PCTCPU if (dresult = p2->pcpu - p1->pcpu,\
1172 1.1 christos (result = dresult > 0.0 ? 1 : dresult < 0.0 ? -1 : 0) == 0)
1173 1.1 christos #define ORDERKEY_CPTICKS if ((result = (long)p2->time - (long)p1->time) == 0)
1174 1.1 christos #define ORDERKEY_STATE if ((result = (sort_state[p2->state] - \
1175 1.1 christos sort_state[p1->state])) == 0)
1176 1.1 christos #define ORDERKEY_PRIO if ((result = p2->pri - p1->pri) == 0)
1177 1.1 christos #define ORDERKEY_RSSIZE if ((result = p2->rss - p1->rss) == 0)
1178 1.1 christos #define ORDERKEY_MEM if ((result = p2->size - p1->size) == 0)
1179 1.1 christos #define ORDERKEY_NAME if ((result = strcmp(p1->name, p2->name)) == 0)
1180 1.1 christos
1181 1.1 christos /* Now the array that maps process state to a weight */
1182 1.1 christos
1183 1.1 christos unsigned char sort_state[] =
1184 1.1 christos {
1185 1.1 christos 0, /* empty */
1186 1.1 christos 6, /* run */
1187 1.1 christos 3, /* sleep */
1188 1.1 christos 5, /* disk wait */
1189 1.1 christos 1, /* zombie */
1190 1.1 christos 2, /* stop */
1191 1.1 christos 4 /* swap */
1192 1.1 christos };
1193 1.1 christos
1194 1.1 christos
1195 1.1 christos /* compare_cpu - the comparison function for sorting by cpu percentage */
1196 1.1 christos
1197 1.1 christos int
1198 1.1 christos compare_cpu (
1199 1.1 christos struct top_proc **pp1,
1200 1.1 christos struct top_proc **pp2)
1201 1.1 christos {
1202 1.1 christos register struct top_proc *p1;
1203 1.1 christos register struct top_proc *p2;
1204 1.1 christos register long result;
1205 1.1 christos double dresult;
1206 1.1 christos
1207 1.1 christos /* remove one level of indirection */
1208 1.1 christos p1 = *pp1;
1209 1.1 christos p2 = *pp2;
1210 1.1 christos
1211 1.1 christos ORDERKEY_PCTCPU
1212 1.1 christos ORDERKEY_CPTICKS
1213 1.1 christos ORDERKEY_STATE
1214 1.1 christos ORDERKEY_PRIO
1215 1.1 christos ORDERKEY_RSSIZE
1216 1.1 christos ORDERKEY_MEM
1217 1.1 christos ;
1218 1.1 christos
1219 1.1 christos return result == 0 ? 0 : result < 0 ? -1 : 1;
1220 1.1 christos }
1221 1.1 christos
1222 1.1 christos /* compare_size - the comparison function for sorting by total memory usage */
1223 1.1 christos
1224 1.1 christos int
1225 1.1 christos compare_size (
1226 1.1 christos struct top_proc **pp1,
1227 1.1 christos struct top_proc **pp2)
1228 1.1 christos {
1229 1.1 christos register struct top_proc *p1;
1230 1.1 christos register struct top_proc *p2;
1231 1.1 christos register long result;
1232 1.1 christos double dresult;
1233 1.1 christos
1234 1.1 christos /* remove one level of indirection */
1235 1.1 christos p1 = *pp1;
1236 1.1 christos p2 = *pp2;
1237 1.1 christos
1238 1.1 christos ORDERKEY_MEM
1239 1.1 christos ORDERKEY_RSSIZE
1240 1.1 christos ORDERKEY_PCTCPU
1241 1.1 christos ORDERKEY_CPTICKS
1242 1.1 christos ORDERKEY_STATE
1243 1.1 christos ORDERKEY_PRIO
1244 1.1 christos ;
1245 1.1 christos
1246 1.1 christos return result == 0 ? 0 : result < 0 ? -1 : 1;
1247 1.1 christos }
1248 1.1 christos
1249 1.1 christos /* compare_res - the comparison function for sorting by resident set size */
1250 1.1 christos
1251 1.1 christos int
1252 1.1 christos compare_res (
1253 1.1 christos struct top_proc **pp1,
1254 1.1 christos struct top_proc **pp2)
1255 1.1 christos {
1256 1.1 christos register struct top_proc *p1;
1257 1.1 christos register struct top_proc *p2;
1258 1.1 christos register long result;
1259 1.1 christos double dresult;
1260 1.1 christos
1261 1.1 christos /* remove one level of indirection */
1262 1.1 christos p1 = *pp1;
1263 1.1 christos p2 = *pp2;
1264 1.1 christos
1265 1.1 christos ORDERKEY_RSSIZE
1266 1.1 christos ORDERKEY_MEM
1267 1.1 christos ORDERKEY_PCTCPU
1268 1.1 christos ORDERKEY_CPTICKS
1269 1.1 christos ORDERKEY_STATE
1270 1.1 christos ORDERKEY_PRIO
1271 1.1 christos ;
1272 1.1 christos
1273 1.1 christos return result == 0 ? 0 : result < 0 ? -1 : 1;
1274 1.1 christos }
1275 1.1 christos
1276 1.1 christos /* compare_time - the comparison function for sorting by total cpu time */
1277 1.1 christos
1278 1.1 christos int
1279 1.1 christos compare_time (
1280 1.1 christos struct top_proc **pp1,
1281 1.1 christos struct top_proc **pp2)
1282 1.1 christos {
1283 1.1 christos register struct top_proc *p1;
1284 1.1 christos register struct top_proc *p2;
1285 1.1 christos register long result;
1286 1.1 christos double dresult;
1287 1.1 christos
1288 1.1 christos /* remove one level of indirection */
1289 1.1 christos p1 = *pp1;
1290 1.1 christos p2 = *pp2;
1291 1.1 christos
1292 1.1 christos ORDERKEY_CPTICKS
1293 1.1 christos ORDERKEY_PCTCPU
1294 1.1 christos ORDERKEY_STATE
1295 1.1 christos ORDERKEY_PRIO
1296 1.1 christos ORDERKEY_MEM
1297 1.1 christos ORDERKEY_RSSIZE
1298 1.1 christos ;
1299 1.1 christos
1300 1.1 christos return result == 0 ? 0 : result < 0 ? -1 : 1;
1301 1.1 christos }
1302 1.1 christos
1303 1.1 christos
1304 1.1 christos /* compare_cmd - the comparison function for sorting by command name */
1305 1.1 christos
1306 1.1 christos int
1307 1.1 christos compare_cmd (
1308 1.1 christos struct top_proc **pp1,
1309 1.1 christos struct top_proc **pp2)
1310 1.1 christos {
1311 1.1 christos register struct top_proc *p1;
1312 1.1 christos register struct top_proc *p2;
1313 1.1 christos register long result;
1314 1.1 christos double dresult;
1315 1.1 christos
1316 1.1 christos /* remove one level of indirection */
1317 1.1 christos p1 = *pp1;
1318 1.1 christos p2 = *pp2;
1319 1.1 christos
1320 1.1 christos ORDERKEY_NAME
1321 1.1 christos ORDERKEY_PCTCPU
1322 1.1 christos ORDERKEY_CPTICKS
1323 1.1 christos ORDERKEY_STATE
1324 1.1 christos ORDERKEY_PRIO
1325 1.1 christos ORDERKEY_RSSIZE
1326 1.1 christos ORDERKEY_MEM
1327 1.1 christos ;
1328 1.1 christos
1329 1.1 christos return result == 0 ? 0 : result < 0 ? -1 : 1;
1330 1.1 christos }
1331 1.1 christos
1332 1.1 christos
1333 1.1 christos /*
1334 1.1 christos * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
1335 1.1 christos * the process does not exist.
1336 1.1 christos * It is EXTREMLY IMPORTANT that this function work correctly.
1337 1.1 christos * If top runs setuid root (as in SVR4), then this function
1338 1.1 christos * is the only thing that stands in the way of a serious
1339 1.1 christos * security problem. It validates requests for the "kill"
1340 1.1 christos * and "renice" commands.
1341 1.1 christos */
1342 1.1 christos
1343 1.1 christos int
1344 1.1 christos proc_owner(int pid)
1345 1.1 christos
1346 1.1 christos {
1347 1.1 christos struct stat sb;
1348 1.1 christos char buffer[32];
1349 1.1 christos sprintf(buffer, "%d", pid);
1350 1.1 christos
1351 1.1 christos if (stat(buffer, &sb) < 0)
1352 1.1 christos return -1;
1353 1.1 christos else
1354 1.1 christos return (int)sb.st_uid;
1355 1.1 christos }
1356