Corium 1.1.0
High-Performance Zero-Heap C++20 MPSC Application Runtime
Loading...
Searching...
No Matches
WatchdogSupervisor.hpp
Go to the documentation of this file.
1
7#pragma once
8
9#include <atomic>
10#include <chrono>
11#include <cstddef>
12#include <cstdint>
13#include <type_traits>
14
18
19namespace corium::safety {
20
27template <
28 std::size_t MaxServices = 16,
29 typename ClockPolicy = ChronoClockPolicy
30>
32public:
33 using KickCallbackFn = void (*)(void* userData);
34
35 WatchdogSupervisor() = default;
36
40 void setWatchdogKickCallback(KickCallbackFn kickFn, void* userData = nullptr) noexcept
41 {
42 _kickFn = kickFn;
43 _userData = userData;
44 }
45
48 {
49 return _monitor;
50 }
51
53 [[nodiscard]] const HeartbeatMonitor<MaxServices>& heartbeatMonitor() const noexcept
54 {
55 return _monitor;
56 }
57
59 bool registerService(uint32_t serviceId, uint64_t timeoutNs) noexcept
60 {
61 const uint64_t now = nowNs();
62 return _monitor.registerService(serviceId, timeoutNs, now);
63 }
64
66 void beat(uint32_t serviceId) noexcept
67 {
68 _monitor.beat(serviceId, nowNs());
69 }
70
76 template <typename EventSinkType>
77 bool supervise(const EventSinkType& sink) noexcept
78 {
79 const uint64_t now = nowNs();
80 uint32_t timedOutId = 0;
81 uint64_t lastBeat = 0;
82 uint64_t budget = 0;
83
84 if (_monitor.checkHealth(now, timedOutId, lastBeat, budget)) {
85 // System is healthy: feed the watchdog
86 if (_kickFn) {
87 _kickFn(_userData);
88 }
89 _kicksCount.fetch_add(1, std::memory_order_relaxed);
90 _lastHealthyTimeNs.store(now, std::memory_order_release);
91 return true;
92 }
93
94 // Failure detected: suppress watchdog kick and post emergency event
95 _suppressionsCount.fetch_add(1, std::memory_order_relaxed);
96 sink.postHighPriority(WatchdogTimeoutEvent{.serviceId = timedOutId, .lastHeartbeatNs = lastBeat, .timeoutBudgetNs = budget});
97 return false;
98 }
99
101 [[nodiscard]] uint64_t totalKicks() const noexcept
102 {
103 return _kicksCount.load(std::memory_order_relaxed);
104 }
105
107 [[nodiscard]] uint64_t totalSuppressions() const noexcept
108 {
109 return _suppressionsCount.load(std::memory_order_relaxed);
110 }
111
112private:
113 [[nodiscard]] static uint64_t nowNs() noexcept
114 {
115 if constexpr (std::is_integral_v<typename ClockPolicy::time_point>) {
116 return static_cast<uint64_t>(ClockPolicy::now());
117 } else {
118 return static_cast<uint64_t>(
119 std::chrono::duration_cast<std::chrono::nanoseconds>(
120 ClockPolicy::now().time_since_epoch()
121 ).count()
122 );
123 }
124 }
125
126 HeartbeatMonitor<MaxServices> _monitor{};
127 KickCallbackFn _kickFn{nullptr};
128 void* _userData{nullptr};
129 std::atomic<uint64_t> _kicksCount{0};
130 std::atomic<uint64_t> _suppressionsCount{0};
131 std::atomic<uint64_t> _lastHealthyTimeNs{0};
132};
133
134} // namespace corium::safety
Hardware and simulated clock policies (Chrono, Manual, Tick, EspTimer, FreeRTOS).
Lock-free SLA deadline tracker for multi-service heartbeats.
Heartbeat and fault notification event structures.
Zero-heap multi-service heartbeat tracker for safety-critical execution. Thread-safe for concurrent h...
Definition HeartbeatMonitor.hpp:21
bool checkHealth(uint64_t nowNs, uint32_t &outTimedOutServiceId, uint64_t &outLastHeartbeatNs, uint64_t &outTimeoutBudgetNs) const noexcept
Check health status of all registered active services.
Definition HeartbeatMonitor.hpp:87
bool registerService(uint32_t serviceId, uint64_t timeoutNs, uint64_t initialTimestampNs=0) noexcept
Register a service for heartbeat monitoring with a specified timeout.
Definition HeartbeatMonitor.hpp:37
void beat(uint32_t serviceId, uint64_t nowNs) noexcept
Record a heartbeat for a given service.
Definition HeartbeatMonitor.hpp:60
Dedicated safety supervisor monitoring subsystem heartbeats and controlling watchdog refresh....
Definition WatchdogSupervisor.hpp:31
void beat(uint32_t serviceId) noexcept
Submit a heartbeat for a monitored service.
Definition WatchdogSupervisor.hpp:66
HeartbeatMonitor< MaxServices > & heartbeatMonitor() noexcept
Access reference to the internal HeartbeatMonitor.
Definition WatchdogSupervisor.hpp:47
bool supervise(const EventSinkType &sink) noexcept
Perform a supervisor check iteration. Kicks the hardware watchdog if healthy; suppresses the kick and...
Definition WatchdogSupervisor.hpp:77
void(*)(void *userData) KickCallbackFn
Definition WatchdogSupervisor.hpp:33
bool registerService(uint32_t serviceId, uint64_t timeoutNs) noexcept
Register a service for supervision.
Definition WatchdogSupervisor.hpp:59
const HeartbeatMonitor< MaxServices > & heartbeatMonitor() const noexcept
Access const reference to the internal HeartbeatMonitor.
Definition WatchdogSupervisor.hpp:53
uint64_t totalKicks() const noexcept
Get total number of successful watchdog kicks.
Definition WatchdogSupervisor.hpp:101
void setWatchdogKickCallback(KickCallbackFn kickFn, void *userData=nullptr) noexcept
Configure the physical hardware/software watchdog kick callback.
Definition WatchdogSupervisor.hpp:40
uint64_t totalSuppressions() const noexcept
Get total number of watchdog kick suppressions due to health violations.
Definition WatchdogSupervisor.hpp:107
Definition CircuitBreaker.hpp:13
Dispatched when a monitored service fails to deliver its heartbeat within its timeout window.
Definition SafetyEvents.hpp:15
uint32_t serviceId
Definition SafetyEvents.hpp:16