2 * Copyright (c) 1983, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/lib/libc/gmon/gmon.c,v 1.8 2000/01/27 23:06:25 jasone Exp $
36 #if !defined(lint) && defined(LIBC_SCCS)
37 static char sccsid[] = "@(#)gmon.c 8.1 (Berkeley) 6/4/93";
40 #include <sys/param.h>
43 #include <sys/sysctl.h>
51 #if defined(__ELF__) && defined(i386)
52 extern char *minbrk asm (".minbrk");
54 extern char *minbrk asm ("minbrk");
57 extern char *__progname;
59 struct gmonparam _gmonparam = { GMON_PROF_OFF };
62 /* see profil(2) where this is describe (incorrectly) */
63 #define SCALE_1_TO_1 0x10000L
65 #define ERR(s) _write(2, s, sizeof(s))
67 void moncontrol __P((int));
68 static int hertz __P((void));
71 monstartup(lowpc, highpc)
77 struct gmonparam *p = &_gmonparam;
80 * round lowpc and highpc to multiples of the density we're using
81 * so the rest of the scaling (here and in gprof) stays in ints.
83 p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
84 p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
85 p->textsize = p->highpc - p->lowpc;
86 p->kcountsize = p->textsize / HISTFRACTION;
87 p->hashfraction = HASHFRACTION;
88 p->fromssize = p->textsize / HASHFRACTION;
89 p->tolimit = p->textsize * ARCDENSITY / 100;
90 if (p->tolimit < MINARCS)
92 else if (p->tolimit > MAXARCS)
94 p->tossize = p->tolimit * sizeof(struct tostruct);
96 cp = sbrk(p->kcountsize + p->fromssize + p->tossize);
97 if (cp == (char *)-1) {
98 ERR("monstartup: out of memory\n");
102 bzero(cp, p->kcountsize + p->fromssize + p->tossize);
104 p->tos = (struct tostruct *)cp;
106 p->kcount = (u_short *)cp;
108 p->froms = (u_short *)cp;
113 o = p->highpc - p->lowpc;
114 if (p->kcountsize < o) {
116 s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
117 #else /* avoid floating point */
118 int quot = o / p->kcountsize;
122 else if (quot >= 0x100)
123 s_scale = 0x10000 / quot;
124 else if (o >= 0x800000)
125 s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
127 s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
130 s_scale = SCALE_1_TO_1;
143 struct rawarc rawarc;
144 struct gmonparam *p = &_gmonparam;
145 struct gmonhdr gmonhdr, *hdr;
146 struct clockinfo clockinfo;
155 if (p->state == GMON_PROF_ERROR)
156 ERR("_mcleanup: tos overflow\n");
158 size = sizeof(clockinfo);
160 mib[1] = KERN_CLOCKRATE;
161 if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
165 clockinfo.profhz = hertz();
166 } else if (clockinfo.profhz == 0) {
167 if (clockinfo.hz != 0)
168 clockinfo.profhz = clockinfo.hz;
170 clockinfo.profhz = hertz();
174 snprintf(outname,sizeof(outname),"%s.gmon",__progname);
175 fd = _open(outname, O_CREAT|O_TRUNC|O_WRONLY, 0666);
177 warnx("_mcleanup: %s - %s",outname,strerror(errno));
181 log = _open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
183 perror("_mcleanup: gmon.log");
186 len = sprintf(buf, "[mcleanup1] kcount 0x%x ssiz %d\n",
187 p->kcount, p->kcountsize);
188 _write(log, buf, len);
190 hdr = (struct gmonhdr *)&gmonhdr;
192 hdr->hpc = p->highpc;
193 hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
194 hdr->version = GMONVERSION;
195 hdr->profrate = clockinfo.profhz;
196 _write(fd, (char *)hdr, sizeof *hdr);
197 _write(fd, p->kcount, p->kcountsize);
198 endfrom = p->fromssize / sizeof(*p->froms);
199 for (fromindex = 0; fromindex < endfrom; fromindex++) {
200 if (p->froms[fromindex] == 0)
204 frompc += fromindex * p->hashfraction * sizeof(*p->froms);
205 for (toindex = p->froms[fromindex]; toindex != 0;
206 toindex = p->tos[toindex].link) {
209 "[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
210 frompc, p->tos[toindex].selfpc,
211 p->tos[toindex].count);
212 _write(log, buf, len);
214 rawarc.raw_frompc = frompc;
215 rawarc.raw_selfpc = p->tos[toindex].selfpc;
216 rawarc.raw_count = p->tos[toindex].count;
217 _write(fd, &rawarc, sizeof rawarc);
225 * profiling is what mcount checks to see if
226 * all the data structures are ready.
232 struct gmonparam *p = &_gmonparam;
236 profil((char *)p->kcount, p->kcountsize, p->lowpc, s_scale);
237 p->state = GMON_PROF_ON;
240 profil((char *)0, 0, 0, 0);
241 p->state = GMON_PROF_OFF;
246 * discover the tick frequency of the machine
247 * if something goes wrong, we return 0, an impossible hertz.
252 struct itimerval tim;
254 tim.it_interval.tv_sec = 0;
255 tim.it_interval.tv_usec = 1;
256 tim.it_value.tv_sec = 0;
257 tim.it_value.tv_usec = 0;
258 setitimer(ITIMER_REAL, &tim, 0);
259 setitimer(ITIMER_REAL, 0, &tim);
260 if (tim.it_interval.tv_usec < 2)
262 return (1000000 / tim.it_interval.tv_usec);