xref: /freebsd/usr.sbin/pmcstat/pmcstat_log.c (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
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_PROC_CREATE:
456 			PMCSTAT_PRINT_ENTRY("create","%d %x \"%s\"",
457 			    ev.pl_u.pl_pc.pl_pid,
458 			    ev.pl_u.pl_pc.pl_flags,
459 			    ev.pl_u.pl_pc.pl_pcomm);
460 			break;
461 		case PMCLOG_TYPE_PROCEXEC:
462 			PMCSTAT_PRINT_ENTRY("exec","0x%x %d %p \"%s\"",
463 			    ev.pl_u.pl_x.pl_pmcid,
464 			    ev.pl_u.pl_x.pl_pid,
465 			    (void *) ev.pl_u.pl_x.pl_entryaddr,
466 			    ev.pl_u.pl_x.pl_pathname);
467 			break;
468 		case PMCLOG_TYPE_PROCEXIT:
469 			PMCSTAT_PRINT_ENTRY("exitval","0x%x %d %jd",
470 			    ev.pl_u.pl_e.pl_pmcid,
471 			    ev.pl_u.pl_e.pl_pid,
472 			    ev.pl_u.pl_e.pl_value);
473 			break;
474 		case PMCLOG_TYPE_PROCFORK:
475 			PMCSTAT_PRINT_ENTRY("fork","%d %d",
476 			    ev.pl_u.pl_f.pl_oldpid,
477 			    ev.pl_u.pl_f.pl_newpid);
478 			break;
479 		case PMCLOG_TYPE_USERDATA:
480 			PMCSTAT_PRINT_ENTRY("userdata","0x%x",
481 			    ev.pl_u.pl_u.pl_userdata);
482 			break;
483 		case PMCLOG_TYPE_SYSEXIT:
484 			PMCSTAT_PRINT_ENTRY("exit","%d",
485 			    ev.pl_u.pl_se.pl_pid);
486 			break;
487 		case PMCLOG_TYPE_THR_CREATE:
488 			PMCSTAT_PRINT_ENTRY("thr-create","%d %d %x \"%s\"",
489 			    ev.pl_u.pl_tc.pl_tid,
490 			    ev.pl_u.pl_tc.pl_pid,
491 			    ev.pl_u.pl_tc.pl_flags,
492 			    ev.pl_u.pl_tc.pl_tdname);
493 			break;
494 		case PMCLOG_TYPE_THR_EXIT:
495 			PMCSTAT_PRINT_ENTRY("thr-exit","%d",
496 			    ev.pl_u.pl_tc.pl_tid);
497 			break;
498 		default:
499 			fprintf(args.pa_printfile, "unknown event (type %d).\n",
500 			    ev.pl_type);
501 		}
502 	}
503 
504 	if (ev.pl_state == PMCLOG_EOF)
505 		return (PMCSTAT_FINISHED);
506 	else if (ev.pl_state == PMCLOG_REQUIRE_DATA)
507 		return (PMCSTAT_RUNNING);
508 
509 	errx(EX_DATAERR,
510 	    "ERROR: event parsing failed (record %jd, offset 0x%jx).",
511 	    (uintmax_t) ev.pl_count + 1, ev.pl_offset);
512 	/*NOTREACHED*/
513 }
514 
515 /*
516  * Public Interfaces.
517  */
518 
519 /*
520  * Process a log file in offline analysis mode.
521  */
522 
523 int
524 pmcstat_process_log(void)
525 {
526 
527 	/*
528 	 * If analysis has not been asked for, just print the log to
529 	 * the current output file.
530 	 */
531 	if (args.pa_flags & FLAG_DO_PRINT)
532 		return (pmcstat_print_log());
533 	else
534 		return (pmcstat_analyze_log(&args, plugins, &pmcstat_stats, pmcstat_kernproc,
535 		    pmcstat_mergepmc, &pmcstat_npmcs, &ps_samples_period));
536 }
537 
538 /*
539  * Refresh top display.
540  */
541 
542 static void
543 pmcstat_refresh_top(void)
544 {
545 	int v_attrs;
546 	float v;
547 	char pmcname[40];
548 	struct pmcstat_pmcrecord *pmcpr;
549 
550 	/* If in pause mode do not refresh display. */
551 	if (pmcstat_pause)
552 		return;
553 
554 	/* Wait until PMC pop in the log. */
555 	pmcpr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
556 	if (pmcpr == NULL)
557 		return;
558 
559 	/* Format PMC name. */
560 	if (pmcstat_mergepmc)
561 		snprintf(pmcname, sizeof(pmcname), "[%s]",
562 		    pmcstat_string_unintern(pmcpr->pr_pmcname));
563 	else
564 		snprintf(pmcname, sizeof(pmcname), "%s.%d",
565 		    pmcstat_string_unintern(pmcpr->pr_pmcname),
566 		    pmcstat_pmcinfilter);
567 
568 	/* Format samples count. */
569 	if (ps_samples_period > 0)
570 		v = (pmcpr->pr_samples * 100.0) / ps_samples_period;
571 	else
572 		v = 0.;
573 	v_attrs = PMCSTAT_ATTRPERCENT(v);
574 
575 	PMCSTAT_PRINTBEGIN();
576 	PMCSTAT_PRINTW("PMC: %s Samples: %u ",
577 	    pmcname,
578 	    pmcpr->pr_samples);
579 	PMCSTAT_ATTRON(v_attrs);
580 	PMCSTAT_PRINTW("(%.1f%%) ", v);
581 	PMCSTAT_ATTROFF(v_attrs);
582 	PMCSTAT_PRINTW(", %u unresolved\n\n",
583 	    pmcpr->pr_dubious_frames);
584 	if (plugins[args.pa_plugin].pl_topdisplay != NULL)
585 		plugins[args.pa_plugin].pl_topdisplay();
586 	PMCSTAT_PRINTEND();
587 }
588 
589 /*
590  * Find the next pmc index to display.
591  */
592 
593 static void
594 pmcstat_changefilter(void)
595 {
596 	int pmcin;
597 	struct pmcstat_pmcrecord *pmcr;
598 
599 	/*
600 	 * Find the next merge target.
601 	 */
602 	if (pmcstat_mergepmc) {
603 		pmcin = pmcstat_pmcinfilter;
604 
605 		do {
606 			pmcr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
607 			if (pmcr == NULL || pmcr == pmcr->pr_merge)
608 				break;
609 
610 			pmcstat_pmcinfilter++;
611 			if (pmcstat_pmcinfilter >= pmcstat_npmcs)
612 				pmcstat_pmcinfilter = 0;
613 
614 		} while (pmcstat_pmcinfilter != pmcin);
615 	}
616 }
617 
618 /*
619  * Top mode keypress.
620  */
621 
622 int
623 pmcstat_keypress_log(void)
624 {
625 	int c, ret = 0;
626 	WINDOW *w;
627 
628 	w = newwin(1, 0, 1, 0);
629 	c = wgetch(w);
630 	wprintw(w, "Key: %c => ", c);
631 	switch (c) {
632 	case 'A':
633 		if (args.pa_flags & FLAG_SKIP_TOP_FN_RES)
634 			args.pa_flags &= ~FLAG_SKIP_TOP_FN_RES;
635 		else
636 			args.pa_flags |= FLAG_SKIP_TOP_FN_RES;
637 		break;
638 	case 'c':
639 		wprintw(w, "enter mode 'd' or 'a' => ");
640 		c = wgetch(w);
641 		if (c == 'd') {
642 			args.pa_topmode = PMCSTAT_TOP_DELTA;
643 			wprintw(w, "switching to delta mode");
644 		} else {
645 			args.pa_topmode = PMCSTAT_TOP_ACCUM;
646 			wprintw(w, "switching to accumulation mode");
647 		}
648 		break;
649 	case 'I':
650 		if (args.pa_flags & FLAG_SHOW_OFFSET)
651 			args.pa_flags &= ~FLAG_SHOW_OFFSET;
652 		else
653 			args.pa_flags |= FLAG_SHOW_OFFSET;
654 		break;
655 	case 'm':
656 		pmcstat_mergepmc = !pmcstat_mergepmc;
657 		/*
658 		 * Changing merge state require data reset.
659 		 */
660 		if (plugins[args.pa_plugin].pl_shutdown != NULL)
661 			plugins[args.pa_plugin].pl_shutdown(NULL);
662 		pmcstat_stats_reset(0);
663 		if (plugins[args.pa_plugin].pl_init != NULL)
664 			plugins[args.pa_plugin].pl_init();
665 
666 		/* Update filter to be on a merge target. */
667 		pmcstat_changefilter();
668 		wprintw(w, "merge PMC %s", pmcstat_mergepmc ? "on" : "off");
669 		break;
670 	case 'n':
671 		/* Close current plugin. */
672 		if (plugins[args.pa_plugin].pl_shutdown != NULL)
673 			plugins[args.pa_plugin].pl_shutdown(NULL);
674 
675 		/* Find next top display available. */
676 		do {
677 			args.pa_plugin++;
678 			if (plugins[args.pa_plugin].pl_name == NULL)
679 				args.pa_plugin = 0;
680 		} while (plugins[args.pa_plugin].pl_topdisplay == NULL);
681 
682 		/* Open new plugin. */
683 		pmcstat_stats_reset(0);
684 		if (plugins[args.pa_plugin].pl_init != NULL)
685 			plugins[args.pa_plugin].pl_init();
686 		wprintw(w, "switching to plugin %s",
687 		    plugins[args.pa_plugin].pl_name);
688 		break;
689 	case 'p':
690 		pmcstat_pmcinfilter++;
691 		if (pmcstat_pmcinfilter >= pmcstat_npmcs)
692 			pmcstat_pmcinfilter = 0;
693 		pmcstat_changefilter();
694 		wprintw(w, "switching to PMC %s.%d",
695 		    pmcstat_pmcindex_to_name(pmcstat_pmcinfilter),
696 		    pmcstat_pmcinfilter);
697 		break;
698 	case ' ':
699 		pmcstat_pause = !pmcstat_pause;
700 		if (pmcstat_pause)
701 			wprintw(w, "pause => press space again to continue");
702 		break;
703 	case 'q':
704 		wprintw(w, "exiting...");
705 		ret = 1;
706 		break;
707 	default:
708 		if (plugins[args.pa_plugin].pl_topkeypress != NULL)
709 			if (plugins[args.pa_plugin].pl_topkeypress(c, (void *)w))
710 				ret = 1;
711 	}
712 
713 	wrefresh(w);
714 	delwin(w);
715 	return ret;
716 }
717 
718 
719 /*
720  * Top mode display.
721  */
722 
723 void
724 pmcstat_display_log(void)
725 {
726 
727 	pmcstat_refresh_top();
728 
729 	/* Reset everythings if delta mode. */
730 	if (args.pa_topmode == PMCSTAT_TOP_DELTA) {
731 		if (plugins[args.pa_plugin].pl_shutdown != NULL)
732 			plugins[args.pa_plugin].pl_shutdown(NULL);
733 		pmcstat_stats_reset(0);
734 		if (plugins[args.pa_plugin].pl_init != NULL)
735 			plugins[args.pa_plugin].pl_init();
736 	}
737 }
738 
739 /*
740  * Configure a plugins.
741  */
742 
743 void
744 pmcstat_pluginconfigure_log(char *opt)
745 {
746 
747 	if (strncmp(opt, "threshold=", 10) == 0) {
748 		pmcstat_threshold = atof(opt+10);
749 	} else {
750 		if (plugins[args.pa_plugin].pl_configure != NULL) {
751 			if (!plugins[args.pa_plugin].pl_configure(opt))
752 				err(EX_USAGE,
753 				    "ERROR: unknown option <%s>.", opt);
754 		}
755 	}
756 }
757 
758 void
759 pmcstat_log_shutdown_logging(void)
760 {
761 
762 	pmcstat_shutdown_logging(&args, plugins, &pmcstat_stats);
763 }
764 
765 void
766 pmcstat_log_initialize_logging(void)
767 {
768 
769 	pmcstat_initialize_logging(&pmcstat_kernproc,
770 	    &args, plugins, &pmcstat_npmcs, &pmcstat_mergepmc);
771 }
772