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>
57 #include "pmcstat_log.h"
58 #include "pmcstat_top.h"
59 #include "pmcpl_calltree.h"
61 #define PMCPL_CT_GROWSIZE 4
63 static pmcstat_interned_string pmcpl_ct_prevfn;
65 static int pmcstat_skiplink = 0;
69 /* Get the sample value for PMC a. */
70 #define PMCPL_CT_SAMPLE(a, b) \
71 ((a) < (b)->npmcs ? (b)->sb[a] : 0)
73 /* Get the sample value in percent related to rsamples. */
74 #define PMCPL_CT_SAMPLEP(a, b) \
75 (PMCPL_CT_SAMPLE(a, b) * 100.0 / rsamples->sb[a])
77 struct pmcpl_ct_sample {
78 int npmcs; /* Max pmc index available. */
79 unsigned *sb; /* Sample buffer for 0..npmcs. */
83 struct pmcpl_ct_sample pcta_samples;
84 struct pmcpl_ct_sample pcta_callid;
86 struct pmcpl_ct_node *pcta_child;
89 struct pmcpl_ct_instr {
91 struct pmcpl_ct_sample pctf_samples;
95 * Each calltree node is tracked by a pmcpl_ct_node struct.
97 struct pmcpl_ct_node {
98 #define PMCPL_PCT_TAG 0x00000001 /* Loop detection. */
100 struct pmcstat_image *pct_image;
102 struct pmcpl_ct_sample pct_samples;
106 struct pmcpl_ct_arc *pct_arc;
108 /* TODO: optimize for large number of items. */
111 struct pmcpl_ct_instr *pct_instr;
114 struct pmcpl_ct_node_hash {
115 struct pmcpl_ct_node *pch_ctnode;
116 LIST_ENTRY(pmcpl_ct_node_hash) pch_next;
119 struct pmcpl_ct_sample pmcpl_ct_callid;
121 #define PMCPL_CT_MAXCOL PMC_CALLCHAIN_DEPTH_MAX
122 #define PMCPL_CT_MAXLINE 1024 /* TODO: dynamic. */
124 struct pmcpl_ct_line {
129 struct pmcpl_ct_line pmcpl_ct_topmax[PMCPL_CT_MAXLINE+1];
130 struct pmcpl_ct_node *pmcpl_ct_topscreen[PMCPL_CT_MAXCOL+1][PMCPL_CT_MAXLINE+1];
133 * All nodes indexed by function/image name are placed in a hash table.
135 static LIST_HEAD(,pmcpl_ct_node_hash) pmcpl_ct_node_hash[PMCSTAT_NHASH];
138 * Root node for the graph.
140 static struct pmcpl_ct_node *pmcpl_ct_root;
147 * Initialize a samples.
151 pmcpl_ct_samples_init(struct pmcpl_ct_sample *samples)
163 pmcpl_ct_samples_free(struct pmcpl_ct_sample *samples)
172 * Grow a sample block to store pmcstat_npmcs PMCs.
176 pmcpl_ct_samples_grow(struct pmcpl_ct_sample *samples)
180 /* Enough storage. */
181 if (pmcstat_npmcs <= samples->npmcs)
184 npmcs = samples->npmcs +
185 max(pmcstat_npmcs - samples->npmcs, PMCPL_CT_GROWSIZE);
186 samples->sb = realloc(samples->sb, npmcs * sizeof(unsigned));
187 if (samples->sb == NULL)
188 errx(EX_SOFTWARE, "ERROR: out of memory");
189 bzero((char *)samples->sb + samples->npmcs * sizeof(unsigned),
190 (npmcs - samples->npmcs) * sizeof(unsigned));
191 samples->npmcs = npmcs;
195 * Compute the sum of all root arcs.
199 pmcpl_ct_samples_root(struct pmcpl_ct_sample *samples)
203 pmcpl_ct_samples_init(samples);
204 pmcpl_ct_samples_grow(samples);
206 for (i = 0; i < pmcpl_ct_root->pct_narc; i++)
207 for (pmcin = 0; pmcin < pmcstat_npmcs; pmcin++)
208 samples->sb[pmcin] += PMCPL_CT_SAMPLE(pmcin,
209 &pmcpl_ct_root->pct_arc[i].pcta_samples);
213 * Grow the arc table.
217 pmcpl_ct_arc_grow(int cursize, int *maxsize, struct pmcpl_ct_arc **items)
221 if (cursize < *maxsize)
224 nmaxsize = *maxsize + max(cursize + 1 - *maxsize, PMCPL_CT_GROWSIZE);
225 *items = realloc(*items, nmaxsize * sizeof(struct pmcpl_ct_arc));
227 errx(EX_SOFTWARE, "ERROR: out of memory");
228 bzero((char *)*items + *maxsize * sizeof(struct pmcpl_ct_arc),
229 (nmaxsize - *maxsize) * sizeof(struct pmcpl_ct_arc));
234 * Grow the instr table.
238 pmcpl_ct_instr_grow(int cursize, int *maxsize, struct pmcpl_ct_instr **items)
242 if (cursize < *maxsize)
245 nmaxsize = *maxsize + max(cursize + 1 - *maxsize, PMCPL_CT_GROWSIZE);
246 *items = realloc(*items, nmaxsize * sizeof(struct pmcpl_ct_instr));
248 errx(EX_SOFTWARE, "ERROR: out of memory");
249 bzero((char *)*items + *maxsize * sizeof(struct pmcpl_ct_instr),
250 (nmaxsize - *maxsize) * sizeof(struct pmcpl_ct_instr));
255 * Add a new instruction sample to given node.
259 pmcpl_ct_instr_add(struct pmcpl_ct_node *ct, int pmcin, uintfptr_t pc)
262 struct pmcpl_ct_instr *in;
264 for (i = 0; i<ct->pct_ninstr; i++) {
265 if (ct->pct_instr[i].pctf_func == pc) {
266 in = &ct->pct_instr[i];
267 pmcpl_ct_samples_grow(&in->pctf_samples);
268 in->pctf_samples.sb[pmcin]++;
273 pmcpl_ct_instr_grow(ct->pct_ninstr, &ct->pct_instr_c, &ct->pct_instr);
274 in = &ct->pct_instr[ct->pct_ninstr];
276 pmcpl_ct_samples_init(&in->pctf_samples);
277 pmcpl_ct_samples_grow(&in->pctf_samples);
278 in->pctf_samples.sb[pmcin] = 1;
283 * Allocate a new node.
286 static struct pmcpl_ct_node *
287 pmcpl_ct_node_allocate(struct pmcstat_image *image, uintfptr_t pc)
289 struct pmcpl_ct_node *ct;
291 if ((ct = malloc(sizeof(*ct))) == NULL)
292 err(EX_OSERR, "ERROR: Cannot allocate callgraph node");
295 ct->pct_image = image;
298 pmcpl_ct_samples_init(&ct->pct_samples);
306 ct->pct_instr = NULL;
316 pmcpl_ct_node_free(struct pmcpl_ct_node *ct)
320 for (i = 0; i < ct->pct_narc; i++) {
321 pmcpl_ct_samples_free(&ct->pct_arc[i].pcta_samples);
322 pmcpl_ct_samples_free(&ct->pct_arc[i].pcta_callid);
325 pmcpl_ct_samples_free(&ct->pct_samples);
332 * Clear the graph tag on each node.
335 pmcpl_ct_node_cleartag(void)
338 struct pmcpl_ct_node_hash *pch;
340 for (i = 0; i < PMCSTAT_NHASH; i++)
341 LIST_FOREACH(pch, &pmcpl_ct_node_hash[i], pch_next)
342 pch->pch_ctnode->pct_flags &= ~PMCPL_PCT_TAG;
344 pmcpl_ct_root->pct_flags &= ~PMCPL_PCT_TAG;
348 * Print the callchain line by line with maximum cost at top.
352 pmcpl_ct_node_dumptop(int pmcin, struct pmcpl_ct_node *ct,
353 struct pmcpl_ct_sample *rsamples, int x, int *y)
356 struct pmcpl_ct_arc *arc;
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 arc = &ct->pct_arc[i];
377 if (PMCPL_CT_SAMPLE(pmcin,
378 &arc->pcta_samples) != 0 &&
379 PMCPL_CT_SAMPLEP(pmcin,
380 &arc->pcta_samples) > pmcstat_threshold &&
381 (arc->pcta_child->pct_flags & PMCPL_PCT_TAG) == 0) {
387 if (ct->pct_narc == 0 || terminal) {
388 pmcpl_ct_topscreen[x+1][*y] = NULL;
389 if (*y >= PMCPL_CT_MAXLINE)
392 for (i=0; i < x; i++)
393 pmcpl_ct_topscreen[i][*y] =
394 pmcpl_ct_topscreen[i][*y - 1];
398 for (i = 0; i < ct->pct_narc; i++) {
399 if (PMCPL_CT_SAMPLE(pmcin,
400 &ct->pct_arc[i].pcta_samples) == 0)
402 if (PMCPL_CT_SAMPLEP(pmcin,
403 &ct->pct_arc[i].pcta_samples) > pmcstat_threshold) {
404 if (pmcpl_ct_node_dumptop(pmcin,
405 ct->pct_arc[i].pcta_child,
415 * Compare two top line by sum.
418 pmcpl_ct_line_compare(const void *a, const void *b)
420 const struct pmcpl_ct_line *ct1, *ct2;
422 ct1 = (const struct pmcpl_ct_line *) a;
423 ct2 = (const struct pmcpl_ct_line *) b;
425 /* Sort in reverse order */
426 if (ct1->ln_sum < ct2->ln_sum)
428 if (ct1->ln_sum > ct2->ln_sum)
434 * Format and display given PMC index.
438 pmcpl_ct_node_printtop(struct pmcpl_ct_sample *rsamples, int pmcin, int maxy)
442 #define TS(x, y) (pmcpl_ct_topscreen[x][y])
443 #define TSI(x, y) (pmcpl_ct_topscreen[x][pmcpl_ct_topmax[y].ln_index])
445 int v_attrs, ns_len, vs_len, is_len, width, indentwidth, x, y;
447 char ns[30], vs[10], is[20];
448 struct pmcpl_ct_node *ct;
449 struct pmcstat_symbol *sym;
450 const char *space = " ";
460 pmcpl_ct_topmax[y].ln_sum = 0;
461 pmcpl_ct_topmax[y].ln_index = y;
462 for (x = 1; TS(x, y) != NULL; x++) {
463 pmcpl_ct_topmax[y].ln_sum +=
464 PMCPL_CT_SAMPLE(pmcin, &TS(x, y)->pct_samples);
467 qsort(pmcpl_ct_topmax, y, sizeof(pmcpl_ct_topmax[0]),
468 pmcpl_ct_line_compare);
469 pmcpl_ct_topmax[y].ln_index = y;
471 for (y = 0; y < maxy; y++) {
477 PMCSTAT_PRINTW("\n");
480 v = pmcpl_ct_topmax[y].ln_sum * 100.0 /
482 snprintf(vs, sizeof(vs), "%.1f", v);
483 v_attrs = PMCSTAT_ATTRPERCENT(v);
484 PMCSTAT_ATTRON(v_attrs);
485 PMCSTAT_PRINTW("%5.5s ", vs);
486 PMCSTAT_ATTROFF(v_attrs);
488 width = indentwidth = 5 + 1;
490 for (x = 1; (ct = TSI(x, y)) != NULL; x++) {
492 vs[0] = '\0'; vs_len = 0;
493 is[0] = '\0'; is_len = 0;
496 v = PMCPL_CT_SAMPLEP(pmcin, &ct->pct_samples);
497 if (v > pmcstat_threshold)
498 vs_len = snprintf(vs, sizeof(vs),
500 v_attrs = PMCSTAT_ATTRPERCENT(v);
502 if (pmcstat_skiplink && v <= pmcstat_threshold) {
503 strlcpy(ns, ".", sizeof(ns));
506 sym = pmcstat_symbol_search(ct->pct_image, ct->pct_func);
508 ns_len = snprintf(ns, sizeof(ns), "%s",
509 pmcstat_string_unintern(sym->ps_name));
511 ns_len = snprintf(ns, sizeof(ns), "%p",
512 (void *)ct->pct_func);
516 TSI(x-1, y)->pct_image != ct->pct_image)
517 is_len = snprintf(is, sizeof(is), "@%s",
518 pmcstat_string_unintern(ct->pct_image->pi_name));
520 /* Check for line wrap. */
521 width += ns_len + is_len + vs_len + 1;
523 if (width >= pmcstat_displaywidth) {
527 PMCSTAT_PRINTW("\n%*s", indentwidth, space);
528 width = indentwidth + ns_len + is_len + vs_len;
531 PMCSTAT_ATTRON(v_attrs);
532 PMCSTAT_PRINTW("%s%s%s ", ns, is, vs);
533 PMCSTAT_ATTROFF(v_attrs);
539 * Output top mode snapshot.
543 pmcpl_ct_topdisplay(void)
546 struct pmcpl_ct_sample r, *rsamples;
549 pmcpl_ct_samples_root(rsamples);
551 pmcpl_ct_node_cleartag();
553 PMCSTAT_PRINTW("%5.5s %s\n", "%SAMP", "CALLTREE");
556 if (pmcpl_ct_node_dumptop(pmcstat_pmcinfilter,
557 pmcpl_ct_root, rsamples, 0, &y))
558 PMCSTAT_PRINTW("...\n");
559 pmcpl_ct_topscreen[1][y] = NULL;
561 pmcpl_ct_node_printtop(rsamples,
562 pmcstat_pmcinfilter, pmcstat_displayheight - 2);
564 pmcpl_ct_samples_free(rsamples);
568 * Handle top mode keypress.
572 pmcpl_ct_topkeypress(int c, WINDOW *w)
577 pmcstat_skiplink = !pmcstat_skiplink;
578 wprintw(w, "skip empty link %s",
579 pmcstat_skiplink ? "on" : "off");
587 * Look for a callgraph node associated with pmc `pmcid' in the global
588 * hash table that corresponds to the given `pc' value in the process map
592 static struct pmcpl_ct_node *
593 pmcpl_ct_node_hash_lookup_pc(struct pmcpl_ct_node *parent,
594 struct pmcstat_pcmap *ppm, uintfptr_t pc, int pmcin)
596 struct pmcstat_symbol *sym;
597 struct pmcstat_image *image;
598 struct pmcpl_ct_node *ct;
599 struct pmcpl_ct_node_hash *h;
600 struct pmcpl_ct_arc *arc;
601 uintfptr_t loadaddress;
605 assert(parent != NULL);
607 image = ppm->ppm_image;
609 loadaddress = ppm->ppm_lowpc + image->pi_vaddr - image->pi_start;
610 pc -= loadaddress; /* Convert to an offset in the image. */
613 * Try determine the function at this offset. If we can't
614 * find a function round leave the `pc' value alone.
616 if ((sym = pmcstat_symbol_search(image, pc)) != NULL)
619 pmcstat_stats.ps_samples_unknown_function++;
621 for (hash = i = 0; i < (int)sizeof(uintfptr_t); i++)
622 hash += (pc >> i) & 0xFF;
624 hash &= PMCSTAT_HASH_MASK;
627 LIST_FOREACH(h, &pmcpl_ct_node_hash[hash], pch_next) {
632 if (ct->pct_image == image && ct->pct_func == pc) {
634 * Find related arc in parent node and
635 * increment the sample count.
637 for (i = 0; i < parent->pct_narc; i++) {
638 if (parent->pct_arc[i].pcta_child == ct) {
639 arc = &parent->pct_arc[i];
640 pmcpl_ct_samples_grow(&arc->pcta_samples);
641 arc->pcta_samples.sb[pmcin]++;
642 /* Estimate call count. */
643 pmcpl_ct_samples_grow(&arc->pcta_callid);
644 if (pmcpl_ct_callid.sb[pmcin] -
645 arc->pcta_callid.sb[pmcin] > 1)
647 arc->pcta_callid.sb[pmcin] =
648 pmcpl_ct_callid.sb[pmcin];
654 * No arc found for us, add ourself to the parent.
656 pmcpl_ct_arc_grow(parent->pct_narc,
657 &parent->pct_arc_c, &parent->pct_arc);
658 arc = &parent->pct_arc[parent->pct_narc];
659 pmcpl_ct_samples_grow(&arc->pcta_samples);
660 arc->pcta_samples.sb[pmcin] = 1;
662 pmcpl_ct_samples_grow(&arc->pcta_callid);
663 arc->pcta_callid.sb[pmcin] = pmcpl_ct_callid.sb[pmcin];
664 arc->pcta_child = ct;
671 * We haven't seen this (pmcid, pc) tuple yet, so allocate a
672 * new callgraph node and a new hash table entry for it.
674 ct = pmcpl_ct_node_allocate(image, pc);
675 if ((h = malloc(sizeof(*h))) == NULL)
676 err(EX_OSERR, "ERROR: Could not allocate callgraph node");
679 LIST_INSERT_HEAD(&pmcpl_ct_node_hash[hash], h, pch_next);
681 pmcpl_ct_arc_grow(parent->pct_narc,
682 &parent->pct_arc_c, &parent->pct_arc);
683 arc = &parent->pct_arc[parent->pct_narc];
684 pmcpl_ct_samples_grow(&arc->pcta_samples);
685 arc->pcta_samples.sb[pmcin] = 1;
687 pmcpl_ct_samples_grow(&arc->pcta_callid);
688 arc->pcta_callid.sb[pmcin] = pmcpl_ct_callid.sb[pmcin];
689 arc->pcta_child = ct;
695 * Record a callchain.
699 pmcpl_ct_process(struct pmcstat_process *pp, struct pmcstat_pmcrecord *pmcr,
700 uint32_t nsamples, uintfptr_t *cc, int usermode, uint32_t cpu)
703 struct pmcstat_pcmap *ppm[PMC_CALLCHAIN_DEPTH_MAX];
704 struct pmcstat_process *km;
705 struct pmcpl_ct_node *parent, *child;
709 assert(nsamples>0 && nsamples<=PMC_CALLCHAIN_DEPTH_MAX);
711 /* Get the PMC index. */
712 pmcin = pmcr->pr_pmcin;
715 * Validate mapping for the callchain.
716 * Go from bottom to first invalid entry.
718 km = pmcstat_kernproc;
719 for (n = 0; n < (int)nsamples; n++) {
720 ppm[n] = pmcstat_process_find_map(usermode ?
722 if (ppm[n] == NULL) {
723 /* Detect full frame capture (kernel + user). */
725 ppm[n] = pmcstat_process_find_map(pp, cc[n]);
734 pmcstat_stats.ps_callchain_dubious_frames++;
735 pmcr->pr_dubious_frames++;
739 /* Increase the call generation counter. */
740 pmcpl_ct_samples_grow(&pmcpl_ct_callid);
741 pmcpl_ct_callid.sb[pmcin]++;
744 * Iterate remaining addresses.
746 for (parent = pmcpl_ct_root, child = NULL; n >= 0; n--) {
747 child = pmcpl_ct_node_hash_lookup_pc(parent, ppm[n], cc[n],
750 pmcstat_stats.ps_callchain_dubious_frames++;
757 * Increment the sample count for this PMC.
760 pmcpl_ct_samples_grow(&child->pct_samples);
761 child->pct_samples.sb[pmcin]++;
763 /* Update per instruction sample if required. */
764 if (args.pa_ctdumpinstr)
765 pmcpl_ct_instr_add(child, pmcin, cc[0] -
766 (ppm[0]->ppm_lowpc + ppm[0]->ppm_image->pi_vaddr -
767 ppm[0]->ppm_image->pi_start));
772 * Print node self cost.
776 pmcpl_ct_node_printself(struct pmcpl_ct_node *ct)
780 struct pmcstat_symbol *sym;
781 char sourcefile[PATH_MAX];
782 char funcname[PATH_MAX];
787 #ifdef PMCPL_CT_OPTIMIZEFN
788 if (pmcpl_ct_prevfn != ct->pct_image->pi_fullpath) {
790 pmcpl_ct_prevfn = ct->pct_image->pi_fullpath;
791 fprintf(args.pa_graphfile, "ob=%s\n",
792 pmcstat_string_unintern(pmcpl_ct_prevfn));
793 #ifdef PMCPL_CT_OPTIMIZEFN
800 if (pmcstat_image_addr2line(ct->pct_image, ct->pct_func,
801 sourcefile, sizeof(sourcefile), &line,
802 funcname, sizeof(funcname))) {
803 fprintf(args.pa_graphfile, "fn=%s\n",
806 sym = pmcstat_symbol_search(ct->pct_image, ct->pct_func);
808 fprintf(args.pa_graphfile, "fn=%s\n",
809 pmcstat_string_unintern(sym->ps_name));
811 fprintf(args.pa_graphfile, "fn=%p\n",
812 (void *)(ct->pct_image->pi_vaddr + ct->pct_func));
818 if (ct->pct_ninstr > 0) {
819 for (i = 0; i < ct->pct_ninstr; i++) {
820 addr = ct->pct_image->pi_vaddr +
821 ct->pct_instr[i].pctf_func;
823 if (pmcstat_image_addr2line(ct->pct_image, addr,
824 sourcefile, sizeof(sourcefile), &line,
825 funcname, sizeof(funcname)))
826 fprintf(args.pa_graphfile, "fl=%s\n", sourcefile);
827 fprintf(args.pa_graphfile, "%p %u", (void *)addr, line);
828 for (j = 0; j<pmcstat_npmcs; j++)
829 fprintf(args.pa_graphfile, " %u",
831 &ct->pct_instr[i].pctf_samples));
832 fprintf(args.pa_graphfile, "\n");
835 addr = ct->pct_image->pi_vaddr + ct->pct_func;
837 if (pmcstat_image_addr2line(ct->pct_image, addr,
838 sourcefile, sizeof(sourcefile), &line,
839 funcname, sizeof(funcname)))
840 fprintf(args.pa_graphfile, "fl=%s\n", sourcefile);
841 fprintf(args.pa_graphfile, "* *");
842 for (i = 0; i<pmcstat_npmcs ; i++)
843 fprintf(args.pa_graphfile, " %u",
844 PMCPL_CT_SAMPLE(i, &ct->pct_samples));
845 fprintf(args.pa_graphfile, "\n");
850 * Print node child cost.
854 pmcpl_ct_node_printchild(struct pmcpl_ct_node *ct)
858 struct pmcstat_symbol *sym;
859 struct pmcpl_ct_node *child;
860 char sourcefile[PATH_MAX];
861 char funcname[PATH_MAX];
865 * TODO: attach child cost to the real position in the funtion.
866 * TODO: cfn=<fn> / call <ncall> addr(<fn>) / addr(call <fn>) <arccost>
868 for (i=0 ; i<ct->pct_narc; i++) {
869 child = ct->pct_arc[i].pcta_child;
872 #ifdef PMCPL_CT_OPTIMIZEFN
873 if (pmcpl_ct_prevfn != child->pct_image->pi_fullpath) {
875 pmcpl_ct_prevfn = child->pct_image->pi_fullpath;
876 fprintf(args.pa_graphfile, "cob=%s\n",
877 pmcstat_string_unintern(pmcpl_ct_prevfn));
878 #if PMCPL_CT_OPTIMIZEFN
881 /* Child function name. */
882 addr = child->pct_image->pi_vaddr + child->pct_func;
883 /* Child function source file. */
884 if (pmcstat_image_addr2line(child->pct_image, addr,
885 sourcefile, sizeof(sourcefile), &line,
886 funcname, sizeof(funcname))) {
887 fprintf(args.pa_graphfile, "cfn=%s\n", funcname);
888 fprintf(args.pa_graphfile, "cfl=%s\n", sourcefile);
890 sym = pmcstat_symbol_search(child->pct_image,
893 fprintf(args.pa_graphfile, "cfn=%s\n",
894 pmcstat_string_unintern(sym->ps_name));
896 fprintf(args.pa_graphfile, "cfn=%p\n", (void *)addr);
899 /* Child function address, line and call count. */
900 fprintf(args.pa_graphfile, "calls=%u %p %u\n",
901 ct->pct_arc[i].pcta_call, (void *)addr, line);
903 if (ct->pct_image != NULL) {
904 /* Call address, line, sample. */
905 addr = ct->pct_image->pi_vaddr + ct->pct_func;
907 if (pmcstat_image_addr2line(ct->pct_image, addr, sourcefile,
908 sizeof(sourcefile), &line,
909 funcname, sizeof(funcname)))
910 fprintf(args.pa_graphfile, "%p %u", (void *)addr, line);
912 fprintf(args.pa_graphfile, "* *");
915 fprintf(args.pa_graphfile, "* *");
916 for (j = 0; j<pmcstat_npmcs; j++)
917 fprintf(args.pa_graphfile, " %u",
918 PMCPL_CT_SAMPLE(j, &ct->pct_arc[i].pcta_samples));
919 fprintf(args.pa_graphfile, "\n");
924 * Clean the PMC name for Kcachegrind formula
928 pmcpl_ct_fixup_pmcname(char *s)
938 * Print a calltree (KCachegrind) for all PMCs.
945 struct pmcpl_ct_node_hash *pch;
946 struct pmcpl_ct_sample rsamples;
949 pmcpl_ct_samples_root(&rsamples);
950 pmcpl_ct_prevfn = NULL;
952 fprintf(args.pa_graphfile,
955 "positions: instr line\n"
957 for (i=0; i<pmcstat_npmcs; i++) {
958 snprintf(name, sizeof(name), "%s_%d",
959 pmcstat_pmcindex_to_name(i), i);
960 pmcpl_ct_fixup_pmcname(name);
961 fprintf(args.pa_graphfile, " %s", name);
963 fprintf(args.pa_graphfile, "\nsummary:");
964 for (i=0; i<pmcstat_npmcs ; i++)
965 fprintf(args.pa_graphfile, " %u",
966 PMCPL_CT_SAMPLE(i, &rsamples));
967 fprintf(args.pa_graphfile, "\n\n");
972 fprintf(args.pa_graphfile, "ob=FreeBSD\n");
973 fprintf(args.pa_graphfile, "fn=ROOT\n");
974 fprintf(args.pa_graphfile, "* *");
975 for (i = 0; i<pmcstat_npmcs ; i++)
976 fprintf(args.pa_graphfile, " 0");
977 fprintf(args.pa_graphfile, "\n");
978 pmcpl_ct_node_printchild(pmcpl_ct_root);
980 for (n = 0; n < PMCSTAT_NHASH; n++)
981 LIST_FOREACH(pch, &pmcpl_ct_node_hash[n], pch_next) {
982 pmcpl_ct_node_printself(pch->pch_ctnode);
983 pmcpl_ct_node_printchild(pch->pch_ctnode);
986 pmcpl_ct_samples_free(&rsamples);
990 pmcpl_ct_configure(char *opt)
993 if (strncmp(opt, "skiplink=", 9) == 0) {
994 pmcstat_skiplink = atoi(opt+9);
1006 pmcpl_ct_prevfn = NULL;
1007 pmcpl_ct_root = pmcpl_ct_node_allocate(NULL, 0);
1009 for (i = 0; i < PMCSTAT_NHASH; i++)
1010 LIST_INIT(&pmcpl_ct_node_hash[i]);
1012 pmcpl_ct_samples_init(&pmcpl_ct_callid);
1018 pmcpl_ct_shutdown(FILE *mf)
1021 struct pmcpl_ct_node_hash *pch, *pchtmp;
1025 if (args.pa_flags & FLAG_DO_CALLGRAPHS)
1032 for (i = 0; i < PMCSTAT_NHASH; i++) {
1033 LIST_FOREACH_SAFE(pch, &pmcpl_ct_node_hash[i], pch_next,
1035 pmcpl_ct_node_free(pch->pch_ctnode);
1040 pmcpl_ct_node_free(pmcpl_ct_root);
1041 pmcpl_ct_root = NULL;
1043 pmcpl_ct_samples_free(&pmcpl_ct_callid);