diff options
-rw-r--r-- | profcollectd/libprofcollectd/Android.bp | 3 | ||||
-rw-r--r-- | profcollectd/libprofcollectd/lib.rs | 8 | ||||
-rw-r--r-- | profcollectd/libprofcollectd/logging_trace_provider.rs | 56 | ||||
-rw-r--r-- | profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs | 6 | ||||
-rw-r--r-- | profcollectd/libprofcollectd/trace_provider.rs | 19 |
5 files changed, 82 insertions, 10 deletions
diff --git a/profcollectd/libprofcollectd/Android.bp b/profcollectd/libprofcollectd/Android.bp index 2e37af9e..7fb1b567 100644 --- a/profcollectd/libprofcollectd/Android.bp +++ b/profcollectd/libprofcollectd/Android.bp @@ -60,4 +60,7 @@ rust_library { "libsimpleperf_profcollect_rust", ], shared_libs: ["libsimpleperf_profcollect"], + + // Enable 'test' feature for more verbose logging and the logging trace provider. + // features: ["test"], } diff --git a/profcollectd/libprofcollectd/lib.rs b/profcollectd/libprofcollectd/lib.rs index aacbf8e3..f417bd8c 100644 --- a/profcollectd/libprofcollectd/lib.rs +++ b/profcollectd/libprofcollectd/lib.rs @@ -23,6 +23,9 @@ mod service; mod simpleperf_etm_trace_provider; mod trace_provider; +#[cfg(feature = "test")] +mod logging_trace_provider; + use anyhow::{Context, Result}; use profcollectd_aidl_interface::aidl::com::android::server::profcollect::IProfCollectd::{ self, BnProfCollectd, @@ -90,9 +93,8 @@ pub fn report() -> Result<String> { /// Inits logging for Android pub fn init_logging() { + let min_log_level = if cfg!(feature = "test") { log::Level::Info } else { log::Level::Error }; android_logger::init_once( - android_logger::Config::default() - .with_tag("profcollectd") - .with_min_level(log::Level::Error), + android_logger::Config::default().with_tag("profcollectd").with_min_level(min_log_level), ); } diff --git a/profcollectd/libprofcollectd/logging_trace_provider.rs b/profcollectd/libprofcollectd/logging_trace_provider.rs new file mode 100644 index 00000000..1325855d --- /dev/null +++ b/profcollectd/libprofcollectd/logging_trace_provider.rs @@ -0,0 +1,56 @@ +// +// Copyright (C) 2021 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +//! Logging trace provider for development and testing purposes. + +use anyhow::Result; +use std::path::Path; +use std::time::Duration; +use trace_provider::TraceProvider; + +use crate::trace_provider; + +static LOGGING_TRACEFILE_EXTENSION: &str = "loggingtrace"; + +pub struct LoggingTraceProvider {} + +impl TraceProvider for LoggingTraceProvider { + fn get_name(&self) -> &'static str { + "logging" + } + + fn trace(&self, trace_dir: &Path, tag: &str, sampling_period: &Duration) { + let trace_file = trace_provider::get_path(trace_dir, tag, LOGGING_TRACEFILE_EXTENSION); + + log::info!( + "Trace event triggered, tag {}, sampling for {}ms, saving to {}", + tag, + sampling_period.as_millis(), + trace_file.display() + ); + } + + fn process(&self, _trace_dir: &Path, _profile_dir: &Path) -> Result<()> { + log::info!("Process event triggered"); + Ok(()) + } +} + +impl LoggingTraceProvider { + pub fn supported() -> bool { + true + } +} diff --git a/profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs b/profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs index 2038a5be..d3baaa36 100644 --- a/profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs +++ b/profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs @@ -35,12 +35,10 @@ impl TraceProvider for SimpleperfEtmTraceProvider { } fn trace(&self, trace_dir: &Path, tag: &str, sampling_period: &Duration) { - let mut trace_file = PathBuf::from(trace_dir); - trace_file.push(trace_provider::construct_file_name(tag)); - trace_file.set_extension(ETM_TRACEFILE_EXTENSION); + let trace_file = trace_provider::get_path(trace_dir, tag, ETM_TRACEFILE_EXTENSION); simpleperf_profcollect::record( - &trace_file, + &*trace_file, sampling_period, simpleperf_profcollect::RecordScope::BOTH, ); diff --git a/profcollectd/libprofcollectd/trace_provider.rs b/profcollectd/libprofcollectd/trace_provider.rs index ed0d56f5..44d4cee9 100644 --- a/profcollectd/libprofcollectd/trace_provider.rs +++ b/profcollectd/libprofcollectd/trace_provider.rs @@ -18,12 +18,15 @@ use anyhow::{anyhow, Result}; use chrono::Utc; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; use std::time::Duration; use crate::simpleperf_etm_trace_provider::SimpleperfEtmTraceProvider; +#[cfg(feature = "test")] +use crate::logging_trace_provider::LoggingTraceProvider; + pub trait TraceProvider { fn get_name(&self) -> &'static str; fn trace(&self, trace_dir: &Path, tag: &str, sampling_period: &Duration); @@ -36,9 +39,19 @@ pub fn get_trace_provider() -> Result<Arc<Mutex<dyn TraceProvider + Send>>> { return Ok(Arc::new(Mutex::new(SimpleperfEtmTraceProvider {}))); } + #[cfg(feature = "test")] + if LoggingTraceProvider::supported() { + log::info!("logging trace provider registered."); + return Ok(Arc::new(Mutex::new(LoggingTraceProvider {}))); + } + Err(anyhow!("No trace provider found for this device.")) } -pub fn construct_file_name(tag: &str) -> String { - format!("{}_{}", Utc::now().format("%Y%m%d-%H%M%S"), tag) +pub fn get_path(dir: &Path, tag: &str, ext: &str) -> Box<Path> { + let filename = format!("{}_{}", Utc::now().format("%Y%m%d-%H%M%S"), tag); + let mut trace_file = PathBuf::from(dir); + trace_file.push(filename); + trace_file.set_extension(ext); + trace_file.into_boxed_path() } |