2
0

prof_accum.c 1.8 KB

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