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