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", pmcstat_skiplink ? "on" : "off");
586 * Look for a callgraph node associated with pmc `pmcid' in the global
587 * hash table that corresponds to the given `pc' value in the process map
591 static struct pmcpl_ct_node *
592 pmcpl_ct_node_hash_lookup_pc(struct pmcpl_ct_node *parent,
593 struct pmcstat_pcmap *ppm, uintfptr_t pc, int pmcin)
595 struct pmcstat_symbol *sym;
596 struct pmcstat_image *image;
597 struct pmcpl_ct_node *ct;
598 struct pmcpl_ct_node_hash *h;
599 struct pmcpl_ct_arc *arc;
600 uintfptr_t loadaddress;
604 assert(parent != NULL);
606 image = ppm->ppm_image;
608 loadaddress = ppm->ppm_lowpc + image->pi_vaddr - image->pi_start;
609 pc -= loadaddress; /* Convert to an offset in the image. */
612 * Try determine the function at this offset. If we can't
613 * find a function round leave the `pc' value alone.
615 if ((sym = pmcstat_symbol_search(image, pc)) != NULL)
618 pmcstat_stats.ps_samples_unknown_function++;
620 for (hash = i = 0; i < (int)sizeof(uintfptr_t); i++)
621 hash += (pc >> i) & 0xFF;
623 hash &= PMCSTAT_HASH_MASK;
626 LIST_FOREACH(h, &pmcpl_ct_node_hash[hash], pch_next) {
631 if (ct->pct_image == image && ct->pct_func == pc) {
633 * Find related arc in parent node and
634 * increment the sample count.
636 for (i = 0; i < parent->pct_narc; i++) {
637 if (parent->pct_arc[i].pcta_child == ct) {
638 arc = &parent->pct_arc[i];
639 pmcpl_ct_samples_grow(&arc->pcta_samples);
640 arc->pcta_samples.sb[pmcin]++;
641 /* Estimate call count. */
642 pmcpl_ct_samples_grow(&arc->pcta_callid);
643 if (pmcpl_ct_callid.sb[pmcin] -
644 arc->pcta_callid.sb[pmcin] > 1)
646 arc->pcta_callid.sb[pmcin] =
647 pmcpl_ct_callid.sb[pmcin];
653 * No arc found for us, add ourself to the parent.
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;
670 * We haven't seen this (pmcid, pc) tuple yet, so allocate a
671 * new callgraph node and a new hash table entry for it.
673 ct = pmcpl_ct_node_allocate(image, pc);
674 if ((h = malloc(sizeof(*h))) == NULL)
675 err(EX_OSERR, "ERROR: Could not allocate callgraph node");
678 LIST_INSERT_HEAD(&pmcpl_ct_node_hash[hash], h, pch_next);
680 pmcpl_ct_arc_grow(parent->pct_narc,
681 &parent->pct_arc_c, &parent->pct_arc);
682 arc = &parent->pct_arc[parent->pct_narc];
683 pmcpl_ct_samples_grow(&arc->pcta_samples);
684 arc->pcta_samples.sb[pmcin] = 1;
686 pmcpl_ct_samples_grow(&arc->pcta_callid);
687 arc->pcta_callid.sb[pmcin] = pmcpl_ct_callid.sb[pmcin];
688 arc->pcta_child = ct;
694 * Record a callchain.
698 pmcpl_ct_process(struct pmcstat_process *pp, struct pmcstat_pmcrecord *pmcr,
699 uint32_t nsamples, uintfptr_t *cc, int usermode, uint32_t cpu)
702 struct pmcstat_pcmap *ppm[PMC_CALLCHAIN_DEPTH_MAX];
703 struct pmcstat_process *km;
704 struct pmcpl_ct_node *parent, *child;
708 assert(nsamples>0 && nsamples<=PMC_CALLCHAIN_DEPTH_MAX);
710 /* Get the PMC index. */
711 pmcin = pmcr->pr_pmcin;
714 * Validate mapping for the callchain.
715 * Go from bottom to first invalid entry.
717 km = pmcstat_kernproc;
718 for (n = 0; n < (int)nsamples; n++) {
719 ppm[n] = pmcstat_process_find_map(usermode ?
721 if (ppm[n] == NULL) {
722 /* Detect full frame capture (kernel + user). */
724 ppm[n] = pmcstat_process_find_map(pp, cc[n]);
733 pmcstat_stats.ps_callchain_dubious_frames++;
734 pmcr->pr_dubious_frames++;
738 /* Increase the call generation counter. */
739 pmcpl_ct_samples_grow(&pmcpl_ct_callid);
740 pmcpl_ct_callid.sb[pmcin]++;
743 * Iterate remaining addresses.
745 for (parent = pmcpl_ct_root, child = NULL; n >= 0; n--) {
746 child = pmcpl_ct_node_hash_lookup_pc(parent, ppm[n], cc[n],
749 pmcstat_stats.ps_callchain_dubious_frames++;
756 * Increment the sample count for this PMC.
759 pmcpl_ct_samples_grow(&child->pct_samples);
760 child->pct_samples.sb[pmcin]++;
762 /* Update per instruction sample if required. */
763 if (args.pa_ctdumpinstr)
764 pmcpl_ct_instr_add(child, pmcin, cc[0] -
765 (ppm[0]->ppm_lowpc + ppm[0]->ppm_image->pi_vaddr -
766 ppm[0]->ppm_image->pi_start));
771 * Print node self cost.
775 pmcpl_ct_node_printself(struct pmcpl_ct_node *ct)
779 struct pmcstat_symbol *sym;
780 char sourcefile[PATH_MAX];
781 char funcname[PATH_MAX];
786 #ifdef PMCPL_CT_OPTIMIZEFN
787 if (pmcpl_ct_prevfn != ct->pct_image->pi_fullpath) {
789 pmcpl_ct_prevfn = ct->pct_image->pi_fullpath;
790 fprintf(args.pa_graphfile, "ob=%s\n",
791 pmcstat_string_unintern(pmcpl_ct_prevfn));
792 #ifdef PMCPL_CT_OPTIMIZEFN
799 if (pmcstat_image_addr2line(ct->pct_image, ct->pct_func,
800 sourcefile, sizeof(sourcefile), &line,
801 funcname, sizeof(funcname))) {
802 fprintf(args.pa_graphfile, "fn=%s\n",
805 sym = pmcstat_symbol_search(ct->pct_image, ct->pct_func);
807 fprintf(args.pa_graphfile, "fn=%s\n",
808 pmcstat_string_unintern(sym->ps_name));
810 fprintf(args.pa_graphfile, "fn=%p\n",
811 (void *)(ct->pct_image->pi_vaddr + ct->pct_func));
817 if (ct->pct_ninstr > 0) {
818 for (i = 0; i < ct->pct_ninstr; i++) {
819 addr = ct->pct_image->pi_vaddr +
820 ct->pct_instr[i].pctf_func;
822 if (pmcstat_image_addr2line(ct->pct_image, addr,
823 sourcefile, sizeof(sourcefile), &line,
824 funcname, sizeof(funcname)))
825 fprintf(args.pa_graphfile, "fl=%s\n", sourcefile);
826 fprintf(args.pa_graphfile, "%p %u", (void *)addr, line);
827 for (j = 0; j<pmcstat_npmcs; j++)
828 fprintf(args.pa_graphfile, " %u",
830 &ct->pct_instr[i].pctf_samples));
831 fprintf(args.pa_graphfile, "\n");
834 addr = ct->pct_image->pi_vaddr + ct->pct_func;
836 if (pmcstat_image_addr2line(ct->pct_image, addr,
837 sourcefile, sizeof(sourcefile), &line,
838 funcname, sizeof(funcname)))
839 fprintf(args.pa_graphfile, "fl=%s\n", sourcefile);
840 fprintf(args.pa_graphfile, "* *");
841 for (i = 0; i<pmcstat_npmcs ; i++)
842 fprintf(args.pa_graphfile, " %u",
843 PMCPL_CT_SAMPLE(i, &ct->pct_samples));
844 fprintf(args.pa_graphfile, "\n");
849 * Print node child cost.
853 pmcpl_ct_node_printchild(struct pmcpl_ct_node *ct)
857 struct pmcstat_symbol *sym;
858 struct pmcpl_ct_node *child;
859 char sourcefile[PATH_MAX];
860 char funcname[PATH_MAX];
864 * TODO: attach child cost to the real position in the funtion.
865 * TODO: cfn=<fn> / call <ncall> addr(<fn>) / addr(call <fn>) <arccost>
867 for (i=0 ; i<ct->pct_narc; i++) {
868 child = ct->pct_arc[i].pcta_child;
871 #ifdef PMCPL_CT_OPTIMIZEFN
872 if (pmcpl_ct_prevfn != child->pct_image->pi_fullpath) {
874 pmcpl_ct_prevfn = child->pct_image->pi_fullpath;
875 fprintf(args.pa_graphfile, "cob=%s\n",
876 pmcstat_string_unintern(pmcpl_ct_prevfn));
877 #if PMCPL_CT_OPTIMIZEFN
880 /* Child function name. */
881 addr = child->pct_image->pi_vaddr + child->pct_func;
882 /* Child function source file. */
883 if (pmcstat_image_addr2line(child->pct_image, addr,
884 sourcefile, sizeof(sourcefile), &line,
885 funcname, sizeof(funcname))) {
886 fprintf(args.pa_graphfile, "cfn=%s\n", funcname);
887 fprintf(args.pa_graphfile, "cfl=%s\n", sourcefile);
889 sym = pmcstat_symbol_search(child->pct_image,
892 fprintf(args.pa_graphfile, "cfn=%s\n",
893 pmcstat_string_unintern(sym->ps_name));
895 fprintf(args.pa_graphfile, "cfn=%p\n", (void *)addr);
898 /* Child function address, line and call count. */
899 fprintf(args.pa_graphfile, "calls=%u %p %u\n",
900 ct->pct_arc[i].pcta_call, (void *)addr, line);
902 if (ct->pct_image != NULL) {
903 /* Call address, line, sample. */
904 addr = ct->pct_image->pi_vaddr + ct->pct_func;
906 if (pmcstat_image_addr2line(ct->pct_image, addr, sourcefile,
907 sizeof(sourcefile), &line,
908 funcname, sizeof(funcname)))
909 fprintf(args.pa_graphfile, "%p %u", (void *)addr, line);
911 fprintf(args.pa_graphfile, "* *");
914 fprintf(args.pa_graphfile, "* *");
915 for (j = 0; j<pmcstat_npmcs; j++)
916 fprintf(args.pa_graphfile, " %u",
917 PMCPL_CT_SAMPLE(j, &ct->pct_arc[i].pcta_samples));
918 fprintf(args.pa_graphfile, "\n");
923 * Clean the PMC name for Kcachegrind formula
927 pmcpl_ct_fixup_pmcname(char *s)
937 * Print a calltree (KCachegrind) for all PMCs.
944 struct pmcpl_ct_node_hash *pch;
945 struct pmcpl_ct_sample rsamples;
948 pmcpl_ct_samples_root(&rsamples);
949 pmcpl_ct_prevfn = NULL;
951 fprintf(args.pa_graphfile,
954 "positions: instr line\n"
956 for (i=0; i<pmcstat_npmcs; i++) {
957 snprintf(name, sizeof(name), "%s_%d",
958 pmcstat_pmcindex_to_name(i), i);
959 pmcpl_ct_fixup_pmcname(name);
960 fprintf(args.pa_graphfile, " %s", name);
962 fprintf(args.pa_graphfile, "\nsummary:");
963 for (i=0; i<pmcstat_npmcs ; i++)
964 fprintf(args.pa_graphfile, " %u",
965 PMCPL_CT_SAMPLE(i, &rsamples));
966 fprintf(args.pa_graphfile, "\n\n");
971 fprintf(args.pa_graphfile, "ob=FreeBSD\n");
972 fprintf(args.pa_graphfile, "fn=ROOT\n");
973 fprintf(args.pa_graphfile, "* *");
974 for (i = 0; i<pmcstat_npmcs ; i++)
975 fprintf(args.pa_graphfile, " 0");
976 fprintf(args.pa_graphfile, "\n");
977 pmcpl_ct_node_printchild(pmcpl_ct_root);
979 for (n = 0; n < PMCSTAT_NHASH; n++)
980 LIST_FOREACH(pch, &pmcpl_ct_node_hash[n], pch_next) {
981 pmcpl_ct_node_printself(pch->pch_ctnode);
982 pmcpl_ct_node_printchild(pch->pch_ctnode);
985 pmcpl_ct_samples_free(&rsamples);
989 pmcpl_ct_configure(char *opt)
992 if (strncmp(opt, "skiplink=", 9) == 0) {
993 pmcstat_skiplink = atoi(opt+9);
1005 pmcpl_ct_prevfn = NULL;
1006 pmcpl_ct_root = pmcpl_ct_node_allocate(NULL, 0);
1008 for (i = 0; i < PMCSTAT_NHASH; i++)
1009 LIST_INIT(&pmcpl_ct_node_hash[i]);
1011 pmcpl_ct_samples_init(&pmcpl_ct_callid);
1017 pmcpl_ct_shutdown(FILE *mf)
1020 struct pmcpl_ct_node_hash *pch, *pchtmp;
1024 if (args.pa_flags & FLAG_DO_CALLGRAPHS)
1031 for (i = 0; i < PMCSTAT_NHASH; i++) {
1032 LIST_FOREACH_SAFE(pch, &pmcpl_ct_node_hash[i], pch_next,
1034 pmcpl_ct_node_free(pch->pch_ctnode);
1039 pmcpl_ct_node_free(pmcpl_ct_root);
1040 pmcpl_ct_root = NULL;
1042 pmcpl_ct_samples_free(&pmcpl_ct_callid);