2 * Copyright (c) 2009, Fabien Thomas
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Process hwpmc(4) samples as calltree.
30 * Output file format compatible with Kcachegrind (kdesdk).
31 * Handle top mode with a sorted tree display.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/endian.h>
39 #include <sys/queue.h>
58 #include "pmcstat_log.h"
59 #include "pmcstat_top.h"
60 #include "pmcpl_calltree.h"
62 #define PMCPL_CT_GROWSIZE 4
64 static pmcstat_interned_string pmcpl_ct_prevfn;
66 static int pmcstat_skiplink = 0;
70 /* Get the sample value for PMC a. */
71 #define PMCPL_CT_SAMPLE(a, b) \
72 ((a) < (b)->npmcs ? (b)->sb[a] : 0)
74 /* Get the sample value in percent related to rsamples. */
75 #define PMCPL_CT_SAMPLEP(a, b) \
76 (PMCPL_CT_SAMPLE(a, b) * 100.0 / rsamples->sb[a])
78 struct pmcpl_ct_sample {
79 int npmcs; /* Max pmc index available. */
80 unsigned *sb; /* Sample buffer for 0..npmcs. */
84 struct pmcpl_ct_sample pcta_samples;
85 struct pmcpl_ct_sample pcta_callid;
87 struct pmcpl_ct_node *pcta_child;
90 struct pmcpl_ct_instr {
92 struct pmcpl_ct_sample pctf_samples;
96 * Each calltree node is tracked by a pmcpl_ct_node struct.
98 struct pmcpl_ct_node {
99 #define PMCPL_PCT_TAG 0x00000001 /* Loop detection. */
101 struct pmcstat_image *pct_image;
103 struct pmcpl_ct_sample pct_samples;
107 struct pmcpl_ct_arc *pct_arc;
109 /* TODO: optimize for large number of items. */
112 struct pmcpl_ct_instr *pct_instr;
115 struct pmcpl_ct_node_hash {
116 struct pmcpl_ct_node *pch_ctnode;
117 LIST_ENTRY(pmcpl_ct_node_hash) pch_next;
120 struct pmcpl_ct_sample pmcpl_ct_callid;
122 #define PMCPL_CT_MAXCOL PMC_CALLCHAIN_DEPTH_MAX
123 #define PMCPL_CT_MAXLINE 256
124 struct pmcpl_ct_node *pmcpl_ct_topscreen[PMCPL_CT_MAXCOL][PMCPL_CT_MAXLINE];
127 * All nodes indexed by function/image name are placed in a hash table.
129 static LIST_HEAD(,pmcpl_ct_node_hash) pmcpl_ct_node_hash[PMCSTAT_NHASH];
132 * Root node for the graph.
134 static struct pmcpl_ct_node *pmcpl_ct_root;
141 * Initialize a samples.
145 pmcpl_ct_samples_init(struct pmcpl_ct_sample *samples)
157 pmcpl_ct_samples_free(struct pmcpl_ct_sample *samples)
166 * Grow a sample block to store pmcstat_npmcs PMCs.
170 pmcpl_ct_samples_grow(struct pmcpl_ct_sample *samples)
174 /* Enough storage. */
175 if (pmcstat_npmcs <= samples->npmcs)
178 npmcs = samples->npmcs +
179 max(pmcstat_npmcs - samples->npmcs, PMCPL_CT_GROWSIZE);
180 samples->sb = realloc(samples->sb, npmcs * sizeof(unsigned));
181 if (samples->sb == NULL)
182 errx(EX_SOFTWARE, "ERROR: out of memory");
183 bzero((char *)samples->sb + samples->npmcs * sizeof(unsigned),
184 (npmcs - samples->npmcs) * sizeof(unsigned));
185 samples->npmcs = npmcs;
189 * Compute the sum of all root arcs.
193 pmcpl_ct_samples_root(struct pmcpl_ct_sample *samples)
197 pmcpl_ct_samples_init(samples);
198 pmcpl_ct_samples_grow(samples);
200 for (i = 0; i < pmcpl_ct_root->pct_narc; i++)
201 for (pmcin = 0; pmcin < pmcstat_npmcs; pmcin++)
202 samples->sb[pmcin] += PMCPL_CT_SAMPLE(pmcin,
203 &pmcpl_ct_root->pct_arc[i].pcta_samples);
207 * Grow the arc table.
211 pmcpl_ct_arc_grow(int cursize, int *maxsize, struct pmcpl_ct_arc **items)
215 if (cursize < *maxsize)
218 nmaxsize = *maxsize + max(cursize + 1 - *maxsize, PMCPL_CT_GROWSIZE);
219 *items = realloc(*items, nmaxsize * sizeof(struct pmcpl_ct_arc));
221 errx(EX_SOFTWARE, "ERROR: out of memory");
222 bzero((char *)*items + *maxsize * sizeof(struct pmcpl_ct_arc),
223 (nmaxsize - *maxsize) * sizeof(struct pmcpl_ct_arc));
228 * Compare two arc by samples value.
231 pmcpl_ct_arc_compare(void *thunk, const void *a, const void *b)
233 const struct pmcpl_ct_arc *ct1, *ct2;
234 int pmcin = *(int *)thunk;
236 ct1 = (const struct pmcpl_ct_arc *) a;
237 ct2 = (const struct pmcpl_ct_arc *) b;
239 /* Sort in reverse order */
240 if (PMCPL_CT_SAMPLE(pmcin, &ct1->pcta_samples) <
241 PMCPL_CT_SAMPLE(pmcin, &ct2->pcta_samples))
243 if (PMCPL_CT_SAMPLE(pmcin, &ct1->pcta_samples) >
244 PMCPL_CT_SAMPLE(pmcin, &ct2->pcta_samples))
250 * Grow the instr table.
254 pmcpl_ct_instr_grow(int cursize, int *maxsize, struct pmcpl_ct_instr **items)
258 if (cursize < *maxsize)
261 nmaxsize = *maxsize + max(cursize + 1 - *maxsize, PMCPL_CT_GROWSIZE);
262 *items = realloc(*items, nmaxsize * sizeof(struct pmcpl_ct_instr));
264 errx(EX_SOFTWARE, "ERROR: out of memory");
265 bzero((char *)*items + *maxsize * sizeof(struct pmcpl_ct_instr),
266 (nmaxsize - *maxsize) * sizeof(struct pmcpl_ct_instr));
271 * Add a new instruction sample to given node.
275 pmcpl_ct_instr_add(struct pmcpl_ct_node *ct, int pmcin, uintfptr_t pc)
278 struct pmcpl_ct_instr *in;
280 for (i = 0; i<ct->pct_ninstr; i++) {
281 if (ct->pct_instr[i].pctf_func == pc) {
282 in = &ct->pct_instr[i];
283 pmcpl_ct_samples_grow(&in->pctf_samples);
284 in->pctf_samples.sb[pmcin]++;
289 pmcpl_ct_instr_grow(ct->pct_ninstr, &ct->pct_instr_c, &ct->pct_instr);
290 in = &ct->pct_instr[ct->pct_ninstr];
292 pmcpl_ct_samples_init(&in->pctf_samples);
293 pmcpl_ct_samples_grow(&in->pctf_samples);
294 in->pctf_samples.sb[pmcin] = 1;
299 * Allocate a new node.
302 static struct pmcpl_ct_node *
303 pmcpl_ct_node_allocate(struct pmcstat_image *image, uintfptr_t pc)
305 struct pmcpl_ct_node *ct;
307 if ((ct = malloc(sizeof(*ct))) == NULL)
308 err(EX_OSERR, "ERROR: Cannot allocate callgraph node");
311 ct->pct_image = image;
314 pmcpl_ct_samples_init(&ct->pct_samples);
322 ct->pct_instr = NULL;
332 pmcpl_ct_node_free(struct pmcpl_ct_node *ct)
336 for (i = 0; i < ct->pct_narc; i++) {
337 pmcpl_ct_samples_free(&ct->pct_arc[i].pcta_samples);
338 pmcpl_ct_samples_free(&ct->pct_arc[i].pcta_callid);
341 pmcpl_ct_samples_free(&ct->pct_samples);
348 * Clear the graph tag on each node.
351 pmcpl_ct_node_cleartag(void)
354 struct pmcpl_ct_node_hash *pch;
356 for (i = 0; i < PMCSTAT_NHASH; i++)
357 LIST_FOREACH(pch, &pmcpl_ct_node_hash[i], pch_next)
358 pch->pch_ctnode->pct_flags &= ~PMCPL_PCT_TAG;
360 pmcpl_ct_root->pct_flags &= ~PMCPL_PCT_TAG;
364 * Print the callchain line by line with maximum cost at top.
368 pmcpl_ct_node_dumptop(int pmcin, struct pmcpl_ct_node *ct,
369 struct pmcpl_ct_sample *rsamples, int x, int *y, int maxy)
373 if (ct->pct_flags & PMCPL_PCT_TAG)
376 ct->pct_flags |= PMCPL_PCT_TAG;
378 if (x >= PMCPL_CT_MAXCOL) {
379 pmcpl_ct_topscreen[x][*y] = NULL;
382 pmcpl_ct_topscreen[x][*y] = ct;
385 * This is a terminal node
387 if (ct->pct_narc == 0) {
388 pmcpl_ct_topscreen[x+1][*y] = NULL;
389 if (*y >= PMCPL_CT_MAXLINE ||
393 for (i=0; i < x; i++)
394 pmcpl_ct_topscreen[i][*y] =
395 pmcpl_ct_topscreen[i][*y - 1];
400 * Quicksort the arcs.
402 qsort_r(ct->pct_arc, ct->pct_narc, sizeof(struct pmcpl_ct_arc),
403 &pmcin, pmcpl_ct_arc_compare);
405 for (i = 0; i < ct->pct_narc; i++) {
406 /* Skip this arc if there is no sample at all. */
407 if (PMCPL_CT_SAMPLE(pmcin,
408 &ct->pct_arc[i].pcta_samples) == 0)
410 if (PMCPL_CT_SAMPLEP(pmcin,
411 &ct->pct_arc[i].pcta_samples) > pmcstat_threshold) {
412 if (pmcpl_ct_node_dumptop(pmcin,
413 ct->pct_arc[i].pcta_child,
414 rsamples, x+1, y, maxy))
423 * Format and display given PMC index.
427 pmcpl_ct_node_printtop(struct pmcpl_ct_sample *rsamples, int pmcin, int maxy)
429 int v_attrs, ns_len, vs_len, is_len, width, indentwidth, x, y;
431 char ns[30], vs[10], is[20];
432 struct pmcpl_ct_node *ct;
433 struct pmcstat_symbol *sym;
434 const char *space = " ";
436 for (y = 0; y < maxy; y++) {
438 ct = pmcpl_ct_topscreen[0][y];
439 snprintf(is, sizeof(is), "%-10.10s",
440 pmcstat_string_unintern(ct->pct_image->pi_name));
441 PMCSTAT_PRINTW("%s ", is);
442 width = indentwidth = 11;
444 for (x = 0; pmcpl_ct_topscreen[x][y] !=NULL; x++) {
446 ct = pmcpl_ct_topscreen[x][y];
448 ns[0] = '\0'; ns_len = 0;
449 vs[0] = '\0'; vs_len = 0;
450 is[0] = '\0'; is_len = 0;
453 v = PMCPL_CT_SAMPLEP(pmcin, &ct->pct_samples);
454 if (v > pmcstat_threshold)
455 vs_len = snprintf(vs, sizeof(vs), "(%.1f%%)", v);
456 v_attrs = PMCSTAT_ATTRPERCENT(v);
458 if (pmcstat_skiplink && v <= pmcstat_threshold) {
459 PMCSTAT_PRINTW(". ");
463 sym = pmcstat_symbol_search(ct->pct_image, ct->pct_func);
465 ns_len = snprintf(ns, sizeof(ns), "%s",
466 pmcstat_string_unintern(sym->ps_name));
468 ns_len = snprintf(ns, sizeof(ns), "%p",
469 (void *)ct->pct_func);
472 if (x > 0 && pmcpl_ct_topscreen[x-1][y]->pct_image != ct->pct_image)
473 is_len = snprintf(is, sizeof(is), "@%s",
474 pmcstat_string_unintern(ct->pct_image->pi_name));
476 /* Check for line wrap. */
477 width += ns_len + is_len + vs_len + 1;
478 if (width >= pmcstat_displaywidth) {
482 PMCSTAT_PRINTW("\n%*s", indentwidth, space);
483 width = indentwidth + ns_len + is_len + vs_len;
486 PMCSTAT_ATTRON(v_attrs);
487 PMCSTAT_PRINTW("%s%s%s ", ns, is, vs);
488 PMCSTAT_ATTROFF(v_attrs);
490 PMCSTAT_PRINTW("\n");
495 * Output top mode snapshot.
499 pmcpl_ct_topdisplay(void)
502 struct pmcpl_ct_sample r, *rsamples;
505 pmcpl_ct_samples_root(rsamples);
507 PMCSTAT_PRINTW("%-10.10s %s\n", "IMAGE", "CALLTREE");
509 for (pmcin = 0; pmcin < pmcstat_npmcs; pmcin++) {
511 if (pmcstat_pmcinfilter != pmcin)
514 pmcpl_ct_node_cleartag();
516 /* Quicksort the arcs. */
517 qsort_r(pmcpl_ct_root->pct_arc,
518 pmcpl_ct_root->pct_narc,
519 sizeof(struct pmcpl_ct_arc),
520 &pmcin, pmcpl_ct_arc_compare);
523 for (i = 0; i < pmcpl_ct_root->pct_narc; i++) {
524 /* Skip this arc if there is no sample at all. */
525 if (PMCPL_CT_SAMPLE(pmcin,
526 &pmcpl_ct_root->pct_arc[i].pcta_samples) == 0)
528 if (PMCPL_CT_SAMPLEP(pmcin,
529 &pmcpl_ct_root->pct_arc[i].pcta_samples) <=
532 if (pmcpl_ct_node_dumptop(pmcin,
533 pmcpl_ct_root->pct_arc[i].pcta_child,
534 rsamples, x, &y, pmcstat_displayheight - 2)) {
539 pmcpl_ct_node_printtop(rsamples, pmcin, y);
541 pmcpl_ct_samples_free(rsamples);
545 * Handle top mode keypress.
549 pmcpl_ct_topkeypress(int c, WINDOW *w)
554 pmcstat_skiplink = !pmcstat_skiplink;
555 wprintw(w, "skip empty link %s", pmcstat_skiplink ? "on" : "off");
563 * Look for a callgraph node associated with pmc `pmcid' in the global
564 * hash table that corresponds to the given `pc' value in the process map
568 static struct pmcpl_ct_node *
569 pmcpl_ct_node_hash_lookup_pc(struct pmcpl_ct_node *parent,
570 struct pmcstat_pcmap *ppm, uintfptr_t pc, int pmcin)
572 struct pmcstat_symbol *sym;
573 struct pmcstat_image *image;
574 struct pmcpl_ct_node *ct;
575 struct pmcpl_ct_node_hash *h;
576 struct pmcpl_ct_arc *arc;
577 uintfptr_t loadaddress;
581 assert(parent != NULL);
583 image = ppm->ppm_image;
585 loadaddress = ppm->ppm_lowpc + image->pi_vaddr - image->pi_start;
586 pc -= loadaddress; /* Convert to an offset in the image. */
589 * Try determine the function at this offset. If we can't
590 * find a function round leave the `pc' value alone.
592 if ((sym = pmcstat_symbol_search(image, pc)) != NULL)
595 for (hash = i = 0; i < (int)sizeof(uintfptr_t); i++)
596 hash += (pc >> i) & 0xFF;
598 hash &= PMCSTAT_HASH_MASK;
601 LIST_FOREACH(h, &pmcpl_ct_node_hash[hash], pch_next) {
606 if (ct->pct_image == image && ct->pct_func == pc) {
608 * Find related arc in parent node and
609 * increment the sample count.
611 for (i = 0; i < parent->pct_narc; i++) {
612 if (parent->pct_arc[i].pcta_child == ct) {
613 arc = &parent->pct_arc[i];
614 pmcpl_ct_samples_grow(&arc->pcta_samples);
615 arc->pcta_samples.sb[pmcin]++;
616 /* Estimate call count. */
617 pmcpl_ct_samples_grow(&arc->pcta_callid);
618 if (pmcpl_ct_callid.sb[pmcin] -
619 arc->pcta_callid.sb[pmcin] > 1)
621 arc->pcta_callid.sb[pmcin] =
622 pmcpl_ct_callid.sb[pmcin];
628 * No arc found for us, add ourself to the parent.
630 pmcpl_ct_arc_grow(parent->pct_narc,
631 &parent->pct_arc_c, &parent->pct_arc);
632 arc = &parent->pct_arc[parent->pct_narc];
633 pmcpl_ct_samples_grow(&arc->pcta_samples);
634 arc->pcta_samples.sb[pmcin] = 1;
636 pmcpl_ct_samples_grow(&arc->pcta_callid);
637 arc->pcta_callid.sb[pmcin] = pmcpl_ct_callid.sb[pmcin];
638 arc->pcta_child = ct;
645 * We haven't seen this (pmcid, pc) tuple yet, so allocate a
646 * new callgraph node and a new hash table entry for it.
648 ct = pmcpl_ct_node_allocate(image, pc);
649 if ((h = malloc(sizeof(*h))) == NULL)
650 err(EX_OSERR, "ERROR: Could not allocate callgraph node");
653 LIST_INSERT_HEAD(&pmcpl_ct_node_hash[hash], h, pch_next);
655 pmcpl_ct_arc_grow(parent->pct_narc,
656 &parent->pct_arc_c, &parent->pct_arc);
657 arc = &parent->pct_arc[parent->pct_narc];
658 pmcpl_ct_samples_grow(&arc->pcta_samples);
659 arc->pcta_samples.sb[pmcin] = 1;
661 pmcpl_ct_samples_grow(&arc->pcta_callid);
662 arc->pcta_callid.sb[pmcin] = pmcpl_ct_callid.sb[pmcin];
663 arc->pcta_child = ct;
669 * Record a callchain.
673 pmcpl_ct_process(struct pmcstat_process *pp, struct pmcstat_pmcrecord *pmcr,
674 uint32_t nsamples, uintfptr_t *cc, int usermode, uint32_t cpu)
677 struct pmcstat_pcmap *ppm[PMC_CALLCHAIN_DEPTH_MAX];
678 struct pmcstat_process *km;
679 struct pmcpl_ct_node *parent, *child;
683 assert(nsamples>0 && nsamples<=PMC_CALLCHAIN_DEPTH_MAX);
685 /* Get the PMC index. */
686 pmcin = pmcr->pr_pmcin;
689 * Validate mapping for the callchain.
690 * Go from bottom to first invalid entry.
692 km = pmcstat_kernproc;
693 for (n = 0; n < (int)nsamples; n++) {
694 ppm[n] = pmcstat_process_find_map(usermode ?
696 if (ppm[n] == NULL) {
697 /* Detect full frame capture (kernel + user). */
699 ppm[n] = pmcstat_process_find_map(pp, cc[n]);
708 pmcstat_stats.ps_callchain_dubious_frames++;
709 pmcr->pr_dubious_frames++;
713 /* Increase the call generation counter. */
714 pmcpl_ct_samples_grow(&pmcpl_ct_callid);
715 pmcpl_ct_callid.sb[pmcin]++;
718 * Iterate remaining addresses.
720 for (parent = pmcpl_ct_root, child = NULL; n >= 0; n--) {
721 child = pmcpl_ct_node_hash_lookup_pc(parent, ppm[n], cc[n],
724 pmcstat_stats.ps_callchain_dubious_frames++;
731 * Increment the sample count for this PMC.
734 pmcpl_ct_samples_grow(&child->pct_samples);
735 child->pct_samples.sb[pmcin]++;
737 /* Update per instruction sample if required. */
738 if (args.pa_ctdumpinstr)
739 pmcpl_ct_instr_add(child, pmcin, cc[0] -
740 (ppm[0]->ppm_lowpc + ppm[0]->ppm_image->pi_vaddr -
741 ppm[0]->ppm_image->pi_start));
746 * Print node self cost.
750 pmcpl_ct_node_printself(struct pmcpl_ct_node *ct)
754 struct pmcstat_symbol *sym;
755 char sourcefile[PATH_MAX];
756 char funcname[PATH_MAX];
761 #ifdef PMCPL_CT_OPTIMIZEFN
762 if (pmcpl_ct_prevfn != ct->pct_image->pi_fullpath) {
764 pmcpl_ct_prevfn = ct->pct_image->pi_fullpath;
765 fprintf(args.pa_graphfile, "ob=%s\n",
766 pmcstat_string_unintern(pmcpl_ct_prevfn));
767 #ifdef PMCPL_CT_OPTIMIZEFN
774 if (pmcstat_image_addr2line(ct->pct_image, ct->pct_func,
775 sourcefile, sizeof(sourcefile), &line,
776 funcname, sizeof(funcname))) {
777 fprintf(args.pa_graphfile, "fn=%s\n",
780 sym = pmcstat_symbol_search(ct->pct_image, ct->pct_func);
782 fprintf(args.pa_graphfile, "fn=%s\n",
783 pmcstat_string_unintern(sym->ps_name));
785 fprintf(args.pa_graphfile, "fn=%p\n",
786 (void *)(ct->pct_image->pi_vaddr + ct->pct_func));
792 if (ct->pct_ninstr > 0) {
793 for (i = 0; i < ct->pct_ninstr; i++) {
794 addr = ct->pct_image->pi_vaddr +
795 ct->pct_instr[i].pctf_func;
797 if (pmcstat_image_addr2line(ct->pct_image, addr,
798 sourcefile, sizeof(sourcefile), &line,
799 funcname, sizeof(funcname)))
800 fprintf(args.pa_graphfile, "fl=%s\n", sourcefile);
801 fprintf(args.pa_graphfile, "%p %u", (void *)addr, line);
802 for (j = 0; j<pmcstat_npmcs; j++)
803 fprintf(args.pa_graphfile, " %u",
805 &ct->pct_instr[i].pctf_samples));
806 fprintf(args.pa_graphfile, "\n");
809 addr = ct->pct_image->pi_vaddr + ct->pct_func;
811 if (pmcstat_image_addr2line(ct->pct_image, addr,
812 sourcefile, sizeof(sourcefile), &line,
813 funcname, sizeof(funcname)))
814 fprintf(args.pa_graphfile, "fl=%s\n", sourcefile);
815 fprintf(args.pa_graphfile, "* *");
816 for (i = 0; i<pmcstat_npmcs ; i++)
817 fprintf(args.pa_graphfile, " %u",
818 PMCPL_CT_SAMPLE(i, &ct->pct_samples));
819 fprintf(args.pa_graphfile, "\n");
824 * Print node child cost.
828 pmcpl_ct_node_printchild(struct pmcpl_ct_node *ct)
832 struct pmcstat_symbol *sym;
833 struct pmcpl_ct_node *child;
834 char sourcefile[PATH_MAX];
835 char funcname[PATH_MAX];
839 * TODO: attach child cost to the real position in the funtion.
840 * TODO: cfn=<fn> / call <ncall> addr(<fn>) / addr(call <fn>) <arccost>
842 for (i=0 ; i<ct->pct_narc; i++) {
843 child = ct->pct_arc[i].pcta_child;
846 #ifdef PMCPL_CT_OPTIMIZEFN
847 if (pmcpl_ct_prevfn != child->pct_image->pi_fullpath) {
849 pmcpl_ct_prevfn = child->pct_image->pi_fullpath;
850 fprintf(args.pa_graphfile, "cob=%s\n",
851 pmcstat_string_unintern(pmcpl_ct_prevfn));
852 #if PMCPL_CT_OPTIMIZEFN
855 /* Child function name. */
856 addr = child->pct_image->pi_vaddr + child->pct_func;
857 /* Child function source file. */
858 if (pmcstat_image_addr2line(child->pct_image, addr,
859 sourcefile, sizeof(sourcefile), &line,
860 funcname, sizeof(funcname))) {
861 fprintf(args.pa_graphfile, "cfn=%s\n", funcname);
862 fprintf(args.pa_graphfile, "cfl=%s\n", sourcefile);
864 sym = pmcstat_symbol_search(child->pct_image,
867 fprintf(args.pa_graphfile, "cfn=%s\n",
868 pmcstat_string_unintern(sym->ps_name));
870 fprintf(args.pa_graphfile, "cfn=%p\n", (void *)addr);
873 /* Child function address, line and call count. */
874 fprintf(args.pa_graphfile, "calls=%u %p %u\n",
875 ct->pct_arc[i].pcta_call, (void *)addr, line);
877 if (ct->pct_image != NULL) {
878 /* Call address, line, sample. */
879 addr = ct->pct_image->pi_vaddr + ct->pct_func;
881 pmcstat_image_addr2line(ct->pct_image, addr, sourcefile,
882 sizeof(sourcefile), &line,
883 funcname, sizeof(funcname));
884 fprintf(args.pa_graphfile, "%p %u", (void *)addr, line);
887 fprintf(args.pa_graphfile, "* *");
888 for (j = 0; j<pmcstat_npmcs; j++)
889 fprintf(args.pa_graphfile, " %u",
890 PMCPL_CT_SAMPLE(j, &ct->pct_arc[i].pcta_samples));
891 fprintf(args.pa_graphfile, "\n");
896 * Clean the PMC name for Kcachegrind formula
900 pmcpl_ct_fixup_pmcname(char *s)
910 * Print a calltree (KCachegrind) for all PMCs.
917 struct pmcpl_ct_node_hash *pch;
918 struct pmcpl_ct_sample rsamples;
921 pmcpl_ct_samples_root(&rsamples);
922 pmcpl_ct_prevfn = NULL;
924 fprintf(args.pa_graphfile,
927 "positions: instr line\n"
929 for (i=0; i<pmcstat_npmcs; i++) {
930 snprintf(name, sizeof(name), "%s_%d",
931 pmcstat_pmcindex_to_name(i), i);
932 pmcpl_ct_fixup_pmcname(name);
933 fprintf(args.pa_graphfile, " %s", name);
935 fprintf(args.pa_graphfile, "\nsummary:");
936 for (i=0; i<pmcstat_npmcs ; i++)
937 fprintf(args.pa_graphfile, " %u",
938 PMCPL_CT_SAMPLE(i, &rsamples));
939 fprintf(args.pa_graphfile, "\n\n");
944 fprintf(args.pa_graphfile, "ob=FreeBSD\n");
945 fprintf(args.pa_graphfile, "fn=ROOT\n");
946 fprintf(args.pa_graphfile, "* *");
947 for (i = 0; i<pmcstat_npmcs ; i++)
948 fprintf(args.pa_graphfile, " 0");
949 fprintf(args.pa_graphfile, "\n");
950 pmcpl_ct_node_printchild(pmcpl_ct_root);
952 for (n = 0; n < PMCSTAT_NHASH; n++)
953 LIST_FOREACH(pch, &pmcpl_ct_node_hash[n], pch_next) {
954 pmcpl_ct_node_printself(pch->pch_ctnode);
955 pmcpl_ct_node_printchild(pch->pch_ctnode);
958 pmcpl_ct_samples_free(&rsamples);
962 pmcpl_ct_configure(char *opt)
965 if (strncmp(opt, "skiplink=", 9) == 0) {
966 pmcstat_skiplink = atoi(opt+9);
978 pmcpl_ct_prevfn = NULL;
979 pmcpl_ct_root = pmcpl_ct_node_allocate(NULL, 0);
981 for (i = 0; i < PMCSTAT_NHASH; i++)
982 LIST_INIT(&pmcpl_ct_node_hash[i]);
984 pmcpl_ct_samples_init(&pmcpl_ct_callid);
990 pmcpl_ct_shutdown(FILE *mf)
993 struct pmcpl_ct_node_hash *pch, *pchtmp;
997 if (args.pa_flags & FLAG_DO_CALLGRAPHS)
1004 for (i = 0; i < PMCSTAT_NHASH; i++) {
1005 LIST_FOREACH_SAFE(pch, &pmcpl_ct_node_hash[i], pch_next,
1007 pmcpl_ct_node_free(pch->pch_ctnode);
1012 pmcpl_ct_node_free(pmcpl_ct_root);
1013 pmcpl_ct_root = NULL;
1015 pmcpl_ct_samples_free(&pmcpl_ct_callid);