1 /*- 2 * Copyright (c) 1982, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)subr_prof.c 8.3 (Berkeley) 9/23/93 30 * $FreeBSD: src/sys/kern/subr_prof.c,v 1.32.2.2 2000/08/03 00:09:32 ps Exp $ 31 */ 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/sysproto.h> 36 #include <sys/kernel.h> 37 #include <sys/proc.h> 38 #include <sys/resourcevar.h> 39 #include <sys/sysctl.h> 40 41 #include <sys/thread2.h> 42 43 #include <machine/cpu.h> 44 45 #ifdef GPROF 46 #include <sys/malloc.h> 47 #include <sys/gmon.h> 48 #undef MCOUNT 49 50 static MALLOC_DEFINE(M_GPROF, "gprof", "kernel profiling buffer"); 51 52 static void kmstartup (void *); 53 SYSINIT(kmem, SI_SUB_KPROF, SI_ORDER_FIRST, kmstartup, NULL); 54 55 struct gmonparam _gmonparam = { GMON_PROF_OFF }; 56 57 #ifdef GUPROF 58 #include <machine/asmacros.h> 59 60 void 61 nullfunc_loop_profiled(void) 62 { 63 int i; 64 65 for (i = 0; i < CALIB_SCALE; i++) 66 nullfunc_profiled(); 67 } 68 69 #define nullfunc_loop_profiled_end nullfunc_profiled /* XXX */ 70 71 void 72 nullfunc_profiled(void) 73 { 74 } 75 #endif /* GUPROF */ 76 77 static void 78 kmstartup(void *dummy) 79 { 80 char *cp; 81 struct gmonparam *p = &_gmonparam; 82 #ifdef GUPROF 83 int cputime_overhead; 84 int empty_loop_time; 85 int i; 86 int mcount_overhead; 87 int mexitcount_overhead; 88 int nullfunc_loop_overhead; 89 int nullfunc_loop_profiled_time; 90 uintfptr_t tmp_addr; 91 #endif 92 93 /* 94 * Round lowpc and highpc to multiples of the density we're using 95 * so the rest of the scaling (here and in gprof) stays in ints. 96 */ 97 p->lowpc = ROUNDDOWN((u_long)btext, HISTFRACTION * sizeof(HISTCOUNTER)); 98 p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER)); 99 p->textsize = p->highpc - p->lowpc; 100 kprintf("Profiling kernel, textsize=%lu [%jx..%jx]\n", 101 p->textsize, (uintmax_t)p->lowpc, (uintmax_t)p->highpc); 102 p->kcountsize = p->textsize / HISTFRACTION; 103 p->hashfraction = HASHFRACTION; 104 p->fromssize = p->textsize / HASHFRACTION; 105 p->tolimit = p->textsize * ARCDENSITY / 100; 106 if (p->tolimit < MINARCS) 107 p->tolimit = MINARCS; 108 else if (p->tolimit > MAXARCS) 109 p->tolimit = MAXARCS; 110 p->tossize = p->tolimit * sizeof(struct tostruct); 111 cp = (char *)kmalloc(p->kcountsize + p->fromssize + p->tossize, 112 M_GPROF, M_NOWAIT | M_ZERO); 113 if (cp == NULL) { 114 kprintf("No memory for profiling.\n"); 115 return; 116 } 117 p->tos = (struct tostruct *)cp; 118 cp += p->tossize; 119 p->kcount = (HISTCOUNTER *)cp; 120 cp += p->kcountsize; 121 p->froms = (u_short *)cp; 122 123 #ifdef GUPROF 124 /* Initialize pointers to overhead counters. */ 125 p->cputime_count = &KCOUNT(p, PC_TO_I(p, cputime)); 126 p->mcount_count = &KCOUNT(p, PC_TO_I(p, mcount)); 127 p->mexitcount_count = &KCOUNT(p, PC_TO_I(p, mexitcount)); 128 129 /* 130 * Disable interrupts to avoid interference while we calibrate 131 * things. 132 */ 133 cpu_disable_intr(); 134 135 /* 136 * Determine overheads. 137 * XXX this needs to be repeated for each useful timer/counter. 138 */ 139 cputime_overhead = 0; 140 startguprof(p); 141 for (i = 0; i < CALIB_SCALE; i++) 142 cputime_overhead += cputime(); 143 144 empty_loop(); 145 startguprof(p); 146 empty_loop(); 147 empty_loop_time = cputime(); 148 149 nullfunc_loop_profiled(); 150 151 /* 152 * Start profiling. There won't be any normal function calls since 153 * interrupts are disabled, but we will call the profiling routines 154 * directly to determine their overheads. 155 */ 156 p->state = GMON_PROF_HIRES; 157 158 startguprof(p); 159 nullfunc_loop_profiled(); 160 161 startguprof(p); 162 for (i = 0; i < CALIB_SCALE; i++) 163 #if defined(__i386__) && __GNUC__ >= 2 164 __asm("pushl %0; call __mcount; popl %%ecx" 165 : 166 : "i" (profil) 167 : "ax", "bx", "cx", "dx", "memory"); 168 #else 169 #error 170 #endif 171 mcount_overhead = KCOUNT(p, PC_TO_I(p, profil)); 172 173 startguprof(p); 174 for (i = 0; i < CALIB_SCALE; i++) 175 #if defined(__i386__) && __GNUC__ >= 2 176 __asm("call " __XSTRING(HIDENAME(mexitcount)) "; 1:" 177 : : : "ax", "bx", "cx", "dx", "memory"); 178 __asm("movl $1b,%0" : "=rm" (tmp_addr)); 179 #else 180 #error 181 #endif 182 mexitcount_overhead = KCOUNT(p, PC_TO_I(p, tmp_addr)); 183 184 p->state = GMON_PROF_OFF; 185 stopguprof(p); 186 187 cpu_enable_intr(); 188 189 nullfunc_loop_profiled_time = 0; 190 for (tmp_addr = (uintfptr_t)nullfunc_loop_profiled; 191 tmp_addr < (uintfptr_t)nullfunc_loop_profiled_end; 192 tmp_addr += HISTFRACTION * sizeof(HISTCOUNTER)) 193 nullfunc_loop_profiled_time += KCOUNT(p, PC_TO_I(p, tmp_addr)); 194 #define CALIB_DOSCALE(count) (((count) + CALIB_SCALE / 3) / CALIB_SCALE) 195 #define c2n(count, freq) ((int)((count) * 1000000000LL / freq)) 196 kprintf("cputime %d, empty_loop %d, nullfunc_loop_profiled %d, mcount %d, mexitcount %d\n", 197 CALIB_DOSCALE(c2n(cputime_overhead, p->profrate)), 198 CALIB_DOSCALE(c2n(empty_loop_time, p->profrate)), 199 CALIB_DOSCALE(c2n(nullfunc_loop_profiled_time, p->profrate)), 200 CALIB_DOSCALE(c2n(mcount_overhead, p->profrate)), 201 CALIB_DOSCALE(c2n(mexitcount_overhead, p->profrate))); 202 cputime_overhead -= empty_loop_time; 203 mcount_overhead -= empty_loop_time; 204 mexitcount_overhead -= empty_loop_time; 205 206 /*- 207 * Profiling overheads are determined by the times between the 208 * following events: 209 * MC1: mcount() is called 210 * MC2: cputime() (called from mcount()) latches the timer 211 * MC3: mcount() completes 212 * ME1: mexitcount() is called 213 * ME2: cputime() (called from mexitcount()) latches the timer 214 * ME3: mexitcount() completes. 215 * The times between the events vary slightly depending on instruction 216 * combination and cache misses, etc. Attempt to determine the 217 * minimum times. These can be subtracted from the profiling times 218 * without much risk of reducing the profiling times below what they 219 * would be when profiling is not configured. Abbreviate: 220 * ab = minimum time between MC1 and MC3 221 * a = minumum time between MC1 and MC2 222 * b = minimum time between MC2 and MC3 223 * cd = minimum time between ME1 and ME3 224 * c = minimum time between ME1 and ME2 225 * d = minimum time between ME2 and ME3. 226 * These satisfy the relations: 227 * ab <= mcount_overhead (just measured) 228 * a + b <= ab 229 * cd <= mexitcount_overhead (just measured) 230 * c + d <= cd 231 * a + d <= nullfunc_loop_profiled_time (just measured) 232 * a >= 0, b >= 0, c >= 0, d >= 0. 233 * Assume that ab and cd are equal to the minimums. 234 */ 235 p->cputime_overhead = CALIB_DOSCALE(cputime_overhead); 236 p->mcount_overhead = CALIB_DOSCALE(mcount_overhead - cputime_overhead); 237 p->mexitcount_overhead = CALIB_DOSCALE(mexitcount_overhead 238 - cputime_overhead); 239 nullfunc_loop_overhead = nullfunc_loop_profiled_time - empty_loop_time; 240 p->mexitcount_post_overhead = CALIB_DOSCALE((mcount_overhead 241 - nullfunc_loop_overhead) 242 / 4); 243 p->mexitcount_pre_overhead = p->mexitcount_overhead 244 + p->cputime_overhead 245 - p->mexitcount_post_overhead; 246 p->mcount_pre_overhead = CALIB_DOSCALE(nullfunc_loop_overhead) 247 - p->mexitcount_post_overhead; 248 p->mcount_post_overhead = p->mcount_overhead 249 + p->cputime_overhead 250 - p->mcount_pre_overhead; 251 kprintf( 252 "Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d nsec\n", 253 c2n(p->cputime_overhead, p->profrate), 254 c2n(p->mcount_overhead, p->profrate), 255 c2n(p->mcount_pre_overhead, p->profrate), 256 c2n(p->mcount_post_overhead, p->profrate), 257 c2n(p->cputime_overhead, p->profrate), 258 c2n(p->mexitcount_overhead, p->profrate), 259 c2n(p->mexitcount_pre_overhead, p->profrate), 260 c2n(p->mexitcount_post_overhead, p->profrate)); 261 kprintf( 262 "Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d cycles\n", 263 p->cputime_overhead, p->mcount_overhead, 264 p->mcount_pre_overhead, p->mcount_post_overhead, 265 p->cputime_overhead, p->mexitcount_overhead, 266 p->mexitcount_pre_overhead, p->mexitcount_post_overhead); 267 #endif /* GUPROF */ 268 } 269 270 /* 271 * Return kernel profiling information. 272 */ 273 static int 274 sysctl_kern_prof(SYSCTL_HANDLER_ARGS) 275 { 276 int *name = (int *) arg1; 277 u_int namelen = arg2; 278 struct gmonparam *gp = &_gmonparam; 279 int error; 280 int state; 281 282 /* all sysctl names at this level are terminal */ 283 if (namelen != 1) 284 return (ENOTDIR); /* overloaded */ 285 286 switch (name[0]) { 287 case GPROF_STATE: 288 state = gp->state; 289 error = sysctl_handle_int(oidp, &state, 0, req); 290 if (error) 291 return (error); 292 if (!req->newptr) 293 return (0); 294 lwkt_gettoken(&proc0.p_token); 295 if (state == GMON_PROF_OFF) { 296 gp->state = state; 297 stopprofclock(&proc0); 298 stopguprof(gp); 299 } else if (state == GMON_PROF_ON) { 300 gp->state = GMON_PROF_OFF; 301 stopguprof(gp); 302 gp->profrate = profhz; 303 startprofclock(&proc0); 304 gp->state = state; 305 #ifdef GUPROF 306 } else if (state == GMON_PROF_HIRES) { 307 gp->state = GMON_PROF_OFF; 308 stopprofclock(&proc0); 309 startguprof(gp); 310 gp->state = state; 311 #endif 312 } else if (state != gp->state) { 313 error = EINVAL; 314 } 315 lwkt_reltoken(&proc0.p_token); 316 return (error); 317 case GPROF_COUNT: 318 return (sysctl_handle_opaque(oidp, 319 gp->kcount, gp->kcountsize, req)); 320 case GPROF_FROMS: 321 return (sysctl_handle_opaque(oidp, 322 gp->froms, gp->fromssize, req)); 323 case GPROF_TOS: 324 return (sysctl_handle_opaque(oidp, 325 gp->tos, gp->tossize, req)); 326 case GPROF_GMONPARAM: 327 return (sysctl_handle_opaque(oidp, gp, sizeof *gp, req)); 328 default: 329 return (EOPNOTSUPP); 330 } 331 /* NOTREACHED */ 332 } 333 334 SYSCTL_NODE(_kern, KERN_PROF, prof, CTLFLAG_RW, sysctl_kern_prof, ""); 335 #endif /* GPROF */ 336 337 /* 338 * Profiling system call. 339 * 340 * The scale factor is a fixed point number with 16 bits of fraction, so that 341 * 1.0 is represented as 0x10000. A scale factor of 0 turns off profiling. 342 * 343 * MPALMOSTSAFE 344 */ 345 int 346 sys_profil(struct profil_args *uap) 347 { 348 struct proc *p = curproc; 349 struct uprof *upp; 350 351 if (uap->scale > (1 << 16)) 352 return (EINVAL); 353 lwkt_gettoken(&p->p_token); 354 if (uap->scale == 0) { 355 stopprofclock(p); 356 } else { 357 upp = &p->p_prof; 358 359 /* Block profile interrupts while changing state. */ 360 crit_enter(); 361 upp->pr_off = uap->offset; 362 upp->pr_scale = uap->scale; 363 upp->pr_base = uap->samples; 364 upp->pr_size = uap->size; 365 startprofclock(p); 366 crit_exit(); 367 } 368 lwkt_reltoken(&p->p_token); 369 370 return (0); 371 } 372 373 /* 374 * Scale is a fixed-point number with the binary point 16 bits 375 * into the value, and is <= 1.0. pc is at most 32 bits, so the 376 * intermediate result is at most 48 bits. 377 */ 378 #define PC_TO_INDEX(pc, prof) \ 379 ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \ 380 (u_quad_t)((prof)->pr_scale)) >> 16) & ~1) 381 382 /* 383 * Collect user-level profiling statistics; called on a profiling tick, 384 * when a process is running in user-mode. This routine may be called 385 * from an interrupt context. 386 * 387 * Note that we may (rarely) not get around to the AST soon enough, and 388 * lose profile ticks when the next tick overwrites this one, but in this 389 * case the system is overloaded and the profile is probably already 390 * inaccurate. 391 */ 392 void 393 addupc_intr(struct proc *p, u_long pc, u_int ticks) 394 { 395 struct uprof *prof; 396 u_int i; 397 398 if (ticks == 0) 399 return; 400 prof = &p->p_prof; 401 if (pc < prof->pr_off || 402 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) 403 return; /* out of range; ignore */ 404 405 prof->pr_addr = pc; 406 prof->pr_ticks = ticks; 407 need_proftick(); 408 } 409 410 /* 411 * Much like before, but we can afford to take faults here. If the 412 * update fails, we simply turn off profiling. 413 */ 414 void 415 addupc_task(struct proc *p, u_long pc, u_int ticks) 416 { 417 struct uprof *prof; 418 caddr_t addr; 419 u_int i; 420 u_short v; 421 422 /* Testing P_PROFIL may be unnecessary, but is certainly safe. */ 423 if ((p->p_flags & P_PROFIL) == 0 || ticks == 0) 424 return; 425 426 prof = &p->p_prof; 427 if (pc < prof->pr_off || 428 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) 429 return; 430 431 addr = prof->pr_base + i; 432 if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) { 433 v += ticks; 434 if (copyout((caddr_t)&v, addr, sizeof(v)) == 0) 435 return; 436 } 437 stopprofclock(p); 438 } 439