Skip to content

Commit

Permalink
Add support for collecting Aperf Run logs
Browse files Browse the repository at this point in the history
By default, the runlog will always collect at LevelFilter::Debug level.
The users' choice determines what gets printed to the console.

Also, rename the lib to 'aperf'. This will allow us to use the
CARGO_CRATE_NAME when creating the logger.
  • Loading branch information
janaknat committed Dec 2, 2024
1 parent bc0fa31 commit 953d02f
Show file tree
Hide file tree
Showing 12 changed files with 363 additions and 21 deletions.
173 changes: 168 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ anyhow = "1.0"
vergen = { version = "8.3", features = ["build", "git", "gitcl"] }

[lib]
name = "aperf_lib"
name = "aperf"
path = "src/lib.rs"

[[bin]]
Expand All @@ -28,7 +28,6 @@ rustix = { version = "0.38.28", features = ["system"] }
serde_yaml = "0.9"
thiserror = "1.0"
log = "0.4.21"
env_logger = "0.10.0"
lazy_static = "1.4.0"
timerfd = "1.6.0"
procfs = "0.12.0"
Expand All @@ -53,3 +52,4 @@ indexmap = "2.1.0"
cfg-if = "1.0"
tempfile = "3"
serial_test = "3.1.1"
log4rs = "1.3.0"
56 changes: 47 additions & 9 deletions src/bin/aperf.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use anyhow::Result;
use aperf_lib::record::{record, Record};
use aperf_lib::report::{report, Report};
use aperf_lib::{PDError, APERF_TMP};
use aperf::record::{record, Record};
use aperf::report::{report, Report};
use aperf::{PDError, APERF_RUNLOG, APERF_TMP};
use clap::{Parser, Subcommand};
use env_logger::Builder;
use log::LevelFilter;
use std::{fs, os::unix::fs::PermissionsExt};
use log4rs::{
append::console::ConsoleAppender,
append::file::FileAppender,
config::{Appender, Config, Logger, Root},
encode::pattern::PatternEncoder,
filter::threshold::ThresholdFilter,
};
use std::{fs, os::unix::fs::PermissionsExt, path::PathBuf};
use tempfile::Builder as TempBuilder;

#[derive(Parser)]
Expand Down Expand Up @@ -35,14 +41,45 @@ enum Commands {
Report(Report),
}

fn init_logger(verbose: u8) -> Result<()> {
fn init_logger(verbose: u8, runlog: &PathBuf) -> Result<()> {
let level = match verbose {
0 => LevelFilter::Info,
1 => LevelFilter::Debug,
2 => LevelFilter::Trace,
_ => return Err(PDError::InvalidVerboseOption.into()),
};
Builder::new().filter_level(level).init();
let pattern = "[{d(%Y-%m-%dT%H:%M:%SZ)} {h({l}):5.5} {M}] {m}{n}";
let stdout = ConsoleAppender::builder()
.encoder(Box::new(PatternEncoder::new(pattern)))
.build();

let fileout = FileAppender::builder()
.encoder(Box::new(PatternEncoder::new(pattern)))
.build(runlog)?;

let config = Config::builder()
/* This prints only the user selected level to the console. */
.appender(
Appender::builder()
.filter(Box::new(ThresholdFilter::new(level)))
.build("stdout", Box::new(stdout)),
)
.appender(Appender::builder().build("aperflog", Box::new(fileout)))
/* This creates a logger for our module at a default Debug level. */
.logger(
Logger::builder()
.appender("aperflog")
.appender("stdout")
.build(env!("CARGO_CRATE_NAME"), LevelFilter::Debug),
)
.build(
/* Set the Root to Warn. Underlying dependencies also print if set to debug.
* See: https://github.com/estk/log4rs/issues/196
*/
Root::builder().build(LevelFilter::Warn),
)?;

log4rs::init_config(config)?;
Ok(())
}

Expand All @@ -54,11 +91,12 @@ fn main() -> Result<()> {
.tempdir_in(&cli.tmp_dir)?;
fs::set_permissions(&tmp_dir, fs::Permissions::from_mode(0o1777))?;
let tmp_dir_path_buf = tmp_dir.path().to_path_buf();
let runlog = tmp_dir_path_buf.join(APERF_RUNLOG);

init_logger(cli.verbose)?;
init_logger(cli.verbose, &runlog)?;

match cli.command {
Commands::Record(r) => record(&r, &tmp_dir_path_buf),
Commands::Record(r) => record(&r, &tmp_dir_path_buf, &runlog),
Commands::Report(r) => report(&r, &tmp_dir_path_buf),
}?;
fs::remove_dir_all(tmp_dir_path_buf)?;
Expand Down
Loading

0 comments on commit 953d02f

Please sign in to comment.