gmon.c revision 1.15.10.1 1 /* $NetBSD: gmon.c,v 1.15.10.1 2002/04/26 17:09:51 he 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.10.1 2002/04/26 17:09:51 he 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 /*
161 * We disallow writing to the profiling file, if we are a
162 * set{u,g}id program and our effective {u,g}id does not match
163 * our real one.
164 */
165 if (issetugid() && (geteuid() != getuid() || getegid() != getgid())) {
166 warnx("mcount: Profiling of set{u,g}id binaries is not"
167 " allowed");
168 return;
169 }
170
171 if (p->state == GMON_PROF_ERROR)
172 ERR("_mcleanup: tos overflow\n");
173
174 size = sizeof(clockinfo);
175 mib[0] = CTL_KERN;
176 mib[1] = KERN_CLOCKRATE;
177 if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
178 /*
179 * Best guess
180 */
181 clockinfo.profhz = hertz();
182 } else if (clockinfo.profhz == 0) {
183 if (clockinfo.hz != 0)
184 clockinfo.profhz = clockinfo.hz;
185 else
186 clockinfo.profhz = hertz();
187 }
188
189 moncontrol(0);
190
191 if ((profdir = getenv("PROFDIR")) != NULL) {
192 extern char *__progname;
193 char *s, *t;
194 pid_t pid;
195 long divisor;
196
197 /* If PROFDIR contains a null value, no profiling
198 output is produced */
199 if (*profdir == '\0') {
200 return;
201 }
202
203 t = buf;
204 s = profdir;
205 while ((*t = *s) != '\0') {
206 if (len-- == 0) {
207 warnx("_mcleanup: internal buffer overflow, PROFDIR too long");
208 return;
209 }
210 t++;
211 s++;
212 }
213 *t++ = '/';
214
215 /*
216 * Copy and convert pid from a pid_t to a string. For
217 * best performance, divisor should be initialized to
218 * the largest power of 10 less than PID_MAX.
219 */
220 pid = getpid();
221 divisor=10000;
222 while (divisor > pid) divisor /= 10; /* skip leading zeros */
223 do {
224 *t++ = (char)((pid/divisor) + '0');
225 pid %= (pid_t)divisor;
226 } while (divisor /= 10);
227 *t++ = '.';
228
229 s = __progname;
230 while ((*t++ = *s++) != '\0')
231 ;
232
233 proffile = buf;
234 } else {
235 proffile = "gmon.out";
236 }
237
238 fd = open(proffile , O_CREAT|O_TRUNC|O_WRONLY, 0666);
239 if (fd < 0) {
240 warn("mcount: Cannot open `%s'", proffile);
241 return;
242 }
243 #ifdef DEBUG
244 log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
245 if (log < 0) {
246 warn("mcount: Cannot open `gmon.log'");
247 return;
248 }
249 len = snprintf(buf2, sizeof buf2, "[mcleanup1] kcount 0x%x ssiz %d\n",
250 p->kcount, p->kcountsize);
251 write(log, buf2, len);
252 #endif
253 hdr = (struct gmonhdr *)&gmonhdr;
254 hdr->lpc = p->lowpc;
255 hdr->hpc = p->highpc;
256 hdr->ncnt = (int)(p->kcountsize + sizeof(gmonhdr));
257 hdr->version = GMONVERSION;
258 hdr->profrate = clockinfo.profhz;
259 (void)write(fd, hdr, sizeof *hdr);
260 (void)write(fd, p->kcount, (size_t)p->kcountsize);
261 endfrom = (int)(p->fromssize / sizeof(*p->froms));
262 for (fromindex = 0; fromindex < endfrom; fromindex++) {
263 if (p->froms[fromindex] == 0)
264 continue;
265
266 frompc = p->lowpc;
267 frompc += fromindex * p->hashfraction * sizeof(*p->froms);
268 for (toindex = p->froms[fromindex]; toindex != 0;
269 toindex = p->tos[toindex].link) {
270 #ifdef DEBUG
271 len = snprintf(buf2, sizeof buf2,
272 "[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
273 frompc, p->tos[toindex].selfpc,
274 p->tos[toindex].count);
275 write(log, buf2, len);
276 #endif
277 rawarc.raw_frompc = frompc;
278 rawarc.raw_selfpc = p->tos[toindex].selfpc;
279 rawarc.raw_count = p->tos[toindex].count;
280 write(fd, &rawarc, sizeof rawarc);
281 }
282 }
283 close(fd);
284 }
285
286 /*
287 * Control profiling
288 * profiling is what mcount checks to see if
289 * all the data structures are ready.
290 */
291 void
292 moncontrol(mode)
293 int mode;
294 {
295 struct gmonparam *p = &_gmonparam;
296
297 if (mode) {
298 /* start */
299 profil((char *)(void *)p->kcount, (size_t)p->kcountsize,
300 p->lowpc, s_scale);
301 p->state = GMON_PROF_ON;
302 } else {
303 /* stop */
304 profil(NULL, 0, (u_long)0, 0);
305 p->state = GMON_PROF_OFF;
306 }
307 }
308
309 /*
310 * discover the tick frequency of the machine
311 * if something goes wrong, we return 0, an impossible hertz.
312 */
313 static int
314 hertz()
315 {
316 struct itimerval tim;
317
318 tim.it_interval.tv_sec = 0;
319 tim.it_interval.tv_usec = 1;
320 tim.it_value.tv_sec = 0;
321 tim.it_value.tv_usec = 0;
322 setitimer(ITIMER_REAL, &tim, 0);
323 setitimer(ITIMER_REAL, 0, &tim);
324 if (tim.it_interval.tv_usec < 2)
325 return(0);
326 return (int)(1000000 / tim.it_interval.tv_usec);
327 }
328