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