etc/protocols - sync with NetBSD-8
[minix.git] / bin / ps / nlist.c
blob013785575b9ca199a5a142ca6cc0e57bc7ff138f
1 /* $NetBSD: nlist.c,v 1.26 2008/04/28 20:22:51 martin Exp $ */
3 /*
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Simon Burge.
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.
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
38 * are met:
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
58 * SUCH DAMAGE.
61 #include <sys/cdefs.h>
62 #ifndef lint
63 #if 0
64 static char sccsid[] = "@(#)nlist.c 8.4 (Berkeley) 4/2/94";
65 #else
66 __RCSID("$NetBSD: nlist.c,v 1.26 2008/04/28 20:22:51 martin Exp $");
67 #endif
68 #endif /* not lint */
70 #include <sys/param.h>
71 #include <sys/time.h>
72 #include <sys/lwp.h>
73 #include <sys/proc.h>
74 #include <sys/resource.h>
75 #include <sys/sysctl.h>
77 #include <err.h>
78 #include <errno.h>
79 #include <kvm.h>
80 #include <math.h>
81 #include <nlist.h>
82 #include <stdio.h>
83 #include <string.h>
84 #include <unistd.h>
86 #include "ps.h"
88 struct nlist psnl[] = {
89 { .n_name = "_fscale" },
90 #define X_FSCALE 0
91 { .n_name = "_ccpu" },
92 #define X_CCPU 1
93 { .n_name = "_physmem" },
94 #define X_PHYSMEM 2
95 { .n_name = "_maxslp" },
96 #define X_MAXSLP 3
97 { .n_name = NULL }
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)
111 donlist(void)
113 int rval;
114 fixpt_t xccpu;
116 rval = 0;
117 nlistread = 1;
118 if (kvm_nlist(kd, psnl)) {
119 nlisterr(psnl);
120 eval = 1;
121 return (1);
123 if (kread(X_FSCALE, fscale)) {
124 warnx("fscale: %s", kvm_geterr(kd));
125 eval = rval = 1;
127 if (kread(X_PHYSMEM, mempages)) {
128 warnx("avail_start: %s", kvm_geterr(kd));
129 eval = rval = 1;
131 if (kread(X_CCPU, xccpu)) {
132 warnx("ccpu: %s", kvm_geterr(kd));
133 eval = rval = 1;
135 if (kread(X_MAXSLP, maxslp)) {
136 warnx("maxslp: %s", kvm_geterr(kd));
137 eval = rval = 1;
139 ccpu = (double)xccpu / fscale;
140 return (rval);
144 donlist_sysctl(void)
146 int mib[2];
147 size_t size;
148 fixpt_t xccpu;
149 uint64_t memsize;
151 nlistread = 1;
152 mib[0] = CTL_HW;
153 mib[1] = HW_PHYSMEM64;
154 size = sizeof(memsize);
155 if (sysctl(mib, 2, &memsize, &size, NULL, 0) == 0)
156 mempages = memsize / getpagesize();
157 else
158 mempages = 0;
160 mib[0] = CTL_KERN;
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 */
166 mib[0] = CTL_KERN;
167 mib[1] = KERN_CCPU;
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 */
171 else
172 ccpu = (double)xccpu / fscale;
174 mib[0] = CTL_VM;
175 mib[1] = VM_MAXSLP;
176 size = sizeof(maxslp);
177 if (sysctl(mib, 2, &maxslp, &size, NULL, 0) == -1)
178 #ifdef MAXSLP
179 maxslp = MAXSLP;
180 #else
181 maxslp = 20; /* XXX Hopefully reasonable default */
182 #endif
184 mib[0] = CTL_VM;
185 mib[1] = VM_USPACE;
186 size = sizeof(uspace);
187 if (sysctl(mib, 2, &uspace, &size, NULL, 0) == -1)
188 #ifdef USPACE
189 uspace = USPACE;
190 #else
191 uspace = getpagesize(); /* XXX Hopefully reasonable default */
192 #endif
194 return 0;
197 void
198 nlisterr(struct nlist nl[])
200 int i;
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");