| 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_wait_helper.h" |
| 12 | |
| 13 | #include <atomic> |
| 14 | #include <array> |
| 15 | #include <chrono> |
| 16 | #include <cstdint> |
| 17 | #include <numeric> |
| 18 | #include <stop_token> |
| 19 | #include <thread> |
| 20 | |
| 21 | #include "benchmark/benchmark.h" |
| 22 | #include "make_test_thread.h" |
| 23 | |
| 24 | using namespace std::chrono_literals; |
| 25 | |
| 26 | template <class NotifyPolicy, class NumPrioTasks> |
| 27 | void BM_1_atomic_1_waiter_1_notifier(benchmark::State& state) { |
| 28 | [[maybe_unused]] std::array<HighPrioTask, NumPrioTasks::value> tasks{}; |
| 29 | std::atomic<std::uint64_t> a; |
| 30 | auto thread_func = [&](std::stop_token st) { NotifyPolicy::notify(a, st); }; |
| 31 | |
| 32 | std::uint64_t total_loop_test_param = state.range(0); |
| 33 | |
| 34 | auto thread = support::make_test_jthread(thread_func); |
| 35 | |
| 36 | for (auto _ : state) { |
| 37 | for (std::uint64_t i = 0; i < total_loop_test_param; ++i) { |
| 38 | auto old = a.load(std::memory_order_relaxed); |
| 39 | a.wait(old); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | BENCHMARK(BM_1_atomic_1_waiter_1_notifier<KeepNotifying, NumHighPrioTasks<0>>) |
| 45 | ->RangeMultiplier(2) |
| 46 | ->Range(1 << 16, 1 << 18); |
| 47 | BENCHMARK(BM_1_atomic_1_waiter_1_notifier<NotifyEveryNus<50>, NumHighPrioTasks<0>>) |
| 48 | ->RangeMultiplier(2) |
| 49 | ->Range(1 << 10, 1 << 12); |
| 50 | BENCHMARK(BM_1_atomic_1_waiter_1_notifier<NotifyEveryNus<100>, NumHighPrioTasks<0>>) |
| 51 | ->RangeMultiplier(2) |
| 52 | ->Range(1 << 10, 1 << 12); |
| 53 | |
| 54 | BENCHMARK(BM_1_atomic_1_waiter_1_notifier<KeepNotifying, NumHighPrioTasks<4>>) |
| 55 | ->RangeMultiplier(2) |
| 56 | ->Range(1 << 16, 1 << 18); |
| 57 | BENCHMARK(BM_1_atomic_1_waiter_1_notifier<NotifyEveryNus<50>, NumHighPrioTasks<4>>) |
| 58 | ->RangeMultiplier(2) |
| 59 | ->Range(1 << 10, 1 << 12); |
| 60 | BENCHMARK(BM_1_atomic_1_waiter_1_notifier<NotifyEveryNus<100>, NumHighPrioTasks<4>>) |
| 61 | ->RangeMultiplier(2) |
| 62 | ->Range(1 << 10, 1 << 12); |
| 63 | |
| 64 | BENCHMARK(BM_1_atomic_1_waiter_1_notifier<KeepNotifying, NumHighPrioTasks<7>>) |
| 65 | ->RangeMultiplier(2) |
| 66 | ->Range(1 << 4, 1 << 6); |
| 67 | BENCHMARK(BM_1_atomic_1_waiter_1_notifier<NotifyEveryNus<50>, NumHighPrioTasks<7>>) |
| 68 | ->RangeMultiplier(2) |
| 69 | ->Range(1 << 3, 1 << 5); |
| 70 | BENCHMARK(BM_1_atomic_1_waiter_1_notifier<NotifyEveryNus<100>, NumHighPrioTasks<7>>) |
| 71 | ->RangeMultiplier(2) |
| 72 | ->Range(1 << 3, 1 << 5); |
| 73 | |
| 74 | BENCHMARK_MAIN(); |
| 75 | |