xref: /freebsd/usr.sbin/pmcstat/pmcpl_gprof.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005-2007, Joseph Koshy
5  * Copyright (c) 2007 The FreeBSD Foundation
6  * Copyright (c) 2009, Fabien Thomas
7  * All rights reserved.
8  *
9  * Portions of this software were developed by A. Joseph Koshy under
10  * sponsorship from the FreeBSD Foundation and Google, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*
35  * Transform a hwpmc(4) log into human readable form, and into
36  * gprof(1) compatible profiles.
37  */
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 "pmcpl_callgraph.h"
74 #include "pmcpl_gprof.h"
75 
76 typedef	uint64_t	WIDEHISTCOUNTER;
77 
78 #define	min(A,B)		((A) < (B) ? (A) : (B))
79 #define	max(A,B)		((A) > (B) ? (A) : (B))
80 
81 #define	WIDEHISTCOUNTER_MAX		UINT64_MAX
82 #define	HISTCOUNTER_MAX			USHRT_MAX
83 #define	WIDEHISTCOUNTER_GMONTYPE	((int) 64)
84 #define	HISTCOUNTER_GMONTYPE		((int) 0)
85 static int hc_sz=0;
86 
87 /*
88  * struct pmcstat_gmonfile tracks a given 'gmon.out' file.  These
89  * files are mmap()'ed in as needed.
90  */
91 
92 struct pmcstat_gmonfile {
93 	LIST_ENTRY(pmcstat_gmonfile)	pgf_next; /* list of entries */
94 	int		pgf_overflow;	/* whether a count overflowed */
95 	pmc_id_t	pgf_pmcid;	/* id of the associated pmc */
96 	size_t		pgf_nbuckets;	/* #buckets in this gmon.out */
97 	unsigned int	pgf_nsamples;	/* #samples in this gmon.out */
98 	pmcstat_interned_string pgf_name;	/* pathname of gmon.out file */
99 	size_t		pgf_ndatabytes;	/* number of bytes mapped */
100 	void		*pgf_gmondata;	/* pointer to mmap'ed data */
101 	FILE		*pgf_file;	/* used when writing gmon arcs */
102 };
103 
104 /*
105  * Prototypes
106  */
107 
108 static void	pmcstat_gmon_create_file(struct pmcstat_gmonfile *_pgf,
109     struct pmcstat_image *_image);
110 static pmcstat_interned_string pmcstat_gmon_create_name(const char *_sd,
111     struct pmcstat_image *_img, pmc_id_t _pmcid);
112 static void	pmcstat_gmon_map_file(struct pmcstat_gmonfile *_pgf);
113 static void	pmcstat_gmon_unmap_file(struct pmcstat_gmonfile *_pgf);
114 
115 static struct pmcstat_gmonfile *pmcstat_image_find_gmonfile(struct
116     pmcstat_image *_i, pmc_id_t _id);
117 
118 /*
119  * Create a gmon.out file and size it.
120  */
121 
122 static void
123 pmcstat_gmon_create_file(struct pmcstat_gmonfile *pgf,
124     struct pmcstat_image *image)
125 {
126 	int fd;
127 	size_t count;
128 	struct gmonhdr gm;
129 	const char *pathname;
130 	char buffer[DEFAULT_BUFFER_SIZE];
131 
132 	pathname = pmcstat_string_unintern(pgf->pgf_name);
133 	if ((fd = open(pathname, O_RDWR|O_NOFOLLOW|O_CREAT,
134 		 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
135 		err(EX_OSERR, "ERROR: Cannot open \"%s\"", pathname);
136 
137 	gm.lpc = image->pi_start;
138 	gm.hpc = image->pi_end;
139 	gm.ncnt = (pgf->pgf_nbuckets * hc_sz) + sizeof(struct gmonhdr);
140 	gm.version = GMONVERSION;
141 	gm.profrate = 0;		/* use ticks */
142 	if (args.pa_flags & FLAG_DO_WIDE_GPROF_HC)
143 		gm.histcounter_type = WIDEHISTCOUNTER_GMONTYPE;
144 	else
145 		gm.histcounter_type = HISTCOUNTER_GMONTYPE;
146 	gm.spare[0] = gm.spare[1] = 0;
147 
148 	/* Write out the gmon header */
149 	if (write(fd, &gm, sizeof(gm)) < 0)
150 		goto error;
151 
152 	/* Zero fill the samples[] array */
153 	(void) memset(buffer, 0, sizeof(buffer));
154 
155 	count = pgf->pgf_ndatabytes - sizeof(struct gmonhdr);
156 	while (count > sizeof(buffer)) {
157 		if (write(fd, &buffer, sizeof(buffer)) < 0)
158 			goto error;
159 		count -= sizeof(buffer);
160 	}
161 
162 	if (write(fd, &buffer, count) < 0)
163 		goto error;
164 
165 	(void) close(fd);
166 
167 	return;
168 
169  error:
170 	err(EX_OSERR, "ERROR: Cannot write \"%s\"", pathname);
171 }
172 
173 /*
174  * Determine the full pathname of a gmon.out file for a given
175  * (image,pmcid) combination.  Return the interned string.
176  */
177 
178 pmcstat_interned_string
179 pmcstat_gmon_create_name(const char *samplesdir, struct pmcstat_image *image,
180     pmc_id_t pmcid)
181 {
182 	const char *pmcname;
183 	char fullpath[PATH_MAX];
184 
185 	pmcname = pmcstat_pmcid_to_name(pmcid);
186 	if (!pmcname)
187 		err(EX_SOFTWARE, "ERROR: cannot find pmcid");
188 
189 	(void) snprintf(fullpath, sizeof(fullpath),
190 	    "%s/%s/%s", samplesdir, pmcname,
191 	    pmcstat_string_unintern(image->pi_samplename));
192 
193 	return (pmcstat_string_intern(fullpath));
194 }
195 
196 
197 /*
198  * Mmap in a gmon.out file for processing.
199  */
200 
201 static void
202 pmcstat_gmon_map_file(struct pmcstat_gmonfile *pgf)
203 {
204 	int fd;
205 	const char *pathname;
206 
207 	pathname = pmcstat_string_unintern(pgf->pgf_name);
208 
209 	/* the gmon.out file must already exist */
210 	if ((fd = open(pathname, O_RDWR | O_NOFOLLOW, 0)) < 0)
211 		err(EX_OSERR, "ERROR: cannot open \"%s\"", pathname);
212 
213 	pgf->pgf_gmondata = mmap(NULL, pgf->pgf_ndatabytes,
214 	    PROT_READ|PROT_WRITE, MAP_NOSYNC|MAP_SHARED, fd, 0);
215 
216 	if (pgf->pgf_gmondata == MAP_FAILED)
217 		err(EX_OSERR, "ERROR: cannot map \"%s\"", pathname);
218 
219 	(void) close(fd);
220 }
221 
222 /*
223  * Unmap a gmon.out file after sync'ing its data to disk.
224  */
225 
226 static void
227 pmcstat_gmon_unmap_file(struct pmcstat_gmonfile *pgf)
228 {
229 	(void) msync(pgf->pgf_gmondata, pgf->pgf_ndatabytes,
230 	    MS_SYNC);
231 	(void) munmap(pgf->pgf_gmondata, pgf->pgf_ndatabytes);
232 	pgf->pgf_gmondata = NULL;
233 }
234 
235 static void
236 pmcstat_gmon_append_arc(struct pmcstat_image *image, pmc_id_t pmcid,
237     uintptr_t rawfrom, uintptr_t rawto, uint32_t count)
238 {
239 	struct rawarc arc;	/* from <sys/gmon.h> */
240 	const char *pathname;
241 	struct pmcstat_gmonfile *pgf;
242 
243 	if ((pgf = pmcstat_image_find_gmonfile(image, pmcid)) == NULL)
244 		return;
245 
246 	if (pgf->pgf_file == NULL) {
247 		pathname = pmcstat_string_unintern(pgf->pgf_name);
248 		if ((pgf->pgf_file = fopen(pathname, "a")) == NULL)
249 			return;
250 	}
251 
252 	arc.raw_frompc = rawfrom + image->pi_vaddr;
253 	arc.raw_selfpc = rawto + image->pi_vaddr;
254 	arc.raw_count = count;
255 
256 	(void) fwrite(&arc, sizeof(arc), 1, pgf->pgf_file);
257 
258 }
259 
260 static struct pmcstat_gmonfile *
261 pmcstat_image_find_gmonfile(struct pmcstat_image *image, pmc_id_t pmcid)
262 {
263 	struct pmcstat_gmonfile *pgf;
264 	LIST_FOREACH(pgf, &image->pi_gmlist, pgf_next)
265 	    if (pgf->pgf_pmcid == pmcid)
266 		    return (pgf);
267 	return (NULL);
268 }
269 
270 static void
271 pmcstat_cgnode_do_gmon_arcs(struct pmcstat_cgnode *cg, pmc_id_t pmcid)
272 {
273 	struct pmcstat_cgnode *cgc;
274 
275 	/*
276 	 * Look for child nodes that belong to the same image.
277 	 */
278 
279 	LIST_FOREACH(cgc, &cg->pcg_children, pcg_sibling) {
280 		if (cgc->pcg_image == cg->pcg_image)
281 			pmcstat_gmon_append_arc(cg->pcg_image, pmcid,
282 			    cgc->pcg_func, cg->pcg_func, cgc->pcg_count);
283 		if (cgc->pcg_nchildren > 0)
284 			pmcstat_cgnode_do_gmon_arcs(cgc, pmcid);
285 	}
286 }
287 
288 static void
289 pmcstat_callgraph_do_gmon_arcs_for_pmcid(pmc_id_t pmcid)
290 {
291 	int n;
292 	struct pmcstat_cgnode_hash *pch;
293 
294 	for (n = 0; n < PMCSTAT_NHASH; n++)
295 		LIST_FOREACH(pch, &pmcstat_cgnode_hash[n], pch_next)
296 			if (pch->pch_pmcid == pmcid &&
297 			    pch->pch_cgnode->pcg_nchildren > 1)
298 				pmcstat_cgnode_do_gmon_arcs(pch->pch_cgnode,
299 				    pmcid);
300 }
301 
302 
303 static void
304 pmcstat_callgraph_do_gmon_arcs(void)
305 {
306 	struct pmcstat_pmcrecord *pmcr;
307 
308 	LIST_FOREACH(pmcr, &pmcstat_pmcs, pr_next)
309 		pmcstat_callgraph_do_gmon_arcs_for_pmcid(pmcr->pr_pmcid);
310 }
311 
312 void
313 pmcpl_gmon_initimage(struct pmcstat_image *pi)
314 {
315 	const char *execpath;
316 	int count, nlen;
317 	char *sn, *snbuf;
318 	char name[NAME_MAX];
319 
320 	/*
321 	 * Look for a suitable name for the sample files associated
322 	 * with this image: if `basename(path)`+".gmon" is available,
323 	 * we use that, otherwise we try iterating through
324 	 * `basename(path)`+ "~" + NNN + ".gmon" till we get a free
325 	 * entry.
326 	 */
327 	execpath = pmcstat_string_unintern(pi->pi_execpath);
328 	if ((snbuf = strdup(execpath)) == NULL)
329 		err(EX_OSERR, "ERROR: Cannot copy \"%s\"", execpath);
330 	if ((sn = basename(snbuf)) == NULL)
331 		err(EX_OSERR, "ERROR: Cannot process \"%s\"", execpath);
332 
333 	nlen = strlen(sn);
334 	nlen = min(nlen, (int) (sizeof(name) - sizeof(".gmon")));
335 
336 	snprintf(name, sizeof(name), "%.*s.gmon", nlen, sn);
337 
338 	/* try use the unabridged name first */
339 	if (pmcstat_string_lookup(name) == NULL)
340 		pi->pi_samplename = pmcstat_string_intern(name);
341 	else {
342 		/*
343 		 * Otherwise use a prefix from the original name and
344 		 * up to 3 digits.
345 		 */
346 		nlen = strlen(sn);
347 		nlen = min(nlen, (int) (sizeof(name)-sizeof("~NNN.gmon")));
348 		count = 0;
349 		do {
350 			if (++count > 999)
351 				errx(EX_CANTCREAT,
352 				    "ERROR: cannot create a gmon file for"
353 				    " \"%s\"", name);
354 			snprintf(name, sizeof(name), "%.*s~%3.3d.gmon",
355 			    nlen, sn, count);
356 			if (pmcstat_string_lookup(name) == NULL) {
357 				pi->pi_samplename =
358 				    pmcstat_string_intern(name);
359 				count = 0;
360 			}
361 		} while (count > 0);
362 	}
363 	free(snbuf);
364 
365 	LIST_INIT(&pi->pi_gmlist);
366 }
367 
368 void
369 pmcpl_gmon_shutdownimage(struct pmcstat_image *pi)
370 {
371 	struct pmcstat_gmonfile *pgf, *pgftmp;
372 
373 	LIST_FOREACH_SAFE(pgf, &pi->pi_gmlist, pgf_next, pgftmp) {
374 		if (pgf->pgf_file)
375 			(void) fclose(pgf->pgf_file);
376 		LIST_REMOVE(pgf, pgf_next);
377 		free(pgf);
378 	}
379 }
380 
381 void
382 pmcpl_gmon_newpmc(pmcstat_interned_string ps, struct pmcstat_pmcrecord *pr)
383 {
384 	struct stat st;
385 	char fullpath[PATH_MAX];
386 
387 	(void) pr;
388 
389 	/*
390 	 * Create the appropriate directory to hold gmon.out files.
391 	 */
392 
393 	(void) snprintf(fullpath, sizeof(fullpath), "%s/%s", args.pa_samplesdir,
394 	    pmcstat_string_unintern(ps));
395 
396 	/* If the path name exists, it should be a directory */
397 	if (stat(fullpath, &st) == 0 && S_ISDIR(st.st_mode))
398 		return;
399 
400 	if (mkdir(fullpath, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) < 0)
401 		err(EX_OSERR, "ERROR: Cannot create directory \"%s\"",
402 		    fullpath);
403 }
404 
405 /*
406  * Increment the bucket in the gmon.out file corresponding to 'pmcid'
407  * and 'pc'.
408  */
409 
410 void
411 pmcpl_gmon_process(struct pmcstat_process *pp, struct pmcstat_pmcrecord *pmcr,
412     uint32_t nsamples, uintfptr_t *cc, int usermode, uint32_t cpu)
413 {
414 	struct pmcstat_pcmap *map;
415 	struct pmcstat_image *image;
416 	struct pmcstat_gmonfile *pgf;
417 	uintfptr_t bucket;
418 	HISTCOUNTER *hc;
419 	WIDEHISTCOUNTER *whc;
420 	pmc_id_t pmcid;
421 
422 	(void) nsamples; (void) usermode; (void) cpu;
423 
424 	map = pmcstat_process_find_map(usermode ? pp : pmcstat_kernproc, cc[0]);
425 	if (map == NULL) {
426 		/* Unknown offset. */
427 		pmcstat_stats.ps_samples_unknown_offset++;
428 		return;
429 	}
430 
431 	assert(cc[0] >= map->ppm_lowpc && cc[0] < map->ppm_highpc);
432 
433 	image = map->ppm_image;
434 	pmcid = pmcr->pr_pmcid;
435 
436 	/*
437 	 * If this is the first time we are seeing a sample for
438 	 * this executable image, try determine its parameters.
439 	 */
440 	if (image->pi_type == PMCSTAT_IMAGE_UNKNOWN)
441 		pmcstat_image_determine_type(image, &args);
442 
443 	assert(image->pi_type != PMCSTAT_IMAGE_UNKNOWN);
444 
445 	/* Ignore samples in images that we know nothing about. */
446 	if (image->pi_type == PMCSTAT_IMAGE_INDETERMINABLE) {
447 		pmcstat_stats.ps_samples_indeterminable++;
448 		return;
449 	}
450 
451 	/*
452 	 * Find the gmon file corresponding to 'pmcid', creating it if
453 	 * needed.
454 	 */
455 	pgf = pmcstat_image_find_gmonfile(image, pmcid);
456 	if (pgf == NULL) {
457 		if (hc_sz == 0) {
458 			/* Determine the correct histcounter size. */
459 			if (args.pa_flags & FLAG_DO_WIDE_GPROF_HC)
460 				hc_sz = sizeof(WIDEHISTCOUNTER);
461 			else
462 				hc_sz = sizeof(HISTCOUNTER);
463 		}
464 
465 		if ((pgf = calloc(1, sizeof(*pgf))) == NULL)
466 			err(EX_OSERR, "ERROR:");
467 
468 		pgf->pgf_gmondata = NULL;	/* mark as unmapped */
469 		pgf->pgf_name = pmcstat_gmon_create_name(args.pa_samplesdir,
470 		    image, pmcid);
471 		pgf->pgf_pmcid = pmcid;
472 		assert(image->pi_end > image->pi_start);
473 		pgf->pgf_nbuckets = howmany(image->pi_end - image->pi_start,
474 		    FUNCTION_ALIGNMENT);	/* see <machine/profile.h> */
475 		pgf->pgf_ndatabytes = sizeof(struct gmonhdr) +
476 		    pgf->pgf_nbuckets * hc_sz;
477 		pgf->pgf_nsamples = 0;
478 		pgf->pgf_file = NULL;
479 
480 		pmcstat_gmon_create_file(pgf, image);
481 
482 		LIST_INSERT_HEAD(&image->pi_gmlist, pgf, pgf_next);
483 	}
484 
485 	/*
486 	 * Map the gmon file in if needed.  It may have been mapped
487 	 * out under memory pressure.
488 	 */
489 	if (pgf->pgf_gmondata == NULL)
490 		pmcstat_gmon_map_file(pgf);
491 
492 	assert(pgf->pgf_gmondata != NULL);
493 
494 	/*
495 	 *
496 	 */
497 
498 	bucket = (cc[0] - map->ppm_lowpc) / FUNCTION_ALIGNMENT;
499 
500 	assert(bucket < pgf->pgf_nbuckets);
501 
502 	if (args.pa_flags & FLAG_DO_WIDE_GPROF_HC) {
503 		whc = (WIDEHISTCOUNTER *) ((uintptr_t) pgf->pgf_gmondata +
504 		    sizeof(struct gmonhdr));
505 
506 		/* saturating add */
507 		if (whc[bucket] < WIDEHISTCOUNTER_MAX)
508 			whc[bucket]++;
509 		else /* mark that an overflow occurred */
510 			pgf->pgf_overflow = 1;
511 	} else {
512 		hc = (HISTCOUNTER *) ((uintptr_t) pgf->pgf_gmondata +
513 		    sizeof(struct gmonhdr));
514 
515 		/* saturating add */
516 		if (hc[bucket] < HISTCOUNTER_MAX)
517 			hc[bucket]++;
518 		else /* mark that an overflow occurred */
519 			pgf->pgf_overflow = 1;
520 	}
521 
522 	pgf->pgf_nsamples++;
523 }
524 
525 /*
526  * Shutdown module.
527  */
528 
529 void
530 pmcpl_gmon_shutdown(FILE *mf)
531 {
532 	int i;
533 	struct pmcstat_gmonfile *pgf;
534 	struct pmcstat_image *pi;
535 
536 	/*
537 	 * Sync back all gprof flat profile data.
538 	 */
539 	for (i = 0; i < PMCSTAT_NHASH; i++) {
540 		LIST_FOREACH(pi, &pmcstat_image_hash[i], pi_next) {
541 			if (mf)
542 				(void) fprintf(mf, " \"%s\" => \"%s\"",
543 				    pmcstat_string_unintern(pi->pi_execpath),
544 				    pmcstat_string_unintern(
545 				    pi->pi_samplename));
546 
547 			/* flush gmon.out data to disk */
548 			LIST_FOREACH(pgf, &pi->pi_gmlist, pgf_next) {
549 				pmcstat_gmon_unmap_file(pgf);
550 				if (mf)
551 					(void) fprintf(mf, " %s/%d",
552 					    pmcstat_pmcid_to_name(
553 					    pgf->pgf_pmcid),
554 					    pgf->pgf_nsamples);
555 				if (pgf->pgf_overflow && args.pa_verbosity >= 1)
556 					warnx(
557 "WARNING: profile \"%s\" overflowed.",
558 					    pmcstat_string_unintern(
559 					        pgf->pgf_name));
560 			}
561 
562 			if (mf)
563 				(void) fprintf(mf, "\n");
564 		}
565 	}
566 
567 	/*
568 	 * Compute arcs and add these to the gprof files.
569 	 */
570 	if (args.pa_flags & FLAG_DO_GPROF && args.pa_graphdepth > 1)
571 		pmcstat_callgraph_do_gmon_arcs();
572 }
573