Deleted Added
sdiff udiff text old ( 131310 ) new ( 131402 )
full compact
1/*
2 * top - a top users display for Unix
3 *
4 * SYNOPSIS: For FreeBSD-2.x and later
5 *
6 * DESCRIPTION:
7 * Originally written for BSD4.4 system by Christos Zoulas.
8 * Ported to FreeBSD 2.x by Steven Wallace && Wolfram Schneider

--- 6 unchanged lines hidden (view full) ---

15 *
16 * LIBS: -lkvm
17 *
18 * AUTHOR: Christos Zoulas <christos@ee.cornell.edu>
19 * Steven Wallace <swallace@freebsd.org>
20 * Wolfram Schneider <wosch@FreeBSD.org>
21 * Thomas Moestl <tmoestl@gmx.net>
22 *
23 * $FreeBSD: head/usr.bin/top/machine.c 131310 2004-06-30 04:19:23Z alfred $
24 */
25
26
27#include <sys/time.h>
28#include <sys/types.h>
29#include <sys/signal.h>
30#include <sys/param.h>
31

--- 26 unchanged lines hidden (view full) ---

58
59static void getsysctl(char *, void *, size_t);
60
61#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
62
63extern char* printable(char *);
64int swapmode(int *retavail, int *retfree);
65static int smpmode;
66static int namelength;
67static int cmdlengthdelta;
68
69/* Prototypes for top internals */
70void quit(int);
71
72/* get_process_info passes back a handle. This is what it looks like: */
73
74struct handle
75{
76 struct kinfo_proc **next_proc; /* points to next valid proc pointer */
77 int remaining; /* number of pointers remaining */
78};
79
80/* declarations for load_avg */
81#include "loadavg.h"
82
83/* define what weighted cpu is. */
84#define weighted_cpu(pct, pp) ((pp)->ki_swtime == 0 ? 0.0 : \
85 ((pct) / (1.0 - exp((pp)->ki_swtime * logcpu))))
86
87/* what we consider to be process size: */
88#define PROCSIZE(pp) ((pp)->ki_size / 1024)
89
90/* definitions for indices in the nlist array */
91
92/*
93 * These definitions control the format of the per-process area
94 */
95
96static char smp_header[] =
97 " PID %-*.*s PRI NICE SIZE RES STATE C TIME WCPU CPU COMMAND";
98
99#define smp_Proc_format \
100 "%5d %-*.*s %3d %4d%7s %6s %-6.6s %1x%7s %5.2f%% %5.2f%% %.*s"
101
102static char up_header[] =
103 " PID %-*.*s PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND";

--- 67 unchanged lines hidden (view full) ---

171
172/* these are for keeping track of the proc array */
173
174static int nproc;
175static int onproc = -1;
176static int pref_len;
177static struct kinfo_proc *pbase;
178static struct kinfo_proc **pref;
179
180/* these are for getting the memory statistics */
181
182static int pageshift; /* log base 2 of the pagesize */
183
184/* define pagetok in terms of pageshift */
185
186#define pagetok(size) ((size) << pageshift)

--- 71 unchanged lines hidden (view full) ---

258}
259
260char *
261format_header(uname_field)
262 char *uname_field;
263
264{
265 static char Header[128];
266
267 snprintf(Header, sizeof(Header), smpmode ? smp_header : up_header,
268 namelength, namelength, uname_field);
269
270 cmdlengthdelta = strlen(Header) - 7;
271
272 return Header;
273}
274
275static int swappgsin = -1;

--- 106 unchanged lines hidden (view full) ---

