gmon.c revision 1.27 1 1.27 dogcow /* $NetBSD: gmon.c,v 1.27 2006/10/04 20:22:14 dogcow Exp $ */
2 1.22 thorpej
3 1.22 thorpej /*
4 1.22 thorpej * Copyright (c) 2003, 2004 Wasabi Systems, Inc.
5 1.22 thorpej * All rights reserved.
6 1.22 thorpej *
7 1.22 thorpej * Written by Nathan J. Williams for Wasabi Systems, Inc.
8 1.22 thorpej *
9 1.22 thorpej * Redistribution and use in source and binary forms, with or without
10 1.22 thorpej * modification, are permitted provided that the following conditions
11 1.22 thorpej * are met:
12 1.22 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.22 thorpej * notice, this list of conditions and the following disclaimer.
14 1.22 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.22 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.22 thorpej * documentation and/or other materials provided with the distribution.
17 1.22 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.22 thorpej * must display the following acknowledgement:
19 1.22 thorpej * This product includes software developed for the NetBSD Project by
20 1.22 thorpej * Wasabi Systems, Inc.
21 1.22 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.22 thorpej * or promote products derived from this software without specific prior
23 1.22 thorpej * written permission.
24 1.22 thorpej *
25 1.22 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.22 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.22 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.22 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.22 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.22 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.22 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.22 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.22 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.22 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.22 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.22 thorpej */
37 1.3 cgd
38 1.1 cgd /*-
39 1.1 cgd * Copyright (c) 1983, 1992, 1993
40 1.1 cgd * The Regents of the University of California. All rights reserved.
41 1.1 cgd *
42 1.1 cgd * Redistribution and use in source and binary forms, with or without
43 1.1 cgd * modification, are permitted provided that the following conditions
44 1.1 cgd * are met:
45 1.1 cgd * 1. Redistributions of source code must retain the above copyright
46 1.1 cgd * notice, this list of conditions and the following disclaimer.
47 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
48 1.1 cgd * notice, this list of conditions and the following disclaimer in the
49 1.1 cgd * documentation and/or other materials provided with the distribution.
50 1.21 agc * 3. Neither the name of the University nor the names of its contributors
51 1.1 cgd * may be used to endorse or promote products derived from this software
52 1.1 cgd * without specific prior written permission.
53 1.1 cgd *
54 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 1.1 cgd * SUCH DAMAGE.
65 1.1 cgd */
66 1.1 cgd
67 1.10 christos #include <sys/cdefs.h>
68 1.1 cgd #if !defined(lint) && defined(LIBC_SCCS)
69 1.3 cgd #if 0
70 1.3 cgd static char sccsid[] = "@(#)gmon.c 8.1 (Berkeley) 6/4/93";
71 1.3 cgd #else
72 1.27 dogcow __RCSID("$NetBSD: gmon.c,v 1.27 2006/10/04 20:22:14 dogcow Exp $");
73 1.3 cgd #endif
74 1.1 cgd #endif
75 1.1 cgd
76 1.10 christos #include "namespace.h"
77 1.1 cgd #include <sys/param.h>
78 1.1 cgd #include <sys/time.h>
79 1.1 cgd #include <sys/gmon.h>
80 1.22 thorpej #include <sys/mman.h>
81 1.27 dogcow #include <sys/param.h>
82 1.1 cgd #include <sys/sysctl.h>
83 1.1 cgd
84 1.1 cgd #include <stdio.h>
85 1.5 jtc #include <stdlib.h>
86 1.22 thorpej #include <string.h>
87 1.1 cgd #include <fcntl.h>
88 1.5 jtc #include <limits.h>
89 1.1 cgd #include <unistd.h>
90 1.10 christos #include <err.h>
91 1.15 kleink #include "extern.h"
92 1.22 thorpej #include "reentrant.h"
93 1.1 cgd
94 1.1 cgd struct gmonparam _gmonparam = { GMON_PROF_OFF };
95 1.1 cgd
96 1.22 thorpej #ifdef _REENTRANT
97 1.22 thorpej struct gmonparam *_gmonfree;
98 1.22 thorpej struct gmonparam *_gmoninuse;
99 1.22 thorpej mutex_t _gmonlock = MUTEX_INITIALIZER;
100 1.22 thorpej thread_key_t _gmonkey;
101 1.22 thorpej struct gmonparam _gmondummy;
102 1.22 thorpej #endif
103 1.22 thorpej
104 1.14 christos static u_int s_scale;
105 1.1 cgd /* see profil(2) where this is describe (incorrectly) */
106 1.1 cgd #define SCALE_1_TO_1 0x10000L
107 1.1 cgd
108 1.12 kleink #define ERR(s) write(STDERR_FILENO, s, sizeof(s))
109 1.1 cgd
110 1.1 cgd void moncontrol __P((int));
111 1.10 christos void monstartup __P((u_long, u_long));
112 1.10 christos void _mcleanup __P((void));
113 1.1 cgd static int hertz __P((void));
114 1.1 cgd
115 1.22 thorpej #ifdef _REENTRANT
116 1.22 thorpej static void _m_gmon_destructor(void *);
117 1.22 thorpej struct gmonparam *_m_gmon_alloc(void) __attribute__((__no_instrument_function__));
118 1.22 thorpej static void _m_gmon_merge(void);
119 1.22 thorpej static void _m_gmon_merge_two(struct gmonparam *, struct gmonparam *);
120 1.22 thorpej #endif
121 1.16 christos
122 1.1 cgd void
123 1.1 cgd monstartup(lowpc, highpc)
124 1.1 cgd u_long lowpc;
125 1.1 cgd u_long highpc;
126 1.1 cgd {
127 1.19 thorpej u_long o;
128 1.1 cgd char *cp;
129 1.1 cgd struct gmonparam *p = &_gmonparam;
130 1.1 cgd
131 1.1 cgd /*
132 1.1 cgd * round lowpc and highpc to multiples of the density we're using
133 1.1 cgd * so the rest of the scaling (here and in gprof) stays in ints.
134 1.1 cgd */
135 1.27 dogcow p->lowpc = rounddown(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
136 1.27 dogcow p->highpc = roundup(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
137 1.1 cgd p->textsize = p->highpc - p->lowpc;
138 1.1 cgd p->kcountsize = p->textsize / HISTFRACTION;
139 1.1 cgd p->hashfraction = HASHFRACTION;
140 1.6 cgd p->fromssize = p->textsize / p->hashfraction;
141 1.1 cgd p->tolimit = p->textsize * ARCDENSITY / 100;
142 1.1 cgd if (p->tolimit < MINARCS)
143 1.1 cgd p->tolimit = MINARCS;
144 1.1 cgd else if (p->tolimit > MAXARCS)
145 1.1 cgd p->tolimit = MAXARCS;
146 1.1 cgd p->tossize = p->tolimit * sizeof(struct tostruct);
147 1.1 cgd
148 1.16 christos cp = sbrk((intptr_t)(p->kcountsize + p->fromssize + p->tossize));
149 1.1 cgd if (cp == (char *)-1) {
150 1.1 cgd ERR("monstartup: out of memory\n");
151 1.1 cgd return;
152 1.1 cgd }
153 1.1 cgd #ifdef notdef
154 1.13 perry memset(cp, 0, p->kcountsize + p->fromssize + p->tossize);
155 1.1 cgd #endif
156 1.14 christos p->tos = (struct tostruct *)(void *)cp;
157 1.14 christos cp += (size_t)p->tossize;
158 1.14 christos p->kcount = (u_short *)(void *)cp;
159 1.14 christos cp += (size_t)p->kcountsize;
160 1.14 christos p->froms = (u_short *)(void *)cp;
161 1.1 cgd
162 1.16 christos __minbrk = sbrk((intptr_t)0);
163 1.1 cgd p->tos[0].link = 0;
164 1.1 cgd
165 1.1 cgd o = p->highpc - p->lowpc;
166 1.1 cgd if (p->kcountsize < o) {
167 1.2 cgd #ifndef notdef
168 1.1 cgd s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
169 1.1 cgd #else /* avoid floating point */
170 1.19 thorpej u_long quot = o / p->kcountsize;
171 1.1 cgd
172 1.1 cgd if (quot >= 0x10000)
173 1.1 cgd s_scale = 1;
174 1.1 cgd else if (quot >= 0x100)
175 1.1 cgd s_scale = 0x10000 / quot;
176 1.1 cgd else if (o >= 0x800000)
177 1.1 cgd s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
178 1.1 cgd else
179 1.1 cgd s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
180 1.1 cgd #endif
181 1.1 cgd } else
182 1.1 cgd s_scale = SCALE_1_TO_1;
183 1.1 cgd
184 1.22 thorpej #ifdef _REENTRANT
185 1.22 thorpej _gmondummy.state = GMON_PROF_BUSY;
186 1.22 thorpej thr_keycreate(&_gmonkey, _m_gmon_destructor);
187 1.22 thorpej #endif
188 1.1 cgd moncontrol(1);
189 1.1 cgd }
190 1.1 cgd
191 1.22 thorpej #ifdef _REENTRANT
192 1.22 thorpej static void
193 1.22 thorpej _m_gmon_destructor(void *arg)
194 1.22 thorpej {
195 1.22 thorpej struct gmonparam *p = arg, *q, **prev;
196 1.22 thorpej
197 1.22 thorpej if (p == &_gmondummy)
198 1.22 thorpej return;
199 1.22 thorpej
200 1.22 thorpej thr_setspecific(_gmonkey, &_gmondummy);
201 1.22 thorpej
202 1.22 thorpej mutex_lock(&_gmonlock);
203 1.22 thorpej /* XXX eww, linear list traversal. */
204 1.22 thorpej for (q = _gmoninuse, prev = &_gmoninuse;
205 1.22 thorpej q != NULL;
206 1.26 mrg prev = (struct gmonparam **)(void *)&q->kcount, /* XXX */
207 1.22 thorpej q = (struct gmonparam *)(void *)q->kcount) {
208 1.22 thorpej if (q == p)
209 1.22 thorpej *prev = (struct gmonparam *)(void *)q->kcount;
210 1.22 thorpej }
211 1.22 thorpej p->kcount = (u_short *)(void *)_gmonfree;
212 1.22 thorpej _gmonfree = p;
213 1.22 thorpej mutex_unlock(&_gmonlock);
214 1.22 thorpej
215 1.22 thorpej thr_setspecific(_gmonkey, NULL);
216 1.22 thorpej }
217 1.22 thorpej
218 1.22 thorpej struct gmonparam *
219 1.22 thorpej _m_gmon_alloc(void)
220 1.22 thorpej {
221 1.22 thorpej struct gmonparam *p;
222 1.22 thorpej char *cp;
223 1.22 thorpej
224 1.22 thorpej mutex_lock(&_gmonlock);
225 1.22 thorpej if (_gmonfree != NULL) {
226 1.22 thorpej p = _gmonfree;
227 1.22 thorpej _gmonfree = (struct gmonparam *)(void *)p->kcount;
228 1.22 thorpej p->kcount = (u_short *)(void *)_gmoninuse;
229 1.22 thorpej _gmoninuse = p;
230 1.22 thorpej } else {
231 1.22 thorpej mutex_unlock(&_gmonlock);
232 1.22 thorpej cp = mmap(NULL,
233 1.22 thorpej (size_t)(sizeof (struct gmonparam) +
234 1.22 thorpej _gmonparam.fromssize + _gmonparam.tossize),
235 1.22 thorpej PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0LL);
236 1.22 thorpej p = (void *)cp;
237 1.22 thorpej *p = _gmonparam;
238 1.22 thorpej p->kcount = NULL;
239 1.22 thorpej cp += sizeof (struct gmonparam);
240 1.22 thorpej memset(cp, 0, (size_t)(p->fromssize + p->tossize));
241 1.22 thorpej p->froms = (u_short *)(void *)cp;
242 1.22 thorpej p->tos = (struct tostruct *)(void *)(cp + p->fromssize);
243 1.22 thorpej mutex_lock(&_gmonlock);
244 1.22 thorpej p->kcount = (u_short *)(void *)_gmoninuse;
245 1.22 thorpej _gmoninuse = p;
246 1.22 thorpej }
247 1.22 thorpej mutex_unlock(&_gmonlock);
248 1.22 thorpej thr_setspecific(_gmonkey, p);
249 1.22 thorpej
250 1.22 thorpej return p;
251 1.22 thorpej }
252 1.22 thorpej
253 1.22 thorpej static void
254 1.22 thorpej _m_gmon_merge_two(struct gmonparam *p, struct gmonparam *q)
255 1.22 thorpej {
256 1.22 thorpej u_long fromindex;
257 1.22 thorpej u_short *frompcindex, qtoindex, toindex;
258 1.22 thorpej u_long selfpc;
259 1.22 thorpej int endfrom;
260 1.22 thorpej long count;
261 1.22 thorpej struct tostruct *top;
262 1.22 thorpej
263 1.22 thorpej endfrom = (int)(q->fromssize / sizeof(*q->froms));
264 1.22 thorpej for (fromindex = 0; fromindex < endfrom; fromindex++) {
265 1.22 thorpej if (q->froms[fromindex] == 0)
266 1.22 thorpej continue;
267 1.22 thorpej for (qtoindex = q->froms[fromindex]; qtoindex != 0;
268 1.22 thorpej qtoindex = q->tos[qtoindex].link) {
269 1.22 thorpej selfpc = q->tos[qtoindex].selfpc;
270 1.22 thorpej count = q->tos[qtoindex].count;
271 1.22 thorpej /* cribbed from mcount */
272 1.22 thorpej frompcindex = &p->froms[fromindex];
273 1.22 thorpej toindex = *frompcindex;
274 1.22 thorpej if (toindex == 0) {
275 1.22 thorpej /*
276 1.22 thorpej * first time traversing this arc
277 1.22 thorpej */
278 1.22 thorpej toindex = ++p->tos[0].link;
279 1.22 thorpej if (toindex >= p->tolimit)
280 1.22 thorpej /* halt further profiling */
281 1.22 thorpej goto overflow;
282 1.22 thorpej
283 1.22 thorpej *frompcindex = (u_short)toindex;
284 1.22 thorpej top = &p->tos[(size_t)toindex];
285 1.22 thorpej top->selfpc = selfpc;
286 1.22 thorpej top->count = count;
287 1.22 thorpej top->link = 0;
288 1.22 thorpej goto done;
289 1.22 thorpej }
290 1.22 thorpej top = &p->tos[(size_t)toindex];
291 1.22 thorpej if (top->selfpc == selfpc) {
292 1.22 thorpej /*
293 1.22 thorpej * arc at front of chain; usual case.
294 1.22 thorpej */
295 1.22 thorpej top->count+= count;
296 1.22 thorpej goto done;
297 1.22 thorpej }
298 1.22 thorpej /*
299 1.22 thorpej * have to go looking down chain for it.
300 1.22 thorpej * top points to what we are looking at,
301 1.22 thorpej * we know it is not at the head of the chain.
302 1.22 thorpej */
303 1.22 thorpej for (; /* goto done */; ) {
304 1.22 thorpej if (top->link == 0) {
305 1.22 thorpej /*
306 1.22 thorpej * top is end of the chain and
307 1.22 thorpej * none of the chain had
308 1.22 thorpej * top->selfpc == selfpc. so
309 1.22 thorpej * we allocate a new tostruct
310 1.22 thorpej * and link it to the head of
311 1.22 thorpej * the chain.
312 1.22 thorpej */
313 1.22 thorpej toindex = ++p->tos[0].link;
314 1.22 thorpej if (toindex >= p->tolimit)
315 1.22 thorpej goto overflow;
316 1.22 thorpej
317 1.22 thorpej top = &p->tos[(size_t)toindex];
318 1.22 thorpej top->selfpc = selfpc;
319 1.22 thorpej top->count = count;
320 1.22 thorpej top->link = *frompcindex;
321 1.22 thorpej *frompcindex = (u_short)toindex;
322 1.22 thorpej goto done;
323 1.22 thorpej }
324 1.22 thorpej /*
325 1.22 thorpej * otherwise, check the next arc on the chain.
326 1.22 thorpej */
327 1.22 thorpej top = &p->tos[top->link];
328 1.22 thorpej if (top->selfpc == selfpc) {
329 1.22 thorpej /*
330 1.22 thorpej * there it is.
331 1.22 thorpej * add to its count.
332 1.22 thorpej */
333 1.22 thorpej top->count += count;
334 1.22 thorpej goto done;
335 1.22 thorpej }
336 1.22 thorpej
337 1.22 thorpej }
338 1.22 thorpej
339 1.22 thorpej done: ;
340 1.22 thorpej }
341 1.22 thorpej
342 1.22 thorpej }
343 1.22 thorpej overflow: ;
344 1.22 thorpej
345 1.22 thorpej }
346 1.22 thorpej
347 1.22 thorpej static void
348 1.22 thorpej _m_gmon_merge(void)
349 1.22 thorpej {
350 1.22 thorpej struct gmonparam *q;
351 1.22 thorpej
352 1.22 thorpej mutex_lock(&_gmonlock);
353 1.22 thorpej
354 1.22 thorpej for (q = _gmonfree; q != NULL; q = (struct gmonparam *)(void *)q->kcount)
355 1.22 thorpej _m_gmon_merge_two(&_gmonparam, q);
356 1.22 thorpej
357 1.22 thorpej for (q = _gmoninuse; q != NULL; q = (struct gmonparam *)(void *)q->kcount) {
358 1.22 thorpej q->state = GMON_PROF_OFF;
359 1.22 thorpej _m_gmon_merge_two(&_gmonparam, q);
360 1.22 thorpej }
361 1.22 thorpej
362 1.22 thorpej mutex_unlock(&_gmonlock);
363 1.22 thorpej }
364 1.22 thorpej #endif
365 1.22 thorpej
366 1.1 cgd void
367 1.1 cgd _mcleanup()
368 1.1 cgd {
369 1.1 cgd int fd;
370 1.1 cgd int fromindex;
371 1.1 cgd int endfrom;
372 1.1 cgd u_long frompc;
373 1.1 cgd int toindex;
374 1.1 cgd struct rawarc rawarc;
375 1.1 cgd struct gmonparam *p = &_gmonparam;
376 1.1 cgd struct gmonhdr gmonhdr, *hdr;
377 1.1 cgd struct clockinfo clockinfo;
378 1.1 cgd int mib[2];
379 1.1 cgd size_t size;
380 1.5 jtc char *profdir;
381 1.23 christos const char *proffile;
382 1.5 jtc char buf[PATH_MAX];
383 1.1 cgd #ifdef DEBUG
384 1.25 christos int logfd, len;
385 1.9 mrg char buf2[200];
386 1.1 cgd #endif
387 1.18 christos
388 1.18 christos /*
389 1.18 christos * We disallow writing to the profiling file, if we are a
390 1.18 christos * set{u,g}id program and our effective {u,g}id does not match
391 1.18 christos * our real one.
392 1.18 christos */
393 1.18 christos if (issetugid() && (geteuid() != getuid() || getegid() != getgid())) {
394 1.18 christos warnx("mcount: Profiling of set{u,g}id binaries is not"
395 1.18 christos " allowed");
396 1.18 christos return;
397 1.18 christos }
398 1.1 cgd
399 1.1 cgd if (p->state == GMON_PROF_ERROR)
400 1.1 cgd ERR("_mcleanup: tos overflow\n");
401 1.1 cgd
402 1.1 cgd size = sizeof(clockinfo);
403 1.1 cgd mib[0] = CTL_KERN;
404 1.1 cgd mib[1] = KERN_CLOCKRATE;
405 1.1 cgd if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
406 1.1 cgd /*
407 1.1 cgd * Best guess
408 1.1 cgd */
409 1.1 cgd clockinfo.profhz = hertz();
410 1.1 cgd } else if (clockinfo.profhz == 0) {
411 1.1 cgd if (clockinfo.hz != 0)
412 1.1 cgd clockinfo.profhz = clockinfo.hz;
413 1.1 cgd else
414 1.1 cgd clockinfo.profhz = hertz();
415 1.1 cgd }
416 1.1 cgd
417 1.1 cgd moncontrol(0);
418 1.5 jtc
419 1.5 jtc if ((profdir = getenv("PROFDIR")) != NULL) {
420 1.5 jtc /* If PROFDIR contains a null value, no profiling
421 1.5 jtc output is produced */
422 1.20 dsl if (*profdir == '\0')
423 1.20 dsl return;
424 1.20 dsl
425 1.20 dsl if (snprintf(buf, sizeof buf, "%s/%d.%s",
426 1.20 dsl profdir, getpid(), getprogname()) >= sizeof buf) {
427 1.20 dsl warnx("_mcleanup: internal buffer overflow, PROFDIR too long");
428 1.5 jtc return;
429 1.5 jtc }
430 1.5 jtc
431 1.5 jtc proffile = buf;
432 1.5 jtc } else {
433 1.5 jtc proffile = "gmon.out";
434 1.5 jtc }
435 1.5 jtc
436 1.5 jtc fd = open(proffile , O_CREAT|O_TRUNC|O_WRONLY, 0666);
437 1.1 cgd if (fd < 0) {
438 1.10 christos warn("mcount: Cannot open `%s'", proffile);
439 1.1 cgd return;
440 1.1 cgd }
441 1.1 cgd #ifdef DEBUG
442 1.25 christos logfd = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
443 1.25 christos if (logfd < 0) {
444 1.10 christos warn("mcount: Cannot open `gmon.log'");
445 1.1 cgd return;
446 1.1 cgd }
447 1.25 christos len = snprintf(buf2, sizeof buf2, "[mcleanup1] kcount %p ssiz %lu\n",
448 1.1 cgd p->kcount, p->kcountsize);
449 1.25 christos (void)write(logfd, buf2, (size_t)len);
450 1.1 cgd #endif
451 1.22 thorpej #ifdef _REENTRANT
452 1.22 thorpej _m_gmon_merge();
453 1.22 thorpej #endif
454 1.1 cgd hdr = (struct gmonhdr *)&gmonhdr;
455 1.1 cgd hdr->lpc = p->lowpc;
456 1.1 cgd hdr->hpc = p->highpc;
457 1.14 christos hdr->ncnt = (int)(p->kcountsize + sizeof(gmonhdr));
458 1.1 cgd hdr->version = GMONVERSION;
459 1.1 cgd hdr->profrate = clockinfo.profhz;
460 1.14 christos (void)write(fd, hdr, sizeof *hdr);
461 1.14 christos (void)write(fd, p->kcount, (size_t)p->kcountsize);
462 1.14 christos endfrom = (int)(p->fromssize / sizeof(*p->froms));
463 1.1 cgd for (fromindex = 0; fromindex < endfrom; fromindex++) {
464 1.1 cgd if (p->froms[fromindex] == 0)
465 1.1 cgd continue;
466 1.1 cgd
467 1.1 cgd frompc = p->lowpc;
468 1.1 cgd frompc += fromindex * p->hashfraction * sizeof(*p->froms);
469 1.1 cgd for (toindex = p->froms[fromindex]; toindex != 0;
470 1.1 cgd toindex = p->tos[toindex].link) {
471 1.1 cgd #ifdef DEBUG
472 1.9 mrg len = snprintf(buf2, sizeof buf2,
473 1.25 christos "[mcleanup2] frompc 0x%lx selfpc 0x%lx count %lu\n" ,
474 1.25 christos (u_long)frompc, (u_long)p->tos[toindex].selfpc,
475 1.25 christos (u_long)p->tos[toindex].count);
476 1.25 christos (void)write(logfd, buf2, (size_t)len);
477 1.1 cgd #endif
478 1.1 cgd rawarc.raw_frompc = frompc;
479 1.1 cgd rawarc.raw_selfpc = p->tos[toindex].selfpc;
480 1.1 cgd rawarc.raw_count = p->tos[toindex].count;
481 1.1 cgd write(fd, &rawarc, sizeof rawarc);
482 1.1 cgd }
483 1.1 cgd }
484 1.1 cgd close(fd);
485 1.1 cgd }
486 1.1 cgd
487 1.1 cgd /*
488 1.1 cgd * Control profiling
489 1.1 cgd * profiling is what mcount checks to see if
490 1.1 cgd * all the data structures are ready.
491 1.1 cgd */
492 1.1 cgd void
493 1.1 cgd moncontrol(mode)
494 1.1 cgd int mode;
495 1.1 cgd {
496 1.1 cgd struct gmonparam *p = &_gmonparam;
497 1.1 cgd
498 1.1 cgd if (mode) {
499 1.1 cgd /* start */
500 1.14 christos profil((char *)(void *)p->kcount, (size_t)p->kcountsize,
501 1.14 christos p->lowpc, s_scale);
502 1.1 cgd p->state = GMON_PROF_ON;
503 1.1 cgd } else {
504 1.1 cgd /* stop */
505 1.14 christos profil(NULL, 0, (u_long)0, 0);
506 1.1 cgd p->state = GMON_PROF_OFF;
507 1.1 cgd }
508 1.1 cgd }
509 1.1 cgd
510 1.1 cgd /*
511 1.1 cgd * discover the tick frequency of the machine
512 1.1 cgd * if something goes wrong, we return 0, an impossible hertz.
513 1.1 cgd */
514 1.1 cgd static int
515 1.1 cgd hertz()
516 1.1 cgd {
517 1.1 cgd struct itimerval tim;
518 1.1 cgd
519 1.1 cgd tim.it_interval.tv_sec = 0;
520 1.1 cgd tim.it_interval.tv_usec = 1;
521 1.1 cgd tim.it_value.tv_sec = 0;
522 1.1 cgd tim.it_value.tv_usec = 0;
523 1.1 cgd setitimer(ITIMER_REAL, &tim, 0);
524 1.1 cgd setitimer(ITIMER_REAL, 0, &tim);
525 1.1 cgd if (tim.it_interval.tv_usec < 2)
526 1.1 cgd return(0);
527 1.14 christos return (int)(1000000 / tim.it_interval.tv_usec);
528 1.1 cgd }
529