1df8bae1dSRodney W. Grimes /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33df8bae1dSRodney W. Grimes * @(#)subr_prof.c 8.3 (Berkeley) 9/23/93 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #include <sys/param.h> 38df8bae1dSRodney W. Grimes #include <sys/systm.h> 39d2d3e875SBruce Evans #include <sys/sysproto.h> 4035e0e5b3SJohn Baldwin #include <sys/ipl.h> 419e420850SBruce Evans #include <sys/kernel.h> 42fb919e4dSMark Murray #include <sys/lock.h> 43fb919e4dSMark Murray #include <sys/mutex.h> 44df8bae1dSRodney W. Grimes #include <sys/proc.h> 452baeef32SBruce Evans #include <sys/resourcevar.h> 469e420850SBruce Evans #include <sys/sysctl.h> 47b5e8ce9fSBruce Evans 48df8bae1dSRodney W. Grimes #include <machine/cpu.h> 49df8bae1dSRodney W. Grimes 50df8bae1dSRodney W. Grimes #ifdef GPROF 51df8bae1dSRodney W. Grimes #include <sys/malloc.h> 52df8bae1dSRodney W. Grimes #include <sys/gmon.h> 53ea2b3e3dSBruce Evans #undef MCOUNT 54df8bae1dSRodney W. Grimes 55a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_GPROF, "gprof", "kernel profiling buffer"); 5655166637SPoul-Henning Kamp 574590fd3aSDavid Greenman static void kmstartup __P((void *)); 582b14f991SJulian Elischer SYSINIT(kmem, SI_SUB_KPROF, SI_ORDER_FIRST, kmstartup, NULL) 592b14f991SJulian Elischer 60df8bae1dSRodney W. Grimes struct gmonparam _gmonparam = { GMON_PROF_OFF }; 61df8bae1dSRodney W. Grimes 62d6b9e17eSBruce Evans #ifdef GUPROF 63ea2b3e3dSBruce Evans #include <machine/asmacros.h> 64ea2b3e3dSBruce Evans 65d6b9e17eSBruce Evans void 66d6b9e17eSBruce Evans nullfunc_loop_profiled() 67d6b9e17eSBruce Evans { 68d6b9e17eSBruce Evans int i; 69d6b9e17eSBruce Evans 70d6b9e17eSBruce Evans for (i = 0; i < CALIB_SCALE; i++) 71d6b9e17eSBruce Evans nullfunc_profiled(); 72d6b9e17eSBruce Evans } 73d6b9e17eSBruce Evans 74e408eccfSBruce Evans #define nullfunc_loop_profiled_end nullfunc_profiled /* XXX */ 75e408eccfSBruce Evans 76d6b9e17eSBruce Evans void 77d6b9e17eSBruce Evans nullfunc_profiled() 78d6b9e17eSBruce Evans { 79d6b9e17eSBruce Evans } 80d6b9e17eSBruce Evans #endif /* GUPROF */ 81d6b9e17eSBruce Evans 825c7761b2SBruce Evans static void 83d841aaa7SBruce Evans kmstartup(dummy) 84d841aaa7SBruce Evans void *dummy; 85df8bae1dSRodney W. Grimes { 86df8bae1dSRodney W. Grimes char *cp; 87df8bae1dSRodney W. Grimes struct gmonparam *p = &_gmonparam; 88912e6037SBruce Evans #ifdef GUPROF 89d6b9e17eSBruce Evans int cputime_overhead; 90d6b9e17eSBruce Evans int empty_loop_time; 91912e6037SBruce Evans int i; 92d6b9e17eSBruce Evans int mcount_overhead; 93d6b9e17eSBruce Evans int mexitcount_overhead; 94d6b9e17eSBruce Evans int nullfunc_loop_overhead; 95d6b9e17eSBruce Evans int nullfunc_loop_profiled_time; 9637889b39SBruce Evans uintfptr_t tmp_addr; 970006681fSJohn Baldwin critical_t savecrit; 985e1aea9fSPoul-Henning Kamp #endif 99912e6037SBruce Evans 100df8bae1dSRodney W. Grimes /* 101df8bae1dSRodney W. Grimes * Round lowpc and highpc to multiples of the density we're using 102df8bae1dSRodney W. Grimes * so the rest of the scaling (here and in gprof) stays in ints. 103df8bae1dSRodney W. Grimes */ 104e45d35c3SBruce Evans p->lowpc = ROUNDDOWN((u_long)btext, HISTFRACTION * sizeof(HISTCOUNTER)); 105df8bae1dSRodney W. Grimes p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER)); 106df8bae1dSRodney W. Grimes p->textsize = p->highpc - p->lowpc; 107d6b9e17eSBruce Evans printf("Profiling kernel, textsize=%lu [%x..%x]\n", 108df8bae1dSRodney W. Grimes p->textsize, p->lowpc, p->highpc); 109df8bae1dSRodney W. Grimes p->kcountsize = p->textsize / HISTFRACTION; 110df8bae1dSRodney W. Grimes p->hashfraction = HASHFRACTION; 111df8bae1dSRodney W. Grimes p->fromssize = p->textsize / HASHFRACTION; 112df8bae1dSRodney W. Grimes p->tolimit = p->textsize * ARCDENSITY / 100; 113df8bae1dSRodney W. Grimes if (p->tolimit < MINARCS) 114df8bae1dSRodney W. Grimes p->tolimit = MINARCS; 115df8bae1dSRodney W. Grimes else if (p->tolimit > MAXARCS) 116df8bae1dSRodney W. Grimes p->tolimit = MAXARCS; 117df8bae1dSRodney W. Grimes p->tossize = p->tolimit * sizeof(struct tostruct); 118df8bae1dSRodney W. Grimes cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize, 1197cc0979fSDavid Malone M_GPROF, M_NOWAIT | M_ZERO); 120df8bae1dSRodney W. Grimes if (cp == 0) { 121df8bae1dSRodney W. Grimes printf("No memory for profiling.\n"); 122df8bae1dSRodney W. Grimes return; 123df8bae1dSRodney W. Grimes } 124df8bae1dSRodney W. Grimes p->tos = (struct tostruct *)cp; 125df8bae1dSRodney W. Grimes cp += p->tossize; 126912e6037SBruce Evans p->kcount = (HISTCOUNTER *)cp; 127df8bae1dSRodney W. Grimes cp += p->kcountsize; 128df8bae1dSRodney W. Grimes p->froms = (u_short *)cp; 129912e6037SBruce Evans 130912e6037SBruce Evans #ifdef GUPROF 131d6b9e17eSBruce Evans /* Initialize pointers to overhead counters. */ 132912e6037SBruce Evans p->cputime_count = &KCOUNT(p, PC_TO_I(p, cputime)); 133912e6037SBruce Evans p->mcount_count = &KCOUNT(p, PC_TO_I(p, mcount)); 134912e6037SBruce Evans p->mexitcount_count = &KCOUNT(p, PC_TO_I(p, mexitcount)); 135912e6037SBruce Evans 136912e6037SBruce Evans /* 137d6b9e17eSBruce Evans * Disable interrupts to avoid interference while we calibrate 138d6b9e17eSBruce Evans * things. 139912e6037SBruce Evans */ 1400006681fSJohn Baldwin savecrit = critical_enter(); 141d6b9e17eSBruce Evans 142d6b9e17eSBruce Evans /* 143d6b9e17eSBruce Evans * Determine overheads. 144d6b9e17eSBruce Evans * XXX this needs to be repeated for each useful timer/counter. 145d6b9e17eSBruce Evans */ 146d6b9e17eSBruce Evans cputime_overhead = 0; 147d6b9e17eSBruce Evans startguprof(p); 148d6b9e17eSBruce Evans for (i = 0; i < CALIB_SCALE; i++) 149d6b9e17eSBruce Evans cputime_overhead += cputime(); 150d6b9e17eSBruce Evans 151d6b9e17eSBruce Evans empty_loop(); 152d6b9e17eSBruce Evans startguprof(p); 153d6b9e17eSBruce Evans empty_loop(); 154d6b9e17eSBruce Evans empty_loop_time = cputime(); 155d6b9e17eSBruce Evans 156d6b9e17eSBruce Evans nullfunc_loop_profiled(); 157d6b9e17eSBruce Evans 158d6b9e17eSBruce Evans /* 159d6b9e17eSBruce Evans * Start profiling. There won't be any normal function calls since 160d6b9e17eSBruce Evans * interrupts are disabled, but we will call the profiling routines 161d6b9e17eSBruce Evans * directly to determine their overheads. 162d6b9e17eSBruce Evans */ 163912e6037SBruce Evans p->state = GMON_PROF_HIRES; 164912e6037SBruce Evans 165d6b9e17eSBruce Evans startguprof(p); 166d6b9e17eSBruce Evans nullfunc_loop_profiled(); 167912e6037SBruce Evans 168d6b9e17eSBruce Evans startguprof(p); 169912e6037SBruce Evans for (i = 0; i < CALIB_SCALE; i++) 17077849078SBruce Evans #if defined(__i386__) && __GNUC__ >= 2 171c1087c13SBruce Evans __asm("pushl %0; call __mcount; popl %%ecx" 172912e6037SBruce Evans : 173d6b9e17eSBruce Evans : "i" (profil) 174912e6037SBruce Evans : "ax", "bx", "cx", "dx", "memory"); 175912e6037SBruce Evans #else 176912e6037SBruce Evans #error 177912e6037SBruce Evans #endif 178d6b9e17eSBruce Evans mcount_overhead = KCOUNT(p, PC_TO_I(p, profil)); 179912e6037SBruce Evans 180d6b9e17eSBruce Evans startguprof(p); 181912e6037SBruce Evans for (i = 0; i < CALIB_SCALE; i++) 18277849078SBruce Evans #if defined(__i386__) && __GNUC__ >= 2 183ea2b3e3dSBruce Evans __asm("call " __XSTRING(HIDENAME(mexitcount)) "; 1:" 184912e6037SBruce Evans : : : "ax", "bx", "cx", "dx", "memory"); 185c1087c13SBruce Evans __asm("movl $1b,%0" : "=rm" (tmp_addr)); 186912e6037SBruce Evans #else 187912e6037SBruce Evans #error 188912e6037SBruce Evans #endif 189e408eccfSBruce Evans mexitcount_overhead = KCOUNT(p, PC_TO_I(p, tmp_addr)); 190912e6037SBruce Evans 191912e6037SBruce Evans p->state = GMON_PROF_OFF; 192d6b9e17eSBruce Evans stopguprof(p); 193d6b9e17eSBruce Evans 1940006681fSJohn Baldwin critical_exit(savecrit); 195912e6037SBruce Evans 196d6b9e17eSBruce Evans nullfunc_loop_profiled_time = 0; 19737889b39SBruce Evans for (tmp_addr = (uintfptr_t)nullfunc_loop_profiled; 19837889b39SBruce Evans tmp_addr < (uintfptr_t)nullfunc_loop_profiled_end; 199e408eccfSBruce Evans tmp_addr += HISTFRACTION * sizeof(HISTCOUNTER)) 200e408eccfSBruce Evans nullfunc_loop_profiled_time += KCOUNT(p, PC_TO_I(p, tmp_addr)); 201d6b9e17eSBruce Evans #define CALIB_DOSCALE(count) (((count) + CALIB_SCALE / 3) / CALIB_SCALE) 202d6b9e17eSBruce Evans #define c2n(count, freq) ((int)((count) * 1000000000LL / freq)) 203d6b9e17eSBruce Evans printf("cputime %d, empty_loop %d, nullfunc_loop_profiled %d, mcount %d, mexitcount %d\n", 204d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(cputime_overhead, p->profrate)), 205d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(empty_loop_time, p->profrate)), 206d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(nullfunc_loop_profiled_time, p->profrate)), 207d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(mcount_overhead, p->profrate)), 208d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(mexitcount_overhead, p->profrate))); 209d6b9e17eSBruce Evans cputime_overhead -= empty_loop_time; 210d6b9e17eSBruce Evans mcount_overhead -= empty_loop_time; 211d6b9e17eSBruce Evans mexitcount_overhead -= empty_loop_time; 212d6b9e17eSBruce Evans 213d6b9e17eSBruce Evans /*- 214d6b9e17eSBruce Evans * Profiling overheads are determined by the times between the 215d6b9e17eSBruce Evans * following events: 216d6b9e17eSBruce Evans * MC1: mcount() is called 217d6b9e17eSBruce Evans * MC2: cputime() (called from mcount()) latches the timer 218d6b9e17eSBruce Evans * MC3: mcount() completes 219d6b9e17eSBruce Evans * ME1: mexitcount() is called 220d6b9e17eSBruce Evans * ME2: cputime() (called from mexitcount()) latches the timer 221d6b9e17eSBruce Evans * ME3: mexitcount() completes. 222d6b9e17eSBruce Evans * The times between the events vary slightly depending on instruction 223d6b9e17eSBruce Evans * combination and cache misses, etc. Attempt to determine the 224d6b9e17eSBruce Evans * minimum times. These can be subtracted from the profiling times 225d6b9e17eSBruce Evans * without much risk of reducing the profiling times below what they 226d6b9e17eSBruce Evans * would be when profiling is not configured. Abbreviate: 227d6b9e17eSBruce Evans * ab = minimum time between MC1 and MC3 228d6b9e17eSBruce Evans * a = minumum time between MC1 and MC2 229d6b9e17eSBruce Evans * b = minimum time between MC2 and MC3 230d6b9e17eSBruce Evans * cd = minimum time between ME1 and ME3 231d6b9e17eSBruce Evans * c = minimum time between ME1 and ME2 232d6b9e17eSBruce Evans * d = minimum time between ME2 and ME3. 233d6b9e17eSBruce Evans * These satisfy the relations: 234d6b9e17eSBruce Evans * ab <= mcount_overhead (just measured) 235d6b9e17eSBruce Evans * a + b <= ab 236d6b9e17eSBruce Evans * cd <= mexitcount_overhead (just measured) 237d6b9e17eSBruce Evans * c + d <= cd 238d6b9e17eSBruce Evans * a + d <= nullfunc_loop_profiled_time (just measured) 239d6b9e17eSBruce Evans * a >= 0, b >= 0, c >= 0, d >= 0. 240d6b9e17eSBruce Evans * Assume that ab and cd are equal to the minimums. 241d6b9e17eSBruce Evans */ 242d6b9e17eSBruce Evans p->cputime_overhead = CALIB_DOSCALE(cputime_overhead); 243d6b9e17eSBruce Evans p->mcount_overhead = CALIB_DOSCALE(mcount_overhead - cputime_overhead); 244d6b9e17eSBruce Evans p->mexitcount_overhead = CALIB_DOSCALE(mexitcount_overhead 245d6b9e17eSBruce Evans - cputime_overhead); 246d6b9e17eSBruce Evans nullfunc_loop_overhead = nullfunc_loop_profiled_time - empty_loop_time; 247d6b9e17eSBruce Evans p->mexitcount_post_overhead = CALIB_DOSCALE((mcount_overhead 248d6b9e17eSBruce Evans - nullfunc_loop_overhead) 249d6b9e17eSBruce Evans / 4); 250d6b9e17eSBruce Evans p->mexitcount_pre_overhead = p->mexitcount_overhead 251d6b9e17eSBruce Evans + p->cputime_overhead 252d6b9e17eSBruce Evans - p->mexitcount_post_overhead; 253d6b9e17eSBruce Evans p->mcount_pre_overhead = CALIB_DOSCALE(nullfunc_loop_overhead) 254d6b9e17eSBruce Evans - p->mexitcount_post_overhead; 255d6b9e17eSBruce Evans p->mcount_post_overhead = p->mcount_overhead 256d6b9e17eSBruce Evans + p->cputime_overhead 257d6b9e17eSBruce Evans - p->mcount_pre_overhead; 258d6b9e17eSBruce Evans printf( 259d6b9e17eSBruce Evans "Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d nsec\n", 260d6b9e17eSBruce Evans c2n(p->cputime_overhead, p->profrate), 261d6b9e17eSBruce Evans c2n(p->mcount_overhead, p->profrate), 262d6b9e17eSBruce Evans c2n(p->mcount_pre_overhead, p->profrate), 263d6b9e17eSBruce Evans c2n(p->mcount_post_overhead, p->profrate), 264d6b9e17eSBruce Evans c2n(p->cputime_overhead, p->profrate), 265d6b9e17eSBruce Evans c2n(p->mexitcount_overhead, p->profrate), 266d6b9e17eSBruce Evans c2n(p->mexitcount_pre_overhead, p->profrate), 267d6b9e17eSBruce Evans c2n(p->mexitcount_post_overhead, p->profrate)); 268d6b9e17eSBruce Evans printf( 269d6b9e17eSBruce Evans "Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d cycles\n", 270d6b9e17eSBruce Evans p->cputime_overhead, p->mcount_overhead, 271d6b9e17eSBruce Evans p->mcount_pre_overhead, p->mcount_post_overhead, 272d6b9e17eSBruce Evans p->cputime_overhead, p->mexitcount_overhead, 273d6b9e17eSBruce Evans p->mexitcount_pre_overhead, p->mexitcount_post_overhead); 274912e6037SBruce Evans #endif /* GUPROF */ 275df8bae1dSRodney W. Grimes } 276df8bae1dSRodney W. Grimes 277df8bae1dSRodney W. Grimes /* 278df8bae1dSRodney W. Grimes * Return kernel profiling information. 279df8bae1dSRodney W. Grimes */ 2804b2af45fSPoul-Henning Kamp static int 28182d9ae4eSPoul-Henning Kamp sysctl_kern_prof(SYSCTL_HANDLER_ARGS) 282df8bae1dSRodney W. Grimes { 2834b2af45fSPoul-Henning Kamp int *name = (int *) arg1; 2844b2af45fSPoul-Henning Kamp u_int namelen = arg2; 285df8bae1dSRodney W. Grimes struct gmonparam *gp = &_gmonparam; 286df8bae1dSRodney W. Grimes int error; 287912e6037SBruce Evans int state; 288df8bae1dSRodney W. Grimes 289df8bae1dSRodney W. Grimes /* all sysctl names at this level are terminal */ 290df8bae1dSRodney W. Grimes if (namelen != 1) 291df8bae1dSRodney W. Grimes return (ENOTDIR); /* overloaded */ 292df8bae1dSRodney W. Grimes 293df8bae1dSRodney W. Grimes switch (name[0]) { 294df8bae1dSRodney W. Grimes case GPROF_STATE: 295912e6037SBruce Evans state = gp->state; 296912e6037SBruce Evans error = sysctl_handle_int(oidp, &state, 0, req); 297df8bae1dSRodney W. Grimes if (error) 298df8bae1dSRodney W. Grimes return (error); 299912e6037SBruce Evans if (!req->newptr) 300912e6037SBruce Evans return (0); 301912e6037SBruce Evans if (state == GMON_PROF_OFF) { 302d6b9e17eSBruce Evans gp->state = state; 303df8bae1dSRodney W. Grimes stopprofclock(&proc0); 304d6b9e17eSBruce Evans stopguprof(gp); 305912e6037SBruce Evans } else if (state == GMON_PROF_ON) { 306d6b9e17eSBruce Evans gp->state = GMON_PROF_OFF; 307d6b9e17eSBruce Evans stopguprof(gp); 308912e6037SBruce Evans gp->profrate = profhz; 309df8bae1dSRodney W. Grimes startprofclock(&proc0); 310d6b9e17eSBruce Evans gp->state = state; 311912e6037SBruce Evans #ifdef GUPROF 312912e6037SBruce Evans } else if (state == GMON_PROF_HIRES) { 313d6b9e17eSBruce Evans gp->state = GMON_PROF_OFF; 314912e6037SBruce Evans stopprofclock(&proc0); 315d6b9e17eSBruce Evans startguprof(gp); 316912e6037SBruce Evans gp->state = state; 317912e6037SBruce Evans #endif 318912e6037SBruce Evans } else if (state != gp->state) 319912e6037SBruce Evans return (EINVAL); 320df8bae1dSRodney W. Grimes return (0); 321df8bae1dSRodney W. Grimes case GPROF_COUNT: 3224b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, 3234b2af45fSPoul-Henning Kamp gp->kcount, gp->kcountsize, req)); 324df8bae1dSRodney W. Grimes case GPROF_FROMS: 3254b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, 3264b2af45fSPoul-Henning Kamp gp->froms, gp->fromssize, req)); 327df8bae1dSRodney W. Grimes case GPROF_TOS: 3284b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, 3294b2af45fSPoul-Henning Kamp gp->tos, gp->tossize, req)); 330df8bae1dSRodney W. Grimes case GPROF_GMONPARAM: 3314b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, gp, sizeof *gp, req)); 332df8bae1dSRodney W. Grimes default: 333df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 334df8bae1dSRodney W. Grimes } 335df8bae1dSRodney W. Grimes /* NOTREACHED */ 336df8bae1dSRodney W. Grimes } 3374b2af45fSPoul-Henning Kamp 3384b2af45fSPoul-Henning Kamp SYSCTL_NODE(_kern, KERN_PROF, prof, CTLFLAG_RW, sysctl_kern_prof, ""); 339df8bae1dSRodney W. Grimes #endif /* GPROF */ 340df8bae1dSRodney W. Grimes 341df8bae1dSRodney W. Grimes /* 342df8bae1dSRodney W. Grimes * Profiling system call. 343df8bae1dSRodney W. Grimes * 344df8bae1dSRodney W. Grimes * The scale factor is a fixed point number with 16 bits of fraction, so that 345df8bae1dSRodney W. Grimes * 1.0 is represented as 0x10000. A scale factor of 0 turns off profiling. 346df8bae1dSRodney W. Grimes */ 347d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 348df8bae1dSRodney W. Grimes struct profil_args { 349df8bae1dSRodney W. Grimes caddr_t samples; 350134e06feSBruce Evans size_t size; 351134e06feSBruce Evans size_t offset; 352df8bae1dSRodney W. Grimes u_int scale; 353df8bae1dSRodney W. Grimes }; 354d2d3e875SBruce Evans #endif 355df8bae1dSRodney W. Grimes /* ARGSUSED */ 35626f9a767SRodney W. Grimes int 357cb226aaaSPoul-Henning Kamp profil(p, uap) 358df8bae1dSRodney W. Grimes struct proc *p; 359df8bae1dSRodney W. Grimes register struct profil_args *uap; 360df8bae1dSRodney W. Grimes { 361df8bae1dSRodney W. Grimes register struct uprof *upp; 362df8bae1dSRodney W. Grimes int s; 363df8bae1dSRodney W. Grimes 364df8bae1dSRodney W. Grimes if (uap->scale > (1 << 16)) 365df8bae1dSRodney W. Grimes return (EINVAL); 366df8bae1dSRodney W. Grimes if (uap->scale == 0) { 367df8bae1dSRodney W. Grimes stopprofclock(p); 368df8bae1dSRodney W. Grimes return (0); 369df8bae1dSRodney W. Grimes } 370df8bae1dSRodney W. Grimes upp = &p->p_stats->p_prof; 371df8bae1dSRodney W. Grimes 372df8bae1dSRodney W. Grimes /* Block profile interrupts while changing state. */ 373df8bae1dSRodney W. Grimes s = splstatclock(); 374df8bae1dSRodney W. Grimes upp->pr_off = uap->offset; 375df8bae1dSRodney W. Grimes upp->pr_scale = uap->scale; 376df8bae1dSRodney W. Grimes upp->pr_base = uap->samples; 377df8bae1dSRodney W. Grimes upp->pr_size = uap->size; 378df8bae1dSRodney W. Grimes startprofclock(p); 379df8bae1dSRodney W. Grimes splx(s); 380df8bae1dSRodney W. Grimes 381df8bae1dSRodney W. Grimes return (0); 382df8bae1dSRodney W. Grimes } 383df8bae1dSRodney W. Grimes 384df8bae1dSRodney W. Grimes /* 385df8bae1dSRodney W. Grimes * Scale is a fixed-point number with the binary point 16 bits 386df8bae1dSRodney W. Grimes * into the value, and is <= 1.0. pc is at most 32 bits, so the 387df8bae1dSRodney W. Grimes * intermediate result is at most 48 bits. 388df8bae1dSRodney W. Grimes */ 389df8bae1dSRodney W. Grimes #define PC_TO_INDEX(pc, prof) \ 390df8bae1dSRodney W. Grimes ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \ 391df8bae1dSRodney W. Grimes (u_quad_t)((prof)->pr_scale)) >> 16) & ~1) 392df8bae1dSRodney W. Grimes 393df8bae1dSRodney W. Grimes /* 394df8bae1dSRodney W. Grimes * Collect user-level profiling statistics; called on a profiling tick, 395df8bae1dSRodney W. Grimes * when a process is running in user-mode. This routine may be called 396df8bae1dSRodney W. Grimes * from an interrupt context. We try to update the user profiling buffers 397df8bae1dSRodney W. Grimes * cheaply with fuswintr() and suswintr(). If that fails, we revert to 398df8bae1dSRodney W. Grimes * an AST that will vector us to trap() with a context in which copyin 399df8bae1dSRodney W. Grimes * and copyout will work. Trap will then call addupc_task(). 400df8bae1dSRodney W. Grimes * 401df8bae1dSRodney W. Grimes * Note that we may (rarely) not get around to the AST soon enough, and 402df8bae1dSRodney W. Grimes * lose profile ticks when the next tick overwrites this one, but in this 403df8bae1dSRodney W. Grimes * case the system is overloaded and the profile is probably already 404df8bae1dSRodney W. Grimes * inaccurate. 405df8bae1dSRodney W. Grimes */ 406df8bae1dSRodney W. Grimes void 407df8bae1dSRodney W. Grimes addupc_intr(p, pc, ticks) 408df8bae1dSRodney W. Grimes register struct proc *p; 40951c91299SJohn Baldwin register uintptr_t pc; 410df8bae1dSRodney W. Grimes u_int ticks; 411df8bae1dSRodney W. Grimes { 412df8bae1dSRodney W. Grimes register struct uprof *prof; 413df8bae1dSRodney W. Grimes register caddr_t addr; 414df8bae1dSRodney W. Grimes register u_int i; 415df8bae1dSRodney W. Grimes register int v; 416df8bae1dSRodney W. Grimes 417df8bae1dSRodney W. Grimes if (ticks == 0) 418df8bae1dSRodney W. Grimes return; 419df8bae1dSRodney W. Grimes prof = &p->p_stats->p_prof; 420df8bae1dSRodney W. Grimes if (pc < prof->pr_off || 421df8bae1dSRodney W. Grimes (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) 422df8bae1dSRodney W. Grimes return; /* out of range; ignore */ 423df8bae1dSRodney W. Grimes 424df8bae1dSRodney W. Grimes addr = prof->pr_base + i; 425df8bae1dSRodney W. Grimes if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + ticks) == -1) { 426df8bae1dSRodney W. Grimes prof->pr_addr = pc; 427df8bae1dSRodney W. Grimes prof->pr_ticks = ticks; 428df8bae1dSRodney W. Grimes need_proftick(p); 429df8bae1dSRodney W. Grimes } 430df8bae1dSRodney W. Grimes } 431df8bae1dSRodney W. Grimes 432df8bae1dSRodney W. Grimes /* 433df8bae1dSRodney W. Grimes * Much like before, but we can afford to take faults here. If the 434df8bae1dSRodney W. Grimes * update fails, we simply turn off profiling. 435df8bae1dSRodney W. Grimes */ 436037d027cSBruce Evans void 437df8bae1dSRodney W. Grimes addupc_task(p, pc, ticks) 438df8bae1dSRodney W. Grimes register struct proc *p; 43951c91299SJohn Baldwin register uintptr_t pc; 440df8bae1dSRodney W. Grimes u_int ticks; 441df8bae1dSRodney W. Grimes { 442df8bae1dSRodney W. Grimes register struct uprof *prof; 443df8bae1dSRodney W. Grimes register caddr_t addr; 444df8bae1dSRodney W. Grimes register u_int i; 445df8bae1dSRodney W. Grimes u_short v; 446df8bae1dSRodney W. Grimes 447ec5a741dSJohn Baldwin /* Testing PS_PROFIL may be unnecessary, but is certainly safe. */ 4489ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 449ec5a741dSJohn Baldwin if ((p->p_sflag & PS_PROFIL) == 0 || ticks == 0) { 4509ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 451df8bae1dSRodney W. Grimes return; 452ec5a741dSJohn Baldwin } 4539ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 454df8bae1dSRodney W. Grimes 455df8bae1dSRodney W. Grimes prof = &p->p_stats->p_prof; 456df8bae1dSRodney W. Grimes if (pc < prof->pr_off || 457df8bae1dSRodney W. Grimes (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) 458df8bae1dSRodney W. Grimes return; 459df8bae1dSRodney W. Grimes 460df8bae1dSRodney W. Grimes addr = prof->pr_base + i; 461df8bae1dSRodney W. Grimes if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) { 462df8bae1dSRodney W. Grimes v += ticks; 463df8bae1dSRodney W. Grimes if (copyout((caddr_t)&v, addr, sizeof(v)) == 0) 464df8bae1dSRodney W. Grimes return; 465df8bae1dSRodney W. Grimes } 466df8bae1dSRodney W. Grimes stopprofclock(p); 467df8bae1dSRodney W. Grimes } 468