]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/prof_machdep.c
MFV r329793, r329795:
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / prof_machdep.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1996 Bruce D. Evans.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #ifdef GUPROF
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/timerreg.h>
46
47 #define CPUTIME_CLOCK_UNINITIALIZED     0
48 #define CPUTIME_CLOCK_I8254             1
49 #define CPUTIME_CLOCK_TSC               2
50 #define CPUTIME_CLOCK_I8254_SHIFT       7
51
52 int     cputime_bias = 1;       /* initialize for locality of reference */
53
54 static int      cputime_clock = CPUTIME_CLOCK_UNINITIALIZED;
55 static int      cputime_prof_active;
56 #endif /* GUPROF */
57
58 #ifdef __GNUCLIKE_ASM
59 __asm("                                                         \n\
60 GM_STATE        =       0                                       \n\
61 GMON_PROF_OFF   =       3                                       \n\
62                                                                 \n\
63         .text                                                   \n\
64         .p2align 4,0x90                                         \n\
65         .globl  __mcount                                        \n\
66         .type   __mcount,@function                              \n\
67 __mcount:                                                       \n\
68         #                                                       \n\
69         # Check that we are profiling.  Do it early for speed.  \n\
70         #                                                       \n\
71         cmpl    $GMON_PROF_OFF,_gmonparam+GM_STATE              \n\
72         je      .mcount_exit                                    \n\
73         #                                                       \n\
74         # __mcount is the same as [.]mcount except the caller   \n\
75         # hasn't changed the stack except to call here, so the  \n\
76         # caller's raddr is above our raddr.                    \n\
77         #                                                       \n\
78         pushq   %rax                                            \n\
79         pushq   %rdx                                            \n\
80         pushq   %rcx                                            \n\
81         pushq   %rsi                                            \n\
82         pushq   %rdi                                            \n\
83         pushq   %r8                                             \n\
84         pushq   %r9                                             \n\
85         movq    7*8+8(%rsp),%rdi                                \n\
86         jmp     .got_frompc                                     \n\
87                                                                 \n\
88         .p2align 4,0x90                                         \n\
89         .globl  .mcount                                         \n\
90 .mcount:                                                        \n\
91         cmpl    $GMON_PROF_OFF,_gmonparam+GM_STATE              \n\
92         je      .mcount_exit                                    \n\
93         #                                                       \n\
94         # The caller's stack frame has already been built, so   \n\
95         # %rbp is the caller's frame pointer.  The caller's     \n\
96         # raddr is in the caller's frame following the caller's \n\
97         # caller's frame pointer.                               \n\
98         #                                                       \n\
99         pushq   %rax                                            \n\
100         pushq   %rdx                                            \n\
101         pushq   %rcx                                            \n\
102         pushq   %rsi                                            \n\
103         pushq   %rdi                                            \n\
104         pushq   %r8                                             \n\
105         pushq   %r9                                             \n\
106         movq    8(%rbp),%rdi                                    \n\
107 .got_frompc:                                                    \n\
108         #                                                       \n\
109         # Our raddr is the caller's pc.                         \n\
110         #                                                       \n\
111         movq    7*8(%rsp),%rsi                                  \n\
112                                                                 \n\
113         pushfq                                                  \n\
114         cli                                                     \n\
115         call    mcount                                          \n\
116         popfq                                                   \n\
117         popq    %r9                                             \n\
118         popq    %r8                                             \n\
119         popq    %rdi                                            \n\
120         popq    %rsi                                            \n\
121         popq    %rcx                                            \n\
122         popq    %rdx                                            \n\
123         popq    %rax                                            \n\
124 .mcount_exit:                                                   \n\
125         ret     $0                                              \n\
126 ");
127 #else /* !__GNUCLIKE_ASM */
128 #error "this file needs to be ported to your compiler"
129 #endif /* __GNUCLIKE_ASM */
130
131 #ifdef GUPROF
132 /*
133  * [.]mexitcount saves the return register(s), loads selfpc and calls
134  * mexitcount(selfpc) to do the work.  Someday it should be in a machine
135  * dependent file together with cputime(), __mcount and [.]mcount.  cputime()
136  * can't just be put in machdep.c because it has to be compiled without -pg.
137  */
138 #ifdef __GNUCLIKE_ASM
139 __asm("                                                         \n\
140         .text                                                   \n\
141 #                                                               \n\
142 # Dummy label to be seen when gprof -u hides [.]mexitcount.     \n\
143 #                                                               \n\
144         .p2align 4,0x90                                         \n\
145         .globl  __mexitcount                                    \n\
146         .type   __mexitcount,@function                          \n\
147 __mexitcount:                                                   \n\
148         nop                                                     \n\
149                                                                 \n\
150 GMON_PROF_HIRES =       4                                       \n\
151                                                                 \n\
152         .p2align 4,0x90                                         \n\
153         .globl  .mexitcount                                     \n\
154 .mexitcount:                                                    \n\
155         cmpl    $GMON_PROF_HIRES,_gmonparam+GM_STATE            \n\
156         jne     .mexitcount_exit                                \n\
157         pushq   %rax                                            \n\
158         pushq   %rdx                                            \n\
159         pushq   %rcx                                            \n\
160         pushq   %rsi                                            \n\
161         pushq   %rdi                                            \n\
162         pushq   %r8                                             \n\
163         pushq   %r9                                             \n\
164         movq    7*8(%rsp),%rdi                                  \n\
165         pushfq                                                  \n\
166         cli                                                     \n\
167         call    mexitcount                                      \n\
168         popfq                                                   \n\
169         popq    %r9                                             \n\
170         popq    %r8                                             \n\
171         popq    %rdi                                            \n\
172         popq    %rsi                                            \n\
173         popq    %rcx                                            \n\
174         popq    %rdx                                            \n\
175         popq    %rax                                            \n\
176 .mexitcount_exit:                                               \n\
177         ret     $0                                              \n\
178 ");
179 #endif /* __GNUCLIKE_ASM */
180
181 /*
182  * Return the time elapsed since the last call.  The units are machine-
183  * dependent.
184  */
185 int
186 cputime()
187 {
188         u_int count;
189         int delta;
190         u_char high, low;
191         static u_int prev_count;
192
193         if (cputime_clock == CPUTIME_CLOCK_TSC) {
194                 /*
195                  * Scale the TSC a little to make cputime()'s frequency
196                  * fit in an int, assuming that the TSC frequency fits
197                  * in a u_int.  Use a fixed scale since dynamic scaling
198                  * would be slower and we can't really use the low bit
199                  * of precision.
200                  */
201                 count = (u_int)rdtsc() & ~1u;
202                 delta = (int)(count - prev_count) >> 1;
203                 prev_count = count;
204                 return (delta);
205         }
206
207         /*
208          * Read the current value of the 8254 timer counter 0.
209          */
210         outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
211         low = inb(TIMER_CNTR0);
212         high = inb(TIMER_CNTR0);
213         count = ((high << 8) | low) << CPUTIME_CLOCK_I8254_SHIFT;
214
215         /*
216          * The timer counts down from TIMER_CNTR0_MAX to 0 and then resets.
217          * While profiling is enabled, this routine is called at least twice
218          * per timer reset (for mcounting and mexitcounting hardclock()),
219          * so at most one reset has occurred since the last call, and one
220          * has occurred iff the current count is larger than the previous
221          * count.  This allows counter underflow to be detected faster
222          * than in microtime().
223          */
224         delta = prev_count - count;
225         prev_count = count;
226         if ((int) delta <= 0)
227                 return (delta + (i8254_max_count << CPUTIME_CLOCK_I8254_SHIFT));
228         return (delta);
229 }
230
231 static int
232 sysctl_machdep_cputime_clock(SYSCTL_HANDLER_ARGS)
233 {
234         int clock;
235         int error;
236
237         clock = cputime_clock;
238         error = sysctl_handle_opaque(oidp, &clock, sizeof clock, req);
239         if (error == 0 && req->newptr != NULL) {
240                 if (clock < 0 || clock > CPUTIME_CLOCK_TSC)
241                         return (EINVAL);
242                 cputime_clock = clock;
243         }
244         return (error);
245 }
246
247 SYSCTL_PROC(_machdep, OID_AUTO, cputime_clock, CTLTYPE_INT | CTLFLAG_RW,
248             0, sizeof(u_int), sysctl_machdep_cputime_clock, "I", "");
249
250 /*
251  * The start and stop routines need not be here since we turn off profiling
252  * before calling them.  They are here for convenience.
253  */
254
255 void
256 startguprof(gp)
257         struct gmonparam *gp;
258 {
259         uint64_t freq;
260
261         freq = atomic_load_acq_64(&tsc_freq);
262         if (cputime_clock == CPUTIME_CLOCK_UNINITIALIZED) {
263                 if (freq != 0 && mp_ncpus == 1)
264                         cputime_clock = CPUTIME_CLOCK_TSC;
265                 else
266                         cputime_clock = CPUTIME_CLOCK_I8254;
267         }
268         if (cputime_clock == CPUTIME_CLOCK_TSC) {
269                 gp->profrate = freq >> 1;
270                 cputime_prof_active = 1;
271         } else
272                 gp->profrate = i8254_freq << CPUTIME_CLOCK_I8254_SHIFT;
273         cputime_bias = 0;
274         cputime();
275 }
276
277 void
278 stopguprof(gp)
279         struct gmonparam *gp;
280 {
281         if (cputime_clock == CPUTIME_CLOCK_TSC)
282                 cputime_prof_active = 0;
283 }
284
285 /* If the cpu frequency changed while profiling, report a warning. */
286 static void
287 tsc_freq_changed(void *arg, const struct cf_level *level, int status)
288 {
289
290         /*
291          * If there was an error during the transition or
292          * TSC is P-state invariant, don't do anything.
293          */
294         if (status != 0 || tsc_is_invariant)
295                 return;
296         if (cputime_prof_active && cputime_clock == CPUTIME_CLOCK_TSC)
297                 printf("warning: cpu freq changed while profiling active\n");
298 }
299
300 EVENTHANDLER_DEFINE(cpufreq_post_change, tsc_freq_changed, NULL,
301     EVENTHANDLER_PRI_ANY);
302
303 #endif /* GUPROF */