git.delta.rocks / jrsonnet / refs/commits / 0031c6e45bf8

difftreelog

feat gc options

Yaroslav Bolyukin2021-07-04parent: #a9d1e03.patch.diff
in: master

4 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -158,6 +158,7 @@
 dependencies = [
  "clap",
  "jrsonnet-evaluator",
+ "jrsonnet-gc",
  "jrsonnet-parser",
 ]
 
modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -1,5 +1,5 @@
 use clap::{AppSettings, Clap, IntoApp};
-use jrsonnet_cli::{ConfigureState, GeneralOpts, InputOpts, ManifestOpts, OutputOpts};
+use jrsonnet_cli::{ConfigureState, GcOpts, GeneralOpts, InputOpts, ManifestOpts, OutputOpts};
 use jrsonnet_evaluator::{error::LocError, EvaluationState, ManifestFormat};
 use std::{
 	fs::{create_dir_all, File},
@@ -61,6 +61,8 @@
 	output: OutputOpts,
 	#[clap(flatten)]
 	debug: DebugOpts,
+	#[clap(flatten)]
+	gc: GcOpts,
 }
 
 fn main() {
@@ -114,6 +116,7 @@
 }
 
 fn main_catch(opts: Opts) -> bool {
+	let _printer = opts.gc.stats_printer();
 	let state = EvaluationState::default();
 	if let Err(e) = main_real(&state, opts) {
 		if let Error::Evaluation(e) = e {
@@ -127,6 +130,7 @@
 }
 
 fn main_real(state: &EvaluationState, opts: Opts) -> Result<(), Error> {
+	opts.gc.configure_global();
 	opts.general.configure(&state)?;
 	opts.manifest.configure(&state)?;
 
modifiedcrates/jrsonnet-cli/Cargo.tomldiffbeforeafterboth
before · crates/jrsonnet-cli/Cargo.toml
1[package]2name = "jrsonnet-cli"3description = "Utilities for building jrsonnet CLIs"4version = "0.3.8"5authors = ["Yaroslav Bolyukin <iam@lach.pw>"]6license = "MIT"7edition = "2018"8publish = false910[dependencies]11jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.3.6", features = ["explaining-traces"] }12jrsonnet-parser = { path = "../../crates/jrsonnet-parser", version = "0.3.6" }1314[dependencies.clap]15git = "https://github.com/clap-rs/clap"16rev = "92f744cc49d12d32261010d355dc215a6d2487b9"
after · crates/jrsonnet-cli/Cargo.toml
1[package]2name = "jrsonnet-cli"3description = "Utilities for building jrsonnet CLIs"4version = "0.3.8"5authors = ["Yaroslav Bolyukin <iam@lach.pw>"]6license = "MIT"7edition = "2018"8publish = false910[dependencies]11jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.3.6", features = ["explaining-traces"] }12jrsonnet-parser = { path = "../../crates/jrsonnet-parser", version = "0.3.6" }13jrsonnet-gc = { version = "0.4.2", features = ["derive", "unstable-config", "unstable-stats"] }1415[dependencies.clap]16git = "https://github.com/clap-rs/clap"17rev = "92f744cc49d12d32261010d355dc215a6d2487b9"
modifiedcrates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/lib.rs
+++ b/crates/jrsonnet-cli/src/lib.rs
@@ -95,3 +95,55 @@
 		Ok(())
 	}
 }
+
+#[derive(Clap)]
+#[clap(help_heading = "GARBAGE COLLECTION")]
+pub struct GcOpts {
+	/// Min bytes allocated to start garbage collection
+	#[clap(long, default_value = "20000000")]
+	gc_initial_threshold: usize,
+	/// How much heap should grow after unsuccessful garbage collection
+	#[clap(long)]
+	gc_used_space_ratio: Option<f64>,
+	/// Do not skip gc on exit
+	#[clap(long)]
+	gc_collect_on_exit: bool,
+	/// Print gc stats before exit
+	#[clap(long)]
+	gc_print_stats: bool,
+	/// Force garbage collection before printing stats
+	/// Useful for checking for memory leaks
+	/// Does nothing useless --gc-print-stats is specified
+	#[clap(long)]
+	gc_collect_before_printing_stats: bool,
+}
+impl GcOpts {
+	pub fn stats_printer(&self) -> Option<GcStatsPrinter> {
+		self.gc_print_stats
+			.then(|| GcStatsPrinter(self.gc_collect_before_printing_stats))
+	}
+	pub fn configure_global(&self) {
+		jrsonnet_gc::configure(|config| {
+			config.leak_on_drop = !self.gc_collect_on_exit;
+			config.threshold = self.gc_initial_threshold;
+			if let Some(used_space_ratio) = self.gc_used_space_ratio {
+				config.used_space_ratio = used_space_ratio;
+			}
+		});
+	}
+}
+pub struct GcStatsPrinter(bool);
+impl Drop for GcStatsPrinter {
+	fn drop(&mut self) {
+		if self.0 {
+			jrsonnet_gc::force_collect()
+		}
+		eprintln!("=== GC STATS ===");
+		jrsonnet_gc::configure(|c| {
+			eprintln!("Final threshold: {:?}", c.threshold);
+		});
+		let stats = jrsonnet_gc::stats();
+		eprintln!("Collections performed: {}", stats.collections_performed);
+		eprintln!("Bytes still allocated: {}", stats.bytes_allocated);
+	}
+}