00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #ifndef lint
00058 static char sccsid[] = "@(#)gmon.c 5.3 (Berkeley) 5/22/91";
00059 #endif
00060
00061 #if 0
00062 #include <unistd.h>
00063
00064 #endif
00065 #ifdef DEBUG
00066 #include <stdio.h>
00067 #endif
00068
00069 #if 0
00070 #include "i386/gmon.h"
00071 #else
00072
00073 struct phdr {
00074 char *lpc;
00075 char *hpc;
00076 int ncnt;
00077 };
00078
00079
00080 #define HISTFRACTION 2
00081 #define HISTCOUNTER unsigned short
00082 #define HASHFRACTION 1
00083 #define ARCDENSITY 2
00084 #define MINARCS 50
00085 #define BASEADDRESS 0x8000000
00086
00087
00088 struct tostruct {
00089 char *selfpc;
00090 long count;
00091 unsigned short link;
00092 };
00093 struct rawarc {
00094 unsigned long raw_frompc;
00095 unsigned long raw_selfpc;
00096 long raw_count;
00097 };
00098 #define ROUNDDOWN(x,y) (((x)/(y))*(y))
00099 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
00100 #endif
00101
00102
00103
00104 #ifdef __alpha
00105 extern char *sbrk ();
00106 #endif
00107
00108
00109
00110
00111 static int profiling = 3;
00112 static unsigned short *froms;
00113 static struct tostruct *tos = 0;
00114 static long tolimit = 0;
00115 static char *s_lowpc = 0;
00116 static char *s_highpc = 0;
00117 static unsigned long s_textsize = 0;
00118
00119 static int ssiz;
00120 static char *sbuf;
00121 static int s_scale;
00122
00123 #define SCALE_1_TO_1 0x10000L
00124
00125 #define MSG "No space for profiling buffer(s)\n"
00126
00127 extern int errno;
00128
00129 monstartup(lowpc, highpc)
00130 char *lowpc;
00131 char *highpc;
00132 {
00133 int monsize;
00134 char *buffer;
00135 register int o;
00136
00137
00138
00139
00140
00141 lowpc = (char *)
00142 ROUNDDOWN((unsigned)lowpc, HISTFRACTION*sizeof(HISTCOUNTER));
00143 s_lowpc = lowpc;
00144 highpc = (char *)
00145 ROUNDUP((unsigned)highpc, HISTFRACTION*sizeof(HISTCOUNTER));
00146 s_highpc = highpc;
00147 s_textsize = highpc - lowpc;
00148 monsize = (s_textsize / HISTFRACTION) + sizeof(struct phdr);
00149 buffer = (char *) sbrk( monsize );
00150 if ( buffer == (char *) -1 ) {
00151 write( 2 , MSG , sizeof(MSG) );
00152 return;
00153 }
00154 froms = (unsigned short *) sbrk( s_textsize / HASHFRACTION );
00155 if ( froms == (unsigned short *) -1 ) {
00156 write( 2 , MSG , sizeof(MSG) );
00157 froms = 0;
00158 return;
00159 }
00160 tolimit = s_textsize * ARCDENSITY / 100;
00161 if ( tolimit < MINARCS ) {
00162 tolimit = MINARCS;
00163 } else if ( tolimit > 65534 ) {
00164 tolimit = 65534;
00165 }
00166 tos = (struct tostruct *) sbrk( tolimit * sizeof( struct tostruct ) );
00167 if ( tos == (struct tostruct *) -1 ) {
00168 write( 2 , MSG , sizeof(MSG) );
00169 froms = 0;
00170 tos = 0;
00171 return;
00172 }
00173
00174 tos[0].link = 0;
00175 sbuf = buffer;
00176 ssiz = monsize;
00177 ( (struct phdr *) buffer ) -> lpc = lowpc;
00178 ( (struct phdr *) buffer ) -> hpc = highpc;
00179 ( (struct phdr *) buffer ) -> ncnt = ssiz;
00180 monsize -= sizeof(struct phdr);
00181 if ( monsize <= 0 )
00182 return;
00183 o = highpc - lowpc;
00184 if( monsize < o )
00185 #ifndef hp300
00186 s_scale = ( (float) monsize / o ) * SCALE_1_TO_1;
00187 #else
00188 {
00189 int quot = o / monsize;
00190
00191 if (quot >= 0x10000)
00192 s_scale = 1;
00193 else if (quot >= 0x100)
00194 s_scale = 0x10000 / quot;
00195 else if (o >= 0x800000)
00196 s_scale = 0x1000000 / (o / (monsize >> 8));
00197 else
00198 s_scale = 0x1000000 / ((o << 8) / monsize);
00199 }
00200 #endif
00201 else
00202 s_scale = SCALE_1_TO_1;
00203 moncontrol(1);
00204 }
00205
00206 _mcleanup()
00207 {
00208 int fd;
00209 int fromindex;
00210 int endfrom;
00211 char *frompc;
00212 int toindex;
00213 struct rawarc rawarc;
00214
00215 moncontrol(0);
00216 fd = creat( "gmon.out" , 0666 );
00217 if ( fd < 0 ) {
00218 perror( "mcount: gmon.out" );
00219 return;
00220 }
00221 # ifdef DEBUG
00222 fprintf( stderr , "[mcleanup] sbuf 0x%x ssiz %d\n" , sbuf , ssiz );
00223 # endif DEBUG
00224
00225 write( fd , sbuf , ssiz );
00226 endfrom = s_textsize / (HASHFRACTION * sizeof(*froms));
00227 for ( fromindex = 0 ; fromindex < endfrom ; fromindex++ ) {
00228 if ( froms[fromindex] == 0 ) {
00229 continue;
00230 }
00231 frompc = s_lowpc + (fromindex * HASHFRACTION * sizeof(*froms));
00232 for (toindex=froms[fromindex]; toindex!=0; toindex=tos[toindex].link) {
00233 # ifdef DEBUG
00234 fprintf( stderr ,
00235 "[mcleanup] frompc 0x%x selfpc 0x%x count %d\n" ,
00236 frompc , tos[toindex].selfpc , tos[toindex].count );
00237 # endif DEBUG
00238 rawarc.raw_frompc = (unsigned long) frompc;
00239 rawarc.raw_selfpc = (unsigned long) tos[toindex].selfpc;
00240 rawarc.raw_count = tos[toindex].count;
00241 write( fd , &rawarc , sizeof rawarc );
00242 }
00243 }
00244 close( fd );
00245 }
00246
00247
00248 asm(".globl _mcount; _mcount: jmp internal_mcount");
00249
00250 asm(".globl mcount; mcount: jmp internal_mcount");
00251
00252 internal_mcount()
00253 {
00254 register char *selfpc;
00255 register unsigned short *frompcindex;
00256 register struct tostruct *top;
00257 register struct tostruct *prevtop;
00258 register long toindex;
00259 static char already_setup;
00260
00261
00262
00263
00264
00265
00266
00267
00268 selfpc = (void *) __builtin_return_address (0);
00269
00270
00271 frompcindex = (void *) __builtin_return_address (1);
00272
00273 if(!already_setup) {
00274 extern etext();
00275 already_setup = 1;
00276
00277 monstartup(0x08040000, etext);
00278 #ifdef USE_ONEXIT
00279 on_exit(_mcleanup, 0);
00280 #else
00281 atexit(_mcleanup);
00282 #endif
00283 }
00284
00285
00286
00287
00288 if (profiling) {
00289 goto out;
00290 }
00291 profiling++;
00292
00293
00294
00295
00296
00297 frompcindex = (unsigned short *)((long)frompcindex - (long)s_lowpc);
00298 if ((unsigned long)frompcindex > s_textsize) {
00299 goto done;
00300 }
00301 frompcindex =
00302 &froms[((long)frompcindex) / (HASHFRACTION * sizeof(*froms))];
00303 toindex = *frompcindex;
00304 if (toindex == 0) {
00305
00306
00307
00308 toindex = ++tos[0].link;
00309 if (toindex >= tolimit) {
00310 goto overflow;
00311 }
00312 *frompcindex = toindex;
00313 top = &tos[toindex];
00314 top->selfpc = selfpc;
00315 top->count = 1;
00316 top->link = 0;
00317 goto done;
00318 }
00319 top = &tos[toindex];
00320 if (top->selfpc == selfpc) {
00321
00322
00323
00324 top->count++;
00325 goto done;
00326 }
00327
00328
00329
00330
00331
00332
00333 for (; ; ) {
00334 if (top->link == 0) {
00335
00336
00337
00338
00339
00340
00341 toindex = ++tos[0].link;
00342 if (toindex >= tolimit) {
00343 goto overflow;
00344 }
00345 top = &tos[toindex];
00346 top->selfpc = selfpc;
00347 top->count = 1;
00348 top->link = *frompcindex;
00349 *frompcindex = toindex;
00350 goto done;
00351 }
00352
00353
00354
00355 prevtop = top;
00356 top = &tos[top->link];
00357 if (top->selfpc == selfpc) {
00358
00359
00360
00361
00362
00363 top->count++;
00364 toindex = prevtop->link;
00365 prevtop->link = top->link;
00366 top->link = *frompcindex;
00367 *frompcindex = toindex;
00368 goto done;
00369 }
00370
00371 }
00372 done:
00373 profiling--;
00374
00375 out:
00376 return;
00377
00378 overflow:
00379 profiling++;
00380 # define TOLIMIT "mcount: tos overflow\n"
00381 write(2, TOLIMIT, sizeof(TOLIMIT));
00382 goto out;
00383 }
00384
00385
00386
00387
00388
00389
00390 moncontrol(mode)
00391 int mode;
00392 {
00393 if (mode)
00394 {
00395
00396 profil((unsigned short *)(sbuf + sizeof(struct phdr)),
00397 ssiz - sizeof(struct phdr),
00398 (int)s_lowpc, s_scale);
00399
00400 profiling = 0;
00401 } else {
00402
00403 profil((unsigned short *)0, 0, 0, 0);
00404 profiling = 3;
00405 }
00406 }