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