gmon.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <features.h>
  30. #include <sys/param.h>
  31. #include <sys/time.h>
  32. #include <sys/gmon.h>
  33. #include <sys/gmon_out.h>
  34. #include <sys/uio.h>
  35. #include <errno.h>
  36. #include <stdio.h>
  37. #include <fcntl.h>
  38. #include <unistd.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <unistd.h>
  43. #include <signal.h>
  44. #include <sys/time.h>
  45. #include <sys/types.h>
  46. #ifdef __UCLIBC_PROFILING__
  47. /* Head of basic-block list or NULL. */
  48. struct __bb *__bb_head;
  49. struct gmonparam _gmonparam = { GMON_PROF_OFF };
  50. /*
  51. * See profil(2) where this is described:
  52. */
  53. static int s_scale;
  54. #define SCALE_1_TO_1 0x10000L
  55. #define ERR(s) write (STDERR_FILENO, s, sizeof (s) - 1)
  56. void moncontrol __P ((int mode));
  57. static void write_hist __P ((int fd));
  58. static void write_call_graph __P ((int fd));
  59. static void write_bb_counts __P ((int fd));
  60. /*
  61. * Control profiling
  62. * profiling is what mcount checks to see if
  63. * all the data structures are ready.
  64. */
  65. void moncontrol (int mode)
  66. {
  67. struct gmonparam *p = &_gmonparam;
  68. /* Don't change the state if we ran into an error. */
  69. if (p->state == GMON_PROF_ERROR)
  70. return;
  71. if (mode)
  72. {
  73. /* start */
  74. profil((void *) p->kcount, p->kcountsize, p->lowpc, s_scale);
  75. p->state = GMON_PROF_ON;
  76. }
  77. else
  78. {
  79. /* stop */
  80. profil(NULL, 0, 0, 0);
  81. p->state = GMON_PROF_OFF;
  82. }
  83. }
  84. void monstartup (u_long lowpc, u_long highpc)
  85. {
  86. register int o;
  87. char *cp;
  88. struct gmonparam *p = &_gmonparam;
  89. /*
  90. * round lowpc and highpc to multiples of the density we're using
  91. * so the rest of the scaling (here and in gprof) stays in ints.
  92. */
  93. p->lowpc = ROUNDDOWN(lowpc, HISTFRACTION * sizeof(HISTCOUNTER));
  94. p->highpc = ROUNDUP(highpc, HISTFRACTION * sizeof(HISTCOUNTER));
  95. p->textsize = p->highpc - p->lowpc;
  96. p->kcountsize = p->textsize / HISTFRACTION;
  97. p->hashfraction = HASHFRACTION;
  98. p->log_hashfraction = -1;
  99. /* The following test must be kept in sync with the corresponding
  100. test in mcount.c. */
  101. if ((HASHFRACTION & (HASHFRACTION - 1)) == 0) {
  102. /* if HASHFRACTION is a power of two, mcount can use shifting
  103. instead of integer division. Precompute shift amount. */
  104. p->log_hashfraction = ffs(p->hashfraction * sizeof(*p->froms)) - 1;
  105. }
  106. p->fromssize = p->textsize / HASHFRACTION;
  107. p->tolimit = p->textsize * ARCDENSITY / 100;
  108. if (p->tolimit < MINARCS)
  109. p->tolimit = MINARCS;
  110. else if (p->tolimit > MAXARCS)
  111. p->tolimit = MAXARCS;
  112. p->tossize = p->tolimit * sizeof(struct tostruct);
  113. cp = calloc (p->kcountsize + p->fromssize + p->tossize, 1);
  114. if (! cp)
  115. {
  116. ERR("monstartup: out of memory\n");
  117. p->tos = NULL;
  118. p->state = GMON_PROF_ERROR;
  119. return;
  120. }
  121. p->tos = (struct tostruct *)cp;
  122. cp += p->tossize;
  123. p->kcount = (HISTCOUNTER *)cp;
  124. cp += p->kcountsize;
  125. p->froms = (ARCINDEX *)cp;
  126. p->tos[0].link = 0;
  127. o = p->highpc - p->lowpc;
  128. if (p->kcountsize < (u_long) o)
  129. {
  130. #ifndef hp300
  131. s_scale = ((float)p->kcountsize / o ) * SCALE_1_TO_1;
  132. #else
  133. /* avoid floating point operations */
  134. int quot = o / p->kcountsize;
  135. if (quot >= 0x10000)
  136. s_scale = 1;
  137. else if (quot >= 0x100)
  138. s_scale = 0x10000 / quot;
  139. else if (o >= 0x800000)
  140. s_scale = 0x1000000 / (o / (p->kcountsize >> 8));
  141. else
  142. s_scale = 0x1000000 / ((o << 8) / p->kcountsize);
  143. #endif
  144. } else
  145. s_scale = SCALE_1_TO_1;
  146. moncontrol(1);
  147. }
  148. /* Return frequency of ticks reported by profil. */
  149. static int profile_frequency (void)
  150. {
  151. /*
  152. * Discover the tick frequency of the machine if something goes wrong,
  153. * we return 0, an impossible hertz.
  154. */
  155. struct itimerval tim;
  156. tim.it_interval.tv_sec = 0;
  157. tim.it_interval.tv_usec = 1;
  158. tim.it_value.tv_sec = 0;
  159. tim.it_value.tv_usec = 0;
  160. setitimer(ITIMER_REAL, &tim, 0);
  161. setitimer(ITIMER_REAL, 0, &tim);
  162. if (tim.it_interval.tv_usec < 2)
  163. return 0;
  164. return (1000000 / tim.it_interval.tv_usec);
  165. }
  166. static void write_hist (int fd)
  167. {
  168. u_char tag = GMON_TAG_TIME_HIST;
  169. struct gmon_hist_hdr thdr __attribute__ ((aligned (__alignof__ (char *))));
  170. if (_gmonparam.kcountsize > 0)
  171. {
  172. struct iovec iov[3] =
  173. {
  174. { &tag, sizeof (tag) },
  175. { &thdr, sizeof (struct gmon_hist_hdr) },
  176. { _gmonparam.kcount, _gmonparam.kcountsize }
  177. };
  178. *(char **) thdr.low_pc = (char *) _gmonparam.lowpc;
  179. *(char **) thdr.high_pc = (char *) _gmonparam.highpc;
  180. *(int32_t *) thdr.hist_size = (_gmonparam.kcountsize
  181. / sizeof (HISTCOUNTER));
  182. *(int32_t *) thdr.prof_rate = profile_frequency ();
  183. strncpy (thdr.dimen, "seconds", sizeof (thdr.dimen));
  184. thdr.dimen_abbrev = 's';
  185. writev (fd, iov, 3);
  186. }
  187. }
  188. static void write_call_graph (int fd)
  189. {
  190. #define NARCS_PER_WRITEV 32
  191. u_char tag = GMON_TAG_CG_ARC;
  192. struct gmon_cg_arc_record raw_arc[NARCS_PER_WRITEV]
  193. __attribute__ ((aligned (__alignof__ (char*))));
  194. ARCINDEX from_index, to_index;
  195. int from_len;
  196. u_long frompc;
  197. struct iovec iov[2 * NARCS_PER_WRITEV];
  198. int nfilled;
  199. for (nfilled = 0; nfilled < NARCS_PER_WRITEV; ++nfilled)
  200. {
  201. iov[2 * nfilled].iov_base = &tag;
  202. iov[2 * nfilled].iov_len = sizeof (tag);
  203. iov[2 * nfilled + 1].iov_base = &raw_arc[nfilled];
  204. iov[2 * nfilled + 1].iov_len = sizeof (struct gmon_cg_arc_record);
  205. }
  206. nfilled = 0;
  207. from_len = _gmonparam.fromssize / sizeof (*_gmonparam.froms);
  208. for (from_index = 0; from_index < from_len; ++from_index)
  209. {
  210. if (_gmonparam.froms[from_index] == 0)
  211. continue;
  212. frompc = _gmonparam.lowpc;
  213. frompc += (from_index * _gmonparam.hashfraction
  214. * sizeof (*_gmonparam.froms));
  215. for (to_index = _gmonparam.froms[from_index];
  216. to_index != 0;
  217. to_index = _gmonparam.tos[to_index].link)
  218. {
  219. struct arc
  220. {
  221. char *frompc;
  222. char *selfpc;
  223. int32_t count;
  224. }
  225. arc;
  226. arc.frompc = (char *) frompc;
  227. arc.selfpc = (char *) _gmonparam.tos[to_index].selfpc;
  228. arc.count = _gmonparam.tos[to_index].count;
  229. memcpy (raw_arc + nfilled, &arc, sizeof (raw_arc [0]));
  230. if (++nfilled == NARCS_PER_WRITEV)
  231. {
  232. writev (fd, iov, 2 * nfilled);
  233. nfilled = 0;
  234. }
  235. }
  236. }
  237. if (nfilled > 0)
  238. writev (fd, iov, 2 * nfilled);
  239. }
  240. static void write_bb_counts (int fd)
  241. {
  242. struct __bb *grp;
  243. u_char tag = GMON_TAG_BB_COUNT;
  244. size_t ncounts;
  245. size_t i;
  246. struct iovec bbhead[2] =
  247. {
  248. { &tag, sizeof (tag) },
  249. { &ncounts, sizeof (ncounts) }
  250. };
  251. struct iovec bbbody[8];
  252. size_t nfilled;
  253. for (i = 0; i < (sizeof (bbbody) / sizeof (bbbody[0])); i += 2)
  254. {
  255. bbbody[i].iov_len = sizeof (grp->addresses[0]);
  256. bbbody[i + 1].iov_len = sizeof (grp->counts[0]);
  257. }
  258. /* Write each group of basic-block info (all basic-blocks in a
  259. compilation unit form a single group). */
  260. for (grp = __bb_head; grp; grp = grp->next)
  261. {
  262. ncounts = grp->ncounts;
  263. writev (fd, bbhead, 2);
  264. for (nfilled = i = 0; i < ncounts; ++i)
  265. {
  266. if (nfilled > (sizeof (bbbody) / sizeof (bbbody[0])) - 2)
  267. {
  268. writev (fd, bbbody, nfilled);
  269. nfilled = 0;
  270. }
  271. bbbody[nfilled++].iov_base = (char *) &grp->addresses[i];
  272. bbbody[nfilled++].iov_base = &grp->counts[i];
  273. }
  274. if (nfilled > 0)
  275. writev (fd, bbbody, nfilled);
  276. }
  277. }
  278. static void write_gmon (void)
  279. {
  280. struct gmon_hdr ghdr __attribute__ ((aligned (__alignof__ (int))));
  281. int fd = -1;
  282. char *env;
  283. #ifndef O_NOFOLLOW
  284. # define O_NOFOLLOW 0
  285. #endif
  286. env = getenv ("GMON_OUT_PREFIX");
  287. if (env != NULL
  288. #if 0
  289. && !__libc_enable_secure
  290. #endif
  291. )
  292. {
  293. size_t len = strlen (env);
  294. char buf[len + 20];
  295. sprintf (buf, "%s.%u", env, getpid ());
  296. fd = open (buf, O_CREAT|O_TRUNC|O_WRONLY|O_NOFOLLOW, 0666);
  297. }
  298. if (fd == -1)
  299. {
  300. fd = open ("gmon.out", O_CREAT|O_TRUNC|O_WRONLY|O_NOFOLLOW, 0666);
  301. if (fd < 0)
  302. {
  303. char buf[300];
  304. int errnum = errno;
  305. fprintf (stderr, "_mcleanup: gmon.out: %s\n",
  306. strerror_r (errnum, buf, sizeof buf));
  307. return;
  308. }
  309. }
  310. /* write gmon.out header: */
  311. memset (&ghdr, '\0', sizeof (struct gmon_hdr));
  312. memcpy (&ghdr.cookie[0], GMON_MAGIC, sizeof (ghdr.cookie));
  313. *(int32_t *) ghdr.version = GMON_VERSION;
  314. write (fd, &ghdr, sizeof (struct gmon_hdr));
  315. /* write PC histogram: */
  316. write_hist (fd);
  317. /* write call-graph: */
  318. write_call_graph (fd);
  319. /* write basic-block execution counts: */
  320. write_bb_counts (fd);
  321. close (fd);
  322. }
  323. void write_profiling (void)
  324. {
  325. int save = _gmonparam.state;
  326. _gmonparam.state = GMON_PROF_OFF;
  327. if (save == GMON_PROF_ON)
  328. write_gmon ();
  329. _gmonparam.state = save;
  330. }
  331. void _mcleanup (void)
  332. {
  333. moncontrol (0);
  334. if (_gmonparam.state != GMON_PROF_ERROR)
  335. write_gmon ();
  336. /* free the memory. */
  337. if (_gmonparam.tos != NULL)
  338. free (_gmonparam.tos);
  339. }
  340. #ifndef SIGPROF
  341. /* Enable statistical profiling, writing samples of the PC into at most
  342. SIZE bytes of SAMPLE_BUFFER; every processor clock tick while profiling
  343. is enabled, the system examines the user PC and increments
  344. SAMPLE_BUFFER[((PC - OFFSET) / 2) * SCALE / 65536]. If SCALE is zero,
  345. disable profiling. Returns zero on success, -1 on error. */
  346. int profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
  347. {
  348. if (scale == 0)
  349. /* Disable profiling. */
  350. return 0;
  351. __set_errno (ENOSYS);
  352. return -1;
  353. }
  354. #else
  355. static u_short *samples;
  356. static size_t nsamples;
  357. static size_t pc_offset;
  358. static u_int pc_scale;
  359. static inline void profil_count (void *pc)
  360. {
  361. size_t i = (pc - pc_offset - (void *) 0) / 2;
  362. if (sizeof (unsigned long long int) > sizeof (size_t))
  363. i = (unsigned long long int) i * pc_scale / 65536;
  364. else
  365. i = i / 65536 * pc_scale + i % 65536 * pc_scale / 65536;
  366. if (i < nsamples)
  367. ++samples[i];
  368. }
  369. /* Get the machine-dependent definition of `profil_counter', the signal
  370. handler for SIGPROF. It calls `profil_count' (above) with the PC of the
  371. interrupted code. */
  372. #include <bits/profil-counter.h>
  373. /* Enable statistical profiling, writing samples of the PC into at most
  374. SIZE bytes of SAMPLE_BUFFER; every processor clock tick while profiling
  375. is enabled, the system examines the user PC and increments
  376. SAMPLE_BUFFER[((PC - OFFSET) / 2) * SCALE / 65536]. If SCALE is zero,
  377. disable profiling. Returns zero on success, -1 on error. */
  378. int profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
  379. {
  380. static struct sigaction oact;
  381. static struct itimerval otimer;
  382. struct sigaction act;
  383. struct itimerval timer;
  384. if (sample_buffer == NULL)
  385. {
  386. /* Disable profiling. */
  387. if (samples == NULL)
  388. /* Wasn't turned on. */
  389. return 0;
  390. if (setitimer (ITIMER_PROF, &otimer, NULL) < 0)
  391. return -1;
  392. samples = NULL;
  393. return sigaction (SIGPROF, &oact, NULL);
  394. }
  395. if (samples)
  396. {
  397. /* Was already turned on. Restore old timer and signal handler
  398. first. */
  399. if (setitimer (ITIMER_PROF, &otimer, NULL) < 0
  400. || sigaction (SIGPROF, &oact, NULL) < 0)
  401. return -1;
  402. }
  403. samples = sample_buffer;
  404. nsamples = size / sizeof *samples;
  405. pc_offset = offset;
  406. pc_scale = scale;
  407. act.sa_handler = (__sighandler_t) &profil_counter;
  408. act.sa_flags = SA_RESTART;
  409. __sigfillset (&act.sa_mask);
  410. if (sigaction (SIGPROF, &act, &oact) < 0)
  411. return -1;
  412. timer.it_value.tv_sec = 0;
  413. timer.it_value.tv_usec = 1;
  414. timer.it_interval = timer.it_value;
  415. return setitimer (ITIMER_PROF, &timer, &otimer);
  416. }
  417. #endif
  418. /* This file provides the machine-dependent definitions of the _MCOUNT_DECL
  419. and MCOUNT macros. */
  420. #include <bits/machine-gmon.h>
  421. #include <bits/atomicity.h>
  422. /*
  423. * mcount is called on entry to each function compiled with the profiling
  424. * switch set. _mcount(), which is declared in a machine-dependent way
  425. * with _MCOUNT_DECL, does the actual work and is either inlined into a
  426. * C routine or called by an assembly stub. In any case, this magic is
  427. * taken care of by the MCOUNT definition in <machine/profile.h>.
  428. *
  429. * _mcount updates data structures that represent traversals of the
  430. * program's call graph edges. frompc and selfpc are the return
  431. * address and function address that represents the given call graph edge.
  432. *
  433. * Note: the original BSD code used the same variable (frompcindex) for
  434. * both frompcindex and frompc. Any reasonable, modern compiler will
  435. * perform this optimization.
  436. */
  437. _MCOUNT_DECL(frompc, selfpc) /* _mcount; may be static, inline, etc */
  438. {
  439. register ARCINDEX *frompcindex;
  440. register struct tostruct *top, *prevtop;
  441. register struct gmonparam *p;
  442. register ARCINDEX toindex;
  443. int i;
  444. p = &_gmonparam;
  445. /*
  446. * check that we are profiling
  447. * and that we aren't recursively invoked.
  448. */
  449. if (! compare_and_swap (&p->state, GMON_PROF_ON, GMON_PROF_BUSY))
  450. return;
  451. /*
  452. * check that frompcindex is a reasonable pc value.
  453. * for example: signal catchers get called from the stack,
  454. * not from text space. too bad.
  455. */
  456. frompc -= p->lowpc;
  457. if (frompc > p->textsize)
  458. goto done;
  459. /* The following test used to be
  460. if (p->log_hashfraction >= 0)
  461. But we can simplify this if we assume the profiling data
  462. is always initialized by the functions in gmon.c. But
  463. then it is possible to avoid a runtime check and use the
  464. smae `if' as in gmon.c. So keep these tests in sync. */
  465. if ((HASHFRACTION & (HASHFRACTION - 1)) == 0) {
  466. /* avoid integer divide if possible: */
  467. i = frompc >> p->log_hashfraction;
  468. } else {
  469. i = frompc / (p->hashfraction * sizeof(*p->froms));
  470. }
  471. frompcindex = &p->froms[i];
  472. toindex = *frompcindex;
  473. if (toindex == 0) {
  474. /*
  475. * first time traversing this arc
  476. */
  477. toindex = ++p->tos[0].link;
  478. if (toindex >= p->tolimit)
  479. /* halt further profiling */
  480. goto overflow;
  481. *frompcindex = toindex;
  482. top = &p->tos[toindex];
  483. top->selfpc = selfpc;
  484. top->count = 1;
  485. top->link = 0;
  486. goto done;
  487. }
  488. top = &p->tos[toindex];
  489. if (top->selfpc == selfpc) {
  490. /*
  491. * arc at front of chain; usual case.
  492. */
  493. top->count++;
  494. goto done;
  495. }
  496. /*
  497. * have to go looking down chain for it.
  498. * top points to what we are looking at,
  499. * prevtop points to previous top.
  500. * we know it is not at the head of the chain.
  501. */
  502. for (; /* goto done */; ) {
  503. if (top->link == 0) {
  504. /*
  505. * top is end of the chain and none of the chain
  506. * had top->selfpc == selfpc.
  507. * so we allocate a new tostruct
  508. * and link it to the head of the chain.
  509. */
  510. toindex = ++p->tos[0].link;
  511. if (toindex >= p->tolimit)
  512. goto overflow;
  513. top = &p->tos[toindex];
  514. top->selfpc = selfpc;
  515. top->count = 1;
  516. top->link = *frompcindex;
  517. *frompcindex = toindex;
  518. goto done;
  519. }
  520. /*
  521. * otherwise, check the next arc on the chain.
  522. */
  523. prevtop = top;
  524. top = &p->tos[top->link];
  525. if (top->selfpc == selfpc) {
  526. /*
  527. * there it is.
  528. * increment its count
  529. * move it to the head of the chain.
  530. */
  531. top->count++;
  532. toindex = prevtop->link;
  533. prevtop->link = top->link;
  534. top->link = *frompcindex;
  535. *frompcindex = toindex;
  536. goto done;
  537. }
  538. }
  539. done:
  540. p->state = GMON_PROF_ON;
  541. return;
  542. overflow:
  543. p->state = GMON_PROF_ERROR;
  544. return;
  545. }
  546. /*
  547. * Actual definition of mcount function. Defined in <machine/profile.h>,
  548. * which is included by <sys/gmon.h>.
  549. */
  550. MCOUNT
  551. #endif