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