1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2005-2007, Joseph Koshy
5 * Copyright (c) 2007 The FreeBSD Foundation
6 * All rights reserved.
7 *
8 * Portions of this software were developed by A. Joseph Koshy under
9 * sponsorship from the FreeBSD Foundation and Google, Inc.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * Transform a hwpmc(4) log into human readable form, and into
35 * gprof(1) compatible profiles.
36 */
37
38 #include <sys/cdefs.h>
39 #include <sys/param.h>
40 #include <sys/endian.h>
41 #include <sys/cpuset.h>
42 #include <sys/gmon.h>
43 #include <sys/imgact_aout.h>
44 #include <sys/imgact_elf.h>
45 #include <sys/mman.h>
46 #include <sys/pmc.h>
47 #include <sys/queue.h>
48 #include <sys/socket.h>
49 #include <sys/stat.h>
50 #include <sys/wait.h>
51
52 #include <netinet/in.h>
53
54 #include <assert.h>
55 #include <curses.h>
56 #include <err.h>
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <gelf.h>
60 #include <libgen.h>
61 #include <limits.h>
62 #include <netdb.h>
63 #include <pmc.h>
64 #include <pmclog.h>
65 #include <sysexits.h>
66 #include <stdint.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <unistd.h>
71
72 #include "pmcstat.h"
73 #include "pmcstat_log.h"
74 #include "pmcstat_top.h"
75
76 /*
77 * PUBLIC INTERFACES
78 *
79 * pmcstat_initialize_logging() initialize this module, called first
80 * pmcstat_shutdown_logging() orderly shutdown, called last
81 * pmcstat_open_log() open an eventlog for processing
82 * pmcstat_process_log() print/convert an event log
83 * pmcstat_display_log() top mode display for the log
84 * pmcstat_close_log() finish processing an event log
85 *
86 * IMPLEMENTATION NOTES
87 *
88 * We correlate each 'callchain' or 'sample' entry seen in the event
89 * log back to an executable object in the system. Executable objects
90 * include:
91 * - program executables,
92 * - shared libraries loaded by the runtime loader,
93 * - dlopen()'ed objects loaded by the program,
94 * - the runtime loader itself,
95 * - the kernel and kernel modules.
96 *
97 * Each process that we know about is treated as a set of regions that
98 * map to executable objects. Processes are described by
99 * 'pmcstat_process' structures. Executable objects are tracked by
100 * 'pmcstat_image' structures. The kernel and kernel modules are
101 * common to all processes (they reside at the same virtual addresses
102 * for all processes). Individual processes can have their text
103 * segments and shared libraries loaded at process-specific locations.
104 *
105 * A given executable object can be in use by multiple processes
106 * (e.g., libc.so) and loaded at a different address in each.
107 * pmcstat_pcmap structures track per-image mappings.
108 *
109 * The sample log could have samples from multiple PMCs; we
110 * generate one 'gmon.out' profile per PMC.
111 *
112 * IMPLEMENTATION OF GMON OUTPUT
113 *
114 * Each executable object gets one 'gmon.out' profile, per PMC in
115 * use. Creation of 'gmon.out' profiles is done lazily. The
116 * 'gmon.out' profiles generated for a given sampling PMC are
117 * aggregates of all the samples for that particular executable
118 * object.
119 *
120 * IMPLEMENTATION OF SYSTEM-WIDE CALLGRAPH OUTPUT
121 *
122 * Each active pmcid has its own callgraph structure, described by a
123 * 'struct pmcstat_callgraph'. Given a process id and a list of pc
124 * values, we map each pc value to a tuple (image, symbol), where
125 * 'image' denotes an executable object and 'symbol' is the closest
126 * symbol that precedes the pc value. Each pc value in the list is
127 * also given a 'rank' that reflects its depth in the call stack.
128 */
129
130 struct pmcstat_pmcs pmcstat_pmcs = LIST_HEAD_INITIALIZER(pmcstat_pmcs);
131
132 /*
133 * All image descriptors are kept in a hash table.
134 */
135 struct pmcstat_image_hash_list pmcstat_image_hash[PMCSTAT_NHASH];
136
137 /*
138 * All process descriptors are kept in a hash table.
139 */
140 struct pmcstat_process_hash_list pmcstat_process_hash[PMCSTAT_NHASH];
141
142 struct pmcstat_stats pmcstat_stats; /* statistics */
143 static int ps_samples_period; /* samples count between top refresh. */
144
145 struct pmcstat_process *pmcstat_kernproc; /* kernel 'process' */
146
147 #include "pmcpl_gprof.h"
148 #include "pmcpl_callgraph.h"
149 #include "pmcpl_annotate.h"
150 #include "pmcpl_annotate_cg.h"
151 #include "pmcpl_calltree.h"
152
153 static struct pmc_plugins plugins[] = {
154 {
155 .pl_name = "none",
156 },
157 {
158 .pl_name = "callgraph",
159 .pl_init = pmcpl_cg_init,
160 .pl_shutdown = pmcpl_cg_shutdown,
161 .pl_process = pmcpl_cg_process,
162 .pl_topkeypress = pmcpl_cg_topkeypress,
163 .pl_topdisplay = pmcpl_cg_topdisplay
164 },
165 {
166 .pl_name = "gprof",
167 .pl_shutdown = pmcpl_gmon_shutdown,
168 .pl_process = pmcpl_gmon_process,
169 .pl_initimage = pmcpl_gmon_initimage,
170 .pl_shutdownimage = pmcpl_gmon_shutdownimage,
171 .pl_newpmc = pmcpl_gmon_newpmc
172 },
173 {
174 .pl_name = "annotate",
175 .pl_process = pmcpl_annotate_process
176 },
177 {
178 .pl_name = "calltree",
179 .pl_configure = pmcpl_ct_configure,
180 .pl_init = pmcpl_ct_init,
181 .pl_shutdown = pmcpl_ct_shutdown,
182 .pl_process = pmcpl_ct_process,
183 .pl_topkeypress = pmcpl_ct_topkeypress,
184 .pl_topdisplay = pmcpl_ct_topdisplay
185 },
186 {
187 .pl_name = "annotate_cg",
188 .pl_process = pmcpl_annotate_cg_process
189 },
190
191 {
192 .pl_name = NULL
193 }
194 };
195
196 static int pmcstat_mergepmc;
197
198 int pmcstat_pmcinfilter = 0; /* PMC filter for top mode. */
199 float pmcstat_threshold = 0.5; /* Cost filter for top mode. */
200
201 /*
202 * Prototypes
203 */
204
205 static void pmcstat_stats_reset(int _reset_global);
206
207 /*
208 * PMC count.
209 */
210 int pmcstat_npmcs;
211
212 /*
213 * PMC Top mode pause state.
214 */
215 static int pmcstat_pause;
216
217 static void
pmcstat_stats_reset(int reset_global)218 pmcstat_stats_reset(int reset_global)
219 {
220 struct pmcstat_pmcrecord *pr;
221
222 /* Flush PMCs stats. */
223 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) {
224 pr->pr_samples = 0;
225 pr->pr_dubious_frames = 0;
226 }
227 ps_samples_period = 0;
228
229 /* Flush global stats. */
230 if (reset_global)
231 bzero(&pmcstat_stats, sizeof(struct pmcstat_stats));
232 }
233
234 /*
235 * Resolve file name and line number for the given address.
236 */
237 int
pmcstat_image_addr2line(struct pmcstat_image * image,uintfptr_t addr,char * sourcefile,size_t sourcefile_len,unsigned * sourceline,char * funcname,size_t funcname_len)238 pmcstat_image_addr2line(struct pmcstat_image *image, uintfptr_t addr,
239 char *sourcefile, size_t sourcefile_len, unsigned *sourceline,
240 char *funcname, size_t funcname_len)
241 {
242 static int addr2line_warn = 0;
243
244 char *sep, cmdline[PATH_MAX], imagepath[PATH_MAX];
245 unsigned l;
246 int fd;
247
248 if (image->pi_addr2line == NULL) {
249 /* Try default debug file location. */
250 snprintf(imagepath, sizeof(imagepath),
251 "/usr/lib/debug/%s%s.debug",
252 args.pa_fsroot,
253 pmcstat_string_unintern(image->pi_fullpath));
254 fd = open(imagepath, O_RDONLY);
255 if (fd < 0) {
256 /* Old kernel symbol path. */
257 snprintf(imagepath, sizeof(imagepath), "%s%s.symbols",
258 args.pa_fsroot,
259 pmcstat_string_unintern(image->pi_fullpath));
260 fd = open(imagepath, O_RDONLY);
261 if (fd < 0) {
262 snprintf(imagepath, sizeof(imagepath), "%s%s",
263 args.pa_fsroot,
264 pmcstat_string_unintern(
265 image->pi_fullpath));
266 }
267 }
268 if (fd >= 0)
269 close(fd);
270 /*
271 * New addr2line support recursive inline function with -i
272 * but the format does not add a marker when no more entries
273 * are available.
274 */
275 snprintf(cmdline, sizeof(cmdline), "addr2line -Cfe \"%s\"",
276 imagepath);
277 image->pi_addr2line = popen(cmdline, "r+");
278 if (image->pi_addr2line == NULL) {
279 if (!addr2line_warn) {
280 addr2line_warn = 1;
281 warnx(
282 "WARNING: addr2line is needed for source code information."
283 );
284 }
285 return (0);
286 }
287 }
288
289 if (feof(image->pi_addr2line) || ferror(image->pi_addr2line)) {
290 warnx("WARNING: addr2line pipe error");
291 pclose(image->pi_addr2line);
292 image->pi_addr2line = NULL;
293 return (0);
294 }
295
296 fprintf(image->pi_addr2line, "%p\n", (void *)addr);
297
298 if (fgets(funcname, funcname_len, image->pi_addr2line) == NULL) {
299 warnx("WARNING: addr2line function name read error");
300 return (0);
301 }
302 sep = strchr(funcname, '\n');
303 if (sep != NULL)
304 *sep = '\0';
305
306 if (fgets(sourcefile, sourcefile_len, image->pi_addr2line) == NULL) {
307 warnx("WARNING: addr2line source file read error");
308 return (0);
309 }
310 sep = strchr(sourcefile, ':');
311 if (sep == NULL) {
312 warnx("WARNING: addr2line source line separator missing");
313 return (0);
314 }
315 *sep = '\0';
316 l = atoi(sep+1);
317 if (l == 0)
318 return (0);
319 *sourceline = l;
320 return (1);
321 }
322
323 /*
324 * Given a pmcid in use, find its human-readable name.
325 */
326
327 const char *
pmcstat_pmcid_to_name(pmc_id_t pmcid)328 pmcstat_pmcid_to_name(pmc_id_t pmcid)
329 {
330 struct pmcstat_pmcrecord *pr;
331
332 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
333 if (pr->pr_pmcid == pmcid)
334 return (pmcstat_string_unintern(pr->pr_pmcname));
335
336 return NULL;
337 }
338
339 /*
340 * Convert PMC index to name.
341 */
342
343 const char *
pmcstat_pmcindex_to_name(int pmcin)344 pmcstat_pmcindex_to_name(int pmcin)
345 {
346 struct pmcstat_pmcrecord *pr;
347
348 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
349 if (pr->pr_pmcin == pmcin)
350 return pmcstat_string_unintern(pr->pr_pmcname);
351
352 return NULL;
353 }
354
355 /*
356 * Return PMC record with given index.
357 */
358
359 struct pmcstat_pmcrecord *
pmcstat_pmcindex_to_pmcr(int pmcin)360 pmcstat_pmcindex_to_pmcr(int pmcin)
361 {
362 struct pmcstat_pmcrecord *pr;
363
364 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
365 if (pr->pr_pmcin == pmcin)
366 return pr;
367
368 return NULL;
369 }
370
371 /*
372 * Print log entries as text.
373 */
374
375 static int
pmcstat_print_log(void)376 pmcstat_print_log(void)
377 {
378 struct pmclog_ev ev;
379 uint32_t npc;
380
381 while (pmclog_read(args.pa_logparser, &ev) == 0) {
382 assert(ev.pl_state == PMCLOG_OK);
383 switch (ev.pl_type) {
384 case PMCLOG_TYPE_CALLCHAIN:
385 PMCSTAT_PRINT_ENTRY("callchain",
386 "%d 0x%x %d %d %c", ev.pl_u.pl_cc.pl_pid,
387 ev.pl_u.pl_cc.pl_pmcid,
388 PMC_CALLCHAIN_CPUFLAGS_TO_CPU(ev.pl_u.pl_cc. \
389 pl_cpuflags), ev.pl_u.pl_cc.pl_npc,
390 PMC_CALLCHAIN_CPUFLAGS_TO_USERMODE(ev.pl_u.pl_cc.\
391 pl_cpuflags) ? 'u' : 's');
392 for (npc = 0; npc < ev.pl_u.pl_cc.pl_npc; npc++)
393 PMCSTAT_PRINT_ENTRY("...", "%p",
394 (void *) ev.pl_u.pl_cc.pl_pc[npc]);
395 break;
396 case PMCLOG_TYPE_CLOSELOG:
397 PMCSTAT_PRINT_ENTRY("closelog",);
398 break;
399 case PMCLOG_TYPE_DROPNOTIFY:
400 PMCSTAT_PRINT_ENTRY("drop",);
401 break;
402 case PMCLOG_TYPE_INITIALIZE:
403 PMCSTAT_PRINT_ENTRY("initlog","0x%x \"%s\"",
404 ev.pl_u.pl_i.pl_version,
405 pmc_name_of_cputype(ev.pl_u.pl_i.pl_arch));
406 if ((ev.pl_u.pl_i.pl_version & 0xFF000000) !=
407 PMC_VERSION_MAJOR << 24)
408 warnx(
409 "WARNING: Log version 0x%x != expected version 0x%x.",
410 ev.pl_u.pl_i.pl_version, PMC_VERSION);
411 break;
412 case PMCLOG_TYPE_MAP_IN:
413 PMCSTAT_PRINT_ENTRY("map-in","%d %p \"%s\"",
414 ev.pl_u.pl_mi.pl_pid,
415 (void *) ev.pl_u.pl_mi.pl_start,
416 ev.pl_u.pl_mi.pl_pathname);
417 break;
418 case PMCLOG_TYPE_MAP_OUT:
419 PMCSTAT_PRINT_ENTRY("map-out","%d %p %p",
420 ev.pl_u.pl_mo.pl_pid,
421 (void *) ev.pl_u.pl_mo.pl_start,
422 (void *) ev.pl_u.pl_mo.pl_end);
423 break;
424 case PMCLOG_TYPE_PMCALLOCATE:
425 PMCSTAT_PRINT_ENTRY("allocate","0x%x \"%s\" 0x%x",
426 ev.pl_u.pl_a.pl_pmcid,
427 ev.pl_u.pl_a.pl_evname,
428 ev.pl_u.pl_a.pl_flags);
429 break;
430 case PMCLOG_TYPE_PMCALLOCATEDYN:
431 PMCSTAT_PRINT_ENTRY("allocatedyn","0x%x \"%s\" 0x%x",
432 ev.pl_u.pl_ad.pl_pmcid,
433 ev.pl_u.pl_ad.pl_evname,
434 ev.pl_u.pl_ad.pl_flags);
435 break;
436 case PMCLOG_TYPE_PMCATTACH:
437 PMCSTAT_PRINT_ENTRY("attach","0x%x %d \"%s\"",
438 ev.pl_u.pl_t.pl_pmcid,
439 ev.pl_u.pl_t.pl_pid,
440 ev.pl_u.pl_t.pl_pathname);
441 break;
442 case PMCLOG_TYPE_PMCDETACH:
443 PMCSTAT_PRINT_ENTRY("detach","0x%x %d",
444 ev.pl_u.pl_d.pl_pmcid,
445 ev.pl_u.pl_d.pl_pid);
446 break;
447 case PMCLOG_TYPE_PROCCSW:
448 PMCSTAT_PRINT_ENTRY("cswval","0x%x %d %jd",
449 ev.pl_u.pl_c.pl_pmcid,
450 ev.pl_u.pl_c.pl_pid,
451 ev.pl_u.pl_c.pl_value);
452 break;
453 case PMCLOG_TYPE_PROC_CREATE:
454 PMCSTAT_PRINT_ENTRY("create","%d %x \"%s\"",
455 ev.pl_u.pl_pc.pl_pid,
456 ev.pl_u.pl_pc.pl_flags,
457 ev.pl_u.pl_pc.pl_pcomm);
458 break;
459 case PMCLOG_TYPE_PROCEXEC:
460 PMCSTAT_PRINT_ENTRY("exec","0x%x %d %p %p \"%s\"",
461 ev.pl_u.pl_x.pl_pmcid,
462 ev.pl_u.pl_x.pl_pid,
463 (void *)ev.pl_u.pl_x.pl_baseaddr,
464 (void *)ev.pl_u.pl_x.pl_dynaddr,
465 ev.pl_u.pl_x.pl_pathname);
466 break;
467 case PMCLOG_TYPE_PROCEXIT:
468 PMCSTAT_PRINT_ENTRY("exitval","0x%x %d %jd",
469 ev.pl_u.pl_e.pl_pmcid,
470 ev.pl_u.pl_e.pl_pid,
471 ev.pl_u.pl_e.pl_value);
472 break;
473 case PMCLOG_TYPE_PROCFORK:
474 PMCSTAT_PRINT_ENTRY("fork","%d %d",
475 ev.pl_u.pl_f.pl_oldpid,
476 ev.pl_u.pl_f.pl_newpid);
477 break;
478 case PMCLOG_TYPE_USERDATA:
479 PMCSTAT_PRINT_ENTRY("userdata","0x%x",
480 ev.pl_u.pl_u.pl_userdata);
481 break;
482 case PMCLOG_TYPE_SYSEXIT:
483 PMCSTAT_PRINT_ENTRY("exit","%d",
484 ev.pl_u.pl_se.pl_pid);
485 break;
486 case PMCLOG_TYPE_THR_CREATE:
487 PMCSTAT_PRINT_ENTRY("thr-create","%d %d %x \"%s\"",
488 ev.pl_u.pl_tc.pl_tid,
489 ev.pl_u.pl_tc.pl_pid,
490 ev.pl_u.pl_tc.pl_flags,
491 ev.pl_u.pl_tc.pl_tdname);
492 break;
493 case PMCLOG_TYPE_THR_EXIT:
494 PMCSTAT_PRINT_ENTRY("thr-exit","%d",
495 ev.pl_u.pl_tc.pl_tid);
496 break;
497 default:
498 fprintf(args.pa_printfile, "unknown event (type %d).\n",
499 ev.pl_type);
500 }
501 }
502
503 if (ev.pl_state == PMCLOG_EOF)
504 return (PMCSTAT_FINISHED);
505 else if (ev.pl_state == PMCLOG_REQUIRE_DATA)
506 return (PMCSTAT_RUNNING);
507
508 errx(EX_DATAERR,
509 "ERROR: event parsing failed (record %jd, offset 0x%jx).",
510 (uintmax_t) ev.pl_count + 1, ev.pl_offset);
511 /*NOTREACHED*/
512 }
513
514 /*
515 * Public Interfaces.
516 */
517
518 /*
519 * Process a log file in offline analysis mode.
520 */
521
522 int
pmcstat_process_log(void)523 pmcstat_process_log(void)
524 {
525
526 /*
527 * If analysis has not been asked for, just print the log to
528 * the current output file.
529 */
530 if (args.pa_flags & FLAG_DO_PRINT)
531 return (pmcstat_print_log());
532 else
533 return (pmcstat_analyze_log(&args, plugins, &pmcstat_stats, pmcstat_kernproc,
534 pmcstat_mergepmc, &pmcstat_npmcs, &ps_samples_period));
535 }
536
537 /*
538 * Refresh top display.
539 */
540
541 static void
pmcstat_refresh_top(void)542 pmcstat_refresh_top(void)
543 {
544 int v_attrs;
545 float v;
546 char pmcname[40];
547 struct pmcstat_pmcrecord *pmcpr;
548
549 /* If in pause mode do not refresh display. */
550 if (pmcstat_pause)
551 return;
552
553 /* Wait until PMC pop in the log. */
554 pmcpr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
555 if (pmcpr == NULL)
556 return;
557
558 /* Format PMC name. */
559 if (pmcstat_mergepmc)
560 snprintf(pmcname, sizeof(pmcname), "[%s]",
561 pmcstat_string_unintern(pmcpr->pr_pmcname));
562 else
563 snprintf(pmcname, sizeof(pmcname), "%s.%d",
564 pmcstat_string_unintern(pmcpr->pr_pmcname),
565 pmcstat_pmcinfilter);
566
567 /* Format samples count. */
568 if (ps_samples_period > 0)
569 v = (pmcpr->pr_samples * 100.0) / ps_samples_period;
570 else
571 v = 0.;
572 v_attrs = PMCSTAT_ATTRPERCENT(v);
573
574 PMCSTAT_PRINTBEGIN();
575 PMCSTAT_PRINTW("PMC: %s Samples: %u ",
576 pmcname,
577 pmcpr->pr_samples);
578 PMCSTAT_ATTRON(v_attrs);
579 PMCSTAT_PRINTW("(%.1f%%) ", v);
580 PMCSTAT_ATTROFF(v_attrs);
581 PMCSTAT_PRINTW(", %u unresolved\n\n",
582 pmcpr->pr_dubious_frames);
583 if (plugins[args.pa_plugin].pl_topdisplay != NULL)
584 plugins[args.pa_plugin].pl_topdisplay();
585 PMCSTAT_PRINTEND();
586 }
587
588 /*
589 * Find the next pmc index to display.
590 */
591
592 static void
pmcstat_changefilter(void)593 pmcstat_changefilter(void)
594 {
595 int pmcin;
596 struct pmcstat_pmcrecord *pmcr;
597
598 /*
599 * Find the next merge target.
600 */
601 if (pmcstat_mergepmc) {
602 pmcin = pmcstat_pmcinfilter;
603
604 do {
605 pmcr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
606 if (pmcr == NULL || pmcr == pmcr->pr_merge)
607 break;
608
609 pmcstat_pmcinfilter++;
610 if (pmcstat_pmcinfilter >= pmcstat_npmcs)
611 pmcstat_pmcinfilter = 0;
612
613 } while (pmcstat_pmcinfilter != pmcin);
614 }
615 }
616
617 /*
618 * Top mode keypress.
619 */
620
621 int
pmcstat_keypress_log(void)622 pmcstat_keypress_log(void)
623 {
624 int c, ret = 0;
625 WINDOW *w;
626
627 w = newwin(1, 0, 1, 0);
628 c = wgetch(w);
629 wprintw(w, "Key: %c => ", c);
630 switch (c) {
631 case 'A':
632 if (args.pa_flags & FLAG_SKIP_TOP_FN_RES)
633 args.pa_flags &= ~FLAG_SKIP_TOP_FN_RES;
634 else
635 args.pa_flags |= FLAG_SKIP_TOP_FN_RES;
636 break;
637 case 'c':
638 wprintw(w, "enter mode 'd' or 'a' => ");
639 c = wgetch(w);
640 if (c == 'd') {
641 args.pa_topmode = PMCSTAT_TOP_DELTA;
642 wprintw(w, "switching to delta mode");
643 } else {
644 args.pa_topmode = PMCSTAT_TOP_ACCUM;
645 wprintw(w, "switching to accumulation mode");
646 }
647 break;
648 case 'I':
649 if (args.pa_flags & FLAG_SHOW_OFFSET)
650 args.pa_flags &= ~FLAG_SHOW_OFFSET;
651 else
652 args.pa_flags |= FLAG_SHOW_OFFSET;
653 break;
654 case 'm':
655 pmcstat_mergepmc = !pmcstat_mergepmc;
656 /*
657 * Changing merge state require data reset.
658 */
659 if (plugins[args.pa_plugin].pl_shutdown != NULL)
660 plugins[args.pa_plugin].pl_shutdown(NULL);
661 pmcstat_stats_reset(0);
662 if (plugins[args.pa_plugin].pl_init != NULL)
663 plugins[args.pa_plugin].pl_init();
664
665 /* Update filter to be on a merge target. */
666 pmcstat_changefilter();
667 wprintw(w, "merge PMC %s", pmcstat_mergepmc ? "on" : "off");
668 break;
669 case 'n':
670 /* Close current plugin. */
671 if (plugins[args.pa_plugin].pl_shutdown != NULL)
672 plugins[args.pa_plugin].pl_shutdown(NULL);
673
674 /* Find next top display available. */
675 do {
676 args.pa_plugin++;
677 if (plugins[args.pa_plugin].pl_name == NULL)
678 args.pa_plugin = 0;
679 } while (plugins[args.pa_plugin].pl_topdisplay == NULL);
680
681 /* Open new plugin. */
682 pmcstat_stats_reset(0);
683 if (plugins[args.pa_plugin].pl_init != NULL)
684 plugins[args.pa_plugin].pl_init();
685 wprintw(w, "switching to plugin %s",
686 plugins[args.pa_plugin].pl_name);
687 break;
688 case 'p':
689 pmcstat_pmcinfilter++;
690 if (pmcstat_pmcinfilter >= pmcstat_npmcs)
691 pmcstat_pmcinfilter = 0;
692 pmcstat_changefilter();
693 wprintw(w, "switching to PMC %s.%d",
694 pmcstat_pmcindex_to_name(pmcstat_pmcinfilter),
695 pmcstat_pmcinfilter);
696 break;
697 case ' ':
698 pmcstat_pause = !pmcstat_pause;
699 if (pmcstat_pause)
700 wprintw(w, "pause => press space again to continue");
701 break;
702 case 'q':
703 wprintw(w, "exiting...");
704 ret = 1;
705 break;
706 default:
707 if (plugins[args.pa_plugin].pl_topkeypress != NULL)
708 if (plugins[args.pa_plugin].pl_topkeypress(c, (void *)w))
709 ret = 1;
710 }
711
712 wrefresh(w);
713 delwin(w);
714 return ret;
715 }
716
717
718 /*
719 * Top mode display.
720 */
721
722 void
pmcstat_display_log(void)723 pmcstat_display_log(void)
724 {
725
726 pmcstat_refresh_top();
727
728 /* Reset everything if delta mode. */
729 if (args.pa_topmode == PMCSTAT_TOP_DELTA) {
730 if (plugins[args.pa_plugin].pl_shutdown != NULL)
731 plugins[args.pa_plugin].pl_shutdown(NULL);
732 pmcstat_stats_reset(0);
733 if (plugins[args.pa_plugin].pl_init != NULL)
734 plugins[args.pa_plugin].pl_init();
735 }
736 }
737
738 /*
739 * Configure a plugins.
740 */
741
742 void
pmcstat_pluginconfigure_log(char * opt)743 pmcstat_pluginconfigure_log(char *opt)
744 {
745
746 if (strncmp(opt, "threshold=", 10) == 0) {
747 pmcstat_threshold = atof(opt+10);
748 } else {
749 if (plugins[args.pa_plugin].pl_configure != NULL) {
750 if (!plugins[args.pa_plugin].pl_configure(opt))
751 err(EX_USAGE,
752 "ERROR: unknown option <%s>.", opt);
753 }
754 }
755 }
756
757 void
pmcstat_log_shutdown_logging(void)758 pmcstat_log_shutdown_logging(void)
759 {
760
761 pmcstat_shutdown_logging(&args, plugins, &pmcstat_stats);
762 }
763
764 void
pmcstat_log_initialize_logging(void)765 pmcstat_log_initialize_logging(void)
766 {
767
768 pmcstat_initialize_logging(&pmcstat_kernproc,
769 &args, plugins, &pmcstat_npmcs, &pmcstat_mergepmc);
770 }
771