Skip to content

Commit

Permalink
Reduce the number of instrumenting files. If profiling level is INFO,…
Browse files Browse the repository at this point in the history
… only process 0 logs its runtime. If profiling level is TRACE, each process will create a file.
  • Loading branch information
janekdererste committed Jul 15, 2024
1 parent 192614c commit 7ea022f
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions src/simulation/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::io;
use std::path::PathBuf;

use tracing::level_filters::LevelFilter;
use tracing::Level;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_appender::{non_blocking, rolling};
use tracing_subscriber::fmt;
Expand All @@ -25,16 +26,7 @@ pub fn init_logging(config: &Config, part: u32) -> (Option<WorkerGuard>, Option<
let file_discriminant = part.to_string();
let dir = PathBuf::from(&config.output().output_dir);

let (csv_layer, guard) = if let Profiling::CSV(level) = config.output().profiling {
let duration_dir = dir.join("instrument");
let duration_file_name = format!("instrument_process_{file_discriminant}.csv");
let duration_path = duration_dir.join(duration_file_name);
let (layer, writer_guard) =
SpanDurationToCSVLayer::new(&duration_path, level.create_tracing_level());
(Some(layer), Some(writer_guard))
} else {
(None, None)
};
let (csv_layer, guard) = init_tracing(config, part, &file_discriminant, &dir);
let (log_layer, log_guard) = if Logging::Info == config.output().logging {
let log_file_name = format!("log_process_{file_discriminant}.txt");
let log_file_appender = rolling::never(&dir, log_file_name);
Expand Down Expand Up @@ -63,3 +55,29 @@ pub fn init_logging(config: &Config, part: u32) -> (Option<WorkerGuard>, Option<
tracing::subscriber::set_global_default(collector).expect("Unable to set a global collector");
(log_guard, guard)
}

fn init_tracing(
config: &Config,
part: u32,
file_discriminant: &String,
dir: &PathBuf,
) -> (Option<SpanDurationToCSVLayer>, Option<WriterGuard>) {
// if we set profiling at all and if profiling is set to level trace, then each process creates an instrumenting file
// if profiling level is set to INFO, only process 0 creates an instrument files. This is important if we run on a lot of
// processes, because then we spent a lot of computing time on creating instrument files for each process.
let (csv_layer, guard) = if let Profiling::CSV(level_string) = config.output().profiling {
let level = level_string.create_tracing_level();
if level.eq(&Level::INFO) && part == 0 || level.eq(&Level::TRACE) {
let duration_dir = dir.join("instrument");
let duration_file_name = format!("instrument_process_{file_discriminant}.csv");
let duration_path = duration_dir.join(duration_file_name);
let (layer, writer_guard) = SpanDurationToCSVLayer::new(&duration_path, level);
(Some(layer), Some(writer_guard))
} else {
(None, None)
}
} else {
(None, None)
};
(csv_layer, guard)
}

0 comments on commit 7ea022f

Please sign in to comment.