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 /* 46 * Profiling system call. 47 * 48 * The scale factor is a fixed point number with 16 bits of fraction, so that 49 * 1.0 is represented as 0x10000. A scale factor of 0 turns off profiling. 50 * 51 * MPALMOSTSAFE 52 */ 53 int 54 sys_profil(struct profil_args *uap) 55 { 56 struct proc *p = curproc; 57 struct uprof *upp; 58 59 if (uap->scale > (1 << 16)) 60 return (EINVAL); 61 lwkt_gettoken(&p->p_token); 62 if (uap->scale == 0) { 63 stopprofclock(p); 64 } else { 65 upp = &p->p_prof; 66 67 /* Block profile interrupts while changing state. */ 68 crit_enter(); 69 upp->pr_off = uap->offset; 70 upp->pr_scale = uap->scale; 71 upp->pr_base = uap->samples; 72 upp->pr_size = uap->size; 73 startprofclock(p); 74 crit_exit(); 75 } 76 lwkt_reltoken(&p->p_token); 77 78 return (0); 79 } 80 81 /* 82 * Scale is a fixed-point number with the binary point 16 bits 83 * into the value, and is <= 1.0. pc is at most 32 bits, so the 84 * intermediate result is at most 48 bits. 85 */ 86 #define PC_TO_INDEX(pc, prof) \ 87 ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \ 88 (u_quad_t)((prof)->pr_scale)) >> 16) & ~1) 89 90 /* 91 * Collect user-level profiling statistics; called on a profiling tick, 92 * when a process is running in user-mode. This routine may be called 93 * from an interrupt context. 94 * 95 * Note that we may (rarely) not get around to the AST soon enough, and 96 * lose profile ticks when the next tick overwrites this one, but in this 97 * case the system is overloaded and the profile is probably already 98 * inaccurate. 99 */ 100 void 101 addupc_intr(struct proc *p, u_long pc, u_int ticks) 102 { 103 struct uprof *prof; 104 u_int i; 105 106 if (ticks == 0) 107 return; 108 prof = &p->p_prof; 109 if (pc < prof->pr_off || 110 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) 111 return; /* out of range; ignore */ 112 113 prof->pr_addr = pc; 114 prof->pr_ticks = ticks; 115 need_proftick(); 116 } 117 118 /* 119 * Much like before, but we can afford to take faults here. If the 120 * update fails, we simply turn off profiling. 121 */ 122 void 123 addupc_task(struct proc *p, u_long pc, u_int ticks) 124 { 125 struct uprof *prof; 126 caddr_t addr; 127 u_int i; 128 u_short v; 129 130 /* Testing P_PROFIL may be unnecessary, but is certainly safe. */ 131 if ((p->p_flags & P_PROFIL) == 0 || ticks == 0) 132 return; 133 134 prof = &p->p_prof; 135 if (pc < prof->pr_off || 136 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) 137 return; 138 139 addr = prof->pr_base + i; 140 if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) { 141 v += ticks; 142 if (copyout((caddr_t)&v, addr, sizeof(v)) == 0) 143 return; 144 } 145 stopprofclock(p); 146 } 147