rxada_0.1.1_dd9da799/dirx/src/examples/dirx-hash_recursive.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
with Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;

with DirX.Examples;
with DirX.Observables;

with Rx.Schedulers;
with Rx.Subscriptions;

with System.Multiprocessors;

procedure DirX.Hash_Recursive is
   use Ada.Command_Line;

   use Observables;
   use Observables.RxEntries.Observables;
   use Examples.RxHashed.Observables;
   use Examples.Entry_To_Hash;

   Target : constant Path := (if Argument_Count = 0
                              then "."
                              else Argument (1));

   Context : String (1 .. 3) := "1-1";

   -------------
   -- Inspect --
   -------------

   procedure Inspect (Kind               : Rx.Rx_Event_Kinds;
                      Since_Previous     : Duration;
                      Since_Subscription : Duration)
   is
      pragma Unreferenced (Since_Previous);
      use all type Rx.Rx_Event_Kinds;
   begin
      if Kind = On_Complete then
         New_Line;
         Put_Line ("Wall time [" & Context & "]:" & Since_Subscription'Img);
      else
         null;
         --  Put_Line ("Incr time [" & Context & "]:" & Since_Previous'Img);
      end if;
   end Inspect;

   Sub : Rx.Subscriptions.Subscription;
begin
   Put_Line ("Number of CPUs:" & System.Multiprocessors.Number_Of_CPUs'Img);

   --  Sequential listing & hashing of files, with printing
   Sub :=
     Directory_Entries (Target, Recursive => True)
     & Examples.Hash'Access
     & Subscribe (On_Next => Examples.Print_Hash'Access);

   --  Sequential timing
   Sub :=
     Directory_Entries (Target, Recursive => True)
     & Examples.Hash'Access
     & Stopwatch (Inspect'Unrestricted_Access)
     & Subscribe;

   --  Parallel hashing timing
   Context := "1-N";
   Sub :=
     Directory_Entries (Target, Recursive => True)
     & Flat_Map (Observe_On (Rx.Schedulers.Computation)
                 & Examples.Hash'Access)
     & Stopwatch (Inspect'Unrestricted_Access)
     & Subscribe;

   while Sub.Is_Subscribed loop
      delay 0.1;
   end loop;

   --  Parallel enumeration and hashing
   Context := "M-N";
   Sub :=
     Directory_Entries (Target, Recursive => False)
     & Expand (Observe_On (Rx.Schedulers.IO)
               & Dirx.Observables.Observe'Access)
     & Flat_Map (Observe_On (Rx.Schedulers.Computation)
                 & Examples.Hash'Access)
     & Stopwatch (Inspect'Unrestricted_Access)
     & Subscribe; -- (On_Next => Examples.Print_Hash'Access);

   while Sub.Is_Subscribed loop
      delay 0.1;
   end loop;

end DirX.Hash_Recursive;