diff options
author | Yabin Cui <yabinc@google.com> | 2023-04-27 12:50:59 -0700 |
---|---|---|
committer | Yabin Cui <yabinc@google.com> | 2023-05-01 11:20:56 -0700 |
commit | f1d91d222eb77a04bfe7449816da6ee417365edd (patch) | |
tree | 7be4ff684eea31fee44bd52ddda750509b215eca /profcollectd | |
parent | c759ef2c8c6b3cb799025dd6bcd4297419cf4356 (diff) | |
download | extras-f1d91d222eb77a04bfe7449816da6ee417365edd.tar.gz |
profcollectd: Add a log file in the report
This is to detect possible errors while collecting ETM data.
1. Create a trace.log file in the output directory. It will
be put in the report zip file.
2. Register a custom log function writing to the log file via
SetLogger. So both rust and c++ code can log to the file.
3. Add log messages when calling simpleperf functions.
4. The log file is cleared after config change (e.g. after a device
update).
Bug: 272547391
Test: run profcollectd manually
Change-Id: I1f0d5e5209eab46f1b9893406ba365da43dd77c2
Diffstat (limited to 'profcollectd')
-rw-r--r-- | profcollectd/libprofcollectd/config.rs | 3 | ||||
-rw-r--r-- | profcollectd/libprofcollectd/logging_trace_provider.rs | 3 | ||||
-rw-r--r-- | profcollectd/libprofcollectd/scheduler.rs | 14 | ||||
-rw-r--r-- | profcollectd/libprofcollectd/service.rs | 1 | ||||
-rw-r--r-- | profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs | 8 | ||||
-rw-r--r-- | profcollectd/libprofcollectd/trace_provider.rs | 2 |
6 files changed, 29 insertions, 2 deletions
diff --git a/profcollectd/libprofcollectd/config.rs b/profcollectd/libprofcollectd/config.rs index d68f02e7..2888aab7 100644 --- a/profcollectd/libprofcollectd/config.rs +++ b/profcollectd/libprofcollectd/config.rs @@ -40,6 +40,7 @@ lazy_static! { pub static ref REPORT_OUTPUT_DIR: &'static Path = Path::new("/data/misc/profcollectd/report/"); pub static ref CONFIG_FILE: &'static Path = Path::new("/data/misc/profcollectd/output/config.json"); + pub static ref LOG_FILE: &'static Path = Path::new("/data/misc/profcollectd/output/trace.log"); } /// Dynamic configs, stored in config.json. @@ -151,7 +152,7 @@ pub fn clear_data() -> Result<()> { read_dir(path)? .filter_map(|e| e.ok()) .map(|e| e.path()) - .filter(|e| e.is_file()) + .filter(|e| e.is_file() && e != *LOG_FILE) .try_for_each(remove_file)?; Ok(()) } diff --git a/profcollectd/libprofcollectd/logging_trace_provider.rs b/profcollectd/libprofcollectd/logging_trace_provider.rs index fda4c66a..0b9e36fc 100644 --- a/profcollectd/libprofcollectd/logging_trace_provider.rs +++ b/profcollectd/libprofcollectd/logging_trace_provider.rs @@ -51,6 +51,9 @@ impl TraceProvider for LoggingTraceProvider { log::info!("Process event triggered"); Ok(()) } + + fn set_log_file(&self, filename: &Path) {} + fn reset_log_file(&self) {} } impl LoggingTraceProvider { diff --git a/profcollectd/libprofcollectd/scheduler.rs b/profcollectd/libprofcollectd/scheduler.rs index 9af4d5d3..c4d68805 100644 --- a/profcollectd/libprofcollectd/scheduler.rs +++ b/profcollectd/libprofcollectd/scheduler.rs @@ -25,7 +25,7 @@ use std::sync::Mutex; use std::thread; use std::time::{Duration, Instant}; -use crate::config::{Config, PROFILE_OUTPUT_DIR, TRACE_OUTPUT_DIR}; +use crate::config::{Config, LOG_FILE, PROFILE_OUTPUT_DIR, TRACE_OUTPUT_DIR}; use crate::trace_provider::{self, TraceProvider}; use anyhow::{anyhow, ensure, Context, Result}; @@ -41,6 +41,7 @@ pub struct Scheduler { impl Scheduler { pub fn new() -> Result<Self> { let p = trace_provider::get_trace_provider()?; + p.lock().map_err(|e| anyhow!(e.to_string()))?.set_log_file(&LOG_FILE); Ok(Scheduler { termination_ch: None, trace_provider: p, @@ -158,6 +159,17 @@ impl Scheduler { } }); } + + pub fn clear_trace_log(&self) -> Result<()> { + let provider = self.trace_provider.lock().map_err(|e| anyhow!(e.to_string()))?; + provider.reset_log_file(); + let mut result = Ok(()); + if LOG_FILE.exists() { + result = fs::remove_file(*LOG_FILE).map_err(|e| anyhow!(e)); + } + provider.set_log_file(&LOG_FILE); + result + } } /// Run if space usage is under limit. diff --git a/profcollectd/libprofcollectd/service.rs b/profcollectd/libprofcollectd/service.rs index 8ae51527..87c3b3cf 100644 --- a/profcollectd/libprofcollectd/service.rs +++ b/profcollectd/libprofcollectd/service.rs @@ -132,6 +132,7 @@ impl ProfcollectdBinderService { clear_data()?; write(*CONFIG_FILE, new_config.to_string())?; + new_scheduler.clear_trace_log()?; } // Clear profile reports out of rentention period. diff --git a/profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs b/profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs index 14911ffb..6b326b4a 100644 --- a/profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs +++ b/profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs @@ -76,6 +76,14 @@ impl TraceProvider for SimpleperfEtmTraceProvider { .filter(is_etm_extension) .try_for_each(process_trace_file) } + + fn set_log_file(&self, filename: &Path) { + simpleperf_profcollect::set_log_file(filename); + } + + fn reset_log_file(&self) { + simpleperf_profcollect::reset_log_file(); + } } impl SimpleperfEtmTraceProvider { diff --git a/profcollectd/libprofcollectd/trace_provider.rs b/profcollectd/libprofcollectd/trace_provider.rs index 13059199..704eb9cb 100644 --- a/profcollectd/libprofcollectd/trace_provider.rs +++ b/profcollectd/libprofcollectd/trace_provider.rs @@ -32,6 +32,8 @@ pub trait TraceProvider { fn is_ready(&self) -> bool; fn trace(&self, trace_dir: &Path, tag: &str, sampling_period: &Duration); fn process(&self, trace_dir: &Path, profile_dir: &Path, binary_filter: &str) -> Result<()>; + fn set_log_file(&self, filename: &Path); + fn reset_log_file(&self); } pub fn get_trace_provider() -> Result<Arc<Mutex<dyn TraceProvider + Send>>> { |