Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
ExecutorWatchdog.hxx
Go to the documentation of this file.
1
35#ifndef _UTILS_EXECUTORWATCHDOG_HXX_
36#define _UTILS_EXECUTORWATCHDOG_HXX_
37
39#include "os/os.h"
40#include "utils/logging.h"
41
46{
47public:
55
56private:
57 Action take_stamp()
58 {
60 return sleep_and_call(&timer_, MSEC_TO_NSEC(50), STATE(woken));
61 }
62
63 Action woken()
64 {
65 uint32_t new_time_msec = NSEC_TO_MSEC(os_get_time_monotonic());
66 auto diff = new_time_msec - lastTimeMsec_;
67 if (diff > 100)
68 {
69 LOG(WARNING, "[WARN] Executor was blocked for %d msec",
70 (int)(diff - 50));
71 }
72 if (++count_ > (5000 / 50))
73 {
74 count_ = 0;
75 LOG(INFO, "Watchdog alive.");
76 }
77 return call_immediately(STATE(take_stamp));
78 }
79
80 StateFlowTimer timer_ {this};
82 uint32_t lastTimeMsec_ {0};
84 int count_ {0};
85};
86
87#endif // _UTILS_EXECUTORWATCHDOG_HXX_
#define STATE(_fn)
Turns a function name into an argument to be supplied to functions expecting a state.
Definition StateFlow.hxx:61
This stateflow checks an executor every 50 msec.
uint32_t lastTimeMsec_
Timestamp when we last went to sleep.
ExecutorWatchdog(Service *service)
Constructor.
int count_
Counter that controls printing a heartbeat message that we are fine.
Collection of related state machines that pend on incoming messages.
Base class for state machines.
Service * service()
Return a pointer to the service I am bound to.
void start_flow(Callback c)
Resets the flow to the specified state and starts it.
Action call_immediately(Callback c)
Imediately call the next state upon return.
Action sleep_and_call(::Timer *timer, long long timeout_nsec, Callback c)
Suspends execution of this control flow for a specified time.
#define LOG(level, message...)
Conditionally write a message to the logging output.
Definition logging.h:99
static const int WARNING
Loglevel that is always printed, reporting a warning or a retryable error.
Definition logging.h:55
static const int INFO
Loglevel that is printed by default, reporting some status information.
Definition logging.h:57
long long os_get_time_monotonic(void)
Get the monotonic time since the system started.
Definition os.c:571
#define MSEC_TO_NSEC(_msec)
Convert a millisecond value to a nanosecond value.
Definition os.h:268
#define NSEC_TO_MSEC(_nsec)
Convert a nanosecond value to a millisecond value.
Definition os.h:232