1 //===----------------------------------------------------------------------===//
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 // UNSUPPORTED: c++03, c++11, c++14, c++17
10
11 #include <atomic>
12 #include <cstdint>
13 #include <numeric>
14 #include <stop_token>
15 #include <thread>
16
17 #include "benchmark/benchmark.h"
18 #include "make_test_thread.h"
19
20 using namespace std::chrono_literals;
21
BM_atomic_wait_one_thread_one_atomic_wait(benchmark::State & state)22 void BM_atomic_wait_one_thread_one_atomic_wait(benchmark::State& state) {
23 std::atomic<std::uint64_t> a;
24 auto thread_func = [&](std::stop_token st) {
25 while (!st.stop_requested()) {
26 a.fetch_add(1, std::memory_order_relaxed);
27 a.notify_all();
28 }
29 };
30
31 std::uint64_t total_loop_test_param = state.range(0);
32
33 auto thread = support::make_test_jthread(thread_func);
34
35 for (auto _ : state) {
36 for (std::uint64_t i = 0; i < total_loop_test_param; ++i) {
37 auto old = a.load(std::memory_order_relaxed);
38 a.wait(old);
39 }
40 }
41 }
42 BENCHMARK(BM_atomic_wait_one_thread_one_atomic_wait)->RangeMultiplier(2)->Range(1 << 10, 1 << 24);
43
BM_atomic_wait_multi_thread_one_atomic_wait(benchmark::State & state)44 void BM_atomic_wait_multi_thread_one_atomic_wait(benchmark::State& state) {
45 std::atomic<std::uint64_t> a;
46 auto notify_func = [&](std::stop_token st) {
47 while (!st.stop_requested()) {
48 a.fetch_add(1, std::memory_order_relaxed);
49 a.notify_all();
50 }
51 };
52
53 std::uint64_t total_loop_test_param = state.range(0);
54 constexpr auto num_waiting_threads = 15;
55 std::vector<std::jthread> wait_threads;
56 wait_threads.reserve(num_waiting_threads);
57
58 auto notify_thread = support::make_test_jthread(notify_func);
59
60 std::atomic<std::uint64_t> start_flag = 0;
61 std::atomic<std::uint64_t> done_count = 0;
62 auto wait_func = [&a, &start_flag, &done_count, total_loop_test_param](std::stop_token st) {
63 auto old_start = 0;
64 while (!st.stop_requested()) {
65 start_flag.wait(old_start);
66 old_start = start_flag.load();
67 for (std::uint64_t i = 0; i < total_loop_test_param; ++i) {
68 auto old = a.load(std::memory_order_relaxed);
69 a.wait(old);
70 }
71 done_count.fetch_add(1);
72 }
73 };
74
75 for (size_t i = 0; i < num_waiting_threads; ++i) {
76 wait_threads.emplace_back(support::make_test_jthread(wait_func));
77 }
78
79 for (auto _ : state) {
80 done_count = 0;
81 start_flag.fetch_add(1);
82 start_flag.notify_all();
83 while (done_count < num_waiting_threads) {
84 std::this_thread::yield();
85 }
86 }
87 for (auto& t : wait_threads) {
88 t.request_stop();
89 }
90 start_flag.fetch_add(1);
91 start_flag.notify_all();
92 for (auto& t : wait_threads) {
93 t.join();
94 }
95 }
96 BENCHMARK(BM_atomic_wait_multi_thread_one_atomic_wait)->RangeMultiplier(2)->Range(1 << 10, 1 << 20);
97
BM_atomic_wait_multi_thread_wait_different_atomics(benchmark::State & state)98 void BM_atomic_wait_multi_thread_wait_different_atomics(benchmark::State& state) {
99 const std::uint64_t total_loop_test_param = state.range(0);
100 constexpr std::uint64_t num_atomics = 7;
101 std::vector<std::atomic<std::uint64_t>> atomics(num_atomics);
102
103 auto notify_func = [&](std::stop_token st, size_t idx) {
104 while (!st.stop_requested()) {
105 atomics[idx].fetch_add(1, std::memory_order_relaxed);
106 atomics[idx].notify_all();
107 }
108 };
109
110 std::atomic<std::uint64_t> start_flag = 0;
111 std::atomic<std::uint64_t> done_count = 0;
112
113 auto wait_func = [&, total_loop_test_param](std::stop_token st, size_t idx) {
114 auto old_start = 0;
115 while (!st.stop_requested()) {
116 start_flag.wait(old_start);
117 old_start = start_flag.load();
118 for (std::uint64_t i = 0; i < total_loop_test_param; ++i) {
119 auto old = atomics[idx].load(std::memory_order_relaxed);
120 atomics[idx].wait(old);
121 }
122 done_count.fetch_add(1);
123 }
124 };
125
126 std::vector<std::jthread> notify_threads;
127 notify_threads.reserve(num_atomics);
128
129 std::vector<std::jthread> wait_threads;
130 wait_threads.reserve(num_atomics);
131
132 for (size_t i = 0; i < num_atomics; ++i) {
133 notify_threads.emplace_back(support::make_test_jthread(notify_func, i));
134 }
135
136 for (size_t i = 0; i < num_atomics; ++i) {
137 wait_threads.emplace_back(support::make_test_jthread(wait_func, i));
138 }
139
140 for (auto _ : state) {
141 done_count = 0;
142 start_flag.fetch_add(1);
143 start_flag.notify_all();
144 while (done_count < num_atomics) {
145 std::this_thread::yield();
146 }
147 }
148 for (auto& t : wait_threads) {
149 t.request_stop();
150 }
151 start_flag.fetch_add(1);
152 start_flag.notify_all();
153 for (auto& t : wait_threads) {
154 t.join();
155 }
156 }
157 BENCHMARK(BM_atomic_wait_multi_thread_wait_different_atomics)->RangeMultiplier(2)->Range(1 << 10, 1 << 20);
158
159 BENCHMARK_MAIN();
160