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