difftreelog
fix(cli) gc stats were always printed
in: master
1 file changed
crates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth1mod manifest;2mod stdlib;3mod tla;4mod trace;56use std::{env, path::PathBuf};78use clap::Parser;9use jrsonnet_evaluator::{error::Result, FileImportResolver, State};10use jrsonnet_gcmodule::with_thread_object_space;11pub use manifest::*;12pub use stdlib::*;13pub use tla::*;14pub use trace::*;1516pub trait ConfigureState {17 fn configure(&self, s: &State) -> Result<()>;18}1920#[derive(Parser)]21#[clap(next_help_heading = "INPUT")]22pub struct InputOpts {23 /// Treat input as code, evaluate them instead of reading file24 #[clap(long, short = 'e')]25 pub exec: bool,2627 /// Path to the file to be compiled if `--evaluate` is unset, otherwise code itself28 pub input: String,29}3031#[derive(Parser)]32#[clap(next_help_heading = "OPTIONS")]33pub struct MiscOpts {34 /// Maximal allowed number of stack frames,35 /// stack overflow error will be raised if this number gets exceeded.36 #[clap(long, short = 's', default_value = "200")]37 max_stack: usize,3839 /// Library search dirs. (right-most wins)40 /// Any not found `imported` file will be searched in these.41 /// This can also be specified via `JSONNET_PATH` variable,42 /// which should contain a colon-separated (semicolon-separated on Windows) list of directories.43 #[clap(long, short = 'J', multiple_occurrences = true)]44 jpath: Vec<PathBuf>,45}46impl ConfigureState for MiscOpts {47 fn configure(&self, s: &State) -> Result<()> {48 let mut library_paths = self.jpath.clone();49 library_paths.reverse();50 if let Some(path) = env::var_os("JSONNET_PATH") {51 library_paths.extend(env::split_paths(path.as_os_str()));52 }5354 s.set_import_resolver(Box::new(FileImportResolver { library_paths }));5556 s.set_max_stack(self.max_stack);57 Ok(())58 }59}6061/// General configuration of jsonnet62#[derive(Parser)]63#[clap(name = "jrsonnet", version, author)]64pub struct GeneralOpts {65 #[clap(flatten)]66 misc: MiscOpts,6768 #[clap(flatten)]69 tla: TLAOpts,70 #[clap(flatten)]71 std: StdOpts,7273 #[clap(flatten)]74 trace: TraceOpts,75}7677impl ConfigureState for GeneralOpts {78 fn configure(&self, s: &State) -> Result<()> {79 // Configure trace first, because tla-code/ext-code can throw80 self.trace.configure(s)?;81 self.misc.configure(s)?;82 self.tla.configure(s)?;83 self.std.configure(s)?;84 Ok(())85 }86}8788#[derive(Parser)]89#[clap(next_help_heading = "GARBAGE COLLECTION")]90pub struct GcOpts {91 /// Do not skip gc on exit92 #[clap(long)]93 gc_collect_on_exit: bool,94 /// Print gc stats before exit95 #[clap(long)]96 gc_print_stats: bool,97 /// Force garbage collection before printing stats98 /// Useful for checking for memory leaks99 /// Does nothing useless --gc-print-stats is specified100 #[clap(long)]101 gc_collect_before_printing_stats: bool,102}103impl GcOpts {104 pub fn stats_printer(&self) -> (Option<GcStatsPrinter>, Option<LeakSpace>) {105 (106 self.gc_print_stats.then_some(GcStatsPrinter {107 collect_before_printing_stats: self.gc_collect_before_printing_stats,108 }),109 (!self.gc_collect_on_exit).then_some(LeakSpace {}),110 )111 }112}113114pub struct LeakSpace {}115116impl Drop for LeakSpace {117 fn drop(&mut self) {118 with_thread_object_space(|s| s.leak())119 }120}121122pub struct GcStatsPrinter {123 collect_before_printing_stats: bool,124}125impl Drop for GcStatsPrinter {126 fn drop(&mut self) {127 eprintln!("=== GC STATS ===");128 if self.collect_before_printing_stats {129 let collected = jrsonnet_gcmodule::collect_thread_cycles();130 eprintln!("Collected: {}", collected);131 }132 eprintln!("Tracked: {}", jrsonnet_gcmodule::count_thread_tracked())133 }134}