RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
Watchdog.java
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5package org.wpilib.system;
6
7import static org.wpilib.units.Units.Seconds;
8
9import java.io.Closeable;
10import java.util.PriorityQueue;
11import java.util.concurrent.locks.ReentrantLock;
12import org.wpilib.driverstation.DriverStationErrors;
13import org.wpilib.hardware.hal.HALUtil;
14import org.wpilib.hardware.hal.NotifierJNI;
15import org.wpilib.units.measure.Time;
16import org.wpilib.util.WPIUtilJNI;
17
26public class Watchdog implements Closeable, Comparable<Watchdog> {
27 // Used for timeout print rate-limiting
28 private static final long MIN_PRINT_PERIOD = 1_000_000_000L; // ns
29
30 // Monotonic times in nanoseconds. Seconds in a double can't hold
31 // nanosecond-resolution timestamps once the clock is large.
32 private long m_startTime;
33 private double m_timeout;
34 private long m_expirationTime;
35 private final Runnable m_callback;
36 private long m_lastTimeoutPrint; // ns
37
38 boolean m_isExpired;
39
40 boolean m_suppressTimeoutMessage;
41
42 private final Tracer m_tracer;
43
44 private static final PriorityQueue<Watchdog> m_watchdogs = new PriorityQueue<>();
45 private static ReentrantLock m_queueMutex = new ReentrantLock();
46 private static int m_notifier;
47
48 static {
49 m_notifier = NotifierJNI.createNotifier();
50 NotifierJNI.setNotifierName(m_notifier, "Watchdog");
51 startDaemonThread(Watchdog::schedulerFunc);
52 }
53
60 public Watchdog(double timeout, Runnable callback) {
61 m_timeout = timeout;
62 m_callback = callback;
63 m_tracer = new Tracer();
64 }
65
72 public Watchdog(Time timeout, Runnable callback) {
73 this(timeout.in(Seconds), callback);
74 }
75
76 @Override
77 public void close() {
78 disable();
79 }
80
81 @Override
82 public boolean equals(Object obj) {
83 return obj instanceof Watchdog watchdog && m_expirationTime == watchdog.m_expirationTime;
84 }
85
86 @Override
87 public int hashCode() {
88 return Long.hashCode(m_expirationTime);
89 }
90
91 @Override
92 public int compareTo(Watchdog rhs) {
93 // Elements with sooner expiration times are sorted as lesser. The head of
94 // Java's PriorityQueue is the least element.
95 return Long.compare(m_expirationTime, rhs.m_expirationTime);
96 }
97
103 public double getTime() {
104 return (HALUtil.getMonotonicTime() - m_startTime) / 1e9;
105 }
106
112 public void setTimeout(double timeout) {
113 m_startTime = HALUtil.getMonotonicTime();
114 m_tracer.clearEpochs();
115
116 m_queueMutex.lock();
117 try {
118 m_timeout = timeout;
119 m_isExpired = false;
120
121 m_watchdogs.remove(this);
122 m_expirationTime = m_startTime + Math.round(m_timeout * 1e9);
123 m_watchdogs.add(this);
124 updateAlarm();
125 } finally {
126 m_queueMutex.unlock();
127 }
128 }
129
135 public double getTimeout() {
136 m_queueMutex.lock();
137 try {
138 return m_timeout;
139 } finally {
140 m_queueMutex.unlock();
141 }
142 }
143
149 public boolean isExpired() {
150 m_queueMutex.lock();
151 try {
152 return m_isExpired;
153 } finally {
154 m_queueMutex.unlock();
155 }
156 }
157
164 public void addEpoch(String epochName) {
165 m_tracer.addEpoch(epochName);
166 }
167
173 public void printEpochs() {
174 m_tracer.printEpochs();
175 }
176
182 public void reset() {
183 enable();
184 }
185
187 public void enable() {
188 m_startTime = HALUtil.getMonotonicTime();
189 m_tracer.clearEpochs();
190
191 m_queueMutex.lock();
192 try {
193 m_isExpired = false;
194
195 m_watchdogs.remove(this);
196 m_expirationTime = m_startTime + Math.round(m_timeout * 1e9);
197 m_watchdogs.add(this);
198 updateAlarm();
199 } finally {
200 m_queueMutex.unlock();
201 }
202 }
203
205 public void disable() {
206 m_queueMutex.lock();
207 try {
208 m_watchdogs.remove(this);
209 updateAlarm();
210 } finally {
211 m_queueMutex.unlock();
212 }
213 }
214
222 public void suppressTimeoutMessage(boolean suppress) {
223 m_suppressTimeoutMessage = suppress;
224 }
225
226 @SuppressWarnings("resource")
227 private static void updateAlarm() {
228 if (m_watchdogs.isEmpty()) {
229 NotifierJNI.cancelNotifierAlarm(m_notifier, true);
230 } else {
231 NotifierJNI.setNotifierAlarm(m_notifier, m_watchdogs.peek().m_expirationTime, 0, true, true);
232 }
233 }
234
235 private static Thread startDaemonThread(Runnable target) {
236 Thread inst = new Thread(target);
237 inst.setDaemon(true);
238 inst.start();
239 return inst;
240 }
241
242 private static void schedulerFunc() {
243 while (!Thread.currentThread().isInterrupted()) {
244 try {
245 WPIUtilJNI.waitForObject(m_notifier);
246 } catch (InterruptedException ex) {
247 Thread.currentThread().interrupt();
248 break;
249 }
250 long curTime = HALUtil.getMonotonicTime();
251
252 m_queueMutex.lock();
253 try {
254 if (m_watchdogs.isEmpty()) {
255 continue;
256 }
257
258 // If the condition variable timed out, that means a Watchdog timeout
259 // has occurred, so call its timeout function.
260 Watchdog watchdog = m_watchdogs.poll();
261
262 if (curTime - watchdog.m_lastTimeoutPrint > MIN_PRINT_PERIOD) {
263 watchdog.m_lastTimeoutPrint = curTime;
264 if (!watchdog.m_suppressTimeoutMessage) {
265 DriverStationErrors.reportWarning(
266 String.format("Watchdog not fed within %.6fs\n", watchdog.m_timeout), false);
267 }
268 }
269
270 // Set expiration flag before calling the callback so any
271 // manipulation of the flag in the callback (e.g., calling
272 // Disable()) isn't clobbered.
273 watchdog.m_isExpired = true;
274
275 m_queueMutex.unlock();
276 try {
277 watchdog.m_callback.run();
278 } finally {
279 m_queueMutex.lock();
280 }
281
282 updateAlarm();
283 } finally {
284 m_queueMutex.unlock();
285 }
286 }
287 }
288}
Watchdog(double timeout, Runnable callback)
Definition Watchdog.java:60
void addEpoch(String epochName)
void suppressTimeoutMessage(boolean suppress)
void setTimeout(double timeout)
Watchdog(Time timeout, Runnable callback)
Definition Watchdog.java:72