summaryrefslogtreecommitdiff
path: root/lib/tsan/benchmarks/mini_bench_local.cc
diff options
context:
space:
mode:
authorKostya Serebryany <kcc@google.com>2012-05-10 14:18:22 +0000
committerKostya Serebryany <kcc@google.com>2012-05-10 14:18:22 +0000
commitda4edd850db1a333c15fc3b0abc01a2e8d2f08fe (patch)
tree365b956c9a1a3741b85d813df65ab50e9f6fd34b /lib/tsan/benchmarks/mini_bench_local.cc
parentd40895d1305d630a5bfeb103fdbbf2a4d955b404 (diff)
[tsan] ThreadSanitizer tests and micro benchmarks. No makefiles yet.
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@156545 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/tsan/benchmarks/mini_bench_local.cc')
-rw-r--r--lib/tsan/benchmarks/mini_bench_local.cc49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/tsan/benchmarks/mini_bench_local.cc b/lib/tsan/benchmarks/mini_bench_local.cc
new file mode 100644
index 000000000..accdcb638
--- /dev/null
+++ b/lib/tsan/benchmarks/mini_bench_local.cc
@@ -0,0 +1,49 @@
+// Mini-benchmark for tsan: non-shared memory writes.
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+
+int len;
+int *a;
+const int kNumIter = 1000;
+
+__attribute__((noinline))
+void Run(int idx) {
+ for (int i = 0, n = len; i < n; i++)
+ a[i + idx * n] = i;
+}
+
+void *Thread(void *arg) {
+ long idx = (long)arg;
+ printf("Thread %ld started\n", idx);
+ for (int i = 0; i < kNumIter; i++)
+ Run(idx);
+ printf("Thread %ld done\n", idx);
+ return 0;
+}
+
+int main(int argc, char **argv) {
+ int n_threads = 0;
+ if (argc != 3) {
+ n_threads = 4;
+ len = 1000000;
+ } else {
+ n_threads = atoi(argv[1]);
+ assert(n_threads > 0 && n_threads <= 32);
+ len = atoi(argv[2]);
+ }
+ printf("%s: n_threads=%d len=%d iter=%d\n",
+ __FILE__, n_threads, len, kNumIter);
+ a = new int[n_threads * len];
+ pthread_t *t = new pthread_t[n_threads];
+ for (int i = 0; i < n_threads; i++) {
+ pthread_create(&t[i], 0, Thread, (void*)i);
+ }
+ for (int i = 0; i < n_threads; i++) {
+ pthread_join(t[i], 0);
+ }
+ delete [] t;
+ delete [] a;
+ return 0;
+}