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