summaryrefslogtreecommitdiff
path: root/usr/src/cmd/stat/mpstat/mpstat.c
blob: e945988636b317690dac44b4f31c2d0283dcd929 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/pset.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/sysinfo.h>

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <unistd.h>
#include <memory.h>
#include <string.h>
#include <strings.h>
#include <fcntl.h>
#include <errno.h>
#include <kstat.h>
#include <poll.h>
#include <signal.h>
#include <locale.h>

#include "statcommon.h"

#define	SNAP(s, i, l, n)	((s) ? agg_proc_snap(s, i, l, n) : 0)

#define	REPRINT		20

char *cmdname = "mpstat";
int caught_cont = 0;

static uint_t timestamp_fmt = NODATE;

static int hz;
static int display_pset = -1;
static int show_set = 0;
static int suppress_state;

static void print_header(int, int);
static void show_cpu_usage(struct snapshot *, struct snapshot *, int);
static void usage(void);

int
main(int argc, char **argv)
{
	int c;
	int display_agg = 0;
	int iter = 1;
	int interval = 0;
	char *endptr;
	int infinite_cycles = 0;
	kstat_ctl_t *kc;
	struct snapshot *old = NULL;
	struct snapshot *new = NULL;
	enum snapshot_types types = SNAP_CPUS;
	hrtime_t start_n;
	hrtime_t period_n;

	(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
#define	TEXT_DOMAIN "SYS_TEST"		/* Use this only if it weren't */
#endif
	(void) textdomain(TEXT_DOMAIN);

	while ((c = getopt(argc, argv, "apP:qT:")) != (int)EOF)
		switch (c) {
			case 'a':
				/*
				 * Display aggregate data for processor sets.
				 */
				display_agg = 1;
				break;
			case 'p':
				/*
				 * Display all processor sets.
				 */
				if (display_pset != -1)
					usage();
				show_set = 1;
				break;
			case 'P':
				/*
				 * Display specific processor set.
				 */
				if (show_set == 1)
					usage();
				display_pset = (int)strtol
				    (optarg, &endptr, 10);
				if (*endptr != '\0')
					usage();
				/*
				 * Not valid to specify a negative processor
				 * set value.
				 */
				if (display_pset < 0)
					usage();
				break;
			case 'q':
				suppress_state = 1;
				break;
			case 'T':
				if (optarg) {
					if (*optarg == 'u')
						timestamp_fmt = UDATE;
					else if (*optarg == 'd')
						timestamp_fmt = DDATE;
					else
						usage();
				} else {
					usage();
				}
				break;
			case '?':
				usage();
				break;
		}

	hz = sysconf(_SC_CLK_TCK);

	if (argc > optind) {
		interval = (int)strtol(argv[optind], &endptr, 10);
		if (*endptr != '\0')
			usage();
		period_n = (hrtime_t)interval * NANOSEC;
		if (argc > optind + 1) {
			iter = (unsigned int)strtoul
			    (argv[optind + 1], &endptr, 10);
			if (*endptr != '\0' || iter < 0)
				usage();
			if (iter == 0)
				return (0);
		} else {
			infinite_cycles = 1;
		}
	}

	if (display_agg || show_set || display_pset != -1)
		types |= SNAP_PSETS;

	kc = open_kstat();

	/* Set up handler for SIGCONT */
	if (signal(SIGCONT, cont_handler) == SIG_ERR)
		fail(1, "signal failed");

	start_n = gethrtime();

	while (infinite_cycles || iter > 0) {
		free_snapshot(old);
		old = new;
		new = acquire_snapshot(kc, types, NULL);

		if (!suppress_state)
			snapshot_report_changes(old, new);

		/* if config changed, show stats from boot */
		if (snapshot_has_changed(old, new)) {
			free_snapshot(old);
			old = NULL;
		}

		show_cpu_usage(old, new, display_agg);

		if (!infinite_cycles && --iter < 1)
			break;

		/* Have a kip */
		sleep_until(&start_n, period_n, infinite_cycles, &caught_cont);
	}
	(void) kstat_close(kc);

	return (0);
}

/*
 * Print an mpstat output header.
 */
static void
print_header(int display_agg, int show_set)
{
	if (display_agg == 1)
		(void) printf("SET minf mjf xcal  intr ithr  csw icsw migr "
		    "smtx  srw syscl  usr sys  wt idl sze");
	else {
		(void) printf("CPU minf mjf xcal  intr ithr  csw icsw migr "
		    "smtx  srw syscl  usr sys  wt idl");
		if (show_set == 1)
			(void) printf(" set");
	}
	(void) printf("\n");
}

static void
print_cpu(struct cpu_snapshot *c1, struct cpu_snapshot *c2)
{
	uint64_t ticks = 0;
	double etime, percent;
	kstat_t *old_vm = NULL;
	kstat_t *old_sys = NULL;

	if (display_pset != -1 && display_pset != c2->cs_pset_id)
		return;

	/*
	 * the first mpstat output will have c1 = NULL, to give
	 * results since boot
	 */
	if (c1) {
		old_vm = &c1->cs_vm;
		old_sys = &c1->cs_sys;

		/* check there are stats to report */
		if (!CPU_ACTIVE(c1))
			return;
	}

	/* check there are stats to report */
	if (!CPU_ACTIVE(c2))
		return;

	ticks = cpu_ticks_delta(old_sys, &c2->cs_sys);

	etime = (double)ticks / hz;
	if (etime == 0.0) /* Prevent divide by zero errors */
		etime = 1.0;
	percent = 100.0 / etime / hz;

	(void) printf("%3d %4.0f %3.0f %4.0f %5.0f %4.0f "
	    "%4.0f %4.0f %4.0f %4.0f %4.0f %5.0f  %3.0f %3.0f "
	    "%3.0f %3.0f",
	    c2->cs_id,
	    (kstat_delta(old_vm, &c2->cs_vm, "hat_fault") +
	    kstat_delta(old_vm, &c2->cs_vm, "as_fault")) / etime,
	    kstat_delta(old_vm, &c2->cs_vm, "maj_fault") / etime,
	    kstat_delta(old_sys, &c2->cs_sys, "xcalls") / etime,
	    kstat_delta(old_sys, &c2->cs_sys, "intr") / etime,
	    kstat_delta(old_sys, &c2->cs_sys, "intrthread") / etime,
	    kstat_delta(old_sys, &c2->cs_sys, "pswitch") / etime,
	    kstat_delta(old_sys, &c2->cs_sys, "inv_swtch") / etime,
	    kstat_delta(old_sys, &c2->cs_sys, "cpumigrate") / etime,
	    kstat_delta(old_sys, &c2->cs_sys, "mutex_adenters") / etime,
	    (kstat_delta(old_sys, &c2->cs_sys, "rw_rdfails") +
	    kstat_delta(old_sys, &c2->cs_sys, "rw_wrfails")) / etime,
	    kstat_delta(old_sys, &c2->cs_sys, "syscall") / etime,
	    kstat_delta(old_sys, &c2->cs_sys, "cpu_ticks_user") * percent,
	    kstat_delta(old_sys, &c2->cs_sys, "cpu_ticks_kernel") * percent,
	    kstat_delta(old_sys, &c2->cs_sys, "cpu_ticks_wait") * percent,
	    kstat_delta(old_sys, &c2->cs_sys, "cpu_ticks_idle") * percent);

	if (show_set)
		(void) printf(" %3d", c2->cs_pset_id);
	(void) printf("\n");
}

/*ARGSUSED*/
static void
compare_cpu(void *v1, void *v2, void *data)
{
	struct cpu_snapshot *c1 = (struct cpu_snapshot *)v1;
	struct cpu_snapshot *c2 = (struct cpu_snapshot *)v2;

	if (c2 == NULL)
		return;

	print_cpu(c1, c2);
}

static int
pset_has_stats(struct pset_snapshot *p)
{
	int count = 0;
	size_t i;
	for (i = 0; i < p->ps_nr_cpus; i++) {
		if (CPU_ACTIVE(p->ps_cpus[i]))
			count++;
	}
	return (count);
}

static void
agg_stat(kstat_t *k1, kstat_t *k2, char *name)
{
	kstat_named_t *ksn = kstat_data_lookup(k1, name);
	kstat_named_t *ksn2 = kstat_data_lookup(k2, name);
	ksn->value.ui64 += ksn2->value.ui64;
}

static kstat_t *
agg_vm(struct pset_snapshot *p, kstat_t *ks)
{
	size_t i;

	if (p->ps_nr_cpus == 0)
		return (NULL);

	if (kstat_copy(&p->ps_cpus[0]->cs_vm, ks))
		return (NULL);

	for (i = 1; i < p->ps_nr_cpus; i++) {
		agg_stat(ks, &p->ps_cpus[i]->cs_vm, "hat_fault");
		agg_stat(ks, &p->ps_cpus[i]->cs_vm, "as_fault");
		agg_stat(ks, &p->ps_cpus[i]->cs_vm, "maj_fault");
	}

	return (ks);
}

static kstat_t *
agg_sys(struct pset_snapshot *p, kstat_t *ks)
{
	size_t i;

	if (p->ps_nr_cpus == 0)
		return (NULL);

	if (kstat_copy(&p->ps_cpus[0]->cs_sys, ks))
		return (NULL);

	for (i = 1; i < p->ps_nr_cpus; i++) {
		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "xcalls");
		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "intr");
		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "intrthread");
		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "pswitch");
		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "inv_swtch");
		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpumigrate");
		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "mutex_adenters");
		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "rw_rdfails");
		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "rw_wrfails");
		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "syscall");
		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpu_ticks_user");
		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpu_ticks_kernel");
		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpu_ticks_wait");
		agg_stat(ks, &p->ps_cpus[i]->cs_sys, "cpu_ticks_idle");
	}

	return (ks);
}

