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>
57 #include "pmcstat_log.h"
58 #include "pmcstat_top.h"
59 #include "pmcpl_calltree.h"
61 #define PMCPL_CT_GROWSIZE 4
63 static int pmcstat_skiplink = 0;
67 /* Get the sample value for PMC a. */
68 #define PMCPL_CT_SAMPLE(a, b) \
69 ((a) < (b)->npmcs ? (b)->sb[a] : 0)
71 /* Get the sample value in percent related to rsamples. */
72 #define PMCPL_CT_SAMPLEP(a, b) \
73 (PMCPL_CT_SAMPLE(a, b) * 100.0 / rsamples->sb[a])
75 struct pmcpl_ct_sample {
76 int npmcs; /* Max pmc index available. */
77 unsigned *sb; /* Sample buffer for 0..npmcs. */
81 struct pmcpl_ct_sample pcta_samples;
82 struct pmcpl_ct_sample pcta_callid;
84 struct pmcpl_ct_node *pcta_child;
87 struct pmcpl_ct_instr {
89 struct pmcpl_ct_sample pctf_samples;
93 * Each calltree node is tracked by a pmcpl_ct_node struct.
95 struct pmcpl_ct_node {
96 struct pmcstat_image *pct_image;
99 struct pmcstat_symbol *pct_sym;
100 pmcstat_interned_string pct_ifl;
101 pmcstat_interned_string pct_ifn;
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;
114 #define PMCPL_PCT_ADDR 0
115 #define PMCPL_PCT_NAME 1
117 #define PMCPL_PCT_WHITE 0
118 #define PMCPL_PCT_GREY 1
119 #define PMCPL_PCT_BLACK 2
123 struct pmcpl_ct_node_hash {
124 struct pmcpl_ct_node *pch_ctnode;
125 STAILQ_ENTRY(pmcpl_ct_node_hash) pch_next;
128 static struct pmcpl_ct_sample pmcpl_ct_callid;
130 #define PMCPL_CT_MAXCOL PMC_CALLCHAIN_DEPTH_MAX
131 #define PMCPL_CT_MAXLINE 1024 /* TODO: dynamic. */
133 struct pmcpl_ct_line {
138 static struct pmcpl_ct_line pmcpl_ct_topmax[PMCPL_CT_MAXLINE+1];
139 static struct pmcpl_ct_node
140 *pmcpl_ct_topscreen[PMCPL_CT_MAXCOL+1][PMCPL_CT_MAXLINE+1];
143 * All nodes indexed by function/image name are placed in a hash table.
145 static STAILQ_HEAD(,pmcpl_ct_node_hash) pmcpl_ct_node_hash[PMCSTAT_NHASH];
148 * Root node for the graph.
150 static struct pmcpl_ct_node *pmcpl_ct_root;
157 * Initialize a samples.
161 pmcpl_ct_samples_init(struct pmcpl_ct_sample *samples)
173 pmcpl_ct_samples_free(struct pmcpl_ct_sample *samples)
182 * Grow a sample block to store pmcstat_npmcs PMCs.
186 pmcpl_ct_samples_grow(struct pmcpl_ct_sample *samples)
190 /* Enough storage. */
191 if (pmcstat_npmcs <= samples->npmcs)
194 npmcs = samples->npmcs +
195 max(pmcstat_npmcs - samples->npmcs, PMCPL_CT_GROWSIZE);
196 samples->sb = realloc(samples->sb, npmcs * sizeof(unsigned));
197 if (samples->sb == NULL)
198 errx(EX_SOFTWARE, "ERROR: out of memory");
199 bzero((char *)samples->sb + samples->npmcs * sizeof(unsigned),
200 (npmcs - samples->npmcs) * sizeof(unsigned));
201 samples->npmcs = npmcs;
205 * Compute the sum of all root arcs.
209 pmcpl_ct_samples_root(struct pmcpl_ct_sample *samples)
213 pmcpl_ct_samples_init(samples);
214 pmcpl_ct_samples_grow(samples);
216 for (i = 0; i < pmcpl_ct_root->pct_narc; i++)
217 for (pmcin = 0; pmcin < pmcstat_npmcs; pmcin++)
218 samples->sb[pmcin] += PMCPL_CT_SAMPLE(pmcin,
219 &pmcpl_ct_root->pct_arc[i].pcta_samples);
223 * Grow the arc table.
227 pmcpl_ct_arc_grow(int cursize, int *maxsize, struct pmcpl_ct_arc **items)
231 if (cursize < *maxsize)
234 nmaxsize = *maxsize + max(cursize + 1 - *maxsize, PMCPL_CT_GROWSIZE);
235 *items = realloc(*items, nmaxsize * sizeof(struct pmcpl_ct_arc));
237 errx(EX_SOFTWARE, "ERROR: out of memory");
238 bzero((char *)*items + *maxsize * sizeof(struct pmcpl_ct_arc),
239 (nmaxsize - *maxsize) * sizeof(struct pmcpl_ct_arc));
244 * Grow the instr table.
248 pmcpl_ct_instr_grow(int cursize, int *maxsize, struct pmcpl_ct_instr **items)
252 if (cursize < *maxsize)
255 nmaxsize = *maxsize + max(cursize + 1 - *maxsize, PMCPL_CT_GROWSIZE);
256 *items = realloc(*items, nmaxsize * sizeof(struct pmcpl_ct_instr));
258 errx(EX_SOFTWARE, "ERROR: out of memory");
259 bzero((char *)*items + *maxsize * sizeof(struct pmcpl_ct_instr),
260 (nmaxsize - *maxsize) * sizeof(struct pmcpl_ct_instr));
265 * Add a new instruction sample to given node.
269 pmcpl_ct_instr_add(struct pmcpl_ct_node *ct, int pmcin,
270 uintfptr_t pc, unsigned v)
273 struct pmcpl_ct_instr *in;
275 for (i = 0; i<ct->pct_ninstr; i++) {
276 if (ct->pct_instr[i].pctf_func == pc) {
277 in = &ct->pct_instr[i];
278 pmcpl_ct_samples_grow(&in->pctf_samples);
279 in->pctf_samples.sb[pmcin] += v;
284 pmcpl_ct_instr_grow(ct->pct_ninstr, &ct->pct_instr_c, &ct->pct_instr);
285 in = &ct->pct_instr[ct->pct_ninstr];
287 pmcpl_ct_samples_init(&in->pctf_samples);
288 pmcpl_ct_samples_grow(&in->pctf_samples);
289 in->pctf_samples.sb[pmcin] = v;
294 * Allocate a new node.
297 static struct pmcpl_ct_node *
298 pmcpl_ct_node_allocate(void)
300 struct pmcpl_ct_node *ct;
302 if ((ct = malloc(sizeof(*ct))) == NULL)
303 err(EX_OSERR, "ERROR: Cannot allocate callgraph node");
305 pmcpl_ct_samples_init(&ct->pct_samples);
308 ct->pct_image = NULL;
317 ct->pct_instr = NULL;
319 ct->pct_color = PMCPL_PCT_WHITE;
329 pmcpl_ct_node_free(struct pmcpl_ct_node *ct)
333 for (i = 0; i < ct->pct_narc; i++) {
334 pmcpl_ct_samples_free(&ct->pct_arc[i].pcta_samples);
335 pmcpl_ct_samples_free(&ct->pct_arc[i].pcta_callid);
338 pmcpl_ct_samples_free(&ct->pct_samples);
345 * Clear the graph tag on each node.
348 pmcpl_ct_node_cleartag(void)
351 struct pmcpl_ct_node_hash *pch;
353 for (i = 0; i < PMCSTAT_NHASH; i++)
354 STAILQ_FOREACH(pch, &pmcpl_ct_node_hash[i], pch_next)
355 pch->pch_ctnode->pct_color = PMCPL_PCT_WHITE;
357 pmcpl_ct_root->pct_color = PMCPL_PCT_WHITE;
361 * Print the callchain line by line with maximum cost at top.
365 pmcpl_ct_node_dumptop(int pmcin, struct pmcpl_ct_node *ct,
366 struct pmcpl_ct_sample *rsamples, int x, int *y)
369 struct pmcpl_ct_arc *arc;
371 if (ct->pct_color == PMCPL_PCT_GREY)
374 if (x >= PMCPL_CT_MAXCOL) {
375 pmcpl_ct_topscreen[x][*y] = NULL;
378 pmcpl_ct_topscreen[x][*y] = ct;
381 * Check if this is a terminal node.
382 * We need to check that some samples exist
383 * for at least one arc for that PMC.
386 for (i = 0; i < ct->pct_narc; i++) {
387 arc = &ct->pct_arc[i];
388 if (arc->pcta_child->pct_color != PMCPL_PCT_GREY &&
389 PMCPL_CT_SAMPLE(pmcin,
390 &arc->pcta_samples) != 0 &&
391 PMCPL_CT_SAMPLEP(pmcin,
392 &arc->pcta_samples) > pmcstat_threshold) {
398 if (ct->pct_narc == 0 || terminal) {
399 pmcpl_ct_topscreen[x+1][*y] = NULL;
400 if (*y >= PMCPL_CT_MAXLINE)
403 for (i=0; i < x; i++)
404 pmcpl_ct_topscreen[i][*y] =
405 pmcpl_ct_topscreen[i][*y - 1];
409 ct->pct_color = PMCPL_PCT_GREY;
410 for (i = 0; i < ct->pct_narc; i++) {
411 if (PMCPL_CT_SAMPLE(pmcin,
412 &ct->pct_arc[i].pcta_samples) == 0)
414 if (PMCPL_CT_SAMPLEP(pmcin,
415 &ct->pct_arc[i].pcta_samples) > pmcstat_threshold) {
416 if (pmcpl_ct_node_dumptop(pmcin,
417 ct->pct_arc[i].pcta_child,
419 ct->pct_color = PMCPL_PCT_BLACK;
424 ct->pct_color = PMCPL_PCT_BLACK;
430 * Compare two top line by sum.
433 pmcpl_ct_line_compare(const void *a, const void *b)
435 const struct pmcpl_ct_line *ct1, *ct2;
437 ct1 = (const struct pmcpl_ct_line *) a;
438 ct2 = (const struct pmcpl_ct_line *) b;
440 /* Sort in reverse order */
441 if (ct1->ln_sum < ct2->ln_sum)
443 if (ct1->ln_sum > ct2->ln_sum)
449 * Format and display given PMC index.
453 pmcpl_ct_node_printtop(struct pmcpl_ct_sample *rsamples, int pmcin, int maxy)
457 #define TS(x, y) (pmcpl_ct_topscreen[x][y])
458 #define TSI(x, y) (pmcpl_ct_topscreen[x][pmcpl_ct_topmax[y].ln_index])
460 int v_attrs, ns_len, vs_len, is_len, width, indentwidth, x, y;
462 char ns[30], vs[10], is[20];
463 struct pmcpl_ct_node *ct;
464 const char *space = " ";
474 pmcpl_ct_topmax[y].ln_sum = 0;
475 pmcpl_ct_topmax[y].ln_index = y;
476 for (x = 1; TS(x, y) != NULL; x++) {
477 pmcpl_ct_topmax[y].ln_sum +=
478 PMCPL_CT_SAMPLE(pmcin, &TS(x, y)->pct_samples);
481 qsort(pmcpl_ct_topmax, y, sizeof(pmcpl_ct_topmax[0]),
482 pmcpl_ct_line_compare);
483 pmcpl_ct_topmax[y].ln_index = y;
485 for (y = 0; y < maxy; y++) {
491 PMCSTAT_PRINTW("\n");
494 v = pmcpl_ct_topmax[y].ln_sum * 100.0 /
496 snprintf(vs, sizeof(vs), "%.1f", v);
497 v_attrs = PMCSTAT_ATTRPERCENT(v);
498 PMCSTAT_ATTRON(v_attrs);
499 PMCSTAT_PRINTW("%5.5s ", vs);
500 PMCSTAT_ATTROFF(v_attrs);
502 width = indentwidth = 5 + 1;
504 for (x = 1; (ct = TSI(x, y)) != NULL; x++) {
506 vs[0] = '\0'; vs_len = 0;
507 is[0] = '\0'; is_len = 0;
510 v = PMCPL_CT_SAMPLEP(pmcin, &ct->pct_samples);
511 if (v > pmcstat_threshold)
512 vs_len = snprintf(vs, sizeof(vs),
514 v_attrs = PMCSTAT_ATTRPERCENT(v);
516 if (pmcstat_skiplink && v <= pmcstat_threshold) {
517 strlcpy(ns, ".", sizeof(ns));
520 if (ct->pct_sym != NULL) {
521 ns_len = snprintf(ns, sizeof(ns), "%s",
522 pmcstat_string_unintern(ct->pct_sym->ps_name));
524 ns_len = snprintf(ns, sizeof(ns), "%p",
525 (void *)ct->pct_func);
529 TSI(x-1, y)->pct_image != ct->pct_image)
530 is_len = snprintf(is, sizeof(is), "@%s",
531 pmcstat_string_unintern(ct->pct_image->pi_name));
533 /* Check for line wrap. */
534 width += ns_len + is_len + vs_len + 1;
536 if (width >= pmcstat_displaywidth) {
540 PMCSTAT_PRINTW("\n%*s", indentwidth, space);
541 width = indentwidth + ns_len + is_len + vs_len;
544 PMCSTAT_ATTRON(v_attrs);
545 PMCSTAT_PRINTW("%s%s%s ", ns, is, vs);
546 PMCSTAT_ATTROFF(v_attrs);
552 * Output top mode snapshot.
556 pmcpl_ct_topdisplay(void)
559 struct pmcpl_ct_sample r, *rsamples;
562 pmcpl_ct_samples_root(rsamples);
563 pmcpl_ct_node_cleartag();
565 PMCSTAT_PRINTW("%5.5s %s\n", "%SAMP", "CALLTREE");
568 if (pmcpl_ct_node_dumptop(pmcstat_pmcinfilter,
569 pmcpl_ct_root, rsamples, 0, &y))
570 PMCSTAT_PRINTW("...\n");
571 pmcpl_ct_topscreen[1][y] = NULL;
573 pmcpl_ct_node_printtop(rsamples,
574 pmcstat_pmcinfilter, pmcstat_displayheight - 2);
576 pmcpl_ct_samples_free(rsamples);
580 * Handle top mode keypress.
584 pmcpl_ct_topkeypress(int c, WINDOW *w)
589 pmcstat_skiplink = !pmcstat_skiplink;
590 wprintw(w, "skip empty link %s",
591 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 function.
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;
1026 * Resolve parent and compare to each instr location.
1028 faddr = ct->pct_image->pi_vaddr + ct->pct_func;
1030 if (!pmcstat_image_addr2line(ct->pct_image, faddr,
1031 sourcefile, sizeof(sourcefile), &fline,
1032 ffuncname, sizeof(ffuncname)))
1035 for (i = 0; i < ct->pct_ninstr; i++) {
1036 addr = ct->pct_image->pi_vaddr +
1037 ct->pct_instr[i].pctf_func;
1039 if (!pmcstat_image_addr2line(ct->pct_image, addr,
1040 sourcefile, sizeof(sourcefile), &line,
1041 funcname, sizeof(funcname)))
1044 if (strcmp(funcname, ffuncname) == 0)
1048 * - Lookup/create inline node by function name.
1049 * - Move instr PMCs to the inline node.
1051 * The lookup create a specific node per image/pc.
1053 if (args.pa_verbosity >= 2)
1054 fprintf(args.pa_printfile,
1055 "WARNING: inlined function at %p %s in %s\n",
1056 (void *)addr, funcname, ffuncname);
1058 snprintf(buffer, sizeof(buffer), "%s@%s",
1059 funcname, ffuncname);
1060 child = pmcpl_ct_node_hash_lookup(ct->pct_image,
1061 ct->pct_func, ct->pct_sym, sourcefile, buffer);
1062 assert(child != NULL);
1063 pc = ct->pct_instr[i].pctf_func;
1064 for (j = 0; j<pmcstat_npmcs; j++) {
1065 v = PMCPL_CT_SAMPLE(j,
1066 &ct->pct_instr[i].pctf_samples);
1069 pmcpl_ct_instr_add(child, j, pc, v);
1070 pmcpl_ct_node_update(ct, child, j, v, 0);
1071 if (j < ct->pct_samples.npmcs)
1072 ct->pct_samples.sb[j] -=
1073 ct->pct_instr[i].pctf_samples.sb[j];
1074 ct->pct_instr[i].pctf_samples.sb[j] = 0;
1080 pmcpl_ct_expand_inline(void)
1083 struct pmcpl_ct_node_hash *pch;
1085 if (!args.pa_ctdumpinstr)
1088 for (i = 0; i < PMCSTAT_NHASH; i++)
1089 STAILQ_FOREACH(pch, &pmcpl_ct_node_hash[i], pch_next)
1090 if (pch->pch_ctnode->pct_type == PMCPL_PCT_ADDR)
1091 _pmcpl_ct_expand_inline(pch->pch_ctnode);
1095 * Clean the PMC name for Kcachegrind formula
1099 pmcpl_ct_fixup_pmcname(char *s)
1103 for (p = s; *p; p++)
1109 * Print a calltree (KCachegrind) for all PMCs.
1113 pmcpl_ct_print(void)
1117 struct pmcpl_ct_sample rsamples;
1119 pmcpl_ct_samples_root(&rsamples);
1120 pmcpl_ct_expand_inline();
1122 fprintf(args.pa_graphfile,
1124 "creator: pmcstat\n"
1125 "positions: instr line\n"
1127 for (i=0; i<pmcstat_npmcs; i++) {
1128 snprintf(name, sizeof(name), "%s_%d",
1129 pmcstat_pmcindex_to_name(i), i);
1130 pmcpl_ct_fixup_pmcname(name);
1131 fprintf(args.pa_graphfile, " %s", name);
1133 fprintf(args.pa_graphfile, "\nsummary:");
1134 for (i=0; i<pmcstat_npmcs ; i++)
1135 fprintf(args.pa_graphfile, " %u",
1136 PMCPL_CT_SAMPLE(i, &rsamples));
1137 fprintf(args.pa_graphfile, "\n");
1138 pmcpl_ct_bfs(pmcpl_ct_root);
1139 pmcpl_ct_samples_free(&rsamples);
1143 pmcpl_ct_configure(char *opt)
1146 if (strncmp(opt, "skiplink=", 9) == 0) {
1147 pmcstat_skiplink = atoi(opt+9);
1159 pmcpl_ct_root = pmcpl_ct_node_allocate();
1161 for (i = 0; i < PMCSTAT_NHASH; i++)
1162 STAILQ_INIT(&pmcpl_ct_node_hash[i]);
1164 pmcpl_ct_samples_init(&pmcpl_ct_callid);
1170 pmcpl_ct_shutdown(FILE *mf)
1173 struct pmcpl_ct_node_hash *pch, *pchtmp;
1177 if (args.pa_flags & FLAG_DO_CALLGRAPHS)
1184 for (i = 0; i < PMCSTAT_NHASH; i++) {
1185 STAILQ_FOREACH_SAFE(pch, &pmcpl_ct_node_hash[i], pch_next,
1187 pmcpl_ct_node_free(pch->pch_ctnode);
1192 pmcpl_ct_node_free(pmcpl_ct_root);
1193 pmcpl_ct_root = NULL;
1195 pmcpl_ct_samples_free(&pmcpl_ct_callid);