xref: /freebsd/usr.sbin/pmcstat/pmcpl_calltree.c (revision 46514c778360a66b0715d4df1e2b752b06bb49ff)
1 /*-
2  * Copyright (c) 2009, Fabien Thomas
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Process hwpmc(4) samples as calltree.
29  *
30  * Output file format compatible with Kcachegrind (kdesdk).
31  * Handle top mode with a sorted tree display.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 #include <sys/endian.h>
39 #include <sys/queue.h>
40 
41 #include <assert.h>
42 #include <curses.h>
43 #include <ctype.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <pmc.h>
48 #include <pmclog.h>
49 #include <sysexits.h>
50 #include <stdint.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <sysexits.h>
56 
57 #include "pmcstat.h"
58 #include "pmcstat_log.h"
59 #include "pmcstat_top.h"
60 #include "pmcpl_calltree.h"
61 
62 #define PMCPL_CT_GROWSIZE	4
63 
64 static pmcstat_interned_string pmcpl_ct_prevfn;
65 
66 static int pmcstat_skiplink = 0;
67 
68 struct pmcpl_ct_node;
69 
70 /* Get the sample value for PMC a. */
71 #define PMCPL_CT_SAMPLE(a, b) \
72 	((a) < (b)->npmcs ? (b)->sb[a] : 0)
73 
74 /* Get the sample value in percent related to rsamples. */
75 #define PMCPL_CT_SAMPLEP(a, b) \
76 	(PMCPL_CT_SAMPLE(a, b) * 100.0 / rsamples->sb[a])
77 
78 struct pmcpl_ct_sample {
79 	int		npmcs;		/* Max pmc index available. */
80 	unsigned	*sb;		/* Sample buffer for 0..npmcs. */
81 };
82 
83 struct pmcpl_ct_arc {
84 	struct pmcpl_ct_sample	pcta_samples;
85 	struct pmcpl_ct_sample	pcta_callid;
86 	unsigned		pcta_call;
87 	struct pmcpl_ct_node	*pcta_child;
88 };
89 
90 struct pmcpl_ct_instr {
91 	uintfptr_t		pctf_func;
92 	struct pmcpl_ct_sample	pctf_samples;
93 };
94 
95 /*
96  * Each calltree node is tracked by a pmcpl_ct_node struct.
97  */
98 struct pmcpl_ct_node {
99 #define PMCPL_PCT_TAG	0x00000001	/* Loop detection. */
100 	uint32_t		pct_flags;
101 	struct pmcstat_image	*pct_image;
102 	uintfptr_t		pct_func;
103 	struct pmcpl_ct_sample	pct_samples;
104 
105 	int			pct_narc;
106 	int			pct_arc_c;
107 	struct pmcpl_ct_arc 	*pct_arc;
108 
109 	/* TODO: optimize for large number of items. */
110 	int			pct_ninstr;
111 	int			pct_instr_c;
112 	struct pmcpl_ct_instr	*pct_instr;
113 };
114 
115 struct pmcpl_ct_node_hash {
116 	struct pmcpl_ct_node  *pch_ctnode;
117 	LIST_ENTRY(pmcpl_ct_node_hash) pch_next;
118 };
119 
120 struct pmcpl_ct_sample pmcpl_ct_callid;
121 
122 #define PMCPL_CT_MAXCOL		PMC_CALLCHAIN_DEPTH_MAX
123 #define PMCPL_CT_MAXLINE	1024	/* TODO: dynamic. */
124 
125 struct pmcpl_ct_line {
126 	unsigned	ln_sum;
127 	unsigned	ln_index;
128 };
129 
130 struct pmcpl_ct_line	pmcpl_ct_topmax[PMCPL_CT_MAXLINE+1];
131 struct pmcpl_ct_node	*pmcpl_ct_topscreen[PMCPL_CT_MAXCOL+1][PMCPL_CT_MAXLINE+1];
132 
133 /*
134  * All nodes indexed by function/image name are placed in a hash table.
135  */
136 static LIST_HEAD(,pmcpl_ct_node_hash) pmcpl_ct_node_hash[PMCSTAT_NHASH];
137 
138 /*
139  * Root node for the graph.
140  */
141 static struct pmcpl_ct_node *pmcpl_ct_root;
142 
143 /*
144  * Prototypes
145  */
146 
147 /*
148  * Initialize a samples.
149  */
150 
151 static void
152 pmcpl_ct_samples_init(struct pmcpl_ct_sample *samples)
153 {
154 
155 	samples->npmcs = 0;
156 	samples->sb = NULL;
157 }
158 
159 /*
160  * Free a samples.
161  */
162 
163 static void
164 pmcpl_ct_samples_free(struct pmcpl_ct_sample *samples)
165 {
166 
167 	samples->npmcs = 0;
168 	free(samples->sb);
169 	samples->sb = NULL;
170 }
171 
172 /*
173  * Grow a sample block to store pmcstat_npmcs PMCs.
174  */
175 
176 static void
177 pmcpl_ct_samples_grow(struct pmcpl_ct_sample *samples)
178 {
179 	int npmcs;
180 
181 	/* Enough storage. */
182 	if (pmcstat_npmcs <= samples->npmcs)
183                 return;
184 
185 	npmcs = samples->npmcs +
186 	    max(pmcstat_npmcs - samples->npmcs, PMCPL_CT_GROWSIZE);
187 	samples->sb = realloc(samples->sb, npmcs * sizeof(unsigned));
188 	if (samples->sb == NULL)
189 		errx(EX_SOFTWARE, "ERROR: out of memory");
190 	bzero((char *)samples->sb + samples->npmcs * sizeof(unsigned),
191 	    (npmcs - samples->npmcs) * sizeof(unsigned));
192 	samples->npmcs = npmcs;
193 }
194 
195 /*
196  * Compute the sum of all root arcs.
197  */
198 
199 static void
200 pmcpl_ct_samples_root(struct pmcpl_ct_sample *samples)
201 {
202 	int i, pmcin;
203 
204 	pmcpl_ct_samples_init(samples);
205 	pmcpl_ct_samples_grow(samples);
206 
207 	for (i = 0; i < pmcpl_ct_root->pct_narc; i++)
208 		for (pmcin = 0; pmcin < pmcstat_npmcs; pmcin++)
209 			samples->sb[pmcin] += PMCPL_CT_SAMPLE(pmcin,
210 			    &pmcpl_ct_root->pct_arc[i].pcta_samples);
211 }
212 
213 /*
214  * Grow the arc table.
215  */
216 
217 static void
218 pmcpl_ct_arc_grow(int cursize, int *maxsize, struct pmcpl_ct_arc **items)
219 {
220 	int nmaxsize;
221 
222 	if (cursize < *maxsize)
223 		return;
224 
225 	nmaxsize = *maxsize + max(cursize + 1 - *maxsize, PMCPL_CT_GROWSIZE);
226 	*items = realloc(*items, nmaxsize * sizeof(struct pmcpl_ct_arc));
227 	if (*items == NULL)
228 		errx(EX_SOFTWARE, "ERROR: out of memory");
229 	bzero((char *)*items + *maxsize * sizeof(struct pmcpl_ct_arc),
230 	    (nmaxsize - *maxsize) * sizeof(struct pmcpl_ct_arc));
231 	*maxsize = nmaxsize;
232 }
233 
234 /*
235  * Grow the instr table.
236  */
237 
238 static void
239 pmcpl_ct_instr_grow(int cursize, int *maxsize, struct pmcpl_ct_instr **items)
240 {
241 	int nmaxsize;
242 
243 	if (cursize < *maxsize)
244 		return;
245 
246 	nmaxsize = *maxsize + max(cursize + 1 - *maxsize, PMCPL_CT_GROWSIZE);
247 	*items = realloc(*items, nmaxsize * sizeof(struct pmcpl_ct_instr));
248 	if (*items == NULL)
249 		errx(EX_SOFTWARE, "ERROR: out of memory");
250 	bzero((char *)*items + *maxsize * sizeof(struct pmcpl_ct_instr),
251 	    (nmaxsize - *maxsize) * sizeof(struct pmcpl_ct_instr));
252 	*maxsize = nmaxsize;
253 }
254 
255 /*
256  * Add a new instruction sample to given node.
257  */
258 
259 static void
260 pmcpl_ct_instr_add(struct pmcpl_ct_node *ct, int pmcin, uintfptr_t pc)
261 {
262 	int i;
263 	struct pmcpl_ct_instr *in;
264 
265 	for (i = 0; i<ct->pct_ninstr; i++) {
266 		if (ct->pct_instr[i].pctf_func == pc) {
267 			in = &ct->pct_instr[i];
268 			pmcpl_ct_samples_grow(&in->pctf_samples);
269 			in->pctf_samples.sb[pmcin]++;
270 			return;
271 		}
272 	}
273 
274 	pmcpl_ct_instr_grow(ct->pct_ninstr, &ct->pct_instr_c, &ct->pct_instr);
275 	in = &ct->pct_instr[ct->pct_ninstr];
276 	in->pctf_func = pc;
277 	pmcpl_ct_samples_init(&in->pctf_samples);
278 	pmcpl_ct_samples_grow(&in->pctf_samples);
279 	in->pctf_samples.sb[pmcin] = 1;
280 	ct->pct_ninstr++;
281 }
282 
283 /*
284  * Allocate a new node.
285  */
286 
287 static struct pmcpl_ct_node *
288 pmcpl_ct_node_allocate(struct pmcstat_image *image, uintfptr_t pc)
289 {
290 	struct pmcpl_ct_node *ct;
291 
292 	if ((ct = malloc(sizeof(*ct))) == NULL)
293 		err(EX_OSERR, "ERROR: Cannot allocate callgraph node");
294 
295 	ct->pct_flags	= 0;
296 	ct->pct_image 	= image;
297 	ct->pct_func	= pc;
298 
299 	pmcpl_ct_samples_init(&ct->pct_samples);
300 
301 	ct->pct_narc	= 0;
302 	ct->pct_arc_c	= 0;
303 	ct->pct_arc	= NULL;
304 
305 	ct->pct_ninstr	= 0;
306 	ct->pct_instr_c	= 0;
307 	ct->pct_instr	= NULL;
308 
309 	return (ct);
310 }
311 
312 /*
313  * Free a node.
314  */
315 
316 static void
317 pmcpl_ct_node_free(struct pmcpl_ct_node *ct)
318 {
319 	int i;
320 
321 	for (i = 0; i < ct->pct_narc; i++) {
322 		pmcpl_ct_samples_free(&ct->pct_arc[i].pcta_samples);
323 		pmcpl_ct_samples_free(&ct->pct_arc[i].pcta_callid);
324 	}
325 
326 	pmcpl_ct_samples_free(&ct->pct_samples);
327 	free(ct->pct_arc);
328 	free(ct->pct_instr);
329 	free(ct);
330 }
331 
332 /*
333  * Clear the graph tag on each node.
334  */
335 static void
336 pmcpl_ct_node_cleartag(void)
337 {
338 	int i;
339 	struct pmcpl_ct_node_hash *pch;
340 
341 	for (i = 0; i < PMCSTAT_NHASH; i++)
342 		LIST_FOREACH(pch, &pmcpl_ct_node_hash[i], pch_next)
343 			pch->pch_ctnode->pct_flags &= ~PMCPL_PCT_TAG;
344 
345 	pmcpl_ct_root->pct_flags &= ~PMCPL_PCT_TAG;
346 }
347 
348 /*
349  * Print the callchain line by line with maximum cost at top.
350  */
351 
352 static int
353 pmcpl_ct_node_dumptop(int pmcin, struct pmcpl_ct_node *ct,
354     struct pmcpl_ct_sample *rsamples, int x, int *y)
355 {
356 	int i, terminal;
357 	struct pmcpl_ct_arc *arc;
358 
359 	if (ct->pct_flags & PMCPL_PCT_TAG)
360 		return 0;
361 
362 	ct->pct_flags |= PMCPL_PCT_TAG;
363 
364 	if (x >= PMCPL_CT_MAXCOL) {
365 		pmcpl_ct_topscreen[x][*y] = NULL;
366 		return 1;
367 	}
368 	pmcpl_ct_topscreen[x][*y] = ct;
369 
370 	/*
371 	 * Check if this is a terminal node.
372 	 * We need to check that some samples exist
373 	 * for at least one arc for that PMC.
374 	 */
375 	terminal = 1;
376 	for (i = 0; i < ct->pct_narc; i++) {
377 		arc = &ct->pct_arc[i];
378 		if (PMCPL_CT_SAMPLE(pmcin,
379 		    &arc->pcta_samples) != 0 &&
380 		    PMCPL_CT_SAMPLEP(pmcin,
381 		    &arc->pcta_samples) > pmcstat_threshold &&
382 		    (arc->pcta_child->pct_flags & PMCPL_PCT_TAG) == 0) {
383 			terminal = 0;
384 			break;
385 		}
386 	}
387 
388 	if (ct->pct_narc == 0 || terminal) {
389 		pmcpl_ct_topscreen[x+1][*y] = NULL;
390 		if (*y >= PMCPL_CT_MAXLINE)
391 			return 1;
392 		*y = *y + 1;
393 		for (i=0; i < x; i++)
394 			pmcpl_ct_topscreen[i][*y] =
395 			    pmcpl_ct_topscreen[i][*y - 1];
396 		return 0;
397 	}
398 
399 	for (i = 0; i < ct->pct_narc; i++) {
400 		if (PMCPL_CT_SAMPLE(pmcin,
401 		    &ct->pct_arc[i].pcta_samples) == 0)
402 			continue;
403 		if (PMCPL_CT_SAMPLEP(pmcin,
404 		    &ct->pct_arc[i].pcta_samples) > pmcstat_threshold) {
405 			if (pmcpl_ct_node_dumptop(pmcin,
406 			        ct->pct_arc[i].pcta_child,
407 			        rsamples, x+1, y))
408 				return 1;
409 		}
410 	}
411 
412 	return 0;
413 }
414 
415 /*
416  * Compare two top line by sum.
417  */
418 static int
419 pmcpl_ct_line_compare(const void *a, const void *b)
420 {
421 	const struct pmcpl_ct_line *ct1, *ct2;
422 
423 	ct1 = (const struct pmcpl_ct_line *) a;
424 	ct2 = (const struct pmcpl_ct_line *) b;
425 
426 	/* Sort in reverse order */
427 	if (ct1->ln_sum < ct2->ln_sum)
428 		return (1);
429 	if (ct1->ln_sum > ct2->ln_sum)
430 		return (-1);
431 	return (0);
432 }
433 
434 /*
435  * Format and display given PMC index.
436  */
437 
438 static void
439 pmcpl_ct_node_printtop(struct pmcpl_ct_sample *rsamples, int pmcin, int maxy)
440 {
441 #undef	TS
442 #undef	TSI
443 #define	TS(x, y)	(pmcpl_ct_topscreen[x][y])
444 #define	TSI(x, y)	(pmcpl_ct_topscreen[x][pmcpl_ct_topmax[y].ln_index])
445 
446 	int v_attrs, ns_len, vs_len, is_len, width, indentwidth, x, y;
447 	float v;
448 	char ns[30], vs[10], is[20];
449 	struct pmcpl_ct_node *ct;
450 	struct pmcstat_symbol *sym;
451 	const char *space = " ";
452 
453 	/*
454 	 * Sort by line cost.
455 	 */
456 	for (y = 0; ; y++) {
457 		ct = TS(1, y);
458 		if (ct == NULL)
459 			break;
460 
461 		pmcpl_ct_topmax[y].ln_sum = 0;
462 		pmcpl_ct_topmax[y].ln_index = y;
463 		for (x = 1; TS(x, y) != NULL; x++) {
464 			pmcpl_ct_topmax[y].ln_sum +=
465 			    PMCPL_CT_SAMPLE(pmcin, &TS(x, y)->pct_samples);
466 		}
467 	}
468 	qsort(pmcpl_ct_topmax, y, sizeof(pmcpl_ct_topmax[0]),
469 	    pmcpl_ct_line_compare);
470 	pmcpl_ct_topmax[y].ln_index = y;
471 
472 	for (y = 0; y < maxy; y++) {
473 		ct = TSI(1, y);
474 		if (ct == NULL)
475 			break;
476 
477 		if (y > 0)
478 			PMCSTAT_PRINTW("\n");
479 
480 		/* Output sum. */
481 		v = pmcpl_ct_topmax[y].ln_sum * 100.0 /
482 		    rsamples->sb[pmcin];
483 		snprintf(vs, sizeof(vs), "%.1f", v);
484 		v_attrs = PMCSTAT_ATTRPERCENT(v);
485 		PMCSTAT_ATTRON(v_attrs);
486 		PMCSTAT_PRINTW("%5.5s ", vs);
487 		PMCSTAT_ATTROFF(v_attrs);
488 
489 		width = indentwidth = 5 + 1;
490 
491 		for (x = 1; (ct = TSI(x, y)) != NULL; x++) {
492 
493 			vs[0] = '\0'; vs_len = 0;
494 			is[0] = '\0'; is_len = 0;
495 
496 			/* Format value. */
497 			v = PMCPL_CT_SAMPLEP(pmcin, &ct->pct_samples);
498 			if (v > pmcstat_threshold)
499 				vs_len  = snprintf(vs, sizeof(vs),
500 				    "(%.1f%%)", v);
501 			v_attrs = PMCSTAT_ATTRPERCENT(v);
502 
503 			if (pmcstat_skiplink && v <= pmcstat_threshold) {
504 				strlcpy(ns, ".", sizeof(ns));
505 				ns_len = 1;
506 			} else {
507 			sym = pmcstat_symbol_search(ct->pct_image, ct->pct_func);
508 			if (sym != NULL) {
509 				ns_len = snprintf(ns, sizeof(ns), "%s",
510 				    pmcstat_string_unintern(sym->ps_name));
511 			} else
512 				ns_len = snprintf(ns, sizeof(ns), "%p",
513 				    (void *)ct->pct_func);
514 
515 			/* Format image. */
516 			if (x == 1 ||
517 			    TSI(x-1, y)->pct_image != ct->pct_image)
518 				is_len = snprintf(is, sizeof(is), "@%s",
519 				    pmcstat_string_unintern(ct->pct_image->pi_name));
520 
521 			/* Check for line wrap. */
522 			width += ns_len + is_len + vs_len + 1;
523 			}
524 			if (width >= pmcstat_displaywidth) {
525 				maxy--;
526 				if (y >= maxy)
527 					break;
528 				PMCSTAT_PRINTW("\n%*s", indentwidth, space);
529 				width = indentwidth + ns_len + is_len + vs_len;
530 			}
531 
532 			PMCSTAT_ATTRON(v_attrs);
533 			PMCSTAT_PRINTW("%s%s%s ", ns, is, vs);
534 			PMCSTAT_ATTROFF(v_attrs);
535 		}
536 	}
537 }
538 
539 /*
540  * Output top mode snapshot.
541  */
542 
543 void
544 pmcpl_ct_topdisplay(void)
545 {
546 	int y;
547 	struct pmcpl_ct_sample r, *rsamples;
548 
549 	rsamples = &r;
550 	pmcpl_ct_samples_root(rsamples);
551 
552 	pmcpl_ct_node_cleartag();
553 
554 	PMCSTAT_PRINTW("%5.5s %s\n", "%SAMP", "CALLTREE");
555 
556 	y = 0;
557 	if (pmcpl_ct_node_dumptop(pmcstat_pmcinfilter,
558 	    pmcpl_ct_root, rsamples, 0, &y))
559 		PMCSTAT_PRINTW("...\n");
560 	pmcpl_ct_topscreen[1][y] = NULL;
561 
562 	pmcpl_ct_node_printtop(rsamples,
563 	    pmcstat_pmcinfilter, pmcstat_displayheight - 2);
564 
565 	pmcpl_ct_samples_free(rsamples);
566 }
567 
568 /*
569  * Handle top mode keypress.
570  */
571 
572 int
573 pmcpl_ct_topkeypress(int c, WINDOW *w)
574 {
575 
576 	switch (c) {
577 	case 'f':
578 		pmcstat_skiplink = !pmcstat_skiplink;
579 		wprintw(w, "skip empty link %s", pmcstat_skiplink ? "on" : "off");
580 		break;
581 	}
582 
583 	return 0;
584 }
585 
586 /*
587  * Look for a callgraph node associated with pmc `pmcid' in the global
588  * hash table that corresponds to the given `pc' value in the process map
589  * `ppm'.
590  */
591 
592 static struct pmcpl_ct_node *
593 pmcpl_ct_node_hash_lookup_pc(struct pmcpl_ct_node *parent,
594     struct pmcstat_pcmap *ppm, uintfptr_t pc, int pmcin)
595 {
596 	struct pmcstat_symbol *sym;
597 	struct pmcstat_image *image;
598 	struct pmcpl_ct_node *ct;
599 	struct pmcpl_ct_node_hash *h;
600 	struct pmcpl_ct_arc *arc;
601 	uintfptr_t loadaddress;
602 	int i;
603 	unsigned int hash;
604 
605 	assert(parent != NULL);
606 
607 	image = ppm->ppm_image;
608 
609 	loadaddress = ppm->ppm_lowpc + image->pi_vaddr - image->pi_start;
610 	pc -= loadaddress;	/* Convert to an offset in the image. */
611 
612 	/*
613 	 * Try determine the function at this offset.  If we can't
614 	 * find a function round leave the `pc' value alone.
615 	 */
616 	if ((sym = pmcstat_symbol_search(image, pc)) != NULL)
617 		pc = sym->ps_start;
618 
619 	for (hash = i = 0; i < (int)sizeof(uintfptr_t); i++)
620 		hash += (pc >> i) & 0xFF;
621 
622 	hash &= PMCSTAT_HASH_MASK;
623 
624 	ct = NULL;
625 	LIST_FOREACH(h, &pmcpl_ct_node_hash[hash], pch_next) {
626 		ct = h->pch_ctnode;
627 
628 		assert(ct != NULL);
629 
630 		if (ct->pct_image == image && ct->pct_func == pc) {
631 			/*
632 			 * Find related arc in parent node and
633 			 * increment the sample count.
634 			 */
635 			for (i = 0; i < parent->pct_narc; i++) {
636 				if (parent->pct_arc[i].pcta_child == ct) {
637 					arc = &parent->pct_arc[i];
638 					pmcpl_ct_samples_grow(&arc->pcta_samples);
639 					arc->pcta_samples.sb[pmcin]++;
640 					/* Estimate call count. */
641 					pmcpl_ct_samples_grow(&arc->pcta_callid);
642 					if (pmcpl_ct_callid.sb[pmcin] -
643 					    arc->pcta_callid.sb[pmcin] > 1)
644 						arc->pcta_call++;
645 					arc->pcta_callid.sb[pmcin] =
646 					    pmcpl_ct_callid.sb[pmcin];
647 					return (ct);
648 				}
649 			}
650 
651 			/*
652 			 * No arc found for us, add ourself to the parent.
653 			 */
654 			pmcpl_ct_arc_grow(parent->pct_narc,
655 			    &parent->pct_arc_c, &parent->pct_arc);
656 			arc = &parent->pct_arc[parent->pct_narc];
657 			pmcpl_ct_samples_grow(&arc->pcta_samples);
658 			arc->pcta_samples.sb[pmcin] = 1;
659 			arc->pcta_call = 1;
660 			pmcpl_ct_samples_grow(&arc->pcta_callid);
661 			arc->pcta_callid.sb[pmcin] = pmcpl_ct_callid.sb[pmcin];
662 			arc->pcta_child = ct;
663 			parent->pct_narc++;
664 			return (ct);
665 		}
666 	}
667 
668 	/*
669 	 * We haven't seen this (pmcid, pc) tuple yet, so allocate a
670 	 * new callgraph node and a new hash table entry for it.
671 	 */
672 	ct = pmcpl_ct_node_allocate(image, pc);
673 	if ((h = malloc(sizeof(*h))) == NULL)
674 		err(EX_OSERR, "ERROR: Could not allocate callgraph node");
675 
676 	h->pch_ctnode = ct;
677 	LIST_INSERT_HEAD(&pmcpl_ct_node_hash[hash], h, pch_next);
678 
679 	pmcpl_ct_arc_grow(parent->pct_narc,
680 	    &parent->pct_arc_c, &parent->pct_arc);
681 	arc = &parent->pct_arc[parent->pct_narc];
682 	pmcpl_ct_samples_grow(&arc->pcta_samples);
683 	arc->pcta_samples.sb[pmcin] = 1;
684 	arc->pcta_call = 1;
685 	pmcpl_ct_samples_grow(&arc->pcta_callid);
686 	arc->pcta_callid.sb[pmcin] = pmcpl_ct_callid.sb[pmcin];
687 	arc->pcta_child = ct;
688 	parent->pct_narc++;
689 	return (ct);
690 }
691 
692 /*
693  * Record a callchain.
694  */
695 
696 void
697 pmcpl_ct_process(struct pmcstat_process *pp, struct pmcstat_pmcrecord *pmcr,
698     uint32_t nsamples, uintfptr_t *cc, int usermode, uint32_t cpu)
699 {
700 	int n, pmcin;
701 	struct pmcstat_pcmap *ppm[PMC_CALLCHAIN_DEPTH_MAX];
702 	struct pmcstat_process *km;
703 	struct pmcpl_ct_node *parent, *child;
704 
705 	(void) cpu;
706 
707 	assert(nsamples>0 && nsamples<=PMC_CALLCHAIN_DEPTH_MAX);
708 
709 	/* Get the PMC index. */
710 	pmcin = pmcr->pr_pmcin;
711 
712 	/*
713 	 * Validate mapping for the callchain.
714 	 * Go from bottom to first invalid entry.
715 	 */
716 	km = pmcstat_kernproc;
717 	for (n = 0; n < (int)nsamples; n++) {
718 		ppm[n] = pmcstat_process_find_map(usermode ?
719 		    pp : km, cc[n]);
720 		if (ppm[n] == NULL) {
721 			/* Detect full frame capture (kernel + user). */
722 			if (!usermode) {
723 				ppm[n] = pmcstat_process_find_map(pp, cc[n]);
724 				if (ppm[n] != NULL)
725 					km = pp;
726 			}
727 		}
728 		if (ppm[n] == NULL)
729 			break;
730 	}
731 	if (n-- == 0) {
732 		pmcstat_stats.ps_callchain_dubious_frames++;
733 		pmcr->pr_dubious_frames++;
734 		return;
735 	}
736 
737 	/* Increase the call generation counter. */
738 	pmcpl_ct_samples_grow(&pmcpl_ct_callid);
739 	pmcpl_ct_callid.sb[pmcin]++;
740 
741 	/*
742 	 * Iterate remaining addresses.
743 	 */
744 	for (parent = pmcpl_ct_root, child = NULL; n >= 0; n--) {
745 		child = pmcpl_ct_node_hash_lookup_pc(parent, ppm[n], cc[n],
746 		    pmcin);
747 		if (child == NULL) {
748 			pmcstat_stats.ps_callchain_dubious_frames++;
749 			continue;
750 		}
751 		parent = child;
752 	}
753 
754 	/*
755 	 * Increment the sample count for this PMC.
756 	 */
757 	if (child != NULL) {
758 		pmcpl_ct_samples_grow(&child->pct_samples);
759 		child->pct_samples.sb[pmcin]++;
760 
761 		/* Update per instruction sample if required. */
762 		if (args.pa_ctdumpinstr)
763 			pmcpl_ct_instr_add(child, pmcin, cc[0] -
764 			    (ppm[0]->ppm_lowpc + ppm[0]->ppm_image->pi_vaddr -
765 			     ppm[0]->ppm_image->pi_start));
766 	}
767 }
768 
769 /*
770  * Print node self cost.
771  */
772 
773 static void
774 pmcpl_ct_node_printself(struct pmcpl_ct_node *ct)
775 {
776 	int i, j, line;
777 	uintptr_t addr;
778 	struct pmcstat_symbol *sym;
779 	char sourcefile[PATH_MAX];
780 	char funcname[PATH_MAX];
781 
782 	/*
783 	 * Object binary.
784 	 */
785 #ifdef PMCPL_CT_OPTIMIZEFN
786 	if (pmcpl_ct_prevfn != ct->pct_image->pi_fullpath) {
787 #endif
788 		pmcpl_ct_prevfn = ct->pct_image->pi_fullpath;
789 		fprintf(args.pa_graphfile, "ob=%s\n",
790 		    pmcstat_string_unintern(pmcpl_ct_prevfn));
791 #ifdef PMCPL_CT_OPTIMIZEFN
792 	}
793 #endif
794 
795 	/*
796 	 * Function name.
797 	 */
798 	if (pmcstat_image_addr2line(ct->pct_image, ct->pct_func,
799 	    sourcefile, sizeof(sourcefile), &line,
800 	    funcname, sizeof(funcname))) {
801 		fprintf(args.pa_graphfile, "fn=%s\n",
802 		    funcname);
803 	} else {
804 		sym = pmcstat_symbol_search(ct->pct_image, ct->pct_func);
805 		if (sym != NULL)
806 			fprintf(args.pa_graphfile, "fn=%s\n",
807 			    pmcstat_string_unintern(sym->ps_name));
808 		else
809 			fprintf(args.pa_graphfile, "fn=%p\n",
810 			    (void *)(ct->pct_image->pi_vaddr + ct->pct_func));
811 	}
812 
813 	/*
814 	 * Self cost.
815 	 */
816 	if (ct->pct_ninstr > 0) {
817 		for (i = 0; i < ct->pct_ninstr; i++) {
818 			addr = ct->pct_image->pi_vaddr +
819 			    ct->pct_instr[i].pctf_func;
820 			line = 0;
821 			if (pmcstat_image_addr2line(ct->pct_image, addr,
822 			    sourcefile, sizeof(sourcefile), &line,
823 			    funcname, sizeof(funcname)))
824 				fprintf(args.pa_graphfile, "fl=%s\n", sourcefile);
825 			fprintf(args.pa_graphfile, "%p %u", (void *)addr, line);
826 			for (j = 0; j<pmcstat_npmcs; j++)
827 				fprintf(args.pa_graphfile, " %u",
828 				    PMCPL_CT_SAMPLE(j,
829 				    &ct->pct_instr[i].pctf_samples));
830 			fprintf(args.pa_graphfile, "\n");
831 		}
832 	} else {
833 		addr = ct->pct_image->pi_vaddr + ct->pct_func;
834 		line = 0;
835 		if (pmcstat_image_addr2line(ct->pct_image, addr,
836 		    sourcefile, sizeof(sourcefile), &line,
837 		    funcname, sizeof(funcname)))
838 			fprintf(args.pa_graphfile, "fl=%s\n", sourcefile);
839 		fprintf(args.pa_graphfile, "* *");
840 		for (i = 0; i<pmcstat_npmcs ; i++)
841 			fprintf(args.pa_graphfile, " %u",
842 			    PMCPL_CT_SAMPLE(i, &ct->pct_samples));
843 		fprintf(args.pa_graphfile, "\n");
844 	}
845 }
846 
847 /*
848  * Print node child cost.
849  */
850 
851 static void
852 pmcpl_ct_node_printchild(struct pmcpl_ct_node *ct)
853 {
854 	int i, j, line;
855 	uintptr_t addr;
856 	struct pmcstat_symbol *sym;
857 	struct pmcpl_ct_node *child;
858 	char sourcefile[PATH_MAX];
859 	char funcname[PATH_MAX];
860 
861 	/*
862 	 * Child cost.
863 	 * TODO: attach child cost to the real position in the funtion.
864 	 * TODO: cfn=<fn> / call <ncall> addr(<fn>) / addr(call <fn>) <arccost>
865 	 */
866 	for (i=0 ; i<ct->pct_narc; i++) {
867 		child = ct->pct_arc[i].pcta_child;
868 
869 		/* Object binary. */
870 #ifdef PMCPL_CT_OPTIMIZEFN
871 		if (pmcpl_ct_prevfn != child->pct_image->pi_fullpath) {
872 #endif
873 			pmcpl_ct_prevfn = child->pct_image->pi_fullpath;
874 			fprintf(args.pa_graphfile, "cob=%s\n",
875 			    pmcstat_string_unintern(pmcpl_ct_prevfn));
876 #if PMCPL_CT_OPTIMIZEFN
877 		}
878 #endif
879 		/* Child function name. */
880 		addr = child->pct_image->pi_vaddr + child->pct_func;
881 		/* Child function source file. */
882 		if (pmcstat_image_addr2line(child->pct_image, addr,
883 		    sourcefile, sizeof(sourcefile), &line,
884 		    funcname, sizeof(funcname))) {
885 			fprintf(args.pa_graphfile, "cfn=%s\n", funcname);
886 			fprintf(args.pa_graphfile, "cfl=%s\n", sourcefile);
887 		} else {
888 			sym = pmcstat_symbol_search(child->pct_image,
889 			    child->pct_func);
890 			if (sym != NULL)
891 				fprintf(args.pa_graphfile, "cfn=%s\n",
892 				    pmcstat_string_unintern(sym->ps_name));
893 			else
894 				fprintf(args.pa_graphfile, "cfn=%p\n", (void *)addr);
895 		}
896 
897 		/* Child function address, line and call count. */
898 		fprintf(args.pa_graphfile, "calls=%u %p %u\n",
899 		    ct->pct_arc[i].pcta_call, (void *)addr, line);
900 
901 		if (ct->pct_image != NULL) {
902 			/* Call address, line, sample. */
903 			addr = ct->pct_image->pi_vaddr + ct->pct_func;
904 			line = 0;
905 			if (pmcstat_image_addr2line(ct->pct_image, addr, sourcefile,
906 			    sizeof(sourcefile), &line,
907 			    funcname, sizeof(funcname)))
908 				fprintf(args.pa_graphfile, "%p %u", (void *)addr, line);
909 			else
910 				fprintf(args.pa_graphfile, "* *");
911 		}
912 		else
913 			fprintf(args.pa_graphfile, "* *");
914 		for (j = 0; j<pmcstat_npmcs; j++)
915 			fprintf(args.pa_graphfile, " %u",
916 			    PMCPL_CT_SAMPLE(j, &ct->pct_arc[i].pcta_samples));
917 		fprintf(args.pa_graphfile, "\n");
918 	}
919 }
920 
921 /*
922  * Clean the PMC name for Kcachegrind formula
923  */
924 
925 static void
926 pmcpl_ct_fixup_pmcname(char *s)
927 {
928 	char *p;
929 
930 	for (p = s; *p; p++)
931 		if (!isalnum(*p))
932 			*p = '_';
933 }
934 
935 /*
936  * Print a calltree (KCachegrind) for all PMCs.
937  */
938 
939 static void
940 pmcpl_ct_print(void)
941 {
942 	int n, i;
943 	struct pmcpl_ct_node_hash *pch;
944 	struct pmcpl_ct_sample rsamples;
945 	char name[40];
946 
947 	pmcpl_ct_samples_root(&rsamples);
948 	pmcpl_ct_prevfn = NULL;
949 
950 	fprintf(args.pa_graphfile,
951 		"version: 1\n"
952 		"creator: pmcstat\n"
953 		"positions: instr line\n"
954 		"events:");
955 	for (i=0; i<pmcstat_npmcs; i++) {
956 		snprintf(name, sizeof(name), "%s_%d",
957 		    pmcstat_pmcindex_to_name(i), i);
958 		pmcpl_ct_fixup_pmcname(name);
959 		fprintf(args.pa_graphfile, " %s", name);
960 	}
961 	fprintf(args.pa_graphfile, "\nsummary:");
962 	for (i=0; i<pmcstat_npmcs ; i++)
963 		fprintf(args.pa_graphfile, " %u",
964 		    PMCPL_CT_SAMPLE(i, &rsamples));
965 	fprintf(args.pa_graphfile, "\n\n");
966 
967 	/*
968 	 * Fake root node
969 	 */
970 	fprintf(args.pa_graphfile, "ob=FreeBSD\n");
971 	fprintf(args.pa_graphfile, "fn=ROOT\n");
972 	fprintf(args.pa_graphfile, "* *");
973 	for (i = 0; i<pmcstat_npmcs ; i++)
974 		fprintf(args.pa_graphfile, " 0");
975 	fprintf(args.pa_graphfile, "\n");
976 	pmcpl_ct_node_printchild(pmcpl_ct_root);
977 
978 	for (n = 0; n < PMCSTAT_NHASH; n++)
979 		LIST_FOREACH(pch, &pmcpl_ct_node_hash[n], pch_next) {
980 			pmcpl_ct_node_printself(pch->pch_ctnode);
981 			pmcpl_ct_node_printchild(pch->pch_ctnode);
982 	}
983 
984 	pmcpl_ct_samples_free(&rsamples);
985 }
986 
987 int
988 pmcpl_ct_configure(char *opt)
989 {
990 
991 	if (strncmp(opt, "skiplink=", 9) == 0) {
992 		pmcstat_skiplink = atoi(opt+9);
993 	} else
994 		return (0);
995 
996 	return (1);
997 }
998 
999 int
1000 pmcpl_ct_init(void)
1001 {
1002 	int i;
1003 
1004 	pmcpl_ct_prevfn = NULL;
1005 	pmcpl_ct_root = pmcpl_ct_node_allocate(NULL, 0);
1006 
1007 	for (i = 0; i < PMCSTAT_NHASH; i++)
1008 		LIST_INIT(&pmcpl_ct_node_hash[i]);
1009 
1010 	pmcpl_ct_samples_init(&pmcpl_ct_callid);
1011 
1012 	return (0);
1013 }
1014 
1015 void
1016 pmcpl_ct_shutdown(FILE *mf)
1017 {
1018 	int i;
1019 	struct pmcpl_ct_node_hash *pch, *pchtmp;
1020 
1021 	(void) mf;
1022 
1023 	if (args.pa_flags & FLAG_DO_CALLGRAPHS)
1024 		pmcpl_ct_print();
1025 
1026 	/*
1027 	 * Free memory.
1028 	 */
1029 
1030 	for (i = 0; i < PMCSTAT_NHASH; i++) {
1031 		LIST_FOREACH_SAFE(pch, &pmcpl_ct_node_hash[i], pch_next,
1032 		    pchtmp) {
1033 			pmcpl_ct_node_free(pch->pch_ctnode);
1034 			free(pch);
1035 		}
1036 	}
1037 
1038 	pmcpl_ct_node_free(pmcpl_ct_root);
1039 	pmcpl_ct_root = NULL;
1040 
1041 	pmcpl_ct_samples_free(&pmcpl_ct_callid);
1042 }
1043 
1044