From bff901df3ae17a54add50ebe53ba38f30b4d8218 Mon Sep 17 00:00:00 2001 From: Lance Lui Date: Tue, 23 Jul 2024 15:05:53 +0000 Subject: [PATCH] asprof flamegraphs saved in tmp folder, duplicate jvm arg fix --- Cargo.lock | 83 +++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/data/java_profile.rs | 30 +++++++++++---- src/record.rs | 12 ++++++ tests/test_aperf.rs | 3 ++ 5 files changed, 119 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b937e097..c75ad8d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -125,6 +125,7 @@ dependencies = [ "serde_json", "serde_urlencoded", "serde_yaml", + "serial_test", "strum", "strum_macros", "sysctl", @@ -905,6 +906,21 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "futures" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f21eda599937fba36daeb58a22e8f5cee2d14c4a17b5b7739c7c8e5e3b8230c" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.24" @@ -912,6 +928,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "30bdd20c28fadd505d0fd6712cdfcb0d4b5648baf45faef7f852afb2399bb050" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -920,6 +937,23 @@ version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e5aa3de05362c3fb88de6531e6296e85cde7739cccad4b9dfeeb7f6ebce56bf" +[[package]] +name = "futures-executor" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ff63c23854bee61b6e9cd331d523909f238fc7636290b96826e9cfa5faa00ab" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + [[package]] name = "futures-sink" version = "0.3.24" @@ -938,10 +972,15 @@ version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "44fb6cb1be61cc1d2e43b262516aafcf63b241cffdb1d3fa115f91d9c7b09c90" dependencies = [ + "futures-channel", "futures-core", + "futures-io", + "futures-sink", "futures-task", + "memchr", "pin-project-lite", "pin-utils", + "slab", ] [[package]] @@ -1380,9 +1419,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.18.0" +version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "openssl-probe" @@ -1749,6 +1788,15 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "scc" +version = "2.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4465c22496331e20eb047ff46e7366455bc01c0c02015c4a376de0b2cd3a1af" +dependencies = [ + "sdd", +] + [[package]] name = "schannel" version = "0.1.20" @@ -1781,6 +1829,12 @@ dependencies = [ "untrusted 0.9.0", ] +[[package]] +name = "sdd" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85f05a494052771fc5bd0619742363b5e24e5ad72ab3111ec2e27925b8edc5f3" + [[package]] name = "security-framework" version = "2.7.0" @@ -1866,6 +1920,31 @@ dependencies = [ "unsafe-libyaml", ] +[[package]] +name = "serial_test" +version = "3.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b4b487fe2acf240a021cf57c6b2b4903b1e78ca0ecd862a71b71d2a51fed77d" +dependencies = [ + "futures", + "log", + "once_cell", + "parking_lot", + "scc", + "serial_test_derive", +] + +[[package]] +name = "serial_test_derive" +version = "3.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82fe9db325bcef1fbcde82e078a5cc4efdf787e96b3b9cf45b50b529f2083d67" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.60", +] + [[package]] name = "sha2" version = "0.10.8" diff --git a/Cargo.toml b/Cargo.toml index 5f31c95f..5276c5ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,3 +51,4 @@ inferno = "0.11.19" indexmap = "2.1.0" cfg-if = "1.0" tempfile = "3" +serial_test = "3.1.1" diff --git a/src/data/java_profile.rs b/src/data/java_profile.rs index d9ec0f4b..6645bbfe 100644 --- a/src/data/java_profile.rs +++ b/src/data/java_profile.rs @@ -39,17 +39,17 @@ impl JavaProfileRaw { } fn launch_asprof(&self, jids: Vec, params: CollectorParams) -> Result<()> { - let data_dir = PathBuf::from(params.data_dir.clone()); for jid in &jids { - let mut html_loc = data_dir.clone(); - html_loc.push(format!("{}-java-flamegraph-{}.html", params.run_name, jid)); - match Command::new("asprof") .args([ "-d", &(params.collection_time - params.elapsed_time).to_string(), "-f", - html_loc.to_str().unwrap(), + format!( + "/tmp/aperf_tmp/{}-java-flamegraph-{}.html", + params.run_name, jid + ) + .as_str(), jid.as_str(), ]) .spawn() @@ -79,7 +79,7 @@ impl JavaProfileRaw { fn get_jids(&mut self, arg: &str) -> Vec { let mut jids: Vec = Vec::new(); for (key, value) in self.process_map.clone().into_iter() { - if arg == value[0] { + if arg == value[0] || arg == key { jids.push(key); } } @@ -163,12 +163,13 @@ impl CollectData for JavaProfileRaw { error!("No JVM with name/PID '{}'.", arg); continue; } - jids = self.get_jids(arg); + jids.append(&mut self.get_jids(arg)); } } } } - + jids.sort(); + jids.dedup(); self.launch_asprof(jids, params.clone()) } @@ -211,6 +212,19 @@ impl CollectData for JavaProfileRaw { } let data_dir = PathBuf::from(params.data_dir.clone()); + for key in self.process_map.keys() { + let mut html_path = data_dir.clone(); + html_path.push(format!("{}-java-flamegraph-{}.html", params.run_name, key)); + + let html_loc = html_path.to_str().unwrap(); + let tmp_loc = format!( + "/tmp/aperf_tmp/{}-java-flamegraph-{}.html", + params.run_name, key + ); + + fs::copy(tmp_loc.clone(), html_loc).ok(); + } + let mut jps_map = File::create( data_dir .clone() diff --git a/src/record.rs b/src/record.rs index 83e515ce..e6871faa 100644 --- a/src/record.rs +++ b/src/record.rs @@ -2,6 +2,10 @@ use crate::{data, InitParams, PERFORMANCE_DATA}; use anyhow::Result; use clap::Args; use log::{debug, error, info}; +use std::fs; +use std::os::unix::fs::PermissionsExt; + +pub static APERF_TMP: &str = "/tmp/aperf_tmp"; #[derive(Args, Debug)] pub struct Record { @@ -82,6 +86,12 @@ pub fn record(record: &Record) -> Result<()> { ); } + fs::remove_dir_all(APERF_TMP).ok(); + fs::create_dir(APERF_TMP)?; + let mut perms: fs::Permissions = fs::metadata(APERF_TMP)?.permissions(); + perms.set_mode(0o777); + fs::set_permissions(APERF_TMP, perms)?; + PERFORMANCE_DATA.lock().unwrap().set_params(params); PERFORMANCE_DATA.lock().unwrap().init_collectors()?; info!("Starting Data collection..."); @@ -89,5 +99,7 @@ pub fn record(record: &Record) -> Result<()> { collect_static_data()?; start_collection_serial()?; info!("Data collection complete."); + + fs::remove_dir_all(APERF_TMP)?; Ok(()) } diff --git a/tests/test_aperf.rs b/tests/test_aperf.rs index 24467b2d..4b9abd1a 100644 --- a/tests/test_aperf.rs +++ b/tests/test_aperf.rs @@ -2,6 +2,7 @@ use anyhow::Result; use aperf_lib::record::{record, Record}; use aperf_lib::report::{report, Report}; use flate2::read::GzDecoder; +use serial_test::serial; use std::path::{Path, PathBuf}; use std::{fs, panic}; use tar::Archive; @@ -20,6 +21,7 @@ where } #[test] +#[serial] fn test_record() { run_test(|tempdir| { let run_name = tempdir @@ -46,6 +48,7 @@ fn test_record() { } #[test] +#[serial] fn test_report() { run_test(|tempdir| { let run_name = tempdir