machine.c revision 1.33
1/* $OpenBSD: machine.c,v 1.33 2003/06/15 16:24:44 millert Exp $	 */
2
3/*-
4 * Copyright (c) 1994 Thorsten Lockert <tholo@sigmasoft.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 *    derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * AUTHOR:  Thorsten Lockert <tholo@sigmasoft.com>
30 *          Adapted from BSD4.4 by Christos Zoulas <christos@ee.cornell.edu>
31 *          Patch for process wait display by Jarl F. Greipsland <jarle@idt.unit.no>
32 *	    Patch for -DORDER by Kenneth Stailey <kstailey@disclosure.com>
33 *	    Patch for new swapctl(2) by Tobias Weingartner <weingart@openbsd.org>
34 */
35
36#include <sys/types.h>
37#include <sys/signal.h>
38#include <sys/param.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <limits.h>
43#include <err.h>
44#include <math.h>
45#include <unistd.h>
46#include <sys/errno.h>
47#include <sys/sysctl.h>
48#include <sys/dir.h>
49#include <sys/dkstat.h>
50#include <sys/file.h>
51#include <sys/time.h>
52#include <sys/resource.h>
53#include <sys/swap.h>
54#include <err.h>
55
56#include "top.h"
57#include "display.h"
58#include "machine.h"
59#include "utils.h"
60#include "loadavg.h"
61
62static int      swapmode(int *, int *);
63
64/* get_process_info passes back a handle.  This is what it looks like: */
65
66struct handle {
67	struct kinfo_proc **next_proc;	/* points to next valid proc pointer */
68	int             remaining;	/* number of pointers remaining */
69};
70
71#define PP(pp, field) ((pp)->kp_proc . field)
72#define EP(pp, field) ((pp)->kp_eproc . field)
73#define VP(pp, field) ((pp)->kp_eproc.e_vm . field)
74
75/* what we consider to be process size: */
76#define PROCSIZE(pp) (VP((pp), vm_tsize) + VP((pp), vm_dsize) + VP((pp), vm_ssize))
77
78/*
79 *  These definitions control the format of the per-process area
80 */
81static char     header[] =
82	"  PID X        PRI NICE  SIZE   RES STATE WAIT     TIME    CPU COMMAND";
83
84/* 0123456   -- field to fill in starts at header+6 */
85#define UNAME_START 6
86
87#define Proc_format \
88	"%5d %-8.8s %3d %4d %5s %5s %-5s %-6.6s %6s %5.2f%% %.14s"
89
90/* process state names for the "STATE" column of the display */
91/*
92 * the extra nulls in the string "run" are for adding a slash and the
93 * processor number when needed
94 */
95
96char	*state_abbrev[] = {
97	"", "start", "run\0\0\0", "sleep", "stop", "zomb",
98};
99
100static int      stathz;
101
102/* these are for calculating cpu state percentages */
103static long     cp_time[CPUSTATES];
104static long     cp_old[CPUSTATES];
105static long     cp_diff[CPUSTATES];
106
107/* these are for detailing the process states */
108int process_states[7];
109char *procstatenames[] = {
110	"", " starting, ", " running, ", " idle, ",
111	" stopped, ", " zombie, ",
112	NULL
113};
114
115/* these are for detailing the cpu states */
116int cpu_states[CPUSTATES];
117char *cpustatenames[] = {
118	"user", "nice", "system", "interrupt", "idle", NULL
119};
120
121/* these are for detailing the memory statistics */
122int memory_stats[8];
123char *memorynames[] = {
124	"Real: ", "K/", "K act/tot  ", "Free: ", "K  ",
125	"Swap: ", "K/", "K used/tot",
126	NULL
127};
128
129/* these are names given to allowed sorting orders -- first is default */
130char	*ordernames[] = {
131	"cpu", "size", "res", "time", "pri", NULL
132};
133
134/* these are for keeping track of the proc array */
135static int      nproc;
136static int      onproc = -1;
137static int      pref_len;
138static struct kinfo_proc *pbase;
139static struct kinfo_proc **pref;
140
141/* these are for getting the memory statistics */
142static int      pageshift;	/* log base 2 of the pagesize */
143
144/* define pagetok in terms of pageshift */
145#define pagetok(size) ((size) << pageshift)
146
147unsigned int	maxslp;
148
149int
150getstathz(void)
151{
152	struct clockinfo cinf;
153	size_t size = sizeof(cinf);
154	int mib[2];
155
156	mib[0] = CTL_KERN;
157	mib[1] = KERN_CLOCKRATE;
158	if (sysctl(mib, 2, &cinf, &size, NULL, 0) == -1)
159		return (-1);
160	return (cinf.stathz);
161}
162
163int
164machine_init(struct statics *statics)
165{
166	int pagesize;
167
168	stathz = getstathz();
169	if (stathz == -1)
170		return (-1);
171
172	pbase = NULL;
173	pref = NULL;
174	onproc = -1;
175	nproc = 0;
176
177	/*
178	 * get the page size with "getpagesize" and calculate pageshift from
179	 * it
180	 */
181	pagesize = getpagesize();
182	pageshift = 0;
183	while (pagesize > 1) {
184		pageshift++;
185		pagesize >>= 1;
186	}
187
188	/* we only need the amount of log(2)1024 for our conversion */
189	pageshift -= LOG1024;
190
191	/* fill in the statics information */
192	statics->procstate_names = procstatenames;
193	statics->cpustate_names = cpustatenames;
194	statics->memory_names = memorynames;
195	statics->order_names = ordernames;
196	return (0);
197}
198
199char *
200format_header(char *uname_field)
201{
202	char *ptr;
203
204	ptr = header + UNAME_START;
205	while (*uname_field != '\0')
206		*ptr++ = *uname_field++;
207	return (header);
208}
209
210void
211get_system_info(struct system_info *si)
212{
213	static int sysload_mib[] = {CTL_VM, VM_LOADAVG};
214	static int vmtotal_mib[] = {CTL_VM, VM_METER};
215	static int cp_time_mib[] = {CTL_KERN, KERN_CPTIME};
216	struct loadavg sysload;
217	struct vmtotal vmtotal;
218	double *infoloadp;
219	int total, i;
220	size_t size;
221
222	size = sizeof(cp_time);
223	if (sysctl(cp_time_mib, 2, &cp_time, &size, NULL, 0) < 0) {
224		warn("sysctl kern.cp_time failed");
225		total = 0;
226	}
227	size = sizeof(sysload);
228	if (sysctl(sysload_mib, 2, &sysload, &size, NULL, 0) < 0) {
229		warn("sysctl failed");
230		total = 0;
231	}
232	infoloadp = si->load_avg;
233	for (i = 0; i < 3; i++)
234		*infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale;
235
236	/* convert cp_time counts to percentages */
237	total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
238
239	/* get total -- systemwide main memory usage structure */
240	size = sizeof(vmtotal);
241	if (sysctl(vmtotal_mib, 2, &vmtotal, &size, NULL, 0) < 0) {
242		warn("sysctl failed");
243		bzero(&vmtotal, sizeof(vmtotal));
244	}
245	/* convert memory stats to Kbytes */
246	memory_stats[0] = -1;
247	memory_stats[1] = pagetok(vmtotal.t_arm);
248	memory_stats[2] = pagetok(vmtotal.t_rm);
249	memory_stats[3] = -1;
250	memory_stats[4] = pagetok(vmtotal.t_free);
251	memory_stats[5] = -1;
252
253	if (!swapmode(&memory_stats[6], &memory_stats[7])) {
254		memory_stats[6] = 0;
255		memory_stats[7] = 0;
256	}
257
258	/* set arrays and strings */
259	si->cpustates = cpu_states;
260	si->memory = memory_stats;
261	si->last_pid = -1;
262}
263
264static struct handle handle;
265
266struct kinfo_proc *
267getprocs(int op, int arg, int *cnt)
268{
269	size_t size = sizeof(int);
270	int mib[4] = {CTL_KERN, KERN_PROC, 0, 0};
271	int smib[2] = {CTL_KERN, KERN_NPROCS};
272	static int maxslp_mib[] = {CTL_VM, VM_MAXSLP};
273	static struct kinfo_proc *procbase;
274	int st;
275
276	mib[2] = op;
277	mib[3] = arg;
278
279	size = sizeof(maxslp);
280	if (sysctl(maxslp_mib, 2, &maxslp, &size, NULL, 0) < 0) {
281		warn("sysctl vm.maxslp failed");
282		return (0);
283	}
284	st = sysctl(smib, 2, cnt, &size, NULL, 0);
285	if (st == -1) {
286		/* _kvm_syserr(kd, kd->program, "kvm_getprocs"); */
287		return (0);
288	}
289	if (procbase)
290		free(procbase);
291	size = (6 * (*cnt) * sizeof(struct kinfo_proc)) / 5;
292	procbase = (struct kinfo_proc *) malloc(size);
293	if (procbase == NULL)
294		return (0);
295	st = sysctl(mib, 4, procbase, &size, NULL, 0);
296	if (st == -1) {
297		/* _kvm_syserr(kd, kd->program, "kvm_getprocs"); */
298		return (0);
299	}
300	if (size % sizeof(struct kinfo_proc) != 0) {
301		/*
302		 * _kvm_err(kd, kd->program, "proc size mismatch (%d total,
303		 * %d chunks)", size, sizeof(struct kinfo_proc));
304		 */
305		return (0);
306	}
307	return (procbase);
308}
309
310caddr_t
311get_process_info(struct system_info *si, struct process_select *sel,
312    int (*compare) (const void *, const void *))
313{
314	int show_idle, show_system, show_uid;
315	int total_procs, active_procs, i;
316	struct kinfo_proc **prefp, *pp;
317
318	if ((pbase = getprocs(KERN_PROC_KTHREAD, 0, &nproc)) == NULL) {
319		/* warnx("%s", kvm_geterr(kd)); */
320		quit(23);
321	}
322	if (nproc > onproc)
323		pref = (struct kinfo_proc **)realloc(pref,
324		    sizeof(struct kinfo_proc *) * (onproc = nproc));
325	if (pref == NULL) {
326		warnx("Out of memory.");
327		quit(23);
328	}
329	/* get a pointer to the states summary array */
330	si->procstates = process_states;
331
332	/* set up flags which define what we are going to select */
333	show_idle = sel->idle;
334	show_system = sel->system;
335	show_uid = sel->uid != (uid_t)-1;
336
337	/* count up process states and get pointers to interesting procs */
338	total_procs = 0;
339	active_procs = 0;
340	memset((char *) process_states, 0, sizeof(process_states));
341	prefp = pref;
342	for (pp = pbase, i = 0; i < nproc; pp++, i++) {
343		/*
344		 *  Place pointers to each valid proc structure in pref[].
345		 *  Process slots that are actually in use have a non-zero
346		 *  status field.  Processes with SSYS set are system
347		 *  processes---these get ignored unless show_sysprocs is set.
348		 */
349		if (PP(pp, p_stat) != 0 &&
350		    (show_system || ((PP(pp, p_flag) & P_SYSTEM) == 0))) {
351			total_procs++;
352			process_states[(unsigned char) PP(pp, p_stat)]++;
353			if ((PP(pp, p_stat) != SZOMB) &&
354			    (show_idle || (PP(pp, p_pctcpu) != 0) ||
355			    (PP(pp, p_stat) == SRUN)) &&
356			    (!show_uid || EP(pp, e_pcred.p_ruid) == sel->uid)) {
357				*prefp++ = pp;
358				active_procs++;
359			}
360		}
361	}
362
363	/* if requested, sort the "interesting" processes */
364	if (compare != NULL)
365		qsort((char *) pref, active_procs,
366		    sizeof(struct kinfo_proc *), compare);
367	/* remember active and total counts */
368	si->p_total = total_procs;
369	si->p_active = pref_len = active_procs;
370
371	/* pass back a handle */
372	handle.next_proc = pref;
373	handle.remaining = active_procs;
374	return ((caddr_t) & handle);
375}
376
377char fmt[MAX_COLS];	/* static area where result is built */
378
379char *
380format_next_process(caddr_t handle, char *(*get_userid)())
381{
382	char *p_wait, waddr[sizeof(void *) * 2 + 3];	/* Hexify void pointer */
383	struct kinfo_proc *pp;
384	struct handle *hp;
385	int cputime;
386	double pct;
387
388	/* find and remember the next proc structure */
389	hp = (struct handle *) handle;
390	pp = *(hp->next_proc++);
391	hp->remaining--;
392
393	/* get the process's user struct and set cputime */
394	if ((PP(pp, p_flag) & P_INMEM) == 0) {
395		/*
396		 * Print swapped processes as <pname>
397		 */
398		char *comm = PP(pp, p_comm);
399		char buf[sizeof(PP(pp, p_comm))];
400
401		(void) strlcpy(buf, comm, sizeof buf);
402		comm[0] = '<';
403		(void) strlcpy(&comm[1], buf, sizeof buf - 1);
404		(void) strlcat(comm, ">", sizeof buf);
405	}
406	cputime = (PP(pp, p_uticks) + PP(pp, p_sticks) + PP(pp, p_iticks)) / stathz;
407
408	/* calculate the base for cpu percentages */
409	pct = pctdouble(PP(pp, p_pctcpu));
410
411	if (PP(pp, p_wchan)) {
412		if (PP(pp, p_wmesg))
413			p_wait = EP(pp, e_wmesg);
414		else {
415			snprintf(waddr, sizeof(waddr), "%lx",
416			    (unsigned long) (PP(pp, p_wchan)) & ~KERNBASE);
417			p_wait = waddr;
418		}
419	} else
420		p_wait = "-";
421
422	/* format this entry */
423	snprintf(fmt, sizeof fmt, Proc_format,
424	    PP(pp, p_pid), (*get_userid) (EP(pp, e_pcred.p_ruid)),
425	    PP(pp, p_priority) - PZERO, PP(pp, p_nice) - NZERO,
426	    format_k(pagetok(PROCSIZE(pp))),
427	    format_k(pagetok(VP(pp, vm_rssize))),
428	    (PP(pp, p_stat) == SSLEEP && PP(pp, p_slptime) > maxslp) ?
429	    "idle" : state_abbrev[(unsigned char) PP(pp, p_stat)],
430	    p_wait, format_time(cputime), 100.0 * pct,
431	    printable(PP(pp, p_comm)));
432
433	/* return the result */
434	return (fmt);
435}
436
437/* comparison routine for qsort */
438static unsigned char sorted_state[] =
439{
440	0,			/* not used		 */
441	4,			/* start		 */
442	5,			/* run			 */
443	2,			/* sleep		 */
444	3,			/* stop			 */
445	1			/* zombie		 */
446};
447
448/*
449 *  proc_compares - comparison functions for "qsort"
450 */
451
452/*
453 * First, the possible comparison keys.  These are defined in such a way
454 * that they can be merely listed in the source code to define the actual
455 * desired ordering.
456 */
457
458#define ORDERKEY_PCTCPU \
459	if (lresult = (pctcpu)PP(p2, p_pctcpu) - (pctcpu)PP(p1, p_pctcpu), \
460	    (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0)
461#define ORDERKEY_CPUTIME \
462	if ((result = PP(p2, p_rtime.tv_sec) - PP(p1, p_rtime.tv_sec)) == 0) \
463		if ((result = PP(p2, p_rtime.tv_usec) - \
464		     PP(p1, p_rtime.tv_usec)) == 0)
465#define ORDERKEY_STATE \
466	if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] - \
467	    sorted_state[(unsigned char) PP(p1, p_stat)])  == 0)
468#define ORDERKEY_PRIO \
469	if ((result = PP(p2, p_priority) - PP(p1, p_priority)) == 0)
470#define ORDERKEY_RSSIZE \
471	if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0)
472#define ORDERKEY_MEM \
473	if ((result = PROCSIZE(p2) - PROCSIZE(p1)) == 0)
474
475/* compare_cpu - the comparison function for sorting by cpu percentage */
476int
477compare_cpu(const void *v1, const void *v2)
478{
479	struct proc **pp1 = (struct proc **) v1;
480	struct proc **pp2 = (struct proc **) v2;
481	struct kinfo_proc *p1, *p2;
482	pctcpu lresult;
483	int result;
484
485	/* remove one level of indirection */
486	p1 = *(struct kinfo_proc **) pp1;
487	p2 = *(struct kinfo_proc **) pp2;
488
489	ORDERKEY_PCTCPU
490	ORDERKEY_CPUTIME
491	ORDERKEY_STATE
492	ORDERKEY_PRIO
493	ORDERKEY_RSSIZE
494	ORDERKEY_MEM
495		;
496	return (result);
497}
498
499/* compare_size - the comparison function for sorting by total memory usage */
500int
501compare_size(const void *v1, const void *v2)
502{
503	struct proc **pp1 = (struct proc **) v1;
504	struct proc **pp2 = (struct proc **) v2;
505	struct kinfo_proc *p1, *p2;
506	pctcpu lresult;
507	int result;
508
509	/* remove one level of indirection */
510	p1 = *(struct kinfo_proc **) pp1;
511	p2 = *(struct kinfo_proc **) pp2;
512
513	ORDERKEY_MEM
514	ORDERKEY_RSSIZE
515	ORDERKEY_PCTCPU
516	ORDERKEY_CPUTIME
517	ORDERKEY_STATE
518	ORDERKEY_PRIO
519		;
520	return (result);
521}
522
523/* compare_res - the comparison function for sorting by resident set size */
524int
525compare_res(const void *v1, const void *v2)
526{
527	struct proc **pp1 = (struct proc **) v1;
528	struct proc **pp2 = (struct proc **) v2;
529	struct kinfo_proc *p1, *p2;
530	pctcpu lresult;
531	int result;
532
533	/* remove one level of indirection */
534	p1 = *(struct kinfo_proc **) pp1;
535	p2 = *(struct kinfo_proc **) pp2;
536
537	ORDERKEY_RSSIZE
538	ORDERKEY_MEM
539	ORDERKEY_PCTCPU
540	ORDERKEY_CPUTIME
541	ORDERKEY_STATE
542	ORDERKEY_PRIO
543		;
544	return (result);
545}
546
547/* compare_time - the comparison function for sorting by CPU time */
548int
549compare_time(const void *v1, const void *v2)
550{
551	struct proc **pp1 = (struct proc **) v1;
552	struct proc **pp2 = (struct proc **) v2;
553	struct kinfo_proc *p1, *p2;
554	pctcpu lresult;
555	int result;
556
557	/* remove one level of indirection */
558	p1 = *(struct kinfo_proc **) pp1;
559	p2 = *(struct kinfo_proc **) pp2;
560
561	ORDERKEY_CPUTIME
562	ORDERKEY_PCTCPU
563	ORDERKEY_STATE
564	ORDERKEY_PRIO
565	ORDERKEY_MEM
566	ORDERKEY_RSSIZE
567		;
568	return (result);
569}
570
571/* compare_prio - the comparison function for sorting by CPU time */
572int
573compare_prio(const void *v1, const void *v2)
574{
575	struct proc   **pp1 = (struct proc **) v1;
576	struct proc   **pp2 = (struct proc **) v2;
577	struct kinfo_proc *p1, *p2;
578	pctcpu lresult;
579	int result;
580
581	/* remove one level of indirection */
582	p1 = *(struct kinfo_proc **) pp1;
583	p2 = *(struct kinfo_proc **) pp2;
584
585	ORDERKEY_PRIO
586	ORDERKEY_PCTCPU
587	ORDERKEY_CPUTIME
588	ORDERKEY_STATE
589	ORDERKEY_RSSIZE
590	ORDERKEY_MEM
591		;
592	return (result);
593}
594
595int (*proc_compares[])(const void *, const void *) = {
596	compare_cpu,
597	compare_size,
598	compare_res,
599	compare_time,
600	compare_prio,
601	NULL
602};
603
604/*
605 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
606 *		the process does not exist.
607 *		It is EXTREMLY IMPORTANT that this function work correctly.
608 *		If top runs setuid root (as in SVR4), then this function
609 *		is the only thing that stands in the way of a serious
610 *		security problem.  It validates requests for the "kill"
611 *		and "renice" commands.
612 */
613uid_t
614proc_owner(pid_t pid)
615{
616	struct kinfo_proc **prefp, *pp;
617	int cnt;
618
619	prefp = pref;
620	cnt = pref_len;
621	while (--cnt >= 0) {
622		pp = *prefp++;
623		if (PP(pp, p_pid) == pid)
624			return ((int) EP(pp, e_pcred.p_ruid));
625	}
626	return (-1);
627}
628
629/*
630 * swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
631 * to be based on the new swapctl(2) system call.
632 */
633static int
634swapmode(int *used, int *total)
635{
636	struct swapent *swdev;
637	int nswap, rnswap, i;
638
639	nswap = swapctl(SWAP_NSWAP, 0, 0);
640	if (nswap == 0)
641		return 0;
642
643	swdev = malloc(nswap * sizeof(*swdev));
644	if (swdev == NULL)
645		return 0;
646
647	rnswap = swapctl(SWAP_STATS, swdev, nswap);
648	if (rnswap == -1)
649		return 0;
650
651	/* if rnswap != nswap, then what? */
652
653	/* Total things up */
654	*total = *used = 0;
655	for (i = 0; i < nswap; i++) {
656		if (swdev[i].se_flags & SWF_ENABLE) {
657			*used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
658			*total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
659		}
660	}
661	free(swdev);
662	return 1;
663}
664