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