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
34 #if !defined(lint) && defined(LIBC_SCCS)
35 static char rcsid[] = "$OpenBSD: gmon.c,v 1.8 1997/07/23 21:11:27 kstailey Exp $";
48 //extern char *minbrk __asm ("minbrk");
50 struct gmonparam _gmonparam = { GMON_PROF_OFF };
53 /* see profil(2) where this is describe (incorrectly) */
54 #define SCALE_1_TO_1 0x10000L
56 #define ERR(s) write(2, s, sizeof(s))
58 void moncontrol __P((int));
63 void *rv = malloc(size);
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 / p->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 = fake_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;
110 /* XXX minbrk needed? */
111 //minbrk = fake_sbrk(0);
114 o = p->highpc - p->lowpc;
115 if (p->kcountsize < o) {
117 s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
118 #else /* avoid floating point */
119 int quot = o / p->kcountsize;
123 else if (quot >= 0x100)
124 s_scale = 0x10000 / quot;
125 else if (o >= 0x800000)
126 s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
128 s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
131 s_scale = SCALE_1_TO_1;
145 struct rawarc rawarc;
146 struct gmonparam *p = &_gmonparam;
147 struct gmonhdr gmonhdr, *hdr;
154 if (p->state == GMON_PROF_ERROR)
155 ERR("_mcleanup: tos overflow\n");
161 if ((profdir = getenv("PROFDIR")) != NULL) {
162 extern char *__progname;
167 /* If PROFDIR contains a null value, no profiling
168 output is produced */
169 if (*profdir == '\0') {
173 limit = buf + sizeof buf - 1 - 10 - 1 -
174 strlen(__progname) - 1;
177 while((*t = *s) != '\0' && t < limit) {
184 * Copy and convert pid from a pid_t to a string. For
185 * best performance, divisor should be initialized to
186 * the largest power of 10 less than PID_MAX.
190 while (divisor > pid) divisor /= 10; /* skip leading zeros */
192 *t++ = (pid/divisor) + '0';
194 } while (divisor /= 10);
198 while ((*t++ = *s++) != '\0')
203 proffile = "gmon.out";
207 char gmon_out[] = "gmon.out";
212 fd = open(proffile , O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0666);
218 log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
220 perror("mcount: gmon.log");
223 len = sprintf(dbuf, "[mcleanup1] kcount 0x%x ssiz %d\n",
224 p->kcount, p->kcountsize);
225 write(log, dbuf, len);
227 hdr = (struct gmonhdr *)&gmonhdr;
229 hdr->hpc = p->highpc;
230 hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
231 hdr->version = GMONVERSION;
233 write(fd, (char *)hdr, sizeof *hdr);
234 write(fd, p->kcount, p->kcountsize);
235 endfrom = p->fromssize / sizeof(*p->froms);
236 for (fromindex = 0; fromindex < endfrom; fromindex++) {
237 if (p->froms[fromindex] == 0)
241 frompc += fromindex * p->hashfraction * sizeof(*p->froms);
242 for (toindex = p->froms[fromindex]; toindex != 0;
243 toindex = p->tos[toindex].link) {
246 "[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
247 frompc, p->tos[toindex].selfpc,
248 p->tos[toindex].count);
249 write(log, dbuf, len);
251 rawarc.raw_frompc = frompc;
252 rawarc.raw_selfpc = p->tos[toindex].selfpc;
253 rawarc.raw_count = p->tos[toindex].count;
254 write(fd, &rawarc, sizeof rawarc);
262 * profiling is what mcount checks to see if
263 * all the data structures are ready.
269 struct gmonparam *p = &_gmonparam;
273 profil((char *)p->kcount, p->kcountsize, p->lowpc,
275 p->state = GMON_PROF_ON;
278 profil((char *)0, 0, 0, 0);
279 p->state = GMON_PROF_OFF;