1 /* $NetBSD: nlist.c,v 1.26 2008/04/28 20:22:51 martin Exp $ */
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1990, 1993, 1994
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #include <sys/cdefs.h>
64 static char sccsid
[] = "@(#)nlist.c 8.4 (Berkeley) 4/2/94";
66 __RCSID("$NetBSD: nlist.c,v 1.26 2008/04/28 20:22:51 martin Exp $");
70 #include <sys/param.h>
74 #include <sys/resource.h>
75 #include <sys/sysctl.h>
88 struct nlist psnl
[] = {
89 { .n_name
= "_fscale" },
91 { .n_name
= "_ccpu" },
93 { .n_name
= "_physmem" },
95 { .n_name
= "_maxslp" },
100 double ccpu
; /* kernel _ccpu variable */
101 int nlistread
; /* if nlist already read. */
102 int mempages
; /* number of pages of phys. memory */
103 int fscale
; /* kernel _fscale variable */
104 int maxslp
; /* kernel _maxslp variable */
105 int uspace
; /* kernel USPACE value */
107 #define kread(x, v) \
108 kvm_read(kd, psnl[x].n_value, (char *)&v, sizeof v) != sizeof(v)
118 if (kvm_nlist(kd
, psnl
)) {
123 if (kread(X_FSCALE
, fscale
)) {
124 warnx("fscale: %s", kvm_geterr(kd
));
127 if (kread(X_PHYSMEM
, mempages
)) {
128 warnx("avail_start: %s", kvm_geterr(kd
));
131 if (kread(X_CCPU
, xccpu
)) {
132 warnx("ccpu: %s", kvm_geterr(kd
));
135 if (kread(X_MAXSLP
, maxslp
)) {
136 warnx("maxslp: %s", kvm_geterr(kd
));
139 ccpu
= (double)xccpu
/ fscale
;
153 mib
[1] = HW_PHYSMEM64
;
154 size
= sizeof(memsize
);
155 if (sysctl(mib
, 2, &memsize
, &size
, NULL
, 0) == 0)
156 mempages
= memsize
/ getpagesize();
161 mib
[1] = KERN_FSCALE
;
162 size
= sizeof(fscale
);
163 if (sysctl(mib
, 2, &fscale
, &size
, NULL
, 0) == -1)
164 fscale
= (1 << 8); /* XXX Hopefully reasonable default */
168 size
= sizeof(xccpu
);
169 if (sysctl(mib
, 2, &xccpu
, &size
, NULL
, 0) == -1)
170 ccpu
= exp(-1.0 / 20.0); /* XXX Hopefully reasonable default */
172 ccpu
= (double)xccpu
/ fscale
;
176 size
= sizeof(maxslp
);
177 if (sysctl(mib
, 2, &maxslp
, &size
, NULL
, 0) == -1)
181 maxslp
= 20; /* XXX Hopefully reasonable default */
186 size
= sizeof(uspace
);
187 if (sysctl(mib
, 2, &uspace
, &size
, NULL
, 0) == -1)
191 uspace
= getpagesize(); /* XXX Hopefully reasonable default */
198 nlisterr(struct nlist nl
[])
202 (void)fprintf(stderr
, "ps: nlist: can't find following symbols:");
203 for (i
= 0; nl
[i
].n_name
!= NULL
; i
++)
204 if (nl
[i
].n_value
== 0)
205 (void)fprintf(stderr
, " %s", nl
[i
].n_name
);
206 (void)fprintf(stderr
, "\n");