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 564d77a549SAlfred Perlstein static void kmstartup(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 814a44bd4bSBrian Feldman /* 824a44bd4bSBrian Feldman * Update the histograms to support extending the text region arbitrarily. 834a44bd4bSBrian Feldman * This is done slightly naively (no sparse regions), so will waste slight 844a44bd4bSBrian Feldman * amounts of memory, but will overall work nicely enough to allow profiling 854a44bd4bSBrian Feldman * of KLDs. 864a44bd4bSBrian Feldman */ 874a44bd4bSBrian Feldman void 884a44bd4bSBrian Feldman kmupetext(uintfptr_t nhighpc) 894a44bd4bSBrian Feldman { 904a44bd4bSBrian Feldman struct gmonparam np; /* slightly large */ 914a44bd4bSBrian Feldman struct gmonparam *p = &_gmonparam; 924a44bd4bSBrian Feldman char *cp; 934a44bd4bSBrian Feldman 944a44bd4bSBrian Feldman GIANT_REQUIRED; 954a44bd4bSBrian Feldman bcopy(p, &np, sizeof(*p)); 964a44bd4bSBrian Feldman np.highpc = ROUNDUP(nhighpc, HISTFRACTION * sizeof(HISTCOUNTER)); 974a44bd4bSBrian Feldman if (np.highpc <= p->highpc) 984a44bd4bSBrian Feldman return; 994a44bd4bSBrian Feldman np.textsize = np.highpc - p->lowpc; 1004a44bd4bSBrian Feldman np.kcountsize = np.textsize / HISTFRACTION; 1014a44bd4bSBrian Feldman np.hashfraction = HASHFRACTION; 1024a44bd4bSBrian Feldman np.fromssize = np.textsize / HASHFRACTION; 1034a44bd4bSBrian Feldman np.tolimit = np.textsize * ARCDENSITY / 100; 1044a44bd4bSBrian Feldman if (np.tolimit < MINARCS) 1054a44bd4bSBrian Feldman np.tolimit = MINARCS; 1064a44bd4bSBrian Feldman else if (np.tolimit > MAXARCS) 1074a44bd4bSBrian Feldman np.tolimit = MAXARCS; 1084a44bd4bSBrian Feldman np.tossize = np.tolimit * sizeof(struct tostruct); 1094a44bd4bSBrian Feldman cp = malloc(np.kcountsize + np.fromssize + np.tossize, 1104a44bd4bSBrian Feldman M_GPROF, M_WAITOK); 1114a44bd4bSBrian Feldman /* 1124a44bd4bSBrian Feldman * Check for something else extending highpc while we slept. 1134a44bd4bSBrian Feldman */ 1144a44bd4bSBrian Feldman if (np.highpc <= p->highpc) { 1154a44bd4bSBrian Feldman free(cp, M_GPROF); 1164a44bd4bSBrian Feldman return; 1174a44bd4bSBrian Feldman } 1184a44bd4bSBrian Feldman np.tos = (struct tostruct *)cp; 1194a44bd4bSBrian Feldman cp += np.tossize; 1204a44bd4bSBrian Feldman np.kcount = (HISTCOUNTER *)cp; 1214a44bd4bSBrian Feldman cp += np.kcountsize; 1224a44bd4bSBrian Feldman np.froms = (u_short *)cp; 1234a44bd4bSBrian Feldman #ifdef GUPROF 1244a44bd4bSBrian Feldman /* Reinitialize pointers to overhead counters. */ 1254a44bd4bSBrian Feldman np.cputime_count = &KCOUNT(&np, PC_TO_I(&np, cputime)); 1264a44bd4bSBrian Feldman np.mcount_count = &KCOUNT(&np, PC_TO_I(&np, mcount)); 1274a44bd4bSBrian Feldman np.mexitcount_count = &KCOUNT(&np, PC_TO_I(&np, mexitcount)); 1284a44bd4bSBrian Feldman #endif 1297e1f6dfeSJohn Baldwin critical_enter(); 1304a44bd4bSBrian Feldman bcopy(p->tos, np.tos, p->tossize); 1314a44bd4bSBrian Feldman bzero((char *)np.tos + p->tossize, np.tossize - p->tossize); 1324a44bd4bSBrian Feldman bcopy(p->kcount, np.kcount, p->kcountsize); 1334a44bd4bSBrian Feldman bzero((char *)np.kcount + p->kcountsize, np.kcountsize - 1344a44bd4bSBrian Feldman p->kcountsize); 1354a44bd4bSBrian Feldman bcopy(p->froms, np.froms, p->fromssize); 1364a44bd4bSBrian Feldman bzero((char *)np.froms + p->fromssize, np.fromssize - p->fromssize); 1374a44bd4bSBrian Feldman cp = (char *)p->tos; 1384a44bd4bSBrian Feldman bcopy(&np, p, sizeof(*p)); 1397e1f6dfeSJohn Baldwin critical_exit(); 1404a44bd4bSBrian Feldman free(cp, M_GPROF); 1414a44bd4bSBrian Feldman } 1424a44bd4bSBrian Feldman 1435c7761b2SBruce Evans static void 144d841aaa7SBruce Evans kmstartup(dummy) 145d841aaa7SBruce Evans void *dummy; 146df8bae1dSRodney W. Grimes { 147df8bae1dSRodney W. Grimes char *cp; 148df8bae1dSRodney W. Grimes struct gmonparam *p = &_gmonparam; 149912e6037SBruce Evans #ifdef GUPROF 150d6b9e17eSBruce Evans int cputime_overhead; 151d6b9e17eSBruce Evans int empty_loop_time; 152912e6037SBruce Evans int i; 153d6b9e17eSBruce Evans int mcount_overhead; 154d6b9e17eSBruce Evans int mexitcount_overhead; 155d6b9e17eSBruce Evans int nullfunc_loop_overhead; 156d6b9e17eSBruce Evans int nullfunc_loop_profiled_time; 15737889b39SBruce Evans uintfptr_t tmp_addr; 1585e1aea9fSPoul-Henning Kamp #endif 159912e6037SBruce Evans 160df8bae1dSRodney W. Grimes /* 161df8bae1dSRodney W. Grimes * Round lowpc and highpc to multiples of the density we're using 162df8bae1dSRodney W. Grimes * so the rest of the scaling (here and in gprof) stays in ints. 163df8bae1dSRodney W. Grimes */ 164e45d35c3SBruce Evans p->lowpc = ROUNDDOWN((u_long)btext, HISTFRACTION * sizeof(HISTCOUNTER)); 165df8bae1dSRodney W. Grimes p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER)); 166df8bae1dSRodney W. Grimes p->textsize = p->highpc - p->lowpc; 167d6b9e17eSBruce Evans printf("Profiling kernel, textsize=%lu [%x..%x]\n", 168df8bae1dSRodney W. Grimes p->textsize, p->lowpc, p->highpc); 169df8bae1dSRodney W. Grimes p->kcountsize = p->textsize / HISTFRACTION; 170df8bae1dSRodney W. Grimes p->hashfraction = HASHFRACTION; 171df8bae1dSRodney W. Grimes p->fromssize = p->textsize / HASHFRACTION; 172df8bae1dSRodney W. Grimes p->tolimit = p->textsize * ARCDENSITY / 100; 173df8bae1dSRodney W. Grimes if (p->tolimit < MINARCS) 174df8bae1dSRodney W. Grimes p->tolimit = MINARCS; 175df8bae1dSRodney W. Grimes else if (p->tolimit > MAXARCS) 176df8bae1dSRodney W. Grimes p->tolimit = MAXARCS; 177df8bae1dSRodney W. Grimes p->tossize = p->tolimit * sizeof(struct tostruct); 178df8bae1dSRodney W. Grimes cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize, 1794a44bd4bSBrian Feldman M_GPROF, M_WAITOK | M_ZERO); 180df8bae1dSRodney W. Grimes p->tos = (struct tostruct *)cp; 181df8bae1dSRodney W. Grimes cp += p->tossize; 182912e6037SBruce Evans p->kcount = (HISTCOUNTER *)cp; 183df8bae1dSRodney W. Grimes cp += p->kcountsize; 184df8bae1dSRodney W. Grimes p->froms = (u_short *)cp; 185912e6037SBruce Evans 186912e6037SBruce Evans #ifdef GUPROF 187d6b9e17eSBruce Evans /* Initialize pointers to overhead counters. */ 188912e6037SBruce Evans p->cputime_count = &KCOUNT(p, PC_TO_I(p, cputime)); 189912e6037SBruce Evans p->mcount_count = &KCOUNT(p, PC_TO_I(p, mcount)); 190912e6037SBruce Evans p->mexitcount_count = &KCOUNT(p, PC_TO_I(p, mexitcount)); 191912e6037SBruce Evans 192912e6037SBruce Evans /* 193d6b9e17eSBruce Evans * Disable interrupts to avoid interference while we calibrate 194d6b9e17eSBruce Evans * things. 195912e6037SBruce Evans */ 1967e1f6dfeSJohn Baldwin critical_enter(); 197d6b9e17eSBruce Evans 198d6b9e17eSBruce Evans /* 199d6b9e17eSBruce Evans * Determine overheads. 200d6b9e17eSBruce Evans * XXX this needs to be repeated for each useful timer/counter. 201d6b9e17eSBruce Evans */ 202d6b9e17eSBruce Evans cputime_overhead = 0; 203d6b9e17eSBruce Evans startguprof(p); 204d6b9e17eSBruce Evans for (i = 0; i < CALIB_SCALE; i++) 205d6b9e17eSBruce Evans cputime_overhead += cputime(); 206d6b9e17eSBruce Evans 207d6b9e17eSBruce Evans empty_loop(); 208d6b9e17eSBruce Evans startguprof(p); 209d6b9e17eSBruce Evans empty_loop(); 210d6b9e17eSBruce Evans empty_loop_time = cputime(); 211d6b9e17eSBruce Evans 212d6b9e17eSBruce Evans nullfunc_loop_profiled(); 213d6b9e17eSBruce Evans 214d6b9e17eSBruce Evans /* 215d6b9e17eSBruce Evans * Start profiling. There won't be any normal function calls since 216d6b9e17eSBruce Evans * interrupts are disabled, but we will call the profiling routines 217d6b9e17eSBruce Evans * directly to determine their overheads. 218d6b9e17eSBruce Evans */ 219912e6037SBruce Evans p->state = GMON_PROF_HIRES; 220912e6037SBruce Evans 221d6b9e17eSBruce Evans startguprof(p); 222d6b9e17eSBruce Evans nullfunc_loop_profiled(); 223912e6037SBruce Evans 224d6b9e17eSBruce Evans startguprof(p); 225912e6037SBruce Evans for (i = 0; i < CALIB_SCALE; i++) 22677849078SBruce Evans #if defined(__i386__) && __GNUC__ >= 2 227c1087c13SBruce Evans __asm("pushl %0; call __mcount; popl %%ecx" 228912e6037SBruce Evans : 229d6b9e17eSBruce Evans : "i" (profil) 230912e6037SBruce Evans : "ax", "bx", "cx", "dx", "memory"); 231912e6037SBruce Evans #else 232912e6037SBruce Evans #error 233912e6037SBruce Evans #endif 234d6b9e17eSBruce Evans mcount_overhead = KCOUNT(p, PC_TO_I(p, profil)); 235912e6037SBruce Evans 236d6b9e17eSBruce Evans startguprof(p); 237912e6037SBruce Evans for (i = 0; i < CALIB_SCALE; i++) 23877849078SBruce Evans #if defined(__i386__) && __GNUC__ >= 2 239ea2b3e3dSBruce Evans __asm("call " __XSTRING(HIDENAME(mexitcount)) "; 1:" 240912e6037SBruce Evans : : : "ax", "bx", "cx", "dx", "memory"); 241c1087c13SBruce Evans __asm("movl $1b,%0" : "=rm" (tmp_addr)); 242912e6037SBruce Evans #else 243912e6037SBruce Evans #error 244912e6037SBruce Evans #endif 245e408eccfSBruce Evans mexitcount_overhead = KCOUNT(p, PC_TO_I(p, tmp_addr)); 246912e6037SBruce Evans 247912e6037SBruce Evans p->state = GMON_PROF_OFF; 248d6b9e17eSBruce Evans stopguprof(p); 249d6b9e17eSBruce Evans 2507e1f6dfeSJohn Baldwin critical_exit(); 251912e6037SBruce Evans 252d6b9e17eSBruce Evans nullfunc_loop_profiled_time = 0; 25337889b39SBruce Evans for (tmp_addr = (uintfptr_t)nullfunc_loop_profiled; 25437889b39SBruce Evans tmp_addr < (uintfptr_t)nullfunc_loop_profiled_end; 255e408eccfSBruce Evans tmp_addr += HISTFRACTION * sizeof(HISTCOUNTER)) 256e408eccfSBruce Evans nullfunc_loop_profiled_time += KCOUNT(p, PC_TO_I(p, tmp_addr)); 257d6b9e17eSBruce Evans #define CALIB_DOSCALE(count) (((count) + CALIB_SCALE / 3) / CALIB_SCALE) 258d6b9e17eSBruce Evans #define c2n(count, freq) ((int)((count) * 1000000000LL / freq)) 259d6b9e17eSBruce Evans printf("cputime %d, empty_loop %d, nullfunc_loop_profiled %d, mcount %d, mexitcount %d\n", 260d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(cputime_overhead, p->profrate)), 261d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(empty_loop_time, p->profrate)), 262d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(nullfunc_loop_profiled_time, p->profrate)), 263d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(mcount_overhead, p->profrate)), 264d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(mexitcount_overhead, p->profrate))); 265d6b9e17eSBruce Evans cputime_overhead -= empty_loop_time; 266d6b9e17eSBruce Evans mcount_overhead -= empty_loop_time; 267d6b9e17eSBruce Evans mexitcount_overhead -= empty_loop_time; 268d6b9e17eSBruce Evans 269d6b9e17eSBruce Evans /*- 270d6b9e17eSBruce Evans * Profiling overheads are determined by the times between the 271d6b9e17eSBruce Evans * following events: 272d6b9e17eSBruce Evans * MC1: mcount() is called 273d6b9e17eSBruce Evans * MC2: cputime() (called from mcount()) latches the timer 274d6b9e17eSBruce Evans * MC3: mcount() completes 275d6b9e17eSBruce Evans * ME1: mexitcount() is called 276d6b9e17eSBruce Evans * ME2: cputime() (called from mexitcount()) latches the timer 277d6b9e17eSBruce Evans * ME3: mexitcount() completes. 278d6b9e17eSBruce Evans * The times between the events vary slightly depending on instruction 279d6b9e17eSBruce Evans * combination and cache misses, etc. Attempt to determine the 280d6b9e17eSBruce Evans * minimum times. These can be subtracted from the profiling times 281d6b9e17eSBruce Evans * without much risk of reducing the profiling times below what they 282d6b9e17eSBruce Evans * would be when profiling is not configured. Abbreviate: 283d6b9e17eSBruce Evans * ab = minimum time between MC1 and MC3 284d6b9e17eSBruce Evans * a = minumum time between MC1 and MC2 285d6b9e17eSBruce Evans * b = minimum time between MC2 and MC3 286d6b9e17eSBruce Evans * cd = minimum time between ME1 and ME3 287d6b9e17eSBruce Evans * c = minimum time between ME1 and ME2 288d6b9e17eSBruce Evans * d = minimum time between ME2 and ME3. 289d6b9e17eSBruce Evans * These satisfy the relations: 290d6b9e17eSBruce Evans * ab <= mcount_overhead (just measured) 291d6b9e17eSBruce Evans * a + b <= ab 292d6b9e17eSBruce Evans * cd <= mexitcount_overhead (just measured) 293d6b9e17eSBruce Evans * c + d <= cd 294d6b9e17eSBruce Evans * a + d <= nullfunc_loop_profiled_time (just measured) 295d6b9e17eSBruce Evans * a >= 0, b >= 0, c >= 0, d >= 0. 296d6b9e17eSBruce Evans * Assume that ab and cd are equal to the minimums. 297d6b9e17eSBruce Evans */ 298d6b9e17eSBruce Evans p->cputime_overhead = CALIB_DOSCALE(cputime_overhead); 299d6b9e17eSBruce Evans p->mcount_overhead = CALIB_DOSCALE(mcount_overhead - cputime_overhead); 300d6b9e17eSBruce Evans p->mexitcount_overhead = CALIB_DOSCALE(mexitcount_overhead 301d6b9e17eSBruce Evans - cputime_overhead); 302d6b9e17eSBruce Evans nullfunc_loop_overhead = nullfunc_loop_profiled_time - empty_loop_time; 303d6b9e17eSBruce Evans p->mexitcount_post_overhead = CALIB_DOSCALE((mcount_overhead 304d6b9e17eSBruce Evans - nullfunc_loop_overhead) 305d6b9e17eSBruce Evans / 4); 306d6b9e17eSBruce Evans p->mexitcount_pre_overhead = p->mexitcount_overhead 307d6b9e17eSBruce Evans + p->cputime_overhead 308d6b9e17eSBruce Evans - p->mexitcount_post_overhead; 309d6b9e17eSBruce Evans p->mcount_pre_overhead = CALIB_DOSCALE(nullfunc_loop_overhead) 310d6b9e17eSBruce Evans - p->mexitcount_post_overhead; 311d6b9e17eSBruce Evans p->mcount_post_overhead = p->mcount_overhead 312d6b9e17eSBruce Evans + p->cputime_overhead 313d6b9e17eSBruce Evans - p->mcount_pre_overhead; 314d6b9e17eSBruce Evans printf( 315d6b9e17eSBruce Evans "Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d nsec\n", 316d6b9e17eSBruce Evans c2n(p->cputime_overhead, p->profrate), 317d6b9e17eSBruce Evans c2n(p->mcount_overhead, p->profrate), 318d6b9e17eSBruce Evans c2n(p->mcount_pre_overhead, p->profrate), 319d6b9e17eSBruce Evans c2n(p->mcount_post_overhead, p->profrate), 320d6b9e17eSBruce Evans c2n(p->cputime_overhead, p->profrate), 321d6b9e17eSBruce Evans c2n(p->mexitcount_overhead, p->profrate), 322d6b9e17eSBruce Evans c2n(p->mexitcount_pre_overhead, p->profrate), 323d6b9e17eSBruce Evans c2n(p->mexitcount_post_overhead, p->profrate)); 324d6b9e17eSBruce Evans printf( 325d6b9e17eSBruce Evans "Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d cycles\n", 326d6b9e17eSBruce Evans p->cputime_overhead, p->mcount_overhead, 327d6b9e17eSBruce Evans p->mcount_pre_overhead, p->mcount_post_overhead, 328d6b9e17eSBruce Evans p->cputime_overhead, p->mexitcount_overhead, 329d6b9e17eSBruce Evans p->mexitcount_pre_overhead, p->mexitcount_post_overhead); 330912e6037SBruce Evans #endif /* GUPROF */ 331df8bae1dSRodney W. Grimes } 332df8bae1dSRodney W. Grimes 333df8bae1dSRodney W. Grimes /* 334df8bae1dSRodney W. Grimes * Return kernel profiling information. 335df8bae1dSRodney W. Grimes */ 3364b2af45fSPoul-Henning Kamp static int 33782d9ae4eSPoul-Henning Kamp sysctl_kern_prof(SYSCTL_HANDLER_ARGS) 338df8bae1dSRodney W. Grimes { 3394b2af45fSPoul-Henning Kamp int *name = (int *) arg1; 3404b2af45fSPoul-Henning Kamp u_int namelen = arg2; 341df8bae1dSRodney W. Grimes struct gmonparam *gp = &_gmonparam; 342df8bae1dSRodney W. Grimes int error; 343912e6037SBruce Evans int state; 344df8bae1dSRodney W. Grimes 345df8bae1dSRodney W. Grimes /* all sysctl names at this level are terminal */ 346df8bae1dSRodney W. Grimes if (namelen != 1) 347df8bae1dSRodney W. Grimes return (ENOTDIR); /* overloaded */ 348df8bae1dSRodney W. Grimes 349df8bae1dSRodney W. Grimes switch (name[0]) { 350df8bae1dSRodney W. Grimes case GPROF_STATE: 351912e6037SBruce Evans state = gp->state; 352912e6037SBruce Evans error = sysctl_handle_int(oidp, &state, 0, req); 353df8bae1dSRodney W. Grimes if (error) 354df8bae1dSRodney W. Grimes return (error); 355912e6037SBruce Evans if (!req->newptr) 356912e6037SBruce Evans return (0); 357912e6037SBruce Evans if (state == GMON_PROF_OFF) { 358d6b9e17eSBruce Evans gp->state = state; 359df8bae1dSRodney W. Grimes stopprofclock(&proc0); 360d6b9e17eSBruce Evans stopguprof(gp); 361912e6037SBruce Evans } else if (state == GMON_PROF_ON) { 362d6b9e17eSBruce Evans gp->state = GMON_PROF_OFF; 363d6b9e17eSBruce Evans stopguprof(gp); 364912e6037SBruce Evans gp->profrate = profhz; 365df8bae1dSRodney W. Grimes startprofclock(&proc0); 366d6b9e17eSBruce Evans gp->state = state; 367912e6037SBruce Evans #ifdef GUPROF 368912e6037SBruce Evans } else if (state == GMON_PROF_HIRES) { 369d6b9e17eSBruce Evans gp->state = GMON_PROF_OFF; 370912e6037SBruce Evans stopprofclock(&proc0); 371d6b9e17eSBruce Evans startguprof(gp); 372912e6037SBruce Evans gp->state = state; 373912e6037SBruce Evans #endif 374912e6037SBruce Evans } else if (state != gp->state) 375912e6037SBruce Evans return (EINVAL); 376df8bae1dSRodney W. Grimes return (0); 377df8bae1dSRodney W. Grimes case GPROF_COUNT: 3784b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, 3794b2af45fSPoul-Henning Kamp gp->kcount, gp->kcountsize, req)); 380df8bae1dSRodney W. Grimes case GPROF_FROMS: 3814b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, 3824b2af45fSPoul-Henning Kamp gp->froms, gp->fromssize, req)); 383df8bae1dSRodney W. Grimes case GPROF_TOS: 3844b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, 3854b2af45fSPoul-Henning Kamp gp->tos, gp->tossize, req)); 386df8bae1dSRodney W. Grimes case GPROF_GMONPARAM: 3874b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, gp, sizeof *gp, req)); 388df8bae1dSRodney W. Grimes default: 389df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 390df8bae1dSRodney W. Grimes } 391df8bae1dSRodney W. Grimes /* NOTREACHED */ 392df8bae1dSRodney W. Grimes } 3934b2af45fSPoul-Henning Kamp 3944b2af45fSPoul-Henning Kamp SYSCTL_NODE(_kern, KERN_PROF, prof, CTLFLAG_RW, sysctl_kern_prof, ""); 395df8bae1dSRodney W. Grimes #endif /* GPROF */ 396df8bae1dSRodney W. Grimes 397df8bae1dSRodney W. Grimes /* 398df8bae1dSRodney W. Grimes * Profiling system call. 399df8bae1dSRodney W. Grimes * 400df8bae1dSRodney W. Grimes * The scale factor is a fixed point number with 16 bits of fraction, so that 401df8bae1dSRodney W. Grimes * 1.0 is represented as 0x10000. A scale factor of 0 turns off profiling. 402df8bae1dSRodney W. Grimes */ 403d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 404df8bae1dSRodney W. Grimes struct profil_args { 405df8bae1dSRodney W. Grimes caddr_t samples; 406134e06feSBruce Evans size_t size; 407134e06feSBruce Evans size_t offset; 408df8bae1dSRodney W. Grimes u_int scale; 409df8bae1dSRodney W. Grimes }; 410d2d3e875SBruce Evans #endif 4116f1e8c18SMatthew Dillon /* 4126f1e8c18SMatthew Dillon * MPSAFE 4136f1e8c18SMatthew Dillon */ 414df8bae1dSRodney W. Grimes /* ARGSUSED */ 41526f9a767SRodney W. Grimes int 416b40ce416SJulian Elischer profil(td, uap) 417b40ce416SJulian Elischer struct thread *td; 418df8bae1dSRodney W. Grimes register struct profil_args *uap; 419df8bae1dSRodney W. Grimes { 420df8bae1dSRodney W. Grimes register struct uprof *upp; 421df8bae1dSRodney W. Grimes int s; 4226f1e8c18SMatthew Dillon int error = 0; 423df8bae1dSRodney W. Grimes 4246f1e8c18SMatthew Dillon mtx_lock(&Giant); 4256f1e8c18SMatthew Dillon 4266f1e8c18SMatthew Dillon if (uap->scale > (1 << 16)) { 4276f1e8c18SMatthew Dillon error = EINVAL; 4286f1e8c18SMatthew Dillon goto done2; 4296f1e8c18SMatthew Dillon } 430df8bae1dSRodney W. Grimes if (uap->scale == 0) { 431b40ce416SJulian Elischer stopprofclock(td->td_proc); 4326f1e8c18SMatthew Dillon goto done2; 433df8bae1dSRodney W. Grimes } 434b40ce416SJulian Elischer upp = &td->td_proc->p_stats->p_prof; 435df8bae1dSRodney W. Grimes 436df8bae1dSRodney W. Grimes /* Block profile interrupts while changing state. */ 437df8bae1dSRodney W. Grimes s = splstatclock(); 438df8bae1dSRodney W. Grimes upp->pr_off = uap->offset; 439df8bae1dSRodney W. Grimes upp->pr_scale = uap->scale; 440df8bae1dSRodney W. Grimes upp->pr_base = uap->samples; 441df8bae1dSRodney W. Grimes upp->pr_size = uap->size; 442b40ce416SJulian Elischer startprofclock(td->td_proc); 443df8bae1dSRodney W. Grimes splx(s); 444df8bae1dSRodney W. Grimes 4456f1e8c18SMatthew Dillon done2: 4466f1e8c18SMatthew Dillon mtx_unlock(&Giant); 4476f1e8c18SMatthew Dillon return (error); 448df8bae1dSRodney W. Grimes } 449df8bae1dSRodney W. Grimes 450df8bae1dSRodney W. Grimes /* 451df8bae1dSRodney W. Grimes * Scale is a fixed-point number with the binary point 16 bits 452df8bae1dSRodney W. Grimes * into the value, and is <= 1.0. pc is at most 32 bits, so the 453df8bae1dSRodney W. Grimes * intermediate result is at most 48 bits. 454df8bae1dSRodney W. Grimes */ 455df8bae1dSRodney W. Grimes #define PC_TO_INDEX(pc, prof) \ 456df8bae1dSRodney W. Grimes ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \ 457df8bae1dSRodney W. Grimes (u_quad_t)((prof)->pr_scale)) >> 16) & ~1) 458df8bae1dSRodney W. Grimes 459df8bae1dSRodney W. Grimes /* 460df8bae1dSRodney W. Grimes * Collect user-level profiling statistics; called on a profiling tick, 461df8bae1dSRodney W. Grimes * when a process is running in user-mode. This routine may be called 462df8bae1dSRodney W. Grimes * from an interrupt context. We try to update the user profiling buffers 463df8bae1dSRodney W. Grimes * cheaply with fuswintr() and suswintr(). If that fails, we revert to 464df8bae1dSRodney W. Grimes * an AST that will vector us to trap() with a context in which copyin 465df8bae1dSRodney W. Grimes * and copyout will work. Trap will then call addupc_task(). 466df8bae1dSRodney W. Grimes * 467df8bae1dSRodney W. Grimes * Note that we may (rarely) not get around to the AST soon enough, and 468df8bae1dSRodney W. Grimes * lose profile ticks when the next tick overwrites this one, but in this 469df8bae1dSRodney W. Grimes * case the system is overloaded and the profile is probably already 470df8bae1dSRodney W. Grimes * inaccurate. 471df8bae1dSRodney W. Grimes */ 472df8bae1dSRodney W. Grimes void 473b40ce416SJulian Elischer addupc_intr(ke, pc, ticks) 474b40ce416SJulian Elischer register struct kse *ke; 47551c91299SJohn Baldwin register uintptr_t pc; 476df8bae1dSRodney W. Grimes u_int ticks; 477df8bae1dSRodney W. Grimes { 478df8bae1dSRodney W. Grimes register struct uprof *prof; 479df8bae1dSRodney W. Grimes register caddr_t addr; 480df8bae1dSRodney W. Grimes register u_int i; 481df8bae1dSRodney W. Grimes register int v; 482df8bae1dSRodney W. Grimes 483df8bae1dSRodney W. Grimes if (ticks == 0) 484df8bae1dSRodney W. Grimes return; 485b40ce416SJulian Elischer prof = &ke->ke_proc->p_stats->p_prof; 486df8bae1dSRodney W. Grimes if (pc < prof->pr_off || 487df8bae1dSRodney W. Grimes (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) 488df8bae1dSRodney W. Grimes return; /* out of range; ignore */ 489df8bae1dSRodney W. Grimes 490df8bae1dSRodney W. Grimes addr = prof->pr_base + i; 491df8bae1dSRodney W. Grimes if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + ticks) == -1) { 492688ebe12SJohn Baldwin mtx_lock_spin(&sched_lock); 493df8bae1dSRodney W. Grimes prof->pr_addr = pc; 494df8bae1dSRodney W. Grimes prof->pr_ticks = ticks; 495b40ce416SJulian Elischer ke->ke_flags |= KEF_OWEUPC | KEF_ASTPENDING ; 496688ebe12SJohn Baldwin mtx_unlock_spin(&sched_lock); 497df8bae1dSRodney W. Grimes } 498df8bae1dSRodney W. Grimes } 499df8bae1dSRodney W. Grimes 500df8bae1dSRodney W. Grimes /* 501df8bae1dSRodney W. Grimes * Much like before, but we can afford to take faults here. If the 502df8bae1dSRodney W. Grimes * update fails, we simply turn off profiling. 503df8bae1dSRodney W. Grimes */ 504037d027cSBruce Evans void 505b40ce416SJulian Elischer addupc_task(ke, pc, ticks) 506b40ce416SJulian Elischer register struct kse *ke; 50751c91299SJohn Baldwin register uintptr_t pc; 508df8bae1dSRodney W. Grimes u_int ticks; 509df8bae1dSRodney W. Grimes { 510b40ce416SJulian Elischer struct proc *p = ke->ke_proc; 511df8bae1dSRodney W. Grimes register struct uprof *prof; 512df8bae1dSRodney W. Grimes register caddr_t addr; 513df8bae1dSRodney W. Grimes register u_int i; 514df8bae1dSRodney W. Grimes u_short v; 515df8bae1dSRodney W. Grimes 51648fd1f38SJohn Baldwin if (ticks == 0) 517df8bae1dSRodney W. Grimes return; 518df8bae1dSRodney W. Grimes 519df8bae1dSRodney W. Grimes prof = &p->p_stats->p_prof; 520df8bae1dSRodney W. Grimes if (pc < prof->pr_off || 521df8bae1dSRodney W. Grimes (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) 522df8bae1dSRodney W. Grimes return; 523df8bae1dSRodney W. Grimes 524df8bae1dSRodney W. Grimes addr = prof->pr_base + i; 52501609114SAlfred Perlstein if (copyin(addr, &v, sizeof(v)) == 0) { 526df8bae1dSRodney W. Grimes v += ticks; 52701609114SAlfred Perlstein if (copyout(&v, addr, sizeof(v)) == 0) 528df8bae1dSRodney W. Grimes return; 529df8bae1dSRodney W. Grimes } 530df8bae1dSRodney W. Grimes stopprofclock(p); 531df8bae1dSRodney W. Grimes } 532