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>
44 #include <sys/imgact_aout.h>
45 #include <sys/imgact_elf.h>
48 #include <sys/queue.h>
49 #include <sys/socket.h>
53 #include <netinet/in.h>
74 #include "pmcstat_log.h"
75 #include "pmcstat_top.h"
76 #include "pmcpl_callgraph.h"
78 /* Get the sample value in percent related to nsamples. */
79 #define PMCPL_CG_COUNTP(a) \
80 ((a)->pcg_count * 100.0 / nsamples)
83 * The toplevel CG nodes (i.e., with rank == 0) are placed in a hash table.
86 struct pmcstat_cgnode_hash_list pmcstat_cgnode_hash[PMCSTAT_NHASH];
87 int pmcstat_cgnode_hash_count;
89 static pmcstat_interned_string pmcstat_previous_filename_printed;
91 static struct pmcstat_cgnode *
92 pmcstat_cgnode_allocate(struct pmcstat_image *image, uintfptr_t pc)
94 struct pmcstat_cgnode *cg;
96 if ((cg = malloc(sizeof(*cg))) == NULL)
97 err(EX_OSERR, "ERROR: Cannot allocate callgraph node");
99 cg->pcg_image = image;
103 cg->pcg_nchildren = 0;
104 LIST_INIT(&cg->pcg_children);
110 * Free a node and its children.
113 pmcstat_cgnode_free(struct pmcstat_cgnode *cg)
115 struct pmcstat_cgnode *cgc, *cgtmp;
117 LIST_FOREACH_SAFE(cgc, &cg->pcg_children, pcg_sibling, cgtmp)
118 pmcstat_cgnode_free(cgc);
123 * Look for a callgraph node associated with pmc `pmcid' in the global
124 * hash table that corresponds to the given `pc' value in the process
127 static struct pmcstat_cgnode *
128 pmcstat_cgnode_hash_lookup_pc(struct pmcstat_process *pp, pmc_id_t pmcid,
129 uintfptr_t pc, int usermode)
131 struct pmcstat_pcmap *ppm;
132 struct pmcstat_symbol *sym;
133 struct pmcstat_image *image;
134 struct pmcstat_cgnode *cg;
135 struct pmcstat_cgnode_hash *h;
136 uintfptr_t loadaddress;
137 unsigned int i, hash;
139 ppm = pmcstat_process_find_map(usermode ? pp : pmcstat_kernproc, pc);
143 image = ppm->ppm_image;
145 loadaddress = ppm->ppm_lowpc + image->pi_vaddr - image->pi_start;
146 pc -= loadaddress; /* Convert to an offset in the image. */
149 * Try determine the function at this offset. If we can't
150 * find a function round leave the `pc' value alone.
152 if ((sym = pmcstat_symbol_search(image, pc)) != NULL)
155 pmcstat_stats.ps_samples_unknown_function++;
157 for (hash = i = 0; i < sizeof(uintfptr_t); i++)
158 hash += (pc >> i) & 0xFF;
160 hash &= PMCSTAT_HASH_MASK;
163 LIST_FOREACH(h, &pmcstat_cgnode_hash[hash], pch_next)
165 if (h->pch_pmcid != pmcid)
172 if (cg->pcg_image == image && cg->pcg_func == pc)
177 * We haven't seen this (pmcid, pc) tuple yet, so allocate a
178 * new callgraph node and a new hash table entry for it.
180 cg = pmcstat_cgnode_allocate(image, pc);
181 if ((h = malloc(sizeof(*h))) == NULL)
182 err(EX_OSERR, "ERROR: Could not allocate callgraph node");
184 h->pch_pmcid = pmcid;
186 LIST_INSERT_HEAD(&pmcstat_cgnode_hash[hash], h, pch_next);
188 pmcstat_cgnode_hash_count++;
194 * Compare two callgraph nodes for sorting.
197 pmcstat_cgnode_compare(const void *a, const void *b)
199 const struct pmcstat_cgnode *const *pcg1, *const *pcg2, *cg1, *cg2;
201 pcg1 = (const struct pmcstat_cgnode *const *) a;
203 pcg2 = (const struct pmcstat_cgnode *const *) b;
206 /* Sort in reverse order */
207 if (cg1->pcg_count < cg2->pcg_count)
209 if (cg1->pcg_count > cg2->pcg_count)
215 * Find (allocating if a needed) a callgraph node in the given
216 * parent with the same (image, pcoffset) pair.
219 static struct pmcstat_cgnode *
220 pmcstat_cgnode_find(struct pmcstat_cgnode *parent, struct pmcstat_image *image,
223 struct pmcstat_cgnode *child;
225 LIST_FOREACH(child, &parent->pcg_children, pcg_sibling) {
226 if (child->pcg_image == image &&
227 child->pcg_func == pcoffset)
232 * Allocate a new structure.
235 child = pmcstat_cgnode_allocate(image, pcoffset);
238 * Link it into the parent.
240 LIST_INSERT_HEAD(&parent->pcg_children, child, pcg_sibling);
241 parent->pcg_nchildren++;
247 * Print one callgraph node. The output format is:
249 * indentation %(parent's samples) #nsamples function@object
252 pmcstat_cgnode_print(struct pmcstat_cgnode *cg, int depth, uint32_t total)
256 struct pmcstat_symbol *sym;
257 struct pmcstat_cgnode **sortbuffer, **cgn, *pcg;
262 (void) fprintf(args.pa_graphfile, "%*s", depth, space);
264 if (cg->pcg_count == total)
265 (void) fprintf(args.pa_graphfile, "100.0%% ");
267 (void) fprintf(args.pa_graphfile, "%05.2f%% ",
268 100.0 * cg->pcg_count / total);
270 n = fprintf(args.pa_graphfile, " [%u] ", cg->pcg_count);
272 /* #samples is a 12 character wide field. */
274 (void) fprintf(args.pa_graphfile, "%*s", 12 - n, space);
277 (void) fprintf(args.pa_graphfile, "%*s", depth, space);
279 sym = pmcstat_symbol_search(cg->pcg_image, cg->pcg_func);
281 (void) fprintf(args.pa_graphfile, "%s",
282 pmcstat_string_unintern(sym->ps_name));
284 (void) fprintf(args.pa_graphfile, "%p",
285 (void *) (cg->pcg_image->pi_vaddr + cg->pcg_func));
287 if (pmcstat_previous_filename_printed !=
288 cg->pcg_image->pi_fullpath) {
289 pmcstat_previous_filename_printed = cg->pcg_image->pi_fullpath;
290 (void) fprintf(args.pa_graphfile, " @ %s\n",
291 pmcstat_string_unintern(
292 pmcstat_previous_filename_printed));
294 (void) fprintf(args.pa_graphfile, "\n");
296 if (cg->pcg_nchildren == 0)
299 if ((sortbuffer = (struct pmcstat_cgnode **)
300 malloc(sizeof(struct pmcstat_cgnode *) *
301 cg->pcg_nchildren)) == NULL)
302 err(EX_OSERR, "ERROR: Cannot print callgraph");
305 LIST_FOREACH(pcg, &cg->pcg_children, pcg_sibling)
308 assert(cgn - sortbuffer == (int) cg->pcg_nchildren);
310 qsort(sortbuffer, cg->pcg_nchildren, sizeof(struct pmcstat_cgnode *),
311 pmcstat_cgnode_compare);
313 for (cgn = sortbuffer, n = 0; n < cg->pcg_nchildren; n++, cgn++)
314 pmcstat_cgnode_print(*cgn, depth+1, cg->pcg_count);
320 * Record a callchain.
324 pmcpl_cg_process(struct pmcstat_process *pp, struct pmcstat_pmcrecord *pmcr,
325 uint32_t nsamples, uintfptr_t *cc, int usermode, uint32_t cpu)
327 uintfptr_t pc, loadaddress;
329 struct pmcstat_image *image;
330 struct pmcstat_pcmap *ppm;
331 struct pmcstat_symbol *sym;
332 struct pmcstat_cgnode *parent, *child;
333 struct pmcstat_process *km;
339 * Find the callgraph node recorded in the global hash table
340 * for this (pmcid, pc).
344 pmcid = pmcr->pr_pmcid;
345 parent = pmcstat_cgnode_hash_lookup_pc(pp, pmcid, pc, usermode);
346 if (parent == NULL) {
347 pmcstat_stats.ps_callchain_dubious_frames++;
348 pmcr->pr_dubious_frames++;
355 * For each return address in the call chain record, subject
356 * to the maximum depth desired.
357 * - Find the image associated with the sample. Stop if there
358 * there is no valid image at that address.
359 * - Find the function that overlaps the return address.
360 * - If found: use the start address of the function.
361 * If not found (say an object's symbol table is not present or
362 * is incomplete), round down to th gprof bucket granularity.
363 * - Convert return virtual address to an offset in the image.
364 * - Look for a child with the same {offset,image} tuple,
365 * inserting one if needed.
366 * - Increment the count of occurrences of the child.
368 km = pmcstat_kernproc;
370 for (n = 1; n < (uint32_t) args.pa_graphdepth && n < nsamples; n++,
374 ppm = pmcstat_process_find_map(usermode ? pp : km, pc);
376 /* Detect full frame capture (kernel + user). */
378 ppm = pmcstat_process_find_map(pp, pc);
386 image = ppm->ppm_image;
387 loadaddress = ppm->ppm_lowpc + image->pi_vaddr -
391 if ((sym = pmcstat_symbol_search(image, pc)) != NULL)
394 child = pmcstat_cgnode_find(parent, image, pc);
400 * Printing a callgraph for a PMC.
403 pmcstat_callgraph_print_for_pmcid(struct pmcstat_pmcrecord *pmcr)
408 struct pmcstat_cgnode **sortbuffer, **cgn;
409 struct pmcstat_cgnode_hash *pch;
412 * We pull out all callgraph nodes in the top-level hash table
413 * with a matching PMC id. We then sort these based on the
414 * frequency of occurrence. Each callgraph node is then
419 pmcid = pmcr->pr_pmcid;
420 if ((sortbuffer = (struct pmcstat_cgnode **)
421 malloc(sizeof(struct pmcstat_cgnode *) *
422 pmcstat_cgnode_hash_count)) == NULL)
423 err(EX_OSERR, "ERROR: Cannot sort callgraph");
426 for (n = 0; n < PMCSTAT_NHASH; n++)
427 LIST_FOREACH(pch, &pmcstat_cgnode_hash[n], pch_next)
428 if (pch->pch_pmcid == pmcid) {
429 nsamples += pch->pch_cgnode->pcg_count;
430 *cgn++ = pch->pch_cgnode;
433 nentries = cgn - sortbuffer;
434 assert(nentries <= pmcstat_cgnode_hash_count);
441 qsort(sortbuffer, nentries, sizeof(struct pmcstat_cgnode *),
442 pmcstat_cgnode_compare);
444 (void) fprintf(args.pa_graphfile,
445 "@ %s [%u samples]\n\n",
446 pmcstat_string_unintern(pmcr->pr_pmcname),
449 for (cgn = sortbuffer, n = 0; n < nentries; n++, cgn++) {
450 pmcstat_previous_filename_printed = NULL;
451 pmcstat_cgnode_print(*cgn, 0, nsamples);
452 (void) fprintf(args.pa_graphfile, "\n");
459 * Print out callgraphs.
463 pmcstat_callgraph_print(void)
465 struct pmcstat_pmcrecord *pmcr;
467 LIST_FOREACH(pmcr, &pmcstat_pmcs, pr_next)
468 pmcstat_callgraph_print_for_pmcid(pmcr);
472 pmcstat_cgnode_topprint(struct pmcstat_cgnode *cg,
473 int depth, uint32_t nsamples)
475 int v_attrs, vs_len, ns_len, width, len, n, nchildren;
478 struct pmcstat_symbol *sym;
479 struct pmcstat_cgnode **sortbuffer, **cgn, *pcg;
484 v = PMCPL_CG_COUNTP(cg);
485 snprintf(vs, sizeof(vs), "%.1f", v);
486 v_attrs = PMCSTAT_ATTRPERCENT(v);
489 sym = pmcstat_symbol_search(cg->pcg_image, cg->pcg_func);
491 snprintf(ns, sizeof(ns), "%s",
492 pmcstat_string_unintern(sym->ps_name));
494 snprintf(ns, sizeof(ns), "%p",
495 (void *)cg->pcg_func);
497 PMCSTAT_ATTRON(v_attrs);
498 PMCSTAT_PRINTW("%5.5s", vs);
499 PMCSTAT_ATTROFF(v_attrs);
500 PMCSTAT_PRINTW(" %-10.10s %-20.20s",
501 pmcstat_string_unintern(cg->pcg_image->pi_name),
504 nchildren = cg->pcg_nchildren;
505 if (nchildren == 0) {
506 PMCSTAT_PRINTW("\n");
510 width = pmcstat_displaywidth - 40;
512 if ((sortbuffer = (struct pmcstat_cgnode **)
513 malloc(sizeof(struct pmcstat_cgnode *) *
515 err(EX_OSERR, "ERROR: Cannot print callgraph");
518 LIST_FOREACH(pcg, &cg->pcg_children, pcg_sibling)
521 assert(cgn - sortbuffer == (int)nchildren);
523 qsort(sortbuffer, nchildren, sizeof(struct pmcstat_cgnode *),
524 pmcstat_cgnode_compare);
526 /* Count how many callers. */
527 for (cgn = sortbuffer, n = 0; n < nchildren; n++, cgn++) {
530 v = PMCPL_CG_COUNTP(pcg);
531 if (v < pmcstat_threshold)
536 for (cgn = sortbuffer, n = 0; n < nchildren; n++, cgn++) {
541 v = PMCPL_CG_COUNTP(pcg);
542 vs_len = snprintf(vs, sizeof(vs), ":%.1f", v);
543 v_attrs = PMCSTAT_ATTRPERCENT(v);
548 sym = pmcstat_symbol_search(pcg->pcg_image, pcg->pcg_func);
550 ns_len = snprintf(ns, sizeof(ns), "%s",
551 pmcstat_string_unintern(sym->ps_name));
553 ns_len = snprintf(ns, sizeof(ns), "%p",
554 (void *)pcg->pcg_func);
556 len = ns_len + vs_len + 1;
557 if (width - len < 0) {
558 PMCSTAT_PRINTW(" ...");
563 PMCSTAT_PRINTW(" %s", ns);
565 PMCSTAT_ATTRON(v_attrs);
566 PMCSTAT_PRINTW("%s", vs);
567 PMCSTAT_ATTROFF(v_attrs);
570 PMCSTAT_PRINTW("\n");
579 pmcpl_cg_topdisplay(void)
583 struct pmcstat_cgnode **sortbuffer, **cgn;
584 struct pmcstat_cgnode_hash *pch;
585 struct pmcstat_pmcrecord *pmcr;
587 pmcr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
589 err(EX_SOFTWARE, "ERROR: invalid pmcindex");
592 * We pull out all callgraph nodes in the top-level hash table
593 * with a matching PMC index. We then sort these based on the
594 * frequency of occurrence. Each callgraph node is then
600 if ((sortbuffer = (struct pmcstat_cgnode **)
601 malloc(sizeof(struct pmcstat_cgnode *) *
602 pmcstat_cgnode_hash_count)) == NULL)
603 err(EX_OSERR, "ERROR: Cannot sort callgraph");
606 for (n = 0; n < PMCSTAT_NHASH; n++)
607 LIST_FOREACH(pch, &pmcstat_cgnode_hash[n], pch_next)
608 if (pmcr == NULL || pch->pch_pmcid == pmcr->pr_pmcid) {
609 nsamples += pch->pch_cgnode->pcg_count;
610 *cgn++ = pch->pch_cgnode;
613 nentries = cgn - sortbuffer;
614 assert(nentries <= pmcstat_cgnode_hash_count);
621 qsort(sortbuffer, nentries, sizeof(struct pmcstat_cgnode *),
622 pmcstat_cgnode_compare);
624 PMCSTAT_PRINTW("%5.5s %-10.10s %-20.20s %s\n",
625 "%SAMP", "IMAGE", "FUNCTION", "CALLERS");
627 nentries = min(pmcstat_displayheight - 2, nentries);
629 for (cgn = sortbuffer, n = 0; n < nentries; n++, cgn++) {
630 if (PMCPL_CG_COUNTP(*cgn) < pmcstat_threshold)
632 pmcstat_cgnode_topprint(*cgn, 0, nsamples);
639 * Handle top mode keypress.
643 pmcpl_cg_topkeypress(int c, WINDOW *w)
656 pmcstat_cgnode_hash_count = 0;
657 pmcstat_previous_filename_printed = NULL;
659 for (i = 0; i < PMCSTAT_NHASH; i++) {
660 LIST_INIT(&pmcstat_cgnode_hash[i]);
667 pmcpl_cg_shutdown(FILE *mf)
670 struct pmcstat_cgnode_hash *pch, *pchtmp;
674 if (args.pa_flags & FLAG_DO_CALLGRAPHS)
675 pmcstat_callgraph_print();
680 for (i = 0; i < PMCSTAT_NHASH; i++) {
681 LIST_FOREACH_SAFE(pch, &pmcstat_cgnode_hash[i], pch_next,
683 pmcstat_cgnode_free(pch->pch_cgnode);
684 LIST_REMOVE(pch, pch_next);