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 1024 /* TODO: dynamic. */
125 struct pmcpl_ct_line {
130 struct pmcpl_ct_line pmcpl_ct_topmax[PMCPL_CT_MAXLINE+1];
131 struct pmcpl_ct_node *pmcpl_ct_topscreen[PMCPL_CT_MAXCOL+1][PMCPL_CT_MAXLINE+1];
134 * All nodes indexed by function/image name are placed in a hash table.
136 static LIST_HEAD(,pmcpl_ct_node_hash) pmcpl_ct_node_hash[PMCSTAT_NHASH];
139 * Root node for the graph.
141 static struct pmcpl_ct_node *pmcpl_ct_root;
148 * Initialize a samples.
152 pmcpl_ct_samples_init(struct pmcpl_ct_sample *samples)
164 pmcpl_ct_samples_free(struct pmcpl_ct_sample *samples)
173 * Grow a sample block to store pmcstat_npmcs PMCs.
177 pmcpl_ct_samples_grow(struct pmcpl_ct_sample *samples)
181 /* Enough storage. */
182 if (pmcstat_npmcs <= samples->npmcs)
185 npmcs = samples->npmcs +
186 max(pmcstat_npmcs - samples->npmcs, PMCPL_CT_GROWSIZE);
187 samples->sb = realloc(samples->sb, npmcs * sizeof(unsigned));
188 if (samples->sb == NULL)
189 errx(EX_SOFTWARE, "ERROR: out of memory");
190 bzero((char *)samples->sb + samples->npmcs * sizeof(unsigned),
191 (npmcs - samples->npmcs) * sizeof(unsigned));
192 samples->npmcs = npmcs;
196 * Compute the sum of all root arcs.
200 pmcpl_ct_samples_root(struct pmcpl_ct_sample *samples)
204 pmcpl_ct_samples_init(samples);
205 pmcpl_ct_samples_grow(samples);
207 for (i = 0; i < pmcpl_ct_root->pct_narc; i++)
208 for (pmcin = 0; pmcin < pmcstat_npmcs; pmcin++)
209 samples->sb[pmcin] += PMCPL_CT_SAMPLE(pmcin,
210 &pmcpl_ct_root->pct_arc[i].pcta_samples);
214 * Grow the arc table.
218 pmcpl_ct_arc_grow(int cursize, int *maxsize, struct pmcpl_ct_arc **items)
222 if (cursize < *maxsize)
225 nmaxsize = *maxsize + max(cursize + 1 - *maxsize, PMCPL_CT_GROWSIZE);
226 *items = realloc(*items, nmaxsize * sizeof(struct pmcpl_ct_arc));
228 errx(EX_SOFTWARE, "ERROR: out of memory");
229 bzero((char *)*items + *maxsize * sizeof(struct pmcpl_ct_arc),
230 (nmaxsize - *maxsize) * sizeof(struct pmcpl_ct_arc));
235 * Grow the instr table.
239 pmcpl_ct_instr_grow(int cursize, int *maxsize, struct pmcpl_ct_instr **items)
243 if (cursize < *maxsize)
246 nmaxsize = *maxsize + max(cursize + 1 - *maxsize, PMCPL_CT_GROWSIZE);
247 *items = realloc(*items, nmaxsize * sizeof(struct pmcpl_ct_instr));
249 errx(EX_SOFTWARE, "ERROR: out of memory");
250 bzero((char *)*items + *maxsize * sizeof(struct pmcpl_ct_instr),
251 (nmaxsize - *maxsize) * sizeof(struct pmcpl_ct_instr));
256 * Add a new instruction sample to given node.
260 pmcpl_ct_instr_add(struct pmcpl_ct_node *ct, int pmcin, uintfptr_t pc)
263 struct pmcpl_ct_instr *in;
265 for (i = 0; i<ct->pct_ninstr; i++) {
266 if (ct->pct_instr[i].pctf_func == pc) {
267 in = &ct->pct_instr[i];
268 pmcpl_ct_samples_grow(&in->pctf_samples);
269 in->pctf_samples.sb[pmcin]++;
274 pmcpl_ct_instr_grow(ct->pct_ninstr, &ct->pct_instr_c, &ct->pct_instr);
275 in = &ct->pct_instr[ct->pct_ninstr];
277 pmcpl_ct_samples_init(&in->pctf_samples);
278 pmcpl_ct_samples_grow(&in->pctf_samples);
279 in->pctf_samples.sb[pmcin] = 1;
284 * Allocate a new node.
287 static struct pmcpl_ct_node *
288 pmcpl_ct_node_allocate(struct pmcstat_image *image, uintfptr_t pc)
290 struct pmcpl_ct_node *ct;
292 if ((ct = malloc(sizeof(*ct))) == NULL)
293 err(EX_OSERR, "ERROR: Cannot allocate callgraph node");
296 ct->pct_image = image;
299 pmcpl_ct_samples_init(&ct->pct_samples);
307 ct->pct_instr = NULL;
317 pmcpl_ct_node_free(struct pmcpl_ct_node *ct)
321 for (i = 0; i < ct->pct_narc; i++) {
322 pmcpl_ct_samples_free(&ct->pct_arc[i].pcta_samples);
323 pmcpl_ct_samples_free(&ct->pct_arc[i].pcta_callid);
326 pmcpl_ct_samples_free(&ct->pct_samples);
333 * Clear the graph tag on each node.
336 pmcpl_ct_node_cleartag(void)
339 struct pmcpl_ct_node_hash *pch;
341 for (i = 0; i < PMCSTAT_NHASH; i++)
342 LIST_FOREACH(pch, &pmcpl_ct_node_hash[i], pch_next)
343 pch->pch_ctnode->pct_flags &= ~PMCPL_PCT_TAG;
345 pmcpl_ct_root->pct_flags &= ~PMCPL_PCT_TAG;
349 * Print the callchain line by line with maximum cost at top.
353 pmcpl_ct_node_dumptop(int pmcin, struct pmcpl_ct_node *ct,
354 struct pmcpl_ct_sample *rsamples, int x, int *y)
358 if (ct->pct_flags & PMCPL_PCT_TAG)
361 ct->pct_flags |= PMCPL_PCT_TAG;
363 if (x >= PMCPL_CT_MAXCOL) {
364 pmcpl_ct_topscreen[x][*y] = NULL;
367 pmcpl_ct_topscreen[x][*y] = ct;
370 * Check if this is a terminal node.
371 * We need to check that some samples exist
372 * for at least one arc for that PMC.
375 for (i = 0; i < ct->pct_narc; i++)
376 if (PMCPL_CT_SAMPLE(pmcin,
377 &ct->pct_arc[i].pcta_samples) != 0) {
382 if (ct->pct_narc == 0 || terminal) {
383 pmcpl_ct_topscreen[x+1][*y] = NULL;
384 if (*y >= PMCPL_CT_MAXLINE)
387 for (i=0; i < x; i++)
388 pmcpl_ct_topscreen[i][*y] =
389 pmcpl_ct_topscreen[i][*y - 1];
393 for (i = 0; i < ct->pct_narc; i++) {
394 if (PMCPL_CT_SAMPLE(pmcin,
395 &ct->pct_arc[i].pcta_samples) == 0)
397 if (PMCPL_CT_SAMPLEP(pmcin,
398 &ct->pct_arc[i].pcta_samples) > pmcstat_threshold) {
399 if (pmcpl_ct_node_dumptop(pmcin,
400 ct->pct_arc[i].pcta_child,
410 * Compare two top line by sum.
413 pmcpl_ct_line_compare(const void *a, const void *b)
415 const struct pmcpl_ct_line *ct1, *ct2;
417 ct1 = (const struct pmcpl_ct_line *) a;
418 ct2 = (const struct pmcpl_ct_line *) b;
420 /* Sort in reverse order */
421 if (ct1->ln_sum < ct2->ln_sum)
423 if (ct1->ln_sum > ct2->ln_sum)
429 * Format and display given PMC index.
433 pmcpl_ct_node_printtop(struct pmcpl_ct_sample *rsamples, int pmcin, int maxy)
437 #define TS(x, y) (pmcpl_ct_topscreen[x][y])
438 #define TSI(x, y) (pmcpl_ct_topscreen[x][pmcpl_ct_topmax[y].ln_index])
440 int v_attrs, ns_len, vs_len, is_len, width, indentwidth, x, y;
442 char ns[30], vs[10], is[20];
443 struct pmcpl_ct_node *ct;
444 struct pmcstat_symbol *sym;
445 const char *space = " ";
455 pmcpl_ct_topmax[y].ln_sum = 0;
456 pmcpl_ct_topmax[y].ln_index = y;
457 for (x = 1; TS(x, y) != NULL; x++) {
458 pmcpl_ct_topmax[y].ln_sum +=
459 PMCPL_CT_SAMPLE(pmcin, &TS(x, y)->pct_samples);
462 qsort(pmcpl_ct_topmax, y, sizeof(pmcpl_ct_topmax[0]),
463 pmcpl_ct_line_compare);
464 pmcpl_ct_topmax[y].ln_index = y;
466 for (y = 0; y < maxy; y++) {
472 PMCSTAT_PRINTW("\n");
475 v = pmcpl_ct_topmax[y].ln_sum * 100.0 /
477 snprintf(vs, sizeof(vs), "%.1f", v);
478 v_attrs = PMCSTAT_ATTRPERCENT(v);
479 PMCSTAT_ATTRON(v_attrs);
480 PMCSTAT_PRINTW("%5.5s ", vs);
481 PMCSTAT_ATTROFF(v_attrs);
483 width = indentwidth = 5 + 1;
485 for (x = 1; (ct = TSI(x, y)) != NULL; x++) {
487 vs[0] = '\0'; vs_len = 0;
488 is[0] = '\0'; is_len = 0;
491 v = PMCPL_CT_SAMPLEP(pmcin, &ct->pct_samples);
492 if (v > pmcstat_threshold)
493 vs_len = snprintf(vs, sizeof(vs),
495 v_attrs = PMCSTAT_ATTRPERCENT(v);
497 if (pmcstat_skiplink && v <= pmcstat_threshold) {
498 strlcpy(ns, ".", sizeof(ns));
501 sym = pmcstat_symbol_search(ct->pct_image, ct->pct_func);
503 ns_len = snprintf(ns, sizeof(ns), "%s",
504 pmcstat_string_unintern(sym->ps_name));
506 ns_len = snprintf(ns, sizeof(ns), "%p",
507 (void *)ct->pct_func);
511 TSI(x-1, y)->pct_image != ct->pct_image)
512 is_len = snprintf(is, sizeof(is), "@%s",
513 pmcstat_string_unintern(ct->pct_image->pi_name));
515 /* Check for line wrap. */
516 width += ns_len + is_len + vs_len + 1;
518 if (width >= pmcstat_displaywidth) {
522 PMCSTAT_PRINTW("\n%*s", indentwidth, space);
523 width = indentwidth + ns_len + is_len + vs_len;
526 PMCSTAT_ATTRON(v_attrs);
527 PMCSTAT_PRINTW("%s%s%s ", ns, is, vs);
528 PMCSTAT_ATTROFF(v_attrs);
534 * Output top mode snapshot.
538 pmcpl_ct_topdisplay(void)
541 struct pmcpl_ct_sample r, *rsamples;
544 pmcpl_ct_samples_root(rsamples);
546 pmcpl_ct_node_cleartag();
548 PMCSTAT_PRINTW("%5.5s %s\n", "%SAMP", "CALLTREE");
551 if (pmcpl_ct_node_dumptop(pmcstat_pmcinfilter,
552 pmcpl_ct_root, rsamples, 0, &y))
553 PMCSTAT_PRINTW("...\n");
554 pmcpl_ct_topscreen[1][y] = NULL;
556 pmcpl_ct_node_printtop(rsamples,
557 pmcstat_pmcinfilter, pmcstat_displayheight - 2);
559 pmcpl_ct_samples_free(rsamples);
563 * Handle top mode keypress.
567 pmcpl_ct_topkeypress(int c, WINDOW *w)
572 pmcstat_skiplink = !pmcstat_skiplink;
573 wprintw(w, "skip empty link %s", pmcstat_skiplink ? "on" : "off");
581 * Look for a callgraph node associated with pmc `pmcid' in the global
582 * hash table that corresponds to the given `pc' value in the process map
586 static struct pmcpl_ct_node *
587 pmcpl_ct_node_hash_lookup_pc(struct pmcpl_ct_node *parent,
588 struct pmcstat_pcmap *ppm, uintfptr_t pc, int pmcin)
590 struct pmcstat_symbol *sym;
591 struct pmcstat_image *image;
592 struct pmcpl_ct_node *ct;
593 struct pmcpl_ct_node_hash *h;
594 struct pmcpl_ct_arc *arc;
595 uintfptr_t loadaddress;
599 assert(parent != NULL);
601 image = ppm->ppm_image;
603 loadaddress = ppm->ppm_lowpc + image->pi_vaddr - image->pi_start;
604 pc -= loadaddress; /* Convert to an offset in the image. */
607 * Try determine the function at this offset. If we can't
608 * find a function round leave the `pc' value alone.
610 if ((sym = pmcstat_symbol_search(image, pc)) != NULL)
613 for (hash = i = 0; i < (int)sizeof(uintfptr_t); i++)
614 hash += (pc >> i) & 0xFF;
616 hash &= PMCSTAT_HASH_MASK;
619 LIST_FOREACH(h, &pmcpl_ct_node_hash[hash], pch_next) {
624 if (ct->pct_image == image && ct->pct_func == pc) {
626 * Find related arc in parent node and
627 * increment the sample count.
629 for (i = 0; i < parent->pct_narc; i++) {
630 if (parent->pct_arc[i].pcta_child == ct) {
631 arc = &parent->pct_arc[i];
632 pmcpl_ct_samples_grow(&arc->pcta_samples);
633 arc->pcta_samples.sb[pmcin]++;
634 /* Estimate call count. */
635 pmcpl_ct_samples_grow(&arc->pcta_callid);
636 if (pmcpl_ct_callid.sb[pmcin] -
637 arc->pcta_callid.sb[pmcin] > 1)
639 arc->pcta_callid.sb[pmcin] =
640 pmcpl_ct_callid.sb[pmcin];
646 * No arc found for us, add ourself to the parent.
648 pmcpl_ct_arc_grow(parent->pct_narc,
649 &parent->pct_arc_c, &parent->pct_arc);
650 arc = &parent->pct_arc[parent->pct_narc];
651 pmcpl_ct_samples_grow(&arc->pcta_samples);
652 arc->pcta_samples.sb[pmcin] = 1;
654 pmcpl_ct_samples_grow(&arc->pcta_callid);
655 arc->pcta_callid.sb[pmcin] = pmcpl_ct_callid.sb[pmcin];
656 arc->pcta_child = ct;
663 * We haven't seen this (pmcid, pc) tuple yet, so allocate a
664 * new callgraph node and a new hash table entry for it.
666 ct = pmcpl_ct_node_allocate(image, pc);
667 if ((h = malloc(sizeof(*h))) == NULL)
668 err(EX_OSERR, "ERROR: Could not allocate callgraph node");
671 LIST_INSERT_HEAD(&pmcpl_ct_node_hash[hash], h, pch_next);
673 pmcpl_ct_arc_grow(parent->pct_narc,
674 &parent->pct_arc_c, &parent->pct_arc);
675 arc = &parent->pct_arc[parent->pct_narc];
676 pmcpl_ct_samples_grow(&arc->pcta_samples);
677 arc->pcta_samples.sb[pmcin] = 1;
679 pmcpl_ct_samples_grow(&arc->pcta_callid);
680 arc->pcta_callid.sb[pmcin] = pmcpl_ct_callid.sb[pmcin];
681 arc->pcta_child = ct;
687 * Record a callchain.
691 pmcpl_ct_process(struct pmcstat_process *pp, struct pmcstat_pmcrecord *pmcr,
692 uint32_t nsamples, uintfptr_t *cc, int usermode, uint32_t cpu)
695 struct pmcstat_pcmap *ppm[PMC_CALLCHAIN_DEPTH_MAX];
696 struct pmcstat_process *km;
697 struct pmcpl_ct_node *parent, *child;
701 assert(nsamples>0 && nsamples<=PMC_CALLCHAIN_DEPTH_MAX);
703 /* Get the PMC index. */
704 pmcin = pmcr->pr_pmcin;
707 * Validate mapping for the callchain.
708 * Go from bottom to first invalid entry.
710 km = pmcstat_kernproc;
711 for (n = 0; n < (int)nsamples; n++) {
712 ppm[n] = pmcstat_process_find_map(usermode ?
714 if (ppm[n] == NULL) {
715 /* Detect full frame capture (kernel + user). */
717 ppm[n] = pmcstat_process_find_map(pp, cc[n]);
726 pmcstat_stats.ps_callchain_dubious_frames++;
727 pmcr->pr_dubious_frames++;
731 /* Increase the call generation counter. */
732 pmcpl_ct_samples_grow(&pmcpl_ct_callid);
733 pmcpl_ct_callid.sb[pmcin]++;
736 * Iterate remaining addresses.
738 for (parent = pmcpl_ct_root, child = NULL; n >= 0; n--) {
739 child = pmcpl_ct_node_hash_lookup_pc(parent, ppm[n], cc[n],
742 pmcstat_stats.ps_callchain_dubious_frames++;
749 * Increment the sample count for this PMC.
752 pmcpl_ct_samples_grow(&child->pct_samples);
753 child->pct_samples.sb[pmcin]++;
755 /* Update per instruction sample if required. */
756 if (args.pa_ctdumpinstr)
757 pmcpl_ct_instr_add(child, pmcin, cc[0] -
758 (ppm[0]->ppm_lowpc + ppm[0]->ppm_image->pi_vaddr -
759 ppm[0]->ppm_image->pi_start));
764 * Print node self cost.
768 pmcpl_ct_node_printself(struct pmcpl_ct_node *ct)
772 struct pmcstat_symbol *sym;
773 char sourcefile[PATH_MAX];
774 char funcname[PATH_MAX];
779 #ifdef PMCPL_CT_OPTIMIZEFN
780 if (pmcpl_ct_prevfn != ct->pct_image->pi_fullpath) {
782 pmcpl_ct_prevfn = ct->pct_image->pi_fullpath;
783 fprintf(args.pa_graphfile, "ob=%s\n",
784 pmcstat_string_unintern(pmcpl_ct_prevfn));
785 #ifdef PMCPL_CT_OPTIMIZEFN
792 if (pmcstat_image_addr2line(ct->pct_image, ct->pct_func,
793 sourcefile, sizeof(sourcefile), &line,
794 funcname, sizeof(funcname))) {
795 fprintf(args.pa_graphfile, "fn=%s\n",
798 sym = pmcstat_symbol_search(ct->pct_image, ct->pct_func);
800 fprintf(args.pa_graphfile, "fn=%s\n",
801 pmcstat_string_unintern(sym->ps_name));
803 fprintf(args.pa_graphfile, "fn=%p\n",
804 (void *)(ct->pct_image->pi_vaddr + ct->pct_func));
810 if (ct->pct_ninstr > 0) {
811 for (i = 0; i < ct->pct_ninstr; i++) {
812 addr = ct->pct_image->pi_vaddr +
813 ct->pct_instr[i].pctf_func;
815 if (pmcstat_image_addr2line(ct->pct_image, addr,
816 sourcefile, sizeof(sourcefile), &line,
817 funcname, sizeof(funcname)))
818 fprintf(args.pa_graphfile, "fl=%s\n", sourcefile);
819 fprintf(args.pa_graphfile, "%p %u", (void *)addr, line);
820 for (j = 0; j<pmcstat_npmcs; j++)
821 fprintf(args.pa_graphfile, " %u",
823 &ct->pct_instr[i].pctf_samples));
824 fprintf(args.pa_graphfile, "\n");
827 addr = ct->pct_image->pi_vaddr + ct->pct_func;
829 if (pmcstat_image_addr2line(ct->pct_image, addr,
830 sourcefile, sizeof(sourcefile), &line,
831 funcname, sizeof(funcname)))
832 fprintf(args.pa_graphfile, "fl=%s\n", sourcefile);
833 fprintf(args.pa_graphfile, "* *");
834 for (i = 0; i<pmcstat_npmcs ; i++)
835 fprintf(args.pa_graphfile, " %u",
836 PMCPL_CT_SAMPLE(i, &ct->pct_samples));
837 fprintf(args.pa_graphfile, "\n");
842 * Print node child cost.
846 pmcpl_ct_node_printchild(struct pmcpl_ct_node *ct)
850 struct pmcstat_symbol *sym;
851 struct pmcpl_ct_node *child;
852 char sourcefile[PATH_MAX];
853 char funcname[PATH_MAX];
857 * TODO: attach child cost to the real position in the funtion.
858 * TODO: cfn=<fn> / call <ncall> addr(<fn>) / addr(call <fn>) <arccost>
860 for (i=0 ; i<ct->pct_narc; i++) {
861 child = ct->pct_arc[i].pcta_child;
864 #ifdef PMCPL_CT_OPTIMIZEFN
865 if (pmcpl_ct_prevfn != child->pct_image->pi_fullpath) {
867 pmcpl_ct_prevfn = child->pct_image->pi_fullpath;
868 fprintf(args.pa_graphfile, "cob=%s\n",
869 pmcstat_string_unintern(pmcpl_ct_prevfn));
870 #if PMCPL_CT_OPTIMIZEFN
873 /* Child function name. */
874 addr = child->pct_image->pi_vaddr + child->pct_func;
875 /* Child function source file. */
876 if (pmcstat_image_addr2line(child->pct_image, addr,
877 sourcefile, sizeof(sourcefile), &line,
878 funcname, sizeof(funcname))) {
879 fprintf(args.pa_graphfile, "cfn=%s\n", funcname);
880 fprintf(args.pa_graphfile, "cfl=%s\n", sourcefile);
882 sym = pmcstat_symbol_search(child->pct_image,
885 fprintf(args.pa_graphfile, "cfn=%s\n",
886 pmcstat_string_unintern(sym->ps_name));
888 fprintf(args.pa_graphfile, "cfn=%p\n", (void *)addr);
891 /* Child function address, line and call count. */
892 fprintf(args.pa_graphfile, "calls=%u %p %u\n",
893 ct->pct_arc[i].pcta_call, (void *)addr, line);
895 if (ct->pct_image != NULL) {
896 /* Call address, line, sample. */
897 addr = ct->pct_image->pi_vaddr + ct->pct_func;
899 pmcstat_image_addr2line(ct->pct_image, addr, sourcefile,
900 sizeof(sourcefile), &line,
901 funcname, sizeof(funcname));
902 fprintf(args.pa_graphfile, "%p %u", (void *)addr, line);
905 fprintf(args.pa_graphfile, "* *");
906 for (j = 0; j<pmcstat_npmcs; j++)
907 fprintf(args.pa_graphfile, " %u",
908 PMCPL_CT_SAMPLE(j, &ct->pct_arc[i].pcta_samples));
909 fprintf(args.pa_graphfile, "\n");
914 * Clean the PMC name for Kcachegrind formula
918 pmcpl_ct_fixup_pmcname(char *s)
928 * Print a calltree (KCachegrind) for all PMCs.
935 struct pmcpl_ct_node_hash *pch;
936 struct pmcpl_ct_sample rsamples;
939 pmcpl_ct_samples_root(&rsamples);
940 pmcpl_ct_prevfn = NULL;
942 fprintf(args.pa_graphfile,
945 "positions: instr line\n"
947 for (i=0; i<pmcstat_npmcs; i++) {
948 snprintf(name, sizeof(name), "%s_%d",
949 pmcstat_pmcindex_to_name(i), i);
950 pmcpl_ct_fixup_pmcname(name);
951 fprintf(args.pa_graphfile, " %s", name);
953 fprintf(args.pa_graphfile, "\nsummary:");
954 for (i=0; i<pmcstat_npmcs ; i++)
955 fprintf(args.pa_graphfile, " %u",
956 PMCPL_CT_SAMPLE(i, &rsamples));
957 fprintf(args.pa_graphfile, "\n\n");
962 fprintf(args.pa_graphfile, "ob=FreeBSD\n");
963 fprintf(args.pa_graphfile, "fn=ROOT\n");
964 fprintf(args.pa_graphfile, "* *");
965 for (i = 0; i<pmcstat_npmcs ; i++)
966 fprintf(args.pa_graphfile, " 0");
967 fprintf(args.pa_graphfile, "\n");
968 pmcpl_ct_node_printchild(pmcpl_ct_root);
970 for (n = 0; n < PMCSTAT_NHASH; n++)
971 LIST_FOREACH(pch, &pmcpl_ct_node_hash[n], pch_next) {
972 pmcpl_ct_node_printself(pch->pch_ctnode);
973 pmcpl_ct_node_printchild(pch->pch_ctnode);
976 pmcpl_ct_samples_free(&rsamples);
980 pmcpl_ct_configure(char *opt)
983 if (strncmp(opt, "skiplink=", 9) == 0) {
984 pmcstat_skiplink = atoi(opt+9);
996 pmcpl_ct_prevfn = NULL;
997 pmcpl_ct_root = pmcpl_ct_node_allocate(NULL, 0);
999 for (i = 0; i < PMCSTAT_NHASH; i++)
1000 LIST_INIT(&pmcpl_ct_node_hash[i]);
1002 pmcpl_ct_samples_init(&pmcpl_ct_callid);
1008 pmcpl_ct_shutdown(FILE *mf)
1011 struct pmcpl_ct_node_hash *pch, *pchtmp;
1015 if (args.pa_flags & FLAG_DO_CALLGRAPHS)
1022 for (i = 0; i < PMCSTAT_NHASH; i++) {
1023 LIST_FOREACH_SAFE(pch, &pmcpl_ct_node_hash[i], pch_next,
1025 pmcpl_ct_node_free(pch->pch_ctnode);
1030 pmcpl_ct_node_free(pmcpl_ct_root);
1031 pmcpl_ct_root = NULL;
1033 pmcpl_ct_samples_free(&pmcpl_ct_callid);