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> 409e420850SBruce Evans #include <sys/kernel.h> 41fb919e4dSMark Murray #include <sys/lock.h> 42fb919e4dSMark Murray #include <sys/mutex.h> 43df8bae1dSRodney W. Grimes #include <sys/proc.h> 442baeef32SBruce Evans #include <sys/resourcevar.h> 459e420850SBruce Evans #include <sys/sysctl.h> 46b5e8ce9fSBruce Evans 47df8bae1dSRodney W. Grimes #include <machine/cpu.h> 48df8bae1dSRodney W. Grimes 49df8bae1dSRodney W. Grimes #ifdef GPROF 50df8bae1dSRodney W. Grimes #include <sys/malloc.h> 51df8bae1dSRodney W. Grimes #include <sys/gmon.h> 52ea2b3e3dSBruce Evans #undef MCOUNT 53df8bae1dSRodney W. Grimes 54a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_GPROF, "gprof", "kernel profiling buffer"); 5555166637SPoul-Henning Kamp 564590fd3aSDavid Greenman static void kmstartup __P((void *)); 572b14f991SJulian Elischer SYSINIT(kmem, SI_SUB_KPROF, SI_ORDER_FIRST, kmstartup, NULL) 582b14f991SJulian Elischer 59df8bae1dSRodney W. Grimes struct gmonparam _gmonparam = { GMON_PROF_OFF }; 60df8bae1dSRodney W. Grimes 61d6b9e17eSBruce Evans #ifdef GUPROF 62ea2b3e3dSBruce Evans #include <machine/asmacros.h> 63ea2b3e3dSBruce Evans 64d6b9e17eSBruce Evans void 65d6b9e17eSBruce Evans nullfunc_loop_profiled() 66d6b9e17eSBruce Evans { 67d6b9e17eSBruce Evans int i; 68d6b9e17eSBruce Evans 69d6b9e17eSBruce Evans for (i = 0; i < CALIB_SCALE; i++) 70d6b9e17eSBruce Evans nullfunc_profiled(); 71d6b9e17eSBruce Evans } 72d6b9e17eSBruce Evans 73e408eccfSBruce Evans #define nullfunc_loop_profiled_end nullfunc_profiled /* XXX */ 74e408eccfSBruce Evans 75d6b9e17eSBruce Evans void 76d6b9e17eSBruce Evans nullfunc_profiled() 77d6b9e17eSBruce Evans { 78d6b9e17eSBruce Evans } 79d6b9e17eSBruce Evans #endif /* GUPROF */ 80d6b9e17eSBruce Evans 815c7761b2SBruce Evans static void 82d841aaa7SBruce Evans kmstartup(dummy) 83d841aaa7SBruce Evans void *dummy; 84df8bae1dSRodney W. Grimes { 85df8bae1dSRodney W. Grimes char *cp; 86df8bae1dSRodney W. Grimes struct gmonparam *p = &_gmonparam; 87912e6037SBruce Evans #ifdef GUPROF 88d6b9e17eSBruce Evans int cputime_overhead; 89d6b9e17eSBruce Evans int empty_loop_time; 90912e6037SBruce Evans int i; 91d6b9e17eSBruce Evans int mcount_overhead; 92d6b9e17eSBruce Evans int mexitcount_overhead; 93d6b9e17eSBruce Evans int nullfunc_loop_overhead; 94d6b9e17eSBruce Evans int nullfunc_loop_profiled_time; 9537889b39SBruce Evans uintfptr_t tmp_addr; 960006681fSJohn Baldwin critical_t savecrit; 975e1aea9fSPoul-Henning Kamp #endif 98912e6037SBruce Evans 99df8bae1dSRodney W. Grimes /* 100df8bae1dSRodney W. Grimes * Round lowpc and highpc to multiples of the density we're using 101df8bae1dSRodney W. Grimes * so the rest of the scaling (here and in gprof) stays in ints. 102df8bae1dSRodney W. Grimes */ 103e45d35c3SBruce Evans p->lowpc = ROUNDDOWN((u_long)btext, HISTFRACTION * sizeof(HISTCOUNTER)); 104df8bae1dSRodney W. Grimes p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER)); 105df8bae1dSRodney W. Grimes p->textsize = p->highpc - p->lowpc; 106d6b9e17eSBruce Evans printf("Profiling kernel, textsize=%lu [%x..%x]\n", 107df8bae1dSRodney W. Grimes p->textsize, p->lowpc, p->highpc); 108df8bae1dSRodney W. Grimes p->kcountsize = p->textsize / HISTFRACTION; 109df8bae1dSRodney W. Grimes p->hashfraction = HASHFRACTION; 110df8bae1dSRodney W. Grimes p->fromssize = p->textsize / HASHFRACTION; 111df8bae1dSRodney W. Grimes p->tolimit = p->textsize * ARCDENSITY / 100; 112df8bae1dSRodney W. Grimes if (p->tolimit < MINARCS) 113df8bae1dSRodney W. Grimes p->tolimit = MINARCS; 114df8bae1dSRodney W. Grimes else if (p->tolimit > MAXARCS) 115df8bae1dSRodney W. Grimes p->tolimit = MAXARCS; 116df8bae1dSRodney W. Grimes p->tossize = p->tolimit * sizeof(struct tostruct); 117df8bae1dSRodney W. Grimes cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize, 1187cc0979fSDavid Malone M_GPROF, M_NOWAIT | M_ZERO); 119df8bae1dSRodney W. Grimes if (cp == 0) { 120df8bae1dSRodney W. Grimes printf("No memory for profiling.\n"); 121df8bae1dSRodney W. Grimes return; 122df8bae1dSRodney W. Grimes } 123df8bae1dSRodney W. Grimes p->tos = (struct tostruct *)cp; 124df8bae1dSRodney W. Grimes cp += p->tossize; 125912e6037SBruce Evans p->kcount = (HISTCOUNTER *)cp; 126df8bae1dSRodney W. Grimes cp += p->kcountsize; 127df8bae1dSRodney W. Grimes p->froms = (u_short *)cp; 128912e6037SBruce Evans 129912e6037SBruce Evans #ifdef GUPROF 130d6b9e17eSBruce Evans /* Initialize pointers to overhead counters. */ 131912e6037SBruce Evans p->cputime_count = &KCOUNT(p, PC_TO_I(p, cputime)); 132912e6037SBruce Evans p->mcount_count = &KCOUNT(p, PC_TO_I(p, mcount)); 133912e6037SBruce Evans p->mexitcount_count = &KCOUNT(p, PC_TO_I(p, mexitcount)); 134912e6037SBruce Evans 135912e6037SBruce Evans /* 136d6b9e17eSBruce Evans * Disable interrupts to avoid interference while we calibrate 137d6b9e17eSBruce Evans * things. 138912e6037SBruce Evans */ 1390006681fSJohn Baldwin savecrit = critical_enter(); 140d6b9e17eSBruce Evans 141d6b9e17eSBruce Evans /* 142d6b9e17eSBruce Evans * Determine overheads. 143d6b9e17eSBruce Evans * XXX this needs to be repeated for each useful timer/counter. 144d6b9e17eSBruce Evans */ 145d6b9e17eSBruce Evans cputime_overhead = 0; 146d6b9e17eSBruce Evans startguprof(p); 147d6b9e17eSBruce Evans for (i = 0; i < CALIB_SCALE; i++) 148d6b9e17eSBruce Evans cputime_overhead += cputime(); 149d6b9e17eSBruce Evans 150d6b9e17eSBruce Evans empty_loop(); 151d6b9e17eSBruce Evans startguprof(p); 152d6b9e17eSBruce Evans empty_loop(); 153d6b9e17eSBruce Evans empty_loop_time = cputime(); 154d6b9e17eSBruce Evans 155d6b9e17eSBruce Evans nullfunc_loop_profiled(); 156d6b9e17eSBruce Evans 157d6b9e17eSBruce Evans /* 158d6b9e17eSBruce Evans * Start profiling. There won't be any normal function calls since 159d6b9e17eSBruce Evans * interrupts are disabled, but we will call the profiling routines 160d6b9e17eSBruce Evans * directly to determine their overheads. 161d6b9e17eSBruce Evans */ 162912e6037SBruce Evans p->state = GMON_PROF_HIRES; 163912e6037SBruce Evans 164d6b9e17eSBruce Evans startguprof(p); 165d6b9e17eSBruce Evans nullfunc_loop_profiled(); 166912e6037SBruce Evans 167d6b9e17eSBruce Evans startguprof(p); 168912e6037SBruce Evans for (i = 0; i < CALIB_SCALE; i++) 16977849078SBruce Evans #if defined(__i386__) && __GNUC__ >= 2 170c1087c13SBruce Evans __asm("pushl %0; call __mcount; popl %%ecx" 171912e6037SBruce Evans : 172d6b9e17eSBruce Evans : "i" (profil) 173912e6037SBruce Evans : "ax", "bx", "cx", "dx", "memory"); 174912e6037SBruce Evans #else 175912e6037SBruce Evans #error 176912e6037SBruce Evans #endif 177d6b9e17eSBruce Evans mcount_overhead = KCOUNT(p, PC_TO_I(p, profil)); 178912e6037SBruce Evans 179d6b9e17eSBruce Evans startguprof(p); 180912e6037SBruce Evans for (i = 0; i < CALIB_SCALE; i++) 18177849078SBruce Evans #if defined(__i386__) && __GNUC__ >= 2 182ea2b3e3dSBruce Evans __asm("call " __XSTRING(HIDENAME(mexitcount)) "; 1:" 183912e6037SBruce Evans : : : "ax", "bx", "cx", "dx", "memory"); 184c1087c13SBruce Evans __asm("movl $1b,%0" : "=rm" (tmp_addr)); 185912e6037SBruce Evans #else 186912e6037SBruce Evans #error 187912e6037SBruce Evans #endif 188e408eccfSBruce Evans mexitcount_overhead = KCOUNT(p, PC_TO_I(p, tmp_addr)); 189912e6037SBruce Evans 190912e6037SBruce Evans p->state = GMON_PROF_OFF; 191d6b9e17eSBruce Evans stopguprof(p); 192d6b9e17eSBruce Evans 1930006681fSJohn Baldwin critical_exit(savecrit); 194912e6037SBruce Evans 195d6b9e17eSBruce Evans nullfunc_loop_profiled_time = 0; 19637889b39SBruce Evans for (tmp_addr = (uintfptr_t)nullfunc_loop_profiled; 19737889b39SBruce Evans tmp_addr < (uintfptr_t)nullfunc_loop_profiled_end; 198e408eccfSBruce Evans tmp_addr += HISTFRACTION * sizeof(HISTCOUNTER)) 199e408eccfSBruce Evans nullfunc_loop_profiled_time += KCOUNT(p, PC_TO_I(p, tmp_addr)); 200d6b9e17eSBruce Evans #define CALIB_DOSCALE(count) (((count) + CALIB_SCALE / 3) / CALIB_SCALE) 201d6b9e17eSBruce Evans #define c2n(count, freq) ((int)((count) * 1000000000LL / freq)) 202d6b9e17eSBruce Evans printf("cputime %d, empty_loop %d, nullfunc_loop_profiled %d, mcount %d, mexitcount %d\n", 203d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(cputime_overhead, p->profrate)), 204d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(empty_loop_time, p->profrate)), 205d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(nullfunc_loop_profiled_time, p->profrate)), 206d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(mcount_overhead, p->profrate)), 207d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(mexitcount_overhead, p->profrate))); 208d6b9e17eSBruce Evans cputime_overhead -= empty_loop_time; 209d6b9e17eSBruce Evans mcount_overhead -= empty_loop_time; 210d6b9e17eSBruce Evans mexitcount_overhead -= empty_loop_time; 211d6b9e17eSBruce Evans 212d6b9e17eSBruce Evans /*- 213d6b9e17eSBruce Evans * Profiling overheads are determined by the times between the 214d6b9e17eSBruce Evans * following events: 215d6b9e17eSBruce Evans * MC1: mcount() is called 216d6b9e17eSBruce Evans * MC2: cputime() (called from mcount()) latches the timer 217d6b9e17eSBruce Evans * MC3: mcount() completes 218d6b9e17eSBruce Evans * ME1: mexitcount() is called 219d6b9e17eSBruce Evans * ME2: cputime() (called from mexitcount()) latches the timer 220d6b9e17eSBruce Evans * ME3: mexitcount() completes. 221d6b9e17eSBruce Evans * The times between the events vary slightly depending on instruction 222d6b9e17eSBruce Evans * combination and cache misses, etc. Attempt to determine the 223d6b9e17eSBruce Evans * minimum times. These can be subtracted from the profiling times 224d6b9e17eSBruce Evans * without much risk of reducing the profiling times below what they 225d6b9e17eSBruce Evans * would be when profiling is not configured. Abbreviate: 226d6b9e17eSBruce Evans * ab = minimum time between MC1 and MC3 227d6b9e17eSBruce Evans * a = minumum time between MC1 and MC2 228d6b9e17eSBruce Evans * b = minimum time between MC2 and MC3 229d6b9e17eSBruce Evans * cd = minimum time between ME1 and ME3 230d6b9e17eSBruce Evans * c = minimum time between ME1 and ME2 231d6b9e17eSBruce Evans * d = minimum time between ME2 and ME3. 232d6b9e17eSBruce Evans * These satisfy the relations: 233d6b9e17eSBruce Evans * ab <= mcount_overhead (just measured) 234d6b9e17eSBruce Evans * a + b <= ab 235d6b9e17eSBruce Evans * cd <= mexitcount_overhead (just measured) 236d6b9e17eSBruce Evans * c + d <= cd 237d6b9e17eSBruce Evans * a + d <= nullfunc_loop_profiled_time (just measured) 238d6b9e17eSBruce Evans * a >= 0, b >= 0, c >= 0, d >= 0. 239d6b9e17eSBruce Evans * Assume that ab and cd are equal to the minimums. 240d6b9e17eSBruce Evans */ 241d6b9e17eSBruce Evans p->cputime_overhead = CALIB_DOSCALE(cputime_overhead); 242d6b9e17eSBruce Evans p->mcount_overhead = CALIB_DOSCALE(mcount_overhead - cputime_overhead); 243d6b9e17eSBruce Evans p->mexitcount_overhead = CALIB_DOSCALE(mexitcount_overhead 244d6b9e17eSBruce Evans - cputime_overhead); 245d6b9e17eSBruce Evans nullfunc_loop_overhead = nullfunc_loop_profiled_time - empty_loop_time; 246d6b9e17eSBruce Evans p->mexitcount_post_overhead = CALIB_DOSCALE((mcount_overhead 247d6b9e17eSBruce Evans - nullfunc_loop_overhead) 248d6b9e17eSBruce Evans / 4); 249d6b9e17eSBruce Evans p->mexitcount_pre_overhead = p->mexitcount_overhead 250d6b9e17eSBruce Evans + p->cputime_overhead 251d6b9e17eSBruce Evans - p->mexitcount_post_overhead; 252d6b9e17eSBruce Evans p->mcount_pre_overhead = CALIB_DOSCALE(nullfunc_loop_overhead) 253d6b9e17eSBruce Evans - p->mexitcount_post_overhead; 254d6b9e17eSBruce Evans p->mcount_post_overhead = p->mcount_overhead 255d6b9e17eSBruce Evans + p->cputime_overhead 256d6b9e17eSBruce Evans - p->mcount_pre_overhead; 257d6b9e17eSBruce Evans printf( 258d6b9e17eSBruce Evans "Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d nsec\n", 259d6b9e17eSBruce Evans c2n(p->cputime_overhead, p->profrate), 260d6b9e17eSBruce Evans c2n(p->mcount_overhead, p->profrate), 261d6b9e17eSBruce Evans c2n(p->mcount_pre_overhead, p->profrate), 262d6b9e17eSBruce Evans c2n(p->mcount_post_overhead, p->profrate), 263d6b9e17eSBruce Evans c2n(p->cputime_overhead, p->profrate), 264d6b9e17eSBruce Evans c2n(p->mexitcount_overhead, p->profrate), 265d6b9e17eSBruce Evans c2n(p->mexitcount_pre_overhead, p->profrate), 266d6b9e17eSBruce Evans c2n(p->mexitcount_post_overhead, p->profrate)); 267d6b9e17eSBruce Evans printf( 268d6b9e17eSBruce Evans "Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d cycles\n", 269d6b9e17eSBruce Evans p->cputime_overhead, p->mcount_overhead, 270d6b9e17eSBruce Evans p->mcount_pre_overhead, p->mcount_post_overhead, 271d6b9e17eSBruce Evans p->cputime_overhead, p->mexitcount_overhead, 272d6b9e17eSBruce Evans p->mexitcount_pre_overhead, p->mexitcount_post_overhead); 273912e6037SBruce Evans #endif /* GUPROF */ 274df8bae1dSRodney W. Grimes } 275df8bae1dSRodney W. Grimes 276df8bae1dSRodney W. Grimes /* 277df8bae1dSRodney W. Grimes * Return kernel profiling information. 278df8bae1dSRodney W. Grimes */ 2794b2af45fSPoul-Henning Kamp static int 28082d9ae4eSPoul-Henning Kamp sysctl_kern_prof(SYSCTL_HANDLER_ARGS) 281df8bae1dSRodney W. Grimes { 2824b2af45fSPoul-Henning Kamp int *name = (int *) arg1; 2834b2af45fSPoul-Henning Kamp u_int namelen = arg2; 284df8bae1dSRodney W. Grimes struct gmonparam *gp = &_gmonparam; 285df8bae1dSRodney W. Grimes int error; 286912e6037SBruce Evans int state; 287df8bae1dSRodney W. Grimes 288df8bae1dSRodney W. Grimes /* all sysctl names at this level are terminal */ 289df8bae1dSRodney W. Grimes if (namelen != 1) 290df8bae1dSRodney W. Grimes return (ENOTDIR); /* overloaded */ 291df8bae1dSRodney W. Grimes 292df8bae1dSRodney W. Grimes switch (name[0]) { 293df8bae1dSRodney W. Grimes case GPROF_STATE: 294912e6037SBruce Evans state = gp->state; 295912e6037SBruce Evans error = sysctl_handle_int(oidp, &state, 0, req); 296df8bae1dSRodney W. Grimes if (error) 297df8bae1dSRodney W. Grimes return (error); 298912e6037SBruce Evans if (!req->newptr) 299912e6037SBruce Evans return (0); 300912e6037SBruce Evans if (state == GMON_PROF_OFF) { 301d6b9e17eSBruce Evans gp->state = state; 302df8bae1dSRodney W. Grimes stopprofclock(&proc0); 303d6b9e17eSBruce Evans stopguprof(gp); 304912e6037SBruce Evans } else if (state == GMON_PROF_ON) { 305d6b9e17eSBruce Evans gp->state = GMON_PROF_OFF; 306d6b9e17eSBruce Evans stopguprof(gp); 307912e6037SBruce Evans gp->profrate = profhz; 308df8bae1dSRodney W. Grimes startprofclock(&proc0); 309d6b9e17eSBruce Evans gp->state = state; 310912e6037SBruce Evans #ifdef GUPROF 311912e6037SBruce Evans } else if (state == GMON_PROF_HIRES) { 312d6b9e17eSBruce Evans gp->state = GMON_PROF_OFF; 313912e6037SBruce Evans stopprofclock(&proc0); 314d6b9e17eSBruce Evans startguprof(gp); 315912e6037SBruce Evans gp->state = state; 316912e6037SBruce Evans #endif 317912e6037SBruce Evans } else if (state != gp->state) 318912e6037SBruce Evans return (EINVAL); 319df8bae1dSRodney W. Grimes return (0); 320df8bae1dSRodney W. Grimes case GPROF_COUNT: 3214b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, 3224b2af45fSPoul-Henning Kamp gp->kcount, gp->kcountsize, req)); 323df8bae1dSRodney W. Grimes case GPROF_FROMS: 3244b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, 3254b2af45fSPoul-Henning Kamp gp->froms, gp->fromssize, req)); 326df8bae1dSRodney W. Grimes case GPROF_TOS: 3274b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, 3284b2af45fSPoul-Henning Kamp gp->tos, gp->tossize, req)); 329df8bae1dSRodney W. Grimes case GPROF_GMONPARAM: 3304b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, gp, sizeof *gp, req)); 331df8bae1dSRodney W. Grimes default: 332df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 333df8bae1dSRodney W. Grimes } 334df8bae1dSRodney W. Grimes /* NOTREACHED */ 335df8bae1dSRodney W. Grimes } 3364b2af45fSPoul-Henning Kamp 3374b2af45fSPoul-Henning Kamp SYSCTL_NODE(_kern, KERN_PROF, prof, CTLFLAG_RW, sysctl_kern_prof, ""); 338df8bae1dSRodney W. Grimes #endif /* GPROF */ 339df8bae1dSRodney W. Grimes 340df8bae1dSRodney W. Grimes /* 341df8bae1dSRodney W. Grimes * Profiling system call. 342df8bae1dSRodney W. Grimes * 343df8bae1dSRodney W. Grimes * The scale factor is a fixed point number with 16 bits of fraction, so that 344df8bae1dSRodney W. Grimes * 1.0 is represented as 0x10000. A scale factor of 0 turns off profiling. 345df8bae1dSRodney W. Grimes */ 346d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 347df8bae1dSRodney W. Grimes struct profil_args { 348df8bae1dSRodney W. Grimes caddr_t samples; 349134e06feSBruce Evans size_t size; 350134e06feSBruce Evans size_t offset; 351df8bae1dSRodney W. Grimes u_int scale; 352df8bae1dSRodney W. Grimes }; 353d2d3e875SBruce Evans #endif 3546f1e8c18SMatthew Dillon /* 3556f1e8c18SMatthew Dillon * MPSAFE 3566f1e8c18SMatthew Dillon */ 357df8bae1dSRodney W. Grimes /* ARGSUSED */ 35826f9a767SRodney W. Grimes int 359b40ce416SJulian Elischer profil(td, uap) 360b40ce416SJulian Elischer struct thread *td; 361df8bae1dSRodney W. Grimes register struct profil_args *uap; 362df8bae1dSRodney W. Grimes { 363df8bae1dSRodney W. Grimes register struct uprof *upp; 364df8bae1dSRodney W. Grimes int s; 3656f1e8c18SMatthew Dillon int error = 0; 366df8bae1dSRodney W. Grimes 3676f1e8c18SMatthew Dillon mtx_lock(&Giant); 3686f1e8c18SMatthew Dillon 3696f1e8c18SMatthew Dillon if (uap->scale > (1 << 16)) { 3706f1e8c18SMatthew Dillon error = EINVAL; 3716f1e8c18SMatthew Dillon goto done2; 3726f1e8c18SMatthew Dillon } 373df8bae1dSRodney W. Grimes if (uap->scale == 0) { 374b40ce416SJulian Elischer stopprofclock(td->td_proc); 3756f1e8c18SMatthew Dillon goto done2; 376df8bae1dSRodney W. Grimes } 377b40ce416SJulian Elischer upp = &td->td_proc->p_stats->p_prof; 378df8bae1dSRodney W. Grimes 379df8bae1dSRodney W. Grimes /* Block profile interrupts while changing state. */ 380df8bae1dSRodney W. Grimes s = splstatclock(); 381df8bae1dSRodney W. Grimes upp->pr_off = uap->offset; 382df8bae1dSRodney W. Grimes upp->pr_scale = uap->scale; 383df8bae1dSRodney W. Grimes upp->pr_base = uap->samples; 384df8bae1dSRodney W. Grimes upp->pr_size = uap->size; 385b40ce416SJulian Elischer startprofclock(td->td_proc); 386df8bae1dSRodney W. Grimes splx(s); 387df8bae1dSRodney W. Grimes 3886f1e8c18SMatthew Dillon done2: 3896f1e8c18SMatthew Dillon mtx_unlock(&Giant); 3906f1e8c18SMatthew Dillon return (error); 391df8bae1dSRodney W. Grimes } 392df8bae1dSRodney W. Grimes 393df8bae1dSRodney W. Grimes /* 394df8bae1dSRodney W. Grimes * Scale is a fixed-point number with the binary point 16 bits 395df8bae1dSRodney W. Grimes * into the value, and is <= 1.0. pc is at most 32 bits, so the 396df8bae1dSRodney W. Grimes * intermediate result is at most 48 bits. 397df8bae1dSRodney W. Grimes */ 398df8bae1dSRodney W. Grimes #define PC_TO_INDEX(pc, prof) \ 399df8bae1dSRodney W. Grimes ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \ 400df8bae1dSRodney W. Grimes (u_quad_t)((prof)->pr_scale)) >> 16) & ~1) 401df8bae1dSRodney W. Grimes 402df8bae1dSRodney W. Grimes /* 403df8bae1dSRodney W. Grimes * Collect user-level profiling statistics; called on a profiling tick, 404df8bae1dSRodney W. Grimes * when a process is running in user-mode. This routine may be called 405df8bae1dSRodney W. Grimes * from an interrupt context. We try to update the user profiling buffers 406df8bae1dSRodney W. Grimes * cheaply with fuswintr() and suswintr(). If that fails, we revert to 407df8bae1dSRodney W. Grimes * an AST that will vector us to trap() with a context in which copyin 408df8bae1dSRodney W. Grimes * and copyout will work. Trap will then call addupc_task(). 409df8bae1dSRodney W. Grimes * 410df8bae1dSRodney W. Grimes * Note that we may (rarely) not get around to the AST soon enough, and 411df8bae1dSRodney W. Grimes * lose profile ticks when the next tick overwrites this one, but in this 412df8bae1dSRodney W. Grimes * case the system is overloaded and the profile is probably already 413df8bae1dSRodney W. Grimes * inaccurate. 414df8bae1dSRodney W. Grimes */ 415df8bae1dSRodney W. Grimes void 416b40ce416SJulian Elischer addupc_intr(ke, pc, ticks) 417b40ce416SJulian Elischer register struct kse *ke; 41851c91299SJohn Baldwin register uintptr_t pc; 419df8bae1dSRodney W. Grimes u_int ticks; 420df8bae1dSRodney W. Grimes { 421df8bae1dSRodney W. Grimes register struct uprof *prof; 422df8bae1dSRodney W. Grimes register caddr_t addr; 423df8bae1dSRodney W. Grimes register u_int i; 424df8bae1dSRodney W. Grimes register int v; 425df8bae1dSRodney W. Grimes 426df8bae1dSRodney W. Grimes if (ticks == 0) 427df8bae1dSRodney W. Grimes return; 428b40ce416SJulian Elischer prof = &ke->ke_proc->p_stats->p_prof; 429df8bae1dSRodney W. Grimes if (pc < prof->pr_off || 430df8bae1dSRodney W. Grimes (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) 431df8bae1dSRodney W. Grimes return; /* out of range; ignore */ 432df8bae1dSRodney W. Grimes 433df8bae1dSRodney W. Grimes addr = prof->pr_base + i; 434df8bae1dSRodney W. Grimes if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + ticks) == -1) { 435688ebe12SJohn Baldwin mtx_lock_spin(&sched_lock); 436df8bae1dSRodney W. Grimes prof->pr_addr = pc; 437df8bae1dSRodney W. Grimes prof->pr_ticks = ticks; 438b40ce416SJulian Elischer ke->ke_flags |= KEF_OWEUPC | KEF_ASTPENDING ; 439688ebe12SJohn Baldwin mtx_unlock_spin(&sched_lock); 440df8bae1dSRodney W. Grimes } 441df8bae1dSRodney W. Grimes } 442df8bae1dSRodney W. Grimes 443df8bae1dSRodney W. Grimes /* 444df8bae1dSRodney W. Grimes * Much like before, but we can afford to take faults here. If the 445df8bae1dSRodney W. Grimes * update fails, we simply turn off profiling. 446df8bae1dSRodney W. Grimes */ 447037d027cSBruce Evans void 448b40ce416SJulian Elischer addupc_task(ke, pc, ticks) 449b40ce416SJulian Elischer register struct kse *ke; 45051c91299SJohn Baldwin register uintptr_t pc; 451df8bae1dSRodney W. Grimes u_int ticks; 452df8bae1dSRodney W. Grimes { 453b40ce416SJulian Elischer struct proc *p = ke->ke_proc; 454df8bae1dSRodney W. Grimes register struct uprof *prof; 455df8bae1dSRodney W. Grimes register caddr_t addr; 456df8bae1dSRodney W. Grimes register u_int i; 457df8bae1dSRodney W. Grimes u_short v; 458df8bae1dSRodney W. Grimes 459ec5a741dSJohn Baldwin /* Testing PS_PROFIL may be unnecessary, but is certainly safe. */ 4605beb572bSJohn Baldwin if ((p->p_sflag & PS_PROFIL) == 0 || ticks == 0) 461df8bae1dSRodney W. Grimes return; 462df8bae1dSRodney W. Grimes 463df8bae1dSRodney W. Grimes prof = &p->p_stats->p_prof; 464df8bae1dSRodney W. Grimes if (pc < prof->pr_off || 465df8bae1dSRodney W. Grimes (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) 466df8bae1dSRodney W. Grimes return; 467df8bae1dSRodney W. Grimes 468df8bae1dSRodney W. Grimes addr = prof->pr_base + i; 469df8bae1dSRodney W. Grimes if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) { 470df8bae1dSRodney W. Grimes v += ticks; 471df8bae1dSRodney W. Grimes if (copyout((caddr_t)&v, addr, sizeof(v)) == 0) 472df8bae1dSRodney W. Grimes return; 473df8bae1dSRodney W. Grimes } 474df8bae1dSRodney W. Grimes stopprofclock(p); 475df8bae1dSRodney W. Grimes } 476