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