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 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 
26     /// 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 
31     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.
watch_millis(id: &'static str, millis: u64) -> Option<WatchPoint>37     pub fn watch_millis(id: &'static str, millis: u64) -> Option<WatchPoint> {
38         Watchdog::watch(&WD, id, Duration::from_millis(millis))
39     }
40 
41     /// Sets a watch point with `id` and a default timeout of [`DEFAULT_TIMEOUT_MS`] milliseconds.
watch(id: &'static str) -> Option<WatchPoint>42     pub fn watch(id: &'static str) -> Option<WatchPoint> {
43         Watchdog::watch(&WD, id, DEFAULT_TIMEOUT)
44     }
45 
46     /// Like `watch_millis` but with a callback that is called every time a report
47     /// is printed about this watch point.
watch_millis_with( id: &'static str, millis: u64, callback: impl Fn() -> String + Send + 'static, ) -> Option<WatchPoint>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"))]
59 pub mod watchdog {
60     /// Noop watch point.
61     pub struct WatchPoint();
62     /// Sets a Noop watch point.
watch_millis(_: &'static str, _: u64) -> Option<WatchPoint>63     fn watch_millis(_: &'static str, _: u64) -> Option<WatchPoint> {
64         None
65     }
66     /// Sets a Noop watch point.
watch(_: &'static str) -> Option<WatchPoint>67     fn watch(_: &'static str) -> Option<WatchPoint> {
68         None
69     }
70 
watch_millis_with( _: &'static str, _: u64, _: impl Fn() -> String + Send + 'static, ) -> Option<WatchPoint>71     pub fn watch_millis_with(
72         _: &'static str,
73         _: u64,
74         _: impl Fn() -> String + Send + 'static,
75     ) -> Option<WatchPoint> {
76         None
77     }
78 }
79