1*54790Storek /*- 229100Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 341966Smckusick * All rights reserved. 423380Smckusick * 541966Smckusick * %sccs.include.redist.c% 641966Smckusick * 7*54790Storek * @(#)subr_prof.c 7.15 (Berkeley) 07/08/92 823380Smckusick */ 97332Ssam 10*54790Storek #include <sys/param.h> 11*54790Storek #include <sys/systm.h> 12*54790Storek #include <sys/kernel.h> 13*54790Storek #include <sys/proc.h> 14*54790Storek #include <sys/user.h> 15*54790Storek #include <machine/cpu.h> 16*54790Storek 177332Ssam #ifdef GPROF 18*54790Storek #include <sys/malloc.h> 19*54790Storek #include <sys/gmon.h> 207332Ssam 217332Ssam /* 227332Ssam * Froms is actually a bunch of unsigned shorts indexing tos 237332Ssam */ 24*54790Storek struct gmonparam _gmonparam = { GMON_PROF_OFF }; 25*54790Storek 267332Ssam u_short *kcount; 27*54790Storek extern char etext[]; 287332Ssam 297332Ssam kmstartup() 307332Ssam { 31*54790Storek char *cp; 32*54790Storek int fsize, tsize, ksize; 33*54790Storek struct gmonparam *p = &_gmonparam; 3410292Smckusick /* 3529946Skarels * Round lowpc and highpc to multiples of the density we're using 3629946Skarels * so the rest of the scaling (here and in gprof) stays in ints. 3710292Smckusick */ 38*54790Storek p->lowpc = ROUNDDOWN(KERNBASE, HISTFRACTION * sizeof(HISTCOUNTER)); 39*54790Storek p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER)); 40*54790Storek p->textsize = p->highpc - p->lowpc; 41*54790Storek p->profrate = profhz; 42*54790Storek printf("Profiling kernel, textsize=%d [%x..%x]\n", 43*54790Storek p->textsize, p->lowpc, p->highpc); 44*54790Storek ksize = p->textsize / HISTFRACTION; 45*54790Storek fsize = p->textsize / HASHFRACTION; 46*54790Storek p->tolimit = p->textsize * ARCDENSITY / 100; 47*54790Storek if (p->tolimit < MINARCS) 48*54790Storek p->tolimit = MINARCS; 49*54790Storek else if (p->tolimit > MAXARCS) 50*54790Storek p->tolimit = MAXARCS; 51*54790Storek tsize = p->tolimit * sizeof(struct tostruct); 52*54790Storek cp = (char *)malloc(ksize + fsize + tsize, M_GPROF, M_NOWAIT); 53*54790Storek if (cp == 0) { 54*54790Storek printf("No memory for profiling.\n"); 557332Ssam return; 567332Ssam } 57*54790Storek bzero(cp, ksize + tsize + fsize); 58*54790Storek p->tos = (struct tostruct *)cp; 59*54790Storek cp += tsize; 60*54790Storek kcount = (u_short *)cp; 61*54790Storek cp += ksize; 62*54790Storek p->froms = (u_short *)cp; 6354137Smckusick startprofclock(&proc0); 647332Ssam } 65*54790Storek #endif 667332Ssam 677332Ssam /* 68*54790Storek * Profiling system call. 69*54790Storek * 70*54790Storek * The scale factor is a fixed point number with 16 bits of fraction, so that 71*54790Storek * 1.0 is represented as 0x10000. A scale factor of 0 turns off profiling. 727332Ssam */ 73*54790Storek /* ARGSUSED */ 74*54790Storek profil(p, uap, retval) 75*54790Storek struct proc *p; 76*54790Storek register struct args { 77*54790Storek caddr_t buf; 78*54790Storek u_int bufsize; 79*54790Storek u_int offset; 80*54790Storek u_int scale; 81*54790Storek } *uap; 82*54790Storek int *retval; 837332Ssam { 84*54790Storek register struct uprof *upp; 85*54790Storek int s; 867332Ssam 87*54790Storek if (uap->scale > (1 << 16)) 88*54790Storek return (EINVAL); 89*54790Storek if (uap->scale == 0) { 90*54790Storek stopprofclock(p); 91*54790Storek return (0); 927332Ssam } 93*54790Storek upp = &p->p_stats->p_prof; 94*54790Storek s = splstatclock(); /* block profile interrupts while changing state */ 95*54790Storek upp->pr_base = uap->buf; 96*54790Storek upp->pr_size = uap->bufsize; 97*54790Storek upp->pr_off = uap->offset; 98*54790Storek upp->pr_scale = uap->scale; 99*54790Storek startprofclock(p); 100*54790Storek splx(s); 101*54790Storek return (0); 102*54790Storek } 103*54790Storek 104*54790Storek /* 105*54790Storek * Scale is a fixed-point number with the binary point 16 bits 106*54790Storek * into the value, and is <= 1.0. pc is at most 32 bits, so the 107*54790Storek * intermediate result is at most 48 bits. 108*54790Storek */ 109*54790Storek #define PC_TO_INDEX(pc, prof) \ 110*54790Storek ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \ 111*54790Storek (u_quad_t)((prof)->pr_scale)) >> 16) & ~1) 112*54790Storek 113*54790Storek /* 114*54790Storek * Collect user-level profiling statistics; called on a profiling tick, 115*54790Storek * when a process is running in user-mode. This routine may be called 116*54790Storek * from an interrupt context. We try to update the user profiling buffers 117*54790Storek * cheaply with fuswintr() and suswintr(). If that fails, we revert to 118*54790Storek * an AST that will vector us to trap() with a context in which copyin 119*54790Storek * and copyout will work. Trap will then call addupc_task(). 120*54790Storek * 121*54790Storek * Note that we may (rarely) not get around to the AST soon enough, and 122*54790Storek * lose profile ticks when the next tick overwrites this one, but in this 123*54790Storek * case the system is overloaded and the profile is probably already 124*54790Storek * inaccurate. 125*54790Storek */ 126*54790Storek void 127*54790Storek addupc_intr(p, pc, ticks) 128*54790Storek register struct proc *p; 129*54790Storek register u_long pc; 130*54790Storek u_int ticks; 131*54790Storek { 132*54790Storek register struct uprof *prof; 133*54790Storek register caddr_t addr; 134*54790Storek register u_int i; 135*54790Storek register int v; 136*54790Storek 137*54790Storek if (ticks == 0) 138*54790Storek return; 139*54790Storek prof = &p->p_stats->p_prof; 140*54790Storek if (pc < prof->pr_off || 141*54790Storek (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) 142*54790Storek return; /* out of range; ignore */ 143*54790Storek 144*54790Storek addr = prof->pr_base + i; 145*54790Storek if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + ticks) == -1) { 146*54790Storek prof->pr_addr = pc; 147*54790Storek prof->pr_ticks = ticks; 148*54790Storek need_proftick(p); 14910292Smckusick } 150*54790Storek } 15110292Smckusick 152*54790Storek /* 153*54790Storek * Much like before, but we can afford to take faults here. If the 154*54790Storek * update fails, we simply turn off profiling. 155*54790Storek */ 156*54790Storek void 157*54790Storek addupc_task(p, pc, ticks) 158*54790Storek register struct proc *p; 159*54790Storek register u_long pc; 160*54790Storek u_int ticks; 161*54790Storek { 162*54790Storek register struct uprof *prof; 163*54790Storek register caddr_t addr; 164*54790Storek register u_int i; 165*54790Storek u_short v; 166*54790Storek 167*54790Storek /* testing SPROFIL may be unnecessary, but is certainly safe */ 168*54790Storek if ((p->p_flag & SPROFIL) == 0 || ticks == 0) 169*54790Storek return; 170*54790Storek 171*54790Storek prof = &p->p_stats->p_prof; 172*54790Storek if (pc < prof->pr_off || 173*54790Storek (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) 174*54790Storek return; 175*54790Storek 176*54790Storek addr = prof->pr_base + i; 177*54790Storek if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) { 178*54790Storek v += ticks; 179*54790Storek if (copyout((caddr_t)&v, addr, sizeof(v)) == 0) 180*54790Storek return; 1817332Ssam } 182*54790Storek stopprofclock(p); 1837332Ssam } 184