1 /* $NetBSD: gmon.c,v 1.29 2006/10/15 16:14:46 christos Exp $ */
4 * Copyright (c) 2003, 2004 Wasabi Systems, Inc.
7 * Written by Nathan J. Williams for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * Copyright (c) 1983, 1992, 1993
40 * The Regents of the University of California. All rights reserved.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 #include <sys/cdefs.h>
68 #if !defined(lint) && defined(LIBC_SCCS)
70 static char sccsid
[] = "@(#)gmon.c 8.1 (Berkeley) 6/4/93";
72 __RCSID("$NetBSD: gmon.c,v 1.29 2006/10/15 16:14:46 christos Exp $");
76 #include "namespace.h"
77 #include <sys/param.h>
81 #include <sys/sysctl.h>
91 #include "reentrant.h"
93 struct gmonparam _gmonparam
= { .state
= GMON_PROF_OFF
};
96 struct gmonparam
*_gmonfree
;
97 struct gmonparam
*_gmoninuse
;
98 mutex_t _gmonlock
= MUTEX_INITIALIZER
;
99 thread_key_t _gmonkey
;
100 struct gmonparam _gmondummy
;
103 static u_int s_scale
;
104 /* see profil(2) where this is describe (incorrectly) */
105 #define SCALE_1_TO_1 0x10000L
107 #define ERR(s) write(STDERR_FILENO, s, sizeof(s))
109 void moncontrol
__P((int));
110 void monstartup
__P((u_long
, u_long
));
111 void _mcleanup
__P((void));
112 static int hertz
__P((void));
115 static void _m_gmon_destructor(void *);
116 struct gmonparam
*_m_gmon_alloc(void) __attribute__((__no_instrument_function__
));
117 static void _m_gmon_merge(void);
118 static void _m_gmon_merge_two(struct gmonparam
*, struct gmonparam
*);
122 monstartup(lowpc
, highpc
)
128 struct gmonparam
*p
= &_gmonparam
;
131 * round lowpc and highpc to multiples of the density we're using
132 * so the rest of the scaling (here and in gprof) stays in ints.
134 p
->lowpc
= rounddown(lowpc
, HISTFRACTION
* sizeof(HISTCOUNTER
));
135 p
->highpc
= roundup(highpc
, HISTFRACTION
* sizeof(HISTCOUNTER
));
136 p
->textsize
= p
->highpc
- p
->lowpc
;
137 p
->kcountsize
= p
->textsize
/ HISTFRACTION
;
138 p
->hashfraction
= HASHFRACTION
;
139 p
->fromssize
= p
->textsize
/ p
->hashfraction
;
140 p
->tolimit
= p
->textsize
* ARCDENSITY
/ 100;
141 if (p
->tolimit
< MINARCS
)
142 p
->tolimit
= MINARCS
;
143 else if (p
->tolimit
> MAXARCS
)
144 p
->tolimit
= MAXARCS
;
145 p
->tossize
= p
->tolimit
* sizeof(struct tostruct
);
147 cp
= sbrk((intptr_t)(p
->kcountsize
+ p
->fromssize
+ p
->tossize
));
148 if (cp
== (char *)-1) {
149 ERR("monstartup: out of memory\n");
153 memset(cp
, 0, p
->kcountsize
+ p
->fromssize
+ p
->tossize
);
155 p
->tos
= (struct tostruct
*)(void *)cp
;
156 cp
+= (size_t)p
->tossize
;
157 p
->kcount
= (u_short
*)(void *)cp
;
158 cp
+= (size_t)p
->kcountsize
;
159 p
->froms
= (u_short
*)(void *)cp
;
161 __minbrk
= sbrk((intptr_t)0);
164 o
= p
->highpc
- p
->lowpc
;
165 if (p
->kcountsize
< o
) {
167 s_scale
= ((float)p
->kcountsize
/ o
) * SCALE_1_TO_1
;
168 #else /* avoid floating point */
169 u_long quot
= o
/ p
->kcountsize
;
173 else if (quot
>= 0x100)
174 s_scale
= 0x10000 / quot
;
175 else if (o
>= 0x800000)
176 s_scale
= 0x1000000 / (o
/ (p
->kcountsize
>> 8));
178 s_scale
= 0x1000000 / ((o
<< 8) / p
->kcountsize
);
181 s_scale
= SCALE_1_TO_1
;
184 _gmondummy
.state
= GMON_PROF_BUSY
;
185 thr_keycreate(&_gmonkey
, _m_gmon_destructor
);
192 _m_gmon_destructor(void *arg
)
194 struct gmonparam
*p
= arg
, *q
, **prev
;
196 if (p
== &_gmondummy
)
199 thr_setspecific(_gmonkey
, &_gmondummy
);
201 mutex_lock(&_gmonlock
);
202 /* XXX eww, linear list traversal. */
203 for (q
= _gmoninuse
, prev
= &_gmoninuse
;
205 prev
= (struct gmonparam
**)(void *)&q
->kcount
, /* XXX */
206 q
= (struct gmonparam
*)(void *)q
->kcount
) {
208 *prev
= (struct gmonparam
*)(void *)q
->kcount
;
210 p
->kcount
= (u_short
*)(void *)_gmonfree
;
212 mutex_unlock(&_gmonlock
);
214 thr_setspecific(_gmonkey
, NULL
);
223 mutex_lock(&_gmonlock
);
224 if (_gmonfree
!= NULL
) {
226 _gmonfree
= (struct gmonparam
*)(void *)p
->kcount
;
227 p
->kcount
= (u_short
*)(void *)_gmoninuse
;
230 mutex_unlock(&_gmonlock
);
232 (size_t)(sizeof (struct gmonparam
) +
233 _gmonparam
.fromssize
+ _gmonparam
.tossize
),
234 PROT_READ
|PROT_WRITE
, MAP_ANON
|MAP_PRIVATE
, -1, 0LL);
238 cp
+= sizeof (struct gmonparam
);
239 memset(cp
, 0, (size_t)(p
->fromssize
+ p
->tossize
));
240 p
->froms
= (u_short
*)(void *)cp
;
241 p
->tos
= (struct tostruct
*)(void *)(cp
+ p
->fromssize
);
242 mutex_lock(&_gmonlock
);
243 p
->kcount
= (u_short
*)(void *)_gmoninuse
;
246 mutex_unlock(&_gmonlock
);
247 thr_setspecific(_gmonkey
, p
);
253 _m_gmon_merge_two(struct gmonparam
*p
, struct gmonparam
*q
)
256 u_short
*frompcindex
, qtoindex
, toindex
;
260 struct tostruct
*top
;
262 endfrom
= (q
->fromssize
/ sizeof(*q
->froms
));
263 for (fromindex
= 0; fromindex
< endfrom
; fromindex
++) {
264 if (q
->froms
[fromindex
] == 0)
266 for (qtoindex
= q
->froms
[fromindex
]; qtoindex
!= 0;
267 qtoindex
= q
->tos
[qtoindex
].link
) {
268 selfpc
= q
->tos
[qtoindex
].selfpc
;
269 count
= q
->tos
[qtoindex
].count
;
270 /* cribbed from mcount */
271 frompcindex
= &p
->froms
[fromindex
];
272 toindex
= *frompcindex
;
275 * first time traversing this arc
277 toindex
= ++p
->tos
[0].link
;
278 if (toindex
>= p
->tolimit
)
279 /* halt further profiling */
282 *frompcindex
= (u_short
)toindex
;
283 top
= &p
->tos
[(size_t)toindex
];
284 top
->selfpc
= selfpc
;
289 top
= &p
->tos
[(size_t)toindex
];
290 if (top
->selfpc
== selfpc
) {
292 * arc at front of chain; usual case.
298 * have to go looking down chain for it.
299 * top points to what we are looking at,
300 * we know it is not at the head of the chain.
302 for (; /* goto done */; ) {
303 if (top
->link
== 0) {
305 * top is end of the chain and
306 * none of the chain had
307 * top->selfpc == selfpc. so
308 * we allocate a new tostruct
309 * and link it to the head of
312 toindex
= ++p
->tos
[0].link
;
313 if (toindex
>= p
->tolimit
)
316 top
= &p
->tos
[(size_t)toindex
];
317 top
->selfpc
= selfpc
;
319 top
->link
= *frompcindex
;
320 *frompcindex
= (u_short
)toindex
;
324 * otherwise, check the next arc on the chain.
326 top
= &p
->tos
[top
->link
];
327 if (top
->selfpc
== selfpc
) {
351 mutex_lock(&_gmonlock
);
353 for (q
= _gmonfree
; q
!= NULL
; q
= (struct gmonparam
*)(void *)q
->kcount
)
354 _m_gmon_merge_two(&_gmonparam
, q
);
356 for (q
= _gmoninuse
; q
!= NULL
; q
= (struct gmonparam
*)(void *)q
->kcount
) {
357 q
->state
= GMON_PROF_OFF
;
358 _m_gmon_merge_two(&_gmonparam
, q
);
361 mutex_unlock(&_gmonlock
);
373 struct rawarc rawarc
;
374 struct gmonparam
*p
= &_gmonparam
;
375 struct gmonhdr gmonhdr
, *hdr
;
376 struct clockinfo clockinfo
;
380 const char *proffile
;
388 * We disallow writing to the profiling file, if we are a
389 * set{u,g}id program and our effective {u,g}id does not match
392 if (issetugid() && (geteuid() != getuid() || getegid() != getgid())) {
393 warnx("mcount: Profiling of set{u,g}id binaries is not"
398 if (p
->state
== GMON_PROF_ERROR
)
399 ERR("_mcleanup: tos overflow\n");
401 size
= sizeof(clockinfo
);
403 mib
[1] = KERN_CLOCKRATE
;
404 if (sysctl(mib
, 2, &clockinfo
, &size
, NULL
, 0) < 0) {
408 clockinfo
.profhz
= hertz();
409 } else if (clockinfo
.profhz
== 0) {
410 if (clockinfo
.hz
!= 0)
411 clockinfo
.profhz
= clockinfo
.hz
;
413 clockinfo
.profhz
= hertz();
418 if ((profdir
= getenv("PROFDIR")) != NULL
) {
419 /* If PROFDIR contains a null value, no profiling
420 output is produced */
421 if (*profdir
== '\0')
424 if (snprintf(buf
, sizeof buf
, "%s/%d.%s",
425 profdir
, getpid(), getprogname()) >= (int)(sizeof buf
)) {
426 warnx("_mcleanup: internal buffer overflow, PROFDIR too long");
432 proffile
= "gmon.out";
435 fd
= open(proffile
, O_CREAT
|O_TRUNC
|O_WRONLY
, 0666);
437 warn("mcount: Cannot open `%s'", proffile
);
441 logfd
= open("gmon.log", O_CREAT
|O_TRUNC
|O_WRONLY
, 0664);
443 warn("mcount: Cannot open `gmon.log'");
446 len
= snprintf(buf2
, sizeof buf2
, "[mcleanup1] kcount %p ssiz %lu\n",
447 p
->kcount
, p
->kcountsize
);
448 (void)write(logfd
, buf2
, (size_t)len
);
453 hdr
= (struct gmonhdr
*)&gmonhdr
;
455 hdr
->hpc
= p
->highpc
;
456 hdr
->ncnt
= (int)(p
->kcountsize
+ sizeof(gmonhdr
));
457 hdr
->version
= GMONVERSION
;
458 hdr
->profrate
= clockinfo
.profhz
;
459 (void)write(fd
, hdr
, sizeof *hdr
);
460 (void)write(fd
, p
->kcount
, (size_t)p
->kcountsize
);
461 endfrom
= (int)(p
->fromssize
/ sizeof(*p
->froms
));
462 for (fromindex
= 0; fromindex
< endfrom
; fromindex
++) {
463 if (p
->froms
[fromindex
] == 0)
467 frompc
+= fromindex
* p
->hashfraction
* sizeof(*p
->froms
);
468 for (toindex
= p
->froms
[fromindex
]; toindex
!= 0;
469 toindex
= p
->tos
[toindex
].link
) {
471 len
= snprintf(buf2
, sizeof buf2
,
472 "[mcleanup2] frompc 0x%lx selfpc 0x%lx count %lu\n" ,
473 (u_long
)frompc
, (u_long
)p
->tos
[toindex
].selfpc
,
474 (u_long
)p
->tos
[toindex
].count
);
475 (void)write(logfd
, buf2
, (size_t)len
);
477 rawarc
.raw_frompc
= frompc
;
478 rawarc
.raw_selfpc
= p
->tos
[toindex
].selfpc
;
479 rawarc
.raw_count
= p
->tos
[toindex
].count
;
480 write(fd
, &rawarc
, sizeof rawarc
);
488 * profiling is what mcount checks to see if
489 * all the data structures are ready.
495 struct gmonparam
*p
= &_gmonparam
;
499 profil((char *)(void *)p
->kcount
, (size_t)p
->kcountsize
,
501 p
->state
= GMON_PROF_ON
;
504 profil(NULL
, 0, (u_long
)0, 0);
505 p
->state
= GMON_PROF_OFF
;
510 * discover the tick frequency of the machine
511 * if something goes wrong, we return 0, an impossible hertz.
516 struct itimerval tim
;
518 tim
.it_interval
.tv_sec
= 0;
519 tim
.it_interval
.tv_usec
= 1;
520 tim
.it_value
.tv_sec
= 0;
521 tim
.it_value
.tv_usec
= 0;
522 setitimer(ITIMER_REAL
, &tim
, 0);
523 setitimer(ITIMER_REAL
, 0, &tim
);
524 if (tim
.it_interval
.tv_usec
< 2)
526 return (int)(1000000 / tim
.it_interval
.tv_usec
);