difftreelog
feat gc options
in: master
4 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -158,6 +158,7 @@
dependencies = [
"clap",
"jrsonnet-evaluator",
+ "jrsonnet-gc",
"jrsonnet-parser",
]
cmds/jrsonnet/src/main.rsdiffbeforeafterboth1use clap::{AppSettings, Clap, IntoApp};2use jrsonnet_cli::{ConfigureState, GeneralOpts, InputOpts, ManifestOpts, OutputOpts};3use jrsonnet_evaluator::{error::LocError, EvaluationState, ManifestFormat};4use std::{5 fs::{create_dir_all, File},6 io::Read,7 io::Write,8 path::PathBuf,9 str::FromStr,10};1112#[cfg(feature = "mimalloc")]13#[global_allocator]14static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;1516#[derive(Clap)]17#[clap(help_heading = "DEBUG")]18struct DebugOpts {19 /// Required OS stack size.20 /// This shouldn't be changed unless jrsonnet is failing with stack overflow error.21 #[clap(long, name = "size")]22 pub os_stack: Option<usize>,23 /// Generate completions script24 #[clap(long)]25 generate: Option<GenerateTarget>,26}2728enum GenerateTarget {29 Bash,30 Zsh,31 Fish,32 PowerShell,33}34impl FromStr for GenerateTarget {35 type Err = &'static str;3637 fn from_str(s: &str) -> Result<Self, Self::Err> {38 match s {39 "bash" => Ok(Self::Bash),40 "zsh" => Ok(Self::Zsh),41 "fish" => Ok(Self::Fish),42 "powershell" => Ok(Self::PowerShell),43 _ => Err("unknown target"),44 }45 }46}4748#[derive(Clap)]49#[clap(50 global_setting = AppSettings::ColoredHelp,51 global_setting = AppSettings::DeriveDisplayOrder,52)]53struct Opts {54 #[clap(flatten)]55 input: InputOpts,56 #[clap(flatten)]57 general: GeneralOpts,58 #[clap(flatten)]59 manifest: ManifestOpts,60 #[clap(flatten)]61 output: OutputOpts,62 #[clap(flatten)]63 debug: DebugOpts,64}6566fn main() {67 let opts: Opts = Opts::parse();6869 if let Some(target) = opts.debug.generate {70 use clap_generate::{generate, generators};71 use GenerateTarget::*;72 let app = &mut Opts::into_app();73 let buf = &mut std::io::stdout();74 let bin = "jrsonnet";75 match target {76 Bash => generate::<generators::Bash, _>(app, bin, buf),77 Zsh => generate::<generators::Zsh, _>(app, bin, buf),78 Fish => generate::<generators::Fish, _>(app, bin, buf),79 PowerShell => generate::<generators::PowerShell, _>(app, bin, buf),80 }81 std::process::exit(0);82 };8384 let success;85 if let Some(size) = opts.debug.os_stack {86 success = std::thread::Builder::new()87 .stack_size(size * 1024 * 1024)88 .spawn(|| main_catch(opts))89 .expect("new thread spawned")90 .join()91 .expect("thread finished successfully");92 } else {93 success = main_catch(opts)94 }95 if !success {96 std::process::exit(1);97 }98}99100#[derive(thiserror::Error, Debug)]101enum Error {102 // Handled differently103 #[error("evaluation error")]104 Evaluation(jrsonnet_evaluator::error::LocError),105 #[error("io error")]106 Io(#[from] std::io::Error),107 #[error("input is not utf8 encoded")]108 Utf8(#[from] std::str::Utf8Error),109}110impl From<LocError> for Error {111 fn from(e: LocError) -> Self {112 Self::Evaluation(e)113 }114}115116fn main_catch(opts: Opts) -> bool {117 let state = EvaluationState::default();118 if let Err(e) = main_real(&state, opts) {119 if let Error::Evaluation(e) = e {120 eprintln!("{}", state.stringify_err(&e));121 } else {122 eprintln!("{}", e);123 }124 return false;125 }126 true127}128129fn main_real(state: &EvaluationState, opts: Opts) -> Result<(), Error> {130 opts.general.configure(&state)?;131 opts.manifest.configure(&state)?;132133 let val = if opts.input.exec {134 state.set_manifest_format(ManifestFormat::ToString);135 state.evaluate_snippet_raw(136 PathBuf::from("args").into(),137 (&opts.input.input as &str).into(),138 )?139 } else if opts.input.input == "-" {140 let mut input = Vec::new();141 std::io::stdin().read_to_end(&mut input)?;142 let input_str = std::str::from_utf8(&input)?.into();143 state.evaluate_snippet_raw(PathBuf::from("<stdin>").into(), input_str)?144 } else {145 state.evaluate_file_raw(&PathBuf::from(opts.input.input))?146 };147148 let val = state.with_tla(val)?;149150 if let Some(multi) = opts.output.multi {151 if opts.output.create_output_dirs {152 let mut dir = multi.clone();153 dir.pop();154 create_dir_all(dir)?;155 }156 for (file, data) in state.manifest_multi(val)?.iter() {157 let mut path = multi.clone();158 path.push(&file as &str);159 if opts.output.create_output_dirs {160 let mut dir = path.clone();161 dir.pop();162 create_dir_all(dir)?;163 }164 println!("{}", path.to_str().expect("path"));165 let mut file = File::create(path)?;166 writeln!(file, "{}", data)?;167 }168 } else if let Some(path) = opts.output.output_file {169 if opts.output.create_output_dirs {170 let mut dir = path.clone();171 dir.pop();172 create_dir_all(dir)?;173 }174 let mut file = File::create(path)?;175 writeln!(file, "{}", state.manifest(val)?)?;176 } else {177 let output = state.manifest(val)?;178 if !output.is_empty() {179 println!("{}", output);180 }181 }182183 Ok(())184}crates/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"
crates/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);
+ }
+}