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