Alice Wang | 83c6aef | 2023-11-03 17:17:34 +0000 | [diff] [blame] | 1 | // 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")] |
| 19 | pub mod watchdog { |
Andrew Walbran | a4bc1a9 | 2024-09-03 13:08:17 +0100 | [diff] [blame^] | 20 | use std::sync::{Arc, LazyLock}; |
Alice Wang | 83c6aef | 2023-11-03 17:17:34 +0000 | [diff] [blame] | 21 | use std::time::Duration; |
| 22 | pub use watchdog_rs::WatchPoint; |
| 23 | use watchdog_rs::Watchdog; |
| 24 | |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 25 | /// 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 Walbran | a4bc1a9 | 2024-09-03 13:08:17 +0100 | [diff] [blame^] | 30 | /// 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 Wang | 83c6aef | 2023-11-03 17:17:34 +0000 | [diff] [blame] | 32 | |
| 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 Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 38 | /// 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 Drysdale | 387c85b | 2024-06-10 14:40:45 +0100 | [diff] [blame] | 43 | /// Like `watch_millis` but with context that is included every time a report is printed about |
| 44 | /// this watch point. |
Alice Wang | 83c6aef | 2023-11-03 17:17:34 +0000 | [diff] [blame] | 45 | pub fn watch_millis_with( |
| 46 | id: &'static str, |
| 47 | millis: u64, |
David Drysdale | 387c85b | 2024-06-10 14:40:45 +0100 | [diff] [blame] | 48 | context: impl std::fmt::Debug + Send + 'static, |
Alice Wang | 83c6aef | 2023-11-03 17:17:34 +0000 | [diff] [blame] | 49 | ) -> Option<WatchPoint> { |
David Drysdale | 387c85b | 2024-06-10 14:40:45 +0100 | [diff] [blame] | 50 | Watchdog::watch_with(&WD, id, Duration::from_millis(millis), context) |
Alice Wang | 83c6aef | 2023-11-03 17:17:34 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | /// This module provides empty/noop implementations of the watch dog utility functions. |
| 55 | #[cfg(not(feature = "watchdog"))] |
| 56 | pub 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 Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 63 | /// Sets a Noop watch point. |
| 64 | fn watch(_: &'static str) -> Option<WatchPoint> { |
| 65 | None |
| 66 | } |
Alice Wang | 83c6aef | 2023-11-03 17:17:34 +0000 | [diff] [blame] | 67 | |
| 68 | pub fn watch_millis_with( |
| 69 | _: &'static str, |
| 70 | _: u64, |
David Drysdale | 387c85b | 2024-06-10 14:40:45 +0100 | [diff] [blame] | 71 | _: impl std::fmt::Debug + Send + 'static, |
Alice Wang | 83c6aef | 2023-11-03 17:17:34 +0000 | [diff] [blame] | 72 | ) -> Option<WatchPoint> { |
| 73 | None |
| 74 | } |
| 75 | } |