gmon.c revision 1.1.1.1 1 1.1 cgd /*-
2 1.1 cgd * Copyright (c) 1983, 1992, 1993
3 1.1 cgd * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #if !defined(lint) && defined(LIBC_SCCS)
35 1.1 cgd static char sccsid[] = "@(#)gmon.c 8.1 (Berkeley) 6/4/93";
36 1.1 cgd #endif
37 1.1 cgd
38 1.1 cgd #include <sys/param.h>
39 1.1 cgd #include <sys/time.h>
40 1.1 cgd #include <sys/gmon.h>
41 1.1 cgd #include <sys/sysctl.h>
42 1.1 cgd
43 1.1 cgd #include <stdio.h>
44 1.1 cgd #include <fcntl.h>
45 1.1 cgd #include <unistd.h>
46 1.1 cgd
47 1.1 cgd extern char *minbrk asm ("minbrk");
48 1.1 cgd
49 1.1 cgd struct gmonparam _gmonparam = { GMON_PROF_OFF };
50 1.1 cgd
51 1.1 cgd static int s_scale;
52 1.1 cgd /* see profil(2) where this is describe (incorrectly) */
53 1.1 cgd #define SCALE_1_TO_1 0x10000L
54 1.1 cgd
55 1.1 cgd #define ERR(s) write(2, s, sizeof(s))
56 1.1 cgd
57 1.1 cgd void moncontrol __P((int));
58 1.1 cgd static int hertz __P((void));
59 1.1 cgd
60 1.1 cgd void
61 1.1 cgd monstartup(lowpc, highpc)
62 1.1 cgd u_long lowpc;
63 1.1 cgd u_long highpc;
64 1.1 cgd {
65 1.1 cgd register int o;
66 1.1 cgd char *cp;
67 1.1 cgd struct gmonparam *p = &_gmonparam;
68 1.1 cgd
69 1.1 cgd /*
70 1.1 cgd * round lowpc and highpc to multiples of the density we're using
71 1.1 cgd * so the rest of the scaling (here and in gprof) stays in ints.
72 1.1 cgd */
73 1.1 cgd p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
74 1.1 cgd p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
75 1.1 cgd p->textsize = p->highpc - p->lowpc;
76 1.1 cgd p->kcountsize = p->textsize / HISTFRACTION;
77 1.1 cgd p->hashfraction = HASHFRACTION;
78 1.1 cgd p->fromssize = p->textsize / HASHFRACTION;
79 1.1 cgd p->tolimit = p->textsize * ARCDENSITY / 100;
80 1.1 cgd if (p->tolimit < MINARCS)
81 1.1 cgd p->tolimit = MINARCS;
82 1.1 cgd else if (p->tolimit > MAXARCS)
83 1.1 cgd p->tolimit = MAXARCS;
84 1.1 cgd p->tossize = p->tolimit * sizeof(struct tostruct);
85 1.1 cgd
86 1.1 cgd cp = sbrk(p->kcountsize + p->fromssize + p->tossize);
87 1.1 cgd if (cp == (char *)-1) {
88 1.1 cgd ERR("monstartup: out of memory\n");
89 1.1 cgd return;
90 1.1 cgd }
91 1.1 cgd #ifdef notdef
92 1.1 cgd bzero(cp, p->kcountsize + p->fromssize + p->tossize);
93 1.1 cgd #endif
94 1.1 cgd p->tos = (struct tostruct *)cp;
95 1.1 cgd cp += p->tossize;
96 1.1 cgd p->kcount = (u_short *)cp;
97 1.1 cgd cp += p->kcountsize;
98 1.1 cgd p->froms = (u_short *)cp;
99 1.1 cgd
100 1.1 cgd minbrk = sbrk(0);
101 1.1 cgd p->tos[0].link = 0;
102 1.1 cgd
103 1.1 cgd o = p->highpc - p->lowpc;
104 1.1 cgd if (p->kcountsize < o) {
105 1.1 cgd #ifndef hp300
106 1.1 cgd s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
107 1.1 cgd #else /* avoid floating point */
108 1.1 cgd int quot = o / p->kcountsize;
109 1.1 cgd
110 1.1 cgd if (quot >= 0x10000)
111 1.1 cgd s_scale = 1;
112 1.1 cgd else if (quot >= 0x100)
113 1.1 cgd s_scale = 0x10000 / quot;
114 1.1 cgd else if (o >= 0x800000)
115 1.1 cgd s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
116 1.1 cgd else
117 1.1 cgd s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
118 1.1 cgd #endif
119 1.1 cgd } else
120 1.1 cgd s_scale = SCALE_1_TO_1;
121 1.1 cgd
122 1.1 cgd moncontrol(1);
123 1.1 cgd }
124 1.1 cgd
125 1.1 cgd void
126 1.1 cgd _mcleanup()
127 1.1 cgd {
128 1.1 cgd int fd;
129 1.1 cgd int fromindex;
130 1.1 cgd int endfrom;
131 1.1 cgd u_long frompc;
132 1.1 cgd int toindex;
133 1.1 cgd struct rawarc rawarc;
134 1.1 cgd struct gmonparam *p = &_gmonparam;
135 1.1 cgd struct gmonhdr gmonhdr, *hdr;
136 1.1 cgd struct clockinfo clockinfo;
137 1.1 cgd int mib[2];
138 1.1 cgd size_t size;
139 1.1 cgd #ifdef DEBUG
140 1.1 cgd int log, len;
141 1.1 cgd char buf[200];
142 1.1 cgd #endif
143 1.1 cgd
144 1.1 cgd if (p->state == GMON_PROF_ERROR)
145 1.1 cgd ERR("_mcleanup: tos overflow\n");
146 1.1 cgd
147 1.1 cgd size = sizeof(clockinfo);
148 1.1 cgd mib[0] = CTL_KERN;
149 1.1 cgd mib[1] = KERN_CLOCKRATE;
150 1.1 cgd if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) < 0) {
151 1.1 cgd /*
152 1.1 cgd * Best guess
153 1.1 cgd */
154 1.1 cgd clockinfo.profhz = hertz();
155 1.1 cgd } else if (clockinfo.profhz == 0) {
156 1.1 cgd if (clockinfo.hz != 0)
157 1.1 cgd clockinfo.profhz = clockinfo.hz;
158 1.1 cgd else
159 1.1 cgd clockinfo.profhz = hertz();
160 1.1 cgd }
161 1.1 cgd
162 1.1 cgd moncontrol(0);
163 1.1 cgd fd = open("gmon.out", O_CREAT|O_TRUNC|O_WRONLY, 0666);
164 1.1 cgd if (fd < 0) {
165 1.1 cgd perror("mcount: gmon.out");
166 1.1 cgd return;
167 1.1 cgd }
168 1.1 cgd #ifdef DEBUG
169 1.1 cgd log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
170 1.1 cgd if (log < 0) {
171 1.1 cgd perror("mcount: gmon.log");
172 1.1 cgd return;
173 1.1 cgd }
174 1.1 cgd len = sprintf(buf, "[mcleanup1] kcount 0x%x ssiz %d\n",
175 1.1 cgd p->kcount, p->kcountsize);
176 1.1 cgd write(log, buf, len);
177 1.1 cgd #endif
178 1.1 cgd hdr = (struct gmonhdr *)&gmonhdr;
179 1.1 cgd hdr->lpc = p->lowpc;
180 1.1 cgd hdr->hpc = p->highpc;
181 1.1 cgd hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
182 1.1 cgd hdr->version = GMONVERSION;
183 1.1 cgd hdr->profrate = clockinfo.profhz;
184 1.1 cgd write(fd, (char *)hdr, sizeof *hdr);
185 1.1 cgd write(fd, p->kcount, p->kcountsize);
186 1.1 cgd endfrom = p->fromssize / sizeof(*p->froms);
187 1.1 cgd for (fromindex = 0; fromindex < endfrom; fromindex++) {
188 1.1 cgd if (p->froms[fromindex] == 0)
189 1.1 cgd continue;
190 1.1 cgd
191 1.1 cgd frompc = p->lowpc;
192 1.1 cgd frompc += fromindex * p->hashfraction * sizeof(*p->froms);
193 1.1 cgd for (toindex = p->froms[fromindex]; toindex != 0;
194 1.1 cgd toindex = p->tos[toindex].link) {
195 1.1 cgd #ifdef DEBUG
196 1.1 cgd len = sprintf(buf,
197 1.1 cgd "[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
198 1.1 cgd frompc, p->tos[toindex].selfpc,
199 1.1 cgd p->tos[toindex].count);
200 1.1 cgd write(log, buf, len);
201 1.1 cgd #endif
202 1.1 cgd rawarc.raw_frompc = frompc;
203 1.1 cgd rawarc.raw_selfpc = p->tos[toindex].selfpc;
204 1.1 cgd rawarc.raw_count = p->tos[toindex].count;
205 1.1 cgd write(fd, &rawarc, sizeof rawarc);
206 1.1 cgd }
207 1.1 cgd }
208 1.1 cgd close(fd);
209 1.1 cgd }
210 1.1 cgd
211 1.1 cgd /*
212 1.1 cgd * Control profiling
213 1.1 cgd * profiling is what mcount checks to see if
214 1.1 cgd * all the data structures are ready.
215 1.1 cgd */
216 1.1 cgd void
217 1.1 cgd moncontrol(mode)
218 1.1 cgd int mode;
219 1.1 cgd {
220 1.1 cgd struct gmonparam *p = &_gmonparam;
221 1.1 cgd
222 1.1 cgd if (mode) {
223 1.1 cgd /* start */
224 1.1 cgd profil((char *)p->kcount, p->kcountsize, (int)p->lowpc,
225 1.1 cgd s_scale);
226 1.1 cgd p->state = GMON_PROF_ON;
227 1.1 cgd } else {
228 1.1 cgd /* stop */
229 1.1 cgd profil((char *)0, 0, 0, 0);
230 1.1 cgd p->state = GMON_PROF_OFF;
231 1.1 cgd }
232 1.1 cgd }
233 1.1 cgd
234 1.1 cgd /*
235 1.1 cgd * discover the tick frequency of the machine
236 1.1 cgd * if something goes wrong, we return 0, an impossible hertz.
237 1.1 cgd */
238 1.1 cgd static int
239 1.1 cgd hertz()
240 1.1 cgd {
241 1.1 cgd struct itimerval tim;
242 1.1 cgd
243 1.1 cgd tim.it_interval.tv_sec = 0;
244 1.1 cgd tim.it_interval.tv_usec = 1;
245 1.1 cgd tim.it_value.tv_sec = 0;
246 1.1 cgd tim.it_value.tv_usec = 0;
247 1.1 cgd setitimer(ITIMER_REAL, &tim, 0);
248 1.1 cgd setitimer(ITIMER_REAL, 0, &tim);
249 1.1 cgd if (tim.it_interval.tv_usec < 2)
250 1.1 cgd return(0);
251 1.1 cgd return (1000000 / tim.it_interval.tv_usec);
252 1.1 cgd }
253 1.1 cgd
254 1.1 cgd
255