1df8bae1dSRodney W. Grimes /*- 2*51369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*51369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1569a28758SEd Maste * 3. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes * 31df8bae1dSRodney W. Grimes * @(#)subr_prof.c 8.3 (Berkeley) 9/23/93 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 34677b542eSDavid E. O'Brien #include <sys/cdefs.h> 35677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 36677b542eSDavid E. O'Brien 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 *); 57237fdd78SRobert Watson 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 62d6b9e17eSBruce Evans void 63d6b9e17eSBruce Evans nullfunc_loop_profiled() 64d6b9e17eSBruce Evans { 65d6b9e17eSBruce Evans int i; 66d6b9e17eSBruce Evans 67d6b9e17eSBruce Evans for (i = 0; i < CALIB_SCALE; i++) 68d6b9e17eSBruce Evans nullfunc_profiled(); 69d6b9e17eSBruce Evans } 70d6b9e17eSBruce Evans 71e408eccfSBruce Evans #define nullfunc_loop_profiled_end nullfunc_profiled /* XXX */ 72e408eccfSBruce Evans 73d6b9e17eSBruce Evans void 74d6b9e17eSBruce Evans nullfunc_profiled() 75d6b9e17eSBruce Evans { 76d6b9e17eSBruce Evans } 77d6b9e17eSBruce Evans #endif /* GUPROF */ 78d6b9e17eSBruce Evans 794a44bd4bSBrian Feldman /* 804a44bd4bSBrian Feldman * Update the histograms to support extending the text region arbitrarily. 814a44bd4bSBrian Feldman * This is done slightly naively (no sparse regions), so will waste slight 824a44bd4bSBrian Feldman * amounts of memory, but will overall work nicely enough to allow profiling 834a44bd4bSBrian Feldman * of KLDs. 844a44bd4bSBrian Feldman */ 854a44bd4bSBrian Feldman void 864a44bd4bSBrian Feldman kmupetext(uintfptr_t nhighpc) 874a44bd4bSBrian Feldman { 884a44bd4bSBrian Feldman struct gmonparam np; /* slightly large */ 894a44bd4bSBrian Feldman struct gmonparam *p = &_gmonparam; 904a44bd4bSBrian Feldman char *cp; 914a44bd4bSBrian Feldman 924a44bd4bSBrian Feldman GIANT_REQUIRED; 934a44bd4bSBrian Feldman bcopy(p, &np, sizeof(*p)); 944a44bd4bSBrian Feldman np.highpc = ROUNDUP(nhighpc, HISTFRACTION * sizeof(HISTCOUNTER)); 954a44bd4bSBrian Feldman if (np.highpc <= p->highpc) 964a44bd4bSBrian Feldman return; 974a44bd4bSBrian Feldman np.textsize = np.highpc - p->lowpc; 984a44bd4bSBrian Feldman np.kcountsize = np.textsize / HISTFRACTION; 994a44bd4bSBrian Feldman np.hashfraction = HASHFRACTION; 1004a44bd4bSBrian Feldman np.fromssize = np.textsize / HASHFRACTION; 1014a44bd4bSBrian Feldman np.tolimit = np.textsize * ARCDENSITY / 100; 1024a44bd4bSBrian Feldman if (np.tolimit < MINARCS) 1034a44bd4bSBrian Feldman np.tolimit = MINARCS; 1044a44bd4bSBrian Feldman else if (np.tolimit > MAXARCS) 1054a44bd4bSBrian Feldman np.tolimit = MAXARCS; 1064a44bd4bSBrian Feldman np.tossize = np.tolimit * sizeof(struct tostruct); 1074a44bd4bSBrian Feldman cp = malloc(np.kcountsize + np.fromssize + np.tossize, 108a163d034SWarner Losh M_GPROF, M_WAITOK); 1094a44bd4bSBrian Feldman /* 1104a44bd4bSBrian Feldman * Check for something else extending highpc while we slept. 1114a44bd4bSBrian Feldman */ 1124a44bd4bSBrian Feldman if (np.highpc <= p->highpc) { 1134a44bd4bSBrian Feldman free(cp, M_GPROF); 1144a44bd4bSBrian Feldman return; 1154a44bd4bSBrian Feldman } 1164a44bd4bSBrian Feldman np.tos = (struct tostruct *)cp; 1174a44bd4bSBrian Feldman cp += np.tossize; 1184a44bd4bSBrian Feldman np.kcount = (HISTCOUNTER *)cp; 1194a44bd4bSBrian Feldman cp += np.kcountsize; 1204a44bd4bSBrian Feldman np.froms = (u_short *)cp; 1214a44bd4bSBrian Feldman #ifdef GUPROF 1224a44bd4bSBrian Feldman /* Reinitialize pointers to overhead counters. */ 1234a44bd4bSBrian Feldman np.cputime_count = &KCOUNT(&np, PC_TO_I(&np, cputime)); 1244a44bd4bSBrian Feldman np.mcount_count = &KCOUNT(&np, PC_TO_I(&np, mcount)); 1254a44bd4bSBrian Feldman np.mexitcount_count = &KCOUNT(&np, PC_TO_I(&np, mexitcount)); 1264a44bd4bSBrian Feldman #endif 1277e1f6dfeSJohn Baldwin critical_enter(); 1284a44bd4bSBrian Feldman bcopy(p->tos, np.tos, p->tossize); 1294a44bd4bSBrian Feldman bzero((char *)np.tos + p->tossize, np.tossize - p->tossize); 1304a44bd4bSBrian Feldman bcopy(p->kcount, np.kcount, p->kcountsize); 1314a44bd4bSBrian Feldman bzero((char *)np.kcount + p->kcountsize, np.kcountsize - 1324a44bd4bSBrian Feldman p->kcountsize); 1334a44bd4bSBrian Feldman bcopy(p->froms, np.froms, p->fromssize); 1344a44bd4bSBrian Feldman bzero((char *)np.froms + p->fromssize, np.fromssize - p->fromssize); 1354a44bd4bSBrian Feldman cp = (char *)p->tos; 1364a44bd4bSBrian Feldman bcopy(&np, p, sizeof(*p)); 1377e1f6dfeSJohn Baldwin critical_exit(); 1384a44bd4bSBrian Feldman free(cp, M_GPROF); 1394a44bd4bSBrian Feldman } 1404a44bd4bSBrian Feldman 1415c7761b2SBruce Evans static void 142d841aaa7SBruce Evans kmstartup(dummy) 143d841aaa7SBruce Evans void *dummy; 144df8bae1dSRodney W. Grimes { 145df8bae1dSRodney W. Grimes char *cp; 146df8bae1dSRodney W. Grimes struct gmonparam *p = &_gmonparam; 147912e6037SBruce Evans #ifdef GUPROF 148d6b9e17eSBruce Evans int cputime_overhead; 149d6b9e17eSBruce Evans int empty_loop_time; 150912e6037SBruce Evans int i; 151d6b9e17eSBruce Evans int mcount_overhead; 152d6b9e17eSBruce Evans int mexitcount_overhead; 153d6b9e17eSBruce Evans int nullfunc_loop_overhead; 154d6b9e17eSBruce Evans int nullfunc_loop_profiled_time; 15537889b39SBruce Evans uintfptr_t tmp_addr; 1565e1aea9fSPoul-Henning Kamp #endif 157912e6037SBruce Evans 158df8bae1dSRodney W. Grimes /* 159df8bae1dSRodney W. Grimes * Round lowpc and highpc to multiples of the density we're using 160df8bae1dSRodney W. Grimes * so the rest of the scaling (here and in gprof) stays in ints. 161df8bae1dSRodney W. Grimes */ 162e45d35c3SBruce Evans p->lowpc = ROUNDDOWN((u_long)btext, HISTFRACTION * sizeof(HISTCOUNTER)); 163df8bae1dSRodney W. Grimes p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER)); 164df8bae1dSRodney W. Grimes p->textsize = p->highpc - p->lowpc; 165372c2e96SBruce Evans printf("Profiling kernel, textsize=%lu [%jx..%jx]\n", 166372c2e96SBruce Evans p->textsize, (uintmax_t)p->lowpc, (uintmax_t)p->highpc); 167df8bae1dSRodney W. Grimes p->kcountsize = p->textsize / HISTFRACTION; 168df8bae1dSRodney W. Grimes p->hashfraction = HASHFRACTION; 169df8bae1dSRodney W. Grimes p->fromssize = p->textsize / HASHFRACTION; 170df8bae1dSRodney W. Grimes p->tolimit = p->textsize * ARCDENSITY / 100; 171df8bae1dSRodney W. Grimes if (p->tolimit < MINARCS) 172df8bae1dSRodney W. Grimes p->tolimit = MINARCS; 173df8bae1dSRodney W. Grimes else if (p->tolimit > MAXARCS) 174df8bae1dSRodney W. Grimes p->tolimit = MAXARCS; 175df8bae1dSRodney W. Grimes p->tossize = p->tolimit * sizeof(struct tostruct); 176df8bae1dSRodney W. Grimes cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize, 177a163d034SWarner Losh M_GPROF, M_WAITOK | M_ZERO); 178df8bae1dSRodney W. Grimes p->tos = (struct tostruct *)cp; 179df8bae1dSRodney W. Grimes cp += p->tossize; 180912e6037SBruce Evans p->kcount = (HISTCOUNTER *)cp; 181df8bae1dSRodney W. Grimes cp += p->kcountsize; 182df8bae1dSRodney W. Grimes p->froms = (u_short *)cp; 183c81d4a03SBruce Evans p->histcounter_type = FUNCTION_ALIGNMENT / HISTFRACTION * NBBY; 184912e6037SBruce Evans 185912e6037SBruce Evans #ifdef GUPROF 186c81d4a03SBruce Evans /* Signed counters. */ 187c81d4a03SBruce Evans p->histcounter_type = -p->histcounter_type; 188c81d4a03SBruce Evans 189d6b9e17eSBruce Evans /* Initialize pointers to overhead counters. */ 190912e6037SBruce Evans p->cputime_count = &KCOUNT(p, PC_TO_I(p, cputime)); 191912e6037SBruce Evans p->mcount_count = &KCOUNT(p, PC_TO_I(p, mcount)); 192912e6037SBruce Evans p->mexitcount_count = &KCOUNT(p, PC_TO_I(p, mexitcount)); 193912e6037SBruce Evans 194912e6037SBruce Evans /* 195d6b9e17eSBruce Evans * Disable interrupts to avoid interference while we calibrate 196d6b9e17eSBruce Evans * things. 197912e6037SBruce Evans */ 1987e1f6dfeSJohn Baldwin critical_enter(); 199d6b9e17eSBruce Evans 200d6b9e17eSBruce Evans /* 201d6b9e17eSBruce Evans * Determine overheads. 202d6b9e17eSBruce Evans * XXX this needs to be repeated for each useful timer/counter. 203d6b9e17eSBruce Evans */ 204d6b9e17eSBruce Evans cputime_overhead = 0; 205d6b9e17eSBruce Evans startguprof(p); 206d6b9e17eSBruce Evans for (i = 0; i < CALIB_SCALE; i++) 207d6b9e17eSBruce Evans cputime_overhead += cputime(); 208d6b9e17eSBruce Evans 209d6b9e17eSBruce Evans empty_loop(); 210d6b9e17eSBruce Evans startguprof(p); 211d6b9e17eSBruce Evans empty_loop(); 212d6b9e17eSBruce Evans empty_loop_time = cputime(); 213d6b9e17eSBruce Evans 214d6b9e17eSBruce Evans nullfunc_loop_profiled(); 215d6b9e17eSBruce Evans 216d6b9e17eSBruce Evans /* 217d6b9e17eSBruce Evans * Start profiling. There won't be any normal function calls since 218d6b9e17eSBruce Evans * interrupts are disabled, but we will call the profiling routines 219d6b9e17eSBruce Evans * directly to determine their overheads. 220d6b9e17eSBruce Evans */ 221912e6037SBruce Evans p->state = GMON_PROF_HIRES; 222912e6037SBruce Evans 223d6b9e17eSBruce Evans startguprof(p); 224d6b9e17eSBruce Evans nullfunc_loop_profiled(); 225912e6037SBruce Evans 226d6b9e17eSBruce Evans startguprof(p); 227912e6037SBruce Evans for (i = 0; i < CALIB_SCALE; i++) 2288451d0ddSKip Macy MCOUNT_OVERHEAD(sys_profil); 2298451d0ddSKip Macy mcount_overhead = KCOUNT(p, PC_TO_I(p, sys_profil)); 230912e6037SBruce Evans 231d6b9e17eSBruce Evans startguprof(p); 232912e6037SBruce Evans for (i = 0; i < CALIB_SCALE; i++) 233e77c22bfSBruce Evans MEXITCOUNT_OVERHEAD(); 234e77c22bfSBruce Evans MEXITCOUNT_OVERHEAD_GETLABEL(tmp_addr); 235e408eccfSBruce Evans mexitcount_overhead = KCOUNT(p, PC_TO_I(p, tmp_addr)); 236912e6037SBruce Evans 237912e6037SBruce Evans p->state = GMON_PROF_OFF; 238d6b9e17eSBruce Evans stopguprof(p); 239d6b9e17eSBruce Evans 2407e1f6dfeSJohn Baldwin critical_exit(); 241912e6037SBruce Evans 242d6b9e17eSBruce Evans nullfunc_loop_profiled_time = 0; 24337889b39SBruce Evans for (tmp_addr = (uintfptr_t)nullfunc_loop_profiled; 24437889b39SBruce Evans tmp_addr < (uintfptr_t)nullfunc_loop_profiled_end; 245e408eccfSBruce Evans tmp_addr += HISTFRACTION * sizeof(HISTCOUNTER)) 246e408eccfSBruce Evans nullfunc_loop_profiled_time += KCOUNT(p, PC_TO_I(p, tmp_addr)); 247d6b9e17eSBruce Evans #define CALIB_DOSCALE(count) (((count) + CALIB_SCALE / 3) / CALIB_SCALE) 248d6b9e17eSBruce Evans #define c2n(count, freq) ((int)((count) * 1000000000LL / freq)) 249d6b9e17eSBruce Evans printf("cputime %d, empty_loop %d, nullfunc_loop_profiled %d, mcount %d, mexitcount %d\n", 250d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(cputime_overhead, p->profrate)), 251d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(empty_loop_time, p->profrate)), 252d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(nullfunc_loop_profiled_time, p->profrate)), 253d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(mcount_overhead, p->profrate)), 254d6b9e17eSBruce Evans CALIB_DOSCALE(c2n(mexitcount_overhead, p->profrate))); 255d6b9e17eSBruce Evans cputime_overhead -= empty_loop_time; 256d6b9e17eSBruce Evans mcount_overhead -= empty_loop_time; 257d6b9e17eSBruce Evans mexitcount_overhead -= empty_loop_time; 258d6b9e17eSBruce Evans 2591a996ed1SEdward Tomasz Napierala /*- 260d6b9e17eSBruce Evans * Profiling overheads are determined by the times between the 261d6b9e17eSBruce Evans * following events: 262d6b9e17eSBruce Evans * MC1: mcount() is called 263d6b9e17eSBruce Evans * MC2: cputime() (called from mcount()) latches the timer 264d6b9e17eSBruce Evans * MC3: mcount() completes 265d6b9e17eSBruce Evans * ME1: mexitcount() is called 266d6b9e17eSBruce Evans * ME2: cputime() (called from mexitcount()) latches the timer 267d6b9e17eSBruce Evans * ME3: mexitcount() completes. 268d6b9e17eSBruce Evans * The times between the events vary slightly depending on instruction 269d6b9e17eSBruce Evans * combination and cache misses, etc. Attempt to determine the 270d6b9e17eSBruce Evans * minimum times. These can be subtracted from the profiling times 271d6b9e17eSBruce Evans * without much risk of reducing the profiling times below what they 272d6b9e17eSBruce Evans * would be when profiling is not configured. Abbreviate: 273d6b9e17eSBruce Evans * ab = minimum time between MC1 and MC3 274e3043798SPedro F. Giffuni * a = minimum time between MC1 and MC2 275d6b9e17eSBruce Evans * b = minimum time between MC2 and MC3 276d6b9e17eSBruce Evans * cd = minimum time between ME1 and ME3 277d6b9e17eSBruce Evans * c = minimum time between ME1 and ME2 278d6b9e17eSBruce Evans * d = minimum time between ME2 and ME3. 279d6b9e17eSBruce Evans * These satisfy the relations: 280d6b9e17eSBruce Evans * ab <= mcount_overhead (just measured) 281d6b9e17eSBruce Evans * a + b <= ab 282d6b9e17eSBruce Evans * cd <= mexitcount_overhead (just measured) 283d6b9e17eSBruce Evans * c + d <= cd 284d6b9e17eSBruce Evans * a + d <= nullfunc_loop_profiled_time (just measured) 285d6b9e17eSBruce Evans * a >= 0, b >= 0, c >= 0, d >= 0. 286d6b9e17eSBruce Evans * Assume that ab and cd are equal to the minimums. 287d6b9e17eSBruce Evans */ 288d6b9e17eSBruce Evans p->cputime_overhead = CALIB_DOSCALE(cputime_overhead); 289d6b9e17eSBruce Evans p->mcount_overhead = CALIB_DOSCALE(mcount_overhead - cputime_overhead); 290d6b9e17eSBruce Evans p->mexitcount_overhead = CALIB_DOSCALE(mexitcount_overhead 291d6b9e17eSBruce Evans - cputime_overhead); 292d6b9e17eSBruce Evans nullfunc_loop_overhead = nullfunc_loop_profiled_time - empty_loop_time; 293d6b9e17eSBruce Evans p->mexitcount_post_overhead = CALIB_DOSCALE((mcount_overhead 294d6b9e17eSBruce Evans - nullfunc_loop_overhead) 295d6b9e17eSBruce Evans / 4); 296d6b9e17eSBruce Evans p->mexitcount_pre_overhead = p->mexitcount_overhead 297d6b9e17eSBruce Evans + p->cputime_overhead 298d6b9e17eSBruce Evans - p->mexitcount_post_overhead; 299d6b9e17eSBruce Evans p->mcount_pre_overhead = CALIB_DOSCALE(nullfunc_loop_overhead) 300d6b9e17eSBruce Evans - p->mexitcount_post_overhead; 301d6b9e17eSBruce Evans p->mcount_post_overhead = p->mcount_overhead 302d6b9e17eSBruce Evans + p->cputime_overhead 303d6b9e17eSBruce Evans - p->mcount_pre_overhead; 304d6b9e17eSBruce Evans printf( 305d6b9e17eSBruce Evans "Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d nsec\n", 306d6b9e17eSBruce Evans c2n(p->cputime_overhead, p->profrate), 307d6b9e17eSBruce Evans c2n(p->mcount_overhead, p->profrate), 308d6b9e17eSBruce Evans c2n(p->mcount_pre_overhead, p->profrate), 309d6b9e17eSBruce Evans c2n(p->mcount_post_overhead, p->profrate), 310d6b9e17eSBruce Evans c2n(p->cputime_overhead, p->profrate), 311d6b9e17eSBruce Evans c2n(p->mexitcount_overhead, p->profrate), 312d6b9e17eSBruce Evans c2n(p->mexitcount_pre_overhead, p->profrate), 313d6b9e17eSBruce Evans c2n(p->mexitcount_post_overhead, p->profrate)); 314d6b9e17eSBruce Evans printf( 315d6b9e17eSBruce Evans "Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d cycles\n", 316d6b9e17eSBruce Evans p->cputime_overhead, p->mcount_overhead, 317d6b9e17eSBruce Evans p->mcount_pre_overhead, p->mcount_post_overhead, 318d6b9e17eSBruce Evans p->cputime_overhead, p->mexitcount_overhead, 319d6b9e17eSBruce Evans p->mexitcount_pre_overhead, p->mexitcount_post_overhead); 320912e6037SBruce Evans #endif /* GUPROF */ 321df8bae1dSRodney W. Grimes } 322df8bae1dSRodney W. Grimes 323df8bae1dSRodney W. Grimes /* 324df8bae1dSRodney W. Grimes * Return kernel profiling information. 325df8bae1dSRodney W. Grimes */ 3264b2af45fSPoul-Henning Kamp static int 32782d9ae4eSPoul-Henning Kamp sysctl_kern_prof(SYSCTL_HANDLER_ARGS) 328df8bae1dSRodney W. Grimes { 3294b2af45fSPoul-Henning Kamp int *name = (int *) arg1; 3304b2af45fSPoul-Henning Kamp u_int namelen = arg2; 331df8bae1dSRodney W. Grimes struct gmonparam *gp = &_gmonparam; 332df8bae1dSRodney W. Grimes int error; 333912e6037SBruce Evans int state; 334df8bae1dSRodney W. Grimes 335df8bae1dSRodney W. Grimes /* all sysctl names at this level are terminal */ 336df8bae1dSRodney W. Grimes if (namelen != 1) 337df8bae1dSRodney W. Grimes return (ENOTDIR); /* overloaded */ 338df8bae1dSRodney W. Grimes 339df8bae1dSRodney W. Grimes switch (name[0]) { 340df8bae1dSRodney W. Grimes case GPROF_STATE: 341912e6037SBruce Evans state = gp->state; 342912e6037SBruce Evans error = sysctl_handle_int(oidp, &state, 0, req); 343df8bae1dSRodney W. Grimes if (error) 344df8bae1dSRodney W. Grimes return (error); 345912e6037SBruce Evans if (!req->newptr) 346912e6037SBruce Evans return (0); 347912e6037SBruce Evans if (state == GMON_PROF_OFF) { 348d6b9e17eSBruce Evans gp->state = state; 349a282253aSJulian Elischer PROC_LOCK(&proc0); 350df8bae1dSRodney W. Grimes stopprofclock(&proc0); 351a282253aSJulian Elischer PROC_UNLOCK(&proc0); 352d6b9e17eSBruce Evans stopguprof(gp); 353912e6037SBruce Evans } else if (state == GMON_PROF_ON) { 354d6b9e17eSBruce Evans gp->state = GMON_PROF_OFF; 355d6b9e17eSBruce Evans stopguprof(gp); 356912e6037SBruce Evans gp->profrate = profhz; 3579752f794SJohn Baldwin PROC_LOCK(&proc0); 358df8bae1dSRodney W. Grimes startprofclock(&proc0); 3599752f794SJohn Baldwin PROC_UNLOCK(&proc0); 360d6b9e17eSBruce Evans gp->state = state; 361912e6037SBruce Evans #ifdef GUPROF 362912e6037SBruce Evans } else if (state == GMON_PROF_HIRES) { 363d6b9e17eSBruce Evans gp->state = GMON_PROF_OFF; 364a282253aSJulian Elischer PROC_LOCK(&proc0); 365912e6037SBruce Evans stopprofclock(&proc0); 366a282253aSJulian Elischer PROC_UNLOCK(&proc0); 367d6b9e17eSBruce Evans startguprof(gp); 368912e6037SBruce Evans gp->state = state; 369912e6037SBruce Evans #endif 370912e6037SBruce Evans } else if (state != gp->state) 371912e6037SBruce Evans return (EINVAL); 372df8bae1dSRodney W. Grimes return (0); 373df8bae1dSRodney W. Grimes case GPROF_COUNT: 3744b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, 3754b2af45fSPoul-Henning Kamp gp->kcount, gp->kcountsize, req)); 376df8bae1dSRodney W. Grimes case GPROF_FROMS: 3774b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, 3784b2af45fSPoul-Henning Kamp gp->froms, gp->fromssize, req)); 379df8bae1dSRodney W. Grimes case GPROF_TOS: 3804b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, 3814b2af45fSPoul-Henning Kamp gp->tos, gp->tossize, req)); 382df8bae1dSRodney W. Grimes case GPROF_GMONPARAM: 3834b2af45fSPoul-Henning Kamp return (sysctl_handle_opaque(oidp, gp, sizeof *gp, req)); 384df8bae1dSRodney W. Grimes default: 385df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 386df8bae1dSRodney W. Grimes } 387df8bae1dSRodney W. Grimes /* NOTREACHED */ 388df8bae1dSRodney W. Grimes } 3894b2af45fSPoul-Henning Kamp 3906472ac3dSEd Schouten static SYSCTL_NODE(_kern, KERN_PROF, prof, CTLFLAG_RW, sysctl_kern_prof, ""); 391df8bae1dSRodney W. Grimes #endif /* GPROF */ 392df8bae1dSRodney W. Grimes 393df8bae1dSRodney W. Grimes /* 394df8bae1dSRodney W. Grimes * Profiling system call. 395df8bae1dSRodney W. Grimes * 396df8bae1dSRodney W. Grimes * The scale factor is a fixed point number with 16 bits of fraction, so that 397df8bae1dSRodney W. Grimes * 1.0 is represented as 0x10000. A scale factor of 0 turns off profiling. 398df8bae1dSRodney W. Grimes */ 399d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 400df8bae1dSRodney W. Grimes struct profil_args { 401df8bae1dSRodney W. Grimes caddr_t samples; 402134e06feSBruce Evans size_t size; 403134e06feSBruce Evans size_t offset; 404df8bae1dSRodney W. Grimes u_int scale; 405df8bae1dSRodney W. Grimes }; 406d2d3e875SBruce Evans #endif 407df8bae1dSRodney W. Grimes /* ARGSUSED */ 40826f9a767SRodney W. Grimes int 4098451d0ddSKip Macy sys_profil(struct thread *td, struct profil_args *uap) 410df8bae1dSRodney W. Grimes { 411a282253aSJulian Elischer struct uprof *upp; 4129752f794SJohn Baldwin struct proc *p; 413df8bae1dSRodney W. Grimes 4149752f794SJohn Baldwin if (uap->scale > (1 << 16)) 4159752f794SJohn Baldwin return (EINVAL); 4166f1e8c18SMatthew Dillon 4179752f794SJohn Baldwin p = td->td_proc; 418df8bae1dSRodney W. Grimes if (uap->scale == 0) { 419a3a70178SJohn Baldwin PROC_LOCK(p); 420a3a70178SJohn Baldwin stopprofclock(p); 421a3a70178SJohn Baldwin PROC_UNLOCK(p); 4229752f794SJohn Baldwin return (0); 423df8bae1dSRodney W. Grimes } 424a3a70178SJohn Baldwin PROC_LOCK(p); 425b40ce416SJulian Elischer upp = &td->td_proc->p_stats->p_prof; 4265c7bebf9SKonstantin Belousov PROC_PROFLOCK(p); 427df8bae1dSRodney W. Grimes upp->pr_off = uap->offset; 428df8bae1dSRodney W. Grimes upp->pr_scale = uap->scale; 429df8bae1dSRodney W. Grimes upp->pr_base = uap->samples; 430df8bae1dSRodney W. Grimes upp->pr_size = uap->size; 4315c7bebf9SKonstantin Belousov PROC_PROFUNLOCK(p); 4329752f794SJohn Baldwin startprofclock(p); 4339752f794SJohn Baldwin PROC_UNLOCK(p); 434df8bae1dSRodney W. Grimes 4359752f794SJohn Baldwin return (0); 436df8bae1dSRodney W. Grimes } 437df8bae1dSRodney W. Grimes 438df8bae1dSRodney W. Grimes /* 439df8bae1dSRodney W. Grimes * Scale is a fixed-point number with the binary point 16 bits 440df8bae1dSRodney W. Grimes * into the value, and is <= 1.0. pc is at most 32 bits, so the 441df8bae1dSRodney W. Grimes * intermediate result is at most 48 bits. 442df8bae1dSRodney W. Grimes */ 443df8bae1dSRodney W. Grimes #define PC_TO_INDEX(pc, prof) \ 444df8bae1dSRodney W. Grimes ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \ 445df8bae1dSRodney W. Grimes (u_quad_t)((prof)->pr_scale)) >> 16) & ~1) 446df8bae1dSRodney W. Grimes 447df8bae1dSRodney W. Grimes /* 448df8bae1dSRodney W. Grimes * Collect user-level profiling statistics; called on a profiling tick, 449df8bae1dSRodney W. Grimes * when a process is running in user-mode. This routine may be called 450df8bae1dSRodney W. Grimes * from an interrupt context. We try to update the user profiling buffers 451df8bae1dSRodney W. Grimes * cheaply with fuswintr() and suswintr(). If that fails, we revert to 452df8bae1dSRodney W. Grimes * an AST that will vector us to trap() with a context in which copyin 453df8bae1dSRodney W. Grimes * and copyout will work. Trap will then call addupc_task(). 454df8bae1dSRodney W. Grimes * 455df8bae1dSRodney W. Grimes * Note that we may (rarely) not get around to the AST soon enough, and 456df8bae1dSRodney W. Grimes * lose profile ticks when the next tick overwrites this one, but in this 457df8bae1dSRodney W. Grimes * case the system is overloaded and the profile is probably already 458df8bae1dSRodney W. Grimes * inaccurate. 459df8bae1dSRodney W. Grimes */ 460df8bae1dSRodney W. Grimes void 461cb49fcd1SJohn Baldwin addupc_intr(struct thread *td, uintfptr_t pc, u_int ticks) 462df8bae1dSRodney W. Grimes { 463a282253aSJulian Elischer struct uprof *prof; 464a282253aSJulian Elischer caddr_t addr; 465a282253aSJulian Elischer u_int i; 466a282253aSJulian Elischer int v; 467df8bae1dSRodney W. Grimes 468df8bae1dSRodney W. Grimes if (ticks == 0) 469df8bae1dSRodney W. Grimes return; 4704a338afdSJulian Elischer prof = &td->td_proc->p_stats->p_prof; 4715c7bebf9SKonstantin Belousov PROC_PROFLOCK(td->td_proc); 472df8bae1dSRodney W. Grimes if (pc < prof->pr_off || 473a3a70178SJohn Baldwin (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) { 4745c7bebf9SKonstantin Belousov PROC_PROFUNLOCK(td->td_proc); 475df8bae1dSRodney W. Grimes return; /* out of range; ignore */ 476a3a70178SJohn Baldwin } 477df8bae1dSRodney W. Grimes 478df8bae1dSRodney W. Grimes addr = prof->pr_base + i; 4795c7bebf9SKonstantin Belousov PROC_PROFUNLOCK(td->td_proc); 480df8bae1dSRodney W. Grimes if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + ticks) == -1) { 48152eb8464SJohn Baldwin td->td_profil_addr = pc; 48252eb8464SJohn Baldwin td->td_profil_ticks = ticks; 48352eb8464SJohn Baldwin td->td_pflags |= TDP_OWEUPC; 484982d11f8SJeff Roberson thread_lock(td); 48552eb8464SJohn Baldwin td->td_flags |= TDF_ASTPENDING; 486982d11f8SJeff Roberson thread_unlock(td); 487df8bae1dSRodney W. Grimes } 488df8bae1dSRodney W. Grimes } 489df8bae1dSRodney W. Grimes 490df8bae1dSRodney W. Grimes /* 491df8bae1dSRodney W. Grimes * Much like before, but we can afford to take faults here. If the 492df8bae1dSRodney W. Grimes * update fails, we simply turn off profiling. 493df8bae1dSRodney W. Grimes */ 494037d027cSBruce Evans void 495cb49fcd1SJohn Baldwin addupc_task(struct thread *td, uintfptr_t pc, u_int ticks) 496df8bae1dSRodney W. Grimes { 4974a338afdSJulian Elischer struct proc *p = td->td_proc; 498a282253aSJulian Elischer struct uprof *prof; 499a282253aSJulian Elischer caddr_t addr; 500a282253aSJulian Elischer u_int i; 501df8bae1dSRodney W. Grimes u_short v; 502a282253aSJulian Elischer int stop = 0; 503df8bae1dSRodney W. Grimes 50448fd1f38SJohn Baldwin if (ticks == 0) 505df8bae1dSRodney W. Grimes return; 506df8bae1dSRodney W. Grimes 507a282253aSJulian Elischer PROC_LOCK(p); 5089752f794SJohn Baldwin if (!(p->p_flag & P_PROFIL)) { 509a282253aSJulian Elischer PROC_UNLOCK(p); 510a282253aSJulian Elischer return; 511a282253aSJulian Elischer } 512a282253aSJulian Elischer p->p_profthreads++; 513df8bae1dSRodney W. Grimes prof = &p->p_stats->p_prof; 5145c7bebf9SKonstantin Belousov PROC_PROFLOCK(p); 515df8bae1dSRodney W. Grimes if (pc < prof->pr_off || 516a282253aSJulian Elischer (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) { 5175c7bebf9SKonstantin Belousov PROC_PROFUNLOCK(p); 518a282253aSJulian Elischer goto out; 519a282253aSJulian Elischer } 520df8bae1dSRodney W. Grimes 521df8bae1dSRodney W. Grimes addr = prof->pr_base + i; 5225c7bebf9SKonstantin Belousov PROC_PROFUNLOCK(p); 523a3a70178SJohn Baldwin PROC_UNLOCK(p); 52401609114SAlfred Perlstein if (copyin(addr, &v, sizeof(v)) == 0) { 525df8bae1dSRodney W. Grimes v += ticks; 526a3a70178SJohn Baldwin if (copyout(&v, addr, sizeof(v)) == 0) { 527a3a70178SJohn Baldwin PROC_LOCK(p); 528a282253aSJulian Elischer goto out; 529df8bae1dSRodney W. Grimes } 530a3a70178SJohn Baldwin } 531a282253aSJulian Elischer stop = 1; 532a3a70178SJohn Baldwin PROC_LOCK(p); 533a282253aSJulian Elischer 534a282253aSJulian Elischer out: 535a282253aSJulian Elischer if (--p->p_profthreads == 0) { 5369752f794SJohn Baldwin if (p->p_flag & P_STOPPROF) { 537a282253aSJulian Elischer wakeup(&p->p_profthreads); 5380436fcb8SKonstantin Belousov p->p_flag &= ~P_STOPPROF; 539a282253aSJulian Elischer stop = 0; 540a282253aSJulian Elischer } 541a282253aSJulian Elischer } 542a282253aSJulian Elischer if (stop) 543df8bae1dSRodney W. Grimes stopprofclock(p); 544a282253aSJulian Elischer PROC_UNLOCK(p); 545df8bae1dSRodney W. Grimes } 546