kernel - Major signal path adjustments to fix races, tsleep race fixes, +more
[dragonfly.git] / sys / kern / subr_prof.c
CommitLineData
984263bc
MD
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 * $FreeBSD: src/sys/kern/subr_prof.c,v 1.32.2.2 2000/08/03 00:09:32 ps Exp $
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/sysproto.h>
40#include <sys/kernel.h>
41#include <sys/proc.h>
42#include <sys/resourcevar.h>
43#include <sys/sysctl.h>
684a93c4 44
e43a034f 45#include <sys/thread2.h>
684a93c4 46#include <sys/mplock2.h>
984263bc 47
984263bc
MD
48#include <machine/cpu.h>
49
50#ifdef GPROF
51#include <sys/malloc.h>
52#include <sys/gmon.h>
53#undef MCOUNT
54
55static MALLOC_DEFINE(M_GPROF, "gprof", "kernel profiling buffer");
56
402ed7e1 57static void kmstartup (void *);
984263bc
MD
58SYSINIT(kmem, SI_SUB_KPROF, SI_ORDER_FIRST, kmstartup, NULL)
59
60struct gmonparam _gmonparam = { GMON_PROF_OFF };
61
62#ifdef GUPROF
63#include <machine/asmacros.h>
64
65void
c972a82f 66nullfunc_loop_profiled(void)
984263bc
MD
67{
68 int i;
69
70 for (i = 0; i < CALIB_SCALE; i++)
71 nullfunc_profiled();
72}
73
74#define nullfunc_loop_profiled_end nullfunc_profiled /* XXX */
75
76void
c972a82f 77nullfunc_profiled(void)
984263bc
MD
78{
79}
80#endif /* GUPROF */
81
82static void
493c516a 83kmstartup(void *dummy)
984263bc
MD
84{
85 char *cp;
86 struct gmonparam *p = &_gmonparam;
87#ifdef GUPROF
88 int cputime_overhead;
89 int empty_loop_time;
90 int i;
91 int mcount_overhead;
92 int mexitcount_overhead;
93 int nullfunc_loop_overhead;
94 int nullfunc_loop_profiled_time;
95 uintfptr_t tmp_addr;
96#endif
97
98 /*
99 * Round lowpc and highpc to multiples of the density we're using
100 * so the rest of the scaling (here and in gprof) stays in ints.
101 */
102 p->lowpc = ROUNDDOWN((u_long)btext, HISTFRACTION * sizeof(HISTCOUNTER));
103 p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER));
104 p->textsize = p->highpc - p->lowpc;
6ea70f76 105 kprintf("Profiling kernel, textsize=%lu [%x..%x]\n",
984263bc
MD
106 p->textsize, p->lowpc, p->highpc);
107 p->kcountsize = p->textsize / HISTFRACTION;
108 p->hashfraction = HASHFRACTION;
109 p->fromssize = p->textsize / HASHFRACTION;
110 p->tolimit = p->textsize * ARCDENSITY / 100;
111 if (p->tolimit < MINARCS)
112 p->tolimit = MINARCS;
113 else if (p->tolimit > MAXARCS)
114 p->tolimit = MAXARCS;
115 p->tossize = p->tolimit * sizeof(struct tostruct);
397c071c 116 cp = (char *)kmalloc(p->kcountsize + p->fromssize + p->tossize,
0c374e73 117 M_GPROF, M_NOWAIT | M_ZERO);
984263bc 118 if (cp == 0) {
6ea70f76 119 kprintf("No memory for profiling.\n");
984263bc
MD
120 return;
121 }
984263bc
MD
122 p->tos = (struct tostruct *)cp;
123 cp += p->tossize;
124 p->kcount = (HISTCOUNTER *)cp;
125 cp += p->kcountsize;
126 p->froms = (u_short *)cp;
127
128#ifdef GUPROF
129 /* Initialize pointers to overhead counters. */
130 p->cputime_count = &KCOUNT(p, PC_TO_I(p, cputime));
131 p->mcount_count = &KCOUNT(p, PC_TO_I(p, mcount));
132 p->mexitcount_count = &KCOUNT(p, PC_TO_I(p, mexitcount));
133
134 /*
135 * Disable interrupts to avoid interference while we calibrate
136 * things.
137 */
eac48440 138 cpu_disable_intr();
984263bc
MD
139
140 /*
141 * Determine overheads.
142 * XXX this needs to be repeated for each useful timer/counter.
143 */
144 cputime_overhead = 0;
145 startguprof(p);
146 for (i = 0; i < CALIB_SCALE; i++)
147 cputime_overhead += cputime();
148
149 empty_loop();
150 startguprof(p);
151 empty_loop();
152 empty_loop_time = cputime();
153
154 nullfunc_loop_profiled();
155
156 /*
157 * Start profiling. There won't be any normal function calls since
158 * interrupts are disabled, but we will call the profiling routines
159 * directly to determine their overheads.
160 */
161 p->state = GMON_PROF_HIRES;
162
163 startguprof(p);
164 nullfunc_loop_profiled();
165
166 startguprof(p);
167 for (i = 0; i < CALIB_SCALE; i++)
168#if defined(__i386__) && __GNUC__ >= 2
169 __asm("pushl %0; call __mcount; popl %%ecx"
170 :
171 : "i" (profil)
172 : "ax", "bx", "cx", "dx", "memory");
173#else
174#error
175#endif
176 mcount_overhead = KCOUNT(p, PC_TO_I(p, profil));
177
178 startguprof(p);
179 for (i = 0; i < CALIB_SCALE; i++)
180#if defined(__i386__) && __GNUC__ >= 2
181 __asm("call " __XSTRING(HIDENAME(mexitcount)) "; 1:"
182 : : : "ax", "bx", "cx", "dx", "memory");
183 __asm("movl $1b,%0" : "=rm" (tmp_addr));
184#else
185#error
186#endif
187 mexitcount_overhead = KCOUNT(p, PC_TO_I(p, tmp_addr));
188
189 p->state = GMON_PROF_OFF;
190 stopguprof(p);
191
eac48440 192 cpu_enable_intr();
984263bc
MD
193
194 nullfunc_loop_profiled_time = 0;
195 for (tmp_addr = (uintfptr_t)nullfunc_loop_profiled;
196 tmp_addr < (uintfptr_t)nullfunc_loop_profiled_end;
197 tmp_addr += HISTFRACTION * sizeof(HISTCOUNTER))
198 nullfunc_loop_profiled_time += KCOUNT(p, PC_TO_I(p, tmp_addr));
199#define CALIB_DOSCALE(count) (((count) + CALIB_SCALE / 3) / CALIB_SCALE)
200#define c2n(count, freq) ((int)((count) * 1000000000LL / freq))
6ea70f76 201 kprintf("cputime %d, empty_loop %d, nullfunc_loop_profiled %d, mcount %d, mexitcount %d\n",
984263bc
MD
202 CALIB_DOSCALE(c2n(cputime_overhead, p->profrate)),
203 CALIB_DOSCALE(c2n(empty_loop_time, p->profrate)),
204 CALIB_DOSCALE(c2n(nullfunc_loop_profiled_time, p->profrate)),
205 CALIB_DOSCALE(c2n(mcount_overhead, p->profrate)),
206 CALIB_DOSCALE(c2n(mexitcount_overhead, p->profrate)));
207 cputime_overhead -= empty_loop_time;
208 mcount_overhead -= empty_loop_time;
209 mexitcount_overhead -= empty_loop_time;
210
211 /*-
212 * Profiling overheads are determined by the times between the
213 * following events:
214 * MC1: mcount() is called
215 * MC2: cputime() (called from mcount()) latches the timer
216 * MC3: mcount() completes
217 * ME1: mexitcount() is called
218 * ME2: cputime() (called from mexitcount()) latches the timer
219 * ME3: mexitcount() completes.
220 * The times between the events vary slightly depending on instruction
221 * combination and cache misses, etc. Attempt to determine the
222 * minimum times. These can be subtracted from the profiling times
223 * without much risk of reducing the profiling times below what they
224 * would be when profiling is not configured. Abbreviate:
225 * ab = minimum time between MC1 and MC3
226 * a = minumum time between MC1 and MC2
227 * b = minimum time between MC2 and MC3
228 * cd = minimum time between ME1 and ME3
229 * c = minimum time between ME1 and ME2
230 * d = minimum time between ME2 and ME3.
231 * These satisfy the relations:
232 * ab <= mcount_overhead (just measured)
233 * a + b <= ab
234 * cd <= mexitcount_overhead (just measured)
235 * c + d <= cd
236 * a + d <= nullfunc_loop_profiled_time (just measured)
237 * a >= 0, b >= 0, c >= 0, d >= 0.
238 * Assume that ab and cd are equal to the minimums.
239 */
240 p->cputime_overhead = CALIB_DOSCALE(cputime_overhead);
241 p->mcount_overhead = CALIB_DOSCALE(mcount_overhead - cputime_overhead);
242 p->mexitcount_overhead = CALIB_DOSCALE(mexitcount_overhead
243 - cputime_overhead);
244 nullfunc_loop_overhead = nullfunc_loop_profiled_time - empty_loop_time;
245 p->mexitcount_post_overhead = CALIB_DOSCALE((mcount_overhead
246 - nullfunc_loop_overhead)
247 / 4);
248 p->mexitcount_pre_overhead = p->mexitcount_overhead
249 + p->cputime_overhead
250 - p->mexitcount_post_overhead;
251 p->mcount_pre_overhead = CALIB_DOSCALE(nullfunc_loop_overhead)
252 - p->mexitcount_post_overhead;
253 p->mcount_post_overhead = p->mcount_overhead
254 + p->cputime_overhead
255 - p->mcount_pre_overhead;
6ea70f76 256 kprintf(
984263bc
MD
257"Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d nsec\n",
258 c2n(p->cputime_overhead, p->profrate),
259 c2n(p->mcount_overhead, p->profrate),
260 c2n(p->mcount_pre_overhead, p->profrate),
261 c2n(p->mcount_post_overhead, p->profrate),
262 c2n(p->cputime_overhead, p->profrate),
263 c2n(p->mexitcount_overhead, p->profrate),
264 c2n(p->mexitcount_pre_overhead, p->profrate),
265 c2n(p->mexitcount_post_overhead, p->profrate));
6ea70f76 266 kprintf(
984263bc
MD
267"Profiling overheads: mcount: %d+%d, %d+%d; mexitcount: %d+%d, %d+%d cycles\n",
268 p->cputime_overhead, p->mcount_overhead,
269 p->mcount_pre_overhead, p->mcount_post_overhead,
270 p->cputime_overhead, p->mexitcount_overhead,
271 p->mexitcount_pre_overhead, p->mexitcount_post_overhead);
272#endif /* GUPROF */
273}
274
275/*
276 * Return kernel profiling information.
277 */
278static int
279sysctl_kern_prof(SYSCTL_HANDLER_ARGS)
280{
281 int *name = (int *) arg1;
282 u_int namelen = arg2;
283 struct gmonparam *gp = &_gmonparam;
284 int error;
285 int state;
286
287 /* all sysctl names at this level are terminal */
288 if (namelen != 1)
289 return (ENOTDIR); /* overloaded */
290
291 switch (name[0]) {
292 case GPROF_STATE:
293 state = gp->state;
294 error = sysctl_handle_int(oidp, &state, 0, req);
295 if (error)
296 return (error);
297 if (!req->newptr)
298 return (0);
616516c8 299 lwkt_gettoken(&proc0.p_token);
984263bc
MD
300 if (state == GMON_PROF_OFF) {
301 gp->state = state;
302 stopprofclock(&proc0);
303 stopguprof(gp);
304 } else if (state == GMON_PROF_ON) {
305 gp->state = GMON_PROF_OFF;
306 stopguprof(gp);
307 gp->profrate = profhz;
308 startprofclock(&proc0);
309 gp->state = state;
310#ifdef GUPROF
311 } else if (state == GMON_PROF_HIRES) {
312 gp->state = GMON_PROF_OFF;
313 stopprofclock(&proc0);
314 startguprof(gp);
315 gp->state = state;
316#endif
616516c8
MD
317 } else if (state != gp->state) {
318 error = EINVAL;
319 }
320 lwkt_reltoken(&proc0.p_token);
321 return (error);
984263bc
MD
322 case GPROF_COUNT:
323 return (sysctl_handle_opaque(oidp,
324 gp->kcount, gp->kcountsize, req));
325 case GPROF_FROMS:
326 return (sysctl_handle_opaque(oidp,
327 gp->froms, gp->fromssize, req));
328 case GPROF_TOS:
329 return (sysctl_handle_opaque(oidp,
330 gp->tos, gp->tossize, req));
331 case GPROF_GMONPARAM:
332 return (sysctl_handle_opaque(oidp, gp, sizeof *gp, req));
333 default:
334 return (EOPNOTSUPP);
335 }
336 /* NOTREACHED */
337}
338
339SYSCTL_NODE(_kern, KERN_PROF, prof, CTLFLAG_RW, sysctl_kern_prof, "");
340#endif /* GPROF */
341
342/*
343 * Profiling system call.
344 *
345 * The scale factor is a fixed point number with 16 bits of fraction, so that
346 * 1.0 is represented as 0x10000. A scale factor of 0 turns off profiling.
3919ced0
MD
347 *
348 * MPALMOSTSAFE
984263bc 349 */
984263bc 350int
753fd850 351sys_profil(struct profil_args *uap)
984263bc 352{
41c20dac
MD
353 struct proc *p = curproc;
354 struct uprof *upp;
984263bc
MD
355
356 if (uap->scale > (1 << 16))
357 return (EINVAL);
3919ced0 358 get_mplock();
984263bc
MD
359 if (uap->scale == 0) {
360 stopprofclock(p);
3919ced0
MD
361 } else {
362 upp = &p->p_prof;
363
364 /* Block profile interrupts while changing state. */
365 crit_enter();
366 upp->pr_off = uap->offset;
367 upp->pr_scale = uap->scale;
368 upp->pr_base = uap->samples;
369 upp->pr_size = uap->size;
370 startprofclock(p);
371 crit_exit();
984263bc 372 }
3919ced0 373 rel_mplock();
984263bc
MD
374 return (0);
375}
376
377/*
378 * Scale is a fixed-point number with the binary point 16 bits
379 * into the value, and is <= 1.0. pc is at most 32 bits, so the
380 * intermediate result is at most 48 bits.
381 */
382#define PC_TO_INDEX(pc, prof) \
383 ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
384 (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
385
386/*
387 * Collect user-level profiling statistics; called on a profiling tick,
388 * when a process is running in user-mode. This routine may be called
389 * from an interrupt context. We try to update the user profiling buffers
390 * cheaply with fuswintr() and suswintr(). If that fails, we revert to
391 * an AST that will vector us to trap() with a context in which copyin
392 * and copyout will work. Trap will then call addupc_task().
393 *
9b662cfd
MD
394 * XXX fuswintr() and suswintr() never worked (always returnde -1), remove
395 * them. It's just a bad idea to try to do this from a hard interrupt.
396 *
984263bc
MD
397 * Note that we may (rarely) not get around to the AST soon enough, and
398 * lose profile ticks when the next tick overwrites this one, but in this
399 * case the system is overloaded and the profile is probably already
400 * inaccurate.
401 */
402void
493c516a 403addupc_intr(struct proc *p, u_long pc, u_int ticks)
984263bc 404{
1fd87d54
RG
405 struct uprof *prof;
406 caddr_t addr;
407 u_int i;
984263bc
MD
408
409 if (ticks == 0)
410 return;
93328593 411 prof = &p->p_prof;
984263bc
MD
412 if (pc < prof->pr_off ||
413 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
414 return; /* out of range; ignore */
415
416 addr = prof->pr_base + i;
9b662cfd
MD
417 prof->pr_addr = pc;
418 prof->pr_ticks = ticks;
419 need_proftick();
984263bc
MD
420}
421
422/*
423 * Much like before, but we can afford to take faults here. If the
424 * update fails, we simply turn off profiling.
425 */
426void
493c516a 427addupc_task(struct proc *p, u_long pc, u_int ticks)
984263bc 428{
1fd87d54
RG
429 struct uprof *prof;
430 caddr_t addr;
431 u_int i;
984263bc
MD
432 u_short v;
433
434 /* Testing P_PROFIL may be unnecessary, but is certainly safe. */
4643740a 435 if ((p->p_flags & P_PROFIL) == 0 || ticks == 0)
984263bc
MD
436 return;
437
93328593 438 prof = &p->p_prof;
984263bc
MD
439 if (pc < prof->pr_off ||
440 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
441 return;
442
443 addr = prof->pr_base + i;
444 if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) {
445 v += ticks;
446 if (copyout((caddr_t)&v, addr, sizeof(v)) == 0)
447 return;
448 }
449 stopprofclock(p);
450}