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.
This commit is contained in:
milner committed 2022-07-14 18:24:39 +00:00
1 parent bf81a54bc2
commit b11c6e9f72
1 file changed
+20 -6
+20 -6
View File
@@ -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
| 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)))
(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)