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;
22 use std::sync::{Arc, Mutex};
23 use std::time::Duration;
24
25 use crate::simpleperf_etm_trace_provider::SimpleperfEtmTraceProvider;
26
27 pub trait TraceProvider {
get_name(&self) -> &'static str28 fn get_name(&self) -> &'static str;
trace(&self, trace_dir: &Path, tag: &str, sampling_period: &Duration)29 fn trace(&self, trace_dir: &Path, tag: &str, sampling_period: &Duration);
process(&self, trace_dir: &Path, profile_dir: &Path) -> Result<()>30 fn process(&self, trace_dir: &Path, profile_dir: &Path) -> Result<()>;
31 }
32
get_trace_provider() -> Result<Arc<Mutex<dyn TraceProvider + Send>>>33 pub fn get_trace_provider() -> Result<Arc<Mutex<dyn TraceProvider + Send>>> {
34 if SimpleperfEtmTraceProvider::supported() {
35 log::info!("simpleperf_etm trace provider registered.");
36 return Ok(Arc::new(Mutex::new(SimpleperfEtmTraceProvider {})));
37 }
38
39 Err(anyhow!("No trace provider found for this device."))
40 }
41
construct_file_name(tag: &str) -> String42 pub fn construct_file_name(tag: &str) -> String {
43 format!("{}_{}", Utc::now().format("%Y%m%d-%H%M%S"), tag)
44 }
45