1 /* $NetBSD: nlist.c,v 1.27 2016/11/28 08:19:23 rin 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 * 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. 30 */ 31 32 /* 33 * Copyright (c) 1990, 1993, 1994 34 * The Regents of the University of California. All rights reserved. 35 * 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. 47 * 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. 59 */ 60 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.27 2016/11/28 08:19:23 rin Exp $"); 67 #endif 68 #endif /* not lint */ 69 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> 76 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> 85 86 #include "ps.h" 87 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 } 98 }; 99 100 double log_ccpu; /* log of 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 */ 106 107 /* XXX Hopefully reasonable default */ 108 #define MEMPAGES 0 109 #ifndef FSCALE 110 #define FSCALE (1 << 8) 111 #endif 112 #define LOG_CCPU (-1.0 / 20.0) 113 #ifndef MAXSLP 114 #define MAXSLP 20 115 #endif 116 #ifndef USPACE 117 #define USPACE (getpagesize()) 118 #endif 119 120 #define kread(x, v) \ 121 kvm_read(kd, psnl[x].n_value, (char *)&v, sizeof v) != sizeof(v) 122 123 void 124 donlist(void) 125 { 126 fixpt_t xccpu; 127 128 nlistread = 1; 129 130 if (kvm_nlist(kd, psnl)) { 131 nlisterr(psnl); 132 eval = 1; 133 fscale = FSCALE; 134 mempages = MEMPAGES; 135 log_ccpu = LOG_CCPU; 136 maxslp = MAXSLP; 137 return; 138 } 139 140 if (kread(X_FSCALE, fscale)) { 141 warnx("fscale: %s", kvm_geterr(kd)); 142 eval = 1; 143 fscale = FSCALE; 144 } 145 146 if (kread(X_PHYSMEM, mempages)) { 147 warnx("avail_start: %s", kvm_geterr(kd)); 148 eval = 1; 149 mempages = MEMPAGES; 150 } 151 152 if (kread(X_CCPU, xccpu)) { 153 warnx("ccpu: %s", kvm_geterr(kd)); 154 eval = 1; 155 log_ccpu = LOG_CCPU; 156 } else 157 log_ccpu = log((double)xccpu / fscale); 158 159 if (kread(X_MAXSLP, maxslp)) { 160 warnx("maxslp: %s", kvm_geterr(kd)); 161 eval = 1; 162 maxslp = MAXSLP; 163 } 164 } 165 166 void 167 donlist_sysctl(void) 168 { 169 int mib[2]; 170 size_t size; 171 fixpt_t xccpu; 172 uint64_t memsize; 173 174 nlistread = 1; 175 176 mib[0] = CTL_KERN; 177 mib[1] = KERN_FSCALE; 178 size = sizeof(fscale); 179 if (sysctl(mib, 2, &fscale, &size, NULL, 0)) { 180 warn("fscale"); 181 eval = 1; 182 fscale = FSCALE; 183 } 184 185 mib[0] = CTL_HW; 186 mib[1] = HW_PHYSMEM64; 187 size = sizeof(memsize); 188 if (sysctl(mib, 2, &memsize, &size, NULL, 0)) { 189 warn("avail_start"); 190 eval = 1; 191 mempages = MEMPAGES; 192 } else 193 mempages = memsize / getpagesize(); 194 195 mib[0] = CTL_KERN; 196 mib[1] = KERN_CCPU; 197 size = sizeof(xccpu); 198 if (sysctl(mib, 2, &xccpu, &size, NULL, 0)) { 199 warn("ccpu"); 200 eval = 1; 201 log_ccpu = LOG_CCPU; 202 } else 203 log_ccpu = log((double)xccpu / fscale); 204 205 mib[0] = CTL_VM; 206 mib[1] = VM_MAXSLP; 207 size = sizeof(maxslp); 208 if (sysctl(mib, 2, &maxslp, &size, NULL, 0)) { 209 warn("maxslp"); 210 eval = 1; 211 maxslp = MAXSLP; 212 } 213 214 mib[0] = CTL_VM; 215 mib[1] = VM_USPACE; 216 size = sizeof(uspace); 217 if (sysctl(mib, 2, &uspace, &size, NULL, 0)) { 218 warn("uspace"); 219 eval = 1; 220 uspace = USPACE; 221 } 222 } 223 224 void 225 nlisterr(struct nlist nl[]) 226 { 227 int i; 228 229 (void)fprintf(stderr, "ps: nlist: can't find following symbols:"); 230 for (i = 0; nl[i].n_name != NULL; i++) 231 if (nl[i].n_value == 0) 232 (void)fprintf(stderr, " %s", nl[i].n_name); 233 (void)fprintf(stderr, "\n"); 234 } 235