From 75dab3730076b3c09317b5a5301643e7b9ba8c81 Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Wed, 24 Jul 2019 08:30:02 -0700 Subject: [PATCH] [config] better way to refer to the toplevel results dir Summary: The same logic for reading the env var and defaulting to the local results dir was duplicated in a bunch of places. Reviewed By: artempyanykh Differential Revision: D16458976 fbshipit-source-id: 41f1a4f9c --- infer/src/base/Config.ml | 13 +++++++++---- infer/src/base/Config.mli | 8 ++++++-- infer/src/base/EventLogger.ml | 5 +---- infer/src/base/Logging.ml | 9 ++------- infer/src/base/Memcached.ml | 8 +++----- infer/src/base/PerfEvent.ml | 9 ++------- 6 files changed, 23 insertions(+), 29 deletions(-) diff --git a/infer/src/base/Config.ml b/infer/src/base/Config.ml index 5b4be3c67..569661931 100644 --- a/infer/src/base/Config.ml +++ b/infer/src/base/Config.ml @@ -2485,9 +2485,7 @@ let inferconfig_file = let post_parsing_initialization command_opt = - if CommandLineOption.is_originator then ( - (* let subprocesses know where the toplevel process' results dir is *) - Unix.putenv ~key:infer_top_results_dir_env_var ~data:!results_dir ; + if CommandLineOption.is_originator then (* make sure subprocesses read from the same .inferconfig as the toplevel process *) Option.iter inferconfig_file ~f:(fun filename -> let abs_filename = @@ -2496,7 +2494,7 @@ let post_parsing_initialization command_opt = CLOpt.init_work_dir ^/ filename else filename in - Unix.putenv ~key:CommandDoc.inferconfig_env_var ~data:abs_filename ) ) ; + Unix.putenv ~key:CommandDoc.inferconfig_env_var ~data:abs_filename ) ; ( match !version with | `Full when !buck -> (* Buck reads stderr in some versions, stdout in others *) @@ -3293,3 +3291,10 @@ let is_in_custom_symbols list_name symbol = let execution_id = Random.self_init () ; Random.int64 Int64.max_value + +let toplevel_results_dir = + if CLOpt.is_originator then ( + (* let subprocesses know where the toplevel process' results dir is *) + Unix.putenv ~key:infer_top_results_dir_env_var ~data:results_dir ; + results_dir ) + else Sys.getenv infer_top_results_dir_env_var |> Option.value ~default:results_dir diff --git a/infer/src/base/Config.mli b/infer/src/base/Config.mli index 2f7bbff35..627de22c3 100644 --- a/infer/src/base/Config.mli +++ b/infer/src/base/Config.mli @@ -99,8 +99,6 @@ val idempotent_getters : bool val infer_py_argparse_error_exit_code : int -val infer_top_results_dir_env_var : string - val initial_analysis_time : float val ivar_attributes : string @@ -687,6 +685,12 @@ val threadsafe_aliases : Yojson.Basic.t val topl_properties : string list +val toplevel_results_dir : string +(** In some integrations, eg Buck, infer subprocesses started by the build system (started by the + toplevel infer process) will have their own results directory; this points to the results + directory of the toplevel infer process, which can be useful for, eg, storing debug info. In + other cases this is equal to {!results_dir}. *) + val trace_error : bool val trace_events : bool diff --git a/infer/src/base/EventLogger.ml b/infer/src/base/EventLogger.ml index 01df0c4dc..24443c8d3 100644 --- a/infer/src/base/EventLogger.ml +++ b/infer/src/base/EventLogger.ml @@ -11,10 +11,7 @@ module CLOpt = CommandLineOption module IO = struct let log_file_extension = ".log" - let events_dir = - Option.value (Sys.getenv Config.infer_top_results_dir_env_var) ~default:Config.results_dir - ^/ Config.events_dir_name - + let events_dir = Config.toplevel_results_dir ^/ Config.events_dir_name let out_chan = ref None diff --git a/infer/src/base/Logging.ml b/infer/src/base/Logging.ml index 4ad44ff48..c2c28ec6a 100644 --- a/infer/src/base/Logging.ml +++ b/infer/src/base/Logging.ml @@ -320,14 +320,9 @@ let setup_log_file () = () | None -> let fmt, chan, preexisting_logfile = - let results_dir = - (* if invoked in a sub-dir (e.g., in Buck integrations), log inside the original log - file *) - Sys.getenv Config.infer_top_results_dir_env_var - |> Option.value ~default:Config.results_dir - in + (* if invoked in a sub-dir (e.g., in Buck integrations), log inside the original log file *) (* assumes the results dir exists already *) - let logfile_path = results_dir ^/ Config.log_file in + let logfile_path = Config.toplevel_results_dir ^/ Config.log_file in let preexisting_logfile = PolyVariantEqual.( = ) (Sys.file_exists logfile_path) `Yes in let chan = Pervasives.open_out_gen [Open_append; Open_creat] 0o666 logfile_path in let file_fmt = diff --git a/infer/src/base/Memcached.ml b/infer/src/base/Memcached.ml index 4a650a916..c85b9ea4b 100644 --- a/infer/src/base/Memcached.ml +++ b/infer/src/base/Memcached.ml @@ -11,9 +11,7 @@ module L = Logging let memcached_socket_relative = "memcached.socket" (** find the results_dir of the top-level infer process *) -let results_dir = - Sys.getenv Config.infer_top_results_dir_env_var |> Option.value ~default:Config.results_dir - +let results_dir = Config.toplevel_results_dir (** log file for memcached *) let memcached_log = results_dir ^ "/memcached.log" @@ -26,8 +24,8 @@ let shell = "sh" type server = {input: In_channel.t; output: Out_channel.t} -(* Unix socket paths have a historical length limit of ~100 chars (!?*@&*$). However, this applies - to the argument passed in the system call to create the socket. Thus a workaround is to cd into +(* Unix socket paths have a historical length limit of ~100 chars (!?*@&*$). However, this applies + to the argument passed in the system call to create the socket. Thus a workaround is to cd into the parent dir of the socket and then create it, hence this function. *) let in_results_dir ~f = let cwd = Unix.getcwd () in diff --git a/infer/src/base/PerfEvent.ml b/infer/src/base/PerfEvent.ml index 7a34b6a9a..6bf0919e3 100644 --- a/infer/src/base/PerfEvent.ml +++ b/infer/src/base/PerfEvent.ml @@ -224,13 +224,8 @@ let register_gc_stats logger = let logger = lazy (let log_file = - let results_dir = - (* if invoked in a sub-dir (e.g., in Buck integrations), log inside the original log - file *) - Sys.getenv Config.infer_top_results_dir_env_var - |> Option.value ~default:Config.results_dir - in - results_dir ^/ Config.trace_events_file + (* if invoked in a sub-dir (e.g., in Buck integrations), log inside the original log file *) + Config.toplevel_results_dir ^/ Config.trace_events_file in let is_toplevel_process = CommandLineOption.is_originator && not !ProcessPoolState.in_child in ( if is_toplevel_process then