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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)gmon.c 8.1 (Berkeley) 6/4/93";
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "namespace.h"
37 #include <sys/param.h>
40 #include <sys/sysctl.h>
49 #include "un-namespace.h"
51 #include "libc_private.h"
53 #if defined(__i386__) || defined(__sparc64__) || defined(__amd64__) || (defined(__powerpc__) && !defined(__powerpc64__))
54 extern char *minbrk __asm (".minbrk");
55 #elif defined(__powerpc64__)
56 extern char *minbrk __asm ("_minbrk");
58 extern char *minbrk __asm ("minbrk");
61 struct gmonparam _gmonparam = { GMON_PROF_OFF };
64 /* See profil(2) where this is described (incorrectly). */
65 #define SCALE_SHIFT 16
67 #define ERR(s) _write(2, s, sizeof(s))
70 static int hertz(void);
73 monstartup(lowpc, highpc)
79 struct gmonparam *p = &_gmonparam;
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.
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 / HASHFRACTION;
91 p->tolimit = p->textsize * ARCDENSITY / 100;
92 if (p->tolimit < MINARCS)
94 else if (p->tolimit > MAXARCS)
96 p->tossize = p->tolimit * sizeof(struct tostruct);
98 cp = sbrk(p->kcountsize + p->fromssize + p->tossize);
99 if (cp == (char *)-1) {
100 ERR("monstartup: out of memory\n");
104 bzero(cp, p->kcountsize + p->fromssize + p->tossize);
106 p->tos = (struct tostruct *)cp;
108 p->kcount = (u_short *)cp;
110 p->froms = (u_short *)cp;
115 o = p->highpc - p->lowpc;
116 s_scale = (p->kcountsize < o) ?
117 ((uintmax_t)p->kcountsize << SCALE_SHIFT) / o : (1 << SCALE_SHIFT);
129 struct rawarc rawarc;
130 struct gmonparam *p = &_gmonparam;
131 struct gmonhdr gmonhdr, *hdr;
132 struct clockinfo clockinfo;
141 if (p->state == GMON_PROF_ERROR)
142 ERR("_mcleanup: tos overflow\n");
144 size = sizeof(clockinfo);
146 mib[1] = KERN_CLOCKRATE;
147 if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
151 clockinfo.profhz = hertz();
152 } else if (clockinfo.profhz == 0) {
153 if (clockinfo.hz != 0)
154 clockinfo.profhz = clockinfo.hz;
156 clockinfo.profhz = hertz();
160 if (getenv("PROFIL_USE_PID"))
161 snprintf(outname, sizeof(outname), "%s.%d.gmon",
162 _getprogname(), getpid());
164 snprintf(outname, sizeof(outname), "%s.gmon", _getprogname());
166 fd = _open(outname, O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0666);
168 _warn("_mcleanup: %s", outname);
172 log = _open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0664);
174 _warn("_mcleanup: gmon.log");
177 len = sprintf(buf, "[mcleanup1] kcount 0x%p ssiz %lu\n",
178 p->kcount, p->kcountsize);
179 _write(log, buf, len);
181 hdr = (struct gmonhdr *)&gmonhdr;
182 bzero(hdr, sizeof(*hdr));
184 hdr->hpc = p->highpc;
185 hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
186 hdr->version = GMONVERSION;
187 hdr->profrate = clockinfo.profhz;
188 _write(fd, (char *)hdr, sizeof *hdr);
189 _write(fd, p->kcount, p->kcountsize);
190 endfrom = p->fromssize / sizeof(*p->froms);
191 for (fromindex = 0; fromindex < endfrom; fromindex++) {
192 if (p->froms[fromindex] == 0)
196 frompc += fromindex * p->hashfraction * sizeof(*p->froms);
197 for (toindex = p->froms[fromindex]; toindex != 0;
198 toindex = p->tos[toindex].link) {
201 "[mcleanup2] frompc 0x%lx selfpc 0x%lx count %lu\n" ,
202 frompc, p->tos[toindex].selfpc,
203 p->tos[toindex].count);
204 _write(log, buf, len);
206 rawarc.raw_frompc = frompc;
207 rawarc.raw_selfpc = p->tos[toindex].selfpc;
208 rawarc.raw_count = p->tos[toindex].count;
209 _write(fd, &rawarc, sizeof rawarc);
217 * profiling is what mcount checks to see if
218 * all the data structures are ready.
224 struct gmonparam *p = &_gmonparam;
228 profil((char *)p->kcount, p->kcountsize, p->lowpc, s_scale);
229 p->state = GMON_PROF_ON;
232 profil((char *)0, 0, 0, 0);
233 p->state = GMON_PROF_OFF;
238 * discover the tick frequency of the machine
239 * if something goes wrong, we return 0, an impossible hertz.
244 struct itimerval tim;
246 tim.it_interval.tv_sec = 0;
247 tim.it_interval.tv_usec = 1;
248 tim.it_value.tv_sec = 0;
249 tim.it_value.tv_usec = 0;
250 setitimer(ITIMER_REAL, &tim, 0);
251 setitimer(ITIMER_REAL, 0, &tim);
252 if (tim.it_interval.tv_usec < 2)
254 return (1000000 / tim.it_interval.tv_usec);