blob: 63383aabe4b15f59b9a6c8fd11cab42fa89fc1d1 [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 {
Andrew Walbrana4bc1a92024-09-03 13:08:17 +010020 use std::sync::{Arc, LazyLock};
Alice Wang83c6aef2023-11-03 17:17:34 +000021 use std::time::Duration;
22 pub use watchdog_rs::WatchPoint;
23 use watchdog_rs::Watchdog;
24
David Drysdale541846b2024-05-23 13:16:07 +010025 /// Default timeout interval, in milliseconds.
26 pub const DEFAULT_TIMEOUT_MS: u64 = 500;
27
28 const DEFAULT_TIMEOUT: Duration = Duration::from_millis(DEFAULT_TIMEOUT_MS);
29
Andrew Walbrana4bc1a92024-09-03 13:08:17 +010030 /// A Watchdog thread, that can be used to create watch points.
31 static WD: LazyLock<Arc<Watchdog>> = LazyLock::new(|| Watchdog::new(Duration::from_secs(10)));
Alice Wang83c6aef2023-11-03 17:17:34 +000032
33 /// Sets a watch point with `id` and a timeout of `millis` milliseconds.
34 pub fn watch_millis(id: &'static str, millis: u64) -> Option<WatchPoint> {
35 Watchdog::watch(&WD, id, Duration::from_millis(millis))
36 }
37
David Drysdale541846b2024-05-23 13:16:07 +010038 /// Sets a watch point with `id` and a default timeout of [`DEFAULT_TIMEOUT_MS`] milliseconds.
39 pub fn watch(id: &'static str) -> Option<WatchPoint> {
40 Watchdog::watch(&WD, id, DEFAULT_TIMEOUT)
41 }
42
David Drysdale387c85b2024-06-10 14:40:45 +010043 /// Like `watch_millis` but with context that is included every time a report is printed about
44 /// this watch point.
Alice Wang83c6aef2023-11-03 17:17:34 +000045 pub fn watch_millis_with(
46 id: &'static str,
47 millis: u64,
David Drysdale387c85b2024-06-10 14:40:45 +010048 context: impl std::fmt::Debug + Send + 'static,
Alice Wang83c6aef2023-11-03 17:17:34 +000049 ) -> Option<WatchPoint> {
David Drysdale387c85b2024-06-10 14:40:45 +010050 Watchdog::watch_with(&WD, id, Duration::from_millis(millis), context)
Alice Wang83c6aef2023-11-03 17:17:34 +000051 }
52}
53
54/// This module provides empty/noop implementations of the watch dog utility functions.
55#[cfg(not(feature = "watchdog"))]
56pub mod watchdog {
57 /// Noop watch point.
58 pub struct WatchPoint();
59 /// Sets a Noop watch point.
60 fn watch_millis(_: &'static str, _: u64) -> Option<WatchPoint> {
61 None
62 }
David Drysdale541846b2024-05-23 13:16:07 +010063 /// Sets a Noop watch point.
64 fn watch(_: &'static str) -> Option<WatchPoint> {
65 None
66 }
Alice Wang83c6aef2023-11-03 17:17:34 +000067
68 pub fn watch_millis_with(
69 _: &'static str,
70 _: u64,
David Drysdale387c85b2024-06-10 14:40:45 +010071 _: impl std::fmt::Debug + Send + 'static,
Alice Wang83c6aef2023-11-03 17:17:34 +000072 ) -> Option<WatchPoint> {
73 None
74 }
75}