From 244609ba1affc1b3eaa2863ab3dad5684e82d0ff Mon Sep 17 00:00:00 2001 From: sneeker Date: Thu, 14 Jul 2022 18:24:39 +0000 Subject: [PATCH] fix(cli): detect the core count on systems without /proc/cpuinfo The default job limit read /proc/cpuinfo and silently fell back to four anywhere that file is absent, so macOS and the BSDs ran at a fraction of the available parallelism. Falls back to getconf _NPROCESSORS_ONLN before the constant. --- bin/meowc.ml | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/bin/meowc.ml b/bin/meowc.ml index 762a63f..065178c 100644 --- a/bin/meowc.ml +++ b/bin/meowc.ml @@ -34,15 +34,29 @@ let usage = " --no-color plain output"; ] -let cores () = +let cores_from_cpuinfo () = match Fs.read "/proc/cpuinfo" with - | exception Sys_error _ -> 4 - | s -> - max 1 - (List.length - (List.filter - (fun l -> String.length l >= 9 && String.sub l 0 9 = "processor") - (String.split_on_char '\n' s))) + | exception Sys_error _ -> None + | s -> ( + match + List.length + (List.filter + (fun l -> String.length l >= 9 && String.sub l 0 9 = "processor") + (String.split_on_char '\n' s)) + with + | 0 -> None + | n -> Some n) + +let cores_from_getconf () = + match Exec.capture [| "getconf"; "_NPROCESSORS_ONLN" |] with + | 0, out -> int_of_string_opt (String.trim out) + | _ -> None + | exception _ -> None + +let cores () = + match cores_from_cpuinfo () with + | Some n -> n + | None -> ( match cores_from_getconf () with Some n when n > 0 -> n | _ -> 4) let duration s = if s >= 1.0 then Printf.sprintf "%.2f s" s else Printf.sprintf "%.0f ms" (s *. 1000.0)