nlist.c revision 1.27 1 1.27 rin /* $NetBSD: nlist.c,v 1.27 2016/11/28 08:19:23 rin 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.27 rin __RCSID("$NetBSD: nlist.c,v 1.27 2016/11/28 08:19:23 rin 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.27 rin #ifndef USPACE
117 1.27 rin #define USPACE (getpagesize())
118 1.27 rin #endif
119 1.27 rin
120 1.23 simonb #define kread(x, v) \
121 1.7 cgd kvm_read(kd, psnl[x].n_value, (char *)&v, sizeof v) != sizeof(v)
122 1.1 cgd
123 1.27 rin void
124 1.22 simonb donlist(void)
125 1.1 cgd {
126 1.16 simonb fixpt_t xccpu;
127 1.1 cgd
128 1.1 cgd nlistread = 1;
129 1.27 rin
130 1.7 cgd if (kvm_nlist(kd, psnl)) {
131 1.1 cgd nlisterr(psnl);
132 1.1 cgd eval = 1;
133 1.27 rin fscale = FSCALE;
134 1.27 rin mempages = MEMPAGES;
135 1.27 rin log_ccpu = LOG_CCPU;
136 1.27 rin maxslp = MAXSLP;
137 1.27 rin return;
138 1.1 cgd }
139 1.27 rin
140 1.1 cgd if (kread(X_FSCALE, fscale)) {
141 1.7 cgd warnx("fscale: %s", kvm_geterr(kd));
142 1.27 rin eval = 1;
143 1.27 rin fscale = FSCALE;
144 1.1 cgd }
145 1.27 rin
146 1.14 is if (kread(X_PHYSMEM, mempages)) {
147 1.7 cgd warnx("avail_start: %s", kvm_geterr(kd));
148 1.27 rin eval = 1;
149 1.27 rin mempages = MEMPAGES;
150 1.1 cgd }
151 1.27 rin
152 1.16 simonb if (kread(X_CCPU, xccpu)) {
153 1.7 cgd warnx("ccpu: %s", kvm_geterr(kd));
154 1.27 rin eval = 1;
155 1.27 rin log_ccpu = LOG_CCPU;
156 1.27 rin } else
157 1.27 rin log_ccpu = log((double)xccpu / fscale);
158 1.27 rin
159 1.18 matt if (kread(X_MAXSLP, maxslp)) {
160 1.18 matt warnx("maxslp: %s", kvm_geterr(kd));
161 1.27 rin eval = 1;
162 1.27 rin maxslp = MAXSLP;
163 1.18 matt }
164 1.16 simonb }
165 1.16 simonb
166 1.27 rin void
167 1.22 simonb donlist_sysctl(void)
168 1.16 simonb {
169 1.16 simonb int mib[2];
170 1.16 simonb size_t size;
171 1.16 simonb fixpt_t xccpu;
172 1.24 dsl uint64_t memsize;
173 1.16 simonb
174 1.16 simonb nlistread = 1;
175 1.27 rin
176 1.27 rin mib[0] = CTL_KERN;
177 1.27 rin mib[1] = KERN_FSCALE;
178 1.27 rin size = sizeof(fscale);
179 1.27 rin if (sysctl(mib, 2, &fscale, &size, NULL, 0)) {
180 1.27 rin warn("fscale");
181 1.27 rin eval = 1;
182 1.27 rin fscale = FSCALE;
183 1.27 rin }
184 1.27 rin
185 1.16 simonb mib[0] = CTL_HW;
186 1.24 dsl mib[1] = HW_PHYSMEM64;
187 1.24 dsl size = sizeof(memsize);
188 1.27 rin if (sysctl(mib, 2, &memsize, &size, NULL, 0)) {
189 1.27 rin warn("avail_start");
190 1.27 rin eval = 1;
191 1.27 rin mempages = MEMPAGES;
192 1.27 rin } else
193 1.24 dsl mempages = memsize / getpagesize();
194 1.16 simonb
195 1.16 simonb mib[0] = CTL_KERN;
196 1.16 simonb mib[1] = KERN_CCPU;
197 1.16 simonb size = sizeof(xccpu);
198 1.27 rin if (sysctl(mib, 2, &xccpu, &size, NULL, 0)) {
199 1.27 rin warn("ccpu");
200 1.27 rin eval = 1;
201 1.27 rin log_ccpu = LOG_CCPU;
202 1.27 rin } else
203 1.27 rin log_ccpu = log((double)xccpu / fscale);
204 1.18 matt
205 1.18 matt mib[0] = CTL_VM;
206 1.18 matt mib[1] = VM_MAXSLP;
207 1.18 matt size = sizeof(maxslp);
208 1.27 rin if (sysctl(mib, 2, &maxslp, &size, NULL, 0)) {
209 1.27 rin warn("maxslp");
210 1.27 rin eval = 1;
211 1.18 matt maxslp = MAXSLP;
212 1.27 rin }
213 1.18 matt
214 1.18 matt mib[0] = CTL_VM;
215 1.18 matt mib[1] = VM_USPACE;
216 1.21 jrf size = sizeof(uspace);
217 1.27 rin if (sysctl(mib, 2, &uspace, &size, NULL, 0)) {
218 1.27 rin warn("uspace");
219 1.27 rin eval = 1;
220 1.18 matt uspace = USPACE;
221 1.27 rin }
222 1.1 cgd }
223 1.1 cgd
224 1.7 cgd void
225 1.22 simonb nlisterr(struct nlist nl[])
226 1.1 cgd {
227 1.1 cgd int i;
228 1.1 cgd
229 1.7 cgd (void)fprintf(stderr, "ps: nlist: can't find following symbols:");
230 1.1 cgd for (i = 0; nl[i].n_name != NULL; i++)
231 1.1 cgd if (nl[i].n_value == 0)
232 1.7 cgd (void)fprintf(stderr, " %s", nl[i].n_name);
233 1.7 cgd (void)fprintf(stderr, "\n");
234 1.1 cgd }
235