mcount.c 8.5 KB

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