1 /* $OpenBSD: subr_prof.c,v 1.13 2003/06/02 23:28:06 millert 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/user.h> 40 #include <sys/mount.h> 41 #include <sys/syscallargs.h> 42 43 #include <machine/cpu.h> 44 45 #ifdef GPROF 46 #include <sys/malloc.h> 47 #include <sys/gmon.h> 48 #include <uvm/uvm_extern.h> 49 50 /* 51 * Froms is actually a bunch of unsigned shorts indexing tos 52 */ 53 struct gmonparam _gmonparam = { GMON_PROF_OFF }; 54 55 extern char etext[]; 56 57 58 void 59 kmstartup() 60 { 61 char *cp; 62 struct gmonparam *p = &_gmonparam; 63 int size; 64 65 /* 66 * Round lowpc and highpc to multiples of the density we're using 67 * so the rest of the scaling (here and in gprof) stays in ints. 68 */ 69 p->lowpc = ROUNDDOWN(KERNBASE, HISTFRACTION * sizeof(HISTCOUNTER)); 70 p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER)); 71 p->textsize = p->highpc - p->lowpc; 72 printf("Profiling kernel, textsize=%ld [%lx..%lx]\n", 73 p->textsize, p->lowpc, p->highpc); 74 p->kcountsize = p->textsize / HISTFRACTION; 75 p->hashfraction = HASHFRACTION; 76 p->fromssize = p->textsize / HASHFRACTION; 77 p->tolimit = p->textsize * ARCDENSITY / 100; 78 if (p->tolimit < MINARCS) 79 p->tolimit = MINARCS; 80 else if (p->tolimit > MAXARCS) 81 p->tolimit = MAXARCS; 82 p->tossize = p->tolimit * sizeof(struct tostruct); 83 size = p->kcountsize + p->fromssize + p->tossize; 84 cp = (char *)uvm_km_zalloc(kernel_map, round_page(size)); 85 if (cp == 0) { 86 printf("No memory for profiling.\n"); 87 return; 88 } 89 p->tos = (struct tostruct *)cp; 90 cp += p->tossize; 91 p->kcount = (u_short *)cp; 92 cp += p->kcountsize; 93 p->froms = (u_short *)cp; 94 } 95 96 /* 97 * Return kernel profiling information. 98 */ 99 int 100 sysctl_doprof(name, namelen, oldp, oldlenp, newp, newlen) 101 int *name; 102 u_int namelen; 103 void *oldp; 104 size_t *oldlenp; 105 void *newp; 106 size_t newlen; 107 { 108 struct gmonparam *gp = &_gmonparam; 109 int error; 110 111 /* all sysctl names at this level are terminal */ 112 if (namelen != 1) 113 return (ENOTDIR); /* overloaded */ 114 115 switch (name[0]) { 116 case GPROF_STATE: 117 error = sysctl_int(oldp, oldlenp, newp, newlen, &gp->state); 118 if (error) 119 return (error); 120 if (gp->state == GMON_PROF_OFF) 121 stopprofclock(&proc0); 122 else 123 startprofclock(&proc0); 124 return (0); 125 case GPROF_COUNT: 126 return (sysctl_struct(oldp, oldlenp, newp, newlen, 127 gp->kcount, gp->kcountsize)); 128 case GPROF_FROMS: 129 return (sysctl_struct(oldp, oldlenp, newp, newlen, 130 gp->froms, gp->fromssize)); 131 case GPROF_TOS: 132 return (sysctl_struct(oldp, oldlenp, newp, newlen, 133 gp->tos, gp->tossize)); 134 case GPROF_GMONPARAM: 135 return (sysctl_rdstruct(oldp, oldlenp, newp, gp, sizeof *gp)); 136 default: 137 return (EOPNOTSUPP); 138 } 139 /* NOTREACHED */ 140 } 141 #endif /* GPROF */ 142 143 /* 144 * Profiling system call. 145 * 146 * The scale factor is a fixed point number with 16 bits of fraction, so that 147 * 1.0 is represented as 0x10000. A scale factor of 0 turns off profiling. 148 */ 149 /* ARGSUSED */ 150 int 151 sys_profil(p, v, retval) 152 struct proc *p; 153 void *v; 154 register_t *retval; 155 { 156 register struct sys_profil_args /* { 157 syscallarg(char *) samples; 158 syscallarg(u_int) size; 159 syscallarg(u_int) offset; 160 syscallarg(u_int) scale; 161 } */ *uap = v; 162 register struct uprof *upp; 163 int s; 164 165 if (SCARG(uap, scale) > (1 << 16)) 166 return (EINVAL); 167 if (SCARG(uap, scale) == 0) { 168 stopprofclock(p); 169 return (0); 170 } 171 upp = &p->p_stats->p_prof; 172 173 /* Block profile interrupts while changing state. */ 174 s = splstatclock(); 175 upp->pr_off = SCARG(uap, offset); 176 upp->pr_scale = SCARG(uap, scale); 177 upp->pr_base = (caddr_t)SCARG(uap, samples); 178 upp->pr_size = SCARG(uap, size); 179 startprofclock(p); 180 splx(s); 181 182 return (0); 183 } 184 185 /* 186 * Scale is a fixed-point number with the binary point 16 bits 187 * into the value, and is <= 1.0. pc is at most 32 bits, so the 188 * intermediate result is at most 48 bits. 189 */ 190 #define PC_TO_INDEX(pc, prof) \ 191 ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \ 192 (u_quad_t)((prof)->pr_scale)) >> 16) & ~1) 193 194 /* 195 * Collect user-level profiling statistics; called on a profiling tick, 196 * when a process is running in user-mode. This routine may be called 197 * from an interrupt context. Schedule an AST that will vector us to 198 * trap() with a context in which copyin and copyout will work. 199 * Trap will then call addupc_task(). 200 */ 201 void 202 addupc_intr(struct proc *p, u_long pc) 203 { 204 struct uprof *prof; 205 206 prof = &p->p_stats->p_prof; 207 if (pc < prof->pr_off || PC_TO_INDEX(pc, prof) >= prof->pr_size) 208 return; /* out of range; ignore */ 209 210 prof->pr_addr = pc; 211 need_proftick(p); 212 } 213 214 215 /* 216 * Much like before, but we can afford to take faults here. If the 217 * update fails, we simply turn off profiling. 218 */ 219 void 220 addupc_task(struct proc *p, u_long pc, u_int nticks) 221 { 222 struct uprof *prof; 223 caddr_t addr; 224 u_int i; 225 u_short v; 226 227 /* Testing P_PROFIL may be unnecessary, but is certainly safe. */ 228 if ((p->p_flag & P_PROFIL) == 0 || nticks == 0) 229 return; 230 231 prof = &p->p_stats->p_prof; 232 if (pc < prof->pr_off || 233 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) 234 return; 235 236 addr = prof->pr_base + i; 237 if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) { 238 v += nticks; 239 if (copyout((caddr_t)&v, addr, sizeof(v)) == 0) 240 return; 241 } 242 stopprofclock(p); 243 } 244