static uint64_t
get_nr_ticks(struct pset_snapshot *p1, struct pset_snapshot *p2)
{
	kstat_t *old = NULL;
	kstat_t *new = NULL;
	size_t i = 0;

	for (i = 0; p1 && i < p1->ps_nr_cpus; i++) {
		if (p1->ps_cpus[i]->cs_sys.ks_data) {
			old = &p1->ps_cpus[i]->cs_sys;
			break;
		}
	}

	for (i = 0; p2 && i < p2->ps_nr_cpus; i++) {
		if (p2->ps_cpus[i]->cs_sys.ks_data) {
			new = &p2->ps_cpus[i]->cs_sys;
			break;
		}
	}

	if (old == NULL && new == NULL)
		return (0);

	if (new == NULL) {
		new = old;
		old = NULL;
	}

	return (cpu_ticks_delta(old, new));
}

static void
print_pset(struct pset_snapshot *p1, struct pset_snapshot *p2)
{
	uint64_t ticks = 0;
	double etime, percent;
	kstat_t old_vm;
	kstat_t old_sys;
	kstat_t new_vm;
	kstat_t new_sys;

	if (display_pset != -1 && display_pset != p2->ps_id)
		return;

	if ((p1 && !pset_has_stats(p1)) || !pset_has_stats(p2))
		return;

	old_vm.ks_data = old_sys.ks_data = NULL;
	new_vm.ks_data = new_sys.ks_data = NULL;

	/*
	 * FIXME: these aggs will count "new" or disappeared cpus
	 * in a set, leaving an apparent huge change.
	 */

	/*
	 * the first mpstat output will have p1 = NULL, to give
	 * results since boot
	 */
	if (p1) {
		if (!agg_vm(p1, &old_vm) || !agg_sys(p1, &old_sys))
			goto out;
	}

	if (!agg_vm(p2, &new_vm) || !agg_sys(p2, &new_sys))
		goto out;

	ticks = get_nr_ticks(p1, p2);

	etime = (double)ticks / hz;
	if (etime == 0.0) /* Prevent divide by zero errors */
		etime = 1.0;
	percent = 100.0 / p2->ps_nr_cpus / etime / hz;

	(void) printf("%3d %4.0f %3.0f %4.0f %5.0f %4.0f "
	    "%4.0f %4.0f %4.0f %4.0f %4.0f %5.0f  %3.0f %3.0f "
	    "%3.0f %3.0f %3d\n",
	    p2->ps_id,
	    (kstat_delta(&old_vm, &new_vm, "hat_fault") +
	    kstat_delta(&old_vm, &new_vm, "as_fault")) / etime,
	    kstat_delta(&old_vm, &new_vm, "maj_fault") / etime,
	    kstat_delta(&old_sys, &new_sys, "xcalls") / etime,
	    kstat_delta(&old_sys, &new_sys, "intr") / etime,
	    kstat_delta(&old_sys, &new_sys, "intrthread") / etime,
	    kstat_delta(&old_sys, &new_sys, "pswitch") / etime,
	    kstat_delta(&old_sys, &new_sys, "inv_swtch") / etime,
	    kstat_delta(&old_sys, &new_sys, "cpumigrate") / etime,
	    kstat_delta(&old_sys, &new_sys, "mutex_adenters") / etime,
	    (kstat_delta(&old_sys, &new_sys, "rw_rdfails") +
	    kstat_delta(&old_sys, &new_sys, "rw_wrfails")) / etime,
	    kstat_delta(&old_sys, &new_sys, "syscall") / etime,
	    kstat_delta(&old_sys, &new_sys, "cpu_ticks_user") * percent,
	    kstat_delta(&old_sys, &new_sys, "cpu_ticks_kernel") * percent,
	    kstat_delta(&old_sys, &new_sys, "cpu_ticks_wait") * percent,
	    kstat_delta(&old_sys, &new_sys, "cpu_ticks_idle") * percent,
	    p2->ps_nr_cpus);

out:
	free(old_vm.ks_data);
	free(old_sys.ks_data);
	free(new_vm.ks_data);
	free(new_sys.ks_data);
}

