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