158f0484fSRodney W. Grimes /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 458f0484fSRodney W. Grimes * Copyright (c) 1983, 1992, 1993 558f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 658f0484fSRodney W. Grimes * 758f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 858f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 958f0484fSRodney W. Grimes * are met: 1058f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1258f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1358f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1458f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 1658f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1758f0484fSRodney W. Grimes * without specific prior written permission. 1858f0484fSRodney W. Grimes * 1958f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2058f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2158f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2258f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2358f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2458f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2558f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2658f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2758f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2858f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2958f0484fSRodney W. Grimes * SUCH DAMAGE. 3058f0484fSRodney W. Grimes */ 3158f0484fSRodney W. Grimes 32a9bdcd37SDavid E. O'Brien #if !defined(_KERNEL) && defined(LIBC_SCCS) && !defined(lint) 3358f0484fSRodney W. Grimes static char sccsid[] = "@(#)mcount.c 8.1 (Berkeley) 6/4/93"; 3458f0484fSRodney W. Grimes #endif 35a99d1013SDavid E. O'Brien #include <sys/cdefs.h> 36a99d1013SDavid E. O'Brien __FBSDID("$FreeBSD$"); 3758f0484fSRodney W. Grimes 3858f0484fSRodney W. Grimes #include <sys/param.h> 3958f0484fSRodney W. Grimes #include <sys/gmon.h> 40c4473420SPeter Wemm #ifdef _KERNEL 41912e6037SBruce Evans #include <sys/systm.h> 42912e6037SBruce Evans #include <vm/vm.h> 43912e6037SBruce Evans #include <vm/vm_param.h> 44912e6037SBruce Evans #include <vm/pmap.h> 45a99d1013SDavid E. O'Brien void bintr(void); 46a99d1013SDavid E. O'Brien void btrap(void); 47a99d1013SDavid E. O'Brien void eintr(void); 48a99d1013SDavid E. O'Brien void user(void); 49691071ffSPaul Richards #endif 500fa2f943SDavid Xu #include <machine/atomic.h> 5158f0484fSRodney W. Grimes 5258f0484fSRodney W. Grimes /* 5358f0484fSRodney W. Grimes * mcount is called on entry to each function compiled with the profiling 5458f0484fSRodney W. Grimes * switch set. _mcount(), which is declared in a machine-dependent way 5558f0484fSRodney W. Grimes * with _MCOUNT_DECL, does the actual work and is either inlined into a 5658f0484fSRodney W. Grimes * C routine or called by an assembly stub. In any case, this magic is 5758f0484fSRodney W. Grimes * taken care of by the MCOUNT definition in <machine/profile.h>. 5858f0484fSRodney W. Grimes * 5958f0484fSRodney W. Grimes * _mcount updates data structures that represent traversals of the 6058f0484fSRodney W. Grimes * program's call graph edges. frompc and selfpc are the return 6158f0484fSRodney W. Grimes * address and function address that represents the given call graph edge. 6258f0484fSRodney W. Grimes * 6358f0484fSRodney W. Grimes * Note: the original BSD code used the same variable (frompcindex) for 6458f0484fSRodney W. Grimes * both frompcindex and frompc. Any reasonable, modern compiler will 6558f0484fSRodney W. Grimes * perform this optimization. 6658f0484fSRodney W. Grimes */ 671d0342a3SJacques Vidrine /* _mcount; may be static, inline, etc */ 681d0342a3SJacques Vidrine _MCOUNT_DECL(uintfptr_t frompc, uintfptr_t selfpc) 6958f0484fSRodney W. Grimes { 70912e6037SBruce Evans #ifdef GUPROF 71912e6037SBruce Evans u_int delta; 72912e6037SBruce Evans #endif 73a99d1013SDavid E. O'Brien fptrdiff_t frompci; 74a99d1013SDavid E. O'Brien u_short *frompcindex; 75a99d1013SDavid E. O'Brien struct tostruct *top, *prevtop; 76a99d1013SDavid E. O'Brien struct gmonparam *p; 77a99d1013SDavid E. O'Brien long toindex; 78c4473420SPeter Wemm #ifdef _KERNEL 791f403fcfSBruce Evans MCOUNT_DECL(s) 8058f0484fSRodney W. Grimes #endif 8158f0484fSRodney W. Grimes 8258f0484fSRodney W. Grimes p = &_gmonparam; 83912e6037SBruce Evans #ifndef GUPROF /* XXX */ 8458f0484fSRodney W. Grimes /* 8558f0484fSRodney W. Grimes * check that we are profiling 8658f0484fSRodney W. Grimes * and that we aren't recursively invoked. 8758f0484fSRodney W. Grimes */ 8858f0484fSRodney W. Grimes if (p->state != GMON_PROF_ON) 8958f0484fSRodney W. Grimes return; 90912e6037SBruce Evans #endif 91c4473420SPeter Wemm #ifdef _KERNEL 921f403fcfSBruce Evans MCOUNT_ENTER(s); 9358f0484fSRodney W. Grimes #else 940fa2f943SDavid Xu if (!atomic_cmpset_acq_int(&p->state, GMON_PROF_ON, GMON_PROF_BUSY)) 950fa2f943SDavid Xu return; 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 2630fa2f943SDavid Xu atomic_store_rel_int(&p->state, GMON_PROF_ON); 26458f0484fSRodney W. Grimes #endif 26558f0484fSRodney W. Grimes return; 26658f0484fSRodney W. Grimes overflow: 2670fa2f943SDavid Xu atomic_store_rel_int(&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 282*05bc9aa7SEd Maste mexitcount(uintfptr_t selfpc) 283912e6037SBruce Evans { 284912e6037SBruce Evans struct gmonparam *p; 28537889b39SBruce Evans uintfptr_t selfpcdiff; 286912e6037SBruce Evans 287912e6037SBruce Evans p = &_gmonparam; 28837889b39SBruce Evans selfpcdiff = selfpc - (uintfptr_t)p->lowpc; 289912e6037SBruce Evans if (selfpcdiff < p->textsize) { 290912e6037SBruce Evans u_int delta; 291912e6037SBruce Evans 292912e6037SBruce Evans /* 293912e6037SBruce Evans * Solidify the count for the current function. 294912e6037SBruce Evans */ 295912e6037SBruce Evans delta = cputime() - p->mexitcount_overhead; 296912e6037SBruce Evans p->cputime_overhead_resid += p->cputime_overhead_frac; 297912e6037SBruce Evans p->mexitcount_overhead_resid += p->mexitcount_overhead_frac; 298912e6037SBruce Evans if ((int)delta < 0) 299912e6037SBruce Evans *p->mexitcount_count += delta + p->mexitcount_overhead 300912e6037SBruce Evans - p->cputime_overhead; 301912e6037SBruce Evans else if (delta != 0) { 302912e6037SBruce Evans if (p->cputime_overhead_resid >= CALIB_SCALE) { 303912e6037SBruce Evans p->cputime_overhead_resid -= CALIB_SCALE; 304912e6037SBruce Evans ++*p->cputime_count; 305912e6037SBruce Evans --delta; 306912e6037SBruce Evans } 307912e6037SBruce Evans if (delta != 0) { 308912e6037SBruce Evans if (p->mexitcount_overhead_resid 309912e6037SBruce Evans >= CALIB_SCALE) { 310912e6037SBruce Evans p->mexitcount_overhead_resid 311912e6037SBruce Evans -= CALIB_SCALE; 312912e6037SBruce Evans ++*p->mexitcount_count; 313912e6037SBruce Evans --delta; 314912e6037SBruce Evans } 315912e6037SBruce Evans KCOUNT(p, selfpcdiff) += delta; 316912e6037SBruce Evans } 317912e6037SBruce Evans *p->mexitcount_count += p->mexitcount_overhead_sub; 318912e6037SBruce Evans } 319912e6037SBruce Evans *p->cputime_count += p->cputime_overhead; 320912e6037SBruce Evans } 321912e6037SBruce Evans } 322912e6037SBruce Evans #endif /* GUPROF */ 323