blob: 03c7740086d307d8f14b97ca3053fe84ac3108cb [file] [log] [blame]
Alice Wang83c6aef2023-11-03 17:17:34 +00001// Copyright 2023, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Helpers for the watchdog module.
16
17/// This module provides helpers for simplified use of the watchdog module.
18#[cfg(feature = "watchdog")]
19pub mod watchdog {
20 use lazy_static::lazy_static;
21 use std::sync::Arc;
22 use std::time::Duration;
23 pub use watchdog_rs::WatchPoint;
24 use watchdog_rs::Watchdog;
25
David Drysdale541846b2024-05-23 13:16:07 +010026 /// Default timeout interval, in milliseconds.
27 pub const DEFAULT_TIMEOUT_MS: u64 = 500;
28
29 const DEFAULT_TIMEOUT: Duration = Duration::from_millis(DEFAULT_TIMEOUT_MS);
30
Alice Wang83c6aef2023-11-03 17:17:34 +000031 lazy_static! {
32 /// A Watchdog thread, that can be used to create watch points.
33 static ref WD: Arc<Watchdog> = Watchdog::new(Duration::from_secs(10));
34 }
35
36 /// Sets a watch point with `id` and a timeout of `millis` milliseconds.
37 pub fn watch_millis(id: &'static str, millis: u64) -> Option<WatchPoint> {
38 Watchdog::watch(&WD, id, Duration::from_millis(millis))
39 }
40
David Drysdale541846b2024-05-23 13:16:07 +010041 /// Sets a watch point with `id` and a default timeout of [`DEFAULT_TIMEOUT_MS`] milliseconds.
42 pub fn watch(id: &'static str) -> Option<WatchPoint> {
43 Watchdog::watch(&WD, id, DEFAULT_TIMEOUT)
44 }
45
Alice Wang83c6aef2023-11-03 17:17:34 +000046 /// Like `watch_millis` but with a callback that is called every time a report
47 /// is printed about this watch point.
48 pub fn watch_millis_with(
49 id: &'static str,
50 millis: u64,
51 callback: impl Fn() -> String + Send + 'static,
52 ) -> Option<WatchPoint> {
53 Watchdog::watch_with(&WD, id, Duration::from_millis(millis), callback)
54 }
55}
56
57/// This module provides empty/noop implementations of the watch dog utility functions.
58#[cfg(not(feature = "watchdog"))]
59pub mod watchdog {
60 /// Noop watch point.
61 pub struct WatchPoint();
62 /// Sets a Noop watch point.
63 fn watch_millis(_: &'static str, _: u64) -> Option<WatchPoint> {
64 None
65 }
David Drysdale541846b2024-05-23 13:16:07 +010066 /// Sets a Noop watch point.
67 fn watch(_: &'static str) -> Option<WatchPoint> {
68 None
69 }
Alice Wang83c6aef2023-11-03 17:17:34 +000070
71 pub fn watch_millis_with(
72 _: &'static str,
73 _: u64,
74 _: impl Fn() -> String + Send + 'static,
75 ) -> Option<WatchPoint> {
76 None
77 }
78}