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