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 use anyhow::{anyhow, Result};
20 use chrono::Utc;
21 use std::path::{Path, PathBuf};
22 use std::sync::{Arc, Mutex};
23 use std::time::Duration;
24
25 use crate::simpleperf_etm_trace_provider::SimpleperfEtmTraceProvider;
26 use crate::simpleperf_lbr_trace_provider::SimpleperfLbrTraceProvider;
27
28 #[cfg(feature = "test")]
29 use crate::logging_trace_provider::LoggingTraceProvider;
30
31 pub trait TraceProvider {
get_name(&self) -> &'static str32 fn get_name(&self) -> &'static str;
is_ready(&self) -> bool33 fn is_ready(&self) -> bool;
trace(&self, trace_dir: &Path, tag: &str, sampling_period: &Duration, binary_filter: &str)34 fn trace(&self, trace_dir: &Path, tag: &str, sampling_period: &Duration, binary_filter: &str);
process(&self, trace_dir: &Path, profile_dir: &Path, binary_filter: &str) -> Result<()>35 fn process(&self, trace_dir: &Path, profile_dir: &Path, binary_filter: &str) -> Result<()>;
set_log_file(&self, filename: &Path)36 fn set_log_file(&self, filename: &Path);
reset_log_file(&self)37 fn reset_log_file(&self);
38 }
39
get_trace_provider() -> Result<Arc<Mutex<dyn TraceProvider + Send>>>40 pub fn get_trace_provider() -> Result<Arc<Mutex<dyn TraceProvider + Send>>> {
41 if SimpleperfEtmTraceProvider::supported() {
42 log::info!("simpleperf_etm trace provider registered.");
43 return Ok(Arc::new(Mutex::new(SimpleperfEtmTraceProvider {})));
44 }
45 if SimpleperfLbrTraceProvider::supported() {
46 log::info!("simpleperf_lbr trace provider registered.");
47 return Ok(Arc::new(Mutex::new(SimpleperfLbrTraceProvider {})));
48 }
49
50 #[cfg(feature = "test")]
51 if LoggingTraceProvider::supported() {
52 log::info!("logging trace provider registered.");
53 return Ok(Arc::new(Mutex::new(LoggingTraceProvider {})));
54 }
55
56 Err(anyhow!("No trace provider found for this device."))
57 }
58
get_path(dir: &Path, tag: &str, ext: &str) -> Box<Path>59 pub fn get_path(dir: &Path, tag: &str, ext: &str) -> Box<Path> {
60 let filename = format!("{}_{}", Utc::now().format("%Y%m%d-%H%M%S"), tag);
61 let mut trace_file = PathBuf::from(dir);
62 trace_file.push(filename);
63 trace_file.set_extension(ext);
64 trace_file.into_boxed_path()
65 }
66