xref: /freebsd/usr.sbin/pmcstat/pmcpl_callgraph.c (revision eb707d6039d14d550c656283af3d3bfbb3aafc03)
10b86b1bbSFabien Thomas /*-
20b86b1bbSFabien Thomas  * Copyright (c) 2005-2007, Joseph Koshy
30b86b1bbSFabien Thomas  * Copyright (c) 2007 The FreeBSD Foundation
40b86b1bbSFabien Thomas  * All rights reserved.
50b86b1bbSFabien Thomas  *
60b86b1bbSFabien Thomas  * Portions of this software were developed by A. Joseph Koshy under
70b86b1bbSFabien Thomas  * sponsorship from the FreeBSD Foundation and Google, Inc.
80b86b1bbSFabien Thomas  *
90b86b1bbSFabien Thomas  * Redistribution and use in source and binary forms, with or without
100b86b1bbSFabien Thomas  * modification, are permitted provided that the following conditions
110b86b1bbSFabien Thomas  * are met:
120b86b1bbSFabien Thomas  * 1. Redistributions of source code must retain the above copyright
130b86b1bbSFabien Thomas  *    notice, this list of conditions and the following disclaimer.
140b86b1bbSFabien Thomas  * 2. Redistributions in binary form must reproduce the above copyright
150b86b1bbSFabien Thomas  *    notice, this list of conditions and the following disclaimer in the
160b86b1bbSFabien Thomas  *    documentation and/or other materials provided with the distribution.
170b86b1bbSFabien Thomas  *
180b86b1bbSFabien Thomas  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
190b86b1bbSFabien Thomas  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
200b86b1bbSFabien Thomas  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
210b86b1bbSFabien Thomas  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
220b86b1bbSFabien Thomas  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
230b86b1bbSFabien Thomas  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
240b86b1bbSFabien Thomas  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
250b86b1bbSFabien Thomas  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
260b86b1bbSFabien Thomas  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
270b86b1bbSFabien Thomas  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
280b86b1bbSFabien Thomas  * SUCH DAMAGE.
290b86b1bbSFabien Thomas  */
300b86b1bbSFabien Thomas 
310b86b1bbSFabien Thomas /*
320b86b1bbSFabien Thomas  * Transform a hwpmc(4) log into human readable form, and into
330b86b1bbSFabien Thomas  * gprof(1) compatible profiles.
340b86b1bbSFabien Thomas  */
350b86b1bbSFabien Thomas 
360b86b1bbSFabien Thomas #include <sys/cdefs.h>
370b86b1bbSFabien Thomas __FBSDID("$FreeBSD$");
380b86b1bbSFabien Thomas 
390b86b1bbSFabien Thomas #include <sys/param.h>
400b86b1bbSFabien Thomas #include <sys/endian.h>
410b86b1bbSFabien Thomas #include <sys/gmon.h>
420b86b1bbSFabien Thomas #include <sys/imgact_aout.h>
430b86b1bbSFabien Thomas #include <sys/imgact_elf.h>
440b86b1bbSFabien Thomas #include <sys/mman.h>
450b86b1bbSFabien Thomas #include <sys/pmc.h>
460b86b1bbSFabien Thomas #include <sys/queue.h>
470b86b1bbSFabien Thomas #include <sys/socket.h>
480b86b1bbSFabien Thomas #include <sys/stat.h>
490b86b1bbSFabien Thomas #include <sys/wait.h>
500b86b1bbSFabien Thomas 
510b86b1bbSFabien Thomas #include <netinet/in.h>
520b86b1bbSFabien Thomas 
530b86b1bbSFabien Thomas #include <assert.h>
540b86b1bbSFabien Thomas #include <curses.h>
550b86b1bbSFabien Thomas #include <err.h>
560b86b1bbSFabien Thomas #include <errno.h>
570b86b1bbSFabien Thomas #include <fcntl.h>
580b86b1bbSFabien Thomas #include <gelf.h>
590b86b1bbSFabien Thomas #include <libgen.h>
600b86b1bbSFabien Thomas #include <limits.h>
610b86b1bbSFabien Thomas #include <netdb.h>
620b86b1bbSFabien Thomas #include <pmc.h>
630b86b1bbSFabien Thomas #include <pmclog.h>
640b86b1bbSFabien Thomas #include <sysexits.h>
650b86b1bbSFabien Thomas #include <stdint.h>
660b86b1bbSFabien Thomas #include <stdio.h>
670b86b1bbSFabien Thomas #include <stdlib.h>
680b86b1bbSFabien Thomas #include <string.h>
690b86b1bbSFabien Thomas #include <unistd.h>
700b86b1bbSFabien Thomas 
710b86b1bbSFabien Thomas #include "pmcstat.h"
720b86b1bbSFabien Thomas #include "pmcstat_log.h"
730b86b1bbSFabien Thomas #include "pmcstat_top.h"
740b86b1bbSFabien Thomas #include "pmcpl_callgraph.h"
750b86b1bbSFabien Thomas 
760b86b1bbSFabien Thomas /* Get the sample value in percent related to nsamples. */
770b86b1bbSFabien Thomas #define PMCPL_CG_COUNTP(a) \
780b86b1bbSFabien Thomas 	((a)->pcg_count * 100.0 / nsamples)
790b86b1bbSFabien Thomas 
800b86b1bbSFabien Thomas /*
810b86b1bbSFabien Thomas  * The toplevel CG nodes (i.e., with rank == 0) are placed in a hash table.
820b86b1bbSFabien Thomas  */
830b86b1bbSFabien Thomas 
840b86b1bbSFabien Thomas struct pmcstat_cgnode_hash_list pmcstat_cgnode_hash[PMCSTAT_NHASH];
850b86b1bbSFabien Thomas int pmcstat_cgnode_hash_count;
860b86b1bbSFabien Thomas 
870b86b1bbSFabien Thomas static pmcstat_interned_string pmcstat_previous_filename_printed;
880b86b1bbSFabien Thomas 
890b86b1bbSFabien Thomas static struct pmcstat_cgnode *
900b86b1bbSFabien Thomas pmcstat_cgnode_allocate(struct pmcstat_image *image, uintfptr_t pc)
910b86b1bbSFabien Thomas {
920b86b1bbSFabien Thomas 	struct pmcstat_cgnode *cg;
930b86b1bbSFabien Thomas 
940b86b1bbSFabien Thomas 	if ((cg = malloc(sizeof(*cg))) == NULL)
950b86b1bbSFabien Thomas 		err(EX_OSERR, "ERROR: Cannot allocate callgraph node");
960b86b1bbSFabien Thomas 
970b86b1bbSFabien Thomas 	cg->pcg_image = image;
980b86b1bbSFabien Thomas 	cg->pcg_func = pc;
990b86b1bbSFabien Thomas 
1000b86b1bbSFabien Thomas 	cg->pcg_count = 0;
1010b86b1bbSFabien Thomas 	cg->pcg_nchildren = 0;
1020b86b1bbSFabien Thomas 	LIST_INIT(&cg->pcg_children);
1030b86b1bbSFabien Thomas 
1040b86b1bbSFabien Thomas 	return (cg);
1050b86b1bbSFabien Thomas }
1060b86b1bbSFabien Thomas 
1070b86b1bbSFabien Thomas /*
1080b86b1bbSFabien Thomas  * Free a node and its children.
1090b86b1bbSFabien Thomas  */
1100b86b1bbSFabien Thomas static void
1110b86b1bbSFabien Thomas pmcstat_cgnode_free(struct pmcstat_cgnode *cg)
1120b86b1bbSFabien Thomas {
1130b86b1bbSFabien Thomas 	struct pmcstat_cgnode *cgc, *cgtmp;
1140b86b1bbSFabien Thomas 
1150b86b1bbSFabien Thomas 	LIST_FOREACH_SAFE(cgc, &cg->pcg_children, pcg_sibling, cgtmp)
1160b86b1bbSFabien Thomas 		pmcstat_cgnode_free(cgc);
1170b86b1bbSFabien Thomas 	free(cg);
1180b86b1bbSFabien Thomas }
1190b86b1bbSFabien Thomas 
1200b86b1bbSFabien Thomas /*
1210b86b1bbSFabien Thomas  * Look for a callgraph node associated with pmc `pmcid' in the global
1220b86b1bbSFabien Thomas  * hash table that corresponds to the given `pc' value in the process
1230b86b1bbSFabien Thomas  * `pp'.
1240b86b1bbSFabien Thomas  */
1250b86b1bbSFabien Thomas static struct pmcstat_cgnode *
1260b86b1bbSFabien Thomas pmcstat_cgnode_hash_lookup_pc(struct pmcstat_process *pp, pmc_id_t pmcid,
1270b86b1bbSFabien Thomas     uintfptr_t pc, int usermode)
1280b86b1bbSFabien Thomas {
1290b86b1bbSFabien Thomas 	struct pmcstat_pcmap *ppm;
1300b86b1bbSFabien Thomas 	struct pmcstat_symbol *sym;
1310b86b1bbSFabien Thomas 	struct pmcstat_image *image;
1320b86b1bbSFabien Thomas 	struct pmcstat_cgnode *cg;
1330b86b1bbSFabien Thomas 	struct pmcstat_cgnode_hash *h;
1340b86b1bbSFabien Thomas 	uintfptr_t loadaddress;
1350b86b1bbSFabien Thomas 	unsigned int i, hash;
1360b86b1bbSFabien Thomas 
1370b86b1bbSFabien Thomas 	ppm = pmcstat_process_find_map(usermode ? pp : pmcstat_kernproc, pc);
1380b86b1bbSFabien Thomas 	if (ppm == NULL)
1390b86b1bbSFabien Thomas 		return (NULL);
1400b86b1bbSFabien Thomas 
1410b86b1bbSFabien Thomas 	image = ppm->ppm_image;
1420b86b1bbSFabien Thomas 
1430b86b1bbSFabien Thomas 	loadaddress = ppm->ppm_lowpc + image->pi_vaddr - image->pi_start;
1440b86b1bbSFabien Thomas 	pc -= loadaddress;	/* Convert to an offset in the image. */
1450b86b1bbSFabien Thomas 
1460b86b1bbSFabien Thomas 	/*
1470b86b1bbSFabien Thomas 	 * Try determine the function at this offset.  If we can't
1480b86b1bbSFabien Thomas 	 * find a function round leave the `pc' value alone.
1490b86b1bbSFabien Thomas 	 */
1500b86b1bbSFabien Thomas 	if ((sym = pmcstat_symbol_search(image, pc)) != NULL)
1510b86b1bbSFabien Thomas 		pc = sym->ps_start;
152*eb707d60SFabien Thomas 	else
153*eb707d60SFabien Thomas 		pmcstat_stats.ps_samples_unknown_function++;
1540b86b1bbSFabien Thomas 
1550b86b1bbSFabien Thomas 	for (hash = i = 0; i < sizeof(uintfptr_t); i++)
1560b86b1bbSFabien Thomas 		hash += (pc >> i) & 0xFF;
1570b86b1bbSFabien Thomas 
1580b86b1bbSFabien Thomas 	hash &= PMCSTAT_HASH_MASK;
1590b86b1bbSFabien Thomas 
1600b86b1bbSFabien Thomas 	cg = NULL;
1610b86b1bbSFabien Thomas 	LIST_FOREACH(h, &pmcstat_cgnode_hash[hash], pch_next)
1620b86b1bbSFabien Thomas 	{
1630b86b1bbSFabien Thomas 		if (h->pch_pmcid != pmcid)
1640b86b1bbSFabien Thomas 			continue;
1650b86b1bbSFabien Thomas 
1660b86b1bbSFabien Thomas 		cg = h->pch_cgnode;
1670b86b1bbSFabien Thomas 
1680b86b1bbSFabien Thomas 		assert(cg != NULL);
1690b86b1bbSFabien Thomas 
1700b86b1bbSFabien Thomas 		if (cg->pcg_image == image && cg->pcg_func == pc)
1710b86b1bbSFabien Thomas 			return (cg);
1720b86b1bbSFabien Thomas 	}
1730b86b1bbSFabien Thomas 
1740b86b1bbSFabien Thomas 	/*
1750b86b1bbSFabien Thomas 	 * We haven't seen this (pmcid, pc) tuple yet, so allocate a
1760b86b1bbSFabien Thomas 	 * new callgraph node and a new hash table entry for it.
1770b86b1bbSFabien Thomas 	 */
1780b86b1bbSFabien Thomas 	cg = pmcstat_cgnode_allocate(image, pc);
1790b86b1bbSFabien Thomas 	if ((h = malloc(sizeof(*h))) == NULL)
1800b86b1bbSFabien Thomas 		err(EX_OSERR, "ERROR: Could not allocate callgraph node");
1810b86b1bbSFabien Thomas 
1820b86b1bbSFabien Thomas 	h->pch_pmcid = pmcid;
1830b86b1bbSFabien Thomas 	h->pch_cgnode = cg;
1840b86b1bbSFabien Thomas 	LIST_INSERT_HEAD(&pmcstat_cgnode_hash[hash], h, pch_next);
1850b86b1bbSFabien Thomas 
1860b86b1bbSFabien Thomas 	pmcstat_cgnode_hash_count++;
1870b86b1bbSFabien Thomas 
1880b86b1bbSFabien Thomas 	return (cg);
1890b86b1bbSFabien Thomas }
1900b86b1bbSFabien Thomas 
1910b86b1bbSFabien Thomas /*
1920b86b1bbSFabien Thomas  * Compare two callgraph nodes for sorting.
1930b86b1bbSFabien Thomas  */
1940b86b1bbSFabien Thomas static int
1950b86b1bbSFabien Thomas pmcstat_cgnode_compare(const void *a, const void *b)
1960b86b1bbSFabien Thomas {
1970b86b1bbSFabien Thomas 	const struct pmcstat_cgnode *const *pcg1, *const *pcg2, *cg1, *cg2;
1980b86b1bbSFabien Thomas 
1990b86b1bbSFabien Thomas 	pcg1 = (const struct pmcstat_cgnode *const *) a;
2000b86b1bbSFabien Thomas 	cg1 = *pcg1;
2010b86b1bbSFabien Thomas 	pcg2 = (const struct pmcstat_cgnode *const *) b;
2020b86b1bbSFabien Thomas 	cg2 = *pcg2;
2030b86b1bbSFabien Thomas 
2040b86b1bbSFabien Thomas 	/* Sort in reverse order */
2050b86b1bbSFabien Thomas 	if (cg1->pcg_count < cg2->pcg_count)
2060b86b1bbSFabien Thomas 		return (1);
2070b86b1bbSFabien Thomas 	if (cg1->pcg_count > cg2->pcg_count)
2080b86b1bbSFabien Thomas 		return (-1);
2090b86b1bbSFabien Thomas 	return (0);
2100b86b1bbSFabien Thomas }
2110b86b1bbSFabien Thomas 
2120b86b1bbSFabien Thomas /*
2130b86b1bbSFabien Thomas  * Find (allocating if a needed) a callgraph node in the given
2140b86b1bbSFabien Thomas  * parent with the same (image, pcoffset) pair.
2150b86b1bbSFabien Thomas  */
2160b86b1bbSFabien Thomas 
2170b86b1bbSFabien Thomas static struct pmcstat_cgnode *
2180b86b1bbSFabien Thomas pmcstat_cgnode_find(struct pmcstat_cgnode *parent, struct pmcstat_image *image,
2190b86b1bbSFabien Thomas     uintfptr_t pcoffset)
2200b86b1bbSFabien Thomas {
2210b86b1bbSFabien Thomas 	struct pmcstat_cgnode *child;
2220b86b1bbSFabien Thomas 
2230b86b1bbSFabien Thomas 	LIST_FOREACH(child, &parent->pcg_children, pcg_sibling) {
2240b86b1bbSFabien Thomas 		if (child->pcg_image == image &&
2250b86b1bbSFabien Thomas 		    child->pcg_func == pcoffset)
2260b86b1bbSFabien Thomas 			return (child);
2270b86b1bbSFabien Thomas 	}
2280b86b1bbSFabien Thomas 
2290b86b1bbSFabien Thomas 	/*
2300b86b1bbSFabien Thomas 	 * Allocate a new structure.
2310b86b1bbSFabien Thomas 	 */
2320b86b1bbSFabien Thomas 
2330b86b1bbSFabien Thomas 	child = pmcstat_cgnode_allocate(image, pcoffset);
2340b86b1bbSFabien Thomas 
2350b86b1bbSFabien Thomas 	/*
2360b86b1bbSFabien Thomas 	 * Link it into the parent.
2370b86b1bbSFabien Thomas 	 */
2380b86b1bbSFabien Thomas 	LIST_INSERT_HEAD(&parent->pcg_children, child, pcg_sibling);
2390b86b1bbSFabien Thomas 	parent->pcg_nchildren++;
2400b86b1bbSFabien Thomas 
2410b86b1bbSFabien Thomas 	return (child);
2420b86b1bbSFabien Thomas }
2430b86b1bbSFabien Thomas 
2440b86b1bbSFabien Thomas /*
2450b86b1bbSFabien Thomas  * Print one callgraph node.  The output format is:
2460b86b1bbSFabien Thomas  *
2470b86b1bbSFabien Thomas  * indentation %(parent's samples) #nsamples function@object
2480b86b1bbSFabien Thomas  */
2490b86b1bbSFabien Thomas static void
2500b86b1bbSFabien Thomas pmcstat_cgnode_print(struct pmcstat_cgnode *cg, int depth, uint32_t total)
2510b86b1bbSFabien Thomas {
2520b86b1bbSFabien Thomas 	uint32_t n;
2530b86b1bbSFabien Thomas 	const char *space;
2540b86b1bbSFabien Thomas 	struct pmcstat_symbol *sym;
2550b86b1bbSFabien Thomas 	struct pmcstat_cgnode **sortbuffer, **cgn, *pcg;
2560b86b1bbSFabien Thomas 
2570b86b1bbSFabien Thomas 	space = " ";
2580b86b1bbSFabien Thomas 
2590b86b1bbSFabien Thomas 	if (depth > 0)
2600b86b1bbSFabien Thomas 		(void) fprintf(args.pa_graphfile, "%*s", depth, space);
2610b86b1bbSFabien Thomas 
2620b86b1bbSFabien Thomas 	if (cg->pcg_count == total)
2630b86b1bbSFabien Thomas 		(void) fprintf(args.pa_graphfile, "100.0%% ");
2640b86b1bbSFabien Thomas 	else
2650b86b1bbSFabien Thomas 		(void) fprintf(args.pa_graphfile, "%05.2f%% ",
2660b86b1bbSFabien Thomas 		    100.0 * cg->pcg_count / total);
2670b86b1bbSFabien Thomas 
2680b86b1bbSFabien Thomas 	n = fprintf(args.pa_graphfile, " [%u] ", cg->pcg_count);
2690b86b1bbSFabien Thomas 
2700b86b1bbSFabien Thomas 	/* #samples is a 12 character wide field. */
2710b86b1bbSFabien Thomas 	if (n < 12)
2720b86b1bbSFabien Thomas 		(void) fprintf(args.pa_graphfile, "%*s", 12 - n, space);
2730b86b1bbSFabien Thomas 
2740b86b1bbSFabien Thomas 	if (depth > 0)
2750b86b1bbSFabien Thomas 		(void) fprintf(args.pa_graphfile, "%*s", depth, space);
2760b86b1bbSFabien Thomas 
2770b86b1bbSFabien Thomas 	sym = pmcstat_symbol_search(cg->pcg_image, cg->pcg_func);
2780b86b1bbSFabien Thomas 	if (sym)
2790b86b1bbSFabien Thomas 		(void) fprintf(args.pa_graphfile, "%s",
2800b86b1bbSFabien Thomas 		    pmcstat_string_unintern(sym->ps_name));
2810b86b1bbSFabien Thomas 	else
2820b86b1bbSFabien Thomas 		(void) fprintf(args.pa_graphfile, "%p",
2830b86b1bbSFabien Thomas 		    (void *) (cg->pcg_image->pi_vaddr + cg->pcg_func));
2840b86b1bbSFabien Thomas 
2850b86b1bbSFabien Thomas 	if (pmcstat_previous_filename_printed !=
2860b86b1bbSFabien Thomas 	    cg->pcg_image->pi_fullpath) {
2870b86b1bbSFabien Thomas 		pmcstat_previous_filename_printed = cg->pcg_image->pi_fullpath;
2880b86b1bbSFabien Thomas 		(void) fprintf(args.pa_graphfile, " @ %s\n",
2890b86b1bbSFabien Thomas 		    pmcstat_string_unintern(
2900b86b1bbSFabien Thomas 		    pmcstat_previous_filename_printed));
2910b86b1bbSFabien Thomas 	} else
2920b86b1bbSFabien Thomas 		(void) fprintf(args.pa_graphfile, "\n");
2930b86b1bbSFabien Thomas 
2940b86b1bbSFabien Thomas 	if (cg->pcg_nchildren == 0)
2950b86b1bbSFabien Thomas 		return;
2960b86b1bbSFabien Thomas 
2970b86b1bbSFabien Thomas 	if ((sortbuffer = (struct pmcstat_cgnode **)
2980b86b1bbSFabien Thomas 		malloc(sizeof(struct pmcstat_cgnode *) *
2990b86b1bbSFabien Thomas 		    cg->pcg_nchildren)) == NULL)
3000b86b1bbSFabien Thomas 		err(EX_OSERR, "ERROR: Cannot print callgraph");
3010b86b1bbSFabien Thomas 	cgn = sortbuffer;
3020b86b1bbSFabien Thomas 
3030b86b1bbSFabien Thomas 	LIST_FOREACH(pcg, &cg->pcg_children, pcg_sibling)
3040b86b1bbSFabien Thomas 	    *cgn++ = pcg;
3050b86b1bbSFabien Thomas 
3060b86b1bbSFabien Thomas 	assert(cgn - sortbuffer == (int) cg->pcg_nchildren);
3070b86b1bbSFabien Thomas 
3080b86b1bbSFabien Thomas 	qsort(sortbuffer, cg->pcg_nchildren, sizeof(struct pmcstat_cgnode *),
3090b86b1bbSFabien Thomas 	    pmcstat_cgnode_compare);
3100b86b1bbSFabien Thomas 
3110b86b1bbSFabien Thomas 	for (cgn = sortbuffer, n = 0; n < cg->pcg_nchildren; n++, cgn++)
3120b86b1bbSFabien Thomas 		pmcstat_cgnode_print(*cgn, depth+1, cg->pcg_count);
3130b86b1bbSFabien Thomas 
3140b86b1bbSFabien Thomas 	free(sortbuffer);
3150b86b1bbSFabien Thomas }
3160b86b1bbSFabien Thomas 
3170b86b1bbSFabien Thomas /*
3180b86b1bbSFabien Thomas  * Record a callchain.
3190b86b1bbSFabien Thomas  */
3200b86b1bbSFabien Thomas 
3210b86b1bbSFabien Thomas void
3220b86b1bbSFabien Thomas pmcpl_cg_process(struct pmcstat_process *pp, struct pmcstat_pmcrecord *pmcr,
3230b86b1bbSFabien Thomas     uint32_t nsamples, uintfptr_t *cc, int usermode, uint32_t cpu)
3240b86b1bbSFabien Thomas {
3250b86b1bbSFabien Thomas 	uintfptr_t pc, loadaddress;
3260b86b1bbSFabien Thomas 	uint32_t n;
3270b86b1bbSFabien Thomas 	struct pmcstat_image *image;
3280b86b1bbSFabien Thomas 	struct pmcstat_pcmap *ppm;
3290b86b1bbSFabien Thomas 	struct pmcstat_symbol *sym;
3300b86b1bbSFabien Thomas 	struct pmcstat_cgnode *parent, *child;
3310b86b1bbSFabien Thomas 	struct pmcstat_process *km;
3320b86b1bbSFabien Thomas 	pmc_id_t pmcid;
3330b86b1bbSFabien Thomas 
3340b86b1bbSFabien Thomas 	(void) cpu;
3350b86b1bbSFabien Thomas 
3360b86b1bbSFabien Thomas 	/*
3370b86b1bbSFabien Thomas 	 * Find the callgraph node recorded in the global hash table
3380b86b1bbSFabien Thomas 	 * for this (pmcid, pc).
3390b86b1bbSFabien Thomas 	 */
3400b86b1bbSFabien Thomas 
3410b86b1bbSFabien Thomas 	pc = cc[0];
3420b86b1bbSFabien Thomas 	pmcid = pmcr->pr_pmcid;
3430b86b1bbSFabien Thomas 	parent = pmcstat_cgnode_hash_lookup_pc(pp, pmcid, pc, usermode);
3440b86b1bbSFabien Thomas 	if (parent == NULL) {
3450b86b1bbSFabien Thomas 		pmcstat_stats.ps_callchain_dubious_frames++;
346c86819ecSFabien Thomas 		pmcr->pr_dubious_frames++;
3470b86b1bbSFabien Thomas 		return;
3480b86b1bbSFabien Thomas 	}
3490b86b1bbSFabien Thomas 
3500b86b1bbSFabien Thomas 	parent->pcg_count++;
3510b86b1bbSFabien Thomas 
3520b86b1bbSFabien Thomas 	/*
3530b86b1bbSFabien Thomas 	 * For each return address in the call chain record, subject
3540b86b1bbSFabien Thomas 	 * to the maximum depth desired.
3550b86b1bbSFabien Thomas 	 * - Find the image associated with the sample.  Stop if there
3560b86b1bbSFabien Thomas 	 *   there is no valid image at that address.
3570b86b1bbSFabien Thomas 	 * - Find the function that overlaps the return address.
3580b86b1bbSFabien Thomas 	 * - If found: use the start address of the function.
3590b86b1bbSFabien Thomas 	 *   If not found (say an object's symbol table is not present or
3600b86b1bbSFabien Thomas 	 *   is incomplete), round down to th gprof bucket granularity.
3610b86b1bbSFabien Thomas 	 * - Convert return virtual address to an offset in the image.
3620b86b1bbSFabien Thomas 	 * - Look for a child with the same {offset,image} tuple,
3630b86b1bbSFabien Thomas 	 *   inserting one if needed.
3640b86b1bbSFabien Thomas 	 * - Increment the count of occurrences of the child.
3650b86b1bbSFabien Thomas 	 */
3660b86b1bbSFabien Thomas 	km = pmcstat_kernproc;
3670b86b1bbSFabien Thomas 
3680b86b1bbSFabien Thomas 	for (n = 1; n < (uint32_t) args.pa_graphdepth && n < nsamples; n++,
3690b86b1bbSFabien Thomas 	    parent = child) {
3700b86b1bbSFabien Thomas 		pc = cc[n];
3710b86b1bbSFabien Thomas 
3720b86b1bbSFabien Thomas 		ppm = pmcstat_process_find_map(usermode ? pp : km, pc);
3730b86b1bbSFabien Thomas 		if (ppm == NULL) {
3740b86b1bbSFabien Thomas 			/* Detect full frame capture (kernel + user). */
3750b86b1bbSFabien Thomas 			if (!usermode) {
3760b86b1bbSFabien Thomas 				ppm = pmcstat_process_find_map(pp, pc);
3770b86b1bbSFabien Thomas 				if (ppm != NULL)
3780b86b1bbSFabien Thomas 					km = pp;
3790b86b1bbSFabien Thomas 			}
3800b86b1bbSFabien Thomas 		}
3810b86b1bbSFabien Thomas 		if (ppm == NULL)
3820b86b1bbSFabien Thomas 			return;
3830b86b1bbSFabien Thomas 
3840b86b1bbSFabien Thomas 		image = ppm->ppm_image;
3850b86b1bbSFabien Thomas 		loadaddress = ppm->ppm_lowpc + image->pi_vaddr -
3860b86b1bbSFabien Thomas 		    image->pi_start;
3870b86b1bbSFabien Thomas 		pc -= loadaddress;
3880b86b1bbSFabien Thomas 
3890b86b1bbSFabien Thomas 		if ((sym = pmcstat_symbol_search(image, pc)) != NULL)
3900b86b1bbSFabien Thomas 			pc = sym->ps_start;
3910b86b1bbSFabien Thomas 
3920b86b1bbSFabien Thomas 		child = pmcstat_cgnode_find(parent, image, pc);
3930b86b1bbSFabien Thomas 		child->pcg_count++;
3940b86b1bbSFabien Thomas 	}
3950b86b1bbSFabien Thomas }
3960b86b1bbSFabien Thomas 
3970b86b1bbSFabien Thomas /*
3980b86b1bbSFabien Thomas  * Printing a callgraph for a PMC.
3990b86b1bbSFabien Thomas  */
4000b86b1bbSFabien Thomas static void
4010b86b1bbSFabien Thomas pmcstat_callgraph_print_for_pmcid(struct pmcstat_pmcrecord *pmcr)
4020b86b1bbSFabien Thomas {
4030b86b1bbSFabien Thomas 	int n, nentries;
4040b86b1bbSFabien Thomas 	uint32_t nsamples;
4050b86b1bbSFabien Thomas 	pmc_id_t pmcid;
4060b86b1bbSFabien Thomas 	struct pmcstat_cgnode **sortbuffer, **cgn;
4070b86b1bbSFabien Thomas 	struct pmcstat_cgnode_hash *pch;
4080b86b1bbSFabien Thomas 
4090b86b1bbSFabien Thomas 	/*
4100b86b1bbSFabien Thomas 	 * We pull out all callgraph nodes in the top-level hash table
4110b86b1bbSFabien Thomas 	 * with a matching PMC id.  We then sort these based on the
4120b86b1bbSFabien Thomas 	 * frequency of occurrence.  Each callgraph node is then
4130b86b1bbSFabien Thomas 	 * printed.
4140b86b1bbSFabien Thomas 	 */
4150b86b1bbSFabien Thomas 
4160b86b1bbSFabien Thomas 	nsamples = 0;
4170b86b1bbSFabien Thomas 	pmcid = pmcr->pr_pmcid;
4180b86b1bbSFabien Thomas 	if ((sortbuffer = (struct pmcstat_cgnode **)
4190b86b1bbSFabien Thomas 	    malloc(sizeof(struct pmcstat_cgnode *) *
4200b86b1bbSFabien Thomas 	    pmcstat_cgnode_hash_count)) == NULL)
4210b86b1bbSFabien Thomas 		err(EX_OSERR, "ERROR: Cannot sort callgraph");
4220b86b1bbSFabien Thomas 	cgn = sortbuffer;
4230b86b1bbSFabien Thomas 
4240b86b1bbSFabien Thomas 	for (n = 0; n < PMCSTAT_NHASH; n++)
4250b86b1bbSFabien Thomas 		LIST_FOREACH(pch, &pmcstat_cgnode_hash[n], pch_next)
4260b86b1bbSFabien Thomas 		    if (pch->pch_pmcid == pmcid) {
4270b86b1bbSFabien Thomas 			    nsamples += pch->pch_cgnode->pcg_count;
4280b86b1bbSFabien Thomas 			    *cgn++ = pch->pch_cgnode;
4290b86b1bbSFabien Thomas 		    }
4300b86b1bbSFabien Thomas 
4310b86b1bbSFabien Thomas 	nentries = cgn - sortbuffer;
4320b86b1bbSFabien Thomas 	assert(nentries <= pmcstat_cgnode_hash_count);
4330b86b1bbSFabien Thomas 
4340b86b1bbSFabien Thomas 	if (nentries == 0) {
4350b86b1bbSFabien Thomas 		free(sortbuffer);
4360b86b1bbSFabien Thomas 		return;
4370b86b1bbSFabien Thomas 	}
4380b86b1bbSFabien Thomas 
4390b86b1bbSFabien Thomas 	qsort(sortbuffer, nentries, sizeof(struct pmcstat_cgnode *),
4400b86b1bbSFabien Thomas 	    pmcstat_cgnode_compare);
4410b86b1bbSFabien Thomas 
4420b86b1bbSFabien Thomas 	(void) fprintf(args.pa_graphfile,
4430b86b1bbSFabien Thomas 	    "@ %s [%u samples]\n\n",
4440b86b1bbSFabien Thomas 	    pmcstat_string_unintern(pmcr->pr_pmcname),
4450b86b1bbSFabien Thomas 	    nsamples);
4460b86b1bbSFabien Thomas 
4470b86b1bbSFabien Thomas 	for (cgn = sortbuffer, n = 0; n < nentries; n++, cgn++) {
4480b86b1bbSFabien Thomas 		pmcstat_previous_filename_printed = NULL;
4490b86b1bbSFabien Thomas 		pmcstat_cgnode_print(*cgn, 0, nsamples);
4500b86b1bbSFabien Thomas 		(void) fprintf(args.pa_graphfile, "\n");
4510b86b1bbSFabien Thomas 	}
4520b86b1bbSFabien Thomas 
4530b86b1bbSFabien Thomas 	free(sortbuffer);
4540b86b1bbSFabien Thomas }
4550b86b1bbSFabien Thomas 
4560b86b1bbSFabien Thomas /*
4570b86b1bbSFabien Thomas  * Print out callgraphs.
4580b86b1bbSFabien Thomas  */
4590b86b1bbSFabien Thomas 
4600b86b1bbSFabien Thomas static void
4610b86b1bbSFabien Thomas pmcstat_callgraph_print(void)
4620b86b1bbSFabien Thomas {
4630b86b1bbSFabien Thomas 	struct pmcstat_pmcrecord *pmcr;
4640b86b1bbSFabien Thomas 
4650b86b1bbSFabien Thomas 	LIST_FOREACH(pmcr, &pmcstat_pmcs, pr_next)
4660b86b1bbSFabien Thomas 	    pmcstat_callgraph_print_for_pmcid(pmcr);
4670b86b1bbSFabien Thomas }
4680b86b1bbSFabien Thomas 
4690b86b1bbSFabien Thomas static void
4700b86b1bbSFabien Thomas pmcstat_cgnode_topprint(struct pmcstat_cgnode *cg,
4710b86b1bbSFabien Thomas     int depth, uint32_t nsamples)
4720b86b1bbSFabien Thomas {
4730b86b1bbSFabien Thomas 	int v_attrs, vs_len, ns_len, width, len, n, nchildren;
4740b86b1bbSFabien Thomas 	float v;
4750b86b1bbSFabien Thomas 	char ns[30], vs[10];
4760b86b1bbSFabien Thomas 	struct pmcstat_symbol *sym;
4770b86b1bbSFabien Thomas 	struct pmcstat_cgnode **sortbuffer, **cgn, *pcg;
4780b86b1bbSFabien Thomas 
4790b86b1bbSFabien Thomas 	(void) depth;
4800b86b1bbSFabien Thomas 
4810b86b1bbSFabien Thomas 	/* Format value. */
4820b86b1bbSFabien Thomas 	v = PMCPL_CG_COUNTP(cg);
4830b86b1bbSFabien Thomas 	snprintf(vs, sizeof(vs), "%.1f", v);
4840b86b1bbSFabien Thomas 	v_attrs = PMCSTAT_ATTRPERCENT(v);
4850b86b1bbSFabien Thomas 
4860b86b1bbSFabien Thomas 	/* Format name. */
4870b86b1bbSFabien Thomas 	sym = pmcstat_symbol_search(cg->pcg_image, cg->pcg_func);
4880b86b1bbSFabien Thomas 	if (sym != NULL) {
4890b86b1bbSFabien Thomas 		snprintf(ns, sizeof(ns), "%s",
4900b86b1bbSFabien Thomas 		    pmcstat_string_unintern(sym->ps_name));
4910b86b1bbSFabien Thomas 	} else
4920b86b1bbSFabien Thomas 		snprintf(ns, sizeof(ns), "%p",
4930b86b1bbSFabien Thomas 		    (void *)cg->pcg_func);
4940b86b1bbSFabien Thomas 
4950b86b1bbSFabien Thomas 	PMCSTAT_ATTRON(v_attrs);
4960b86b1bbSFabien Thomas 	PMCSTAT_PRINTW("%5.5s", vs);
4970b86b1bbSFabien Thomas 	PMCSTAT_ATTROFF(v_attrs);
4980b86b1bbSFabien Thomas 	PMCSTAT_PRINTW(" %-10.10s %-20.20s",
4990b86b1bbSFabien Thomas 	    pmcstat_string_unintern(cg->pcg_image->pi_name),
5000b86b1bbSFabien Thomas 	    ns);
5010b86b1bbSFabien Thomas 
5020b86b1bbSFabien Thomas 	nchildren = cg->pcg_nchildren;
5030b86b1bbSFabien Thomas 	if (nchildren == 0) {
5040b86b1bbSFabien Thomas 		PMCSTAT_PRINTW("\n");
5050b86b1bbSFabien Thomas 		return;
5060b86b1bbSFabien Thomas 	}
5070b86b1bbSFabien Thomas 
5080b86b1bbSFabien Thomas 	width = pmcstat_displaywidth - 40;
5090b86b1bbSFabien Thomas 
5100b86b1bbSFabien Thomas 	if ((sortbuffer = (struct pmcstat_cgnode **)
5110b86b1bbSFabien Thomas 		malloc(sizeof(struct pmcstat_cgnode *) *
5120b86b1bbSFabien Thomas 		    nchildren)) == NULL)
5130b86b1bbSFabien Thomas 		err(EX_OSERR, "ERROR: Cannot print callgraph");
5140b86b1bbSFabien Thomas 	cgn = sortbuffer;
5150b86b1bbSFabien Thomas 
5160b86b1bbSFabien Thomas 	LIST_FOREACH(pcg, &cg->pcg_children, pcg_sibling)
5170b86b1bbSFabien Thomas 	    *cgn++ = pcg;
5180b86b1bbSFabien Thomas 
5190b86b1bbSFabien Thomas 	assert(cgn - sortbuffer == (int)nchildren);
5200b86b1bbSFabien Thomas 
5210b86b1bbSFabien Thomas 	qsort(sortbuffer, nchildren, sizeof(struct pmcstat_cgnode *),
5220b86b1bbSFabien Thomas 	    pmcstat_cgnode_compare);
5230b86b1bbSFabien Thomas 
5240b86b1bbSFabien Thomas 	/* Count how many callers. */
5250b86b1bbSFabien Thomas 	for (cgn = sortbuffer, n = 0; n < nchildren; n++, cgn++) {
5260b86b1bbSFabien Thomas 		pcg = *cgn;
5270b86b1bbSFabien Thomas 
5280b86b1bbSFabien Thomas 		v = PMCPL_CG_COUNTP(pcg);
5290b86b1bbSFabien Thomas 		if (v < pmcstat_threshold)
5300b86b1bbSFabien Thomas 			break;
5310b86b1bbSFabien Thomas 	}
5320b86b1bbSFabien Thomas 	nchildren = n;
5330b86b1bbSFabien Thomas 
5340b86b1bbSFabien Thomas 	for (cgn = sortbuffer, n = 0; n < nchildren; n++, cgn++) {
5350b86b1bbSFabien Thomas 		pcg = *cgn;
5360b86b1bbSFabien Thomas 
5370b86b1bbSFabien Thomas 		/* Format value. */
5380b86b1bbSFabien Thomas 		if (nchildren > 1) {
5390b86b1bbSFabien Thomas 			v = PMCPL_CG_COUNTP(pcg);
5400b86b1bbSFabien Thomas 			vs_len = snprintf(vs, sizeof(vs), ":%.1f", v);
5410b86b1bbSFabien Thomas 			v_attrs = PMCSTAT_ATTRPERCENT(v);
5420b86b1bbSFabien Thomas 		} else
5430b86b1bbSFabien Thomas 			vs_len = 0;
5440b86b1bbSFabien Thomas 
5450b86b1bbSFabien Thomas 		/* Format name. */
5460b86b1bbSFabien Thomas 		sym = pmcstat_symbol_search(pcg->pcg_image, pcg->pcg_func);
5470b86b1bbSFabien Thomas 		if (sym != NULL) {
5480b86b1bbSFabien Thomas 			ns_len = snprintf(ns, sizeof(ns), "%s",
5490b86b1bbSFabien Thomas 			    pmcstat_string_unintern(sym->ps_name));
5500b86b1bbSFabien Thomas 		} else
5510b86b1bbSFabien Thomas 			ns_len = snprintf(ns, sizeof(ns), "%p",
5520b86b1bbSFabien Thomas 			    (void *)pcg->pcg_func);
5530b86b1bbSFabien Thomas 
5540b86b1bbSFabien Thomas 		len = ns_len + vs_len + 1;
5550b86b1bbSFabien Thomas 		if (width - len < 0) {
5560b86b1bbSFabien Thomas 			PMCSTAT_PRINTW(" ...");
5570b86b1bbSFabien Thomas 			break;
5580b86b1bbSFabien Thomas 		}
5590b86b1bbSFabien Thomas 		width -= len;
5600b86b1bbSFabien Thomas 
5610b86b1bbSFabien Thomas 		PMCSTAT_PRINTW(" %s", ns);
5620b86b1bbSFabien Thomas 		if (nchildren > 1) {
5630b86b1bbSFabien Thomas 			PMCSTAT_ATTRON(v_attrs);
5640b86b1bbSFabien Thomas 			PMCSTAT_PRINTW("%s", vs);
5650b86b1bbSFabien Thomas 			PMCSTAT_ATTROFF(v_attrs);
5660b86b1bbSFabien Thomas 		}
5670b86b1bbSFabien Thomas 	}
5680b86b1bbSFabien Thomas 	PMCSTAT_PRINTW("\n");
5690b86b1bbSFabien Thomas 	free(sortbuffer);
5700b86b1bbSFabien Thomas }
5710b86b1bbSFabien Thomas 
5720b86b1bbSFabien Thomas /*
5730b86b1bbSFabien Thomas  * Top mode display.
5740b86b1bbSFabien Thomas  */
5750b86b1bbSFabien Thomas 
5760b86b1bbSFabien Thomas void
5770b86b1bbSFabien Thomas pmcpl_cg_topdisplay(void)
5780b86b1bbSFabien Thomas {
5790b86b1bbSFabien Thomas 	int n, nentries;
5800b86b1bbSFabien Thomas 	uint32_t nsamples;
5810b86b1bbSFabien Thomas 	struct pmcstat_cgnode **sortbuffer, **cgn;
5820b86b1bbSFabien Thomas 	struct pmcstat_cgnode_hash *pch;
5830b86b1bbSFabien Thomas 	struct pmcstat_pmcrecord *pmcr;
5840b86b1bbSFabien Thomas 
5850b86b1bbSFabien Thomas 	pmcr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
586410c7662SFabien Thomas 	if (!pmcr)
587410c7662SFabien Thomas 		err(EX_SOFTWARE, "ERROR: invalid pmcindex");
5880b86b1bbSFabien Thomas 
5890b86b1bbSFabien Thomas 	/*
5900b86b1bbSFabien Thomas 	 * We pull out all callgraph nodes in the top-level hash table
5910b86b1bbSFabien Thomas 	 * with a matching PMC index.  We then sort these based on the
5920b86b1bbSFabien Thomas 	 * frequency of occurrence.  Each callgraph node is then
5930b86b1bbSFabien Thomas 	 * printed.
5940b86b1bbSFabien Thomas 	 */
5950b86b1bbSFabien Thomas 
5960b86b1bbSFabien Thomas 	nsamples = 0;
5970b86b1bbSFabien Thomas 
5980b86b1bbSFabien Thomas 	if ((sortbuffer = (struct pmcstat_cgnode **)
5990b86b1bbSFabien Thomas 	    malloc(sizeof(struct pmcstat_cgnode *) *
6000b86b1bbSFabien Thomas 	    pmcstat_cgnode_hash_count)) == NULL)
6010b86b1bbSFabien Thomas 		err(EX_OSERR, "ERROR: Cannot sort callgraph");
6020b86b1bbSFabien Thomas 	cgn = sortbuffer;
6030b86b1bbSFabien Thomas 
6040b86b1bbSFabien Thomas 	for (n = 0; n < PMCSTAT_NHASH; n++)
6050b86b1bbSFabien Thomas 		LIST_FOREACH(pch, &pmcstat_cgnode_hash[n], pch_next)
6060b86b1bbSFabien Thomas 		    if (pmcr == NULL || pch->pch_pmcid == pmcr->pr_pmcid) {
6070b86b1bbSFabien Thomas 			    nsamples += pch->pch_cgnode->pcg_count;
6080b86b1bbSFabien Thomas 			    *cgn++ = pch->pch_cgnode;
6090b86b1bbSFabien Thomas 		    }
6100b86b1bbSFabien Thomas 
6110b86b1bbSFabien Thomas 	nentries = cgn - sortbuffer;
6120b86b1bbSFabien Thomas 	assert(nentries <= pmcstat_cgnode_hash_count);
6130b86b1bbSFabien Thomas 
6140b86b1bbSFabien Thomas 	if (nentries == 0) {
6150b86b1bbSFabien Thomas 		free(sortbuffer);
6160b86b1bbSFabien Thomas 		return;
6170b86b1bbSFabien Thomas 	}
6180b86b1bbSFabien Thomas 
6190b86b1bbSFabien Thomas 	qsort(sortbuffer, nentries, sizeof(struct pmcstat_cgnode *),
6200b86b1bbSFabien Thomas 	    pmcstat_cgnode_compare);
6210b86b1bbSFabien Thomas 
6220b86b1bbSFabien Thomas 	PMCSTAT_PRINTW("%5.5s %-10.10s %-20.20s %s\n",
6230b86b1bbSFabien Thomas 	    "%SAMP", "IMAGE", "FUNCTION", "CALLERS");
6240b86b1bbSFabien Thomas 
6250b86b1bbSFabien Thomas 	nentries = min(pmcstat_displayheight - 2, nentries);
6260b86b1bbSFabien Thomas 
6270b86b1bbSFabien Thomas 	for (cgn = sortbuffer, n = 0; n < nentries; n++, cgn++) {
6280b86b1bbSFabien Thomas 		if (PMCPL_CG_COUNTP(*cgn) < pmcstat_threshold)
6290b86b1bbSFabien Thomas 			break;
6300b86b1bbSFabien Thomas 		pmcstat_cgnode_topprint(*cgn, 0, nsamples);
6310b86b1bbSFabien Thomas 	}
6320b86b1bbSFabien Thomas 
6330b86b1bbSFabien Thomas 	free(sortbuffer);
6340b86b1bbSFabien Thomas }
6350b86b1bbSFabien Thomas 
6360b86b1bbSFabien Thomas /*
6370b86b1bbSFabien Thomas  * Handle top mode keypress.
6380b86b1bbSFabien Thomas  */
6390b86b1bbSFabien Thomas 
6400b86b1bbSFabien Thomas int
6410b86b1bbSFabien Thomas pmcpl_cg_topkeypress(int c, WINDOW *w)
6420b86b1bbSFabien Thomas {
6430b86b1bbSFabien Thomas 
6440b86b1bbSFabien Thomas 	(void) c; (void) w;
6450b86b1bbSFabien Thomas 
6460b86b1bbSFabien Thomas 	return 0;
6470b86b1bbSFabien Thomas }
6480b86b1bbSFabien Thomas 
6490b86b1bbSFabien Thomas int
6500b86b1bbSFabien Thomas pmcpl_cg_init(void)
6510b86b1bbSFabien Thomas {
6520b86b1bbSFabien Thomas 	int i;
6530b86b1bbSFabien Thomas 
6540b86b1bbSFabien Thomas 	pmcstat_cgnode_hash_count = 0;
6550b86b1bbSFabien Thomas 	pmcstat_previous_filename_printed = NULL;
6560b86b1bbSFabien Thomas 
6570b86b1bbSFabien Thomas 	for (i = 0; i < PMCSTAT_NHASH; i++) {
6580b86b1bbSFabien Thomas 		LIST_INIT(&pmcstat_cgnode_hash[i]);
6590b86b1bbSFabien Thomas 	}
6600b86b1bbSFabien Thomas 
6610b86b1bbSFabien Thomas 	return (0);
6620b86b1bbSFabien Thomas }
6630b86b1bbSFabien Thomas 
6640b86b1bbSFabien Thomas void
6650b86b1bbSFabien Thomas pmcpl_cg_shutdown(FILE *mf)
6660b86b1bbSFabien Thomas {
6670b86b1bbSFabien Thomas 	int i;
6680b86b1bbSFabien Thomas 	struct pmcstat_cgnode_hash *pch, *pchtmp;
6690b86b1bbSFabien Thomas 
6700b86b1bbSFabien Thomas 	(void) mf;
6710b86b1bbSFabien Thomas 
6720b86b1bbSFabien Thomas 	if (args.pa_flags & FLAG_DO_CALLGRAPHS)
6730b86b1bbSFabien Thomas 		pmcstat_callgraph_print();
6740b86b1bbSFabien Thomas 
6750b86b1bbSFabien Thomas 	/*
6760b86b1bbSFabien Thomas 	 * Free memory.
6770b86b1bbSFabien Thomas 	 */
6780b86b1bbSFabien Thomas 	for (i = 0; i < PMCSTAT_NHASH; i++) {
6790b86b1bbSFabien Thomas 		LIST_FOREACH_SAFE(pch, &pmcstat_cgnode_hash[i], pch_next,
6800b86b1bbSFabien Thomas 		    pchtmp) {
6810b86b1bbSFabien Thomas 			pmcstat_cgnode_free(pch->pch_cgnode);
6820b86b1bbSFabien Thomas 			LIST_REMOVE(pch, pch_next);
6830b86b1bbSFabien Thomas 			free(pch);
6840b86b1bbSFabien Thomas 		}
6850b86b1bbSFabien Thomas 	}
6860b86b1bbSFabien Thomas }
6870b86b1bbSFabien Thomas 
688