]>
git.proxmox.com Git - rustc.git/blob - src/jemalloc/msvc/projects/vc2015/test_threads/test_threads.cpp
1 // jemalloc C++ threaded test
2 // Author: Rustam Abdullaev
12 #include <jemalloc/jemalloc.h>
16 using std::uniform_int_distribution
;
17 using std::minstd_rand
;
21 je_malloc_conf
= "narenas:3";
23 size_t sz
= sizeof(narenas
);
24 je_mallctl("opt.narenas", &narenas
, &sz
, NULL
, 0);
26 printf("Error: unexpected number of arenas: %d\n", narenas
);
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
);
35 size_t sz1
= sizeof(allocated1
);
36 je_mallctl("stats.active", &allocated1
, &sz1
, NULL
, 0);
37 printf("\nPress Enter to start threads...\n");
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
) {
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
];
54 ptrs
[j
] = (uint8_t*)je_malloc(sz
);
56 printf("Unable to allocate %d bytes in thread %d, iter %d, alloc %d. %d\n", sz
, tid
, i
, j
, x
);
59 for (int k
= 0; k
< sz
; k
++)
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
));
76 for (thread
& t
: workers
) {
79 je_malloc_stats_print(NULL
, NULL
, NULL
);
81 je_mallctl("stats.active", &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");
88 return failed
? 1 : 0;