support for monitoring of external ML process;
authorwenzelm
Mon, 13 Jul 2020 23:10:47 +0200
changeset 72032 a25c7c686176
parent 72031 b7cec26e41d1
child 72033 70bfda10f597
support for monitoring of external ML process;
src/Pure/ML/ml_statistics.ML
--- a/src/Pure/ML/ml_statistics.ML	Mon Jul 13 22:07:18 2020 +0200
+++ b/src/Pure/ML/ml_statistics.ML	Mon Jul 13 23:10:47 2020 +0200
@@ -7,6 +7,8 @@
 signature ML_STATISTICS =
 sig
   val get: unit -> (string * string) list
+  val get_external: int -> (string * string) list
+  val monitor: int -> real -> unit
 end;
 
 structure ML_Statistics: ML_STATISTICS =
@@ -26,35 +28,37 @@
 fun print_real x =
   if x < 0.0 then "-" ^ print_real0 (~ x) else print_real0 x;
 
+val print_properties =
+  String.concatWith "," o map (fn (a, b) => a ^ "=" ^ b);
 
-(* get *)
+
+(* make properties *)
 
-fun get () =
+fun make_properties
+   {gcFullGCs,
+    gcPartialGCs,
+    gcSharePasses,
+    sizeAllocation,
+    sizeAllocationFree,
+    sizeCode,
+    sizeHeap,
+    sizeHeapFreeLastFullGC,
+    sizeHeapFreeLastGC,
+    sizeStacks,
+    threadsInML,
+    threadsTotal,
+    threadsWaitCondVar,
+    threadsWaitIO,
+    threadsWaitMutex,
+    threadsWaitSignal,
+    timeGCReal,
+    timeGCSystem,
+    timeGCUser,
+    timeNonGCReal,
+    timeNonGCSystem,
+    timeNonGCUser,
+    userCounters} =
   let
-    val
-     {gcFullGCs,
-      gcPartialGCs,
-      gcSharePasses,
-      sizeAllocation,
-      sizeAllocationFree,
-      sizeCode,
-      sizeHeap,
-      sizeHeapFreeLastFullGC,
-      sizeHeapFreeLastGC,
-      sizeStacks,
-      threadsInML,
-      threadsTotal,
-      threadsWaitCondVar,
-      threadsWaitIO,
-      threadsWaitMutex,
-      threadsWaitSignal,
-      timeGCReal,
-      timeGCSystem,
-      timeGCUser,
-      timeNonGCReal,
-      timeNonGCSystem,
-      timeNonGCUser,
-      userCounters} = PolyML.Statistics.getLocalStats ();
     val user_counters =
       Vector.foldri
         (fn (i, j, res) => ("user_counter" ^ print_int i, print_int j) :: res)
@@ -83,4 +87,25 @@
     user_counters
   end;
 
+
+(* get properties *)
+
+fun get () =
+  make_properties (PolyML.Statistics.getLocalStats ());
+
+fun get_external pid =
+  make_properties (PolyML.Statistics.getRemoteStats pid);
+
+
+(* monitor process *)
+
+fun monitor pid delay =
+  let
+    fun loop () =
+      (TextIO.output (TextIO.stdOut, print_properties (get_external pid) ^ "\n");
+       TextIO.flushOut TextIO.stdOut;
+       OS.Process.sleep (Time.fromReal delay);
+       loop ());
+  in loop () end;
+
 end;