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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
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  * $FreeBSD: releng/5.1/sys/i386/isa/prof_machdep.c 99932 2002-07-13 22:28:34Z bde $
   27  */
   28 
   29 #ifdef GUPROF
   30 #include "opt_i586_guprof.h"
   31 #include "opt_perfmon.h"
   32 
   33 #include <sys/param.h>
   34 #include <sys/systm.h>
   35 #include <sys/gmon.h>
   36 #include <sys/kernel.h>
   37 #include <sys/sysctl.h>
   38 
   39 #include <machine/clock.h>
   40 #include <machine/perfmon.h>
   41 #include <machine/profile.h>
   42 #undef MCOUNT
   43 #endif
   44 
   45 #include <machine/asmacros.h>
   46 
   47 #ifdef PC98
   48 #include <pc98/pc98/pc98.h>
   49 #else
   50 #include <i386/isa/isa.h>
   51 #endif
   52 #include <i386/isa/timerreg.h>
   53 
   54 #ifdef GUPROF
   55 #define CPUTIME_CLOCK_UNINITIALIZED     0
   56 #define CPUTIME_CLOCK_I8254             1
   57 #define CPUTIME_CLOCK_TSC               2
   58 #define CPUTIME_CLOCK_I586_PMC          3
   59 #define CPUTIME_CLOCK_I8254_SHIFT       7
   60 
   61 int     cputime_bias = 1;       /* initialize for locality of reference */
   62 
   63 static int      cputime_clock = CPUTIME_CLOCK_UNINITIALIZED;
   64 #ifdef I586_PMC_GUPROF
   65 static u_int    cputime_clock_pmc_conf = I586_PMC_GUPROF;
   66 static int      cputime_clock_pmc_init;
   67 static struct gmonparam saved_gmp;
   68 #endif
   69 #endif /* GUPROF */
   70 
   71 #ifdef __GNUC__
   72 __asm("                                                         \n\
   73 GM_STATE        =       0                                       \n\
   74 GMON_PROF_OFF   =       3                                       \n\
   75                                                                 \n\
   76         .text                                                   \n\
   77         .p2align 4,0x90                                         \n\
   78         .globl  __mcount                                        \n\
   79         .type   __mcount,@function                              \n\
   80 __mcount:                                                       \n\
   81         #                                                       \n\
   82         # Check that we are profiling.  Do it early for speed.  \n\
   83         #                                                       \n\
   84         cmpl    $GMON_PROF_OFF," __XSTRING(CNAME(_gmonparam)) "+GM_STATE \n\
   85         je      .mcount_exit                                    \n\
   86         #                                                       \n\
   87         # __mcount is the same as [.]mcount except the caller   \n\
   88         # hasn't changed the stack except to call here, so the  \n\
   89         # caller's raddr is above our raddr.                    \n\
   90         #                                                       \n\
   91         movl    4(%esp),%edx                                    \n\
   92         jmp     .got_frompc                                     \n\
   93                                                                 \n\
   94         .p2align 4,0x90                                         \n\
   95         .globl  " __XSTRING(HIDENAME(mcount)) "                 \n\
   96 " __XSTRING(HIDENAME(mcount)) ":                                \n\
   97         .globl  __cyg_profile_func_enter                        \n\
   98 __cyg_profile_func_enter:                                       \n\
   99         cmpl    $GMON_PROF_OFF," __XSTRING(CNAME(_gmonparam)) "+GM_STATE \n\
  100         je      .mcount_exit                                    \n\
  101         #                                                       \n\
  102         # The caller's stack frame has already been built, so   \n\
  103         # %ebp is the caller's frame pointer.  The caller's     \n\
  104         # raddr is in the caller's frame following the caller's \n\
  105         # caller's frame pointer.                               \n\
  106         #                                                       \n\
  107         movl    4(%ebp),%edx                                    \n\
  108 .got_frompc:                                                    \n\
  109         #                                                       \n\
  110         # Our raddr is the caller's pc.                         \n\
  111         #                                                       \n\
  112         movl    (%esp),%eax                                     \n\
  113                                                                 \n\
  114         pushfl                                                  \n\
  115         pushl   %eax                                            \n\
  116         pushl   %edx                                            \n\
  117         cli                                                     \n\
  118         call    " __XSTRING(CNAME(mcount)) "                    \n\
  119         addl    $8,%esp                                         \n\
  120         popfl                                                   \n\
  121 .mcount_exit:                                                   \n\
  122         ret                                                     \n\
  123 ");
  124 #else /* !__GNUC__ */
  125 #error
  126 #endif /* __GNUC__ */
  127 
  128 #ifdef GUPROF
  129 /*
  130  * [.]mexitcount saves the return register(s), loads selfpc and calls
  131  * mexitcount(selfpc) to do the work.  Someday it should be in a machine
  132  * dependent file together with cputime(), __mcount and [.]mcount.  cputime()
  133  * can't just be put in machdep.c because it has to be compiled without -pg.
  134  */
  135 #ifdef __GNUC__
  136 __asm("                                                         \n\
  137         .text                                                   \n\
  138 #                                                               \n\
  139 # Dummy label to be seen when gprof -u hides [.]mexitcount.     \n\
  140 #                                                               \n\
  141         .p2align 4,0x90                                         \n\
  142         .globl  __mexitcount                                    \n\
  143         .type   __mexitcount,@function                          \n\
  144 __mexitcount:                                                   \n\
  145         nop                                                     \n\
  146                                                                 \n\
  147 GMON_PROF_HIRES =       4                                       \n\
  148                                                                 \n\
  149         .p2align 4,0x90                                         \n\
  150         .globl  " __XSTRING(HIDENAME(mexitcount)) "             \n\
  151 " __XSTRING(HIDENAME(mexitcount)) ":                            \n\
  152         .globl  __cyg_profile_func_exit                         \n\
  153 __cyg_profile_func_exit:                                        \n\
  154         cmpl    $GMON_PROF_HIRES," __XSTRING(CNAME(_gmonparam)) "+GM_STATE \n\
  155         jne     .mexitcount_exit                                \n\
  156         pushl   %edx                                            \n\
  157         pushl   %eax                                            \n\
  158         movl    8(%esp),%eax                                    \n\
  159         pushfl                                                  \n\
  160         pushl   %eax                                            \n\
  161         cli                                                     \n\
  162         call    " __XSTRING(CNAME(mexitcount)) "                \n\
  163         addl    $4,%esp                                         \n\
  164         popfl                                                   \n\
  165         popl    %eax                                            \n\
  166         popl    %edx                                            \n\
  167 .mexitcount_exit:                                               \n\
  168         ret                                                     \n\
  169 ");
  170 #else /* !__GNUC__ */
  171 #error
  172 #endif /* __GNUC__ */
  173 
  174 /*
  175  * Return the time elapsed since the last call.  The units are machine-
  176  * dependent.
  177  */
  178 int
  179 cputime()
  180 {
  181         u_int count;
  182         int delta;
  183 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP) && \
  184     defined(PERFMON) && defined(I586_PMC_GUPROF)
  185         u_quad_t event_count;
  186 #endif
  187         u_char high, low;
  188         static u_int prev_count;
  189 
  190 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
  191         if (cputime_clock == CPUTIME_CLOCK_TSC) {
  192                 count = (u_int)rdtsc();
  193                 delta = (int)(count - prev_count);
  194                 prev_count = count;
  195                 return (delta);
  196         }
  197 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
  198         if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
  199                 /*
  200                  * XXX permon_read() should be inlined so that the
  201                  * perfmon module doesn't need to be compiled with
  202                  * profiling disabled and so that it is fast.
  203                  */
  204                 perfmon_read(0, &event_count);
  205 
  206                 count = (u_int)event_count;
  207                 delta = (int)(count - prev_count);
  208                 prev_count = count;
  209                 return (delta);
  210         }
  211 #endif /* PERFMON && I586_PMC_GUPROF */
  212 #endif /* (I586_CPU || I686_CPU) && !SMP */
  213 
  214         /*
  215          * Read the current value of the 8254 timer counter 0.
  216          */
  217         outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
  218         low = inb(TIMER_CNTR0);
  219         high = inb(TIMER_CNTR0);
  220         count = ((high << 8) | low) << CPUTIME_CLOCK_I8254_SHIFT;
  221 
  222         /*
  223          * The timer counts down from TIMER_CNTR0_MAX to 0 and then resets.
  224          * While profiling is enabled, this routine is called at least twice
  225          * per timer reset (for mcounting and mexitcounting hardclock()),
  226          * so at most one reset has occurred since the last call, and one
  227          * has occurred iff the current count is larger than the previous
  228          * count.  This allows counter underflow to be detected faster
  229          * than in microtime().
  230          */
  231         delta = prev_count - count;
  232         prev_count = count;
  233         if ((int) delta <= 0)
  234                 return (delta + (timer0_max_count << CPUTIME_CLOCK_I8254_SHIFT));
  235         return (delta);
  236 }
  237 
  238 static int
  239 sysctl_machdep_cputime_clock(SYSCTL_HANDLER_ARGS)
  240 {
  241         int clock;
  242         int error;
  243 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
  244         int event;
  245         struct pmc pmc;
  246 #endif
  247 
  248         clock = cputime_clock;
  249 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
  250         if (clock == CPUTIME_CLOCK_I586_PMC) {
  251                 pmc.pmc_val = cputime_clock_pmc_conf;
  252                 clock += pmc.pmc_event;
  253         }
  254 #endif
  255         error = sysctl_handle_opaque(oidp, &clock, sizeof clock, req);
  256         if (error == 0 && req->newptr != NULL) {
  257 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
  258                 if (clock >= CPUTIME_CLOCK_I586_PMC) {
  259                         event = clock - CPUTIME_CLOCK_I586_PMC;
  260                         if (event >= 256)
  261                                 return (EINVAL);
  262                         pmc.pmc_num = 0;
  263                         pmc.pmc_event = event;
  264                         pmc.pmc_unit = 0;
  265                         pmc.pmc_flags = PMCF_E | PMCF_OS | PMCF_USR;
  266                         pmc.pmc_mask = 0;
  267                         cputime_clock_pmc_conf = pmc.pmc_val;
  268                         cputime_clock = CPUTIME_CLOCK_I586_PMC;
  269                 } else
  270 #endif
  271                 {
  272                         if (clock < 0 || clock >= CPUTIME_CLOCK_I586_PMC)
  273                                 return (EINVAL);
  274                         cputime_clock = clock;
  275                 }
  276         }
  277         return (error);
  278 }
  279 
  280 SYSCTL_PROC(_machdep, OID_AUTO, cputime_clock, CTLTYPE_INT | CTLFLAG_RW,
  281             0, sizeof(u_int), sysctl_machdep_cputime_clock, "I", "");
  282 
  283 /*
  284  * The start and stop routines need not be here since we turn off profiling
  285  * before calling them.  They are here for convenience.
  286  */
  287 
  288 void
  289 startguprof(gp)
  290         struct gmonparam *gp;
  291 {
  292         if (cputime_clock == CPUTIME_CLOCK_UNINITIALIZED) {
  293                 cputime_clock = CPUTIME_CLOCK_I8254;
  294 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
  295                 if (tsc_freq != 0)
  296                         cputime_clock = CPUTIME_CLOCK_TSC;
  297 #endif
  298         }
  299         gp->profrate = timer_freq << CPUTIME_CLOCK_I8254_SHIFT;
  300 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP)
  301         if (cputime_clock == CPUTIME_CLOCK_TSC)
  302                 gp->profrate = tsc_freq;
  303 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
  304         else if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
  305                 if (perfmon_avail() &&
  306                     perfmon_setup(0, cputime_clock_pmc_conf) == 0) {
  307                         if (perfmon_start(0) != 0)
  308                                 perfmon_fini(0);
  309                         else {
  310                                 /* XXX 1 event == 1 us. */
  311                                 gp->profrate = 1000000;
  312 
  313                                 saved_gmp = *gp;
  314 
  315                                 /* Zap overheads.  They are invalid. */
  316                                 gp->cputime_overhead = 0;
  317                                 gp->mcount_overhead = 0;
  318                                 gp->mcount_post_overhead = 0;
  319                                 gp->mcount_pre_overhead = 0;
  320                                 gp->mexitcount_overhead = 0;
  321                                 gp->mexitcount_post_overhead = 0;
  322                                 gp->mexitcount_pre_overhead = 0;
  323 
  324                                 cputime_clock_pmc_init = TRUE;
  325                         }
  326                 }
  327         }
  328 #endif /* PERFMON && I586_PMC_GUPROF */
  329 #endif /* (I586_CPU || I686_CPU) && !SMP */
  330         cputime_bias = 0;
  331         cputime();
  332 }
  333 
  334 void
  335 stopguprof(gp)
  336         struct gmonparam *gp;
  337 {
  338 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
  339         if (cputime_clock_pmc_init) {
  340                 *gp = saved_gmp;
  341                 perfmon_fini(0);
  342                 cputime_clock_pmc_init = FALSE;
  343         }
  344 #endif
  345 }
  346 
  347 #else /* !GUPROF */
  348 #ifdef __GNUC__
  349 __asm("                                                         \n\
  350         .text                                                   \n\
  351         .p2align 4,0x90                                         \n\
  352         .globl  " __XSTRING(HIDENAME(mexitcount)) "             \n\
  353 " __XSTRING(HIDENAME(mexitcount)) ":                            \n\
  354         ret                                                     \n\
  355 ");
  356 #else /* !__GNUC__ */
  357 #error
  358 #endif /* __GNUC__ */
  359 #endif /* GUPROF */

Cache object: 699becf6aec04c492c523908bcd2ab21


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