xref: /openbsd/lib/libc/gmon/gmon.c (revision 9b7c3dbb)
1 /*	$OpenBSD: gmon.c,v 1.29 2016/05/07 19:30:52 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 PROTO_NORMAL(moncontrol);
52 PROTO_DEPRECATED(monstartup);
53 static int hertz(void);
54 
55 void
56 monstartup(u_long lowpc, u_long highpc)
57 {
58 	int o;
59 	void *addr;
60 	struct gmonparam *p = &_gmonparam;
61 
62 	/*
63 	 * round lowpc and highpc to multiples of the density we're using
64 	 * so the rest of the scaling (here and in gprof) stays in ints.
65 	 */
66 	p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
67 	p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
68 	p->textsize = p->highpc - p->lowpc;
69 	p->kcountsize = p->textsize / HISTFRACTION;
70 	p->hashfraction = HASHFRACTION;
71 	p->fromssize = p->textsize / p->hashfraction;
72 	p->tolimit = p->textsize * ARCDENSITY / 100;
73 	if (p->tolimit < MINARCS)
74 		p->tolimit = MINARCS;
75 	else if (p->tolimit > MAXARCS)
76 		p->tolimit = MAXARCS;
77 	p->tossize = p->tolimit * sizeof(struct tostruct);
78 
79 	addr = mmap(NULL, p->kcountsize,  PROT_READ|PROT_WRITE,
80 	    MAP_ANON|MAP_PRIVATE, -1, (off_t)0);
81 	if (addr == MAP_FAILED)
82 		goto mapfailed;
83 	p->kcount = addr;
84 
85 	addr = mmap(NULL, p->fromssize,  PROT_READ|PROT_WRITE,
86 	    MAP_ANON|MAP_PRIVATE, -1, (off_t)0);
87 	if (addr == MAP_FAILED)
88 		goto mapfailed;
89 	p->froms = addr;
90 
91 	addr = mmap(NULL, p->tossize,  PROT_READ|PROT_WRITE,
92 	    MAP_ANON|MAP_PRIVATE, -1, (off_t)0);
93 	if (addr == MAP_FAILED)
94 		goto mapfailed;
95 	p->tos = addr;
96 	p->tos[0].link = 0;
97 
98 	o = p->highpc - p->lowpc;
99 	if (p->kcountsize < o) {
100 #ifndef notdef
101 		s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
102 #else /* avoid floating point */
103 		int quot = o / p->kcountsize;
104 
105 		if (quot >= 0x10000)
106 			s_scale = 1;
107 		else if (quot >= 0x100)
108 			s_scale = 0x10000 / quot;
109 		else if (o >= 0x800000)
110 			s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
111 		else
112 			s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
113 #endif
114 	} else
115 		s_scale = SCALE_1_TO_1;
116 
117 	moncontrol(1);
118 	return;
119 
120 mapfailed:
121 	if (p->kcount != NULL) {
122 		munmap(p->kcount, p->kcountsize);
123 		p->kcount = NULL;
124 	}
125 	if (p->froms != NULL) {
126 		munmap(p->froms, p->fromssize);
127 		p->froms = NULL;
128 	}
129 	if (p->tos != NULL) {
130 		munmap(p->tos, p->tossize);
131 		p->tos = NULL;
132 	}
133 	ERR("monstartup: out of memory\n");
134 }
135 __strong_alias(_monstartup,monstartup);
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 		char *s, *t, *limit;
181 		pid_t pid;
182 		long divisor;
183 
184 		/* If PROFDIR contains a null value, no profiling
185 		   output is produced */
186 		if (*profdir == '\0') {
187 			return;
188 		}
189 
190 		limit = buf + sizeof buf - 1 - 10 - 1 -
191 		    strlen(__progname) - 1;
192 		t = buf;
193 		s = profdir;
194 		while((*t = *s) != '\0' && t < limit) {
195 			t++;
196 			s++;
197 		}
198 		*t++ = '/';
199 
200 		/*
201 		 * Copy and convert pid from a pid_t to a string.  For
202 		 * best performance, divisor should be initialized to
203 		 * the largest power of 10 less than PID_MAX.
204 		 */
205 		pid = getpid();
206 		divisor=10000;
207 		while (divisor > pid) divisor /= 10;	/* skip leading zeros */
208 		do {
209 			*t++ = (pid/divisor) + '0';
210 			pid %= divisor;
211 		} while (divisor /= 10);
212 		*t++ = '.';
213 
214 		s = __progname;
215 		while ((*t++ = *s++) != '\0')
216 			;
217 
218 		proffile = buf;
219 	} else {
220 		proffile = "gmon.out";
221 	}
222 
223 	fd = open(proffile , O_CREAT|O_TRUNC|O_WRONLY, 0664);
224 	if (fd < 0) {
225 		perror( proffile );
226 		return;
227 	}
228 #ifdef DEBUG
229 	log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
230 	if (log < 0) {
231 		perror("mcount: gmon.log");
232 		close(fd);
233 		return;
234 	}
235 	snprintf(dbuf, sizeof dbuf, "[mcleanup1] kcount 0x%x ssiz %d\n",
236 	    p->kcount, p->kcountsize);
237 	write(log, dbuf, strlen(dbuf));
238 #endif
239 	hdr = (struct gmonhdr *)&gmonhdr;
240 	bzero(hdr, sizeof(*hdr));
241 	hdr->lpc = p->lowpc;
242 	hdr->hpc = p->highpc;
243 	hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
244 	hdr->version = GMONVERSION;
245 	hdr->profrate = clockinfo.profhz;
246 	write(fd, (char *)hdr, sizeof *hdr);
247 	write(fd, p->kcount, p->kcountsize);
248 	endfrom = p->fromssize / sizeof(*p->froms);
249 	for (fromindex = 0; fromindex < endfrom; fromindex++) {
250 		if (p->froms[fromindex] == 0)
251 			continue;
252 
253 		frompc = p->lowpc;
254 		frompc += fromindex * p->hashfraction * sizeof(*p->froms);
255 		for (toindex = p->froms[fromindex]; toindex != 0;
256 		     toindex = p->tos[toindex].link) {
257 #ifdef DEBUG
258 			(void) snprintf(dbuf, sizeof dbuf,
259 			"[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
260 				frompc, p->tos[toindex].selfpc,
261 				p->tos[toindex].count);
262 			write(log, dbuf, strlen(dbuf));
263 #endif
264 			rawarc.raw_frompc = frompc;
265 			rawarc.raw_selfpc = p->tos[toindex].selfpc;
266 			rawarc.raw_count = p->tos[toindex].count;
267 			write(fd, &rawarc, sizeof rawarc);
268 		}
269 	}
270 	close(fd);
271 #ifdef notyet
272 	if (p->kcount != NULL) {
273 		munmap(p->kcount, p->kcountsize);
274 		p->kcount = NULL;
275 	}
276 	if (p->froms != NULL) {
277 		munmap(p->froms, p->fromssize);
278 		p->froms = NULL;
279 	}
280 	if (p->tos != NULL) {
281 		munmap(p->tos, p->tossize);
282 		p->tos = NULL;
283 	}
284 #endif
285 }
286 
287 /*
288  * Control profiling
289  *	profiling is what mcount checks to see if
290  *	all the data structures are ready.
291  */
292 void
293 moncontrol(int mode)
294 {
295 	struct gmonparam *p = &_gmonparam;
296 
297 	if (mode) {
298 		/* start */
299 		profil((char *)p->kcount, p->kcountsize, p->lowpc,
300 		    s_scale);
301 		p->state = GMON_PROF_ON;
302 	} else {
303 		/* stop */
304 		profil(NULL, 0, 0, 0);
305 		p->state = GMON_PROF_OFF;
306 	}
307 }
308 DEF_WEAK(moncontrol);
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