Skip to content

Commit e270a8f

Browse files
rustyxjasone
authored andcommitted
Make test_threads more generic
1 parent e025c51 commit e270a8f

File tree

1 file changed

+55
-66
lines changed

1 file changed

+55
-66
lines changed

msvc/projects/vc2015/test_threads/test_threads.cpp

Lines changed: 55 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -10,88 +10,77 @@
1010
#include <vector>
1111
#include <stdio.h>
1212
#include <jemalloc/jemalloc.h>
13-
#include <windows.h>
1413

1514
using std::vector;
1615
using std::thread;
1716
using std::uniform_int_distribution;
1817
using std::minstd_rand;
1918

20-
#if NDEBUG && JEMALLOC_ISSUE_318_WORKAROUND
21-
extern "C" JEMALLOC_EXPORT void _malloc_thread_cleanup(void);
22-
23-
static thread_local struct JeMallocThreadHelper {
24-
~JeMallocThreadHelper() {
25-
_malloc_thread_cleanup();
26-
}
27-
} tls_jemallocThreadHelper;
28-
#endif
29-
3019
int test_threads()
3120
{
32-
je_malloc_conf = "narenas:3";
33-
int narenas = 0;
34-
size_t sz = sizeof(narenas);
35-
je_mallctl("opt.narenas", &narenas, &sz, NULL, 0);
36-
if (narenas != 3) {
37-
printf("Error: unexpected number of arenas: %d\n", narenas);
38-
return 1;
39-
}
40-
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 };
41-
static const int numSizes = (int)(sizeof(sizes) / sizeof(sizes[0]));
42-
vector<thread> workers;
43-
static const int numThreads = narenas + 1, numAllocsMax = 25, numIter1 = 50, numIter2 = 50;
44-
je_malloc_stats_print(NULL, NULL, NULL);
21+
je_malloc_conf = "narenas:3";
22+
int narenas = 0;
23+
size_t sz = sizeof(narenas);
24+
je_mallctl("opt.narenas", &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);
4534
size_t allocated1;
4635
size_t sz1 = sizeof(allocated1);
4736
je_mallctl("stats.active", &allocated1, &sz1, NULL, 0);
4837
printf("\nPress Enter to start threads...\n");
49-
getchar();
50-
printf("Starting %d threads x %d x %d iterations...\n", numThreads, numIter1, numIter2);
51-
for (int i = 0; i < numThreads; i++) {
52-
workers.emplace_back([tid=i]() {
53-
uniform_int_distribution<int> sizeDist(0, numSizes - 1);
54-
minstd_rand rnd(tid * 17);
55-
uint8_t* ptrs[numAllocsMax];
56-
int ptrsz[numAllocsMax];
57-
for (int i = 0; i < numIter1; ++i) {
58-
thread t([&]() {
59-
for (int i = 0; i < numIter2; ++i) {
60-
const int numAllocs = numAllocsMax - sizeDist(rnd);
61-
for (int j = 0; j < numAllocs; j += 64) {
62-
const int x = sizeDist(rnd);
63-
const int sz = sizes[x];
64-
ptrsz[j] = sz;
65-
ptrs[j] = (uint8_t*)je_malloc(sz);
66-
if (!ptrs[j]) {
67-
printf("Unable to allocate %d bytes in thread %d, iter %d, alloc %d. %d\n", sz, tid, i, j, x);
68-
exit(1);
69-
}
70-
for (int k = 0; k < sz; k++)
71-
ptrs[j][k] = tid + k;
72-
}
73-
for (int j = 0; j < numAllocs; j += 64) {
74-
for (int k = 0, sz = ptrsz[j]; k < sz; k++)
75-
if (ptrs[j][k] != (uint8_t)(tid + k)) {
76-
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));
77-
exit(1);
78-
}
79-
je_free(ptrs[j]);
80-
}
81-
}
82-
});
83-
t.join();
84-
}
85-
});
86-
}
87-
for (thread& t : workers) {
88-
t.join();
89-
}
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+
}
9079
je_malloc_stats_print(NULL, NULL, NULL);
9180
size_t allocated2;
9281
je_mallctl("stats.active", &allocated2, &sz1, NULL, 0);
9382
size_t leaked = allocated2 - allocated1;
94-
printf("\nDone. Leaked: %Id bytes\n", leaked);
83+
printf("\nDone. Leaked: %zd bytes\n", leaked);
9584
bool failed = leaked > 65536; // in case C++ runtime allocated something (e.g. iostream locale or facet)
9685
printf("\nTest %s!\n", (failed ? "FAILED" : "successful"));
9786
printf("\nPress Enter to continue...\n");

0 commit comments

Comments
 (0)