src/Pure/ML/ml_statistics.ML
author wenzelm
Fri, 05 Mar 2021 16:44:04 +0100
changeset 73384 d21dbfd3d69b
parent 72134 f40200b5bb3c
permissions -rw-r--r--
obsolete;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62459
7a5d88dd8cc9 support only polyml-5.3.0 and polyml-5.6;
wenzelm
parents: 51990
diff changeset
     1
(*  Title:      Pure/ML/ml_statistics.ML
50255
d0ec1f0d1d7d some support for ML runtime statistics;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
d0ec1f0d1d7d some support for ML runtime statistics;
wenzelm
parents:
diff changeset
     3
62945
c38c08889aa9 tuned comments;
wenzelm
parents: 62459
diff changeset
     4
ML runtime statistics.
50255
d0ec1f0d1d7d some support for ML runtime statistics;
wenzelm
parents:
diff changeset
     5
*)
d0ec1f0d1d7d some support for ML runtime statistics;
wenzelm
parents:
diff changeset
     6
d0ec1f0d1d7d some support for ML runtime statistics;
wenzelm
parents:
diff changeset
     7
signature ML_STATISTICS =
d0ec1f0d1d7d some support for ML runtime statistics;
wenzelm
parents:
diff changeset
     8
sig
72038
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
     9
  val set: {tasks_ready: int, tasks_pending: int, tasks_running: int, tasks_passive: int,
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    10
    tasks_urgent: int, workers_total: int, workers_active: int, workers_waiting: int} -> unit
72031
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
    11
  val get: unit -> (string * string) list
72032
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    12
  val get_external: int -> (string * string) list
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    13
  val monitor: int -> real -> unit
50255
d0ec1f0d1d7d some support for ML runtime statistics;
wenzelm
parents:
diff changeset
    14
end;
d0ec1f0d1d7d some support for ML runtime statistics;
wenzelm
parents:
diff changeset
    15
d0ec1f0d1d7d some support for ML runtime statistics;
wenzelm
parents:
diff changeset
    16
structure ML_Statistics: ML_STATISTICS =
d0ec1f0d1d7d some support for ML runtime statistics;
wenzelm
parents:
diff changeset
    17
struct
d0ec1f0d1d7d some support for ML runtime statistics;
wenzelm
parents:
diff changeset
    18
72031
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
    19
(* print *)
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
    20
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
    21
fun print_int x = if x < 0 then "-" ^ Int.toString (~ x) else Int.toString x;
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
    22
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
    23
