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 * $Id$ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/proc.h> 41 #include <sys/user.h> 42 #include <machine/cpu.h> 43 44 #ifdef GPROF 45 #include <sys/malloc.h> 46 #include <sys/gmon.h> 47 48 /* 49 * Froms is actually a bunch of unsigned shorts indexing tos 50 */ 51 struct gmonparam _gmonparam = { GMON_PROF_OFF }; 52 53 extern char etext[]; 54 55 kmstartup() 56 { 57 char *cp; 58 struct gmonparam *p = &_gmonparam; 59 /* 60 * Round lowpc and highpc to multiples of the density we're using 61 * so the rest of the scaling (here and in gprof) stays in ints. 62 */ 63 p->lowpc = ROUNDDOWN(KERNBASE, HISTFRACTION * sizeof(HISTCOUNTER)); 64 p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER)); 65 p->textsize = p->highpc - p->lowpc; 66 printf("Profiling kernel, textsize=%d [%x..%x]\n", 67 p->textsize, p->lowpc, p->highpc); 68 p->kcountsize = p->textsize / HISTFRACTION; 69 p->hashfraction = HASHFRACTION; 70 p->fromssize = p->textsize / HASHFRACTION; 71 p->tolimit = p->textsize * ARCDENSITY / 100; 72 if (p->tolimit < MINARCS) 73 p->tolimit = MINARCS; 74 else if (p->tolimit > MAXARCS) 75 p->tolimit = MAXARCS; 76 p->tossize = p->tolimit * sizeof(struct tostruct); 77 cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize, 78 M_GPROF, M_NOWAIT); 79 if (cp == 0) { 80 printf("No memory for profiling.\n"); 81 return; 82 } 83 bzero(cp, p->kcountsize + p->tossize + p->fromssize); 84 p->tos = (struct tostruct *)cp; 85 cp += p->tossize; 86 p->kcount = (u_short *)cp; 87 cp += p->kcountsize; 88 p->froms = (u_short *)cp; 89 } 90 91 /* 92 * Return kernel profiling information. 93 */ 94 sysctl_doprof(name, namelen, oldp, oldlenp, newp, newlen, p) 95 int *name; 96 u_int namelen; 97 void *oldp; 98 size_t *oldlenp; 99 void *newp; 100 size_t newlen; 101 { 102 struct gmonparam *gp = &_gmonparam; 103 int error; 104 105 /* all sysctl names at this level are terminal */ 106 if (namelen != 1) 107 return (ENOTDIR); /* overloaded */ 108 109 switch (name[0]) { 110 case GPROF_STATE: 111 error = sysctl_int(oldp, oldlenp, newp, newlen, &gp->state); 112 if (error) 113 return (error); 114 if (gp->state == GMON_PROF_OFF) 115 stopprofclock(&proc0); 116 else 117 startprofclock(&proc0); 118 return (0); 119 case GPROF_COUNT: 120 return (sysctl_struct(oldp, oldlenp, newp, newlen, 121 gp->kcount, gp->kcountsize)); 122 case GPROF_FROMS: 123 return (sysctl_struct(oldp, oldlenp, newp, newlen, 124 gp->froms, gp->fromssize)); 125 case GPROF_TOS: 126 return (sysctl_struct(oldp, oldlenp, newp, newlen, 127 gp->tos, gp->tossize)); 128 case GPROF_GMONPARAM: 129 return (sysctl_rdstruct(oldp, oldlenp, newp, gp, sizeof *gp)); 130 default: 131 return (EOPNOTSUPP); 132 } 133 /* NOTREACHED */ 134 } 135 #endif /* GPROF */ 136 137 /* 138 * Profiling system call. 139 * 140 * The scale factor is a fixed point number with 16 bits of fraction, so that 141 * 1.0 is represented as 0x10000. A scale factor of 0 turns off profiling. 142 */ 143 struct profil_args { 144 caddr_t samples; 145 u_int size; 146 u_int offset; 147 u_int scale; 148 }; 149 /* ARGSUSED */ 150 int 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