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