xref: /openbsd-src/lib/libc/gmon/gmon.c (revision cbc0cb7478a4d52269c1cb6b48d259163e33f172)
1 /*	$OpenBSD: gmon.c,v 1.25 2015/09/14 14:17:10 guenther 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 PROTO_NORMAL(moncontrol);
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 		close(fd);
235 		return;
236 	}
237 	snprintf(dbuf, sizeof dbuf, "[mcleanup1] kcount 0x%x ssiz %d\n",
238 	    p->kcount, p->kcountsize);
239 	write(log, dbuf, strlen(dbuf));
240 #endif
241 	hdr = (struct gmonhdr *)&gmonhdr;
242 	bzero(hdr, sizeof(*hdr));
243 	hdr->lpc = p->lowpc;
244 	hdr->hpc = p->highpc;
245 	hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
246 	hdr->version = GMONVERSION;
247 	hdr->profrate = clockinfo.profhz;
248 	write(fd, (char *)hdr, sizeof *hdr);
249 	write(fd, p->kcount, p->kcountsize);
250 	endfrom = p->fromssize / sizeof(*p->froms);
251 	for (fromindex = 0; fromindex < endfrom; fromindex++) {
252 		if (p->froms[fromindex] == 0)
253 			continue;
254 
255 		frompc = p->lowpc;
256 		frompc += fromindex * p->hashfraction * sizeof(*p->froms);
257 		for (toindex = p->froms[fromindex]; toindex != 0;
258 		     toindex = p->tos[toindex].link) {
259 #ifdef DEBUG
260 			(void) snprintf(dbuf, sizeof dbuf,
261 			"[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
262 				frompc, p->tos[toindex].selfpc,
263 				p->tos[toindex].count);
264 			write(log, dbuf, strlen(dbuf));
265 #endif
266 			rawarc.raw_frompc = frompc;
267 			rawarc.raw_selfpc = p->tos[toindex].selfpc;
268 			rawarc.raw_count = p->tos[toindex].count;
269 			write(fd, &rawarc, sizeof rawarc);
270 		}
271 	}
272 	close(fd);
273 #ifdef notyet
274 	if (p->kcount != NULL) {
275 		munmap(p->kcount, p->kcountsize);
276 		p->kcount = NULL;
277 	}
278 	if (p->froms != NULL) {
279 		munmap(p->froms, p->fromssize);
280 		p->froms = NULL;
281 	}
282 	if (p->tos != NULL) {
283 		munmap(p->tos, p->tossize);
284 		p->tos = NULL;
285 	}
286 #endif
287 }
288 
289 /*
290  * Control profiling
291  *	profiling is what mcount checks to see if
292  *	all the data structures are ready.
293  */
294 void
295 moncontrol(int mode)
296 {
297 	struct gmonparam *p = &_gmonparam;
298 
299 	if (mode) {
300 		/* start */
301 		profil((char *)p->kcount, p->kcountsize, p->lowpc,
302 		    s_scale);
303 		p->state = GMON_PROF_ON;
304 	} else {
305 		/* stop */
306 		profil((char *)0, 0, 0, 0);
307 		p->state = GMON_PROF_OFF;
308 	}
309 }
310 DEF_WEAK(moncontrol);
311 
312 /*
313  * discover the tick frequency of the machine
314  * if something goes wrong, we return 0, an impossible hertz.
315  */
316 static int
317 hertz(void)
318 {
319 	struct itimerval tim;
320 
321 	tim.it_interval.tv_sec = 0;
322 	tim.it_interval.tv_usec = 1;
323 	tim.it_value.tv_sec = 0;
324 	tim.it_value.tv_usec = 0;
325 	setitimer(ITIMER_REAL, &tim, 0);
326 	setitimer(ITIMER_REAL, 0, &tim);
327 	if (tim.it_interval.tv_usec < 2)
328 		return(0);
329 	return (1000000 / tim.it_interval.tv_usec);
330 }
331