machine.c revision 1.36
1/* $OpenBSD: machine.c,v 1.36 2003/07/07 21:36:52 deraadt 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
149static int
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	size_t size;
220	int i;
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
226	size = sizeof(sysload);
227	if (sysctl(sysload_mib, 2, &sysload, &size, NULL, 0) < 0)
228		warn("sysctl failed");
229	infoloadp = si->load_avg;
230	for (i = 0; i < 3; i++)
231		*infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale;
232
233	/* convert cp_time counts to percentages */
234	(void) percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
235
236	/* get total -- systemwide main memory usage structure */
237	size = sizeof(vmtotal);
238	if (sysctl(vmtotal_mib, 2, &vmtotal, &size, NULL, 0) < 0) {
239		warn("sysctl failed");
240		bzero(&vmtotal, sizeof(vmtotal));
241	}
242	/* convert memory stats to Kbytes */
243	memory_stats[0] = -1;
244	memory_stats[1] = pagetok(vmtotal.t_arm);
245	memory_stats[2] = pagetok(vmtotal.t_rm);
246	memory_stats[3] = -1;
247	memory_stats[4] = pagetok(vmtotal.t_free);
248	memory_stats[5] = -1;
249
250	if (!swapmode(&memory_stats[6], &memory_stats[7])) {
251		memory_stats[6] = 0;
252		memory_stats[7] = 0;
253	}
254
255	/* set arrays and strings */
256	si->cpustates = cpu_states;
257	si->memory = memory_stats;
258	si->last_pid = -1;
259}
260
261static struct handle handle;
262
263static struct kinfo_proc *
264getprocs(int op, int arg, int *cnt)
265{
266	size_t size = sizeof(int);
267	int mib[4] = {CTL_KERN, KERN_PROC, 0, 0};
268	int smib[2] = {CTL_KERN, KERN_NPROCS};
269	static int maxslp_mib[] = {CTL_VM, VM_MAXSLP};
270	static struct kinfo_proc *procbase;
271	int st;
272
273	mib[2] = op;
274	mib[3] = arg;
275
276	size = sizeof(maxslp);
277	if (sysctl(maxslp_mib, 2, &maxslp, &size, NULL, 0) < 0) {
278		warn("sysctl vm.maxslp failed");
279		return (0);
280	}
281	st = sysctl(smib, 2, cnt, &size, NULL, 0);
282	if (st == -1) {
283		/* _kvm_syserr(kd, kd->program, "kvm_getprocs"); */
284		return (0);
285	}
286	if (procbase)
287		free(procbase);
288	size = (6 * (*cnt) * sizeof(struct kinfo_proc)) / 5;
289	procbase = (struct kinfo_proc *) malloc(size);
290	if (procbase == NULL)
291		return (0);
292	st = sysctl(mib, 4, procbase, &size, NULL, 0);
293	if (st == -1) {
294		/* _kvm_syserr(kd, kd->program, "kvm_getprocs"); */
295		return (0);
296	}
297	if (size % sizeof(struct kinfo_proc) != 0) {
298		/*
299		 * _kvm_err(kd, kd->program, "proc size mismatch (%d total,
300		 * %d chunks)", size, sizeof(struct kinfo_proc));
301		 */
302		return (0);
303	}
304	return (procbase);
305}
306
307caddr_t
308get_process_info(struct system_info *si, struct process_select *sel,
309    int (*compare) (const void *, const void *))
310{
311	int show_idle, show_system, show_uid;
312	int total_procs, active_procs, i;
313	struct kinfo_proc **prefp, *pp;
314
315	if ((pbase = getprocs(KERN_PROC_KTHREAD, 0, &nproc)) == NULL) {
316		/* warnx("%s", kvm_geterr(kd)); */
317		quit(23);
318	}
319	if (nproc > onproc)
320		pref = (struct kinfo_proc **)realloc(pref,
321		    sizeof(struct kinfo_proc *) * (onproc = nproc));
322	if (pref == NULL) {
323		warnx("Out of memory.");
324		quit(23);
325	}
326	/* get a pointer to the states summary array */
327	si->procstates = process_states;
328
329	/* set up flags which define what we are going to select */
330	show_idle = sel->idle;
331	show_system = sel->system;
332	show_uid = sel->uid != (uid_t)-1;
333
334	/* count up process states and get pointers to interesting procs */
335	total_procs = 0;
336	active_procs = 0;
337	memset((char *) process_states, 0, sizeof(process_states));
338	prefp = pref;
339	for (pp = pbase, i = 0; i < nproc; pp++, i++) {
340		/*
341		 *  Place pointers to each valid proc structure in pref[].
342		 *  Process slots that are actually in use have a non-zero
343		 *  status field.  Processes with SSYS set are system
344		 *  processes---these get ignored unless show_sysprocs is set.
345		 */
346		if (PP(pp, p_stat) != 0 &&
347		    (show_system || ((PP(pp, p_flag) & P_SYSTEM) == 0))) {
348			total_procs++;
349			process_states[(unsigned char) PP(pp, p_stat)]++;
350			if ((PP(pp, p_stat) != SZOMB) &&
351			    (show_idle || (PP(pp, p_pctcpu) != 0) ||
352			    (PP(pp, p_stat) == SRUN)) &&
353			    (!show_uid || EP(pp, e_pcred.p_ruid) == sel->uid)) {
354				*prefp++ = pp;
355				active_procs++;
356			}
357		}
358	}
359
360	/* if requested, sort the "interesting" processes */
361	if (compare != NULL)
362		qsort((char *) pref, active_procs,
363		    sizeof(struct kinfo_proc *), compare);
364	/* remember active and total counts */
365	si->p_total = total_procs;
366	si->p_active = pref_len = active_procs;
367
368	/* pass back a handle */
369	handle.next_proc = pref;
370	handle.remaining = active_procs;
371	return ((caddr_t) & handle);
372}
373
374char fmt[MAX_COLS];	/* static area where result is built */
375
376char *
377format_next_process(caddr_t handle, char *(*get_userid)(uid_t))
378{
379	char *p_wait, waddr[sizeof(void *) * 2 + 3];	/* Hexify void pointer */
380	struct kinfo_proc *pp;
381	struct handle *hp;
382	int cputime;
383	double pct;
384
385	/* find and remember the next proc structure */
386	hp = (struct handle *) handle;
387	pp = *(hp->next_proc++);
388	hp->remaining--;
389
390	/* get the process's user struct and set cputime */
391	if ((PP(pp, p_flag) & P_INMEM) == 0) {
392		/*
393		 * Print swapped processes as <pname>
394		 */
395		char *comm = PP(pp, p_comm);
396		char buf[sizeof(PP(pp, p_comm))];
397
398		(void) strlcpy(buf, comm, sizeof buf);
399		comm[0] = '<';
400		(void) strlcpy(&comm[1], buf, sizeof buf - 1);
401		(void) strlcat(comm, ">", sizeof buf);
402	}
403	cputime = (PP(pp, p_uticks) + PP(pp, p_sticks) + PP(pp, p_iticks)) / stathz;
404
405	/* calculate the base for cpu percentages */
406	pct = pctdouble(PP(pp, p_pctcpu));
407
408	if (PP(pp, p_wchan)) {
409		if (PP(pp, p_wmesg))
410			p_wait = EP(pp, e_wmesg);
411		else {
412			snprintf(waddr, sizeof(waddr), "%lx",
413			    (unsigned long) (PP(pp, p_wchan)) & ~KERNBASE);
414			p_wait = waddr;
415		}
416	} else
417		p_wait = "-";
418
419	/* format this entry */
420	snprintf(fmt, sizeof fmt, Proc_format,
421	    PP(pp, p_pid), (*get_userid) (EP(pp, e_pcred.p_ruid)),
422	    PP(pp, p_priority) - PZERO, PP(pp, p_nice) - NZERO,
423	    format_k(pagetok(PROCSIZE(pp))),
424	    format_k(pagetok(VP(pp, vm_rssize))),
425	    (PP(pp, p_stat) == SSLEEP && PP(pp, p_slptime) > maxslp) ?
426	    "idle" : state_abbrev[(unsigned char) PP(pp, p_stat)],
427	    p_wait, format_time(cputime), 100.0 * pct,
428	    printable(PP(pp, p_comm)));
429
430	/* return the result */
431	return (fmt);
432}
433
434/* comparison routine for qsort */
435static unsigned char sorted_state[] =
436{
437	0,			/* not used		 */
438	4,			/* start		 */
439	5,			/* run			 */
440	2,			/* sleep		 */
441	3,			/* stop			 */
442	1			/* zombie		 */
443};
444
445/*
446 *  proc_compares - comparison functions for "qsort"
447 */
448
449/*
450 * First, the possible comparison keys.  These are defined in such a way
451 * that they can be merely listed in the source code to define the actual
452 * desired ordering.
453 */
454
455#define ORDERKEY_PCTCPU \
456	if (lresult = (pctcpu)PP(p2, p_pctcpu) - (pctcpu)PP(p1, p_pctcpu), \
457	    (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0)
458#define ORDERKEY_CPUTIME \
459	if ((result = PP(p2, p_rtime.tv_sec) - PP(p1, p_rtime.tv_sec)) == 0) \
460		if ((result = PP(p2, p_rtime.tv_usec) - \
461		     PP(p1, p_rtime.tv_usec)) == 0)
462#define ORDERKEY_STATE \
463	if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] - \
464	    sorted_state[(unsigned char) PP(p1, p_stat)])  == 0)
465#define ORDERKEY_PRIO \
466	if ((result = PP(p2, p_priority) - PP(p1, p_priority)) == 0)
467#define ORDERKEY_RSSIZE \
468	if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0)
469#define ORDERKEY_MEM \
470	if ((result = PROCSIZE(p2) - PROCSIZE(p1)) == 0)
471
472/* compare_cpu - the comparison function for sorting by cpu percentage */
473static int
474compare_cpu(const void *v1, const void *v2)
475{
476	struct proc **pp1 = (struct proc **) v1;
477	struct proc **pp2 = (struct proc **) v2;
478	struct kinfo_proc *p1, *p2;
479	pctcpu lresult;
480	int result;
481
482	/* remove one level of indirection */
483	p1 = *(struct kinfo_proc **) pp1;
484	p2 = *(struct kinfo_proc **) pp2;
485
486	ORDERKEY_PCTCPU
487	ORDERKEY_CPUTIME
488	ORDERKEY_STATE
489	ORDERKEY_PRIO
490	ORDERKEY_RSSIZE
491	ORDERKEY_MEM
492		;
493	return (result);
494}
495
496/* compare_size - the comparison function for sorting by total memory usage */
497static int
498compare_size(const void *v1, const void *v2)
499{
500	struct proc **pp1 = (struct proc **) v1;
501	struct proc **pp2 = (struct proc **) v2;
502	struct kinfo_proc *p1, *p2;
503	pctcpu lresult;
504	int result;
505
506	/* remove one level of indirection */
507	p1 = *(struct kinfo_proc **) pp1;
508	p2 = *(struct kinfo_proc **) pp2;
509
510	ORDERKEY_MEM
511	ORDERKEY_RSSIZE
512	ORDERKEY_PCTCPU
513	ORDERKEY_CPUTIME
514	ORDERKEY_STATE
515	ORDERKEY_PRIO
516		;
517	return (result);
518}
519
520/* compare_res - the comparison function for sorting by resident set size */
521static int
522compare_res(const void *v1, const void *v2)
523{
524	struct proc **pp1 = (struct proc **) v1;
525	struct proc **pp2 = (struct proc **) v2;
526	struct kinfo_proc *p1, *p2;
527	pctcpu lresult;
528	int result;
529
530	/* remove one level of indirection */
531	p1 = *(struct kinfo_proc **) pp1;
532	p2 = *(struct kinfo_proc **) pp2;
533
534	ORDERKEY_RSSIZE
535	ORDERKEY_MEM
536	ORDERKEY_PCTCPU
537	ORDERKEY_CPUTIME
538	ORDERKEY_STATE
539	ORDERKEY_PRIO
540		;
541	return (result);
542}
543
544/* compare_time - the comparison function for sorting by CPU time */
545static int
546compare_time(const void *v1, const void *v2)
547{
548	struct proc **pp1 = (struct proc **) v1;
549	struct proc **pp2 = (struct proc **) v2;
550	struct kinfo_proc *p1, *p2;
551	pctcpu lresult;
552	int result;
553
554	/* remove one level of indirection */
555	p1 = *(struct kinfo_proc **) pp1;
556	p2 = *(struct kinfo_proc **) pp2;
557
558	ORDERKEY_CPUTIME
559	ORDERKEY_PCTCPU
560	ORDERKEY_STATE
561	ORDERKEY_PRIO
562	ORDERKEY_MEM
563	ORDERKEY_RSSIZE
564		;
565	return (result);
566}
567
568/* compare_prio - the comparison function for sorting by CPU time */
569static int
570compare_prio(const void *v1, const void *v2)
571{
572	struct proc   **pp1 = (struct proc **) v1;
573	struct proc   **pp2 = (struct proc **) v2;
574	struct kinfo_proc *p1, *p2;
575	pctcpu lresult;
576	int result;
577
578	/* remove one level of indirection */
579	p1 = *(struct kinfo_proc **) pp1;
580	p2 = *(struct kinfo_proc **) pp2;
581
582	ORDERKEY_PRIO
583	ORDERKEY_PCTCPU
584	ORDERKEY_CPUTIME
585	ORDERKEY_STATE
586	ORDERKEY_RSSIZE
587	ORDERKEY_MEM
588		;
589	return (result);
590}
591
592int (*proc_compares[])(const void *, const void *) = {
593	compare_cpu,
594	compare_size,
595	compare_res,
596	compare_time,
597	compare_prio,
598	NULL
599};
600
601/*
602 * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
603 *		the process does not exist.
604 *		It is EXTREMLY IMPORTANT that this function work correctly.
605 *		If top runs setuid root (as in SVR4), then this function
606 *		is the only thing that stands in the way of a serious
607 *		security problem.  It validates requests for the "kill"
608 *		and "renice" commands.
609 */
610uid_t
611proc_owner(pid_t pid)
612{
613	struct kinfo_proc **prefp, *pp;
614	int cnt;
615
616	prefp = pref;
617	cnt = pref_len;
618	while (--cnt >= 0) {
619		pp = *prefp++;
620		if (PP(pp, p_pid) == pid)
621			return ((uid_t) EP(pp, e_pcred.p_ruid));
622	}
623	return (uid_t)(-1);
624}
625
626/*
627 * swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
628 * to be based on the new swapctl(2) system call.
629 */
630static int
631swapmode(int *used, int *total)
632{
633	struct swapent *swdev;
634	int nswap, rnswap, i;
635
636	nswap = swapctl(SWAP_NSWAP, 0, 0);
637	if (nswap == 0)
638		return 0;
639
640	swdev = malloc(nswap * sizeof(*swdev));
641	if (swdev == NULL)
642		return 0;
643
644	rnswap = swapctl(SWAP_STATS, swdev, nswap);
645	if (rnswap == -1)
646		return 0;
647
648	/* if rnswap != nswap, then what? */
649
650	/* Total things up */
651	*total = *used = 0;
652	for (i = 0; i < nswap; i++) {
653		if (swdev[i].se_flags & SWF_ENABLE) {
654			*used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
655			*total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
656		}
657	}
658	free(swdev);
659	return 1;
660}
661