35 template <
typename Predicate>
38 if (config_.interval <= std::chrono::milliseconds::zero())
39 throw std::invalid_argument(
"chaos interval must be positive");
40 if (config_.nice_jitter < 0)
41 throw std::invalid_argument(
"chaos nice jitter must not be negative");
43 std::promise<thread_id> started;
44 auto ready = started.get_future();
46 [
this, predicate = std::move(predicate), started = std::move(started)]()
mutable
48 running_.store(
true, std::memory_order_release);
57 std::lock_guard<std::mutex> lock(failure_mutex_);
58 failure_ = std::current_exception();
60 running_.store(
false, std::memory_order_release);
62 worker_id_ = ready.get();
63 (void)worker_.
set_name(
"ts_chaos_ctl");
69 std::lock_guard<std::mutex> lock(wait_mutex_);
70 stop_.store(
true, std::memory_order_release);
73 if (worker_.joinable())
85 if (!worker_.joinable() || !worker_id_ || !running_.load(std::memory_order_acquire))
87 auto name = worker_.get_name();
88 return registered_thread{ *worker_id_, worker_.get_id(), name.value_or(std::string{}),
"chaos",
true };
94 if (!worker_.joinable() || !running_.load(std::memory_order_acquire))
95 return unexpected(std::make_error_code(std::errc::no_such_process));
96 return worker_.configure(config);
103 std::lock_guard<std::mutex> lock(failure_mutex_);
108 [[nodiscard]]
static auto
109 make_random_engine() noexcept ->
std::mt19937
111 auto seed =
static_cast<std::uint32_t
>(std::chrono::steady_clock::now().time_since_epoch().count());
112 seed ^=
static_cast<std::uint32_t
>(std::hash<std::thread::id>{}(std::this_thread::get_id()));
115 std::random_device device;
121 return std::mt19937(seed);
124 template <
typename Predicate>
126 run_loop(Predicate& predicate)
128 auto random = make_random_engine();
129 while (!stop_.load(std::memory_order_acquire))
133 perturb(*entries, predicate, random);
135 std::unique_lock<std::mutex> lock(wait_mutex_);
136 wakeup_.wait_for(lock, config_.interval, [
this] { return stop_.load(std::memory_order_acquire); });
140 template <
typename Predicate>
142 perturb(std::vector<registered_thread>
const& entries, Predicate& predicate, std::mt19937& random)
144 auto const topology = config_.shuffle_affinity ?
read_topology() : cpu_topology{};
145 std::uniform_int_distribution<int> jitter(-config_.nice_jitter, config_.nice_jitter);
146 std::size_t selected_index = 0;
147 for (
auto const& entry : entries)
149 if (!entry.alive || !std::invoke(predicate, entry))
152 if (config_.shuffle_affinity)
154 auto const node = topology.numa_nodes == 0 ? 0 : selected_index % topology.numa_nodes;
155 thread_config affinity;
156 affinity.set_affinity(
157 affinity_for_node(topology,
static_cast<int>(node),
static_cast<int>(selected_index)));
161 if (config_.nice_jitter != 0)
175 chaos_config config_;
176 std::atomic<bool> stop_{
false };
177 std::atomic<bool> running_{
false };
178 mutable std::mutex failure_mutex_;
179 std::exception_ptr failure_;
180 std::mutex wait_mutex_;
181 std::condition_variable wakeup_;
183 std::optional<thread_id> worker_id_;
auto affinity_for_node(cpu_topology const &topo, int node_index, int thread_index, int threads_per_node=1) -> thread_affinity
Build a thread_affinity for the given NUMA node using a pre-read topology.