2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018, Matthew Macy
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/cpuset.h>
34 #include <sys/event.h>
35 #include <sys/queue.h>
36 #include <sys/socket.h>
38 #include <sys/sysctl.h>
40 #include <sys/ttycom.h>
67 #include <libpmcstat.h>
71 * Return the frequency of the kernel's statistics clock.
78 struct clockinfo clockrate;
81 mib[1] = KERN_CLOCKRATE;
82 size = sizeof clockrate;
83 if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1)
84 err(1, "sysctl kern.clockrate");
85 return clockrate.stathz;
88 #define STAT_MODE_NPMCS 6
89 static struct timespec before_ts;
97 static const char *pmc_stat_mode_names[] = {
106 static int pmcstat_sockpair[NSOCKPAIRFD];
112 "\t get basic stats from command line program\n"
113 "\t -j <eventlist>, --events <eventlist> comma-delimited list of event specifiers\n"
118 showtime(FILE *out, struct timespec *before, struct timespec *after,
122 uint64_t real, user, sys;
124 (void)setlocale(LC_NUMERIC, "");
125 decimal_point = localeconv()->decimal_point[0];
127 after->tv_sec -= before->tv_sec;
128 after->tv_nsec -= before->tv_nsec;
129 if (after->tv_nsec < 0)
130 after->tv_sec--, after->tv_nsec += 1000000000;
132 real = (after->tv_sec * 1000000000 + after->tv_nsec) / 1000;
133 user = ru->ru_utime.tv_sec * 1000000 + ru->ru_utime.tv_usec;
134 sys = ru->ru_stime.tv_sec * 1000000 + ru->ru_stime.tv_usec;
135 fprintf(out, "%13jd%c%02ld real\t\t\t#\t%2.02f%% cpu\n",
136 (intmax_t)after->tv_sec, decimal_point,
137 after->tv_nsec / 10000000, 100 * (double)(sys + user + 1) / (double)(real + 1));
138 fprintf(out, "%13jd%c%02ld user\t\t\t#\t%2.2f%% cpu\n",
139 (intmax_t)ru->ru_utime.tv_sec, decimal_point,
140 ru->ru_utime.tv_usec / 10000, 100 * (double)(user + 1) / (double)(real + 1));
141 fprintf(out, "%13jd%c%02ld sys\t\t\t#\t%2.02f%% cpu\n",
142 (intmax_t)ru->ru_stime.tv_sec, decimal_point,
143 ru->ru_stime.tv_usec / 10000, 100 * (double)(sys + 1) / (double)(real + 1));
146 static const char *stat_mode_cntrs[STAT_MODE_NPMCS];
147 static const char *stat_mode_names[STAT_MODE_NPMCS];
150 pmc_stat_setup_stat(int system_mode, const char *arg)
152 const char *new_cntrs[STAT_MODE_NPMCS];
153 static const char **pmc_stat_mode_cntrs;
154 struct pmcstat_ev *ev;
155 char *counters, *counter;
156 int i, c, start, newcnt;
157 cpuset_t cpumask, rootmask;
159 if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
160 sizeof(rootmask), &rootmask) == -1)
161 err(EX_OSERR, "ERROR: Cannot determine the root set of CPUs");
162 CPU_COPY(&rootmask, &cpumask);
164 if (pmc_pmu_stat_mode(&pmc_stat_mode_cntrs) != 0)
165 errx(EX_USAGE, "ERROR: hwmpc.ko not loaded or stat not supported on host.");
166 if (system_mode && geteuid() != 0)
167 errx(EX_USAGE, "ERROR: system mode counters can only be used as root");
169 for (i = 0; i < STAT_MODE_NPMCS; i++) {
170 stat_mode_cntrs[i] = pmc_stat_mode_cntrs[i];
171 stat_mode_names[i] = pmc_stat_mode_names[i];
174 counters = strdup(arg);
176 while ((counter = strsep(&counters, ",")) != NULL &&
177 newcnt < STAT_MODE_NPMCS - IAP_START) {
178 new_cntrs[newcnt++] = counter;
179 if (pmc_pmu_sample_rate_get(counter) == DEFAULT_SAMPLE_COUNT)
180 errx(EX_USAGE, "ERROR: %s not recognized on host", counter);
182 start = IAP_START + STAT_MODE_NPMCS - newcnt;
183 for (i = 0; i < newcnt; i++) {
184 stat_mode_cntrs[start + i] = new_cntrs[i];
185 stat_mode_names[start + i] = new_cntrs[i];
189 pmc_args.pa_flags |= FLAG_HAS_SYSTEM_PMCS;
191 pmc_args.pa_flags |= FLAG_HAS_PROCESS_PMCS;
192 pmc_args.pa_flags |= FLAG_HAS_COUNTING_PMCS;
193 pmc_args.pa_flags |= FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET;
194 pmc_args.pa_flags |= FLAG_HAS_PIPE;
195 pmc_args.pa_required |= FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET | FLAG_HAS_OUTPUT_LOGFILE;
196 pmc_args.pa_outputpath = strdup("/dev/null");
197 pmc_args.pa_logfd = pmcstat_open_log(pmc_args.pa_outputpath,
198 PMCSTAT_OPEN_FOR_WRITE);
199 for (i = 0; i < STAT_MODE_NPMCS; i++) {
200 if ((ev = malloc(sizeof(*ev))) == NULL)
201 errx(EX_SOFTWARE, "ERROR: Out of memory.");
203 ev->ev_mode = PMC_MODE_SC;
205 ev->ev_mode = PMC_MODE_TC;
206 ev->ev_spec = strdup(stat_mode_cntrs[i]);
207 if (ev->ev_spec == NULL)
208 errx(EX_SOFTWARE, "ERROR: Out of memory.");
209 c = strcspn(strdup(stat_mode_cntrs[i]), ", \t");
210 ev->ev_name = malloc(c + 1);
211 if (ev->ev_name == NULL)
212 errx(EX_SOFTWARE, "ERROR: Out of memory.");
213 (void)strncpy(ev->ev_name, stat_mode_cntrs[i], c);
214 *(ev->ev_name + c) = '\0';
218 ev->ev_flags |= PMC_F_DESCENDANTS;
219 ev->ev_cumulative = 1;
222 ev->ev_pmcid = PMC_ID_INVALID;
223 STAILQ_INSERT_TAIL(&pmc_args.pa_events, ev, ev_next);
225 ev->ev_cpu = CPU_FFS(&cpumask) - 1;
226 CPU_CLR(ev->ev_cpu, &cpumask);
227 pmcstat_clone_event_descriptor(ev, &cpumask, &pmc_args);
228 CPU_SET(ev->ev_cpu, &cpumask);
230 ev->ev_cpu = PMC_CPU_ANY;
233 if (clock_gettime(CLOCK_MONOTONIC, &before_ts))
234 err(1, "clock_gettime");
238 pmc_stat_print_stat(struct rusage *ru)
240 struct pmcstat_ev *ev;
241 struct timespec after;
242 uint64_t cvals[STAT_MODE_NPMCS];
243 uint64_t ticks, value;
247 ticks = hz * (ru->ru_utime.tv_sec + ru->ru_stime.tv_sec) +
248 hz * (ru->ru_utime.tv_usec + ru->ru_stime.tv_usec) / 1000000;
249 if (clock_gettime(CLOCK_MONOTONIC, &after))
250 err(1, "clock_gettime");
251 bzero(&cvals, sizeof(cvals));
252 STAILQ_FOREACH(ev, &pmc_args.pa_events, ev_next) {
253 if (pmc_read(ev->ev_pmcid, &value) < 0)
254 err(EX_OSERR, "ERROR: Cannot read pmc \"%s\"",
256 for (i = 0; i < STAT_MODE_NPMCS; i++)
257 if (strcmp(ev->ev_name, stat_mode_cntrs[i]) == 0)
262 * If our round-off on the tick calculation still puts us at 0,
263 * then always assume at least one tick.
267 fprintf(pmc_args.pa_printfile, "%16ld %s\t\t#\t%02.03f M/sec\n",
268 ru->ru_minflt, "page faults", ((double)ru->ru_minflt / (double)ticks) / hz);
269 fprintf(pmc_args.pa_printfile, "%16ld %s\t\t#\t%02.03f M/sec\n",
270 ru->ru_nvcsw, "voluntary csw", ((double)ru->ru_nvcsw / (double)ticks) / hz);
271 fprintf(pmc_args.pa_printfile, "%16ld %s\t#\t%02.03f M/sec\n",
272 ru->ru_nivcsw, "involuntary csw", ((double)ru->ru_nivcsw / (double)ticks) / hz);
274 fprintf(pmc_args.pa_printfile, "%16ld %s\n", cvals[CYCLES], stat_mode_names[CYCLES]);
275 fprintf(pmc_args.pa_printfile, "%16ld %s\t\t#\t%01.03f inst/cycle\n", cvals[INST], stat_mode_names[INST],
276 (double)cvals[INST] / cvals[CYCLES]);
277 fprintf(pmc_args.pa_printfile, "%16ld %s\n", cvals[BR], stat_mode_names[BR]);
278 if (stat_mode_names[BR_MISS] == pmc_stat_mode_names[BR_MISS])
279 fprintf(pmc_args.pa_printfile, "%16ld %s\t\t#\t%.03f%%\n",
280 cvals[BR_MISS], stat_mode_names[BR_MISS],
281 100 * ((double)cvals[BR_MISS] / cvals[BR]));
283 fprintf(pmc_args.pa_printfile, "%16ld %s\n",
284 cvals[BR_MISS], stat_mode_names[BR_MISS]);
285 fprintf(pmc_args.pa_printfile, "%16ld %s%s", cvals[CACHE], stat_mode_names[CACHE],
286 stat_mode_names[CACHE] != pmc_stat_mode_names[CACHE] ? "\n" : "");
287 if (stat_mode_names[CACHE] == pmc_stat_mode_names[CACHE])
288 fprintf(pmc_args.pa_printfile, "\t#\t%.03f refs/inst\n",
289 ((double)cvals[CACHE] / cvals[INST]));
290 fprintf(pmc_args.pa_printfile, "%16ld %s%s", cvals[CACHE_MISS], stat_mode_names[CACHE_MISS],
291 stat_mode_names[CACHE_MISS] != pmc_stat_mode_names[CACHE_MISS] ? "\n" : "");
292 if (stat_mode_names[CACHE_MISS] == pmc_stat_mode_names[CACHE_MISS])
293 fprintf(pmc_args.pa_printfile, "\t\t#\t%.03f%%\n",
294 100 * ((double)cvals[CACHE_MISS] / cvals[CACHE]));
297 showtime(pmc_args.pa_printfile, &before_ts, &after, ru);
301 static struct option longopts[] = {
302 {"events", required_argument, NULL, 'j'},
307 pmc_stat_internal(int argc, char **argv, int system_mode)
314 struct pmcstat_ev *ev;
315 int c, option, runstate, do_print, do_read;
316 int waitstatus, ru_valid;
318 ru_valid = do_print = do_read = 0;
320 while ((option = getopt_long(argc, argv, "j:", longopts, NULL)) != -1) {
323 event = strdup(optarg);
330 pmc_args.pa_argc = (argc -= optind);
331 pmc_args.pa_argv = (argv += optind);
334 pmc_args.pa_flags |= FLAG_HAS_COMMANDLINE;
335 pmc_stat_setup_stat(system_mode, event);
337 EV_SET(&kev, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
338 if (kevent(pmc_kq, &kev, 1, NULL, 0, NULL) < 0)
339 err(EX_OSERR, "ERROR: Cannot register kevent for SIGINT");
341 EV_SET(&kev, SIGIO, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
342 if (kevent(pmc_kq, &kev, 1, NULL, 0, NULL) < 0)
343 err(EX_OSERR, "ERROR: Cannot register kevent for SIGIO");
345 STAILQ_FOREACH(ev, &pmc_args.pa_events, ev_next) {
346 if (pmc_allocate(ev->ev_spec, ev->ev_mode,
347 ev->ev_flags, ev->ev_cpu, &ev->ev_pmcid) < 0)
349 "ERROR: Cannot allocate %s-mode pmc with specification \"%s\"",
350 PMC_IS_SYSTEM_MODE(ev->ev_mode) ?
351 "system" : "process", ev->ev_spec);
353 if (PMC_IS_SAMPLING_MODE(ev->ev_mode) &&
354 pmc_set(ev->ev_pmcid, ev->ev_count) < 0)
356 "ERROR: Cannot set sampling count for PMC \"%s\"",
361 * An exec() failure of a forked child is signalled by the
362 * child sending the parent a SIGCHLD. We don't register an
363 * actual signal handler for SIGCHLD, but instead use our
364 * kqueue to pick up the signal.
366 EV_SET(&kev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
367 if (kevent(pmc_kq, &kev, 1, NULL, 0, NULL) < 0)
368 err(EX_OSERR, "ERROR: Cannot register kevent for SIGCHLD");
370 pmcstat_create_process(pmcstat_sockpair, &pmc_args, pmc_kq);
372 if (SLIST_EMPTY(&pmc_args.pa_targets))
374 "ERROR: No matching target processes.");
375 if (pmc_args.pa_flags & FLAG_HAS_PROCESS_PMCS)
376 pmcstat_attach_pmcs(&pmc_args);
379 pmc_util_start_pmcs(&pmc_args);
381 /* start the (commandline) process if needed */
382 pmcstat_start_process(pmcstat_sockpair);
384 /* Handle SIGINT using the kqueue loop */
385 sa.sa_handler = SIG_IGN;
387 (void)sigemptyset(&sa.sa_mask);
389 if (sigaction(SIGINT, &sa, NULL) < 0)
390 err(EX_OSERR, "ERROR: Cannot install signal handler");
393 * loop till either the target process (if any) exits, or we
394 * are killed by a SIGINT or we reached the time duration.
396 runstate = PMCSTAT_RUNNING;
397 do_print = do_read = 0;
399 if ((c = kevent(pmc_kq, NULL, 0, &kev, 1, NULL)) <= 0) {
401 err(EX_OSERR, "ERROR: kevent failed");
405 if (kev.flags & EV_ERROR)
406 errc(EX_OSERR, kev.data, "ERROR: kevent failed");
408 switch (kev.filter) {
409 case EVFILT_PROC: /* target has exited */
410 if (wait4(pmc_util_get_pid(&pmc_args), &waitstatus, 0, &ru) > 0) {
411 getrusage(RUSAGE_CHILDREN, &ru);
417 case EVFILT_READ: /* log file data is present */
422 if (kev.ident == SIGCHLD) {
424 * The child process sends us a
425 * SIGCHLD if its exec() failed. We
426 * wait for it to exit and then exit
430 runstate = PMCSTAT_FINISHED;
431 } else if (kev.ident == SIGIO) {
433 * We get a SIGIO if a PMC loses all
434 * of its targets, or if logfile
435 * writes encounter an error.
437 if (wait4(pmc_util_get_pid(&pmc_args), &waitstatus, 0, &ru) > 0) {
438 getrusage(RUSAGE_CHILDREN, &ru);
441 runstate = pmcstat_close_log(&pmc_args);
442 do_print = 1; /* print PMCs at exit */
443 } else if (kev.ident == SIGINT) {
444 /* Kill the child process if we started it */
445 if (pmc_args.pa_flags & FLAG_HAS_COMMANDLINE)
446 pmc_util_kill_process(&pmc_args);
447 runstate = pmcstat_close_log(&pmc_args);
448 } else if (kev.ident == SIGWINCH) {
449 if (ioctl(fileno(pmc_args.pa_printfile),
450 TIOCGWINSZ, &ws) < 0)
452 "ERROR: Cannot determine window size");
453 pmc_displayheight = ws.ws_row - 1;
454 pmc_displaywidth = ws.ws_col - 1;
460 } while (runstate != PMCSTAT_FINISHED);
462 warnx("couldn't get rusage");
463 pmc_stat_print_stat(&ru);
464 pmc_util_cleanup(&pmc_args);
469 cmd_pmc_stat(int argc, char **argv)
471 return (pmc_stat_internal(argc, argv, 0));
475 cmd_pmc_stat_system(int argc, char **argv)
477 return (pmc_stat_internal(argc, argv, 1));