ANSIfy syscall args
[unix-history] / usr / src / sys / kern / subr_prof.c
CommitLineData
80c60f62 1/*-
0880b18e 2 * Copyright (c) 1982, 1986 Regents of the University of California.
2b75fa20 3 * All rights reserved.
da7c5cc6 4 *
2b75fa20
KM
5 * %sccs.include.redist.c%
6 *
9a0bcbda 7 * @(#)subr_prof.c 7.16 (Berkeley) %G%
da7c5cc6 8 */
08621963 9
80c60f62
CT
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
3a3e0b91 17#ifdef GPROF
80c60f62
CT
18#include <sys/malloc.h>
19#include <sys/gmon.h>
3a3e0b91
SL
20
21/*
22 * Froms is actually a bunch of unsigned shorts indexing tos
23 */
80c60f62
CT
24struct gmonparam _gmonparam = { GMON_PROF_OFF };
25
3a3e0b91 26u_short *kcount;
80c60f62 27extern char etext[];
3a3e0b91
SL
28
29kmstartup()
30{
80c60f62
CT
31 char *cp;
32 int fsize, tsize, ksize;
33 struct gmonparam *p = &_gmonparam;
08621963 34 /*
fb1db32c
MK
35 * Round lowpc and highpc to multiples of the density we're using
36 * so the rest of the scaling (here and in gprof) stays in ints.
08621963 37 */
80c60f62
CT
38 p->lowpc = ROUNDDOWN(KERNBASE, HISTFRACTION * sizeof(HISTCOUNTER));
39 p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER));
40 p->textsize = p->highpc - p->lowpc;
80c60f62
CT
41 printf("Profiling kernel, textsize=%d [%x..%x]\n",
42 p->textsize, p->lowpc, p->highpc);
43 ksize = p->textsize / HISTFRACTION;
44 fsize = p->textsize / HASHFRACTION;
45 p->tolimit = p->textsize * ARCDENSITY / 100;
46 if (p->tolimit < MINARCS)
47 p->tolimit = MINARCS;
48 else if (p->tolimit > MAXARCS)
49 p->tolimit = MAXARCS;
50 tsize = p->tolimit * sizeof(struct tostruct);
51 cp = (char *)malloc(ksize + fsize + tsize, M_GPROF, M_NOWAIT);
52 if (cp == 0) {
53 printf("No memory for profiling.\n");
3a3e0b91
SL
54 return;
55 }
80c60f62
CT
56 bzero(cp, ksize + tsize + fsize);
57 p->tos = (struct tostruct *)cp;
58 cp += tsize;
59 kcount = (u_short *)cp;
60 cp += ksize;
61 p->froms = (u_short *)cp;
4ce99dbf 62 startprofclock(&proc0);
3a3e0b91 63}
80c60f62 64#endif
3a3e0b91
SL
65
66/*
80c60f62
CT
67 * Profiling system call.
68 *
69 * The scale factor is a fixed point number with 16 bits of fraction, so that
70 * 1.0 is represented as 0x10000. A scale factor of 0 turns off profiling.
3a3e0b91 71 */
80c60f62
CT
72/* ARGSUSED */
73profil(p, uap, retval)
74 struct proc *p;
75 register struct args {
76 caddr_t buf;
77 u_int bufsize;
78 u_int offset;
79 u_int scale;
80 } *uap;
81 int *retval;
3a3e0b91 82{
80c60f62
CT
83 register struct uprof *upp;
84 int s;
3a3e0b91 85
80c60f62
CT
86 if (uap->scale > (1 << 16))
87 return (EINVAL);
88 if (uap->scale == 0) {
89 stopprofclock(p);
90 return (0);
3a3e0b91 91 }
80c60f62
CT
92 upp = &p->p_stats->p_prof;
93 s = splstatclock(); /* block profile interrupts while changing state */
94 upp->pr_base = uap->buf;
95 upp->pr_size = uap->bufsize;
96 upp->pr_off = uap->offset;
97 upp->pr_scale = uap->scale;
98 startprofclock(p);
99 splx(s);
100 return (0);
101}
102
103/*
104 * Scale is a fixed-point number with the binary point 16 bits
105 * into the value, and is <= 1.0. pc is at most 32 bits, so the
106 * intermediate result is at most 48 bits.
107 */
108#define PC_TO_INDEX(pc, prof) \
109 ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
110 (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
111
112/*
113 * Collect user-level profiling statistics; called on a profiling tick,
114 * when a process is running in user-mode. This routine may be called
115 * from an interrupt context. We try to update the user profiling buffers
116 * cheaply with fuswintr() and suswintr(). If that fails, we revert to
117 * an AST that will vector us to trap() with a context in which copyin
118 * and copyout will work. Trap will then call addupc_task().
119 *
120 * Note that we may (rarely) not get around to the AST soon enough, and
121 * lose profile ticks when the next tick overwrites this one, but in this
122 * case the system is overloaded and the profile is probably already
123 * inaccurate.
124 */
125void
126addupc_intr(p, pc, ticks)
127 register struct proc *p;
128 register u_long pc;
129 u_int ticks;
130{
131 register struct uprof *prof;
132 register caddr_t addr;
133 register u_int i;
134 register int v;
135
136 if (ticks == 0)
137 return;
138 prof = &p->p_stats->p_prof;
139 if (pc < prof->pr_off ||
140 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
141 return; /* out of range; ignore */
142
143 addr = prof->pr_base + i;
144 if ((v = fuswintr(addr)) == -1 || suswintr(addr, v + ticks) == -1) {
145 prof->pr_addr = pc;
146 prof->pr_ticks = ticks;
147 need_proftick(p);
08621963 148 }
80c60f62
CT
149}
150
151/*
152 * Much like before, but we can afford to take faults here. If the
153 * update fails, we simply turn off profiling.
154 */
155void
156addupc_task(p, pc, ticks)
157 register struct proc *p;
158 register u_long pc;
159 u_int ticks;
160{
161 register struct uprof *prof;
162 register caddr_t addr;
163 register u_int i;
164 u_short v;
165
166 /* testing SPROFIL may be unnecessary, but is certainly safe */
167 if ((p->p_flag & SPROFIL) == 0 || ticks == 0)
168 return;
08621963 169
80c60f62
CT
170 prof = &p->p_stats->p_prof;
171 if (pc < prof->pr_off ||
172 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
173 return;
174
175 addr = prof->pr_base + i;
176 if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) {
177 v += ticks;
178 if (copyout((caddr_t)&v, addr, sizeof(v)) == 0)
179 return;
3a3e0b91 180 }
80c60f62 181 stopprofclock(p);
3a3e0b91 182}