gmon.c revision 1.2 1 /*-
2 * Copyright (c) 1983, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34 #if !defined(lint) && defined(LIBC_SCCS)
35 /* from: static char sccsid[] = "@(#)gmon.c 8.1 (Berkeley) 6/4/93"; */
36 static char *rcsid = "$Id: gmon.c,v 1.2 1994/05/14 06:31:15 cgd Exp $";
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/gmon.h>
42 #include <sys/sysctl.h>
43
44 #include <stdio.h>
45 #include <fcntl.h>
46 #include <unistd.h>
47
48 extern char *minbrk asm ("minbrk");
49
50 struct gmonparam _gmonparam = { GMON_PROF_OFF };
51
52 static int s_scale;
53 /* see profil(2) where this is describe (incorrectly) */
54 #define SCALE_1_TO_1 0x10000L
55
56 #define ERR(s) write(2, s, sizeof(s))
57
58 void moncontrol __P((int));
59 static int hertz __P((void));
60
61 void
62 monstartup(lowpc, highpc)
63 u_long lowpc;
64 u_long highpc;
65 {
66 register int o;
67 char *cp;
68 struct gmonparam *p = &_gmonparam;
69
70 /*
71 * round lowpc and highpc to multiples of the density we're using
72 * so the rest of the scaling (here and in gprof) stays in ints.
73 */
74 p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
75 p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
76 p->textsize = p->highpc - p->lowpc;
77 p->kcountsize = p->textsize / HISTFRACTION;
78 p->hashfraction = HASHFRACTION;
79 p->fromssize = p->textsize / HASHFRACTION;
80 p->tolimit = p->textsize * ARCDENSITY / 100;
81 if (p->tolimit < MINARCS)
82 p->tolimit = MINARCS;
83 else if (p->tolimit > MAXARCS)
84 p->tolimit = MAXARCS;
85 p->tossize = p->tolimit * sizeof(struct tostruct);
86
87 cp = sbrk(p->kcountsize + p->fromssize + p->tossize);
88 if (cp == (char *)-1) {
89 ERR("monstartup: out of memory\n");
90 return;
91 }
92 #ifdef notdef
93 bzero(cp, p->kcountsize + p->fromssize + p->tossize);
94 #endif
95 p->tos = (struct tostruct *)cp;
96 cp += p->tossize;
97 p->kcount = (u_short *)cp;
98 cp += p->kcountsize;
99 p->froms = (u_short *)cp;
100
101 minbrk = sbrk(0);
102 p->tos[0].link = 0;
103
104 o = p->highpc - p->lowpc;
105 if (p->kcountsize < o) {
106 #ifndef notdef
107 s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
108 #else /* avoid floating point */
109 int quot = o / p->kcountsize;
110
111 if (quot >= 0x10000)
112 s_scale = 1;
113 else if (quot >= 0x100)
114 s_scale = 0x10000 / quot;
115 else if (o >= 0x800000)
116 s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
117 else
118 s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
119 #endif
120 } else
121 s_scale = SCALE_1_TO_1;
122
123 moncontrol(1);
124 }
125
126 void
127 _mcleanup()
128 {
129 int fd;
130 int fromindex;
131 int endfrom;
132 u_long frompc;
133 int toindex;
134 struct rawarc rawarc;
135 struct gmonparam *p = &_gmonparam;
136 struct gmonhdr gmonhdr, *hdr;
137 struct clockinfo clockinfo;
138 int mib[2];
139 size_t size;
140 #ifdef DEBUG
141 int log, len;
142 char buf[200];
143 #endif
144
145 if (p->state == GMON_PROF_ERROR)
146 ERR("_mcleanup: tos overflow\n");
147
148 size = sizeof(clockinfo);
149 mib[0] = CTL_KERN;
150 mib[1] = KERN_CLOCKRATE;
151 if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
152 /*
153 * Best guess
154 */
155 clockinfo.profhz = hertz();
156 } else if (clockinfo.profhz == 0) {
157 if (clockinfo.hz != 0)
158 clockinfo.profhz = clockinfo.hz;
159 else
160 clockinfo.profhz = hertz();
161 }
162
163 moncontrol(0);
164 fd = open("gmon.out", O_CREAT|O_TRUNC|O_WRONLY, 0666);
165 if (fd < 0) {
166 perror("mcount: gmon.out");
167 return;
168 }
169 #ifdef DEBUG
170 log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
171 if (log < 0) {
172 perror("mcount: gmon.log");
173 return;
174 }
175 len = sprintf(buf, "[mcleanup1] kcount 0x%x ssiz %d\n",
176 p->kcount, p->kcountsize);
177 write(log, buf, len);
178 #endif
179 hdr = (struct gmonhdr *)&gmonhdr;
180 hdr->lpc = p->lowpc;
181 hdr->hpc = p->highpc;
182 hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
183 hdr->version = GMONVERSION;
184 hdr->profrate = clockinfo.profhz;
185 write(fd, (char *)hdr, sizeof *hdr);
186 write(fd, p->kcount, p->kcountsize);
187 endfrom = p->fromssize / sizeof(*p->froms);
188 for (fromindex = 0; fromindex < endfrom; fromindex++) {
189 if (p->froms[fromindex] == 0)
190 continue;
191
192 frompc = p->lowpc;
193 frompc += fromindex * p->hashfraction * sizeof(*p->froms);
194 for (toindex = p->froms[fromindex]; toindex != 0;
195 toindex = p->tos[toindex].link) {
196 #ifdef DEBUG
197 len = sprintf(buf,
198 "[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
199 frompc, p->tos[toindex].selfpc,
200 p->tos[toindex].count);
201 write(log, buf, len);
202 #endif
203 rawarc.raw_frompc = frompc;
204 rawarc.raw_selfpc = p->tos[toindex].selfpc;
205 rawarc.raw_count = p->tos[toindex].count;
206 write(fd, &rawarc, sizeof rawarc);
207 }
208 }
209 close(fd);
210 }
211
212 /*
213 * Control profiling
214 * profiling is what mcount checks to see if
215 * all the data structures are ready.
216 */
217 void
218 moncontrol(mode)
219 int mode;
220 {
221 struct gmonparam *p = &_gmonparam;
222
223 if (mode) {
224 /* start */
225 profil((char *)p->kcount, p->kcountsize, (int)p->lowpc,
226 s_scale);
227 p->state = GMON_PROF_ON;
228 } else {
229 /* stop */
230 profil((char *)0, 0, 0, 0);
231 p->state = GMON_PROF_OFF;
232 }
233 }
234
235 /*
236 * discover the tick frequency of the machine
237 * if something goes wrong, we return 0, an impossible hertz.
238 */
239 static int
240 hertz()
241 {
242 struct itimerval tim;
243
244 tim.it_interval.tv_sec = 0;
245 tim.it_interval.tv_usec = 1;
246 tim.it_value.tv_sec = 0;
247 tim.it_value.tv_usec = 0;
248 setitimer(ITIMER_REAL, &tim, 0);
249 setitimer(ITIMER_REAL, 0, &tim);
250 if (tim.it_interval.tv_usec < 2)
251 return(0);
252 return (1000000 / tim.it_interval.tv_usec);
253 }
254
255
256