Skip to content

Commit

Permalink
Merge pull request #1636 from ocaml/macos-stats
Browse files Browse the repository at this point in the history
fix for issue #1634
  • Loading branch information
rgrinberg authored Dec 11, 2018
2 parents 59c4daa + c85ca9e commit 004e1e8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ unreleased
- skip directories when looking up programs in the PATH (#1628, fixes
#1616, @diml)

- Use `lsof` on macOS to implement `--stats` (#1636, fixes #1634, @xclerc)

- Generate `dune-package` files for every package. These files are installed and
read instead of `META` files whenever they are available (#1329, @rgrinberg)

Expand Down
23 changes: 22 additions & 1 deletion src/stats.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,30 @@ let enabled = ref false
module Fd_count = struct
type t = Unknown | This of int

let try_to_use_lsof () =
(* note: we do not use the Process module here, because it would
create a circular dependency *)
let temp = Filename.temp_file "dune" ".lsof" in
let stdout =
Unix.openfile temp [O_WRONLY; O_CREAT; O_TRUNC; O_SHARE_DELETE] 0o666
in
let prog = "/usr/sbin/lsof" in
let argv = [prog; "-w"; "-p"; string_of_int (Unix.getpid ())] in
let pid = Spawn.spawn ~prog ~argv ~stdout () in
Unix.close stdout;
match Unix.waitpid [] pid with
| _, Unix.WEXITED 0 ->
let num_lines = List.length (Io.input_lines (open_in temp)) in
This (num_lines - 1) (* the output contains a header line *)
| _ -> Unknown

let get () =
match Sys.readdir "/proc/self/fd" with
| exception _ -> Unknown
| exception _ ->
begin match try_to_use_lsof () with
| exception _ -> Unknown
| value -> value
end
| files -> This (Array.length files - 1 (* -1 for the dirfd *))

let map2 ~f a b =
Expand Down

0 comments on commit 004e1e8

Please sign in to comment.