diff options
author | Yi Kong <yikong@google.com> | 2021-03-02 13:58:25 +0800 |
---|---|---|
committer | Yi Kong <yikong@google.com> | 2021-03-03 14:24:33 +0800 |
commit | e3aab14ec0bcbbae6ba6edddbe315e1e128dce5f (patch) | |
tree | 1f5970b315e4c38542d5baf1d663040d877a8f5d /profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs | |
parent | f8abfdbd271086cf276fcf738e123cf36d3eb2ca (diff) | |
download | extras-e3aab14ec0bcbbae6ba6edddbe315e1e128dce5f.tar.gz |
profcollectd: Full rewrite in Rust 🦀
Rewritten from the C++ version. No major feature difference except that
the report zip is not compressed (blocked by upstream bug) and now
stored in a different location (to prepare for upload queue support).
Test: Manual testing
Change-Id: I635c9c56a3870c3b96b31d46df3ef9175490925d
Diffstat (limited to 'profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs')
-rw-r--r-- | profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs b/profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs new file mode 100644 index 00000000..c4b9db76 --- /dev/null +++ b/profcollectd/libprofcollectd/simpleperf_etm_trace_provider.rs @@ -0,0 +1,75 @@ +// +// 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. +// + +//! Trace provider backed by ARM Coresight ETM, using simpleperf tool. + +use anyhow::{anyhow, Result}; +use std::fs::{read_dir, remove_file}; +use std::path::{Path, PathBuf}; +use std::time::Duration; +use trace_provider::TraceProvider; + +use crate::trace_provider; + +static ETM_TRACEFILE_EXTENSION: &str = "etmtrace"; +static ETM_PROFILE_EXTENSION: &str = "data"; + +pub struct SimpleperfEtmTraceProvider {} + +impl TraceProvider for SimpleperfEtmTraceProvider { + fn get_name(&self) -> &'static str { + "simpleperf_etm" + } + + 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); + + simpleperf_profcollect::record(&trace_file, sampling_period) + } + + fn process(&self, trace_dir: &Path, profile_dir: &Path) -> Result<()> { + read_dir(trace_dir)? + .filter_map(|e| e.ok()) + .map(|e| e.path()) + .filter(|e| { + e.is_file() + && e.extension() + .and_then(|f| f.to_str()) + .filter(|ext| ext == &ETM_TRACEFILE_EXTENSION) + .is_some() + }) + .try_for_each(|trace_file| -> Result<()> { + let mut profile_file = PathBuf::from(profile_dir); + profile_file.push( + trace_file + .file_name() + .ok_or_else(|| anyhow!("Malformed trace path: {}", trace_file.display()))?, + ); + profile_file.set_extension(ETM_PROFILE_EXTENSION); + simpleperf_profcollect::process(&trace_file, &profile_file); + remove_file(&trace_file)?; + Ok(()) + }) + } +} + +impl SimpleperfEtmTraceProvider { + pub fn supported() -> bool { + simpleperf_profcollect::has_support() + } +} |