123380Smckusick /* 229100Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 341966Smckusick * All rights reserved. 423380Smckusick * 541966Smckusick * %sccs.include.redist.c% 641966Smckusick * 7*44774Swilliam * @(#)subr_prof.c 7.9 (Berkeley) 06/30/90 823380Smckusick */ 97332Ssam 107332Ssam #ifdef GPROF 1117094Sbloom #include "gprof.h" 1217094Sbloom #include "param.h" 1317094Sbloom #include "systm.h" 1431433Smckusick #include "malloc.h" 157332Ssam 167332Ssam /* 177332Ssam * Froms is actually a bunch of unsigned shorts indexing tos 187332Ssam */ 1929946Skarels int profiling = 3; 2029946Skarels u_short *froms; 2129946Skarels struct tostruct *tos = 0; 2229946Skarels long tolimit = 0; 23*44774Swilliam char *s_lowpc = (char *)KERNBASE; 2429946Skarels extern char etext; 2529946Skarels char *s_highpc = &etext; 267332Ssam u_long s_textsize = 0; 2729946Skarels int ssiz; 287332Ssam u_short *sbuf; 297332Ssam u_short *kcount; 307332Ssam 317332Ssam kmstartup() 327332Ssam { 3329946Skarels u_long fromssize, tossize; 347332Ssam 3510292Smckusick /* 3629946Skarels * Round lowpc and highpc to multiples of the density we're using 3729946Skarels * so the rest of the scaling (here and in gprof) stays in ints. 3810292Smckusick */ 3910292Smckusick s_lowpc = (char *) 4029946Skarels ROUNDDOWN((unsigned)s_lowpc, HISTFRACTION*sizeof (HISTCOUNTER)); 4110292Smckusick s_highpc = (char *) 4229946Skarels ROUNDUP((unsigned)s_highpc, HISTFRACTION*sizeof (HISTCOUNTER)); 437332Ssam s_textsize = s_highpc - s_lowpc; 447332Ssam printf("Profiling kernel, s_textsize=%d [%x..%x]\n", 457332Ssam s_textsize, s_lowpc, s_highpc); 4629946Skarels ssiz = (s_textsize / HISTFRACTION) + sizeof (struct phdr); 4731433Smckusick sbuf = (u_short *)malloc(ssiz, M_GPROF, M_WAITOK); 487332Ssam if (sbuf == 0) { 497332Ssam printf("No space for monitor buffer(s)\n"); 507332Ssam return; 517332Ssam } 5231922Smckusick bzero(sbuf, ssiz); 5310292Smckusick fromssize = s_textsize / HASHFRACTION; 5431433Smckusick froms = (u_short *)malloc(fromssize, M_GPROF, M_WAITOK); 557332Ssam if (froms == 0) { 567332Ssam printf("No space for monitor buffer(s)\n"); 5731433Smckusick free(sbuf, M_GPROF); 587332Ssam sbuf = 0; 597332Ssam return; 607332Ssam } 6137535Smckusick bzero(froms, fromssize); 6210292Smckusick tolimit = s_textsize * ARCDENSITY / 100; 6329946Skarels if (tolimit < MINARCS) 6410292Smckusick tolimit = MINARCS; 6529946Skarels else if (tolimit > (0xffff - 1)) 6629946Skarels tolimit = 0xffff - 1; 6729946Skarels tossize = tolimit * sizeof (struct tostruct); 6831433Smckusick tos = (struct tostruct *)malloc(tossize, M_GPROF, M_WAITOK); 697332Ssam if (tos == 0) { 707332Ssam printf("No space for monitor buffer(s)\n"); 7131433Smckusick free(sbuf, M_GPROF), sbuf = 0; 7231433Smckusick free(froms, M_GPROF), froms = 0; 737332Ssam return; 747332Ssam } 7531922Smckusick bzero(tos, tossize); 767332Ssam tos[0].link = 0; 777332Ssam ((struct phdr *)sbuf)->lpc = s_lowpc; 787332Ssam ((struct phdr *)sbuf)->hpc = s_highpc; 797332Ssam ((struct phdr *)sbuf)->ncnt = ssiz; 8029946Skarels kcount = (u_short *)(((int)sbuf) + sizeof (struct phdr)); 817332Ssam } 827332Ssam 837332Ssam /* 8441966Smckusick * Special, non-profiled versions 8541966Smckusick */ 8642348Smckusick #if defined(hp300) && !defined(__GNUC__) 8741966Smckusick #define splhigh _splhigh 8841966Smckusick #define splx _splx 8941966Smckusick #endif 9041966Smckusick 9141966Smckusick /* 9229946Skarels * This routine is massaged so that it may be jsb'ed to on vax. 937332Ssam */ 9410292Smckusick asm(".text"); 9510292Smckusick asm("#the beginning of mcount()"); 9610292Smckusick asm(".data"); 977332Ssam mcount() 987332Ssam { 9929946Skarels register char *selfpc; /* r11 => r5 */ 10029946Skarels register u_short *frompcindex; /* r10 => r4 */ 10129946Skarels register struct tostruct *top; /* r9 => r3 */ 10229946Skarels register struct tostruct *prevtop; /* r8 => r2 */ 10329946Skarels register long toindex; /* r7 => r1 */ 10416924Smckusick static int s; 1057332Ssam 10629946Skarels asm(" .text"); /* make sure we're in text space */ 10729946Skarels /* 10829946Skarels * Check that we are profiling. 10929946Skarels */ 11029946Skarels if (profiling) 11129946Skarels goto out; 11229946Skarels /* 11329946Skarels * Find the return address for mcount, 11429946Skarels * and the return address for mcount's caller. 11529946Skarels */ 1167332Ssam #ifdef lint 11710292Smckusick selfpc = (char *)0; 1187332Ssam frompcindex = 0; 11929946Skarels #else 12033366Smckusick ; /* avoid label botch */ 12141966Smckusick #ifdef __GNUC__ 12229946Skarels #if defined(vax) 12341966Smckusick Fix Me!! 12441966Smckusick #endif 12541966Smckusick #if defined(tahoe) 12641966Smckusick Fix Me!! 12741966Smckusick #endif 12841966Smckusick #if defined(hp300) 12941966Smckusick /* 13041966Smckusick * selfpc = pc pushed by mcount jsr, 13141966Smckusick * frompcindex = pc pushed by jsr into self. 13241966Smckusick * In GCC the caller's stack frame has already been built so we 13341966Smckusick * have to chase a6 to find caller's raddr. This assumes that all 13441966Smckusick * routines we are profiling were built with GCC and that all 13541966Smckusick * profiled routines use link/unlk. 13641966Smckusick */ 13741966Smckusick asm("movl a6@(4),%0" : "=r" (selfpc)); 13841966Smckusick asm("movl a6@(0)@(4),%0" : "=r" (frompcindex)); 13941966Smckusick #endif 14041966Smckusick #else 14141966Smckusick #if defined(vax) 1427332Ssam asm(" movl (sp), r11"); /* selfpc = ... (jsb frame) */ 1437332Ssam asm(" movl 16(fp), r10"); /* frompcindex = (calls frame) */ 14429946Skarels #endif 14529946Skarels #if defined(tahoe) 14629946Skarels asm(" movl -8(fp),r12"); /* selfpc = callf frame */ 14729946Skarels asm(" movl (fp),r11"); 14829946Skarels asm(" movl -8(r11),r11"); /* frompcindex = 1 callf frame back */ 14929946Skarels #endif 15041966Smckusick #if defined(hp300) 15141966Smckusick asm(" .text"); /* make sure we're in text space */ 15241966Smckusick asm(" movl a6@(4),a5"); /* selfpc = pc pushed by mcount jsr */ 15341966Smckusick asm(" movl a6@(8),a4"); /* frompcindex = pc pushed by jsr into 15441966Smckusick self, stack frame not yet built */ 15529946Skarels #endif 15641966Smckusick #endif /* not __GNUC__ */ 15741966Smckusick #endif /* not lint */ 1587332Ssam /* 15929946Skarels * Insure that we cannot be recursively invoked. 16029946Skarels * this requires that splhigh() and splx() below 16129946Skarels * do NOT call mcount! 1627332Ssam */ 16342348Smckusick #if defined(hp300) && defined(__GNUC__) 16442348Smckusick asm("movw sr,%0" : "=g" (s)); 16542348Smckusick asm("movw #0x2700,sr"); 16642348Smckusick #else 16716924Smckusick s = splhigh(); 16842348Smckusick #endif 16916924Smckusick /* 17029946Skarels * Check that frompcindex is a reasonable pc value. 17129946Skarels * For example: signal catchers get called from the stack, 17229946Skarels * not from text space. too bad. 1737332Ssam */ 17429946Skarels frompcindex = (u_short *)((long)frompcindex - (long)s_lowpc); 17529946Skarels if ((u_long)frompcindex > s_textsize) 1767332Ssam goto done; 17710292Smckusick frompcindex = 17829946Skarels &froms[((long)frompcindex) / (HASHFRACTION * sizeof (*froms))]; 17910292Smckusick toindex = *frompcindex; 18010292Smckusick if (toindex == 0) { 18110292Smckusick /* 18229946Skarels * First time traversing this arc 18310292Smckusick */ 18410292Smckusick toindex = ++tos[0].link; 18529946Skarels if (toindex >= tolimit) 1867332Ssam goto overflow; 18710292Smckusick *frompcindex = toindex; 18810292Smckusick top = &tos[toindex]; 1897332Ssam top->selfpc = selfpc; 19010292Smckusick top->count = 1; 1917332Ssam top->link = 0; 19210292Smckusick goto done; 1937332Ssam } 19410292Smckusick top = &tos[toindex]; 19510292Smckusick if (top->selfpc == selfpc) { 19610292Smckusick /* 19729946Skarels * Arc at front of chain; usual case. 19810292Smckusick */ 19910292Smckusick top->count++; 20010292Smckusick goto done; 20110292Smckusick } 20210292Smckusick /* 20329946Skarels * Have to go looking down chain for it. 20429946Skarels * Top points to what we are looking at, 20529946Skarels * prevtop points to previous top. 20629946Skarels * We know it is not at the head of the chain. 20710292Smckusick */ 20810292Smckusick for (; /* goto done */; ) { 20910292Smckusick if (top->link == 0) { 21010292Smckusick /* 21129946Skarels * Top is end of the chain and none of the chain 21229946Skarels * had top->selfpc == selfpc. 21329946Skarels * So we allocate a new tostruct 21429946Skarels * and link it to the head of the chain. 21510292Smckusick */ 21610292Smckusick toindex = ++tos[0].link; 21729946Skarels if (toindex >= tolimit) 21810292Smckusick goto overflow; 21910292Smckusick top = &tos[toindex]; 22010292Smckusick top->selfpc = selfpc; 22110292Smckusick top->count = 1; 22210292Smckusick top->link = *frompcindex; 22310292Smckusick *frompcindex = toindex; 22410292Smckusick goto done; 22510292Smckusick } 22610292Smckusick /* 22729946Skarels * Otherwise, check the next arc on the chain. 22810292Smckusick */ 22910292Smckusick prevtop = top; 23010292Smckusick top = &tos[top->link]; 2317332Ssam if (top->selfpc == selfpc) { 23210292Smckusick /* 23329946Skarels * There it is, increment its count and 23429946Skarels * move it to the head of the chain. 23510292Smckusick */ 2367332Ssam top->count++; 23710292Smckusick toindex = prevtop->link; 23810292Smckusick prevtop->link = top->link; 23910292Smckusick top->link = *frompcindex; 24010292Smckusick *frompcindex = toindex; 24110292Smckusick goto done; 2427332Ssam } 24310292Smckusick 2447332Ssam } 2457332Ssam done: 24642348Smckusick #if defined(hp300) && defined(__GNUC__) 24742348Smckusick asm("movw %0,sr" : : "g" (s)); 24842348Smckusick #else 24916924Smckusick splx(s); 25042348Smckusick #endif 2517332Ssam /* and fall through */ 2527332Ssam out: 25329946Skarels #if defined(vax) 2547332Ssam asm(" rsb"); 25529946Skarels #endif 25629946Skarels return; 2577332Ssam overflow: 25810292Smckusick profiling = 3; 2597332Ssam printf("mcount: tos overflow\n"); 2607332Ssam goto out; 2617332Ssam } 26210292Smckusick asm(".text"); 26310292Smckusick asm("#the end of mcount()"); 26410292Smckusick asm(".data"); 26529946Skarels #endif 266