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