2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012, Fabien Thomas
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Process hwpmc(4) samples as calltree.
32 * Output file format compatible with Kcachegrind (kdesdk).
33 * Handle top mode with a sorted tree display.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/endian.h>
41 #include <sys/queue.h>
59 #include "pmcstat_log.h"
60 #include "pmcstat_top.h"
61 #include "pmcpl_calltree.h"
63 #define PMCPL_CT_GROWSIZE 4
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 struct pmcstat_image *pct_image;
101 struct pmcstat_symbol *pct_sym;
102 pmcstat_interned_string pct_ifl;
103 pmcstat_interned_string pct_ifn;
105 struct pmcpl_ct_sample pct_samples;
109 struct pmcpl_ct_arc *pct_arc;
111 /* TODO: optimize for large number of items. */
114 struct pmcpl_ct_instr *pct_instr;
116 #define PMCPL_PCT_ADDR 0
117 #define PMCPL_PCT_NAME 1
119 #define PMCPL_PCT_WHITE 0
120 #define PMCPL_PCT_GREY 1
121 #define PMCPL_PCT_BLACK 2
125 struct pmcpl_ct_node_hash {
126 struct pmcpl_ct_node *pch_ctnode;
127 STAILQ_ENTRY(pmcpl_ct_node_hash) pch_next;
130 static struct pmcpl_ct_sample pmcpl_ct_callid;
132 #define PMCPL_CT_MAXCOL PMC_CALLCHAIN_DEPTH_MAX
133 #define PMCPL_CT_MAXLINE 1024 /* TODO: dynamic. */
135 struct pmcpl_ct_line {
140 static struct pmcpl_ct_line pmcpl_ct_topmax[PMCPL_CT_MAXLINE+1];
141 static struct pmcpl_ct_node
142 *pmcpl_ct_topscreen[PMCPL_CT_MAXCOL+1][PMCPL_CT_MAXLINE+1];
145 * All nodes indexed by function/image name are placed in a hash table.
147 static STAILQ_HEAD(,pmcpl_ct_node_hash) pmcpl_ct_node_hash[PMCSTAT_NHASH];
150 * Root node for the graph.
152 static struct pmcpl_ct_node *pmcpl_ct_root;
159 * Initialize a samples.
163 pmcpl_ct_samples_init(struct pmcpl_ct_sample *samples)
175 pmcpl_ct_samples_free(struct pmcpl_ct_sample *samples)
184 * Grow a sample block to store pmcstat_npmcs PMCs.
188 pmcpl_ct_samples_grow(struct pmcpl_ct_sample *samples)
192 /* Enough storage. */
193 if (pmcstat_npmcs <= samples->npmcs)
196 npmcs = samples->npmcs +
197 max(pmcstat_npmcs - samples->npmcs, PMCPL_CT_GROWSIZE);
198 samples->sb = reallocarray(samples->sb, npmcs, sizeof(unsigned));
199 if (samples->sb == NULL)
200 errx(EX_SOFTWARE, "ERROR: out of memory");
201 bzero((char *)samples->sb + samples->npmcs * sizeof(unsigned),
202 (npmcs - samples->npmcs) * sizeof(unsigned));
203 samples->npmcs = npmcs;
207 * Compute the sum of all root arcs.
211 pmcpl_ct_samples_root(struct pmcpl_ct_sample *samples)
215 pmcpl_ct_samples_init(samples);
216 pmcpl_ct_samples_grow(samples);
218 for (i = 0; i < pmcpl_ct_root->pct_narc; i++)
219 for (pmcin = 0; pmcin < pmcstat_npmcs; pmcin++)
220 samples->sb[pmcin] += PMCPL_CT_SAMPLE(pmcin,
221 &pmcpl_ct_root->pct_arc[i].pcta_samples);
225 * Grow the arc table.
229 pmcpl_ct_arc_grow(int cursize, int *maxsize, struct pmcpl_ct_arc **items)
231 unsigned int nmaxsize;
233 if (cursize < *maxsize)
236 nmaxsize = *maxsize + max(cursize + 1 - *maxsize, PMCPL_CT_GROWSIZE);
237 *items = reallocarray(*items, nmaxsize, sizeof(struct pmcpl_ct_arc));
239 errx(EX_SOFTWARE, "ERROR: out of memory");
240 bzero((char *)*items + *maxsize * sizeof(struct pmcpl_ct_arc),
241 (nmaxsize - *maxsize) * sizeof(struct pmcpl_ct_arc));
246 * Grow the instr table.
250 pmcpl_ct_instr_grow(int cursize, int *maxsize, struct pmcpl_ct_instr **items)
252 unsigned int nmaxsize;
254 if (cursize < *maxsize)
257 nmaxsize = *maxsize + max(cursize + 1 - *maxsize, PMCPL_CT_GROWSIZE);
258 *items = reallocarray(*items, nmaxsize, sizeof(struct pmcpl_ct_instr));
260 errx(EX_SOFTWARE, "ERROR: out of memory");
261 bzero((char *)*items + *maxsize * sizeof(struct pmcpl_ct_instr),
262 (nmaxsize - *maxsize) * sizeof(struct pmcpl_ct_instr));
267 * Add a new instruction sample to given node.
271 pmcpl_ct_instr_add(struct pmcpl_ct_node *ct, int pmcin,
272 uintfptr_t pc, unsigned v)
275 struct pmcpl_ct_instr *in;
277 for (i = 0; i<ct->pct_ninstr; i++) {
278 if (ct->pct_instr[i].pctf_func == pc) {
279 in = &ct->pct_instr[i];
280 pmcpl_ct_samples_grow(&in->pctf_samples);
281 in->pctf_samples.sb[pmcin] += v;
286 pmcpl_ct_instr_grow(ct->pct_ninstr, &ct->pct_instr_c, &ct->pct_instr);
287 in = &ct->pct_instr[ct->pct_ninstr];
289 pmcpl_ct_samples_init(&in->pctf_samples);
290 pmcpl_ct_samples_grow(&in->pctf_samples);
291 in->pctf_samples.sb[pmcin] = v;
296 * Allocate a new node.
299 static struct pmcpl_ct_node *
300 pmcpl_ct_node_allocate(void)
302 struct pmcpl_ct_node *ct;
304 if ((ct = malloc(sizeof(*ct))) == NULL)
305 err(EX_OSERR, "ERROR: Cannot allocate callgraph node");
307 pmcpl_ct_samples_init(&ct->pct_samples);
310 ct->pct_image = NULL;
319 ct->pct_instr = NULL;
321 ct->pct_color = PMCPL_PCT_WHITE;
331 pmcpl_ct_node_free(struct pmcpl_ct_node *ct)
335 for (i = 0; i < ct->pct_narc; i++) {
336 pmcpl_ct_samples_free(&ct->pct_arc[i].pcta_samples);
337 pmcpl_ct_samples_free(&ct->pct_arc[i].pcta_callid);
340 pmcpl_ct_samples_free(&ct->pct_samples);
347 * Clear the graph tag on each node.
350 pmcpl_ct_node_cleartag(void)
353 struct pmcpl_ct_node_hash *pch;
355 for (i = 0; i < PMCSTAT_NHASH; i++)
356 STAILQ_FOREACH(pch, &pmcpl_ct_node_hash[i], pch_next)
357 pch->pch_ctnode->pct_color = PMCPL_PCT_WHITE;
359 pmcpl_ct_root->pct_color = PMCPL_PCT_WHITE;
363 * Print the callchain line by line with maximum cost at top.
367 pmcpl_ct_node_dumptop(int pmcin, struct pmcpl_ct_node *ct,
368 struct pmcpl_ct_sample *rsamples, int x, int *y)
371 struct pmcpl_ct_arc *arc;
373 if (ct->pct_color == PMCPL_PCT_GREY)
376 if (x >= PMCPL_CT_MAXCOL) {
377 pmcpl_ct_topscreen[x][*y] = NULL;
380 pmcpl_ct_topscreen[x][*y] = ct;
383 * Check if this is a terminal node.
384 * We need to check that some samples exist
385 * for at least one arc for that PMC.
388 for (i = 0; i < ct->pct_narc; i++) {
389 arc = &ct->pct_arc[i];
390 if (arc->pcta_child->pct_color != PMCPL_PCT_GREY &&
391 PMCPL_CT_SAMPLE(pmcin,
392 &arc->pcta_samples) != 0 &&
393 PMCPL_CT_SAMPLEP(pmcin,
394 &arc->pcta_samples) > pmcstat_threshold) {
400 if (ct->pct_narc == 0 || terminal) {
401 pmcpl_ct_topscreen[x+1][*y] = NULL;
402 if (*y >= PMCPL_CT_MAXLINE)
405 for (i=0; i < x; i++)
406 pmcpl_ct_topscreen[i][*y] =
407 pmcpl_ct_topscreen[i][*y - 1];
411 ct->pct_color = PMCPL_PCT_GREY;
412 for (i = 0; i < ct->pct_narc; i++) {
413 if (PMCPL_CT_SAMPLE(pmcin,
414 &ct->pct_arc[i].pcta_samples) == 0)
416 if (PMCPL_CT_SAMPLEP(pmcin,
417 &ct->pct_arc[i].pcta_samples) > pmcstat_threshold) {
418 if (pmcpl_ct_node_dumptop(pmcin,
419 ct->pct_arc[i].pcta_child,
421 ct->pct_color = PMCPL_PCT_BLACK;
426 ct->pct_color = PMCPL_PCT_BLACK;
432 * Compare two top line by sum.
435 pmcpl_ct_line_compare(const void *a, const void *b)
437 const struct pmcpl_ct_line *ct1, *ct2;
439 ct1 = (const struct pmcpl_ct_line *) a;
440 ct2 = (const struct pmcpl_ct_line *) b;
442 /* Sort in reverse order */
443 if (ct1->ln_sum < ct2->ln_sum)
445 if (ct1->ln_sum > ct2->ln_sum)
451 * Format and display given PMC index.
455 pmcpl_ct_node_printtop(struct pmcpl_ct_sample *rsamples, int pmcin, int maxy)
459 #define TS(x, y) (pmcpl_ct_topscreen[x][y])
460 #define TSI(x, y) (pmcpl_ct_topscreen[x][pmcpl_ct_topmax[y].ln_index])
462 int v_attrs, ns_len, vs_len, is_len, width, indentwidth, x, y;
464 char ns[30], vs[10], is[20];
465 struct pmcpl_ct_node *ct;
466 const char *space = " ";
476 pmcpl_ct_topmax[y].ln_sum = 0;
477 pmcpl_ct_topmax[y].ln_index = y;
478 for (x = 1; TS(x, y) != NULL; x++) {
479 pmcpl_ct_topmax[y].ln_sum +=
480 PMCPL_CT_SAMPLE(pmcin, &TS(x, y)->pct_samples);
483 qsort(pmcpl_ct_topmax, y, sizeof(pmcpl_ct_topmax[0]),
484 pmcpl_ct_line_compare);
485 pmcpl_ct_topmax[y].ln_index = y;
487 for (y = 0; y < maxy; y++) {
493 PMCSTAT_PRINTW("\n");
496 v = pmcpl_ct_topmax[y].ln_sum * 100.0 /
498 snprintf(vs, sizeof(vs), "%.1f", v);
499 v_attrs = PMCSTAT_ATTRPERCENT(v);
500 PMCSTAT_ATTRON(v_attrs);
501 PMCSTAT_PRINTW("%5.5s ", vs);
502 PMCSTAT_ATTROFF(v_attrs);
504 width = indentwidth = 5 + 1;
506 for (x = 1; (ct = TSI(x, y)) != NULL; x++) {
508 vs[0] = '\0'; vs_len = 0;
509 is[0] = '\0'; is_len = 0;
512 v = PMCPL_CT_SAMPLEP(pmcin, &ct->pct_samples);
513 if (v > pmcstat_threshold)
514 vs_len = snprintf(vs, sizeof(vs),
516 v_attrs = PMCSTAT_ATTRPERCENT(v);
518 if (pmcstat_skiplink && v <= pmcstat_threshold) {
519 strlcpy(ns, ".", sizeof(ns));
522 if (ct->pct_sym != NULL) {
523 ns_len = snprintf(ns, sizeof(ns), "%s",
524 pmcstat_string_unintern(ct->pct_sym->ps_name));
526 ns_len = snprintf(ns, sizeof(ns), "%p",
527 (void *)ct->pct_func);
531 TSI(x-1, y)->pct_image != ct->pct_image)
532 is_len = snprintf(is, sizeof(is), "@%s",
533 pmcstat_string_unintern(ct->pct_image->pi_name));
535 /* Check for line wrap. */
536 width += ns_len + is_len + vs_len + 1;
538 if (width >= pmcstat_displaywidth) {
542 PMCSTAT_PRINTW("\n%*s", indentwidth, space);
543 width = indentwidth + ns_len + is_len + vs_len;
546 PMCSTAT_ATTRON(v_attrs);
547 PMCSTAT_PRINTW("%s%s%s ", ns, is, vs);
548 PMCSTAT_ATTROFF(v_attrs);
554 * Output top mode snapshot.
558 pmcpl_ct_topdisplay(void)
561 struct pmcpl_ct_sample r, *rsamples;
564 pmcpl_ct_samples_root(rsamples);
565 pmcpl_ct_node_cleartag();
567 PMCSTAT_PRINTW("%5.5s %s\n", "%SAMP", "CALLTREE");
570 if (pmcpl_ct_node_dumptop(pmcstat_pmcinfilter,
571 pmcpl_ct_root, rsamples, 0, &y))
572 PMCSTAT_PRINTW("...\n");
573 pmcpl_ct_topscreen[1][y] = NULL;
575 pmcpl_ct_node_printtop(rsamples,
576 pmcstat_pmcinfilter, pmcstat_displayheight - 2);
578 pmcpl_ct_samples_free(rsamples);
582 * Handle top mode keypress.
586 pmcpl_ct_topkeypress(int c, WINDOW *w)
591 pmcstat_skiplink = !pmcstat_skiplink;
592 wprintw(w, "skip empty link %s",
593 pmcstat_skiplink ? "on" : "off");
601 * Look for a callgraph node associated with pmc `pmcid' in the global
602 * hash table that corresponds to the given `pc' value in the process map
607 pmcpl_ct_node_update(struct pmcpl_ct_node *parent,
608 struct pmcpl_ct_node *child, int pmcin, unsigned v, int cd)
610 struct pmcpl_ct_arc *arc;
613 assert(parent != NULL);
616 * Find related arc in parent node and
617 * increment the sample count.
619 for (i = 0; i < parent->pct_narc; i++) {
620 if (parent->pct_arc[i].pcta_child == child) {
621 arc = &parent->pct_arc[i];
622 pmcpl_ct_samples_grow(&arc->pcta_samples);
623 arc->pcta_samples.sb[pmcin] += v;
624 /* Estimate call count. */
626 pmcpl_ct_samples_grow(&arc->pcta_callid);
627 if (pmcpl_ct_callid.sb[pmcin] -
628 arc->pcta_callid.sb[pmcin] > 1)
630 arc->pcta_callid.sb[pmcin] =
631 pmcpl_ct_callid.sb[pmcin];
638 * No arc found for us, add ourself to the parent.
640 pmcpl_ct_arc_grow(parent->pct_narc,
641 &parent->pct_arc_c, &parent->pct_arc);
642 arc = &parent->pct_arc[parent->pct_narc];
643 pmcpl_ct_samples_grow(&arc->pcta_samples);
644 arc->pcta_samples.sb[pmcin] = v;
647 pmcpl_ct_samples_grow(&arc->pcta_callid);
648 arc->pcta_callid.sb[pmcin] = pmcpl_ct_callid.sb[pmcin];
650 arc->pcta_child = child;
655 * Lookup by image/pc.
658 static struct pmcpl_ct_node *
659 pmcpl_ct_node_hash_lookup(struct pmcstat_image *image, uintfptr_t pc,
660 struct pmcstat_symbol *sym, char *fl, char *fn)
664 struct pmcpl_ct_node *ct;
665 struct pmcpl_ct_node_hash *h;
666 pmcstat_interned_string ifl, ifn;
669 ifl = pmcstat_string_intern(fl);
670 ifn = pmcstat_string_intern(fn);
676 for (hash = i = 0; i < (int)sizeof(uintfptr_t); i++)
677 hash += (pc >> i) & 0xFF;
679 hash &= PMCSTAT_HASH_MASK;
681 STAILQ_FOREACH(h, &pmcpl_ct_node_hash[hash], pch_next) {
686 if (ct->pct_image == image && ct->pct_func == pc) {
689 if (ct->pct_type == PMCPL_PCT_NAME &&
690 ct->pct_ifl == ifl && ct->pct_ifn == ifn)
696 * We haven't seen this (pmcid, pc) tuple yet, so allocate a
697 * new callgraph node and a new hash table entry for it.
699 ct = pmcpl_ct_node_allocate();
700 if ((h = malloc(sizeof(*h))) == NULL)
701 err(EX_OSERR, "ERROR: Could not allocate callgraph node");
704 ct->pct_type = PMCPL_PCT_NAME;
708 ct->pct_type = PMCPL_PCT_ADDR;
709 ct->pct_image = image;
714 STAILQ_INSERT_HEAD(&pmcpl_ct_node_hash[hash], h, pch_next);
719 * Record a callchain.
723 pmcpl_ct_process(struct pmcstat_process *pp, struct pmcstat_pmcrecord *pmcr,
724 uint32_t nsamples, uintfptr_t *cc, int usermode, uint32_t cpu)
727 uintfptr_t pc, loadaddress;
728 struct pmcstat_image *image;
729 struct pmcstat_symbol *sym;
730 struct pmcstat_pcmap *ppm[PMC_CALLCHAIN_DEPTH_MAX];
731 struct pmcstat_process *km;
732 struct pmcpl_ct_node *ct;
733 struct pmcpl_ct_node *ctl[PMC_CALLCHAIN_DEPTH_MAX+1];
737 assert(nsamples>0 && nsamples<=PMC_CALLCHAIN_DEPTH_MAX);
739 /* Get the PMC index. */
740 pmcin = pmcr->pr_pmcin;
743 * Validate mapping for the callchain.
744 * Go from bottom to first invalid entry.
746 km = pmcstat_kernproc;
747 for (n = 0; n < (int)nsamples; n++) {
748 ppm[n] = pmcstat_process_find_map(usermode ?
750 if (ppm[n] == NULL) {
751 /* Detect full frame capture (kernel + user). */
753 ppm[n] = pmcstat_process_find_map(pp, cc[n]);
762 pmcstat_stats.ps_callchain_dubious_frames++;
763 pmcr->pr_dubious_frames++;
767 /* Increase the call generation counter. */
768 pmcpl_ct_samples_grow(&pmcpl_ct_callid);
769 pmcpl_ct_callid.sb[pmcin]++;
774 ctl[0] = pmcpl_ct_root;
775 for (i = 1; n >= 0; n--) {
776 image = ppm[n]->ppm_image;
777 loadaddress = ppm[n]->ppm_lowpc +
778 image->pi_vaddr - image->pi_start;
779 /* Convert to an offset in the image. */
780 pc = cc[n] - loadaddress;
782 * Try determine the function at this offset. If we can't
783 * find a function round leave the `pc' value alone.
785 if ((sym = pmcstat_symbol_search(image, pc)) != NULL)
788 pmcstat_stats.ps_samples_unknown_function++;
790 ct = pmcpl_ct_node_hash_lookup(image, pc, sym, NULL, NULL);
792 pmcstat_stats.ps_callchain_dubious_frames++;
797 /* No valid node found. */
803 for (i = 1; i < n; i++)
804 pmcpl_ct_node_update(ctl[i-1], ctl[i], pmcin, 1, 1);
807 * Increment the sample count for this PMC.
809 pmcpl_ct_samples_grow(&ctl[n-1]->pct_samples);
810 ctl[n-1]->pct_samples.sb[pmcin]++;
812 /* Update per instruction sample if required. */
813 if (args.pa_ctdumpinstr)
814 pmcpl_ct_instr_add(ctl[n-1], pmcin, cc[0] -
815 (ppm[0]->ppm_lowpc + ppm[0]->ppm_image->pi_vaddr -
816 ppm[0]->ppm_image->pi_start), 1);
820 * Print node child cost.
824 pmcpl_ct_node_printchild(struct pmcpl_ct_node *ct, uintfptr_t paddr,
829 struct pmcpl_ct_node *child;
830 char sourcefile[PATH_MAX];
831 char funcname[PATH_MAX];
835 * TODO: attach child cost to the real position in the function.
836 * TODO: cfn=<fn> / call <ncall> addr(<fn>) / addr(call <fn>) <arccost>
838 for (i=0 ; i<ct->pct_narc; i++) {
839 child = ct->pct_arc[i].pcta_child;
841 fprintf(args.pa_graphfile, "cob=%s\n",
842 pmcstat_string_unintern(child->pct_image->pi_fullpath));
843 /* Child function name. */
844 addr = child->pct_image->pi_vaddr + child->pct_func;
846 /* Child function source file. */
847 if (child->pct_type == PMCPL_PCT_NAME) {
848 fprintf(args.pa_graphfile, "cfi=%s\ncfn=%s\n",
849 pmcstat_string_unintern(child->pct_ifl),
850 pmcstat_string_unintern(child->pct_ifn));
851 } else if (pmcstat_image_addr2line(child->pct_image, addr,
852 sourcefile, sizeof(sourcefile), &line,
853 funcname, sizeof(funcname))) {
854 fprintf(args.pa_graphfile, "cfi=%s\ncfn=%s\n",
855 sourcefile, funcname);
857 if (child->pct_sym != NULL)
858 fprintf(args.pa_graphfile,
860 pmcstat_string_unintern(
861 child->pct_sym->ps_name));
863 fprintf(args.pa_graphfile,
864 "cfi=???\ncfn=%p\n", (void *)addr);
867 /* Child function address, line and call count. */
868 fprintf(args.pa_graphfile, "calls=%u %p %u\n",
869 ct->pct_arc[i].pcta_call, (void *)addr, line);
872 * Call address, line, sample.
873 * TODO: Associate call address to the right location.
875 fprintf(args.pa_graphfile, "%p %u", (void *)paddr, pline);
876 for (j = 0; j<pmcstat_npmcs; j++)
877 fprintf(args.pa_graphfile, " %u",
878 PMCPL_CT_SAMPLE(j, &ct->pct_arc[i].pcta_samples));
879 fprintf(args.pa_graphfile, "\n");
884 * Print node self cost.
888 pmcpl_ct_node_printself(struct pmcpl_ct_node *ct)
890 int i, j, fline, line;
891 uintfptr_t faddr, addr;
892 char sourcefile[PATH_MAX];
893 char funcname[PATH_MAX];
898 fprintf(args.pa_graphfile, "ob=%s\n",
899 pmcstat_string_unintern(ct->pct_image->pi_fullpath));
904 faddr = ct->pct_image->pi_vaddr + ct->pct_func;
906 if (ct->pct_type == PMCPL_PCT_NAME) {
907 fprintf(args.pa_graphfile, "fl=%s\nfn=%s\n",
908 pmcstat_string_unintern(ct->pct_ifl),
909 pmcstat_string_unintern(ct->pct_ifn));
910 } else if (pmcstat_image_addr2line(ct->pct_image, faddr,
911 sourcefile, sizeof(sourcefile), &fline,
912 funcname, sizeof(funcname))) {
913 fprintf(args.pa_graphfile, "fl=%s\nfn=%s\n",
914 sourcefile, funcname);
916 if (ct->pct_sym != NULL)
917 fprintf(args.pa_graphfile, "fl=???\nfn=%s\n",
918 pmcstat_string_unintern(ct->pct_sym->ps_name));
920 fprintf(args.pa_graphfile, "fl=???\nfn=%p\n",
921 (void *)(ct->pct_image->pi_vaddr + ct->pct_func));
927 if (ct->pct_ninstr > 0) {
931 for (i = 0; i < ct->pct_ninstr; i++) {
932 addr = ct->pct_image->pi_vaddr +
933 ct->pct_instr[i].pctf_func;
935 pmcstat_image_addr2line(ct->pct_image, addr,
936 sourcefile, sizeof(sourcefile), &line,
937 funcname, sizeof(funcname));
938 fprintf(args.pa_graphfile, "%p %u",
940 for (j = 0; j<pmcstat_npmcs; j++)
941 fprintf(args.pa_graphfile, " %u",
943 &ct->pct_instr[i].pctf_samples));
944 fprintf(args.pa_graphfile, "\n");
947 /* Global cost function cost. */
948 fprintf(args.pa_graphfile, "%p %u", (void *)faddr, fline);
949 for (i = 0; i<pmcstat_npmcs ; i++)
950 fprintf(args.pa_graphfile, " %u",
951 PMCPL_CT_SAMPLE(i, &ct->pct_samples));
952 fprintf(args.pa_graphfile, "\n");
955 pmcpl_ct_node_printchild(ct, faddr, fline);
959 pmcpl_ct_printnode(struct pmcpl_ct_node *ct)
963 if (ct == pmcpl_ct_root) {
964 fprintf(args.pa_graphfile, "fn=root\n");
965 fprintf(args.pa_graphfile, "0x0 1");
966 for (i = 0; i<pmcstat_npmcs ; i++)
967 fprintf(args.pa_graphfile, " 0");
968 fprintf(args.pa_graphfile, "\n");
969 pmcpl_ct_node_printchild(ct, 0, 0);
971 pmcpl_ct_node_printself(ct);
975 * Breadth first traversal.
979 pmcpl_ct_bfs(struct pmcpl_ct_node *ct)
982 struct pmcpl_ct_node_hash *pch, *pchc;
983 struct pmcpl_ct_node *child;
984 STAILQ_HEAD(,pmcpl_ct_node_hash) q;
987 if ((pch = malloc(sizeof(*pch))) == NULL)
988 err(EX_OSERR, "ERROR: Cannot allocate queue");
989 pch->pch_ctnode = ct;
990 STAILQ_INSERT_TAIL(&q, pch, pch_next);
991 ct->pct_color = PMCPL_PCT_BLACK;
993 while (!STAILQ_EMPTY(&q)) {
994 pch = STAILQ_FIRST(&q);
995 STAILQ_REMOVE_HEAD(&q, pch_next);
996 pmcpl_ct_printnode(pch->pch_ctnode);
997 for (i = 0; i<pch->pch_ctnode->pct_narc; i++) {
998 child = pch->pch_ctnode->pct_arc[i].pcta_child;
999 if (child->pct_color == PMCPL_PCT_WHITE) {
1000 child->pct_color = PMCPL_PCT_BLACK;
1001 if ((pchc = malloc(sizeof(*pchc))) == NULL)
1003 "ERROR: Cannot allocate queue");
1004 pchc->pch_ctnode = child;
1005 STAILQ_INSERT_TAIL(&q, pchc, pch_next);
1013 * Detect and fix inlined location.
1017 _pmcpl_ct_expand_inline(struct pmcpl_ct_node *ct)
1020 unsigned fline, line, v;
1021 uintfptr_t faddr, addr, pc;
1022 char sourcefile[PATH_MAX];
1023 char ffuncname[PATH_MAX], funcname[PATH_MAX];
1024 char buffer[PATH_MAX];
1025 struct pmcpl_ct_node *child;
1028 * Resolve parent and compare to each instr location.
1030 faddr = ct->pct_image->pi_vaddr + ct->pct_func;
1032 if (!pmcstat_image_addr2line(ct->pct_image, faddr,
1033 sourcefile, sizeof(sourcefile), &fline,
1034 ffuncname, sizeof(ffuncname)))
1037 for (i = 0; i < ct->pct_ninstr; i++) {
1038 addr = ct->pct_image->pi_vaddr +
1039 ct->pct_instr[i].pctf_func;
1041 if (!pmcstat_image_addr2line(ct->pct_image, addr,
1042 sourcefile, sizeof(sourcefile), &line,
1043 funcname, sizeof(funcname)))
1046 if (strcmp(funcname, ffuncname) == 0)
1050 * - Lookup/create inline node by function name.
1051 * - Move instr PMCs to the inline node.
1053 * The lookup create a specific node per image/pc.
1055 if (args.pa_verbosity >= 2)
1056 fprintf(args.pa_printfile,
1057 "WARNING: inlined function at %p %s in %s\n",
1058 (void *)addr, funcname, ffuncname);
1060 snprintf(buffer, sizeof(buffer), "%s@%s",
1061 funcname, ffuncname);
1062 child = pmcpl_ct_node_hash_lookup(ct->pct_image,
1063 ct->pct_func, ct->pct_sym, sourcefile, buffer);
1064 assert(child != NULL);
1065 pc = ct->pct_instr[i].pctf_func;
1066 for (j = 0; j<pmcstat_npmcs; j++) {
1067 v = PMCPL_CT_SAMPLE(j,
1068 &ct->pct_instr[i].pctf_samples);
1071 pmcpl_ct_instr_add(child, j, pc, v);
1072 pmcpl_ct_node_update(ct, child, j, v, 0);
1073 if (j < ct->pct_samples.npmcs)
1074 ct->pct_samples.sb[j] -=
1075 ct->pct_instr[i].pctf_samples.sb[j];
1076 ct->pct_instr[i].pctf_samples.sb[j] = 0;
1082 pmcpl_ct_expand_inline(void)
1085 struct pmcpl_ct_node_hash *pch;
1087 if (!args.pa_ctdumpinstr)
1090 for (i = 0; i < PMCSTAT_NHASH; i++)
1091 STAILQ_FOREACH(pch, &pmcpl_ct_node_hash[i], pch_next)
1092 if (pch->pch_ctnode->pct_type == PMCPL_PCT_ADDR)
1093 _pmcpl_ct_expand_inline(pch->pch_ctnode);
1097 * Clean the PMC name for Kcachegrind formula
1101 pmcpl_ct_fixup_pmcname(char *s)
1105 for (p = s; *p; p++)
1111 * Print a calltree (KCachegrind) for all PMCs.
1115 pmcpl_ct_print(void)
1119 struct pmcpl_ct_sample rsamples;
1121 pmcpl_ct_samples_root(&rsamples);
1122 pmcpl_ct_expand_inline();
1124 fprintf(args.pa_graphfile,
1126 "creator: pmcstat\n"
1127 "positions: instr line\n"
1129 for (i=0; i<pmcstat_npmcs; i++) {
1130 snprintf(name, sizeof(name), "%s_%d",
1131 pmcstat_pmcindex_to_name(i), i);
1132 pmcpl_ct_fixup_pmcname(name);
1133 fprintf(args.pa_graphfile, " %s", name);
1135 fprintf(args.pa_graphfile, "\nsummary:");
1136 for (i=0; i<pmcstat_npmcs ; i++)
1137 fprintf(args.pa_graphfile, " %u",
1138 PMCPL_CT_SAMPLE(i, &rsamples));
1139 fprintf(args.pa_graphfile, "\n");
1140 pmcpl_ct_bfs(pmcpl_ct_root);
1141 pmcpl_ct_samples_free(&rsamples);
1145 pmcpl_ct_configure(char *opt)
1148 if (strncmp(opt, "skiplink=", 9) == 0) {
1149 pmcstat_skiplink = atoi(opt+9);
1161 pmcpl_ct_root = pmcpl_ct_node_allocate();
1163 for (i = 0; i < PMCSTAT_NHASH; i++)
1164 STAILQ_INIT(&pmcpl_ct_node_hash[i]);
1166 pmcpl_ct_samples_init(&pmcpl_ct_callid);
1172 pmcpl_ct_shutdown(FILE *mf)
1175 struct pmcpl_ct_node_hash *pch, *pchtmp;
1179 if (args.pa_flags & FLAG_DO_CALLGRAPHS)
1186 for (i = 0; i < PMCSTAT_NHASH; i++) {
1187 STAILQ_FOREACH_SAFE(pch, &pmcpl_ct_node_hash[i], pch_next,
1189 pmcpl_ct_node_free(pch->pch_ctnode);
1194 pmcpl_ct_node_free(pmcpl_ct_root);
1195 pmcpl_ct_root = NULL;
1197 pmcpl_ct_samples_free(&pmcpl_ct_callid);