nlist.c revision 1.16 1 /* $NetBSD: nlist.c,v 1.16 2000/05/26 03:04:28 simonb Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)nlist.c 8.4 (Berkeley) 4/2/94";
40 #else
41 __RCSID("$NetBSD: nlist.c,v 1.16 2000/05/26 03:04:28 simonb Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <sys/proc.h>
48 #include <sys/resource.h>
49 #include <sys/sysctl.h>
50
51 #include <err.h>
52 #include <errno.h>
53 #include <kvm.h>
54 #include <math.h>
55 #include <nlist.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <unistd.h>
59
60 #include "ps.h"
61
62 struct nlist psnl[] = {
63 { "_fscale" },
64 #define X_FSCALE 0
65 { "_ccpu" },
66 #define X_CCPU 1
67 { "_physmem" },
68 #define X_PHYSMEM 2
69 { NULL }
70 };
71
72 double ccpu; /* kernel _ccpu variable */
73 int nlistread; /* if nlist already read. */
74 int mempages; /* number of pages of phys. memory */
75 int fscale; /* kernel _fscale variable */
76
77 #define kread(x, v) \
78 kvm_read(kd, psnl[x].n_value, (char *)&v, sizeof v) != sizeof(v)
79
80 int
81 donlist()
82 {
83 int rval;
84 fixpt_t xccpu;
85
86 rval = 0;
87 nlistread = 1;
88 if (kvm_nlist(kd, psnl)) {
89 nlisterr(psnl);
90 eval = 1;
91 return (1);
92 }
93 if (kread(X_FSCALE, fscale)) {
94 warnx("fscale: %s", kvm_geterr(kd));
95 eval = rval = 1;
96 }
97 if (kread(X_PHYSMEM, mempages)) {
98 warnx("avail_start: %s", kvm_geterr(kd));
99 eval = rval = 1;
100 }
101 if (kread(X_CCPU, xccpu)) {
102 warnx("ccpu: %s", kvm_geterr(kd));
103 eval = rval = 1;
104 }
105 ccpu = (double)xccpu / fscale;
106 return (rval);
107 }
108
109 int
110 donlist_sysctl()
111 {
112 int mib[2];
113 size_t size;
114 fixpt_t xccpu;
115
116 nlistread = 1;
117 mib[0] = CTL_HW;
118 mib[1] = HW_PHYSMEM;
119 size = sizeof(mempages);
120 if (sysctl(mib, 2, &mempages, &size, NULL, 0) == 0)
121 mempages /= getpagesize();
122 else
123 mempages = 0;
124
125 mib[0] = CTL_KERN;
126 mib[1] = KERN_FSCALE;
127 size = sizeof(fscale);
128 if (sysctl(mib, 2, &fscale, &size, NULL, 0) == -1)
129 fscale = (1 << 8); /* XXX Hopefully reasonable default */
130
131 mib[0] = CTL_KERN;
132 mib[1] = KERN_CCPU;
133 size = sizeof(xccpu);
134 if (sysctl(mib, 2, &xccpu, &size, NULL, 0) == -1)
135 ccpu = exp(-1.0 / 20.0); /* XXX Hopefully reasonable default */
136 else
137 ccpu = (double)xccpu / fscale;
138
139 return 0;
140 }
141
142 void
143 nlisterr(nl)
144 struct nlist nl[];
145 {
146 int i;
147
148 (void)fprintf(stderr, "ps: nlist: can't find following symbols:");
149 for (i = 0; nl[i].n_name != NULL; i++)
150 if (nl[i].n_value == 0)
151 (void)fprintf(stderr, " %s", nl[i].n_name);
152 (void)fprintf(stderr, "\n");
153 }
154