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