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