prof_accum.c 1.8 KB

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