fun print_real0 x =
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
    24
  let val s = Real.fmt (StringCvt.GEN NONE) x in
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
    25
    (case String.fields (fn c => c = #".") s of
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
    26
      [a, b] => if List.all (fn c => c = #"0") (String.explode b) then a else s
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
    27
    | _ => s)
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
    28
  end;
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
    29
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
    30
fun print_real x =
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
    31
  if x < 0.0 then "-" ^ print_real0 (~ x) else print_real0 x;
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
    32
72032
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    33
val print_properties =
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    34
  String.concatWith "," o map (fn (a, b) => a ^ "=" ^ b);
72031
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
    35
72032
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    36
72038
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    37
(* set user properties *)
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    38
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    39
fun set {tasks_ready, tasks_pending, tasks_running, tasks_passive, tasks_urgent,
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    40
    workers_total, workers_active, workers_waiting} =
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    41
 (PolyML.Statistics.setUserCounter (0, tasks_ready);
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    42
  PolyML.Statistics.setUserCounter (1, tasks_pending);
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    43
  PolyML.Statistics.setUserCounter (2, tasks_running);
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    44
  PolyML.Statistics.setUserCounter (3, tasks_passive);
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    45
  PolyML.Statistics.setUserCounter (4, tasks_urgent);
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    46
  PolyML.Statistics.setUserCounter (5, workers_total);
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    47
  PolyML.Statistics.setUserCounter (6, workers_active);
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    48
  PolyML.Statistics.setUserCounter (7, workers_waiting));
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    49
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    50
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    51
(* get properties *)
72031
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
    52
72041
7b112eedc859 more robust wrt. experimental changes in Poly/ML;
wenzelm
parents: 72040
diff changeset
    53
local
7b112eedc859 more robust wrt. experimental changes in Poly/ML;
wenzelm
parents: 72040
diff changeset
    54
72032
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    55
fun make_properties
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    56
   {gcFullGCs,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    57
    gcPartialGCs,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    58
    gcSharePasses,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    59
    sizeAllocation,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    60
    sizeAllocationFree,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    61
    sizeCode,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    62
    sizeHeap,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    63
    sizeHeapFreeLastFullGC,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    64
    sizeHeapFreeLastGC,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    65
    sizeStacks,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    66
    threadsInML,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    67
    threadsTotal,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    68
    threadsWaitCondVar,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    69
    threadsWaitIO,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    70
    threadsWaitMutex,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    71
    threadsWaitSignal,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    72
    timeGCReal,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    73
    timeGCSystem,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    74
    timeGCUser,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    75
    timeNonGCReal,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    76
    timeNonGCSystem,
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
    77
    timeNonGCUser,
72134
f40200b5bb3c support for GC state;
wenzelm
parents: 72113
diff changeset
    78
    userCounters,
f40200b5bb3c support for GC state;
wenzelm
parents: 72113
diff changeset
    79
    gcState = gc_state} =
50255
d0ec1f0d1d7d some support for ML runtime statistics;
wenzelm
parents:
diff changeset
    80
  let
72038
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    81
    val tasks_ready = Vector.sub (userCounters, 0);
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    82
    val tasks_pending = Vector.sub (userCounters, 1);
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    83
    val tasks_running = Vector.sub (userCounters, 2);
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    84
    val tasks_passive = Vector.sub (userCounters, 3);
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    85
    val tasks_urgent = Vector.sub (userCounters, 4);
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    86
    val tasks_total = tasks_ready + tasks_pending + tasks_running + tasks_passive + tasks_urgent;
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    87
    val workers_total = Vector.sub (userCounters, 5);
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    88
    val workers_active = Vector.sub (userCounters, 6);
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    89
    val workers_waiting = Vector.sub (userCounters, 7);
72134
f40200b5bb3c support for GC state;
wenzelm
parents: 72113
diff changeset
    90
    val gc_percent =
f40200b5bb3c support for GC state;
wenzelm
parents: 72113
diff changeset
    91
      (case gc_state of
f40200b5bb3c support for GC state;
wenzelm
parents: 72113
diff changeset
    92
        PolyML.Statistics.MLCode => 0
f40200b5bb3c support for GC state;
wenzelm
parents: 72113
diff changeset
    93
      | PolyML.Statistics.MinorGC p => p + 1
f40200b5bb3c support for GC state;
wenzelm
parents: 72113
diff changeset
    94
      | PolyML.Statistics.MajorGC p => p + 1
f40200b5bb3c support for GC state;
wenzelm
parents: 72113
diff changeset
    95
      | PolyML.Statistics.GCSharing p => p + 1
f40200b5bb3c support for GC state;
wenzelm
parents: 72113
diff changeset
    96
      | PolyML.Statistics.OtherState p => p + 1);
50255
d0ec1f0d1d7d some support for ML runtime statistics;
wenzelm
parents:
diff changeset
    97
  in
72038
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    98
    [("now", print_real (Time.toReal (Time.now ()))),
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
    99
     ("tasks_ready", print_int tasks_ready),
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
   100
     ("tasks_pending", print_int tasks_pending),
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
   101
     ("tasks_running", print_int tasks_running),
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
   102
     ("tasks_passive", print_int tasks_passive),
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
   103
     ("tasks_urgent", print_int tasks_urgent),
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
   104
     ("tasks_total", print_int tasks_total),
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
   105
     ("workers_total", print_int workers_total),
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
   106
     ("workers_active", print_int workers_active),
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
   107
     ("workers_waiting", print_int workers_waiting),
254c324f31fd clarified user counters: expose tasks to external monitor;
wenzelm
parents: 72033
diff changeset
   108
     ("full_GCs", print_int gcFullGCs),
72031
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   109
     ("partial_GCs", print_int gcPartialGCs),
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   110
     ("share_passes", print_int gcSharePasses),
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   111
     ("size_allocation", print_int sizeAllocation),
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   112
     ("size_allocation_free", print_int sizeAllocationFree),
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   113
     ("size_code", print_int sizeCode),
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   114
     ("size_heap", print_int sizeHeap),
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   115
     ("size_heap_free_last_full_GC", print_int sizeHeapFreeLastFullGC),
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   116
     ("size_heap_free_last_GC", print_int sizeHeapFreeLastGC),
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   117
     ("size_stacks", print_int sizeStacks),
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   118
     ("threads_in_ML", print_int threadsInML),
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   119
     ("threads_total", print_int threadsTotal),
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   120
     ("threads_wait_condvar", print_int threadsWaitCondVar),
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   121
     ("threads_wait_IO", print_int threadsWaitIO),
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   122
     ("threads_wait_mutex", print_int threadsWaitMutex),
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   123
     ("threads_wait_signal", print_int threadsWaitSignal),
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   124
     ("time_elapsed", print_real (Time.toReal timeNonGCReal)),
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   125
     ("time_elapsed_GC", print_real (Time.toReal timeGCReal)),
b7cec26e41d1 clarified modules: ML_Statistics within bootstrap environment;
wenzelm
parents: 69822
diff changeset
   126
     ("time_CPU", print_real (Time.toReal timeNonGCSystem + Time.toReal timeNonGCUser)),
72134
f40200b5bb3c support for GC state;
wenzelm
parents: 72113
diff changeset
   127
     ("time_GC", print_real (Time.toReal timeGCSystem + Time.toReal timeGCUser)),
f40200b5bb3c support for GC state;
wenzelm
parents: 72113
diff changeset
   128
     ("GC_percent", print_int gc_percent)]
50255
d0ec1f0d1d7d some support for ML runtime statistics;
wenzelm
parents:
diff changeset
   129
  end;
d0ec1f0d1d7d some support for ML runtime statistics;
wenzelm
parents:
diff changeset
   130
72041
7b112eedc859 more robust wrt. experimental changes in Poly/ML;
wenzelm
parents: 72040
diff changeset
   131
in
7b112eedc859 more robust wrt. experimental changes in Poly/ML;
wenzelm
parents: 72040
diff changeset
   132
72032
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
   133
fun get () =
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
   134
  make_properties (PolyML.Statistics.getLocalStats ());
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
   135
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
   136
fun get_external pid =
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
   137
  make_properties (PolyML.Statistics.getRemoteStats pid);
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
   138
72041
7b112eedc859 more robust wrt. experimental changes in Poly/ML;
wenzelm
parents: 72040
diff changeset
   139
end;
7b112eedc859 more robust wrt. experimental changes in Poly/ML;
wenzelm
parents: 72040
diff changeset
   140
72032
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
   141
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
   142
(* monitor process *)
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
   143
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
   144
fun monitor pid delay =
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
   145
  let
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
   146
    fun loop () =
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
   147
      (TextIO.output (TextIO.stdOut, print_properties (get_external pid) ^ "\n");
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
   148
       TextIO.flushOut TextIO.stdOut;
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
   149
       OS.Process.sleep (Time.fromReal delay);
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
   150
       loop ());
72040
bc85d93aad23 more robust: handle unavailable statistics;
wenzelm
parents: 72038
diff changeset
   151
    fun exit () = OS.Process.exit OS.Process.success;
72111
b9ded33bd58c clarified catch-all handler --- avoid confusion of Interrupt vs. Exn.Interrupt in varying ML contexts;
wenzelm
parents: 72041
diff changeset
   152
  in loop () handle _ (*sic!*) => exit () end;
72032
a25c7c686176 support for monitoring of external ML process;
wenzelm
parents: 72031
diff changeset
   153
50255
d0ec1f0d1d7d some support for ML runtime statistics;
wenzelm
parents:
diff changeset
   154
end;