-module Stream : sig
- type 'a t
-
- val create : (unit -> 'a option) -> 'a t
-
- val of_queue : 'a Queue.t -> 'a t
-
- val iter : 'a t -> f:('a -> unit) -> unit
-
- val bag_map : 'a t -> njobs:int -> f:('a -> 'b) -> ('a * 'b) t
- (** Parallel map with arbitrarily-reordered elements. *)
-
- val map : 'a t -> f:('a -> 'b) -> 'b t
-
- val filter : 'a t -> f:('a -> bool) -> 'a t
-
- val concat : ('a t) list -> 'a t
-
- val group_by : 'a t -> f:('a -> 'b) -> ('b * int * 'a list) t
-end = struct
- module S = Stream
-
- type 'a t =
- {mutable streams : ('a S.t) list}
-
- type ('input, 'output) msg_from_vassal =
- | Ready of int
- | Result of (int * ('input * 'output))
- | Exiting of int
-
- type 'input msg_from_lord =
- | Job of 'input option
-
- let create f =
- {streams = [S.from (fun _ -> f ())]}
-
- let of_queue q =
- create (fun () ->
- match Queue.take q with
- | exception Queue.Empty ->
- None
- | x ->
- Some x
- )
-
- let rec next t =
- match t.streams with
- | [] ->
- None
- | s :: streams ->
- (match S.next s with
- | exception Stream.Failure ->
- t.streams <- streams;
- next t
- | x ->
- Some x
- )
-
- let map t ~f =
- create (fun () ->
- match next t with
- | None -> None
- | Some x -> Some (f x)
- )
-
- let filter t ~f =
- let rec filter () =
- match next t with
- | None ->
- None
- | Some x when f x ->
- Some x
- | Some _ ->
- filter ()
- in
- create filter
-
- let iter t ~f =
- List.iter t.streams ~f:(S.iter f)
-
- let concat ts =
- {streams = List.concat (List.map ts ~f:(fun {streams} -> streams))}
-
- let group_by t ~f =
- let groups_tbl = Hashtbl.create 1_000_000 in
- let group_update x =
- let group = f x in
- let members =
- match Hashtbl.find_opt groups_tbl group with
- | None ->
- (1, [x])
- | Some (n, xs) ->
- (succ n, x :: xs)
- in
- Hashtbl.replace groups_tbl group members
- in
- iter t ~f:group_update;
- let groups = Queue.create () in
- Hashtbl.iter
- (fun name (length, members) -> Queue.add (name, length, members) groups)
- groups_tbl;
- of_queue groups
-
- module Ipc : sig
- val send : out_channel -> 'a -> unit
- val recv : in_channel -> 'a
- end = struct
- let send oc msg =
- Marshal.to_channel oc msg [];
- flush oc
-
- let recv ic =
- Marshal.from_channel ic
- end
-
- let lord t ~njobs ~vassals ~ic ~ocs =
- eprintf "[debug] [lord] started\n%!";
- let active_vassals = ref njobs in
- let results = Queue.create () in
- let rec dispatch () =
- match Ipc.recv ic with
- | ((Exiting i) : ('input, 'output) msg_from_vassal) ->
- close_out ocs.(i);
- decr active_vassals;
- if !active_vassals = 0 then
- ()
- else
- dispatch ()
- | ((Ready i) : ('input, 'output) msg_from_vassal) ->
- Ipc.send ocs.(i) (Job (next t));
- dispatch ()
- | ((Result (i, result)) : ('input, 'output) msg_from_vassal) ->
- Queue.add result results;
- Ipc.send ocs.(i) (Job (next t));
- dispatch ()
- in
- let rec wait = function
- | [] -> ()
- | vassals ->
- let pid, _process_status = Unix.wait () in
- (* TODO: handle process_status *)
- wait (List.filter vassals ~f:(fun p -> p <> pid))
- in
- dispatch ();
- close_in ic;
- wait vassals;
- of_queue results
-
- let vassal i ~f ~vassal_pipe_r ~lord_pipe_w =
- eprintf "[debug] [vassal %d] started\n%!" i;
- let ic = Unix.in_channel_of_descr vassal_pipe_r in
- let oc = Unix.out_channel_of_descr lord_pipe_w in
- let rec work msg =
- Ipc.send oc msg;
- match Ipc.recv ic with
- | (Job (Some x) : 'input msg_from_lord) ->
- work (Result (i, (x, f x)))
- | (Job None : 'input msg_from_lord) ->
- Ipc.send oc (Exiting i)
- in
- work (Ready i);
- close_in ic;
- close_out oc;
- exit 0
-
- let bag_map t ~njobs ~f =
- let lord_pipe_r, lord_pipe_w = Unix.pipe () in
- let vassal_pipes = Array.init njobs ~f:(fun _ -> Unix.pipe ()) in
- let vassal_pipes_r = Array.map vassal_pipes ~f:(fun (r, _) -> r) in
- let vassal_pipes_w = Array.map vassal_pipes ~f:(fun (_, w) -> w) in
- let vassals = ref [] in
- for i=0 to (njobs - 1) do
- begin match Unix.fork () with
- | 0 ->
- Unix.close lord_pipe_r;
- vassal i ~f ~lord_pipe_w ~vassal_pipe_r:vassal_pipes_r.(i)
- | pid ->
- vassals := pid :: !vassals
- end
- done;
- Unix.close lord_pipe_w;
- lord
- t
- ~njobs
- ~vassals:!vassals
- ~ic:(Unix.in_channel_of_descr lord_pipe_r)
- ~ocs:(Array.map vassal_pipes_w ~f:Unix.out_channel_of_descr)
-end
-
-module In_channel : sig
- val lines : in_channel -> string Stream.t
-end = struct
- let lines ic =
- Stream.create (fun () ->
- match input_line ic with
- | exception End_of_file ->
- None
- | line ->
- Some line
- )
-end
-
-module File : sig
- type t =
- { path : string
- ; size : int
- }
-
- val find : string -> t Stream.t
- (** Find all files in the directory tree, starting from the given root path *)
-
- val lookup : string Stream.t -> t Stream.t
- (** Lookup file info for given paths *)
-
- val filter_out_unique_sizes : t Stream.t -> metrics:M.t -> t Stream.t
- val filter_out_unique_heads : t Stream.t -> len:int -> metrics:M.t -> t Stream.t
-end = struct
- type t =
- { path : string
- ; size : int
- }
-
- let lookup paths =
- Stream.map paths ~f:(fun path ->
- let {Unix.st_size = size; _} = Unix.lstat path in
- {path; size}
- )
-
- let find root =
- let dirs = Queue.create () in
- let files = Queue.create () in
- let explore parent =
- Array.iter (Sys.readdir parent) ~f:(fun child ->
- let path = Filename.concat parent child in
- let {Unix.st_kind = file_kind; st_size; _} = Unix.lstat path in
- match file_kind with
- | Unix.S_REG ->
- let file = {path; size = st_size} in
- Queue.add file files
- | Unix.S_DIR ->
- Queue.add path dirs
- | Unix.S_CHR
- | Unix.S_BLK
- | Unix.S_LNK
- | Unix.S_FIFO
- | Unix.S_SOCK ->
- ()
- )
- in
- explore root;
- let rec next () =
- match Queue.is_empty files, Queue.is_empty dirs with
- | false, _ -> Some (Queue.take files)
- | true , true -> None
- | true , false ->
- explore (Queue.take dirs);
- next ()
- in
- Stream.create next
-
- let filter_out_singletons files ~group ~handle_singleton =
- let q = Queue.create () in
- Stream.iter (Stream.group_by files ~f:group) ~f:(fun group ->
- let (_, n, members) = group in
- if n > 1 then
- List.iter members ~f:(fun m -> Queue.add m q)
- else
- handle_singleton group
- );
- Stream.of_queue q
-
- let filter_out_unique_sizes files ~metrics =
- filter_out_singletons
- files
- ~group:(fun {size; _} -> size)
- ~handle_singleton:(fun (size, _, _) -> M.file_unique_size metrics ~size)
-
- let head path ~len ~metrics =
- let buf = Bytes.make len ' ' in
- let ic = open_in_bin path in
- let rec read pos len =
- assert (len >= 0);
- if len = 0 then
- ()
- else begin
- let chunk_size = input ic buf pos len in
- M.chunk_read metrics ~size:chunk_size;
- if chunk_size = 0 then (* EOF *)
- ()
- else
- read (pos + chunk_size) (len - chunk_size)
- end
- in
- read 0 len;
- close_in ic;
- Bytes.to_string buf
-
- let filter_out_unique_heads files ~len ~metrics =
- filter_out_singletons
- files
- ~group:(fun {path; _} ->
- M.file_sampled metrics;
- head path ~len ~metrics
- )
- ~handle_singleton:(fun (_, _, files) ->
- let {size; _} = List.hd files in (* Guaranteed non-empty *)
- M.file_unique_sample metrics ~size
- )
-end
-