3 module Array = ArrayLabels
4 module List = ListLabels
5 module StrSet = Set.Make(String)
6 module Unix = UnixLabels
16 -> time_group_by_size:float
17 -> time_group_by_head:float
18 -> time_group_by_digest:float
22 : t -> size:int -> unit
24 : t -> size:int -> unit
30 : t -> size:int -> unit
32 : t -> size:int -> unit
33 val file_unique_sample
34 : t -> size:int -> unit
36 : t -> size:int -> unit
40 : t -> size:int -> unit
43 { considered_files : int ref
44 ; considered_bytes : int ref
46 ; ignored_files : int ref
47 ; ignored_bytes : int ref
48 ; unique_size_files : int ref
49 ; unique_size_bytes : int ref
50 ; unique_sample_files : int ref
51 ; unique_sample_bytes : int ref
52 ; sampled_files : int ref
53 ; sampled_bytes : int ref
54 ; hashed_files : int ref
55 ; hashed_bytes : int ref
57 ; redundant_data : int ref
61 { considered_files = ref 0
62 ; considered_bytes = ref 0
64 ; ignored_files = ref 0
65 ; ignored_bytes = ref 0
66 ; unique_size_files = ref 0
67 ; unique_size_bytes = ref 0
68 ; sampled_files = ref 0
69 ; sampled_bytes = ref 0
70 ; hashed_files = ref 0
71 ; hashed_bytes = ref 0
72 ; unique_sample_files = ref 0
73 ; unique_sample_bytes = ref 0
75 ; redundant_data = ref 0
81 let file_considered t ~size =
82 incr t.considered_files;
83 add t.considered_bytes size
85 let file_ignored {ignored_files; ignored_bytes; _} ~size =
87 add ignored_bytes size
92 let chunk_read t ~size =
93 add t.sampled_bytes size
98 let file_unique_size t ~size =
99 incr t.unique_size_files;
100 add t.unique_size_bytes size
102 let file_unique_sample t ~size =
103 incr t.unique_sample_files;
104 add t.unique_sample_bytes size
106 let file_hashed t ~size =
108 add t.hashed_bytes size
113 let redundant_data t ~size =
114 add t.redundant_data size
121 ~time_group_by_digest
123 let b_to_mb b = (float_of_int b) /. 1024. /. 1024. in
124 let b_to_gb b = (b_to_mb b) /. 1024. in
125 eprintf "Time : %8.2f seconds\n%!"
127 eprintf "Considered : %8d files %6.2f Gb\n%!"
128 !(t.considered_files)
129 (b_to_gb !(t.considered_bytes));
130 eprintf "Sampled : %8d files %6.2f Gb\n%!"
132 (b_to_gb !(t.sampled_bytes));
133 eprintf "Hashed : %8d files %6.2f Gb %6.2f seconds\n%!"
135 (b_to_gb !(t.hashed_bytes))
136 time_group_by_digest;
137 eprintf "Digests : %8d\n%!"
139 eprintf "Duplicates (Hashed - Digests): %8d files %6.2f Gb\n%!"
140 (!(t.hashed_files) - !(t.digests))
141 (b_to_gb !(t.redundant_data));
142 eprintf "Skipped due to 0 size : %8d files\n%!" !(t.empty);
143 eprintf "Skipped due to unique size : %8d files %6.2f Gb %6.2f seconds\n%!"
144 !(t.unique_size_files)
145 (b_to_gb !(t.unique_size_bytes))
147 eprintf "Skipped due to unique sample : %8d files %6.2f Gb %6.2f seconds\n%!"
148 !(t.unique_sample_files)
149 (b_to_gb !(t.unique_sample_bytes))
151 eprintf "Ignored due to regex match : %8d files %6.2f Gb\n%!"
153 (b_to_gb !(t.ignored_bytes))
161 val create : (unit -> 'a option) -> 'a t
163 val of_queue : 'a Queue.t -> 'a t
165 val iter : 'a t -> f:('a -> unit) -> unit
167 val bag_map : 'a t -> njobs:int -> f:('a -> 'b) -> ('a * 'b) t
168 (** Parallel map with arbitrarily-reordered elements. *)
170 val map : 'a t -> f:('a -> 'b) -> 'b t
172 val filter : 'a t -> f:('a -> bool) -> 'a t
174 val concat : ('a t) list -> 'a t
176 val group_by : 'a t -> f:('a -> 'b) -> ('b * int * 'a list) t
181 {mutable streams : ('a S.t) list}
183 type ('input, 'output) msg_from_vassal =
185 | Result of (int * ('input * 'output))
188 type 'input msg_from_lord =
189 | Job of 'input option
192 {streams = [S.from (fun _ -> f ())]}
196 match Queue.take q with
197 | exception Queue.Empty ->
209 | exception Stream.Failure ->
210 t.streams <- streams;
220 | Some x -> Some (f x)
236 List.iter t.streams ~f:(S.iter f)
239 {streams = List.concat (List.map ts ~f:(fun {streams} -> streams))}
242 let groups_tbl = Hashtbl.create 1_000_000 in
246 match Hashtbl.find_opt groups_tbl group with
252 Hashtbl.replace groups_tbl group members
254 iter t ~f:group_update;
255 let groups = Queue.create () in
257 (fun name (length, members) -> Queue.add (name, length, members) groups)
262 val send : out_channel -> 'a -> unit
263 val recv : in_channel -> 'a
266 Marshal.to_channel oc msg [];
270 Marshal.from_channel ic
273 let lord t ~njobs ~vassals ~ic ~ocs =
274 eprintf "[debug] [lord] started\n%!";
275 let active_vassals = ref njobs in
276 let results = Queue.create () in
277 let rec dispatch () =
278 match Ipc.recv ic with
279 | ((Exiting i) : ('input, 'output) msg_from_vassal) ->
282 if !active_vassals = 0 then
286 | ((Ready i) : ('input, 'output) msg_from_vassal) ->
287 Ipc.send ocs.(i) (Job (next t));
289 | ((Result (i, result)) : ('input, 'output) msg_from_vassal) ->
290 Queue.add result results;
291 Ipc.send ocs.(i) (Job (next t));
294 let rec wait = function
297 let pid, _process_status = Unix.wait () in
298 (* TODO: handle process_status *)
299 wait (List.filter vassals ~f:(fun p -> p <> pid))
306 let vassal i ~f ~vassal_pipe_r ~lord_pipe_w =
307 eprintf "[debug] [vassal %d] started\n%!" i;
308 let ic = Unix.in_channel_of_descr vassal_pipe_r in
309 let oc = Unix.out_channel_of_descr lord_pipe_w in
312 match Ipc.recv ic with
313 | (Job (Some x) : 'input msg_from_lord) ->
314 work (Result (i, (x, f x)))
315 | (Job None : 'input msg_from_lord) ->
316 Ipc.send oc (Exiting i)
323 let bag_map t ~njobs ~f =
324 let lord_pipe_r, lord_pipe_w = Unix.pipe () in
325 let vassal_pipes = Array.init njobs ~f:(fun _ -> Unix.pipe ()) in
326 let vassal_pipes_r = Array.map vassal_pipes ~f:(fun (r, _) -> r) in
327 let vassal_pipes_w = Array.map vassal_pipes ~f:(fun (_, w) -> w) in
328 let vassals = ref [] in
329 for i=0 to (njobs - 1) do
330 begin match Unix.fork () with
332 Unix.close lord_pipe_r;
333 vassal i ~f ~lord_pipe_w ~vassal_pipe_r:vassal_pipes_r.(i)
335 vassals := pid :: !vassals
338 Unix.close lord_pipe_w;
343 ~ic:(Unix.in_channel_of_descr lord_pipe_r)
344 ~ocs:(Array.map vassal_pipes_w ~f:Unix.out_channel_of_descr)
347 module In_channel : sig
348 val lines : in_channel -> string Stream.t
351 Stream.create (fun () ->
352 match input_line ic with
353 | exception End_of_file ->
366 val find : string -> t Stream.t
367 (** Find all files in the directory tree, starting from the given root path *)
369 val lookup : string Stream.t -> t Stream.t
370 (** Lookup file info for given paths *)
372 val filter_out_unique_sizes : t Stream.t -> metrics:M.t -> t Stream.t
373 val filter_out_unique_heads : t Stream.t -> len:int -> metrics:M.t -> t Stream.t
381 Stream.map paths ~f:(fun path ->
382 let {Unix.st_size = size; _} = Unix.lstat path in
387 let dirs = Queue.create () in
388 let files = Queue.create () in
390 Array.iter (Sys.readdir parent) ~f:(fun child ->
391 let path = Filename.concat parent child in
392 let {Unix.st_kind = file_kind; st_size; _} = Unix.lstat path in
395 let file = {path; size = st_size} in
409 match Queue.is_empty files, Queue.is_empty dirs with
410 | false, _ -> Some (Queue.take files)
411 | true , true -> None
413 explore (Queue.take dirs);
418 let filter_out_singletons files ~group ~handle_singleton =
419 let q = Queue.create () in
420 Stream.iter (Stream.group_by files ~f:group) ~f:(fun group ->
421 let (_, n, members) = group in
423 List.iter members ~f:(fun m -> Queue.add m q)
425 handle_singleton group
429 let filter_out_unique_sizes files ~metrics =
430 filter_out_singletons
432 ~group:(fun {size; _} -> size)
433 ~handle_singleton:(fun (size, _, _) -> M.file_unique_size metrics ~size)
435 let head path ~len ~metrics =
436 let buf = Bytes.make len ' ' in
437 let ic = open_in_bin path in
438 let rec read pos len =
443 let chunk_size = input ic buf pos len in
444 M.chunk_read metrics ~size:chunk_size;
445 if chunk_size = 0 then (* EOF *)
448 read (pos + chunk_size) (len - chunk_size)
455 let filter_out_unique_heads files ~len ~metrics =
456 filter_out_singletons
458 ~group:(fun {path; _} ->
459 M.file_sampled metrics;
460 head path ~len ~metrics
462 ~handle_singleton:(fun (_, _, files) ->
463 let {size; _} = List.hd files in (* Guaranteed non-empty *)
464 M.file_unique_sample metrics ~size
470 | Directories of string list
474 | Directory of string
479 ; ignore : string -> bool
484 let make_input_stream input ignore ~metrics =
488 File.lookup (In_channel.lines stdin)
489 | Directories paths ->
490 let paths = StrSet.elements (StrSet.of_list paths) in
491 Stream.concat (List.map paths ~f:File.find)
493 Stream.filter input ~f:(fun {File.path; size} ->
494 M.file_considered metrics ~size;
495 let empty = size = 0 in
496 let ignored = ignore path in
497 if empty then M.file_empty metrics;
498 if ignored then M.file_ignored metrics ~size;
499 (not empty) && (not ignored)
502 let make_output_fun = function
504 fun digest n_files files ->
505 printf "%s %d\n%!" (Digest.to_hex digest) n_files;
506 List.iter files ~f:(fun {File.path; _} ->
507 printf " %S\n%!" path
510 fun digest _ files ->
511 let digest = Digest.to_hex digest in
512 let dir = Filename.concat dir (String.sub digest 0 2) in
513 Unix.mkdir dir ~perm:0o700;
514 let oc = open_out (Filename.concat dir digest) in
515 List.iter files ~f:(fun {File.path; _} ->
516 output_string oc (sprintf "%S\n%!" path)
523 let main {input; output; ignore; sample = sample_len; njobs} =
524 let t0_all = time () in
525 let metrics = M.init () in
526 let output = make_output_fun output in
527 let input = make_input_stream input ignore ~metrics in
528 (* TODO: Make a nice(r) abstraction to re-assemble pieces in the pipeline:
530 * from input to files_by_size
531 * from files_by_size to files_by_sample
532 * from files_by_sample to files_by_digest
533 * from files_by_digest to output
535 * input |> files_by_size |> files_by_sample |> files_by_digest |> output
540 let t0_group_by_size = time () in
541 eprintf "[debug] filtering-out files with unique size\n%!";
542 let files = File.filter_out_unique_sizes files ~metrics in
543 let t1_group_by_size = time () in
545 let t0_group_by_sample = t1_group_by_size in
546 eprintf "[debug] filtering-out files with unique heads\n%!";
547 let files = File.filter_out_unique_heads files ~len:sample_len ~metrics in
548 let t1_group_by_sample = time () in
550 let t0_group_by_digest = t1_group_by_sample in
551 eprintf "[debug] hashing\n%!";
555 Stream.bag_map files ~njobs ~f:(fun {File.path; _} -> Digest.file path)
557 Stream.map (Stream.group_by digests ~f:(fun (_, d) -> d)) ~f:(
558 fun (digest, n, file_digest_pairs) ->
560 List.map file_digest_pairs ~f:(fun (file, _) ->
561 M.file_hashed metrics ~size:file.File.size;
568 Stream.group_by files ~f:(fun {File.path; size} ->
569 M.file_hashed metrics ~size;
573 let t1_group_by_digest = time () in
575 eprintf "[debug] reporting\n%!";
576 Stream.iter groups ~f:(fun (d, n, files) ->
579 M.redundant_data metrics ~size:(n * (List.hd files).File.size);
583 let t1_all = time () in
586 ~time_all: (t1_all -. t0_all)
587 ~time_group_by_size: (t1_group_by_size -. t0_group_by_size)
588 ~time_group_by_head: (t1_group_by_sample -. t0_group_by_sample)
589 ~time_group_by_digest:(t1_group_by_digest -. t0_group_by_digest)
591 let get_opt () : opt =
592 let assert_ test x msg =
593 if not (test x) then begin
594 eprintf "%s\n%!" msg;
598 let assert_file_exists path =
599 assert_ Sys.file_exists path (sprintf "File does not exist: %S" path)
601 let assert_file_is_dir path =
602 assert_ Sys.is_directory path (sprintf "File is not a directory: %S" path)
604 let input = ref Stdin in
605 let output = ref Stdout in
606 let ignore = ref (fun _ -> false) in
607 let sample = ref 512 in
611 , Arg.String (fun path ->
612 assert_file_exists path;
613 assert_file_is_dir path;
614 output := Directory path
616 , " Output to this directory instead of stdout."
619 , Arg.String (fun regexp ->
620 let regexp = Str.regexp regexp in
621 ignore := fun string -> Str.string_match regexp string 0)
622 , " Ignore file paths which match this regexp pattern (see Str module)."
626 , (sprintf " Byte size of file samples to use. Default: %d" !sample)
630 , (sprintf " Number of parallel jobs. Default: %d" !njobs)
637 assert_file_exists path;
638 assert_file_is_dir path;
641 input := Directories [path]
642 | Directories paths ->
643 input := Directories (path :: paths)
649 (sprintf "Sample size cannot be negative: %d" !sample);