Skip to content

Commit

Permalink
Changed CollectorParams paths to PathBuf from String
Browse files Browse the repository at this point in the history
  • Loading branch information
lancelui-amzn committed Jul 24, 2024
1 parent d4268ec commit 07227d9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
21 changes: 11 additions & 10 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::{File, OpenOptions};
use std::ops::Sub;
use std::path::PathBuf;
use sysctldata::SysctlData;
use systeminfo::SystemInfo;
use vmstat::{Vmstat, VmstatRaw};
Expand All @@ -54,23 +55,23 @@ use vmstat::{Vmstat, VmstatRaw};
pub struct CollectorParams {
pub collection_time: u64,
pub elapsed_time: u64,
pub data_file_path: String,
pub data_dir: String,
pub data_file_path: PathBuf,
pub data_dir: PathBuf,
pub run_name: String,
pub profile: HashMap<String, String>,
pub tmp_dir: String,
pub tmp_dir: PathBuf,
}

impl CollectorParams {
fn new() -> Self {
CollectorParams {
collection_time: 0,
elapsed_time: 0,
data_file_path: String::new(),
data_dir: String::new(),
data_file_path: PathBuf::new(),
data_dir: PathBuf::new(),
run_name: String::new(),
profile: HashMap::new(),
tmp_dir: String::new(),
tmp_dir: PathBuf::new(),
}
}
}
Expand Down Expand Up @@ -117,15 +118,15 @@ impl DataType {
let full_path = format!("{}/{}", param.dir_name, name);

self.file_name = name;
self.full_path = full_path;
self.full_path = full_path.clone();
self.dir_name = param.dir_name.clone();
self.collector_params.run_name = param.dir_name.clone();
self.collector_params.collection_time = param.period;
self.collector_params.elapsed_time = 0;
self.collector_params.data_file_path = self.full_path.clone();
self.collector_params.data_dir = param.dir_name.clone();
self.collector_params.data_file_path = PathBuf::from(full_path);
self.collector_params.data_dir = PathBuf::from(param.dir_name);
self.collector_params.profile = param.profile.clone();
self.collector_params.tmp_dir = param.tmp_dir.clone();
self.collector_params.tmp_dir = PathBuf::from(param.tmp_dir);

self.file_handle = Some(
OpenOptions::new()
Expand Down
6 changes: 4 additions & 2 deletions src/data/flamegraphs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ impl CollectData for FlamegraphRaw {
fn after_data_collection(&mut self, params: CollectorParams) -> Result<()> {
let data_dir = PathBuf::from(&params.data_dir);

let file_pathbuf =
data_dir.join(get_file_name(params.data_dir, "perf_profile".to_string())?);
let file_pathbuf = data_dir.join(get_file_name(
params.data_dir.display().to_string(),
"perf_profile".to_string(),
)?);

let perf_jit_loc = data_dir.join("perf.data.jit");

Expand Down
11 changes: 7 additions & 4 deletions src/data/java_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use log::{debug, error, trace};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::io::Write;
use std::path::PathBuf;
use std::process::{Child, Command};
use std::sync::Mutex;
use std::{fs, fs::File};
Expand Down Expand Up @@ -47,7 +46,9 @@ impl JavaProfileRaw {
"-f",
format!(
"{}/{}-java-flamegraph-{}.html",
params.tmp_dir, params.run_name, jid
params.tmp_dir.display(),
params.run_name,
jid
)
.as_str(),
jid.as_str(),
Expand Down Expand Up @@ -211,15 +212,17 @@ impl CollectData for JavaProfileRaw {
}
}

let data_dir = PathBuf::from(params.data_dir.clone());
let data_dir = 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!(
"{}/{}-java-flamegraph-{}.html",
params.tmp_dir, params.run_name, key
params.tmp_dir.display(),
params.run_name,
key
);

fs::rename(tmp_loc.clone(), html_loc).ok();
Expand Down
8 changes: 4 additions & 4 deletions src/data/perf_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use anyhow::Result;
use ctor::ctor;
use log::{error, trace};
use serde::{Deserialize, Serialize};
use std::fs;
use std::io::Write;
use std::process::{Child, Command, Stdio};
use std::sync::Mutex;
use std::{fs, path::PathBuf};

pub static PERF_PROFILE_FILE_NAME: &str = "perf_profile";
pub static PERF_TOP_FUNCTIONS_FILE_NAME: &str = "top_functions";
Expand Down Expand Up @@ -48,7 +48,7 @@ impl CollectData for PerfProfileRaw {
"-e",
"cpu-clock:pppH",
"-o",
&params.data_file_path,
&params.data_file_path.display().to_string(),
"--",
"sleep",
&params.collection_time.to_string(),
Expand Down Expand Up @@ -88,7 +88,7 @@ impl CollectData for PerfProfileRaw {
Ok(_) => trace!("'perf record' executed successfully."),
}
let mut top_functions_file =
fs::File::create(PathBuf::from(params.data_dir).join(PERF_TOP_FUNCTIONS_FILE_NAME))?;
fs::File::create(params.data_dir.join(PERF_TOP_FUNCTIONS_FILE_NAME))?;

let out = Command::new("perf")
.args([
Expand All @@ -97,7 +97,7 @@ impl CollectData for PerfProfileRaw {
"--percent-limit",
"1",
"-i",
&params.data_file_path,
&params.data_file_path.display().to_string(),
])
.output();

Expand Down

0 comments on commit 07227d9

Please sign in to comment.