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, 110a163d034SWarner Losh 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, 179a163d034SWarner Losh 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"); 231fa15abd8SPoul-Henning Kamp #elif defined(lint) 232912e6037SBruce Evans #else 233912e6037SBruce Evans #error 234912e6037SBruce Evans #endif 235d6b9e17eSBruce Evans mcount_overhead = KCOUNT(p, PC_TO_I(p, profil)); 236912e6037SBruce Evans 237d6b9e17eSBruce Evans startguprof(p); 238912e6037SBruce Evans for (i = 0; i < CALIB_SCALE; i++) 23977849078SBruce Evans #if defined(__i386__) && __GNUC__ >= 2 240ea2b3e3dSBruce Evans __asm("call " __XSTRING(HIDENAME(mexitcount)) "; 1:" 241912e6037SBruce Evans : : : "ax", "bx", "cx", "dx", "memory"); 242c1087c13SBruce Evans __asm("movl $1b,%0" : "=rm" (tmp_addr)); 243fa15abd8SPoul-Henning Kamp #elif defined(lint) 244912e6037SBruce Evans #else 245912e6037SBruce Evans #error 246912e6037SBruce Evans #endif 247e408eccfSBruce Evans mexitcount_overhead = KCOUNT(p, PC_TO_I(p, tmp_addr)); 248912e6037SBruce Evans 249912e6037SBruce Evans p->state = GMON_PROF_OFF; 250d6b9e17eSBruce Evans stopguprof(p); 251d6b9e17eSBruce Evans 2527e1f6dfeSJohn Baldwin critical_exit(); 253912e6037SBruce Evans 254d6b9e17eSBruce Evans nullfunc_loop_profiled_time = 0; 25537889b39SBruce Evans for (tmp_addr = (uintfptr_t)nullfunc_loop_profiled; 25637889b39SBruce Evans tmp_addr < (uintfptr_t)nullfunc_loop_profiled_end; 257e408eccfSBruce Evans tmp_addr += HISTFRACTION * sizeof(HISTCOUNTER)) 258e408eccfSBruce Evans nullfunc_loop_profiled_time += KCOUNT(p, PC_TO_I(p, tmp_addr)); 259d6b9e17eSBruce Evans #define CALIB_DOSCALE(count) (((count) + CALIB_SCALE / 3) / CALIB_SCALE) 260d6b9e17eSBruce Evans #define c2n(count, freq) ((int)((count) * 1000000000LL / freq)) 261d6b9e17eSBruce Evans printf("cputime %d, empty_loop %d, nullfunc_loop_profiled %d, mcount %d, mexitcount %d\n", 262d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(cputime_overhead, p->profrate)), 263d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(empty_loop_time, p->profrate)), 264d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(nullfunc_loop_profiled_time, p->profrate)), 265d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(mcount_overhead, p->profrate)), 266d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(mexitcount_overhead, p->profrate))); 267d6b9e17eSBruce Evans cputime_overhead -= empty_loop_time; 268d6b9e17eSBruce Evans mcount_overhead -= empty_loop_time; 269d6b9e17eSBruce Evans mexitcount_overhead -= empty_loop_time; 270d6b9e17eSBruce Evans 271d6b9e17eSBruce Evans /*- 272d6b9e17eSBruce Evans * Profiling overheads are determined by the times between the 273d6b9e17eSBruce Evans * following events: 274d6b9e17eSBruce Evans * MC1: mcount() is called 275d6b9e17eSBruce Evans * MC2: cputime() (called from mcount()) latches the timer 276d6b9e17eSBruce Evans * MC3: mcount() completes 277d6b9e17eSBruce Evans * ME1: mexitcount() is called 278d6b9e17eSBruce Evans * ME2: cputime() (called from mexitcount()) latches the timer 279d6b9e17eSBruce Evans * ME3: mexitcount() completes. 280d6b9e17eSBruce Evans * The times between the events vary slightly depending on instruction 281d6b9e17eSBruce Evans * combination and cache misses, etc. Attempt to determine the 282d6b9e17eSBruce Evans * minimum times. These can be subtracted from the profiling times 283d6b9e17eSBruce Evans * without much risk of reducing the profiling times below what they 284d6b9e17eSBruce Evans * would be when profiling is not configured. Abbreviate: 285d6b9e17eSBruce Evans * ab = minimum time between MC1 and MC3 286d6b9e17eSBruce Evans * a = minumum time between MC1 and MC2 287d6b9e17eSBruce Evans * b = minimum time between MC2 and MC3 288d6b9e17eSBruce Evans * cd = minimum time between ME1 and ME3 289d6b9e17eSBruce Evans * c = minimum time between ME1 and ME2 290d6b9e17eSBruce Evans * d = minimum time between ME2 and ME3. 291d6b9e17eSBruce Evans * These satisfy the relations: 292d6b9e17eSBruce Evans * ab <= mcount_overhead (just measured) 293d6b9e17eSBruce Evans * a + b <= ab 294d6b9e17eSBruce Evans * cd <= mexitcount_overhead (just measured) 295d6b9e17eSBruce Evans * c + d <= cd 296d6b9e17eSBruce Evans * a + d <= nullfunc_loop_profiled_time (just measured) 297d6b9e17eSBruce Evans * a >= 0, b >= 0, c >= 0, d >= 0. 298d6b9e17eSBruce Evans * Assume that ab and cd are equal to the minimums. 299d6b9e17eSBruce Evans */ 300d6b9e17eSBruce Evans p->cputime_overhead = CALIB_DOSCALE(cputime_overhead); 301d6b9e17eSBruce Evans p->mcount_overhead = CALIB_DOSCALE(mcount_overhead - cputime_overhead); 302d6b9e17eSBruce Evans p->mexitcount_overhead = CALIB_DOSCALE(mexitcount_overhead 303d6b9e17eSBruce Evans - cputime_overhead); 304d6b9e17eSBruce Evans nullfunc_loop_overhead = nullfunc_loop_profiled_time - empty_loop_time; 305d6b9e17eSBruce Evans p->mexitcount_post_overhead = CALIB_DOSCALE((mcount_overhead 306d6b9e17eSBruce Evans - nullfunc_loop_overhead) 307d6b9e17eSBruce Evans / 4); 308d6b9e17eSBruce Evans p->mexitcount_pre_overhead = p->mexitcount_overhead 309d6b9e17eSBruce Evans + p->cputime_overhead 310d6b9e17eSBruce Evans - p->mexitcount_post_overhead; 311d6b9e17eSBruce Evans p->mcount_pre_overhead = CALIB_DOSCALE(nullfunc_loop_overhead) 312d6b9e17eSBruce Evans - p->mexitcount_post_overhead; 313d6b9e17eSBruce Evans p->mcount_post_overhead = p->mcount_overhead 314d6b9e17eSBruce Evans + p->cputime_overhead 315d6b9e17eSBruce Evans - p->mcount_pre_overhead; 316d6b9e17eSBruce Evans printf( 317d6b9e17eSBruce Evans "Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d nsec\n", 318d6b9e17eSBruce Evans c2n(p->cputime_overhead, p->profrate), 319d6b9e17eSBruce Evans c2n(p->mcount_overhead, p->profrate), 320d6b9e17eSBruce Evans c2n(p->mcount_pre_overhead, p->profrate), 321d6b9e17eSBruce Evans c2n(p->mcount_post_overhead, p->profrate), 322d6b9e17eSBruce Evans c2n(p->cputime_overhead, p->profrate), 323d6b9e17eSBruce Evans c2n(p->mexitcount_overhead, p->profrate), 324d6b9e17eSBruce Evans c2n(p->mexitcount_pre_overhead, p->profrate), 325d6b9e17eSBruce Evans c2n(p->mexitcount_post_overhead, p->profrate)); 326d6b9e17eSBruce Evans printf( 327d6b9e17eSBruce Evans "Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d cycles\n", 328d6b9e17eSBruce Evans p->cputime_overhead, p->mcount_overhead, 329d6b9e17eSBruce Evans p->mcount_pre_overhead, p->mcount_post_overhead, 330d6b9e17eSBruce Evans p->cputime_overhead, p->mexitcount_overhead, 331d6b9e17eSBruce Evans p->mexitcount_pre_overhead, p->mexitcount_post_overhead); 332912e6037SBruce Evans #endif /* GUPROF */ 333df8bae1dSRodney W. Grimes } 334df8bae1dSRodney W. Grimes 335df8bae1dSRodney W. Grimes /* 336df8bae1dSRodney W. Grimes * Return kernel profiling information. 337df8bae1dSRodney W. Grimes */ 3384b2af45fSPoul-Henning Kamp static int 33982d9ae4eSPoul-Henning Kamp sysctl_kern_prof(SYSCTL_HANDLER_ARGS) 340df8bae1dSRodney W. Grimes { 3414b2af45fSPoul-Henning Kamp int *name = (int *) arg1; 3424b2af45fSPoul-Henning Kamp u_int namelen = arg2; 343df8bae1dSRodney W. Grimes struct gmonparam *gp = &_gmonparam; 344df8bae1dSRodney W. Grimes int error; 345912e6037SBruce Evans int state; 346df8bae1dSRodney W. Grimes 347df8bae1dSRodney W. Grimes /* all sysctl names at this level are terminal */ 348df8bae1dSRodney W. Grimes if (namelen != 1) 349df8bae1dSRodney W. Grimes return (ENOTDIR); /* overloaded */ 350df8bae1dSRodney W. Grimes 351df8bae1dSRodney W. Grimes switch (name[0]) { 352df8bae1dSRodney W. Grimes case GPROF_STATE: 353912e6037SBruce Evans state = gp->state; 354912e6037SBruce Evans error = sysctl_handle_int(oidp, &state, 0, req); 355df8bae1dSRodney W. Grimes if (error) 356df8bae1dSRodney W. Grimes return (error); 357912e6037SBruce Evans if (!req->newptr) 358912e6037SBruce Evans return (0); 359912e6037SBruce Evans if (state == GMON_PROF_OFF) { 360d6b9e17eSBruce Evans gp->state = state; 361a282253aSJulian Elischer PROC_LOCK(&proc0); 362df8bae1dSRodney W. Grimes stopprofclock(&proc0); 363a282253aSJulian Elischer PROC_UNLOCK(&proc0); 364d6b9e17eSBruce Evans stopguprof(gp); 365912e6037SBruce Evans } else if (state == GMON_PROF_ON) { 366d6b9e17eSBruce Evans gp->state = GMON_PROF_OFF; 367d6b9e17eSBruce Evans stopguprof(gp); 368912e6037SBruce Evans gp->profrate = profhz; 3699752f794SJohn Baldwin PROC_LOCK(&proc0); 370df8bae1dSRodney W. Grimes startprofclock(&proc0); 3719752f794SJohn Baldwin PROC_UNLOCK(&proc0); 372d6b9e17eSBruce Evans gp->state = state; 373912e6037SBruce Evans #ifdef GUPROF 374912e6037SBruce Evans } else if (state == GMON_PROF_HIRES) { 375d6b9e17eSBruce Evans gp->state = GMON_PROF_OFF; 376a282253aSJulian Elischer PROC_LOCK(&proc0); 377912e6037SBruce Evans stopprofclock(&proc0); 378a282253aSJulian Elischer PROC_UNLOCK(&proc0); 379d6b9e17eSBruce Evans startguprof(gp); 380912e6037SBruce Evans gp->state = state; 381912e6037SBruce Evans #endif 382912e6037SBruce Evans } else if (state != gp->state) 383912e6037SBruce Evans return (EINVAL); 384df8bae1dSRodney W. Grimes return (0); 385df8bae1dSRodney W. Grimes case GPROF_COUNT: 3864b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, 3874b2af45fSPoul-Henning Kamp gp->kcount, gp->kcountsize, req)); 388df8bae1dSRodney W. Grimes case GPROF_FROMS: 3894b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, 3904b2af45fSPoul-Henning Kamp gp->froms, gp->fromssize, req)); 391df8bae1dSRodney W. Grimes case GPROF_TOS: 3924b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, 3934b2af45fSPoul-Henning Kamp gp->tos, gp->tossize, req)); 394df8bae1dSRodney W. Grimes case GPROF_GMONPARAM: 3954b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, gp, sizeof *gp, req)); 396df8bae1dSRodney W. Grimes default: 397df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 398df8bae1dSRodney W. Grimes } 399df8bae1dSRodney W. Grimes /* NOTREACHED */ 400df8bae1dSRodney W. Grimes } 4014b2af45fSPoul-Henning Kamp 4024b2af45fSPoul-Henning Kamp SYSCTL_NODE(_kern, KERN_PROF, prof, CTLFLAG_RW, sysctl_kern_prof, ""); 403df8bae1dSRodney W. Grimes #endif /* GPROF */ 404df8bae1dSRodney W. Grimes 405df8bae1dSRodney W. Grimes /* 406df8bae1dSRodney W. Grimes * Profiling system call. 407df8bae1dSRodney W. Grimes * 408df8bae1dSRodney W. Grimes * The scale factor is a fixed point number with 16 bits of fraction, so that 409df8bae1dSRodney W. Grimes * 1.0 is represented as 0x10000. A scale factor of 0 turns off profiling. 410df8bae1dSRodney W. Grimes */ 411d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 412df8bae1dSRodney W. Grimes struct profil_args { 413df8bae1dSRodney W. Grimes caddr_t samples; 414134e06feSBruce Evans size_t size; 415134e06feSBruce Evans size_t offset; 416df8bae1dSRodney W. Grimes u_int scale; 417df8bae1dSRodney W. Grimes }; 418d2d3e875SBruce Evans #endif 4196f1e8c18SMatthew Dillon /* 4206f1e8c18SMatthew Dillon * MPSAFE 4216f1e8c18SMatthew Dillon */ 422df8bae1dSRodney W. Grimes /* ARGSUSED */ 42326f9a767SRodney W. Grimes int 424b40ce416SJulian Elischer profil(td, uap) 425b40ce416SJulian Elischer struct thread *td; 426df8bae1dSRodney W. Grimes register struct profil_args *uap; 427df8bae1dSRodney W. Grimes { 428a282253aSJulian Elischer struct uprof *upp; 4299752f794SJohn Baldwin struct proc *p; 430df8bae1dSRodney W. Grimes 4319752f794SJohn Baldwin if (uap->scale > (1 << 16)) 4329752f794SJohn Baldwin return (EINVAL); 4336f1e8c18SMatthew Dillon 4349752f794SJohn Baldwin p = td->td_proc; 435df8bae1dSRodney W. Grimes if (uap->scale == 0) { 436a282253aSJulian Elischer PROC_LOCK(td->td_proc); 437b40ce416SJulian Elischer stopprofclock(td->td_proc); 438a282253aSJulian Elischer PROC_UNLOCK(td->td_proc); 4399752f794SJohn Baldwin return (0); 440df8bae1dSRodney W. Grimes } 441b40ce416SJulian Elischer upp = &td->td_proc->p_stats->p_prof; 442df8bae1dSRodney W. Grimes upp->pr_off = uap->offset; 443df8bae1dSRodney W. Grimes upp->pr_scale = uap->scale; 444df8bae1dSRodney W. Grimes upp->pr_base = uap->samples; 445df8bae1dSRodney W. Grimes upp->pr_size = uap->size; 4469752f794SJohn Baldwin PROC_LOCK(p); 4479752f794SJohn Baldwin startprofclock(p); 4489752f794SJohn Baldwin PROC_UNLOCK(p); 449df8bae1dSRodney W. Grimes 4509752f794SJohn Baldwin return (0); 451df8bae1dSRodney W. Grimes } 452df8bae1dSRodney W. Grimes 453df8bae1dSRodney W. Grimes /* 454df8bae1dSRodney W. Grimes * Scale is a fixed-point number with the binary point 16 bits 455df8bae1dSRodney W. Grimes * into the value, and is <= 1.0. pc is at most 32 bits, so the 456df8bae1dSRodney W. Grimes * intermediate result is at most 48 bits. 457df8bae1dSRodney W. Grimes */ 458df8bae1dSRodney W. Grimes #define PC_TO_INDEX(pc, prof) \ 459df8bae1dSRodney W. Grimes ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \ 460df8bae1dSRodney W. Grimes (u_quad_t)((prof)->pr_scale)) >> 16) & ~1) 461df8bae1dSRodney W. Grimes 462df8bae1dSRodney W. Grimes /* 463df8bae1dSRodney W. Grimes * Collect user-level profiling statistics; called on a profiling tick, 464df8bae1dSRodney W. Grimes * when a process is running in user-mode. This routine may be called 465df8bae1dSRodney W. Grimes * from an interrupt context. We try to update the user profiling buffers 466df8bae1dSRodney W. Grimes * cheaply with fuswintr() and suswintr(). If that fails, we revert to 467df8bae1dSRodney W. Grimes * an AST that will vector us to trap() with a context in which copyin 468df8bae1dSRodney W. Grimes * and copyout will work. Trap will then call addupc_task(). 469df8bae1dSRodney W. Grimes * 470df8bae1dSRodney W. Grimes * Note that we may (rarely) not get around to the AST soon enough, and 471df8bae1dSRodney W. Grimes * lose profile ticks when the next tick overwrites this one, but in this 472df8bae1dSRodney W. Grimes * case the system is overloaded and the profile is probably already 473df8bae1dSRodney W. Grimes * inaccurate. 474df8bae1dSRodney W. Grimes */ 475df8bae1dSRodney W. Grimes void 4764a338afdSJulian Elischer addupc_intr(struct thread *td, uintptr_t pc, u_int ticks) 477df8bae1dSRodney W. Grimes { 478a282253aSJulian Elischer struct uprof *prof; 479a282253aSJulian Elischer caddr_t addr; 480a282253aSJulian Elischer u_int i; 481a282253aSJulian Elischer int v; 482df8bae1dSRodney W. Grimes 483df8bae1dSRodney W. Grimes if (ticks == 0) 484df8bae1dSRodney W. Grimes return; 4854a338afdSJulian Elischer prof = &td->td_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); 4936f8132a8SJulian Elischer prof->pr_addr = pc; 4946f8132a8SJulian Elischer prof->pr_ticks = ticks; 4954a338afdSJulian Elischer td->td_flags |= TDF_OWEUPC | TDF_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. 503a282253aSJulian Elischer * XXXKSE, don't use kse unless we got sched lock. 504df8bae1dSRodney W. Grimes */ 505037d027cSBruce Evans void 5064a338afdSJulian Elischer addupc_task(struct thread *td, uintptr_t pc, u_int ticks) 507df8bae1dSRodney W. Grimes { 5084a338afdSJulian Elischer struct proc *p = td->td_proc; 509a282253aSJulian Elischer struct uprof *prof; 510a282253aSJulian Elischer caddr_t addr; 511a282253aSJulian Elischer u_int i; 512df8bae1dSRodney W. Grimes u_short v; 513a282253aSJulian Elischer int stop = 0; 514df8bae1dSRodney W. Grimes 51548fd1f38SJohn Baldwin if (ticks == 0) 516df8bae1dSRodney W. Grimes return; 517df8bae1dSRodney W. Grimes 518a282253aSJulian Elischer PROC_LOCK(p); 5199752f794SJohn Baldwin if (!(p->p_flag & P_PROFIL)) { 520a282253aSJulian Elischer PROC_UNLOCK(p); 521a282253aSJulian Elischer return; 522a282253aSJulian Elischer } 523a282253aSJulian Elischer p->p_profthreads++; 524a282253aSJulian Elischer PROC_UNLOCK(p); 525df8bae1dSRodney W. Grimes prof = &p->p_stats->p_prof; 526df8bae1dSRodney W. Grimes if (pc < prof->pr_off || 527a282253aSJulian Elischer (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) { 528a282253aSJulian Elischer goto out; 529a282253aSJulian Elischer } 530df8bae1dSRodney W. Grimes 531df8bae1dSRodney W. Grimes addr = prof->pr_base + i; 53201609114SAlfred Perlstein if (copyin(addr, &v, sizeof(v)) == 0) { 533df8bae1dSRodney W. Grimes v += ticks; 53401609114SAlfred Perlstein if (copyout(&v, addr, sizeof(v)) == 0) 535a282253aSJulian Elischer goto out; 536df8bae1dSRodney W. Grimes } 537a282253aSJulian Elischer stop = 1; 538a282253aSJulian Elischer 539a282253aSJulian Elischer out: 540a282253aSJulian Elischer PROC_LOCK(p); 541a282253aSJulian Elischer if (--p->p_profthreads == 0) { 5429752f794SJohn Baldwin if (p->p_flag & P_STOPPROF) { 543a282253aSJulian Elischer wakeup(&p->p_profthreads); 544a282253aSJulian Elischer stop = 0; 545a282253aSJulian Elischer } 546a282253aSJulian Elischer } 547a282253aSJulian Elischer if (stop) 548df8bae1dSRodney W. Grimes stopprofclock(p); 549a282253aSJulian Elischer PROC_UNLOCK(p); 550df8bae1dSRodney W. Grimes } 551578c4786SPoul-Henning Kamp 552578c4786SPoul-Henning Kamp #if defined(__i386__) && __GNUC__ >= 2 553578c4786SPoul-Henning Kamp /* 554578c4786SPoul-Henning Kamp * Support for "--test-coverage --profile-arcs" in GCC. 555578c4786SPoul-Henning Kamp * 556578c4786SPoul-Henning Kamp * We need to call all the functions in the .ctor section, in order 557578c4786SPoul-Henning Kamp * to get all the counter-arrays strung into a list. 558578c4786SPoul-Henning Kamp * 559578c4786SPoul-Henning Kamp * XXX: the .ctors call __bb_init_func which is located in over in 560578c4786SPoul-Henning Kamp * XXX: i386/i386/support.s for historical reasons. There is probably 561578c4786SPoul-Henning Kamp * XXX: no reason for that to be assembler anymore, but doing it right 562578c4786SPoul-Henning Kamp * XXX: in MI C code requires one to reverse-engineer the type-selection 563578c4786SPoul-Henning Kamp * XXX: inside GCC. Have fun. 564578c4786SPoul-Henning Kamp * 565578c4786SPoul-Henning Kamp * XXX: Worrisome perspective: Calling the .ctors may make C++ in the 566578c4786SPoul-Henning Kamp * XXX: kernel feasible. Don't. 567578c4786SPoul-Henning Kamp */ 568578c4786SPoul-Henning Kamp typedef void (*ctor_t)(void); 569578c4786SPoul-Henning Kamp extern ctor_t _start_ctors, _stop_ctors; 570578c4786SPoul-Henning Kamp 571578c4786SPoul-Henning Kamp static void 572578c4786SPoul-Henning Kamp tcov_init(void *foo __unused) 573578c4786SPoul-Henning Kamp { 574578c4786SPoul-Henning Kamp ctor_t *p, q; 575578c4786SPoul-Henning Kamp 576578c4786SPoul-Henning Kamp for (p = &_start_ctors; p < &_stop_ctors; p++) { 577578c4786SPoul-Henning Kamp q = *p; 578578c4786SPoul-Henning Kamp q(); 579578c4786SPoul-Henning Kamp } 580578c4786SPoul-Henning Kamp } 581578c4786SPoul-Henning Kamp 582c49bddceSPoul-Henning Kamp SYSINIT(tcov_init, SI_SUB_KPROF, SI_ORDER_SECOND, tcov_init, NULL) 583578c4786SPoul-Henning Kamp 584578c4786SPoul-Henning Kamp /* 585578c4786SPoul-Henning Kamp * GCC contains magic to recognize calls to for instance execve() and 586578c4786SPoul-Henning Kamp * puts in calls to this function to preserve the profile counters. 587578c4786SPoul-Henning Kamp * XXX: Put zinging punchline here. 588578c4786SPoul-Henning Kamp */ 589578c4786SPoul-Henning Kamp void __bb_fork_func(void); 590578c4786SPoul-Henning Kamp void 591578c4786SPoul-Henning Kamp __bb_fork_func(void) 592578c4786SPoul-Henning Kamp { 593578c4786SPoul-Henning Kamp } 594578c4786SPoul-Henning Kamp 595578c4786SPoul-Henning Kamp #endif 596578c4786SPoul-Henning Kamp 597