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