(+cflags): Append to this instead of CFLAGS.
[glibc.git] / gmon / gmon.c
blob342801651f6727b5c5fae2c23f5fb8fb0f9f677b
1 /*-
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
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.
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.
34 #if !defined(lint) && defined(LIBC_SCCS)
35 static char sccsid[] = "@(#)gmon.c 8.1 (Berkeley) 6/4/93";
36 #endif
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/gmon.h>
42 #include <stdio.h>
43 #include <fcntl.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
48 struct gmonparam _gmonparam = { GMON_PROF_OFF };
50 static int s_scale;
51 /* see profil(2) where this is describe (incorrectly) */
52 #define SCALE_1_TO_1 0x10000L
54 #define ERR(s) write(2, s, sizeof(s) - 1)
56 void moncontrol __P((int));
57 static int hertz __P((void));
59 void
60 monstartup(lowpc, highpc)
61 u_long lowpc;
62 u_long highpc;
64 register int o;
65 char *cp;
66 struct gmonparam *p = &_gmonparam;
69 * round lowpc and highpc to multiples of the density we're using
70 * so the rest of the scaling (here and in gprof) stays in ints.
72 p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
73 p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
74 p->textsize = p->highpc - p->lowpc;
75 p->kcountsize = p->textsize / HISTFRACTION;
76 p->hashfraction = HASHFRACTION;
77 p->fromssize = p->textsize / HASHFRACTION;
78 p->tolimit = p->textsize * ARCDENSITY / 100;
79 if (p->tolimit < MINARCS)
80 p->tolimit = MINARCS;
81 else if (p->tolimit > MAXARCS)
82 p->tolimit = MAXARCS;
83 p->tossize = p->tolimit * sizeof(struct tostruct);
85 cp = malloc (p->kcountsize + p->fromssize + p->tossize);
86 if (! cp) {
87 ERR("monstartup: out of memory\n");
88 return;
90 bzero(cp, p->kcountsize + p->fromssize + p->tossize);
91 p->tos = (struct tostruct *)cp;
92 cp += p->tossize;
93 p->kcount = (u_short *)cp;
94 cp += p->kcountsize;
95 p->froms = (u_short *)cp;
97 p->tos[0].link = 0;
99 o = p->highpc - p->lowpc;
100 if (p->kcountsize < o) {
101 #ifndef hp300
102 s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
103 #else /* avoid floating point */
104 int quot = o / p->kcountsize;
106 if (quot >= 0x10000)
107 s_scale = 1;
108 else if (quot >= 0x100)
109 s_scale = 0x10000 / quot;
110 else if (o >= 0x800000)
111 s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
112 else
113 s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
114 #endif
115 } else
116 s_scale = SCALE_1_TO_1;
118 moncontrol(1);
121 void
122 _mcleanup()
124 int fd;
125 int fromindex;
126 int endfrom;
127 u_long frompc;
128 int toindex;
129 struct rawarc rawarc;
130 struct gmonparam *p = &_gmonparam;
131 struct gmonhdr gmonhdr, *hdr;
132 #ifdef DEBUG
133 int log, len;
134 char buf[200];
135 #endif
137 if (p->state == GMON_PROF_ERROR)
138 ERR("_mcleanup: tos overflow\n");
140 moncontrol(0);
141 fd = open("gmon.out", O_CREAT|O_TRUNC|O_WRONLY, 0666);
142 if (fd < 0) {
143 perror("mcount: gmon.out");
144 return;
146 #ifdef DEBUG
147 log = open("gmon.log", O_CREAT|O_TRUNC|O_WRONLY, 0664);
148 if (log < 0) {
149 perror("mcount: gmon.log");
150 return;
152 len = sprintf(buf, "[mcleanup1] kcount 0x%x ssiz %d\n",
153 p->kcount, p->kcountsize);
154 write(log, buf, len);
155 #endif
156 hdr = (struct gmonhdr *)&gmonhdr;
157 hdr->lpc = p->lowpc;
158 hdr->hpc = p->highpc;
159 hdr->ncnt = p->kcountsize + sizeof(gmonhdr);
160 hdr->version = GMONVERSION;
161 hdr->profrate = hertz();
162 write(fd, (char *)hdr, sizeof *hdr);
163 write(fd, p->kcount, p->kcountsize);
164 endfrom = p->fromssize / sizeof(*p->froms);
165 for (fromindex = 0; fromindex < endfrom; fromindex++) {
166 if (p->froms[fromindex] == 0)
167 continue;
169 frompc = p->lowpc;
170 frompc += fromindex * p->hashfraction * sizeof(*p->froms);
171 for (toindex = p->froms[fromindex]; toindex != 0;
172 toindex = p->tos[toindex].link) {
173 #ifdef DEBUG
174 len = sprintf(buf,
175 "[mcleanup2] frompc 0x%x selfpc 0x%x count %d\n" ,
176 frompc, p->tos[toindex].selfpc,
177 p->tos[toindex].count);
178 write(log, buf, len);
179 #endif
180 rawarc.raw_frompc = frompc;
181 rawarc.raw_selfpc = p->tos[toindex].selfpc;
182 rawarc.raw_count = p->tos[toindex].count;
183 write(fd, &rawarc, sizeof rawarc);
186 close(fd);
190 * Control profiling
191 * profiling is what mcount checks to see if
192 * all the data structures are ready.
194 void
195 moncontrol(mode)
196 int mode;
198 struct gmonparam *p = &_gmonparam;
200 if (mode) {
201 /* start */
202 profil(p->kcount, p->kcountsize, (int)p->lowpc,
203 s_scale);
204 p->state = GMON_PROF_ON;
205 } else {
206 /* stop */
207 profil(0, 0, 0, 0);
208 p->state = GMON_PROF_OFF;
213 * discover the tick frequency of the machine
214 * if something goes wrong, we return 0, an impossible hertz.
216 static int
217 hertz()
219 struct itimerval tim;
221 tim.it_interval.tv_sec = 0;
222 tim.it_interval.tv_usec = 1;
223 tim.it_value.tv_sec = 0;
224 tim.it_value.tv_usec = 0;
225 setitimer(ITIMER_REAL, &tim, 0);
226 setitimer(ITIMER_REAL, 0, &tim);
227 if (tim.it_interval.tv_usec < 2)
228 return(0);
229 return (1000000 / tim.it_interval.tv_usec);