vmstat.c revision 1.230 1 1.230 msaitoh /* $NetBSD: vmstat.c,v 1.230 2019/12/27 09:45:27 msaitoh Exp $ */
2 1.45 thorpej
3 1.45 thorpej /*-
4 1.229 ad * Copyright (c) 1998, 2000, 2001, 2007, 2019 The NetBSD Foundation, Inc.
5 1.45 thorpej * All rights reserved.
6 1.45 thorpej *
7 1.87 lukem * This code is derived from software contributed to The NetBSD Foundation by:
8 1.87 lukem * - Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.87 lukem * NASA Ames Research Center.
10 1.87 lukem * - Simon Burge and Luke Mewburn of Wasabi Systems, Inc.
11 1.45 thorpej *
12 1.45 thorpej * Redistribution and use in source and binary forms, with or without
13 1.45 thorpej * modification, are permitted provided that the following conditions
14 1.45 thorpej * are met:
15 1.45 thorpej * 1. Redistributions of source code must retain the above copyright
16 1.45 thorpej * notice, this list of conditions and the following disclaimer.
17 1.45 thorpej * 2. Redistributions in binary form must reproduce the above copyright
18 1.45 thorpej * notice, this list of conditions and the following disclaimer in the
19 1.45 thorpej * documentation and/or other materials provided with the distribution.
20 1.45 thorpej *
21 1.45 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 1.45 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.45 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.45 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 1.45 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.45 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.45 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.45 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.45 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.45 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.45 thorpej * POSSIBILITY OF SUCH DAMAGE.
32 1.45 thorpej */
33 1.21 cgd
34 1.1 cgd /*
35 1.13 cgd * Copyright (c) 1980, 1986, 1991, 1993
36 1.13 cgd * The Regents of the University of California. All rights reserved.
37 1.1 cgd *
38 1.1 cgd * Redistribution and use in source and binary forms, with or without
39 1.1 cgd * modification, are permitted provided that the following conditions
40 1.1 cgd * are met:
41 1.1 cgd * 1. Redistributions of source code must retain the above copyright
42 1.1 cgd * notice, this list of conditions and the following disclaimer.
43 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
44 1.1 cgd * notice, this list of conditions and the following disclaimer in the
45 1.1 cgd * documentation and/or other materials provided with the distribution.
46 1.117 agc * 3. Neither the name of the University nor the names of its contributors
47 1.1 cgd * may be used to endorse or promote products derived from this software
48 1.1 cgd * without specific prior written permission.
49 1.1 cgd *
50 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 1.1 cgd * SUCH DAMAGE.
61 1.1 cgd */
62 1.1 cgd
63 1.38 mrg #include <sys/cdefs.h>
64 1.1 cgd #ifndef lint
65 1.161 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1991, 1993\
66 1.161 lukem The Regents of the University of California. All rights reserved.");
67 1.1 cgd #endif /* not lint */
68 1.1 cgd
69 1.1 cgd #ifndef lint
70 1.21 cgd #if 0
71 1.37 mrg static char sccsid[] = "@(#)vmstat.c 8.2 (Berkeley) 3/1/95";
72 1.21 cgd #else
73 1.230 msaitoh __RCSID("$NetBSD: vmstat.c,v 1.230 2019/12/27 09:45:27 msaitoh Exp $");
74 1.21 cgd #endif
75 1.1 cgd #endif /* not lint */
76 1.57 thorpej
77 1.57 thorpej #define __POOL_EXPOSE
78 1.228 christos #define __NAMECACHE_PRIVATE
79 1.1 cgd
80 1.1 cgd #include <sys/param.h>
81 1.174 christos #include <sys/types.h>
82 1.87 lukem #include <sys/mount.h>
83 1.87 lukem #include <sys/uio.h>
84 1.87 lukem
85 1.87 lukem #include <sys/buf.h>
86 1.146 yamt #include <sys/evcnt.h>
87 1.87 lukem #include <sys/ioctl.h>
88 1.87 lukem #include <sys/malloc.h>
89 1.109 thorpej #include <sys/mallocvar.h>
90 1.1 cgd #include <sys/namei.h>
91 1.87 lukem #include <sys/pool.h>
92 1.87 lukem #include <sys/proc.h>
93 1.64 perry #include <sys/sched.h>
94 1.87 lukem #include <sys/socket.h>
95 1.13 cgd #include <sys/sysctl.h>
96 1.87 lukem #include <sys/time.h>
97 1.171 christos #include <sys/queue.h>
98 1.181 mrg #include <sys/kernhist.h>
99 1.229 ad #include <sys/vnode.h>
100 1.229 ad #include <sys/vnode_impl.h>
101 1.67 mrg
102 1.67 mrg #include <uvm/uvm_extern.h>
103 1.67 mrg #include <uvm/uvm_stat.h>
104 1.67 mrg
105 1.87 lukem #include <net/if.h>
106 1.87 lukem #include <netinet/in.h>
107 1.87 lukem #include <netinet/in_var.h>
108 1.87 lukem
109 1.87 lukem #include <ufs/ufs/inode.h>
110 1.87 lukem
111 1.87 lukem #include <nfs/rpcv2.h>
112 1.87 lukem #include <nfs/nfsproto.h>
113 1.87 lukem #include <nfs/nfsnode.h>
114 1.87 lukem
115 1.87 lukem #include <ctype.h>
116 1.45 thorpej #include <err.h>
117 1.87 lukem #include <errno.h>
118 1.55 kleink #include <fcntl.h>
119 1.87 lukem #include <kvm.h>
120 1.87 lukem #include <limits.h>
121 1.1 cgd #include <nlist.h>
122 1.87 lukem #undef n_hash
123 1.87 lukem #include <paths.h>
124 1.22 jtc #include <signal.h>
125 1.1 cgd #include <stdio.h>
126 1.87 lukem #include <stddef.h>
127 1.1 cgd #include <stdlib.h>
128 1.1 cgd #include <string.h>
129 1.87 lukem #include <time.h>
130 1.87 lukem #include <unistd.h>
131 1.104 mrg #include <util.h>
132 1.87 lukem
133 1.140 blymn #include "drvstats.h"
134 1.45 thorpej
135 1.90 lukem /*
136 1.174 christos * All this mess will go away once everything is converted.
137 1.174 christos */
138 1.174 christos #ifdef __HAVE_CPU_DATA_FIRST
139 1.175 christos
140 1.175 christos # include <sys/cpu_data.h>
141 1.175 christos struct cpu_info {
142 1.174 christos struct cpu_data ci_data;
143 1.174 christos };
144 1.174 christos #else
145 1.190 rmind # include <sys/cpu.h>
146 1.190 rmind #endif
147 1.175 christos
148 1.174 christos /*
149 1.90 lukem * General namelist
150 1.90 lukem */
151 1.87 lukem struct nlist namelist[] =
152 1.87 lukem {
153 1.65 itojun #define X_BOOTTIME 0
154 1.153 christos { .n_name = "_boottime" },
155 1.75 enami #define X_HZ 1
156 1.153 christos { .n_name = "_hz" },
157 1.75 enami #define X_STATHZ 2
158 1.153 christos { .n_name = "_stathz" },
159 1.75 enami #define X_NCHSTATS 3
160 1.153 christos { .n_name = "_nchstats" },
161 1.188 para #define X_ALLEVENTS 4
162 1.153 christos { .n_name = "_allevents" },
163 1.188 para #define X_POOLHEAD 5
164 1.153 christos { .n_name = "_pool_head" },
165 1.188 para #define X_UVMEXP 6
166 1.153 christos { .n_name = "_uvmexp" },
167 1.188 para #define X_TIME_SECOND 7
168 1.153 christos { .n_name = "_time_second" },
169 1.188 para #define X_TIME 8
170 1.153 christos { .n_name = "_time" },
171 1.190 rmind #define X_CPU_INFOS 9
172 1.190 rmind { .n_name = "_cpu_infos" },
173 1.188 para #define X_NL_SIZE 10
174 1.153 christos { .n_name = NULL },
175 1.90 lukem };
176 1.90 lukem
177 1.90 lukem /*
178 1.133 chs * Namelist for pre-evcnt interrupt counters.
179 1.133 chs */
180 1.133 chs struct nlist intrnl[] =
181 1.133 chs {
182 1.133 chs #define X_INTRNAMES 0
183 1.153 christos { .n_name = "_intrnames" },
184 1.133 chs #define X_EINTRNAMES 1
185 1.153 christos { .n_name = "_eintrnames" },
186 1.133 chs #define X_INTRCNT 2
187 1.153 christos { .n_name = "_intrcnt" },
188 1.133 chs #define X_EINTRCNT 3
189 1.153 christos { .n_name = "_eintrcnt" },
190 1.133 chs #define X_INTRNL_SIZE 4
191 1.153 christos { .n_name = NULL },
192 1.133 chs };
193 1.133 chs
194 1.133 chs
195 1.133 chs /*
196 1.90 lukem * Namelist for hash statistics
197 1.90 lukem */
198 1.90 lukem struct nlist hashnl[] =
199 1.90 lukem {
200 1.90 lukem #define X_NFSNODE 0
201 1.153 christos { .n_name = "_nfsnodehash" },
202 1.90 lukem #define X_NFSNODETBL 1
203 1.153 christos { .n_name = "_nfsnodehashtbl" },
204 1.90 lukem #define X_IHASH 2
205 1.153 christos { .n_name = "_ihash" },
206 1.90 lukem #define X_IHASHTBL 3
207 1.153 christos { .n_name = "_ihashtbl" },
208 1.90 lukem #define X_BUFHASH 4
209 1.153 christos { .n_name = "_bufhash" },
210 1.90 lukem #define X_BUFHASHTBL 5
211 1.153 christos { .n_name = "_bufhashtbl" },
212 1.122 junyoung #define X_UIHASH 6
213 1.153 christos { .n_name = "_uihash" },
214 1.122 junyoung #define X_UIHASHTBL 7
215 1.153 christos { .n_name = "_uihashtbl" },
216 1.122 junyoung #define X_IFADDRHASH 8
217 1.153 christos { .n_name = "_in_ifaddrhash" },
218 1.122 junyoung #define X_IFADDRHASHTBL 9
219 1.153 christos { .n_name = "_in_ifaddrhashtbl" },
220 1.122 junyoung #define X_NCHASH 10
221 1.153 christos { .n_name = "_nchash" },
222 1.122 junyoung #define X_NCHASHTBL 11
223 1.153 christos { .n_name = "_nchashtbl" },
224 1.122 junyoung #define X_NCVHASH 12
225 1.153 christos { .n_name = "_ncvhash" },
226 1.122 junyoung #define X_NCVHASHTBL 13
227 1.153 christos { .n_name = "_ncvhashtbl" },
228 1.229 ad #define X_NCVCACHEHASH 14
229 1.229 ad { .n_name = "_vcache_hashmask" },
230 1.229 ad #define X_NCVCACHETBL 15
231 1.229 ad { .n_name = "_vcache_hashtab" },
232 1.229 ad #define X_HASHNL_SIZE 16 /* must be last */
233 1.153 christos { .n_name = NULL },
234 1.90 lukem };
235 1.87 lukem
236 1.90 lukem /*
237 1.181 mrg * Namelist for kernel histories
238 1.90 lukem */
239 1.90 lukem struct nlist histnl[] =
240 1.90 lukem {
241 1.181 mrg { .n_name = "_kern_histories" },
242 1.181 mrg #define X_KERN_HISTORIES 0
243 1.153 christos { .n_name = NULL },
244 1.1 cgd };
245 1.1 cgd
246 1.87 lukem
247 1.205 skrll #define KILO 1024
248 1.90 lukem
249 1.171 christos struct cpu_counter {
250 1.171 christos uint64_t nintr;
251 1.171 christos uint64_t nsyscall;
252 1.171 christos uint64_t nswtch;
253 1.171 christos uint64_t nfault;
254 1.171 christos uint64_t ntrap;
255 1.171 christos uint64_t nsoft;
256 1.171 christos } cpucounter, ocpucounter;
257 1.171 christos
258 1.196 joerg struct uvmexp_sysctl uvmexp, ouvmexp;
259 1.73 simonb int ndrives;
260 1.1 cgd
261 1.1 cgd int winlines = 20;
262 1.1 cgd
263 1.13 cgd kvm_t *kd;
264 1.13 cgd
265 1.174 christos
266 1.185 christos #define FORKSTAT 0x001
267 1.185 christos #define INTRSTAT 0x002
268 1.185 christos #define MEMSTAT 0x004
269 1.185 christos #define SUMSTAT 0x008
270 1.185 christos #define EVCNTSTAT 0x010
271 1.185 christos #define VMSTAT 0x020
272 1.185 christos #define HISTLIST 0x040
273 1.185 christos #define HISTDUMP 0x080
274 1.185 christos #define HASHSTAT 0x100
275 1.185 christos #define HASHLIST 0x200
276 1.185 christos #define VMTOTAL 0x400
277 1.185 christos #define POOLCACHESTAT 0x800
278 1.1 cgd
279 1.151 yamt /*
280 1.151 yamt * Print single word. `ovflow' is number of characters didn't fit
281 1.151 yamt * on the last word. `fmt' is a format string to print this word.
282 1.151 yamt * It must contain asterisk for field width. `width' is a width
283 1.151 yamt * occupied by this word. `fixed' is a number of constant chars in
284 1.151 yamt * `fmt'. `val' is a value to be printed using format string `fmt'.
285 1.151 yamt */
286 1.151 yamt #define PRWORD(ovflw, fmt, width, fixed, val) do { \
287 1.151 yamt (ovflw) += printf((fmt), \
288 1.151 yamt (width) - (fixed) - (ovflw) > 0 ? \
289 1.151 yamt (width) - (fixed) - (ovflw) : 0, \
290 1.151 yamt (val)) - (width); \
291 1.151 yamt if ((ovflw) < 0) \
292 1.151 yamt (ovflw) = 0; \
293 1.151 yamt } while (/* CONSTCOND */0)
294 1.151 yamt
295 1.151 yamt void cpustats(int *);
296 1.171 christos void cpucounters(struct cpu_counter *);
297 1.87 lukem void deref_kptr(const void *, void *, size_t, const char *);
298 1.151 yamt void drvstats(int *);
299 1.176 matt void doevcnt(int verbose, int type);
300 1.88 lukem void dohashstat(int, int, const char *);
301 1.73 simonb void dointr(int verbose);
302 1.126 simonb void dopool(int, int);
303 1.182 yamt void dopoolcache(int);
304 1.73 simonb void dosum(void);
305 1.103 mycroft void dovmstat(struct timespec *, int);
306 1.130 he void print_total_hdr(void);
307 1.130 he void dovmtotal(struct timespec *, int);
308 1.133 chs void kread(struct nlist *, int, void *, size_t);
309 1.147 kardel int kreadc(struct nlist *, int, void *, size_t);
310 1.73 simonb void needhdr(int);
311 1.176 matt void getnlist(int);
312 1.73 simonb long getuptime(void);
313 1.73 simonb void printhdr(void);
314 1.203 nakayama long pct(u_long, u_long);
315 1.183 joerg __dead static void usage(void);
316 1.73 simonb void doforkst(void);
317 1.73 simonb
318 1.73 simonb void hist_traverse(int, const char *);
319 1.210 pgoyette void hist_dodump(struct kern_history *);
320 1.210 pgoyette void hist_traverse_sysctl(int, const char *);
321 1.210 pgoyette void hist_dodump_sysctl(int[], unsigned int);
322 1.73 simonb
323 1.73 simonb char **choosedrives(char **);
324 1.38 mrg
325 1.29 thorpej /* Namelist and memory file names. */
326 1.29 thorpej char *nlistf, *memf;
327 1.29 thorpej
328 1.47 mrg /* allow old usage [vmstat 1] */
329 1.47 mrg #define BACKWARD_COMPATIBILITY
330 1.47 mrg
331 1.197 joerg static const int clockrate_mib[] = { CTL_KERN, KERN_CLOCKRATE };
332 1.170 christos static const int vmmeter_mib[] = { CTL_VM, VM_METER };
333 1.170 christos static const int uvmexp2_mib[] = { CTL_VM, VM_UVMEXP2 };
334 1.176 matt static const int boottime_mib[] = { CTL_KERN, KERN_BOOTTIME };
335 1.176 matt static char kvm_errbuf[_POSIX2_LINE_MAX];
336 1.170 christos
337 1.38 mrg int
338 1.75 enami main(int argc, char *argv[])
339 1.1 cgd {
340 1.126 simonb int c, todo, verbose, wide;
341 1.103 mycroft struct timespec interval;
342 1.1 cgd int reps;
343 1.75 enami gid_t egid = getegid();
344 1.88 lukem const char *histname, *hashname;
345 1.1 cgd
346 1.88 lukem histname = hashname = NULL;
347 1.48 mrg (void)setegid(getgid());
348 1.13 cgd memf = nlistf = NULL;
349 1.126 simonb reps = todo = verbose = wide = 0;
350 1.103 mycroft interval.tv_sec = 0;
351 1.103 mycroft interval.tv_nsec = 0;
352 1.154 ad while ((c = getopt(argc, argv, "Cc:efh:HilLM:mN:stu:UvWw:")) != -1) {
353 1.1 cgd switch (c) {
354 1.1 cgd case 'c':
355 1.1 cgd reps = atoi(optarg);
356 1.1 cgd break;
357 1.154 ad case 'C':
358 1.154 ad todo |= POOLCACHESTAT;
359 1.154 ad break;
360 1.66 cgd case 'e':
361 1.66 cgd todo |= EVCNTSTAT;
362 1.66 cgd break;
363 1.1 cgd case 'f':
364 1.1 cgd todo |= FORKSTAT;
365 1.1 cgd break;
366 1.45 thorpej case 'h':
367 1.88 lukem hashname = optarg;
368 1.88 lukem /* FALLTHROUGH */
369 1.88 lukem case 'H':
370 1.87 lukem todo |= HASHSTAT;
371 1.45 thorpej break;
372 1.1 cgd case 'i':
373 1.1 cgd todo |= INTRSTAT;
374 1.1 cgd break;
375 1.45 thorpej case 'l':
376 1.45 thorpej todo |= HISTLIST;
377 1.45 thorpej break;
378 1.88 lukem case 'L':
379 1.88 lukem todo |= HASHLIST;
380 1.88 lukem break;
381 1.1 cgd case 'M':
382 1.13 cgd memf = optarg;
383 1.1 cgd break;
384 1.1 cgd case 'm':
385 1.1 cgd todo |= MEMSTAT;
386 1.1 cgd break;
387 1.1 cgd case 'N':
388 1.13 cgd nlistf = optarg;
389 1.1 cgd break;
390 1.1 cgd case 's':
391 1.1 cgd todo |= SUMSTAT;
392 1.1 cgd break;
393 1.130 he case 't':
394 1.130 he todo |= VMTOTAL;
395 1.130 he break;
396 1.87 lukem case 'u':
397 1.87 lukem histname = optarg;
398 1.87 lukem /* FALLTHROUGH */
399 1.87 lukem case 'U':
400 1.87 lukem todo |= HISTDUMP;
401 1.87 lukem break;
402 1.66 cgd case 'v':
403 1.87 lukem verbose++;
404 1.66 cgd break;
405 1.126 simonb case 'W':
406 1.126 simonb wide++;
407 1.126 simonb break;
408 1.1 cgd case 'w':
409 1.103 mycroft interval.tv_sec = atol(optarg);
410 1.1 cgd break;
411 1.1 cgd case '?':
412 1.1 cgd default:
413 1.1 cgd usage();
414 1.1 cgd }
415 1.1 cgd }
416 1.1 cgd argc -= optind;
417 1.1 cgd argv += optind;
418 1.1 cgd
419 1.1 cgd if (todo == 0)
420 1.1 cgd todo = VMSTAT;
421 1.1 cgd
422 1.13 cgd /*
423 1.48 mrg * Discard setgid privileges. If not the running kernel, we toss
424 1.48 mrg * them away totally so that bad guys can't print interesting stuff
425 1.48 mrg * from kernel memory, otherwise switch back to kmem for the
426 1.48 mrg * duration of the kvm_openfiles() call.
427 1.13 cgd */
428 1.13 cgd if (nlistf != NULL || memf != NULL)
429 1.48 mrg (void)setgid(getgid());
430 1.48 mrg else
431 1.48 mrg (void)setegid(egid);
432 1.13 cgd
433 1.176 matt kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, kvm_errbuf);
434 1.176 matt if (kd == NULL) {
435 1.176 matt if (nlistf != NULL || memf != NULL) {
436 1.176 matt errx(1, "kvm_openfiles: %s", kvm_errbuf);
437 1.176 matt }
438 1.176 matt }
439 1.48 mrg
440 1.95 simonb if (nlistf == NULL && memf == NULL)
441 1.95 simonb (void)setgid(getgid());
442 1.1 cgd
443 1.1 cgd
444 1.1 cgd if (todo & VMSTAT) {
445 1.1 cgd struct winsize winsize;
446 1.1 cgd
447 1.153 christos (void)drvinit(0);/* Initialize disk stats, no disks selected. */
448 1.49 drochner
449 1.49 drochner (void)setgid(getgid()); /* don't need privs anymore */
450 1.49 drochner
451 1.29 thorpej argv = choosedrives(argv); /* Select disks. */
452 1.1 cgd winsize.ws_row = 0;
453 1.115 simonb (void)ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize);
454 1.1 cgd if (winsize.ws_row > 0)
455 1.1 cgd winlines = winsize.ws_row;
456 1.1 cgd
457 1.1 cgd }
458 1.1 cgd
459 1.1 cgd #ifdef BACKWARD_COMPATIBILITY
460 1.1 cgd if (*argv) {
461 1.103 mycroft interval.tv_sec = atol(*argv);
462 1.1 cgd if (*++argv)
463 1.1 cgd reps = atoi(*argv);
464 1.1 cgd }
465 1.1 cgd #endif
466 1.1 cgd
467 1.103 mycroft if (interval.tv_sec) {
468 1.1 cgd if (!reps)
469 1.1 cgd reps = -1;
470 1.1 cgd } else if (reps)
471 1.103 mycroft interval.tv_sec = 1;
472 1.1 cgd
473 1.78 jhawk
474 1.185 christos getnlist(todo);
475 1.78 jhawk /*
476 1.78 jhawk * Statistics dumping is incompatible with the default
477 1.78 jhawk * VMSTAT/dovmstat() output. So perform the interval/reps handling
478 1.78 jhawk * for it here.
479 1.78 jhawk */
480 1.130 he if ((todo & (VMSTAT|VMTOTAL)) == 0) {
481 1.99 enami for (;;) {
482 1.99 enami if (todo & (HISTLIST|HISTDUMP)) {
483 1.99 enami if ((todo & (HISTLIST|HISTDUMP)) ==
484 1.99 enami (HISTLIST|HISTDUMP))
485 1.99 enami errx(1, "you may list or dump,"
486 1.99 enami " but not both!");
487 1.210 pgoyette if (memf != NULL)
488 1.210 pgoyette hist_traverse(todo, histname);
489 1.210 pgoyette else
490 1.210 pgoyette hist_traverse_sysctl(todo, histname);
491 1.153 christos (void)putchar('\n');
492 1.99 enami }
493 1.99 enami if (todo & FORKSTAT) {
494 1.99 enami doforkst();
495 1.153 christos (void)putchar('\n');
496 1.99 enami }
497 1.99 enami if (todo & MEMSTAT) {
498 1.126 simonb dopool(verbose, wide);
499 1.153 christos (void)putchar('\n');
500 1.99 enami }
501 1.154 ad if (todo & POOLCACHESTAT) {
502 1.182 yamt dopoolcache(verbose);
503 1.154 ad (void)putchar('\n');
504 1.154 ad }
505 1.99 enami if (todo & SUMSTAT) {
506 1.99 enami dosum();
507 1.153 christos (void)putchar('\n');
508 1.99 enami }
509 1.99 enami if (todo & INTRSTAT) {
510 1.99 enami dointr(verbose);
511 1.153 christos (void)putchar('\n');
512 1.99 enami }
513 1.99 enami if (todo & EVCNTSTAT) {
514 1.176 matt doevcnt(verbose, EVCNT_TYPE_ANY);
515 1.153 christos (void)putchar('\n');
516 1.99 enami }
517 1.99 enami if (todo & (HASHLIST|HASHSTAT)) {
518 1.99 enami if ((todo & (HASHLIST|HASHSTAT)) ==
519 1.99 enami (HASHLIST|HASHSTAT))
520 1.99 enami errx(1, "you may list or display,"
521 1.99 enami " but not both!");
522 1.99 enami dohashstat(verbose, todo, hashname);
523 1.153 christos (void)putchar('\n');
524 1.99 enami }
525 1.101 sommerfe
526 1.164 dholland fflush(stdout);
527 1.101 sommerfe if (reps >= 0 && --reps <=0)
528 1.99 enami break;
529 1.153 christos (void)nanosleep(&interval, NULL);
530 1.87 lukem }
531 1.130 he } else {
532 1.130 he if ((todo & (VMSTAT|VMTOTAL)) == (VMSTAT|VMTOTAL)) {
533 1.130 he errx(1, "you may not both do vmstat and vmtotal");
534 1.130 he }
535 1.130 he if (todo & VMSTAT)
536 1.130 he dovmstat(&interval, reps);
537 1.130 he if (todo & VMTOTAL)
538 1.130 he dovmtotal(&interval, reps);
539 1.130 he }
540 1.153 christos return 0;
541 1.1 cgd }
542 1.1 cgd
543 1.176 matt void
544 1.176 matt getnlist(int todo)
545 1.176 matt {
546 1.176 matt static int namelist_done = 0;
547 1.185 christos static int done = 0;
548 1.176 matt int c;
549 1.176 matt size_t i;
550 1.176 matt
551 1.176 matt if (kd == NULL)
552 1.176 matt errx(1, "kvm_openfiles: %s", kvm_errbuf);
553 1.176 matt
554 1.176 matt if (!namelist_done) {
555 1.176 matt namelist_done = 1;
556 1.176 matt if ((c = kvm_nlist(kd, namelist)) != 0) {
557 1.176 matt int doexit = 0;
558 1.176 matt if (c == -1)
559 1.176 matt errx(1, "kvm_nlist: %s %s",
560 1.176 matt "namelist", kvm_geterr(kd));
561 1.176 matt for (i = 0; i < __arraycount(namelist)-1; i++)
562 1.176 matt if (namelist[i].n_type == 0 &&
563 1.176 matt i != X_TIME_SECOND &&
564 1.176 matt i != X_TIME) {
565 1.176 matt if (doexit++ == 0)
566 1.176 matt (void)fprintf(stderr,
567 1.176 matt "%s: undefined symbols:",
568 1.176 matt getprogname());
569 1.176 matt (void)fprintf(stderr, " %s",
570 1.176 matt namelist[i].n_name);
571 1.176 matt }
572 1.176 matt if (doexit) {
573 1.176 matt (void)fputc('\n', stderr);
574 1.176 matt exit(1);
575 1.176 matt }
576 1.176 matt }
577 1.176 matt }
578 1.185 christos if ((todo & (SUMSTAT|INTRSTAT)) && !(done & (SUMSTAT|INTRSTAT))) {
579 1.185 christos done |= SUMSTAT|INTRSTAT;
580 1.176 matt (void) kvm_nlist(kd, intrnl);
581 1.176 matt }
582 1.185 christos if ((todo & (HASHLIST|HASHSTAT)) && !(done & (HASHLIST|HASHSTAT))) {
583 1.185 christos done |= HASHLIST|HASHSTAT;
584 1.176 matt if ((c = kvm_nlist(kd, hashnl)) == -1 || c == X_HASHNL_SIZE)
585 1.176 matt errx(1, "kvm_nlist: %s %s", "hashnl", kvm_geterr(kd));
586 1.176 matt }
587 1.185 christos if ((todo & (HISTLIST|HISTDUMP)) && !(done & (HISTLIST|HISTDUMP))) {
588 1.185 christos done |= HISTLIST|HISTDUMP;
589 1.176 matt if (kvm_nlist(kd, histnl) == -1)
590 1.176 matt errx(1, "kvm_nlist: %s %s", "histnl", kvm_geterr(kd));
591 1.176 matt }
592 1.176 matt }
593 1.176 matt
594 1.1 cgd char **
595 1.73 simonb choosedrives(char **argv)
596 1.1 cgd {
597 1.165 lukem size_t i;
598 1.1 cgd
599 1.1 cgd /*
600 1.1 cgd * Choose drives to be displayed. Priority goes to (in order) drives
601 1.1 cgd * supplied as arguments, default drives. If everything isn't filled
602 1.1 cgd * in and there are drives not taken care of, display the first few
603 1.1 cgd * that fit.
604 1.1 cgd */
605 1.75 enami #define BACKWARD_COMPATIBILITY
606 1.1 cgd for (ndrives = 0; *argv; ++argv) {
607 1.1 cgd #ifdef BACKWARD_COMPATIBILITY
608 1.124 dsl if (isdigit((unsigned char)**argv))
609 1.1 cgd break;
610 1.1 cgd #endif
611 1.140 blymn for (i = 0; i < ndrive; i++) {
612 1.1 cgd if (strcmp(dr_name[i], *argv))
613 1.1 cgd continue;
614 1.140 blymn drv_select[i] = 1;
615 1.136 blymn ++ndrives;
616 1.136 blymn break;
617 1.136 blymn }
618 1.1 cgd }
619 1.151 yamt for (i = 0; i < ndrive && ndrives < 2; i++) {
620 1.140 blymn if (drv_select[i])
621 1.1 cgd continue;
622 1.140 blymn drv_select[i] = 1;
623 1.1 cgd ++ndrives;
624 1.1 cgd }
625 1.140 blymn
626 1.75 enami return (argv);
627 1.1 cgd }
628 1.1 cgd
629 1.1 cgd long
630 1.73 simonb getuptime(void)
631 1.1 cgd {
632 1.176 matt static struct timespec boottime;
633 1.176 matt struct timespec now;
634 1.149 kardel time_t uptime, nowsec;
635 1.1 cgd
636 1.176 matt if (memf == NULL) {
637 1.176 matt if (boottime.tv_sec == 0) {
638 1.176 matt size_t buflen = sizeof(boottime);
639 1.176 matt if (sysctl(boottime_mib, __arraycount(boottime_mib),
640 1.176 matt &boottime, &buflen, NULL, 0) == -1)
641 1.176 matt warn("Can't get boottime");
642 1.176 matt }
643 1.176 matt clock_gettime(CLOCK_REALTIME, &now);
644 1.147 kardel } else {
645 1.176 matt if (boottime.tv_sec == 0)
646 1.176 matt kread(namelist, X_BOOTTIME, &boottime,
647 1.176 matt sizeof(boottime));
648 1.176 matt if (kreadc(namelist, X_TIME_SECOND, &nowsec, sizeof(nowsec))) {
649 1.176 matt /*
650 1.176 matt * XXX this assignment dance can be removed once
651 1.176 matt * timeval tv_sec is SUS mandated time_t
652 1.176 matt */
653 1.176 matt now.tv_sec = nowsec;
654 1.176 matt now.tv_nsec = 0;
655 1.176 matt } else {
656 1.176 matt kread(namelist, X_TIME, &now, sizeof(now));
657 1.176 matt }
658 1.147 kardel }
659 1.145 kardel uptime = now.tv_sec - boottime.tv_sec;
660 1.87 lukem if (uptime <= 0 || uptime > 60*60*24*365*10)
661 1.87 lukem errx(1, "time makes no sense; namelist must be wrong.");
662 1.75 enami return (uptime);
663 1.1 cgd }
664 1.1 cgd
665 1.1 cgd int hz, hdrcnt;
666 1.1 cgd
667 1.1 cgd void
668 1.187 matt print_total_hdr(void)
669 1.130 he {
670 1.130 he
671 1.166 rmind (void)printf("procs memory\n");
672 1.166 rmind (void)printf("ru dw pw sl");
673 1.130 he (void)printf(" total-v active-v active-r");
674 1.130 he (void)printf(" vm-sh avm-sh rm-sh arm-sh free\n");
675 1.130 he hdrcnt = winlines - 2;
676 1.130 he }
677 1.130 he
678 1.130 he void
679 1.130 he dovmtotal(struct timespec *interval, int reps)
680 1.130 he {
681 1.130 he struct vmtotal total;
682 1.130 he size_t size;
683 1.130 he
684 1.130 he (void)signal(SIGCONT, needhdr);
685 1.130 he
686 1.130 he for (hdrcnt = 1;;) {
687 1.130 he if (!--hdrcnt)
688 1.130 he print_total_hdr();
689 1.130 he if (memf != NULL) {
690 1.170 christos warnx("Unable to get vmtotals from crash dump.");
691 1.153 christos (void)memset(&total, 0, sizeof(total));
692 1.130 he } else {
693 1.130 he size = sizeof(total);
694 1.170 christos if (sysctl(vmmeter_mib, __arraycount(vmmeter_mib),
695 1.170 christos &total, &size, NULL, 0) == -1) {
696 1.170 christos warn("Can't get vmtotals");
697 1.153 christos (void)memset(&total, 0, sizeof(total));
698 1.130 he }
699 1.130 he }
700 1.153 christos (void)printf("%2d ", total.t_rq);
701 1.153 christos (void)printf("%2d ", total.t_dw);
702 1.153 christos (void)printf("%2d ", total.t_pw);
703 1.153 christos (void)printf("%2d ", total.t_sl);
704 1.153 christos
705 1.153 christos (void)printf("%9d ", total.t_vm);
706 1.153 christos (void)printf("%9d ", total.t_avm);
707 1.153 christos (void)printf("%9d ", total.t_arm);
708 1.153 christos (void)printf("%5d ", total.t_vmshr);
709 1.153 christos (void)printf("%6d ", total.t_avmshr);
710 1.153 christos (void)printf("%5d ", total.t_rmshr);
711 1.153 christos (void)printf("%6d ", total.t_armshr);
712 1.153 christos (void)printf("%5d", total.t_free);
713 1.130 he
714 1.153 christos (void)putchar('\n');
715 1.130 he
716 1.130 he (void)fflush(stdout);
717 1.130 he if (reps >= 0 && --reps <= 0)
718 1.130 he break;
719 1.130 he
720 1.153 christos (void)nanosleep(interval, NULL);
721 1.130 he }
722 1.130 he }
723 1.130 he
724 1.130 he void
725 1.103 mycroft dovmstat(struct timespec *interval, int reps)
726 1.1 cgd {
727 1.1 cgd struct vmtotal total;
728 1.1 cgd time_t uptime, halfuptime;
729 1.17 cgd size_t size;
730 1.41 mrg int pagesize = getpagesize();
731 1.151 yamt int ovflw;
732 1.1 cgd
733 1.1 cgd uptime = getuptime();
734 1.1 cgd halfuptime = uptime / 2;
735 1.1 cgd (void)signal(SIGCONT, needhdr);
736 1.1 cgd
737 1.197 joerg if (memf != NULL) {
738 1.197 joerg if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
739 1.197 joerg kread(namelist, X_STATHZ, &hz, sizeof(hz));
740 1.197 joerg if (!hz)
741 1.197 joerg kread(namelist, X_HZ, &hz, sizeof(hz));
742 1.197 joerg } else {
743 1.197 joerg struct clockinfo clockinfo;
744 1.197 joerg size = sizeof(clockinfo);
745 1.197 joerg if (sysctl(clockrate_mib, 2, &clockinfo, &size, NULL, 0) == -1)
746 1.197 joerg err(1, "sysctl kern.clockrate failed");
747 1.197 joerg hz = clockinfo.stathz;
748 1.197 joerg if (!hz)
749 1.197 joerg hz = clockinfo.hz;
750 1.197 joerg }
751 1.1 cgd
752 1.1 cgd for (hdrcnt = 1;;) {
753 1.1 cgd if (!--hdrcnt)
754 1.1 cgd printhdr();
755 1.29 thorpej /* Read new disk statistics */
756 1.139 dsl cpureadstats();
757 1.140 blymn drvreadstats();
758 1.139 dsl tkreadstats();
759 1.58 thorpej if (memf != NULL) {
760 1.196 joerg struct uvmexp uvmexp_kernel;
761 1.58 thorpej /*
762 1.58 thorpej * XXX Can't do this if we're reading a crash
763 1.58 thorpej * XXX dump because they're lazily-calculated.
764 1.58 thorpej */
765 1.170 christos warnx("Unable to get vmtotals from crash dump.");
766 1.153 christos (void)memset(&total, 0, sizeof(total));
767 1.196 joerg kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel));
768 1.196 joerg #define COPY(field) uvmexp.field = uvmexp_kernel.field
769 1.196 joerg COPY(pdreact);
770 1.196 joerg COPY(pageins);
771 1.196 joerg COPY(pgswapout);
772 1.196 joerg COPY(pdfreed);
773 1.196 joerg COPY(pdscans);
774 1.196 joerg #undef COPY
775 1.58 thorpej } else {
776 1.58 thorpej size = sizeof(total);
777 1.170 christos if (sysctl(vmmeter_mib, __arraycount(vmmeter_mib),
778 1.170 christos &total, &size, NULL, 0) == -1) {
779 1.170 christos warn("Can't get vmtotals");
780 1.153 christos (void)memset(&total, 0, sizeof(total));
781 1.58 thorpej }
782 1.196 joerg size = sizeof(uvmexp);
783 1.196 joerg if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp,
784 1.196 joerg &size, NULL, 0) == -1)
785 1.196 joerg warn("sysctl vm.uvmexp2 failed");
786 1.13 cgd }
787 1.171 christos cpucounters(&cpucounter);
788 1.151 yamt ovflw = 0;
789 1.151 yamt PRWORD(ovflw, " %*d", 2, 1, total.t_rq - 1);
790 1.151 yamt PRWORD(ovflw, " %*d", 2, 1, total.t_dw + total.t_pw);
791 1.153 christos #define pgtok(a) (long)((a) * ((uint32_t)pagesize >> 10))
792 1.38 mrg #define rate(x) (u_long)(((x) + halfuptime) / uptime) /* round */
793 1.166 rmind PRWORD(ovflw, " %*ld", 9, 1, pgtok(total.t_avm));
794 1.151 yamt PRWORD(ovflw, " %*ld", 7, 1, pgtok(total.t_free));
795 1.151 yamt PRWORD(ovflw, " %*ld", 5, 1,
796 1.171 christos rate(cpucounter.nfault - ocpucounter.nfault));
797 1.151 yamt PRWORD(ovflw, " %*ld", 4, 1,
798 1.151 yamt rate(uvmexp.pdreact - ouvmexp.pdreact));
799 1.151 yamt PRWORD(ovflw, " %*ld", 4, 1,
800 1.151 yamt rate(uvmexp.pageins - ouvmexp.pageins));
801 1.151 yamt PRWORD(ovflw, " %*ld", 5, 1,
802 1.44 mrg rate(uvmexp.pgswapout - ouvmexp.pgswapout));
803 1.151 yamt PRWORD(ovflw, " %*ld", 5, 1,
804 1.151 yamt rate(uvmexp.pdfreed - ouvmexp.pdfreed));
805 1.151 yamt PRWORD(ovflw, " %*ld", 6, 2,
806 1.151 yamt rate(uvmexp.pdscans - ouvmexp.pdscans));
807 1.151 yamt drvstats(&ovflw);
808 1.151 yamt PRWORD(ovflw, " %*ld", 5, 1,
809 1.171 christos rate(cpucounter.nintr - ocpucounter.nintr));
810 1.151 yamt PRWORD(ovflw, " %*ld", 5, 1,
811 1.171 christos rate(cpucounter.nsyscall - ocpucounter.nsyscall));
812 1.151 yamt PRWORD(ovflw, " %*ld", 4, 1,
813 1.171 christos rate(cpucounter.nswtch - ocpucounter.nswtch));
814 1.151 yamt cpustats(&ovflw);
815 1.153 christos (void)putchar('\n');
816 1.42 mrg (void)fflush(stdout);
817 1.42 mrg if (reps >= 0 && --reps <= 0)
818 1.42 mrg break;
819 1.172 enami ouvmexp = uvmexp;
820 1.171 christos ocpucounter = cpucounter;
821 1.103 mycroft uptime = interval->tv_sec;
822 1.1 cgd /*
823 1.1 cgd * We round upward to avoid losing low-frequency events
824 1.1 cgd * (i.e., >= 1 per interval but < 1 per second).
825 1.1 cgd */
826 1.33 thorpej halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
827 1.153 christos (void)nanosleep(interval, NULL);
828 1.1 cgd }
829 1.1 cgd }
830 1.1 cgd
831 1.38 mrg void
832 1.73 simonb printhdr(void)
833 1.1 cgd {
834 1.165 lukem size_t i;
835 1.1 cgd
836 1.104 mrg (void)printf(" procs memory page%*s", 23, "");
837 1.29 thorpej if (ndrives > 0)
838 1.70 sommerfe (void)printf("%s %*sfaults cpu\n",
839 1.75 enami ((ndrives > 1) ? "disks" : "disk"),
840 1.75 enami ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
841 1.1 cgd else
842 1.29 thorpej (void)printf("%*s faults cpu\n",
843 1.75 enami ndrives * 3, "");
844 1.29 thorpej
845 1.166 rmind (void)printf(" r b avm fre flt re pi po fr sr ");
846 1.140 blymn for (i = 0; i < ndrive; i++)
847 1.140 blymn if (drv_select[i])
848 1.1 cgd (void)printf("%c%c ", dr_name[i][0],
849 1.1 cgd dr_name[i][strlen(dr_name[i]) - 1]);
850 1.1 cgd (void)printf(" in sy cs us sy id\n");
851 1.1 cgd hdrcnt = winlines - 2;
852 1.1 cgd }
853 1.1 cgd
854 1.1 cgd /*
855 1.1 cgd * Force a header to be prepended to the next output.
856 1.1 cgd */
857 1.1 cgd void
858 1.153 christos /*ARGSUSED*/
859 1.73 simonb needhdr(int dummy)
860 1.1 cgd {
861 1.1 cgd
862 1.1 cgd hdrcnt = 1;
863 1.1 cgd }
864 1.1 cgd
865 1.38 mrg long
866 1.203 nakayama pct(u_long top, u_long bot)
867 1.1 cgd {
868 1.13 cgd long ans;
869 1.13 cgd
870 1.1 cgd if (bot == 0)
871 1.75 enami return (0);
872 1.153 christos ans = (long)((quad_t)top * 100 / bot);
873 1.13 cgd return (ans);
874 1.1 cgd }
875 1.1 cgd
876 1.203 nakayama #define PCT(top, bot) (int)pct((u_long)(top), (u_long)(bot))
877 1.1 cgd
878 1.1 cgd void
879 1.73 simonb dosum(void)
880 1.1 cgd {
881 1.206 dennis struct nchstats nch_stats;
882 1.194 joerg uint64_t nchtotal;
883 1.162 he size_t ssize;
884 1.162 he int active_kernel;
885 1.171 christos struct cpu_counter cc;
886 1.162 he
887 1.162 he /*
888 1.162 he * The "active" and "inactive" variables
889 1.162 he * are now estimated by the kernel and sadly
890 1.162 he * can not easily be dug out of a crash dump.
891 1.162 he */
892 1.195 joerg ssize = sizeof(uvmexp);
893 1.195 joerg memset(&uvmexp, 0, ssize);
894 1.162 he active_kernel = (memf == NULL);
895 1.162 he if (active_kernel) {
896 1.162 he /* only on active kernel */
897 1.195 joerg if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp,
898 1.170 christos &ssize, NULL, 0) == -1)
899 1.170 christos warn("sysctl vm.uvmexp2 failed");
900 1.195 joerg } else {
901 1.195 joerg struct uvmexp uvmexp_kernel;
902 1.221 mrg struct pool pool, *pp = &pool;
903 1.222 mrg struct pool_allocator pa;
904 1.221 mrg TAILQ_HEAD(,pool) pool_head;
905 1.221 mrg void *addr;
906 1.222 mrg uint64_t bytes;
907 1.221 mrg
908 1.195 joerg kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel));
909 1.195 joerg #define COPY(field) uvmexp.field = uvmexp_kernel.field
910 1.195 joerg COPY(pagesize);
911 1.195 joerg COPY(ncolors);
912 1.195 joerg COPY(npages);
913 1.195 joerg COPY(free);
914 1.195 joerg COPY(paging);
915 1.195 joerg COPY(wired);
916 1.195 joerg COPY(zeropages);
917 1.195 joerg COPY(reserve_pagedaemon);
918 1.195 joerg COPY(reserve_kernel);
919 1.195 joerg COPY(anonpages);
920 1.195 joerg COPY(filepages);
921 1.195 joerg COPY(execpages);
922 1.195 joerg COPY(freemin);
923 1.195 joerg COPY(freetarg);
924 1.195 joerg COPY(wiredmax);
925 1.195 joerg COPY(nswapdev);
926 1.195 joerg COPY(swpages);
927 1.195 joerg COPY(swpginuse);
928 1.195 joerg COPY(nswget);
929 1.195 joerg COPY(pageins);
930 1.195 joerg COPY(pdpageouts);
931 1.195 joerg COPY(pgswapin);
932 1.195 joerg COPY(pgswapout);
933 1.195 joerg COPY(forks);
934 1.195 joerg COPY(forks_ppwait);
935 1.195 joerg COPY(forks_sharevm);
936 1.195 joerg COPY(pga_zerohit);
937 1.195 joerg COPY(pga_zeromiss);
938 1.195 joerg COPY(zeroaborts);
939 1.195 joerg COPY(colorhit);
940 1.195 joerg COPY(colormiss);
941 1.195 joerg COPY(cpuhit);
942 1.195 joerg COPY(cpumiss);
943 1.195 joerg COPY(fltnoram);
944 1.195 joerg COPY(fltnoanon);
945 1.195 joerg COPY(fltpgwait);
946 1.195 joerg COPY(fltpgrele);
947 1.195 joerg COPY(fltrelck);
948 1.195 joerg COPY(fltrelckok);
949 1.195 joerg COPY(fltanget);
950 1.195 joerg COPY(fltanretry);
951 1.195 joerg COPY(fltamcopy);
952 1.195 joerg COPY(fltamcopy);
953 1.195 joerg COPY(fltnomap);
954 1.195 joerg COPY(fltlget);
955 1.195 joerg COPY(fltget);
956 1.195 joerg COPY(flt_anon);
957 1.195 joerg COPY(flt_acow);
958 1.195 joerg COPY(flt_obj);
959 1.195 joerg COPY(flt_prcopy);
960 1.195 joerg COPY(flt_przero);
961 1.195 joerg COPY(pdwoke);
962 1.195 joerg COPY(pdrevs);
963 1.195 joerg COPY(pdfreed);
964 1.195 joerg COPY(pdscans);
965 1.195 joerg COPY(pdanscan);
966 1.195 joerg COPY(pdobscan);
967 1.195 joerg COPY(pdreact);
968 1.195 joerg COPY(pdbusy);
969 1.195 joerg COPY(pdpending);
970 1.195 joerg COPY(pddeact);
971 1.221 mrg COPY(bootpages);
972 1.195 joerg #undef COPY
973 1.221 mrg kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
974 1.221 mrg addr = TAILQ_FIRST(&pool_head);
975 1.222 mrg uvmexp.poolpages = 0;
976 1.221 mrg for (; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist)) {
977 1.221 mrg deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
978 1.224 mrg deref_kptr(pp->pr_alloc, &pa, sizeof(pa),
979 1.224 mrg "pool allocator trashed");
980 1.222 mrg bytes = pp->pr_npages * pa.pa_pagesz;
981 1.222 mrg if ((pp->pr_roflags & PR_RECURSIVE) != 0)
982 1.222 mrg bytes -= (pp->pr_nout * pp->pr_size);
983 1.222 mrg uvmexp.poolpages += bytes / uvmexp.pagesize;
984 1.221 mrg }
985 1.162 he }
986 1.1 cgd
987 1.41 mrg
988 1.195 joerg (void)printf("%9" PRIu64 " bytes per page\n", uvmexp.pagesize);
989 1.44 mrg
990 1.195 joerg (void)printf("%9" PRIu64 " page color%s\n",
991 1.81 thorpej uvmexp.ncolors, uvmexp.ncolors == 1 ? "" : "s");
992 1.81 thorpej
993 1.195 joerg (void)printf("%9" PRIu64 " pages managed\n", uvmexp.npages);
994 1.195 joerg (void)printf("%9" PRIu64 " pages free\n", uvmexp.free);
995 1.162 he if (active_kernel) {
996 1.195 joerg (void)printf("%9" PRIu64 " pages active\n", uvmexp.active);
997 1.195 joerg (void)printf("%9" PRIu64 " pages inactive\n", uvmexp.inactive);
998 1.162 he }
999 1.195 joerg (void)printf("%9" PRIu64 " pages paging\n", uvmexp.paging);
1000 1.195 joerg (void)printf("%9" PRIu64 " pages wired\n", uvmexp.wired);
1001 1.195 joerg (void)printf("%9" PRIu64 " zero pages\n", uvmexp.zeropages);
1002 1.195 joerg (void)printf("%9" PRIu64 " reserve pagedaemon pages\n",
1003 1.44 mrg uvmexp.reserve_pagedaemon);
1004 1.195 joerg (void)printf("%9" PRIu64 " reserve kernel pages\n", uvmexp.reserve_kernel);
1005 1.221 mrg (void)printf("%9" PRIu64 " boot kernel pages\n", uvmexp.bootpages);
1006 1.221 mrg (void)printf("%9" PRIu64 " kernel pool pages\n", uvmexp.poolpages);
1007 1.195 joerg (void)printf("%9" PRIu64 " anonymous pages\n", uvmexp.anonpages);
1008 1.195 joerg (void)printf("%9" PRIu64 " cached file pages\n", uvmexp.filepages);
1009 1.195 joerg (void)printf("%9" PRIu64 " cached executable pages\n", uvmexp.execpages);
1010 1.195 joerg
1011 1.195 joerg (void)printf("%9" PRIu64 " minimum free pages\n", uvmexp.freemin);
1012 1.195 joerg (void)printf("%9" PRIu64 " target free pages\n", uvmexp.freetarg);
1013 1.195 joerg (void)printf("%9" PRIu64 " maximum wired pages\n", uvmexp.wiredmax);
1014 1.195 joerg
1015 1.195 joerg (void)printf("%9" PRIu64 " swap devices\n", uvmexp.nswapdev);
1016 1.195 joerg (void)printf("%9" PRIu64 " swap pages\n", uvmexp.swpages);
1017 1.195 joerg (void)printf("%9" PRIu64 " swap pages in use\n", uvmexp.swpginuse);
1018 1.195 joerg (void)printf("%9" PRIu64 " swap allocations\n", uvmexp.nswget);
1019 1.44 mrg
1020 1.171 christos cpucounters(&cc);
1021 1.190 rmind
1022 1.171 christos (void)printf("%9" PRIu64 " total faults taken\n", cc.nfault);
1023 1.171 christos (void)printf("%9" PRIu64 " traps\n", cc.ntrap);
1024 1.171 christos (void)printf("%9" PRIu64 " device interrupts\n", cc.nintr);
1025 1.171 christos (void)printf("%9" PRIu64 " CPU context switches\n", cc.nswtch);
1026 1.171 christos (void)printf("%9" PRIu64 " software interrupts\n", cc.nsoft);
1027 1.171 christos (void)printf("%9" PRIu64 " system calls\n", cc.nsyscall);
1028 1.195 joerg (void)printf("%9" PRIu64 " pagein requests\n", uvmexp.pageins);
1029 1.195 joerg (void)printf("%9" PRIu64 " pageout requests\n", uvmexp.pdpageouts);
1030 1.195 joerg (void)printf("%9" PRIu64 " pages swapped in\n", uvmexp.pgswapin);
1031 1.195 joerg (void)printf("%9" PRIu64 " pages swapped out\n", uvmexp.pgswapout);
1032 1.195 joerg (void)printf("%9" PRIu64 " forks total\n", uvmexp.forks);
1033 1.195 joerg (void)printf("%9" PRIu64 " forks blocked parent\n", uvmexp.forks_ppwait);
1034 1.195 joerg (void)printf("%9" PRIu64 " forks shared address space with parent\n",
1035 1.43 mrg uvmexp.forks_sharevm);
1036 1.195 joerg (void)printf("%9" PRIu64 " pagealloc zero wanted and avail\n",
1037 1.63 thorpej uvmexp.pga_zerohit);
1038 1.195 joerg (void)printf("%9" PRIu64 " pagealloc zero wanted and not avail\n",
1039 1.63 thorpej uvmexp.pga_zeromiss);
1040 1.195 joerg (void)printf("%9" PRIu64 " aborts of idle page zeroing\n",
1041 1.68 thorpej uvmexp.zeroaborts);
1042 1.195 joerg (void)printf("%9" PRIu64 " pagealloc desired color avail\n",
1043 1.79 thorpej uvmexp.colorhit);
1044 1.195 joerg (void)printf("%9" PRIu64 " pagealloc desired color not avail\n",
1045 1.79 thorpej uvmexp.colormiss);
1046 1.195 joerg (void)printf("%9" PRIu64 " pagealloc local cpu avail\n",
1047 1.159 ad uvmexp.cpuhit);
1048 1.195 joerg (void)printf("%9" PRIu64 " pagealloc local cpu not avail\n",
1049 1.159 ad uvmexp.cpumiss);
1050 1.44 mrg
1051 1.195 joerg (void)printf("%9" PRIu64 " faults with no memory\n", uvmexp.fltnoram);
1052 1.195 joerg (void)printf("%9" PRIu64 " faults with no anons\n", uvmexp.fltnoanon);
1053 1.195 joerg (void)printf("%9" PRIu64 " faults had to wait on pages\n", uvmexp.fltpgwait);
1054 1.195 joerg (void)printf("%9" PRIu64 " faults found released page\n", uvmexp.fltpgrele);
1055 1.195 joerg (void)printf("%9" PRIu64 " faults relock (%" PRIu64 " ok)\n", uvmexp.fltrelck,
1056 1.43 mrg uvmexp.fltrelckok);
1057 1.195 joerg (void)printf("%9" PRIu64 " anon page faults\n", uvmexp.fltanget);
1058 1.195 joerg (void)printf("%9" PRIu64 " anon retry faults\n", uvmexp.fltanretry);
1059 1.195 joerg (void)printf("%9" PRIu64 " amap copy faults\n", uvmexp.fltamcopy);
1060 1.195 joerg (void)printf("%9" PRIu64 " neighbour anon page faults\n", uvmexp.fltnamap);
1061 1.195 joerg (void)printf("%9" PRIu64 " neighbour object page faults\n", uvmexp.fltnomap);
1062 1.195 joerg (void)printf("%9" PRIu64 " locked pager get faults\n", uvmexp.fltlget);
1063 1.195 joerg (void)printf("%9" PRIu64 " unlocked pager get faults\n", uvmexp.fltget);
1064 1.195 joerg (void)printf("%9" PRIu64 " anon faults\n", uvmexp.flt_anon);
1065 1.195 joerg (void)printf("%9" PRIu64 " anon copy on write faults\n", uvmexp.flt_acow);
1066 1.195 joerg (void)printf("%9" PRIu64 " object faults\n", uvmexp.flt_obj);
1067 1.195 joerg (void)printf("%9" PRIu64 " promote copy faults\n", uvmexp.flt_prcopy);
1068 1.195 joerg (void)printf("%9" PRIu64 " promote zero fill faults\n", uvmexp.flt_przero);
1069 1.195 joerg
1070 1.195 joerg (void)printf("%9" PRIu64 " times daemon wokeup\n",uvmexp.pdwoke);
1071 1.195 joerg (void)printf("%9" PRIu64 " revolutions of the clock hand\n", uvmexp.pdrevs);
1072 1.195 joerg (void)printf("%9" PRIu64 " pages freed by daemon\n", uvmexp.pdfreed);
1073 1.195 joerg (void)printf("%9" PRIu64 " pages scanned by daemon\n", uvmexp.pdscans);
1074 1.195 joerg (void)printf("%9" PRIu64 " anonymous pages scanned by daemon\n",
1075 1.75 enami uvmexp.pdanscan);
1076 1.195 joerg (void)printf("%9" PRIu64 " object pages scanned by daemon\n", uvmexp.pdobscan);
1077 1.195 joerg (void)printf("%9" PRIu64 " pages reactivated\n", uvmexp.pdreact);
1078 1.195 joerg (void)printf("%9" PRIu64 " pages found busy by daemon\n", uvmexp.pdbusy);
1079 1.195 joerg (void)printf("%9" PRIu64 " total pending pageouts\n", uvmexp.pdpending);
1080 1.195 joerg (void)printf("%9" PRIu64 " pages deactivated\n", uvmexp.pddeact);
1081 1.123 enami
1082 1.194 joerg if (active_kernel) {
1083 1.194 joerg ssize = sizeof(nch_stats);
1084 1.194 joerg if (sysctlbyname("vfs.namecache_stats", &nch_stats, &ssize,
1085 1.194 joerg NULL, 0)) {
1086 1.194 joerg warn("vfs.namecache_stats failed");
1087 1.194 joerg memset(&nch_stats, 0, sizeof(nch_stats));
1088 1.194 joerg }
1089 1.194 joerg } else {
1090 1.206 dennis kread(namelist, X_NCHSTATS, &nch_stats, sizeof(nch_stats));
1091 1.194 joerg }
1092 1.194 joerg
1093 1.194 joerg nchtotal = nch_stats.ncs_goodhits + nch_stats.ncs_neghits +
1094 1.194 joerg nch_stats.ncs_badhits + nch_stats.ncs_falsehits +
1095 1.194 joerg nch_stats.ncs_miss + nch_stats.ncs_long;
1096 1.194 joerg (void)printf("%9" PRIu64 " total name lookups\n", nchtotal);
1097 1.194 joerg (void)printf("%9" PRIu64 " good hits\n", nch_stats.ncs_goodhits);
1098 1.194 joerg (void)printf("%9" PRIu64 " negative hits\n", nch_stats.ncs_neghits);
1099 1.194 joerg (void)printf("%9" PRIu64 " bad hits\n", nch_stats.ncs_badhits);
1100 1.194 joerg (void)printf("%9" PRIu64 " false hits\n", nch_stats.ncs_falsehits);
1101 1.194 joerg (void)printf("%9" PRIu64 " miss\n", nch_stats.ncs_miss);
1102 1.194 joerg (void)printf("%9" PRIu64 " too long\n", nch_stats.ncs_long);
1103 1.194 joerg (void)printf("%9" PRIu64 " pass2 hits\n", nch_stats.ncs_pass2);
1104 1.194 joerg (void)printf("%9" PRIu64 " 2passes\n", nch_stats.ncs_2passes);
1105 1.1 cgd (void)printf(
1106 1.1 cgd "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
1107 1.194 joerg "", PCT(nch_stats.ncs_goodhits, nchtotal),
1108 1.194 joerg PCT(nch_stats.ncs_neghits, nchtotal),
1109 1.194 joerg PCT(nch_stats.ncs_pass2, nchtotal));
1110 1.1 cgd (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
1111 1.194 joerg PCT(nch_stats.ncs_badhits, nchtotal),
1112 1.194 joerg PCT(nch_stats.ncs_falsehits, nchtotal),
1113 1.194 joerg PCT(nch_stats.ncs_long, nchtotal));
1114 1.1 cgd }
1115 1.1 cgd
1116 1.1 cgd void
1117 1.73 simonb doforkst(void)
1118 1.1 cgd {
1119 1.196 joerg if (memf != NULL) {
1120 1.196 joerg struct uvmexp uvmexp_kernel;
1121 1.196 joerg kread(namelist, X_UVMEXP, &uvmexp_kernel, sizeof(uvmexp_kernel));
1122 1.196 joerg #define COPY(field) uvmexp.field = uvmexp_kernel.field
1123 1.196 joerg COPY(forks);
1124 1.196 joerg COPY(forks_ppwait);
1125 1.196 joerg COPY(forks_sharevm);
1126 1.196 joerg #undef COPY
1127 1.196 joerg } else {
1128 1.196 joerg size_t size = sizeof(uvmexp);
1129 1.196 joerg if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp,
1130 1.196 joerg &size, NULL, 0) == -1)
1131 1.196 joerg warn("sysctl vm.uvmexp2 failed");
1132 1.196 joerg }
1133 1.58 thorpej
1134 1.196 joerg (void)printf("%" PRIu64 " forks total\n", uvmexp.forks);
1135 1.196 joerg (void)printf("%" PRIu64 " forks blocked parent\n", uvmexp.forks_ppwait);
1136 1.196 joerg (void)printf("%" PRIu64 " forks shared address space with parent\n",
1137 1.41 mrg uvmexp.forks_sharevm);
1138 1.1 cgd }
1139 1.1 cgd
1140 1.1 cgd void
1141 1.151 yamt drvstats(int *ovflwp)
1142 1.1 cgd {
1143 1.165 lukem size_t dn;
1144 1.217 mlelstv double dtime;
1145 1.151 yamt int ovflw = *ovflwp;
1146 1.1 cgd
1147 1.29 thorpej /* Calculate disk stat deltas. */
1148 1.139 dsl cpuswap();
1149 1.140 blymn drvswap();
1150 1.139 dsl tkswap();
1151 1.101 sommerfe
1152 1.140 blymn for (dn = 0; dn < ndrive; ++dn) {
1153 1.217 mlelstv /* elapsed time for disk stats */
1154 1.218 mlelstv dtime = cur.cp_etime;
1155 1.218 mlelstv if (cur.timestamp[dn].tv_sec || cur.timestamp[dn].tv_usec) {
1156 1.218 mlelstv dtime = (double)cur.timestamp[dn].tv_sec +
1157 1.218 mlelstv ((double)cur.timestamp[dn].tv_usec / (double)1000000);
1158 1.218 mlelstv }
1159 1.217 mlelstv
1160 1.140 blymn if (!drv_select[dn])
1161 1.151 yamt continue;
1162 1.151 yamt PRWORD(ovflw, " %*.0f", 3, 1,
1163 1.217 mlelstv (cur.rxfer[dn] + cur.wxfer[dn]) / dtime);
1164 1.136 blymn }
1165 1.151 yamt *ovflwp = ovflw;
1166 1.136 blymn }
1167 1.136 blymn
1168 1.136 blymn void
1169 1.171 christos cpucounters(struct cpu_counter *cc)
1170 1.171 christos {
1171 1.198 joerg static struct cpu_info **cpu_infos;
1172 1.198 joerg static int initialised;
1173 1.198 joerg struct cpu_info **slot;
1174 1.198 joerg
1175 1.198 joerg if (memf == NULL) {
1176 1.198 joerg cc->nintr = uvmexp.intrs;
1177 1.198 joerg cc->nsyscall = uvmexp.syscalls;
1178 1.198 joerg cc->nswtch = uvmexp.swtch;
1179 1.198 joerg cc->nfault = uvmexp.faults;
1180 1.198 joerg cc->ntrap = uvmexp.traps;
1181 1.198 joerg cc->nsoft = uvmexp.softs;
1182 1.198 joerg return;
1183 1.198 joerg }
1184 1.198 joerg
1185 1.198 joerg if (!initialised) {
1186 1.198 joerg kread(namelist, X_CPU_INFOS, &cpu_infos, sizeof(cpu_infos));
1187 1.198 joerg initialised = 1;
1188 1.198 joerg }
1189 1.198 joerg
1190 1.198 joerg slot = cpu_infos;
1191 1.190 rmind
1192 1.190 rmind memset(cc, 0, sizeof(*cc));
1193 1.190 rmind
1194 1.190 rmind for (;;) {
1195 1.190 rmind struct cpu_info tci, *ci = NULL;
1196 1.190 rmind
1197 1.190 rmind deref_kptr(slot++, &ci, sizeof(ci), "CPU array trashed");
1198 1.190 rmind if (!ci) {
1199 1.190 rmind break;
1200 1.190 rmind }
1201 1.190 rmind
1202 1.171 christos if ((size_t)kvm_read(kd, (u_long)ci, &tci, sizeof(tci))
1203 1.171 christos != sizeof(tci)) {
1204 1.190 rmind warnx("Can't read cpu info from %p (%s)",
1205 1.190 rmind ci, kvm_geterr(kd));
1206 1.190 rmind memset(cc, 0, sizeof(*cc));
1207 1.190 rmind return;
1208 1.171 christos }
1209 1.171 christos cc->nintr += tci.ci_data.cpu_nintr;
1210 1.171 christos cc->nsyscall += tci.ci_data.cpu_nsyscall;
1211 1.171 christos cc->nswtch = tci.ci_data.cpu_nswtch;
1212 1.171 christos cc->nfault = tci.ci_data.cpu_nfault;
1213 1.171 christos cc->ntrap = tci.ci_data.cpu_ntrap;
1214 1.171 christos cc->nsoft = tci.ci_data.cpu_nsoft;
1215 1.171 christos }
1216 1.171 christos }
1217 1.171 christos
1218 1.171 christos void
1219 1.151 yamt cpustats(int *ovflwp)
1220 1.1 cgd {
1221 1.38 mrg int state;
1222 1.129 dsl double pcnt, total;
1223 1.120 dbj double stat_us, stat_sy, stat_id;
1224 1.151 yamt int ovflw = *ovflwp;
1225 1.1 cgd
1226 1.1 cgd total = 0;
1227 1.1 cgd for (state = 0; state < CPUSTATES; ++state)
1228 1.29 thorpej total += cur.cp_time[state];
1229 1.1 cgd if (total)
1230 1.129 dsl pcnt = 100 / total;
1231 1.1 cgd else
1232 1.129 dsl pcnt = 0;
1233 1.129 dsl stat_us = (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pcnt;
1234 1.129 dsl stat_sy = (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pcnt;
1235 1.129 dsl stat_id = cur.cp_time[CP_IDLE] * pcnt;
1236 1.151 yamt PRWORD(ovflw, " %*.0f", ((stat_sy >= 100) ? 2 : 3), 1, stat_us);
1237 1.151 yamt PRWORD(ovflw, " %*.0f", ((stat_us >= 100 || stat_id >= 100) ? 2 : 3), 1,
1238 1.151 yamt stat_sy);
1239 1.151 yamt PRWORD(ovflw, " %*.0f", 3, 1, stat_id);
1240 1.151 yamt *ovflwp = ovflw;
1241 1.1 cgd }
1242 1.1 cgd
1243 1.1 cgd void
1244 1.66 cgd dointr(int verbose)
1245 1.1 cgd {
1246 1.138 nonaka unsigned long *intrcnt, *ointrcnt;
1247 1.85 enami unsigned long long inttotal, uptime;
1248 1.38 mrg int nintr, inamlen;
1249 1.138 nonaka char *intrname, *ointrname;
1250 1.1 cgd
1251 1.133 chs inttotal = 0;
1252 1.1 cgd uptime = getuptime();
1253 1.133 chs nintr = intrnl[X_EINTRCNT].n_value - intrnl[X_INTRCNT].n_value;
1254 1.133 chs inamlen = intrnl[X_EINTRNAMES].n_value - intrnl[X_INTRNAMES].n_value;
1255 1.133 chs if (nintr != 0 && inamlen != 0) {
1256 1.216 ryo (void)printf("%-34s %16s %8s\n", "interrupt", "total", "rate");
1257 1.216 ryo
1258 1.138 nonaka ointrcnt = intrcnt = malloc((size_t)nintr);
1259 1.138 nonaka ointrname = intrname = malloc((size_t)inamlen);
1260 1.133 chs if (intrcnt == NULL || intrname == NULL)
1261 1.133 chs errx(1, "%s", "");
1262 1.133 chs kread(intrnl, X_INTRCNT, intrcnt, (size_t)nintr);
1263 1.133 chs kread(intrnl, X_INTRNAMES, intrname, (size_t)inamlen);
1264 1.133 chs nintr /= sizeof(long);
1265 1.133 chs while (--nintr >= 0) {
1266 1.133 chs if (*intrcnt || verbose)
1267 1.133 chs (void)printf("%-34s %16llu %8llu\n", intrname,
1268 1.133 chs (unsigned long long)*intrcnt,
1269 1.133 chs (unsigned long long)
1270 1.133 chs (*intrcnt / uptime));
1271 1.133 chs intrname += strlen(intrname) + 1;
1272 1.133 chs inttotal += *intrcnt++;
1273 1.133 chs }
1274 1.138 nonaka free(ointrcnt);
1275 1.138 nonaka free(ointrname);
1276 1.1 cgd }
1277 1.133 chs
1278 1.176 matt doevcnt(verbose, EVCNT_TYPE_INTR);
1279 1.66 cgd }
1280 1.66 cgd
1281 1.66 cgd void
1282 1.176 matt doevcnt(int verbose, int type)
1283 1.66 cgd {
1284 1.176 matt static const char * const evtypes [] = { "misc", "intr", "trap" };
1285 1.176 matt uint64_t counttotal, uptime;
1286 1.66 cgd struct evcntlist allevents;
1287 1.66 cgd struct evcnt evcnt, *evptr;
1288 1.216 ryo size_t evlen_max, total_max, rate_max;
1289 1.66 cgd char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
1290 1.66 cgd
1291 1.176 matt counttotal = 0;
1292 1.66 cgd uptime = getuptime();
1293 1.176 matt
1294 1.176 matt if (memf == NULL) do {
1295 1.176 matt const int mib[4] = { CTL_KERN, KERN_EVCNT, type,
1296 1.176 matt verbose ? KERN_EVCNT_COUNT_ANY : KERN_EVCNT_COUNT_NONZERO };
1297 1.216 ryo size_t buflen0, buflen = 0;
1298 1.216 ryo void *buf0, *buf = NULL;
1299 1.176 matt const struct evcnt_sysctl *evs, *last_evs;
1300 1.176 matt for (;;) {
1301 1.176 matt size_t newlen;
1302 1.176 matt int error;
1303 1.176 matt if (buflen)
1304 1.176 matt buf = malloc(buflen);
1305 1.176 matt error = sysctl(mib, __arraycount(mib),
1306 1.176 matt buf, &newlen, NULL, 0);
1307 1.176 matt if (error) {
1308 1.193 joerg err(1, "kern.evcnt");
1309 1.176 matt if (buf)
1310 1.176 matt free(buf);
1311 1.176 matt return;
1312 1.176 matt }
1313 1.176 matt if (newlen <= buflen) {
1314 1.176 matt buflen = newlen;
1315 1.176 matt break;
1316 1.176 matt }
1317 1.176 matt if (buf)
1318 1.176 matt free(buf);
1319 1.176 matt buflen = newlen;
1320 1.176 matt }
1321 1.216 ryo buflen0 = buflen;
1322 1.216 ryo evs = buf0 = buf;
1323 1.216 ryo last_evs = (void *)((char *)buf + buflen);
1324 1.216 ryo buflen /= sizeof(uint64_t);
1325 1.216 ryo /* calc columns */
1326 1.216 ryo evlen_max = 0;
1327 1.216 ryo total_max = sizeof("total") - 1;
1328 1.216 ryo rate_max = sizeof("rate") - 1;
1329 1.216 ryo while (evs < last_evs
1330 1.216 ryo && buflen >= sizeof(*evs)/sizeof(uint64_t)
1331 1.216 ryo && buflen >= evs->ev_len) {
1332 1.216 ryo char cbuf[64];
1333 1.216 ryo size_t len;
1334 1.216 ryo len = strlen(evs->ev_strings + evs->ev_grouplen + 1);
1335 1.216 ryo len += evs->ev_grouplen + 1;
1336 1.216 ryo if (evlen_max < len)
1337 1.216 ryo evlen_max= len;
1338 1.216 ryo len = snprintf(cbuf, sizeof(cbuf), "%"PRIu64,
1339 1.216 ryo evs->ev_count);
1340 1.216 ryo if (total_max < len)
1341 1.216 ryo total_max = len;
1342 1.216 ryo len = snprintf(cbuf, sizeof(cbuf), "%"PRIu64,
1343 1.216 ryo evs->ev_count / uptime);
1344 1.216 ryo if (rate_max < len)
1345 1.216 ryo rate_max = len;
1346 1.216 ryo buflen -= evs->ev_len;
1347 1.216 ryo evs = (const void *)
1348 1.216 ryo ((const uint64_t *)evs + evs->ev_len);
1349 1.216 ryo }
1350 1.216 ryo
1351 1.216 ryo (void)printf(type == EVCNT_TYPE_ANY ?
1352 1.216 ryo "%-*s %*s %*s %s\n" :
1353 1.216 ryo "%-*s %*s %*s\n",
1354 1.216 ryo (int)evlen_max, "interrupt",
1355 1.216 ryo (int)total_max, "total",
1356 1.216 ryo (int)rate_max, "rate",
1357 1.216 ryo "type");
1358 1.216 ryo
1359 1.216 ryo buflen = buflen0;
1360 1.216 ryo evs = buf0;
1361 1.176 matt last_evs = (void *)((char *)buf + buflen);
1362 1.176 matt buflen /= sizeof(uint64_t);
1363 1.176 matt while (evs < last_evs
1364 1.176 matt && buflen >= sizeof(*evs)/sizeof(uint64_t)
1365 1.176 matt && buflen >= evs->ev_len) {
1366 1.180 nakayama (void)printf(type == EVCNT_TYPE_ANY ?
1367 1.216 ryo "%s %s%*s %*"PRIu64" %*"PRIu64" %s\n" :
1368 1.216 ryo "%s %s%*s %*"PRIu64" %*"PRIu64"\n",
1369 1.176 matt evs->ev_strings,
1370 1.176 matt evs->ev_strings + evs->ev_grouplen + 1,
1371 1.216 ryo (int)evlen_max - (evs->ev_grouplen + 1
1372 1.216 ryo + evs->ev_namelen), "",
1373 1.216 ryo (int)total_max, evs->ev_count,
1374 1.216 ryo (int)rate_max, evs->ev_count / uptime,
1375 1.176 matt (evs->ev_type < __arraycount(evtypes) ?
1376 1.216 ryo evtypes[evs->ev_type] : "?"));
1377 1.176 matt buflen -= evs->ev_len;
1378 1.180 nakayama counttotal += evs->ev_count;
1379 1.216 ryo evs = (const void *)
1380 1.216 ryo ((const uint64_t *)evs + evs->ev_len);
1381 1.176 matt }
1382 1.176 matt free(buf);
1383 1.176 matt if (type != EVCNT_TYPE_ANY)
1384 1.216 ryo (void)printf("%-*s %*"PRIu64" %*"PRIu64"\n",
1385 1.216 ryo (int)evlen_max, "Total",
1386 1.216 ryo (int)total_max, counttotal,
1387 1.216 ryo (int)rate_max, counttotal / uptime);
1388 1.176 matt return;
1389 1.176 matt } while (/*CONSTCOND*/ 0);
1390 1.176 matt
1391 1.216 ryo if (type == EVCNT_TYPE_ANY)
1392 1.216 ryo (void)printf("%-34s %16s %8s %s\n", "event", "total", "rate",
1393 1.216 ryo "type");
1394 1.216 ryo
1395 1.133 chs kread(namelist, X_ALLEVENTS, &allevents, sizeof allevents);
1396 1.133 chs evptr = TAILQ_FIRST(&allevents);
1397 1.66 cgd while (evptr) {
1398 1.87 lukem deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed");
1399 1.32 cgd
1400 1.133 chs evptr = TAILQ_NEXT(&evcnt, ev_list);
1401 1.66 cgd if (evcnt.ev_count == 0 && !verbose)
1402 1.66 cgd continue;
1403 1.176 matt if (type != EVCNT_TYPE_ANY && evcnt.ev_type != type)
1404 1.176 matt continue;
1405 1.66 cgd
1406 1.153 christos deref_kptr(evcnt.ev_group, evgroup,
1407 1.153 christos (size_t)evcnt.ev_grouplen + 1, "event chain trashed");
1408 1.153 christos deref_kptr(evcnt.ev_name, evname,
1409 1.153 christos (size_t)evcnt.ev_namelen + 1, "event chain trashed");
1410 1.66 cgd
1411 1.180 nakayama (void)printf(type == EVCNT_TYPE_ANY ?
1412 1.180 nakayama "%s %s%*s %16"PRIu64" %8"PRIu64" %s\n" :
1413 1.180 nakayama "%s %s%*s %16"PRIu64" %8"PRIu64"\n",
1414 1.176 matt evgroup, evname,
1415 1.83 matt 34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
1416 1.176 matt evcnt.ev_count,
1417 1.176 matt (evcnt.ev_count / uptime),
1418 1.176 matt (evcnt.ev_type < __arraycount(evtypes) ?
1419 1.86 enami evtypes[evcnt.ev_type] : "?"));
1420 1.176 matt
1421 1.180 nakayama counttotal += evcnt.ev_count;
1422 1.18 pk }
1423 1.176 matt if (type != EVCNT_TYPE_ANY)
1424 1.176 matt (void)printf("%-34s %16"PRIu64" %8"PRIu64"\n",
1425 1.176 matt "Total", counttotal, counttotal / uptime);
1426 1.1 cgd }
1427 1.1 cgd
1428 1.200 joerg static void
1429 1.200 joerg dopool_sysctl(int verbose, int wide)
1430 1.200 joerg {
1431 1.200 joerg uint64_t total, inuse, this_total, this_inuse;
1432 1.200 joerg struct {
1433 1.200 joerg uint64_t pt_nget;
1434 1.200 joerg uint64_t pt_nfail;
1435 1.200 joerg uint64_t pt_nput;
1436 1.200 joerg uint64_t pt_nout;
1437 1.200 joerg uint64_t pt_nitems;
1438 1.200 joerg uint64_t pt_npagealloc;
1439 1.200 joerg uint64_t pt_npagefree;
1440 1.200 joerg uint64_t pt_npages;
1441 1.200 joerg } pool_totals;
1442 1.200 joerg size_t i, len;
1443 1.200 joerg int name_len, ovflw;
1444 1.200 joerg struct pool_sysctl *pp, *data;
1445 1.226 simonb char maxp[32];
1446 1.200 joerg
1447 1.200 joerg data = asysctlbyname("kern.pool", &len);
1448 1.200 joerg if (data == NULL)
1449 1.225 sevan err(1, "failed to read kern.pool");
1450 1.200 joerg
1451 1.202 joerg memset(&pool_totals, 0, sizeof pool_totals);
1452 1.200 joerg total = inuse = 0;
1453 1.200 joerg len /= sizeof(*data);
1454 1.200 joerg
1455 1.200 joerg (void)printf("Memory resource pool statistics\n");
1456 1.200 joerg (void)printf(
1457 1.226 simonb "%-*s%*s%*s%*s%*s%s%s%*s%*s%*s%s%*s%6s%*s%5s%s%s\n",
1458 1.200 joerg wide ? 16 : 11, "Name",
1459 1.226 simonb wide ? 7 : 5, "Size",
1460 1.200 joerg wide ? 12 : 9, "Requests",
1461 1.226 simonb wide ? 8 : 5, "Fail",
1462 1.200 joerg wide ? 12 : 9, "Releases",
1463 1.226 simonb wide ? " InUse" : "",
1464 1.226 simonb wide ? " Avail" : "",
1465 1.226 simonb wide ? 11 : 6, "Pgreq",
1466 1.226 simonb wide ? 11 : 6, "Pgrel",
1467 1.226 simonb wide ? 8 : 6, "Npage",
1468 1.200 joerg wide ? " PageSz" : "",
1469 1.226 simonb wide ? 7 : 6, "Hiwat",
1470 1.200 joerg "Minpg",
1471 1.226 simonb wide ? 7 : 6, "Maxpg",
1472 1.200 joerg "Idle",
1473 1.226 simonb wide ? " Flags" : "",
1474 1.200 joerg wide ? " Util" : "");
1475 1.200 joerg
1476 1.200 joerg name_len = MIN((int)sizeof(pp->pr_wchan), wide ? 16 : 11);
1477 1.200 joerg for (i = 0; i < len; ++i) {
1478 1.200 joerg pp = &data[i];
1479 1.200 joerg if (pp->pr_nget == 0 && !verbose)
1480 1.200 joerg continue;
1481 1.200 joerg if (pp->pr_maxpages == UINT_MAX)
1482 1.200 joerg (void)snprintf(maxp, sizeof(maxp), "inf");
1483 1.200 joerg else
1484 1.200 joerg (void)snprintf(maxp, sizeof(maxp), "%" PRIu64,
1485 1.200 joerg pp->pr_maxpages);
1486 1.200 joerg ovflw = 0;
1487 1.200 joerg PRWORD(ovflw, "%-*s", name_len, 0, pp->pr_wchan);
1488 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 7 : 5, 1, pp->pr_size);
1489 1.200 joerg PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pp->pr_nget);
1490 1.200 joerg pool_totals.pt_nget += pp->pr_nget;
1491 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 5, 1, pp->pr_nfail);
1492 1.200 joerg pool_totals.pt_nfail += pp->pr_nfail;
1493 1.200 joerg PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pp->pr_nput);
1494 1.200 joerg pool_totals.pt_nput += pp->pr_nput;
1495 1.200 joerg if (wide) {
1496 1.226 simonb PRWORD(ovflw, " %*" PRIu64, 9, 1, pp->pr_nout);
1497 1.200 joerg pool_totals.pt_nout += pp->pr_nout;
1498 1.226 simonb PRWORD(ovflw, " %*" PRIu64, 9, 1, pp->pr_nitems);
1499 1.200 joerg pool_totals.pt_nitems += pp->pr_nitems;
1500 1.200 joerg }
1501 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pp->pr_npagealloc);
1502 1.200 joerg pool_totals.pt_npagealloc += pp->pr_npagealloc;
1503 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pp->pr_npagefree);
1504 1.200 joerg pool_totals.pt_npagefree += pp->pr_npagefree;
1505 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 6, 1, pp->pr_npages);
1506 1.200 joerg pool_totals.pt_npages += pp->pr_npages;
1507 1.200 joerg if (wide)
1508 1.200 joerg PRWORD(ovflw, " %*" PRIu64, 7, 1, pp->pr_pagesize);
1509 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 7 : 6, 1, pp->pr_hiwat);
1510 1.200 joerg PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_minpages);
1511 1.226 simonb PRWORD(ovflw, " %*s", wide ? 7 : 6, 1, maxp);
1512 1.200 joerg PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_nidle);
1513 1.200 joerg if (wide)
1514 1.226 simonb PRWORD(ovflw, " 0x%0*" PRIx64, 5, 1,
1515 1.200 joerg pp->pr_flags);
1516 1.200 joerg
1517 1.200 joerg this_inuse = pp->pr_nout * pp->pr_size;
1518 1.200 joerg this_total = pp->pr_npages * pp->pr_pagesize;
1519 1.200 joerg if (pp->pr_flags & PR_RECURSIVE) {
1520 1.200 joerg /*
1521 1.200 joerg * Don't count in-use memory, since it's part
1522 1.200 joerg * of another pool and will be accounted for
1523 1.200 joerg * there.
1524 1.200 joerg */
1525 1.200 joerg total += (this_total - this_inuse);
1526 1.200 joerg } else {
1527 1.200 joerg inuse += this_inuse;
1528 1.200 joerg total += this_total;
1529 1.200 joerg }
1530 1.200 joerg if (wide) {
1531 1.200 joerg if (this_total == 0)
1532 1.200 joerg (void)printf(" ---");
1533 1.200 joerg else
1534 1.200 joerg (void)printf(" %5.1f%%",
1535 1.200 joerg (100.0 * this_inuse) / this_total);
1536 1.200 joerg }
1537 1.200 joerg (void)printf("\n");
1538 1.200 joerg }
1539 1.226 simonb ovflw = 0;
1540 1.226 simonb PRWORD(ovflw, "%-*s", name_len, 0, "Totals");
1541 1.226 simonb PRWORD(ovflw, " %*s", wide ? 7 : 5, 1, "");
1542 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pool_totals.pt_nget);
1543 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 5, 1, pool_totals.pt_nfail);
1544 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pool_totals.pt_nput);
1545 1.200 joerg if (wide) {
1546 1.226 simonb PRWORD(ovflw, " %*" PRIu64, 9, 1, pool_totals.pt_nout);
1547 1.226 simonb PRWORD(ovflw, " %*" PRIu64, 9, 1, pool_totals.pt_nitems);
1548 1.200 joerg }
1549 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pool_totals.pt_npagealloc);
1550 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pool_totals.pt_npagefree);
1551 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 6, 1, pool_totals.pt_npages);
1552 1.226 simonb (void)printf("\n");
1553 1.200 joerg
1554 1.200 joerg inuse /= KILO;
1555 1.200 joerg total /= KILO;
1556 1.200 joerg (void)printf(
1557 1.201 joerg "\nIn use %" PRIu64 "K, "
1558 1.201 joerg "total allocated %" PRIu64 "K; utilization %.1f%%\n",
1559 1.200 joerg inuse, total, (100.0 * inuse) / total);
1560 1.200 joerg
1561 1.200 joerg free(data);
1562 1.200 joerg }
1563 1.200 joerg
1564 1.51 pk void
1565 1.126 simonb dopool(int verbose, int wide)
1566 1.51 pk {
1567 1.69 enami int first, ovflw;
1568 1.87 lukem void *addr;
1569 1.126 simonb long total, inuse, this_total, this_inuse;
1570 1.189 mrg struct {
1571 1.189 mrg uint64_t pt_nget;
1572 1.189 mrg uint64_t pt_nfail;
1573 1.189 mrg uint64_t pt_nput;
1574 1.189 mrg uint64_t pt_nout;
1575 1.189 mrg uint64_t pt_nitems;
1576 1.189 mrg uint64_t pt_npagealloc;
1577 1.189 mrg uint64_t pt_npagefree;
1578 1.189 mrg uint64_t pt_npages;
1579 1.189 mrg } pool_totals;
1580 1.157 ad TAILQ_HEAD(,pool) pool_head;
1581 1.51 pk struct pool pool, *pp = &pool;
1582 1.98 christos struct pool_allocator pa;
1583 1.226 simonb char maxp[32], name[32];
1584 1.51 pk
1585 1.200 joerg if (memf == NULL)
1586 1.200 joerg return dopool_sysctl(verbose, wide);
1587 1.200 joerg
1588 1.189 mrg memset(&pool_totals, 0, sizeof pool_totals);
1589 1.133 chs kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
1590 1.157 ad addr = TAILQ_FIRST(&pool_head);
1591 1.51 pk
1592 1.126 simonb total = inuse = 0;
1593 1.126 simonb
1594 1.157 ad for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) {
1595 1.87 lukem deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
1596 1.98 christos deref_kptr(pp->pr_alloc, &pa, sizeof(pa),
1597 1.125 dsainty "pool allocator trashed");
1598 1.87 lukem deref_kptr(pp->pr_wchan, name, sizeof(name),
1599 1.98 christos "pool wait channel trashed");
1600 1.87 lukem name[sizeof(name)-1] = '\0';
1601 1.51 pk
1602 1.51 pk if (first) {
1603 1.51 pk (void)printf("Memory resource pool statistics\n");
1604 1.51 pk (void)printf(
1605 1.226 simonb "%-*s%*s%*s%*s%*s%s%s%*s%*s%*s%s%*s%6s%*s%5s%s%s\n",
1606 1.126 simonb wide ? 16 : 11, "Name",
1607 1.226 simonb wide ? 7 : 5, "Size",
1608 1.126 simonb wide ? 12 : 9, "Requests",
1609 1.226 simonb wide ? 8 : 5, "Fail",
1610 1.126 simonb wide ? 12 : 9, "Releases",
1611 1.226 simonb wide ? " InUse" : "",
1612 1.226 simonb wide ? " Avail" : "",
1613 1.226 simonb wide ? 11 : 6, "Pgreq",
1614 1.226 simonb wide ? 11 : 6, "Pgrel",
1615 1.226 simonb wide ? 8 : 6, "Npage",
1616 1.126 simonb wide ? " PageSz" : "",
1617 1.226 simonb wide ? 7 : 6, "Hiwat",
1618 1.75 enami "Minpg",
1619 1.226 simonb wide ? 7 : 6, "Maxpg",
1620 1.126 simonb "Idle",
1621 1.226 simonb wide ? " Flags" : "",
1622 1.126 simonb wide ? " Util" : "");
1623 1.51 pk first = 0;
1624 1.51 pk }
1625 1.113 dsl if (pp->pr_nget == 0 && !verbose)
1626 1.113 dsl continue;
1627 1.51 pk if (pp->pr_maxpages == UINT_MAX)
1628 1.153 christos (void)snprintf(maxp, sizeof(maxp), "inf");
1629 1.51 pk else
1630 1.153 christos (void)snprintf(maxp, sizeof(maxp), "%u",
1631 1.153 christos pp->pr_maxpages);
1632 1.69 enami ovflw = 0;
1633 1.126 simonb PRWORD(ovflw, "%-*s", wide ? 16 : 11, 0, name);
1634 1.226 simonb PRWORD(ovflw, " %*u", wide ? 7 : 5, 1, pp->pr_size);
1635 1.126 simonb PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nget);
1636 1.189 mrg pool_totals.pt_nget += pp->pr_nget;
1637 1.226 simonb PRWORD(ovflw, " %*lu", wide ? 8 : 5, 1, pp->pr_nfail);
1638 1.189 mrg pool_totals.pt_nfail += pp->pr_nfail;
1639 1.126 simonb PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nput);
1640 1.189 mrg pool_totals.pt_nput += pp->pr_nput;
1641 1.189 mrg if (wide) {
1642 1.226 simonb PRWORD(ovflw, " %*u", 9, 1, pp->pr_nout);
1643 1.189 mrg pool_totals.pt_nout += pp->pr_nout;
1644 1.226 simonb PRWORD(ovflw, " %*u", 9, 1, pp->pr_nitems);
1645 1.189 mrg pool_totals.pt_nitems += pp->pr_nitems;
1646 1.189 mrg }
1647 1.226 simonb PRWORD(ovflw, " %*lu", wide ? 11 : 6, 1, pp->pr_npagealloc);
1648 1.189 mrg pool_totals.pt_npagealloc += pp->pr_npagealloc;
1649 1.226 simonb PRWORD(ovflw, " %*lu", wide ? 11 : 6, 1, pp->pr_npagefree);
1650 1.189 mrg pool_totals.pt_npagefree += pp->pr_npagefree;
1651 1.226 simonb PRWORD(ovflw, " %*u", wide ? 8 : 6, 1, pp->pr_npages);
1652 1.189 mrg pool_totals.pt_npages += pp->pr_npages;
1653 1.126 simonb if (wide)
1654 1.126 simonb PRWORD(ovflw, " %*u", 7, 1, pa.pa_pagesz);
1655 1.226 simonb PRWORD(ovflw, " %*u", wide ? 7 : 6, 1, pp->pr_hiwat);
1656 1.126 simonb PRWORD(ovflw, " %*u", 6, 1, pp->pr_minpages);
1657 1.226 simonb PRWORD(ovflw, " %*s", wide ? 7 : 6, 1, maxp);
1658 1.126 simonb PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nidle);
1659 1.126 simonb if (wide)
1660 1.226 simonb PRWORD(ovflw, " 0x%0*x", 5, 1,
1661 1.126 simonb pp->pr_flags | pp->pr_roflags);
1662 1.51 pk
1663 1.126 simonb this_inuse = pp->pr_nout * pp->pr_size;
1664 1.126 simonb this_total = pp->pr_npages * pa.pa_pagesz;
1665 1.84 bjh21 if (pp->pr_roflags & PR_RECURSIVE) {
1666 1.84 bjh21 /*
1667 1.84 bjh21 * Don't count in-use memory, since it's part
1668 1.84 bjh21 * of another pool and will be accounted for
1669 1.84 bjh21 * there.
1670 1.84 bjh21 */
1671 1.126 simonb total += (this_total - this_inuse);
1672 1.84 bjh21 } else {
1673 1.126 simonb inuse += this_inuse;
1674 1.126 simonb total += this_total;
1675 1.84 bjh21 }
1676 1.126 simonb if (wide) {
1677 1.126 simonb if (this_total == 0)
1678 1.153 christos (void)printf(" ---");
1679 1.126 simonb else
1680 1.153 christos (void)printf(" %5.1f%%",
1681 1.126 simonb (100.0 * this_inuse) / this_total);
1682 1.126 simonb }
1683 1.153 christos (void)printf("\n");
1684 1.51 pk }
1685 1.226 simonb ovflw = 0;
1686 1.226 simonb PRWORD(ovflw, "%-*s", wide ? 16 : 11, 0, "Totals");
1687 1.226 simonb PRWORD(ovflw, " %*s", wide ? 7 : 5, 1, "");
1688 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pool_totals.pt_nget);
1689 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 5, 1, pool_totals.pt_nfail);
1690 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 12 : 9, 1, pool_totals.pt_nput);
1691 1.226 simonb if (wide) {
1692 1.226 simonb PRWORD(ovflw, " %*" PRIu64, 9, 1, pool_totals.pt_nout);
1693 1.226 simonb PRWORD(ovflw, " %*" PRIu64, 9, 1, pool_totals.pt_nitems);
1694 1.226 simonb }
1695 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pool_totals.pt_npagealloc);
1696 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 11 : 6, 1, pool_totals.pt_npagefree);
1697 1.226 simonb PRWORD(ovflw, " %*" PRIu64, wide ? 8 : 6, 1, pool_totals.pt_npages);
1698 1.226 simonb (void)printf("\n");
1699 1.51 pk
1700 1.152 christos inuse /= KILO;
1701 1.152 christos total /= KILO;
1702 1.153 christos (void)printf(
1703 1.153 christos "\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
1704 1.126 simonb inuse, total, (100.0 * inuse) / total);
1705 1.1 cgd }
1706 1.96 enami
1707 1.200 joerg static void
1708 1.200 joerg dopoolcache_sysctl(int verbose)
1709 1.200 joerg {
1710 1.200 joerg struct pool_sysctl *data, *pp;
1711 1.200 joerg size_t i, len;
1712 1.200 joerg bool first = true;
1713 1.200 joerg int ovflw;
1714 1.200 joerg uint64_t tot;
1715 1.208 christos double p;
1716 1.200 joerg
1717 1.200 joerg data = asysctlbyname("kern.pool", &len);
1718 1.200 joerg if (data == NULL)
1719 1.225 sevan err(1, "failed to read kern.pool");
1720 1.200 joerg len /= sizeof(*data);
1721 1.200 joerg
1722 1.200 joerg for (i = 0; i < len; ++i) {
1723 1.200 joerg pp = &data[i];
1724 1.200 joerg if (pp->pr_cache_meta_size == 0)
1725 1.200 joerg continue;
1726 1.200 joerg
1727 1.200 joerg if (pp->pr_cache_nmiss_global == 0 && !verbose)
1728 1.200 joerg continue;
1729 1.200 joerg
1730 1.200 joerg if (first) {
1731 1.200 joerg (void)printf("Pool cache statistics.\n");
1732 1.200 joerg (void)printf("%-*s%*s%*s%*s%*s%*s%*s%*s%*s%*s\n",
1733 1.200 joerg 12, "Name",
1734 1.200 joerg 6, "Spin",
1735 1.200 joerg 6, "GrpSz",
1736 1.200 joerg 5, "Full",
1737 1.200 joerg 5, "Emty",
1738 1.200 joerg 10, "PoolLayer",
1739 1.200 joerg 11, "CacheLayer",
1740 1.200 joerg 6, "Hit%",
1741 1.200 joerg 12, "CpuLayer",
1742 1.200 joerg 6, "Hit%"
1743 1.200 joerg );
1744 1.200 joerg first = false;
1745 1.200 joerg }
1746 1.200 joerg
1747 1.200 joerg ovflw = 0;
1748 1.200 joerg PRWORD(ovflw, "%-*s", MIN((int)sizeof(pp->pr_wchan), 13), 1,
1749 1.200 joerg pp->pr_wchan);
1750 1.200 joerg PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_cache_ncontended);
1751 1.200 joerg PRWORD(ovflw, " %*" PRIu64, 6, 1, pp->pr_cache_meta_size);
1752 1.200 joerg PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_cache_nfull);
1753 1.200 joerg PRWORD(ovflw, " %*" PRIu64, 5, 1, pp->pr_cache_nempty);
1754 1.200 joerg PRWORD(ovflw, " %*" PRIu64, 10, 1, pp->pr_cache_nmiss_global);
1755 1.200 joerg
1756 1.200 joerg tot = pp->pr_cache_nhit_global + pp->pr_cache_nmiss_global;
1757 1.200 joerg p = pp->pr_cache_nhit_global * 100.0 / tot;
1758 1.200 joerg PRWORD(ovflw, " %*" PRIu64, 11, 1, tot);
1759 1.200 joerg PRWORD(ovflw, " %*.1f", 6, 1, p);
1760 1.200 joerg
1761 1.200 joerg tot = pp->pr_cache_nhit_pcpu + pp->pr_cache_nmiss_pcpu;
1762 1.200 joerg p = pp->pr_cache_nhit_pcpu * 100.0 / tot;
1763 1.200 joerg PRWORD(ovflw, " %*" PRIu64, 12, 1, tot);
1764 1.200 joerg PRWORD(ovflw, " %*.1f", 6, 1, p);
1765 1.200 joerg printf("\n");
1766 1.200 joerg }
1767 1.200 joerg }
1768 1.200 joerg
1769 1.96 enami void
1770 1.182 yamt dopoolcache(int verbose)
1771 1.96 enami {
1772 1.96 enami struct pool_cache pool_cache, *pc = &pool_cache;
1773 1.154 ad pool_cache_cpu_t cache_cpu, *cc = &cache_cpu;
1774 1.157 ad TAILQ_HEAD(,pool) pool_head;
1775 1.154 ad struct pool pool, *pp = &pool;
1776 1.154 ad char name[32];
1777 1.154 ad uint64_t cpuhit, cpumiss, tot;
1778 1.154 ad void *addr;
1779 1.165 lukem int first, ovflw;
1780 1.165 lukem size_t i;
1781 1.154 ad double p;
1782 1.96 enami
1783 1.200 joerg if (memf == NULL)
1784 1.200 joerg return dopoolcache_sysctl(verbose);
1785 1.200 joerg
1786 1.154 ad kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
1787 1.157 ad addr = TAILQ_FIRST(&pool_head);
1788 1.96 enami
1789 1.157 ad for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) {
1790 1.154 ad deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
1791 1.154 ad if (pp->pr_cache == NULL)
1792 1.96 enami continue;
1793 1.154 ad deref_kptr(pp->pr_wchan, name, sizeof(name),
1794 1.154 ad "pool wait channel trashed");
1795 1.154 ad deref_kptr(pp->pr_cache, pc, sizeof(*pc), "pool cache trashed");
1796 1.182 yamt if (pc->pc_misses == 0 && !verbose)
1797 1.182 yamt continue;
1798 1.154 ad name[sizeof(name)-1] = '\0';
1799 1.154 ad
1800 1.154 ad cpuhit = 0;
1801 1.154 ad cpumiss = 0;
1802 1.184 jym for (i = 0; i < __arraycount(pc->pc_cpus); i++) {
1803 1.154 ad if ((addr = pc->pc_cpus[i]) == NULL)
1804 1.154 ad continue;
1805 1.154 ad deref_kptr(addr, cc, sizeof(*cc),
1806 1.154 ad "pool cache cpu trashed");
1807 1.154 ad cpuhit += cc->cc_hits;
1808 1.154 ad cpumiss += cc->cc_misses;
1809 1.137 chs }
1810 1.154 ad
1811 1.154 ad if (first) {
1812 1.154 ad (void)printf("Pool cache statistics.\n");
1813 1.156 ad (void)printf("%-*s%*s%*s%*s%*s%*s%*s%*s%*s%*s\n",
1814 1.154 ad 12, "Name",
1815 1.154 ad 6, "Spin",
1816 1.156 ad 6, "GrpSz",
1817 1.156 ad 5, "Full",
1818 1.156 ad 5, "Emty",
1819 1.156 ad 10, "PoolLayer",
1820 1.156 ad 11, "CacheLayer",
1821 1.154 ad 6, "Hit%",
1822 1.154 ad 12, "CpuLayer",
1823 1.154 ad 6, "Hit%"
1824 1.154 ad );
1825 1.154 ad first = 0;
1826 1.96 enami }
1827 1.154 ad
1828 1.154 ad ovflw = 0;
1829 1.154 ad PRWORD(ovflw, "%-*s", 13, 1, name);
1830 1.154 ad PRWORD(ovflw, " %*llu", 6, 1, (long long)pc->pc_contended);
1831 1.156 ad PRWORD(ovflw, " %*u", 6, 1, pc->pc_pcgsize);
1832 1.156 ad PRWORD(ovflw, " %*u", 5, 1, pc->pc_nfull);
1833 1.156 ad PRWORD(ovflw, " %*u", 5, 1, pc->pc_nempty);
1834 1.156 ad PRWORD(ovflw, " %*llu", 10, 1, (long long)pc->pc_misses);
1835 1.154 ad
1836 1.154 ad tot = pc->pc_hits + pc->pc_misses;
1837 1.154 ad p = pc->pc_hits * 100.0 / (tot);
1838 1.156 ad PRWORD(ovflw, " %*llu", 11, 1, (long long)tot);
1839 1.154 ad PRWORD(ovflw, " %*.1f", 6, 1, p);
1840 1.154 ad
1841 1.154 ad tot = cpuhit + cpumiss;
1842 1.154 ad p = cpuhit * 100.0 / (tot);
1843 1.154 ad PRWORD(ovflw, " %*llu", 12, 1, (long long)tot);
1844 1.154 ad PRWORD(ovflw, " %*.1f", 6, 1, p);
1845 1.154 ad printf("\n");
1846 1.96 enami }
1847 1.96 enami }
1848 1.90 lukem
1849 1.87 lukem enum hashtype { /* from <sys/systm.h> */
1850 1.87 lukem HASH_LIST,
1851 1.229 ad HASH_SLIST,
1852 1.229 ad HASH_TAILQ,
1853 1.229 ad HASH_PSLIST
1854 1.87 lukem };
1855 1.87 lukem
1856 1.87 lukem struct uidinfo { /* XXX: no kernel header file */
1857 1.87 lukem LIST_ENTRY(uidinfo) ui_hash;
1858 1.87 lukem uid_t ui_uid;
1859 1.87 lukem long ui_proccnt;
1860 1.87 lukem };
1861 1.87 lukem
1862 1.87 lukem struct kernel_hash {
1863 1.90 lukem const char * description; /* description */
1864 1.90 lukem int hashsize; /* nlist index for hash size */
1865 1.90 lukem int hashtbl; /* nlist index for hash table */
1866 1.90 lukem enum hashtype type; /* type of hash table */
1867 1.101 sommerfe size_t offset; /* offset of {LIST,TAILQ}_NEXT */
1868 1.87 lukem } khashes[] =
1869 1.87 lukem {
1870 1.87 lukem {
1871 1.90 lukem "buffer hash",
1872 1.90 lukem X_BUFHASH, X_BUFHASHTBL,
1873 1.90 lukem HASH_LIST, offsetof(struct buf, b_hash)
1874 1.90 lukem }, {
1875 1.90 lukem "ipv4 address -> interface hash",
1876 1.87 lukem X_IFADDRHASH, X_IFADDRHASHTBL,
1877 1.87 lukem HASH_LIST, offsetof(struct in_ifaddr, ia_hash),
1878 1.90 lukem }, {
1879 1.90 lukem "name cache hash",
1880 1.89 lukem X_NCHASH, X_NCHASHTBL,
1881 1.89 lukem HASH_LIST, offsetof(struct namecache, nc_hash),
1882 1.90 lukem }, {
1883 1.90 lukem "name cache directory hash",
1884 1.89 lukem X_NCVHASH, X_NCVHASHTBL,
1885 1.89 lukem HASH_LIST, offsetof(struct namecache, nc_vhash),
1886 1.90 lukem }, {
1887 1.90 lukem "user info (uid -> used processes) hash",
1888 1.90 lukem X_UIHASH, X_UIHASHTBL,
1889 1.90 lukem HASH_LIST, offsetof(struct uidinfo, ui_hash),
1890 1.90 lukem }, {
1891 1.229 ad "vnode cache hash",
1892 1.229 ad X_NCVCACHEHASH, X_NCVCACHETBL,
1893 1.229 ad HASH_SLIST, offsetof(struct vnode_impl, vi_hash),
1894 1.229 ad }, {
1895 1.90 lukem NULL, -1, -1, 0, 0,
1896 1.87 lukem }
1897 1.87 lukem };
1898 1.87 lukem
1899 1.87 lukem void
1900 1.88 lukem dohashstat(int verbose, int todo, const char *hashname)
1901 1.101 sommerfe {
1902 1.87 lukem LIST_HEAD(, generic) *hashtbl_list;
1903 1.229 ad SLIST_HEAD(, generic) *hashtbl_slist;
1904 1.87 lukem TAILQ_HEAD(, generic) *hashtbl_tailq;
1905 1.87 lukem struct kernel_hash *curhash;
1906 1.118 itojun void *hashaddr, *hashbuf, *nhashbuf, *nextaddr;
1907 1.87 lukem size_t elemsize, hashbufsize, thissize;
1908 1.165 lukem u_long hashsize, i;
1909 1.165 lukem int used, items, chain, maxchain;
1910 1.87 lukem
1911 1.87 lukem hashbuf = NULL;
1912 1.87 lukem hashbufsize = 0;
1913 1.88 lukem
1914 1.88 lukem if (todo & HASHLIST) {
1915 1.153 christos (void)printf("Supported hashes:\n");
1916 1.90 lukem for (curhash = khashes; curhash->description; curhash++) {
1917 1.101 sommerfe if (hashnl[curhash->hashsize].n_value == 0 ||
1918 1.90 lukem hashnl[curhash->hashtbl].n_value == 0)
1919 1.90 lukem continue;
1920 1.153 christos (void)printf("\t%-16s%s\n",
1921 1.90 lukem hashnl[curhash->hashsize].n_name + 1,
1922 1.90 lukem curhash->description);
1923 1.88 lukem }
1924 1.88 lukem return;
1925 1.88 lukem }
1926 1.88 lukem
1927 1.88 lukem if (hashname != NULL) {
1928 1.90 lukem for (curhash = khashes; curhash->description; curhash++) {
1929 1.90 lukem if (strcmp(hashnl[curhash->hashsize].n_name + 1,
1930 1.90 lukem hashname) == 0 &&
1931 1.101 sommerfe hashnl[curhash->hashsize].n_value != 0 &&
1932 1.90 lukem hashnl[curhash->hashtbl].n_value != 0)
1933 1.88 lukem break;
1934 1.88 lukem }
1935 1.90 lukem if (curhash->description == NULL) {
1936 1.90 lukem warnx("%s: no such hash", hashname);
1937 1.90 lukem return;
1938 1.90 lukem }
1939 1.88 lukem }
1940 1.88 lukem
1941 1.153 christos (void)printf(
1942 1.88 lukem "%-16s %8s %8s %8s %8s %8s %8s\n"
1943 1.88 lukem "%-16s %8s %8s %8s %8s %8s %8s\n",
1944 1.88 lukem "", "total", "used", "util", "num", "average", "maximum",
1945 1.88 lukem "hash table", "buckets", "buckets", "%", "items", "chain",
1946 1.88 lukem "chain");
1947 1.87 lukem
1948 1.90 lukem for (curhash = khashes; curhash->description; curhash++) {
1949 1.101 sommerfe if (hashnl[curhash->hashsize].n_value == 0 ||
1950 1.90 lukem hashnl[curhash->hashtbl].n_value == 0)
1951 1.90 lukem continue;
1952 1.88 lukem if (hashname != NULL &&
1953 1.90 lukem strcmp(hashnl[curhash->hashsize].n_name + 1, hashname))
1954 1.88 lukem continue;
1955 1.87 lukem elemsize = curhash->type == HASH_LIST ?
1956 1.87 lukem sizeof(*hashtbl_list) : sizeof(*hashtbl_tailq);
1957 1.90 lukem deref_kptr((void *)hashnl[curhash->hashsize].n_value,
1958 1.90 lukem &hashsize, sizeof(hashsize),
1959 1.90 lukem hashnl[curhash->hashsize].n_name);
1960 1.87 lukem hashsize++;
1961 1.90 lukem deref_kptr((void *)hashnl[curhash->hashtbl].n_value,
1962 1.90 lukem &hashaddr, sizeof(hashaddr),
1963 1.90 lukem hashnl[curhash->hashtbl].n_name);
1964 1.87 lukem if (verbose)
1965 1.153 christos (void)printf(
1966 1.153 christos "%s %lu, %s %p, offset %ld, elemsize %llu\n",
1967 1.90 lukem hashnl[curhash->hashsize].n_name + 1, hashsize,
1968 1.90 lukem hashnl[curhash->hashtbl].n_name + 1, hashaddr,
1969 1.101 sommerfe (long)curhash->offset,
1970 1.91 jmc (unsigned long long)elemsize);
1971 1.87 lukem thissize = hashsize * elemsize;
1972 1.144 christos if (hashbuf == NULL || thissize > hashbufsize) {
1973 1.118 itojun if ((nhashbuf = realloc(hashbuf, thissize)) == NULL)
1974 1.101 sommerfe errx(1, "malloc hashbuf %llu",
1975 1.91 jmc (unsigned long long)hashbufsize);
1976 1.118 itojun hashbuf = nhashbuf;
1977 1.118 itojun hashbufsize = thissize;
1978 1.87 lukem }
1979 1.87 lukem deref_kptr(hashaddr, hashbuf, thissize,
1980 1.90 lukem hashnl[curhash->hashtbl].n_name);
1981 1.87 lukem used = 0;
1982 1.87 lukem items = maxchain = 0;
1983 1.135 lukem if (curhash->type == HASH_LIST) {
1984 1.87 lukem hashtbl_list = hashbuf;
1985 1.229 ad hashtbl_slist = NULL;
1986 1.229 ad hashtbl_tailq = NULL;
1987 1.229 ad } else if (curhash->type == HASH_SLIST) {
1988 1.229 ad hashtbl_list = NULL;
1989 1.229 ad hashtbl_slist = hashbuf;
1990 1.135 lukem hashtbl_tailq = NULL;
1991 1.135 lukem } else {
1992 1.135 lukem hashtbl_list = NULL;
1993 1.229 ad hashtbl_slist = NULL;
1994 1.87 lukem hashtbl_tailq = hashbuf;
1995 1.135 lukem }
1996 1.87 lukem for (i = 0; i < hashsize; i++) {
1997 1.87 lukem if (curhash->type == HASH_LIST)
1998 1.87 lukem nextaddr = LIST_FIRST(&hashtbl_list[i]);
1999 1.229 ad else if (curhash->type == HASH_SLIST)
2000 1.229 ad nextaddr = SLIST_FIRST(&hashtbl_slist[i]);
2001 1.87 lukem else
2002 1.87 lukem nextaddr = TAILQ_FIRST(&hashtbl_tailq[i]);
2003 1.87 lukem if (nextaddr == NULL)
2004 1.87 lukem continue;
2005 1.87 lukem if (verbose)
2006 1.165 lukem (void)printf("%5lu: %p\n", i, nextaddr);
2007 1.87 lukem used++;
2008 1.87 lukem chain = 0;
2009 1.87 lukem do {
2010 1.87 lukem chain++;
2011 1.87 lukem deref_kptr((char *)nextaddr + curhash->offset,
2012 1.87 lukem &nextaddr, sizeof(void *),
2013 1.87 lukem "hash chain corrupted");
2014 1.87 lukem if (verbose > 1)
2015 1.153 christos (void)printf("got nextaddr as %p\n",
2016 1.87 lukem nextaddr);
2017 1.87 lukem } while (nextaddr != NULL);
2018 1.87 lukem items += chain;
2019 1.87 lukem if (verbose && chain > 1)
2020 1.153 christos (void)printf("\tchain = %d\n", chain);
2021 1.87 lukem if (chain > maxchain)
2022 1.87 lukem maxchain = chain;
2023 1.87 lukem }
2024 1.153 christos (void)printf("%-16s %8ld %8d %8.2f %8d %8.2f %8d\n",
2025 1.90 lukem hashnl[curhash->hashsize].n_name + 1,
2026 1.87 lukem hashsize, used, used * 100.0 / hashsize,
2027 1.93 lukem items, used ? (double)items / used : 0.0, maxchain);
2028 1.87 lukem }
2029 1.87 lukem }
2030 1.87 lukem
2031 1.1 cgd /*
2032 1.230 msaitoh * kreadc like kread but returns 1 if successful, 0 otherwise
2033 1.147 kardel */
2034 1.147 kardel int
2035 1.147 kardel kreadc(struct nlist *nl, int nlx, void *addr, size_t size)
2036 1.147 kardel {
2037 1.147 kardel const char *sym;
2038 1.147 kardel
2039 1.147 kardel sym = nl[nlx].n_name;
2040 1.147 kardel if (*sym == '_')
2041 1.147 kardel ++sym;
2042 1.147 kardel if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0)
2043 1.147 kardel return 0;
2044 1.147 kardel deref_kptr((void *)nl[nlx].n_value, addr, size, sym);
2045 1.147 kardel return 1;
2046 1.147 kardel }
2047 1.147 kardel
2048 1.147 kardel /*
2049 1.90 lukem * kread reads something from the kernel, given its nlist index in namelist[].
2050 1.1 cgd */
2051 1.1 cgd void
2052 1.133 chs kread(struct nlist *nl, int nlx, void *addr, size_t size)
2053 1.1 cgd {
2054 1.50 mycroft const char *sym;
2055 1.1 cgd
2056 1.133 chs sym = nl[nlx].n_name;
2057 1.87 lukem if (*sym == '_')
2058 1.87 lukem ++sym;
2059 1.133 chs if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0)
2060 1.87 lukem errx(1, "symbol %s not defined", sym);
2061 1.133 chs deref_kptr((void *)nl[nlx].n_value, addr, size, sym);
2062 1.87 lukem }
2063 1.87 lukem
2064 1.87 lukem /*
2065 1.101 sommerfe * Dereference the kernel pointer `kptr' and fill in the local copy
2066 1.87 lukem * pointed to by `ptr'. The storage space must be pre-allocated,
2067 1.87 lukem * and the size of the copy passed in `len'.
2068 1.87 lukem */
2069 1.87 lukem void
2070 1.87 lukem deref_kptr(const void *kptr, void *ptr, size_t len, const char *msg)
2071 1.87 lukem {
2072 1.87 lukem
2073 1.87 lukem if (*msg == '_')
2074 1.87 lukem msg++;
2075 1.165 lukem if ((size_t)kvm_read(kd, (u_long)kptr, (char *)ptr, len) != len)
2076 1.87 lukem errx(1, "kptr %lx: %s: %s", (u_long)kptr, msg, kvm_geterr(kd));
2077 1.1 cgd }
2078 1.1 cgd
2079 1.45 thorpej /*
2080 1.181 mrg * Traverse the kernel history buffers, performing the requested action.
2081 1.45 thorpej *
2082 1.45 thorpej * Note, we assume that if we're not listing, we're dumping.
2083 1.45 thorpej */
2084 1.45 thorpej void
2085 1.73 simonb hist_traverse(int todo, const char *histname)
2086 1.45 thorpej {
2087 1.210 pgoyette struct kern_history_head histhead;
2088 1.210 pgoyette struct kern_history hist, *histkva;
2089 1.210 pgoyette char *name = NULL;
2090 1.210 pgoyette size_t namelen = 0;
2091 1.210 pgoyette
2092 1.210 pgoyette if (histnl[0].n_value == 0) {
2093 1.210 pgoyette warnx("kernel history is not compiled into the kernel.");
2094 1.210 pgoyette return;
2095 1.210 pgoyette }
2096 1.210 pgoyette
2097 1.210 pgoyette deref_kptr((void *)histnl[X_KERN_HISTORIES].n_value, &histhead,
2098 1.210 pgoyette sizeof(histhead), histnl[X_KERN_HISTORIES].n_name);
2099 1.210 pgoyette
2100 1.210 pgoyette if (histhead.lh_first == NULL) {
2101 1.210 pgoyette warnx("No active kernel history logs.");
2102 1.210 pgoyette return;
2103 1.210 pgoyette }
2104 1.210 pgoyette
2105 1.210 pgoyette if (todo & HISTLIST)
2106 1.210 pgoyette (void)printf("Active kernel histories:");
2107 1.210 pgoyette
2108 1.210 pgoyette for (histkva = LIST_FIRST(&histhead); histkva != NULL;
2109 1.210 pgoyette histkva = LIST_NEXT(&hist, list)) {
2110 1.210 pgoyette deref_kptr(histkva, &hist, sizeof(hist), "histkva");
2111 1.210 pgoyette if (name == NULL || hist.namelen > namelen) {
2112 1.210 pgoyette if (name != NULL)
2113 1.210 pgoyette free(name);
2114 1.210 pgoyette namelen = hist.namelen;
2115 1.210 pgoyette if ((name = malloc(namelen + 1)) == NULL)
2116 1.210 pgoyette err(1, "malloc history name");
2117 1.210 pgoyette }
2118 1.210 pgoyette
2119 1.210 pgoyette deref_kptr(hist.name, name, namelen, "history name");
2120 1.210 pgoyette name[namelen] = '\0';
2121 1.210 pgoyette if (todo & HISTLIST)
2122 1.210 pgoyette (void)printf(" %s", name);
2123 1.210 pgoyette else {
2124 1.210 pgoyette /*
2125 1.210 pgoyette * If we're dumping all histories, do it, else
2126 1.210 pgoyette * check to see if this is the one we want.
2127 1.210 pgoyette */
2128 1.210 pgoyette if (histname == NULL || strcmp(histname, name) == 0) {
2129 1.210 pgoyette if (histname == NULL)
2130 1.210 pgoyette (void)printf(
2131 1.210 pgoyette "\nkernel history `%s':\n", name);
2132 1.210 pgoyette hist_dodump(&hist);
2133 1.210 pgoyette }
2134 1.210 pgoyette }
2135 1.210 pgoyette }
2136 1.210 pgoyette
2137 1.210 pgoyette if (todo & HISTLIST)
2138 1.210 pgoyette (void)putchar('\n');
2139 1.210 pgoyette
2140 1.210 pgoyette if (name != NULL)
2141 1.210 pgoyette free(name);
2142 1.210 pgoyette }
2143 1.210 pgoyette
2144 1.210 pgoyette /*
2145 1.210 pgoyette * Actually dump the history buffer at the specified KVA.
2146 1.210 pgoyette */
2147 1.210 pgoyette void
2148 1.210 pgoyette hist_dodump(struct kern_history *histp)
2149 1.210 pgoyette {
2150 1.210 pgoyette struct kern_history_ent *histents, *e;
2151 1.215 pgoyette struct timeval tv;
2152 1.210 pgoyette size_t histsize;
2153 1.210 pgoyette char *fmt = NULL, *fn = NULL;
2154 1.210 pgoyette size_t fmtlen = 0, fnlen = 0;
2155 1.210 pgoyette unsigned i;
2156 1.210 pgoyette
2157 1.210 pgoyette histsize = sizeof(struct kern_history_ent) * histp->n;
2158 1.210 pgoyette
2159 1.210 pgoyette if ((histents = malloc(histsize)) == NULL)
2160 1.210 pgoyette err(1, "malloc history entries");
2161 1.210 pgoyette
2162 1.210 pgoyette (void)memset(histents, 0, histsize);
2163 1.210 pgoyette
2164 1.211 pgoyette (void)printf("%"PRIu32" entries, next is %"PRIu32"\n",
2165 1.211 pgoyette histp->n, histp->f);
2166 1.211 pgoyette
2167 1.210 pgoyette deref_kptr(histp->e, histents, histsize, "history entries");
2168 1.210 pgoyette i = histp->f;
2169 1.210 pgoyette do {
2170 1.210 pgoyette e = &histents[i];
2171 1.210 pgoyette if (e->fmt != NULL) {
2172 1.210 pgoyette if (fmt == NULL || e->fmtlen > fmtlen) {
2173 1.227 mrg free(fmt);
2174 1.210 pgoyette fmtlen = e->fmtlen;
2175 1.210 pgoyette if ((fmt = malloc(fmtlen + 1)) == NULL)
2176 1.210 pgoyette err(1, "malloc printf format");
2177 1.210 pgoyette }
2178 1.210 pgoyette if (fn == NULL || e->fnlen > fnlen) {
2179 1.227 mrg free(fn);
2180 1.210 pgoyette fnlen = e->fnlen;
2181 1.210 pgoyette if ((fn = malloc(fnlen + 1)) == NULL)
2182 1.210 pgoyette err(1, "malloc function name");
2183 1.210 pgoyette }
2184 1.210 pgoyette
2185 1.210 pgoyette deref_kptr(e->fmt, fmt, fmtlen, "printf format");
2186 1.210 pgoyette fmt[fmtlen] = '\0';
2187 1.227 mrg for (unsigned z = 0; z < fmtlen - 1; z++) {
2188 1.227 mrg if (fmt[z] == '%' && fmt[z+1] == 's')
2189 1.227 mrg fmt[z+1] = 'p';
2190 1.227 mrg }
2191 1.210 pgoyette
2192 1.210 pgoyette deref_kptr(e->fn, fn, fnlen, "function name");
2193 1.210 pgoyette fn[fnlen] = '\0';
2194 1.210 pgoyette
2195 1.215 pgoyette bintime2timeval(&e->bt, &tv);
2196 1.215 pgoyette (void)printf("%06ld.%06ld ", (long int)tv.tv_sec,
2197 1.215 pgoyette (long int)tv.tv_usec);
2198 1.219 pgoyette (void)printf("%s#%" PRId32 "@%" PRId32 "d: ",
2199 1.219 pgoyette fn, e->call, e->cpunum);
2200 1.210 pgoyette (void)printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
2201 1.210 pgoyette (void)putchar('\n');
2202 1.210 pgoyette }
2203 1.210 pgoyette i = (i + 1) % histp->n;
2204 1.210 pgoyette } while (i != histp->f);
2205 1.210 pgoyette
2206 1.210 pgoyette free(histents);
2207 1.227 mrg free(fmt);
2208 1.227 mrg free(fn);
2209 1.210 pgoyette }
2210 1.210 pgoyette
2211 1.210 pgoyette void
2212 1.210 pgoyette hist_traverse_sysctl(int todo, const char *histname)
2213 1.210 pgoyette {
2214 1.209 pgoyette int error;
2215 1.209 pgoyette int mib[4];
2216 1.209 pgoyette unsigned int i;
2217 1.209 pgoyette size_t len, miblen;
2218 1.209 pgoyette struct sysctlnode query, histnode[32];
2219 1.209 pgoyette
2220 1.209 pgoyette /* retrieve names of available histories */
2221 1.209 pgoyette miblen = __arraycount(mib);
2222 1.209 pgoyette error = sysctlnametomib("kern.hist", mib, &miblen);
2223 1.209 pgoyette if (error != 0) {
2224 1.214 pgoyette if (errno == ENOENT) {
2225 1.214 pgoyette warnx("kernel history is not compiled into the kernel.");
2226 1.214 pgoyette return;
2227 1.214 pgoyette } else
2228 1.214 pgoyette err(EXIT_FAILURE, "nametomib failed");
2229 1.209 pgoyette }
2230 1.210 pgoyette
2231 1.209 pgoyette /* get the list of nodenames below kern.hist */
2232 1.209 pgoyette mib[2] = CTL_QUERY;
2233 1.209 pgoyette memset(&query, 0, sizeof(query));
2234 1.209 pgoyette query.sysctl_flags = SYSCTL_VERSION;
2235 1.209 pgoyette len = sizeof(histnode);
2236 1.209 pgoyette error = sysctl(mib, 3, &histnode[0], &len, &query, sizeof(query));
2237 1.209 pgoyette if (error != 0) {
2238 1.209 pgoyette err(1, "query failed");
2239 1.209 pgoyette return;
2240 1.209 pgoyette }
2241 1.209 pgoyette if (len == 0) {
2242 1.210 pgoyette warnx("No active kernel history logs.");
2243 1.210 pgoyette return;
2244 1.210 pgoyette }
2245 1.210 pgoyette
2246 1.209 pgoyette len = len / sizeof(histnode[0]); /* get # of entries returned */
2247 1.209 pgoyette
2248 1.210 pgoyette if (todo & HISTLIST)
2249 1.210 pgoyette (void)printf("Active kernel histories:");
2250 1.210 pgoyette
2251 1.209 pgoyette for (i = 0; i < len; i++) {
2252 1.210 pgoyette if (todo & HISTLIST)
2253 1.209 pgoyette (void)printf(" %s", histnode[i].sysctl_name);
2254 1.210 pgoyette else {
2255 1.210 pgoyette /*
2256 1.210 pgoyette * If we're dumping all histories, do it, else
2257 1.210 pgoyette * check to see if this is the one we want.
2258 1.210 pgoyette */
2259 1.209 pgoyette if (histname == NULL ||
2260 1.209 pgoyette strcmp(histname, histnode[i].sysctl_name) == 0) {
2261 1.210 pgoyette if (histname == NULL)
2262 1.210 pgoyette (void)printf(
2263 1.209 pgoyette "\nkernel history `%s':\n",
2264 1.209 pgoyette histnode[i].sysctl_name);
2265 1.209 pgoyette mib[2] = histnode[i].sysctl_num;
2266 1.209 pgoyette mib[3] = CTL_EOL;
2267 1.210 pgoyette hist_dodump_sysctl(mib, 4);
2268 1.210 pgoyette }
2269 1.210 pgoyette }
2270 1.210 pgoyette }
2271 1.210 pgoyette
2272 1.210 pgoyette if (todo & HISTLIST)
2273 1.210 pgoyette (void)putchar('\n');
2274 1.219 pgoyette else if (mib[2] == CTL_QUERY)
2275 1.219 pgoyette warnx("history %s not found", histname);
2276 1.210 pgoyette }
2277 1.210 pgoyette
2278 1.210 pgoyette /*
2279 1.210 pgoyette * Actually dump the history buffer at the specified KVA.
2280 1.210 pgoyette */
2281 1.221 mrg void
2282 1.210 pgoyette hist_dodump_sysctl(int mib[], unsigned int miblen)
2283 1.221 mrg {
2284 1.209 pgoyette struct sysctl_history *hist;
2285 1.215 pgoyette struct timeval tv;
2286 1.209 pgoyette struct sysctl_history_event *e;
2287 1.210 pgoyette size_t histsize;
2288 1.209 pgoyette char *strp;
2289 1.210 pgoyette unsigned i;
2290 1.45 thorpej char *fmt = NULL, *fn = NULL;
2291 1.210 pgoyette
2292 1.209 pgoyette hist = NULL;
2293 1.209 pgoyette histsize = 0;
2294 1.210 pgoyette do {
2295 1.209 pgoyette errno = 0;
2296 1.209 pgoyette if (sysctl(mib, miblen, hist, &histsize, NULL, 0) == 0)
2297 1.209 pgoyette break;
2298 1.209 pgoyette if (errno != ENOMEM)
2299 1.209 pgoyette break;
2300 1.209 pgoyette if ((hist = realloc(hist, histsize)) == NULL)
2301 1.209 pgoyette errx(1, "realloc history buffer");
2302 1.209 pgoyette } while (errno == ENOMEM);
2303 1.209 pgoyette if (errno != 0)
2304 1.209 pgoyette err(1, "sysctl failed");
2305 1.210 pgoyette
2306 1.219 pgoyette strp = (char *)(&hist->sh_events[hist->sh_numentries]);
2307 1.210 pgoyette
2308 1.209 pgoyette (void)printf("%"PRIu32" entries, next is %"PRIu32"\n",
2309 1.219 pgoyette hist->sh_numentries,
2310 1.219 pgoyette hist->sh_nextfree);
2311 1.210 pgoyette
2312 1.219 pgoyette i = hist->sh_nextfree;
2313 1.45 thorpej
2314 1.45 thorpej do {
2315 1.209 pgoyette e = &hist->sh_events[i];
2316 1.209 pgoyette if (e->she_fmtoffset != 0) {
2317 1.209 pgoyette fmt = &strp[e->she_fmtoffset];
2318 1.227 mrg size_t fmtlen = strlen(fmt);
2319 1.227 mrg for (unsigned z = 0; z < fmtlen - 1; z++) {
2320 1.227 mrg if (fmt[z] == '%' && fmt[z+1] == 's')
2321 1.227 mrg fmt[z+1] = 'p';
2322 1.227 mrg }
2323 1.209 pgoyette fn = &strp[e->she_funcoffset];
2324 1.215 pgoyette bintime2timeval(&e->she_bintime, &tv);
2325 1.219 pgoyette (void)printf("%06ld.%06ld %s#%"PRIu32"@%"PRIu32": ",
2326 1.215 pgoyette (long int)tv.tv_sec, (long int)tv.tv_usec,
2327 1.213 pgoyette fn, e->she_callnumber, e->she_cpunum);
2328 1.209 pgoyette (void)printf(fmt, e->she_values[0], e->she_values[1],
2329 1.209 pgoyette e->she_values[2], e->she_values[3]);
2330 1.210 pgoyette (void)putchar('\n');
2331 1.210 pgoyette }
2332 1.219 pgoyette i = (i + 1) % hist->sh_numentries;
2333 1.219 pgoyette } while (i != hist->sh_nextfree);
2334 1.210 pgoyette
2335 1.209 pgoyette free(hist);
2336 1.210 pgoyette }
2337 1.45 thorpej
2338 1.183 joerg static void
2339 1.73 simonb usage(void)
2340 1.1 cgd {
2341 1.47 mrg
2342 1.1 cgd (void)fprintf(stderr,
2343 1.155 yamt "usage: %s [-CefHiLlmstUvW] [-c count] [-h hashname] [-M core] [-N system]\n"
2344 1.127 wiz "\t\t[-u histname] [-w wait] [disks]\n", getprogname());
2345 1.1 cgd exit(1);
2346 1.1 cgd }
2347