seqeron 0.18.0
Loading...
Searching...
No Matches
IdleStrategy.hpp
Go to the documentation of this file.
1#pragma once
2
3// Aeron's C++ idle strategies (BusySpinIdleStrategy, YieldingIdleStrategy, BackoffIdleStrategy) are
4// duck-typed with no common base — normally selected as a compile-time template parameter. That's
5// wrong for this project: busy-spin/yielding only pay off when the calling thread owns an isolated
6// core, but a C++ client of seqeron typically shares a
7// handful of cores with the co-located SequencerServer/ReplayerServer and each other — on a dev box or
8// the chaos/e2e harness that's a dozen-plus processes on a handful of cores. DynamicIdleStrategy
9// wraps the choice in a std::variant so it becomes a SEQERON_IDLE_STRATEGY env var instead, matching
10// SequencerServer's sequencer.idleStrategy / ReplayerServer's replayer.idleStrategy on the Java side.
11
12#include <cctype>
13#include <cstdlib>
14#include <stdexcept>
15#include <string>
16#include <variant>
17
18#include "concurrent/BackOffIdleStrategy.h"
19#include "concurrent/BusySpinIdleStrategy.h"
20#include "concurrent/YieldingIdleStrategy.h"
21
23
25{
26 public:
27 using Variant = std::variant<aeron::concurrent::BusySpinIdleStrategy, aeron::concurrent::YieldingIdleStrategy,
28 aeron::concurrent::BackoffIdleStrategy>;
29
30 explicit DynamicIdleStrategy(Variant strategy) : m_strategy(std::move(strategy))
31 {}
32
33 inline void idle(int workCount)
34 {
35 std::visit([workCount](auto& s) { s.idle(workCount); }, m_strategy);
36 }
37
38 inline void idle()
39 {
40 std::visit([](auto& s) { s.idle(); }, m_strategy);
41 }
42
43 inline void reset()
44 {
45 std::visit([](auto& s) { s.reset(); }, m_strategy);
46 }
47
48 private:
49 Variant m_strategy;
50};
51
52// Resolves SEQERON_IDLE_STRATEGY (case-insensitive): "backoff" (default), "yielding", or "busyspin".
54{
55 const char* v = std::getenv("SEQERON_IDLE_STRATEGY");
56 std::string name = (v != nullptr && *v != '\0') ? v : "backoff";
57 for (char& c : name)
58 {
59 c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
60 }
61 if (name == "busyspin")
62 {
63 return DynamicIdleStrategy{ aeron::concurrent::BusySpinIdleStrategy{} };
64 }
65 if (name == "yielding")
66 {
67 return DynamicIdleStrategy{ aeron::concurrent::YieldingIdleStrategy{} };
68 }
69 if (name == "backoff")
70 {
71 return DynamicIdleStrategy{ aeron::concurrent::BackoffIdleStrategy{} };
72 }
73 throw std::invalid_argument("Unknown SEQERON_IDLE_STRATEGY=" + name +
74 " (expected 'backoff', 'yielding', or 'busyspin')");
75}
76
77} // namespace org::limitless::seqeron::util
DynamicIdleStrategy(Variant strategy)
Definition IdleStrategy.hpp:30
std::variant< aeron::concurrent::BusySpinIdleStrategy, aeron::concurrent::YieldingIdleStrategy, aeron::concurrent::BackoffIdleStrategy > Variant
Definition IdleStrategy.hpp:28
void idle()
Definition IdleStrategy.hpp:38
void reset()
Definition IdleStrategy.hpp:43
void idle(int workCount)
Definition IdleStrategy.hpp:33
Definition Env.hpp:12
DynamicIdleStrategy resolveIdleStrategy()
Definition IdleStrategy.hpp:53