The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/libkern/mcount.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-3-Clause
    3  *
    4  * Copyright (c) 1983, 1992, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. Neither the name of the University nor the names of its contributors
   16  *    may be used to endorse or promote products derived from this software
   17  *    without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD$");
   34 
   35 #include <sys/param.h>
   36 #include <sys/gmon.h>
   37 #ifdef _KERNEL
   38 #ifndef GUPROF
   39 #include <sys/systm.h>
   40 #endif
   41 #include <vm/vm.h>
   42 #include <vm/vm_param.h>
   43 #include <vm/pmap.h>
   44 #endif
   45 
   46 /*
   47  * mcount is called on entry to each function compiled with the profiling
   48  * switch set.  _mcount(), which is declared in a machine-dependent way
   49  * with _MCOUNT_DECL, does the actual work and is either inlined into a
   50  * C routine or called by an assembly stub.  In any case, this magic is
   51  * taken care of by the MCOUNT definition in <machine/profile.h>.
   52  *
   53  * _mcount updates data structures that represent traversals of the
   54  * program's call graph edges.  frompc and selfpc are the return
   55  * address and function address that represents the given call graph edge.
   56  *
   57  * Note: the original BSD code used the same variable (frompcindex) for
   58  * both frompcindex and frompc.  Any reasonable, modern compiler will
   59  * perform this optimization.
   60  */
   61 /* _mcount; may be static, inline, etc */
   62 _MCOUNT_DECL(uintfptr_t frompc, uintfptr_t selfpc)
   63 {
   64 #ifdef GUPROF
   65         int delta;
   66 #endif
   67         fptrdiff_t frompci;
   68         u_short *frompcindex;
   69         struct tostruct *top, *prevtop;
   70         struct gmonparam *p;
   71         long toindex;
   72 #ifdef _KERNEL
   73         MCOUNT_DECL(s)
   74 #endif
   75 
   76         p = &_gmonparam;
   77 #ifndef GUPROF                  /* XXX */
   78         /*
   79          * check that we are profiling
   80          * and that we aren't recursively invoked.
   81          */
   82         if (p->state != GMON_PROF_ON)
   83                 return;
   84 #endif
   85 #ifdef _KERNEL
   86         MCOUNT_ENTER(s);
   87 #else
   88         p->state = GMON_PROF_BUSY;
   89 #endif
   90 
   91 #ifdef _KERNEL
   92         /* De-relocate any addresses in a (single) trampoline. */
   93 #ifdef MCOUNT_DETRAMP
   94         MCOUNT_DETRAMP(frompc);
   95         MCOUNT_DETRAMP(selfpc);
   96 #endif
   97         /*
   98          * When we are called from an exception handler, frompc may be
   99          * a user address.  Convert such frompc's to some representation
  100          * in kernel address space.
  101          */
  102 #ifdef MCOUNT_FROMPC_USER
  103         frompc = MCOUNT_FROMPC_USER(frompc);
  104 #elif defined(MCOUNT_USERPC)
  105         /*
  106          * For separate address spaces, we can only guess that addresses
  107          * in the range known to us are actually kernel addresses.  Outside
  108          * of this range, conerting to the user address is fail-safe.
  109          */
  110         if (frompc < p->lowpc || frompc - p->lowpc >= p->textsize)
  111                 frompc = MCOUNT_USERPC;
  112 #endif
  113 #endif /* _KERNEL */
  114 
  115         frompci = frompc - p->lowpc;
  116         if (frompci >= p->textsize)
  117                 goto done;
  118 
  119 #ifdef GUPROF
  120         if (p->state == GMON_PROF_HIRES) {
  121                 /*
  122                  * Count the time since cputime() was previously called
  123                  * against `frompc'.  Compensate for overheads.
  124                  *
  125                  * cputime() sets its prev_count variable to the count when
  126                  * it is called.  This in effect starts a counter for
  127                  * the next period of execution (normally from now until 
  128                  * the next call to mcount() or mexitcount()).  We set
  129                  * cputime_bias to compensate for our own overhead.
  130                  *
  131                  * We use the usual sampling counters since they can be
  132                  * located efficiently.  4-byte counters are usually
  133                  * necessary.  gprof will add up the scattered counts
  134                  * just like it does for statistical profiling.  All
  135                  * counts are signed so that underflow in the subtractions
  136                  * doesn't matter much (negative counts are normally
  137                  * compensated for by larger counts elsewhere).  Underflow
  138                  * shouldn't occur, but may be caused by slightly wrong
  139                  * calibrations or from not clearing cputime_bias.
  140                  */
  141                 delta = cputime() - cputime_bias - p->mcount_pre_overhead;
  142                 cputime_bias = p->mcount_post_overhead;
  143                 KCOUNT(p, frompci) += delta;
  144                 *p->cputime_count += p->cputime_overhead;
  145                 *p->mcount_count += p->mcount_overhead;
  146         }
  147 #endif /* GUPROF */
  148 
  149 #ifdef _KERNEL
  150         /*
  151          * When we are called from an exception handler, frompc is faked
  152          * to be for where the exception occurred.  We've just solidified
  153          * the count for there.  Now convert frompci to an index that
  154          * represents the kind of exception so that interruptions appear
  155          * in the call graph as calls from those index instead of calls
  156          * from all over.
  157          */
  158         frompc = MCOUNT_FROMPC_INTR(selfpc);
  159         if ((frompc - p->lowpc) < p->textsize)
  160                 frompci = frompc - p->lowpc;
  161 #endif
  162 
  163         /*
  164          * check that frompc is a reasonable pc value.
  165          * for example: signal catchers get called from the stack,
  166          *              not from text space.  too bad.
  167          */
  168         if (frompci >= p->textsize)
  169                 goto done;
  170 
  171         frompcindex = &p->froms[frompci / (p->hashfraction * sizeof(*p->froms))];
  172         toindex = *frompcindex;
  173         if (toindex == 0) {
  174                 /*
  175                  *      first time traversing this arc
  176                  */
  177                 toindex = ++p->tos[0].link;
  178                 if (toindex >= p->tolimit)
  179                         /* halt further profiling */
  180                         goto overflow;
  181 
  182                 *frompcindex = toindex;
  183                 top = &p->tos[toindex];
  184                 top->selfpc = selfpc;
  185                 top->count = 1;
  186                 top->link = 0;
  187                 goto done;
  188         }
  189         top = &p->tos[toindex];
  190         if (top->selfpc == selfpc) {
  191                 /*
  192                  * arc at front of chain; usual case.
  193                  */
  194                 top->count++;
  195                 goto done;
  196         }
  197         /*
  198          * have to go looking down chain for it.
  199          * top points to what we are looking at,
  200          * prevtop points to previous top.
  201          * we know it is not at the head of the chain.
  202          */
  203         for (; /* goto done */; ) {
  204                 if (top->link == 0) {
  205                         /*
  206                          * top is end of the chain and none of the chain
  207                          * had top->selfpc == selfpc.
  208                          * so we allocate a new tostruct
  209                          * and link it to the head of the chain.
  210                          */
  211                         toindex = ++p->tos[0].link;
  212                         if (toindex >= p->tolimit)
  213                                 goto overflow;
  214 
  215                         top = &p->tos[toindex];
  216                         top->selfpc = selfpc;
  217                         top->count = 1;
  218                         top->link = *frompcindex;
  219                         *frompcindex = toindex;
  220                         goto done;
  221                 }
  222                 /*
  223                  * otherwise, check the next arc on the chain.
  224                  */
  225                 prevtop = top;
  226                 top = &p->tos[top->link];
  227                 if (top->selfpc == selfpc) {
  228                         /*
  229                          * there it is.
  230                          * increment its count
  231                          * move it to the head of the chain.
  232                          */
  233                         top->count++;
  234                         toindex = prevtop->link;
  235                         prevtop->link = top->link;
  236                         top->link = *frompcindex;
  237                         *frompcindex = toindex;
  238                         goto done;
  239                 }
  240         }
  241 done:
  242 #ifdef _KERNEL
  243         MCOUNT_EXIT(s);
  244 #else
  245         p->state = GMON_PROF_ON;
  246 #endif
  247         return;
  248 overflow:
  249         p->state = GMON_PROF_ERROR;
  250 #ifdef _KERNEL
  251         MCOUNT_EXIT(s);
  252 #endif
  253         return;
  254 }
  255 
  256 /*
  257  * Actual definition of mcount function.  Defined in <machine/profile.h>,
  258  * which is included by <sys/gmon.h>.
  259  */
  260 MCOUNT
  261 
  262 #ifdef GUPROF
  263 void
  264 mexitcount(uintfptr_t selfpc)
  265 {
  266         struct gmonparam *p;
  267         uintfptr_t selfpcdiff;
  268 
  269         p = &_gmonparam;
  270 #ifdef MCOUNT_DETRAMP
  271         MCOUNT_DETRAMP(selfpc);
  272 #endif
  273         selfpcdiff = selfpc - (uintfptr_t)p->lowpc;
  274         if (selfpcdiff < p->textsize) {
  275                 int delta;
  276 
  277                 /*
  278                  * Count the time since cputime() was previously called
  279                  * against `selfpc'.  Compensate for overheads.
  280                  */
  281                 delta = cputime() - cputime_bias - p->mexitcount_pre_overhead;
  282                 cputime_bias = p->mexitcount_post_overhead;
  283                 KCOUNT(p, selfpcdiff) += delta;
  284                 *p->cputime_count += p->cputime_overhead;
  285                 *p->mexitcount_count += p->mexitcount_overhead;
  286         }
  287 }
  288 
  289 #ifndef __GNUCLIKE_ASM
  290 #error "This file uses null asms to prevent timing loops being optimized away."
  291 #endif
  292 
  293 void
  294 empty_loop(void)
  295 {
  296         int i;
  297 
  298         for (i = 0; i < CALIB_SCALE; i++)
  299                 __asm __volatile("");
  300 }
  301 
  302 void
  303 nullfunc(void)
  304 {
  305         __asm __volatile("");
  306 }
  307 
  308 void
  309 nullfunc_loop(void)
  310 {
  311         int i;
  312 
  313         for (i = 0; i < CALIB_SCALE; i++)
  314                 nullfunc();
  315 }
  316 #endif /* GUPROF */

Cache object: 333dbaf942c4cead6b9e356425a7a56e


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.