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
27 #[cfg(feature = "test")]
28 use crate::logging_trace_provider::LoggingTraceProvider;
29
30 pub trait TraceProvider {
get_name(&self) -> &'static str31 fn get_name(&self) -> &'static str;
trace(&self, trace_dir: &Path, tag: &str, sampling_period: &Duration)32 fn trace(&self, trace_dir: &Path, tag: &str, sampling_period: &Duration);
process(&self, trace_dir: &Path, profile_dir: &Path) -> Result<()>33 fn process(&self, trace_dir: &Path, profile_dir: &Path) -> Result<()>;
34 }
35
get_trace_provider() -> Result<Arc<Mutex<dyn TraceProvider + Send>>>36 pub fn get_trace_provider() -> Result<Arc<Mutex<dyn TraceProvider + Send>>> {
37 if SimpleperfEtmTraceProvider::supported() {
38 log::info!("simpleperf_etm trace provider registered.");
39 return Ok(Arc::new(Mutex::new(SimpleperfEtmTraceProvider {})));
40 }
41
42 #[cfg(feature = "test")]
43 if LoggingTraceProvider::supported() {
44 log::info!("logging trace provider registered.");
45 return Ok(Arc::new(Mutex::new(LoggingTraceProvider {})));
46 }
47
48 Err(anyhow!("No trace provider found for this device."))
49 }
50
get_path(dir: &Path, tag: &str, ext: &str) -> Box<Path>51 pub fn get_path(dir: &Path, tag: &str, ext: &str) -> Box<Path> {
52 let filename = format!("{}_{}", Utc::now().format("%Y%m%d-%H%M%S"), tag);
53 let mut trace_file = PathBuf::from(dir);
54 trace_file.push(filename);
55 trace_file.set_extension(ext);
56 trace_file.into_boxed_path()
57 }
58