382 if (sysctl(mib, 2, &boottime, &bt_size, NULL, 0) != -1 &&
383 boottime.tv_sec != 0) {
384 si->boottime = boottime;
385 } else {
386 si->boottime.tv_sec = -1;
387 }
388}
389
390static struct handle handle;
391
392caddr_t
393get_process_info(si, sel, compare)
394 struct system_info *si;
395 struct process_select *sel;
396 int (*compare)();
397{

--- 6 unchanged lines hidden (view full) ---

404
405 /* these are copied out of sel for speed */
406 int show_idle;
407 int show_self;
408 int show_system;
409 int show_uid;
410 int show_command;
411
412 pbase = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc);
413 if (nproc > onproc)
414 pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *)
415 * (onproc = nproc));
416 if (pref == NULL || pbase == NULL) {
417 (void) fprintf(stderr, "top: Out of memory.\n");
418 quit(23);
419 }

--- 22 unchanged lines hidden (view full) ---

442 */
443 if (pp->ki_stat != 0 &&
444 (show_self != pp->ki_pid) &&
445 (show_system || ((pp->ki_flag & P_SYSTEM) == 0)))
446 {
447 total_procs++;
448 process_states[(unsigned char) pp->ki_stat]++;
449 if ((pp->ki_stat != SZOMB) &&
450 (show_idle || (pp->ki_pctcpu != 0) ||
451 (pp->ki_stat == SRUN)) &&
452 (!show_uid || pp->ki_ruid == (uid_t)sel->uid))
453 {
454 /*
455 * When not showing threads, take the first thread
456 * for output and add the fields that we can from
457 * the rest of the process's threads rather than
458 * using the system's mostly-broken KERN_PROC_PROC.
459 */

--- 29 unchanged lines hidden (view full) ---

489char fmt[128]; /* static area where result is built */
490
491char *
492format_next_process(handle, get_userid)
493 caddr_t handle;
494 char *(*get_userid)();
495{
496 struct kinfo_proc *pp;
497 long cputime;
498 double pct;
499 struct handle *hp;
500 char status[16];
501 int state;
502
503 /* find and remember the next proc structure */
504 hp = (struct handle *)handle;
505 pp = *(hp->next_proc++);
506 hp->remaining--;
507
508 /* get the process's command name */
509 if ((pp->ki_sflag & PS_INMEM) == 0) {

--- 46 unchanged lines hidden (view full) ---

556 if (state >= 0 &&
557 state < sizeof(state_abbrev) / sizeof(*state_abbrev))
558 sprintf(status, "%.6s", state_abbrev[(unsigned char) state]);
559 else
560 sprintf(status, "?%5d", state);
561 break;
562 }
563
564 /* format this entry */
565 sprintf(fmt,
566 smpmode ? smp_Proc_format : up_Proc_format,
567 pp->ki_pid,
568 namelength, namelength,
569 (*get_userid)(pp->ki_ruid),
570 pp->ki_pri.pri_level - PZERO,
571

--- 40 unchanged lines hidden (view full) ---

612 fprintf(stderr, "top: sysctl(%s...) expected %lu, got %lu\n", name,
613 (unsigned long)len, (unsigned long)nlen);
614 quit(23);
615 }
616}
617
618/* comparison routines for qsort */
619
620/*
621 * proc_compare - comparison function for "qsort"
622 * Compares the resource consumption of two processes using five
623 * distinct keys. The keys (in descending order of importance) are:
624 * percent cpu, cpu ticks, state, resident set size, total virtual
625 * memory usage. The process states are ordered as follows (from least
626 * to most important): WAIT, zombie, sleep, stop, start, run. The
627 * array declaration below maps a process state index into a number

--- 181 unchanged lines hidden (view full) ---

809 ORDERKEY_RSSIZE
810 ORDERKEY_MEM
811 ;
812
813 return(result);
814}
815#endif
816
817/*
818 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
819 * the process does not exist.
820 * It is EXTREMLY IMPORTANT that this function work correctly.
821 * If top runs setuid root (as in SVR4), then this function
822 * is the only thing that stands in the way of a serious
823 * security problem. It validates requests for the "kill"
824 * and "renice" commands.

--- 49 unchanged lines hidden ---