1 //===-- tsan_bench.cc -----------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "tsan_test_util.h"
14 #include "tsan_interface.h"
15 #include "tsan_defs.h"
16 #include "gtest/gtest.h"
17 #include <stdint.h>
18 
19 const int kSize = 128;
20 const int kRepeat = 2*1024*1024;
21 
noinstr(void * p)22 void noinstr(void *p) {}
23 
24 template<typename T, void(*__tsan_mop)(void *p)>
Benchmark()25 static void Benchmark() {
26   volatile T data[kSize];
27   for (int i = 0; i < kRepeat; i++) {
28     for (int j = 0; j < kSize; j++) {
29       __tsan_mop((void*)&data[j]);
30       data[j]++;
31     }
32   }
33 }
34 
TEST(DISABLED_BENCH,Mop1)35 TEST(DISABLED_BENCH, Mop1) {
36   Benchmark<uint8_t, noinstr>();
37 }
38 
TEST(DISABLED_BENCH,Mop1Read)39 TEST(DISABLED_BENCH, Mop1Read) {
40   Benchmark<uint8_t, __tsan_read1>();
41 }
42 
TEST(DISABLED_BENCH,Mop1Write)43 TEST(DISABLED_BENCH, Mop1Write) {
44   Benchmark<uint8_t, __tsan_write1>();
45 }
46 
TEST(DISABLED_BENCH,Mop2)47 TEST(DISABLED_BENCH, Mop2) {
48   Benchmark<uint16_t, noinstr>();
49 }
50 
TEST(DISABLED_BENCH,Mop2Read)51 TEST(DISABLED_BENCH, Mop2Read) {
52   Benchmark<uint16_t, __tsan_read2>();
53 }
54 
TEST(DISABLED_BENCH,Mop2Write)55 TEST(DISABLED_BENCH, Mop2Write) {
56   Benchmark<uint16_t, __tsan_write2>();
57 }
58 
TEST(DISABLED_BENCH,Mop4)59 TEST(DISABLED_BENCH, Mop4) {
60   Benchmark<uint32_t, noinstr>();
61 }
62 
TEST(DISABLED_BENCH,Mop4Read)63 TEST(DISABLED_BENCH, Mop4Read) {
64   Benchmark<uint32_t, __tsan_read4>();
65 }
66 
TEST(DISABLED_BENCH,Mop4Write)67 TEST(DISABLED_BENCH, Mop4Write) {
68   Benchmark<uint32_t, __tsan_write4>();
69 }
70 
TEST(DISABLED_BENCH,Mop8)71 TEST(DISABLED_BENCH, Mop8) {
72   Benchmark<uint8_t, noinstr>();
73 }
74 
TEST(DISABLED_BENCH,Mop8Read)75 TEST(DISABLED_BENCH, Mop8Read) {
76   Benchmark<uint64_t, __tsan_read8>();
77 }
78 
TEST(DISABLED_BENCH,Mop8Write)79 TEST(DISABLED_BENCH, Mop8Write) {
80   Benchmark<uint64_t, __tsan_write8>();
81 }
82 
TEST(DISABLED_BENCH,FuncCall)83 TEST(DISABLED_BENCH, FuncCall) {
84   for (int i = 0; i < kRepeat; i++) {
85     for (int j = 0; j < kSize; j++)
86       __tsan_func_entry((void*)(uintptr_t)j);
87     for (int j = 0; j < kSize; j++)
88       __tsan_func_exit();
89   }
90 }
91 
TEST(DISABLED_BENCH,MutexLocal)92 TEST(DISABLED_BENCH, MutexLocal) {
93   Mutex m;
94   ScopedThread().Create(m);
95   for (int i = 0; i < 50; i++) {
96     ScopedThread t;
97     t.Lock(m);
98     t.Unlock(m);
99   }
100   for (int i = 0; i < 16*1024*1024; i++) {
101     m.Lock();
102     m.Unlock();
103   }
104   ScopedThread().Destroy(m);
105 }
106