1 // Synthetic benchmark for __tsan_read/write{1,2,4,8}.
2 // As compared to mini_bench_local/shared.cc this benchmark passes through
3 // deduplication logic (ContainsSameAccess).
4 // First argument is access size (1, 2, 4, 8). Second optional arg switches
5 // from writes to reads.
11 #include <linux/futex.h>
12 #include <sys/syscall.h>
15 template<typename T
, bool write
>
16 void* thread(void *arg
) {
17 const int kSize
= 2 << 10;
18 static volatile long data
[kSize
];
19 static volatile long turn
;
20 const int kRepeat
= 1 << 17;
22 for (int i
= 0; i
< kRepeat
; i
++) {
24 int t
= __atomic_load_n(&turn
, __ATOMIC_ACQUIRE
);
27 syscall(SYS_futex
, &turn
, FUTEX_WAIT
, t
, 0, 0, 0);
29 for (int j
= 0; j
< kSize
; j
++) {
31 ((volatile T
*)&data
[j
])[0] = 1;
32 ((volatile T
*)&data
[j
])[sizeof(T
) == 8 ? 0 : 1] = 1;
34 T v0
= ((volatile T
*)&data
[j
])[0];
35 T v1
= ((volatile T
*)&data
[j
])[sizeof(T
) == 8 ? 0 : 1];
40 __atomic_store_n(&turn
, 1 - id
, __ATOMIC_RELEASE
);
41 syscall(SYS_futex
, &turn
, FUTEX_WAKE
, 0, 0, 0, 0);
46 template<typename T
, bool write
>
49 pthread_create(&th
, 0, thread
<T
, write
>, (void*)1);
55 void testw(int size
) {
57 case 1: return test
<char, write
>();
58 case 2: return test
<short, write
>();
59 case 4: return test
<int, write
>();
60 case 8: return test
<long long, write
>();
64 int main(int argc
, char** argv
) {
69 if (size
!= 1 && size
!= 2 && size
!= 4 && size
!= 8)
74 printf("%s%d\n", write
? "write" : "read", size
);