Home | History | Annotate | Line # | Download | only in ps
nlist.c revision 1.18
      1 /*	$NetBSD: nlist.c,v 1.18 2001/07/14 06:53:44 matt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Simon Burge.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1990, 1993, 1994
     41  *	The Regents of the University of California.  All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the University of
     54  *	California, Berkeley and its contributors.
     55  * 4. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  */
     71 
     72 #include <sys/cdefs.h>
     73 #ifndef lint
     74 #if 0
     75 static char sccsid[] = "@(#)nlist.c	8.4 (Berkeley) 4/2/94";
     76 #else
     77 __RCSID("$NetBSD: nlist.c,v 1.18 2001/07/14 06:53:44 matt Exp $");
     78 #endif
     79 #endif /* not lint */
     80 
     81 #include <sys/param.h>
     82 #include <sys/time.h>
     83 #include <sys/proc.h>
     84 #include <sys/resource.h>
     85 #include <sys/sysctl.h>
     86 
     87 #include <err.h>
     88 #include <errno.h>
     89 #include <kvm.h>
     90 #include <math.h>
     91 #include <nlist.h>
     92 #include <stdio.h>
     93 #include <string.h>
     94 #include <unistd.h>
     95 
     96 #include "ps.h"
     97 
     98 struct	nlist psnl[] = {
     99 	{ "_fscale" },
    100 #define	X_FSCALE	0
    101 	{ "_ccpu" },
    102 #define	X_CCPU		1
    103 	{ "_physmem" },
    104 #define	X_PHYSMEM	2
    105 	{ "_maxslp" },
    106 #define	X_MAXSLP	3
    107 	{ NULL }
    108 };
    109 
    110 double	ccpu;				/* kernel _ccpu variable */
    111 int	nlistread;			/* if nlist already read. */
    112 int	mempages;			/* number of pages of phys. memory */
    113 int	fscale;				/* kernel _fscale variable */
    114 int	maxslp;				/* kernel _maxslp variable */
    115 int	uspace;				/* kernel USPACE value */
    116 
    117 #define kread(x, v) \
    118 	kvm_read(kd, psnl[x].n_value, (char *)&v, sizeof v) != sizeof(v)
    119 
    120 int
    121 donlist()
    122 {
    123 	int rval;
    124 	fixpt_t xccpu;
    125 
    126 	rval = 0;
    127 	nlistread = 1;
    128 	if (kvm_nlist(kd, psnl)) {
    129 		nlisterr(psnl);
    130 		eval = 1;
    131 		return (1);
    132 	}
    133 	if (kread(X_FSCALE, fscale)) {
    134 		warnx("fscale: %s", kvm_geterr(kd));
    135 		eval = rval = 1;
    136 	}
    137 	if (kread(X_PHYSMEM, mempages)) {
    138 		warnx("avail_start: %s", kvm_geterr(kd));
    139 		eval = rval = 1;
    140 	}
    141 	if (kread(X_CCPU, xccpu)) {
    142 		warnx("ccpu: %s", kvm_geterr(kd));
    143 		eval = rval = 1;
    144 	}
    145 	if (kread(X_MAXSLP, maxslp)) {
    146 		warnx("maxslp: %s", kvm_geterr(kd));
    147 		eval = rval = 1;
    148 	}
    149 	ccpu = (double)xccpu / fscale;
    150 	return (rval);
    151 }
    152 
    153 int
    154 donlist_sysctl()
    155 {
    156 	int mib[2];
    157 	size_t size;
    158 	fixpt_t xccpu;
    159 
    160 	nlistread = 1;
    161 	mib[0] = CTL_HW;
    162 	mib[1] = HW_PHYSMEM;
    163 	size = sizeof(mempages);
    164 	if (sysctl(mib, 2, &mempages, &size, NULL, 0) == 0)
    165 		mempages /= getpagesize();
    166 	else
    167 		mempages = 0;
    168 
    169 	mib[0] = CTL_KERN;
    170 	mib[1] = KERN_FSCALE;
    171 	size = sizeof(fscale);
    172 	if (sysctl(mib, 2, &fscale, &size, NULL, 0) == -1)
    173 		fscale = (1 << 8);	/* XXX Hopefully reasonable default */
    174 
    175 	mib[0] = CTL_KERN;
    176 	mib[1] = KERN_CCPU;
    177 	size = sizeof(xccpu);
    178 	if (sysctl(mib, 2, &xccpu, &size, NULL, 0) == -1)
    179 		ccpu = exp(-1.0 / 20.0); /* XXX Hopefully reasonable default */
    180 	else
    181 		ccpu = (double)xccpu / fscale;
    182 
    183 	mib[0] = CTL_VM;
    184 	mib[1] = VM_MAXSLP;
    185 	size = sizeof(maxslp);
    186 	if (sysctl(mib, 2, &maxslp, &size, NULL, 0) == -1)
    187 #ifdef MAXSLP
    188 		maxslp = MAXSLP;
    189 #else
    190 		maxslp = 20;		/* XXX Hopefully reasonable default */
    191 #endif
    192 
    193 	mib[0] = CTL_VM;
    194 	mib[1] = VM_USPACE;
    195 	size = sizeof(maxslp);
    196 	if (sysctl(mib, 2, &maxslp, &size, NULL, 0) == -1)
    197 #ifdef USPACE
    198 		uspace = USPACE;
    199 #else
    200 		uspace = getpagesize();	/* XXX Hopefully reasonable default */
    201 #endif
    202 
    203 	return 0;
    204 }
    205 
    206 void
    207 nlisterr(nl)
    208 	struct nlist nl[];
    209 {
    210 	int i;
    211 
    212 	(void)fprintf(stderr, "ps: nlist: can't find following symbols:");
    213 	for (i = 0; nl[i].n_name != NULL; i++)
    214 		if (nl[i].n_value == 0)
    215 			(void)fprintf(stderr, " %s", nl[i].n_name);
    216 	(void)fprintf(stderr, "\n");
    217 }
    218