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