vmstat.c revision 1.175 1 1.175 christos /* $NetBSD: vmstat.c,v 1.175 2010/12/25 23:36:59 christos Exp $ */
2 1.45 thorpej
3 1.45 thorpej /*-
4 1.154 ad * Copyright (c) 1998, 2000, 2001, 2007 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.175 christos __RCSID("$NetBSD: vmstat.c,v 1.175 2010/12/25 23:36:59 christos 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.1 cgd
79 1.1 cgd #include <sys/param.h>
80 1.174 christos #include <sys/types.h>
81 1.87 lukem #include <sys/mount.h>
82 1.87 lukem #include <sys/uio.h>
83 1.87 lukem
84 1.87 lukem #include <sys/buf.h>
85 1.146 yamt #include <sys/evcnt.h>
86 1.87 lukem #include <sys/ioctl.h>
87 1.87 lukem #include <sys/malloc.h>
88 1.109 thorpej #include <sys/mallocvar.h>
89 1.1 cgd #include <sys/namei.h>
90 1.87 lukem #include <sys/pool.h>
91 1.87 lukem #include <sys/proc.h>
92 1.64 perry #include <sys/sched.h>
93 1.87 lukem #include <sys/socket.h>
94 1.13 cgd #include <sys/sysctl.h>
95 1.87 lukem #include <sys/time.h>
96 1.87 lukem #include <sys/user.h>
97 1.171 christos #include <sys/queue.h>
98 1.67 mrg
99 1.67 mrg #include <uvm/uvm_extern.h>
100 1.67 mrg #include <uvm/uvm_stat.h>
101 1.67 mrg
102 1.87 lukem #include <net/if.h>
103 1.87 lukem #include <netinet/in.h>
104 1.87 lukem #include <netinet/in_var.h>
105 1.87 lukem
106 1.87 lukem #include <ufs/ufs/inode.h>
107 1.87 lukem
108 1.87 lukem #include <nfs/rpcv2.h>
109 1.87 lukem #include <nfs/nfsproto.h>
110 1.87 lukem #include <nfs/nfsnode.h>
111 1.87 lukem
112 1.87 lukem #include <ctype.h>
113 1.45 thorpej #include <err.h>
114 1.87 lukem #include <errno.h>
115 1.55 kleink #include <fcntl.h>
116 1.87 lukem #include <kvm.h>
117 1.87 lukem #include <limits.h>
118 1.1 cgd #include <nlist.h>
119 1.87 lukem #undef n_hash
120 1.87 lukem #include <paths.h>
121 1.22 jtc #include <signal.h>
122 1.1 cgd #include <stdio.h>
123 1.87 lukem #include <stddef.h>
124 1.1 cgd #include <stdlib.h>
125 1.1 cgd #include <string.h>
126 1.87 lukem #include <time.h>
127 1.87 lukem #include <unistd.h>
128 1.104 mrg #include <util.h>
129 1.87 lukem
130 1.140 blymn #include "drvstats.h"
131 1.45 thorpej
132 1.90 lukem /*
133 1.174 christos * All this mess will go away once everything is converted.
134 1.174 christos */
135 1.174 christos #ifdef __HAVE_CPU_DATA_FIRST
136 1.175 christos
137 1.175 christos # include <sys/cpu_data.h>
138 1.175 christos struct cpu_info {
139 1.174 christos struct cpu_data ci_data;
140 1.174 christos };
141 1.175 christos CIRCLEQ_HEAD(cpuqueue, cpu_info);
142 1.175 christos struct cpuqueue cpu_queue;
143 1.175 christos
144 1.174 christos #else
145 1.175 christos
146 1.175 christos # include <sys/cpu.h>
147 1.175 christos struct cpuqueue cpu_queue;
148 1.175 christos
149 1.174 christos #endif
150 1.174 christos /*
151 1.90 lukem * General namelist
152 1.90 lukem */
153 1.87 lukem struct nlist namelist[] =
154 1.87 lukem {
155 1.65 itojun #define X_BOOTTIME 0
156 1.153 christos { .n_name = "_boottime" },
157 1.75 enami #define X_HZ 1
158 1.153 christos { .n_name = "_hz" },
159 1.75 enami #define X_STATHZ 2
160 1.153 christos { .n_name = "_stathz" },
161 1.75 enami #define X_NCHSTATS 3
162 1.153 christos { .n_name = "_nchstats" },
163 1.133 chs #define X_KMEMSTAT 4
164 1.153 christos { .n_name = "_kmemstatistics" },
165 1.133 chs #define X_KMEMBUCKETS 5
166 1.153 christos { .n_name = "_kmembuckets" },
167 1.133 chs #define X_ALLEVENTS 6
168 1.153 christos { .n_name = "_allevents" },
169 1.133 chs #define X_POOLHEAD 7
170 1.153 christos { .n_name = "_pool_head" },
171 1.133 chs #define X_UVMEXP 8
172 1.153 christos { .n_name = "_uvmexp" },
173 1.145 kardel #define X_TIME_SECOND 9
174 1.153 christos { .n_name = "_time_second" },
175 1.147 kardel #define X_TIME 10
176 1.153 christos { .n_name = "_time" },
177 1.171 christos #define X_CPU_QUEUE 11
178 1.171 christos { .n_name = "_cpu_queue" },
179 1.171 christos #define X_NL_SIZE 12
180 1.153 christos { .n_name = NULL },
181 1.90 lukem };
182 1.90 lukem
183 1.90 lukem /*
184 1.133 chs * Namelist for pre-evcnt interrupt counters.
185 1.133 chs */
186 1.133 chs struct nlist intrnl[] =
187 1.133 chs {
188 1.133 chs #define X_INTRNAMES 0
189 1.153 christos { .n_name = "_intrnames" },
190 1.133 chs #define X_EINTRNAMES 1
191 1.153 christos { .n_name = "_eintrnames" },
192 1.133 chs #define X_INTRCNT 2
193 1.153 christos { .n_name = "_intrcnt" },
194 1.133 chs #define X_EINTRCNT 3
195 1.153 christos { .n_name = "_eintrcnt" },
196 1.133 chs #define X_INTRNL_SIZE 4
197 1.153 christos { .n_name = NULL },
198 1.133 chs };
199 1.133 chs
200 1.133 chs
201 1.133 chs /*
202 1.90 lukem * Namelist for hash statistics
203 1.90 lukem */
204 1.90 lukem struct nlist hashnl[] =
205 1.90 lukem {
206 1.90 lukem #define X_NFSNODE 0
207 1.153 christos { .n_name = "_nfsnodehash" },
208 1.90 lukem #define X_NFSNODETBL 1
209 1.153 christos { .n_name = "_nfsnodehashtbl" },
210 1.90 lukem #define X_IHASH 2
211 1.153 christos { .n_name = "_ihash" },
212 1.90 lukem #define X_IHASHTBL 3
213 1.153 christos { .n_name = "_ihashtbl" },
214 1.90 lukem #define X_BUFHASH 4
215 1.153 christos { .n_name = "_bufhash" },
216 1.90 lukem #define X_BUFHASHTBL 5
217 1.153 christos { .n_name = "_bufhashtbl" },
218 1.122 junyoung #define X_UIHASH 6
219 1.153 christos { .n_name = "_uihash" },
220 1.122 junyoung #define X_UIHASHTBL 7
221 1.153 christos { .n_name = "_uihashtbl" },
222 1.122 junyoung #define X_IFADDRHASH 8
223 1.153 christos { .n_name = "_in_ifaddrhash" },
224 1.122 junyoung #define X_IFADDRHASHTBL 9
225 1.153 christos { .n_name = "_in_ifaddrhashtbl" },
226 1.122 junyoung #define X_NCHASH 10
227 1.153 christos { .n_name = "_nchash" },
228 1.122 junyoung #define X_NCHASHTBL 11
229 1.153 christos { .n_name = "_nchashtbl" },
230 1.122 junyoung #define X_NCVHASH 12
231 1.153 christos { .n_name = "_ncvhash" },
232 1.122 junyoung #define X_NCVHASHTBL 13
233 1.153 christos { .n_name = "_ncvhashtbl" },
234 1.122 junyoung #define X_HASHNL_SIZE 14 /* must be last */
235 1.153 christos { .n_name = NULL },
236 1.90 lukem };
237 1.87 lukem
238 1.90 lukem /*
239 1.90 lukem * Namelist for UVM histories
240 1.90 lukem */
241 1.90 lukem struct nlist histnl[] =
242 1.90 lukem {
243 1.153 christos { .n_name = "_uvm_histories" },
244 1.90 lukem #define X_UVM_HISTORIES 0
245 1.153 christos { .n_name = NULL },
246 1.1 cgd };
247 1.1 cgd
248 1.87 lukem
249 1.152 christos #define KILO 1024
250 1.90 lukem
251 1.171 christos struct cpu_counter {
252 1.171 christos uint64_t nintr;
253 1.171 christos uint64_t nsyscall;
254 1.171 christos uint64_t nswtch;
255 1.171 christos uint64_t nfault;
256 1.171 christos uint64_t ntrap;
257 1.171 christos uint64_t nsoft;
258 1.171 christos } cpucounter, ocpucounter;
259 1.171 christos
260 1.41 mrg struct uvmexp uvmexp, ouvmexp;
261 1.73 simonb int ndrives;
262 1.1 cgd
263 1.1 cgd int winlines = 20;
264 1.1 cgd
265 1.13 cgd kvm_t *kd;
266 1.13 cgd
267 1.174 christos
268 1.87 lukem #define FORKSTAT 1<<0
269 1.87 lukem #define INTRSTAT 1<<1
270 1.87 lukem #define MEMSTAT 1<<2
271 1.87 lukem #define SUMSTAT 1<<3
272 1.87 lukem #define EVCNTSTAT 1<<4
273 1.87 lukem #define VMSTAT 1<<5
274 1.87 lukem #define HISTLIST 1<<6
275 1.87 lukem #define HISTDUMP 1<<7
276 1.87 lukem #define HASHSTAT 1<<8
277 1.88 lukem #define HASHLIST 1<<9
278 1.130 he #define VMTOTAL 1<<10
279 1.154 ad #define POOLCACHESTAT 1<<11
280 1.1 cgd
281 1.151 yamt /*
282 1.151 yamt * Print single word. `ovflow' is number of characters didn't fit
283 1.151 yamt * on the last word. `fmt' is a format string to print this word.
284 1.151 yamt * It must contain asterisk for field width. `width' is a width
285 1.151 yamt * occupied by this word. `fixed' is a number of constant chars in
286 1.151 yamt * `fmt'. `val' is a value to be printed using format string `fmt'.
287 1.151 yamt */
288 1.151 yamt #define PRWORD(ovflw, fmt, width, fixed, val) do { \
289 1.151 yamt (ovflw) += printf((fmt), \
290 1.151 yamt (width) - (fixed) - (ovflw) > 0 ? \
291 1.151 yamt (width) - (fixed) - (ovflw) : 0, \
292 1.151 yamt (val)) - (width); \
293 1.151 yamt if ((ovflw) < 0) \
294 1.151 yamt (ovflw) = 0; \
295 1.151 yamt } while (/* CONSTCOND */0)
296 1.151 yamt
297 1.151 yamt void cpustats(int *);
298 1.171 christos void cpucounters(struct cpu_counter *);
299 1.87 lukem void deref_kptr(const void *, void *, size_t, const char *);
300 1.151 yamt void drvstats(int *);
301 1.73 simonb void doevcnt(int verbose);
302 1.88 lukem void dohashstat(int, int, const char *);
303 1.73 simonb void dointr(int verbose);
304 1.73 simonb void domem(void);
305 1.126 simonb void dopool(int, int);
306 1.154 ad void dopoolcache(void);
307 1.73 simonb void dosum(void);
308 1.103 mycroft void dovmstat(struct timespec *, int);
309 1.130 he void print_total_hdr(void);
310 1.130 he void dovmtotal(struct timespec *, int);
311 1.133 chs void kread(struct nlist *, int, void *, size_t);
312 1.147 kardel int kreadc(struct nlist *, int, void *, size_t);
313 1.73 simonb void needhdr(int);
314 1.73 simonb long getuptime(void);
315 1.73 simonb void printhdr(void);
316 1.73 simonb long pct(long, long);
317 1.73 simonb void usage(void);
318 1.73 simonb void doforkst(void);
319 1.73 simonb
320 1.73 simonb void hist_traverse(int, const char *);
321 1.73 simonb void hist_dodump(struct uvm_history *);
322 1.73 simonb
323 1.73 simonb int main(int, char **);
324 1.73 simonb char **choosedrives(char **);
325 1.38 mrg
326 1.29 thorpej /* Namelist and memory file names. */
327 1.29 thorpej char *nlistf, *memf;
328 1.29 thorpej
329 1.47 mrg /* allow old usage [vmstat 1] */
330 1.47 mrg #define BACKWARD_COMPATIBILITY
331 1.47 mrg
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.170 christos
335 1.38 mrg int
336 1.75 enami main(int argc, char *argv[])
337 1.1 cgd {
338 1.126 simonb int c, todo, verbose, wide;
339 1.103 mycroft struct timespec interval;
340 1.1 cgd int reps;
341 1.75 enami char errbuf[_POSIX2_LINE_MAX];
342 1.75 enami gid_t egid = getegid();
343 1.88 lukem const char *histname, *hashname;
344 1.165 lukem size_t i;
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.75 enami kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
434 1.75 enami if (kd == NULL)
435 1.87 lukem errx(1, "kvm_openfiles: %s", errbuf);
436 1.48 mrg
437 1.95 simonb if (nlistf == NULL && memf == NULL)
438 1.95 simonb (void)setgid(getgid());
439 1.1 cgd
440 1.13 cgd if ((c = kvm_nlist(kd, namelist)) != 0) {
441 1.147 kardel int doexit = 0;
442 1.90 lukem if (c == -1)
443 1.90 lukem errx(1, "kvm_nlist: %s %s", "namelist", kvm_geterr(kd));
444 1.165 lukem for (i = 0; i < sizeof(namelist) / sizeof(namelist[0])-1; i++)
445 1.165 lukem if (namelist[i].n_type == 0 &&
446 1.165 lukem i != X_TIME_SECOND &&
447 1.165 lukem i != X_TIME) {
448 1.147 kardel if (doexit++ == 0)
449 1.170 christos (void)fprintf(stderr,
450 1.170 christos "%s: undefined symbols:",
451 1.170 christos getprogname());
452 1.153 christos (void)fprintf(stderr, " %s",
453 1.165 lukem namelist[i].n_name);
454 1.147 kardel }
455 1.147 kardel if (doexit) {
456 1.147 kardel (void)fputc('\n', stderr);
457 1.147 kardel exit(1);
458 1.147 kardel }
459 1.1 cgd }
460 1.133 chs if (todo & INTRSTAT)
461 1.133 chs (void) kvm_nlist(kd, intrnl);
462 1.90 lukem if ((c = kvm_nlist(kd, hashnl)) == -1 || c == X_HASHNL_SIZE)
463 1.90 lukem errx(1, "kvm_nlist: %s %s", "hashnl", kvm_geterr(kd));
464 1.90 lukem if (kvm_nlist(kd, histnl) == -1)
465 1.90 lukem errx(1, "kvm_nlist: %s %s", "histnl", kvm_geterr(kd));
466 1.1 cgd
467 1.1 cgd if (todo & VMSTAT) {
468 1.1 cgd struct winsize winsize;
469 1.1 cgd
470 1.153 christos (void)drvinit(0);/* Initialize disk stats, no disks selected. */
471 1.49 drochner
472 1.49 drochner (void)setgid(getgid()); /* don't need privs anymore */
473 1.49 drochner
474 1.29 thorpej argv = choosedrives(argv); /* Select disks. */
475 1.1 cgd winsize.ws_row = 0;
476 1.115 simonb (void)ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize);
477 1.1 cgd if (winsize.ws_row > 0)
478 1.1 cgd winlines = winsize.ws_row;
479 1.1 cgd
480 1.1 cgd }
481 1.1 cgd
482 1.1 cgd #ifdef BACKWARD_COMPATIBILITY
483 1.1 cgd if (*argv) {
484 1.103 mycroft interval.tv_sec = atol(*argv);
485 1.1 cgd if (*++argv)
486 1.1 cgd reps = atoi(*argv);
487 1.1 cgd }
488 1.1 cgd #endif
489 1.1 cgd
490 1.103 mycroft if (interval.tv_sec) {
491 1.1 cgd if (!reps)
492 1.1 cgd reps = -1;
493 1.1 cgd } else if (reps)
494 1.103 mycroft interval.tv_sec = 1;
495 1.1 cgd
496 1.78 jhawk
497 1.78 jhawk /*
498 1.78 jhawk * Statistics dumping is incompatible with the default
499 1.78 jhawk * VMSTAT/dovmstat() output. So perform the interval/reps handling
500 1.78 jhawk * for it here.
501 1.78 jhawk */
502 1.130 he if ((todo & (VMSTAT|VMTOTAL)) == 0) {
503 1.99 enami for (;;) {
504 1.99 enami if (todo & (HISTLIST|HISTDUMP)) {
505 1.99 enami if ((todo & (HISTLIST|HISTDUMP)) ==
506 1.99 enami (HISTLIST|HISTDUMP))
507 1.99 enami errx(1, "you may list or dump,"
508 1.99 enami " but not both!");
509 1.99 enami hist_traverse(todo, histname);
510 1.153 christos (void)putchar('\n');
511 1.99 enami }
512 1.99 enami if (todo & FORKSTAT) {
513 1.99 enami doforkst();
514 1.153 christos (void)putchar('\n');
515 1.99 enami }
516 1.99 enami if (todo & MEMSTAT) {
517 1.99 enami domem();
518 1.126 simonb dopool(verbose, wide);
519 1.153 christos (void)putchar('\n');
520 1.99 enami }
521 1.154 ad if (todo & POOLCACHESTAT) {
522 1.154 ad dopoolcache();
523 1.154 ad (void)putchar('\n');
524 1.154 ad }
525 1.99 enami if (todo & SUMSTAT) {
526 1.99 enami dosum();
527 1.153 christos (void)putchar('\n');
528 1.99 enami }
529 1.99 enami if (todo & INTRSTAT) {
530 1.99 enami dointr(verbose);
531 1.153 christos (void)putchar('\n');
532 1.99 enami }
533 1.99 enami if (todo & EVCNTSTAT) {
534 1.99 enami doevcnt(verbose);
535 1.153 christos (void)putchar('\n');
536 1.99 enami }
537 1.99 enami if (todo & (HASHLIST|HASHSTAT)) {
538 1.99 enami if ((todo & (HASHLIST|HASHSTAT)) ==
539 1.99 enami (HASHLIST|HASHSTAT))
540 1.99 enami errx(1, "you may list or display,"
541 1.99 enami " but not both!");
542 1.99 enami dohashstat(verbose, todo, hashname);
543 1.153 christos (void)putchar('\n');
544 1.99 enami }
545 1.101 sommerfe
546 1.164 dholland fflush(stdout);
547 1.101 sommerfe if (reps >= 0 && --reps <=0)
548 1.99 enami break;
549 1.153 christos (void)nanosleep(&interval, NULL);
550 1.87 lukem }
551 1.130 he } else {
552 1.130 he if ((todo & (VMSTAT|VMTOTAL)) == (VMSTAT|VMTOTAL)) {
553 1.130 he errx(1, "you may not both do vmstat and vmtotal");
554 1.130 he }
555 1.130 he if (todo & VMSTAT)
556 1.130 he dovmstat(&interval, reps);
557 1.130 he if (todo & VMTOTAL)
558 1.130 he dovmtotal(&interval, reps);
559 1.130 he }
560 1.153 christos return 0;
561 1.1 cgd }
562 1.1 cgd
563 1.1 cgd char **
564 1.73 simonb choosedrives(char **argv)
565 1.1 cgd {
566 1.165 lukem size_t i;
567 1.1 cgd
568 1.1 cgd /*
569 1.1 cgd * Choose drives to be displayed. Priority goes to (in order) drives
570 1.1 cgd * supplied as arguments, default drives. If everything isn't filled
571 1.1 cgd * in and there are drives not taken care of, display the first few
572 1.1 cgd * that fit.
573 1.1 cgd */
574 1.75 enami #define BACKWARD_COMPATIBILITY
575 1.1 cgd for (ndrives = 0; *argv; ++argv) {
576 1.1 cgd #ifdef BACKWARD_COMPATIBILITY
577 1.124 dsl if (isdigit((unsigned char)**argv))
578 1.1 cgd break;
579 1.1 cgd #endif
580 1.140 blymn for (i = 0; i < ndrive; i++) {
581 1.1 cgd if (strcmp(dr_name[i], *argv))
582 1.1 cgd continue;
583 1.140 blymn drv_select[i] = 1;
584 1.136 blymn ++ndrives;
585 1.136 blymn break;
586 1.136 blymn }
587 1.1 cgd }
588 1.151 yamt for (i = 0; i < ndrive && ndrives < 2; i++) {
589 1.140 blymn if (drv_select[i])
590 1.1 cgd continue;
591 1.140 blymn drv_select[i] = 1;
592 1.1 cgd ++ndrives;
593 1.1 cgd }
594 1.140 blymn
595 1.75 enami return (argv);
596 1.1 cgd }
597 1.1 cgd
598 1.1 cgd long
599 1.73 simonb getuptime(void)
600 1.1 cgd {
601 1.30 cgd static struct timeval boottime;
602 1.145 kardel struct timeval now;
603 1.149 kardel time_t uptime, nowsec;
604 1.1 cgd
605 1.30 cgd if (boottime.tv_sec == 0)
606 1.133 chs kread(namelist, X_BOOTTIME, &boottime, sizeof(boottime));
607 1.149 kardel if (kreadc(namelist, X_TIME_SECOND, &nowsec, sizeof(nowsec))) {
608 1.149 kardel /*
609 1.149 kardel * XXX this assignment dance can be removed once timeval tv_sec
610 1.149 kardel * is SUS mandated time_t
611 1.149 kardel */
612 1.149 kardel now.tv_sec = nowsec;
613 1.147 kardel now.tv_usec = 0;
614 1.147 kardel } else {
615 1.147 kardel kread(namelist, X_TIME, &now, sizeof(now));
616 1.147 kardel }
617 1.145 kardel uptime = now.tv_sec - boottime.tv_sec;
618 1.87 lukem if (uptime <= 0 || uptime > 60*60*24*365*10)
619 1.87 lukem errx(1, "time makes no sense; namelist must be wrong.");
620 1.75 enami return (uptime);
621 1.1 cgd }
622 1.1 cgd
623 1.1 cgd int hz, hdrcnt;
624 1.1 cgd
625 1.1 cgd void
626 1.130 he print_total_hdr()
627 1.130 he {
628 1.130 he
629 1.166 rmind (void)printf("procs memory\n");
630 1.166 rmind (void)printf("ru dw pw sl");
631 1.130 he (void)printf(" total-v active-v active-r");
632 1.130 he (void)printf(" vm-sh avm-sh rm-sh arm-sh free\n");
633 1.130 he hdrcnt = winlines - 2;
634 1.130 he }
635 1.130 he
636 1.130 he void
637 1.130 he dovmtotal(struct timespec *interval, int reps)
638 1.130 he {
639 1.130 he struct vmtotal total;
640 1.130 he size_t size;
641 1.130 he
642 1.130 he (void)signal(SIGCONT, needhdr);
643 1.130 he
644 1.130 he for (hdrcnt = 1;;) {
645 1.130 he if (!--hdrcnt)
646 1.130 he print_total_hdr();
647 1.130 he if (memf != NULL) {
648 1.170 christos warnx("Unable to get vmtotals from crash dump.");
649 1.153 christos (void)memset(&total, 0, sizeof(total));
650 1.130 he } else {
651 1.130 he size = sizeof(total);
652 1.170 christos if (sysctl(vmmeter_mib, __arraycount(vmmeter_mib),
653 1.170 christos &total, &size, NULL, 0) == -1) {
654 1.170 christos warn("Can't get vmtotals");
655 1.153 christos (void)memset(&total, 0, sizeof(total));
656 1.130 he }
657 1.130 he }
658 1.153 christos (void)printf("%2d ", total.t_rq);
659 1.153 christos (void)printf("%2d ", total.t_dw);
660 1.153 christos (void)printf("%2d ", total.t_pw);
661 1.153 christos (void)printf("%2d ", total.t_sl);
662 1.153 christos
663 1.153 christos (void)printf("%9d ", total.t_vm);
664 1.153 christos (void)printf("%9d ", total.t_avm);
665 1.153 christos (void)printf("%9d ", total.t_arm);
666 1.153 christos (void)printf("%5d ", total.t_vmshr);
667 1.153 christos (void)printf("%6d ", total.t_avmshr);
668 1.153 christos (void)printf("%5d ", total.t_rmshr);
669 1.153 christos (void)printf("%6d ", total.t_armshr);
670 1.153 christos (void)printf("%5d", total.t_free);
671 1.130 he
672 1.153 christos (void)putchar('\n');
673 1.130 he
674 1.130 he (void)fflush(stdout);
675 1.130 he if (reps >= 0 && --reps <= 0)
676 1.130 he break;
677 1.130 he
678 1.153 christos (void)nanosleep(interval, NULL);
679 1.130 he }
680 1.130 he }
681 1.130 he
682 1.130 he void
683 1.103 mycroft dovmstat(struct timespec *interval, int reps)
684 1.1 cgd {
685 1.1 cgd struct vmtotal total;
686 1.1 cgd time_t uptime, halfuptime;
687 1.17 cgd size_t size;
688 1.41 mrg int pagesize = getpagesize();
689 1.151 yamt int ovflw;
690 1.1 cgd
691 1.1 cgd uptime = getuptime();
692 1.1 cgd halfuptime = uptime / 2;
693 1.1 cgd (void)signal(SIGCONT, needhdr);
694 1.1 cgd
695 1.13 cgd if (namelist[X_STATHZ].n_type != 0 && namelist[X_STATHZ].n_value != 0)
696 1.133 chs kread(namelist, X_STATHZ, &hz, sizeof(hz));
697 1.1 cgd if (!hz)
698 1.133 chs kread(namelist, X_HZ, &hz, sizeof(hz));
699 1.1 cgd
700 1.171 christos kread(namelist, X_CPU_QUEUE, &cpu_queue, sizeof(cpu_queue));
701 1.171 christos
702 1.1 cgd for (hdrcnt = 1;;) {
703 1.1 cgd if (!--hdrcnt)
704 1.1 cgd printhdr();
705 1.29 thorpej /* Read new disk statistics */
706 1.139 dsl cpureadstats();
707 1.140 blymn drvreadstats();
708 1.139 dsl tkreadstats();
709 1.133 chs kread(namelist, X_UVMEXP, &uvmexp, sizeof(uvmexp));
710 1.58 thorpej if (memf != NULL) {
711 1.58 thorpej /*
712 1.58 thorpej * XXX Can't do this if we're reading a crash
713 1.58 thorpej * XXX dump because they're lazily-calculated.
714 1.58 thorpej */
715 1.170 christos warnx("Unable to get vmtotals from crash dump.");
716 1.153 christos (void)memset(&total, 0, sizeof(total));
717 1.58 thorpej } else {
718 1.58 thorpej size = sizeof(total);
719 1.170 christos if (sysctl(vmmeter_mib, __arraycount(vmmeter_mib),
720 1.170 christos &total, &size, NULL, 0) == -1) {
721 1.170 christos warn("Can't get vmtotals");
722 1.153 christos (void)memset(&total, 0, sizeof(total));
723 1.58 thorpej }
724 1.13 cgd }
725 1.171 christos cpucounters(&cpucounter);
726 1.151 yamt ovflw = 0;
727 1.151 yamt PRWORD(ovflw, " %*d", 2, 1, total.t_rq - 1);
728 1.151 yamt PRWORD(ovflw, " %*d", 2, 1, total.t_dw + total.t_pw);
729 1.153 christos #define pgtok(a) (long)((a) * ((uint32_t)pagesize >> 10))
730 1.38 mrg #define rate(x) (u_long)(((x) + halfuptime) / uptime) /* round */
731 1.166 rmind PRWORD(ovflw, " %*ld", 9, 1, pgtok(total.t_avm));
732 1.151 yamt PRWORD(ovflw, " %*ld", 7, 1, pgtok(total.t_free));
733 1.151 yamt PRWORD(ovflw, " %*ld", 5, 1,
734 1.171 christos rate(cpucounter.nfault - ocpucounter.nfault));
735 1.151 yamt PRWORD(ovflw, " %*ld", 4, 1,
736 1.151 yamt rate(uvmexp.pdreact - ouvmexp.pdreact));
737 1.151 yamt PRWORD(ovflw, " %*ld", 4, 1,
738 1.151 yamt rate(uvmexp.pageins - ouvmexp.pageins));
739 1.151 yamt PRWORD(ovflw, " %*ld", 5, 1,
740 1.44 mrg rate(uvmexp.pgswapout - ouvmexp.pgswapout));
741 1.151 yamt PRWORD(ovflw, " %*ld", 5, 1,
742 1.151 yamt rate(uvmexp.pdfreed - ouvmexp.pdfreed));
743 1.151 yamt PRWORD(ovflw, " %*ld", 6, 2,
744 1.151 yamt rate(uvmexp.pdscans - ouvmexp.pdscans));
745 1.151 yamt drvstats(&ovflw);
746 1.151 yamt PRWORD(ovflw, " %*ld", 5, 1,
747 1.171 christos rate(cpucounter.nintr - ocpucounter.nintr));
748 1.151 yamt PRWORD(ovflw, " %*ld", 5, 1,
749 1.171 christos rate(cpucounter.nsyscall - ocpucounter.nsyscall));
750 1.151 yamt PRWORD(ovflw, " %*ld", 4, 1,
751 1.171 christos rate(cpucounter.nswtch - ocpucounter.nswtch));
752 1.151 yamt cpustats(&ovflw);
753 1.153 christos (void)putchar('\n');
754 1.42 mrg (void)fflush(stdout);
755 1.42 mrg if (reps >= 0 && --reps <= 0)
756 1.42 mrg break;
757 1.172 enami ouvmexp = uvmexp;
758 1.171 christos ocpucounter = cpucounter;
759 1.103 mycroft uptime = interval->tv_sec;
760 1.1 cgd /*
761 1.1 cgd * We round upward to avoid losing low-frequency events
762 1.1 cgd * (i.e., >= 1 per interval but < 1 per second).
763 1.1 cgd */
764 1.33 thorpej halfuptime = uptime == 1 ? 0 : (uptime + 1) / 2;
765 1.153 christos (void)nanosleep(interval, NULL);
766 1.1 cgd }
767 1.1 cgd }
768 1.1 cgd
769 1.38 mrg void
770 1.73 simonb printhdr(void)
771 1.1 cgd {
772 1.165 lukem size_t i;
773 1.1 cgd
774 1.104 mrg (void)printf(" procs memory page%*s", 23, "");
775 1.29 thorpej if (ndrives > 0)
776 1.70 sommerfe (void)printf("%s %*sfaults cpu\n",
777 1.75 enami ((ndrives > 1) ? "disks" : "disk"),
778 1.75 enami ((ndrives > 1) ? ndrives * 3 - 4 : 0), "");
779 1.1 cgd else
780 1.29 thorpej (void)printf("%*s faults cpu\n",
781 1.75 enami ndrives * 3, "");
782 1.29 thorpej
783 1.166 rmind (void)printf(" r b avm fre flt re pi po fr sr ");
784 1.140 blymn for (i = 0; i < ndrive; i++)
785 1.140 blymn if (drv_select[i])
786 1.1 cgd (void)printf("%c%c ", dr_name[i][0],
787 1.1 cgd dr_name[i][strlen(dr_name[i]) - 1]);
788 1.1 cgd (void)printf(" in sy cs us sy id\n");
789 1.1 cgd hdrcnt = winlines - 2;
790 1.1 cgd }
791 1.1 cgd
792 1.1 cgd /*
793 1.1 cgd * Force a header to be prepended to the next output.
794 1.1 cgd */
795 1.1 cgd void
796 1.153 christos /*ARGSUSED*/
797 1.73 simonb needhdr(int dummy)
798 1.1 cgd {
799 1.1 cgd
800 1.1 cgd hdrcnt = 1;
801 1.1 cgd }
802 1.1 cgd
803 1.38 mrg long
804 1.73 simonb pct(long top, long bot)
805 1.1 cgd {
806 1.13 cgd long ans;
807 1.13 cgd
808 1.1 cgd if (bot == 0)
809 1.75 enami return (0);
810 1.153 christos ans = (long)((quad_t)top * 100 / bot);
811 1.13 cgd return (ans);
812 1.1 cgd }
813 1.1 cgd
814 1.38 mrg #define PCT(top, bot) (int)pct((long)(top), (long)(bot))
815 1.1 cgd
816 1.1 cgd void
817 1.73 simonb dosum(void)
818 1.1 cgd {
819 1.1 cgd struct nchstats nchstats;
820 1.123 enami u_long nchtotal;
821 1.162 he struct uvmexp_sysctl uvmexp2;
822 1.162 he size_t ssize;
823 1.162 he int active_kernel;
824 1.171 christos struct cpu_counter cc;
825 1.162 he
826 1.162 he /*
827 1.162 he * The "active" and "inactive" variables
828 1.162 he * are now estimated by the kernel and sadly
829 1.162 he * can not easily be dug out of a crash dump.
830 1.162 he */
831 1.162 he ssize = sizeof(uvmexp2);
832 1.162 he memset(&uvmexp2, 0, ssize);
833 1.162 he active_kernel = (memf == NULL);
834 1.162 he if (active_kernel) {
835 1.162 he /* only on active kernel */
836 1.170 christos if (sysctl(uvmexp2_mib, __arraycount(uvmexp2_mib), &uvmexp2,
837 1.170 christos &ssize, NULL, 0) == -1)
838 1.170 christos warn("sysctl vm.uvmexp2 failed");
839 1.162 he }
840 1.1 cgd
841 1.133 chs kread(namelist, X_UVMEXP, &uvmexp, sizeof(uvmexp));
842 1.41 mrg
843 1.44 mrg (void)printf("%9u bytes per page\n", uvmexp.pagesize);
844 1.44 mrg
845 1.81 thorpej (void)printf("%9u page color%s\n",
846 1.81 thorpej uvmexp.ncolors, uvmexp.ncolors == 1 ? "" : "s");
847 1.81 thorpej
848 1.44 mrg (void)printf("%9u pages managed\n", uvmexp.npages);
849 1.44 mrg (void)printf("%9u pages free\n", uvmexp.free);
850 1.162 he if (active_kernel) {
851 1.162 he (void)printf("%9" PRIu64 " pages active\n", uvmexp2.active);
852 1.162 he (void)printf("%9" PRIu64 " pages inactive\n", uvmexp2.inactive);
853 1.162 he }
854 1.44 mrg (void)printf("%9u pages paging\n", uvmexp.paging);
855 1.44 mrg (void)printf("%9u pages wired\n", uvmexp.wired);
856 1.63 thorpej (void)printf("%9u zero pages\n", uvmexp.zeropages);
857 1.44 mrg (void)printf("%9u reserve pagedaemon pages\n",
858 1.44 mrg uvmexp.reserve_pagedaemon);
859 1.44 mrg (void)printf("%9u reserve kernel pages\n", uvmexp.reserve_kernel);
860 1.94 chs (void)printf("%9u anonymous pages\n", uvmexp.anonpages);
861 1.94 chs (void)printf("%9u cached file pages\n", uvmexp.filepages);
862 1.94 chs (void)printf("%9u cached executable pages\n", uvmexp.execpages);
863 1.44 mrg
864 1.44 mrg (void)printf("%9u minimum free pages\n", uvmexp.freemin);
865 1.44 mrg (void)printf("%9u target free pages\n", uvmexp.freetarg);
866 1.44 mrg (void)printf("%9u maximum wired pages\n", uvmexp.wiredmax);
867 1.44 mrg
868 1.44 mrg (void)printf("%9u swap devices\n", uvmexp.nswapdev);
869 1.44 mrg (void)printf("%9u swap pages\n", uvmexp.swpages);
870 1.44 mrg (void)printf("%9u swap pages in use\n", uvmexp.swpginuse);
871 1.44 mrg (void)printf("%9u swap allocations\n", uvmexp.nswget);
872 1.44 mrg
873 1.171 christos kread(namelist, X_CPU_QUEUE, &cpu_queue, sizeof(cpu_queue));
874 1.171 christos cpucounters(&cc);
875 1.171 christos (void)printf("%9" PRIu64 " total faults taken\n", cc.nfault);
876 1.171 christos (void)printf("%9" PRIu64 " traps\n", cc.ntrap);
877 1.171 christos (void)printf("%9" PRIu64 " device interrupts\n", cc.nintr);
878 1.171 christos (void)printf("%9" PRIu64 " CPU context switches\n", cc.nswtch);
879 1.171 christos (void)printf("%9" PRIu64 " software interrupts\n", cc.nsoft);
880 1.171 christos (void)printf("%9" PRIu64 " system calls\n", cc.nsyscall);
881 1.60 fredb (void)printf("%9u pagein requests\n", uvmexp.pageins);
882 1.60 fredb (void)printf("%9u pageout requests\n", uvmexp.pdpageouts);
883 1.60 fredb (void)printf("%9u pages swapped in\n", uvmexp.pgswapin);
884 1.60 fredb (void)printf("%9u pages swapped out\n", uvmexp.pgswapout);
885 1.43 mrg (void)printf("%9u forks total\n", uvmexp.forks);
886 1.43 mrg (void)printf("%9u forks blocked parent\n", uvmexp.forks_ppwait);
887 1.43 mrg (void)printf("%9u forks shared address space with parent\n",
888 1.43 mrg uvmexp.forks_sharevm);
889 1.63 thorpej (void)printf("%9u pagealloc zero wanted and avail\n",
890 1.63 thorpej uvmexp.pga_zerohit);
891 1.63 thorpej (void)printf("%9u pagealloc zero wanted and not avail\n",
892 1.63 thorpej uvmexp.pga_zeromiss);
893 1.68 thorpej (void)printf("%9u aborts of idle page zeroing\n",
894 1.68 thorpej uvmexp.zeroaborts);
895 1.79 thorpej (void)printf("%9u pagealloc desired color avail\n",
896 1.79 thorpej uvmexp.colorhit);
897 1.79 thorpej (void)printf("%9u pagealloc desired color not avail\n",
898 1.79 thorpej uvmexp.colormiss);
899 1.159 ad (void)printf("%9u pagealloc local cpu avail\n",
900 1.159 ad uvmexp.cpuhit);
901 1.159 ad (void)printf("%9u pagealloc local cpu not avail\n",
902 1.159 ad uvmexp.cpumiss);
903 1.44 mrg
904 1.44 mrg (void)printf("%9u faults with no memory\n", uvmexp.fltnoram);
905 1.44 mrg (void)printf("%9u faults with no anons\n", uvmexp.fltnoanon);
906 1.43 mrg (void)printf("%9u faults had to wait on pages\n", uvmexp.fltpgwait);
907 1.43 mrg (void)printf("%9u faults found released page\n", uvmexp.fltpgrele);
908 1.43 mrg (void)printf("%9u faults relock (%u ok)\n", uvmexp.fltrelck,
909 1.43 mrg uvmexp.fltrelckok);
910 1.43 mrg (void)printf("%9u anon page faults\n", uvmexp.fltanget);
911 1.43 mrg (void)printf("%9u anon retry faults\n", uvmexp.fltanretry);
912 1.43 mrg (void)printf("%9u amap copy faults\n", uvmexp.fltamcopy);
913 1.43 mrg (void)printf("%9u neighbour anon page faults\n", uvmexp.fltnamap);
914 1.43 mrg (void)printf("%9u neighbour object page faults\n", uvmexp.fltnomap);
915 1.43 mrg (void)printf("%9u locked pager get faults\n", uvmexp.fltlget);
916 1.43 mrg (void)printf("%9u unlocked pager get faults\n", uvmexp.fltget);
917 1.43 mrg (void)printf("%9u anon faults\n", uvmexp.flt_anon);
918 1.43 mrg (void)printf("%9u anon copy on write faults\n", uvmexp.flt_acow);
919 1.43 mrg (void)printf("%9u object faults\n", uvmexp.flt_obj);
920 1.43 mrg (void)printf("%9u promote copy faults\n", uvmexp.flt_prcopy);
921 1.43 mrg (void)printf("%9u promote zero fill faults\n", uvmexp.flt_przero);
922 1.44 mrg
923 1.44 mrg (void)printf("%9u times daemon wokeup\n",uvmexp.pdwoke);
924 1.44 mrg (void)printf("%9u revolutions of the clock hand\n", uvmexp.pdrevs);
925 1.44 mrg (void)printf("%9u pages freed by daemon\n", uvmexp.pdfreed);
926 1.44 mrg (void)printf("%9u pages scanned by daemon\n", uvmexp.pdscans);
927 1.75 enami (void)printf("%9u anonymous pages scanned by daemon\n",
928 1.75 enami uvmexp.pdanscan);
929 1.44 mrg (void)printf("%9u object pages scanned by daemon\n", uvmexp.pdobscan);
930 1.44 mrg (void)printf("%9u pages reactivated\n", uvmexp.pdreact);
931 1.44 mrg (void)printf("%9u pages found busy by daemon\n", uvmexp.pdbusy);
932 1.44 mrg (void)printf("%9u total pending pageouts\n", uvmexp.pdpending);
933 1.44 mrg (void)printf("%9u pages deactivated\n", uvmexp.pddeact);
934 1.123 enami
935 1.133 chs kread(namelist, X_NCHSTATS, &nchstats, sizeof(nchstats));
936 1.1 cgd nchtotal = nchstats.ncs_goodhits + nchstats.ncs_neghits +
937 1.1 cgd nchstats.ncs_badhits + nchstats.ncs_falsehits +
938 1.1 cgd nchstats.ncs_miss + nchstats.ncs_long;
939 1.123 enami (void)printf("%9lu total name lookups\n", nchtotal);
940 1.123 enami (void)printf("%9lu good hits\n", nchstats.ncs_goodhits);
941 1.123 enami (void)printf("%9lu negative hits\n", nchstats.ncs_neghits);
942 1.123 enami (void)printf("%9lu bad hits\n", nchstats.ncs_badhits);
943 1.123 enami (void)printf("%9lu false hits\n", nchstats.ncs_falsehits);
944 1.123 enami (void)printf("%9lu miss\n", nchstats.ncs_miss);
945 1.123 enami (void)printf("%9lu too long\n", nchstats.ncs_long);
946 1.123 enami (void)printf("%9lu pass2 hits\n", nchstats.ncs_pass2);
947 1.123 enami (void)printf("%9lu 2passes\n", nchstats.ncs_2passes);
948 1.1 cgd (void)printf(
949 1.1 cgd "%9s cache hits (%d%% pos + %d%% neg) system %d%% per-process\n",
950 1.1 cgd "", PCT(nchstats.ncs_goodhits, nchtotal),
951 1.1 cgd PCT(nchstats.ncs_neghits, nchtotal),
952 1.1 cgd PCT(nchstats.ncs_pass2, nchtotal));
953 1.1 cgd (void)printf("%9s deletions %d%%, falsehits %d%%, toolong %d%%\n", "",
954 1.1 cgd PCT(nchstats.ncs_badhits, nchtotal),
955 1.1 cgd PCT(nchstats.ncs_falsehits, nchtotal),
956 1.1 cgd PCT(nchstats.ncs_long, nchtotal));
957 1.1 cgd }
958 1.1 cgd
959 1.1 cgd void
960 1.73 simonb doforkst(void)
961 1.1 cgd {
962 1.41 mrg
963 1.133 chs kread(namelist, X_UVMEXP, &uvmexp, sizeof(uvmexp));
964 1.58 thorpej
965 1.41 mrg (void)printf("%u forks total\n", uvmexp.forks);
966 1.41 mrg (void)printf("%u forks blocked parent\n", uvmexp.forks_ppwait);
967 1.41 mrg (void)printf("%u forks shared address space with parent\n",
968 1.41 mrg uvmexp.forks_sharevm);
969 1.1 cgd }
970 1.1 cgd
971 1.1 cgd void
972 1.151 yamt drvstats(int *ovflwp)
973 1.1 cgd {
974 1.165 lukem size_t dn;
975 1.1 cgd double etime;
976 1.151 yamt int ovflw = *ovflwp;
977 1.1 cgd
978 1.29 thorpej /* Calculate disk stat deltas. */
979 1.139 dsl cpuswap();
980 1.140 blymn drvswap();
981 1.139 dsl tkswap();
982 1.101 sommerfe etime = cur.cp_etime;
983 1.101 sommerfe
984 1.140 blymn for (dn = 0; dn < ndrive; ++dn) {
985 1.140 blymn if (!drv_select[dn])
986 1.151 yamt continue;
987 1.151 yamt PRWORD(ovflw, " %*.0f", 3, 1,
988 1.151 yamt (cur.rxfer[dn] + cur.wxfer[dn]) / etime);
989 1.136 blymn }
990 1.151 yamt *ovflwp = ovflw;
991 1.136 blymn }
992 1.136 blymn
993 1.136 blymn void
994 1.171 christos cpucounters(struct cpu_counter *cc)
995 1.171 christos {
996 1.175 christos struct cpu_info *ci;
997 1.171 christos (void)memset(cc, 0, sizeof(*cc));
998 1.171 christos CIRCLEQ_FOREACH(ci, &cpu_queue, ci_data.cpu_qchain) {
999 1.175 christos struct cpu_info tci;
1000 1.171 christos if ((size_t)kvm_read(kd, (u_long)ci, &tci, sizeof(tci))
1001 1.171 christos != sizeof(tci)) {
1002 1.171 christos warnx("Can't read cpu info from %p (%s)",
1003 1.171 christos ci, kvm_geterr(kd));
1004 1.173 christos (void)memset(cc, 0, sizeof(*cc));
1005 1.173 christos return;
1006 1.171 christos }
1007 1.171 christos /* Found the fake element, done */
1008 1.171 christos if (tci.ci_data.cpu_qchain.cqe_prev == NULL)
1009 1.171 christos break;
1010 1.171 christos cc->nintr += tci.ci_data.cpu_nintr;
1011 1.171 christos cc->nsyscall += tci.ci_data.cpu_nsyscall;
1012 1.171 christos cc->nswtch = tci.ci_data.cpu_nswtch;
1013 1.171 christos cc->nfault = tci.ci_data.cpu_nfault;
1014 1.171 christos cc->ntrap = tci.ci_data.cpu_ntrap;
1015 1.171 christos cc->nsoft = tci.ci_data.cpu_nsoft;
1016 1.171 christos ci = &tci;
1017 1.171 christos }
1018 1.171 christos }
1019 1.171 christos
1020 1.171 christos void
1021 1.151 yamt cpustats(int *ovflwp)
1022 1.1 cgd {
1023 1.38 mrg int state;
1024 1.129 dsl double pcnt, total;
1025 1.120 dbj double stat_us, stat_sy, stat_id;
1026 1.151 yamt int ovflw = *ovflwp;
1027 1.1 cgd
1028 1.1 cgd total = 0;
1029 1.1 cgd for (state = 0; state < CPUSTATES; ++state)
1030 1.29 thorpej total += cur.cp_time[state];
1031 1.1 cgd if (total)
1032 1.129 dsl pcnt = 100 / total;
1033 1.1 cgd else
1034 1.129 dsl pcnt = 0;
1035 1.129 dsl stat_us = (cur.cp_time[CP_USER] + cur.cp_time[CP_NICE]) * pcnt;
1036 1.129 dsl stat_sy = (cur.cp_time[CP_SYS] + cur.cp_time[CP_INTR]) * pcnt;
1037 1.129 dsl stat_id = cur.cp_time[CP_IDLE] * pcnt;
1038 1.151 yamt PRWORD(ovflw, " %*.0f", ((stat_sy >= 100) ? 2 : 3), 1, stat_us);
1039 1.151 yamt PRWORD(ovflw, " %*.0f", ((stat_us >= 100 || stat_id >= 100) ? 2 : 3), 1,
1040 1.151 yamt stat_sy);
1041 1.151 yamt PRWORD(ovflw, " %*.0f", 3, 1, stat_id);
1042 1.151 yamt *ovflwp = ovflw;
1043 1.1 cgd }
1044 1.1 cgd
1045 1.1 cgd void
1046 1.66 cgd dointr(int verbose)
1047 1.1 cgd {
1048 1.138 nonaka unsigned long *intrcnt, *ointrcnt;
1049 1.85 enami unsigned long long inttotal, uptime;
1050 1.38 mrg int nintr, inamlen;
1051 1.138 nonaka char *intrname, *ointrname;
1052 1.28 cgd struct evcntlist allevents;
1053 1.28 cgd struct evcnt evcnt, *evptr;
1054 1.66 cgd char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
1055 1.1 cgd
1056 1.133 chs inttotal = 0;
1057 1.1 cgd uptime = getuptime();
1058 1.83 matt (void)printf("%-34s %16s %8s\n", "interrupt", "total", "rate");
1059 1.133 chs nintr = intrnl[X_EINTRCNT].n_value - intrnl[X_INTRCNT].n_value;
1060 1.133 chs inamlen = intrnl[X_EINTRNAMES].n_value - intrnl[X_INTRNAMES].n_value;
1061 1.133 chs if (nintr != 0 && inamlen != 0) {
1062 1.138 nonaka ointrcnt = intrcnt = malloc((size_t)nintr);
1063 1.138 nonaka ointrname = intrname = malloc((size_t)inamlen);
1064 1.133 chs if (intrcnt == NULL || intrname == NULL)
1065 1.133 chs errx(1, "%s", "");
1066 1.133 chs kread(intrnl, X_INTRCNT, intrcnt, (size_t)nintr);
1067 1.133 chs kread(intrnl, X_INTRNAMES, intrname, (size_t)inamlen);
1068 1.133 chs nintr /= sizeof(long);
1069 1.133 chs while (--nintr >= 0) {
1070 1.133 chs if (*intrcnt || verbose)
1071 1.133 chs (void)printf("%-34s %16llu %8llu\n", intrname,
1072 1.133 chs (unsigned long long)*intrcnt,
1073 1.133 chs (unsigned long long)
1074 1.133 chs (*intrcnt / uptime));
1075 1.133 chs intrname += strlen(intrname) + 1;
1076 1.133 chs inttotal += *intrcnt++;
1077 1.133 chs }
1078 1.138 nonaka free(ointrcnt);
1079 1.138 nonaka free(ointrname);
1080 1.1 cgd }
1081 1.133 chs
1082 1.133 chs kread(namelist, X_ALLEVENTS, &allevents, sizeof allevents);
1083 1.133 chs evptr = TAILQ_FIRST(&allevents);
1084 1.28 cgd while (evptr) {
1085 1.87 lukem deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed");
1086 1.133 chs evptr = TAILQ_NEXT(&evcnt, ev_list);
1087 1.66 cgd if (evcnt.ev_type != EVCNT_TYPE_INTR)
1088 1.66 cgd continue;
1089 1.66 cgd
1090 1.66 cgd if (evcnt.ev_count == 0 && !verbose)
1091 1.66 cgd continue;
1092 1.66 cgd
1093 1.153 christos deref_kptr(evcnt.ev_group, evgroup,
1094 1.153 christos (size_t)evcnt.ev_grouplen + 1, "event chain trashed");
1095 1.153 christos deref_kptr(evcnt.ev_name, evname,
1096 1.153 christos (size_t)evcnt.ev_namelen + 1, "event chain trashed");
1097 1.66 cgd
1098 1.85 enami (void)printf("%s %s%*s %16llu %8llu\n", evgroup, evname,
1099 1.83 matt 34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
1100 1.85 enami (unsigned long long)evcnt.ev_count,
1101 1.85 enami (unsigned long long)(evcnt.ev_count / uptime));
1102 1.66 cgd
1103 1.66 cgd inttotal += evcnt.ev_count++;
1104 1.66 cgd }
1105 1.85 enami (void)printf("%-34s %16llu %8llu\n", "Total", inttotal,
1106 1.85 enami (unsigned long long)(inttotal / uptime));
1107 1.66 cgd }
1108 1.66 cgd
1109 1.66 cgd void
1110 1.66 cgd doevcnt(int verbose)
1111 1.66 cgd {
1112 1.83 matt static const char * evtypes [] = { "misc", "intr", "trap" };
1113 1.85 enami unsigned long long uptime;
1114 1.66 cgd struct evcntlist allevents;
1115 1.66 cgd struct evcnt evcnt, *evptr;
1116 1.66 cgd char evgroup[EVCNT_STRING_MAX], evname[EVCNT_STRING_MAX];
1117 1.66 cgd
1118 1.66 cgd /* XXX should print type! */
1119 1.66 cgd
1120 1.66 cgd uptime = getuptime();
1121 1.83 matt (void)printf("%-34s %16s %8s %s\n", "event", "total", "rate", "type");
1122 1.133 chs kread(namelist, X_ALLEVENTS, &allevents, sizeof allevents);
1123 1.133 chs evptr = TAILQ_FIRST(&allevents);
1124 1.66 cgd while (evptr) {
1125 1.87 lukem deref_kptr(evptr, &evcnt, sizeof(evcnt), "event chain trashed");
1126 1.32 cgd
1127 1.133 chs evptr = TAILQ_NEXT(&evcnt, ev_list);
1128 1.66 cgd if (evcnt.ev_count == 0 && !verbose)
1129 1.66 cgd continue;
1130 1.66 cgd
1131 1.153 christos deref_kptr(evcnt.ev_group, evgroup,
1132 1.153 christos (size_t)evcnt.ev_grouplen + 1, "event chain trashed");
1133 1.153 christos deref_kptr(evcnt.ev_name, evname,
1134 1.153 christos (size_t)evcnt.ev_namelen + 1, "event chain trashed");
1135 1.66 cgd
1136 1.85 enami (void)printf("%s %s%*s %16llu %8llu %s\n", evgroup, evname,
1137 1.83 matt 34 - (evcnt.ev_grouplen + 1 + evcnt.ev_namelen), "",
1138 1.85 enami (unsigned long long)evcnt.ev_count,
1139 1.85 enami (unsigned long long)(evcnt.ev_count / uptime),
1140 1.86 enami (evcnt.ev_type < sizeof(evtypes)/sizeof(evtypes[0]) ?
1141 1.86 enami evtypes[evcnt.ev_type] : "?"));
1142 1.18 pk }
1143 1.1 cgd }
1144 1.1 cgd
1145 1.109 thorpej static char memname[64];
1146 1.1 cgd
1147 1.1 cgd void
1148 1.73 simonb domem(void)
1149 1.1 cgd {
1150 1.38 mrg struct kmembuckets *kp;
1151 1.109 thorpej struct malloc_type ks, *ksp;
1152 1.38 mrg int i, j;
1153 1.13 cgd int len, size, first;
1154 1.13 cgd long totuse = 0, totfree = 0, totreq = 0;
1155 1.1 cgd struct kmembuckets buckets[MINBUCKET + 16];
1156 1.1 cgd
1157 1.133 chs kread(namelist, X_KMEMBUCKETS, buckets, sizeof(buckets));
1158 1.34 thorpej for (first = 1, i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16;
1159 1.34 thorpej i++, kp++) {
1160 1.1 cgd if (kp->kb_calls == 0)
1161 1.1 cgd continue;
1162 1.34 thorpej if (first) {
1163 1.34 thorpej (void)printf("Memory statistics by bucket size\n");
1164 1.34 thorpej (void)printf(
1165 1.34 thorpej " Size In Use Free Requests HighWater Couldfree\n");
1166 1.34 thorpej first = 0;
1167 1.34 thorpej }
1168 1.1 cgd size = 1 << i;
1169 1.75 enami (void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
1170 1.75 enami kp->kb_total - kp->kb_totalfree,
1171 1.75 enami kp->kb_totalfree, kp->kb_calls,
1172 1.75 enami kp->kb_highwat, kp->kb_couldfree);
1173 1.1 cgd totfree += size * kp->kb_totalfree;
1174 1.34 thorpej }
1175 1.34 thorpej
1176 1.34 thorpej /*
1177 1.34 thorpej * If kmem statistics are not being gathered by the kernel,
1178 1.34 thorpej * first will still be 1.
1179 1.34 thorpej */
1180 1.34 thorpej if (first) {
1181 1.87 lukem warnx("Kmem statistics are not being gathered by the kernel.");
1182 1.34 thorpej return;
1183 1.1 cgd }
1184 1.1 cgd
1185 1.13 cgd (void)printf("\nMemory usage type by bucket size\n");
1186 1.13 cgd (void)printf(" Size Type(s)\n");
1187 1.13 cgd kp = &buckets[MINBUCKET];
1188 1.13 cgd for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1, kp++) {
1189 1.13 cgd if (kp->kb_calls == 0)
1190 1.13 cgd continue;
1191 1.13 cgd first = 1;
1192 1.13 cgd len = 8;
1193 1.133 chs for (kread(namelist, X_KMEMSTAT, &ksp, sizeof(ksp));
1194 1.109 thorpej ksp != NULL; ksp = ks.ks_next) {
1195 1.109 thorpej deref_kptr(ksp, &ks, sizeof(ks), "malloc type");
1196 1.109 thorpej if (ks.ks_calls == 0)
1197 1.13 cgd continue;
1198 1.109 thorpej if ((ks.ks_size & j) == 0)
1199 1.13 cgd continue;
1200 1.109 thorpej deref_kptr(ks.ks_shortdesc, memname,
1201 1.109 thorpej sizeof(memname), "malloc type name");
1202 1.109 thorpej len += 2 + strlen(memname);
1203 1.13 cgd if (first)
1204 1.153 christos (void)printf("%8d %s", j, memname);
1205 1.13 cgd else
1206 1.153 christos (void)printf(",");
1207 1.13 cgd if (len >= 80) {
1208 1.153 christos (void)printf("\n\t ");
1209 1.109 thorpej len = 10 + strlen(memname);
1210 1.13 cgd }
1211 1.13 cgd if (!first)
1212 1.153 christos (void)printf(" %s", memname);
1213 1.13 cgd first = 0;
1214 1.13 cgd }
1215 1.153 christos (void)putchar('\n');
1216 1.13 cgd }
1217 1.13 cgd
1218 1.1 cgd (void)printf(
1219 1.160 he "\nMemory statistics by type Type Kern\n");
1220 1.13 cgd (void)printf(
1221 1.160 he " Type InUse MemUse HighUse Limit Requests Limit Limit Size(s)\n");
1222 1.133 chs for (kread(namelist, X_KMEMSTAT, &ksp, sizeof(ksp));
1223 1.109 thorpej ksp != NULL; ksp = ks.ks_next) {
1224 1.109 thorpej deref_kptr(ksp, &ks, sizeof(ks), "malloc type");
1225 1.109 thorpej if (ks.ks_calls == 0)
1226 1.1 cgd continue;
1227 1.109 thorpej deref_kptr(ks.ks_shortdesc, memname,
1228 1.109 thorpej sizeof(memname), "malloc type name");
1229 1.160 he (void)printf("%15s %5ld %6ldK %6ldK %6ldK %10ld %5u %5u",
1230 1.109 thorpej memname,
1231 1.152 christos ks.ks_inuse, howmany(ks.ks_memuse, KILO),
1232 1.152 christos howmany(ks.ks_maxused, KILO),
1233 1.152 christos howmany(ks.ks_limit, KILO), ks.ks_calls,
1234 1.109 thorpej ks.ks_limblocks, ks.ks_mapblocks);
1235 1.13 cgd first = 1;
1236 1.167 he for (j = 1 << MINBUCKET, i = MINBUCKET;
1237 1.167 he j < 1 << (MINBUCKET + 16);
1238 1.167 he j <<= 1, i++)
1239 1.167 he {
1240 1.109 thorpej if ((ks.ks_size & j) == 0)
1241 1.13 cgd continue;
1242 1.13 cgd if (first)
1243 1.160 he (void)printf(" %d", j);
1244 1.13 cgd else
1245 1.153 christos (void)printf(",%d", j);
1246 1.13 cgd first = 0;
1247 1.168 he (void)printf(":%u", ks.ks_active[i - MINBUCKET]);
1248 1.13 cgd }
1249 1.153 christos (void)printf("\n");
1250 1.109 thorpej totuse += ks.ks_memuse;
1251 1.109 thorpej totreq += ks.ks_calls;
1252 1.1 cgd }
1253 1.102 soren (void)printf("\nMemory totals: In Use Free Requests\n");
1254 1.102 soren (void)printf(" %7ldK %6ldK %8ld\n\n",
1255 1.152 christos howmany(totuse, KILO), howmany(totfree, KILO), totreq);
1256 1.51 pk }
1257 1.51 pk
1258 1.51 pk void
1259 1.126 simonb dopool(int verbose, int wide)
1260 1.51 pk {
1261 1.69 enami int first, ovflw;
1262 1.87 lukem void *addr;
1263 1.126 simonb long total, inuse, this_total, this_inuse;
1264 1.157 ad TAILQ_HEAD(,pool) pool_head;
1265 1.51 pk struct pool pool, *pp = &pool;
1266 1.98 christos struct pool_allocator pa;
1267 1.87 lukem char name[32], maxp[32];
1268 1.51 pk
1269 1.133 chs kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
1270 1.157 ad addr = TAILQ_FIRST(&pool_head);
1271 1.51 pk
1272 1.126 simonb total = inuse = 0;
1273 1.126 simonb
1274 1.157 ad for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) {
1275 1.87 lukem deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
1276 1.98 christos deref_kptr(pp->pr_alloc, &pa, sizeof(pa),
1277 1.125 dsainty "pool allocator trashed");
1278 1.87 lukem deref_kptr(pp->pr_wchan, name, sizeof(name),
1279 1.98 christos "pool wait channel trashed");
1280 1.87 lukem name[sizeof(name)-1] = '\0';
1281 1.51 pk
1282 1.51 pk if (first) {
1283 1.51 pk (void)printf("Memory resource pool statistics\n");
1284 1.51 pk (void)printf(
1285 1.126 simonb "%-*s%*s%*s%5s%*s%s%s%*s%*s%6s%s%6s%6s%6s%5s%s%s\n",
1286 1.126 simonb wide ? 16 : 11, "Name",
1287 1.126 simonb wide ? 6 : 5, "Size",
1288 1.126 simonb wide ? 12 : 9, "Requests",
1289 1.75 enami "Fail",
1290 1.126 simonb wide ? 12 : 9, "Releases",
1291 1.148 simonb wide ? " InUse" : "",
1292 1.126 simonb wide ? " Avail" : "",
1293 1.126 simonb wide ? 7 : 6, "Pgreq",
1294 1.126 simonb wide ? 7 : 6, "Pgrel",
1295 1.75 enami "Npage",
1296 1.126 simonb wide ? " PageSz" : "",
1297 1.75 enami "Hiwat",
1298 1.75 enami "Minpg",
1299 1.75 enami "Maxpg",
1300 1.126 simonb "Idle",
1301 1.126 simonb wide ? " Flags" : "",
1302 1.126 simonb wide ? " Util" : "");
1303 1.51 pk first = 0;
1304 1.51 pk }
1305 1.113 dsl if (pp->pr_nget == 0 && !verbose)
1306 1.113 dsl continue;
1307 1.51 pk if (pp->pr_maxpages == UINT_MAX)
1308 1.153 christos (void)snprintf(maxp, sizeof(maxp), "inf");
1309 1.51 pk else
1310 1.153 christos (void)snprintf(maxp, sizeof(maxp), "%u",
1311 1.153 christos pp->pr_maxpages);
1312 1.69 enami ovflw = 0;
1313 1.126 simonb PRWORD(ovflw, "%-*s", wide ? 16 : 11, 0, name);
1314 1.126 simonb PRWORD(ovflw, " %*u", wide ? 6 : 5, 1, pp->pr_size);
1315 1.126 simonb PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nget);
1316 1.69 enami PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail);
1317 1.126 simonb PRWORD(ovflw, " %*lu", wide ? 12 : 9, 1, pp->pr_nput);
1318 1.126 simonb if (wide)
1319 1.148 simonb PRWORD(ovflw, " %*u", 7, 1, pp->pr_nout);
1320 1.126 simonb if (wide)
1321 1.126 simonb PRWORD(ovflw, " %*u", 6, 1, pp->pr_nitems);
1322 1.126 simonb PRWORD(ovflw, " %*lu", wide ? 7 : 6, 1, pp->pr_npagealloc);
1323 1.126 simonb PRWORD(ovflw, " %*lu", wide ? 7 : 6, 1, pp->pr_npagefree);
1324 1.126 simonb PRWORD(ovflw, " %*u", 6, 1, pp->pr_npages);
1325 1.126 simonb if (wide)
1326 1.126 simonb PRWORD(ovflw, " %*u", 7, 1, pa.pa_pagesz);
1327 1.126 simonb PRWORD(ovflw, " %*u", 6, 1, pp->pr_hiwat);
1328 1.126 simonb PRWORD(ovflw, " %*u", 6, 1, pp->pr_minpages);
1329 1.69 enami PRWORD(ovflw, " %*s", 6, 1, maxp);
1330 1.126 simonb PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nidle);
1331 1.126 simonb if (wide)
1332 1.126 simonb PRWORD(ovflw, " 0x%0*x", 4, 1,
1333 1.126 simonb pp->pr_flags | pp->pr_roflags);
1334 1.51 pk
1335 1.126 simonb this_inuse = pp->pr_nout * pp->pr_size;
1336 1.126 simonb this_total = pp->pr_npages * pa.pa_pagesz;
1337 1.84 bjh21 if (pp->pr_roflags & PR_RECURSIVE) {
1338 1.84 bjh21 /*
1339 1.84 bjh21 * Don't count in-use memory, since it's part
1340 1.84 bjh21 * of another pool and will be accounted for
1341 1.84 bjh21 * there.
1342 1.84 bjh21 */
1343 1.126 simonb total += (this_total - this_inuse);
1344 1.84 bjh21 } else {
1345 1.126 simonb inuse += this_inuse;
1346 1.126 simonb total += this_total;
1347 1.84 bjh21 }
1348 1.126 simonb if (wide) {
1349 1.126 simonb if (this_total == 0)
1350 1.153 christos (void)printf(" ---");
1351 1.126 simonb else
1352 1.153 christos (void)printf(" %5.1f%%",
1353 1.126 simonb (100.0 * this_inuse) / this_total);
1354 1.126 simonb }
1355 1.153 christos (void)printf("\n");
1356 1.51 pk }
1357 1.51 pk
1358 1.152 christos inuse /= KILO;
1359 1.152 christos total /= KILO;
1360 1.153 christos (void)printf(
1361 1.153 christos "\nIn use %ldK, total allocated %ldK; utilization %.1f%%\n",
1362 1.126 simonb inuse, total, (100.0 * inuse) / total);
1363 1.1 cgd }
1364 1.96 enami
1365 1.96 enami void
1366 1.154 ad dopoolcache(void)
1367 1.96 enami {
1368 1.96 enami struct pool_cache pool_cache, *pc = &pool_cache;
1369 1.154 ad pool_cache_cpu_t cache_cpu, *cc = &cache_cpu;
1370 1.157 ad TAILQ_HEAD(,pool) pool_head;
1371 1.154 ad struct pool pool, *pp = &pool;
1372 1.154 ad char name[32];
1373 1.154 ad uint64_t cpuhit, cpumiss, tot;
1374 1.154 ad void *addr;
1375 1.165 lukem int first, ovflw;
1376 1.165 lukem size_t i;
1377 1.154 ad double p;
1378 1.96 enami
1379 1.154 ad kread(namelist, X_POOLHEAD, &pool_head, sizeof(pool_head));
1380 1.157 ad addr = TAILQ_FIRST(&pool_head);
1381 1.96 enami
1382 1.157 ad for (first = 1; addr != NULL; addr = TAILQ_NEXT(pp, pr_poollist) ) {
1383 1.154 ad deref_kptr(addr, pp, sizeof(*pp), "pool chain trashed");
1384 1.154 ad if (pp->pr_cache == NULL)
1385 1.96 enami continue;
1386 1.154 ad deref_kptr(pp->pr_wchan, name, sizeof(name),
1387 1.154 ad "pool wait channel trashed");
1388 1.154 ad deref_kptr(pp->pr_cache, pc, sizeof(*pc), "pool cache trashed");
1389 1.154 ad name[sizeof(name)-1] = '\0';
1390 1.154 ad
1391 1.154 ad cpuhit = 0;
1392 1.154 ad cpumiss = 0;
1393 1.154 ad for (i = 0; i < sizeof(pc->pc_cpus) / sizeof(pc->pc_cpus[0]);
1394 1.154 ad i++) {
1395 1.154 ad if ((addr = pc->pc_cpus[i]) == NULL)
1396 1.154 ad continue;
1397 1.154 ad deref_kptr(addr, cc, sizeof(*cc),
1398 1.154 ad "pool cache cpu trashed");
1399 1.154 ad cpuhit += cc->cc_hits;
1400 1.154 ad cpumiss += cc->cc_misses;
1401 1.137 chs }
1402 1.154 ad
1403 1.154 ad if (first) {
1404 1.154 ad (void)printf("Pool cache statistics.\n");
1405 1.156 ad (void)printf("%-*s%*s%*s%*s%*s%*s%*s%*s%*s%*s\n",
1406 1.154 ad 12, "Name",
1407 1.154 ad 6, "Spin",
1408 1.156 ad 6, "GrpSz",
1409 1.156 ad 5, "Full",
1410 1.156 ad 5, "Emty",
1411 1.156 ad 10, "PoolLayer",
1412 1.156 ad 11, "CacheLayer",
1413 1.154 ad 6, "Hit%",
1414 1.154 ad 12, "CpuLayer",
1415 1.154 ad 6, "Hit%"
1416 1.154 ad );
1417 1.154 ad first = 0;
1418 1.96 enami }
1419 1.154 ad
1420 1.154 ad ovflw = 0;
1421 1.154 ad PRWORD(ovflw, "%-*s", 13, 1, name);
1422 1.154 ad PRWORD(ovflw, " %*llu", 6, 1, (long long)pc->pc_contended);
1423 1.156 ad PRWORD(ovflw, " %*u", 6, 1, pc->pc_pcgsize);
1424 1.156 ad PRWORD(ovflw, " %*u", 5, 1, pc->pc_nfull);
1425 1.156 ad PRWORD(ovflw, " %*u", 5, 1, pc->pc_nempty);
1426 1.156 ad PRWORD(ovflw, " %*llu", 10, 1, (long long)pc->pc_misses);
1427 1.154 ad
1428 1.154 ad tot = pc->pc_hits + pc->pc_misses;
1429 1.154 ad p = pc->pc_hits * 100.0 / (tot);
1430 1.156 ad PRWORD(ovflw, " %*llu", 11, 1, (long long)tot);
1431 1.154 ad PRWORD(ovflw, " %*.1f", 6, 1, p);
1432 1.154 ad
1433 1.154 ad tot = cpuhit + cpumiss;
1434 1.154 ad p = cpuhit * 100.0 / (tot);
1435 1.154 ad PRWORD(ovflw, " %*llu", 12, 1, (long long)tot);
1436 1.154 ad PRWORD(ovflw, " %*.1f", 6, 1, p);
1437 1.154 ad printf("\n");
1438 1.96 enami }
1439 1.96 enami }
1440 1.90 lukem
1441 1.87 lukem enum hashtype { /* from <sys/systm.h> */
1442 1.87 lukem HASH_LIST,
1443 1.87 lukem HASH_TAILQ
1444 1.87 lukem };
1445 1.87 lukem
1446 1.87 lukem struct uidinfo { /* XXX: no kernel header file */
1447 1.87 lukem LIST_ENTRY(uidinfo) ui_hash;
1448 1.87 lukem uid_t ui_uid;
1449 1.87 lukem long ui_proccnt;
1450 1.87 lukem };
1451 1.87 lukem
1452 1.87 lukem struct kernel_hash {
1453 1.90 lukem const char * description; /* description */
1454 1.90 lukem int hashsize; /* nlist index for hash size */
1455 1.90 lukem int hashtbl; /* nlist index for hash table */
1456 1.90 lukem enum hashtype type; /* type of hash table */
1457 1.101 sommerfe size_t offset; /* offset of {LIST,TAILQ}_NEXT */
1458 1.87 lukem } khashes[] =
1459 1.87 lukem {
1460 1.87 lukem {
1461 1.90 lukem "buffer hash",
1462 1.90 lukem X_BUFHASH, X_BUFHASHTBL,
1463 1.90 lukem HASH_LIST, offsetof(struct buf, b_hash)
1464 1.90 lukem }, {
1465 1.90 lukem "inode cache (ihash)",
1466 1.87 lukem X_IHASH, X_IHASHTBL,
1467 1.87 lukem HASH_LIST, offsetof(struct inode, i_hash)
1468 1.90 lukem }, {
1469 1.90 lukem "ipv4 address -> interface hash",
1470 1.87 lukem X_IFADDRHASH, X_IFADDRHASHTBL,
1471 1.87 lukem HASH_LIST, offsetof(struct in_ifaddr, ia_hash),
1472 1.90 lukem }, {
1473 1.90 lukem "name cache hash",
1474 1.89 lukem X_NCHASH, X_NCHASHTBL,
1475 1.89 lukem HASH_LIST, offsetof(struct namecache, nc_hash),
1476 1.90 lukem }, {
1477 1.90 lukem "name cache directory hash",
1478 1.89 lukem X_NCVHASH, X_NCVHASHTBL,
1479 1.89 lukem HASH_LIST, offsetof(struct namecache, nc_vhash),
1480 1.90 lukem }, {
1481 1.90 lukem "user info (uid -> used processes) hash",
1482 1.90 lukem X_UIHASH, X_UIHASHTBL,
1483 1.90 lukem HASH_LIST, offsetof(struct uidinfo, ui_hash),
1484 1.90 lukem }, {
1485 1.90 lukem NULL, -1, -1, 0, 0,
1486 1.87 lukem }
1487 1.87 lukem };
1488 1.87 lukem
1489 1.87 lukem void
1490 1.88 lukem dohashstat(int verbose, int todo, const char *hashname)
1491 1.101 sommerfe {
1492 1.87 lukem LIST_HEAD(, generic) *hashtbl_list;
1493 1.87 lukem TAILQ_HEAD(, generic) *hashtbl_tailq;
1494 1.87 lukem struct kernel_hash *curhash;
1495 1.118 itojun void *hashaddr, *hashbuf, *nhashbuf, *nextaddr;
1496 1.87 lukem size_t elemsize, hashbufsize, thissize;
1497 1.165 lukem u_long hashsize, i;
1498 1.165 lukem int used, items, chain, maxchain;
1499 1.87 lukem
1500 1.87 lukem hashbuf = NULL;
1501 1.87 lukem hashbufsize = 0;
1502 1.88 lukem
1503 1.88 lukem if (todo & HASHLIST) {
1504 1.153 christos (void)printf("Supported hashes:\n");
1505 1.90 lukem for (curhash = khashes; curhash->description; curhash++) {
1506 1.101 sommerfe if (hashnl[curhash->hashsize].n_value == 0 ||
1507 1.90 lukem hashnl[curhash->hashtbl].n_value == 0)
1508 1.90 lukem continue;
1509 1.153 christos (void)printf("\t%-16s%s\n",
1510 1.90 lukem hashnl[curhash->hashsize].n_name + 1,
1511 1.90 lukem curhash->description);
1512 1.88 lukem }
1513 1.88 lukem return;
1514 1.88 lukem }
1515 1.88 lukem
1516 1.88 lukem if (hashname != NULL) {
1517 1.90 lukem for (curhash = khashes; curhash->description; curhash++) {
1518 1.90 lukem if (strcmp(hashnl[curhash->hashsize].n_name + 1,
1519 1.90 lukem hashname) == 0 &&
1520 1.101 sommerfe hashnl[curhash->hashsize].n_value != 0 &&
1521 1.90 lukem hashnl[curhash->hashtbl].n_value != 0)
1522 1.88 lukem break;
1523 1.88 lukem }
1524 1.90 lukem if (curhash->description == NULL) {
1525 1.90 lukem warnx("%s: no such hash", hashname);
1526 1.90 lukem return;
1527 1.90 lukem }
1528 1.88 lukem }
1529 1.88 lukem
1530 1.153 christos (void)printf(
1531 1.88 lukem "%-16s %8s %8s %8s %8s %8s %8s\n"
1532 1.88 lukem "%-16s %8s %8s %8s %8s %8s %8s\n",
1533 1.88 lukem "", "total", "used", "util", "num", "average", "maximum",
1534 1.88 lukem "hash table", "buckets", "buckets", "%", "items", "chain",
1535 1.88 lukem "chain");
1536 1.87 lukem
1537 1.90 lukem for (curhash = khashes; curhash->description; curhash++) {
1538 1.101 sommerfe if (hashnl[curhash->hashsize].n_value == 0 ||
1539 1.90 lukem hashnl[curhash->hashtbl].n_value == 0)
1540 1.90 lukem continue;
1541 1.88 lukem if (hashname != NULL &&
1542 1.90 lukem strcmp(hashnl[curhash->hashsize].n_name + 1, hashname))
1543 1.88 lukem continue;
1544 1.87 lukem elemsize = curhash->type == HASH_LIST ?
1545 1.87 lukem sizeof(*hashtbl_list) : sizeof(*hashtbl_tailq);
1546 1.90 lukem deref_kptr((void *)hashnl[curhash->hashsize].n_value,
1547 1.90 lukem &hashsize, sizeof(hashsize),
1548 1.90 lukem hashnl[curhash->hashsize].n_name);
1549 1.87 lukem hashsize++;
1550 1.90 lukem deref_kptr((void *)hashnl[curhash->hashtbl].n_value,
1551 1.90 lukem &hashaddr, sizeof(hashaddr),
1552 1.90 lukem hashnl[curhash->hashtbl].n_name);
1553 1.87 lukem if (verbose)
1554 1.153 christos (void)printf(
1555 1.153 christos "%s %lu, %s %p, offset %ld, elemsize %llu\n",
1556 1.90 lukem hashnl[curhash->hashsize].n_name + 1, hashsize,
1557 1.90 lukem hashnl[curhash->hashtbl].n_name + 1, hashaddr,
1558 1.101 sommerfe (long)curhash->offset,
1559 1.91 jmc (unsigned long long)elemsize);
1560 1.87 lukem thissize = hashsize * elemsize;
1561 1.144 christos if (hashbuf == NULL || thissize > hashbufsize) {
1562 1.118 itojun if ((nhashbuf = realloc(hashbuf, thissize)) == NULL)
1563 1.101 sommerfe errx(1, "malloc hashbuf %llu",
1564 1.91 jmc (unsigned long long)hashbufsize);
1565 1.118 itojun hashbuf = nhashbuf;
1566 1.118 itojun hashbufsize = thissize;
1567 1.87 lukem }
1568 1.87 lukem deref_kptr(hashaddr, hashbuf, thissize,
1569 1.90 lukem hashnl[curhash->hashtbl].n_name);
1570 1.87 lukem used = 0;
1571 1.87 lukem items = maxchain = 0;
1572 1.135 lukem if (curhash->type == HASH_LIST) {
1573 1.87 lukem hashtbl_list = hashbuf;
1574 1.135 lukem hashtbl_tailq = NULL;
1575 1.135 lukem } else {
1576 1.135 lukem hashtbl_list = NULL;
1577 1.87 lukem hashtbl_tailq = hashbuf;
1578 1.135 lukem }
1579 1.87 lukem for (i = 0; i < hashsize; i++) {
1580 1.87 lukem if (curhash->type == HASH_LIST)
1581 1.87 lukem nextaddr = LIST_FIRST(&hashtbl_list[i]);
1582 1.87 lukem else
1583 1.87 lukem nextaddr = TAILQ_FIRST(&hashtbl_tailq[i]);
1584 1.87 lukem if (nextaddr == NULL)
1585 1.87 lukem continue;
1586 1.87 lukem if (verbose)
1587 1.165 lukem (void)printf("%5lu: %p\n", i, nextaddr);
1588 1.87 lukem used++;
1589 1.87 lukem chain = 0;
1590 1.87 lukem do {
1591 1.87 lukem chain++;
1592 1.87 lukem deref_kptr((char *)nextaddr + curhash->offset,
1593 1.87 lukem &nextaddr, sizeof(void *),
1594 1.87 lukem "hash chain corrupted");
1595 1.87 lukem if (verbose > 1)
1596 1.153 christos (void)printf("got nextaddr as %p\n",
1597 1.87 lukem nextaddr);
1598 1.87 lukem } while (nextaddr != NULL);
1599 1.87 lukem items += chain;
1600 1.87 lukem if (verbose && chain > 1)
1601 1.153 christos (void)printf("\tchain = %d\n", chain);
1602 1.87 lukem if (chain > maxchain)
1603 1.87 lukem maxchain = chain;
1604 1.87 lukem }
1605 1.153 christos (void)printf("%-16s %8ld %8d %8.2f %8d %8.2f %8d\n",
1606 1.90 lukem hashnl[curhash->hashsize].n_name + 1,
1607 1.87 lukem hashsize, used, used * 100.0 / hashsize,
1608 1.93 lukem items, used ? (double)items / used : 0.0, maxchain);
1609 1.87 lukem }
1610 1.87 lukem }
1611 1.87 lukem
1612 1.1 cgd /*
1613 1.147 kardel * kreadc like kread but returns 1 if sucessful, 0 otherwise
1614 1.147 kardel */
1615 1.147 kardel int
1616 1.147 kardel kreadc(struct nlist *nl, int nlx, void *addr, size_t size)
1617 1.147 kardel {
1618 1.147 kardel const char *sym;
1619 1.147 kardel
1620 1.147 kardel sym = nl[nlx].n_name;
1621 1.147 kardel if (*sym == '_')
1622 1.147 kardel ++sym;
1623 1.147 kardel if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0)
1624 1.147 kardel return 0;
1625 1.147 kardel deref_kptr((void *)nl[nlx].n_value, addr, size, sym);
1626 1.147 kardel return 1;
1627 1.147 kardel }
1628 1.147 kardel
1629 1.147 kardel /*
1630 1.90 lukem * kread reads something from the kernel, given its nlist index in namelist[].
1631 1.1 cgd */
1632 1.1 cgd void
1633 1.133 chs kread(struct nlist *nl, int nlx, void *addr, size_t size)
1634 1.1 cgd {
1635 1.50 mycroft const char *sym;
1636 1.1 cgd
1637 1.133 chs sym = nl[nlx].n_name;
1638 1.87 lukem if (*sym == '_')
1639 1.87 lukem ++sym;
1640 1.133 chs if (nl[nlx].n_type == 0 || nl[nlx].n_value == 0)
1641 1.87 lukem errx(1, "symbol %s not defined", sym);
1642 1.133 chs deref_kptr((void *)nl[nlx].n_value, addr, size, sym);
1643 1.87 lukem }
1644 1.87 lukem
1645 1.87 lukem /*
1646 1.101 sommerfe * Dereference the kernel pointer `kptr' and fill in the local copy
1647 1.87 lukem * pointed to by `ptr'. The storage space must be pre-allocated,
1648 1.87 lukem * and the size of the copy passed in `len'.
1649 1.87 lukem */
1650 1.87 lukem void
1651 1.87 lukem deref_kptr(const void *kptr, void *ptr, size_t len, const char *msg)
1652 1.87 lukem {
1653 1.87 lukem
1654 1.87 lukem if (*msg == '_')
1655 1.87 lukem msg++;
1656 1.165 lukem if ((size_t)kvm_read(kd, (u_long)kptr, (char *)ptr, len) != len)
1657 1.87 lukem errx(1, "kptr %lx: %s: %s", (u_long)kptr, msg, kvm_geterr(kd));
1658 1.1 cgd }
1659 1.1 cgd
1660 1.45 thorpej /*
1661 1.45 thorpej * Traverse the UVM history buffers, performing the requested action.
1662 1.45 thorpej *
1663 1.45 thorpej * Note, we assume that if we're not listing, we're dumping.
1664 1.45 thorpej */
1665 1.45 thorpej void
1666 1.73 simonb hist_traverse(int todo, const char *histname)
1667 1.45 thorpej {
1668 1.45 thorpej struct uvm_history_head histhead;
1669 1.45 thorpej struct uvm_history hist, *histkva;
1670 1.45 thorpej char *name = NULL;
1671 1.45 thorpej size_t namelen = 0;
1672 1.45 thorpej
1673 1.90 lukem if (histnl[0].n_value == 0) {
1674 1.87 lukem warnx("UVM history is not compiled into the kernel.");
1675 1.45 thorpej return;
1676 1.45 thorpej }
1677 1.45 thorpej
1678 1.87 lukem deref_kptr((void *)histnl[X_UVM_HISTORIES].n_value, &histhead,
1679 1.87 lukem sizeof(histhead), histnl[X_UVM_HISTORIES].n_name);
1680 1.45 thorpej
1681 1.45 thorpej if (histhead.lh_first == NULL) {
1682 1.87 lukem warnx("No active UVM history logs.");
1683 1.45 thorpej return;
1684 1.45 thorpej }
1685 1.45 thorpej
1686 1.45 thorpej if (todo & HISTLIST)
1687 1.153 christos (void)printf("Active UVM histories:");
1688 1.45 thorpej
1689 1.75 enami for (histkva = LIST_FIRST(&histhead); histkva != NULL;
1690 1.75 enami histkva = LIST_NEXT(&hist, list)) {
1691 1.87 lukem deref_kptr(histkva, &hist, sizeof(hist), "histkva");
1692 1.142 christos if (name == NULL || hist.namelen > namelen) {
1693 1.45 thorpej if (name != NULL)
1694 1.45 thorpej free(name);
1695 1.45 thorpej namelen = hist.namelen;
1696 1.45 thorpej if ((name = malloc(namelen + 1)) == NULL)
1697 1.45 thorpej err(1, "malloc history name");
1698 1.45 thorpej }
1699 1.45 thorpej
1700 1.87 lukem deref_kptr(hist.name, name, namelen, "history name");
1701 1.45 thorpej name[namelen] = '\0';
1702 1.45 thorpej if (todo & HISTLIST)
1703 1.153 christos (void)printf(" %s", name);
1704 1.45 thorpej else {
1705 1.45 thorpej /*
1706 1.45 thorpej * If we're dumping all histories, do it, else
1707 1.45 thorpej * check to see if this is the one we want.
1708 1.45 thorpej */
1709 1.45 thorpej if (histname == NULL || strcmp(histname, name) == 0) {
1710 1.45 thorpej if (histname == NULL)
1711 1.153 christos (void)printf(
1712 1.153 christos "\nUVM history `%s':\n", name);
1713 1.45 thorpej hist_dodump(&hist);
1714 1.45 thorpej }
1715 1.45 thorpej }
1716 1.45 thorpej }
1717 1.45 thorpej
1718 1.45 thorpej if (todo & HISTLIST)
1719 1.153 christos (void)putchar('\n');
1720 1.45 thorpej
1721 1.45 thorpej if (name != NULL)
1722 1.45 thorpej free(name);
1723 1.45 thorpej }
1724 1.45 thorpej
1725 1.45 thorpej /*
1726 1.45 thorpej * Actually dump the history buffer at the specified KVA.
1727 1.45 thorpej */
1728 1.45 thorpej void
1729 1.73 simonb hist_dodump(struct uvm_history *histp)
1730 1.45 thorpej {
1731 1.45 thorpej struct uvm_history_ent *histents, *e;
1732 1.45 thorpej size_t histsize;
1733 1.45 thorpej char *fmt = NULL, *fn = NULL;
1734 1.45 thorpej size_t fmtlen = 0, fnlen = 0;
1735 1.169 pooka unsigned i;
1736 1.45 thorpej
1737 1.45 thorpej histsize = sizeof(struct uvm_history_ent) * histp->n;
1738 1.45 thorpej
1739 1.45 thorpej if ((histents = malloc(histsize)) == NULL)
1740 1.45 thorpej err(1, "malloc history entries");
1741 1.45 thorpej
1742 1.153 christos (void)memset(histents, 0, histsize);
1743 1.45 thorpej
1744 1.87 lukem deref_kptr(histp->e, histents, histsize, "history entries");
1745 1.45 thorpej i = histp->f;
1746 1.45 thorpej do {
1747 1.45 thorpej e = &histents[i];
1748 1.45 thorpej if (e->fmt != NULL) {
1749 1.143 christos if (fmt == NULL || e->fmtlen > fmtlen) {
1750 1.45 thorpej if (fmt != NULL)
1751 1.45 thorpej free(fmt);
1752 1.45 thorpej fmtlen = e->fmtlen;
1753 1.45 thorpej if ((fmt = malloc(fmtlen + 1)) == NULL)
1754 1.45 thorpej err(1, "malloc printf format");
1755 1.45 thorpej }
1756 1.143 christos if (fn == NULL || e->fnlen > fnlen) {
1757 1.45 thorpej if (fn != NULL)
1758 1.45 thorpej free(fn);
1759 1.45 thorpej fnlen = e->fnlen;
1760 1.45 thorpej if ((fn = malloc(fnlen + 1)) == NULL)
1761 1.45 thorpej err(1, "malloc function name");
1762 1.45 thorpej }
1763 1.45 thorpej
1764 1.87 lukem deref_kptr(e->fmt, fmt, fmtlen, "printf format");
1765 1.45 thorpej fmt[fmtlen] = '\0';
1766 1.45 thorpej
1767 1.87 lukem deref_kptr(e->fn, fn, fnlen, "function name");
1768 1.45 thorpej fn[fnlen] = '\0';
1769 1.45 thorpej
1770 1.153 christos (void)printf("%06ld.%06ld ", (long int)e->tv.tv_sec,
1771 1.61 kleink (long int)e->tv.tv_usec);
1772 1.153 christos (void)printf("%s#%ld: ", fn, e->call);
1773 1.153 christos (void)printf(fmt, e->v[0], e->v[1], e->v[2], e->v[3]);
1774 1.153 christos (void)putchar('\n');
1775 1.45 thorpej }
1776 1.45 thorpej i = (i + 1) % histp->n;
1777 1.45 thorpej } while (i != histp->f);
1778 1.45 thorpej
1779 1.45 thorpej free(histents);
1780 1.45 thorpej if (fmt != NULL)
1781 1.45 thorpej free(fmt);
1782 1.45 thorpej if (fn != NULL)
1783 1.45 thorpej free(fn);
1784 1.45 thorpej }
1785 1.45 thorpej
1786 1.1 cgd void
1787 1.73 simonb usage(void)
1788 1.1 cgd {
1789 1.47 mrg
1790 1.1 cgd (void)fprintf(stderr,
1791 1.155 yamt "usage: %s [-CefHiLlmstUvW] [-c count] [-h hashname] [-M core] [-N system]\n"
1792 1.127 wiz "\t\t[-u histname] [-w wait] [disks]\n", getprogname());
1793 1.1 cgd exit(1);
1794 1.1 cgd }
1795