2 * Copyright (c) 2012, 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 int pmcstat_skiplink = 0;
68 /* Get the sample value for PMC a. */
69 #define PMCPL_CT_SAMPLE(a, b) \
70 ((a) < (b)->npmcs ? (b)->sb[a] : 0)
72 /* Get the sample value in percent related to rsamples. */
73 #define PMCPL_CT_SAMPLEP(a, b) \
74 (PMCPL_CT_SAMPLE(a, b) * 100.0 / rsamples->sb[a])
76 struct pmcpl_ct_sample {
77 int npmcs; /* Max pmc index available. */
78 unsigned *sb; /* Sample buffer for 0..npmcs. */
82 struct pmcpl_ct_sample pcta_samples;
83 struct pmcpl_ct_sample pcta_callid;
85 struct pmcpl_ct_node *pcta_child;
88 struct pmcpl_ct_instr {
90 struct pmcpl_ct_sample pctf_samples;
94 * Each calltree node is tracked by a pmcpl_ct_node struct.
96 struct pmcpl_ct_node {
97 struct pmcstat_image *pct_image;
100 struct pmcstat_symbol *pct_sym;
101 pmcstat_interned_string pct_ifl;
102 pmcstat_interned_string pct_ifn;
104 struct pmcpl_ct_sample pct_samples;
108 struct pmcpl_ct_arc *pct_arc;
110 /* TODO: optimize for large number of items. */
113 struct pmcpl_ct_instr *pct_instr;
115 #define PMCPL_PCT_ADDR 0
116 #define PMCPL_PCT_NAME 1
118 #define PMCPL_PCT_WHITE 0
119 #define PMCPL_PCT_GREY 1
120 #define PMCPL_PCT_BLACK 2
124 struct pmcpl_ct_node_hash {
125 struct pmcpl_ct_node *pch_ctnode;
126 STAILQ_ENTRY(pmcpl_ct_node_hash) pch_next;
129 struct pmcpl_ct_sample pmcpl_ct_callid;
131 #define PMCPL_CT_MAXCOL PMC_CALLCHAIN_DEPTH_MAX
132 #define PMCPL_CT_MAXLINE 1024 /* TODO: dynamic. */
134 struct pmcpl_ct_line {
139 struct pmcpl_ct_line pmcpl_ct_topmax[PMCPL_CT_MAXLINE+1];
141 *pmcpl_ct_topscreen[PMCPL_CT_MAXCOL+1][PMCPL_CT_MAXLINE+1];
144 * All nodes indexed by function/image name are placed in a hash table.
146 static STAILQ_HEAD(,pmcpl_ct_node_hash) pmcpl_ct_node_hash[PMCSTAT_NHASH];
149 * Root node for the graph.
151 static struct pmcpl_ct_node *pmcpl_ct_root;
158 * Initialize a samples.
162 pmcpl_ct_samples_init(struct pmcpl_ct_sample *samples)
174 pmcpl_ct_samples_free(struct pmcpl_ct_sample *samples)
183 * Grow a sample block to store pmcstat_npmcs PMCs.
187 pmcpl_ct_samples_grow(struct pmcpl_ct_sample *samples)
191 /* Enough storage. */
192 if (pmcstat_npmcs <= samples->npmcs)
195 npmcs = samples->npmcs +
196 max(pmcstat_npmcs - samples->npmcs, PMCPL_CT_GROWSIZE);
197 samples->sb = realloc(samples->sb, npmcs * sizeof(unsigned));
198 if (samples->sb == NULL)
199 errx(EX_SOFTWARE, "ERROR: out of memory");
200 bzero((char *)samples->sb + samples->npmcs * sizeof(unsigned),
201 (npmcs - samples->npmcs) * sizeof(unsigned));
202 samples->npmcs = npmcs;
206 * Compute the sum of all root arcs.
210 pmcpl_ct_samples_root(struct pmcpl_ct_sample *samples)
214 pmcpl_ct_samples_init(samples);
215 pmcpl_ct_samples_grow(samples);
217 for (i = 0; i < pmcpl_ct_root->pct_narc; i++)
218 for (pmcin = 0; pmcin < pmcstat_npmcs; pmcin++)
219 samples->sb[pmcin] += PMCPL_CT_SAMPLE(pmcin,
220 &pmcpl_ct_root->pct_arc[i].pcta_samples);
224 * Grow the arc table.
228 pmcpl_ct_arc_grow(int cursize, int *maxsize, struct pmcpl_ct_arc **items)
232 if (cursize < *maxsize)
235 nmaxsize = *maxsize + max(cursize + 1 - *maxsize, PMCPL_CT_GROWSIZE);
236 *items = realloc(*items, nmaxsize * sizeof(struct pmcpl_ct_arc));
238 errx(EX_SOFTWARE, "ERROR: out of memory");
239 bzero((char *)*items + *maxsize * sizeof(struct pmcpl_ct_arc),
240 (nmaxsize - *maxsize) * sizeof(struct pmcpl_ct_arc));
245 * Grow the instr table.
249 pmcpl_ct_instr_grow(int cursize, int *maxsize, struct pmcpl_ct_instr **items)
253 if (cursize < *maxsize)
256 nmaxsize = *maxsize + max(cursize + 1 - *maxsize, PMCPL_CT_GROWSIZE);
257 *items = realloc(*items, nmaxsize * sizeof(struct pmcpl_ct_instr));
259 errx(EX_SOFTWARE, "ERROR: out of memory");
260 bzero((char *)*items + *maxsize * sizeof(struct pmcpl_ct_instr),
261 (nmaxsize - *maxsize) * sizeof(struct pmcpl_ct_instr));
266 * Add a new instruction sample to given node.
270 pmcpl_ct_instr_add(struct pmcpl_ct_node *ct, int pmcin,
271 uintfptr_t pc, unsigned v)
274 struct pmcpl_ct_instr *in;
276 for (i = 0; i<ct->pct_ninstr; i++) {
277 if (ct->pct_instr[i].pctf_func == pc) {
278 in = &ct->pct_instr[i];
279 pmcpl_ct_samples_grow(&in->pctf_samples);
280 in->pctf_samples.sb[pmcin] += v;
285 pmcpl_ct_instr_grow(ct->pct_ninstr, &ct->pct_instr_c, &ct->pct_instr);
286 in = &ct->pct_instr[ct->pct_ninstr];
288 pmcpl_ct_samples_init(&in->pctf_samples);
289 pmcpl_ct_samples_grow(&in->pctf_samples);
290 in->pctf_samples.sb[pmcin] = v;
295 * Allocate a new node.
298 static struct pmcpl_ct_node *
299 pmcpl_ct_node_allocate(void)
301 struct pmcpl_ct_node *ct;
303 if ((ct = malloc(sizeof(*ct))) == NULL)
304 err(EX_OSERR, "ERROR: Cannot allocate callgraph node");
306 pmcpl_ct_samples_init(&ct->pct_samples);
309 ct->pct_image = NULL;
318 ct->pct_instr = NULL;
320 ct->pct_color = PMCPL_PCT_WHITE;
330 pmcpl_ct_node_free(struct pmcpl_ct_node *ct)
334 for (i = 0; i < ct->pct_narc; i++) {
335 pmcpl_ct_samples_free(&ct->pct_arc[i].pcta_samples);
336 pmcpl_ct_samples_free(&ct->pct_arc[i].pcta_callid);
339 pmcpl_ct_samples_free(&ct->pct_samples);
346 * Clear the graph tag on each node.
349 pmcpl_ct_node_cleartag(void)
352 struct pmcpl_ct_node_hash *pch;
354 for (i = 0; i < PMCSTAT_NHASH; i++)
355 STAILQ_FOREACH(pch, &pmcpl_ct_node_hash[i], pch_next)
356 pch->pch_ctnode->pct_color = PMCPL_PCT_WHITE;
358 pmcpl_ct_root->pct_color = PMCPL_PCT_WHITE;
362 * Print the callchain line by line with maximum cost at top.
366 pmcpl_ct_node_dumptop(int pmcin, struct pmcpl_ct_node *ct,
367 struct pmcpl_ct_sample *rsamples, int x, int *y)
370 struct pmcpl_ct_arc *arc;
372 if (ct->pct_color == PMCPL_PCT_GREY)
375 if (x >= PMCPL_CT_MAXCOL) {
376 pmcpl_ct_topscreen[x][*y] = NULL;
379 pmcpl_ct_topscreen[x][*y] = ct;
382 * Check if this is a terminal node.
383 * We need to check that some samples exist
384 * for at least one arc for that PMC.
387 for (i = 0; i < ct->pct_narc; i++) {
388 arc = &ct->pct_arc[i];
389 if (arc->pcta_child->pct_color != PMCPL_PCT_GREY &&
390 PMCPL_CT_SAMPLE(pmcin,
391 &arc->pcta_samples) != 0 &&
392 PMCPL_CT_SAMPLEP(pmcin,
393 &arc->pcta_samples) > pmcstat_threshold) {
399 if (ct->pct_narc == 0 || terminal) {
400 pmcpl_ct_topscreen[x+1][*y] = NULL;
401 if (*y >= PMCPL_CT_MAXLINE)
404 for (i=0; i < x; i++)
405 pmcpl_ct_topscreen[i][*y] =
406 pmcpl_ct_topscreen[i][*y - 1];
410 ct->pct_color = PMCPL_PCT_GREY;
411 for (i = 0; i < ct->pct_narc; i++) {
412 if (PMCPL_CT_SAMPLE(pmcin,
413 &ct->pct_arc[i].pcta_samples) == 0)
415 if (PMCPL_CT_SAMPLEP(pmcin,
416 &ct->pct_arc[i].pcta_samples) > pmcstat_threshold) {
417 if (pmcpl_ct_node_dumptop(pmcin,
418 ct->pct_arc[i].pcta_child,
420 ct->pct_color = PMCPL_PCT_BLACK;
425 ct->pct_color = PMCPL_PCT_BLACK;
431 * Compare two top line by sum.
434 pmcpl_ct_line_compare(const void *a, const void *b)
436 const struct pmcpl_ct_line *ct1, *ct2;
438 ct1 = (const struct pmcpl_ct_line *) a;
439 ct2 = (const struct pmcpl_ct_line *) b;
441 /* Sort in reverse order */
442 if (ct1->ln_sum < ct2->ln_sum)
444 if (ct1->ln_sum > ct2->ln_sum)
450 * Format and display given PMC index.
454 pmcpl_ct_node_printtop(struct pmcpl_ct_sample *rsamples, int pmcin, int maxy)
458 #define TS(x, y) (pmcpl_ct_topscreen[x][y])
459 #define TSI(x, y) (pmcpl_ct_topscreen[x][pmcpl_ct_topmax[y].ln_index])
461 int v_attrs, ns_len, vs_len, is_len, width, indentwidth, x, y;
463 char ns[30], vs[10], is[20];
464 struct pmcpl_ct_node *ct;
465 const char *space = " ";
475 pmcpl_ct_topmax[y].ln_sum = 0;
476 pmcpl_ct_topmax[y].ln_index = y;
477 for (x = 1; TS(x, y) != NULL; x++) {
478 pmcpl_ct_topmax[y].ln_sum +=
479 PMCPL_CT_SAMPLE(pmcin, &TS(x, y)->pct_samples);
482 qsort(pmcpl_ct_topmax, y, sizeof(pmcpl_ct_topmax[0]),
483 pmcpl_ct_line_compare);
484 pmcpl_ct_topmax[y].ln_index = y;
486 for (y = 0; y < maxy; y++) {
492 PMCSTAT_PRINTW("\n");
495 v = pmcpl_ct_topmax[y].ln_sum * 100.0 /
497 snprintf(vs, sizeof(vs), "%.1f", v);
498 v_attrs = PMCSTAT_ATTRPERCENT(v);
499 PMCSTAT_ATTRON(v_attrs);
500 PMCSTAT_PRINTW("%5.5s ", vs);
501 PMCSTAT_ATTROFF(v_attrs);
503 width = indentwidth = 5 + 1;
505 for (x = 1; (ct = TSI(x, y)) != NULL; x++) {
507 vs[0] = '\0'; vs_len = 0;
508 is[0] = '\0'; is_len = 0;
511 v = PMCPL_CT_SAMPLEP(pmcin, &ct->pct_samples);
512 if (v > pmcstat_threshold)
513 vs_len = snprintf(vs, sizeof(vs),
515 v_attrs = PMCSTAT_ATTRPERCENT(v);
517 if (pmcstat_skiplink && v <= pmcstat_threshold) {
518 strlcpy(ns, ".", sizeof(ns));
521 if (ct->pct_sym != NULL) {
522 ns_len = snprintf(ns, sizeof(ns), "%s",
523 pmcstat_string_unintern(ct->pct_sym->ps_name));
525 ns_len = snprintf(ns, sizeof(ns), "%p",
526 (void *)ct->pct_func);
530 TSI(x-1, y)->pct_image != ct->pct_image)
531 is_len = snprintf(is, sizeof(is), "@%s",
532 pmcstat_string_unintern(ct->pct_image->pi_name));
534 /* Check for line wrap. */
535 width += ns_len + is_len + vs_len + 1;
537 if (width >= pmcstat_displaywidth) {
541 PMCSTAT_PRINTW("\n%*s", indentwidth, space);
542 width = indentwidth + ns_len + is_len + vs_len;
545 PMCSTAT_ATTRON(v_attrs);
546 PMCSTAT_PRINTW("%s%s%s ", ns, is, vs);
547 PMCSTAT_ATTROFF(v_attrs);
553 * Output top mode snapshot.
557 pmcpl_ct_topdisplay(void)
560 struct pmcpl_ct_sample r, *rsamples;
563 pmcpl_ct_samples_root(rsamples);
564 pmcpl_ct_node_cleartag();
566 PMCSTAT_PRINTW("%5.5s %s\n", "%SAMP", "CALLTREE");
569 if (pmcpl_ct_node_dumptop(pmcstat_pmcinfilter,
570 pmcpl_ct_root, rsamples, 0, &y))
571 PMCSTAT_PRINTW("...\n");
572 pmcpl_ct_topscreen[1][y] = NULL;
574 pmcpl_ct_node_printtop(rsamples,
575 pmcstat_pmcinfilter, pmcstat_displayheight - 2);
577 pmcpl_ct_samples_free(rsamples);
581 * Handle top mode keypress.
585 pmcpl_ct_topkeypress(int c, WINDOW *w)
590 pmcstat_skiplink = !pmcstat_skiplink;
591 wprintw(w, "skip empty link %s", pmcstat_skiplink ? "on" : "off");
599 * Look for a callgraph node associated with pmc `pmcid' in the global
600 * hash table that corresponds to the given `pc' value in the process map
605 pmcpl_ct_node_update(struct pmcpl_ct_node *parent,
606 struct pmcpl_ct_node *child, int pmcin, unsigned v, int cd)
608 struct pmcpl_ct_arc *arc;
611 assert(parent != NULL);
614 * Find related arc in parent node and
615 * increment the sample count.
617 for (i = 0; i < parent->pct_narc; i++) {
618 if (parent->pct_arc[i].pcta_child == child) {
619 arc = &parent->pct_arc[i];
620 pmcpl_ct_samples_grow(&arc->pcta_samples);
621 arc->pcta_samples.sb[pmcin] += v;
622 /* Estimate call count. */
624 pmcpl_ct_samples_grow(&arc->pcta_callid);
625 if (pmcpl_ct_callid.sb[pmcin] -
626 arc->pcta_callid.sb[pmcin] > 1)
628 arc->pcta_callid.sb[pmcin] =
629 pmcpl_ct_callid.sb[pmcin];
636 * No arc found for us, add ourself to the parent.
638 pmcpl_ct_arc_grow(parent->pct_narc,
639 &parent->pct_arc_c, &parent->pct_arc);
640 arc = &parent->pct_arc[parent->pct_narc];
641 pmcpl_ct_samples_grow(&arc->pcta_samples);
642 arc->pcta_samples.sb[pmcin] = v;
645 pmcpl_ct_samples_grow(&arc->pcta_callid);
646 arc->pcta_callid.sb[pmcin] = pmcpl_ct_callid.sb[pmcin];
648 arc->pcta_child = child;
653 * Lookup by image/pc.
656 static struct pmcpl_ct_node *
657 pmcpl_ct_node_hash_lookup(struct pmcstat_image *image, uintfptr_t pc,
658 struct pmcstat_symbol *sym, char *fl, char *fn)
662 struct pmcpl_ct_node *ct;
663 struct pmcpl_ct_node_hash *h;
664 pmcstat_interned_string ifl, ifn;
667 ifl = pmcstat_string_intern(fl);
668 ifn = pmcstat_string_intern(fn);
674 for (hash = i = 0; i < (int)sizeof(uintfptr_t); i++)
675 hash += (pc >> i) & 0xFF;
677 hash &= PMCSTAT_HASH_MASK;
679 STAILQ_FOREACH(h, &pmcpl_ct_node_hash[hash], pch_next) {
684 if (ct->pct_image == image && ct->pct_func == pc) {
687 if (ct->pct_type == PMCPL_PCT_NAME &&
688 ct->pct_ifl == ifl && ct->pct_ifn == ifn)
694 * We haven't seen this (pmcid, pc) tuple yet, so allocate a
695 * new callgraph node and a new hash table entry for it.
697 ct = pmcpl_ct_node_allocate();
698 if ((h = malloc(sizeof(*h))) == NULL)
699 err(EX_OSERR, "ERROR: Could not allocate callgraph node");
702 ct->pct_type = PMCPL_PCT_NAME;
706 ct->pct_type = PMCPL_PCT_ADDR;
707 ct->pct_image = image;
712 STAILQ_INSERT_HEAD(&pmcpl_ct_node_hash[hash], h, pch_next);
717 * Record a callchain.
721 pmcpl_ct_process(struct pmcstat_process *pp, struct pmcstat_pmcrecord *pmcr,
722 uint32_t nsamples, uintfptr_t *cc, int usermode, uint32_t cpu)
725 uintfptr_t pc, loadaddress;
726 struct pmcstat_image *image;
727 struct pmcstat_symbol *sym;
728 struct pmcstat_pcmap *ppm[PMC_CALLCHAIN_DEPTH_MAX];
729 struct pmcstat_process *km;
730 struct pmcpl_ct_node *ct;
731 struct pmcpl_ct_node *ctl[PMC_CALLCHAIN_DEPTH_MAX+1];
735 assert(nsamples>0 && nsamples<=PMC_CALLCHAIN_DEPTH_MAX);
737 /* Get the PMC index. */
738 pmcin = pmcr->pr_pmcin;
741 * Validate mapping for the callchain.
742 * Go from bottom to first invalid entry.
744 km = pmcstat_kernproc;
745 for (n = 0; n < (int)nsamples; n++) {
746 ppm[n] = pmcstat_process_find_map(usermode ?
748 if (ppm[n] == NULL) {
749 /* Detect full frame capture (kernel + user). */
751 ppm[n] = pmcstat_process_find_map(pp, cc[n]);
760 pmcstat_stats.ps_callchain_dubious_frames++;
761 pmcr->pr_dubious_frames++;
765 /* Increase the call generation counter. */
766 pmcpl_ct_samples_grow(&pmcpl_ct_callid);
767 pmcpl_ct_callid.sb[pmcin]++;
772 ctl[0] = pmcpl_ct_root;
773 for (i = 1; n >= 0; n--) {
774 image = ppm[n]->ppm_image;
775 loadaddress = ppm[n]->ppm_lowpc +
776 image->pi_vaddr - image->pi_start;
777 /* Convert to an offset in the image. */
778 pc = cc[n] - loadaddress;
780 * Try determine the function at this offset. If we can't
781 * find a function round leave the `pc' value alone.
783 if ((sym = pmcstat_symbol_search(image, pc)) != NULL)
786 pmcstat_stats.ps_samples_unknown_function++;
788 ct = pmcpl_ct_node_hash_lookup(image, pc, sym, NULL, NULL);
790 pmcstat_stats.ps_callchain_dubious_frames++;
795 /* No valid node found. */
801 for (i = 1; i < n; i++)
802 pmcpl_ct_node_update(ctl[i-1], ctl[i], pmcin, 1, 1);
805 * Increment the sample count for this PMC.
807 pmcpl_ct_samples_grow(&ctl[n-1]->pct_samples);
808 ctl[n-1]->pct_samples.sb[pmcin]++;
810 /* Update per instruction sample if required. */
811 if (args.pa_ctdumpinstr)
812 pmcpl_ct_instr_add(ctl[n-1], pmcin, cc[0] -
813 (ppm[0]->ppm_lowpc + ppm[0]->ppm_image->pi_vaddr -
814 ppm[0]->ppm_image->pi_start), 1);
818 * Print node child cost.
822 pmcpl_ct_node_printchild(struct pmcpl_ct_node *ct, uintfptr_t paddr,
827 struct pmcpl_ct_node *child;
828 char sourcefile[PATH_MAX];
829 char funcname[PATH_MAX];
833 * TODO: attach child cost to the real position in the funtion.
834 * TODO: cfn=<fn> / call <ncall> addr(<fn>) / addr(call <fn>) <arccost>
836 for (i=0 ; i<ct->pct_narc; i++) {
837 child = ct->pct_arc[i].pcta_child;
839 fprintf(args.pa_graphfile, "cob=%s\n",
840 pmcstat_string_unintern(child->pct_image->pi_fullpath));
841 /* Child function name. */
842 addr = child->pct_image->pi_vaddr + child->pct_func;
844 /* Child function source file. */
845 if (child->pct_type == PMCPL_PCT_NAME) {
846 fprintf(args.pa_graphfile, "cfi=%s\ncfn=%s\n",
847 pmcstat_string_unintern(child->pct_ifl),
848 pmcstat_string_unintern(child->pct_ifn));
849 } else if (pmcstat_image_addr2line(child->pct_image, addr,
850 sourcefile, sizeof(sourcefile), &line,
851 funcname, sizeof(funcname))) {
852 fprintf(args.pa_graphfile, "cfi=%s\ncfn=%s\n",
853 sourcefile, funcname);
855 if (child->pct_sym != NULL)
856 fprintf(args.pa_graphfile,
858 pmcstat_string_unintern(
859 child->pct_sym->ps_name));
861 fprintf(args.pa_graphfile,
862 "cfi=???\ncfn=%p\n", (void *)addr);
865 /* Child function address, line and call count. */
866 fprintf(args.pa_graphfile, "calls=%u %p %u\n",
867 ct->pct_arc[i].pcta_call, (void *)addr, line);
870 * Call address, line, sample.
871 * TODO: Associate call address to the right location.
873 fprintf(args.pa_graphfile, "%p %u", (void *)paddr, pline);
874 for (j = 0; j<pmcstat_npmcs; j++)
875 fprintf(args.pa_graphfile, " %u",
876 PMCPL_CT_SAMPLE(j, &ct->pct_arc[i].pcta_samples));
877 fprintf(args.pa_graphfile, "\n");
882 * Print node self cost.
886 pmcpl_ct_node_printself(struct pmcpl_ct_node *ct)
888 int i, j, fline, line;
889 uintfptr_t faddr, addr;
890 char sourcefile[PATH_MAX];
891 char funcname[PATH_MAX];
896 fprintf(args.pa_graphfile, "ob=%s\n",
897 pmcstat_string_unintern(ct->pct_image->pi_fullpath));
902 faddr = ct->pct_image->pi_vaddr + ct->pct_func;
904 if (ct->pct_type == PMCPL_PCT_NAME) {
905 fprintf(args.pa_graphfile, "fl=%s\nfn=%s\n",
906 pmcstat_string_unintern(ct->pct_ifl),
907 pmcstat_string_unintern(ct->pct_ifn));
908 } else if (pmcstat_image_addr2line(ct->pct_image, faddr,
909 sourcefile, sizeof(sourcefile), &fline,
910 funcname, sizeof(funcname))) {
911 fprintf(args.pa_graphfile, "fl=%s\nfn=%s\n",
912 sourcefile, funcname);
914 if (ct->pct_sym != NULL)
915 fprintf(args.pa_graphfile, "fl=???\nfn=%s\n",
916 pmcstat_string_unintern(ct->pct_sym->ps_name));
918 fprintf(args.pa_graphfile, "fl=???\nfn=%p\n",
919 (void *)(ct->pct_image->pi_vaddr + ct->pct_func));
925 if (ct->pct_ninstr > 0) {
929 for (i = 0; i < ct->pct_ninstr; i++) {
930 addr = ct->pct_image->pi_vaddr +
931 ct->pct_instr[i].pctf_func;
933 pmcstat_image_addr2line(ct->pct_image, addr,
934 sourcefile, sizeof(sourcefile), &line,
935 funcname, sizeof(funcname));
936 fprintf(args.pa_graphfile, "%p %u",
938 for (j = 0; j<pmcstat_npmcs; j++)
939 fprintf(args.pa_graphfile, " %u",
941 &ct->pct_instr[i].pctf_samples));
942 fprintf(args.pa_graphfile, "\n");
945 /* Global cost function cost. */
946 fprintf(args.pa_graphfile, "%p %u", (void *)faddr, fline);
947 for (i = 0; i<pmcstat_npmcs ; i++)
948 fprintf(args.pa_graphfile, " %u",
949 PMCPL_CT_SAMPLE(i, &ct->pct_samples));
950 fprintf(args.pa_graphfile, "\n");
953 pmcpl_ct_node_printchild(ct, faddr, fline);
957 pmcpl_ct_printnode(struct pmcpl_ct_node *ct)
961 if (ct == pmcpl_ct_root) {
962 fprintf(args.pa_graphfile, "fn=root\n");
963 fprintf(args.pa_graphfile, "0x0 1");
964 for (i = 0; i<pmcstat_npmcs ; i++)
965 fprintf(args.pa_graphfile, " 0");
966 fprintf(args.pa_graphfile, "\n");
967 pmcpl_ct_node_printchild(ct, 0, 0);
969 pmcpl_ct_node_printself(ct);
973 * Breadth first traversal.
977 pmcpl_ct_bfs(struct pmcpl_ct_node *ct)
980 struct pmcpl_ct_node_hash *pch, *pchc;
981 struct pmcpl_ct_node *child;
982 STAILQ_HEAD(,pmcpl_ct_node_hash) q;
985 if ((pch = malloc(sizeof(*pch))) == NULL)
986 err(EX_OSERR, "ERROR: Cannot allocate queue");
987 pch->pch_ctnode = ct;
988 STAILQ_INSERT_TAIL(&q, pch, pch_next);
989 ct->pct_color = PMCPL_PCT_BLACK;
991 while (!STAILQ_EMPTY(&q)) {
992 pch = STAILQ_FIRST(&q);
993 STAILQ_REMOVE_HEAD(&q, pch_next);
994 pmcpl_ct_printnode(pch->pch_ctnode);
995 for (i = 0; i<pch->pch_ctnode->pct_narc; i++) {
996 child = pch->pch_ctnode->pct_arc[i].pcta_child;
997 if (child->pct_color == PMCPL_PCT_WHITE) {
998 child->pct_color = PMCPL_PCT_BLACK;
999 if ((pchc = malloc(sizeof(*pchc))) == NULL)
1001 "ERROR: Cannot allocate queue");
1002 pchc->pch_ctnode = child;
1003 STAILQ_INSERT_TAIL(&q, pchc, pch_next);
1011 * Detect and fix inlined location.
1015 _pmcpl_ct_expand_inline(struct pmcpl_ct_node *ct)
1018 unsigned fline, line, v;
1019 uintfptr_t faddr, addr, pc;
1020 char sourcefile[PATH_MAX];
1021 char ffuncname[PATH_MAX], funcname[PATH_MAX];
1022 char buffer[PATH_MAX];
1023 struct pmcpl_ct_node *child;
1027 * TODO: attach child cost to the real position in the funtion.
1028 * TODO: cfn=<fn> / call <ncall> addr(<fn>) / addr(call <fn>) <arccost>
1029 * Resolve parent and compare to each instr location.
1031 faddr = ct->pct_image->pi_vaddr + ct->pct_func;
1033 if (!pmcstat_image_addr2line(ct->pct_image, faddr,
1034 sourcefile, sizeof(sourcefile), &fline,
1035 ffuncname, sizeof(ffuncname)))
1038 for (i = 0; i < ct->pct_ninstr; i++) {
1039 addr = ct->pct_image->pi_vaddr +
1040 ct->pct_instr[i].pctf_func;
1042 if (!pmcstat_image_addr2line(ct->pct_image, addr,
1043 sourcefile, sizeof(sourcefile), &line,
1044 funcname, sizeof(funcname)))
1047 if (strcmp(funcname, ffuncname) == 0)
1051 * - Lookup/create inline node by function name.
1052 * - Move instr PMCs to the inline node.
1054 * The lookup create a specific node per image/pc.
1056 if (args.pa_verbosity >= 2)
1057 fprintf(args.pa_printfile,
1058 "WARNING: inlined function at %p %s in %s\n",
1059 (void *)addr, funcname, ffuncname);
1061 snprintf(buffer, sizeof(buffer), "%s@%s",
1062 funcname, ffuncname);
1063 child = pmcpl_ct_node_hash_lookup(ct->pct_image,
1064 ct->pct_func, ct->pct_sym, sourcefile, buffer);
1065 assert(child != NULL);
1066 pc = ct->pct_instr[i].pctf_func;
1067 for (j = 0; j<pmcstat_npmcs; j++) {
1068 v = PMCPL_CT_SAMPLE(j,
1069 &ct->pct_instr[i].pctf_samples);
1072 pmcpl_ct_instr_add(child, j, pc, v);
1073 pmcpl_ct_node_update(ct, child, j, v, 0);
1074 if (j < ct->pct_samples.npmcs)
1075 ct->pct_samples.sb[j] -=
1076 ct->pct_instr[i].pctf_samples.sb[j];
1077 ct->pct_instr[i].pctf_samples.sb[j] = 0;
1083 pmcpl_ct_expand_inline(void)
1086 struct pmcpl_ct_node_hash *pch;
1088 if (!args.pa_ctdumpinstr)
1091 for (i = 0; i < PMCSTAT_NHASH; i++)
1092 STAILQ_FOREACH(pch, &pmcpl_ct_node_hash[i], pch_next)
1093 if (pch->pch_ctnode->pct_type == PMCPL_PCT_ADDR)
1094 _pmcpl_ct_expand_inline(pch->pch_ctnode);
1098 * Clean the PMC name for Kcachegrind formula
1102 pmcpl_ct_fixup_pmcname(char *s)
1106 for (p = s; *p; p++)
1112 * Print a calltree (KCachegrind) for all PMCs.
1116 pmcpl_ct_print(void)
1120 struct pmcpl_ct_sample rsamples;
1122 pmcpl_ct_samples_root(&rsamples);
1123 pmcpl_ct_expand_inline();
1125 fprintf(args.pa_graphfile,
1127 "creator: pmcstat\n"
1128 "positions: instr line\n"
1130 for (i=0; i<pmcstat_npmcs; i++) {
1131 snprintf(name, sizeof(name), "%s_%d",
1132 pmcstat_pmcindex_to_name(i), i);
1133 pmcpl_ct_fixup_pmcname(name);
1134 fprintf(args.pa_graphfile, " %s", name);
1136 fprintf(args.pa_graphfile, "\nsummary:");
1137 for (i=0; i<pmcstat_npmcs ; i++)
1138 fprintf(args.pa_graphfile, " %u",
1139 PMCPL_CT_SAMPLE(i, &rsamples));
1140 fprintf(args.pa_graphfile, "\n");
1141 pmcpl_ct_bfs(pmcpl_ct_root);
1142 pmcpl_ct_samples_free(&rsamples);
1146 pmcpl_ct_configure(char *opt)
1149 if (strncmp(opt, "skiplink=", 9) == 0) {
1150 pmcstat_skiplink = atoi(opt+9);
1162 pmcpl_ct_root = pmcpl_ct_node_allocate();
1164 for (i = 0; i < PMCSTAT_NHASH; i++)
1165 STAILQ_INIT(&pmcpl_ct_node_hash[i]);
1167 pmcpl_ct_samples_init(&pmcpl_ct_callid);
1173 pmcpl_ct_shutdown(FILE *mf)
1176 struct pmcpl_ct_node_hash *pch, *pchtmp;
1180 if (args.pa_flags & FLAG_DO_CALLGRAPHS)
1187 for (i = 0; i < PMCSTAT_NHASH; i++) {
1188 STAILQ_FOREACH_SAFE(pch, &pmcpl_ct_node_hash[i], pch_next,
1190 pmcpl_ct_node_free(pch->pch_ctnode);
1195 pmcpl_ct_node_free(pmcpl_ct_root);
1196 pmcpl_ct_root = NULL;
1198 pmcpl_ct_samples_free(&pmcpl_ct_callid);