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