wisitoken_4.2.1_dc778486/recover_stats.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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
--  Abstract :
--
--  Summarize error recover log.
--
--  Copyright (C) 2019 - 2021 Stephen Leake All Rights Reserved.
--
--  This program is free software; you can redistribute it and/or
--  modify it under terms of the GNU General Public License as
--  published by the Free Software Foundation; either version 3, or (at
--  your option) any later version. This program is distributed in the
--  hope that it will be useful, but WITHOUT ANY WARRANTY; without even
--  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
--  PURPOSE. See the GNU General Public License for more details. You
--  should have received a copy of the GNU General Public License
--  distributed with this program; see file COPYING. If not, write to
--  the Free Software Foundation, 51 Franklin Street, Suite 500, Boston,
--  MA 02110-1335, USA.

pragma License (GPL);

with Ada.Command_Line;
with Ada.Exceptions;
with Ada.Long_Float_Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Strings.Maps;
with Ada.Text_IO; use Ada.Text_IO;
with GNAT.Traceback.Symbolic;
with SAL.Gen_Stats.Gen_Image;
with SAL.Long_Float_Stats;
with WisiToken.Parse.LR;
procedure Recover_Stats
is
   subtype Strategies is WisiToken.Parse.LR.Strategies;

   File : File_Type;

   Delimiters : constant Ada.Strings.Maps.Character_Set := Ada.Strings.Maps.To_Set (",() ");
   Number     : constant Ada.Strings.Maps.Character_Set := Ada.Strings.Maps.To_Set ("0123456789");

   type Strategy_Counts is array (Strategies) of Natural;

   type Recover_Label is (Full, Partial);

   type Recover_Summary is record
      Event_Count : Integer := 0;
      --  1 per recover event (1 line in log file)

      Enqueue_Stats : SAL.Long_Float_Stats.Stats_Type;
      Check_Stats   : SAL.Long_Float_Stats.Stats_Type;

      Strat_Counts_Total   : Strategy_Counts := (others => 0);
      Strat_Counts_Present : Strategy_Counts := (others => 0);
      --  1 per recover event if used

      Recover_Count_Present : Integer := 0;
      --  1 per parser in recover result

      Recover_Count_Total : Integer := 0;
      --  Sum of all strategy counts

      Fail_Event_Count      : Integer := 0; -- for all reasons
      Fail_Enqueue_Limit    : Integer := 0;
      Fail_No_Configs_Left  : Integer := 0;
      Fail_Programmer_Error : Integer := 0;
      Fail_Other            : Integer := 0;
   end record;

   Summary : array (Recover_Label) of Recover_Summary;
