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