nlist.c revision 1.22 1 /* $NetBSD: nlist.c,v 1.22 2004/03/27 12:09:28 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. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 */
67
68 #include <sys/cdefs.h>
69 #ifndef lint
70 #if 0
71 static char sccsid[] = "@(#)nlist.c 8.4 (Berkeley) 4/2/94";
72 #else
73 __RCSID("$NetBSD: nlist.c,v 1.22 2004/03/27 12:09:28 simonb Exp $");
74 #endif
75 #endif /* not lint */
76
77 #include <sys/param.h>
78 #include <sys/time.h>
79 #include <sys/lwp.h>
80 #include <sys/proc.h>
81 #include <sys/resource.h>
82 #include <sys/sysctl.h>
83
84 #include <err.h>
85 #include <errno.h>
86 #include <kvm.h>
87 #include <math.h>
88 #include <nlist.h>
89 #include <stdio.h>
90 #include <string.h>
91 #include <unistd.h>
92
93 #include "ps.h"
94
95 struct nlist psnl[] = {
96 { "_fscale" },
97 #define X_FSCALE 0
98 { "_ccpu" },
99 #define X_CCPU 1
100 { "_physmem" },
101 #define X_PHYSMEM 2
102 { "_maxslp" },
103 #define X_MAXSLP 3
104 { NULL }
105 };
106
107 double ccpu; /* kernel _ccpu variable */
108 int nlistread; /* if nlist already read. */
109 int mempages; /* number of pages of phys. memory */
110 int fscale; /* kernel _fscale variable */
111 int maxslp; /* kernel _maxslp variable */
112 int uspace; /* kernel USPACE value */
113
114 #define kread(x, v) \
115 kvm_read(kd, psnl[x].n_value, (char *)&v, sizeof v) != sizeof(v)
116
117 int
118 donlist(void)
119 {
120 int rval;
121 fixpt_t xccpu;
122
123 rval = 0;
124 nlistread = 1;
125 if (kvm_nlist(kd, psnl)) {
126 nlisterr(psnl);
127 eval = 1;
128 return (1);
129 }
130 if (kread(X_FSCALE, fscale)) {
131 warnx("fscale: %s", kvm_geterr(kd));
132 eval = rval = 1;
133 }
134 if (kread(X_PHYSMEM, mempages)) {
135 warnx("avail_start: %s", kvm_geterr(kd));
136 eval = rval = 1;
137 }
138 if (kread(X_CCPU, xccpu)) {
139 warnx("ccpu: %s", kvm_geterr(kd));
140 eval = rval = 1;
141 }
142 if (kread(X_MAXSLP, maxslp)) {
143 warnx("maxslp: %s", kvm_geterr(kd));
144 eval = rval = 1;
145 }
146 ccpu = (double)xccpu / fscale;
147 return (rval);
148 }
149
150 int
151 donlist_sysctl(void)
152 {
153 int mib[2];
154 size_t size;
155 fixpt_t xccpu;
156
157 nlistread = 1;
158 mib[0] = CTL_HW;
159 mib[1] = HW_PHYSMEM;
160 size = sizeof(mempages);
161 if (sysctl(mib, 2, &mempages, &size, NULL, 0) == 0)
162 mempages /= getpagesize();
163 else
164 mempages = 0;
165
166 mib[0] = CTL_KERN;
167 mib[1] = KERN_FSCALE;
168 size = sizeof(fscale);
169 if (sysctl(mib, 2, &fscale, &size, NULL, 0) == -1)
170 fscale = (1 << 8); /* XXX Hopefully reasonable default */
171
172 mib[0] = CTL_KERN;
173 mib[1] = KERN_CCPU;
174 size = sizeof(xccpu);
175 if (sysctl(mib, 2, &xccpu, &size, NULL, 0) == -1)
176 ccpu = exp(-1.0 / 20.0); /* XXX Hopefully reasonable default */
177 else
178 ccpu = (double)xccpu / fscale;
179
180 mib[0] = CTL_VM;
181 mib[1] = VM_MAXSLP;
182 size = sizeof(maxslp);
183 if (sysctl(mib, 2, &maxslp, &size, NULL, 0) == -1)
184 #ifdef MAXSLP
185 maxslp = MAXSLP;
186 #else
187 maxslp = 20; /* XXX Hopefully reasonable default */
188 #endif
189
190 mib[0] = CTL_VM;
191 mib[1] = VM_USPACE;
192 size = sizeof(uspace);
193 if (sysctl(mib, 2, &uspace, &size, NULL, 0) == -1)
194 #ifdef USPACE
195 uspace = USPACE;
196 #else
197 uspace = getpagesize(); /* XXX Hopefully reasonable default */
198 #endif
199
200 return 0;
201 }
202
203 void
204 nlisterr(struct nlist nl[])
205 {
206 int i;
207
208 (void)fprintf(stderr, "ps: nlist: can't find following symbols:");
209 for (i = 0; nl[i].n_name != NULL; i++)
210 if (nl[i].n_value == 0)
211 (void)fprintf(stderr, " %s", nl[i].n_name);
212 (void)fprintf(stderr, "\n");
213 }
214