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
19const int kSize = 128;
20const int kRepeat = 2*1024*1024;
21
22void noinstr(void *p) {}
23
24template<typename T, void(*__tsan_mop)(void *p)>
25static 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
35TEST(DISABLED_BENCH, Mop1) {
36  Benchmark<uint8_t, noinstr>();
37}
38
39TEST(DISABLED_BENCH, Mop1Read) {
40  Benchmark<uint8_t, __tsan_read1>();
41}
42
43TEST(DISABLED_BENCH, Mop1Write) {
44  Benchmark<uint8_t, __tsan_write1>();
45}
46
47TEST(DISABLED_BENCH, Mop2) {
48  Benchmark<uint16_t, noinstr>();
49}
50
51TEST(DISABLED_BENCH, Mop2Read) {
52  Benchmark<uint16_t, __tsan_read2>();
53}
54
55TEST(DISABLED_BENCH, Mop2Write) {
56  Benchmark<uint16_t, __tsan_write2>();
57}
58
59TEST(DISABLED_BENCH, Mop4) {
60  Benchmark<uint32_t, noinstr>();
61}
62
63TEST(DISABLED_BENCH, Mop4Read) {
64  Benchmark<uint32_t, __tsan_read4>();
65}
66
67TEST(DISABLED_BENCH, Mop4Write) {
68  Benchmark<uint32_t, __tsan_write4>();
69}
70
71TEST(DISABLED_BENCH, Mop8) {
72  Benchmark<uint8_t, noinstr>();
73}
74
75TEST(DISABLED_BENCH, Mop8Read) {
76  Benchmark<uint64_t, __tsan_read8>();
77}
78
79TEST(DISABLED_BENCH, Mop8Write) {
80  Benchmark<uint64_t, __tsan_write8>();
81}
82
83TEST(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
92TEST(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