vkernel: Fix compilation with profiling support.
[dragonfly.git] / sys / libkern / mcount.c
1 /*-
2  * Copyright (c) 1983, 1992, 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  * $FreeBSD: src/sys/libkern/mcount.c,v 1.16 1999/12/29 04:54:41 peter Exp $
34  */
35
36 #include <sys/param.h>
37 #include <sys/gmon.h>
38 #if defined(_KERNEL) && !defined(_KERNEL_VIRTUAL)
39 #ifndef GUPROF
40 #include <sys/systm.h>
41 #endif
42 #include <vm/vm.h>
43 #include <vm/vm_param.h>
44 #include <vm/pmap.h>
45 void    bintr(void);
46 void    btrap(void);
47 void    eintr(void);
48 void    user(void);
49 #endif
50
51 /*
52  * mcount is called on entry to each function compiled with the profiling
53  * switch set.  _mcount(), which is declared in a machine-dependent way
54  * with _MCOUNT_DECL, does the actual work and is either inlined into a
55  * C routine or called by an assembly stub.  In any case, this magic is
56  * taken care of by the MCOUNT definition in <machine/profile.h>.
57  *
58  * _mcount updates data structures that represent traversals of the
59  * program's call graph edges.  frompc and selfpc are the return
60  * address and function address that represents the given call graph edge.
61  *
62  * Note: the original BSD code used the same variable (frompcindex) for
63  * both frompcindex and frompc.  Any reasonable, modern compiler will
64  * perform this optimization.
65  */
66 /* _mcount; may be static, inline, etc */
67 _MCOUNT_DECL(uintfptr_t frompc, uintfptr_t selfpc)
68 {
69 #ifdef GUPROF
70         int delta;
71 #endif
72         fptrdiff_t frompci;
73         u_short *frompcindex;
74         struct tostruct *top, *prevtop;
75         struct gmonparam *p;
76         long toindex;
77 #if defined(_KERNEL) && !defined(_KERNEL_VIRTUAL)
78         MCOUNT_DECL(s)
79 #endif
80
81         p = &_gmonparam;
82 #ifndef GUPROF                  /* XXX */
83         /*
84          * check that we are profiling
85          * and that we aren't recursively invoked.
86          */
87         if (p->state != GMON_PROF_ON)
88                 return;
89 #endif
90 #if defined(_KERNEL) && !defined(_KERNEL_VIRTUAL)
91         MCOUNT_ENTER(s);
92 #else
93         p->state = GMON_PROF_BUSY;
94 #endif
95         frompci = frompc - p->lowpc;
96
97 #if defined(_KERNEL) && !defined(_KERNEL_VIRTUAL)
98         /*
99          * When we are called from an exception handler, frompci may be
100          * for a user address.  Convert such frompci's to the index of
101          * user() to merge all user counts.
102          *
103          * XXX doesn't work properly with vkernel
104          */
105         if (frompci >= p->textsize) {
106                 if (frompci + p->lowpc
107                     >= (uintfptr_t)(VM_MAX_USER_ADDRESS + UPAGES * PAGE_SIZE))
108                         goto done;
109                 frompci = (uintfptr_t)user - p->lowpc;
110                 if (frompci >= p->textsize)
111                     goto done;
112         }
113 #endif
114
115 #ifdef GUPROF
116         if (p->state == GMON_PROF_HIRES) {
117                 /*
118                  * Count the time since cputime() was previously called
119                  * against `frompc'.  Compensate for overheads.
120                  *
121                  * cputime() sets its prev_count variable to the count when
122                  * it is called.  This in effect starts a counter for
123                  * the next period of execution (normally from now until 
124                  * the next call to mcount() or mexitcount()).  We set
125                  * cputime_bias to compensate for our own overhead.
126                  *
127                  * We use the usual sampling counters since they can be
128                  * located efficiently.  4-byte counters are usually
129                  * necessary.  gprof will add up the scattered counts
130                  * just like it does for statistical profiling.  All
131                  * counts are signed so that underflow in the subtractions
132                  * doesn't matter much (negative counts are normally
133                  * compensated for by larger counts elsewhere).  Underflow
134                  * shouldn't occur, but may be caused by slightly wrong
135                  * calibrations or from not clearing cputime_bias.
136                  */
137                 delta = cputime() - cputime_bias - p->mcount_pre_overhead;
138                 cputime_bias = p->mcount_post_overhead;
139                 KCOUNT(p, frompci) += delta;
140                 *p->cputime_count += p->cputime_overhead;
141                 *p->mcount_count += p->mcount_overhead;
142         }
143 #endif /* GUPROF */
144
145 #if defined(_KERNEL) && !defined(_KERNEL_VIRTUAL)
146         /*
147          * When we are called from an exception handler, frompc is faked
148          * to be for where the exception occurred.  We've just solidified
149          * the count for there.  Now convert frompci to the index of btrap()
150          * for trap handlers and bintr() for interrupt handlers to make
151          * exceptions appear in the call graph as calls from btrap() and
152          * bintr() instead of calls from all over.
153          */
154         if ((uintfptr_t)selfpc >= (uintfptr_t)btrap
155             && (uintfptr_t)selfpc < (uintfptr_t)eintr) {
156                 if ((uintfptr_t)selfpc >= (uintfptr_t)bintr)
157                         frompci = (uintfptr_t)bintr - p->lowpc;
158                 else
159                         frompci = (uintfptr_t)btrap - p->lowpc;
160         }
161 #endif
162
163         /*
164          * check that frompc is a reasonable pc value.
165          * for example: signal catchers get called from the stack,
166          *              not from text space.  too bad.
167          */
168         if (frompci >= p->textsize)
169                 goto done;
170
171         frompcindex = &p->froms[frompci / (p->hashfraction * sizeof(*p->froms))];
172         toindex = *frompcindex;
173         if (toindex == 0) {
174                 /*
175                  *      first time traversing this arc
176                  */
177                 toindex = ++p->tos[0].link;
178                 if (toindex >= p->tolimit)
179                         /* halt further profiling */
180                         goto overflow;
181
182                 *frompcindex = toindex;
183                 top = &p->tos[toindex];
184                 top->selfpc = selfpc;
185                 top->count = 1;
186                 top->link = 0;
187                 goto done;
188         }
189         top = &p->tos[toindex];
190         if (top->selfpc == selfpc) {
191                 /*
192                  * arc at front of chain; usual case.
193                  */
194                 top->count++;
195                 goto done;
196         }
197         /*
198          * have to go looking down chain for it.
199          * top points to what we are looking at,
200          * prevtop points to previous top.
201          * we know it is not at the head of the chain.
202          */
203         for (; /* goto done */; ) {
204                 if (top->link == 0) {
205                         /*
206                          * top is end of the chain and none of the chain
207                          * had top->selfpc == selfpc.
208                          * so we allocate a new tostruct
209                          * and link it to the head of the chain.
210                          */
211                         toindex = ++p->tos[0].link;
212                         if (toindex >= p->tolimit)
213                                 goto overflow;
214
215                         top = &p->tos[toindex];
216                         top->selfpc = selfpc;
217                         top->count = 1;
218                         top->link = *frompcindex;
219                         *frompcindex = toindex;
220                         goto done;
221                 }
222                 /*
223                  * otherwise, check the next arc on the chain.
224                  */
225                 prevtop = top;
226                 top = &p->tos[top->link];
227                 if (top->selfpc == selfpc) {
228                         /*
229                          * there it is.
230                          * increment its count
231                          * move it to the head of the chain.
232                          */
233                         top->count++;
234                         toindex = prevtop->link;
235                         prevtop->link = top->link;
236                         top->link = *frompcindex;
237                         *frompcindex = toindex;
238                         goto done;
239                 }
240
241         }
242 done:
243 #if defined(_KERNEL) && !defined(_KERNEL_VIRTUAL)
244         MCOUNT_EXIT(s);
245 #else
246         p->state = GMON_PROF_ON;
247 #endif
248         return;
249 overflow:
250         p->state = GMON_PROF_ERROR;
251 #if defined(_KERNEL) && !defined(_KERNEL_VIRTUAL)
252         MCOUNT_EXIT(s);
253 #endif
254         return;
255 }
256
257 /*
258  * Actual definition of mcount function.  Defined in <machine/profile.h>,
259  * which is included by <sys/gmon.h>.
260  */
261 MCOUNT
262
263 #ifdef GUPROF
264 void
265 mexitcount(uintfptr_t selfpc)
266 {
267         struct gmonparam *p;
268         uintfptr_t selfpcdiff;
269
270         p = &_gmonparam;
271         selfpcdiff = selfpc - (uintfptr_t)p->lowpc;
272         if (selfpcdiff < p->textsize) {
273                 int delta;
274
275                 /*
276                  * Count the time since cputime() was previously called
277                  * against `selfpc'.  Compensate for overheads.
278                  */
279                 delta = cputime() - cputime_bias - p->mexitcount_pre_overhead;
280                 cputime_bias = p->mexitcount_post_overhead;
281                 KCOUNT(p, selfpcdiff) += delta;
282                 *p->cputime_count += p->cputime_overhead;
283                 *p->mexitcount_count += p->mexitcount_overhead;
284         }
285 }
286
287 void
288 empty_loop(void)
289 {
290         int i;
291
292         for (i = 0; i < CALIB_SCALE; i++)
293                 ;
294 }
295
296 void
297 nullfunc(void)
298 {
299 }
300
301 void
302 nullfunc_loop(void)
303 {
304         int i;
305
306         for (i = 0; i < CALIB_SCALE; i++)
307                 nullfunc();
308 }
309 #endif /* GUPROF */