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/kern/subr_prof.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  * Copyright (c) 1982, 1986, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by the University of
   16  *      California, Berkeley and its contributors.
   17  * 4. Neither the name of the University nor the names of its contributors
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  *
   33  *      @(#)subr_prof.c 8.3 (Berkeley) 9/23/93
   34  * $FreeBSD: src/sys/kern/subr_prof.c,v 1.17.2.2 1999/09/05 08:15:14 peter Exp $
   35  */
   36 
   37 #include <sys/param.h>
   38 #include <sys/systm.h>
   39 #include <sys/sysproto.h>
   40 #include <sys/kernel.h>
   41 #include <sys/proc.h>
   42 #include <sys/resourcevar.h>
   43 #include <sys/sysctl.h>
   44 
   45 #include <machine/cpu.h>
   46 
   47 #ifdef GPROF
   48 #include <sys/malloc.h>
   49 #include <sys/gmon.h>
   50 
   51 static void kmstartup __P((void *));
   52 SYSINIT(kmem, SI_SUB_KPROF, SI_ORDER_FIRST, kmstartup, NULL)
   53 
   54 struct gmonparam _gmonparam = { GMON_PROF_OFF };
   55 
   56 extern char btext[];
   57 extern char etext[];
   58 
   59 #ifdef GUPROF
   60 void
   61 nullfunc_loop_profiled()
   62 {
   63         int i;
   64 
   65         for (i = 0; i < CALIB_SCALE; i++)
   66                 nullfunc_profiled();
   67 }
   68 
   69 #define nullfunc_loop_profiled_end      nullfunc_profiled       /* XXX */
   70 
   71 void
   72 nullfunc_profiled()
   73 {
   74 }
   75 #endif /* GUPROF */
   76 
   77 static void
   78 kmstartup(dummy)
   79         void *dummy;
   80 {
   81         char *cp;
   82         struct gmonparam *p = &_gmonparam;
   83 #ifdef GUPROF
   84         int cputime_overhead;
   85         int empty_loop_time;
   86         int i;
   87         int mcount_overhead;
   88         int mexitcount_overhead;
   89         int nullfunc_loop_overhead;
   90         int nullfunc_loop_profiled_time;
   91         fptrint_t tmp_addr;
   92 #endif
   93 
   94         /*
   95          * Round lowpc and highpc to multiples of the density we're using
   96          * so the rest of the scaling (here and in gprof) stays in ints.
   97          */
   98         p->lowpc = ROUNDDOWN((u_long)btext, HISTFRACTION * sizeof(HISTCOUNTER));
   99         p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER));
  100         p->textsize = p->highpc - p->lowpc;
  101         printf("Profiling kernel, textsize=%lu [%x..%x]\n",
  102                p->textsize, p->lowpc, p->highpc);
  103         p->kcountsize = p->textsize / HISTFRACTION;
  104         p->hashfraction = HASHFRACTION;
  105         p->fromssize = p->textsize / HASHFRACTION;
  106         p->tolimit = p->textsize * ARCDENSITY / 100;
  107         if (p->tolimit < MINARCS)
  108                 p->tolimit = MINARCS;
  109         else if (p->tolimit > MAXARCS)
  110                 p->tolimit = MAXARCS;
  111         p->tossize = p->tolimit * sizeof(struct tostruct);
  112         cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize,
  113             M_GPROF, M_NOWAIT);
  114         if (cp == 0) {
  115                 printf("No memory for profiling.\n");
  116                 return;
  117         }
  118         bzero(cp, p->kcountsize + p->tossize + p->fromssize);
  119         p->tos = (struct tostruct *)cp;
  120         cp += p->tossize;
  121         p->kcount = (HISTCOUNTER *)cp;
  122         cp += p->kcountsize;
  123         p->froms = (u_short *)cp;
  124 
  125 #ifdef GUPROF
  126         /* Initialize pointers to overhead counters. */
  127         p->cputime_count = &KCOUNT(p, PC_TO_I(p, cputime));
  128         p->mcount_count = &KCOUNT(p, PC_TO_I(p, mcount));
  129         p->mexitcount_count = &KCOUNT(p, PC_TO_I(p, mexitcount));
  130 
  131         /*
  132          * Disable interrupts to avoid interference while we calibrate
  133          * things.
  134          */
  135         disable_intr();
  136 
  137         /*
  138          * Determine overheads.
  139          * XXX this needs to be repeated for each useful timer/counter.
  140          */
  141         cputime_overhead = 0;
  142         startguprof(p);
  143         for (i = 0; i < CALIB_SCALE; i++)
  144                 cputime_overhead += cputime();
  145 
  146         empty_loop();
  147         startguprof(p);
  148         empty_loop();
  149         empty_loop_time = cputime();
  150 
  151         nullfunc_loop_profiled();
  152 
  153         /*
  154          * Start profiling.  There won't be any normal function calls since
  155          * interrupts are disabled, but we will call the profiling routines
  156          * directly to determine their overheads.
  157          */
  158         p->state = GMON_PROF_HIRES;
  159 
  160         startguprof(p);
  161         nullfunc_loop_profiled();
  162 
  163         startguprof(p);
  164         for (i = 0; i < CALIB_SCALE; i++)
  165 #if defined(i386) && __GNUC__ >= 2
  166                 asm("pushl %0; call __mcount; popl %%ecx"
  167                     :
  168                     : "i" (profil)
  169                     : "ax", "bx", "cx", "dx", "memory");
  170 #else
  171 #error
  172 #endif
  173         mcount_overhead = KCOUNT(p, PC_TO_I(p, profil));
  174 
  175         startguprof(p);
  176         for (i = 0; i < CALIB_SCALE; i++)
  177 #if defined(i386) && __GNUC__ >= 2
  178                     asm("call mexitcount; 1:"
  179                         : : : "ax", "bx", "cx", "dx", "memory");
  180         asm("movl $1b,%0" : "=rm" (tmp_addr));
  181 #else
  182 #error
  183 #endif
  184         mexitcount_overhead = KCOUNT(p, PC_TO_I(p, tmp_addr));
  185 
  186         p->state = GMON_PROF_OFF;
  187         stopguprof(p);
  188 
  189         enable_intr();
  190 
  191         nullfunc_loop_profiled_time = 0;
  192         for (tmp_addr = (fptrint_t)nullfunc_loop_profiled;
  193              tmp_addr < (fptrint_t)nullfunc_loop_profiled_end;
  194              tmp_addr += HISTFRACTION * sizeof(HISTCOUNTER))
  195                 nullfunc_loop_profiled_time += KCOUNT(p, PC_TO_I(p, tmp_addr));
  196 #define CALIB_DOSCALE(count)    (((count) + CALIB_SCALE / 3) / CALIB_SCALE)
  197 #define c2n(count, freq)        ((int)((count) * 1000000000LL / freq))
  198         printf("cputime %d, empty_loop %d, nullfunc_loop_profiled %d, mcount %d, mexitcount %d\n",
  199                CALIB_DOSCALE(c2n(cputime_overhead, p->profrate)),
  200                CALIB_DOSCALE(c2n(empty_loop_time, p->profrate)),
  201                CALIB_DOSCALE(c2n(nullfunc_loop_profiled_time, p->profrate)),
  202                CALIB_DOSCALE(c2n(mcount_overhead, p->profrate)),
  203                CALIB_DOSCALE(c2n(mexitcount_overhead, p->profrate)));
  204         cputime_overhead -= empty_loop_time;
  205         mcount_overhead -= empty_loop_time;
  206         mexitcount_overhead -= empty_loop_time;
  207 
  208         /*-
  209          * Profiling overheads are determined by the times between the
  210          * following events:
  211          *      MC1: mcount() is called
  212          *      MC2: cputime() (called from mcount()) latches the timer
  213          *      MC3: mcount() completes
  214          *      ME1: mexitcount() is called
  215          *      ME2: cputime() (called from mexitcount()) latches the timer
  216          *      ME3: mexitcount() completes.
  217          * The times between the events vary slightly depending on instruction
  218          * combination and cache misses, etc.  Attempt to determine the
  219          * minimum times.  These can be subtracted from the profiling times
  220          * without much risk of reducing the profiling times below what they
  221          * would be when profiling is not configured.  Abbreviate:
  222          *      ab = minimum time between MC1 and MC3
  223          *      a  = minumum time between MC1 and MC2
  224          *      b  = minimum time between MC2 and MC3
  225          *      cd = minimum time between ME1 and ME3
  226          *      c  = minimum time between ME1 and ME2
  227          *      d  = minimum time between ME2 and ME3.
  228          * These satisfy the relations:
  229          *      ab            <= mcount_overhead                (just measured)
  230          *      a + b         <= ab
  231          *              cd    <= mexitcount_overhead            (just measured)
  232          *              c + d <= cd
  233          *      a         + d <= nullfunc_loop_profiled_time    (just measured)
  234          *      a >= 0, b >= 0, c >= 0, d >= 0.
  235          * Assume that ab and cd are equal to the minimums.
  236          */
  237         p->cputime_overhead = CALIB_DOSCALE(cputime_overhead);
  238         p->mcount_overhead = CALIB_DOSCALE(mcount_overhead - cputime_overhead);
  239         p->mexitcount_overhead = CALIB_DOSCALE(mexitcount_overhead
  240                                                - cputime_overhead);
  241         nullfunc_loop_overhead = nullfunc_loop_profiled_time - empty_loop_time;
  242         p->mexitcount_post_overhead = CALIB_DOSCALE((mcount_overhead
  243                                                      - nullfunc_loop_overhead)
  244                                                     / 4);
  245         p->mexitcount_pre_overhead = p->mexitcount_overhead
  246                                      + p->cputime_overhead
  247                                      - p->mexitcount_post_overhead;
  248         p->mcount_pre_overhead = CALIB_DOSCALE(nullfunc_loop_overhead)
  249                                  - p->mexitcount_post_overhead;
  250         p->mcount_post_overhead = p->mcount_overhead
  251                                   + p->cputime_overhead
  252                                   - p->mcount_pre_overhead;
  253         printf(
  254 "Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d nsec\n",
  255                c2n(p->cputime_overhead, p->profrate),
  256                c2n(p->mcount_overhead, p->profrate),
  257                c2n(p->mcount_pre_overhead, p->profrate),
  258                c2n(p->mcount_post_overhead, p->profrate),
  259                c2n(p->cputime_overhead, p->profrate),
  260                c2n(p->mexitcount_overhead, p->profrate),
  261                c2n(p->mexitcount_pre_overhead, p->profrate),
  262                c2n(p->mexitcount_post_overhead, p->profrate));
  263         printf(
  264 "Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d cycles\n",
  265                p->cputime_overhead, p->mcount_overhead,
  266                p->mcount_pre_overhead, p->mcount_post_overhead,
  267                p->cputime_overhead, p->mexitcount_overhead,
  268                p->mexitcount_pre_overhead, p->mexitcount_post_overhead);
  269 #endif /* GUPROF */
  270 }
  271 
  272 /*
  273  * Return kernel profiling information.
  274  */
  275 static int
  276 sysctl_kern_prof SYSCTL_HANDLER_ARGS
  277 {
  278         int *name = (int *) arg1;
  279         u_int namelen = arg2;
  280         struct gmonparam *gp = &_gmonparam;
  281         int error;
  282         int state;
  283 
  284         /* all sysctl names at this level are terminal */
  285         if (namelen != 1)
  286                 return (ENOTDIR);               /* overloaded */
  287 
  288         switch (name[0]) {
  289         case GPROF_STATE:
  290                 state = gp->state;
  291                 error = sysctl_handle_int(oidp, &state, 0, req);
  292                 if (error)
  293                         return (error);
  294                 if (!req->newptr)
  295                         return (0);
  296                 if (state == GMON_PROF_OFF) {
  297                         gp->state = state;
  298                         stopprofclock(&proc0);
  299                         stopguprof(gp);
  300                 } else if (state == GMON_PROF_ON) {
  301                         gp->state = GMON_PROF_OFF;
  302                         stopguprof(gp);
  303                         gp->profrate = profhz;
  304                         startprofclock(&proc0);
  305                         gp->state = state;
  306 #ifdef GUPROF
  307                 } else if (state == GMON_PROF_HIRES) {
  308                         gp->state = GMON_PROF_OFF;
  309                         stopprofclock(&proc0);
  310                         startguprof(gp);
  311                         gp->state = state;
  312 #endif
  313                 } else if (state != gp->state)
  314                         return (EINVAL);
  315                 return (0);
  316         case GPROF_COUNT:
  317                 return (sysctl_handle_opaque(oidp, 
  318                         gp->kcount, gp->kcountsize, req));
  319         case GPROF_FROMS:
  320                 return (sysctl_handle_opaque(oidp, 
  321                         gp->froms, gp->fromssize, req));
  322         case GPROF_TOS:
  323                 return (sysctl_handle_opaque(oidp, 
  324                         gp->tos, gp->tossize, req));
  325         case GPROF_GMONPARAM:
  326                 return (sysctl_handle_opaque(oidp, gp, sizeof *gp, req));
  327         default:
  328                 return (EOPNOTSUPP);
  329         }
  330         /* NOTREACHED */
  331 }
  332 
  333 SYSCTL_NODE(_kern, KERN_PROF, prof, CTLFLAG_RW, sysctl_kern_prof, "");
  334 #endif /* GPROF */
  335 
  336 /*
  337  * Profiling system call.
  338  *
  339  * The scale factor is a fixed point number with 16 bits of fraction, so that
  340  * 1.0 is represented as 0x10000.  A scale factor of 0 turns off profiling.
  341  */
  342 #ifndef _SYS_SYSPROTO_H_
  343 struct profil_args {
  344         caddr_t samples;
  345         u_int   size;
  346         u_int   offset;
  347         u_int   scale;
  348 };
  349 #endif
  350 /* ARGSUSED */
  351 int
  352 profil(p, uap, retval)
  353         struct proc *p;
  354         register struct profil_args *uap;
  355         int *retval;
  356 {
  357         register struct uprof *upp;
  358         int s;
  359 
  360         if (uap->scale > (1 << 16))
  361                 return (EINVAL);
  362         if (uap->scale == 0) {
  363                 stopprofclock(p);
  364                 return (0);
  365         }
  366         upp = &p->p_stats->p_prof;
  367 
  368         /* Block profile interrupts while changing state. */
  369         s = splstatclock();
  370         upp->pr_off = uap->offset;
  371         upp->pr_scale = uap->scale;
  372         upp->pr_base = uap->samples;
  373         upp->pr_size = uap->size;
  374         startprofclock(p);
  375         splx(s);
  376 
  377         return (0);
  378 }
  379 
  380 /*
  381  * Scale is a fixed-point number with the binary point 16 bits
  382  * into the value, and is <= 1.0.  pc is at most 32 bits, so the
  383  * intermediate result is at most 48 bits.
  384  */
  385 #define PC_TO_INDEX(pc, prof) \
  386         ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
  387             (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
  388 
  389 /*
  390  * Collect user-level profiling statistics; called on a profiling tick,
  391  * when a process is running in user-mode.  This routine may be called
  392  * from an interrupt context.  We try to update the user profiling buffers
  393  * cheaply with fuswintr() and suswintr().  If that fails, we revert to
  394  * an AST that will vector us to trap() with a context in which copyin
  395  * and copyout will work.  Trap will then call addupc_task().
  396  *
  397  * Note that we may (rarely) not get around to the AST soon enough, and
  398  * lose profile ticks when the next tick overwrites this one, but in this
  399  * case the system is overloaded and the profile is probably already
  400  * inaccurate.
  401  */
  402 void
  403 addupc_intr(p, pc, ticks)
  404         register struct proc *p;
  405         register u_long pc;
  406         u_int ticks;
  407 {
  408         register struct uprof *prof;
  409         register caddr_t addr;
  410         register u_int i;
  411         register int v;
  412 
  413         if (ticks == 0)
  414                 return;
  415         prof = &p->p_stats->p_prof;
  416         if (pc < prof->pr_off ||
  417             (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
  418                 return;                 /* out of range; ignore */
  419 
  420         addr = prof->pr_base + i;
  421         if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + ticks) == -1) {
  422                 prof->pr_addr = pc;
  423                 prof->pr_ticks = ticks;
  424                 need_proftick(p);
  425         }
  426 }
  427 
  428 /*
  429  * Much like before, but we can afford to take faults here.  If the
  430  * update fails, we simply turn off profiling.
  431  */
  432 void
  433 addupc_task(p, pc, ticks)
  434         register struct proc *p;
  435         register u_long pc;
  436         u_int ticks;
  437 {
  438         register struct uprof *prof;
  439         register caddr_t addr;
  440         register u_int i;
  441         u_short v;
  442 
  443         /* Testing P_PROFIL may be unnecessary, but is certainly safe. */
  444         if ((p->p_flag & P_PROFIL) == 0 || ticks == 0)
  445                 return;
  446 
  447         prof = &p->p_stats->p_prof;
  448         if (pc < prof->pr_off ||
  449             (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
  450                 return;
  451 
  452         addr = prof->pr_base + i;
  453         if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) {
  454                 v += ticks;
  455                 if (copyout((caddr_t)&v, addr, sizeof(v)) == 0)
  456                         return;
  457         }
  458         stopprofclock(p);
  459 }

Cache object: be5f971f58948071ea954eae2d4fb103


[ 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.