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/i386/isa/prof_machdep.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) 1996 Bruce D. Evans.
    3  * 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  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/6.2/sys/i386/isa/prof_machdep.c 146458 2005-05-20 17:16:24Z njl $");
   29 
   30 #include <sys/param.h>
   31 #include <sys/systm.h>
   32 
   33 #include <machine/asmacros.h>
   34 #include <machine/timerreg.h>
   35 
   36 /*
   37  * There are 2 definitions of MCOUNT to have a C version and an asm version
   38  * with the same name and not have LOCORE #ifdefs to distinguish them.
   39  * <machine/profile.h> provides a C version, and <machine/asmacros.h>
   40  * provides an asm version.  To avoid conflicts, #undef the asm version.
   41  */
   42 #undef MCOUNT
   43 
   44 #ifdef GUPROF
   45 #include "opt_i586_guprof.h"
   46 #include "opt_perfmon.h"
   47 
   48 #include <sys/gmon.h>
   49 #include <sys/kernel.h>
   50 #include <sys/sysctl.h>
   51 
   52 #include <machine/clock.h>
   53 #include <machine/perfmon.h>
   54 #include <machine/profile.h>
   55 
   56 #define CPUTIME_CLOCK_UNINITIALIZED     0
   57 #define CPUTIME_CLOCK_I8254             1
   58 #define CPUTIME_CLOCK_TSC               2
   59 #define CPUTIME_CLOCK_I586_PMC          3
   60 #define CPUTIME_CLOCK_I8254_SHIFT       7
   61 
   62 int     cputime_bias = 1;       /* initialize for locality of reference */
   63 
   64 static int      cputime_clock = CPUTIME_CLOCK_UNINITIALIZED;
   65 #ifdef I586_PMC_GUPROF
   66 static u_int    cputime_clock_pmc_conf = I586_PMC_GUPROF;
   67 static int      cputime_clock_pmc_init;
   68 static struct gmonparam saved_gmp;
   69 #endif
   70 #endif /* GUPROF */
   71 
   72 #ifdef __GNUCLIKE_ASM
   73 __asm("                                                         \n\
   74 GM_STATE        =       0                                       \n\
   75 GMON_PROF_OFF   =       3                                       \n\
   76                                                                 \n\
   77         .text                                                   \n\
   78         .p2align 4,0x90                                         \n\
   79         .globl  __mcount                                        \n\
   80         .type   __mcount,@function                              \n\
   81 __mcount:                                                       \n\
   82         #                                                       \n\
   83         # Check that we are profiling.  Do it early for speed.  \n\
   84         #                                                       \n\
   85         cmpl    $GMON_PROF_OFF," __XSTRING(CNAME(_gmonparam)) "+GM_STATE \n\
   86         je      .mcount_exit                                    \n\
   87         #                                                       \n\
   88         # __mcount is the same as [.]mcount except the caller   \n\
   89         # hasn't changed the stack except to call here, so the  \n\
   90         # caller's raddr is above our raddr.                    \n\
   91         #                                                       \n\
   92         movl    4(%esp),%edx                                    \n\
   93         jmp     .got_frompc                                     \n\
   94                                                                 \n\
   95         .p2align 4,0x90                                         \n\
   96         .globl  " __XSTRING(HIDENAME(mcount)) "                 \n\
   97 " __XSTRING(HIDENAME(mcount)) ":                                \n\
   98         .globl  __cyg_profile_func_enter                        \n\
   99 __cyg_profile_func_enter:                                       \n\
  100         cmpl    $GMON_PROF_OFF," __XSTRING(CNAME(_gmonparam)) "+GM_STATE \n\
  101         je      .mcount_exit                                    \n\
  102         #                                                       \n\
  103         # The caller's stack frame has already been built, so   \n\
  104         # %ebp is the caller's frame pointer.  The caller's     \n\
  105         # raddr is in the caller's frame following the caller's \n\
  106         # caller's frame pointer.                               \n\
  107         #                                                       \n\
  108         movl    4(%ebp),%edx                                    \n\
  109 .got_frompc:                                                    \n\
  110         #                                                       \n\
  111         # Our raddr is the caller's pc.                         \n\
  112         #                                                       \n\
  113         movl    (%esp),%eax                                     \n\
  114                                                                 \n\
  115         pushfl                                                  \n\
  116         pushl   %eax                                            \n\
  117         pushl   %edx                                            \n\
  118         cli                                                     \n\
  119         call    " __XSTRING(CNAME(mcount)) "                    \n\
  120         addl    $8,%esp                                         \n\
  121         popfl                                                   \n\
  122 .mcount_exit:                                                   \n\
  123         ret                                                     \n\
  124 ");
  125 #else /* !__GNUCLIKE_ASM */
  126 #error this file needs to be ported to your compiler
  127 #endif /* __GNUCLIKE_ASM */
  128 
  129 #ifdef GUPROF
  130 /*
  131  * [.]mexitcount saves the return register(s), loads selfpc and calls
  132  * mexitcount(selfpc) to do the work.  Someday it should be in a machine
  133  * dependent file together with cputime(), __mcount and [.]mcount.  cputime()
  134  * can't just be put in machdep.c because it has to be compiled without -pg.
  135  */
  136 #ifdef __GNUCLIKE_ASM
  137 __asm("                                                         \n\
  138         .text                                                   \n\
  139 #                                                               \n\
  140 # Dummy label to be seen when gprof -u hides [.]mexitcount.     \n\
  141 #                                                               \n\
  142         .p2align 4,0x90                                         \n\
  143         .globl  __mexitcount                                    \n\
  144         .type   __mexitcount,@function                          \n\
  145 __mexitcount:                                                   \n\
  146         nop                                                     \n\
  147                                                                 \n\
  148 GMON_PROF_HIRES =       4                                       \n\
  149                                                                 \n\
  150         .p2align 4,0x90                                         \n\
  151         .globl  " __XSTRING(HIDENAME(mexitcount)) "             \n\
  152 " __XSTRING(HIDENAME(mexitcount)) ":                            \n\
  153         .globl  __cyg_profile_func_exit                         \n\
  154 __cyg_profile_func_exit:                                        \n\
  155         cmpl    $GMON_PROF_HIRES," __XSTRING(CNAME(_gmonparam)) "+GM_STATE \n\
  156         jne     .mexitcount_exit                                \n\
  157         pushl   %edx                                            \n\
  158         pushl   %eax                                            \n\
  159         movl    8(%esp),%eax                                    \n\
  160         pushfl                                                  \n\
  161         pushl   %eax                                            \n\
  162         cli                                                     \n\
  163         call    " __XSTRING(CNAME(mexitcount)) "                \n\
  164         addl    $4,%esp                                         \n\
  165         popfl                                                   \n\
  166         popl    %eax                                            \n\
  167         popl    %edx                                            \n\
  168 .mexitcount_exit:                                               \n\
  169         ret                                                     \n\
  170 ");
  171 #endif /* __GNUCLIKE_ASM */
  172 
  173 /*
  174  * Return the time elapsed since the last call.  The units are machine-
  175  * dependent.
  176  */
  177 int
  178 cputime()
  179 {
  180         u_int count;
  181         int delta;
  182 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP) && \
  183     defined(PERFMON) && defined(I586_PMC_GUPROF)
  184         u_quad_t event_count;
  185 #endif
  186         u_char high, low;
  187         static u_int prev_count;
  188 
  189 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
  190         if (cputime_clock == CPUTIME_CLOCK_TSC) {
  191                 /*
  192                  * Scale the TSC a little to make cputime()'s frequency
  193                  * fit in an int, assuming that the TSC frequency fits
  194                  * in a u_int.  Use a fixed scale since dynamic scaling
  195                  * would be slower and we can't really use the low bit
  196                  * of precision.
  197                  */
  198                 count = (u_int)rdtsc() & ~1u;
  199                 delta = (int)(count - prev_count) >> 1;
  200                 prev_count = count;
  201                 return (delta);
  202         }
  203 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
  204         if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
  205                 /*
  206                  * XXX permon_read() should be inlined so that the
  207                  * perfmon module doesn't need to be compiled with
  208                  * profiling disabled and so that it is fast.
  209                  */
  210                 perfmon_read(0, &event_count);
  211 
  212                 count = (u_int)event_count;
  213                 delta = (int)(count - prev_count);
  214                 prev_count = count;
  215                 return (delta);
  216         }
  217 #endif /* PERFMON && I586_PMC_GUPROF */
  218 #endif /* (I586_CPU || I686_CPU) && !SMP */
  219 
  220         /*
  221          * Read the current value of the 8254 timer counter 0.
  222          */
  223         outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
  224         low = inb(TIMER_CNTR0);
  225         high = inb(TIMER_CNTR0);
  226         count = ((high << 8) | low) << CPUTIME_CLOCK_I8254_SHIFT;
  227 
  228         /*
  229          * The timer counts down from TIMER_CNTR0_MAX to 0 and then resets.
  230          * While profiling is enabled, this routine is called at least twice
  231          * per timer reset (for mcounting and mexitcounting hardclock()),
  232          * so at most one reset has occurred since the last call, and one
  233          * has occurred iff the current count is larger than the previous
  234          * count.  This allows counter underflow to be detected faster
  235          * than in microtime().
  236          */
  237         delta = prev_count - count;
  238         prev_count = count;
  239         if ((int) delta <= 0)
  240                 return (delta + (timer0_max_count << CPUTIME_CLOCK_I8254_SHIFT));
  241         return (delta);
  242 }
  243 
  244 static int
  245 sysctl_machdep_cputime_clock(SYSCTL_HANDLER_ARGS)
  246 {
  247         int clock;
  248         int error;
  249 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
  250         int event;
  251         struct pmc pmc;
  252 #endif
  253 
  254         clock = cputime_clock;
  255 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
  256         if (clock == CPUTIME_CLOCK_I586_PMC) {
  257                 pmc.pmc_val = cputime_clock_pmc_conf;
  258                 clock += pmc.pmc_event;
  259         }
  260 #endif
  261         error = sysctl_handle_opaque(oidp, &clock, sizeof clock, req);
  262         if (error == 0 && req->newptr != NULL) {
  263 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
  264                 if (clock >= CPUTIME_CLOCK_I586_PMC) {
  265                         event = clock - CPUTIME_CLOCK_I586_PMC;
  266                         if (event >= 256)
  267                                 return (EINVAL);
  268                         pmc.pmc_num = 0;
  269                         pmc.pmc_event = event;
  270                         pmc.pmc_unit = 0;
  271                         pmc.pmc_flags = PMCF_E | PMCF_OS | PMCF_USR;
  272                         pmc.pmc_mask = 0;
  273                         cputime_clock_pmc_conf = pmc.pmc_val;
  274                         cputime_clock = CPUTIME_CLOCK_I586_PMC;
  275                 } else
  276 #endif
  277                 {
  278                         if (clock < 0 || clock >= CPUTIME_CLOCK_I586_PMC)
  279                                 return (EINVAL);
  280                         cputime_clock = clock;
  281                 }
  282         }
  283         return (error);
  284 }
  285 
  286 SYSCTL_PROC(_machdep, OID_AUTO, cputime_clock, CTLTYPE_INT | CTLFLAG_RW,
  287             0, sizeof(u_int), sysctl_machdep_cputime_clock, "I", "");
  288 
  289 /*
  290  * The start and stop routines need not be here since we turn off profiling
  291  * before calling them.  They are here for convenience.
  292  */
  293 
  294 void
  295 startguprof(gp)
  296         struct gmonparam *gp;
  297 {
  298         if (cputime_clock == CPUTIME_CLOCK_UNINITIALIZED) {
  299                 cputime_clock = CPUTIME_CLOCK_I8254;
  300 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
  301                 if (tsc_freq != 0)
  302                         cputime_clock = CPUTIME_CLOCK_TSC;
  303 #endif
  304         }
  305         gp->profrate = timer_freq << CPUTIME_CLOCK_I8254_SHIFT;
  306 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
  307         if (cputime_clock == CPUTIME_CLOCK_TSC)
  308                 gp->profrate = tsc_freq >> 1;
  309 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
  310         else if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
  311                 if (perfmon_avail() &&
  312                     perfmon_setup(0, cputime_clock_pmc_conf) == 0) {
  313                         if (perfmon_start(0) != 0)
  314                                 perfmon_fini(0);
  315                         else {
  316                                 /* XXX 1 event == 1 us. */
  317                                 gp->profrate = 1000000;
  318 
  319                                 saved_gmp = *gp;
  320 
  321                                 /* Zap overheads.  They are invalid. */
  322                                 gp->cputime_overhead = 0;
  323                                 gp->mcount_overhead = 0;
  324                                 gp->mcount_post_overhead = 0;
  325                                 gp->mcount_pre_overhead = 0;
  326                                 gp->mexitcount_overhead = 0;
  327                                 gp->mexitcount_post_overhead = 0;
  328                                 gp->mexitcount_pre_overhead = 0;
  329 
  330                                 cputime_clock_pmc_init = TRUE;
  331                         }
  332                 }
  333         }
  334 #endif /* PERFMON && I586_PMC_GUPROF */
  335 #endif /* (I586_CPU || I686_CPU) && !SMP */
  336         cputime_bias = 0;
  337         cputime();
  338 }
  339 
  340 void
  341 stopguprof(gp)
  342         struct gmonparam *gp;
  343 {
  344 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
  345         if (cputime_clock_pmc_init) {
  346                 *gp = saved_gmp;
  347                 perfmon_fini(0);
  348                 cputime_clock_pmc_init = FALSE;
  349         }
  350 #endif
  351 }
  352 
  353 #else /* !GUPROF */
  354 #ifdef __GNUCLIKE_ASM
  355 __asm("                                                         \n\
  356         .text                                                   \n\
  357         .p2align 4,0x90                                         \n\
  358         .globl  " __XSTRING(HIDENAME(mexitcount)) "             \n\
  359 " __XSTRING(HIDENAME(mexitcount)) ":                            \n\
  360         ret                                                     \n\
  361 ");
  362 #endif /* __GNUCLIKE_ASM */
  363 #endif /* GUPROF */

Cache object: 0b49afb0663781c2e4f5b80e26da12fa


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