3 module Array = ArrayLabels
4 module List = ListLabels
5 module StrSet = Set.Make(String)
6 module Unix = UnixLabels
15 -> wall_time_all:float
16 -> wall_time_group_by_size:float
17 -> wall_time_group_by_head:float
18 -> wall_time_group_by_digest:float
19 -> proc_time_all:float
20 -> proc_time_group_by_size:float
21 -> proc_time_group_by_head:float
22 -> proc_time_group_by_digest:float
26 : t -> size:int -> unit
28 : t -> size:int -> unit
34 : t -> size:int -> unit
36 : t -> size:int -> unit
37 val file_unique_sample
38 : t -> size:int -> unit
40 : t -> size:int -> unit
44 : t -> size:int -> unit
47 { considered_files : int ref
48 ; considered_bytes : int ref
50 ; ignored_files : int ref
51 ; ignored_bytes : int ref
52 ; unique_size_files : int ref
53 ; unique_size_bytes : int ref
54 ; unique_sample_files : int ref
55 ; unique_sample_bytes : int ref
56 ; sampled_files : int ref
57 ; sampled_bytes : int ref
58 ; hashed_files : int ref
59 ; hashed_bytes : int ref
61 ; redundant_data : int ref
65 { considered_files = ref 0
66 ; considered_bytes = ref 0
68 ; ignored_files = ref 0
69 ; ignored_bytes = ref 0
70 ; unique_size_files = ref 0
71 ; unique_size_bytes = ref 0
72 ; sampled_files = ref 0
73 ; sampled_bytes = ref 0
74 ; hashed_files = ref 0
75 ; hashed_bytes = ref 0
76 ; unique_sample_files = ref 0
77 ; unique_sample_bytes = ref 0
79 ; redundant_data = ref 0
85 let file_considered t ~size =
86 incr t.considered_files;
87 add t.considered_bytes size
89 let file_ignored {ignored_files; ignored_bytes; _} ~size =
91 add ignored_bytes size
96 let chunk_read t ~size =
97 add t.sampled_bytes size
102 let file_unique_size t ~size =
103 incr t.unique_size_files;
104 add t.unique_size_bytes size
106 let file_unique_sample t ~size =
107 incr t.unique_sample_files;
108 add t.unique_sample_bytes size
110 let file_hashed t ~size =
112 add t.hashed_bytes size
117 let redundant_data t ~size =
118 add t.redundant_data size
123 ~wall_time_group_by_size
124 ~wall_time_group_by_head
125 ~wall_time_group_by_digest
127 ~proc_time_group_by_size
128 ~proc_time_group_by_head
129 ~proc_time_group_by_digest
131 let b_to_mb b = (float_of_int b) /. 1024. /. 1024. in
132 let b_to_gb b = (b_to_mb b) /. 1024. in
133 eprintf "Total time : %.2f wall sec %.2f proc sec\n%!"
136 eprintf "Considered : %8d files %6.2f Gb\n%!"
137 !(t.considered_files)
138 (b_to_gb !(t.considered_bytes));
139 eprintf "Sampled : %8d files %6.2f Gb\n%!"
141 (b_to_gb !(t.sampled_bytes));
142 eprintf "Hashed : %8d files %6.2f Gb %6.2f wall sec %6.2f proc sec\n%!"
144 (b_to_gb !(t.hashed_bytes))
145 wall_time_group_by_digest
146 proc_time_group_by_digest;
147 eprintf "Digests : %8d\n%!"
149 eprintf "Duplicates (Hashed - Digests): %8d files %6.2f Gb\n%!"
150 (!(t.hashed_files) - !(t.digests))
151 (b_to_gb !(t.redundant_data));
152 eprintf "Skipped due to 0 size : %8d files\n%!" !(t.empty);
153 eprintf "Skipped due to unique size : %8d files %6.2f Gb %6.2f wall sec %6.2f proc sec\n%!"
154 !(t.unique_size_files)
155 (b_to_gb !(t.unique_size_bytes))
156 wall_time_group_by_size
157 proc_time_group_by_size;
158 eprintf "Skipped due to unique sample : %8d files %6.2f Gb %6.2f wall sec %6.2f proc sec\n%!"
159 !(t.unique_sample_files)
160 (b_to_gb !(t.unique_sample_bytes))
161 wall_time_group_by_head
162 proc_time_group_by_head;
163 eprintf "Ignored due to regex match : %8d files %6.2f Gb\n%!"
165 (b_to_gb !(t.ignored_bytes))
173 val create : (unit -> 'a option) -> 'a t
175 val of_queue : 'a Queue.t -> 'a t
177 val iter : 'a t -> f:('a -> unit) -> unit
179 val bag_map : 'a t -> njobs:int -> f:('a -> 'b) -> ('a * 'b) t
180 (** Parallel map with arbitrarily-reordered elements. *)
182 val map : 'a t -> f:('a -> 'b) -> 'b t
184 val filter : 'a t -> f:('a -> bool) -> 'a t
186 val concat : ('a t) list -> 'a t
188 val group_by : 'a t -> f:('a -> 'b) -> ('b * int * 'a list) t
193 {mutable streams : ('a S.t) list}
195 type ('input, 'output) msg_from_vassal =
197 | Result of (int * ('input * 'output))
200 type 'input msg_from_lord =
201 | Job of 'input option
204 {streams = [S.from (fun _ -> f ())]}
208 match Queue.take q with
209 | exception Queue.Empty ->
221 | exception Stream.Failure ->
222 t.streams <- streams;
232 | Some x -> Some (f x)
248 List.iter t.streams ~f:(S.iter f)
251 {streams = List.concat (List.map ts ~f:(fun {streams} -> streams))}
254 let groups_tbl = Hashtbl.create 1_000_000 in
258 match Hashtbl.find_opt groups_tbl group with
264 Hashtbl.replace groups_tbl group members
266 iter t ~f:group_update;
267 let groups = Queue.create () in
269 (fun name (length, members) -> Queue.add (name, length, members) groups)
274 val send : out_channel -> 'a -> unit
275 val recv : in_channel -> 'a
278 Marshal.to_channel oc msg [];
282 Marshal.from_channel ic
285 let lord t ~njobs ~vassals ~ic ~ocs =
286 let active_vassals = ref njobs in
287 let results = Queue.create () in
288 let rec dispatch () =
289 match Ipc.recv ic with
290 | ((Exiting i) : ('input, 'output) msg_from_vassal) ->
293 if !active_vassals = 0 then
297 | ((Ready i) : ('input, 'output) msg_from_vassal) ->
298 Ipc.send ocs.(i) (Job (next t));
300 | ((Result (i, result)) : ('input, 'output) msg_from_vassal) ->
301 Queue.add result results;
302 Ipc.send ocs.(i) (Job (next t));
305 let rec wait = function
308 let pid, _process_status = Unix.wait () in
309 (* TODO: handle process_status *)
310 wait (List.filter vassals ~f:(fun p -> p <> pid))
317 let vassal i ~f ~vassal_pipe_r ~lord_pipe_w =
318 let ic = Unix.in_channel_of_descr vassal_pipe_r in
319 let oc = Unix.out_channel_of_descr lord_pipe_w in
322 match Ipc.recv ic with
323 | (Job (Some x) : 'input msg_from_lord) ->
324 work (Result (i, (x, f x)))
325 | (Job None : 'input msg_from_lord) ->
326 Ipc.send oc (Exiting i)
333 let bag_map t ~njobs ~f =
334 let lord_pipe_r, lord_pipe_w = Unix.pipe () in
335 let vassal_pipes = Array.init njobs ~f:(fun _ -> Unix.pipe ()) in
336 let vassal_pipes_r = Array.map vassal_pipes ~f:(fun (r, _) -> r) in
337 let vassal_pipes_w = Array.map vassal_pipes ~f:(fun (_, w) -> w) in
338 let vassals = ref [] in
339 for i=0 to (njobs - 1) do
340 begin match Unix.fork () with
342 Unix.close lord_pipe_r;
343 vassal i ~f ~lord_pipe_w ~vassal_pipe_r:vassal_pipes_r.(i)
345 vassals := pid :: !vassals
348 Unix.close lord_pipe_w;
353 ~ic:(Unix.in_channel_of_descr lord_pipe_r)
354 ~ocs:(Array.map vassal_pipes_w ~f:Unix.out_channel_of_descr)
357 module In_channel : sig
358 val lines : in_channel -> string Stream.t
361 Stream.create (fun () ->
362 match input_line ic with
363 | exception End_of_file ->
376 val find : string -> t Stream.t
377 (** Find all files in the directory tree, starting from the given root path *)
379 val lookup : string Stream.t -> t Stream.t
380 (** Lookup file info for given paths *)
382 val head : t -> len:int -> metrics:M.t -> string
384 val filter_out_unique_sizes : t Stream.t -> metrics:M.t -> t Stream.t
385 val filter_out_unique_heads : t Stream.t -> len:int -> metrics:M.t -> t Stream.t
393 Stream.map paths ~f:(fun path ->
394 let {Unix.st_size = size; _} = Unix.lstat path in
399 let dirs = Queue.create () in
400 let files = Queue.create () in
402 Array.iter (Sys.readdir parent) ~f:(fun child ->
403 let path = Filename.concat parent child in
404 let {Unix.st_kind = file_kind; st_size; _} = Unix.lstat path in
407 let file = {path; size = st_size} in
421 match Queue.is_empty files, Queue.is_empty dirs with
422 | false, _ -> Some (Queue.take files)
423 | true , true -> None
425 explore (Queue.take dirs);
430 let filter_out_singletons files ~group ~handle_singleton =
431 let q = Queue.create () in
432 Stream.iter (Stream.group_by files ~f:group) ~f:(fun group ->
433 let (_, n, members) = group in
435 List.iter members ~f:(fun m -> Queue.add m q)
437 handle_singleton group
441 let filter_out_unique_sizes files ~metrics =
442 filter_out_singletons
444 ~group:(fun {size; _} -> size)
445 ~handle_singleton:(fun (size, _, _) -> M.file_unique_size metrics ~size)
447 let head {path; _} ~len ~metrics =
448 M.file_sampled metrics;
449 let buf = Bytes.make len ' ' in
450 let ic = open_in_bin path in
451 let rec read pos len =
456 let chunk_size = input ic buf pos len in
457 M.chunk_read metrics ~size:chunk_size;
458 if chunk_size = 0 then (* EOF *)
461 read (pos + chunk_size) (len - chunk_size)
468 let filter_out_unique_heads files ~len ~metrics =
469 filter_out_singletons
471 ~group:(head ~len ~metrics)
472 ~handle_singleton:(fun (_, _, files) ->
473 let {size; _} = List.hd files in (* Guaranteed non-empty *)
474 M.file_unique_sample metrics ~size
480 | Directories of string list
484 | Directory of string
489 ; ignore : string -> bool
494 let make_input_stream input ignore ~metrics =
498 File.lookup (In_channel.lines stdin)
499 | Directories paths ->
500 let paths = StrSet.elements (StrSet.of_list paths) in
501 Stream.concat (List.map paths ~f:File.find)
503 Stream.filter input ~f:(fun {File.path; size} ->
504 M.file_considered metrics ~size;
505 let empty = size = 0 in
506 let ignored = ignore path in
507 if empty then M.file_empty metrics;
508 if ignored then M.file_ignored metrics ~size;
509 (not empty) && (not ignored)
512 let make_output_fun = function
514 fun digest n_files files ->
515 printf "%s %d\n%!" (Digest.to_hex digest) n_files;
516 List.iter files ~f:(fun {File.path; _} ->
517 printf " %S\n%!" path
520 fun digest _ files ->
521 let digest = Digest.to_hex digest in
522 let dir = Filename.concat dir (String.sub digest 0 2) in
523 Unix.mkdir dir ~perm:0o700;
524 let oc = open_out (Filename.concat dir digest) in
525 List.iter files ~f:(fun {File.path; _} ->
526 output_string oc (sprintf "%S\n%!" path)
536 let main {input; output; ignore; sample = sample_len; njobs} =
537 let wt0_all = time_wall () in
538 let pt0_all = time_proc () in
539 let metrics = M.init () in
540 let output = make_output_fun output in
541 let input = make_input_stream input ignore ~metrics in
542 (* TODO: Make a nice(r) abstraction to re-assemble pieces in the pipeline:
544 * from input to files_by_size
545 * from files_by_size to files_by_sample
546 * from files_by_sample to files_by_digest
547 * from files_by_digest to output
549 * input |> files_by_size |> files_by_sample |> files_by_digest |> output
554 let wt0_group_by_size = time_wall () in
555 let pt0_group_by_size = time_proc () in
556 eprintf "[debug] filtering-out files with unique size\n%!";
557 let files = File.filter_out_unique_sizes files ~metrics in
558 let pt1_group_by_size = time_proc () in
559 let wt1_group_by_size = time_wall () in
561 let wt0_group_by_sample = wt1_group_by_size in
562 let pt0_group_by_sample = pt1_group_by_size in
563 eprintf "[debug] filtering-out files with unique heads\n%!";
565 if njobs > 1 then begin
566 let q = Queue.create () in
568 |> Stream.bag_map ~njobs ~f:(File.head ~len:sample_len ~metrics)
569 |> Stream.group_by ~f:snd
570 |> Stream.map ~f:(fun (d, n, pairs) -> (d, n, List.map pairs ~f:fst))
571 |> Stream.filter ~f:(fun (_, n, _) -> n > 1)
572 |> Stream.iter ~f:(fun (_, _, fs) -> List.iter fs ~f:(fun f -> Queue.add f q))
576 File.filter_out_unique_heads files ~len:sample_len ~metrics
578 let pt1_group_by_sample = time_proc () in
579 let wt1_group_by_sample = time_wall () in
581 let wt0_group_by_digest = wt1_group_by_sample in
582 let pt0_group_by_digest = pt1_group_by_sample in
583 eprintf "[debug] hashing\n%!";
587 Stream.bag_map files ~njobs ~f:(fun {File.path; _} -> Digest.file path)
589 Stream.map (Stream.group_by with_digests ~f:snd) ~f:(
590 fun (digest, n, file_digest_pairs) ->
592 List.map file_digest_pairs ~f:(fun (file, _) ->
593 M.file_hashed metrics ~size:file.File.size;
600 Stream.group_by files ~f:(fun {File.path; size} ->
601 M.file_hashed metrics ~size;
605 let pt1_group_by_digest = time_proc () in
606 let wt1_group_by_digest = time_wall () in
608 eprintf "[debug] reporting\n%!";
609 Stream.iter groups ~f:(fun (d, n, files) ->
612 M.redundant_data metrics ~size:(n * (List.hd files).File.size);
616 let pt1_all = time_proc () in
617 let wt1_all = time_wall () in
620 ~wall_time_all: (wt1_all -. wt0_all)
621 ~wall_time_group_by_size: (wt1_group_by_size -. wt0_group_by_size)
622 ~wall_time_group_by_head: (wt1_group_by_sample -. wt0_group_by_sample)
623 ~wall_time_group_by_digest:(wt1_group_by_digest -. wt0_group_by_digest)
624 ~proc_time_all: (pt1_all -. pt0_all)
625 ~proc_time_group_by_size: (pt1_group_by_size -. pt0_group_by_size)
626 ~proc_time_group_by_head: (pt1_group_by_sample -. pt0_group_by_sample)
627 ~proc_time_group_by_digest:(pt1_group_by_digest -. pt0_group_by_digest)
629 let get_opt () : opt =
630 let assert_ test x msg =
631 if not (test x) then begin
632 eprintf "%s\n%!" msg;
636 let assert_file_exists path =
637 assert_ Sys.file_exists path (sprintf "File does not exist: %S" path)
639 let assert_file_is_dir path =
640 assert_ Sys.is_directory path (sprintf "File is not a directory: %S" path)
642 let input = ref Stdin in
643 let output = ref Stdout in
644 let ignore = ref (fun _ -> false) in
645 let sample = ref 512 in
649 , Arg.String (fun path ->
650 assert_file_exists path;
651 assert_file_is_dir path;
652 output := Directory path
654 , " Output to this directory instead of stdout."
657 , Arg.String (fun regexp ->
658 let regexp = Str.regexp regexp in
659 ignore := fun string -> Str.string_match regexp string 0)
660 , " Ignore file paths which match this regexp pattern (see Str module)."
664 , (sprintf " Byte size of file samples to use. Default: %d" !sample)
668 , (sprintf " Number of parallel jobs. Default: %d" !njobs)
675 assert_file_exists path;
676 assert_file_is_dir path;
679 input := Directories [path]
680 | Directories paths ->
681 input := Directories (path :: paths)
687 (sprintf "Sample size cannot be negative: %d" !sample);