gmon.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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 = { state: 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, from_len;
  195. u_long frompc;
  196. struct iovec iov[2 * NARCS_PER_WRITEV];
  197. int nfilled;
  198. for (nfilled = 0; nfilled < NARCS_PER_WRITEV; ++nfilled)
  199. {
  200. iov[2 * nfilled].iov_base = &tag;
  201. iov[2 * nfilled].iov_len = sizeof (tag);
  202. iov[2 * nfilled + 1].iov_base = &raw_arc[nfilled];
  203. iov[2 * nfilled + 1].iov_len = sizeof (struct gmon_cg_arc_record);
  204. }
  205. nfilled = 0;
  206. from_len = _gmonparam.fromssize / sizeof (*_gmonparam.froms);
  207. for (from_index = 0; from_index < from_len; ++from_index)
  208. {
  209. if (_gmonparam.froms[from_index] == 0)
  210. continue;
  211. frompc = _gmonparam.lowpc;
  212. frompc += (from_index * _gmonparam.hashfraction
  213. * sizeof (*_gmonparam.froms));
  214. for (to_index = _gmonparam.froms[from_index];
  215. to_index != 0;
  216. to_index = _gmonparam.tos[to_index].link)
  217. {
  218. struct arc
  219. {
  220. char *frompc;
  221. char *selfpc;
  222. int32_t count;
  223. }
  224. arc;
  225. arc.frompc = (char *) frompc;
  226. arc.selfpc = (char *) _gmonparam.tos[to_index].selfpc;
  227. arc.count = _gmonparam.tos[to_index].count;
  228. memcpy (raw_arc + nfilled, &arc, sizeof (raw_arc [0]));
  229. if (++nfilled == NARCS_PER_WRITEV)
  230. {
  231. writev (fd, iov, 2 * nfilled);
  232. nfilled = 0;
  233. }
  234. }
  235. }
  236. if (nfilled > 0)
  237. writev (fd, iov, 2 * nfilled);
  238. }
  239. static void write_bb_counts (int fd)
  240. {
  241. struct __bb *grp;
  242. u_char tag = GMON_TAG_BB_COUNT;
  243. size_t ncounts;
  244. size_t i;
  245. struct iovec bbhead[2] =
  246. {
  247. { &tag, sizeof (tag) },
  248. { &ncounts, sizeof (ncounts) }
  249. };
  250. struct iovec bbbody[8];
  251. size_t nfilled;
  252. for (i = 0; i < (sizeof (bbbody) / sizeof (bbbody[0])); i += 2)
  253. {
  254. bbbody[i].iov_len = sizeof (grp->addresses[0]);
  255. bbbody[i + 1].iov_len = sizeof (grp->counts[0]);
  256. }
  257. /* Write each group of basic-block info (all basic-blocks in a
  258. compilation unit form a single group). */
  259. for (grp = __bb_head; grp; grp = grp->next)
  260. {
  261. ncounts = grp->ncounts;
  262. writev (fd, bbhead, 2);
  263. for (nfilled = i = 0; i < ncounts; ++i)
  264. {
  265. if (nfilled > (sizeof (bbbody) / sizeof (bbbody[0])) - 2)
  266. {
  267. writev (fd, bbbody, nfilled);
  268. nfilled = 0;
  269. }
  270. bbbody[nfilled++].iov_base = (char *) &grp->addresses[i];
  271. bbbody[nfilled++].iov_base = &grp->counts[i];
  272. }
  273. if (nfilled > 0)
  274. writev (fd, bbbody, nfilled);
  275. }
  276. }
  277. static void write_gmon (void)
  278. {
  279. struct gmon_hdr ghdr __attribute__ ((aligned (__alignof__ (int))));
  280. int fd = -1;
  281. char *env;
  282. #ifndef O_NOFOLLOW
  283. # define O_NOFOLLOW 0
  284. #endif
  285. env = getenv ("GMON_OUT_PREFIX");
  286. if (env != NULL
  287. #if 0
  288. && !__libc_enable_secure
  289. #endif
  290. )
  291. {
  292. size_t len = strlen (env);
  293. char buf[len + 20];
  294. sprintf (buf, "%s.%u", env, getpid ());
  295. fd = open (buf, O_CREAT|O_TRUNC|O_WRONLY|O_NOFOLLOW, 0666);
  296. }
  297. if (fd == -1)
  298. {
  299. fd = open ("gmon.out", O_CREAT|O_TRUNC|O_WRONLY|O_NOFOLLOW, 0666);
  300. if (fd < 0)
  301. {
  302. char buf[300];
  303. int errnum = errno;
  304. fprintf (stderr, "_mcleanup: gmon.out: %s\n",
  305. strerror_r (errnum, buf, sizeof buf));
  306. return;
  307. }
  308. }
  309. /* write gmon.out header: */
  310. memset (&ghdr, '\0', sizeof (struct gmon_hdr));
  311. memcpy (&ghdr.cookie[0], GMON_MAGIC, sizeof (ghdr.cookie));
  312. *(int32_t *) ghdr.version = GMON_VERSION;
  313. write (fd, &ghdr, sizeof (struct gmon_hdr));
  314. /* write PC histogram: */
  315. write_hist (fd);
  316. /* write call-graph: */
  317. write_call_graph (fd);
  318. /* write basic-block execution counts: */
  319. write_bb_counts (fd);
  320. close (fd);
  321. }
  322. void write_profiling (void)
  323. {
  324. int save = _gmonparam.state;
  325. _gmonparam.state = GMON_PROF_OFF;
  326. if (save == GMON_PROF_ON)
  327. write_gmon ();
  328. _gmonparam.state = save;
  329. }
  330. void _mcleanup (void)
  331. {
  332. moncontrol (0);
  333. if (_gmonparam.state != GMON_PROF_ERROR)
  334. write_gmon ();
  335. /* free the memory. */
  336. if (_gmonparam.tos != NULL)
  337. free (_gmonparam.tos);
  338. }
  339. #ifndef SIGPROF
  340. /* Enable statistical profiling, writing samples of the PC into at most
  341. SIZE bytes of SAMPLE_BUFFER; every processor clock tick while profiling
  342. is enabled, the system examines the user PC and increments
  343. SAMPLE_BUFFER[((PC - OFFSET) / 2) * SCALE / 65536]. If SCALE is zero,
  344. disable profiling. Returns zero on success, -1 on error. */
  345. int profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
  346. {
  347. if (scale == 0)
  348. /* Disable profiling. */
  349. return 0;
  350. __set_errno (ENOSYS);
  351. return -1;
  352. }
  353. #else
  354. static u_short *samples;
  355. static size_t nsamples;
  356. static size_t pc_offset;
  357. static u_int pc_scale;
  358. static inline void profil_count (void *pc)
  359. {
  360. size_t i = (pc - pc_offset - (void *) 0) / 2;
  361. if (sizeof (unsigned long long int) > sizeof (size_t))
  362. i = (unsigned long long int) i * pc_scale / 65536;
  363. else
  364. i = i / 65536 * pc_scale + i % 65536 * pc_scale / 65536;
  365. if (i < nsamples)
  366. ++samples[i];
  367. }
  368. /* Get the machine-dependent definition of `profil_counter', the signal
  369. handler for SIGPROF. It calls `profil_count' (above) with the PC of the
  370. interrupted code. */
  371. #include <bits/profil-counter.h>
  372. /* Enable statistical profiling, writing samples of the PC into at most
  373. SIZE bytes of SAMPLE_BUFFER; every processor clock tick while profiling
  374. is enabled, the system examines the user PC and increments
  375. SAMPLE_BUFFER[((PC - OFFSET) / 2) * SCALE / 65536]. If SCALE is zero,
  376. disable profiling. Returns zero on success, -1 on error. */
  377. int profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
  378. {
  379. static struct sigaction oact;
  380. static struct itimerval otimer;
  381. struct sigaction act;
  382. struct itimerval timer;
  383. if (sample_buffer == NULL)
  384. {
  385. /* Disable profiling. */
  386. if (samples == NULL)
  387. /* Wasn't turned on. */
  388. return 0;
  389. if (setitimer (ITIMER_PROF, &otimer, NULL) < 0)
  390. return -1;
  391. samples = NULL;
  392. return sigaction (SIGPROF, &oact, NULL);
  393. }
  394. if (samples)
  395. {
  396. /* Was already turned on. Restore old timer and signal handler
  397. first. */
  398. if (setitimer (ITIMER_PROF, &otimer, NULL) < 0
  399. || sigaction (SIGPROF, &oact, NULL) < 0)
  400. return -1;
  401. }
  402. samples = sample_buffer;
  403. nsamples = size / sizeof *samples;
  404. pc_offset = offset;
  405. pc_scale = scale;
  406. act.sa_handler = (__sighandler_t) &profil_counter;
  407. act.sa_flags = SA_RESTART;
  408. __sigfillset (&act.sa_mask);
  409. if (sigaction (SIGPROF, &act, &oact) < 0)
  410. return -1;
  411. timer.it_value.tv_sec = 0;
  412. timer.it_value.tv_usec = 1;
  413. timer.it_interval = timer.it_value;
  414. return setitimer (ITIMER_PROF, &timer, &otimer);
  415. }
  416. #endif
  417. /* This file provides the machine-dependent definitions of the _MCOUNT_DECL
  418. and MCOUNT macros. */
  419. #include <bits/machine-gmon.h>
  420. #include <bits/atomicity.h>
  421. /*
  422. * mcount is called on entry to each function compiled with the profiling
  423. * switch set. _mcount(), which is declared in a machine-dependent way
  424. * with _MCOUNT_DECL, does the actual work and is either inlined into a
  425. * C routine or called by an assembly stub. In any case, this magic is
  426. * taken care of by the MCOUNT definition in <machine/profile.h>.
  427. *
  428. * _mcount updates data structures that represent traversals of the
  429. * program's call graph edges. frompc and selfpc are the return
  430. * address and function address that represents the given call graph edge.
  431. *
  432. * Note: the original BSD code used the same variable (frompcindex) for
  433. * both frompcindex and frompc. Any reasonable, modern compiler will
  434. * perform this optimization.
  435. */
  436. _MCOUNT_DECL(frompc, selfpc) /* _mcount; may be static, inline, etc */
  437. {
  438. register ARCINDEX *frompcindex;
  439. register struct tostruct *top, *prevtop;
  440. register struct gmonparam *p;
  441. register ARCINDEX toindex;
  442. int i;
  443. p = &_gmonparam;
  444. /*
  445. * check that we are profiling
  446. * and that we aren't recursively invoked.
  447. */
  448. if (! compare_and_swap (&p->state, GMON_PROF_ON, GMON_PROF_BUSY))
  449. return;
  450. /*
  451. * check that frompcindex is a reasonable pc value.
  452. * for example: signal catchers get called from the stack,
  453. * not from text space. too bad.
  454. */
  455. frompc -= p->lowpc;
  456. if (frompc > p->textsize)
  457. goto done;
  458. /* The following test used to be
  459. if (p->log_hashfraction >= 0)
  460. But we can simplify this if we assume the profiling data
  461. is always initialized by the functions in gmon.c. But
  462. then it is possible to avoid a runtime check and use the
  463. smae `if' as in gmon.c. So keep these tests in sync. */
  464. if ((HASHFRACTION & (HASHFRACTION - 1)) == 0) {
  465. /* avoid integer divide if possible: */
  466. i = frompc >> p->log_hashfraction;
  467. } else {
  468. i = frompc / (p->hashfraction * sizeof(*p->froms));
  469. }
  470. frompcindex = &p->froms[i];
  471. toindex = *frompcindex;
  472. if (toindex == 0) {
  473. /*
  474. * first time traversing this arc
  475. */
  476. toindex = ++p->tos[0].link;
  477. if (toindex >= p->tolimit)
  478. /* halt further profiling */
  479. goto overflow;
  480. *frompcindex = toindex;
  481. top = &p->tos[toindex];
  482. top->selfpc = selfpc;
  483. top->count = 1;
  484. top->link = 0;
  485. goto done;
  486. }
  487. top = &p->tos[toindex];
  488. if (top->selfpc == selfpc) {
  489. /*
  490. * arc at front of chain; usual case.
  491. */
  492. top->count++;
  493. goto done;
  494. }
  495. /*
  496. * have to go looking down chain for it.
  497. * top points to what we are looking at,
  498. * prevtop points to previous top.
  499. * we know it is not at the head of the chain.
  500. */
  501. for (; /* goto done */; ) {
  502. if (top->link == 0) {
  503. /*
  504. * top is end of the chain and none of the chain
  505. * had top->selfpc == selfpc.
  506. * so we allocate a new tostruct
  507. * and link it to the head of the chain.
  508. */
  509. toindex = ++p->tos[0].link;
  510. if (toindex >= p->tolimit)
  511. goto overflow;
  512. top = &p->tos[toindex];
  513. top->selfpc = selfpc;
  514. top->count = 1;
  515. top->link = *frompcindex;
  516. *frompcindex = toindex;
  517. goto done;
  518. }
  519. /*
  520. * otherwise, check the next arc on the chain.
  521. */
  522. prevtop = top;
  523. top = &p->tos[top->link];
  524. if (top->selfpc == selfpc) {
  525. /*
  526. * there it is.
  527. * increment its count
  528. * move it to the head of the chain.
  529. */
  530. top->count++;
  531. toindex = prevtop->link;
  532. prevtop->link = top->link;
  533. top->link = *frompcindex;
  534. *frompcindex = toindex;
  535. goto done;
  536. }
  537. }
  538. done:
  539. p->state = GMON_PROF_ON;
  540. return;
  541. overflow:
  542. p->state = GMON_PROF_ERROR;
  543. return;
  544. }
  545. /*
  546. * Actual definition of mcount function. Defined in <machine/profile.h>,
  547. * which is included by <sys/gmon.h>.
  548. */
  549. MCOUNT
  550. #endif