]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/libkern/mcount.c
Upgrade our copies of clang, llvm, lld, lldb, compiler-rt and libc++ to
[FreeBSD/FreeBSD.git] / sys / libkern / mcount.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1992, 1993
5  *      The Regents of the University of California.  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  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/gmon.h>
37 #ifdef _KERNEL
38 #ifndef GUPROF
39 #include <sys/systm.h>
40 #endif
41 #include <vm/vm.h>
42 #include <vm/vm_param.h>
43 #include <vm/pmap.h>
44 #endif
45
46 /*
47  * mcount is called on entry to each function compiled with the profiling
48  * switch set.  _mcount(), which is declared in a machine-dependent way
49  * with _MCOUNT_DECL, does the actual work and is either inlined into a
50  * C routine or called by an assembly stub.  In any case, this magic is
51  * taken care of by the MCOUNT definition in <machine/profile.h>.
52  *
53  * _mcount updates data structures that represent traversals of the
54  * program's call graph edges.  frompc and selfpc are the return
55  * address and function address that represents the given call graph edge.
56  *
57  * Note: the original BSD code used the same variable (frompcindex) for
58  * both frompcindex and frompc.  Any reasonable, modern compiler will
59  * perform this optimization.
60  */
61 _MCOUNT_DECL(uintfptr_t frompc, uintfptr_t selfpc)      /* _mcount; may be static, inline, etc */
62 {
63 #ifdef GUPROF
64         int delta;
65 #endif
66         fptrdiff_t frompci;
67         u_short *frompcindex;
68         struct tostruct *top, *prevtop;
69         struct gmonparam *p;
70         long toindex;
71 #ifdef _KERNEL
72         MCOUNT_DECL(s)
73 #endif
74
75         p = &_gmonparam;
76 #ifndef GUPROF                  /* XXX */
77         /*
78          * check that we are profiling
79          * and that we aren't recursively invoked.
80          */
81         if (p->state != GMON_PROF_ON)
82                 return;
83 #endif
84 #ifdef _KERNEL
85         MCOUNT_ENTER(s);
86 #else
87         p->state = GMON_PROF_BUSY;
88 #endif
89
90 #ifdef _KERNEL
91         /*
92          * When we are called from an exception handler, frompc may be
93          * a user address.  Convert such frompc's to some representation
94          * in kernel address space.
95          */
96         frompc = MCOUNT_FROMPC_USER(frompc);
97 #endif
98
99         frompci = frompc - p->lowpc;
100         if (frompci >= p->textsize)
101                 goto done;
102
103 #ifdef GUPROF
104         if (p->state == GMON_PROF_HIRES) {
105                 /*
106                  * Count the time since cputime() was previously called
107                  * against `frompc'.  Compensate for overheads.
108                  *
109                  * cputime() sets its prev_count variable to the count when
110                  * it is called.  This in effect starts a counter for
111                  * the next period of execution (normally from now until 
112                  * the next call to mcount() or mexitcount()).  We set
113                  * cputime_bias to compensate for our own overhead.
114                  *
115                  * We use the usual sampling counters since they can be
116                  * located efficiently.  4-byte counters are usually
117                  * necessary.  gprof will add up the scattered counts
118                  * just like it does for statistical profiling.  All
119                  * counts are signed so that underflow in the subtractions
120                  * doesn't matter much (negative counts are normally
121                  * compensated for by larger counts elsewhere).  Underflow
122                  * shouldn't occur, but may be caused by slightly wrong
123                  * calibrations or from not clearing cputime_bias.
124                  */
125                 delta = cputime() - cputime_bias - p->mcount_pre_overhead;
126                 cputime_bias = p->mcount_post_overhead;
127                 KCOUNT(p, frompci) += delta;
128                 *p->cputime_count += p->cputime_overhead;
129                 *p->mcount_count += p->mcount_overhead;
130         }
131 #endif /* GUPROF */
132
133 #ifdef _KERNEL
134         /*
135          * When we are called from an exception handler, frompc is faked
136          * to be for where the exception occurred.  We've just solidified
137          * the count for there.  Now convert frompci to an index that
138          * represents the kind of exception so that interruptions appear
139          * in the call graph as calls from those index instead of calls
140          * from all over.
141          */
142         frompc = MCOUNT_FROMPC_INTR(selfpc);
143         if ((frompc - p->lowpc) < p->textsize)
144                 frompci = frompc - p->lowpc;
145 #endif
146
147         /*
148          * check that frompc is a reasonable pc value.
149          * for example: signal catchers get called from the stack,
150          *              not from text space.  too bad.
151          */
152         if (frompci >= p->textsize)
153                 goto done;
154
155         frompcindex = &p->froms[frompci / (p->hashfraction * sizeof(*p->froms))];
156         toindex = *frompcindex;
157         if (toindex == 0) {
158                 /*
159                  *      first time traversing this arc
160                  */
161                 toindex = ++p->tos[0].link;
162                 if (toindex >= p->tolimit)
163                         /* halt further profiling */
164                         goto overflow;
165
166                 *frompcindex = toindex;
167                 top = &p->tos[toindex];
168                 top->selfpc = selfpc;
169                 top->count = 1;
170                 top->link = 0;
171                 goto done;
172         }
173         top = &p->tos[toindex];
174         if (top->selfpc == selfpc) {
175                 /*
176                  * arc at front of chain; usual case.
177                  */
178                 top->count++;
179                 goto done;
180         }
181         /*
182          * have to go looking down chain for it.
183          * top points to what we are looking at,
184          * prevtop points to previous top.
185          * we know it is not at the head of the chain.
186          */
187         for (; /* goto done */; ) {
188                 if (top->link == 0) {
189                         /*
190                          * top is end of the chain and none of the chain
191                          * had top->selfpc == selfpc.
192                          * so we allocate a new tostruct
193                          * and link it to the head of the chain.
194                          */
195                         toindex = ++p->tos[0].link;
196                         if (toindex >= p->tolimit)
197                                 goto overflow;
198
199                         top = &p->tos[toindex];
200                         top->selfpc = selfpc;
201                         top->count = 1;
202                         top->link = *frompcindex;
203                         *frompcindex = toindex;
204                         goto done;
205                 }
206                 /*
207                  * otherwise, check the next arc on the chain.
208                  */
209                 prevtop = top;
210                 top = &p->tos[top->link];
211                 if (top->selfpc == selfpc) {
212                         /*
213                          * there it is.
214                          * increment its count
215                          * move it to the head of the chain.
216                          */
217                         top->count++;
218                         toindex = prevtop->link;
219                         prevtop->link = top->link;
220                         top->link = *frompcindex;
221                         *frompcindex = toindex;
222                         goto done;
223                 }
224
225         }
226 done:
227 #ifdef _KERNEL
228         MCOUNT_EXIT(s);
229 #else
230         p->state = GMON_PROF_ON;
231 #endif
232         return;
233 overflow:
234         p->state = GMON_PROF_ERROR;
235 #ifdef _KERNEL
236         MCOUNT_EXIT(s);
237 #endif
238         return;
239 }
240
241 /*
242  * Actual definition of mcount function.  Defined in <machine/profile.h>,
243  * which is included by <sys/gmon.h>.
244  */
245 MCOUNT
246
247 #ifdef GUPROF
248 void
249 mexitcount(uintfptr_t selfpc)
250 {
251         struct gmonparam *p;
252         uintfptr_t selfpcdiff;
253
254         p = &_gmonparam;
255         selfpcdiff = selfpc - (uintfptr_t)p->lowpc;
256         if (selfpcdiff < p->textsize) {
257                 int delta;
258
259                 /*
260                  * Count the time since cputime() was previously called
261                  * against `selfpc'.  Compensate for overheads.
262                  */
263                 delta = cputime() - cputime_bias - p->mexitcount_pre_overhead;
264                 cputime_bias = p->mexitcount_post_overhead;
265                 KCOUNT(p, selfpcdiff) += delta;
266                 *p->cputime_count += p->cputime_overhead;
267                 *p->mexitcount_count += p->mexitcount_overhead;
268         }
269 }
270
271 #ifndef __GNUCLIKE_ASM
272 #error "This file uses null asms to prevent timing loops being optimized away."
273 #endif
274
275 void
276 empty_loop()
277 {
278         int i;
279
280         for (i = 0; i < CALIB_SCALE; i++)
281                 __asm __volatile("");
282 }
283
284 void
285 nullfunc()
286 {
287         __asm __volatile("");
288 }
289
290 void
291 nullfunc_loop()
292 {
293         int i;
294
295         for (i = 0; i < CALIB_SCALE; i++)
296                 nullfunc();
297 }
298 #endif /* GUPROF */