xref: /openbsd-src/lib/libc/gmon/gmon.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: gmon.c,v 1.20 2005/11/20 17:06:06 millert 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/param.h>
32 #include <sys/time.h>
33 #include <sys/gmon.h>
34 #include <sys/mman.h>
35 #include <sys/sysctl.h>
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <fcntl.h>
41 #include <limits.h>
42 #include <unistd.h>
43 
44 struct gmonparam _gmonparam = { GMON_PROF_OFF };
45 
46 static int	s_scale;
47 /* see profil(2) where this is describe (incorrectly) */
48 #define		SCALE_1_TO_1	0x10000L
49 
50 #define ERR(s) write(STDERR_FILENO, s, sizeof(s))
51 
52 void	moncontrol(int);
53 static int hertz(void);
54 void	monstartup(u_long lowpc, u_long highpc);
55 void	_mcleanup(void);
56 
57 void
58 monstartup(u_long lowpc, u_long highpc)
59 {
60 	int o;
61 	void *addr;
62 	struct gmonparam *p = &_gmonparam;
63 
64 	/*
65 	 * round lowpc and highpc to multiples of the density we're using
66 	 * so the rest of the scaling (here and in gprof) stays in ints.
67 	 */
68 	p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
69 	p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
70 	p->textsize = p->highpc - p->lowpc;
71 	p->kcountsize = p->textsize / HISTFRACTION;
72 	p->hashfraction = HASHFRACTION;
73 	p->fromssize = p->textsize / p->hashfraction;
74 	p->tolimit = p->textsize * ARCDENSITY / 100;
75 	if (p->tolimit < MINARCS)
76 		p->tolimit = MINARCS;
77 	else if (p->tolimit > MAXARCS)
78 		p->tolimit = MAXARCS;
79 	p->tossize = p->tolimit * sizeof(struct tostruct);
80 
81 	addr = mmap((void *)0, p->kcountsize,  PROT_READ|PROT_WRITE,
82 	    MAP_ANON|MAP_PRIVATE, -1, (off_t)0);
83 	if (addr == MAP_FAILED)
84 		goto mapfailed;
85 	p->kcount = addr;
86 
87 	addr = mmap((void *)0, p->fromssize,  PROT_READ|PROT_WRITE,
88 	    MAP_ANON|MAP_PRIVATE, -1, (off_t)0);
89 	if (addr == MAP_FAILED)
90 		goto mapfailed;
91 	p->froms = addr;
92 
93 	addr = mmap((void *)0, p->tossize,  PROT_READ|PROT_WRITE,
94 	    MAP_ANON|MAP_PRIVATE, -1, (off_t)0);
95 	if (addr == MAP_FAILED)
96 		goto mapfailed;
97 	p->tos = addr;
98 	p->tos[0].link = 0;
99 
100 	o = p->highpc - p->lowpc;
101 	if (p->kcountsize < o) {
102 #ifndef notdef
103 		s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
104 #else /* avoid floating point */
105 		int quot = o / p->kcountsize;
106 
107 		if (quot >= 0x10000)
108 			s_scale = 1;
109 		else if (quot >= 0x100)
110 			s_scale = 0x10000 / quot;
111 		else if (o >= 0x800000)
112 			s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
113 		else
114 			s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
115 #endif
116 	} else
117 		s_scale = SCALE_1_TO_1;
118 
119 	moncontrol(1);
120 	return;
121 
122 mapfailed:
123 	if (p->kcount != NULL) {
124 		munmap(p->kcount, p->kcountsize);
125 		p->kcount = NULL;
126 	}
127 	if (p->froms != NULL) {
128 		munmap(p->froms, p->fromssize);
129 		p->froms = NULL;
130 	}
131 	if (p->tos != NULL) {
132 		munmap(p->tos, p->tossize);
133 		p->tos = NULL;
134 	}
135 	ERR("monstartup: out of memory\n");
136 }
137 
138 void
139 _mcleanup(void)
140 {
141 	int fd;
142 	int fromindex;
143 	int endfrom;
144 	u_long frompc;
145 	int toindex;
146 	struct rawarc rawarc;
147 	struct gmonparam *p = &_gmonparam;
148 	struct gmonhdr gmonhdr, *hdr;
149 	struct clockinfo clockinfo;
150 	int mib[2];
151 	size_t size;
152 	char *profdir;
153 	char *proffile;
154 	char  buf[PATH_MAX];
155 #ifdef DEBUG
156 	int log, len;
157 	char dbuf[200];
158 #endif
159 
160 	if (p->state == GMON_PROF_ERROR)
161 		ERR("_mcleanup: tos overflow\n");
162 
163 	size = sizeof(clockinfo);
164 	mib[0] = CTL_KERN;
165 	mib[1] = KERN_CLOCKRATE;
166 	if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
167 		/*
168 		 * Best guess
169 		 */
170 		clockinfo.profhz = hertz();
171 	} else if (clockinfo.profhz == 0) {
172 		if (clockinfo.hz != 0)
173 			clockinfo.profhz = clockinfo.hz;
174 		else
175 			clockinfo.profhz = hertz();
176 	}
177 
178 	moncontrol(0);
179 
180 	if (issetugid() == 0 && (profdir = getenv("PROFDIR")) != NULL) {
181 		extern char *__progname;
182 		char *s, *t, *limit;
183 		pid_t pid;
184 		long divisor;
185 
186 		/* If PROFDIR contains a null value, no profiling
187 		   output is produced */
188 		if (*profdir == '\0') {
189 			return;
190 		}
191 
192 		limit = buf + sizeof buf - 1 - 10 - 1 -
193 		    strlen(__progname) - 1;
194 		t = buf;
195 		s = profdir;
196 		while((*t = *s) != '\0' && t < limit) {
197 			t++;
198 			s++;
199 		}
200 		*t++ = '/';
201 
202 		/*
203 		 * Copy and convert pid from a pid_t to a string.  For
204 		 * best performance, divisor should be initialized to
205 		 * the largest power of 10 less than PID_MAX.
206 		 */
207 		pid = getpid();
208 		divisor=10000;
209 		while (divisor > pid) divisor /= 10;	/* skip leading zeros */
210 		do {
211 			*t++ = (pid/divisor) + '0';
212 			pid %= divisor;
213 		} while (divisor /= 10);
214 		*t++ = '.';
215 
216 		s = __progname;
217 		while ((*t++ = *s++) != '\0')
218 			;
219 
220 		proffile = buf;
221 	} else {
222 		proffile = "gmon.out";
223 	}
224 
225 	fd = open(proffile , O_CREAT|O_TRUNC|O_WRONLY, 0664);
226 	if (fd < 0) {
227 		perror( proffile );
228 		return;
229 	}
230 #ifdef DEBUG
231 	log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
232 	if (log < 0) {
233 		perror("mcount: gmon.log");
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