]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/prof_machdep.c
Update to bmake-20171028
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / prof_machdep.c
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
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/cpu.h>
36 #include <sys/eventhandler.h>
37 #include <sys/gmon.h>
38 #include <sys/kernel.h>
39 #include <sys/smp.h>
40 #include <sys/sysctl.h>
41
42 #include <machine/clock.h>
43 #include <machine/timerreg.h>
44
45 #define CPUTIME_CLOCK_UNINITIALIZED     0
46 #define CPUTIME_CLOCK_I8254             1
47 #define CPUTIME_CLOCK_TSC               2
48 #define CPUTIME_CLOCK_I8254_SHIFT       7
49
50 int     cputime_bias = 1;       /* initialize for locality of reference */
51
52 static int      cputime_clock = CPUTIME_CLOCK_UNINITIALIZED;
53 static int      cputime_prof_active;
54 #endif /* GUPROF */
55
56 #ifdef __GNUCLIKE_ASM
57 __asm("                                                         \n\
58 GM_STATE        =       0                                       \n\
59 GMON_PROF_OFF   =       3                                       \n\
60                                                                 \n\
61         .text                                                   \n\
62         .p2align 4,0x90                                         \n\
63         .globl  __mcount                                        \n\
64         .type   __mcount,@function                              \n\
65 __mcount:                                                       \n\
66         #                                                       \n\
67         # Check that we are profiling.  Do it early for speed.  \n\
68         #                                                       \n\
69         cmpl    $GMON_PROF_OFF,_gmonparam+GM_STATE              \n\
70         je      .mcount_exit                                    \n\
71         #                                                       \n\
72         # __mcount is the same as [.]mcount except the caller   \n\
73         # hasn't changed the stack except to call here, so the  \n\
74         # caller's raddr is above our raddr.                    \n\
75         #                                                       \n\
76         pushq   %rax                                            \n\
77         pushq   %rdx                                            \n\
78         pushq   %rcx                                            \n\
79         pushq   %rsi                                            \n\
80         pushq   %rdi                                            \n\
81         pushq   %r8                                             \n\
82         pushq   %r9                                             \n\
83         movq    7*8+8(%rsp),%rdi                                \n\
84         jmp     .got_frompc                                     \n\
85                                                                 \n\
86         .p2align 4,0x90                                         \n\
87         .globl  .mcount                                         \n\
88 .mcount:                                                        \n\
89         cmpl    $GMON_PROF_OFF,_gmonparam+GM_STATE              \n\
90         je      .mcount_exit                                    \n\
91         #                                                       \n\
92         # The caller's stack frame has already been built, so   \n\
93         # %rbp is the caller's frame pointer.  The caller's     \n\
94         # raddr is in the caller's frame following the caller's \n\
95         # caller's frame pointer.                               \n\
96         #                                                       \n\
97         pushq   %rax                                            \n\
98         pushq   %rdx                                            \n\
99         pushq   %rcx                                            \n\
100         pushq   %rsi                                            \n\
101         pushq   %rdi                                            \n\
102         pushq   %r8                                             \n\
103         pushq   %r9                                             \n\
104         movq    8(%rbp),%rdi                                    \n\
105 .got_frompc:                                                    \n\
106         #                                                       \n\
107         # Our raddr is the caller's pc.                         \n\
108         #                                                       \n\
109         movq    7*8(%rsp),%rsi                                  \n\
110                                                                 \n\
111         pushfq                                                  \n\
112         cli                                                     \n\
113         call    mcount                                          \n\
114         popfq                                                   \n\
115         popq    %r9                                             \n\
116         popq    %r8                                             \n\
117         popq    %rdi                                            \n\
118         popq    %rsi                                            \n\
119         popq    %rcx                                            \n\
120         popq    %rdx                                            \n\
121         popq    %rax                                            \n\
122 .mcount_exit:                                                   \n\
123         ret     $0                                              \n\
124 ");
125 #else /* !__GNUCLIKE_ASM */
126 #error "this file needs to be ported to your compiler"
127 #endif /* __GNUCLIKE_ASM */
128
129 #ifdef GUPROF
130 /*
131  * [.]mexitcount saves the return register(s), loads selfpc and calls
132  * mexitcount(selfpc) to do the work.  Someday it should be in a machine
133  * dependent file together with cputime(), __mcount and [.]mcount.  cputime()
134  * can't just be put in machdep.c because it has to be compiled without -pg.
135  */
136 #ifdef __GNUCLIKE_ASM
137 __asm("                                                         \n\
138         .text                                                   \n\
139 #                                                               \n\
140 # Dummy label to be seen when gprof -u hides [.]mexitcount.     \n\
141 #                                                               \n\
142         .p2align 4,0x90                                         \n\
143         .globl  __mexitcount                                    \n\
144         .type   __mexitcount,@function                          \n\
145 __mexitcount:                                                   \n\
146         nop                                                     \n\
147                                                                 \n\
148 GMON_PROF_HIRES =       4                                       \n\
149                                                                 \n\
150         .p2align 4,0x90                                         \n\
151         .globl  .mexitcount                                     \n\
152 .mexitcount:                                                    \n\
153         cmpl    $GMON_PROF_HIRES,_gmonparam+GM_STATE            \n\
154         jne     .mexitcount_exit                                \n\
155         pushq   %rax                                            \n\
156         pushq   %rdx                                            \n\
157         pushq   %rcx                                            \n\
158         pushq   %rsi                                            \n\
159         pushq   %rdi                                            \n\
160         pushq   %r8                                             \n\
161         pushq   %r9                                             \n\
162         movq    7*8(%rsp),%rdi                                  \n\
163         pushfq                                                  \n\
164         cli                                                     \n\
165         call    mexitcount                                      \n\
166         popfq                                                   \n\
167         popq    %r9                                             \n\
168         popq    %r8                                             \n\
169         popq    %rdi                                            \n\
170         popq    %rsi                                            \n\
171         popq    %rcx                                            \n\
172         popq    %rdx                                            \n\
173         popq    %rax                                            \n\
174 .mexitcount_exit:                                               \n\
175         ret     $0                                              \n\
176 ");
177 #endif /* __GNUCLIKE_ASM */
178
179 /*
180  * Return the time elapsed since the last call.  The units are machine-
181  * dependent.
182  */
183 int
184 cputime()
185 {
186         u_int count;
187         int delta;
188         u_char high, low;
189         static u_int prev_count;
190
191         if (cputime_clock == CPUTIME_CLOCK_TSC) {
192                 /*
193                  * Scale the TSC a little to make cputime()'s frequency
194                  * fit in an int, assuming that the TSC frequency fits
195                  * in a u_int.  Use a fixed scale since dynamic scaling
196                  * would be slower and we can't really use the low bit
197                  * of precision.
198                  */
199                 count = (u_int)rdtsc() & ~1u;
200                 delta = (int)(count - prev_count) >> 1;
201                 prev_count = count;
202                 return (delta);
203         }
204
205         /*
206          * Read the current value of the 8254 timer counter 0.
207          */
208         outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
209         low = inb(TIMER_CNTR0);
210         high = inb(TIMER_CNTR0);
211         count = ((high << 8) | low) << CPUTIME_CLOCK_I8254_SHIFT;
212
213         /*
214          * The timer counts down from TIMER_CNTR0_MAX to 0 and then resets.
215          * While profiling is enabled, this routine is called at least twice
216          * per timer reset (for mcounting and mexitcounting hardclock()),
217          * so at most one reset has occurred since the last call, and one
218          * has occurred iff the current count is larger than the previous
219          * count.  This allows counter underflow to be detected faster
220          * than in microtime().
221          */
222         delta = prev_count - count;
223         prev_count = count;
224         if ((int) delta <= 0)
225                 return (delta + (i8254_max_count << CPUTIME_CLOCK_I8254_SHIFT));
226         return (delta);
227 }
228
229 static int
230 sysctl_machdep_cputime_clock(SYSCTL_HANDLER_ARGS)
231 {
232         int clock;
233         int error;
234
235         clock = cputime_clock;
236         error = sysctl_handle_opaque(oidp, &clock, sizeof clock, req);
237         if (error == 0 && req->newptr != NULL) {
238                 if (clock < 0 || clock > CPUTIME_CLOCK_TSC)
239                         return (EINVAL);
240                 cputime_clock = clock;
241         }
242         return (error);
243 }
244
245 SYSCTL_PROC(_machdep, OID_AUTO, cputime_clock, CTLTYPE_INT | CTLFLAG_RW,
246             0, sizeof(u_int), sysctl_machdep_cputime_clock, "I", "");
247
248 /*
249  * The start and stop routines need not be here since we turn off profiling
250  * before calling them.  They are here for convenience.
251  */
252
253 void
254 startguprof(gp)
255         struct gmonparam *gp;
256 {
257         uint64_t freq;
258
259         freq = atomic_load_acq_64(&tsc_freq);
260         if (cputime_clock == CPUTIME_CLOCK_UNINITIALIZED) {
261                 if (freq != 0 && mp_ncpus == 1)
262                         cputime_clock = CPUTIME_CLOCK_TSC;
263                 else
264                         cputime_clock = CPUTIME_CLOCK_I8254;
265         }
266         if (cputime_clock == CPUTIME_CLOCK_TSC) {
267                 gp->profrate = freq >> 1;
268                 cputime_prof_active = 1;
269         } else
270                 gp->profrate = i8254_freq << CPUTIME_CLOCK_I8254_SHIFT;
271         cputime_bias = 0;
272         cputime();
273 }
274
275 void
276 stopguprof(gp)
277         struct gmonparam *gp;
278 {
279         if (cputime_clock == CPUTIME_CLOCK_TSC)
280                 cputime_prof_active = 0;
281 }
282
283 /* If the cpu frequency changed while profiling, report a warning. */
284 static void
285 tsc_freq_changed(void *arg, const struct cf_level *level, int status)
286 {
287
288         /*
289          * If there was an error during the transition or
290          * TSC is P-state invariant, don't do anything.
291          */
292         if (status != 0 || tsc_is_invariant)
293                 return;
294         if (cputime_prof_active && cputime_clock == CPUTIME_CLOCK_TSC)
295                 printf("warning: cpu freq changed while profiling active\n");
296 }
297
298 EVENTHANDLER_DEFINE(cpufreq_post_change, tsc_freq_changed, NULL,
299     EVENTHANDLER_PRI_ANY);
300
301 #endif /* GUPROF */