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