xref: /freebsd/lib/libc/gmon/gmon.c (revision d500a85e640d1cd270747c12e17c511b53864436)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char sccsid[] = "@(#)gmon.c	8.1 (Berkeley) 6/4/93";
34 #endif
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include "namespace.h"
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/gmon.h>
42 #include <sys/mman.h>
43 #include <sys/sysctl.h>
44 
45 #include <err.h>
46 #include <fcntl.h>
47 #include <inttypes.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include "un-namespace.h"
53 
54 #include "libc_private.h"
55 
56 struct gmonparam _gmonparam = { GMON_PROF_OFF };
57 
58 static int	s_scale;
59 /* See profil(2) where this is described (incorrectly). */
60 #define	SCALE_SHIFT	16
61 
62 #define ERR(s) _write(2, s, sizeof(s))
63 
64 void	moncontrol(int);
65 static int hertz(void);
66 void	_mcleanup(void);
67 
68 void
69 monstartup(u_long lowpc, u_long highpc)
70 {
71 	int o;
72 	char *cp;
73 	struct gmonparam *p = &_gmonparam;
74 
75 	/*
76 	 * round lowpc and highpc to multiples of the density we're using
77 	 * so the rest of the scaling (here and in gprof) stays in ints.
78 	 */
79 	p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
80 	p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
81 	p->textsize = p->highpc - p->lowpc;
82 	p->kcountsize = p->textsize / HISTFRACTION;
83 	p->hashfraction = HASHFRACTION;
84 	p->fromssize = p->textsize / HASHFRACTION;
85 	p->tolimit = p->textsize * ARCDENSITY / 100;
86 	if (p->tolimit < MINARCS)
87 		p->tolimit = MINARCS;
88 	else if (p->tolimit > MAXARCS)
89 		p->tolimit = MAXARCS;
90 	p->tossize = p->tolimit * sizeof(struct tostruct);
91 
92 	cp = mmap(NULL, p->kcountsize + p->fromssize + p->tossize,
93 	    PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
94 	if (cp == MAP_FAILED) {
95 		ERR("monstartup: out of memory\n");
96 		return;
97 	}
98 #ifdef notdef
99 	bzero(cp, p->kcountsize + p->fromssize + p->tossize);
100 #endif
101 	p->tos = (struct tostruct *)cp;
102 	cp += p->tossize;
103 	p->kcount = (u_short *)cp;
104 	cp += p->kcountsize;
105 	p->froms = (u_short *)cp;
106 
107 	p->tos[0].link = 0;
108 
109 	o = p->highpc - p->lowpc;
110 	s_scale = (p->kcountsize < o) ?
111 	    ((uintmax_t)p->kcountsize << SCALE_SHIFT) / o : (1 << SCALE_SHIFT);
112 	moncontrol(1);
113 }
114 
115 void
116 _mcleanup(void)
117 {
118 	int fd;
119 	int fromindex;
120 	int endfrom;
121 	u_long frompc;
122 	int toindex;
123 	struct rawarc rawarc;
124 	struct gmonparam *p = &_gmonparam;
125 	struct gmonhdr gmonhdr, *hdr;
126 	struct clockinfo clockinfo;
127 	char outname[128];
128 	int mib[2];
129 	size_t size;
130 #ifdef DEBUG
131 	int log, len;
132 	char buf[200];
133 #endif
134 
135 	if (p->state == GMON_PROF_ERROR)
136 		ERR("_mcleanup: tos overflow\n");
137 
138 	size = sizeof(clockinfo);
139 	mib[0] = CTL_KERN;
140 	mib[1] = KERN_CLOCKRATE;
141 	if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
142 		/*
143 		 * Best guess
144 		 */
145 		clockinfo.profhz = hertz();
146 	} else if (clockinfo.profhz == 0) {
147 		if (clockinfo.hz != 0)
148 			clockinfo.profhz = clockinfo.hz;
149 		else
150 			clockinfo.profhz = hertz();
151 	}
152 
153 	moncontrol(0);
154 	if (getenv("PROFIL_USE_PID"))
155 		snprintf(outname, sizeof(outname), "%s.%d.gmon",
156 		    _getprogname(), getpid());
157 	else
158 		snprintf(outname, sizeof(outname), "%s.gmon", _getprogname());
159 
160 	fd = _open(outname, O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0666);
161 	if (fd < 0) {
162 		_warn("_mcleanup: %s", outname);
163 		return;
164 	}
165 #ifdef DEBUG
166 	log = _open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0664);
167 	if (log < 0) {
168 		_warn("_mcleanup: gmon.log");
169 		return;
170 	}
171 	len = sprintf(buf, "[mcleanup1] kcount 0x%p ssiz %lu\n",
172 	    p->kcount, p->kcountsize);
173 	_write(log, buf, len);
174 #endif
175 	hdr = (struct gmonhdr *)&gmonhdr;
176 	bzero(hdr, sizeof(*hdr));
177 	hdr->lpc = p->lowpc;
178 	hdr->hpc = p->highpc;
179 	hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
180 	hdr->version = GMONVERSION;
181 	hdr->profrate = clockinfo.profhz;
182 	_write(fd, (char *)hdr, sizeof *hdr);
183 	_write(fd, p->kcount, p->kcountsize);
184 	endfrom = p->fromssize / sizeof(*p->froms);
185 	for (fromindex = 0; fromindex < endfrom; fromindex++) {
186 		if (p->froms[fromindex] == 0)
187 			continue;
188 
189 		frompc = p->lowpc;
190 		frompc += fromindex * p->hashfraction * sizeof(*p->froms);
191 		for (toindex = p->froms[fromindex]; toindex != 0;
192 		     toindex = p->tos[toindex].link) {
193 #ifdef DEBUG
194 			len = sprintf(buf,
195 			"[mcleanup2] frompc 0x%lx selfpc 0x%lx count %lu\n" ,
196 				frompc, p->tos[toindex].selfpc,
197 				p->tos[toindex].count);
198 			_write(log, buf, len);
199 #endif
200 			rawarc.raw_frompc = frompc;
201 			rawarc.raw_selfpc = p->tos[toindex].selfpc;
202 			rawarc.raw_count = p->tos[toindex].count;
203 			_write(fd, &rawarc, sizeof rawarc);
204 		}
205 	}
206 	_close(fd);
207 }
208 
209 /*
210  * Control profiling
211  *	profiling is what mcount checks to see if
212  *	all the data structures are ready.
213  */
214 void
215 moncontrol(int mode)
216 {
217 	struct gmonparam *p = &_gmonparam;
218 
219 	if (mode) {
220 		/* start */
221 		profil((char *)p->kcount, p->kcountsize, p->lowpc, s_scale);
222 		p->state = GMON_PROF_ON;
223 	} else {
224 		/* stop */
225 		profil((char *)0, 0, 0, 0);
226 		p->state = GMON_PROF_OFF;
227 	}
228 }
229 
230 /*
231  * discover the tick frequency of the machine
232  * if something goes wrong, we return 0, an impossible hertz.
233  */
234 static int
235 hertz(void)
236 {
237 	struct itimerval tim;
238 
239 	tim.it_interval.tv_sec = 0;
240 	tim.it_interval.tv_usec = 1;
241 	tim.it_value.tv_sec = 0;
242 	tim.it_value.tv_usec = 0;
243 	setitimer(ITIMER_REAL, &tim, 0);
244 	setitimer(ITIMER_REAL, 0, &tim);
245 	if (tim.it_interval.tv_usec < 2)
246 		return(0);
247 	return (1000000 / tim.it_interval.tv_usec);
248 }
249