xref: /openbsd-src/lib/libc/gmon/gmon.c (revision ee993850557956b27e55fa65b556e9fd65e21593)
1 /*	$OpenBSD: gmon.c,v 1.24 2015/05/06 23:52:49 jsg Exp $ */
2 /*-
3  * Copyright (c) 1983, 1992, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/time.h>
32 #include <sys/gmon.h>
33 #include <sys/mman.h>
34 #include <sys/sysctl.h>
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <fcntl.h>
40 #include <limits.h>
41 #include <unistd.h>
42 
43 struct gmonparam _gmonparam = { GMON_PROF_OFF };
44 
45 static int	s_scale;
46 /* see profil(2) where this is describe (incorrectly) */
47 #define		SCALE_1_TO_1	0x10000L
48 
49 #define ERR(s) write(STDERR_FILENO, s, sizeof(s))
50 
51 void	moncontrol(int);
52 static int hertz(void);
53 void	monstartup(u_long lowpc, u_long highpc);
54 void	_mcleanup(void);
55 
56 void
57 monstartup(u_long lowpc, u_long highpc)
58 {
59 	int o;
60 	void *addr;
61 	struct gmonparam *p = &_gmonparam;
62 
63 	/*
64 	 * round lowpc and highpc to multiples of the density we're using
65 	 * so the rest of the scaling (here and in gprof) stays in ints.
66 	 */
67 	p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
68 	p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
69 	p->textsize = p->highpc - p->lowpc;
70 	p->kcountsize = p->textsize / HISTFRACTION;
71 	p->hashfraction = HASHFRACTION;
72 	p->fromssize = p->textsize / p->hashfraction;
73 	p->tolimit = p->textsize * ARCDENSITY / 100;
74 	if (p->tolimit < MINARCS)
75 		p->tolimit = MINARCS;
76 	else if (p->tolimit > MAXARCS)
77 		p->tolimit = MAXARCS;
78 	p->tossize = p->tolimit * sizeof(struct tostruct);
79 
80 	addr = mmap((void *)0, p->kcountsize,  PROT_READ|PROT_WRITE,
81 	    MAP_ANON|MAP_PRIVATE, -1, (off_t)0);
82 	if (addr == MAP_FAILED)
83 		goto mapfailed;
84 	p->kcount = addr;
85 
86 	addr = mmap((void *)0, p->fromssize,  PROT_READ|PROT_WRITE,
87 	    MAP_ANON|MAP_PRIVATE, -1, (off_t)0);
88 	if (addr == MAP_FAILED)
89 		goto mapfailed;
90 	p->froms = addr;
91 
92 	addr = mmap((void *)0, p->tossize,  PROT_READ|PROT_WRITE,
93 	    MAP_ANON|MAP_PRIVATE, -1, (off_t)0);
94 	if (addr == MAP_FAILED)
95 		goto mapfailed;
96 	p->tos = addr;
97 	p->tos[0].link = 0;
98 
99 	o = p->highpc - p->lowpc;
100 	if (p->kcountsize < o) {
101 #ifndef notdef
102 		s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
103 #else /* avoid floating point */
104 		int quot = o / p->kcountsize;
105 
106 		if (quot >= 0x10000)
107 			s_scale = 1;
108 		else if (quot >= 0x100)
109 			s_scale = 0x10000 / quot;
110 		else if (o >= 0x800000)
111 			s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
112 		else
113 			s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
114 #endif
115 	} else
116 		s_scale = SCALE_1_TO_1;
117 
118 	moncontrol(1);
119 	return;
120 
121 mapfailed:
122 	if (p->kcount != NULL) {
123 		munmap(p->kcount, p->kcountsize);
124 		p->kcount = NULL;
125 	}
126 	if (p->froms != NULL) {
127 		munmap(p->froms, p->fromssize);
128 		p->froms = NULL;
129 	}
130 	if (p->tos != NULL) {
131 		munmap(p->tos, p->tossize);
132 		p->tos = NULL;
133 	}
134 	ERR("monstartup: out of memory\n");
135 }
136 
137 void
138 _mcleanup(void)
139 {
140 	int fd;
141 	int fromindex;
142 	int endfrom;
143 	u_long frompc;
144 	int toindex;
145 	struct rawarc rawarc;
146 	struct gmonparam *p = &_gmonparam;
147 	struct gmonhdr gmonhdr, *hdr;
148 	struct clockinfo clockinfo;
149 	int mib[2];
150 	size_t size;
151 	char *profdir;
152 	char *proffile;
153 	char  buf[PATH_MAX];
154 #ifdef DEBUG
155 	int log, len;
156 	char dbuf[200];
157 #endif
158 
159 	if (p->state == GMON_PROF_ERROR)
160 		ERR("_mcleanup: tos overflow\n");
161 
162 	size = sizeof(clockinfo);
163 	mib[0] = CTL_KERN;
164 	mib[1] = KERN_CLOCKRATE;
165 	if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
166 		/*
167 		 * Best guess
168 		 */
169 		clockinfo.profhz = hertz();
170 	} else if (clockinfo.profhz == 0) {
171 		if (clockinfo.hz != 0)
172 			clockinfo.profhz = clockinfo.hz;
173 		else
174 			clockinfo.profhz = hertz();
175 	}
176 
177 	moncontrol(0);
178 
179 	if (issetugid() == 0 && (profdir = getenv("PROFDIR")) != NULL) {
180 		extern char *__progname;
181 		char *s, *t, *limit;
182 		pid_t pid;
183 		long divisor;
184 
185 		/* If PROFDIR contains a null value, no profiling
186 		   output is produced */
187 		if (*profdir == '\0') {
188 			return;
189 		}
190 
191 		limit = buf + sizeof buf - 1 - 10 - 1 -
192 		    strlen(__progname) - 1;
193 		t = buf;
194 		s = profdir;
195 		while((*t = *s) != '\0' && t < limit) {
196 			t++;
197 			s++;
198 		}
199 		*t++ = '/';
200 
201 		/*
202 		 * Copy and convert pid from a pid_t to a string.  For
203 		 * best performance, divisor should be initialized to
204 		 * the largest power of 10 less than PID_MAX.
205 		 */
206 		pid = getpid();
207 		divisor=10000;
208 		while (divisor > pid) divisor /= 10;	/* skip leading zeros */
209 		do {
210 			*t++ = (pid/divisor) + '0';
211 			pid %= divisor;
212 		} while (divisor /= 10);
213 		*t++ = '.';
214 
215 		s = __progname;
216 		while ((*t++ = *s++) != '\0')
217 			;
218 
219 		proffile = buf;
220 	} else {
221 		proffile = "gmon.out";
222 	}
223 
224 	fd = open(proffile , O_CREAT|O_TRUNC|O_WRONLY, 0664);
225 	if (fd < 0) {
226 		perror( proffile );
227 		return;
228 	}
229 #ifdef DEBUG
230 	log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
231 	if (log < 0) {
232 		perror("mcount: gmon.log");
233 		close(fd);
234 		return;
235 	}
236 	snprintf(dbuf, sizeof dbuf, "[mcleanup1] kcount 0x%x ssiz %d\n",
237 	    p->kcount, p->kcountsize);
238 	write(log, dbuf, strlen(dbuf));
239 #endif
240 	hdr = (struct gmonhdr *)&gmonhdr;
241 	bzero(hdr, sizeof(*hdr));
242 	hdr->lpc = p->lowpc;
243 	hdr->hpc = p->highpc;
244 	hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
245 	hdr->version = GMONVERSION;
246 	hdr->profrate = clockinfo.profhz;
247 	write(fd, (char *)hdr, sizeof *hdr);
248 	write(fd, p->kcount, p->kcountsize);
249 	endfrom = p->fromssize / sizeof(*p->froms);
250 	for (fromindex = 0; fromindex < endfrom; fromindex++) {
251 		if (p->froms[fromindex] == 0)
252 			continue;
253 
254 		frompc = p->lowpc;
255 		frompc += fromindex * p->hashfraction * sizeof(*p->froms);
256 		for (toindex = p->froms[fromindex]; toindex != 0;
257 		     toindex = p->tos[toindex].link) {
258 #ifdef DEBUG
259 			(void) snprintf(dbuf, sizeof dbuf,
260 			"[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
261 				frompc, p->tos[toindex].selfpc,
262 				p->tos[toindex].count);
263 			write(log, dbuf, strlen(dbuf));
264 #endif
265 			rawarc.raw_frompc = frompc;
266 			rawarc.raw_selfpc = p->tos[toindex].selfpc;
267 			rawarc.raw_count = p->tos[toindex].count;
268 			write(fd, &rawarc, sizeof rawarc);
269 		}
270 	}
271 	close(fd);
272 #ifdef notyet
273 	if (p->kcount != NULL) {
274 		munmap(p->kcount, p->kcountsize);
275 		p->kcount = NULL;
276 	}
277 	if (p->froms != NULL) {
278 		munmap(p->froms, p->fromssize);
279 		p->froms = NULL;
280 	}
281 	if (p->tos != NULL) {
282 		munmap(p->tos, p->tossize);
283 		p->tos = NULL;
284 	}
285 #endif
286 }
287 
288 /*
289  * Control profiling
290  *	profiling is what mcount checks to see if
291  *	all the data structures are ready.
292  */
293 void
294 moncontrol(int mode)
295 {
296 	struct gmonparam *p = &_gmonparam;
297 
298 	if (mode) {
299 		/* start */
300 		profil((char *)p->kcount, p->kcountsize, p->lowpc,
301 		    s_scale);
302 		p->state = GMON_PROF_ON;
303 	} else {
304 		/* stop */
305 		profil((char *)0, 0, 0, 0);
306 		p->state = GMON_PROF_OFF;
307 	}
308 }
309 
310 /*
311  * discover the tick frequency of the machine
312  * if something goes wrong, we return 0, an impossible hertz.
313  */
314 static int
315 hertz(void)
316 {
317 	struct itimerval tim;
318 
319 	tim.it_interval.tv_sec = 0;
320 	tim.it_interval.tv_usec = 1;
321 	tim.it_value.tv_sec = 0;
322 	tim.it_value.tv_usec = 0;
323 	setitimer(ITIMER_REAL, &tim, 0);
324 	setitimer(ITIMER_REAL, 0, &tim);
325 	if (tim.it_interval.tv_usec < 2)
326 		return(0);
327 	return (1000000 / tim.it_interval.tv_usec);
328 }
329