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