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