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