xref: /aosp_15_r20/system/extras/profcollectd/libprofcollectd/trace_provider.rs (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1 //
2 // Copyright (C) 2021 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 //! ProfCollect trace provider trait and helper functions.
18 
19 mod simpleperf_etm;
20 mod simpleperf_lbr;
21 
22 #[cfg(feature = "test")]
23 mod logging;
24 
25 use anyhow::{anyhow, Result};
26 use chrono::Utc;
27 use std::path::{Path, PathBuf};
28 use std::sync::{Arc, Mutex};
29 use std::time::Duration;
30 
31 use simpleperf_etm::SimpleperfEtmTraceProvider;
32 use simpleperf_lbr::SimpleperfLbrTraceProvider;
33 
34 #[cfg(feature = "test")]
35 use logging::LoggingTraceProvider;
36 
37 pub trait TraceProvider {
get_name(&self) -> &'static str38     fn get_name(&self) -> &'static str;
is_ready(&self) -> bool39     fn is_ready(&self) -> bool;
trace_system( &self, trace_dir: &Path, tag: &str, sampling_period: &Duration, binary_filter: &str, )40     fn trace_system(
41         &self,
42         trace_dir: &Path,
43         tag: &str,
44         sampling_period: &Duration,
45         binary_filter: &str,
46     );
trace_process( &self, trace_dir: &Path, tag: &str, sampling_period: &Duration, processes: &str, )47     fn trace_process(
48         &self,
49         trace_dir: &Path,
50         tag: &str,
51         sampling_period: &Duration,
52         processes: &str,
53     );
process(&self, trace_dir: &Path, profile_dir: &Path, binary_filter: &str) -> Result<()>54     fn process(&self, trace_dir: &Path, profile_dir: &Path, binary_filter: &str) -> Result<()>;
set_log_file(&self, filename: &Path)55     fn set_log_file(&self, filename: &Path);
reset_log_file(&self)56     fn reset_log_file(&self);
57 }
58 
get_trace_provider() -> Result<Arc<Mutex<dyn TraceProvider + Send>>>59 pub fn get_trace_provider() -> Result<Arc<Mutex<dyn TraceProvider + Send>>> {
60     if SimpleperfEtmTraceProvider::supported() {
61         log::info!("simpleperf_etm trace provider registered.");
62         return Ok(Arc::new(Mutex::new(SimpleperfEtmTraceProvider {})));
63     }
64     if SimpleperfLbrTraceProvider::supported() {
65         log::info!("simpleperf_lbr trace provider registered.");
66         return Ok(Arc::new(Mutex::new(SimpleperfLbrTraceProvider {})));
67     }
68 
69     #[cfg(feature = "test")]
70     if LoggingTraceProvider::supported() {
71         log::info!("logging trace provider registered.");
72         return Ok(Arc::new(Mutex::new(LoggingTraceProvider {})));
73     }
74 
75     Err(anyhow!("No trace provider found for this device."))
76 }
77 
get_path(dir: &Path, tag: &str, ext: &str) -> Box<Path>78 pub fn get_path(dir: &Path, tag: &str, ext: &str) -> Box<Path> {
79     let filename = format!("{}_{}", Utc::now().format("%Y%m%d-%H%M%S"), tag);
80     let mut trace_file = PathBuf::from(dir);
81     trace_file.push(filename);
82     trace_file.set_extension(ext);
83     trace_file.into_boxed_path()
84 }
85