1 /*- 2 * Copyright (c) 1982, 1986 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)subr_prof.c 7.19 (Berkeley) 04/29/93 8 */ 9 10 #include <sys/param.h> 11 #include <sys/systm.h> 12 #include <sys/kernel.h> 13 #include <sys/proc.h> 14 #include <sys/user.h> 15 #include <machine/cpu.h> 16 17 #ifdef GPROF 18 #include <sys/malloc.h> 19 #include <sys/gmon.h> 20 21 /* 22 * Froms is actually a bunch of unsigned shorts indexing tos 23 */ 24 struct gmonparam _gmonparam = { GMON_PROF_OFF }; 25 26 extern char etext[]; 27 28 kmstartup() 29 { 30 char *cp; 31 struct gmonparam *p = &_gmonparam; 32 /* 33 * Round lowpc and highpc to multiples of the density we're using 34 * so the rest of the scaling (here and in gprof) stays in ints. 35 */ 36 p->lowpc = ROUNDDOWN(KERNBASE, HISTFRACTION * sizeof(HISTCOUNTER)); 37 p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER)); 38 p->textsize = p->highpc - p->lowpc; 39 printf("Profiling kernel, textsize=%d [%x..%x]\n", 40 p->textsize, p->lowpc, p->highpc); 41 p->kcountsize = p->textsize / HISTFRACTION; 42 p->hashfraction = HASHFRACTION; 43 p->fromssize = p->textsize / HASHFRACTION; 44 p->tolimit = p->textsize * ARCDENSITY / 100; 45 if (p->tolimit < MINARCS) 46 p->tolimit = MINARCS; 47 else if (p->tolimit > MAXARCS) 48 p->tolimit = MAXARCS; 49 p->tossize = p->tolimit * sizeof(struct tostruct); 50 cp = (char *)malloc(p->kcountsize + p->fromssize + p->tossize, 51 M_GPROF, M_NOWAIT); 52 if (cp == 0) { 53 printf("No memory for profiling.\n"); 54 return; 55 } 56 bzero(cp, p->kcountsize + p->tossize + p->fromssize); 57 p->tos = (struct tostruct *)cp; 58 cp += p->tossize; 59 p->kcount = (u_short *)cp; 60 cp += p->kcountsize; 61 p->froms = (u_short *)cp; 62 startprofclock(&proc0); 63 } 64 65 /* 66 * Return kernel profiling information. 67 */ 68 sysctl_doprof(name, namelen, oldp, oldlenp, newp, newlen, p) 69 int *name; 70 u_int namelen; 71 void *oldp; 72 size_t *oldlenp; 73 void *newp; 74 size_t newlen; 75 { 76 struct gmonparam *gp = &_gmonparam; 77 78 /* all sysctl names at this level are terminal */ 79 if (namelen != 1) 80 return (ENOTDIR); /* overloaded */ 81 82 switch (name[0]) { 83 case GPROF_STATE: 84 return (sysctl_int(oldp, oldlenp, newp, newlen, &gp->state)); 85 case GPROF_COUNT: 86 return (sysctl_struct(oldp, oldlenp, newp, newlen, 87 gp->kcount, gp->kcountsize)); 88 case GPROF_FROMS: 89 return (sysctl_struct(oldp, oldlenp, newp, newlen, 90 gp->froms, gp->fromssize)); 91 case GPROF_TOS: 92 return (sysctl_struct(oldp, oldlenp, newp, newlen, 93 gp->tos, gp->tossize)); 94 case GPROF_GMONPARAM: 95 return (sysctl_rdstruct(oldp, oldlenp, newp, gp, sizeof *gp)); 96 default: 97 return (EOPNOTSUPP); 98 } 99 /* NOTREACHED */ 100 } 101 #endif /* GPROF */ 102 103 /* 104 * Profiling system call. 105 * 106 * The scale factor is a fixed point number with 16 bits of fraction, so that 107 * 1.0 is represented as 0x10000. A scale factor of 0 turns off profiling. 108 */ 109 struct profil_args { 110 caddr_t buf; 111 u_int bufsize; 112 u_int offset; 113 u_int scale; 114 }; 115 /* ARGSUSED */ 116 profil(p, uap, retval) 117 struct proc *p; 118 register struct profil_args *uap; 119 int *retval; 120 { 121 register struct uprof *upp; 122 int s; 123 124 if (uap->scale > (1 << 16)) 125 return (EINVAL); 126 if (uap->scale == 0) { 127 stopprofclock(p); 128 return (0); 129 } 130 upp = &p->p_stats->p_prof; 131 s = splstatclock(); /* block profile interrupts while changing state */ 132 upp->pr_base = uap->buf; 133 upp->pr_size = uap->bufsize; 134 upp->pr_off = uap->offset; 135 upp->pr_scale = uap->scale; 136 startprofclock(p); 137 splx(s); 138 return (0); 139 } 140 141 /* 142 * Scale is a fixed-point number with the binary point 16 bits 143 * into the value, and is <= 1.0. pc is at most 32 bits, so the 144 * intermediate result is at most 48 bits. 145 */ 146 #define PC_TO_INDEX(pc, prof) \ 147 ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \ 148 (u_quad_t)((prof)->pr_scale)) >> 16) & ~1) 149 150 /* 151 * Collect user-level profiling statistics; called on a profiling tick, 152 * when a process is running in user-mode. This routine may be called 153 * from an interrupt context. We try to update the user profiling buffers 154 * cheaply with fuswintr() and suswintr(). If that fails, we revert to 155 * an AST that will vector us to trap() with a context in which copyin 156 * and copyout will work. Trap will then call addupc_task(). 157 * 158 * Note that we may (rarely) not get around to the AST soon enough, and 159 * lose profile ticks when the next tick overwrites this one, but in this 160 * case the system is overloaded and the profile is probably already 161 * inaccurate. 162 */ 163 void 164 addupc_intr(p, pc, ticks) 165 register struct proc *p; 166 register u_long pc; 167 u_int ticks; 168 { 169 register struct uprof *prof; 170 register caddr_t addr; 171 register u_int i; 172 register int v; 173 174 if (ticks == 0) 175 return; 176 prof = &p->p_stats->p_prof; 177 if (pc < prof->pr_off || 178 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) 179 return; /* out of range; ignore */ 180 181 addr = prof->pr_base + i; 182 if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + ticks) == -1) { 183 prof->pr_addr = pc; 184 prof->pr_ticks = ticks; 185 need_proftick(p); 186 } 187 } 188 189 /* 190 * Much like before, but we can afford to take faults here. If the 191 * update fails, we simply turn off profiling. 192 */ 193 void 194 addupc_task(p, pc, ticks) 195 register struct proc *p; 196 register u_long pc; 197 u_int ticks; 198 { 199 register struct uprof *prof; 200 register caddr_t addr; 201 register u_int i; 202 u_short v; 203 204 /* testing SPROFIL may be unnecessary, but is certainly safe */ 205 if ((p->p_flag & SPROFIL) == 0 || ticks == 0) 206 return; 207 208 prof = &p->p_stats->p_prof; 209 if (pc < prof->pr_off || 210 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) 211 return; 212 213 addr = prof->pr_base + i; 214 if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) { 215 v += ticks; 216 if (copyout((caddr_t)&v, addr, sizeof(v)) == 0) 217 return; 218 } 219 stopprofclock(p); 220 } 221