xref: /freebsd/usr.sbin/pmcstat/pmcpl_callgraph.c (revision f0cfa1b168014f56c02b83e5f28412cc5f78d117)
1 /*-
2  * Copyright (c) 2005-2007, Joseph Koshy
3  * Copyright (c) 2007 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by A. Joseph Koshy under
7  * sponsorship from the FreeBSD Foundation and Google, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /*
32  * Transform a hwpmc(4) log into human readable form, and into
33  * gprof(1) compatible profiles.
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.h>
40 #include <sys/endian.h>
41 #include <sys/gmon.h>
42 #include <sys/imgact_aout.h>
43 #include <sys/imgact_elf.h>
44 #include <sys/mman.h>
45 #include <sys/pmc.h>
46 #include <sys/queue.h>
47 #include <sys/socket.h>
48 #include <sys/stat.h>
49 #include <sys/wait.h>
50 
51 #include <netinet/in.h>
52 
53 #include <assert.h>
54 #include <curses.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <gelf.h>
59 #include <libgen.h>
60 #include <limits.h>
61 #include <netdb.h>
62 #include <pmc.h>
63 #include <pmclog.h>
64 #include <sysexits.h>
65 #include <stdint.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
70 
71 #include "pmcstat.h"
72 #include "pmcstat_log.h"
73 #include "pmcstat_top.h"
74 #include "pmcpl_callgraph.h"
75 
76 #define	min(A,B)		((A) < (B) ? (A) : (B))
77 #define	max(A,B)		((A) > (B) ? (A) : (B))
78 
79 /* Get the sample value in percent related to nsamples. */
80 #define PMCPL_CG_COUNTP(a) \
81 	((a)->pcg_count * 100.0 / nsamples)
82 
83 /*
84  * The toplevel CG nodes (i.e., with rank == 0) are placed in a hash table.
85  */
86 
87 struct pmcstat_cgnode_hash_list pmcstat_cgnode_hash[PMCSTAT_NHASH];
88 int pmcstat_cgnode_hash_count;
89 
90 static pmcstat_interned_string pmcstat_previous_filename_printed;
91 
92 static struct pmcstat_cgnode *
93 pmcstat_cgnode_allocate(struct pmcstat_image *image, uintfptr_t pc)
94 {
95 	struct pmcstat_cgnode *cg;
96 
97 	if ((cg = malloc(sizeof(*cg))) == NULL)
98 		err(EX_OSERR, "ERROR: Cannot allocate callgraph node");
99 
100 	cg->pcg_image = image;
101 	cg->pcg_func = pc;
102 
103 	cg->pcg_count = 0;
104 	cg->pcg_nchildren = 0;
105 	LIST_INIT(&cg->pcg_children);
106 
107 	return (cg);
108 }
109 
110 /*
111  * Free a node and its children.
112  */
113 static void
114 pmcstat_cgnode_free(struct pmcstat_cgnode *cg)
115 {
116 	struct pmcstat_cgnode *cgc, *cgtmp;
117 
118 	LIST_FOREACH_SAFE(cgc, &cg->pcg_children, pcg_sibling, cgtmp)
119 		pmcstat_cgnode_free(cgc);
120 	free(cg);
121 }
122 
123 /*
124  * Look for a callgraph node associated with pmc `pmcid' in the global
125  * hash table that corresponds to the given `pc' value in the process
126  * `pp'.
127  */
128 static struct pmcstat_cgnode *
129 pmcstat_cgnode_hash_lookup_pc(struct pmcstat_process *pp, pmc_id_t pmcid,
130     uintfptr_t pc, int usermode)
131 {
132 	struct pmcstat_pcmap *ppm;
133 	struct pmcstat_symbol *sym;
134 	struct pmcstat_image *image;
135 	struct pmcstat_cgnode *cg;
136 	struct pmcstat_cgnode_hash *h;
137 	uintfptr_t loadaddress;
138 	unsigned int i, hash;
139 
140 	ppm = pmcstat_process_find_map(usermode ? pp : pmcstat_kernproc, pc);
141 	if (ppm == NULL)
142 		return (NULL);
143 
144 	image = ppm->ppm_image;
145 
146 	loadaddress = ppm->ppm_lowpc + image->pi_vaddr - image->pi_start;
147 	pc -= loadaddress;	/* Convert to an offset in the image. */
148 
149 	/*
150 	 * Try determine the function at this offset.  If we can't
151 	 * find a function round leave the `pc' value alone.
152 	 */
153 	if ((sym = pmcstat_symbol_search(image, pc)) != NULL)
154 		pc = sym->ps_start;
155 	else
156 		pmcstat_stats.ps_samples_unknown_function++;
157 
158 	for (hash = i = 0; i < sizeof(uintfptr_t); i++)
159 		hash += (pc >> i) & 0xFF;
160 
161 	hash &= PMCSTAT_HASH_MASK;
162 
163 	cg = NULL;
164 	LIST_FOREACH(h, &pmcstat_cgnode_hash[hash], pch_next)
165 	{
166 		if (h->pch_pmcid != pmcid)
167 			continue;
168 
169 		cg = h->pch_cgnode;
170 
171 		assert(cg != NULL);
172 
173 		if (cg->pcg_image == image && cg->pcg_func == pc)
174 			return (cg);
175 	}
176 
177 	/*
178 	 * We haven't seen this (pmcid, pc) tuple yet, so allocate a
179 	 * new callgraph node and a new hash table entry for it.
180 	 */
181 	cg = pmcstat_cgnode_allocate(image, pc);
182 	if ((h = malloc(sizeof(*h))) == NULL)
183 		err(EX_OSERR, "ERROR: Could not allocate callgraph node");
184 
185 	h->pch_pmcid = pmcid;
186 	h->pch_cgnode = cg;
187 	LIST_INSERT_HEAD(&pmcstat_cgnode_hash[hash], h, pch_next);
188 
189 	pmcstat_cgnode_hash_count++;
190 
191 	return (cg);
192 }
193 
194 /*
195  * Compare two callgraph nodes for sorting.
196  */
197 static int
198 pmcstat_cgnode_compare(const void *a, const void *b)
199 {
200 	const struct pmcstat_cgnode *const *pcg1, *const *pcg2, *cg1, *cg2;
201 
202 	pcg1 = (const struct pmcstat_cgnode *const *) a;
203 	cg1 = *pcg1;
204 	pcg2 = (const struct pmcstat_cgnode *const *) b;
205 	cg2 = *pcg2;
206 
207 	/* Sort in reverse order */
208 	if (cg1->pcg_count < cg2->pcg_count)
209 		return (1);
210 	if (cg1->pcg_count > cg2->pcg_count)
211 		return (-1);
212 	return (0);
213 }
214 
215 /*
216  * Find (allocating if a needed) a callgraph node in the given
217  * parent with the same (image, pcoffset) pair.
218  */
219 
220 static struct pmcstat_cgnode *
221 pmcstat_cgnode_find(struct pmcstat_cgnode *parent, struct pmcstat_image *image,
222     uintfptr_t pcoffset)
223 {
224 	struct pmcstat_cgnode *child;
225 
226 	LIST_FOREACH(child, &parent->pcg_children, pcg_sibling) {
227 		if (child->pcg_image == image &&
228 		    child->pcg_func == pcoffset)
229 			return (child);
230 	}
231 
232 	/*
233 	 * Allocate a new structure.
234 	 */
235 
236 	child = pmcstat_cgnode_allocate(image, pcoffset);
237 
238 	/*
239 	 * Link it into the parent.
240 	 */
241 	LIST_INSERT_HEAD(&parent->pcg_children, child, pcg_sibling);
242 	parent->pcg_nchildren++;
243 
244 	return (child);
245 }
246 
247 /*
248  * Print one callgraph node.  The output format is:
249  *
250  * indentation %(parent's samples) #nsamples function@object
251  */
252 static void
253 pmcstat_cgnode_print(struct pmcstat_cgnode *cg, int depth, uint32_t total)
254 {
255 	uint32_t n;
256 	const char *space;
257 	struct pmcstat_symbol *sym;
258 	struct pmcstat_cgnode **sortbuffer, **cgn, *pcg;
259 
260 	space = " ";
261 
262 	if (depth > 0)
263 		(void) fprintf(args.pa_graphfile, "%*s", depth, space);
264 
265 	if (cg->pcg_count == total)
266 		(void) fprintf(args.pa_graphfile, "100.0%% ");
267 	else
268 		(void) fprintf(args.pa_graphfile, "%05.2f%% ",
269 		    100.0 * cg->pcg_count / total);
270 
271 	n = fprintf(args.pa_graphfile, " [%u] ", cg->pcg_count);
272 
273 	/* #samples is a 12 character wide field. */
274 	if (n < 12)
275 		(void) fprintf(args.pa_graphfile, "%*s", 12 - n, space);
276 
277 	if (depth > 0)
278 		(void) fprintf(args.pa_graphfile, "%*s", depth, space);
279 
280 	sym = pmcstat_symbol_search(cg->pcg_image, cg->pcg_func);
281 	if (sym)
282 		(void) fprintf(args.pa_graphfile, "%s",
283 		    pmcstat_string_unintern(sym->ps_name));
284 	else
285 		(void) fprintf(args.pa_graphfile, "%p",
286 		    (void *) (cg->pcg_image->pi_vaddr + cg->pcg_func));
287 
288 	if (pmcstat_previous_filename_printed !=
289 	    cg->pcg_image->pi_fullpath) {
290 		pmcstat_previous_filename_printed = cg->pcg_image->pi_fullpath;
291 		(void) fprintf(args.pa_graphfile, " @ %s\n",
292 		    pmcstat_string_unintern(
293 		    pmcstat_previous_filename_printed));
294 	} else
295 		(void) fprintf(args.pa_graphfile, "\n");
296 
297 	if (cg->pcg_nchildren == 0)
298 		return;
299 
300 	if ((sortbuffer = (struct pmcstat_cgnode **)
301 		malloc(sizeof(struct pmcstat_cgnode *) *
302 		    cg->pcg_nchildren)) == NULL)
303 		err(EX_OSERR, "ERROR: Cannot print callgraph");
304 	cgn = sortbuffer;
305 
306 	LIST_FOREACH(pcg, &cg->pcg_children, pcg_sibling)
307 	    *cgn++ = pcg;
308 
309 	assert(cgn - sortbuffer == (int) cg->pcg_nchildren);
310 
311 	qsort(sortbuffer, cg->pcg_nchildren, sizeof(struct pmcstat_cgnode *),
312 	    pmcstat_cgnode_compare);
313 
314 	for (cgn = sortbuffer, n = 0; n < cg->pcg_nchildren; n++, cgn++)
315 		pmcstat_cgnode_print(*cgn, depth+1, cg->pcg_count);
316 
317 	free(sortbuffer);
318 }
319 
320 /*
321  * Record a callchain.
322  */
323 
324 void
325 pmcpl_cg_process(struct pmcstat_process *pp, struct pmcstat_pmcrecord *pmcr,
326     uint32_t nsamples, uintfptr_t *cc, int usermode, uint32_t cpu)
327 {
328 	uintfptr_t pc, loadaddress;
329 	uint32_t n;
330 	struct pmcstat_image *image;
331 	struct pmcstat_pcmap *ppm;
332 	struct pmcstat_symbol *sym;
333 	struct pmcstat_cgnode *parent, *child;
334 	struct pmcstat_process *km;
335 	pmc_id_t pmcid;
336 
337 	(void) cpu;
338 
339 	/*
340 	 * Find the callgraph node recorded in the global hash table
341 	 * for this (pmcid, pc).
342 	 */
343 
344 	pc = cc[0];
345 	pmcid = pmcr->pr_pmcid;
346 	parent = pmcstat_cgnode_hash_lookup_pc(pp, pmcid, pc, usermode);
347 	if (parent == NULL) {
348 		pmcstat_stats.ps_callchain_dubious_frames++;
349 		pmcr->pr_dubious_frames++;
350 		return;
351 	}
352 
353 	parent->pcg_count++;
354 
355 	/*
356 	 * For each return address in the call chain record, subject
357 	 * to the maximum depth desired.
358 	 * - Find the image associated with the sample.  Stop if there
359 	 *   there is no valid image at that address.
360 	 * - Find the function that overlaps the return address.
361 	 * - If found: use the start address of the function.
362 	 *   If not found (say an object's symbol table is not present or
363 	 *   is incomplete), round down to th gprof bucket granularity.
364 	 * - Convert return virtual address to an offset in the image.
365 	 * - Look for a child with the same {offset,image} tuple,
366 	 *   inserting one if needed.
367 	 * - Increment the count of occurrences of the child.
368 	 */
369 	km = pmcstat_kernproc;
370 
371 	for (n = 1; n < (uint32_t) args.pa_graphdepth && n < nsamples; n++,
372 	    parent = child) {
373 		pc = cc[n];
374 
375 		ppm = pmcstat_process_find_map(usermode ? pp : km, pc);
376 		if (ppm == NULL) {
377 			/* Detect full frame capture (kernel + user). */
378 			if (!usermode) {
379 				ppm = pmcstat_process_find_map(pp, pc);
380 				if (ppm != NULL)
381 					km = pp;
382 			}
383 		}
384 		if (ppm == NULL)
385 			return;
386 
387 		image = ppm->ppm_image;
388 		loadaddress = ppm->ppm_lowpc + image->pi_vaddr -
389 		    image->pi_start;
390 		pc -= loadaddress;
391 
392 		if ((sym = pmcstat_symbol_search(image, pc)) != NULL)
393 			pc = sym->ps_start;
394 
395 		child = pmcstat_cgnode_find(parent, image, pc);
396 		child->pcg_count++;
397 	}
398 }
399 
400 /*
401  * Printing a callgraph for a PMC.
402  */
403 static void
404 pmcstat_callgraph_print_for_pmcid(struct pmcstat_pmcrecord *pmcr)
405 {
406 	int n, nentries;
407 	uint32_t nsamples;
408 	pmc_id_t pmcid;
409 	struct pmcstat_cgnode **sortbuffer, **cgn;
410 	struct pmcstat_cgnode_hash *pch;
411 
412 	/*
413 	 * We pull out all callgraph nodes in the top-level hash table
414 	 * with a matching PMC id.  We then sort these based on the
415 	 * frequency of occurrence.  Each callgraph node is then
416 	 * printed.
417 	 */
418 
419 	nsamples = 0;
420 	pmcid = pmcr->pr_pmcid;
421 	if ((sortbuffer = (struct pmcstat_cgnode **)
422 	    malloc(sizeof(struct pmcstat_cgnode *) *
423 	    pmcstat_cgnode_hash_count)) == NULL)
424 		err(EX_OSERR, "ERROR: Cannot sort callgraph");
425 	cgn = sortbuffer;
426 
427 	for (n = 0; n < PMCSTAT_NHASH; n++)
428 		LIST_FOREACH(pch, &pmcstat_cgnode_hash[n], pch_next)
429 		    if (pch->pch_pmcid == pmcid) {
430 			    nsamples += pch->pch_cgnode->pcg_count;
431 			    *cgn++ = pch->pch_cgnode;
432 		    }
433 
434 	nentries = cgn - sortbuffer;
435 	assert(nentries <= pmcstat_cgnode_hash_count);
436 
437 	if (nentries == 0) {
438 		free(sortbuffer);
439 		return;
440 	}
441 
442 	qsort(sortbuffer, nentries, sizeof(struct pmcstat_cgnode *),
443 	    pmcstat_cgnode_compare);
444 
445 	(void) fprintf(args.pa_graphfile,
446 	    "@ %s [%u samples]\n\n",
447 	    pmcstat_string_unintern(pmcr->pr_pmcname),
448 	    nsamples);
449 
450 	for (cgn = sortbuffer, n = 0; n < nentries; n++, cgn++) {
451 		pmcstat_previous_filename_printed = NULL;
452 		pmcstat_cgnode_print(*cgn, 0, nsamples);
453 		(void) fprintf(args.pa_graphfile, "\n");
454 	}
455 
456 	free(sortbuffer);
457 }
458 
459 /*
460  * Print out callgraphs.
461  */
462 
463 static void
464 pmcstat_callgraph_print(void)
465 {
466 	struct pmcstat_pmcrecord *pmcr;
467 
468 	LIST_FOREACH(pmcr, &pmcstat_pmcs, pr_next)
469 	    pmcstat_callgraph_print_for_pmcid(pmcr);
470 }
471 
472 static void
473 pmcstat_cgnode_topprint(struct pmcstat_cgnode *cg,
474     int depth, uint32_t nsamples)
475 {
476 	int v_attrs, vs_len, ns_len, width, len, n, nchildren;
477 	float v;
478 	char ns[30], vs[10];
479 	struct pmcstat_symbol *sym;
480 	struct pmcstat_cgnode **sortbuffer, **cgn, *pcg;
481 
482 	(void) depth;
483 
484 	/* Format value. */
485 	v = PMCPL_CG_COUNTP(cg);
486 	snprintf(vs, sizeof(vs), "%.1f", v);
487 	v_attrs = PMCSTAT_ATTRPERCENT(v);
488 
489 	/* Format name. */
490 	sym = pmcstat_symbol_search(cg->pcg_image, cg->pcg_func);
491 	if (sym != NULL) {
492 		snprintf(ns, sizeof(ns), "%s",
493 		    pmcstat_string_unintern(sym->ps_name));
494 	} else
495 		snprintf(ns, sizeof(ns), "%p",
496 		    (void *)cg->pcg_func);
497 
498 	PMCSTAT_ATTRON(v_attrs);
499 	PMCSTAT_PRINTW("%5.5s", vs);
500 	PMCSTAT_ATTROFF(v_attrs);
501 	PMCSTAT_PRINTW(" %-10.10s %-20.20s",
502 	    pmcstat_string_unintern(cg->pcg_image->pi_name),
503 	    ns);
504 
505 	nchildren = cg->pcg_nchildren;
506 	if (nchildren == 0) {
507 		PMCSTAT_PRINTW("\n");
508 		return;
509 	}
510 
511 	width = pmcstat_displaywidth - 40;
512 
513 	if ((sortbuffer = (struct pmcstat_cgnode **)
514 		malloc(sizeof(struct pmcstat_cgnode *) *
515 		    nchildren)) == NULL)
516 		err(EX_OSERR, "ERROR: Cannot print callgraph");
517 	cgn = sortbuffer;
518 
519 	LIST_FOREACH(pcg, &cg->pcg_children, pcg_sibling)
520 	    *cgn++ = pcg;
521 
522 	assert(cgn - sortbuffer == (int)nchildren);
523 
524 	qsort(sortbuffer, nchildren, sizeof(struct pmcstat_cgnode *),
525 	    pmcstat_cgnode_compare);
526 
527 	/* Count how many callers. */
528 	for (cgn = sortbuffer, n = 0; n < nchildren; n++, cgn++) {
529 		pcg = *cgn;
530 
531 		v = PMCPL_CG_COUNTP(pcg);
532 		if (v < pmcstat_threshold)
533 			break;
534 	}
535 	nchildren = n;
536 
537 	for (cgn = sortbuffer, n = 0; n < nchildren; n++, cgn++) {
538 		pcg = *cgn;
539 
540 		/* Format value. */
541 		if (nchildren > 1) {
542 			v = PMCPL_CG_COUNTP(pcg);
543 			vs_len = snprintf(vs, sizeof(vs), ":%.1f", v);
544 			v_attrs = PMCSTAT_ATTRPERCENT(v);
545 		} else
546 			vs_len = 0;
547 
548 		/* Format name. */
549 		sym = pmcstat_symbol_search(pcg->pcg_image, pcg->pcg_func);
550 		if (sym != NULL) {
551 			ns_len = snprintf(ns, sizeof(ns), "%s",
552 			    pmcstat_string_unintern(sym->ps_name));
553 		} else
554 			ns_len = snprintf(ns, sizeof(ns), "%p",
555 			    (void *)pcg->pcg_func);
556 
557 		len = ns_len + vs_len + 1;
558 		if (width - len < 0) {
559 			PMCSTAT_PRINTW(" ...");
560 			break;
561 		}
562 		width -= len;
563 
564 		PMCSTAT_PRINTW(" %s", ns);
565 		if (nchildren > 1) {
566 			PMCSTAT_ATTRON(v_attrs);
567 			PMCSTAT_PRINTW("%s", vs);
568 			PMCSTAT_ATTROFF(v_attrs);
569 		}
570 	}
571 	PMCSTAT_PRINTW("\n");
572 	free(sortbuffer);
573 }
574 
575 /*
576  * Top mode display.
577  */
578 
579 void
580 pmcpl_cg_topdisplay(void)
581 {
582 	int n, nentries;
583 	uint32_t nsamples;
584 	struct pmcstat_cgnode **sortbuffer, **cgn;
585 	struct pmcstat_cgnode_hash *pch;
586 	struct pmcstat_pmcrecord *pmcr;
587 
588 	pmcr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter);
589 	if (!pmcr)
590 		err(EX_SOFTWARE, "ERROR: invalid pmcindex");
591 
592 	/*
593 	 * We pull out all callgraph nodes in the top-level hash table
594 	 * with a matching PMC index.  We then sort these based on the
595 	 * frequency of occurrence.  Each callgraph node is then
596 	 * printed.
597 	 */
598 
599 	nsamples = 0;
600 
601 	if ((sortbuffer = (struct pmcstat_cgnode **)
602 	    malloc(sizeof(struct pmcstat_cgnode *) *
603 	    pmcstat_cgnode_hash_count)) == NULL)
604 		err(EX_OSERR, "ERROR: Cannot sort callgraph");
605 	cgn = sortbuffer;
606 
607 	for (n = 0; n < PMCSTAT_NHASH; n++)
608 		LIST_FOREACH(pch, &pmcstat_cgnode_hash[n], pch_next)
609 		    if (pmcr == NULL || pch->pch_pmcid == pmcr->pr_pmcid) {
610 			    nsamples += pch->pch_cgnode->pcg_count;
611 			    *cgn++ = pch->pch_cgnode;
612 		    }
613 
614 	nentries = cgn - sortbuffer;
615 	assert(nentries <= pmcstat_cgnode_hash_count);
616 
617 	if (nentries == 0) {
618 		free(sortbuffer);
619 		return;
620 	}
621 
622 	qsort(sortbuffer, nentries, sizeof(struct pmcstat_cgnode *),
623 	    pmcstat_cgnode_compare);
624 
625 	PMCSTAT_PRINTW("%5.5s %-10.10s %-20.20s %s\n",
626 	    "%SAMP", "IMAGE", "FUNCTION", "CALLERS");
627 
628 	nentries = min(pmcstat_displayheight - 2, nentries);
629 
630 	for (cgn = sortbuffer, n = 0; n < nentries; n++, cgn++) {
631 		if (PMCPL_CG_COUNTP(*cgn) < pmcstat_threshold)
632 			break;
633 		pmcstat_cgnode_topprint(*cgn, 0, nsamples);
634 	}
635 
636 	free(sortbuffer);
637 }
638 
639 /*
640  * Handle top mode keypress.
641  */
642 
643 int
644 pmcpl_cg_topkeypress(int c, void *arg)
645 {
646 	WINDOW *w;
647 
648 	w = (WINDOW *)arg;
649 
650 	(void) c; (void) w;
651 
652 	return 0;
653 }
654 
655 int
656 pmcpl_cg_init(void)
657 {
658 	int i;
659 
660 	pmcstat_cgnode_hash_count = 0;
661 	pmcstat_previous_filename_printed = NULL;
662 
663 	for (i = 0; i < PMCSTAT_NHASH; i++) {
664 		LIST_INIT(&pmcstat_cgnode_hash[i]);
665 	}
666 
667 	return (0);
668 }
669 
670 void
671 pmcpl_cg_shutdown(FILE *mf)
672 {
673 	int i;
674 	struct pmcstat_cgnode_hash *pch, *pchtmp;
675 
676 	(void) mf;
677 
678 	if (args.pa_flags & FLAG_DO_CALLGRAPHS)
679 		pmcstat_callgraph_print();
680 
681 	/*
682 	 * Free memory.
683 	 */
684 	for (i = 0; i < PMCSTAT_NHASH; i++) {
685 		LIST_FOREACH_SAFE(pch, &pmcstat_cgnode_hash[i], pch_next,
686 		    pchtmp) {
687 			pmcstat_cgnode_free(pch->pch_cgnode);
688 			LIST_REMOVE(pch, pch_next);
689 			free(pch);
690 		}
691 	}
692 }
693 
694