[m-rev.] for review: update stream line numbers when reporting statistics

Julien Fischer jfischer at opturion.com
Sun Apr 4 00:54:10 AEDT 2021


For review by anyone.

--------------------

Update stream line numbers when reporting statistics.

Stream line number counters were not being consistently updated by the
predicates used to report statistics. This change fixes the places where these
line number counters are not being updated.

runtime/mercury_report_stats.{h,c}:
     Pass the stream line number counter to the functions used to write statistics.

     Update stream line number counts as statistics are printed.

library/io.m:
library/benchmarking.m:
     Pass the stream line number counters to the C versions of the statistics
     reporting functions. (The C# and Java versions already update the counter
     correctly.)

Julien.

diff --git a/library/benchmarking.m b/library/benchmarking.m
index a7a76f8fe..137f381a6 100644
--- a/library/benchmarking.m
+++ b/library/benchmarking.m
@@ -172,48 +172,60 @@

  %---------------------%

+report_stats :-
+    impure do_report_stats(io.stderr_stream).
+
+:- impure pred do_report_stats(io.text_output_stream::in) is det.
+
  :- pragma foreign_proc("C",
-    report_stats,
+    do_report_stats(S::in),
      [will_not_call_mercury],
  "
-    MR_report_standard_stats(stderr);
+    MercuryFile mf = *(MR_unwrap_output_stream(S));
+    MR_report_standard_stats(MR_file(mf), &MR_line_number(mf));
  ").

  :- pragma foreign_proc("C#",
-    report_stats,
+    do_report_stats(Stream::in),
      [may_call_mercury, terminates],
  "
-    ML_report_standard_stats(io.mercury_stderr);
+    ML_report_standard_stats(Stream.F1);
  ").

  :- pragma foreign_proc("Java",
-    report_stats,
+    do_report_stats(Stream::in),
      [may_call_mercury, terminates],
  "
-    ML_report_standard_stats(io.mercury_stderr);
+    ML_report_standard_stats((io.MR_TextOutputFile) Stream.F1);
  ").

  %---------------------%

+report_full_memory_stats :-
+    impure do_report_full_memory_stats(io.stderr_stream).
+
+:- impure pred do_report_full_memory_stats(io.text_output_stream::in) is det.
+
  :- pragma foreign_proc("C",
-    report_full_memory_stats,
+    do_report_full_memory_stats(Stream::in),
      [will_not_call_mercury],
  "
-    MR_report_full_memory_stats(stderr);
+    MercuryFile mf = *(MR_unwrap_output_stream(Stream));
+    MR_report_full_memory_stats(MR_file(mf), &MR_line_number(mf));
  ").

  :- pragma foreign_proc("C#",
-    report_full_memory_stats,
+    do_report_full_memory_stats(Stream::in),
      [will_not_call_mercury],
  "
-    ML_report_full_memory_stats(io.mercury_stderr);
+    ML_report_full_memory_stats(Stream.F1);
  ").

  :- pragma foreign_proc("Java",
-    report_full_memory_stats,
+    do_report_full_memory_stats(Stream::in),
      [will_not_call_mercury],
  "
-    ML_report_full_memory_stats(io.mercury_stderr);
+    ML_report_full_memory_stats((io.MR_TextOutputFile) Stream.F1);
  ").

  %---------------------------------------------------------------------------%
diff --git a/library/io.m b/library/io.m
index 697bbe437..3ecd14ed9 100644
--- a/library/io.m
+++ b/library/io.m
@@ -12123,7 +12123,7 @@ report_standard_stats(!IO) :-
      [promise_pure, will_not_call_mercury, tabled_for_io,
          does_not_affect_liveness],
  "
-    MR_report_standard_stats(MR_file(*Stream));
+    MR_report_standard_stats(MR_file(*Stream), &MR_line_number(*Stream));
  ").

  :- pragma foreign_proc("C#",
@@ -12157,7 +12157,7 @@ report_full_memory_stats(!IO) :-
      [promise_pure, will_not_call_mercury, tabled_for_io,
          does_not_affect_liveness],
  "
-    MR_report_full_memory_stats(MR_file(*Stream));
+    MR_report_full_memory_stats(MR_file(*Stream), &MR_line_number(*Stream));
  ").

  :- pragma foreign_proc("C#",
diff --git a/runtime/mercury_report_stats.c b/runtime/mercury_report_stats.c
index 54657a439..0efa0bad1 100644
--- a/runtime/mercury_report_stats.c
+++ b/runtime/mercury_report_stats.c
@@ -57,7 +57,7 @@
    static    int     MR_memory_profile_fill_table(MR_memprof_record *node,
                          MR_memprof_report_entry *table, int next_slot);

-  static    void    MR_memory_profile_report(FILE *fp,
+  static    void    MR_memory_profile_report(FILE *fp, int *line_number,
                          const MR_memprof_report_entry *,
                          int num_entries, MR_bool complete);

@@ -67,7 +67,7 @@
  #endif // MR_MPROF_PROFILE_MEMORY

  void
-MR_report_standard_stats(FILE *fp)
+MR_report_standard_stats(FILE *fp, int *line_number)
  {
      int                 user_time_at_prev_stat;
      int                 real_time_at_prev_stat;
@@ -153,11 +153,13 @@ MR_report_standard_stats(FILE *fp)
          GC_get_bytes_since_gc() / 1024.0,
          GC_get_heap_size() / 1024.0
      );
+    (*line_number)++;
    #endif
  #else // !MR_CONSERVATIVE_GC
      fprintf(fp, "\nHeap: %.3fk",
          ((char *) MR_hp - (char *) eng->MR_eng_heap_zone->MR_zone_min) / 1024.0
      );
+    (*line_number)++;
  #endif // !MR_CONSERVATIVE_GC

  #ifdef  MR_MPROF_PROFILE_MEMORY
@@ -170,13 +172,17 @@ MR_report_standard_stats(FILE *fp)
      num_table_entries = MR_memory_profile_top_table(MR_memprof_procs.root,
          table, MEMORY_PROFILE_SIZE, 0);
      fprintf(fp, "\nMemory profile by procedure\n");
-    MR_memory_profile_report(fp, table, num_table_entries, MR_FALSE);
+    *line_number += 2;
+    MR_memory_profile_report(fp, line_number, table, num_table_entries,
+        MR_FALSE);

      // Print out the per-type memory profile (top N entries).
      num_table_entries = MR_memory_profile_top_table(MR_memprof_types.root,
          table, MEMORY_PROFILE_SIZE, 0);
      fprintf(fp, "\nMemory profile by type\n");
-    MR_memory_profile_report(fp, table, num_table_entries, MR_FALSE);
+    *line_number += 2;
+    MR_memory_profile_report(fp, line_number, table, num_table_entries,
+        MR_FALSE);

      // Print out the overall memory usage.
      fprintf(fp, "Overall memory usage:"
@@ -186,17 +192,20 @@ MR_report_standard_stats(FILE *fp)
          MR_overall_memprof_counter.words_since_period_start,
          MR_overall_memprof_counter.words_at_period_end
      );
+    (*line_number)++;

  #endif // MR_MPROF_PROFILE_MEMORY

      fprintf(fp, "]\n");
+    (*line_number)++;
  }

  void
-MR_report_full_memory_stats(FILE *fp)
+MR_report_full_memory_stats(FILE *fp, int *line_number)
  {
  #ifndef MR_MPROF_PROFILE_MEMORY
      fprintf(fp, "\nMemory profiling is not enabled.\n");
+    *line_number += 2;
  #else
      int                     num_table_entries;
      int                     table_size;
@@ -220,9 +229,12 @@ MR_report_full_memory_stats(FILE *fp)
      qsort(table, MR_memprof_procs.num_entries, sizeof(MR_memprof_report_entry),
          MR_memory_profile_compare_final);
      fprintf(fp, "\nMemory profile by procedure\n");
+    *line_number += 2;
      fprintf(fp, "%14s %14s  %s\n",
          "Cells", "Words", "Procedure label");
-    MR_memory_profile_report(fp, table, num_table_entries, MR_TRUE);
+    (*line_number)++;
+    MR_memory_profile_report(fp, line_number, table, num_table_entries,
+        MR_TRUE);

      // Print the by-type memory profile.
      num_table_entries = MR_memory_profile_fill_table(MR_memprof_types.root,
@@ -230,9 +242,12 @@ MR_report_full_memory_stats(FILE *fp)
      qsort(table, MR_memprof_types.num_entries, sizeof(MR_memprof_report_entry),
          MR_memory_profile_compare_final);
      fprintf(fp, "\nMemory profile by type\n");
+    *line_number += 2;
      fprintf(fp, "%14s %14s  %s\n",
          "Cells", "Words", "Procedure label");
-    MR_memory_profile_report(fp, table, num_table_entries, MR_TRUE);
+    (*line_number)++;
+    MR_memory_profile_report(fp, line_number, table, num_table_entries,
+        MR_TRUE);

      // Deallocate space for the table.
      MR_GC_free(table);
@@ -242,6 +257,7 @@ MR_report_full_memory_stats(FILE *fp)
          MR_overall_memprof_counter.cells_at_period_end,
          MR_overall_memprof_counter.words_at_period_end
      );
+    *line_number += 2;
  #endif // MR_MPROF_PROFILE_MEMORY
  }

@@ -394,13 +410,13 @@ MR_memory_profile_fill_table(MR_memprof_record *node,
      return next_slot;
  }

-// MR_memory_profile_report(fp, table, num_entries, complete):
+// MR_memory_profile_report(fp, line_number, table, num_entries, complete):
  //
  // Print out a profiling report for the specified table.

  static void
-MR_memory_profile_report(FILE *fp, const MR_memprof_report_entry *table,
-    int num_entries, MR_bool complete)
+MR_memory_profile_report(FILE *fp, int *line_number,
+    const MR_memprof_report_entry *table, int num_entries, MR_bool complete)
  {
      int         i;
      const char  *name;
@@ -410,6 +426,7 @@ MR_memory_profile_report(FILE *fp, const MR_memprof_report_entry *table,
          ||  MR_overall_memprof_counter.words_at_period_end < 1.0)
          {
              fprintf(fp, "no allocations to report\n");
+            (*line_number)++;
              return;
          }
      } else {
@@ -417,6 +434,7 @@ MR_memory_profile_report(FILE *fp, const MR_memprof_report_entry *table,
          ||  MR_overall_memprof_counter.words_since_period_start < 1.0)
          {
              fprintf(fp, "no allocations to report\n");
+            (*line_number)++;
              return;
          }
      }
@@ -436,6 +454,7 @@ MR_memory_profile_report(FILE *fp, const MR_memprof_report_entry *table,
                      MR_overall_memprof_counter.words_at_period_end,
                  table[i].name
              );
+            (*line_number)++;
          } else {
              fprintf(fp, "%8.8g/%4.1f%% %8.8g/%4.1f%%  %s\n",
                  table[i].counter.cells_since_period_start,
@@ -446,6 +465,7 @@ MR_memory_profile_report(FILE *fp, const MR_memprof_report_entry *table,
                     MR_overall_memprof_counter.words_since_period_start,
                  table[i].name
              );
+            (*line_number)++;
          }
      }
  }
diff --git a/runtime/mercury_report_stats.h b/runtime/mercury_report_stats.h
index 3552115d9..3ffdcbdfc 100644
--- a/runtime/mercury_report_stats.h
+++ b/runtime/mercury_report_stats.h
@@ -13,8 +13,8 @@
  #include "mercury_timing.h"
  #include "mercury_heap.h"

-extern void MR_report_standard_stats(FILE *fp);
+extern void MR_report_standard_stats(FILE *fp, int *line_number);

-extern void MR_report_full_memory_stats(FILE *fp);
+extern void MR_report_full_memory_stats(FILE *fp, int *line_number);

  #endif  // not MERCURY_REPORT_STATS_H


More information about the reviews mailing list