114 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++1710 11// This benchmark is very expensive and we don't want to run it on a regular basis,12// only to ensure the code doesn't rot.13// REQUIRES: enable-benchmarks=dry-run14 15#include <atomic>16#include <cstdint>17#include <mutex>18#include <stop_token>19#include <thread>20 21#include "benchmark/benchmark.h"22#include "make_test_thread.h"23 24using namespace std::chrono_literals;25 26struct AtomicLock {27 std::atomic<bool>& locked_;28 29 AtomicLock(const AtomicLock&) = delete;30 AtomicLock& operator=(const AtomicLock&) = delete;31 32 AtomicLock(std::atomic<bool>& l) : locked_(l) { lock(); }33 ~AtomicLock() { unlock(); }34 35 void lock() {36 while (true) {37 locked_.wait(true, std::memory_order_relaxed);38 bool expected = false;39 if (locked_.compare_exchange_weak(expected, true, std::memory_order_acquire, std::memory_order_relaxed))40 break;41 }42 }43 44 void unlock() {45 locked_.store(false, std::memory_order_release);46 locked_.notify_all();47 }48};49 50// using LockState = std::atomic<bool>;51// using Lock = AtomicLock;52 53// using LockState = std::mutex;54// using Lock = std::unique_lock<std::mutex>;55 56template <class LockState, class Lock>57void test_multi_thread_lock_unlock(benchmark::State& state) {58 std::uint64_t total_loop_test_param = state.range(0);59 constexpr auto num_threads = 15;60 std::vector<std::jthread> threads;61 threads.reserve(num_threads);62 63 std::atomic<std::uint64_t> start_flag = 0;64 std::atomic<std::uint64_t> done_count = 0;65 66 LockState lock_state{};67 68 auto func = [&start_flag, &done_count, &lock_state, total_loop_test_param](std::stop_token st) {69 auto old_start = 0;70 while (!st.stop_requested()) {71 start_flag.wait(old_start);72 old_start = start_flag.load();73 74 // main things under test: locking and unlocking in the loop75 for (std::uint64_t i = 0; i < total_loop_test_param; ++i) {76 Lock l{lock_state};77 }78 79 done_count.fetch_add(1);80 }81 };82 83 for (size_t i = 0; i < num_threads; ++i) {84 threads.emplace_back(support::make_test_jthread(func));85 }86 87 for (auto _ : state) {88 done_count = 0;89 start_flag.fetch_add(1);90 start_flag.notify_all();91 while (done_count < num_threads) {92 std::this_thread::yield();93 }94 }95 for (auto& t : threads) {96 t.request_stop();97 }98 start_flag.fetch_add(1);99 start_flag.notify_all();100 for (auto& t : threads) {101 t.join();102 }103}104 105void BM_atomic_wait(benchmark::State& state) { test_multi_thread_lock_unlock<std::atomic<bool>, AtomicLock>(state); }106BENCHMARK(BM_atomic_wait)->RangeMultiplier(2)->Range(1 << 10, 1 << 20);107 108void BM_mutex(benchmark::State& state) {109 test_multi_thread_lock_unlock<std::mutex, std::unique_lock<std::mutex>>(state);110}111BENCHMARK(BM_mutex)->RangeMultiplier(2)->Range(1 << 10, 1 << 20);112 113BENCHMARK_MAIN();114