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