File: stop_token.bench.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (183 lines) | stat: -rw-r--r-- 6,066 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//===----------------------------------------------------------------------===//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <numeric>
#include <stop_token>
#include <thread>

#include "benchmark/benchmark.h"
#include "make_test_thread.h"

using namespace std::chrono_literals;

// We have a single thread created by std::jthread consuming the stop_token:
// polling for stop_requested.
void BM_stop_token_single_thread_polling_stop_requested(benchmark::State& state) {
  auto thread_func = [&](std::stop_token st, std::atomic<std::uint64_t>* loop_count) {
    while (!st.stop_requested()) {
      // doing some work
      loop_count->fetch_add(1, std::memory_order_relaxed);
    }
  };

  std::atomic<std::uint64_t> loop_count(0);
  std::uint64_t total_loop_test_param = state.range(0);

  auto thread = support::make_test_jthread(thread_func, &loop_count);

  for (auto _ : state) {
    auto start_total = loop_count.load(std::memory_order_relaxed);

    while (loop_count.load(std::memory_order_relaxed) - start_total < total_loop_test_param) {
      std::this_thread::yield();
    }
  }
}

BENCHMARK(BM_stop_token_single_thread_polling_stop_requested)->RangeMultiplier(2)->Range(1 << 10, 1 << 24);

// We have multiple threads polling for stop_requested of the same stop_token.
void BM_stop_token_multi_thread_polling_stop_requested(benchmark::State& state) {
  std::atomic<bool> start{false};

  auto thread_func = [&start](std::atomic<std::uint64_t>* loop_count, std::stop_token st) {
    start.wait(false);
    while (!st.stop_requested()) {
      // doing some work
      loop_count->fetch_add(1, std::memory_order_relaxed);
    }
  };

  constexpr size_t thread_count = 20;

  std::uint64_t total_loop_test_param = state.range(0);

  std::vector<std::atomic<std::uint64_t>> loop_counts(thread_count);
  std::stop_source ss;
  std::vector<std::jthread> threads;
  threads.reserve(thread_count);

  for (size_t i = 0; i < thread_count; ++i) {
    threads.emplace_back(support::make_test_jthread(thread_func, &loop_counts[i], ss.get_token()));
  }

  auto get_total_loop = [&loop_counts] {
    std::uint64_t total = 0;
    for (const auto& loop_count : loop_counts) {
      total += loop_count.load(std::memory_order_relaxed);
    }
    return total;
  };

  start = true;
  start.notify_all();

  for (auto _ : state) {
    auto start_total = get_total_loop();

    while (get_total_loop() - start_total < total_loop_test_param) {
      std::this_thread::yield();
    }
  }

  ss.request_stop();
}

BENCHMARK(BM_stop_token_multi_thread_polling_stop_requested)->RangeMultiplier(2)->Range(1 << 10, 1 << 24);

// We have a single thread created by std::jthread consuming the stop_token:
// registering/deregistering callbacks, one at a time.
void BM_stop_token_single_thread_reg_unreg_callback(benchmark::State& state) {
  auto thread_func = [&](std::stop_token st, std::atomic<std::uint64_t>* reg_count) {
    while (!st.stop_requested()) {
      std::stop_callback cb{st, [&]() noexcept {}};
      benchmark::DoNotOptimize(cb);
      reg_count->fetch_add(1, std::memory_order_relaxed);
    }
  };

  std::atomic<std::uint64_t> reg_count(0);
  std::uint64_t total_reg_test_param = state.range(0);

  auto thread = support::make_test_jthread(thread_func, &reg_count);

  for (auto _ : state) {
    auto start_total = reg_count.load(std::memory_order_relaxed);

    while (reg_count.load(std::memory_order_relaxed) - start_total < total_reg_test_param) {
      std::this_thread::yield();
    }
  }
}
BENCHMARK(BM_stop_token_single_thread_reg_unreg_callback)->RangeMultiplier(2)->Range(1 << 10, 1 << 24);

// At startup, it creates a single stop_source which it will then pass an associated stop_token to every
// request.
//
// Assume a thread-pool handles these requests and for each request it polls for stop_requested(), then attaches a
// stop-callback, does some work, then detaches the stop-callback some time later. The lifetime of requests/callbacks
// would overlap with other requests/callback from the same thread.
//
// Say something like each thread keeping a circular buffer of N stop-callbacks and destroying the stop-callbacks in
// FIFO order
void BM_stop_token_async_reg_unreg_callback(benchmark::State& state) {
  struct dummy_stop_callback {
    void operator()() const noexcept {}
  };

  constexpr size_t thread_count             = 20;
  constexpr size_t concurrent_request_count = 1000;
  std::atomic<bool> start{false};

  std::uint64_t total_reg_test_param = state.range(0);
  std::vector<std::atomic<std::uint64_t>> reg_counts(thread_count);

  std::stop_source ss;
  std::vector<std::jthread> threads;
  threads.reserve(thread_count);

  auto thread_func = [&start](std::atomic<std::uint64_t>* count, std::stop_token st) {
    std::vector<std::optional<std::stop_callback<dummy_stop_callback>>> cbs(concurrent_request_count);

    start.wait(false);

    std::uint32_t index = 0;
    while (!st.stop_requested()) {
      cbs[index].emplace(st, dummy_stop_callback{});
      index = (index + 1) % concurrent_request_count;
      count->fetch_add(1, std::memory_order_relaxed);
    }
  };

  for (size_t i = 0; i < thread_count; ++i) {
    threads.emplace_back(support::make_test_jthread(thread_func, &reg_counts[i], ss.get_token()));
  }

  auto get_total_reg = [&] {
    std::uint64_t total = 0;
    for (const auto& reg_count : reg_counts) {
      total += reg_count.load(std::memory_order_relaxed);
    }
    return total;
  };

  start = true;
  start.notify_all();

  for (auto _ : state) {
    auto start_total = get_total_reg();

    while (get_total_reg() - start_total < total_reg_test_param) {
      std::this_thread::yield();
    }
  }

  ss.request_stop();
}
BENCHMARK(BM_stop_token_async_reg_unreg_callback)->RangeMultiplier(2)->Range(1 << 10, 1 << 24);

BENCHMARK_MAIN();