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