xref: /freebsd/lib/libc/gmon/gmon.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
158f0484fSRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1983, 1992, 1993
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
858f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
958f0484fSRodney W. Grimes  * are met:
1058f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1258f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1458f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1658f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1758f0484fSRodney W. Grimes  *    without specific prior written permission.
1858f0484fSRodney W. Grimes  *
1958f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2058f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2158f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2258f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2358f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2458f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2558f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2658f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2758f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2858f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2958f0484fSRodney W. Grimes  * SUCH DAMAGE.
3058f0484fSRodney W. Grimes  */
3158f0484fSRodney W. Grimes 
32d201fe46SDaniel Eischen #include "namespace.h"
3358f0484fSRodney W. Grimes #include <sys/param.h>
3458f0484fSRodney W. Grimes #include <sys/time.h>
3558f0484fSRodney W. Grimes #include <sys/gmon.h>
36af6f4233SBrooks Davis #include <sys/mman.h>
3758f0484fSRodney W. Grimes #include <sys/sysctl.h>
3858f0484fSRodney W. Grimes 
39683728f1SJohn Birrell #include <err.h>
4058f0484fSRodney W. Grimes #include <fcntl.h>
41af448c52SMarcel Moolenaar #include <inttypes.h>
424cd01193SMark Murray #include <stdio.h>
434cd01193SMark Murray #include <stdlib.h>
44d201fe46SDaniel Eischen #include <string.h>
4558f0484fSRodney W. Grimes #include <unistd.h>
46d201fe46SDaniel Eischen #include "un-namespace.h"
4758f0484fSRodney W. Grimes 
484cd01193SMark Murray #include "libc_private.h"
494cd01193SMark Murray 
5058f0484fSRodney W. Grimes struct gmonparam _gmonparam = { GMON_PROF_OFF };
5158f0484fSRodney W. Grimes 
5258f0484fSRodney W. Grimes static int	s_scale;
53af448c52SMarcel Moolenaar /* See profil(2) where this is described (incorrectly). */
54af448c52SMarcel Moolenaar #define	SCALE_SHIFT	16
5558f0484fSRodney W. Grimes 
569233c4d9SJason Evans #define ERR(s) _write(2, s, sizeof(s))
5758f0484fSRodney W. Grimes 
58a99d1013SDavid E. O'Brien void	moncontrol(int);
59a99d1013SDavid E. O'Brien static int hertz(void);
606db7b53bSCraig Rodrigues void	_mcleanup(void);
6158f0484fSRodney W. Grimes 
6258f0484fSRodney W. Grimes void
monstartup(u_long lowpc,u_long highpc)6376470dd5SCraig Rodrigues monstartup(u_long lowpc, u_long highpc)
6458f0484fSRodney W. Grimes {
65a99d1013SDavid E. O'Brien 	int o;
6658f0484fSRodney W. Grimes 	char *cp;
6758f0484fSRodney W. Grimes 	struct gmonparam *p = &_gmonparam;
6858f0484fSRodney W. Grimes 
6958f0484fSRodney W. Grimes 	/*
7058f0484fSRodney W. Grimes 	 * round lowpc and highpc to multiples of the density we're using
7158f0484fSRodney W. Grimes 	 * so the rest of the scaling (here and in gprof) stays in ints.
7258f0484fSRodney W. Grimes 	 */
7358f0484fSRodney W. Grimes 	p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
7458f0484fSRodney W. Grimes 	p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
7558f0484fSRodney W. Grimes 	p->textsize = p->highpc - p->lowpc;
7658f0484fSRodney W. Grimes 	p->kcountsize = p->textsize / HISTFRACTION;
7758f0484fSRodney W. Grimes 	p->hashfraction = HASHFRACTION;
7858f0484fSRodney W. Grimes 	p->fromssize = p->textsize / HASHFRACTION;
7958f0484fSRodney W. Grimes 	p->tolimit = p->textsize * ARCDENSITY / 100;
8058f0484fSRodney W. Grimes 	if (p->tolimit < MINARCS)
8158f0484fSRodney W. Grimes 		p->tolimit = MINARCS;
8258f0484fSRodney W. Grimes 	else if (p->tolimit > MAXARCS)
8358f0484fSRodney W. Grimes 		p->tolimit = MAXARCS;
8458f0484fSRodney W. Grimes 	p->tossize = p->tolimit * sizeof(struct tostruct);
8558f0484fSRodney W. Grimes 
86af6f4233SBrooks Davis 	cp = mmap(NULL, p->kcountsize + p->fromssize + p->tossize,
87af6f4233SBrooks Davis 	    PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
88af6f4233SBrooks Davis 	if (cp == MAP_FAILED) {
8958f0484fSRodney W. Grimes 		ERR("monstartup: out of memory\n");
9058f0484fSRodney W. Grimes 		return;
9158f0484fSRodney W. Grimes 	}
9258f0484fSRodney W. Grimes #ifdef notdef
9358f0484fSRodney W. Grimes 	bzero(cp, p->kcountsize + p->fromssize + p->tossize);
9458f0484fSRodney W. Grimes #endif
9558f0484fSRodney W. Grimes 	p->tos = (struct tostruct *)cp;
9658f0484fSRodney W. Grimes 	cp += p->tossize;
9758f0484fSRodney W. Grimes 	p->kcount = (u_short *)cp;
9858f0484fSRodney W. Grimes 	cp += p->kcountsize;
9958f0484fSRodney W. Grimes 	p->froms = (u_short *)cp;
10058f0484fSRodney W. Grimes 
10158f0484fSRodney W. Grimes 	p->tos[0].link = 0;
10258f0484fSRodney W. Grimes 
10358f0484fSRodney W. Grimes 	o = p->highpc - p->lowpc;
104af448c52SMarcel Moolenaar 	s_scale = (p->kcountsize < o) ?
105af448c52SMarcel Moolenaar 	    ((uintmax_t)p->kcountsize << SCALE_SHIFT) / o : (1 << SCALE_SHIFT);
10658f0484fSRodney W. Grimes 	moncontrol(1);
10758f0484fSRodney W. Grimes }
10858f0484fSRodney W. Grimes 
10958f0484fSRodney W. Grimes void
_mcleanup(void)1102c201a9aSEd Schouten _mcleanup(void)
11158f0484fSRodney W. Grimes {
11258f0484fSRodney W. Grimes 	int fd;
11358f0484fSRodney W. Grimes 	int fromindex;
11458f0484fSRodney W. Grimes 	int endfrom;
11558f0484fSRodney W. Grimes 	u_long frompc;
11658f0484fSRodney W. Grimes 	int toindex;
11758f0484fSRodney W. Grimes 	struct rawarc rawarc;
11858f0484fSRodney W. Grimes 	struct gmonparam *p = &_gmonparam;
11958f0484fSRodney W. Grimes 	struct gmonhdr gmonhdr, *hdr;
12058f0484fSRodney W. Grimes 	struct clockinfo clockinfo;
121683728f1SJohn Birrell 	char outname[128];
12258f0484fSRodney W. Grimes 	int mib[2];
12358f0484fSRodney W. Grimes 	size_t size;
12458f0484fSRodney W. Grimes #ifdef DEBUG
12558f0484fSRodney W. Grimes 	int log, len;
12658f0484fSRodney W. Grimes 	char buf[200];
12758f0484fSRodney W. Grimes #endif
12858f0484fSRodney W. Grimes 
12958f0484fSRodney W. Grimes 	if (p->state == GMON_PROF_ERROR)
13058f0484fSRodney W. Grimes 		ERR("_mcleanup: tos overflow\n");
13158f0484fSRodney W. Grimes 
13258f0484fSRodney W. Grimes 	size = sizeof(clockinfo);
13358f0484fSRodney W. Grimes 	mib[0] = CTL_KERN;
13458f0484fSRodney W. Grimes 	mib[1] = KERN_CLOCKRATE;
13558f0484fSRodney W. Grimes 	if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
13658f0484fSRodney W. Grimes 		/*
13758f0484fSRodney W. Grimes 		 * Best guess
13858f0484fSRodney W. Grimes 		 */
13958f0484fSRodney W. Grimes 		clockinfo.profhz = hertz();
14058f0484fSRodney W. Grimes 	} else if (clockinfo.profhz == 0) {
14158f0484fSRodney W. Grimes 		if (clockinfo.hz != 0)
14258f0484fSRodney W. Grimes 			clockinfo.profhz = clockinfo.hz;
14358f0484fSRodney W. Grimes 		else
14458f0484fSRodney W. Grimes 			clockinfo.profhz = hertz();
14558f0484fSRodney W. Grimes 	}
14658f0484fSRodney W. Grimes 
14758f0484fSRodney W. Grimes 	moncontrol(0);
148be849092SEitan Adler 	if (getenv("PROFIL_USE_PID"))
149be849092SEitan Adler 		snprintf(outname, sizeof(outname), "%s.%d.gmon",
150be849092SEitan Adler 		    _getprogname(), getpid());
151be849092SEitan Adler 	else
1524cd01193SMark Murray 		snprintf(outname, sizeof(outname), "%s.gmon", _getprogname());
153be849092SEitan Adler 
15488d961f3SJilles Tjoelker 	fd = _open(outname, O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0666);
15558f0484fSRodney W. Grimes 	if (fd < 0) {
1562c316efdSBruce Evans 		_warn("_mcleanup: %s", outname);
15758f0484fSRodney W. Grimes 		return;
15858f0484fSRodney W. Grimes 	}
15958f0484fSRodney W. Grimes #ifdef DEBUG
16088d961f3SJilles Tjoelker 	log = _open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0664);
16158f0484fSRodney W. Grimes 	if (log < 0) {
1622c316efdSBruce Evans 		_warn("_mcleanup: gmon.log");
16358f0484fSRodney W. Grimes 		return;
16458f0484fSRodney W. Grimes 	}
165312f1851SJun Kuriyama 	len = sprintf(buf, "[mcleanup1] kcount 0x%p ssiz %lu\n",
16658f0484fSRodney W. Grimes 	    p->kcount, p->kcountsize);
1679233c4d9SJason Evans 	_write(log, buf, len);
16858f0484fSRodney W. Grimes #endif
16958f0484fSRodney W. Grimes 	hdr = (struct gmonhdr *)&gmonhdr;
170bdc5bab2SBruce Evans 	bzero(hdr, sizeof(*hdr));
17158f0484fSRodney W. Grimes 	hdr->lpc = p->lowpc;
17258f0484fSRodney W. Grimes 	hdr->hpc = p->highpc;
17358f0484fSRodney W. Grimes 	hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
17458f0484fSRodney W. Grimes 	hdr->version = GMONVERSION;
17558f0484fSRodney W. Grimes 	hdr->profrate = clockinfo.profhz;
1769233c4d9SJason Evans 	_write(fd, (char *)hdr, sizeof *hdr);
1779233c4d9SJason Evans 	_write(fd, p->kcount, p->kcountsize);
17858f0484fSRodney W. Grimes 	endfrom = p->fromssize / sizeof(*p->froms);
17958f0484fSRodney W. Grimes 	for (fromindex = 0; fromindex < endfrom; fromindex++) {
18058f0484fSRodney W. Grimes 		if (p->froms[fromindex] == 0)
18158f0484fSRodney W. Grimes 			continue;
18258f0484fSRodney W. Grimes 
18358f0484fSRodney W. Grimes 		frompc = p->lowpc;
18458f0484fSRodney W. Grimes 		frompc += fromindex * p->hashfraction * sizeof(*p->froms);
18558f0484fSRodney W. Grimes 		for (toindex = p->froms[fromindex]; toindex != 0;
18658f0484fSRodney W. Grimes 		     toindex = p->tos[toindex].link) {
18758f0484fSRodney W. Grimes #ifdef DEBUG
18858f0484fSRodney W. Grimes 			len = sprintf(buf,
189312f1851SJun Kuriyama 			"[mcleanup2] frompc 0x%lx selfpc 0x%lx count %lu\n" ,
19058f0484fSRodney W. Grimes 				frompc, p->tos[toindex].selfpc,
19158f0484fSRodney W. Grimes 				p->tos[toindex].count);
1929233c4d9SJason Evans 			_write(log, buf, len);
19358f0484fSRodney W. Grimes #endif
19458f0484fSRodney W. Grimes 			rawarc.raw_frompc = frompc;
19558f0484fSRodney W. Grimes 			rawarc.raw_selfpc = p->tos[toindex].selfpc;
19658f0484fSRodney W. Grimes 			rawarc.raw_count = p->tos[toindex].count;
1979233c4d9SJason Evans 			_write(fd, &rawarc, sizeof rawarc);
19858f0484fSRodney W. Grimes 		}
19958f0484fSRodney W. Grimes 	}
2009233c4d9SJason Evans 	_close(fd);
20158f0484fSRodney W. Grimes }
20258f0484fSRodney W. Grimes 
20358f0484fSRodney W. Grimes /*
20458f0484fSRodney W. Grimes  * Control profiling
20558f0484fSRodney W. Grimes  *	profiling is what mcount checks to see if
20658f0484fSRodney W. Grimes  *	all the data structures are ready.
20758f0484fSRodney W. Grimes  */
20858f0484fSRodney W. Grimes void
moncontrol(int mode)20976470dd5SCraig Rodrigues moncontrol(int mode)
21058f0484fSRodney W. Grimes {
21158f0484fSRodney W. Grimes 	struct gmonparam *p = &_gmonparam;
21258f0484fSRodney W. Grimes 
21358f0484fSRodney W. Grimes 	if (mode) {
21458f0484fSRodney W. Grimes 		/* start */
215daa2e8d8SHidetoshi Shimokawa 		profil((char *)p->kcount, p->kcountsize, p->lowpc, s_scale);
21658f0484fSRodney W. Grimes 		p->state = GMON_PROF_ON;
21758f0484fSRodney W. Grimes 	} else {
21858f0484fSRodney W. Grimes 		/* stop */
21958f0484fSRodney W. Grimes 		profil((char *)0, 0, 0, 0);
22058f0484fSRodney W. Grimes 		p->state = GMON_PROF_OFF;
22158f0484fSRodney W. Grimes 	}
22258f0484fSRodney W. Grimes }
22358f0484fSRodney W. Grimes 
22458f0484fSRodney W. Grimes /*
22558f0484fSRodney W. Grimes  * discover the tick frequency of the machine
22658f0484fSRodney W. Grimes  * if something goes wrong, we return 0, an impossible hertz.
22758f0484fSRodney W. Grimes  */
22858f0484fSRodney W. Grimes static int
hertz(void)22976470dd5SCraig Rodrigues hertz(void)
23058f0484fSRodney W. Grimes {
23158f0484fSRodney W. Grimes 	struct itimerval tim;
23258f0484fSRodney W. Grimes 
23358f0484fSRodney W. Grimes 	tim.it_interval.tv_sec = 0;
23458f0484fSRodney W. Grimes 	tim.it_interval.tv_usec = 1;
23558f0484fSRodney W. Grimes 	tim.it_value.tv_sec = 0;
23658f0484fSRodney W. Grimes 	tim.it_value.tv_usec = 0;
23758f0484fSRodney W. Grimes 	setitimer(ITIMER_REAL, &tim, 0);
23858f0484fSRodney W. Grimes 	setitimer(ITIMER_REAL, 0, &tim);
23958f0484fSRodney W. Grimes 	if (tim.it_interval.tv_usec < 2)
24058f0484fSRodney W. Grimes 		return(0);
24158f0484fSRodney W. Grimes 	return (1000000 / tim.it_interval.tv_usec);
24258f0484fSRodney W. Grimes }
243