1 /* $OpenBSD: subr_prof.c,v 1.20 2012/03/23 15:51:26 guenther Exp $ */ 2 /* $NetBSD: subr_prof.c,v 1.12 1996/04/22 01:38:50 christos Exp $ */ 3 4 /*- 5 * Copyright (c) 1982, 1986, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)subr_prof.c 8.3 (Berkeley) 9/23/93 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/proc.h> 39 #include <sys/resourcevar.h> 40 #include <sys/mount.h> 41 #include <sys/sysctl.h> 42 #include <sys/syscallargs.h> 43 44 #include <machine/cpu.h> 45 46 #ifdef GPROF 47 #include <sys/malloc.h> 48 #include <sys/gmon.h> 49 #include <uvm/uvm_extern.h> 50 51 /* 52 * Froms is actually a bunch of unsigned shorts indexing tos 53 */ 54 struct gmonparam _gmonparam = { GMON_PROF_OFF }; 55 56 extern char etext[]; 57 58 59 void 60 kmstartup(void) 61 { 62 char *cp; 63 struct gmonparam *p = &_gmonparam; 64 int size; 65 66 /* 67 * Round lowpc and highpc to multiples of the density we're using 68 * so the rest of the scaling (here and in gprof) stays in ints. 69 */ 70 p->lowpc = ROUNDDOWN(KERNBASE, HISTFRACTION * sizeof(HISTCOUNTER)); 71 p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER)); 72 p->textsize = p->highpc - p->lowpc; 73 printf("Profiling kernel, textsize=%ld [%lx..%lx]\n", 74 p->textsize, p->lowpc, p->highpc); 75 p->kcountsize = p->textsize / HISTFRACTION; 76 p->hashfraction = HASHFRACTION; 77 p->fromssize = p->textsize / HASHFRACTION; 78 p->tolimit = p->textsize * ARCDENSITY / 100; 79 if (p->tolimit < MINARCS) 80 p->tolimit = MINARCS; 81 else if (p->tolimit > MAXARCS) 82 p->tolimit = MAXARCS; 83 p->tossize = p->tolimit * sizeof(struct tostruct); 84 size = p->kcountsize + p->fromssize + p->tossize; 85 cp = (char *)uvm_km_zalloc(kernel_map, round_page(size)); 86 if (cp == 0) { 87 printf("No memory for profiling.\n"); 88 return; 89 } 90 p->tos = (struct tostruct *)cp; 91 cp += p->tossize; 92 p->kcount = (u_short *)cp; 93 cp += p->kcountsize; 94 p->froms = (u_short *)cp; 95 } 96 97 /* 98 * Return kernel profiling information. 99 */ 100 int 101 sysctl_doprof(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 102 size_t newlen) 103 { 104 struct gmonparam *gp = &_gmonparam; 105 int error; 106 107 /* all sysctl names at this level are terminal */ 108 if (namelen != 1) 109 return (ENOTDIR); /* overloaded */ 110 111 switch (name[0]) { 112 case GPROF_STATE: 113 error = sysctl_int(oldp, oldlenp, newp, newlen, &gp->state); 114 if (error) 115 return (error); 116 if (gp->state == GMON_PROF_OFF) 117 stopprofclock(&proc0); 118 else 119 startprofclock(&proc0); 120 return (0); 121 case GPROF_COUNT: 122 return (sysctl_struct(oldp, oldlenp, newp, newlen, 123 gp->kcount, gp->kcountsize)); 124 case GPROF_FROMS: 125 return (sysctl_struct(oldp, oldlenp, newp, newlen, 126 gp->froms, gp->fromssize)); 127 case GPROF_TOS: 128 return (sysctl_struct(oldp, oldlenp, newp, newlen, 129 gp->tos, gp->tossize)); 130 case GPROF_GMONPARAM: 131 return (sysctl_rdstruct(oldp, oldlenp, newp, gp, sizeof *gp)); 132 default: 133 return (EOPNOTSUPP); 134 } 135 /* NOTREACHED */ 136 } 137 #endif /* GPROF */ 138 139 /* 140 * Profiling system call. 141 * 142 * The scale factor is a fixed point number with 16 bits of fraction, so that 143 * 1.0 is represented as 0x10000. A scale factor of 0 turns off profiling. 144 */ 145 /* ARGSUSED */ 146 int 147 sys_profil(struct proc *p, void *v, register_t *retval) 148 { 149 struct sys_profil_args /* { 150 syscallarg(caddr_t) samples; 151 syscallarg(size_t) size; 152 syscallarg(u_long) offset; 153 syscallarg(u_int) scale; 154 } */ *uap = v; 155 struct uprof *upp; 156 int s; 157 158 if (SCARG(uap, scale) > (1 << 16)) 159 return (EINVAL); 160 if (SCARG(uap, scale) == 0) { 161 stopprofclock(p); 162 return (0); 163 } 164 upp = &p->p_p->ps_prof; 165 166 /* Block profile interrupts while changing state. */ 167 s = splstatclock(); 168 upp->pr_off = SCARG(uap, offset); 169 upp->pr_scale = SCARG(uap, scale); 170 upp->pr_base = (caddr_t)SCARG(uap, samples); 171 upp->pr_size = SCARG(uap, size); 172 startprofclock(p); 173 splx(s); 174 175 return (0); 176 } 177 178 /* 179 * Scale is a fixed-point number with the binary point 16 bits 180 * into the value, and is <= 1.0. pc is at most 32 bits, so the 181 * intermediate result is at most 48 bits. 182 */ 183 #define PC_TO_INDEX(pc, prof) \ 184 ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \ 185 (u_quad_t)((prof)->pr_scale)) >> 16) & ~1) 186 187 /* 188 * Collect user-level profiling statistics; called on a profiling tick, 189 * when a process is running in user-mode. This routine may be called 190 * from an interrupt context. Schedule an AST that will vector us to 191 * trap() with a context in which copyin and copyout will work. 192 * Trap will then call addupc_task(). 193 */ 194 void 195 addupc_intr(struct proc *p, u_long pc) 196 { 197 struct uprof *prof; 198 199 prof = &p->p_p->ps_prof; 200 if (pc < prof->pr_off || PC_TO_INDEX(pc, prof) >= prof->pr_size) 201 return; /* out of range; ignore */ 202 203 p->p_prof_addr = pc; 204 p->p_prof_ticks++; 205 atomic_setbits_int(&p->p_flag, P_OWEUPC); 206 need_proftick(p); 207 } 208 209 210 /* 211 * Much like before, but we can afford to take faults here. If the 212 * update fails, we simply turn off profiling. 213 */ 214 void 215 addupc_task(struct proc *p, u_long pc, u_int nticks) 216 { 217 struct uprof *prof; 218 caddr_t addr; 219 u_int i; 220 u_short v; 221 222 /* Testing P_PROFIL may be unnecessary, but is certainly safe. */ 223 if ((p->p_flag & P_PROFIL) == 0 || nticks == 0) 224 return; 225 226 prof = &p->p_p->ps_prof; 227 if (pc < prof->pr_off || 228 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) 229 return; 230 231 addr = prof->pr_base + i; 232 if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) { 233 v += nticks; 234 if (copyout((caddr_t)&v, addr, sizeof(v)) == 0) 235 return; 236 } 237 stopprofclock(p); 238 } 239