xref: /netbsd-src/lib/libc/gmon/gmon.c (revision d0fed6c87ddc40a8bffa6f99e7433ddfc864dd83)
1 /*	$NetBSD: gmon.c,v 1.9 1997/04/22 11:17:58 mrg 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 #if !defined(lint) && defined(LIBC_SCCS)
37 #if 0
38 static char sccsid[] = "@(#)gmon.c	8.1 (Berkeley) 6/4/93";
39 #else
40 static char rcsid[] = "$NetBSD: gmon.c,v 1.9 1997/04/22 11:17:58 mrg Exp $";
41 #endif
42 #endif
43 
44 #include <sys/param.h>
45 #include <sys/time.h>
46 #include <sys/gmon.h>
47 #include <sys/sysctl.h>
48 
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <fcntl.h>
52 #include <limits.h>
53 #include <unistd.h>
54 
55 extern char *minbrk __asm ("minbrk");
56 
57 struct gmonparam _gmonparam = { GMON_PROF_OFF };
58 
59 static int	s_scale;
60 /* see profil(2) where this is describe (incorrectly) */
61 #define		SCALE_1_TO_1	0x10000L
62 
63 #define ERR(s) write(2, s, sizeof(s))
64 
65 void	moncontrol __P((int));
66 static int hertz __P((void));
67 
68 void
69 monstartup(lowpc, highpc)
70 	u_long lowpc;
71 	u_long highpc;
72 {
73 	register int o;
74 	char *cp;
75 	struct gmonparam *p = &_gmonparam;
76 
77 	/*
78 	 * round lowpc and highpc to multiples of the density we're using
79 	 * so the rest of the scaling (here and in gprof) stays in ints.
80 	 */
81 	p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
82 	p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
83 	p->textsize = p->highpc - p->lowpc;
84 	p->kcountsize = p->textsize / HISTFRACTION;
85 	p->hashfraction = HASHFRACTION;
86 	p->fromssize = p->textsize / p->hashfraction;
87 	p->tolimit = p->textsize * ARCDENSITY / 100;
88 	if (p->tolimit < MINARCS)
89 		p->tolimit = MINARCS;
90 	else if (p->tolimit > MAXARCS)
91 		p->tolimit = MAXARCS;
92 	p->tossize = p->tolimit * sizeof(struct tostruct);
93 
94 	cp = sbrk(p->kcountsize + p->fromssize + p->tossize);
95 	if (cp == (char *)-1) {
96 		ERR("monstartup: out of memory\n");
97 		return;
98 	}
99 #ifdef notdef
100 	bzero(cp, p->kcountsize + p->fromssize + p->tossize);
101 #endif
102 	p->tos = (struct tostruct *)cp;
103 	cp += p->tossize;
104 	p->kcount = (u_short *)cp;
105 	cp += p->kcountsize;
106 	p->froms = (u_short *)cp;
107 
108 	minbrk = sbrk(0);
109 	p->tos[0].link = 0;
110 
111 	o = p->highpc - p->lowpc;
112 	if (p->kcountsize < o) {
113 #ifndef notdef
114 		s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
115 #else /* avoid floating point */
116 		int quot = o / p->kcountsize;
117 
118 		if (quot >= 0x10000)
119 			s_scale = 1;
120 		else if (quot >= 0x100)
121 			s_scale = 0x10000 / quot;
122 		else if (o >= 0x800000)
123 			s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
124 		else
125 			s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
126 #endif
127 	} else
128 		s_scale = SCALE_1_TO_1;
129 
130 	moncontrol(1);
131 }
132 
133 void
134 _mcleanup()
135 {
136 	int fd;
137 	int fromindex;
138 	int endfrom;
139 	u_long frompc;
140 	int toindex;
141 	struct rawarc rawarc;
142 	struct gmonparam *p = &_gmonparam;
143 	struct gmonhdr gmonhdr, *hdr;
144 	struct clockinfo clockinfo;
145 	int mib[2];
146 	size_t size;
147 	char *profdir;
148 	char *proffile;
149 	char  buf[PATH_MAX];
150 	int len = sizeof(buf) - 1;
151 #ifdef DEBUG
152 	int log, len;
153 	char buf2[200];
154 #endif
155 
156 	if (p->state == GMON_PROF_ERROR)
157 		ERR("_mcleanup: tos overflow\n");
158 
159 	size = sizeof(clockinfo);
160 	mib[0] = CTL_KERN;
161 	mib[1] = KERN_CLOCKRATE;
162 	if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
163 		/*
164 		 * Best guess
165 		 */
166 		clockinfo.profhz = hertz();
167 	} else if (clockinfo.profhz == 0) {
168 		if (clockinfo.hz != 0)
169 			clockinfo.profhz = clockinfo.hz;
170 		else
171 			clockinfo.profhz = hertz();
172 	}
173 
174 	moncontrol(0);
175 
176 	if ((profdir = getenv("PROFDIR")) != NULL) {
177 		extern char *__progname;
178 		char *s, *t;
179 		pid_t pid;
180 		long divisor;
181 
182 		/* If PROFDIR contains a null value, no profiling
183 		   output is produced */
184 		if (*profdir == '\0') {
185 			return;
186 		}
187 
188 		t = buf;
189 		s = profdir;
190 		while ((*t = *s) != '\0') {
191 			if (len-- == 0) {
192 				warnx("_mcleanup: internal buffer overflow, PROFDIR too long");
193 				return;
194 			}
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, 0666);
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 		return;
233 	}
234 	len = snprintf(buf2, sizeof buf2, "[mcleanup1] kcount 0x%x ssiz %d\n",
235 	    p->kcount, p->kcountsize);
236 	write(log, buf2, len);
237 #endif
238 	hdr = (struct gmonhdr *)&gmonhdr;
239 	hdr->lpc = p->lowpc;
240 	hdr->hpc = p->highpc;
241 	hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
242 	hdr->version = GMONVERSION;
243 	hdr->profrate = clockinfo.profhz;
244 	write(fd, (char *)hdr, sizeof *hdr);
245 	write(fd, p->kcount, p->kcountsize);
246 	endfrom = p->fromssize / sizeof(*p->froms);
247 	for (fromindex = 0; fromindex < endfrom; fromindex++) {
248 		if (p->froms[fromindex] == 0)
249 			continue;
250 
251 		frompc = p->lowpc;
252 		frompc += fromindex * p->hashfraction * sizeof(*p->froms);
253 		for (toindex = p->froms[fromindex]; toindex != 0;
254 		     toindex = p->tos[toindex].link) {
255 #ifdef DEBUG
256 			len = snprintf(buf2, sizeof buf2,
257 			"[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
258 				frompc, p->tos[toindex].selfpc,
259 				p->tos[toindex].count);
260 			write(log, buf2, len);
261 #endif
262 			rawarc.raw_frompc = frompc;
263 			rawarc.raw_selfpc = p->tos[toindex].selfpc;
264 			rawarc.raw_count = p->tos[toindex].count;
265 			write(fd, &rawarc, sizeof rawarc);
266 		}
267 	}
268 	close(fd);
269 }
270 
271 /*
272  * Control profiling
273  *	profiling is what mcount checks to see if
274  *	all the data structures are ready.
275  */
276 void
277 moncontrol(mode)
278 	int mode;
279 {
280 	struct gmonparam *p = &_gmonparam;
281 
282 	if (mode) {
283 		/* start */
284 		profil((char *)p->kcount, p->kcountsize, p->lowpc,
285 		    s_scale);
286 		p->state = GMON_PROF_ON;
287 	} else {
288 		/* stop */
289 		profil((char *)0, 0, 0, 0);
290 		p->state = GMON_PROF_OFF;
291 	}
292 }
293 
294 /*
295  * discover the tick frequency of the machine
296  * if something goes wrong, we return 0, an impossible hertz.
297  */
298 static int
299 hertz()
300 {
301 	struct itimerval tim;
302 
303 	tim.it_interval.tv_sec = 0;
304 	tim.it_interval.tv_usec = 1;
305 	tim.it_value.tv_sec = 0;
306 	tim.it_value.tv_usec = 0;
307 	setitimer(ITIMER_REAL, &tim, 0);
308 	setitimer(ITIMER_REAL, 0, &tim);
309 	if (tim.it_interval.tv_usec < 2)
310 		return(0);
311 	return (1000000 / tim.it_interval.tv_usec);
312 }
313 
314 
315