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 { 20 use std::sync::{Arc, LazyLock}; 21 use std::time::Duration; 22 pub use watchdog_rs::WatchPoint; 23 use watchdog_rs::Watchdog; 24 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 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))); 32 33 /// Sets a watch point with `id` and a timeout of `millis` milliseconds. watch_millis(id: &'static str, millis: u64) -> Option<WatchPoint>34 pub fn watch_millis(id: &'static str, millis: u64) -> Option<WatchPoint> { 35 Watchdog::watch(&WD, id, Duration::from_millis(millis)) 36 } 37 38 /// Sets a watch point with `id` and a default timeout of [`DEFAULT_TIMEOUT_MS`] milliseconds. watch(id: &'static str) -> Option<WatchPoint>39 pub fn watch(id: &'static str) -> Option<WatchPoint> { 40 Watchdog::watch(&WD, id, DEFAULT_TIMEOUT) 41 } 42 43 /// Like `watch_millis` but with context that is included every time a report is printed about 44 /// this watch point. watch_millis_with( id: &'static str, millis: u64, context: impl std::fmt::Debug + Send + 'static, ) -> Option<WatchPoint>45 pub fn watch_millis_with( 46 id: &'static str, 47 millis: u64, 48 context: impl std::fmt::Debug + Send + 'static, 49 ) -> Option<WatchPoint> { 50 Watchdog::watch_with(&WD, id, Duration::from_millis(millis), context) 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. watch_millis(_: &'static str, _: u64) -> Option<WatchPoint>60 fn watch_millis(_: &'static str, _: u64) -> Option<WatchPoint> { 61 None 62 } 63 /// Sets a Noop watch point. watch(_: &'static str) -> Option<WatchPoint>64 fn watch(_: &'static str) -> Option<WatchPoint> { 65 None 66 } 67 watch_millis_with( _: &'static str, _: u64, _: impl std::fmt::Debug + Send + 'static, ) -> Option<WatchPoint>68 pub fn watch_millis_with( 69 _: &'static str, 70 _: u64, 71 _: impl std::fmt::Debug + Send + 'static, 72 ) -> Option<WatchPoint> { 73 None 74 } 75 } 76