xref: /freebsd/usr.sbin/pmcstat/pmcpl_gprof.c (revision 4d65a7c6)
10b86b1bbSFabien Thomas /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
40b86b1bbSFabien Thomas  * Copyright (c) 2005-2007, Joseph Koshy
50b86b1bbSFabien Thomas  * Copyright (c) 2007 The FreeBSD Foundation
60b86b1bbSFabien Thomas  * Copyright (c) 2009, Fabien Thomas
70b86b1bbSFabien Thomas  * All rights reserved.
80b86b1bbSFabien Thomas  *
90b86b1bbSFabien Thomas  * Portions of this software were developed by A. Joseph Koshy under
100b86b1bbSFabien Thomas  * sponsorship from the FreeBSD Foundation and Google, Inc.
110b86b1bbSFabien Thomas  *
120b86b1bbSFabien Thomas  * Redistribution and use in source and binary forms, with or without
130b86b1bbSFabien Thomas  * modification, are permitted provided that the following conditions
140b86b1bbSFabien Thomas  * are met:
150b86b1bbSFabien Thomas  * 1. Redistributions of source code must retain the above copyright
160b86b1bbSFabien Thomas  *    notice, this list of conditions and the following disclaimer.
170b86b1bbSFabien Thomas  * 2. Redistributions in binary form must reproduce the above copyright
180b86b1bbSFabien Thomas  *    notice, this list of conditions and the following disclaimer in the
190b86b1bbSFabien Thomas  *    documentation and/or other materials provided with the distribution.
200b86b1bbSFabien Thomas  *
210b86b1bbSFabien Thomas  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
220b86b1bbSFabien Thomas  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
230b86b1bbSFabien Thomas  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
240b86b1bbSFabien Thomas  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
250b86b1bbSFabien Thomas  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
260b86b1bbSFabien Thomas  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
270b86b1bbSFabien Thomas  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
280b86b1bbSFabien Thomas  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
290b86b1bbSFabien Thomas  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
300b86b1bbSFabien Thomas  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
310b86b1bbSFabien Thomas  * SUCH DAMAGE.
320b86b1bbSFabien Thomas  */
330b86b1bbSFabien Thomas 
340b86b1bbSFabien Thomas /*
350b86b1bbSFabien Thomas  * Transform a hwpmc(4) log into human readable form, and into
360b86b1bbSFabien Thomas  * gprof(1) compatible profiles.
370b86b1bbSFabien Thomas  */
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 "pmcpl_callgraph.h"
740b86b1bbSFabien Thomas #include "pmcpl_gprof.h"
750b86b1bbSFabien Thomas 
76465dadb5SJonathan T. Looney typedef	uint64_t	WIDEHISTCOUNTER;
77465dadb5SJonathan T. Looney 
78d27927f7SRuslan Bukin #define	min(A,B)		((A) < (B) ? (A) : (B))
79d27927f7SRuslan Bukin #define	max(A,B)		((A) > (B) ? (A) : (B))
80d27927f7SRuslan Bukin 
81465dadb5SJonathan T. Looney #define	WIDEHISTCOUNTER_MAX		UINT64_MAX
82465dadb5SJonathan T. Looney #define	HISTCOUNTER_MAX			USHRT_MAX
83465dadb5SJonathan T. Looney #define	WIDEHISTCOUNTER_GMONTYPE	((int) 64)
84465dadb5SJonathan T. Looney #define	HISTCOUNTER_GMONTYPE		((int) 0)
85465dadb5SJonathan T. Looney static int hc_sz=0;
86465dadb5SJonathan T. Looney 
870b86b1bbSFabien Thomas /*
880b86b1bbSFabien Thomas  * struct pmcstat_gmonfile tracks a given 'gmon.out' file.  These
890b86b1bbSFabien Thomas  * files are mmap()'ed in as needed.
900b86b1bbSFabien Thomas  */
910b86b1bbSFabien Thomas 
920b86b1bbSFabien Thomas struct pmcstat_gmonfile {
930b86b1bbSFabien Thomas 	LIST_ENTRY(pmcstat_gmonfile)	pgf_next; /* list of entries */
940b86b1bbSFabien Thomas 	int		pgf_overflow;	/* whether a count overflowed */
950b86b1bbSFabien Thomas 	pmc_id_t	pgf_pmcid;	/* id of the associated pmc */
960b86b1bbSFabien Thomas 	size_t		pgf_nbuckets;	/* #buckets in this gmon.out */
970b86b1bbSFabien Thomas 	unsigned int	pgf_nsamples;	/* #samples in this gmon.out */
980b86b1bbSFabien Thomas 	pmcstat_interned_string pgf_name;	/* pathname of gmon.out file */
990b86b1bbSFabien Thomas 	size_t		pgf_ndatabytes;	/* number of bytes mapped */
1000b86b1bbSFabien Thomas 	void		*pgf_gmondata;	/* pointer to mmap'ed data */
1010b86b1bbSFabien Thomas 	FILE		*pgf_file;	/* used when writing gmon arcs */
1020b86b1bbSFabien Thomas };
1030b86b1bbSFabien Thomas 
1040b86b1bbSFabien Thomas /*
1050b86b1bbSFabien Thomas  * Prototypes
1060b86b1bbSFabien Thomas  */
1070b86b1bbSFabien Thomas 
1080b86b1bbSFabien Thomas static void	pmcstat_gmon_create_file(struct pmcstat_gmonfile *_pgf,
1090b86b1bbSFabien Thomas     struct pmcstat_image *_image);
1100b86b1bbSFabien Thomas static pmcstat_interned_string pmcstat_gmon_create_name(const char *_sd,
1110b86b1bbSFabien Thomas     struct pmcstat_image *_img, pmc_id_t _pmcid);
1120b86b1bbSFabien Thomas static void	pmcstat_gmon_map_file(struct pmcstat_gmonfile *_pgf);
1130b86b1bbSFabien Thomas static void	pmcstat_gmon_unmap_file(struct pmcstat_gmonfile *_pgf);
1140b86b1bbSFabien Thomas 
1150b86b1bbSFabien Thomas static struct pmcstat_gmonfile *pmcstat_image_find_gmonfile(struct
1160b86b1bbSFabien Thomas     pmcstat_image *_i, pmc_id_t _id);
1170b86b1bbSFabien Thomas 
1180b86b1bbSFabien Thomas /*
1190b86b1bbSFabien Thomas  * Create a gmon.out file and size it.
1200b86b1bbSFabien Thomas  */
1210b86b1bbSFabien Thomas 
1220b86b1bbSFabien Thomas static void
pmcstat_gmon_create_file(struct pmcstat_gmonfile * pgf,struct pmcstat_image * image)1230b86b1bbSFabien Thomas pmcstat_gmon_create_file(struct pmcstat_gmonfile *pgf,
1240b86b1bbSFabien Thomas     struct pmcstat_image *image)
1250b86b1bbSFabien Thomas {
1260b86b1bbSFabien Thomas 	int fd;
1270b86b1bbSFabien Thomas 	size_t count;
1280b86b1bbSFabien Thomas 	struct gmonhdr gm;
1290b86b1bbSFabien Thomas 	const char *pathname;
1300b86b1bbSFabien Thomas 	char buffer[DEFAULT_BUFFER_SIZE];
1310b86b1bbSFabien Thomas 
1320b86b1bbSFabien Thomas 	pathname = pmcstat_string_unintern(pgf->pgf_name);
1330b86b1bbSFabien Thomas 	if ((fd = open(pathname, O_RDWR|O_NOFOLLOW|O_CREAT,
1340b86b1bbSFabien Thomas 		 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
1350b86b1bbSFabien Thomas 		err(EX_OSERR, "ERROR: Cannot open \"%s\"", pathname);
1360b86b1bbSFabien Thomas 
1370b86b1bbSFabien Thomas 	gm.lpc = image->pi_start;
1380b86b1bbSFabien Thomas 	gm.hpc = image->pi_end;
139465dadb5SJonathan T. Looney 	gm.ncnt = (pgf->pgf_nbuckets * hc_sz) + sizeof(struct gmonhdr);
1400b86b1bbSFabien Thomas 	gm.version = GMONVERSION;
1410b86b1bbSFabien Thomas 	gm.profrate = 0;		/* use ticks */
142465dadb5SJonathan T. Looney 	if (args.pa_flags & FLAG_DO_WIDE_GPROF_HC)
143465dadb5SJonathan T. Looney 		gm.histcounter_type = WIDEHISTCOUNTER_GMONTYPE;
144465dadb5SJonathan T. Looney 	else
145465dadb5SJonathan T. Looney 		gm.histcounter_type = HISTCOUNTER_GMONTYPE;
1460b86b1bbSFabien Thomas 	gm.spare[0] = gm.spare[1] = 0;
1470b86b1bbSFabien Thomas 
1480b86b1bbSFabien Thomas 	/* Write out the gmon header */
1490b86b1bbSFabien Thomas 	if (write(fd, &gm, sizeof(gm)) < 0)
1500b86b1bbSFabien Thomas 		goto error;
1510b86b1bbSFabien Thomas 
1520b86b1bbSFabien Thomas 	/* Zero fill the samples[] array */
1530b86b1bbSFabien Thomas 	(void) memset(buffer, 0, sizeof(buffer));
1540b86b1bbSFabien Thomas 
1550b86b1bbSFabien Thomas 	count = pgf->pgf_ndatabytes - sizeof(struct gmonhdr);
1560b86b1bbSFabien Thomas 	while (count > sizeof(buffer)) {
1570b86b1bbSFabien Thomas 		if (write(fd, &buffer, sizeof(buffer)) < 0)
1580b86b1bbSFabien Thomas 			goto error;
1590b86b1bbSFabien Thomas 		count -= sizeof(buffer);
1600b86b1bbSFabien Thomas 	}
1610b86b1bbSFabien Thomas 
1620b86b1bbSFabien Thomas 	if (write(fd, &buffer, count) < 0)
1630b86b1bbSFabien Thomas 		goto error;
1640b86b1bbSFabien Thomas 
1650b86b1bbSFabien Thomas 	(void) close(fd);
1660b86b1bbSFabien Thomas 
1670b86b1bbSFabien Thomas 	return;
1680b86b1bbSFabien Thomas 
1690b86b1bbSFabien Thomas  error:
1700b86b1bbSFabien Thomas 	err(EX_OSERR, "ERROR: Cannot write \"%s\"", pathname);
1710b86b1bbSFabien Thomas }
1720b86b1bbSFabien Thomas 
1730b86b1bbSFabien Thomas /*
1740b86b1bbSFabien Thomas  * Determine the full pathname of a gmon.out file for a given
1750b86b1bbSFabien Thomas  * (image,pmcid) combination.  Return the interned string.
1760b86b1bbSFabien Thomas  */
1770b86b1bbSFabien Thomas 
1780b86b1bbSFabien Thomas pmcstat_interned_string
pmcstat_gmon_create_name(const char * samplesdir,struct pmcstat_image * image,pmc_id_t pmcid)1790b86b1bbSFabien Thomas pmcstat_gmon_create_name(const char *samplesdir, struct pmcstat_image *image,
1800b86b1bbSFabien Thomas     pmc_id_t pmcid)
1810b86b1bbSFabien Thomas {
1820b86b1bbSFabien Thomas 	const char *pmcname;
1830b86b1bbSFabien Thomas 	char fullpath[PATH_MAX];
1840b86b1bbSFabien Thomas 
1850b86b1bbSFabien Thomas 	pmcname = pmcstat_pmcid_to_name(pmcid);
186410c7662SFabien Thomas 	if (!pmcname)
187410c7662SFabien Thomas 		err(EX_SOFTWARE, "ERROR: cannot find pmcid");
1880b86b1bbSFabien Thomas 
1890b86b1bbSFabien Thomas 	(void) snprintf(fullpath, sizeof(fullpath),
1900b86b1bbSFabien Thomas 	    "%s/%s/%s", samplesdir, pmcname,
1910b86b1bbSFabien Thomas 	    pmcstat_string_unintern(image->pi_samplename));
1920b86b1bbSFabien Thomas 
1930b86b1bbSFabien Thomas 	return (pmcstat_string_intern(fullpath));
1940b86b1bbSFabien Thomas }
1950b86b1bbSFabien Thomas 
1960b86b1bbSFabien Thomas 
1970b86b1bbSFabien Thomas /*
1980b86b1bbSFabien Thomas  * Mmap in a gmon.out file for processing.
1990b86b1bbSFabien Thomas  */
2000b86b1bbSFabien Thomas 
2010b86b1bbSFabien Thomas static void
pmcstat_gmon_map_file(struct pmcstat_gmonfile * pgf)2020b86b1bbSFabien Thomas pmcstat_gmon_map_file(struct pmcstat_gmonfile *pgf)
2030b86b1bbSFabien Thomas {
2040b86b1bbSFabien Thomas 	int fd;
2050b86b1bbSFabien Thomas 	const char *pathname;
2060b86b1bbSFabien Thomas 
2070b86b1bbSFabien Thomas 	pathname = pmcstat_string_unintern(pgf->pgf_name);
2080b86b1bbSFabien Thomas 
2090b86b1bbSFabien Thomas 	/* the gmon.out file must already exist */
2100b86b1bbSFabien Thomas 	if ((fd = open(pathname, O_RDWR | O_NOFOLLOW, 0)) < 0)
2110b86b1bbSFabien Thomas 		err(EX_OSERR, "ERROR: cannot open \"%s\"", pathname);
2120b86b1bbSFabien Thomas 
2130b86b1bbSFabien Thomas 	pgf->pgf_gmondata = mmap(NULL, pgf->pgf_ndatabytes,
2140b86b1bbSFabien Thomas 	    PROT_READ|PROT_WRITE, MAP_NOSYNC|MAP_SHARED, fd, 0);
2150b86b1bbSFabien Thomas 
2160b86b1bbSFabien Thomas 	if (pgf->pgf_gmondata == MAP_FAILED)
2170b86b1bbSFabien Thomas 		err(EX_OSERR, "ERROR: cannot map \"%s\"", pathname);
2180b86b1bbSFabien Thomas 
2190b86b1bbSFabien Thomas 	(void) close(fd);
2200b86b1bbSFabien Thomas }
2210b86b1bbSFabien Thomas 
2220b86b1bbSFabien Thomas /*
2230b86b1bbSFabien Thomas  * Unmap a gmon.out file after sync'ing its data to disk.
2240b86b1bbSFabien Thomas  */
2250b86b1bbSFabien Thomas 
2260b86b1bbSFabien Thomas static void
pmcstat_gmon_unmap_file(struct pmcstat_gmonfile * pgf)2270b86b1bbSFabien Thomas pmcstat_gmon_unmap_file(struct pmcstat_gmonfile *pgf)
2280b86b1bbSFabien Thomas {
2290b86b1bbSFabien Thomas 	(void) msync(pgf->pgf_gmondata, pgf->pgf_ndatabytes,
2300b86b1bbSFabien Thomas 	    MS_SYNC);
2310b86b1bbSFabien Thomas 	(void) munmap(pgf->pgf_gmondata, pgf->pgf_ndatabytes);
2320b86b1bbSFabien Thomas 	pgf->pgf_gmondata = NULL;
2330b86b1bbSFabien Thomas }
2340b86b1bbSFabien Thomas 
2350b86b1bbSFabien Thomas static void
pmcstat_gmon_append_arc(struct pmcstat_image * image,pmc_id_t pmcid,uintptr_t rawfrom,uintptr_t rawto,uint32_t count)2360b86b1bbSFabien Thomas pmcstat_gmon_append_arc(struct pmcstat_image *image, pmc_id_t pmcid,
2370b86b1bbSFabien Thomas     uintptr_t rawfrom, uintptr_t rawto, uint32_t count)
2380b86b1bbSFabien Thomas {
2390b86b1bbSFabien Thomas 	struct rawarc arc;	/* from <sys/gmon.h> */
2400b86b1bbSFabien Thomas 	const char *pathname;
2410b86b1bbSFabien Thomas 	struct pmcstat_gmonfile *pgf;
2420b86b1bbSFabien Thomas 
2430b86b1bbSFabien Thomas 	if ((pgf = pmcstat_image_find_gmonfile(image, pmcid)) == NULL)
2440b86b1bbSFabien Thomas 		return;
2450b86b1bbSFabien Thomas 
2460b86b1bbSFabien Thomas 	if (pgf->pgf_file == NULL) {
2470b86b1bbSFabien Thomas 		pathname = pmcstat_string_unintern(pgf->pgf_name);
2480b86b1bbSFabien Thomas 		if ((pgf->pgf_file = fopen(pathname, "a")) == NULL)
2490b86b1bbSFabien Thomas 			return;
2500b86b1bbSFabien Thomas 	}
2510b86b1bbSFabien Thomas 
2520b86b1bbSFabien Thomas 	arc.raw_frompc = rawfrom + image->pi_vaddr;
2530b86b1bbSFabien Thomas 	arc.raw_selfpc = rawto + image->pi_vaddr;
2540b86b1bbSFabien Thomas 	arc.raw_count = count;
2550b86b1bbSFabien Thomas 
2560b86b1bbSFabien Thomas 	(void) fwrite(&arc, sizeof(arc), 1, pgf->pgf_file);
2570b86b1bbSFabien Thomas 
2580b86b1bbSFabien Thomas }
2590b86b1bbSFabien Thomas 
2600b86b1bbSFabien Thomas static struct pmcstat_gmonfile *
pmcstat_image_find_gmonfile(struct pmcstat_image * image,pmc_id_t pmcid)2610b86b1bbSFabien Thomas pmcstat_image_find_gmonfile(struct pmcstat_image *image, pmc_id_t pmcid)
2620b86b1bbSFabien Thomas {
2630b86b1bbSFabien Thomas 	struct pmcstat_gmonfile *pgf;
2640b86b1bbSFabien Thomas 	LIST_FOREACH(pgf, &image->pi_gmlist, pgf_next)
2650b86b1bbSFabien Thomas 	    if (pgf->pgf_pmcid == pmcid)
2660b86b1bbSFabien Thomas 		    return (pgf);
2670b86b1bbSFabien Thomas 	return (NULL);
2680b86b1bbSFabien Thomas }
2690b86b1bbSFabien Thomas 
2700b86b1bbSFabien Thomas static void
pmcstat_cgnode_do_gmon_arcs(struct pmcstat_cgnode * cg,pmc_id_t pmcid)2710b86b1bbSFabien Thomas pmcstat_cgnode_do_gmon_arcs(struct pmcstat_cgnode *cg, pmc_id_t pmcid)
2720b86b1bbSFabien Thomas {
2730b86b1bbSFabien Thomas 	struct pmcstat_cgnode *cgc;
2740b86b1bbSFabien Thomas 
2750b86b1bbSFabien Thomas 	/*
2760b86b1bbSFabien Thomas 	 * Look for child nodes that belong to the same image.
2770b86b1bbSFabien Thomas 	 */
2780b86b1bbSFabien Thomas 
2790b86b1bbSFabien Thomas 	LIST_FOREACH(cgc, &cg->pcg_children, pcg_sibling) {
2800b86b1bbSFabien Thomas 		if (cgc->pcg_image == cg->pcg_image)
2810b86b1bbSFabien Thomas 			pmcstat_gmon_append_arc(cg->pcg_image, pmcid,
2820b86b1bbSFabien Thomas 			    cgc->pcg_func, cg->pcg_func, cgc->pcg_count);
2830b86b1bbSFabien Thomas 		if (cgc->pcg_nchildren > 0)
2840b86b1bbSFabien Thomas 			pmcstat_cgnode_do_gmon_arcs(cgc, pmcid);
2850b86b1bbSFabien Thomas 	}
2860b86b1bbSFabien Thomas }
2870b86b1bbSFabien Thomas 
2880b86b1bbSFabien Thomas static void
pmcstat_callgraph_do_gmon_arcs_for_pmcid(pmc_id_t pmcid)2890b86b1bbSFabien Thomas pmcstat_callgraph_do_gmon_arcs_for_pmcid(pmc_id_t pmcid)
2900b86b1bbSFabien Thomas {
2910b86b1bbSFabien Thomas 	int n;
2920b86b1bbSFabien Thomas 	struct pmcstat_cgnode_hash *pch;
2930b86b1bbSFabien Thomas 
2940b86b1bbSFabien Thomas 	for (n = 0; n < PMCSTAT_NHASH; n++)
2950b86b1bbSFabien Thomas 		LIST_FOREACH(pch, &pmcstat_cgnode_hash[n], pch_next)
2960b86b1bbSFabien Thomas 			if (pch->pch_pmcid == pmcid &&
2970b86b1bbSFabien Thomas 			    pch->pch_cgnode->pcg_nchildren > 1)
2980b86b1bbSFabien Thomas 				pmcstat_cgnode_do_gmon_arcs(pch->pch_cgnode,
2990b86b1bbSFabien Thomas 				    pmcid);
3000b86b1bbSFabien Thomas }
3010b86b1bbSFabien Thomas 
3020b86b1bbSFabien Thomas 
3030b86b1bbSFabien Thomas static void
pmcstat_callgraph_do_gmon_arcs(void)3040b86b1bbSFabien Thomas pmcstat_callgraph_do_gmon_arcs(void)
3050b86b1bbSFabien Thomas {
3060b86b1bbSFabien Thomas 	struct pmcstat_pmcrecord *pmcr;
3070b86b1bbSFabien Thomas 
3080b86b1bbSFabien Thomas 	LIST_FOREACH(pmcr, &pmcstat_pmcs, pr_next)
3090b86b1bbSFabien Thomas 		pmcstat_callgraph_do_gmon_arcs_for_pmcid(pmcr->pr_pmcid);
3100b86b1bbSFabien Thomas }
3110b86b1bbSFabien Thomas 
3120b86b1bbSFabien Thomas void
pmcpl_gmon_initimage(struct pmcstat_image * pi)3130b86b1bbSFabien Thomas pmcpl_gmon_initimage(struct pmcstat_image *pi)
3140b86b1bbSFabien Thomas {
315d93c9128SEd Schouten 	const char *execpath;
3160b86b1bbSFabien Thomas 	int count, nlen;
317d93c9128SEd Schouten 	char *sn, *snbuf;
3180b86b1bbSFabien Thomas 	char name[NAME_MAX];
3190b86b1bbSFabien Thomas 
3200b86b1bbSFabien Thomas 	/*
3210b86b1bbSFabien Thomas 	 * Look for a suitable name for the sample files associated
3220b86b1bbSFabien Thomas 	 * with this image: if `basename(path)`+".gmon" is available,
3230b86b1bbSFabien Thomas 	 * we use that, otherwise we try iterating through
3240b86b1bbSFabien Thomas 	 * `basename(path)`+ "~" + NNN + ".gmon" till we get a free
3250b86b1bbSFabien Thomas 	 * entry.
3260b86b1bbSFabien Thomas 	 */
327d93c9128SEd Schouten 	execpath = pmcstat_string_unintern(pi->pi_execpath);
328d93c9128SEd Schouten 	if ((snbuf = strdup(execpath)) == NULL)
329d93c9128SEd Schouten 		err(EX_OSERR, "ERROR: Cannot copy \"%s\"", execpath);
330d93c9128SEd Schouten 	if ((sn = basename(snbuf)) == NULL)
331d93c9128SEd Schouten 		err(EX_OSERR, "ERROR: Cannot process \"%s\"", execpath);
3320b86b1bbSFabien Thomas 
3330b86b1bbSFabien Thomas 	nlen = strlen(sn);
3340b86b1bbSFabien Thomas 	nlen = min(nlen, (int) (sizeof(name) - sizeof(".gmon")));
3350b86b1bbSFabien Thomas 
3360b86b1bbSFabien Thomas 	snprintf(name, sizeof(name), "%.*s.gmon", nlen, sn);
3370b86b1bbSFabien Thomas 
3380b86b1bbSFabien Thomas 	/* try use the unabridged name first */
3390b86b1bbSFabien Thomas 	if (pmcstat_string_lookup(name) == NULL)
3400b86b1bbSFabien Thomas 		pi->pi_samplename = pmcstat_string_intern(name);
3410b86b1bbSFabien Thomas 	else {
3420b86b1bbSFabien Thomas 		/*
3430b86b1bbSFabien Thomas 		 * Otherwise use a prefix from the original name and
3440b86b1bbSFabien Thomas 		 * up to 3 digits.
3450b86b1bbSFabien Thomas 		 */
3460b86b1bbSFabien Thomas 		nlen = strlen(sn);
3470b86b1bbSFabien Thomas 		nlen = min(nlen, (int) (sizeof(name)-sizeof("~NNN.gmon")));
3480b86b1bbSFabien Thomas 		count = 0;
3490b86b1bbSFabien Thomas 		do {
3500b86b1bbSFabien Thomas 			if (++count > 999)
35137d6f8a9SDavid E. O'Brien 				errx(EX_CANTCREAT,
35237d6f8a9SDavid E. O'Brien 				    "ERROR: cannot create a gmon file for"
35337d6f8a9SDavid E. O'Brien 				    " \"%s\"", name);
3540b86b1bbSFabien Thomas 			snprintf(name, sizeof(name), "%.*s~%3.3d.gmon",
3550b86b1bbSFabien Thomas 			    nlen, sn, count);
3560b86b1bbSFabien Thomas 			if (pmcstat_string_lookup(name) == NULL) {
3570b86b1bbSFabien Thomas 				pi->pi_samplename =
3580b86b1bbSFabien Thomas 				    pmcstat_string_intern(name);
3590b86b1bbSFabien Thomas 				count = 0;
3600b86b1bbSFabien Thomas 			}
3610b86b1bbSFabien Thomas 		} while (count > 0);
3620b86b1bbSFabien Thomas 	}
363d93c9128SEd Schouten 	free(snbuf);
3640b86b1bbSFabien Thomas 
3650b86b1bbSFabien Thomas 	LIST_INIT(&pi->pi_gmlist);
3660b86b1bbSFabien Thomas }
3670b86b1bbSFabien Thomas 
3680b86b1bbSFabien Thomas void
pmcpl_gmon_shutdownimage(struct pmcstat_image * pi)3690b86b1bbSFabien Thomas pmcpl_gmon_shutdownimage(struct pmcstat_image *pi)
3700b86b1bbSFabien Thomas {
3710b86b1bbSFabien Thomas 	struct pmcstat_gmonfile *pgf, *pgftmp;
3720b86b1bbSFabien Thomas 
3730b86b1bbSFabien Thomas 	LIST_FOREACH_SAFE(pgf, &pi->pi_gmlist, pgf_next, pgftmp) {
3740b86b1bbSFabien Thomas 		if (pgf->pgf_file)
3750b86b1bbSFabien Thomas 			(void) fclose(pgf->pgf_file);
3760b86b1bbSFabien Thomas 		LIST_REMOVE(pgf, pgf_next);
3770b86b1bbSFabien Thomas 		free(pgf);
3780b86b1bbSFabien Thomas 	}
3790b86b1bbSFabien Thomas }
3800b86b1bbSFabien Thomas 
3810b86b1bbSFabien Thomas void
pmcpl_gmon_newpmc(pmcstat_interned_string ps,struct pmcstat_pmcrecord * pr)3820b86b1bbSFabien Thomas pmcpl_gmon_newpmc(pmcstat_interned_string ps, struct pmcstat_pmcrecord *pr)
3830b86b1bbSFabien Thomas {
3840b86b1bbSFabien Thomas 	struct stat st;
3850b86b1bbSFabien Thomas 	char fullpath[PATH_MAX];
3860b86b1bbSFabien Thomas 
3870b86b1bbSFabien Thomas 	(void) pr;
3880b86b1bbSFabien Thomas 
3890b86b1bbSFabien Thomas 	/*
3900b86b1bbSFabien Thomas 	 * Create the appropriate directory to hold gmon.out files.
3910b86b1bbSFabien Thomas 	 */
3920b86b1bbSFabien Thomas 
3930b86b1bbSFabien Thomas 	(void) snprintf(fullpath, sizeof(fullpath), "%s/%s", args.pa_samplesdir,
3940b86b1bbSFabien Thomas 	    pmcstat_string_unintern(ps));
3950b86b1bbSFabien Thomas 
3960b86b1bbSFabien Thomas 	/* If the path name exists, it should be a directory */
3970b86b1bbSFabien Thomas 	if (stat(fullpath, &st) == 0 && S_ISDIR(st.st_mode))
3980b86b1bbSFabien Thomas 		return;
3990b86b1bbSFabien Thomas 
4000b86b1bbSFabien Thomas 	if (mkdir(fullpath, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) < 0)
4010b86b1bbSFabien Thomas 		err(EX_OSERR, "ERROR: Cannot create directory \"%s\"",
4020b86b1bbSFabien Thomas 		    fullpath);
4030b86b1bbSFabien Thomas }
4040b86b1bbSFabien Thomas 
4050b86b1bbSFabien Thomas /*
4060b86b1bbSFabien Thomas  * Increment the bucket in the gmon.out file corresponding to 'pmcid'
4070b86b1bbSFabien Thomas  * and 'pc'.
4080b86b1bbSFabien Thomas  */
4090b86b1bbSFabien Thomas 
4100b86b1bbSFabien Thomas void
pmcpl_gmon_process(struct pmcstat_process * pp,struct pmcstat_pmcrecord * pmcr,uint32_t nsamples,uintfptr_t * cc,int usermode,uint32_t cpu)4110b86b1bbSFabien Thomas pmcpl_gmon_process(struct pmcstat_process *pp, struct pmcstat_pmcrecord *pmcr,
4120b86b1bbSFabien Thomas     uint32_t nsamples, uintfptr_t *cc, int usermode, uint32_t cpu)
4130b86b1bbSFabien Thomas {
4140b86b1bbSFabien Thomas 	struct pmcstat_pcmap *map;
4150b86b1bbSFabien Thomas 	struct pmcstat_image *image;
4160b86b1bbSFabien Thomas 	struct pmcstat_gmonfile *pgf;
4170b86b1bbSFabien Thomas 	uintfptr_t bucket;
4180b86b1bbSFabien Thomas 	HISTCOUNTER *hc;
419465dadb5SJonathan T. Looney 	WIDEHISTCOUNTER *whc;
4200b86b1bbSFabien Thomas 	pmc_id_t pmcid;
4210b86b1bbSFabien Thomas 
4220b86b1bbSFabien Thomas 	(void) nsamples; (void) usermode; (void) cpu;
4230b86b1bbSFabien Thomas 
4240b86b1bbSFabien Thomas 	map = pmcstat_process_find_map(usermode ? pp : pmcstat_kernproc, cc[0]);
4250b86b1bbSFabien Thomas 	if (map == NULL) {
4260b86b1bbSFabien Thomas 		/* Unknown offset. */
4270b86b1bbSFabien Thomas 		pmcstat_stats.ps_samples_unknown_offset++;
4280b86b1bbSFabien Thomas 		return;
4290b86b1bbSFabien Thomas 	}
4300b86b1bbSFabien Thomas 
4310b86b1bbSFabien Thomas 	assert(cc[0] >= map->ppm_lowpc && cc[0] < map->ppm_highpc);
4320b86b1bbSFabien Thomas 
4330b86b1bbSFabien Thomas 	image = map->ppm_image;
4340b86b1bbSFabien Thomas 	pmcid = pmcr->pr_pmcid;
4350b86b1bbSFabien Thomas 
4360b86b1bbSFabien Thomas 	/*
4370b86b1bbSFabien Thomas 	 * If this is the first time we are seeing a sample for
4380b86b1bbSFabien Thomas 	 * this executable image, try determine its parameters.
4390b86b1bbSFabien Thomas 	 */
4400b86b1bbSFabien Thomas 	if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
441d27927f7SRuslan Bukin 		pmcstat_image_determine_type(image, &args);
4420b86b1bbSFabien Thomas 
4430b86b1bbSFabien Thomas 	assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN);
4440b86b1bbSFabien Thomas 
4450b86b1bbSFabien Thomas 	/* Ignore samples in images that we know nothing about. */
4460b86b1bbSFabien Thomas 	if (image->pi_type == PMCSTAT_IMAGE_INDETERMINABLE) {
4470b86b1bbSFabien Thomas 		pmcstat_stats.ps_samples_indeterminable++;
4480b86b1bbSFabien Thomas 		return;
4490b86b1bbSFabien Thomas 	}
4500b86b1bbSFabien Thomas 
4510b86b1bbSFabien Thomas 	/*
4520b86b1bbSFabien Thomas 	 * Find the gmon file corresponding to 'pmcid', creating it if
4530b86b1bbSFabien Thomas 	 * needed.
4540b86b1bbSFabien Thomas 	 */
4550b86b1bbSFabien Thomas 	pgf = pmcstat_image_find_gmonfile(image, pmcid);
4560b86b1bbSFabien Thomas 	if (pgf == NULL) {
457465dadb5SJonathan T. Looney 		if (hc_sz == 0) {
458465dadb5SJonathan T. Looney 			/* Determine the correct histcounter size. */
459465dadb5SJonathan T. Looney 			if (args.pa_flags & FLAG_DO_WIDE_GPROF_HC)
460465dadb5SJonathan T. Looney 				hc_sz = sizeof(WIDEHISTCOUNTER);
461465dadb5SJonathan T. Looney 			else
462465dadb5SJonathan T. Looney 				hc_sz = sizeof(HISTCOUNTER);
463465dadb5SJonathan T. Looney 		}
464465dadb5SJonathan T. Looney 
4650b86b1bbSFabien Thomas 		if ((pgf = calloc(1, sizeof(*pgf))) == NULL)
4660b86b1bbSFabien Thomas 			err(EX_OSERR, "ERROR:");
4670b86b1bbSFabien Thomas 
4680b86b1bbSFabien Thomas 		pgf->pgf_gmondata = NULL;	/* mark as unmapped */
4690b86b1bbSFabien Thomas 		pgf->pgf_name = pmcstat_gmon_create_name(args.pa_samplesdir,
4700b86b1bbSFabien Thomas 		    image, pmcid);
4710b86b1bbSFabien Thomas 		pgf->pgf_pmcid = pmcid;
4720b86b1bbSFabien Thomas 		assert(image->pi_end > image->pi_start);
473e28f262aSKonstantin Belousov 		pgf->pgf_nbuckets = howmany(image->pi_end - image->pi_start,
474e28f262aSKonstantin Belousov 		    FUNCTION_ALIGNMENT);	/* see <machine/profile.h> */
4750b86b1bbSFabien Thomas 		pgf->pgf_ndatabytes = sizeof(struct gmonhdr) +
476465dadb5SJonathan T. Looney 		    pgf->pgf_nbuckets * hc_sz;
4770b86b1bbSFabien Thomas 		pgf->pgf_nsamples = 0;
4780b86b1bbSFabien Thomas 		pgf->pgf_file = NULL;
4790b86b1bbSFabien Thomas 
4800b86b1bbSFabien Thomas 		pmcstat_gmon_create_file(pgf, image);
4810b86b1bbSFabien Thomas 
4820b86b1bbSFabien Thomas 		LIST_INSERT_HEAD(&image->pi_gmlist, pgf, pgf_next);
4830b86b1bbSFabien Thomas 	}
4840b86b1bbSFabien Thomas 
4850b86b1bbSFabien Thomas 	/*
4860b86b1bbSFabien Thomas 	 * Map the gmon file in if needed.  It may have been mapped
4870b86b1bbSFabien Thomas 	 * out under memory pressure.
4880b86b1bbSFabien Thomas 	 */
4890b86b1bbSFabien Thomas 	if (pgf->pgf_gmondata == NULL)
4900b86b1bbSFabien Thomas 		pmcstat_gmon_map_file(pgf);
4910b86b1bbSFabien Thomas 
4920b86b1bbSFabien Thomas 	assert(pgf->pgf_gmondata != NULL);
4930b86b1bbSFabien Thomas 
4940b86b1bbSFabien Thomas 	/*
4950b86b1bbSFabien Thomas 	 *
4960b86b1bbSFabien Thomas 	 */
4970b86b1bbSFabien Thomas 
4980b86b1bbSFabien Thomas 	bucket = (cc[0] - map->ppm_lowpc) / FUNCTION_ALIGNMENT;
4990b86b1bbSFabien Thomas 
5000b86b1bbSFabien Thomas 	assert(bucket < pgf->pgf_nbuckets);
5010b86b1bbSFabien Thomas 
502465dadb5SJonathan T. Looney 	if (args.pa_flags & FLAG_DO_WIDE_GPROF_HC) {
503465dadb5SJonathan T. Looney 		whc = (WIDEHISTCOUNTER *) ((uintptr_t) pgf->pgf_gmondata +
504465dadb5SJonathan T. Looney 		    sizeof(struct gmonhdr));
505465dadb5SJonathan T. Looney 
506465dadb5SJonathan T. Looney 		/* saturating add */
507465dadb5SJonathan T. Looney 		if (whc[bucket] < WIDEHISTCOUNTER_MAX)
508465dadb5SJonathan T. Looney 			whc[bucket]++;
509465dadb5SJonathan T. Looney 		else /* mark that an overflow occurred */
510465dadb5SJonathan T. Looney 			pgf->pgf_overflow = 1;
511465dadb5SJonathan T. Looney 	} else {
5120b86b1bbSFabien Thomas 		hc = (HISTCOUNTER *) ((uintptr_t) pgf->pgf_gmondata +
5130b86b1bbSFabien Thomas 		    sizeof(struct gmonhdr));
5140b86b1bbSFabien Thomas 
5150b86b1bbSFabien Thomas 		/* saturating add */
516465dadb5SJonathan T. Looney 		if (hc[bucket] < HISTCOUNTER_MAX)
5170b86b1bbSFabien Thomas 			hc[bucket]++;
5180b86b1bbSFabien Thomas 		else /* mark that an overflow occurred */
5190b86b1bbSFabien Thomas 			pgf->pgf_overflow = 1;
520465dadb5SJonathan T. Looney 	}
5210b86b1bbSFabien Thomas 
5220b86b1bbSFabien Thomas 	pgf->pgf_nsamples++;
5230b86b1bbSFabien Thomas }
5240b86b1bbSFabien Thomas 
5250b86b1bbSFabien Thomas /*
5260b86b1bbSFabien Thomas  * Shutdown module.
5270b86b1bbSFabien Thomas  */
5280b86b1bbSFabien Thomas 
5290b86b1bbSFabien Thomas void
pmcpl_gmon_shutdown(FILE * mf)5300b86b1bbSFabien Thomas pmcpl_gmon_shutdown(FILE *mf)
5310b86b1bbSFabien Thomas {
5320b86b1bbSFabien Thomas 	int i;
5330b86b1bbSFabien Thomas 	struct pmcstat_gmonfile *pgf;
5340b86b1bbSFabien Thomas 	struct pmcstat_image *pi;
5350b86b1bbSFabien Thomas 
5360b86b1bbSFabien Thomas 	/*
5370b86b1bbSFabien Thomas 	 * Sync back all gprof flat profile data.
5380b86b1bbSFabien Thomas 	 */
5390b86b1bbSFabien Thomas 	for (i = 0; i < PMCSTAT_NHASH; i++) {
5400b86b1bbSFabien Thomas 		LIST_FOREACH(pi, &pmcstat_image_hash[i], pi_next) {
5410b86b1bbSFabien Thomas 			if (mf)
5420b86b1bbSFabien Thomas 				(void) fprintf(mf, " \"%s\" => \"%s\"",
5430b86b1bbSFabien Thomas 				    pmcstat_string_unintern(pi->pi_execpath),
5440b86b1bbSFabien Thomas 				    pmcstat_string_unintern(
5450b86b1bbSFabien Thomas 				    pi->pi_samplename));
5460b86b1bbSFabien Thomas 
5470b86b1bbSFabien Thomas 			/* flush gmon.out data to disk */
5480b86b1bbSFabien Thomas 			LIST_FOREACH(pgf, &pi->pi_gmlist, pgf_next) {
5490b86b1bbSFabien Thomas 				pmcstat_gmon_unmap_file(pgf);
5500b86b1bbSFabien Thomas 				if (mf)
5510b86b1bbSFabien Thomas 					(void) fprintf(mf, " %s/%d",
5520b86b1bbSFabien Thomas 					    pmcstat_pmcid_to_name(
5530b86b1bbSFabien Thomas 					    pgf->pgf_pmcid),
5540b86b1bbSFabien Thomas 					    pgf->pgf_nsamples);
5550b86b1bbSFabien Thomas 				if (pgf->pgf_overflow && args.pa_verbosity >= 1)
55637d6f8a9SDavid E. O'Brien 					warnx(
55737d6f8a9SDavid E. O'Brien "WARNING: profile \"%s\" overflowed.",
5580b86b1bbSFabien Thomas 					    pmcstat_string_unintern(
5590b86b1bbSFabien Thomas 					        pgf->pgf_name));
5600b86b1bbSFabien Thomas 			}
5610b86b1bbSFabien Thomas 
5620b86b1bbSFabien Thomas 			if (mf)
5630b86b1bbSFabien Thomas 				(void) fprintf(mf, "\n");
5640b86b1bbSFabien Thomas 		}
5650b86b1bbSFabien Thomas 	}
5660b86b1bbSFabien Thomas 
5670b86b1bbSFabien Thomas 	/*
5680b86b1bbSFabien Thomas 	 * Compute arcs and add these to the gprof files.
5690b86b1bbSFabien Thomas 	 */
5700b86b1bbSFabien Thomas 	if (args.pa_flags & FLAG_DO_GPROF && args.pa_graphdepth > 1)
5710b86b1bbSFabien Thomas 		pmcstat_callgraph_do_gmon_arcs();
5720b86b1bbSFabien Thomas }
573