123380Smckusick /* 229100Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 341966Smckusick * All rights reserved. 423380Smckusick * 541966Smckusick * %sccs.include.redist.c% 641966Smckusick * 7*53872Smckusick * @(#)subr_prof.c 7.13 (Berkeley) 06/04/92 823380Smckusick */ 97332Ssam 107332Ssam #ifdef GPROF 1117094Sbloom #include "gprof.h" 1217094Sbloom #include "param.h" 1317094Sbloom #include "systm.h" 1453015Smckusick #include "kernel.h" 1531433Smckusick #include "malloc.h" 167332Ssam 177332Ssam /* 187332Ssam * Froms is actually a bunch of unsigned shorts indexing tos 197332Ssam */ 2029946Skarels int profiling = 3; 2129946Skarels u_short *froms; 2229946Skarels struct tostruct *tos = 0; 2329946Skarels long tolimit = 0; 2444774Swilliam char *s_lowpc = (char *)KERNBASE; 2529946Skarels extern char etext; 2629946Skarels char *s_highpc = &etext; 277332Ssam u_long s_textsize = 0; 2829946Skarels int ssiz; 297332Ssam u_short *sbuf; 307332Ssam u_short *kcount; 317332Ssam 327332Ssam kmstartup() 337332Ssam { 3429946Skarels u_long fromssize, tossize; 357332Ssam 3610292Smckusick /* 3729946Skarels * Round lowpc and highpc to multiples of the density we're using 3829946Skarels * so the rest of the scaling (here and in gprof) stays in ints. 3910292Smckusick */ 4010292Smckusick s_lowpc = (char *) 4129946Skarels ROUNDDOWN((unsigned)s_lowpc, HISTFRACTION*sizeof (HISTCOUNTER)); 4210292Smckusick s_highpc = (char *) 4329946Skarels ROUNDUP((unsigned)s_highpc, HISTFRACTION*sizeof (HISTCOUNTER)); 447332Ssam s_textsize = s_highpc - s_lowpc; 457332Ssam printf("Profiling kernel, s_textsize=%d [%x..%x]\n", 467332Ssam s_textsize, s_lowpc, s_highpc); 4729946Skarels ssiz = (s_textsize / HISTFRACTION) + sizeof (struct phdr); 4831433Smckusick sbuf = (u_short *)malloc(ssiz, M_GPROF, M_WAITOK); 497332Ssam if (sbuf == 0) { 507332Ssam printf("No space for monitor buffer(s)\n"); 517332Ssam return; 527332Ssam } 5331922Smckusick bzero(sbuf, ssiz); 5410292Smckusick fromssize = s_textsize / HASHFRACTION; 5549283Shibler froms = (u_short *)malloc(fromssize, M_GPROF, M_NOWAIT); 567332Ssam if (froms == 0) { 577332Ssam printf("No space for monitor buffer(s)\n"); 5831433Smckusick free(sbuf, M_GPROF); 597332Ssam sbuf = 0; 607332Ssam return; 617332Ssam } 6237535Smckusick bzero(froms, fromssize); 6310292Smckusick tolimit = s_textsize * ARCDENSITY / 100; 6429946Skarels if (tolimit < MINARCS) 6510292Smckusick tolimit = MINARCS; 6629946Skarels else if (tolimit > (0xffff - 1)) 6729946Skarels tolimit = 0xffff - 1; 6829946Skarels tossize = tolimit * sizeof (struct tostruct); 6931433Smckusick tos = (struct tostruct *)malloc(tossize, M_GPROF, M_WAITOK); 707332Ssam if (tos == 0) { 717332Ssam printf("No space for monitor buffer(s)\n"); 7231433Smckusick free(sbuf, M_GPROF), sbuf = 0; 7331433Smckusick free(froms, M_GPROF), froms = 0; 747332Ssam return; 757332Ssam } 7631922Smckusick bzero(tos, tossize); 777332Ssam tos[0].link = 0; 787332Ssam ((struct phdr *)sbuf)->lpc = s_lowpc; 797332Ssam ((struct phdr *)sbuf)->hpc = s_highpc; 807332Ssam ((struct phdr *)sbuf)->ncnt = ssiz; 8153015Smckusick ((struct phdr *)sbuf)->version = GMONVERSION; 8253015Smckusick #ifdef PROFTIMER 8353015Smckusick initprofclock(); 8453015Smckusick ((struct phdr *)sbuf)->profrate = profhz; 8553015Smckusick #else 8653015Smckusick ((struct phdr *)sbuf)->profrate = hz; 8753015Smckusick #endif 8829946Skarels kcount = (u_short *)(((int)sbuf) + sizeof (struct phdr)); 897332Ssam } 907332Ssam 917332Ssam /* 9229946Skarels * This routine is massaged so that it may be jsb'ed to on vax. 937332Ssam */ 947332Ssam mcount() 957332Ssam { 9629946Skarels register char *selfpc; /* r11 => r5 */ 9729946Skarels register u_short *frompcindex; /* r10 => r4 */ 9829946Skarels register struct tostruct *top; /* r9 => r3 */ 9929946Skarels register struct tostruct *prevtop; /* r8 => r2 */ 10029946Skarels register long toindex; /* r7 => r1 */ 10116924Smckusick static int s; 1027332Ssam 10329946Skarels /* 10429946Skarels * Check that we are profiling. 10529946Skarels */ 10629946Skarels if (profiling) 10729946Skarels goto out; 10829946Skarels /* 10929946Skarels * Find the return address for mcount, 11029946Skarels * and the return address for mcount's caller. 11129946Skarels */ 1127332Ssam #ifdef lint 11310292Smckusick selfpc = (char *)0; 1147332Ssam frompcindex = 0; 11529946Skarels #else 11633366Smckusick ; /* avoid label botch */ 11741966Smckusick #ifdef __GNUC__ 11829946Skarels #if defined(vax) 11941966Smckusick Fix Me!! 12041966Smckusick #endif 12141966Smckusick #if defined(tahoe) 12241966Smckusick Fix Me!! 12341966Smckusick #endif 124*53872Smckusick #if defined(hp300) || defined(luna68k) 12541966Smckusick /* 12641966Smckusick * selfpc = pc pushed by mcount jsr, 12741966Smckusick * frompcindex = pc pushed by jsr into self. 12841966Smckusick * In GCC the caller's stack frame has already been built so we 12941966Smckusick * have to chase a6 to find caller's raddr. This assumes that all 13041966Smckusick * routines we are profiling were built with GCC and that all 13141966Smckusick * profiled routines use link/unlk. 13241966Smckusick */ 13341966Smckusick asm("movl a6@(4),%0" : "=r" (selfpc)); 13441966Smckusick asm("movl a6@(0)@(4),%0" : "=r" (frompcindex)); 13541966Smckusick #endif 13641966Smckusick #else 13741966Smckusick #if defined(vax) 1387332Ssam asm(" movl (sp), r11"); /* selfpc = ... (jsb frame) */ 1397332Ssam asm(" movl 16(fp), r10"); /* frompcindex = (calls frame) */ 14029946Skarels #endif 14129946Skarels #if defined(tahoe) 14229946Skarels asm(" movl -8(fp),r12"); /* selfpc = callf frame */ 14329946Skarels asm(" movl (fp),r11"); 14429946Skarels asm(" movl -8(r11),r11"); /* frompcindex = 1 callf frame back */ 14529946Skarels #endif 146*53872Smckusick #if defined(hp300) || defined(luna68k) 14749283Shibler Fix Me!! 14829946Skarels #endif 14941966Smckusick #endif /* not __GNUC__ */ 15041966Smckusick #endif /* not lint */ 1517332Ssam /* 15229946Skarels * Insure that we cannot be recursively invoked. 15329946Skarels * this requires that splhigh() and splx() below 15429946Skarels * do NOT call mcount! 1557332Ssam */ 156*53872Smckusick #if defined(hp300) || defined(luna68k) 15742348Smckusick asm("movw sr,%0" : "=g" (s)); 15842348Smckusick asm("movw #0x2700,sr"); 15942348Smckusick #else 16016924Smckusick s = splhigh(); 16142348Smckusick #endif 16216924Smckusick /* 16329946Skarels * Check that frompcindex is a reasonable pc value. 16429946Skarels * For example: signal catchers get called from the stack, 16529946Skarels * not from text space. too bad. 1667332Ssam */ 16729946Skarels frompcindex = (u_short *)((long)frompcindex - (long)s_lowpc); 16829946Skarels if ((u_long)frompcindex > s_textsize) 1697332Ssam goto done; 17010292Smckusick frompcindex = 17129946Skarels &froms[((long)frompcindex) / (HASHFRACTION * sizeof (*froms))]; 17210292Smckusick toindex = *frompcindex; 17310292Smckusick if (toindex == 0) { 17410292Smckusick /* 17529946Skarels * First time traversing this arc 17610292Smckusick */ 17710292Smckusick toindex = ++tos[0].link; 17829946Skarels if (toindex >= tolimit) 1797332Ssam goto overflow; 18010292Smckusick *frompcindex = toindex; 18110292Smckusick top = &tos[toindex]; 1827332Ssam top->selfpc = selfpc; 18310292Smckusick top->count = 1; 1847332Ssam top->link = 0; 18510292Smckusick goto done; 1867332Ssam } 18710292Smckusick top = &tos[toindex]; 18810292Smckusick if (top->selfpc == selfpc) { 18910292Smckusick /* 19029946Skarels * Arc at front of chain; usual case. 19110292Smckusick */ 19210292Smckusick top->count++; 19310292Smckusick goto done; 19410292Smckusick } 19510292Smckusick /* 19629946Skarels * Have to go looking down chain for it. 19729946Skarels * Top points to what we are looking at, 19829946Skarels * prevtop points to previous top. 19929946Skarels * We know it is not at the head of the chain. 20010292Smckusick */ 20110292Smckusick for (; /* goto done */; ) { 20210292Smckusick if (top->link == 0) { 20310292Smckusick /* 20429946Skarels * Top is end of the chain and none of the chain 20529946Skarels * had top->selfpc == selfpc. 20629946Skarels * So we allocate a new tostruct 20729946Skarels * and link it to the head of the chain. 20810292Smckusick */ 20910292Smckusick toindex = ++tos[0].link; 21029946Skarels if (toindex >= tolimit) 21110292Smckusick goto overflow; 21210292Smckusick top = &tos[toindex]; 21310292Smckusick top->selfpc = selfpc; 21410292Smckusick top->count = 1; 21510292Smckusick top->link = *frompcindex; 21610292Smckusick *frompcindex = toindex; 21710292Smckusick goto done; 21810292Smckusick } 21910292Smckusick /* 22029946Skarels * Otherwise, check the next arc on the chain. 22110292Smckusick */ 22210292Smckusick prevtop = top; 22310292Smckusick top = &tos[top->link]; 2247332Ssam if (top->selfpc == selfpc) { 22510292Smckusick /* 22629946Skarels * There it is, increment its count and 22729946Skarels * move it to the head of the chain. 22810292Smckusick */ 2297332Ssam top->count++; 23010292Smckusick toindex = prevtop->link; 23110292Smckusick prevtop->link = top->link; 23210292Smckusick top->link = *frompcindex; 23310292Smckusick *frompcindex = toindex; 23410292Smckusick goto done; 2357332Ssam } 23610292Smckusick 2377332Ssam } 2387332Ssam done: 239*53872Smckusick #if defined(hp300) || defined(luna68k) 24042348Smckusick asm("movw %0,sr" : : "g" (s)); 24142348Smckusick #else 24216924Smckusick splx(s); 24342348Smckusick #endif 2447332Ssam /* and fall through */ 2457332Ssam out: 24629946Skarels #if defined(vax) 2477332Ssam asm(" rsb"); 24829946Skarels #endif 24929946Skarels return; 2507332Ssam overflow: 25110292Smckusick profiling = 3; 2527332Ssam printf("mcount: tos overflow\n"); 2537332Ssam goto out; 2547332Ssam } 25510292Smckusick asm(".text"); 25610292Smckusick asm("#the end of mcount()"); 25710292Smckusick asm(".data"); 25829946Skarels #endif 259