prof_accum.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "test/jemalloc_test.h"
  2. #define NTHREADS 4
  3. #define NALLOCS_PER_THREAD 50
  4. #define DUMP_INTERVAL 1
  5. #define BT_COUNT_CHECK_INTERVAL 5
  6. #ifdef JEMALLOC_PROF
  7. const char *malloc_conf =
  8. "prof:true,prof_accum:true,prof_active:false,lg_prof_sample:0";
  9. #endif
  10. static int
  11. prof_dump_open_intercept(bool propagate_err, const char *filename)
  12. {
  13. int fd;
  14. fd = open("/dev/null", O_WRONLY);
  15. assert_d_ne(fd, -1, "Unexpected open() failure");
  16. return (fd);
  17. }
  18. static void *
  19. alloc_from_permuted_backtrace(unsigned thd_ind, unsigned iteration)
  20. {
  21. return (btalloc(1, thd_ind*NALLOCS_PER_THREAD + iteration));
  22. }
  23. static void *
  24. thd_start(void *varg)
  25. {
  26. unsigned thd_ind = *(unsigned *)varg;
  27. size_t bt_count_prev, bt_count;
  28. unsigned i_prev, i;
  29. i_prev = 0;
  30. bt_count_prev = 0;
  31. for (i = 0; i < NALLOCS_PER_THREAD; i++) {
  32. void *p = alloc_from_permuted_backtrace(thd_ind, i);
  33. dallocx(p, 0);
  34. if (i % DUMP_INTERVAL == 0) {
  35. assert_d_eq(mallctl("prof.dump", NULL, NULL, NULL, 0),
  36. 0, "Unexpected error while dumping heap profile");
  37. }
  38. if (i % BT_COUNT_CHECK_INTERVAL == 0 ||
  39. i+1 == NALLOCS_PER_THREAD) {
  40. bt_count = prof_bt_count();
  41. assert_zu_le(bt_count_prev+(i-i_prev), bt_count,
  42. "Expected larger backtrace count increase");
  43. i_prev = i;
  44. bt_count_prev = bt_count;
  45. }
  46. }
  47. return (NULL);
  48. }
  49. TEST_BEGIN(test_idump)
  50. {
  51. bool active;
  52. thd_t thds[NTHREADS];
  53. unsigned thd_args[NTHREADS];
  54. unsigned i;
  55. test_skip_if(!config_prof);
  56. active = true;
  57. assert_d_eq(mallctl("prof.active", NULL, NULL, &active, sizeof(active)),
  58. 0, "Unexpected mallctl failure while activating profiling");
  59. prof_dump_open = prof_dump_open_intercept;
  60. for (i = 0; i < NTHREADS; i++) {
  61. thd_args[i] = i;
  62. thd_create(&thds[i], thd_start, (void *)&thd_args[i]);
  63. }
  64. for (i = 0; i < NTHREADS; i++)
  65. thd_join(thds[i], NULL);
  66. }
  67. TEST_END
  68. int
  69. main(void)
  70. {
  71. return (test(
  72. test_idump));
  73. }