xref: /openbsd-src/lib/libc/gmon/gmon.c (revision ee40e6f6fabe77ce6fe4e8eb1a95f5cf1a44deb5)
1 /*-
2  * Copyright (c) 1983, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #if !defined(lint) && defined(LIBC_SCCS)
31 static char rcsid[] = "$OpenBSD: gmon.c,v 1.17 2005/03/23 19:32:09 otto Exp $";
32 #endif
33 
34 #include <sys/param.h>
35 #include <sys/time.h>
36 #include <sys/gmon.h>
37 #include <sys/sysctl.h>
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <fcntl.h>
43 #include <limits.h>
44 #include <unistd.h>
45 
46 extern char *minbrk __asm ("minbrk");
47 
48 struct gmonparam _gmonparam = { GMON_PROF_OFF };
49 
50 static int	s_scale;
51 /* see profil(2) where this is describe (incorrectly) */
52 #define		SCALE_1_TO_1	0x10000L
53 
54 #define ERR(s) write(STDERR_FILENO, s, sizeof(s))
55 
56 void	moncontrol(int);
57 static int hertz(void);
58 void	monstartup(u_long lowpc, u_long highpc);
59 void	_mcleanup(void);
60 
61 void
62 monstartup(u_long lowpc, u_long highpc)
63 {
64 	int o;
65 	char *cp;
66 	struct gmonparam *p = &_gmonparam;
67 
68 	/*
69 	 * round lowpc and highpc to multiples of the density we're using
70 	 * so the rest of the scaling (here and in gprof) stays in ints.
71 	 */
72 	p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
73 	p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
74 	p->textsize = p->highpc - p->lowpc;
75 	p->kcountsize = p->textsize / HISTFRACTION;
76 	p->hashfraction = HASHFRACTION;
77 	p->fromssize = p->textsize / p->hashfraction;
78 	p->tolimit = p->textsize * ARCDENSITY / 100;
79 	if (p->tolimit < MINARCS)
80 		p->tolimit = MINARCS;
81 	else if (p->tolimit > MAXARCS)
82 		p->tolimit = MAXARCS;
83 	p->tossize = p->tolimit * sizeof(struct tostruct);
84 
85 	cp = sbrk(p->kcountsize + p->fromssize + p->tossize);
86 	if (cp == (char *)-1) {
87 		ERR("monstartup: out of memory\n");
88 		return;
89 	}
90 #ifdef notdef
91 	bzero(cp, p->kcountsize + p->fromssize + p->tossize);
92 #endif
93 	p->tos = (struct tostruct *)cp;
94 	cp += p->tossize;
95 	p->kcount = (u_short *)cp;
96 	cp += p->kcountsize;
97 	p->froms = (u_short *)cp;
98 
99 	minbrk = sbrk(0);
100 	p->tos[0].link = 0;
101 
102 	o = p->highpc - p->lowpc;
103 	if (p->kcountsize < o) {
104 #ifndef notdef
105 		s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
106 #else /* avoid floating point */
107 		int quot = o / p->kcountsize;
108 
109 		if (quot >= 0x10000)
110 			s_scale = 1;
111 		else if (quot >= 0x100)
112 			s_scale = 0x10000 / quot;
113 		else if (o >= 0x800000)
114 			s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
115 		else
116 			s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
117 #endif
118 	} else
119 		s_scale = SCALE_1_TO_1;
120 
121 	moncontrol(1);
122 }
123 
124 void
125 _mcleanup(void)
126 {
127 	int fd;
128 	int fromindex;
129 	int endfrom;
130 	u_long frompc;
131 	int toindex;
132 	struct rawarc rawarc;
133 	struct gmonparam *p = &_gmonparam;
134 	struct gmonhdr gmonhdr, *hdr;
135 	struct clockinfo clockinfo;
136 	int mib[2];
137 	size_t size;
138 	char *profdir;
139 	char *proffile;
140 	char  buf[PATH_MAX];
141 #ifdef DEBUG
142 	int log, len;
143 	char dbuf[200];
144 #endif
145 
146 	if (p->state == GMON_PROF_ERROR)
147 		ERR("_mcleanup: tos overflow\n");
148 
149 	size = sizeof(clockinfo);
150 	mib[0] = CTL_KERN;
151 	mib[1] = KERN_CLOCKRATE;
152 	if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
153 		/*
154 		 * Best guess
155 		 */
156 		clockinfo.profhz = hertz();
157 	} else if (clockinfo.profhz == 0) {
158 		if (clockinfo.hz != 0)
159 			clockinfo.profhz = clockinfo.hz;
160 		else
161 			clockinfo.profhz = hertz();
162 	}
163 
164 	moncontrol(0);
165 
166 	if (issetugid() == 0 && (profdir = getenv("PROFDIR")) != NULL) {
167 		extern char *__progname;
168 		char *s, *t, *limit;
169 		pid_t pid;
170 		long divisor;
171 
172 		/* If PROFDIR contains a null value, no profiling
173 		   output is produced */
174 		if (*profdir == '\0') {
175 			return;
176 		}
177 
178 		limit = buf + sizeof buf - 1 - 10 - 1 -
179 		    strlen(__progname) - 1;
180 		t = buf;
181 		s = profdir;
182 		while((*t = *s) != '\0' && t < limit) {
183 			t++;
184 			s++;
185 		}
186 		*t++ = '/';
187 
188 		/*
189 		 * Copy and convert pid from a pid_t to a string.  For
190 		 * best performance, divisor should be initialized to
191 		 * the largest power of 10 less than PID_MAX.
192 		 */
193 		pid = getpid();
194 		divisor=10000;
195 		while (divisor > pid) divisor /= 10;	/* skip leading zeros */
196 		do {
197 			*t++ = (pid/divisor) + '0';
198 			pid %= divisor;
199 		} while (divisor /= 10);
200 		*t++ = '.';
201 
202 		s = __progname;
203 		while ((*t++ = *s++) != '\0')
204 			;
205 
206 		proffile = buf;
207 	} else {
208 		proffile = "gmon.out";
209 	}
210 
211 	fd = open(proffile , O_CREAT|O_TRUNC|O_WRONLY, 0664);
212 	if (fd < 0) {
213 		perror( proffile );
214 		return;
215 	}
216 #ifdef DEBUG
217 	log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
218 	if (log < 0) {
219 		perror("mcount: gmon.log");
220 		return;
221 	}
222 	len = snprintf(dbuf, sizeof dbuf, "[mcleanup1] kcount 0x%x ssiz %d\n",
223 	    p->kcount, p->kcountsize);
224 	write(log, dbuf, len);
225 #endif
226 	hdr = (struct gmonhdr *)&gmonhdr;
227 	bzero(hdr, sizeof(*hdr));
228 	hdr->lpc = p->lowpc;
229 	hdr->hpc = p->highpc;
230 	hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
231 	hdr->version = GMONVERSION;
232 	hdr->profrate = clockinfo.profhz;
233 	write(fd, (char *)hdr, sizeof *hdr);
234 	write(fd, p->kcount, p->kcountsize);
235 	endfrom = p->fromssize / sizeof(*p->froms);
236 	for (fromindex = 0; fromindex < endfrom; fromindex++) {
237 		if (p->froms[fromindex] == 0)
238 			continue;
239 
240 		frompc = p->lowpc;
241 		frompc += fromindex * p->hashfraction * sizeof(*p->froms);
242 		for (toindex = p->froms[fromindex]; toindex != 0;
243 		     toindex = p->tos[toindex].link) {
244 #ifdef DEBUG
245 			len = snprintf(dbuf, sizeof dbuf,
246 			"[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
247 				frompc, p->tos[toindex].selfpc,
248 				p->tos[toindex].count);
249 			write(log, dbuf, len);
250 #endif
251 			rawarc.raw_frompc = frompc;
252 			rawarc.raw_selfpc = p->tos[toindex].selfpc;
253 			rawarc.raw_count = p->tos[toindex].count;
254 			write(fd, &rawarc, sizeof rawarc);
255 		}
256 	}
257 	close(fd);
258 }
259 
260 /*
261  * Control profiling
262  *	profiling is what mcount checks to see if
263  *	all the data structures are ready.
264  */
265 void
266 moncontrol(int mode)
267 {
268 	struct gmonparam *p = &_gmonparam;
269 
270 	if (mode) {
271 		/* start */
272 		profil((char *)p->kcount, p->kcountsize, p->lowpc,
273 		    s_scale);
274 		p->state = GMON_PROF_ON;
275 	} else {
276 		/* stop */
277 		profil((char *)0, 0, 0, 0);
278 		p->state = GMON_PROF_OFF;
279 	}
280 }
281 
282 /*
283  * discover the tick frequency of the machine
284  * if something goes wrong, we return 0, an impossible hertz.
285  */
286 static int
287 hertz(void)
288 {
289 	struct itimerval tim;
290 
291 	tim.it_interval.tv_sec = 0;
292 	tim.it_interval.tv_usec = 1;
293 	tim.it_value.tv_sec = 0;
294 	tim.it_value.tv_usec = 0;
295 	setitimer(ITIMER_REAL, &tim, 0);
296 	setitimer(ITIMER_REAL, 0, &tim);
297 	if (tim.it_interval.tv_usec < 2)
298 		return(0);
299 	return (1000000 / tim.it_interval.tv_usec);
300 }
301 
302 
303