blob: b67f94dfd1310e68d5faceb2a7836cb0973bdfb2 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) 2018 FIRST. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include "frc/Watchdog.h"
9
10#include <wpi/Format.h>
11#include <wpi/PriorityQueue.h>
12#include <wpi/raw_ostream.h>
13
14using namespace frc;
15
16constexpr std::chrono::milliseconds Watchdog::kMinPrintPeriod;
17
18class Watchdog::Thread : public wpi::SafeThread {
19 public:
20 template <typename T>
21 struct DerefGreater : public std::binary_function<T, T, bool> {
22 constexpr bool operator()(const T& lhs, const T& rhs) const {
23 return *lhs > *rhs;
24 }
25 };
26
27 wpi::PriorityQueue<Watchdog*, std::vector<Watchdog*>, DerefGreater<Watchdog*>>
28 m_watchdogs;
29
30 private:
31 void Main() override;
32};
33
34void Watchdog::Thread::Main() {
35 std::unique_lock<wpi::mutex> lock(m_mutex);
36
37 while (m_active) {
38 if (m_watchdogs.size() > 0) {
39 if (m_cond.wait_until(lock, m_watchdogs.top()->m_expirationTime) ==
40 std::cv_status::timeout) {
41 if (m_watchdogs.size() == 0 ||
42 m_watchdogs.top()->m_expirationTime > hal::fpga_clock::now()) {
43 continue;
44 }
45
46 // If the condition variable timed out, that means a Watchdog timeout
47 // has occurred, so call its timeout function.
48 auto watchdog = m_watchdogs.top();
49 m_watchdogs.pop();
50
51 auto now = hal::fpga_clock::now();
52 if (now - watchdog->m_lastTimeoutPrintTime > kMinPrintPeriod) {
53 watchdog->m_lastTimeoutPrintTime = now;
54 if (!watchdog->m_suppressTimeoutMessage) {
55 wpi::outs() << "Watchdog not fed within "
56 << wpi::format("%.6f",
57 watchdog->m_timeout.count() / 1.0e6)
58 << "s\n";
59 }
60 }
61
62 // Set expiration flag before calling the callback so any manipulation
63 // of the flag in the callback (e.g., calling Disable()) isn't
64 // clobbered.
65 watchdog->m_isExpired = true;
66
67 lock.unlock();
68 watchdog->m_callback();
69 lock.lock();
70 }
71 // Otherwise, a Watchdog removed itself from the queue (it notifies the
72 // scheduler of this) or a spurious wakeup occurred, so just rewait with
73 // the soonest watchdog timeout.
74 } else {
75 m_cond.wait(lock, [&] { return m_watchdogs.size() > 0 || !m_active; });
76 }
77 }
78}
79
80Watchdog::Watchdog(double timeout, std::function<void()> callback)
81 : m_timeout(static_cast<int64_t>(timeout * 1.0e6)),
82 m_callback(callback),
83 m_owner(&GetThreadOwner()) {}
84
85Watchdog::~Watchdog() { Disable(); }
86
87double Watchdog::GetTime() const {
88 return (hal::fpga_clock::now() - m_startTime).count() / 1.0e6;
89}
90
91void Watchdog::SetTimeout(double timeout) {
92 m_startTime = hal::fpga_clock::now();
93 m_epochs.clear();
94
95 // Locks mutex
96 auto thr = m_owner->GetThread();
97 if (!thr) return;
98
99 m_timeout = std::chrono::microseconds(static_cast<int64_t>(timeout * 1.0e6));
100 m_isExpired = false;
101
102 thr->m_watchdogs.remove(this);
103 m_expirationTime = m_startTime + m_timeout;
104 thr->m_watchdogs.emplace(this);
105 thr->m_cond.notify_all();
106}
107
108double Watchdog::GetTimeout() const {
109 // Locks mutex
110 auto thr = m_owner->GetThread();
111
112 return m_timeout.count() / 1.0e6;
113}
114
115bool Watchdog::IsExpired() const {
116 // Locks mutex
117 auto thr = m_owner->GetThread();
118
119 return m_isExpired;
120}
121
122void Watchdog::AddEpoch(wpi::StringRef epochName) {
123 auto currentTime = hal::fpga_clock::now();
124 m_epochs[epochName] = currentTime - m_startTime;
125 m_startTime = currentTime;
126}
127
128void Watchdog::PrintEpochs() {
129 auto now = hal::fpga_clock::now();
130 if (now - m_lastEpochsPrintTime > kMinPrintPeriod) {
131 m_lastEpochsPrintTime = now;
132 for (const auto& epoch : m_epochs) {
133 wpi::outs() << '\t' << epoch.getKey() << ": "
134 << wpi::format("%.6f", epoch.getValue().count() / 1.0e6)
135 << "s\n";
136 }
137 }
138}
139
140void Watchdog::Reset() { Enable(); }
141
142void Watchdog::Enable() {
143 m_startTime = hal::fpga_clock::now();
144 m_epochs.clear();
145
146 // Locks mutex
147 auto thr = m_owner->GetThread();
148 if (!thr) return;
149
150 m_isExpired = false;
151
152 thr->m_watchdogs.remove(this);
153 m_expirationTime = m_startTime + m_timeout;
154 thr->m_watchdogs.emplace(this);
155 thr->m_cond.notify_all();
156}
157
158void Watchdog::Disable() {
159 // Locks mutex
160 auto thr = m_owner->GetThread();
161 if (!thr) return;
162
163 m_isExpired = false;
164
165 thr->m_watchdogs.remove(this);
166 thr->m_cond.notify_all();
167}
168
169void Watchdog::SuppressTimeoutMessage(bool suppress) {
170 m_suppressTimeoutMessage = suppress;
171}
172
173bool Watchdog::operator>(const Watchdog& rhs) {
174 return m_expirationTime > rhs.m_expirationTime;
175}
176
177wpi::SafeThreadOwner<Watchdog::Thread>& Watchdog::GetThreadOwner() {
178 static wpi::SafeThreadOwner<Thread> inst = [] {
179 wpi::SafeThreadOwner<Watchdog::Thread> inst;
180 inst.Start();
181 return inst;
182 }();
183 return inst;
184}