]> rtime.felk.cvut.cz Git - l4.git/blob - kernel/fiasco/src/lib/libk/ia32/gmon.cpp
update
[l4.git] / kernel / fiasco / src / lib / libk / ia32 / gmon.cpp
1 INTERFACE:
2
3 /*-
4  * Copyright (c) 1991 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
22  *
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
33  * SUCH DAMAGE.
34  *
35  *      @(#)gmon.h      5.2 (Berkeley) 5/6/91
36  */
37
38 #define GMON_OUT_44BSD          /* use the 4.4BSD gmon.out format */
39
40 struct phdr {
41     char        *lpc;
42     char        *hpc;
43     int         ncnt;
44 #ifdef GMON_OUT_44BSD
45     int         version;                /* version number */
46     int         profrate;               /* profiling clock rate */
47     char        spare[3*4];             /* reserved */
48 #endif /* GMON_OUT_44BSD */
49 };
50 #define GMONVERSION     0x00051879
51
52     /*
53      *  histogram counters are unsigned shorts (according to the kernel).
54      */
55 #define HISTCOUNTER     unsigned short
56
57     /*
58      *  fraction of text space to allocate for histogram counters
59      *  here, 1/2
60      */
61 #define HISTFRACTION    2
62
63     /*
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:
69      *
70      *          HASHFRACTION = MIN_SUBR_SEPARATION / (2 * sizeof(short) - 1);
71      *
72      *  For the VAX, the shortest two call sequence is:
73      *
74      *          calls   $0,(r0)
75      *          calls   $0,(r0)
76      *
77      *  which is separated by only three bytes, thus HASHFRACTION is 
78      *  calculated as:
79      *
80      *          HASHFRACTION = 3 / (2 * 2 - 1) = 1
81      *
82      *  Note that the division above rounds down, thus if MIN_SUBR_FRACTION
83      *  is less than three, this algorithm will not work!
84      */
85 #define HASHFRACTION    1
86
87     /*
88      *  percent of text space to allocate for tostructs
89      *  with a minimum.
90      */
91 #define ARCDENSITY      2
92 #define MINARCS         50
93
94 struct tostruct {
95     char                *selfpc;
96     long                count;
97     unsigned short      link;
98 };
99
100     /*
101      *  a raw arc,
102      *      with pointers to the calling site and the called site
103      *      and a count.
104      */
105 struct rawarc {
106     unsigned long       raw_frompc;
107     unsigned long       raw_selfpc;
108     long                raw_count;
109 };
110
111     /*
112      *  general rounding functions.
113      */
114 #define ROUNDDOWN(x,y)  (((x)/(y))*(y))
115 #define ROUNDUP(x,y)    ((((x)+(y)-1)/(y))*(y))
116
117 IMPLEMENTATION:
118
119 // adapted freely from gcc's gmon.c
120
121 #include <unistd.h>
122 // #include <fcntl.h>
123 // #include <errno.h>
124
125 #ifdef DEBUG
126 #include <cstdio>
127 #endif
128
129 // /* extern asm ("mcount"); */
130 // /* extern */ char *minbrk; /* asm ("_minbrk"); */
131
132 /*
133  *      froms is actually a bunch of unsigned shorts indexing tos
134  */
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;
142
143 static int      ssiz;
144 static char     *sbuf;
145 static int      s_scale;
146     /* see profil(2) where this is describe (incorrectly) */
147 #define         SCALE_1_TO_1    0x10000L
148
149 /*
150  * Control profiling
151  *      profiling is what mcount checks to see if
152  *      all the data structures are ready.
153  */
154 extern "C" void
155 moncontrol( int mode )
156 {
157     int ret;
158 #if 0                           // L4 kernel
159     static int warned = 0;
160 #endif
161
162     if (mode) {
163         /* start */
164         ret = profil(sbuf + sizeof(struct phdr), ssiz - sizeof(struct phdr),
165                 (Address)s_lowpc, s_scale);
166 #if 0                           // L4 kernel
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));
170                 warned = 1;
171         }
172 #endif
173         profiling = 0;
174     } else {
175         /* stop */
176         profil((char *)0, 0, 0, 0);
177         profiling = 3;
178     }
179 }
180
181 #define MSG "No space for profiling buffer(s)\n"
182
183 extern "C" void
184 monstartup( char *lowpc, char *highpc, unsigned prof_rate )
185 {
186     int                 monsize;
187     char                *buffer;
188     register int        o;
189
190         /*
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.
193          */
194     lowpc = (char *)
195             ROUNDDOWN((unsigned)lowpc, HISTFRACTION*sizeof(HISTCOUNTER));
196     s_lowpc = lowpc;
197     highpc = (char *)
198             ROUNDUP((unsigned)highpc, HISTFRACTION*sizeof(HISTCOUNTER));
199     s_highpc = highpc;
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) );
205         return;
206     }
207     froms = (unsigned short *) sbrk( s_textsize / HASHFRACTION );
208     if ( froms == (unsigned short *) -1 ) {
209         write( 2 , MSG , sizeof(MSG) );
210         froms = 0;
211         return;
212     }
213     tolimit = s_textsize * ARCDENSITY / 100;
214     if ( tolimit < MINARCS ) {
215         tolimit = MINARCS;
216     } else if ( tolimit > 65534 ) {
217         tolimit = 65534;
218     }
219     tos = (struct tostruct *) sbrk( tolimit * sizeof( struct tostruct ) );
220     if ( tos == (struct tostruct *) -1 ) {
221         write( 2 , MSG , sizeof(MSG) );
222         froms = 0;
223         tos = 0;
224         return;
225     }
226 #if 0                           // L4 kernel
227     minbrk = (char *) sbrk(0);
228 #endif
229     tos[0].link = 0;
230     sbuf = buffer;
231     ssiz = monsize;
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);
240     if ( monsize <= 0 )
241         return;
242     o = highpc - lowpc;
243     if( monsize < o )
244 #ifndef hp300
245         s_scale = (int)(( (float) monsize / o ) * SCALE_1_TO_1);
246 #else /* avoid floating point */
247     {
248         int quot = o / monsize;
249
250         if (quot >= 0x10000)
251                 s_scale = 1;
252         else if (quot >= 0x100)
253                 s_scale = 0x10000 / quot;
254         else if (o >= 0x800000)
255                 s_scale = 0x1000000 / (o / (monsize >> 8));
256         else
257                 s_scale = 0x1000000 / ((o << 8) / monsize);
258     }
259 #endif
260     else
261         s_scale = SCALE_1_TO_1;
262     moncontrol(1);
263 }
264
265 extern "C" void
266 _mcleanup( void )
267 {
268     int                 fd;
269     int                 fromindex;
270     int                 endfrom;
271     char                *frompc;
272     int                 toindex;
273     struct rawarc       rawarc;
274
275     moncontrol(0);
276     fd = creat( "gmon.out" , 0666 );
277     if ( fd < 0 ) {
278         perror( "mcount: gmon.out" );
279         return;
280     }
281 #   ifdef DEBUG
282         fprintf( stderr , "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf , ssiz );
283 #   endif
284     write( fd , sbuf , ssiz );
285     endfrom = s_textsize / (HASHFRACTION * sizeof(*froms));
286     for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) {
287         if ( froms[fromindex] == 0 ) {
288             continue;
289         }
290         frompc = s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms));
291         for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link) {
292 #           ifdef DEBUG
293                 fprintf( stderr ,
294                         "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
295                         frompc , tos[toindex].selfpc , tos[toindex].count );
296 #           endif
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 );
301         }
302     }
303     close( fd );
304
305 #if 1                           // L4 kernel
306     sbrk_free(sbuf, ssiz);
307     sbrk_free(froms, s_textsize / HASHFRACTION);
308     sbrk_free(tos, tolimit * sizeof(struct tostruct));
309 #endif
310 }
311
312 extern "C" void
313 __mcount_internal(unsigned short *frompcindex, char *selfpc )
314 {
315         register struct tostruct        *top;
316         register struct tostruct        *prevtop;
317         register long                   toindex;
318
319 #if 0
320         register char                   *selfpc;
321         register unsigned short         *frompcindex;
322         /*
323          *      find the return address for mcount,
324          *      and the return address for mcount's caller.
325          */
326
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);
333 #endif
334
335         /*
336          *      check that we are profiling
337          *      and that we aren't recursively invoked.
338          */
339         if (profiling) {
340                 goto out;
341         }
342         profiling++;
343         /*
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.
347          */
348         frompcindex = (unsigned short *)((long)frompcindex - (long)s_lowpc);
349         if ((unsigned long)frompcindex > s_textsize) {
350                 goto done;
351         }
352         frompcindex =
353             &froms[((long)frompcindex) / (HASHFRACTION * sizeof(*froms))];
354         toindex = *frompcindex;
355         if (toindex == 0) {
356                 /*
357                  *      first time traversing this arc
358                  */
359                 toindex = ++tos[0].link;
360                 if (toindex >= tolimit) {
361                         goto overflow;
362                 }
363                 *frompcindex = toindex;
364                 top = &tos[toindex];
365                 top->selfpc = selfpc;
366                 top->count = 1;
367                 top->link = 0;
368                 goto done;
369         }
370         top = &tos[toindex];
371         if (top->selfpc == selfpc) {
372                 /*
373                  *      arc at front of chain; usual case.
374                  */
375                 top->count++;
376                 goto done;
377         }
378         /*
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.
383          */
384         for (; /* goto done */; ) {
385                 if (top->link == 0) {
386                         /*
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.
391                          */
392                         toindex = ++tos[0].link;
393                         if (toindex >= tolimit) {
394                                 goto overflow;
395                         }
396                         top = &tos[toindex];
397                         top->selfpc = selfpc;
398                         top->count = 1;
399                         top->link = *frompcindex;
400                         *frompcindex = toindex;
401                         goto done;
402                 }
403                 /*
404                  *      otherwise, check the next arc on the chain.
405                  */
406                 prevtop = top;
407                 top = &tos[top->link];
408                 if (top->selfpc == selfpc) {
409                         /*
410                          *      there it is.
411                          *      increment its count
412                          *      move it to the head of the chain.
413                          */
414                         top->count++;
415                         toindex = prevtop->link;
416                         prevtop->link = top->link;
417                         top->link = *frompcindex;
418                         *frompcindex = toindex;
419                         goto done;
420                 }
421
422         }
423 done:
424         profiling--;
425         /* and fall through */
426 out:
427         return;         /* normal return restores saved registers */
428
429 overflow:
430         profiling++; /* halt further profiling */
431 #   define      TOLIMIT "mcount: tos overflow\n"
432         write(2, TOLIMIT, sizeof(TOLIMIT));
433         goto out;
434 }