Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / scudo / threads.c
blobf4637a77baad62e2657603cb108ff8e305a8c31a
1 // RUN: %clang_scudo %s -o %t
2 // RUN: %env_scudo_opts="QuarantineSizeKb=0:ThreadLocalQuarantineSizeKb=0" %run %t 5 1000000 2>&1
3 // RUN: %env_scudo_opts="QuarantineSizeKb=1024:ThreadLocalQuarantineSizeKb=64" %run %t 5 1000000 2>&1
5 // Tests parallel allocations and deallocations of memory chunks from a number
6 // of concurrent threads, with and without quarantine.
7 // This test passes if everything executes properly without crashing.
9 #include <assert.h>
10 #include <pthread.h>
11 #include <stdio.h>
12 #include <stdlib.h>
14 #include <sanitizer/allocator_interface.h>
16 int num_threads;
17 int total_num_alloc;
18 const int kMaxNumThreads = 500;
19 pthread_t tid[kMaxNumThreads];
21 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
22 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
23 char go = 0;
25 void *thread_fun(void *arg) {
26 pthread_mutex_lock(&mutex);
27 while (!go)
28 pthread_cond_wait(&cond, &mutex);
29 pthread_mutex_unlock(&mutex);
30 for (int i = 0; i < total_num_alloc / num_threads; i++) {
31 void *p = malloc(10);
32 __asm__ __volatile__(""
34 : "r"(p)
35 : "memory");
36 free(p);
38 return 0;
41 int main(int argc, char **argv) {
42 assert(argc == 3);
43 num_threads = atoi(argv[1]);
44 assert(num_threads > 0);
45 assert(num_threads <= kMaxNumThreads);
46 total_num_alloc = atoi(argv[2]);
47 assert(total_num_alloc > 0);
49 printf("%d threads, %d allocations in each\n", num_threads,
50 total_num_alloc / num_threads);
51 fprintf(stderr, "Heap size before: %zd\n", __sanitizer_get_heap_size());
52 fprintf(stderr, "Allocated bytes before: %zd\n",
53 __sanitizer_get_current_allocated_bytes());
55 for (int i = 0; i < num_threads; i++)
56 pthread_create(&tid[i], 0, thread_fun, 0);
57 pthread_mutex_lock(&mutex);
58 go = 1;
59 pthread_cond_broadcast(&cond);
60 pthread_mutex_unlock(&mutex);
61 for (int i = 0; i < num_threads; i++)
62 pthread_join(tid[i], 0);
64 fprintf(stderr, "Heap size after: %zd\n", __sanitizer_get_heap_size());
65 fprintf(stderr, "Allocated bytes after: %zd\n",
66 __sanitizer_get_current_allocated_bytes());
68 return 0;