4 * Copyright (c) 1991 The Regents of the University of California.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)gmon.h 5.2 (Berkeley) 5/6/91
38 #define GMON_OUT_44BSD /* use the 4.4BSD gmon.out format */
45 int version; /* version number */
46 int profrate; /* profiling clock rate */
47 char spare[3*4]; /* reserved */
48 #endif /* GMON_OUT_44BSD */
50 #define GMONVERSION 0x00051879
53 * histogram counters are unsigned shorts (according to the kernel).
55 #define HISTCOUNTER unsigned short
58 * fraction of text space to allocate for histogram counters
61 #define HISTFRACTION 2
64 * Fraction of text space to allocate for from hash buckets.
65 * The value of HASHFRACTION is based on the minimum number of bytes
66 * of separation between two subroutine call points in the object code.
67 * Given MIN_SUBR_SEPARATION bytes of separation the value of
68 * HASHFRACTION is calculated as:
70 * HASHFRACTION = MIN_SUBR_SEPARATION / (2 * sizeof(short) - 1);
72 * For the VAX, the shortest two call sequence is:
77 * which is separated by only three bytes, thus HASHFRACTION is
80 * HASHFRACTION = 3 / (2 * 2 - 1) = 1
82 * Note that the division above rounds down, thus if MIN_SUBR_FRACTION
83 * is less than three, this algorithm will not work!
85 #define HASHFRACTION 1
88 * percent of text space to allocate for tostructs
102 * with pointers to the calling site and the called site
106 unsigned long raw_frompc;
107 unsigned long raw_selfpc;
112 * general rounding functions.
114 #define ROUNDDOWN(x,y) (((x)/(y))*(y))
115 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
119 // adapted freely from gcc's gmon.c
122 // #include <fcntl.h>
123 // #include <errno.h>
129 // /* extern asm ("mcount"); */
130 // /* extern */ char *minbrk; /* asm ("_minbrk"); */
133 * froms is actually a bunch of unsigned shorts indexing tos
135 static int profiling = 3;
136 static unsigned short *froms;
137 static struct tostruct *tos = 0;
138 static long tolimit = 0;
139 static char *s_lowpc = 0;
140 static char *s_highpc = 0;
141 static unsigned long s_textsize = 0;
146 /* see profil(2) where this is describe (incorrectly) */
147 #define SCALE_1_TO_1 0x10000L
151 * profiling is what mcount checks to see if
152 * all the data structures are ready.
155 moncontrol( int mode )
159 static int warned = 0;
164 ret = profil(sbuf + sizeof(struct phdr), ssiz - sizeof(struct phdr),
165 (Address)s_lowpc, s_scale);
167 if (ret < 0 && errno == ENOSYS && !warned) {
168 # define PROFIL_WARN "warning: kernel does not support profiling\n"
169 write(2, PROFIL_WARN, sizeof(PROFIL_WARN));
176 profil((char *)0, 0, 0, 0);
181 #define MSG "No space for profiling buffer(s)\n"
184 monstartup( char *lowpc, char *highpc, unsigned prof_rate )
191 * round lowpc and highpc to multiples of the density we're using
192 * so the rest of the scaling (here and in gprof) stays in ints.
195 ROUNDDOWN((unsigned)lowpc, HISTFRACTION*sizeof(HISTCOUNTER));
198 ROUNDUP((unsigned)highpc, HISTFRACTION*sizeof(HISTCOUNTER));
200 s_textsize = highpc - lowpc;
201 monsize = (s_textsize / HISTFRACTION) + sizeof(struct phdr);
202 buffer = (char *) sbrk( monsize );
203 if ( buffer == (char *) -1 ) {
204 write( 2 , MSG , sizeof(MSG) );
207 froms = (unsigned short *) sbrk( s_textsize / HASHFRACTION );
208 if ( froms == (unsigned short *) -1 ) {
209 write( 2 , MSG , sizeof(MSG) );
213 tolimit = s_textsize * ARCDENSITY / 100;
214 if ( tolimit < MINARCS ) {
216 } else if ( tolimit > 65534 ) {
219 tos = (struct tostruct *) sbrk( tolimit * sizeof( struct tostruct ) );
220 if ( tos == (struct tostruct *) -1 ) {
221 write( 2 , MSG , sizeof(MSG) );
227 minbrk = (char *) sbrk(0);
232 ( (struct phdr *) buffer ) -> lpc = lowpc;
233 ( (struct phdr *) buffer ) -> hpc = highpc;
234 ( (struct phdr *) buffer ) -> ncnt = ssiz;
235 #ifdef GMON_OUT_44BSD
236 ( (struct phdr *) buffer ) -> version = GMONVERSION;
237 ( (struct phdr *) buffer ) -> profrate = prof_rate;
238 #endif // GMON_OUT_44BSD
239 monsize -= sizeof(struct phdr);
245 s_scale = (int)(( (float) monsize / o ) * SCALE_1_TO_1);
246 #else /* avoid floating point */
248 int quot = o / monsize;
252 else if (quot >= 0x100)
253 s_scale = 0x10000 / quot;
254 else if (o >= 0x800000)
255 s_scale = 0x1000000 / (o / (monsize >> 8));
257 s_scale = 0x1000000 / ((o << 8) / monsize);
261 s_scale = SCALE_1_TO_1;
273 struct rawarc rawarc;
276 fd = creat( "gmon.out" , 0666 );
278 perror( "mcount: gmon.out" );
282 fprintf( stderr , "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf , ssiz );
284 write( fd , sbuf , ssiz );
285 endfrom = s_textsize / (HASHFRACTION * sizeof(*froms));
286 for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) {
287 if ( froms[fromindex] == 0 ) {
290 frompc = s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms));
291 for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link) {
294 "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
295 frompc , tos[toindex].selfpc , tos[toindex].count );
297 rawarc.raw_frompc = (unsigned long) frompc;
298 rawarc.raw_selfpc = (unsigned long) tos[toindex].selfpc;
299 rawarc.raw_count = tos[toindex].count;
300 write( fd , (char *) &rawarc , sizeof rawarc );
306 sbrk_free(sbuf, ssiz);
307 sbrk_free(froms, s_textsize / HASHFRACTION);
308 sbrk_free(tos, tolimit * sizeof(struct tostruct));
313 __mcount_internal(unsigned short *frompcindex, char *selfpc )
315 register struct tostruct *top;
316 register struct tostruct *prevtop;
317 register long toindex;
320 register char *selfpc;
321 register unsigned short *frompcindex;
323 * find the return address for mcount,
324 * and the return address for mcount's caller.
327 /* selfpc = pc pushed by mcount call.
328 This identifies the function that was just entered. */
329 selfpc = (char *) __builtin_return_address (0);
330 /* frompcindex = pc in preceding frame.
331 This identifies the caller of the function just entered. */
332 frompcindex = (unsigned short *) __builtin_return_address (1);
336 * check that we are profiling
337 * and that we aren't recursively invoked.
344 * check that frompcindex is a reasonable pc value.
345 * for example: signal catchers get called from the stack,
346 * not from text space. too bad.
348 frompcindex = (unsigned short *)((long)frompcindex - (long)s_lowpc);
349 if ((unsigned long)frompcindex > s_textsize) {
353 &froms[((long)frompcindex) / (HASHFRACTION * sizeof(*froms))];
354 toindex = *frompcindex;
357 * first time traversing this arc
359 toindex = ++tos[0].link;
360 if (toindex >= tolimit) {
363 *frompcindex = toindex;
365 top->selfpc = selfpc;
371 if (top->selfpc == selfpc) {
373 * arc at front of chain; usual case.
379 * have to go looking down chain for it.
380 * top points to what we are looking at,
381 * prevtop points to previous top.
382 * we know it is not at the head of the chain.
384 for (; /* goto done */; ) {
385 if (top->link == 0) {
387 * top is end of the chain and none of the chain
388 * had top->selfpc == selfpc.
389 * so we allocate a new tostruct
390 * and link it to the head of the chain.
392 toindex = ++tos[0].link;
393 if (toindex >= tolimit) {
397 top->selfpc = selfpc;
399 top->link = *frompcindex;
400 *frompcindex = toindex;
404 * otherwise, check the next arc on the chain.
407 top = &tos[top->link];
408 if (top->selfpc == selfpc) {
411 * increment its count
412 * move it to the head of the chain.
415 toindex = prevtop->link;
416 prevtop->link = top->link;
417 top->link = *frompcindex;
418 *frompcindex = toindex;
425 /* and fall through */
427 return; /* normal return restores saved registers */
430 profiling++; /* halt further profiling */
431 # define TOLIMIT "mcount: tos overflow\n"
432 write(2, TOLIMIT, sizeof(TOLIMIT));