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
--- a/crates/jrsonnet-cli/Cargo.toml
+++ b/crates/jrsonnet-cli/Cargo.toml
@@ -10,6 +10,7 @@
 [dependencies]
 jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.3.6", features = ["explaining-traces"] }
 jrsonnet-parser = { path = "../../crates/jrsonnet-parser", version = "0.3.6" }
+jrsonnet-gc = { version = "0.4.2", features = ["derive", "unstable-config", "unstable-stats"] }
 
 [dependencies.clap]
 git = "https://github.com/clap-rs/clap"
modifiedcrates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth
96 }96 }
97}97}
98
99#[derive(Clap)]
100#[clap(help_heading = "GARBAGE COLLECTION")]
101pub struct GcOpts {
102 /// Min bytes allocated to start garbage collection
103 #[clap(long, default_value = "20000000")]
104 gc_initial_threshold: usize,
105 /// How much heap should grow after unsuccessful garbage collection
106 #[clap(long)]
107 gc_used_space_ratio: Option<f64>,
108 /// Do not skip gc on exit
109 #[clap(long)]
110 gc_collect_on_exit: bool,
111 /// Print gc stats before exit
112 #[clap(long)]
113 gc_print_stats: bool,
114 /// Force garbage collection before printing stats
115 /// Useful for checking for memory leaks
116 /// Does nothing useless --gc-print-stats is specified
117 #[clap(long)]
118 gc_collect_before_printing_stats: bool,
119}
120impl GcOpts {
121 pub fn stats_printer(&self) -> Option<GcStatsPrinter> {
122 self.gc_print_stats
123 .then(|| GcStatsPrinter(self.gc_collect_before_printing_stats))
124 }
125 pub fn configure_global(&self) {
126 jrsonnet_gc::configure(|config| {
127 config.leak_on_drop = !self.gc_collect_on_exit;
128 config.threshold = self.gc_initial_threshold;
129 if let Some(used_space_ratio) = self.gc_used_space_ratio {
130 config.used_space_ratio = used_space_ratio;
131 }
132 });
133 }
134}
135pub struct GcStatsPrinter(bool);
136impl Drop for GcStatsPrinter {
137 fn drop(&mut self) {
138 if self.0 {
139 jrsonnet_gc::force_collect()
140 }
141 eprintln!("=== GC STATS ===");
142 jrsonnet_gc::configure(|c| {
143 eprintln!("Final threshold: {:?}", c.threshold);
144 });
145 let stats = jrsonnet_gc::stats();
146 eprintln!("Collections performed: {}", stats.collections_performed);
147 eprintln!("Bytes still allocated: {}", stats.bytes_allocated);
148 }
149}
98150