Home | History | Annotate | Line # | Download | only in ps
nlist.c revision 1.17
      1 /*	$NetBSD: nlist.c,v 1.17 2000/06/08 13:30:39 simonb 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.17 2000/06/08 13:30:39 simonb 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 	{ NULL }
    106 };
    107 
    108 double	ccpu;				/* kernel _ccpu variable */
    109 int	nlistread;			/* if nlist already read. */
    110 int	mempages;			/* number of pages of phys. memory */
    111 int	fscale;				/* kernel _fscale variable */
    112 
    113 #define kread(x, v) \
    114 	kvm_read(kd, psnl[x].n_value, (char *)&v, sizeof v) != sizeof(v)
    115 
    116 int
    117 donlist()
    118 {
    119 	int rval;
    120 	fixpt_t xccpu;
    121 
    122 	rval = 0;
    123 	nlistread = 1;
    124 	if (kvm_nlist(kd, psnl)) {
    125 		nlisterr(psnl);
    126 		eval = 1;
    127 		return (1);
    128 	}
    129 	if (kread(X_FSCALE, fscale)) {
    130 		warnx("fscale: %s", kvm_geterr(kd));
    131 		eval = rval = 1;
    132 	}
    133 	if (kread(X_PHYSMEM, mempages)) {
    134 		warnx("avail_start: %s", kvm_geterr(kd));
    135 		eval = rval = 1;
    136 	}
    137 	if (kread(X_CCPU, xccpu)) {
    138 		warnx("ccpu: %s", kvm_geterr(kd));
    139 		eval = rval = 1;
    140 	}
    141 	ccpu = (double)xccpu / fscale;
    142 	return (rval);
    143 }
    144 
    145 int
    146 donlist_sysctl()
    147 {
    148 	int mib[2];
    149 	size_t size;
    150 	fixpt_t xccpu;
    151 
    152 	nlistread = 1;
    153 	mib[0] = CTL_HW;
    154 	mib[1] = HW_PHYSMEM;
    155 	size = sizeof(mempages);
    156 	if (sysctl(mib, 2, &mempages, &size, NULL, 0) == 0)
    157 		mempages /= getpagesize();
    158 	else
    159 		mempages = 0;
    160 
    161 	mib[0] = CTL_KERN;
    162 	mib[1] = KERN_FSCALE;
    163 	size = sizeof(fscale);
    164 	if (sysctl(mib, 2, &fscale, &size, NULL, 0) == -1)
    165 		fscale = (1 << 8);	/* XXX Hopefully reasonable default */
    166 
    167 	mib[0] = CTL_KERN;
    168 	mib[1] = KERN_CCPU;
    169 	size = sizeof(xccpu);
    170 	if (sysctl(mib, 2, &xccpu, &size, NULL, 0) == -1)
    171 		ccpu = exp(-1.0 / 20.0); /* XXX Hopefully reasonable default */
    172 	else
    173 		ccpu = (double)xccpu / fscale;
    174 
    175 	return 0;
    176 }
    177 
    178 void
    179 nlisterr(nl)
    180 	struct nlist nl[];
    181 {
    182 	int i;
    183 
    184 	(void)fprintf(stderr, "ps: nlist: can't find following symbols:");
    185 	for (i = 0; nl[i].n_name != NULL; i++)
    186 		if (nl[i].n_value == 0)
    187 			(void)fprintf(stderr, " %s", nl[i].n_name);
    188 	(void)fprintf(stderr, "\n");
    189 }
    190