test_threads.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // jemalloc C++ threaded test
  2. // Author: Rustam Abdullaev
  3. // Public Domain
  4. #include <atomic>
  5. #include <functional>
  6. #include <future>
  7. #include <random>
  8. #include <thread>
  9. #include <vector>
  10. #include <stdio.h>
  11. #define JEMALLOC_NO_DEMANGLE
  12. #include <jemalloc/jemalloc.h>
  13. using std::vector;
  14. using std::thread;
  15. using std::uniform_int_distribution;
  16. using std::minstd_rand;
  17. int test_threads() {
  18. je_malloc_conf = "narenas:3";
  19. int narenas = 0;
  20. size_t sz = sizeof(narenas);
  21. je_mallctl("opt.narenas", (void *)&narenas, &sz, NULL, 0);
  22. if (narenas != 3) {
  23. printf("Error: unexpected number of arenas: %d\n", narenas);
  24. return 1;
  25. }
  26. static const int sizes[] = { 7, 16, 32, 60, 91, 100, 120, 144, 169, 199, 255, 400, 670, 900, 917, 1025, 3333, 5190, 13131, 49192, 99999, 123123, 255265, 2333111 };
  27. static const int numSizes = (int)(sizeof(sizes) / sizeof(sizes[0]));
  28. vector<thread> workers;
  29. static const int numThreads = narenas + 1, numAllocsMax = 25, numIter1 = 50, numIter2 = 50;
  30. je_malloc_stats_print(NULL, NULL, NULL);
  31. size_t allocated1;
  32. size_t sz1 = sizeof(allocated1);
  33. je_mallctl("stats.active", (void *)&allocated1, &sz1, NULL, 0);
  34. printf("\nPress Enter to start threads...\n");
  35. getchar();
  36. printf("Starting %d threads x %d x %d iterations...\n", numThreads, numIter1, numIter2);
  37. for (int i = 0; i < numThreads; i++) {
  38. workers.emplace_back([tid=i]() {
  39. uniform_int_distribution<int> sizeDist(0, numSizes - 1);
  40. minstd_rand rnd(tid * 17);
  41. uint8_t* ptrs[numAllocsMax];
  42. int ptrsz[numAllocsMax];
  43. for (int i = 0; i < numIter1; ++i) {
  44. thread t([&]() {
  45. for (int i = 0; i < numIter2; ++i) {
  46. const int numAllocs = numAllocsMax - sizeDist(rnd);
  47. for (int j = 0; j < numAllocs; j += 64) {
  48. const int x = sizeDist(rnd);
  49. const int sz = sizes[x];
  50. ptrsz[j] = sz;
  51. ptrs[j] = (uint8_t*)je_malloc(sz);
  52. if (!ptrs[j]) {
  53. printf("Unable to allocate %d bytes in thread %d, iter %d, alloc %d. %d\n", sz, tid, i, j, x);
  54. exit(1);
  55. }
  56. for (int k = 0; k < sz; k++)
  57. ptrs[j][k] = tid + k;
  58. }
  59. for (int j = 0; j < numAllocs; j += 64) {
  60. for (int k = 0, sz = ptrsz[j]; k < sz; k++)
  61. if (ptrs[j][k] != (uint8_t)(tid + k)) {
  62. printf("Memory error in thread %d, iter %d, alloc %d @ %d : %02X!=%02X\n", tid, i, j, k, ptrs[j][k], (uint8_t)(tid + k));
  63. exit(1);
  64. }
  65. je_free(ptrs[j]);
  66. }
  67. }
  68. });
  69. t.join();
  70. }
  71. });
  72. }
  73. for (thread& t : workers) {
  74. t.join();
  75. }
  76. je_malloc_stats_print(NULL, NULL, NULL);
  77. size_t allocated2;
  78. je_mallctl("stats.active", (void *)&allocated2, &sz1, NULL, 0);
  79. size_t leaked = allocated2 - allocated1;
  80. printf("\nDone. Leaked: %zd bytes\n", leaked);
  81. bool failed = leaked > 65536; // in case C++ runtime allocated something (e.g. iostream locale or facet)
  82. printf("\nTest %s!\n", (failed ? "FAILED" : "successful"));
  83. printf("\nPress Enter to continue...\n");
  84. getchar();
  85. return failed ? 1 : 0;
  86. }