2 * Copyright (c) 2005-2007, Joseph Koshy
3 * Copyright (c) 2007 The FreeBSD Foundation
6 * Portions of this software were developed by A. Joseph Koshy under
7 * sponsorship from the FreeBSD Foundation and Google, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Transform a hwpmc(4) log into human readable form, and into
33 * gprof(1) compatible profiles.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/endian.h>
42 #include <sys/imgact_aout.h>
43 #include <sys/imgact_elf.h>
46 #include <sys/queue.h>
47 #include <sys/socket.h>
51 #include <netinet/in.h>
72 #include "pmcstat_log.h"
73 #include "pmcstat_top.h"
74 #include "pmcpl_callgraph.h"
76 /* Get the sample value in percent related to nsamples. */
77 #define PMCPL_CG_COUNTP(a) \
78 ((a)->pcg_count * 100.0 / nsamples)
81 * The toplevel CG nodes (i.e., with rank == 0) are placed in a hash table.
84 struct pmcstat_cgnode_hash_list pmcstat_cgnode_hash[PMCSTAT_NHASH];
85 int pmcstat_cgnode_hash_count;
87 static pmcstat_interned_string pmcstat_previous_filename_printed;
89 static struct pmcstat_cgnode *
90 pmcstat_cgnode_allocate(struct pmcstat_image *image, uintfptr_t pc)
92 struct pmcstat_cgnode *cg;
94 if ((cg = malloc(sizeof(*cg))) == NULL)
95 err(EX_OSERR, "ERROR: Cannot allocate callgraph node");
97 cg->pcg_image = image;
101 cg->pcg_nchildren = 0;
102 LIST_INIT(&cg->pcg_children);
108 * Free a node and its children.
111 pmcstat_cgnode_free(struct pmcstat_cgnode *cg)
113 struct pmcstat_cgnode *cgc, *cgtmp;
115 LIST_FOREACH_SAFE(cgc, &cg->pcg_children, pcg_sibling, cgtmp)
116 pmcstat_cgnode_free(cgc);
121 * Look for a callgraph node associated with pmc `pmcid' in the global
122 * hash table that corresponds to the given `pc' value in the process
125 static struct pmcstat_cgnode *
126 pmcstat_cgnode_hash_lookup_pc(struct pmcstat_process *pp, pmc_id_t pmcid,
127 uintfptr_t pc, int usermode)
129 struct pmcstat_pcmap *ppm;
130 struct pmcstat_symbol *sym;
131 struct pmcstat_image *image;
132 struct pmcstat_cgnode *cg;
133 struct pmcstat_cgnode_hash *h;
134 uintfptr_t loadaddress;
135 unsigned int i, hash;
137 ppm = pmcstat_process_find_map(usermode ? pp : pmcstat_kernproc, pc);
141 image = ppm->ppm_image;
143 loadaddress = ppm->ppm_lowpc + image->pi_vaddr - image->pi_start;
144 pc -= loadaddress; /* Convert to an offset in the image. */
147 * Try determine the function at this offset. If we can't
148 * find a function round leave the `pc' value alone.
150 if ((sym = pmcstat_symbol_search(image, pc)) != NULL)
153 for (hash = i = 0; i < sizeof(uintfptr_t); i++)
154 hash += (pc >> i) & 0xFF;
156 hash &= PMCSTAT_HASH_MASK;
159 LIST_FOREACH(h, &pmcstat_cgnode_hash[hash], pch_next)
161 if (h->pch_pmcid != pmcid)
168 if (cg->pcg_image == image && cg->pcg_func == pc)
173 * We haven't seen this (pmcid, pc) tuple yet, so allocate a
174 * new callgraph node and a new hash table entry for it.
176 cg = pmcstat_cgnode_allocate(image, pc);
177 if ((h = malloc(sizeof(*h))) == NULL)
178 err(EX_OSERR, "ERROR: Could not allocate callgraph node");
180 h->pch_pmcid = pmcid;
182 LIST_INSERT_HEAD(&pmcstat_cgnode_hash[hash], h, pch_next);
184 pmcstat_cgnode_hash_count++;
190 * Compare two callgraph nodes for sorting.
193 pmcstat_cgnode_compare(const void *a, const void *b)
195 const struct pmcstat_cgnode *const *pcg1, *const *pcg2, *cg1, *cg2;
197 pcg1 = (const struct pmcstat_cgnode *const *) a;
199 pcg2 = (const struct pmcstat_cgnode *const *) b;
202 /* Sort in reverse order */
203 if (cg1->pcg_count < cg2->pcg_count)
205 if (cg1->pcg_count > cg2->pcg_count)
211 * Find (allocating if a needed) a callgraph node in the given
212 * parent with the same (image, pcoffset) pair.
215 static struct pmcstat_cgnode *
216 pmcstat_cgnode_find(struct pmcstat_cgnode *parent, struct pmcstat_image *image,
219 struct pmcstat_cgnode *child;
221 LIST_FOREACH(child, &parent->pcg_children, pcg_sibling) {
222 if (child->pcg_image == image &&
223 child->pcg_func == pcoffset)
228 * Allocate a new structure.
231 child = pmcstat_cgnode_allocate(image, pcoffset);
234 * Link it into the parent.
236 LIST_INSERT_HEAD(&parent->pcg_children, child, pcg_sibling);
237 parent->pcg_nchildren++;
243 * Print one callgraph node. The output format is:
245 * indentation %(parent's samples) #nsamples function@object
248 pmcstat_cgnode_print(struct pmcstat_cgnode *cg, int depth, uint32_t total)
252 struct pmcstat_symbol *sym;
253 struct pmcstat_cgnode **sortbuffer, **cgn, *pcg;
258 (void) fprintf(args.pa_graphfile, "%*s", depth, space);
260 if (cg->pcg_count == total)
261 (void) fprintf(args.pa_graphfile, "100.0%% ");
263 (void) fprintf(args.pa_graphfile, "%05.2f%% ",
264 100.0 * cg->pcg_count / total);
266 n = fprintf(args.pa_graphfile, " [%u] ", cg->pcg_count);
268 /* #samples is a 12 character wide field. */
270 (void) fprintf(args.pa_graphfile, "%*s", 12 - n, space);
273 (void) fprintf(args.pa_graphfile, "%*s", depth, space);
275 sym = pmcstat_symbol_search(cg->pcg_image, cg->pcg_func);
277 (void) fprintf(args.pa_graphfile, "%s",
278 pmcstat_string_unintern(sym->ps_name));
280 (void) fprintf(args.pa_graphfile, "%p",
281 (void *) (cg->pcg_image->pi_vaddr + cg->pcg_func));
283 if (pmcstat_previous_filename_printed !=
284 cg->pcg_image->pi_fullpath) {
285 pmcstat_previous_filename_printed = cg->pcg_image->pi_fullpath;
286 (void) fprintf(args.pa_graphfile, " @ %s\n",
287 pmcstat_string_unintern(
288 pmcstat_previous_filename_printed));
290 (void) fprintf(args.pa_graphfile, "\n");
292 if (cg->pcg_nchildren == 0)
295 if ((sortbuffer = (struct pmcstat_cgnode **)
296 malloc(sizeof(struct pmcstat_cgnode *) *
297 cg->pcg_nchildren)) == NULL)
298 err(EX_OSERR, "ERROR: Cannot print callgraph");
301 LIST_FOREACH(pcg, &cg->pcg_children, pcg_sibling)
304 assert(cgn - sortbuffer == (int) cg->pcg_nchildren);
306 qsort(sortbuffer, cg->pcg_nchildren, sizeof(struct pmcstat_cgnode *),
307 pmcstat_cgnode_compare);
309 for (cgn = sortbuffer, n = 0; n < cg->pcg_nchildren; n++, cgn++)
310 pmcstat_cgnode_print(*cgn, depth+1, cg->pcg_count);
316 * Record a callchain.
320 pmcpl_cg_process(struct pmcstat_process *pp, struct pmcstat_pmcrecord *pmcr,
321 uint32_t nsamples, uintfptr_t *cc, int usermode, uint32_t cpu)
323 uintfptr_t pc, loadaddress;
325 struct pmcstat_image *image;
326 struct pmcstat_pcmap *ppm;
327 struct pmcstat_symbol *sym;
328 struct pmcstat_cgnode *parent, *child;
329 struct pmcstat_process *km;
335 * Find the callgraph node recorded in the global hash table
336 * for this (pmcid, pc).
340 pmcid = pmcr->pr_pmcid;
341 parent = pmcstat_cgnode_hash_lookup_pc(pp, pmcid, pc, usermode);
342 if (parent == NULL) {
343 pmcstat_stats.ps_callchain_dubious_frames++;
344 pmcr->pr_dubious_frames++;
351 * For each return address in the call chain record, subject
352 * to the maximum depth desired.
353 * - Find the image associated with the sample. Stop if there
354 * there is no valid image at that address.
355 * - Find the function that overlaps the return address.
356 * - If found: use the start address of the function.
357 * If not found (say an object's symbol table is not present or
358 * is incomplete), round down to th gprof bucket granularity.
359 * - Convert return virtual address to an offset in the image.
360 * - Look for a child with the same {offset,image} tuple,
361 * inserting one if needed.
362 * - Increment the count of occurrences of the child.
364 km = pmcstat_kernproc;
366 for (n = 1; n < (uint32_t) args.pa_graphdepth && n < nsamples; n++,
370 ppm = pmcstat_process_find_map(usermode ? pp : km, pc);
372 /* Detect full frame capture (kernel + user). */
374 ppm = pmcstat_process_find_map(pp, pc);
382 image = ppm->ppm_image;
383 loadaddress = ppm->ppm_lowpc + image->pi_vaddr -
387 if ((sym = pmcstat_symbol_search(image, pc)) != NULL)
390 child = pmcstat_cgnode_find(parent, image, pc);
396 * Printing a callgraph for a PMC.
399 pmcstat_callgraph_print_for_pmcid(struct pmcstat_pmcrecord *pmcr)
404 struct pmcstat_cgnode **sortbuffer, **cgn;
405 struct pmcstat_cgnode_hash *pch;
408 * We pull out all callgraph nodes in the top-level hash table
409 * with a matching PMC id. We then sort these based on the
410 * frequency of occurrence. Each callgraph node is then
415 pmcid = pmcr->pr_pmcid;
416 if ((sortbuffer = (struct pmcstat_cgnode **)
417 malloc(sizeof(struct pmcstat_cgnode *) *
418 pmcstat_cgnode_hash_count)) == NULL)
419 err(EX_OSERR, "ERROR: Cannot sort callgraph");
422 for (n = 0; n < PMCSTAT_NHASH; n++)
423 LIST_FOREACH(pch, &pmcstat_cgnode_hash[n], pch_next)
424 if (pch->pch_pmcid == pmcid) {
425 nsamples += pch->pch_cgnode->pcg_count;
426 *cgn++ = pch->pch_cgnode;
429 nentries = cgn - sortbuffer;
430 assert(nentries <= pmcstat_cgnode_hash_count);
437 qsort(sortbuffer, nentries, sizeof(struct pmcstat_cgnode *),
438 pmcstat_cgnode_compare);
440 (void) fprintf(args.pa_graphfile,
441 "@ %s [%u samples]\n\n",
442 pmcstat_string_unintern(pmcr->pr_pmcname),
445 for (cgn = sortbuffer, n = 0; n < nentries; n++, cgn++) {
446 pmcstat_previous_filename_printed = NULL;
447 pmcstat_cgnode_print(*cgn, 0, nsamples);
448 (void) fprintf(args.pa_graphfile, "\n");
455 * Print out callgraphs.
459 pmcstat_callgraph_print(void)
461 struct pmcstat_pmcrecord *pmcr;
463 LIST_FOREACH(pmcr, &pmcstat_pmcs, pr_next)
464 pmcstat_callgraph_print_for_pmcid(pmcr);
468 pmcstat_cgnode_topprint(struct pmcstat_cgnode *cg,
469 int depth, uint32_t nsamples)
471 int v_attrs, vs_len, ns_len, width, len, n, nchildren;
474 struct pmcstat_symbol *sym;
475 struct pmcstat_cgnode **sortbuffer, **cgn, *pcg;
480 v = PMCPL_CG_COUNTP(cg);
481 snprintf(vs, sizeof(vs), "%.1f", v);
482 v_attrs = PMCSTAT_ATTRPERCENT(v);
485 sym = pmcstat_symbol_search(cg->pcg_image, cg->pcg_func);
487 snprintf(ns, sizeof(ns), "%s",
488 pmcstat_string_unintern(sym->ps_name));
490 snprintf(ns, sizeof(ns), "%p",
491 (void *)cg->pcg_func);
493 PMCSTAT_ATTRON(v_attrs);
494 PMCSTAT_PRINTW("%5.5s", vs);
495 PMCSTAT_ATTROFF(v_attrs);
496 PMCSTAT_PRINTW(" %-10.10s %-20.20s",
497 pmcstat_string_unintern(cg->pcg_image->pi_name),
500 nchildren = cg->pcg_nchildren;
501 if (nchildren == 0) {
502 PMCSTAT_PRINTW("\n");
506 width = pmcstat_displaywidth - 40;
508 if ((sortbuffer = (struct pmcstat_cgnode **)
509 malloc(sizeof(struct pmcstat_cgnode *) *
511 err(EX_OSERR, "ERROR: Cannot print callgraph");
514 LIST_FOREACH(pcg, &cg->pcg_children, pcg_sibling)
517 assert(cgn - sortbuffer == (int)nchildren);
519 qsort(sortbuffer, nchildren, sizeof(struct pmcstat_cgnode *),
520 pmcstat_cgnode_compare);
522 /* Count how many callers. */
523 for (cgn = sortbuffer, n = 0; n < nchildren; n++, cgn++) {
526 v = PMCPL_CG_COUNTP(pcg);
527 if (v < pmcstat_threshold)
532 for (cgn = sortbuffer, n = 0; n < nchildren; n++, cgn++) {
537 v = PMCPL_CG_COUNTP(pcg);
538 vs_len = snprintf(vs, sizeof(vs), ":%.1f", v);
539 v_attrs = PMCSTAT_ATTRPERCENT(v);
544 sym = pmcstat_symbol_search(pcg->pcg_image, pcg->pcg_func);
546 ns_len = snprintf(ns, sizeof(ns), "%s",
547 pmcstat_string_unintern(sym->ps_name));
549 ns_len = snprintf(ns, sizeof(ns), "%p",
550 (void *)pcg->pcg_func);
552 len = ns_len + vs_len + 1;
553 if (width - len < 0) {
554 PMCSTAT_PRINTW(" ...");
559 PMCSTAT_PRINTW(" %s", ns);
561 PMCSTAT_ATTRON(v_attrs);
562 PMCSTAT_PRINTW("%s", vs);
563 PMCSTAT_ATTROFF(v_attrs);
566 PMCSTAT_PRINTW("\n");
575 pmcpl_cg_topdisplay(void)
579 struct pmcstat_cgnode **sortbuffer, **cgn;
580 struct pmcstat_cgnode_hash *pch;
581 struct pmcstat_pmcrecord *pmcr;
583 pmcr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
586 * We pull out all callgraph nodes in the top-level hash table
587 * with a matching PMC index. We then sort these based on the
588 * frequency of occurrence. Each callgraph node is then
594 if ((sortbuffer = (struct pmcstat_cgnode **)
595 malloc(sizeof(struct pmcstat_cgnode *) *
596 pmcstat_cgnode_hash_count)) == NULL)
597 err(EX_OSERR, "ERROR: Cannot sort callgraph");
600 for (n = 0; n < PMCSTAT_NHASH; n++)
601 LIST_FOREACH(pch, &pmcstat_cgnode_hash[n], pch_next)
602 if (pmcr == NULL || pch->pch_pmcid == pmcr->pr_pmcid) {
603 nsamples += pch->pch_cgnode->pcg_count;
604 *cgn++ = pch->pch_cgnode;
607 nentries = cgn - sortbuffer;
608 assert(nentries <= pmcstat_cgnode_hash_count);
615 qsort(sortbuffer, nentries, sizeof(struct pmcstat_cgnode *),
616 pmcstat_cgnode_compare);
618 PMCSTAT_PRINTW("%5.5s %-10.10s %-20.20s %s\n",
619 "%SAMP", "IMAGE", "FUNCTION", "CALLERS");
621 nentries = min(pmcstat_displayheight - 2, nentries);
623 for (cgn = sortbuffer, n = 0; n < nentries; n++, cgn++) {
624 if (PMCPL_CG_COUNTP(*cgn) < pmcstat_threshold)
626 pmcstat_cgnode_topprint(*cgn, 0, nsamples);
633 * Handle top mode keypress.
637 pmcpl_cg_topkeypress(int c, WINDOW *w)
650 pmcstat_cgnode_hash_count = 0;
651 pmcstat_previous_filename_printed = NULL;
653 for (i = 0; i < PMCSTAT_NHASH; i++) {
654 LIST_INIT(&pmcstat_cgnode_hash[i]);
661 pmcpl_cg_shutdown(FILE *mf)
664 struct pmcstat_cgnode_hash *pch, *pchtmp;
668 if (args.pa_flags & FLAG_DO_CALLGRAPHS)
669 pmcstat_callgraph_print();
674 for (i = 0; i < PMCSTAT_NHASH; i++) {
675 LIST_FOREACH_SAFE(pch, &pmcstat_cgnode_hash[i], pch_next,
677 pmcstat_cgnode_free(pch->pch_cgnode);
678 LIST_REMOVE(pch, pch_next);