2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2007, Joseph Koshy
5 * Copyright (c) 2007 The FreeBSD Foundation
8 * Portions of this software were developed by A. Joseph Koshy under
9 * sponsorship from the FreeBSD Foundation and Google, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
34 * Transform a hwpmc(4) log into human readable form, and into
35 * gprof(1) compatible profiles.
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/endian.h>
43 #include <sys/cpuset.h>
45 #include <sys/imgact_aout.h>
46 #include <sys/imgact_elf.h>
49 #include <sys/queue.h>
50 #include <sys/socket.h>
54 #include <netinet/in.h>
75 #include "pmcstat_log.h"
76 #include "pmcstat_top.h"
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
88 * IMPLEMENTATION NOTES
90 * We correlate each 'callchain' or 'sample' entry seen in the event
91 * log back to an executable object in the system. Executable objects
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.
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.
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.
111 * The sample log could have samples from multiple PMCs; we
112 * generate one 'gmon.out' profile per PMC.
114 * IMPLEMENTATION OF GMON OUTPUT
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
122 * IMPLEMENTATION OF SYSTEM-WIDE CALLGRAPH OUTPUT
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.
132 struct pmcstat_pmcs pmcstat_pmcs = LIST_HEAD_INITIALIZER(pmcstat_pmcs);
135 * All image descriptors are kept in a hash table.
137 struct pmcstat_image_hash_list pmcstat_image_hash[PMCSTAT_NHASH];
140 * All process descriptors are kept in a hash table.
142 struct pmcstat_process_hash_list pmcstat_process_hash[PMCSTAT_NHASH];
144 struct pmcstat_stats pmcstat_stats; /* statistics */
145 static int ps_samples_period; /* samples count between top refresh. */
147 struct pmcstat_process *pmcstat_kernproc; /* kernel 'process' */
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"
155 static struct pmc_plugins plugins[] = {
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
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
176 .pl_name = "annotate",
177 .pl_process = pmcpl_annotate_process
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
189 .pl_name = "annotate_cg",
190 .pl_process = pmcpl_annotate_cg_process
198 static int pmcstat_mergepmc;
200 int pmcstat_pmcinfilter = 0; /* PMC filter for top mode. */
201 float pmcstat_threshold = 0.5; /* Cost filter for top mode. */
207 static void pmcstat_stats_reset(int _reset_global);
215 * PMC Top mode pause state.
217 static int pmcstat_pause;
220 pmcstat_stats_reset(int reset_global)
222 struct pmcstat_pmcrecord *pr;
224 /* Flush PMCs stats. */
225 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next) {
227 pr->pr_dubious_frames = 0;
229 ps_samples_period = 0;
231 /* Flush global stats. */
233 bzero(&pmcstat_stats, sizeof(struct pmcstat_stats));
237 * Resolve file name and line number for the given address.
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)
244 static int addr2line_warn = 0;
246 char *sep, cmdline[PATH_MAX], imagepath[PATH_MAX];
250 if (image->pi_addr2line == NULL) {
251 /* Try default debug file location. */
252 snprintf(imagepath, sizeof(imagepath),
253 "/usr/lib/debug/%s%s.debug",
255 pmcstat_string_unintern(image->pi_fullpath));
256 fd = open(imagepath, O_RDONLY);
258 /* Old kernel symbol path. */
259 snprintf(imagepath, sizeof(imagepath), "%s%s.symbols",
261 pmcstat_string_unintern(image->pi_fullpath));
262 fd = open(imagepath, O_RDONLY);
264 snprintf(imagepath, sizeof(imagepath), "%s%s",
266 pmcstat_string_unintern(
267 image->pi_fullpath));
273 * New addr2line support recursive inline function with -i
274 * but the format does not add a marker when no more entries
277 snprintf(cmdline, sizeof(cmdline), "addr2line -Cfe \"%s\"",
279 image->pi_addr2line = popen(cmdline, "r+");
280 if (image->pi_addr2line == NULL) {
281 if (!addr2line_warn) {
284 "WARNING: addr2line is needed for source code information."
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;
298 fprintf(image->pi_addr2line, "%p\n", (void *)addr);
300 if (fgets(funcname, funcname_len, image->pi_addr2line) == NULL) {
301 warnx("WARNING: addr2line function name read error");
304 sep = strchr(funcname, '\n');
308 if (fgets(sourcefile, sourcefile_len, image->pi_addr2line) == NULL) {
309 warnx("WARNING: addr2line source file read error");
312 sep = strchr(sourcefile, ':');
314 warnx("WARNING: addr2line source line separator missing");
326 * Given a pmcid in use, find its human-readable name.
330 pmcstat_pmcid_to_name(pmc_id_t pmcid)
332 struct pmcstat_pmcrecord *pr;
334 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
335 if (pr->pr_pmcid == pmcid)
336 return (pmcstat_string_unintern(pr->pr_pmcname));
342 * Convert PMC index to name.
346 pmcstat_pmcindex_to_name(int pmcin)
348 struct pmcstat_pmcrecord *pr;
350 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
351 if (pr->pr_pmcin == pmcin)
352 return pmcstat_string_unintern(pr->pr_pmcname);
358 * Return PMC record with given index.
361 struct pmcstat_pmcrecord *
362 pmcstat_pmcindex_to_pmcr(int pmcin)
364 struct pmcstat_pmcrecord *pr;
366 LIST_FOREACH(pr, &pmcstat_pmcs, pr_next)
367 if (pr->pr_pmcin == pmcin)
374 * Print log entries as text.
378 pmcstat_print_log(void)
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]);
398 case PMCLOG_TYPE_CLOSELOG:
399 PMCSTAT_PRINT_ENTRY("closelog",);
401 case PMCLOG_TYPE_DROPNOTIFY:
402 PMCSTAT_PRINT_ENTRY("drop",);
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)
411 "WARNING: Log version 0x%x != expected version 0x%x.",
412 ev.pl_u.pl_i.pl_version, PMC_VERSION);
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);
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);
426 case PMCLOG_TYPE_PCSAMPLE:
427 PMCSTAT_PRINT_ENTRY("sample","0x%x %d %p %c",
428 ev.pl_u.pl_s.pl_pmcid,
430 (void *) ev.pl_u.pl_s.pl_pc,
431 ev.pl_u.pl_s.pl_usermode ? 'u' : 's');
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);
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);
445 case PMCLOG_TYPE_PMCATTACH:
446 PMCSTAT_PRINT_ENTRY("attach","0x%x %d \"%s\"",
447 ev.pl_u.pl_t.pl_pmcid,
449 ev.pl_u.pl_t.pl_pathname);
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);
456 case PMCLOG_TYPE_PROCCSW:
457 PMCSTAT_PRINT_ENTRY("cswval","0x%x %d %jd",
458 ev.pl_u.pl_c.pl_pmcid,
460 ev.pl_u.pl_c.pl_value);
462 case PMCLOG_TYPE_PROCEXEC:
463 PMCSTAT_PRINT_ENTRY("exec","0x%x %d %p \"%s\"",
464 ev.pl_u.pl_x.pl_pmcid,
466 (void *) ev.pl_u.pl_x.pl_entryaddr,
467 ev.pl_u.pl_x.pl_pathname);
469 case PMCLOG_TYPE_PROCEXIT:
470 PMCSTAT_PRINT_ENTRY("exitval","0x%x %d %jd",
471 ev.pl_u.pl_e.pl_pmcid,
473 ev.pl_u.pl_e.pl_value);
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);
480 case PMCLOG_TYPE_USERDATA:
481 PMCSTAT_PRINT_ENTRY("userdata","0x%x",
482 ev.pl_u.pl_u.pl_userdata);
484 case PMCLOG_TYPE_SYSEXIT:
485 PMCSTAT_PRINT_ENTRY("exit","%d",
486 ev.pl_u.pl_se.pl_pid);
489 fprintf(args.pa_printfile, "unknown event (type %d).\n",
494 if (ev.pl_state == PMCLOG_EOF)
495 return (PMCSTAT_FINISHED);
496 else if (ev.pl_state == PMCLOG_REQUIRE_DATA)
497 return (PMCSTAT_RUNNING);
500 "ERROR: event parsing failed (record %jd, offset 0x%jx).",
501 (uintmax_t) ev.pl_count + 1, ev.pl_offset);
510 * Process a log file in offline analysis mode.
514 pmcstat_process_log(void)
518 * If analysis has not been asked for, just print the log to
519 * the current output file.
521 if (args.pa_flags & FLAG_DO_PRINT)
522 return (pmcstat_print_log());
524 return (pmcstat_analyze_log(&args, plugins, &pmcstat_stats, pmcstat_kernproc,
525 pmcstat_mergepmc, &pmcstat_npmcs, &ps_samples_period));
529 * Refresh top display.
533 pmcstat_refresh_top(void)
538 struct pmcstat_pmcrecord *pmcpr;
540 /* If in pause mode do not refresh display. */
544 /* Wait until PMC pop in the log. */
545 pmcpr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
549 /* Format PMC name. */
550 if (pmcstat_mergepmc)
551 snprintf(pmcname, sizeof(pmcname), "[%s]",
552 pmcstat_string_unintern(pmcpr->pr_pmcname));
554 snprintf(pmcname, sizeof(pmcname), "%s.%d",
555 pmcstat_string_unintern(pmcpr->pr_pmcname),
556 pmcstat_pmcinfilter);
558 /* Format samples count. */
559 if (ps_samples_period > 0)
560 v = (pmcpr->pr_samples * 100.0) / ps_samples_period;
563 v_attrs = PMCSTAT_ATTRPERCENT(v);
565 PMCSTAT_PRINTBEGIN();
566 PMCSTAT_PRINTW("PMC: %s Samples: %u ",
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();
580 * Find the next pmc index to display.
584 pmcstat_changefilter(void)
587 struct pmcstat_pmcrecord *pmcr;
590 * Find the next merge target.
592 if (pmcstat_mergepmc) {
593 pmcin = pmcstat_pmcinfilter;
596 pmcr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
597 if (pmcr == NULL || pmcr == pmcr->pr_merge)
600 pmcstat_pmcinfilter++;
601 if (pmcstat_pmcinfilter >= pmcstat_npmcs)
602 pmcstat_pmcinfilter = 0;
604 } while (pmcstat_pmcinfilter != pmcin);
613 pmcstat_keypress_log(void)
618 w = newwin(1, 0, 1, 0);
620 wprintw(w, "Key: %c => ", c);
623 wprintw(w, "enter mode 'd' or 'a' => ");
626 args.pa_topmode = PMCSTAT_TOP_DELTA;
627 wprintw(w, "switching to delta mode");
629 args.pa_topmode = PMCSTAT_TOP_ACCUM;
630 wprintw(w, "switching to accumulation mode");
634 pmcstat_mergepmc = !pmcstat_mergepmc;
636 * Changing merge state require data reset.
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();
644 /* Update filter to be on a merge target. */
645 pmcstat_changefilter();
646 wprintw(w, "merge PMC %s", pmcstat_mergepmc ? "on" : "off");
649 /* Close current plugin. */
650 if (plugins[args.pa_plugin].pl_shutdown != NULL)
651 plugins[args.pa_plugin].pl_shutdown(NULL);
653 /* Find next top display available. */
656 if (plugins[args.pa_plugin].pl_name == NULL)
658 } while (plugins[args.pa_plugin].pl_topdisplay == NULL);
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);
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);
677 pmcstat_pause = !pmcstat_pause;
679 wprintw(w, "pause => press space again to continue");
682 wprintw(w, "exiting...");
686 if (plugins[args.pa_plugin].pl_topkeypress != NULL)
687 if (plugins[args.pa_plugin].pl_topkeypress(c, (void *)w))
702 pmcstat_display_log(void)
705 pmcstat_refresh_top();
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();
718 * Configure a plugins.
722 pmcstat_pluginconfigure_log(char *opt)
725 if (strncmp(opt, "threshold=", 10) == 0) {
726 pmcstat_threshold = atof(opt+10);
728 if (plugins[args.pa_plugin].pl_configure != NULL) {
729 if (!plugins[args.pa_plugin].pl_configure(opt))
731 "ERROR: unknown option <%s>.", opt);
737 pmcstat_log_shutdown_logging(void)
740 pmcstat_shutdown_logging(&args, plugins, &pmcstat_stats);
744 pmcstat_log_initialize_logging(void)
747 pmcstat_initialize_logging(&pmcstat_kernproc,
748 &args, plugins, &pmcstat_npmcs, &pmcstat_mergepmc);