2
0

test_threads.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include <jemalloc/jemalloc.h>
  12. using std::vector;
  13. using std::thread;
  14. using std::uniform_int_distribution;
  15. using std::minstd_rand;
  16. int test_threads() {
  17. je_malloc_conf = "narenas:3";
  18. int narenas = 0;
  19. size_t sz = sizeof(narenas);
  20. je_mallctl("opt.narenas", (void *)&narenas, &sz, NULL, 0);
  21. if (narenas != 3) {
  22. printf("Error: unexpected number of arenas: %d\n", narenas);
  23. return 1;
  24. }
  25. 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 };
  26. static const int numSizes = (int)(sizeof(sizes) / sizeof(sizes[0]));
  27. vector<thread> workers;
  28. static const int numThreads = narenas + 1, numAllocsMax = 25, numIter1 = 50, numIter2 = 50;
  29. je_malloc_stats_print(NULL, NULL, NULL);
  30. size_t allocated1;
  31. size_t sz1 = sizeof(allocated1);
  32. je_mallctl("stats.active", (void *)&allocated1, &sz1, NULL, 0);
  33. printf("\nPress Enter to start threads...\n");
  34. getchar();
  35. printf("Starting %d threads x %d x %d iterations...\n", numThreads, numIter1, numIter2);
  36. for (int i = 0; i < numThreads; i++) {
  37. workers.emplace_back([tid=i]() {
  38. uniform_int_distribution<int> sizeDist(0, numSizes - 1);
  39. minstd_rand rnd(tid * 17);
  40. uint8_t* ptrs[numAllocsMax];
  41. int ptrsz[numAllocsMax];
  42. for (int i = 0; i < numIter1; ++i) {
  43. thread t([&]() {
  44. for (int i = 0; i < numIter2; ++i) {
  45. const int numAllocs = numAllocsMax - sizeDist(rnd);
  46. for (int j = 0; j < numAllocs; j += 64) {
  47. const int x = sizeDist(rnd);
  48. const int sz = sizes[x];
  49. ptrsz[j] = sz;
  50. ptrs[j] = (uint8_t*)je_malloc(sz);
  51. if (!ptrs[j]) {
  52. printf("Unable to allocate %d bytes in thread %d, iter %d, alloc %d. %d\n", sz, tid, i, j, x);
  53. exit(1);
  54. }
  55. for (int k = 0; k < sz; k++)
  56. ptrs[j][k] = tid + k;
  57. }
  58. for (int j = 0; j < numAllocs; j += 64) {
  59. for (int k = 0, sz = ptrsz[j]; k < sz; k++)
  60. if (ptrs[j][k] != (uint8_t)(tid + k)) {
  61. 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));
  62. exit(1);
  63. }
  64. je_free(ptrs[j]);
  65. }
  66. }
  67. });
  68. t.join();
  69. }
  70. });
  71. }
  72. for (thread& t : workers) {
  73. t.join();
  74. }
  75. je_malloc_stats_print(NULL, NULL, NULL);
  76. size_t allocated2;
  77. je_mallctl("stats.active", (void *)&allocated2, &sz1, NULL, 0);
  78. size_t leaked = allocated2 - allocated1;
  79. printf("\nDone. Leaked: %zd bytes\n", leaked);
  80. bool failed = leaked > 65536; // in case C++ runtime allocated something (e.g. iostream locale or facet)
  81. printf("\nTest %s!\n", (failed ? "FAILED" : "successful"));
  82. printf("\nPress Enter to continue...\n");
  83. getchar();
  84. return failed ? 1 : 0;
  85. }