158f0484fSRodney W. Grimes /*- 258f0484fSRodney W. Grimes * Copyright (c) 1983, 1992, 1993 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 658f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 758f0484fSRodney W. Grimes * are met: 858f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 958f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1058f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1258f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1358f0484fSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 1458f0484fSRodney W. Grimes * must display the following acknowledgement: 1558f0484fSRodney W. Grimes * This product includes software developed by the University of 1658f0484fSRodney W. Grimes * California, Berkeley and its contributors. 1758f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 1858f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1958f0484fSRodney W. Grimes * without specific prior written permission. 2058f0484fSRodney W. Grimes * 2158f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2258f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2358f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2458f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2558f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2658f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2758f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2858f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2958f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3058f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3158f0484fSRodney W. Grimes * SUCH DAMAGE. 3258f0484fSRodney W. Grimes */ 3358f0484fSRodney W. Grimes 34c4473420SPeter Wemm #if !defined(lint) && !defined(_KERNEL) && defined(LIBC_SCCS) 3558f0484fSRodney W. Grimes static char sccsid[] = "@(#)mcount.c 8.1 (Berkeley) 6/4/93"; 3658f0484fSRodney W. Grimes #endif 37a99d1013SDavid E. O'Brien #include <sys/cdefs.h> 38a99d1013SDavid E. O'Brien __FBSDID("$FreeBSD$"); 3958f0484fSRodney W. Grimes 4058f0484fSRodney W. Grimes #include <sys/param.h> 4158f0484fSRodney W. Grimes #include <sys/gmon.h> 42c4473420SPeter Wemm #ifdef _KERNEL 43912e6037SBruce Evans #include <sys/systm.h> 44912e6037SBruce Evans #include <vm/vm.h> 45912e6037SBruce Evans #include <vm/vm_param.h> 46912e6037SBruce Evans #include <vm/pmap.h> 47a99d1013SDavid E. O'Brien void bintr(void); 48a99d1013SDavid E. O'Brien void btrap(void); 49a99d1013SDavid E. O'Brien void eintr(void); 50a99d1013SDavid E. O'Brien void user(void); 51691071ffSPaul Richards #endif 5258f0484fSRodney W. Grimes 5358f0484fSRodney W. Grimes /* 5458f0484fSRodney W. Grimes * mcount is called on entry to each function compiled with the profiling 5558f0484fSRodney W. Grimes * switch set. _mcount(), which is declared in a machine-dependent way 5658f0484fSRodney W. Grimes * with _MCOUNT_DECL, does the actual work and is either inlined into a 5758f0484fSRodney W. Grimes * C routine or called by an assembly stub. In any case, this magic is 5858f0484fSRodney W. Grimes * taken care of by the MCOUNT definition in <machine/profile.h>. 5958f0484fSRodney W. Grimes * 6058f0484fSRodney W. Grimes * _mcount updates data structures that represent traversals of the 6158f0484fSRodney W. Grimes * program's call graph edges. frompc and selfpc are the return 6258f0484fSRodney W. Grimes * address and function address that represents the given call graph edge. 6358f0484fSRodney W. Grimes * 6458f0484fSRodney W. Grimes * Note: the original BSD code used the same variable (frompcindex) for 6558f0484fSRodney W. Grimes * both frompcindex and frompc. Any reasonable, modern compiler will 6658f0484fSRodney W. Grimes * perform this optimization. 6758f0484fSRodney W. Grimes */ 6858f0484fSRodney W. Grimes _MCOUNT_DECL(frompc, selfpc) /* _mcount; may be static, inline, etc */ 69a99d1013SDavid E. O'Brien uintfptr_t frompc, selfpc; 7058f0484fSRodney W. Grimes { 71912e6037SBruce Evans #ifdef GUPROF 72912e6037SBruce Evans u_int delta; 73912e6037SBruce Evans #endif 74a99d1013SDavid E. O'Brien fptrdiff_t frompci; 75a99d1013SDavid E. O'Brien u_short *frompcindex; 76a99d1013SDavid E. O'Brien struct tostruct *top, *prevtop; 77a99d1013SDavid E. O'Brien struct gmonparam *p; 78a99d1013SDavid E. O'Brien long toindex; 79c4473420SPeter Wemm #ifdef _KERNEL 801f403fcfSBruce Evans MCOUNT_DECL(s) 8158f0484fSRodney W. Grimes #endif 8258f0484fSRodney W. Grimes 8358f0484fSRodney W. Grimes p = &_gmonparam; 84912e6037SBruce Evans #ifndef GUPROF /* XXX */ 8558f0484fSRodney W. Grimes /* 8658f0484fSRodney W. Grimes * check that we are profiling 8758f0484fSRodney W. Grimes * and that we aren't recursively invoked. 8858f0484fSRodney W. Grimes */ 8958f0484fSRodney W. Grimes if (p->state != GMON_PROF_ON) 9058f0484fSRodney W. Grimes return; 91912e6037SBruce Evans #endif 92c4473420SPeter Wemm #ifdef _KERNEL 931f403fcfSBruce Evans MCOUNT_ENTER(s); 9458f0484fSRodney W. Grimes #else 9558f0484fSRodney W. Grimes p->state = GMON_PROF_BUSY; 9658f0484fSRodney W. Grimes #endif 97912e6037SBruce Evans frompci = frompc - p->lowpc; 98912e6037SBruce Evans 99c4473420SPeter Wemm #ifdef _KERNEL 10058f0484fSRodney W. Grimes /* 101912e6037SBruce Evans * When we are called from an exception handler, frompci may be 102912e6037SBruce Evans * for a user address. Convert such frompci's to the index of 103912e6037SBruce Evans * user() to merge all user counts. 104912e6037SBruce Evans */ 105912e6037SBruce Evans if (frompci >= p->textsize) { 106912e6037SBruce Evans if (frompci + p->lowpc 10737889b39SBruce Evans >= (uintfptr_t)(VM_MAXUSER_ADDRESS + UPAGES * PAGE_SIZE)) 108912e6037SBruce Evans goto done; 10937889b39SBruce Evans frompci = (uintfptr_t)user - p->lowpc; 110912e6037SBruce Evans if (frompci >= p->textsize) 111912e6037SBruce Evans goto done; 112912e6037SBruce Evans } 113c4473420SPeter Wemm #endif 114912e6037SBruce Evans 115912e6037SBruce Evans #ifdef GUPROF 116912e6037SBruce Evans if (p->state != GMON_PROF_HIRES) 117912e6037SBruce Evans goto skip_guprof_stuff; 118912e6037SBruce Evans /* 119912e6037SBruce Evans * Look at the clock and add the count of clock cycles since the 120912e6037SBruce Evans * clock was last looked at to a counter for frompc. This 121912e6037SBruce Evans * solidifies the count for the function containing frompc and 122912e6037SBruce Evans * effectively starts another clock for the current function. 123912e6037SBruce Evans * The count for the new clock will be solidified when another 124912e6037SBruce Evans * function call is made or the function returns. 125912e6037SBruce Evans * 126912e6037SBruce Evans * We use the usual sampling counters since they can be located 127912e6037SBruce Evans * efficiently. 4-byte counters are usually necessary. 128912e6037SBruce Evans * 129912e6037SBruce Evans * There are many complications for subtracting the profiling 130912e6037SBruce Evans * overheads from the counts for normal functions and adding 131912e6037SBruce Evans * them to the counts for mcount(), mexitcount() and cputime(). 132912e6037SBruce Evans * We attempt to handle fractional cycles, but the overheads 133912e6037SBruce Evans * are usually underestimated because they are calibrated for 134912e6037SBruce Evans * a simpler than usual setup. 135912e6037SBruce Evans */ 136912e6037SBruce Evans delta = cputime() - p->mcount_overhead; 137912e6037SBruce Evans p->cputime_overhead_resid += p->cputime_overhead_frac; 138912e6037SBruce Evans p->mcount_overhead_resid += p->mcount_overhead_frac; 139912e6037SBruce Evans if ((int)delta < 0) 140912e6037SBruce Evans *p->mcount_count += delta + p->mcount_overhead 141912e6037SBruce Evans - p->cputime_overhead; 142912e6037SBruce Evans else if (delta != 0) { 143912e6037SBruce Evans if (p->cputime_overhead_resid >= CALIB_SCALE) { 144912e6037SBruce Evans p->cputime_overhead_resid -= CALIB_SCALE; 145912e6037SBruce Evans ++*p->cputime_count; 146912e6037SBruce Evans --delta; 147912e6037SBruce Evans } 148912e6037SBruce Evans if (delta != 0) { 149912e6037SBruce Evans if (p->mcount_overhead_resid >= CALIB_SCALE) { 150912e6037SBruce Evans p->mcount_overhead_resid -= CALIB_SCALE; 151912e6037SBruce Evans ++*p->mcount_count; 152912e6037SBruce Evans --delta; 153912e6037SBruce Evans } 154912e6037SBruce Evans KCOUNT(p, frompci) += delta; 155912e6037SBruce Evans } 156912e6037SBruce Evans *p->mcount_count += p->mcount_overhead_sub; 157912e6037SBruce Evans } 158912e6037SBruce Evans *p->cputime_count += p->cputime_overhead; 159912e6037SBruce Evans skip_guprof_stuff: 160912e6037SBruce Evans #endif /* GUPROF */ 161912e6037SBruce Evans 162c4473420SPeter Wemm #ifdef _KERNEL 163912e6037SBruce Evans /* 164912e6037SBruce Evans * When we are called from an exception handler, frompc is faked 165912e6037SBruce Evans * to be for where the exception occurred. We've just solidified 166912e6037SBruce Evans * the count for there. Now convert frompci to the index of btrap() 167912e6037SBruce Evans * for trap handlers and bintr() for interrupt handlers to make 168912e6037SBruce Evans * exceptions appear in the call graph as calls from btrap() and 169912e6037SBruce Evans * bintr() instead of calls from all over. 170912e6037SBruce Evans */ 17137889b39SBruce Evans if ((uintfptr_t)selfpc >= (uintfptr_t)btrap 17237889b39SBruce Evans && (uintfptr_t)selfpc < (uintfptr_t)eintr) { 17337889b39SBruce Evans if ((uintfptr_t)selfpc >= (uintfptr_t)bintr) 17437889b39SBruce Evans frompci = (uintfptr_t)bintr - p->lowpc; 175912e6037SBruce Evans else 17637889b39SBruce Evans frompci = (uintfptr_t)btrap - p->lowpc; 177912e6037SBruce Evans } 178c4473420SPeter Wemm #endif 179912e6037SBruce Evans 180912e6037SBruce Evans /* 181912e6037SBruce Evans * check that frompc is a reasonable pc value. 18258f0484fSRodney W. Grimes * for example: signal catchers get called from the stack, 18358f0484fSRodney W. Grimes * not from text space. too bad. 18458f0484fSRodney W. Grimes */ 185912e6037SBruce Evans if (frompci >= p->textsize) 18658f0484fSRodney W. Grimes goto done; 18758f0484fSRodney W. Grimes 188912e6037SBruce Evans frompcindex = &p->froms[frompci / (p->hashfraction * sizeof(*p->froms))]; 18958f0484fSRodney W. Grimes toindex = *frompcindex; 19058f0484fSRodney W. Grimes if (toindex == 0) { 19158f0484fSRodney W. Grimes /* 19258f0484fSRodney W. Grimes * first time traversing this arc 19358f0484fSRodney W. Grimes */ 19458f0484fSRodney W. Grimes toindex = ++p->tos[0].link; 19558f0484fSRodney W. Grimes if (toindex >= p->tolimit) 19658f0484fSRodney W. Grimes /* halt further profiling */ 19758f0484fSRodney W. Grimes goto overflow; 19858f0484fSRodney W. Grimes 19958f0484fSRodney W. Grimes *frompcindex = toindex; 20058f0484fSRodney W. Grimes top = &p->tos[toindex]; 20158f0484fSRodney W. Grimes top->selfpc = selfpc; 20258f0484fSRodney W. Grimes top->count = 1; 20358f0484fSRodney W. Grimes top->link = 0; 20458f0484fSRodney W. Grimes goto done; 20558f0484fSRodney W. Grimes } 20658f0484fSRodney W. Grimes top = &p->tos[toindex]; 20758f0484fSRodney W. Grimes if (top->selfpc == selfpc) { 20858f0484fSRodney W. Grimes /* 20958f0484fSRodney W. Grimes * arc at front of chain; usual case. 21058f0484fSRodney W. Grimes */ 21158f0484fSRodney W. Grimes top->count++; 21258f0484fSRodney W. Grimes goto done; 21358f0484fSRodney W. Grimes } 21458f0484fSRodney W. Grimes /* 21558f0484fSRodney W. Grimes * have to go looking down chain for it. 21658f0484fSRodney W. Grimes * top points to what we are looking at, 21758f0484fSRodney W. Grimes * prevtop points to previous top. 21858f0484fSRodney W. Grimes * we know it is not at the head of the chain. 21958f0484fSRodney W. Grimes */ 22058f0484fSRodney W. Grimes for (; /* goto done */; ) { 22158f0484fSRodney W. Grimes if (top->link == 0) { 22258f0484fSRodney W. Grimes /* 22358f0484fSRodney W. Grimes * top is end of the chain and none of the chain 22458f0484fSRodney W. Grimes * had top->selfpc == selfpc. 22558f0484fSRodney W. Grimes * so we allocate a new tostruct 22658f0484fSRodney W. Grimes * and link it to the head of the chain. 22758f0484fSRodney W. Grimes */ 22858f0484fSRodney W. Grimes toindex = ++p->tos[0].link; 22958f0484fSRodney W. Grimes if (toindex >= p->tolimit) 23058f0484fSRodney W. Grimes goto overflow; 23158f0484fSRodney W. Grimes 23258f0484fSRodney W. Grimes top = &p->tos[toindex]; 23358f0484fSRodney W. Grimes top->selfpc = selfpc; 23458f0484fSRodney W. Grimes top->count = 1; 23558f0484fSRodney W. Grimes top->link = *frompcindex; 23658f0484fSRodney W. Grimes *frompcindex = toindex; 23758f0484fSRodney W. Grimes goto done; 23858f0484fSRodney W. Grimes } 23958f0484fSRodney W. Grimes /* 24058f0484fSRodney W. Grimes * otherwise, check the next arc on the chain. 24158f0484fSRodney W. Grimes */ 24258f0484fSRodney W. Grimes prevtop = top; 24358f0484fSRodney W. Grimes top = &p->tos[top->link]; 24458f0484fSRodney W. Grimes if (top->selfpc == selfpc) { 24558f0484fSRodney W. Grimes /* 24658f0484fSRodney W. Grimes * there it is. 24758f0484fSRodney W. Grimes * increment its count 24858f0484fSRodney W. Grimes * move it to the head of the chain. 24958f0484fSRodney W. Grimes */ 25058f0484fSRodney W. Grimes top->count++; 25158f0484fSRodney W. Grimes toindex = prevtop->link; 25258f0484fSRodney W. Grimes prevtop->link = top->link; 25358f0484fSRodney W. Grimes top->link = *frompcindex; 25458f0484fSRodney W. Grimes *frompcindex = toindex; 25558f0484fSRodney W. Grimes goto done; 25658f0484fSRodney W. Grimes } 25758f0484fSRodney W. Grimes 25858f0484fSRodney W. Grimes } 25958f0484fSRodney W. Grimes done: 260c4473420SPeter Wemm #ifdef _KERNEL 2611f403fcfSBruce Evans MCOUNT_EXIT(s); 26258f0484fSRodney W. Grimes #else 26358f0484fSRodney W. Grimes p->state = GMON_PROF_ON; 26458f0484fSRodney W. Grimes #endif 26558f0484fSRodney W. Grimes return; 26658f0484fSRodney W. Grimes overflow: 26758f0484fSRodney W. Grimes p->state = GMON_PROF_ERROR; 268c4473420SPeter Wemm #ifdef _KERNEL 2691f403fcfSBruce Evans MCOUNT_EXIT(s); 27058f0484fSRodney W. Grimes #endif 27158f0484fSRodney W. Grimes return; 27258f0484fSRodney W. Grimes } 27358f0484fSRodney W. Grimes 27458f0484fSRodney W. Grimes /* 27558f0484fSRodney W. Grimes * Actual definition of mcount function. Defined in <machine/profile.h>, 27658f0484fSRodney W. Grimes * which is included by <sys/gmon.h>. 27758f0484fSRodney W. Grimes */ 27858f0484fSRodney W. Grimes MCOUNT 279912e6037SBruce Evans 280912e6037SBruce Evans #ifdef GUPROF 281912e6037SBruce Evans void 282912e6037SBruce Evans mexitcount(selfpc) 28337889b39SBruce Evans uintfptr_t selfpc; 284912e6037SBruce Evans { 285912e6037SBruce Evans struct gmonparam *p; 28637889b39SBruce Evans uintfptr_t selfpcdiff; 287912e6037SBruce Evans 288912e6037SBruce Evans p = &_gmonparam; 28937889b39SBruce Evans selfpcdiff = selfpc - (uintfptr_t)p->lowpc; 290912e6037SBruce Evans if (selfpcdiff < p->textsize) { 291912e6037SBruce Evans u_int delta; 292912e6037SBruce Evans 293912e6037SBruce Evans /* 294912e6037SBruce Evans * Solidify the count for the current function. 295912e6037SBruce Evans */ 296912e6037SBruce Evans delta = cputime() - p->mexitcount_overhead; 297912e6037SBruce Evans p->cputime_overhead_resid += p->cputime_overhead_frac; 298912e6037SBruce Evans p->mexitcount_overhead_resid += p->mexitcount_overhead_frac; 299912e6037SBruce Evans if ((int)delta < 0) 300912e6037SBruce Evans *p->mexitcount_count += delta + p->mexitcount_overhead 301912e6037SBruce Evans - p->cputime_overhead; 302912e6037SBruce Evans else if (delta != 0) { 303912e6037SBruce Evans if (p->cputime_overhead_resid >= CALIB_SCALE) { 304912e6037SBruce Evans p->cputime_overhead_resid -= CALIB_SCALE; 305912e6037SBruce Evans ++*p->cputime_count; 306912e6037SBruce Evans --delta; 307912e6037SBruce Evans } 308912e6037SBruce Evans if (delta != 0) { 309912e6037SBruce Evans if (p->mexitcount_overhead_resid 310912e6037SBruce Evans >= CALIB_SCALE) { 311912e6037SBruce Evans p->mexitcount_overhead_resid 312912e6037SBruce Evans -= CALIB_SCALE; 313912e6037SBruce Evans ++*p->mexitcount_count; 314912e6037SBruce Evans --delta; 315912e6037SBruce Evans } 316912e6037SBruce Evans KCOUNT(p, selfpcdiff) += delta; 317912e6037SBruce Evans } 318912e6037SBruce Evans *p->mexitcount_count += p->mexitcount_overhead_sub; 319912e6037SBruce Evans } 320912e6037SBruce Evans *p->cputime_count += p->cputime_overhead; 321912e6037SBruce Evans } 322912e6037SBruce Evans } 323912e6037SBruce Evans #endif /* GUPROF */ 324