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