xref: /netbsd-src/lib/libc/gmon/gmon.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: gmon.c,v 1.15 1999/01/14 22:48:19 kleink Exp $	*/
2 
3 /*-
4  * Copyright (c) 1983, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #if !defined(lint) && defined(LIBC_SCCS)
38 #if 0
39 static char sccsid[] = "@(#)gmon.c	8.1 (Berkeley) 6/4/93";
40 #else
41 __RCSID("$NetBSD: gmon.c,v 1.15 1999/01/14 22:48:19 kleink Exp $");
42 #endif
43 #endif
44 
45 #include "namespace.h"
46 #include <sys/param.h>
47 #include <sys/time.h>
48 #include <sys/gmon.h>
49 #include <sys/sysctl.h>
50 
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <fcntl.h>
54 #include <limits.h>
55 #include <unistd.h>
56 #include <err.h>
57 #include "extern.h"
58 
59 struct gmonparam _gmonparam = { GMON_PROF_OFF };
60 
61 static u_int	s_scale;
62 /* see profil(2) where this is describe (incorrectly) */
63 #define		SCALE_1_TO_1	0x10000L
64 
65 #define ERR(s) write(STDERR_FILENO, s, sizeof(s))
66 
67 void	moncontrol __P((int));
68 void	monstartup __P((u_long, u_long));
69 void	_mcleanup __P((void));
70 static int hertz __P((void));
71 
72 void
73 monstartup(lowpc, highpc)
74 	u_long lowpc;
75 	u_long highpc;
76 {
77 	long o;
78 	char *cp;
79 	struct gmonparam *p = &_gmonparam;
80 
81 	/*
82 	 * round lowpc and highpc to multiples of the density we're using
83 	 * so the rest of the scaling (here and in gprof) stays in ints.
84 	 */
85 	p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
86 	p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
87 	p->textsize = p->highpc - p->lowpc;
88 	p->kcountsize = p->textsize / HISTFRACTION;
89 	p->hashfraction = HASHFRACTION;
90 	p->fromssize = p->textsize / p->hashfraction;
91 	p->tolimit = p->textsize * ARCDENSITY / 100;
92 	if (p->tolimit < MINARCS)
93 		p->tolimit = MINARCS;
94 	else if (p->tolimit > MAXARCS)
95 		p->tolimit = MAXARCS;
96 	p->tossize = p->tolimit * sizeof(struct tostruct);
97 
98 	cp = sbrk((int)(p->kcountsize + p->fromssize + p->tossize));
99 	if (cp == (char *)-1) {
100 		ERR("monstartup: out of memory\n");
101 		return;
102 	}
103 #ifdef notdef
104 	memset(cp, 0, p->kcountsize + p->fromssize + p->tossize);
105 #endif
106 	p->tos = (struct tostruct *)(void *)cp;
107 	cp += (size_t)p->tossize;
108 	p->kcount = (u_short *)(void *)cp;
109 	cp += (size_t)p->kcountsize;
110 	p->froms = (u_short *)(void *)cp;
111 
112 	__minbrk = sbrk(0);
113 	p->tos[0].link = 0;
114 
115 	o = p->highpc - p->lowpc;
116 	if (p->kcountsize < o) {
117 #ifndef notdef
118 		s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
119 #else /* avoid floating point */
120 		int quot = o / p->kcountsize;
121 
122 		if (quot >= 0x10000)
123 			s_scale = 1;
124 		else if (quot >= 0x100)
125 			s_scale = 0x10000 / quot;
126 		else if (o >= 0x800000)
127 			s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
128 		else
129 			s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
130 #endif
131 	} else
132 		s_scale = SCALE_1_TO_1;
133 
134 	moncontrol(1);
135 }
136 
137 void
138 _mcleanup()
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 	int len = sizeof(buf) - 1;
155 #ifdef DEBUG
156 	int log, len;
157 	char buf2[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 ((profdir = getenv("PROFDIR")) != NULL) {
181 		extern char *__progname;
182 		char *s, *t;
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 		t = buf;
193 		s = profdir;
194 		while ((*t = *s) != '\0') {
195 			if (len-- == 0) {
196 				warnx("_mcleanup: internal buffer overflow, PROFDIR too long");
197 				return;
198 			}
199 			t++;
200 			s++;
201 		}
202 		*t++ = '/';
203 
204 		/*
205 		 * Copy and convert pid from a pid_t to a string.  For
206 		 * best performance, divisor should be initialized to
207 		 * the largest power of 10 less than PID_MAX.
208 		 */
209 		pid = getpid();
210 		divisor=10000;
211 		while (divisor > pid) divisor /= 10;	/* skip leading zeros */
212 		do {
213 			*t++ = (char)((pid/divisor) + '0');
214 			pid %= (pid_t)divisor;
215 		} while (divisor /= 10);
216 		*t++ = '.';
217 
218 		s = __progname;
219 		while ((*t++ = *s++) != '\0')
220 			;
221 
222 		proffile = buf;
223 	} else {
224 		proffile = "gmon.out";
225 	}
226 
227 	fd = open(proffile , O_CREAT|O_TRUNC|O_WRONLY, 0666);
228 	if (fd < 0) {
229 		warn("mcount: Cannot open `%s'", proffile);
230 		return;
231 	}
232 #ifdef DEBUG
233 	log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
234 	if (log < 0) {
235 		warn("mcount: Cannot open `gmon.log'");
236 		return;
237 	}
238 	len = snprintf(buf2, sizeof buf2, "[mcleanup1] kcount 0x%x ssiz %d\n",
239 	    p->kcount, p->kcountsize);
240 	write(log, buf2, len);
241 #endif
242 	hdr = (struct gmonhdr *)&gmonhdr;
243 	hdr->lpc = p->lowpc;
244 	hdr->hpc = p->highpc;
245 	hdr->ncnt = (int)(p->kcountsize + sizeof(gmonhdr));
246 	hdr->version = GMONVERSION;
247 	hdr->profrate = clockinfo.profhz;
248 	(void)write(fd, hdr, sizeof *hdr);
249 	(void)write(fd, p->kcount, (size_t)p->kcountsize);
250 	endfrom = (int)(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 			len = snprintf(buf2, sizeof buf2,
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, buf2, len);
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 }
274 
275 /*
276  * Control profiling
277  *	profiling is what mcount checks to see if
278  *	all the data structures are ready.
279  */
280 void
281 moncontrol(mode)
282 	int mode;
283 {
284 	struct gmonparam *p = &_gmonparam;
285 
286 	if (mode) {
287 		/* start */
288 		profil((char *)(void *)p->kcount, (size_t)p->kcountsize,
289 		    p->lowpc, s_scale);
290 		p->state = GMON_PROF_ON;
291 	} else {
292 		/* stop */
293 		profil(NULL, 0, (u_long)0, 0);
294 		p->state = GMON_PROF_OFF;
295 	}
296 }
297 
298 /*
299  * discover the tick frequency of the machine
300  * if something goes wrong, we return 0, an impossible hertz.
301  */
302 static int
303 hertz()
304 {
305 	struct itimerval tim;
306 
307 	tim.it_interval.tv_sec = 0;
308 	tim.it_interval.tv_usec = 1;
309 	tim.it_value.tv_sec = 0;
310 	tim.it_value.tv_usec = 0;
311 	setitimer(ITIMER_REAL, &tim, 0);
312 	setitimer(ITIMER_REAL, 0, &tim);
313 	if (tim.it_interval.tv_usec < 2)
314 		return(0);
315 	return (int)(1000000 / tim.it_interval.tv_usec);
316 }
317