begin
   Open (File, In_File, Ada.Command_Line.Argument (1));

   loop
      exit when End_Of_File (File);
      declare
         --  The recover log is written by code in
         --  wisitoken-parse-lr-parser.adb Recover_To_Log
         --
         --  A line has the syntax:
         --  yyyy-mm-dd hh:mm:ss <partial> <success> pre_parser_count '<file_name>' (<parser_data>)...
         --
         --  where there is one (<parser_data) for each parser active after recover. <parser_data> is:
         --
         --  (<strategy_counts>) <enqueue_count> <check_count> <success>
         --
         --  Note that the per-parser success is always TRUE; it would not be
         --  active if recover had failed.

         Line  : constant String := Get_Line (File);
         First : Integer         := Index (Line, " "); -- after date
         Last  : Integer;

         Label : Recover_Label := Full;

         function Line_Eq (Item : in String) return Boolean
         is begin
            return Line (First .. First + Item'Length - 1) = Item;
         end Line_Eq;

         function Next_Integer return Integer
         is begin
            Find_Token
              (Line, Number,
               From  => Last + 1,
               Test  => Ada.Strings.Inside,
               First => First,
               Last  => Last);
            return Integer'Value (Line (First .. Last));
         exception
         when Constraint_Error =>
            raise Constraint_Error with "bad integer '" & Line (First .. Last - 1) & "' " &
              Ada.Text_IO.Count'Image (Ada.Text_IO.Line (File) - 1) & First'Image & Last'Image;
         end Next_Integer;

         function Next_Boolean return  Boolean
         is begin
            First := Last + 2;
            Last  := -1 + Index (Line, Delimiters, First);
            return Boolean'Value (Line (First .. Last));
         end Next_Boolean;

         function Read_Strat_Counts return Strategy_Counts
         is
            Temp : constant Integer := Index (Line, "(", Last + 1);
         begin
            if Temp = 0 then
               --  recover failed; no strategy counts output
               return (others => 0);
            else
               Last := Temp;
            end if;

            return Result : Strategy_Counts := (others => 0) do
               for I in Strategies loop
                  Result (I) := Next_Integer;
               end loop;
               Last := 1 + Index (Line, ")", Last + 1);
            end return;
         end Read_Strat_Counts;

      begin
         First := Index (Line, " ", First + 1); -- after time
         Last  := Index (Line, " ", First + 1); -- after Partial_Parse_Active
         if Boolean'Value (Line (First + 1 .. Last - 1)) then
            Label := Partial;
         end if;

         First := Last + 1;
         if Line (First .. First + 3) = "FAIL" then
            Summary (Label).Event_Count := Summary (Label).Event_Count + 1;

            Summary (Label).Fail_Event_Count := Summary (Label).Fail_Event_Count + 1;
            First := First + 5;

            if Line_Eq ("NO_CONFIGS_LEFT") then
               Summary (Label).Fail_No_Configs_Left := Summary (Label).Fail_No_Configs_Left + 1;
            elsif Line_Eq ("ENQUEUE_LIMIT") then
               Summary (Label).Fail_Enqueue_Limit := Summary (Label).Fail_Enqueue_Limit + 1;
            elsif Line_Eq ("PROGRAMMER_ERROR") then
               Summary (Label).Fail_Programmer_Error := Summary (Label).Fail_Programmer_Error + 1;
            else
               Summary (Label).Fail_Other := Summary (Label).Fail_Other + 1;
            end if;

         else
            --  Process per-parser data
            Last := Index (Line, "(", Last + 1);
            One_Line :
            loop
               exit One_Line when Line (Last + 1) = ')';
               Summary (Label).Event_Count := Summary (Label).Event_Count + 1;
               --  One event per parser, so event_count > strategy_count

               declare
                  Strat_Counts   : constant Strategy_Counts := Read_Strat_Counts;
                  Enqueue_Count  : constant Integer         := Next_Integer;
                  Check_Count    : constant Integer         := Next_Integer;
                  Success        : constant Boolean         := Next_Boolean;
                  pragma Unreferenced (Success);
               begin
                  Summary (Label).Recover_Count_Present := Summary (Label).Recover_Count_Present + 1;

                  Summary (Label).Enqueue_Stats.Accumulate (Long_Float (Enqueue_Count));
                  Summary (Label).Check_Stats.Accumulate (Long_Float (Check_Count));
                  for I in Strategies loop
                     if Strat_Counts (I) > 20 then
                        --  Something got garbled
                        Put_Line (Standard_Error, "strat_counts error: line" &
                                    Ada.Text_IO.Count'Image (Ada.Text_IO.Line (File) - 1) &
                                    Strat_Counts (I)'Image);
                        exit One_Line;
                     else
                        Summary (Label).Recover_Count_Total :=
                          Summary (Label).Recover_Count_Total + Strat_Counts (I);

                        Summary (Label).Strat_Counts_Total (I) :=
                          Summary (Label).Strat_Counts_Total (I) + Strat_Counts (I);

                        if Strat_Counts (I) > 0 then
                           Summary (Label).Strat_Counts_Present (I) := Summary (Label).Strat_Counts_Present (I) + 1;
                        end if;
                     end if;
                  end loop;
               end;
            end loop One_Line;
         end if;

         if Summary (Label).Event_Count mod 1000 = 0 then
            Put_Line (Standard_Error,
                      "line:" & Ada.Text_IO.Count'Image (Ada.Text_IO.Line (File) - 1) & " " &
                     Summary (Label).Recover_Count_Total'Image);
         end if;
      exception
      when E : Constraint_Error =>
         --  a line got garbled by a glitch; go on to the next
         Put_Line (Standard_Error, "CONSTRAINT_ERROR: line" & Ada.Text_IO.Count'Image (Ada.Text_IO.Line (File) - 1) &
                     " " & Ada.Exceptions.Exception_Message (E));
      end;
   end loop;

   declare
      use Ada.Strings;

      Label_Field     : String (1 .. 23); -- fits strategy and fail labels
      Count_Field     : String (1 .. 8);
      Percent_Field   : String (1 .. 4);
      --  Shared by Put_If, Put_Percent

      procedure Put_If
        (Summary_Label : in Recover_Label;
         Name          : in String;
         Count         : in Integer;
         Always        : in Boolean := False)
      is
         Percent_Present : constant Integer :=
           Integer (100.0 * Float (Count) / Float (Summary (Summary_Label).Event_Count));
      begin
         if Count > 0 or Always then
            Move (Name, Label_Field); Put (Label_Field & " => ");
            Move (Count'Image, Count_Field, Justify => Right); Put (Count_Field);
            Move (Percent_Present'Image & "%", Percent_Field, Justify => Right); Put_Line (Percent_Field);
         end if;
      end Put_If;

      package Stats_Image is new SAL.Long_Float_Stats.Gen_Image
        (Real_IO           => Ada.Long_Float_Text_IO,
         Default_Mean_Fore => 7,
         Default_Mean_Aft  => 0,
         Default_Mean_Exp  => 0,
         Default_Sd_Fore   => 7,
         Default_Sd_Aft    => 1,
         Default_Sd_Exp    => 0);

      procedure Put_Percent (Summary_Label : in Recover_Label; Present, Total : in Integer; Name : in String)
      is
         Percent_Present : constant Integer :=
           Integer (100.0 * Float (Present) / Float (Summary (Summary_Label).Recover_Count_Present));
         Percent_Total   : constant Integer :=
           Integer (100.0 * Float (Total) / Float (Summary (Summary_Label).Recover_Count_Total));
      begin
         Move (Name, Label_Field); Put (Label_Field);
         Move (Present'Image, Count_Field, Justify => Right); Put (Count_Field);
         Move (Percent_Present'Image & "%", Percent_Field, Justify => Right); Put (Percent_Field & " /");
         Move (Total'Image, Count_Field, Justify => Right); Put (Count_Field);
         Move (Percent_Total'Image & "%", Percent_Field, Justify => Right); Put_Line (Percent_Field);
      end Put_Percent;

   begin
      for I in Recover_Label loop
         Put_Line (I'Image);
         Put_Line ("present/total:" & Summary (I).Event_Count'Image & " /" & Summary (I).Recover_Count_Total'Image);
         if Summary (I).Event_Count > 0 then
            Put_Line ("           mean        std. dev.    min     max");
            Put_Line ("Enqueue: " & Stats_Image.Image (Summary (I).Enqueue_Stats.Display));
            Put_Line ("Check:   " & Stats_Image.Image (Summary (I).Check_Stats.Display));
            Put_If (I, "FAIL", Summary (I).Fail_Event_Count, Always => True);
            Put_If (I, "FAIL_ENQUEUE_LIMIT", Summary (I).Fail_Enqueue_Limit);
            Put_If (I, "FAIL_NO_CONFIGS_LEFT", Summary (I).Fail_No_Configs_Left);
            Put_If (I, "FAIL_PROGRAMMER_ERROR", Summary (I).Fail_Programmer_Error);
            Put_If (I, "FAIL_OTHER", Summary (I).Fail_Other);
            for J in Strategies loop
               Put_Percent
                 (I,
                  Summary (I).Strat_Counts_Present (J),
                  Summary (I).Strat_Counts_Total (J),
                  J'Image);
            end loop;
         end if;
         New_Line;
      end loop;
   end;
exception
when E : others =>
   Put_Line (Ada.Exceptions.Exception_Name (E) & ": " & Ada.Exceptions.Exception_Message (E));
   Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
end Recover_Stats;