/*ARGSUSED*/
static void
compare_pset(void *v1, void *v2, void *data)
{
	struct pset_snapshot *p1 = (struct pset_snapshot *)v1;
	struct pset_snapshot *p2 = (struct pset_snapshot *)v2;

	if (p2 == NULL)
		return;

	print_pset(p1, p2);
}


/*
 * Report statistics for a sample interval.
 */
static void
show_cpu_usage(struct snapshot *old, struct snapshot *new, int display_agg)
{
	static int lines_until_reprint = 0;
	enum snapshot_types type = SNAP_CPUS;
	snapshot_cb cb = compare_cpu;

	if (timestamp_fmt != NODATE)
		print_timestamp(timestamp_fmt);

	if (lines_until_reprint == 0 || nr_active_cpus(new) > 1) {
		print_header(display_agg, show_set);
		lines_until_reprint = REPRINT;
	}

	lines_until_reprint--;

	if (display_agg) {
		type = SNAP_PSETS;
		cb = compare_pset;
	}

	/* print stats since boot the first time round */
	(void) snapshot_walk(type, old, new, cb, NULL);
	(void) fflush(stdout);
}

/*
 * Usage message on error.
 */
static void
usage(void)
{
	(void) fprintf(stderr,
	    "Usage: mpstat [-aq] [-p | -P processor_set] [-T d|u] "
	    "[interval [count]]\n");
	exit(1);
}