]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/pmcstat/pmcstat_log.c
Update to bmake-201802222
[FreeBSD/FreeBSD.git] / usr.sbin / pmcstat / pmcstat_log.c
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 && args.pa_verbosity > 0)
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_PCSAMPLE:
427                         PMCSTAT_PRINT_ENTRY("sample","0x%x %d %p %c",
428                             ev.pl_u.pl_s.pl_pmcid,
429                             ev.pl_u.pl_s.pl_pid,
430                             (void *) ev.pl_u.pl_s.pl_pc,
431                             ev.pl_u.pl_s.pl_usermode ? 'u' : 's');
432                         break;
433                 case PMCLOG_TYPE_PMCALLOCATE:
434                         PMCSTAT_PRINT_ENTRY("allocate","0x%x \"%s\" 0x%x",
435                             ev.pl_u.pl_a.pl_pmcid,
436                             ev.pl_u.pl_a.pl_evname,
437                             ev.pl_u.pl_a.pl_flags);
438                         break;
439                 case PMCLOG_TYPE_PMCALLOCATEDYN:
440                         PMCSTAT_PRINT_ENTRY("allocatedyn","0x%x \"%s\" 0x%x",
441                             ev.pl_u.pl_ad.pl_pmcid,
442                             ev.pl_u.pl_ad.pl_evname,
443                             ev.pl_u.pl_ad.pl_flags);
444                         break;
445                 case PMCLOG_TYPE_PMCATTACH:
446                         PMCSTAT_PRINT_ENTRY("attach","0x%x %d \"%s\"",
447                             ev.pl_u.pl_t.pl_pmcid,
448                             ev.pl_u.pl_t.pl_pid,
449                             ev.pl_u.pl_t.pl_pathname);
450                         break;
451                 case PMCLOG_TYPE_PMCDETACH:
452                         PMCSTAT_PRINT_ENTRY("detach","0x%x %d",
453                             ev.pl_u.pl_d.pl_pmcid,
454                             ev.pl_u.pl_d.pl_pid);
455                         break;
456                 case PMCLOG_TYPE_PROCCSW:
457                         PMCSTAT_PRINT_ENTRY("cswval","0x%x %d %jd",
458                             ev.pl_u.pl_c.pl_pmcid,
459                             ev.pl_u.pl_c.pl_pid,
460                             ev.pl_u.pl_c.pl_value);
461                         break;
462                 case PMCLOG_TYPE_PROCEXEC:
463                         PMCSTAT_PRINT_ENTRY("exec","0x%x %d %p \"%s\"",
464                             ev.pl_u.pl_x.pl_pmcid,
465                             ev.pl_u.pl_x.pl_pid,
466                             (void *) ev.pl_u.pl_x.pl_entryaddr,
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                 default:
489                         fprintf(args.pa_printfile, "unknown event (type %d).\n",
490                             ev.pl_type);
491                 }
492         }
493
494         if (ev.pl_state == PMCLOG_EOF)
495                 return (PMCSTAT_FINISHED);
496         else if (ev.pl_state == PMCLOG_REQUIRE_DATA)
497                 return (PMCSTAT_RUNNING);
498
499         errx(EX_DATAERR,
500             "ERROR: event parsing failed (record %jd, offset 0x%jx).",
501             (uintmax_t) ev.pl_count + 1, ev.pl_offset);
502         /*NOTREACHED*/
503 }
504
505 /*
506  * Public Interfaces.
507  */
508
509 /*
510  * Process a log file in offline analysis mode.
511  */
512
513 int
514 pmcstat_process_log(void)
515 {
516
517         /*
518          * If analysis has not been asked for, just print the log to
519          * the current output file.
520          */
521         if (args.pa_flags & FLAG_DO_PRINT)
522                 return (pmcstat_print_log());
523         else
524                 return (pmcstat_analyze_log(&args, plugins, &pmcstat_stats, pmcstat_kernproc,
525                     pmcstat_mergepmc, &pmcstat_npmcs, &ps_samples_period));
526 }
527
528 /*
529  * Refresh top display.
530  */
531
532 static void
533 pmcstat_refresh_top(void)
534 {
535         int v_attrs;
536         float v;
537         char pmcname[40];
538         struct pmcstat_pmcrecord *pmcpr;
539
540         /* If in pause mode do not refresh display. */
541         if (pmcstat_pause)
542                 return;
543
544         /* Wait until PMC pop in the log. */
545         pmcpr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
546         if (pmcpr == NULL)
547                 return;
548
549         /* Format PMC name. */
550         if (pmcstat_mergepmc)
551                 snprintf(pmcname, sizeof(pmcname), "[%s]",
552                     pmcstat_string_unintern(pmcpr->pr_pmcname));
553         else
554                 snprintf(pmcname, sizeof(pmcname), "%s.%d",
555                     pmcstat_string_unintern(pmcpr->pr_pmcname),
556                     pmcstat_pmcinfilter);
557
558         /* Format samples count. */
559         if (ps_samples_period > 0)
560                 v = (pmcpr->pr_samples * 100.0) / ps_samples_period;
561         else
562                 v = 0.;
563         v_attrs = PMCSTAT_ATTRPERCENT(v);
564
565         PMCSTAT_PRINTBEGIN();
566         PMCSTAT_PRINTW("PMC: %s Samples: %u ",
567             pmcname,
568             pmcpr->pr_samples);
569         PMCSTAT_ATTRON(v_attrs);
570         PMCSTAT_PRINTW("(%.1f%%) ", v);
571         PMCSTAT_ATTROFF(v_attrs);
572         PMCSTAT_PRINTW(", %u unresolved\n\n",
573             pmcpr->pr_dubious_frames);
574         if (plugins[args.pa_plugin].pl_topdisplay != NULL)
575                 plugins[args.pa_plugin].pl_topdisplay();
576         PMCSTAT_PRINTEND();
577 }
578
579 /*
580  * Find the next pmc index to display.
581  */
582
583 static void
584 pmcstat_changefilter(void)
585 {
586         int pmcin;
587         struct pmcstat_pmcrecord *pmcr;
588
589         /*
590          * Find the next merge target.
591          */
592         if (pmcstat_mergepmc) {
593                 pmcin = pmcstat_pmcinfilter;
594
595                 do {
596                         pmcr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
597                         if (pmcr == NULL || pmcr == pmcr->pr_merge)
598                                 break;
599
600                         pmcstat_pmcinfilter++;
601                         if (pmcstat_pmcinfilter >= pmcstat_npmcs)
602                                 pmcstat_pmcinfilter = 0;
603
604                 } while (pmcstat_pmcinfilter != pmcin);
605         }
606 }
607
608 /*
609  * Top mode keypress.
610  */
611
612 int
613 pmcstat_keypress_log(void)
614 {
615         int c, ret = 0;
616         WINDOW *w;
617
618         w = newwin(1, 0, 1, 0);
619         c = wgetch(w);
620         wprintw(w, "Key: %c => ", c);
621         switch (c) {
622         case 'c':
623                 wprintw(w, "enter mode 'd' or 'a' => ");
624                 c = wgetch(w);
625                 if (c == 'd') {
626                         args.pa_topmode = PMCSTAT_TOP_DELTA;
627                         wprintw(w, "switching to delta mode");
628                 } else {
629                         args.pa_topmode = PMCSTAT_TOP_ACCUM;
630                         wprintw(w, "switching to accumulation mode");
631                 }
632                 break;
633         case 'm':
634                 pmcstat_mergepmc = !pmcstat_mergepmc;
635                 /*
636                  * Changing merge state require data reset.
637                  */
638                 if (plugins[args.pa_plugin].pl_shutdown != NULL)
639                         plugins[args.pa_plugin].pl_shutdown(NULL);
640                 pmcstat_stats_reset(0);
641                 if (plugins[args.pa_plugin].pl_init != NULL)
642                         plugins[args.pa_plugin].pl_init();
643
644                 /* Update filter to be on a merge target. */
645                 pmcstat_changefilter();
646                 wprintw(w, "merge PMC %s", pmcstat_mergepmc ? "on" : "off");
647                 break;
648         case 'n':
649                 /* Close current plugin. */
650                 if (plugins[args.pa_plugin].pl_shutdown != NULL)
651                         plugins[args.pa_plugin].pl_shutdown(NULL);
652
653                 /* Find next top display available. */
654                 do {
655                         args.pa_plugin++;
656                         if (plugins[args.pa_plugin].pl_name == NULL)
657                                 args.pa_plugin = 0;
658                 } while (plugins[args.pa_plugin].pl_topdisplay == NULL);
659
660                 /* Open new plugin. */
661                 pmcstat_stats_reset(0);
662                 if (plugins[args.pa_plugin].pl_init != NULL)
663                         plugins[args.pa_plugin].pl_init();
664                 wprintw(w, "switching to plugin %s",
665                     plugins[args.pa_plugin].pl_name);
666                 break;
667         case 'p':
668                 pmcstat_pmcinfilter++;
669                 if (pmcstat_pmcinfilter >= pmcstat_npmcs)
670                         pmcstat_pmcinfilter = 0;
671                 pmcstat_changefilter();
672                 wprintw(w, "switching to PMC %s.%d",
673                     pmcstat_pmcindex_to_name(pmcstat_pmcinfilter),
674                     pmcstat_pmcinfilter);
675                 break;
676         case ' ':
677                 pmcstat_pause = !pmcstat_pause;
678                 if (pmcstat_pause)
679                         wprintw(w, "pause => press space again to continue");
680                 break;
681         case 'q':
682                 wprintw(w, "exiting...");
683                 ret = 1;
684                 break;
685         default:
686                 if (plugins[args.pa_plugin].pl_topkeypress != NULL)
687                         if (plugins[args.pa_plugin].pl_topkeypress(c, (void *)w))
688                                 ret = 1;
689         }
690
691         wrefresh(w);
692         delwin(w);
693         return ret;
694 }
695
696
697 /*
698  * Top mode display.
699  */
700
701 void
702 pmcstat_display_log(void)
703 {
704
705         pmcstat_refresh_top();
706
707         /* Reset everythings if delta mode. */
708         if (args.pa_topmode == PMCSTAT_TOP_DELTA) {
709                 if (plugins[args.pa_plugin].pl_shutdown != NULL)
710                         plugins[args.pa_plugin].pl_shutdown(NULL);
711                 pmcstat_stats_reset(0);
712                 if (plugins[args.pa_plugin].pl_init != NULL)
713                         plugins[args.pa_plugin].pl_init();
714         }
715 }
716
717 /*
718  * Configure a plugins.
719  */
720
721 void
722 pmcstat_pluginconfigure_log(char *opt)
723 {
724
725         if (strncmp(opt, "threshold=", 10) == 0) {
726                 pmcstat_threshold = atof(opt+10);
727         } else {
728                 if (plugins[args.pa_plugin].pl_configure != NULL) {
729                         if (!plugins[args.pa_plugin].pl_configure(opt))
730                                 err(EX_USAGE,
731                                     "ERROR: unknown option <%s>.", opt);
732                 }
733         }
734 }
735
736 void
737 pmcstat_log_shutdown_logging(void)
738 {
739
740         pmcstat_shutdown_logging(&args, plugins, &pmcstat_stats);
741 }
742
743 void
744 pmcstat_log_initialize_logging(void)
745 {
746
747         pmcstat_initialize_logging(&pmcstat_kernproc,
748             &args, plugins, &pmcstat_npmcs, &pmcstat_mergepmc);
749 }