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; 1520b86b1bbSFabien Thomas 1530b86b1bbSFabien Thomas for (hash = i = 0; i < sizeof(uintfptr_t); i++) 1540b86b1bbSFabien Thomas hash += (pc >> i) & 0xFF; 1550b86b1bbSFabien Thomas 1560b86b1bbSFabien Thomas hash &= PMCSTAT_HASH_MASK; 1570b86b1bbSFabien Thomas 1580b86b1bbSFabien Thomas cg = NULL; 1590b86b1bbSFabien Thomas LIST_FOREACH(h, &pmcstat_cgnode_hash[hash], pch_next) 1600b86b1bbSFabien Thomas { 1610b86b1bbSFabien Thomas if (h->pch_pmcid != pmcid) 1620b86b1bbSFabien Thomas continue; 1630b86b1bbSFabien Thomas 1640b86b1bbSFabien Thomas cg = h->pch_cgnode; 1650b86b1bbSFabien Thomas 1660b86b1bbSFabien Thomas assert(cg != NULL); 1670b86b1bbSFabien Thomas 1680b86b1bbSFabien Thomas if (cg->pcg_image == image && cg->pcg_func == pc) 1690b86b1bbSFabien Thomas return (cg); 1700b86b1bbSFabien Thomas } 1710b86b1bbSFabien Thomas 1720b86b1bbSFabien Thomas /* 1730b86b1bbSFabien Thomas * We haven't seen this (pmcid, pc) tuple yet, so allocate a 1740b86b1bbSFabien Thomas * new callgraph node and a new hash table entry for it. 1750b86b1bbSFabien Thomas */ 1760b86b1bbSFabien Thomas cg = pmcstat_cgnode_allocate(image, pc); 1770b86b1bbSFabien Thomas if ((h = malloc(sizeof(*h))) == NULL) 1780b86b1bbSFabien Thomas err(EX_OSERR, "ERROR: Could not allocate callgraph node"); 1790b86b1bbSFabien Thomas 1800b86b1bbSFabien Thomas h->pch_pmcid = pmcid; 1810b86b1bbSFabien Thomas h->pch_cgnode = cg; 1820b86b1bbSFabien Thomas LIST_INSERT_HEAD(&pmcstat_cgnode_hash[hash], h, pch_next); 1830b86b1bbSFabien Thomas 1840b86b1bbSFabien Thomas pmcstat_cgnode_hash_count++; 1850b86b1bbSFabien Thomas 1860b86b1bbSFabien Thomas return (cg); 1870b86b1bbSFabien Thomas } 1880b86b1bbSFabien Thomas 1890b86b1bbSFabien Thomas /* 1900b86b1bbSFabien Thomas * Compare two callgraph nodes for sorting. 1910b86b1bbSFabien Thomas */ 1920b86b1bbSFabien Thomas static int 1930b86b1bbSFabien Thomas pmcstat_cgnode_compare(const void *a, const void *b) 1940b86b1bbSFabien Thomas { 1950b86b1bbSFabien Thomas const struct pmcstat_cgnode *const *pcg1, *const *pcg2, *cg1, *cg2; 1960b86b1bbSFabien Thomas 1970b86b1bbSFabien Thomas pcg1 = (const struct pmcstat_cgnode *const *) a; 1980b86b1bbSFabien Thomas cg1 = *pcg1; 1990b86b1bbSFabien Thomas pcg2 = (const struct pmcstat_cgnode *const *) b; 2000b86b1bbSFabien Thomas cg2 = *pcg2; 2010b86b1bbSFabien Thomas 2020b86b1bbSFabien Thomas /* Sort in reverse order */ 2030b86b1bbSFabien Thomas if (cg1->pcg_count < cg2->pcg_count) 2040b86b1bbSFabien Thomas return (1); 2050b86b1bbSFabien Thomas if (cg1->pcg_count > cg2->pcg_count) 2060b86b1bbSFabien Thomas return (-1); 2070b86b1bbSFabien Thomas return (0); 2080b86b1bbSFabien Thomas } 2090b86b1bbSFabien Thomas 2100b86b1bbSFabien Thomas /* 2110b86b1bbSFabien Thomas * Find (allocating if a needed) a callgraph node in the given 2120b86b1bbSFabien Thomas * parent with the same (image, pcoffset) pair. 2130b86b1bbSFabien Thomas */ 2140b86b1bbSFabien Thomas 2150b86b1bbSFabien Thomas static struct pmcstat_cgnode * 2160b86b1bbSFabien Thomas pmcstat_cgnode_find(struct pmcstat_cgnode *parent, struct pmcstat_image *image, 2170b86b1bbSFabien Thomas uintfptr_t pcoffset) 2180b86b1bbSFabien Thomas { 2190b86b1bbSFabien Thomas struct pmcstat_cgnode *child; 2200b86b1bbSFabien Thomas 2210b86b1bbSFabien Thomas LIST_FOREACH(child, &parent->pcg_children, pcg_sibling) { 2220b86b1bbSFabien Thomas if (child->pcg_image == image && 2230b86b1bbSFabien Thomas child->pcg_func == pcoffset) 2240b86b1bbSFabien Thomas return (child); 2250b86b1bbSFabien Thomas } 2260b86b1bbSFabien Thomas 2270b86b1bbSFabien Thomas /* 2280b86b1bbSFabien Thomas * Allocate a new structure. 2290b86b1bbSFabien Thomas */ 2300b86b1bbSFabien Thomas 2310b86b1bbSFabien Thomas child = pmcstat_cgnode_allocate(image, pcoffset); 2320b86b1bbSFabien Thomas 2330b86b1bbSFabien Thomas /* 2340b86b1bbSFabien Thomas * Link it into the parent. 2350b86b1bbSFabien Thomas */ 2360b86b1bbSFabien Thomas LIST_INSERT_HEAD(&parent->pcg_children, child, pcg_sibling); 2370b86b1bbSFabien Thomas parent->pcg_nchildren++; 2380b86b1bbSFabien Thomas 2390b86b1bbSFabien Thomas return (child); 2400b86b1bbSFabien Thomas } 2410b86b1bbSFabien Thomas 2420b86b1bbSFabien Thomas /* 2430b86b1bbSFabien Thomas * Print one callgraph node. The output format is: 2440b86b1bbSFabien Thomas * 2450b86b1bbSFabien Thomas * indentation %(parent's samples) #nsamples function@object 2460b86b1bbSFabien Thomas */ 2470b86b1bbSFabien Thomas static void 2480b86b1bbSFabien Thomas pmcstat_cgnode_print(struct pmcstat_cgnode *cg, int depth, uint32_t total) 2490b86b1bbSFabien Thomas { 2500b86b1bbSFabien Thomas uint32_t n; 2510b86b1bbSFabien Thomas const char *space; 2520b86b1bbSFabien Thomas struct pmcstat_symbol *sym; 2530b86b1bbSFabien Thomas struct pmcstat_cgnode **sortbuffer, **cgn, *pcg; 2540b86b1bbSFabien Thomas 2550b86b1bbSFabien Thomas space = " "; 2560b86b1bbSFabien Thomas 2570b86b1bbSFabien Thomas if (depth > 0) 2580b86b1bbSFabien Thomas (void) fprintf(args.pa_graphfile, "%*s", depth, space); 2590b86b1bbSFabien Thomas 2600b86b1bbSFabien Thomas if (cg->pcg_count == total) 2610b86b1bbSFabien Thomas (void) fprintf(args.pa_graphfile, "100.0%% "); 2620b86b1bbSFabien Thomas else 2630b86b1bbSFabien Thomas (void) fprintf(args.pa_graphfile, "%05.2f%% ", 2640b86b1bbSFabien Thomas 100.0 * cg->pcg_count / total); 2650b86b1bbSFabien Thomas 2660b86b1bbSFabien Thomas n = fprintf(args.pa_graphfile, " [%u] ", cg->pcg_count); 2670b86b1bbSFabien Thomas 2680b86b1bbSFabien Thomas /* #samples is a 12 character wide field. */ 2690b86b1bbSFabien Thomas if (n < 12) 2700b86b1bbSFabien Thomas (void) fprintf(args.pa_graphfile, "%*s", 12 - n, space); 2710b86b1bbSFabien Thomas 2720b86b1bbSFabien Thomas if (depth > 0) 2730b86b1bbSFabien Thomas (void) fprintf(args.pa_graphfile, "%*s", depth, space); 2740b86b1bbSFabien Thomas 2750b86b1bbSFabien Thomas sym = pmcstat_symbol_search(cg->pcg_image, cg->pcg_func); 2760b86b1bbSFabien Thomas if (sym) 2770b86b1bbSFabien Thomas (void) fprintf(args.pa_graphfile, "%s", 2780b86b1bbSFabien Thomas pmcstat_string_unintern(sym->ps_name)); 2790b86b1bbSFabien Thomas else 2800b86b1bbSFabien Thomas (void) fprintf(args.pa_graphfile, "%p", 2810b86b1bbSFabien Thomas (void *) (cg->pcg_image->pi_vaddr + cg->pcg_func)); 2820b86b1bbSFabien Thomas 2830b86b1bbSFabien Thomas if (pmcstat_previous_filename_printed != 2840b86b1bbSFabien Thomas cg->pcg_image->pi_fullpath) { 2850b86b1bbSFabien Thomas pmcstat_previous_filename_printed = cg->pcg_image->pi_fullpath; 2860b86b1bbSFabien Thomas (void) fprintf(args.pa_graphfile, " @ %s\n", 2870b86b1bbSFabien Thomas pmcstat_string_unintern( 2880b86b1bbSFabien Thomas pmcstat_previous_filename_printed)); 2890b86b1bbSFabien Thomas } else 2900b86b1bbSFabien Thomas (void) fprintf(args.pa_graphfile, "\n"); 2910b86b1bbSFabien Thomas 2920b86b1bbSFabien Thomas if (cg->pcg_nchildren == 0) 2930b86b1bbSFabien Thomas return; 2940b86b1bbSFabien Thomas 2950b86b1bbSFabien Thomas if ((sortbuffer = (struct pmcstat_cgnode **) 2960b86b1bbSFabien Thomas malloc(sizeof(struct pmcstat_cgnode *) * 2970b86b1bbSFabien Thomas cg->pcg_nchildren)) == NULL) 2980b86b1bbSFabien Thomas err(EX_OSERR, "ERROR: Cannot print callgraph"); 2990b86b1bbSFabien Thomas cgn = sortbuffer; 3000b86b1bbSFabien Thomas 3010b86b1bbSFabien Thomas LIST_FOREACH(pcg, &cg->pcg_children, pcg_sibling) 3020b86b1bbSFabien Thomas *cgn++ = pcg; 3030b86b1bbSFabien Thomas 3040b86b1bbSFabien Thomas assert(cgn - sortbuffer == (int) cg->pcg_nchildren); 3050b86b1bbSFabien Thomas 3060b86b1bbSFabien Thomas qsort(sortbuffer, cg->pcg_nchildren, sizeof(struct pmcstat_cgnode *), 3070b86b1bbSFabien Thomas pmcstat_cgnode_compare); 3080b86b1bbSFabien Thomas 3090b86b1bbSFabien Thomas for (cgn = sortbuffer, n = 0; n < cg->pcg_nchildren; n++, cgn++) 3100b86b1bbSFabien Thomas pmcstat_cgnode_print(*cgn, depth+1, cg->pcg_count); 3110b86b1bbSFabien Thomas 3120b86b1bbSFabien Thomas free(sortbuffer); 3130b86b1bbSFabien Thomas } 3140b86b1bbSFabien Thomas 3150b86b1bbSFabien Thomas /* 3160b86b1bbSFabien Thomas * Record a callchain. 3170b86b1bbSFabien Thomas */ 3180b86b1bbSFabien Thomas 3190b86b1bbSFabien Thomas void 3200b86b1bbSFabien Thomas pmcpl_cg_process(struct pmcstat_process *pp, struct pmcstat_pmcrecord *pmcr, 3210b86b1bbSFabien Thomas uint32_t nsamples, uintfptr_t *cc, int usermode, uint32_t cpu) 3220b86b1bbSFabien Thomas { 3230b86b1bbSFabien Thomas uintfptr_t pc, loadaddress; 3240b86b1bbSFabien Thomas uint32_t n; 3250b86b1bbSFabien Thomas struct pmcstat_image *image; 3260b86b1bbSFabien Thomas struct pmcstat_pcmap *ppm; 3270b86b1bbSFabien Thomas struct pmcstat_symbol *sym; 3280b86b1bbSFabien Thomas struct pmcstat_cgnode *parent, *child; 3290b86b1bbSFabien Thomas struct pmcstat_process *km; 3300b86b1bbSFabien Thomas pmc_id_t pmcid; 3310b86b1bbSFabien Thomas 3320b86b1bbSFabien Thomas (void) cpu; 3330b86b1bbSFabien Thomas 3340b86b1bbSFabien Thomas /* 3350b86b1bbSFabien Thomas * Find the callgraph node recorded in the global hash table 3360b86b1bbSFabien Thomas * for this (pmcid, pc). 3370b86b1bbSFabien Thomas */ 3380b86b1bbSFabien Thomas 3390b86b1bbSFabien Thomas pc = cc[0]; 3400b86b1bbSFabien Thomas pmcid = pmcr->pr_pmcid; 3410b86b1bbSFabien Thomas parent = pmcstat_cgnode_hash_lookup_pc(pp, pmcid, pc, usermode); 3420b86b1bbSFabien Thomas if (parent == NULL) { 3430b86b1bbSFabien Thomas pmcstat_stats.ps_callchain_dubious_frames++; 3440b86b1bbSFabien Thomas return; 3450b86b1bbSFabien Thomas } 3460b86b1bbSFabien Thomas 3470b86b1bbSFabien Thomas parent->pcg_count++; 3480b86b1bbSFabien Thomas 3490b86b1bbSFabien Thomas /* 3500b86b1bbSFabien Thomas * For each return address in the call chain record, subject 3510b86b1bbSFabien Thomas * to the maximum depth desired. 3520b86b1bbSFabien Thomas * - Find the image associated with the sample. Stop if there 3530b86b1bbSFabien Thomas * there is no valid image at that address. 3540b86b1bbSFabien Thomas * - Find the function that overlaps the return address. 3550b86b1bbSFabien Thomas * - If found: use the start address of the function. 3560b86b1bbSFabien Thomas * If not found (say an object's symbol table is not present or 3570b86b1bbSFabien Thomas * is incomplete), round down to th gprof bucket granularity. 3580b86b1bbSFabien Thomas * - Convert return virtual address to an offset in the image. 3590b86b1bbSFabien Thomas * - Look for a child with the same {offset,image} tuple, 3600b86b1bbSFabien Thomas * inserting one if needed. 3610b86b1bbSFabien Thomas * - Increment the count of occurrences of the child. 3620b86b1bbSFabien Thomas */ 3630b86b1bbSFabien Thomas km = pmcstat_kernproc; 3640b86b1bbSFabien Thomas 3650b86b1bbSFabien Thomas for (n = 1; n < (uint32_t) args.pa_graphdepth && n < nsamples; n++, 3660b86b1bbSFabien Thomas parent = child) { 3670b86b1bbSFabien Thomas pc = cc[n]; 3680b86b1bbSFabien Thomas 3690b86b1bbSFabien Thomas ppm = pmcstat_process_find_map(usermode ? pp : km, pc); 3700b86b1bbSFabien Thomas if (ppm == NULL) { 3710b86b1bbSFabien Thomas /* Detect full frame capture (kernel + user). */ 3720b86b1bbSFabien Thomas if (!usermode) { 3730b86b1bbSFabien Thomas ppm = pmcstat_process_find_map(pp, pc); 3740b86b1bbSFabien Thomas if (ppm != NULL) 3750b86b1bbSFabien Thomas km = pp; 3760b86b1bbSFabien Thomas } 3770b86b1bbSFabien Thomas } 3780b86b1bbSFabien Thomas if (ppm == NULL) 3790b86b1bbSFabien Thomas return; 3800b86b1bbSFabien Thomas 3810b86b1bbSFabien Thomas image = ppm->ppm_image; 3820b86b1bbSFabien Thomas loadaddress = ppm->ppm_lowpc + image->pi_vaddr - 3830b86b1bbSFabien Thomas image->pi_start; 3840b86b1bbSFabien Thomas pc -= loadaddress; 3850b86b1bbSFabien Thomas 3860b86b1bbSFabien Thomas if ((sym = pmcstat_symbol_search(image, pc)) != NULL) 3870b86b1bbSFabien Thomas pc = sym->ps_start; 3880b86b1bbSFabien Thomas 3890b86b1bbSFabien Thomas child = pmcstat_cgnode_find(parent, image, pc); 3900b86b1bbSFabien Thomas child->pcg_count++; 3910b86b1bbSFabien Thomas } 3920b86b1bbSFabien Thomas } 3930b86b1bbSFabien Thomas 3940b86b1bbSFabien Thomas /* 3950b86b1bbSFabien Thomas * Printing a callgraph for a PMC. 3960b86b1bbSFabien Thomas */ 3970b86b1bbSFabien Thomas static void 3980b86b1bbSFabien Thomas pmcstat_callgraph_print_for_pmcid(struct pmcstat_pmcrecord *pmcr) 3990b86b1bbSFabien Thomas { 4000b86b1bbSFabien Thomas int n, nentries; 4010b86b1bbSFabien Thomas uint32_t nsamples; 4020b86b1bbSFabien Thomas pmc_id_t pmcid; 4030b86b1bbSFabien Thomas struct pmcstat_cgnode **sortbuffer, **cgn; 4040b86b1bbSFabien Thomas struct pmcstat_cgnode_hash *pch; 4050b86b1bbSFabien Thomas 4060b86b1bbSFabien Thomas /* 4070b86b1bbSFabien Thomas * We pull out all callgraph nodes in the top-level hash table 4080b86b1bbSFabien Thomas * with a matching PMC id. We then sort these based on the 4090b86b1bbSFabien Thomas * frequency of occurrence. Each callgraph node is then 4100b86b1bbSFabien Thomas * printed. 4110b86b1bbSFabien Thomas */ 4120b86b1bbSFabien Thomas 4130b86b1bbSFabien Thomas nsamples = 0; 4140b86b1bbSFabien Thomas pmcid = pmcr->pr_pmcid; 4150b86b1bbSFabien Thomas if ((sortbuffer = (struct pmcstat_cgnode **) 4160b86b1bbSFabien Thomas malloc(sizeof(struct pmcstat_cgnode *) * 4170b86b1bbSFabien Thomas pmcstat_cgnode_hash_count)) == NULL) 4180b86b1bbSFabien Thomas err(EX_OSERR, "ERROR: Cannot sort callgraph"); 4190b86b1bbSFabien Thomas cgn = sortbuffer; 4200b86b1bbSFabien Thomas 4210b86b1bbSFabien Thomas for (n = 0; n < PMCSTAT_NHASH; n++) 4220b86b1bbSFabien Thomas LIST_FOREACH(pch, &pmcstat_cgnode_hash[n], pch_next) 4230b86b1bbSFabien Thomas if (pch->pch_pmcid == pmcid) { 4240b86b1bbSFabien Thomas nsamples += pch->pch_cgnode->pcg_count; 4250b86b1bbSFabien Thomas *cgn++ = pch->pch_cgnode; 4260b86b1bbSFabien Thomas } 4270b86b1bbSFabien Thomas 4280b86b1bbSFabien Thomas nentries = cgn - sortbuffer; 4290b86b1bbSFabien Thomas assert(nentries <= pmcstat_cgnode_hash_count); 4300b86b1bbSFabien Thomas 4310b86b1bbSFabien Thomas if (nentries == 0) { 4320b86b1bbSFabien Thomas free(sortbuffer); 4330b86b1bbSFabien Thomas return; 4340b86b1bbSFabien Thomas } 4350b86b1bbSFabien Thomas 4360b86b1bbSFabien Thomas qsort(sortbuffer, nentries, sizeof(struct pmcstat_cgnode *), 4370b86b1bbSFabien Thomas pmcstat_cgnode_compare); 4380b86b1bbSFabien Thomas 4390b86b1bbSFabien Thomas (void) fprintf(args.pa_graphfile, 4400b86b1bbSFabien Thomas "@ %s [%u samples]\n\n", 4410b86b1bbSFabien Thomas pmcstat_string_unintern(pmcr->pr_pmcname), 4420b86b1bbSFabien Thomas nsamples); 4430b86b1bbSFabien Thomas 4440b86b1bbSFabien Thomas for (cgn = sortbuffer, n = 0; n < nentries; n++, cgn++) { 4450b86b1bbSFabien Thomas pmcstat_previous_filename_printed = NULL; 4460b86b1bbSFabien Thomas pmcstat_cgnode_print(*cgn, 0, nsamples); 4470b86b1bbSFabien Thomas (void) fprintf(args.pa_graphfile, "\n"); 4480b86b1bbSFabien Thomas } 4490b86b1bbSFabien Thomas 4500b86b1bbSFabien Thomas free(sortbuffer); 4510b86b1bbSFabien Thomas } 4520b86b1bbSFabien Thomas 4530b86b1bbSFabien Thomas /* 4540b86b1bbSFabien Thomas * Print out callgraphs. 4550b86b1bbSFabien Thomas */ 4560b86b1bbSFabien Thomas 4570b86b1bbSFabien Thomas static void 4580b86b1bbSFabien Thomas pmcstat_callgraph_print(void) 4590b86b1bbSFabien Thomas { 4600b86b1bbSFabien Thomas struct pmcstat_pmcrecord *pmcr; 4610b86b1bbSFabien Thomas 4620b86b1bbSFabien Thomas LIST_FOREACH(pmcr, &pmcstat_pmcs, pr_next) 4630b86b1bbSFabien Thomas pmcstat_callgraph_print_for_pmcid(pmcr); 4640b86b1bbSFabien Thomas } 4650b86b1bbSFabien Thomas 4660b86b1bbSFabien Thomas static void 4670b86b1bbSFabien Thomas pmcstat_cgnode_topprint(struct pmcstat_cgnode *cg, 4680b86b1bbSFabien Thomas int depth, uint32_t nsamples) 4690b86b1bbSFabien Thomas { 4700b86b1bbSFabien Thomas int v_attrs, vs_len, ns_len, width, len, n, nchildren; 4710b86b1bbSFabien Thomas float v; 4720b86b1bbSFabien Thomas char ns[30], vs[10]; 4730b86b1bbSFabien Thomas struct pmcstat_symbol *sym; 4740b86b1bbSFabien Thomas struct pmcstat_cgnode **sortbuffer, **cgn, *pcg; 4750b86b1bbSFabien Thomas 4760b86b1bbSFabien Thomas (void) depth; 4770b86b1bbSFabien Thomas 4780b86b1bbSFabien Thomas /* Format value. */ 4790b86b1bbSFabien Thomas v = PMCPL_CG_COUNTP(cg); 4800b86b1bbSFabien Thomas snprintf(vs, sizeof(vs), "%.1f", v); 4810b86b1bbSFabien Thomas v_attrs = PMCSTAT_ATTRPERCENT(v); 4820b86b1bbSFabien Thomas 4830b86b1bbSFabien Thomas /* Format name. */ 4840b86b1bbSFabien Thomas sym = pmcstat_symbol_search(cg->pcg_image, cg->pcg_func); 4850b86b1bbSFabien Thomas if (sym != NULL) { 4860b86b1bbSFabien Thomas snprintf(ns, sizeof(ns), "%s", 4870b86b1bbSFabien Thomas pmcstat_string_unintern(sym->ps_name)); 4880b86b1bbSFabien Thomas } else 4890b86b1bbSFabien Thomas snprintf(ns, sizeof(ns), "%p", 4900b86b1bbSFabien Thomas (void *)cg->pcg_func); 4910b86b1bbSFabien Thomas 4920b86b1bbSFabien Thomas PMCSTAT_ATTRON(v_attrs); 4930b86b1bbSFabien Thomas PMCSTAT_PRINTW("%5.5s", vs); 4940b86b1bbSFabien Thomas PMCSTAT_ATTROFF(v_attrs); 4950b86b1bbSFabien Thomas PMCSTAT_PRINTW(" %-10.10s %-20.20s", 4960b86b1bbSFabien Thomas pmcstat_string_unintern(cg->pcg_image->pi_name), 4970b86b1bbSFabien Thomas ns); 4980b86b1bbSFabien Thomas 4990b86b1bbSFabien Thomas nchildren = cg->pcg_nchildren; 5000b86b1bbSFabien Thomas if (nchildren == 0) { 5010b86b1bbSFabien Thomas PMCSTAT_PRINTW("\n"); 5020b86b1bbSFabien Thomas return; 5030b86b1bbSFabien Thomas } 5040b86b1bbSFabien Thomas 5050b86b1bbSFabien Thomas width = pmcstat_displaywidth - 40; 5060b86b1bbSFabien Thomas 5070b86b1bbSFabien Thomas if ((sortbuffer = (struct pmcstat_cgnode **) 5080b86b1bbSFabien Thomas malloc(sizeof(struct pmcstat_cgnode *) * 5090b86b1bbSFabien Thomas nchildren)) == NULL) 5100b86b1bbSFabien Thomas err(EX_OSERR, "ERROR: Cannot print callgraph"); 5110b86b1bbSFabien Thomas cgn = sortbuffer; 5120b86b1bbSFabien Thomas 5130b86b1bbSFabien Thomas LIST_FOREACH(pcg, &cg->pcg_children, pcg_sibling) 5140b86b1bbSFabien Thomas *cgn++ = pcg; 5150b86b1bbSFabien Thomas 5160b86b1bbSFabien Thomas assert(cgn - sortbuffer == (int)nchildren); 5170b86b1bbSFabien Thomas 5180b86b1bbSFabien Thomas qsort(sortbuffer, nchildren, sizeof(struct pmcstat_cgnode *), 5190b86b1bbSFabien Thomas pmcstat_cgnode_compare); 5200b86b1bbSFabien Thomas 5210b86b1bbSFabien Thomas /* Count how many callers. */ 5220b86b1bbSFabien Thomas for (cgn = sortbuffer, n = 0; n < nchildren; n++, cgn++) { 5230b86b1bbSFabien Thomas pcg = *cgn; 5240b86b1bbSFabien Thomas 5250b86b1bbSFabien Thomas v = PMCPL_CG_COUNTP(pcg); 5260b86b1bbSFabien Thomas if (v < pmcstat_threshold) 5270b86b1bbSFabien Thomas break; 5280b86b1bbSFabien Thomas } 5290b86b1bbSFabien Thomas nchildren = n; 5300b86b1bbSFabien Thomas 5310b86b1bbSFabien Thomas for (cgn = sortbuffer, n = 0; n < nchildren; n++, cgn++) { 5320b86b1bbSFabien Thomas pcg = *cgn; 5330b86b1bbSFabien Thomas 5340b86b1bbSFabien Thomas /* Format value. */ 5350b86b1bbSFabien Thomas if (nchildren > 1) { 5360b86b1bbSFabien Thomas v = PMCPL_CG_COUNTP(pcg); 5370b86b1bbSFabien Thomas vs_len = snprintf(vs, sizeof(vs), ":%.1f", v); 5380b86b1bbSFabien Thomas v_attrs = PMCSTAT_ATTRPERCENT(v); 5390b86b1bbSFabien Thomas } else 5400b86b1bbSFabien Thomas vs_len = 0; 5410b86b1bbSFabien Thomas 5420b86b1bbSFabien Thomas /* Format name. */ 5430b86b1bbSFabien Thomas sym = pmcstat_symbol_search(pcg->pcg_image, pcg->pcg_func); 5440b86b1bbSFabien Thomas if (sym != NULL) { 5450b86b1bbSFabien Thomas ns_len = snprintf(ns, sizeof(ns), "%s", 5460b86b1bbSFabien Thomas pmcstat_string_unintern(sym->ps_name)); 5470b86b1bbSFabien Thomas } else 5480b86b1bbSFabien Thomas ns_len = snprintf(ns, sizeof(ns), "%p", 5490b86b1bbSFabien Thomas (void *)pcg->pcg_func); 5500b86b1bbSFabien Thomas 5510b86b1bbSFabien Thomas len = ns_len + vs_len + 1; 5520b86b1bbSFabien Thomas if (width - len < 0) { 5530b86b1bbSFabien Thomas PMCSTAT_PRINTW("..."); 5540b86b1bbSFabien Thomas break; 5550b86b1bbSFabien Thomas } 5560b86b1bbSFabien Thomas width -= len; 5570b86b1bbSFabien Thomas 5580b86b1bbSFabien Thomas PMCSTAT_PRINTW(" %s", ns); 5590b86b1bbSFabien Thomas if (nchildren > 1) { 5600b86b1bbSFabien Thomas PMCSTAT_ATTRON(v_attrs); 5610b86b1bbSFabien Thomas PMCSTAT_PRINTW("%s", vs); 5620b86b1bbSFabien Thomas PMCSTAT_ATTROFF(v_attrs); 5630b86b1bbSFabien Thomas } 5640b86b1bbSFabien Thomas } 5650b86b1bbSFabien Thomas PMCSTAT_PRINTW("\n"); 5660b86b1bbSFabien Thomas free(sortbuffer); 5670b86b1bbSFabien Thomas } 5680b86b1bbSFabien Thomas 5690b86b1bbSFabien Thomas /* 5700b86b1bbSFabien Thomas * Top mode display. 5710b86b1bbSFabien Thomas */ 5720b86b1bbSFabien Thomas 5730b86b1bbSFabien Thomas void 5740b86b1bbSFabien Thomas pmcpl_cg_topdisplay(void) 5750b86b1bbSFabien Thomas { 5760b86b1bbSFabien Thomas int n, nentries; 5770b86b1bbSFabien Thomas uint32_t nsamples; 5780b86b1bbSFabien Thomas struct pmcstat_cgnode **sortbuffer, **cgn; 5790b86b1bbSFabien Thomas struct pmcstat_cgnode_hash *pch; 5800b86b1bbSFabien Thomas struct pmcstat_pmcrecord *pmcr; 5810b86b1bbSFabien Thomas 5820b86b1bbSFabien Thomas pmcr = pmcstat_pmcindex_to_pmcr(pmcstat_pmcinfilter); 5830b86b1bbSFabien Thomas 5840b86b1bbSFabien Thomas /* 5850b86b1bbSFabien Thomas * We pull out all callgraph nodes in the top-level hash table 5860b86b1bbSFabien Thomas * with a matching PMC index. We then sort these based on the 5870b86b1bbSFabien Thomas * frequency of occurrence. Each callgraph node is then 5880b86b1bbSFabien Thomas * printed. 5890b86b1bbSFabien Thomas */ 5900b86b1bbSFabien Thomas 5910b86b1bbSFabien Thomas nsamples = 0; 5920b86b1bbSFabien Thomas 5930b86b1bbSFabien Thomas if ((sortbuffer = (struct pmcstat_cgnode **) 5940b86b1bbSFabien Thomas malloc(sizeof(struct pmcstat_cgnode *) * 5950b86b1bbSFabien Thomas pmcstat_cgnode_hash_count)) == NULL) 5960b86b1bbSFabien Thomas err(EX_OSERR, "ERROR: Cannot sort callgraph"); 5970b86b1bbSFabien Thomas cgn = sortbuffer; 5980b86b1bbSFabien Thomas 5990b86b1bbSFabien Thomas for (n = 0; n < PMCSTAT_NHASH; n++) 6000b86b1bbSFabien Thomas LIST_FOREACH(pch, &pmcstat_cgnode_hash[n], pch_next) 6010b86b1bbSFabien Thomas if (pmcr == NULL || pch->pch_pmcid == pmcr->pr_pmcid) { 6020b86b1bbSFabien Thomas nsamples += pch->pch_cgnode->pcg_count; 6030b86b1bbSFabien Thomas *cgn++ = pch->pch_cgnode; 6040b86b1bbSFabien Thomas } 6050b86b1bbSFabien Thomas 6060b86b1bbSFabien Thomas nentries = cgn - sortbuffer; 6070b86b1bbSFabien Thomas assert(nentries <= pmcstat_cgnode_hash_count); 6080b86b1bbSFabien Thomas 6090b86b1bbSFabien Thomas if (nentries == 0) { 6100b86b1bbSFabien Thomas free(sortbuffer); 6110b86b1bbSFabien Thomas return; 6120b86b1bbSFabien Thomas } 6130b86b1bbSFabien Thomas 6140b86b1bbSFabien Thomas qsort(sortbuffer, nentries, sizeof(struct pmcstat_cgnode *), 6150b86b1bbSFabien Thomas pmcstat_cgnode_compare); 6160b86b1bbSFabien Thomas 6170b86b1bbSFabien Thomas PMCSTAT_PRINTW("%5.5s %-10.10s %-20.20s %s\n", 6180b86b1bbSFabien Thomas "%SAMP", "IMAGE", "FUNCTION", "CALLERS"); 6190b86b1bbSFabien Thomas 6200b86b1bbSFabien Thomas nentries = min(pmcstat_displayheight - 2, nentries); 6210b86b1bbSFabien Thomas 6220b86b1bbSFabien Thomas for (cgn = sortbuffer, n = 0; n < nentries; n++, cgn++) { 6230b86b1bbSFabien Thomas if (PMCPL_CG_COUNTP(*cgn) < pmcstat_threshold) 6240b86b1bbSFabien Thomas break; 6250b86b1bbSFabien Thomas pmcstat_cgnode_topprint(*cgn, 0, nsamples); 6260b86b1bbSFabien Thomas } 6270b86b1bbSFabien Thomas 6280b86b1bbSFabien Thomas free(sortbuffer); 6290b86b1bbSFabien Thomas } 6300b86b1bbSFabien Thomas 6310b86b1bbSFabien Thomas /* 6320b86b1bbSFabien Thomas * Handle top mode keypress. 6330b86b1bbSFabien Thomas */ 6340b86b1bbSFabien Thomas 6350b86b1bbSFabien Thomas int 6360b86b1bbSFabien Thomas pmcpl_cg_topkeypress(int c, WINDOW *w) 6370b86b1bbSFabien Thomas { 6380b86b1bbSFabien Thomas 6390b86b1bbSFabien Thomas (void) c; (void) w; 6400b86b1bbSFabien Thomas 6410b86b1bbSFabien Thomas return 0; 6420b86b1bbSFabien Thomas } 6430b86b1bbSFabien Thomas 6440b86b1bbSFabien Thomas int 6450b86b1bbSFabien Thomas pmcpl_cg_init(void) 6460b86b1bbSFabien Thomas { 6470b86b1bbSFabien Thomas int i; 6480b86b1bbSFabien Thomas 6490b86b1bbSFabien Thomas pmcstat_cgnode_hash_count = 0; 6500b86b1bbSFabien Thomas pmcstat_previous_filename_printed = NULL; 6510b86b1bbSFabien Thomas 6520b86b1bbSFabien Thomas for (i = 0; i < PMCSTAT_NHASH; i++) { 6530b86b1bbSFabien Thomas LIST_INIT(&pmcstat_cgnode_hash[i]); 6540b86b1bbSFabien Thomas } 6550b86b1bbSFabien Thomas 6560b86b1bbSFabien Thomas return (0); 6570b86b1bbSFabien Thomas } 6580b86b1bbSFabien Thomas 6590b86b1bbSFabien Thomas void 6600b86b1bbSFabien Thomas pmcpl_cg_shutdown(FILE *mf) 6610b86b1bbSFabien Thomas { 6620b86b1bbSFabien Thomas int i; 6630b86b1bbSFabien Thomas struct pmcstat_cgnode_hash *pch, *pchtmp; 6640b86b1bbSFabien Thomas 6650b86b1bbSFabien Thomas (void) mf; 6660b86b1bbSFabien Thomas 6670b86b1bbSFabien Thomas if (args.pa_flags & FLAG_DO_CALLGRAPHS) 6680b86b1bbSFabien Thomas pmcstat_callgraph_print(); 6690b86b1bbSFabien Thomas 6700b86b1bbSFabien Thomas /* 6710b86b1bbSFabien Thomas * Free memory. 6720b86b1bbSFabien Thomas */ 6730b86b1bbSFabien Thomas for (i = 0; i < PMCSTAT_NHASH; i++) { 6740b86b1bbSFabien Thomas LIST_FOREACH_SAFE(pch, &pmcstat_cgnode_hash[i], pch_next, 6750b86b1bbSFabien Thomas pchtmp) { 6760b86b1bbSFabien Thomas pmcstat_cgnode_free(pch->pch_cgnode); 6770b86b1bbSFabien Thomas LIST_REMOVE(pch, pch_next); 6780b86b1bbSFabien Thomas free(pch); 6790b86b1bbSFabien Thomas } 6800b86b1bbSFabien Thomas } 6810b86b1bbSFabien Thomas } 6820b86b1bbSFabien Thomas 683