Print this page
10139 smatch fixes for usr/src/cmd/stat
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/stat/vmstat/vmstat.c
+++ new/usr/src/cmd/stat/vmstat/vmstat.c
1 1 /*
2 2 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
3 3 */
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
4 4
5 5 /*
6 6 * Copyright (c) 1980 Regents of the University of California.
7 7 * All rights reserved. The Berkeley software License Agreement
8 8 * specifies the terms and conditions for redistribution.
9 9 */
10 10
11 11 /* from UCB 5.4 5/17/86 */
12 12 /* from SunOS 4.1, SID 1.31 */
13 13
14 +/*
15 + * Copyright (c) 2018, Joyent, Inc.
16 + */
17 +
14 18 #include <stdio.h>
15 19 #include <stdlib.h>
16 20 #include <stdarg.h>
17 21 #include <ctype.h>
18 22 #include <unistd.h>
19 23 #include <memory.h>
20 24 #include <string.h>
21 25 #include <fcntl.h>
22 26 #include <errno.h>
23 27 #include <signal.h>
24 28 #include <values.h>
25 29 #include <poll.h>
26 30 #include <locale.h>
27 31
28 32 #include "statcommon.h"
29 33
30 34 char *cmdname = "vmstat";
31 35 int caught_cont = 0;
32 36
33 37 static uint_t timestamp_fmt = NODATE;
34 38
35 39 static int hz;
36 40 static int pagesize;
37 41 static double etime;
38 42 static int lines = 1;
39 43 static int swflag = 0, pflag = 0;
40 44 static int suppress_state;
41 45 static long iter = 0;
42 46 static hrtime_t period_n = 0;
43 47 static struct snapshot *ss;
44 48
45 49 struct iodev_filter df;
46 50
47 51 #define pgtok(a) ((a) * (pagesize >> 10))
48 52 #define denom(x) ((x) ? (x) : 1)
49 53 #define REPRINT 19
50 54
51 55 static void dovmstats(struct snapshot *old, struct snapshot *new);
52 56 static void printhdr(int);
53 57 static void dosum(struct sys_snapshot *ss);
54 58 static void dointr(struct snapshot *ss);
55 59 static void usage(void);
56 60
57 61 int
58 62 main(int argc, char **argv)
59 63 {
60 64 struct snapshot *old = NULL;
61 65 enum snapshot_types types = SNAP_SYSTEM;
62 66 int summary = 0;
63 67 int intr = 0;
64 68 kstat_ctl_t *kc;
65 69 int forever = 0;
66 70 hrtime_t start_n;
67 71 int c;
68 72
69 73 (void) setlocale(LC_ALL, "");
70 74 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
71 75 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
72 76 #endif
73 77 (void) textdomain(TEXT_DOMAIN);
74 78
75 79 pagesize = sysconf(_SC_PAGESIZE);
76 80 hz = sysconf(_SC_CLK_TCK);
77 81
78 82 while ((c = getopt(argc, argv, "ipqsST:")) != EOF)
79 83 switch (c) {
80 84 case 'S':
81 85 swflag = !swflag;
82 86 break;
83 87 case 's':
84 88 summary = 1;
85 89 break;
86 90 case 'i':
87 91 intr = 1;
88 92 break;
89 93 case 'q':
90 94 suppress_state = 1;
91 95 break;
92 96 case 'p':
93 97 pflag++; /* detailed paging info */
94 98 break;
95 99 case 'T':
96 100 if (optarg) {
97 101 if (*optarg == 'u')
98 102 timestamp_fmt = UDATE;
99 103 else if (*optarg == 'd')
100 104 timestamp_fmt = DDATE;
101 105 else
102 106 usage();
103 107 } else {
104 108 usage();
105 109 }
106 110 break;
107 111 default:
108 112 usage();
109 113 }
110 114
111 115 argc -= optind;
112 116 argv += optind;
113 117
114 118 /* consistency with iostat */
115 119 types |= SNAP_CPUS;
116 120
117 121 if (intr)
118 122 types |= SNAP_INTERRUPTS;
119 123 if (!intr)
120 124 types |= SNAP_IODEVS;
121 125
122 126 /* max to fit in less than 80 characters */
123 127 df.if_max_iodevs = 4;
124 128 df.if_allowed_types = IODEV_DISK;
125 129 df.if_nr_names = 0;
126 130 df.if_names = safe_alloc(df.if_max_iodevs * sizeof (char *));
127 131 (void) memset(df.if_names, 0, df.if_max_iodevs * sizeof (char *));
128 132
129 133 while (argc > 0 && !isdigit(argv[0][0]) &&
130 134 df.if_nr_names < df.if_max_iodevs) {
131 135 df.if_names[df.if_nr_names] = *argv;
132 136 df.if_nr_names++;
133 137 argc--, argv++;
134 138 }
135 139
136 140 kc = open_kstat();
137 141
138 142 start_n = gethrtime();
139 143
140 144 ss = acquire_snapshot(kc, types, &df);
141 145
142 146 /* time, in seconds, since boot */
143 147 etime = ss->s_sys.ss_ticks / hz;
144 148
145 149 if (intr) {
146 150 dointr(ss);
147 151 free_snapshot(ss);
148 152 exit(0);
149 153 }
150 154 if (summary) {
151 155 dosum(&ss->s_sys);
152 156 free_snapshot(ss);
153 157 exit(0);
154 158 }
155 159
156 160 if (argc > 0) {
157 161 long interval;
158 162 char *endptr;
159 163
160 164 errno = 0;
161 165 interval = strtol(argv[0], &endptr, 10);
162 166
163 167 if (errno > 0 || *endptr != '\0' || interval <= 0 ||
164 168 interval > MAXINT)
165 169 usage();
166 170 period_n = (hrtime_t)interval * NANOSEC;
167 171 if (period_n <= 0)
168 172 usage();
169 173 iter = MAXLONG;
170 174 if (argc > 1) {
171 175 iter = strtol(argv[1], NULL, 10);
172 176 if (errno > 0 || *endptr != '\0' || iter <= 0)
173 177 usage();
174 178 } else
175 179 forever = 1;
176 180 if (argc > 2)
177 181 usage();
178 182 }
179 183
180 184 (void) sigset(SIGCONT, printhdr);
181 185
182 186 dovmstats(old, ss);
183 187 while (forever || --iter > 0) {
184 188 /* (void) poll(NULL, 0, poll_interval); */
185 189
186 190 /* Have a kip */
187 191 sleep_until(&start_n, period_n, forever, &caught_cont);
188 192
189 193 free_snapshot(old);
190 194 old = ss;
191 195 ss = acquire_snapshot(kc, types, &df);
192 196
193 197 if (!suppress_state)
194 198 snapshot_report_changes(old, ss);
195 199
196 200 /* if config changed, show stats from boot */
197 201 if (snapshot_has_changed(old, ss)) {
198 202 free_snapshot(old);
199 203 old = NULL;
200 204 }
201 205
202 206 dovmstats(old, ss);
203 207 }
204 208
205 209 free_snapshot(old);
206 210 free_snapshot(ss);
207 211 free(df.if_names);
208 212 (void) kstat_close(kc);
209 213 return (0);
210 214 }
211 215
212 216 #define DELTA(v) (new->v - (old ? old->v : 0))
213 217 #define ADJ(n) ((adj <= 0) ? n : (adj >= n) ? 1 : n - adj)
214 218 #define adjprintf(fmt, n, val) adj -= (n + 1) - printf(fmt, ADJ(n), val)
215 219
216 220 static int adj; /* number of excess columns */
217 221
↓ open down ↓ |
194 lines elided |
↑ open up ↑ |
218 222 /*ARGSUSED*/
219 223 static void
220 224 show_disk(void *v1, void *v2, void *d)
221 225 {
222 226 struct iodev_snapshot *old = (struct iodev_snapshot *)v1;
223 227 struct iodev_snapshot *new = (struct iodev_snapshot *)v2;
224 228 hrtime_t oldtime = new->is_crtime;
225 229 double hr_etime;
226 230 double reads, writes;
227 231
228 - if (new == NULL)
229 - return;
230 -
231 232 if (old)
232 233 oldtime = old->is_stats.wlastupdate;
233 234 hr_etime = new->is_stats.wlastupdate - oldtime;
234 235 if (hr_etime == 0.0)
235 236 hr_etime = NANOSEC;
236 237 reads = new->is_stats.reads - (old ? old->is_stats.reads : 0);
237 238 writes = new->is_stats.writes - (old ? old->is_stats.writes : 0);
238 239 adjprintf(" %*.0f", 2, (reads + writes) / hr_etime * NANOSEC);
239 240 }
240 241
241 242 static void
242 243 dovmstats(struct snapshot *old, struct snapshot *new)
243 244 {
244 245 kstat_t *oldsys = NULL;
245 246 kstat_t *newsys = &new->s_sys.ss_agg_sys;
246 247 kstat_t *oldvm = NULL;
247 248 kstat_t *newvm = &new->s_sys.ss_agg_vm;
248 249 double percent_factor;
249 250 ulong_t sys_updates, vm_updates;
250 251 int count;
251 252
252 253 adj = 0;
253 254
254 255 if (old) {
255 256 oldsys = &old->s_sys.ss_agg_sys;
256 257 oldvm = &old->s_sys.ss_agg_vm;
257 258 }
258 259
259 260 etime = cpu_ticks_delta(oldsys, newsys);
260 261
261 262 percent_factor = 100.0 / denom(etime);
262 263 /*
263 264 * If any time has passed, convert etime to seconds per CPU
264 265 */
265 266 etime = etime >= 1.0 ? (etime / nr_active_cpus(new)) / hz : 1.0;
266 267 sys_updates = denom(DELTA(s_sys.ss_sysinfo.updates));
267 268 vm_updates = denom(DELTA(s_sys.ss_vminfo.updates));
268 269
269 270 if (timestamp_fmt != NODATE) {
270 271 print_timestamp(timestamp_fmt);
271 272 lines--;
272 273 }
273 274
274 275 if (--lines <= 0)
275 276 printhdr(0);
276 277
277 278 adj = 0;
278 279
279 280 if (pflag) {
280 281 adjprintf(" %*u", 6,
281 282 pgtok((int)(DELTA(s_sys.ss_vminfo.swap_avail)
282 283 / vm_updates)));
283 284 adjprintf(" %*u", 5,
284 285 pgtok((int)(DELTA(s_sys.ss_vminfo.freemem) / vm_updates)));
285 286 adjprintf(" %*.0f", 3, kstat_delta(oldvm, newvm, "pgrec")
286 287 / etime);
287 288 adjprintf(" %*.0f", 3, (kstat_delta(oldvm, newvm, "hat_fault") +
288 289 kstat_delta(oldvm, newvm, "as_fault")) / etime);
289 290 adjprintf(" %*.0f", 3, pgtok(kstat_delta(oldvm, newvm, "dfree"))
290 291 / etime);
291 292 adjprintf(" %*ld", 3, pgtok(new->s_sys.ss_deficit));
292 293 adjprintf(" %*.0f", 3, kstat_delta(oldvm, newvm, "scan")
293 294 / etime);
294 295 adjprintf(" %*.0f", 4,
295 296 pgtok(kstat_delta(oldvm, newvm, "execpgin")) / etime);
296 297 adjprintf(" %*.0f", 4,
297 298 pgtok(kstat_delta(oldvm, newvm, "execpgout")) / etime);
298 299 adjprintf(" %*.0f", 4,
299 300 pgtok(kstat_delta(oldvm, newvm, "execfree")) / etime);
300 301 adjprintf(" %*.0f", 4,
301 302 pgtok(kstat_delta(oldvm, newvm, "anonpgin")) / etime);
302 303 adjprintf(" %*.0f", 4,
303 304 pgtok(kstat_delta(oldvm, newvm, "anonpgout")) / etime);
304 305 adjprintf(" %*.0f", 4,
305 306 pgtok(kstat_delta(oldvm, newvm, "anonfree")) / etime);
306 307 adjprintf(" %*.0f", 4,
307 308 pgtok(kstat_delta(oldvm, newvm, "fspgin")) / etime);
308 309 adjprintf(" %*.0f", 4,
309 310 pgtok(kstat_delta(oldvm, newvm, "fspgout")) / etime);
310 311 adjprintf(" %*.0f\n", 4,
311 312 pgtok(kstat_delta(oldvm, newvm, "fsfree")) / etime);
312 313 (void) fflush(stdout);
313 314 return;
314 315 }
315 316
316 317 adjprintf(" %*lu", 1, DELTA(s_sys.ss_sysinfo.runque) / sys_updates);
317 318 adjprintf(" %*lu", 1, DELTA(s_sys.ss_sysinfo.waiting) / sys_updates);
318 319 adjprintf(" %*lu", 1, DELTA(s_sys.ss_sysinfo.swpque) / sys_updates);
319 320 adjprintf(" %*u", 6, pgtok((int)(DELTA(s_sys.ss_vminfo.swap_avail)
320 321 / vm_updates)));
321 322 adjprintf(" %*u", 5, pgtok((int)(DELTA(s_sys.ss_vminfo.freemem)
322 323 / vm_updates)));
323 324 adjprintf(" %*.0f", 3, swflag?
324 325 kstat_delta(oldvm, newvm, "swapin") / etime :
325 326 kstat_delta(oldvm, newvm, "pgrec") / etime);
326 327 adjprintf(" %*.0f", 3, swflag?
327 328 kstat_delta(oldvm, newvm, "swapout") / etime :
328 329 (kstat_delta(oldvm, newvm, "hat_fault")
329 330 + kstat_delta(oldvm, newvm, "as_fault"))
330 331 / etime);
331 332 adjprintf(" %*.0f", 2, pgtok(kstat_delta(oldvm, newvm, "pgpgin"))
332 333 / etime);
333 334 adjprintf(" %*.0f", 2, pgtok(kstat_delta(oldvm, newvm, "pgpgout"))
334 335 / etime);
335 336 adjprintf(" %*.0f", 2, pgtok(kstat_delta(oldvm, newvm, "dfree"))
336 337 / etime);
337 338 adjprintf(" %*ld", 2, pgtok(new->s_sys.ss_deficit));
338 339 adjprintf(" %*.0f", 2, kstat_delta(oldvm, newvm, "scan") / etime);
339 340
340 341 (void) snapshot_walk(SNAP_IODEVS, old, new, show_disk, NULL);
341 342
342 343 count = df.if_max_iodevs - new->s_nr_iodevs;
343 344 while (count-- > 0)
344 345 adjprintf(" %*d", 2, 0);
345 346
346 347 adjprintf(" %*.0f", 4, kstat_delta(oldsys, newsys, "intr") / etime);
347 348 adjprintf(" %*.0f", 4, kstat_delta(oldsys, newsys, "syscall") / etime);
348 349 adjprintf(" %*.0f", 4, kstat_delta(oldsys, newsys, "pswitch") / etime);
349 350 adjprintf(" %*.0f", 2,
350 351 kstat_delta(oldsys, newsys, "cpu_ticks_user") * percent_factor);
351 352 adjprintf(" %*.0f", 2, kstat_delta(oldsys, newsys, "cpu_ticks_kernel")
352 353 * percent_factor);
353 354 adjprintf(" %*.0f\n", 2, (kstat_delta(oldsys, newsys, "cpu_ticks_idle")
354 355 + kstat_delta(oldsys, newsys, "cpu_ticks_wait"))
355 356 * percent_factor);
356 357 (void) fflush(stdout);
357 358 }
358 359
359 360 /*ARGSUSED*/
360 361 static void
361 362 print_disk(void *v, void *v2, void *d)
362 363 {
363 364 struct iodev_snapshot *iodev = (struct iodev_snapshot *)v2;
364 365
365 366 if (iodev == NULL)
366 367 return;
367 368
368 369 (void) printf("%c%c ", iodev->is_name[0], iodev->is_name[2]);
369 370 }
370 371
371 372 /* ARGSUSED */
372 373 static void
373 374 printhdr(int sig)
374 375 {
375 376 int i = df.if_max_iodevs - ss->s_nr_iodevs;
376 377
377 378 if (sig == SIGCONT)
378 379 caught_cont = 1;
379 380
380 381 if (pflag) {
381 382 (void) printf(" memory page ");
382 383 (void) printf("executable anonymous filesystem \n");
383 384 (void) printf(" swap free re mf fr de sr ");
384 385 (void) printf("epi epo epf api apo apf fpi fpo fpf\n");
385 386 lines = REPRINT;
386 387 return;
387 388 }
388 389
389 390 (void) printf(" kthr memory page ");
390 391 (void) printf("disk faults cpu\n");
391 392
392 393 if (swflag)
393 394 (void) printf(" r b w swap free si so pi po fr de sr ");
394 395 else
395 396 (void) printf(" r b w swap free re mf pi po fr de sr ");
396 397
397 398 (void) snapshot_walk(SNAP_IODEVS, NULL, ss, print_disk, NULL);
398 399
399 400 while (i-- > 0)
400 401 (void) printf("-- ");
401 402
402 403 (void) printf(" in sy cs us sy id\n");
403 404 lines = REPRINT;
404 405 }
405 406
406 407 static void
407 408 sum_out(char const *pretty, kstat_t *ks, char *name)
408 409 {
409 410 kstat_named_t *ksn = kstat_data_lookup(ks, name);
410 411 if (ksn == NULL) {
411 412 fail(0, "kstat_data_lookup('%s', '%s') failed",
412 413 ks->ks_name, name);
413 414 }
414 415
415 416 (void) printf("%9llu %s\n", ksn->value.ui64, pretty);
416 417 }
417 418
418 419 static void
419 420 dosum(struct sys_snapshot *ss)
420 421 {
421 422 uint64_t total_faults;
422 423 kstat_named_t *ksn;
423 424 long double nchtotal;
424 425 uint64_t nchhits;
425 426
426 427 sum_out("swap ins", &ss->ss_agg_vm, "swapin");
427 428 sum_out("swap outs", &ss->ss_agg_vm, "swapout");
428 429 sum_out("pages swapped in", &ss->ss_agg_vm, "pgswapin");
429 430 sum_out("pages swapped out", &ss->ss_agg_vm, "pgswapout");
430 431
431 432 ksn = kstat_data_lookup(&ss->ss_agg_vm, "hat_fault");
432 433 if (ksn == NULL) {
433 434 fail(0, "kstat_data_lookup('%s', 'hat_fault') failed",
434 435 ss->ss_agg_vm.ks_name);
435 436 }
436 437 total_faults = ksn->value.ui64;
437 438 ksn = kstat_data_lookup(&ss->ss_agg_vm, "as_fault");
438 439 if (ksn == NULL) {
439 440 fail(0, "kstat_data_lookup('%s', 'as_fault') failed",
440 441 ss->ss_agg_vm.ks_name);
441 442 }
442 443 total_faults += ksn->value.ui64;
443 444
444 445 (void) printf("%9llu total address trans. faults taken\n",
445 446 total_faults);
446 447
447 448 sum_out("page ins", &ss->ss_agg_vm, "pgin");
448 449 sum_out("page outs", &ss->ss_agg_vm, "pgout");
449 450 sum_out("pages paged in", &ss->ss_agg_vm, "pgpgin");
450 451 sum_out("pages paged out", &ss->ss_agg_vm, "pgpgout");
451 452 sum_out("total reclaims", &ss->ss_agg_vm, "pgrec");
452 453 sum_out("reclaims from free list", &ss->ss_agg_vm, "pgfrec");
453 454 sum_out("micro (hat) faults", &ss->ss_agg_vm, "hat_fault");
454 455 sum_out("minor (as) faults", &ss->ss_agg_vm, "as_fault");
455 456 sum_out("major faults", &ss->ss_agg_vm, "maj_fault");
456 457 sum_out("copy-on-write faults", &ss->ss_agg_vm, "cow_fault");
457 458 sum_out("zero fill page faults", &ss->ss_agg_vm, "zfod");
458 459 sum_out("pages examined by the clock daemon", &ss->ss_agg_vm, "scan");
459 460 sum_out("revolutions of the clock hand", &ss->ss_agg_vm, "rev");
460 461 sum_out("pages freed by the clock daemon", &ss->ss_agg_vm, "dfree");
461 462 sum_out("forks", &ss->ss_agg_sys, "sysfork");
462 463 sum_out("vforks", &ss->ss_agg_sys, "sysvfork");
463 464 sum_out("execs", &ss->ss_agg_sys, "sysexec");
464 465 sum_out("cpu context switches", &ss->ss_agg_sys, "pswitch");
465 466 sum_out("device interrupts", &ss->ss_agg_sys, "intr");
466 467 sum_out("traps", &ss->ss_agg_sys, "trap");
467 468 sum_out("system calls", &ss->ss_agg_sys, "syscall");
468 469
469 470 nchtotal = (long double) ss->ss_nc.ncs_hits.value.ui64 +
470 471 (long double) ss->ss_nc.ncs_misses.value.ui64;
471 472 nchhits = ss->ss_nc.ncs_hits.value.ui64;
472 473 (void) printf("%9.0Lf total name lookups (cache hits %.0Lf%%)\n",
473 474 nchtotal, nchhits / denom(nchtotal) * 100);
474 475
475 476 sum_out("user cpu", &ss->ss_agg_sys, "cpu_ticks_user");
476 477 sum_out("system cpu", &ss->ss_agg_sys, "cpu_ticks_kernel");
477 478 sum_out("idle cpu", &ss->ss_agg_sys, "cpu_ticks_idle");
478 479 sum_out("wait cpu", &ss->ss_agg_sys, "cpu_ticks_wait");
479 480 }
480 481
481 482 static void
482 483 dointr(struct snapshot *ss)
483 484 {
484 485 size_t i;
485 486 ulong_t total = 0;
486 487
487 488 (void) printf("interrupt total rate\n");
488 489 (void) printf("--------------------------------\n");
489 490
490 491 for (i = 0; i < ss->s_nr_intrs; i++) {
491 492 (void) printf("%-12.8s %10lu %8.0f\n",
492 493 ss->s_intrs[i].is_name, ss->s_intrs[i].is_total,
493 494 ss->s_intrs[i].is_total / etime);
494 495 total += ss->s_intrs[i].is_total;
495 496 }
496 497
497 498 (void) printf("--------------------------------\n");
498 499 (void) printf("Total %10lu %8.0f\n", total, total / etime);
499 500 }
500 501
501 502 static void
502 503 usage(void)
503 504 {
504 505 (void) fprintf(stderr,
505 506 "Usage: vmstat [-ipqsS] [-T d|u] [disk ...] "
506 507 "[interval [count]]\n");
507 508 exit(1);
508 509 }
↓ open down ↓ |
268 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX