xref: /openbsd-src/lib/libc/gmon/gmon.c (revision 6509ae5655ddaa75c407c660308b558633a32b80)
1 /*	$OpenBSD: gmon.c,v 1.21 2013/02/12 07:31:13 mpi 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 ROUNDDOWN(x,y)(((x)/(y))*(y))
51 #define ROUNDUP(x,y)((((x)+(y)-1)/(y))*(y))
52 
53 #define ERR(s) write(STDERR_FILENO, s, sizeof(s))
54 
55 void	moncontrol(int);
56 static int hertz(void);
57 void	monstartup(u_long lowpc, u_long highpc);
58 void	_mcleanup(void);
59 
60 void
61 monstartup(u_long lowpc, u_long highpc)
62 {
63 	int o;
64 	void *addr;
65 	struct gmonparam *p = &_gmonparam;
66 
67 	/*
68 	 * round lowpc and highpc to multiples of the density we're using
69 	 * so the rest of the scaling (here and in gprof) stays in ints.
70 	 */
71 	p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
72 	p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
73 	p->textsize = p->highpc - p->lowpc;
74 	p->kcountsize = p->textsize / HISTFRACTION;
75 	p->hashfraction = HASHFRACTION;
76 	p->fromssize = p->textsize / p->hashfraction;
77 	p->tolimit = p->textsize * ARCDENSITY / 100;
78 	if (p->tolimit < MINARCS)
79 		p->tolimit = MINARCS;
80 	else if (p->tolimit > MAXARCS)
81 		p->tolimit = MAXARCS;
82 	p->tossize = p->tolimit * sizeof(struct tostruct);
83 
84 	addr = mmap((void *)0, p->kcountsize,  PROT_READ|PROT_WRITE,
85 	    MAP_ANON|MAP_PRIVATE, -1, (off_t)0);
86 	if (addr == MAP_FAILED)
87 		goto mapfailed;
88 	p->kcount = addr;
89 
90 	addr = mmap((void *)0, p->fromssize,  PROT_READ|PROT_WRITE,
91 	    MAP_ANON|MAP_PRIVATE, -1, (off_t)0);
92 	if (addr == MAP_FAILED)
93 		goto mapfailed;
94 	p->froms = addr;
95 
96 	addr = mmap((void *)0, p->tossize,  PROT_READ|PROT_WRITE,
97 	    MAP_ANON|MAP_PRIVATE, -1, (off_t)0);
98 	if (addr == MAP_FAILED)
99 		goto mapfailed;
100 	p->tos = addr;
101 	p->tos[0].link = 0;
102 
103 	o = p->highpc - p->lowpc;
104 	if (p->kcountsize < o) {
105 #ifndef notdef
106 		s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
107 #else /* avoid floating point */
108 		int quot = o / p->kcountsize;
109 
110 		if (quot >= 0x10000)
111 			s_scale = 1;
112 		else if (quot >= 0x100)
113 			s_scale = 0x10000 / quot;
114 		else if (o >= 0x800000)
115 			s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
116 		else
117 			s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
118 #endif
119 	} else
120 		s_scale = SCALE_1_TO_1;
121 
122 	moncontrol(1);
123 	return;
124 
125 mapfailed:
126 	if (p->kcount != NULL) {
127 		munmap(p->kcount, p->kcountsize);
128 		p->kcount = NULL;
129 	}
130 	if (p->froms != NULL) {
131 		munmap(p->froms, p->fromssize);
132 		p->froms = NULL;
133 	}
134 	if (p->tos != NULL) {
135 		munmap(p->tos, p->tossize);
136 		p->tos = NULL;
137 	}
138 	ERR("monstartup: out of memory\n");
139 }
140 
141 void
142 _mcleanup(void)
143 {
144 	int fd;
145 	int fromindex;
146 	int endfrom;
147 	u_long frompc;
148 	int toindex;
149 	struct rawarc rawarc;
150 	struct gmonparam *p = &_gmonparam;
151 	struct gmonhdr gmonhdr, *hdr;
152 	struct clockinfo clockinfo;
153 	int mib[2];
154 	size_t size;
155 	char *profdir;
156 	char *proffile;
157 	char  buf[PATH_MAX];
158 #ifdef DEBUG
159 	int log, len;
160 	char dbuf[200];
161 #endif
162 
163 	if (p->state == GMON_PROF_ERROR)
164 		ERR("_mcleanup: tos overflow\n");
165 
166 	size = sizeof(clockinfo);
167 	mib[0] = CTL_KERN;
168 	mib[1] = KERN_CLOCKRATE;
169 	if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
170 		/*
171 		 * Best guess
172 		 */
173 		clockinfo.profhz = hertz();
174 	} else if (clockinfo.profhz == 0) {
175 		if (clockinfo.hz != 0)
176 			clockinfo.profhz = clockinfo.hz;
177 		else
178 			clockinfo.profhz = hertz();
179 	}
180 
181 	moncontrol(0);
182 
183 	if (issetugid() == 0 && (profdir = getenv("PROFDIR")) != NULL) {
184 		extern char *__progname;
185 		char *s, *t, *limit;
186 		pid_t pid;
187 		long divisor;
188 
189 		/* If PROFDIR contains a null value, no profiling
190 		   output is produced */
191 		if (*profdir == '\0') {
192 			return;
193 		}
194 
195 		limit = buf + sizeof buf - 1 - 10 - 1 -
196 		    strlen(__progname) - 1;
197 		t = buf;
198 		s = profdir;
199 		while((*t = *s) != '\0' && t < limit) {
200 			t++;
201 			s++;
202 		}
203 		*t++ = '/';
204 
205 		/*
206 		 * Copy and convert pid from a pid_t to a string.  For
207 		 * best performance, divisor should be initialized to
208 		 * the largest power of 10 less than PID_MAX.
209 		 */
210 		pid = getpid();
211 		divisor=10000;
212 		while (divisor > pid) divisor /= 10;	/* skip leading zeros */
213 		do {
214 			*t++ = (pid/divisor) + '0';
215 			pid %= divisor;
216 		} while (divisor /= 10);
217 		*t++ = '.';
218 
219 		s = __progname;
220 		while ((*t++ = *s++) != '\0')
221 			;
222 
223 		proffile = buf;
224 	} else {
225 		proffile = "gmon.out";
226 	}
227 
228 	fd = open(proffile , O_CREAT|O_TRUNC|O_WRONLY, 0664);
229 	if (fd < 0) {
230 		perror( proffile );
231 		return;
232 	}
233 #ifdef DEBUG
234 	log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
235 	if (log < 0) {
236 		perror("mcount: gmon.log");
237 		return;
238 	}
239 	snprintf(dbuf, sizeof dbuf, "[mcleanup1] kcount 0x%x ssiz %d\n",
240 	    p->kcount, p->kcountsize);
241 	write(log, dbuf, strlen(dbuf));
242 #endif
243 	hdr = (struct gmonhdr *)&gmonhdr;
244 	bzero(hdr, sizeof(*hdr));
245 	hdr->lpc = p->lowpc;
246 	hdr->hpc = p->highpc;
247 	hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
248 	hdr->version = GMONVERSION;
249 	hdr->profrate = clockinfo.profhz;
250 	write(fd, (char *)hdr, sizeof *hdr);
251 	write(fd, p->kcount, p->kcountsize);
252 	endfrom = p->fromssize / sizeof(*p->froms);
253 	for (fromindex = 0; fromindex < endfrom; fromindex++) {
254 		if (p->froms[fromindex] == 0)
255 			continue;
256 
257 		frompc = p->lowpc;
258 		frompc += fromindex * p->hashfraction * sizeof(*p->froms);
259 		for (toindex = p->froms[fromindex]; toindex != 0;
260 		     toindex = p->tos[toindex].link) {
261 #ifdef DEBUG
262 			(void) snprintf(dbuf, sizeof dbuf,
263 			"[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
264 				frompc, p->tos[toindex].selfpc,
265 				p->tos[toindex].count);
266 			write(log, dbuf, strlen(dbuf));
267 #endif
268 			rawarc.raw_frompc = frompc;
269 			rawarc.raw_selfpc = p->tos[toindex].selfpc;
270 			rawarc.raw_count = p->tos[toindex].count;
271 			write(fd, &rawarc, sizeof rawarc);
272 		}
273 	}
274 	close(fd);
275 #ifdef notyet
276 	if (p->kcount != NULL) {
277 		munmap(p->kcount, p->kcountsize);
278 		p->kcount = NULL;
279 	}
280 	if (p->froms != NULL) {
281 		munmap(p->froms, p->fromssize);
282 		p->froms = NULL;
283 	}
284 	if (p->tos != NULL) {
285 		munmap(p->tos, p->tossize);
286 		p->tos = NULL;
287 	}
288 #endif
289 }
290 
291 /*
292  * Control profiling
293  *	profiling is what mcount checks to see if
294  *	all the data structures are ready.
295  */
296 void
297 moncontrol(int mode)
298 {
299 	struct gmonparam *p = &_gmonparam;
300 
301 	if (mode) {
302 		/* start */
303 		profil((char *)p->kcount, p->kcountsize, p->lowpc,
304 		    s_scale);
305 		p->state = GMON_PROF_ON;
306 	} else {
307 		/* stop */
308 		profil((char *)0, 0, 0, 0);
309 		p->state = GMON_PROF_OFF;
310 	}
311 }
312 
313 /*
314  * discover the tick frequency of the machine
315  * if something goes wrong, we return 0, an impossible hertz.
316  */
317 static int
318 hertz(void)
319 {
320 	struct itimerval tim;
321 
322 	tim.it_interval.tv_sec = 0;
323 	tim.it_interval.tv_usec = 1;
324 	tim.it_value.tv_sec = 0;
325 	tim.it_value.tv_usec = 0;
326 	setitimer(ITIMER_REAL, &tim, 0);
327 	setitimer(ITIMER_REAL, 0, &tim);
328 	if (tim.it_interval.tv_usec < 2)
329 		return(0);
330 	return (1000000 / tim.it_interval.tv_usec);
331 }
332