1 /*-
2 * Copyright (c) 1996 Bruce D. Evans.
3 * 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 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #ifdef GUPROF
31 #include "opt_i586_guprof.h"
32 #include "opt_perfmon.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/cpu.h>
38 #include <sys/eventhandler.h>
39 #include <sys/gmon.h>
40 #include <sys/kernel.h>
41 #include <sys/smp.h>
42 #include <sys/sysctl.h>
43
44 #include <machine/clock.h>
45 #include <machine/perfmon.h>
46 #include <machine/timerreg.h>
47
48 #define CPUTIME_CLOCK_UNINITIALIZED 0
49 #define CPUTIME_CLOCK_I8254 1
50 #define CPUTIME_CLOCK_TSC 2
51 #define CPUTIME_CLOCK_I586_PMC 3
52 #define CPUTIME_CLOCK_I8254_SHIFT 7
53
54 int cputime_bias = 1; /* initialize for locality of reference */
55
56 static int cputime_clock = CPUTIME_CLOCK_UNINITIALIZED;
57 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
58 static u_int cputime_clock_pmc_conf = I586_PMC_GUPROF;
59 static int cputime_clock_pmc_init;
60 static struct gmonparam saved_gmp;
61 #endif
62 #if defined(I586_CPU) || defined(I686_CPU)
63 static int cputime_prof_active;
64 #endif
65 #endif /* GUPROF */
66
67 #ifdef __GNUCLIKE_ASM
68 __asm(" \n\
69 GM_STATE = 0 \n\
70 GMON_PROF_OFF = 3 \n\
71 \n\
72 .text \n\
73 .p2align 4,0x90 \n\
74 .globl __mcount \n\
75 .type __mcount,@function \n\
76 __mcount: \n\
77 # \n\
78 # Check that we are profiling. Do it early for speed. \n\
79 # \n\
80 cmpl $GMON_PROF_OFF,_gmonparam+GM_STATE \n\
81 je .mcount_exit \n\
82 # \n\
83 # __mcount is the same as [.]mcount except the caller \n\
84 # hasn't changed the stack except to call here, so the \n\
85 # caller's raddr is above our raddr. \n\
86 # \n\
87 movl 4(%esp),%edx \n\
88 jmp .got_frompc \n\
89 \n\
90 .p2align 4,0x90 \n\
91 .globl .mcount \n\
92 .mcount: \n\
93 .globl __cyg_profile_func_enter \n\
94 __cyg_profile_func_enter: \n\
95 cmpl $GMON_PROF_OFF,_gmonparam+GM_STATE \n\
96 je .mcount_exit \n\
97 # \n\
98 # The caller's stack frame has already been built, so \n\
99 # %ebp is the caller's frame pointer. The caller's \n\
100 # raddr is in the caller's frame following the caller's \n\
101 # caller's frame pointer. \n\
102 # \n\
103 movl 4(%ebp),%edx \n\
104 .got_frompc: \n\
105 # \n\
106 # Our raddr is the caller's pc. \n\
107 # \n\
108 movl (%esp),%eax \n\
109 \n\
110 pushfl \n\
111 pushl %eax \n\
112 pushl %edx \n\
113 cli \n\
114 call mcount \n\
115 addl $8,%esp \n\
116 popfl \n\
117 .mcount_exit: \n\
118 ret \n\
119 ");
120 #else /* !__GNUCLIKE_ASM */
121 #error "this file needs to be ported to your compiler"
122 #endif /* __GNUCLIKE_ASM */
123
124 #ifdef GUPROF
125 /*
126 * [.]mexitcount saves the return register(s), loads selfpc and calls
127 * mexitcount(selfpc) to do the work. Someday it should be in a machine
128 * dependent file together with cputime(), __mcount and [.]mcount. cputime()
129 * can't just be put in machdep.c because it has to be compiled without -pg.
130 */
131 #ifdef __GNUCLIKE_ASM
132 __asm(" \n\
133 .text \n\
134 # \n\
135 # Dummy label to be seen when gprof -u hides [.]mexitcount. \n\
136 # \n\
137 .p2align 4,0x90 \n\
138 .globl __mexitcount \n\
139 .type __mexitcount,@function \n\
140 __mexitcount: \n\
141 nop \n\
142 \n\
143 GMON_PROF_HIRES = 4 \n\
144 \n\
145 .p2align 4,0x90 \n\
146 .globl .mexitcount \n\
147 .mexitcount: \n\
148 .globl __cyg_profile_func_exit \n\
149 __cyg_profile_func_exit: \n\
150 cmpl $GMON_PROF_HIRES,_gmonparam+GM_STATE \n\
151 jne .mexitcount_exit \n\
152 pushl %edx \n\
153 pushl %eax \n\
154 movl 8(%esp),%eax \n\
155 pushfl \n\
156 pushl %eax \n\
157 cli \n\
158 call mexitcount \n\
159 addl $4,%esp \n\
160 popfl \n\
161 popl %eax \n\
162 popl %edx \n\
163 .mexitcount_exit: \n\
164 ret \n\
165 ");
166 #endif /* __GNUCLIKE_ASM */
167
168 /*
169 * Return the time elapsed since the last call. The units are machine-
170 * dependent.
171 */
172 int
173 cputime()
174 {
175 u_int count;
176 int delta;
177 #if (defined(I586_CPU) || defined(I686_CPU)) && !defined(SMP) && \
178 defined(PERFMON) && defined(I586_PMC_GUPROF)
179 u_quad_t event_count;
180 #endif
181 u_char high, low;
182 static u_int prev_count;
183
184 #if defined(I586_CPU) || defined(I686_CPU)
185 if (cputime_clock == CPUTIME_CLOCK_TSC) {
186 /*
187 * Scale the TSC a little to make cputime()'s frequency
188 * fit in an int, assuming that the TSC frequency fits
189 * in a u_int. Use a fixed scale since dynamic scaling
190 * would be slower and we can't really use the low bit
191 * of precision.
192 */
193 count = (u_int)rdtsc() & ~1u;
194 delta = (int)(count - prev_count) >> 1;
195 prev_count = count;
196 return (delta);
197 }
198 #if defined(PERFMON) && defined(I586_PMC_GUPROF) && !defined(SMP)
199 if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
200 /*
201 * XXX permon_read() should be inlined so that the
202 * perfmon module doesn't need to be compiled with
203 * profiling disabled and so that it is fast.
204 */
205 perfmon_read(0, &event_count);
206
207 count = (u_int)event_count;
208 delta = (int)(count - prev_count);
209 prev_count = count;
210 return (delta);
211 }
212 #endif /* PERFMON && I586_PMC_GUPROF && !SMP */
213 #endif /* I586_CPU || I686_CPU */
214
215 /*
216 * Read the current value of the 8254 timer counter 0.
217 */
218 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
219 low = inb(TIMER_CNTR0);
220 high = inb(TIMER_CNTR0);
221 count = ((high << 8) | low) << CPUTIME_CLOCK_I8254_SHIFT;
222
223 /*
224 * The timer counts down from TIMER_CNTR0_MAX to 0 and then resets.
225 * While profiling is enabled, this routine is called at least twice
226 * per timer reset (for mcounting and mexitcounting hardclock()),
227 * so at most one reset has occurred since the last call, and one
228 * has occurred iff the current count is larger than the previous
229 * count. This allows counter underflow to be detected faster
230 * than in microtime().
231 */
232 delta = prev_count - count;
233 prev_count = count;
234 if ((int) delta <= 0)
235 return (delta + (timer0_max_count << CPUTIME_CLOCK_I8254_SHIFT));
236 return (delta);
237 }
238
239 static int
240 sysctl_machdep_cputime_clock(SYSCTL_HANDLER_ARGS)
241 {
242 int clock;
243 int error;
244 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
245 int event;
246 struct pmc pmc;
247 #endif
248
249 clock = cputime_clock;
250 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
251 if (clock == CPUTIME_CLOCK_I586_PMC) {
252 pmc.pmc_val = cputime_clock_pmc_conf;
253 clock += pmc.pmc_event;
254 }
255 #endif
256 error = sysctl_handle_opaque(oidp, &clock, sizeof clock, req);
257 if (error == 0 && req->newptr != NULL) {
258 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
259 if (clock >= CPUTIME_CLOCK_I586_PMC) {
260 event = clock - CPUTIME_CLOCK_I586_PMC;
261 if (event >= 256)
262 return (EINVAL);
263 pmc.pmc_num = 0;
264 pmc.pmc_event = event;
265 pmc.pmc_unit = 0;
266 pmc.pmc_flags = PMCF_E | PMCF_OS | PMCF_USR;
267 pmc.pmc_mask = 0;
268 cputime_clock_pmc_conf = pmc.pmc_val;
269 cputime_clock = CPUTIME_CLOCK_I586_PMC;
270 } else
271 #endif
272 {
273 if (clock < 0 || clock >= CPUTIME_CLOCK_I586_PMC)
274 return (EINVAL);
275 cputime_clock = clock;
276 }
277 }
278 return (error);
279 }
280
281 SYSCTL_PROC(_machdep, OID_AUTO, cputime_clock, CTLTYPE_INT | CTLFLAG_RW,
282 0, sizeof(u_int), sysctl_machdep_cputime_clock, "I", "");
283
284 /*
285 * The start and stop routines need not be here since we turn off profiling
286 * before calling them. They are here for convenience.
287 */
288
289 void
290 startguprof(gp)
291 struct gmonparam *gp;
292 {
293 if (cputime_clock == CPUTIME_CLOCK_UNINITIALIZED) {
294 cputime_clock = CPUTIME_CLOCK_I8254;
295 #if defined(I586_CPU) || defined(I686_CPU)
296 if (tsc_freq != 0 && !tsc_is_broken && mp_ncpus == 1)
297 cputime_clock = CPUTIME_CLOCK_TSC;
298 #endif
299 }
300 gp->profrate = timer_freq << CPUTIME_CLOCK_I8254_SHIFT;
301 #if defined(I586_CPU) || defined(I686_CPU)
302 if (cputime_clock == CPUTIME_CLOCK_TSC) {
303 gp->profrate = tsc_freq >> 1;
304 cputime_prof_active = 1;
305 }
306 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
307 else if (cputime_clock == CPUTIME_CLOCK_I586_PMC) {
308 if (perfmon_avail() &&
309 perfmon_setup(0, cputime_clock_pmc_conf) == 0) {
310 if (perfmon_start(0) != 0)
311 perfmon_fini(0);
312 else {
313 /* XXX 1 event == 1 us. */
314 gp->profrate = 1000000;
315
316 saved_gmp = *gp;
317
318 /* Zap overheads. They are invalid. */
319 gp->cputime_overhead = 0;
320 gp->mcount_overhead = 0;
321 gp->mcount_post_overhead = 0;
322 gp->mcount_pre_overhead = 0;
323 gp->mexitcount_overhead = 0;
324 gp->mexitcount_post_overhead = 0;
325 gp->mexitcount_pre_overhead = 0;
326
327 cputime_clock_pmc_init = TRUE;
328 }
329 }
330 }
331 #endif /* PERFMON && I586_PMC_GUPROF */
332 #endif /* I586_CPU || I686_CPU */
333 cputime_bias = 0;
334 cputime();
335 }
336
337 void
338 stopguprof(gp)
339 struct gmonparam *gp;
340 {
341 #if defined(PERFMON) && defined(I586_PMC_GUPROF)
342 if (cputime_clock_pmc_init) {
343 *gp = saved_gmp;
344 perfmon_fini(0);
345 cputime_clock_pmc_init = FALSE;
346 }
347 #endif
348 #if defined(I586_CPU) || defined(I686_CPU)
349 if (cputime_clock == CPUTIME_CLOCK_TSC)
350 cputime_prof_active = 0;
351 #endif
352 }
353
354 #if defined(I586_CPU) || defined(I686_CPU)
355 /* If the cpu frequency changed while profiling, report a warning. */
356 static void
357 tsc_freq_changed(void *arg, const struct cf_level *level, int status)
358 {
359
360 /*
361 * If there was an error during the transition or
362 * TSC is P-state invariant, don't do anything.
363 */
364 if (status != 0 || tsc_is_invariant)
365 return;
366 if (cputime_prof_active && cputime_clock == CPUTIME_CLOCK_TSC)
367 printf("warning: cpu freq changed while profiling active\n");
368 }
369
370 EVENTHANDLER_DEFINE(cpufreq_post_change, tsc_freq_changed, NULL,
371 EVENTHANDLER_PRI_ANY);
372 #endif /* I586_CPU || I686_CPU */
373
374 #endif /* GUPROF */
Cache object: f7ab8bd37a24b6ec190d694dbd9603ec
|