xref: /freebsd/usr.sbin/pmc/cmd_pmc_stat.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2018, Matthew Macy
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/cpuset.h>
32 #include <sys/event.h>
33 #include <sys/queue.h>
34 #include <sys/socket.h>
35 #include <sys/stat.h>
36 #include <sys/sysctl.h>
37 #include <sys/time.h>
38 #include <sys/ttycom.h>
39 #include <sys/user.h>
40 #include <sys/wait.h>
41 
42 #include <assert.h>
43 #include <curses.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <getopt.h>
48 #include <kvm.h>
49 #include <libgen.h>
50 #include <limits.h>
51 #include <locale.h>
52 #include <math.h>
53 #include <pmc.h>
54 #include <pmclog.h>
55 #include <regex.h>
56 #include <signal.h>
57 #include <stdarg.h>
58 #include <stdint.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <sysexits.h>
63 #include <unistd.h>
64 
65 #include <libpmcstat.h>
66 #include "cmd_pmc.h"
67 
68 /*
69  * Return the frequency of the kernel's statistics clock.
70  */
71 static int
72 getstathz(void)
73 {
74 	int mib[2];
75 	size_t size;
76 	struct clockinfo clockrate;
77 
78 	mib[0] = CTL_KERN;
79 	mib[1] = KERN_CLOCKRATE;
80 	size = sizeof clockrate;
81 	if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1)
82 		err(1, "sysctl kern.clockrate");
83 	return clockrate.stathz;
84 }
85 
86 #define STAT_MODE_NPMCS 6
87 #define FIXED_MODE_NPMCS 2
88 static struct timespec before_ts;
89 #define CYCLES		0
90 #define INST		1
91 #define BR		2
92 #define IAP_START	BR
93 #define BR_MISS	3
94 #define CACHE	4
95 #define CACHE_MISS	5
96 static const char *pmc_stat_mode_names[] = {
97 	"cycles",
98 	"instructions",
99 	"branches",
100 	"branch-misses",
101 	"cache-references",
102 	"cache-misses",
103 };
104 
105 /* Common aliases for the desired stat counter */
106 static const char *pmc_stat_mode_aliases[] = {
107 	"unhalted-cycles",
108 	"instructions",
109 	"branches",
110 	"branch-mispredicts",
111 	"LLC-REFERENCE",
112 	"LLC-MISSES",
113 };
114 
115 static int pmcstat_sockpair[NSOCKPAIRFD];
116 
117 static void __dead2
118 usage(void)
119 {
120 	errx(EX_USAGE,
121 	    "\t get basic stats from command line program\n"
122 	    "\t -j <eventlist>, --events <eventlist> comma-delimited list of event specifiers\n"
123 	    );
124 }
125 
126 static void
127 showtime(FILE *out, struct timespec *before, struct timespec *after,
128     struct rusage *ru)
129 {
130 	char decimal_point;
131 	uint64_t real, user, sys;
132 
133 	(void)setlocale(LC_NUMERIC, "");
134 	decimal_point = localeconv()->decimal_point[0];
135 
136 	after->tv_sec -= before->tv_sec;
137 	after->tv_nsec -= before->tv_nsec;
138 	if (after->tv_nsec < 0) {
139 		after->tv_sec--;
140 		after->tv_nsec += 1000000000;
141 	}
142 
143 	real = (after->tv_sec * 1000000000 + after->tv_nsec) / 1000;
144 	user = ru->ru_utime.tv_sec * 1000000 + ru->ru_utime.tv_usec;
145 	sys = ru->ru_stime.tv_sec * 1000000 + ru->ru_stime.tv_usec;
146 	fprintf(out, "%13jd%c%02ld  real\t\t\t#\t%2.02f%% cpu\n",
147 	    (intmax_t)after->tv_sec, decimal_point,
148 	    after->tv_nsec / 10000000, 100 * (double)(sys + user + 1) / (double)(real + 1));
149 	fprintf(out, "%13jd%c%02ld  user\t\t\t#\t%2.2f%% cpu\n",
150 	    (intmax_t)ru->ru_utime.tv_sec, decimal_point,
151 	    ru->ru_utime.tv_usec / 10000, 100 * (double)(user + 1) / (double)(real + 1));
152 	fprintf(out, "%13jd%c%02ld  sys\t\t\t#\t%2.02f%% cpu\n",
153 	    (intmax_t)ru->ru_stime.tv_sec, decimal_point,
154 	    ru->ru_stime.tv_usec / 10000, 100 * (double)(sys + 1) / (double)(real + 1));
155 }
156 
157 static const char *stat_mode_cntrs[STAT_MODE_NPMCS];
158 static const char *stat_mode_names[STAT_MODE_NPMCS];
159 
160 static void
161 pmc_stat_setup_stat(int system_mode, const char *arg)
162 {
163 	const char *new_cntrs[STAT_MODE_NPMCS];
164 	struct pmcstat_ev *ev;
165 	char *counters, *counter;
166 	int i, c, start, newcnt;
167 	cpuset_t cpumask, rootmask;
168 
169 	if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
170 	    sizeof(rootmask), &rootmask) == -1)
171 		err(EX_OSERR, "ERROR: Cannot determine the root set of CPUs");
172 	CPU_COPY(&rootmask, &cpumask);
173 
174 	if (system_mode && geteuid() != 0)
175 		errx(EX_USAGE, "ERROR: system mode counters can only be used as root");
176 	counters = NULL;
177 	for (i = 0; i < STAT_MODE_NPMCS; i++) {
178 		stat_mode_cntrs[i] = pmc_stat_mode_aliases[i];
179 		stat_mode_names[i] = pmc_stat_mode_names[i];
180 	}
181 	if (arg) {
182 		counters = strdup(arg);
183 		newcnt = 0;
184 		while ((counter = strsep(&counters, ",")) != NULL &&
185 		    newcnt < STAT_MODE_NPMCS - IAP_START) {
186 			new_cntrs[newcnt++] = counter;
187 			if (pmc_pmu_sample_rate_get(counter) == DEFAULT_SAMPLE_COUNT)
188 				errx(EX_USAGE, "ERROR: %s not recognized on host", counter);
189 		}
190 		start = IAP_START + STAT_MODE_NPMCS - FIXED_MODE_NPMCS - newcnt;
191 		for (i = 0; i < newcnt; i++) {
192 			stat_mode_cntrs[start + i] = new_cntrs[i];
193 			stat_mode_names[start + i] = new_cntrs[i];
194 		}
195 	}
196 	if (system_mode)
197 		pmc_args.pa_flags |= FLAG_HAS_SYSTEM_PMCS;
198 	else
199 		pmc_args.pa_flags |= FLAG_HAS_PROCESS_PMCS;
200 	pmc_args.pa_flags |= FLAG_HAS_COUNTING_PMCS;
201 	pmc_args.pa_flags |= FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET;
202 	pmc_args.pa_flags |= FLAG_HAS_PIPE;
203 	pmc_args.pa_required |= FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET | FLAG_HAS_OUTPUT_LOGFILE;
204 	pmc_args.pa_outputpath = strdup("/dev/null");
205 	pmc_args.pa_logfd = pmcstat_open_log(pmc_args.pa_outputpath,
206 	    PMCSTAT_OPEN_FOR_WRITE);
207 	for (i = 0; i < STAT_MODE_NPMCS; i++) {
208 		if ((ev = malloc(sizeof(*ev))) == NULL)
209 			errx(EX_SOFTWARE, "ERROR: Out of memory.");
210 		if (system_mode)
211 			ev->ev_mode = PMC_MODE_SC;
212 		else
213 			ev->ev_mode = PMC_MODE_TC;
214 		ev->ev_spec = strdup(stat_mode_cntrs[i]);
215 		if (ev->ev_spec == NULL)
216 			errx(EX_SOFTWARE, "ERROR: Out of memory.");
217 		c = strcspn(strdup(stat_mode_cntrs[i]), ", \t");
218 		ev->ev_name = malloc(c + 1);
219 		if (ev->ev_name == NULL)
220 			errx(EX_SOFTWARE, "ERROR: Out of memory.");
221 		(void)strncpy(ev->ev_name, stat_mode_cntrs[i], c);
222 		*(ev->ev_name + c) = '\0';
223 
224 		ev->ev_count = -1;
225 		ev->ev_flags = 0;
226 		ev->ev_flags |= PMC_F_DESCENDANTS;
227 		ev->ev_cumulative = 1;
228 
229 		ev->ev_saved = 0LL;
230 		ev->ev_pmcid = PMC_ID_INVALID;
231 		STAILQ_INSERT_TAIL(&pmc_args.pa_events, ev, ev_next);
232 		if (system_mode) {
233 			ev->ev_cpu = CPU_FFS(&cpumask) - 1;
234 			CPU_CLR(ev->ev_cpu, &cpumask);
235 			pmcstat_clone_event_descriptor(ev, &cpumask, &pmc_args);
236 			CPU_SET(ev->ev_cpu, &cpumask);
237 		} else
238 			ev->ev_cpu = PMC_CPU_ANY;
239 
240 	}
241 	if (clock_gettime(CLOCK_MONOTONIC, &before_ts))
242 		err(1, "clock_gettime");
243 }
244 
245 static void
246 pmc_stat_print_stat(struct rusage *ru)
247 {
248 	struct pmcstat_ev *ev;
249 	struct timespec after;
250 	uint64_t cvals[STAT_MODE_NPMCS];
251 	uint64_t ticks, value;
252 	int hz, i;
253 
254 	if (ru) {
255 		hz = getstathz();
256 		ticks = hz * (ru->ru_utime.tv_sec + ru->ru_stime.tv_sec) +
257 			hz * (ru->ru_utime.tv_usec + ru->ru_stime.tv_usec) / 1000000;
258 		if (clock_gettime(CLOCK_MONOTONIC, &after))
259 			err(1, "clock_gettime");
260 		/*
261 		 * If our round-off on the tick calculation still puts us at 0,
262 		 * then always assume at least one tick.
263 		 */
264 		if (ticks == 0)
265 			ticks = 1;
266 		fprintf(pmc_args.pa_printfile, "%16ld  %s\t\t#\t%02.03f M/sec\n",
267 			ru->ru_minflt, "page faults", ((double)ru->ru_minflt / (double)ticks) / hz);
268 		fprintf(pmc_args.pa_printfile, "%16ld  %s\t\t#\t%02.03f M/sec\n",
269 			ru->ru_nvcsw, "voluntary csw", ((double)ru->ru_nvcsw / (double)ticks) / hz);
270 		fprintf(pmc_args.pa_printfile, "%16ld  %s\t#\t%02.03f M/sec\n",
271 			ru->ru_nivcsw, "involuntary csw", ((double)ru->ru_nivcsw / (double)ticks) / hz);
272 	}
273 
274 	bzero(&cvals, sizeof(cvals));
275 	STAILQ_FOREACH(ev, &pmc_args.pa_events, ev_next) {
276 		if (pmc_read(ev->ev_pmcid, &value) < 0)
277 			err(EX_OSERR, "ERROR: Cannot read pmc \"%s\"",
278 			    ev->ev_name);
279 		for (i = 0; i < STAT_MODE_NPMCS; i++)
280 			if (strcmp(ev->ev_name, stat_mode_cntrs[i]) == 0)
281 				cvals[i] += value;
282 	}
283 
284 	fprintf(pmc_args.pa_printfile, "%16jd  %s\n", (uintmax_t)cvals[CYCLES], stat_mode_names[CYCLES]);
285 	fprintf(pmc_args.pa_printfile, "%16jd  %s\t\t#\t%01.03f inst/cycle\n", (uintmax_t)cvals[INST], stat_mode_names[INST],
286 	    (double)cvals[INST] / cvals[CYCLES]);
287 	fprintf(pmc_args.pa_printfile, "%16jd  %s\n", (uintmax_t)cvals[BR], stat_mode_names[BR]);
288 	if (stat_mode_names[BR_MISS] == pmc_stat_mode_names[BR_MISS])
289 		fprintf(pmc_args.pa_printfile, "%16jd  %s\t\t#\t%.03f%%\n",
290 		    (uintmax_t)cvals[BR_MISS], stat_mode_names[BR_MISS],
291 		    100 * ((double)cvals[BR_MISS] / cvals[BR]));
292 	else
293 		fprintf(pmc_args.pa_printfile, "%16jd  %s\n",
294 		    (uintmax_t)cvals[BR_MISS], stat_mode_names[BR_MISS]);
295 	fprintf(pmc_args.pa_printfile, "%16jd  %s%s", (uintmax_t)cvals[CACHE], stat_mode_names[CACHE],
296 	    stat_mode_names[CACHE] != pmc_stat_mode_names[CACHE] ? "\n" : "");
297 	if (stat_mode_names[CACHE] == pmc_stat_mode_names[CACHE])
298 		fprintf(pmc_args.pa_printfile, "\t#\t%.03f refs/inst\n",
299 		    ((double)cvals[CACHE] / cvals[INST]));
300 	fprintf(pmc_args.pa_printfile, "%16jd  %s%s", (uintmax_t)cvals[CACHE_MISS], stat_mode_names[CACHE_MISS],
301 	    stat_mode_names[CACHE_MISS] != pmc_stat_mode_names[CACHE_MISS] ? "\n" : "");
302 	if (stat_mode_names[CACHE_MISS] == pmc_stat_mode_names[CACHE_MISS])
303 		fprintf(pmc_args.pa_printfile, "\t\t#\t%.03f%%\n",
304 		    100 * ((double)cvals[CACHE_MISS] / cvals[CACHE]));
305 
306 	if (ru)
307 		showtime(pmc_args.pa_printfile, &before_ts, &after, ru);
308 }
309 
310 static struct option longopts[] = {
311 	{"events", required_argument, NULL, 'j'},
312 	{NULL, 0, NULL, 0}
313 };
314 
315 static int
316 pmc_stat_internal(int argc, char **argv, int system_mode)
317 {
318 	char *event, *r;
319 	struct sigaction sa;
320 	struct kevent kev;
321 	struct rusage ru;
322 	struct winsize ws;
323 	struct pmcstat_ev *ev;
324 	int c, option, runstate;
325 	int waitstatus, ru_valid, do_debug;
326 
327 	do_debug = ru_valid = 0;
328 	r = event = NULL;
329 	while ((option = getopt_long(argc, argv, "dj:", longopts, NULL)) != -1) {
330 		switch (option) {
331 		case 'j':
332 			r = event = strdup(optarg);
333 			break;
334 		case 'd':
335 			do_debug = 1;
336 			break;
337 		case '?':
338 		default:
339 			usage();
340 		}
341 	}
342 	pmc_args.pa_argc = (argc -= optind);
343 	pmc_args.pa_argv = (argv += optind);
344 	if (argc == 0)
345 		usage();
346 	pmc_args.pa_flags |= FLAG_HAS_COMMANDLINE;
347 	pmc_stat_setup_stat(system_mode, event);
348 	free(r);
349 	bzero(&ru, sizeof(ru));
350 	EV_SET(&kev, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
351 	if (kevent(pmc_kq, &kev, 1, NULL, 0, NULL) < 0)
352 		err(EX_OSERR, "ERROR: Cannot register kevent for SIGINT");
353 
354 	EV_SET(&kev, SIGIO, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
355 	if (kevent(pmc_kq, &kev, 1, NULL, 0, NULL) < 0)
356 		err(EX_OSERR, "ERROR: Cannot register kevent for SIGIO");
357 	EV_SET(&kev, 0, EVFILT_TIMER, EV_ADD, 0, 1000, NULL);
358 	if (kevent(pmc_kq, &kev, 1, NULL, 0, NULL) < 0)
359 		err(EX_OSERR,
360 			"ERROR: Cannot register kevent for timer");
361 
362 	STAILQ_FOREACH(ev, &pmc_args.pa_events, ev_next) {
363 		if (pmc_allocate(ev->ev_spec, ev->ev_mode,
364 		    ev->ev_flags, ev->ev_cpu, &ev->ev_pmcid, ev->ev_count) < 0)
365 			err(EX_OSERR,
366 			    "ERROR: Cannot allocate %s-mode pmc with specification \"%s\"",
367 			    PMC_IS_SYSTEM_MODE(ev->ev_mode) ?
368 			    "system" : "process", ev->ev_spec);
369 
370 		if (PMC_IS_SAMPLING_MODE(ev->ev_mode) &&
371 		    pmc_set(ev->ev_pmcid, ev->ev_count) < 0)
372 			err(EX_OSERR,
373 			    "ERROR: Cannot set sampling count for PMC \"%s\"",
374 			    ev->ev_name);
375 	}
376 
377 	/*
378 	 * An exec() failure of a forked child is signalled by the
379 	 * child sending the parent a SIGCHLD.  We don't register an
380 	 * actual signal handler for SIGCHLD, but instead use our
381 	 * kqueue to pick up the signal.
382 	 */
383 	EV_SET(&kev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
384 	if (kevent(pmc_kq, &kev, 1, NULL, 0, NULL) < 0)
385 		err(EX_OSERR, "ERROR: Cannot register kevent for SIGCHLD");
386 
387 	pmcstat_create_process(pmcstat_sockpair, &pmc_args, pmc_kq);
388 
389 	if (SLIST_EMPTY(&pmc_args.pa_targets))
390 		errx(EX_DATAERR,
391 		    "ERROR: No matching target processes.");
392 	if (pmc_args.pa_flags & FLAG_HAS_PROCESS_PMCS)
393 		pmcstat_attach_pmcs(&pmc_args);
394 
395 	/* start the pmcs */
396 	pmc_util_start_pmcs(&pmc_args);
397 
398 	/* start the (commandline) process if needed */
399 	pmcstat_start_process(pmcstat_sockpair);
400 
401 	/* Handle SIGINT using the kqueue loop */
402 	sa.sa_handler = SIG_IGN;
403 	sa.sa_flags = 0;
404 	(void)sigemptyset(&sa.sa_mask);
405 
406 	if (sigaction(SIGINT, &sa, NULL) < 0)
407 		err(EX_OSERR, "ERROR: Cannot install signal handler");
408 
409 	/*
410  * loop till either the target process (if any) exits, or we
411  * are killed by a SIGINT or we reached the time duration.
412  */
413 	runstate = PMCSTAT_RUNNING;
414 	do {
415 		if ((c = kevent(pmc_kq, NULL, 0, &kev, 1, NULL)) <= 0) {
416 			if (errno != EINTR)
417 				err(EX_OSERR, "ERROR: kevent failed");
418 			else
419 				continue;
420 		}
421 		if (kev.flags & EV_ERROR)
422 			errc(EX_OSERR, kev.data, "ERROR: kevent failed");
423 
424 		switch (kev.filter) {
425 		case EVFILT_PROC:	/* target has exited */
426 			if (wait4(pmc_util_get_pid(&pmc_args), &waitstatus, 0, &ru) > 0) {
427 				getrusage(RUSAGE_CHILDREN, &ru);
428 				ru_valid = 1;
429 			}
430 			break;
431 
432 		case EVFILT_READ:	/* log file data is present */
433 			break;
434 		case EVFILT_TIMER:
435 			if (do_debug)
436 				pmc_stat_print_stat(NULL);
437 			break;
438 		case EVFILT_SIGNAL:
439 			if (kev.ident == SIGCHLD) {
440 				/*
441 				 * The child process sends us a
442 				 * SIGCHLD if its exec() failed.  We
443 				 * wait for it to exit and then exit
444 				 * ourselves.
445 				 */
446 				(void)wait(&c);
447 				runstate = PMCSTAT_FINISHED;
448 			} else if (kev.ident == SIGIO) {
449 				/*
450 				 * We get a SIGIO if a PMC loses all
451 				 * of its targets, or if logfile
452 				 * writes encounter an error.
453 				 */
454 				if (wait4(pmc_util_get_pid(&pmc_args), &waitstatus, 0, &ru) > 0) {
455 					getrusage(RUSAGE_CHILDREN, &ru);
456 					ru_valid = 1;
457 				}
458 				runstate = pmcstat_close_log(&pmc_args);
459 			} else if (kev.ident == SIGINT) {
460 				/* Kill the child process if we started it */
461 				if (pmc_args.pa_flags & FLAG_HAS_COMMANDLINE)
462 					pmc_util_kill_process(&pmc_args);
463 				runstate = pmcstat_close_log(&pmc_args);
464 			} else if (kev.ident == SIGWINCH) {
465 				if (ioctl(fileno(pmc_args.pa_printfile),
466 				    TIOCGWINSZ, &ws) < 0)
467 					err(EX_OSERR,
468 					    "ERROR: Cannot determine window size");
469 				pmc_displayheight = ws.ws_row - 1;
470 				pmc_displaywidth = ws.ws_col - 1;
471 			} else
472 				assert(0);
473 
474 			break;
475 		}
476 	} while (runstate != PMCSTAT_FINISHED);
477 	if (!ru_valid)
478 		warnx("couldn't get rusage");
479 	pmc_stat_print_stat(&ru);
480 	pmc_util_cleanup(&pmc_args);
481 	return (0);
482 }
483 
484 int
485 cmd_pmc_stat(int argc, char **argv)
486 {
487 	return (pmc_stat_internal(argc, argv, 0));
488 }
489 
490 int
491 cmd_pmc_stat_system(int argc, char **argv)
492 {
493 	return (pmc_stat_internal(argc, argv, 1));
494 }
495