src/Pure/ML/ml_statistics.scala
author wenzelm
Mon, 20 Mar 2023 10:59:27 +0100
changeset 77692 3e746e684f4b
parent 77368 7c57d9586f4c
child 77708 f137bf5d3d94
permissions -rw-r--r--
clarified operations for ML object sizes;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
65477
64e61b0f6972 clarified directories;
wenzelm
parents: 65318
diff changeset
     1
/*  Title:      Pure/ML/ml_statistics.scala
50685
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
     3
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
     4
ML runtime statistics.
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
     5
*/
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
     6
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
     7
package isabelle
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
     8
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
     9
65858
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
    10
import scala.annotation.tailrec
51432
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    11
import scala.collection.mutable
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    12
import scala.collection.immutable.{SortedSet, SortedMap}
50691
20beafe66748 added standard_frames convenience;
wenzelm
parents: 50690
diff changeset
    13
import scala.swing.{Frame, Component}
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    14
50689
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
    15
import org.jfree.data.xy.{XYSeries, XYSeriesCollection}
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
    16
import org.jfree.chart.{JFreeChart, ChartPanel, ChartFactory}
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
    17
import org.jfree.chart.plot.PlotOrientation
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
    18
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    19
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
    20
object ML_Statistics {
65855
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
    21
  /* properties */
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    22
65855
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
    23
  val Now = new Properties.Double("now")
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
    24
  def now(props: Properties.T): Double = Now.unapply(props).get
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
    25
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    26
72138
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    27
  /* memory status */
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    28
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    29
  val Heap_Size = new Properties.Long("size_heap")
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    30
  val Heap_Free = new Properties.Long("size_heap_free_last_GC")
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    31
  val GC_Percent = new Properties.Int("GC_percent")
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    32
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
    33
  sealed case class Memory_Status(heap_size: Long, heap_free: Long, gc_percent: Int) {
72138
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    34
    def heap_used: Long = (heap_size - heap_free) max 0
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    35
    def heap_used_fraction: Double =
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    36
      if (heap_size == 0) 0.0 else heap_used.toDouble / heap_size
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    37
    def gc_progress: Option[Double] =
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    38
      if (1 <= gc_percent && gc_percent <= 100) Some((gc_percent - 1) * 0.01) else None
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    39
  }
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    40
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
    41
  def memory_status(props: Properties.T): Memory_Status = {
72138
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    42
    val heap_size = Heap_Size.get(props)
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    43
    val heap_free = Heap_Free.get(props)
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    44
    val gc_percent = GC_Percent.get(props)
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    45
    Memory_Status(heap_size, heap_free, gc_percent)
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    46
  }
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    47
fa57d299b46b support for Poly/ML memory status;
wenzelm
parents: 72136
diff changeset
    48
72035
25d5ef16401a support for monitoring of external ML process;
wenzelm
parents: 71601
diff changeset
    49
  /* monitor process */
25d5ef16401a support for monitoring of external ML process;
wenzelm
parents: 71601
diff changeset
    50
25d5ef16401a support for monitoring of external ML process;
wenzelm
parents: 71601
diff changeset
    51
  def monitor(pid: Long,
72119
d115d50a19c0 provide POLYSTATSDIR to keep $HOME/.polyml clean (requires Poly/ML 52881757b127, otherwise ignored);
wenzelm
parents: 72117
diff changeset
    52
    stats_dir: String = "",
72035
25d5ef16401a support for monitoring of external ML process;
wenzelm
parents: 71601
diff changeset
    53
    delay: Time = Time.seconds(0.5),
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
    54
    consume: Properties.T => Unit = Console.println
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
    55
  ): Unit = {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
    56
    def progress_stdout(line: String): Unit = {
77218
86217697863c tuned signature;
wenzelm
parents: 77121
diff changeset
    57
      val props = space_explode(',', line).flatMap(Properties.Eq.unapply)
72035
25d5ef16401a support for monitoring of external ML process;
wenzelm
parents: 71601
diff changeset
    58
      if (props.nonEmpty) consume(props)
25d5ef16401a support for monitoring of external ML process;
wenzelm
parents: 71601
diff changeset
    59
    }
25d5ef16401a support for monitoring of external ML process;
wenzelm
parents: 71601
diff changeset
    60
72119
d115d50a19c0 provide POLYSTATSDIR to keep $HOME/.polyml clean (requires Poly/ML 52881757b127, otherwise ignored);
wenzelm
parents: 72117
diff changeset
    61
    val env_prefix =
77368
wenzelm
parents: 77218
diff changeset
    62
      if_proper(stats_dir, "export POLYSTATSDIR=" + Bash.string(stats_dir) + "\n")
72119
d115d50a19c0 provide POLYSTATSDIR to keep $HOME/.polyml clean (requires Poly/ML 52881757b127, otherwise ignored);
wenzelm
parents: 72117
diff changeset
    63
73604
51b291ae3e2d avoid "exec" to change the winpid;
wenzelm
parents: 73522
diff changeset
    64
    Bash.process(env_prefix + "\"$POLYML_EXE\" -q --use src/Pure/ML/ml_statistics.ML --eval " +
72044
efd169aed4dc more robust: avoid potential problems with encoding of directory name;
wenzelm
parents: 72037
diff changeset
    65
        Bash.string("ML_Statistics.monitor " + ML_Syntax.print_long(pid) + " " +
efd169aed4dc more robust: avoid potential problems with encoding of directory name;
wenzelm
parents: 72037
diff changeset
    66
          ML_Syntax.print_double(delay.seconds)),
73522
b219774a71ae tuned signature -- more explicit types;
wenzelm
parents: 73367
diff changeset
    67
        cwd = Path.ISABELLE_HOME.file)
72035
25d5ef16401a support for monitoring of external ML process;
wenzelm
parents: 71601
diff changeset
    68
      .result(progress_stdout = progress_stdout, strict = false).check
25d5ef16401a support for monitoring of external ML process;
wenzelm
parents: 71601
diff changeset
    69
  }
25d5ef16401a support for monitoring of external ML process;
wenzelm
parents: 71601
diff changeset
    70
25d5ef16401a support for monitoring of external ML process;
wenzelm
parents: 71601
diff changeset
    71
72112
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
    72
  /* protocol handler */
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
    73
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
    74
  class Handler extends Session.Protocol_Handler {
72112
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
    75
    private var session: Session = null
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
    76
    private var monitoring: Future[Unit] = Future.value(())
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
    77
75380
2cb2606ce075 tuned: avoid problems with scala3;
wenzelm
parents: 74853
diff changeset
    78
    override def init(session: Session): Unit = synchronized {
72213
wenzelm
parents: 72212
diff changeset
    79
      this.session = session
72112
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
    80
    }
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
    81
75380
2cb2606ce075 tuned: avoid problems with scala3;
wenzelm
parents: 74853
diff changeset
    82
    override def exit(): Unit = synchronized {
72112
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
    83
      session = null
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73359
diff changeset
    84
      monitoring.cancel()
72112
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
    85
    }
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
    86
75380
2cb2606ce075 tuned: avoid problems with scala3;
wenzelm
parents: 74853
diff changeset
    87
    private def consume(props: Properties.T): Unit = synchronized {
72136
98dca728fc9c removed pointless option "ML_statistics": always enabled;
wenzelm
parents: 72119
diff changeset
    88
      if (session != null) {
73031
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 72250
diff changeset
    89
        val props1 = (session.cache.props(props ::: Java_Statistics.jvm_statistics()))
72149
36a34f3a8cb8 support JVM runtime statistics;
wenzelm
parents: 72143
diff changeset
    90
        session.runtime_statistics.post(Session.Runtime_Statistics(props1))
72112
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
    91
      }
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
    92
    }
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
    93
75380
2cb2606ce075 tuned: avoid problems with scala3;
wenzelm
parents: 74853
diff changeset
    94
    private def ml_statistics(msg: Prover.Protocol_Output): Boolean = synchronized {
72112
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
    95
      msg.properties match {
72119
d115d50a19c0 provide POLYSTATSDIR to keep $HOME/.polyml clean (requires Poly/ML 52881757b127, otherwise ignored);
wenzelm
parents: 72117
diff changeset
    96
        case Markup.ML_Statistics(pid, stats_dir) =>
d115d50a19c0 provide POLYSTATSDIR to keep $HOME/.polyml clean (requires Poly/ML 52881757b127, otherwise ignored);
wenzelm
parents: 72117
diff changeset
    97
          monitoring =
d115d50a19c0 provide POLYSTATSDIR to keep $HOME/.polyml clean (requires Poly/ML 52881757b127, otherwise ignored);
wenzelm
parents: 72117
diff changeset
    98
            Future.thread("ML_statistics") {
d115d50a19c0 provide POLYSTATSDIR to keep $HOME/.polyml clean (requires Poly/ML 52881757b127, otherwise ignored);
wenzelm
parents: 72117
diff changeset
    99
              monitor(pid, stats_dir = stats_dir, consume = consume)
d115d50a19c0 provide POLYSTATSDIR to keep $HOME/.polyml clean (requires Poly/ML 52881757b127, otherwise ignored);
wenzelm
parents: 72117
diff changeset
   100
            }
72112
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
   101
          true
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
   102
        case _ => false
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
   103
      }
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
   104
    }
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
   105
75440
39011d0d2128 tuned signature;
wenzelm
parents: 75393
diff changeset
   106
    override val functions: Session.Protocol_Functions =
39011d0d2128 tuned signature;
wenzelm
parents: 75393
diff changeset
   107
      List(Markup.ML_Statistics.name -> ml_statistics)
72112
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
   108
  }
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
   109
3546dd4ade74 ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents: 72044
diff changeset
   110
77121
ceee2a01322e clarified signature: more explicit types;
wenzelm
parents: 77119
diff changeset
   111
  /* memory fields */
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
   112
77118
f07d6bcbefa8 clarified signature: more robust field_scale;
wenzelm
parents: 77117
diff changeset
   113
  val scale_MiB: Double = 1.0 / 1024 / 1024
69832
b614e3e4146a more memory fields;
wenzelm
parents: 69822
diff changeset
   114
b614e3e4146a more memory fields;
wenzelm
parents: 69822
diff changeset
   115
  val CODE_SIZE = "size_code"
b614e3e4146a more memory fields;
wenzelm
parents: 69822
diff changeset
   116
  val STACK_SIZE = "size_stacks"
65858
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   117
  val HEAP_SIZE = "size_heap"
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   118
65866
00e8b836d4db uniform heap_scale;
wenzelm
parents: 65864
diff changeset
   119
00e8b836d4db uniform heap_scale;
wenzelm
parents: 65864
diff changeset
   120
  /* standard fields */
00e8b836d4db uniform heap_scale;
wenzelm
parents: 65864
diff changeset
   121
77118
f07d6bcbefa8 clarified signature: more robust field_scale;
wenzelm
parents: 77117
diff changeset
   122
  sealed case class Fields(title: String, names: List[String], scale: Double = 1.0)
65051
f094e27e4902 tuned signature;
wenzelm
parents: 64045
diff changeset
   123
f094e27e4902 tuned signature;
wenzelm
parents: 64045
diff changeset
   124
  val tasks_fields: Fields =
77117
9a22256b0a27 clarified signature: more explicit types;
wenzelm
parents: 77115
diff changeset
   125
    Fields("Future tasks",
68129
b73678836f8e record total number of tasks;
wenzelm
parents: 67760
diff changeset
   126
      List("tasks_ready", "tasks_pending", "tasks_running", "tasks_passive",
b73678836f8e record total number of tasks;
wenzelm
parents: 67760
diff changeset
   127
        "tasks_urgent", "tasks_total"))
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
   128
65051
f094e27e4902 tuned signature;
wenzelm
parents: 64045
diff changeset
   129
  val workers_fields: Fields =
77117
9a22256b0a27 clarified signature: more explicit types;
wenzelm
parents: 77115
diff changeset
   130
    Fields("Worker threads", List("workers_total", "workers_active", "workers_waiting"))
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
   131
65051
f094e27e4902 tuned signature;
wenzelm
parents: 64045
diff changeset
   132
  val GC_fields: Fields =
77117
9a22256b0a27 clarified signature: more explicit types;
wenzelm
parents: 77115
diff changeset
   133
    Fields("GCs", List("partial_GCs", "full_GCs", "share_passes"))
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
   134
65051
f094e27e4902 tuned signature;
wenzelm
parents: 64045
diff changeset
   135
  val heap_fields: Fields =
77117
9a22256b0a27 clarified signature: more explicit types;
wenzelm
parents: 77115
diff changeset
   136
    Fields("Heap", List(HEAP_SIZE, "size_allocation", "size_allocation_free",
77118
f07d6bcbefa8 clarified signature: more robust field_scale;
wenzelm
parents: 77117
diff changeset
   137
      "size_heap_free_last_full_GC", "size_heap_free_last_GC"), scale = scale_MiB)
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
   138
69822
8c587dd44f51 updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents: 68129
diff changeset
   139
  val program_fields: Fields =
77118
f07d6bcbefa8 clarified signature: more robust field_scale;
wenzelm
parents: 77117
diff changeset
   140
    Fields("Program", List("size_code", "size_stacks"), scale = scale_MiB)
69822
8c587dd44f51 updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents: 68129
diff changeset
   141
65051
f094e27e4902 tuned signature;
wenzelm
parents: 64045
diff changeset
   142
  val threads_fields: Fields =
77117
9a22256b0a27 clarified signature: more explicit types;
wenzelm
parents: 77115
diff changeset
   143
    Fields("Threads", List("threads_total", "threads_in_ML", "threads_wait_condvar",
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
   144
      "threads_wait_IO", "threads_wait_mutex", "threads_wait_signal"))
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
   145
65051
f094e27e4902 tuned signature;
wenzelm
parents: 64045
diff changeset
   146
  val time_fields: Fields =
77117
9a22256b0a27 clarified signature: more explicit types;
wenzelm
parents: 77115
diff changeset
   147
    Fields("Time", List("time_elapsed", "time_elapsed_GC", "time_CPU", "time_GC"))
51432
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
   148
65051
f094e27e4902 tuned signature;
wenzelm
parents: 64045
diff changeset
   149
  val speed_fields: Fields =
77117
9a22256b0a27 clarified signature: more explicit types;
wenzelm
parents: 77115
diff changeset
   150
    Fields("Speed", List("speed_CPU", "speed_GC"))
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
   151
69822
8c587dd44f51 updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents: 68129
diff changeset
   152
  private val time_speed = Map("time_CPU" -> "speed_CPU", "time_GC" -> "speed_GC")
8c587dd44f51 updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents: 68129
diff changeset
   153
72149
36a34f3a8cb8 support JVM runtime statistics;
wenzelm
parents: 72143
diff changeset
   154
  val java_heap_fields: Fields =
77117
9a22256b0a27 clarified signature: more explicit types;
wenzelm
parents: 77115
diff changeset
   155
    Fields("Java heap", List("java_heap_size", "java_heap_used"))
72149
36a34f3a8cb8 support JVM runtime statistics;
wenzelm
parents: 72143
diff changeset
   156
36a34f3a8cb8 support JVM runtime statistics;
wenzelm
parents: 72143
diff changeset
   157
  val java_thread_fields: Fields =
77117
9a22256b0a27 clarified signature: more explicit types;
wenzelm
parents: 77115
diff changeset
   158
    Fields("Java threads", List("java_threads_total", "java_workers_total", "java_workers_active"))
72149
36a34f3a8cb8 support JVM runtime statistics;
wenzelm
parents: 72143
diff changeset
   159
65053
460f0fd2f77a clarified defaults;
wenzelm
parents: 65051
diff changeset
   160
72143
4a46650bf701 clarified order for GUI;
wenzelm
parents: 72138
diff changeset
   161
  val main_fields: List[Fields] =
4a46650bf701 clarified order for GUI;
wenzelm
parents: 72138
diff changeset
   162
    List(heap_fields, tasks_fields, workers_fields)
4a46650bf701 clarified order for GUI;
wenzelm
parents: 72138
diff changeset
   163
72149
36a34f3a8cb8 support JVM runtime statistics;
wenzelm
parents: 72143
diff changeset
   164
  val other_fields: List[Fields] =
36a34f3a8cb8 support JVM runtime statistics;
wenzelm
parents: 72143
diff changeset
   165
    List(threads_fields, GC_fields, program_fields, time_fields, speed_fields,
36a34f3a8cb8 support JVM runtime statistics;
wenzelm
parents: 72143
diff changeset
   166
      java_heap_fields, java_thread_fields)
36a34f3a8cb8 support JVM runtime statistics;
wenzelm
parents: 72143
diff changeset
   167
36a34f3a8cb8 support JVM runtime statistics;
wenzelm
parents: 72143
diff changeset
   168
  val all_fields: List[Fields] = main_fields ::: other_fields
65855
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   169
77118
f07d6bcbefa8 clarified signature: more robust field_scale;
wenzelm
parents: 77117
diff changeset
   170
  def field_scale(x: String, y: Double): Double =
f07d6bcbefa8 clarified signature: more robust field_scale;
wenzelm
parents: 77117
diff changeset
   171
    all_fields.collectFirst({ case fields if fields.names.contains(x) => y * fields.scale })
f07d6bcbefa8 clarified signature: more robust field_scale;
wenzelm
parents: 77117
diff changeset
   172
      .getOrElse(y)
f07d6bcbefa8 clarified signature: more robust field_scale;
wenzelm
parents: 77117
diff changeset
   173
65855
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   174
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   175
  /* content interpretation */
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   176
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
   177
  final case class Entry(time: Double, data: Map[String, Double]) {
65858
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   178
    def get(field: String): Double = data.getOrElse(field, 0.0)
65855
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   179
  }
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   180
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   181
  val empty: ML_Statistics = apply(Nil)
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   182
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
   183
  def apply(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
   184
    ml_statistics0: List[Properties.T],
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
   185
    heading: String = "",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
   186
    domain: String => Boolean = _ => true
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
   187
  ): ML_Statistics = {
73120
c3589f2dff31 more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents: 73031
diff changeset
   188
    require(ml_statistics0.forall(props => Now.unapply(props).isDefined), "missing \"now\" field")
65855
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   189
72224
d36c109bc773 more robust interpretation of data;
wenzelm
parents: 72213
diff changeset
   190
    val ml_statistics = ml_statistics0.sortBy(now)
65855
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   191
    val time_start = if (ml_statistics.isEmpty) 0.0 else now(ml_statistics.head)
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   192
    val duration = if (ml_statistics.isEmpty) 0.0 else now(ml_statistics.last) - time_start
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   193
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   194
    val fields =
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   195
      SortedSet.empty[String] ++
67760
553d9ad7d679 more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents: 65866
diff changeset
   196
        (for {
553d9ad7d679 more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents: 65866
diff changeset
   197
          props <- ml_statistics.iterator
553d9ad7d679 more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents: 65866
diff changeset
   198
          (x, _) <- props.iterator
553d9ad7d679 more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents: 65866
diff changeset
   199
          if x != Now.name && domain(x) } yield x)
65855
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   200
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
   201
    val content = {
65855
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   202
      var last_edge = Map.empty[String, (Double, Double, Double)]
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   203
      val result = new mutable.ListBuffer[ML_Statistics.Entry]
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   204
      for (props <- ml_statistics) {
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   205
        val time = now(props) - time_start
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   206
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   207
        // rising edges -- relative speed
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   208
        val speeds =
67760
553d9ad7d679 more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents: 65866
diff changeset
   209
          (for {
553d9ad7d679 more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents: 65866
diff changeset
   210
            (key, value) <- props.iterator
69822
8c587dd44f51 updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents: 68129
diff changeset
   211
            key1 <- time_speed.get(key)
8c587dd44f51 updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents: 68129
diff changeset
   212
            if domain(key1)
8c587dd44f51 updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents: 68129
diff changeset
   213
          } yield {
8c587dd44f51 updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents: 68129
diff changeset
   214
            val (x0, y0, s0) = last_edge.getOrElse(key, (0.0, 0.0, 0.0))
65855
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   215
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   216
            val x1 = time
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   217
            val y1 = java.lang.Double.parseDouble(value)
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   218
            val s1 = if (x1 == x0) 0.0 else (y1 - y0) / (x1 - x0)
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   219
67760
553d9ad7d679 more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents: 65866
diff changeset
   220
            if (y1 > y0) {
69822
8c587dd44f51 updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents: 68129
diff changeset
   221
              last_edge += (key -> (x1, y1, s1))
8c587dd44f51 updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents: 68129
diff changeset
   222
              (key1, s1.toString)
67760
553d9ad7d679 more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents: 65866
diff changeset
   223
            }
69822
8c587dd44f51 updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents: 68129
diff changeset
   224
            else (key1, s0.toString)
67760
553d9ad7d679 more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents: 65866
diff changeset
   225
          }).toList
65855
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   226
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   227
        val data =
67760
553d9ad7d679 more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents: 65866
diff changeset
   228
          SortedMap.empty[String, Double] ++
553d9ad7d679 more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents: 65866
diff changeset
   229
            (for {
553d9ad7d679 more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents: 65866
diff changeset
   230
              (x, y) <- props.iterator ++ speeds.iterator
553d9ad7d679 more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents: 65866
diff changeset
   231
              if x != Now.name && domain(x)
553d9ad7d679 more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents: 65866
diff changeset
   232
              z = java.lang.Double.parseDouble(y) if z != 0.0
77121
ceee2a01322e clarified signature: more explicit types;
wenzelm
parents: 77119
diff changeset
   233
            } yield { (x.intern, z) })
65866
00e8b836d4db uniform heap_scale;
wenzelm
parents: 65864
diff changeset
   234
65855
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   235
        result += ML_Statistics.Entry(time, data)
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   236
      }
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   237
      result.toList
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   238
    }
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   239
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   240
    new ML_Statistics(heading, fields, content, time_start, duration)
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   241
  }
50685
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
   242
}
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
   243
65855
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   244
final class ML_Statistics private(
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   245
  val heading: String,
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   246
  val fields: Set[String],
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   247
  val content: List[ML_Statistics.Entry],
33b3e76114f8 store processed content instead of somewhat bulky properties;
wenzelm
parents: 65854
diff changeset
   248
  val time_start: Double,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
   249
  val duration: Double
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
   250
) {
74853
7420a7ac1a4c tuned output;
wenzelm
parents: 74852
diff changeset
   251
  override def toString: String =
7420a7ac1a4c tuned output;
wenzelm
parents: 74852
diff changeset
   252
    if (content.isEmpty) "ML_Statistics.empty"
7420a7ac1a4c tuned output;
wenzelm
parents: 74852
diff changeset
   253
    else "ML_Statistics(length = " + content.length + ", fields = " + fields.size + ")"
7420a7ac1a4c tuned output;
wenzelm
parents: 74852
diff changeset
   254
7420a7ac1a4c tuned output;
wenzelm
parents: 74852
diff changeset
   255
65858
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   256
  /* content */
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   257
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   258
  def maximum(field: String): Double =
73359
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   259
    content.foldLeft(0.0) { case (m, e) => m max e.get(field) }
65858
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   260
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
   261
  def average(field: String): Double = {
65858
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   262
    @tailrec def sum(t0: Double, list: List[ML_Statistics.Entry], acc: Double): Double =
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   263
      list match {
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   264
        case Nil => acc
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   265
        case e :: es =>
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   266
          val t = e.time
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   267
          sum(t, es, (t - t0) * e.get(field) + acc)
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   268
      }
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   269
    content match {
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   270
      case Nil => 0.0
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   271
      case List(e) => e.get(field)
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   272
      case e :: es => sum(e.time, es, 0.0) / duration
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   273
    }
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   274
  }
9418a9fad835 more systematic maximum and average;
wenzelm
parents: 65855
diff changeset
   275
50689
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   276
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   277
  /* charts */
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   278
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
   279
  def update_data(data: XYSeriesCollection, selected_fields: List[String]): Unit = {
74852
wenzelm
parents: 73716
diff changeset
   280
    data.removeAllSeries()
69832
b614e3e4146a more memory fields;
wenzelm
parents: 69822
diff changeset
   281
    for (field <- selected_fields) {
b614e3e4146a more memory fields;
wenzelm
parents: 69822
diff changeset
   282
      val series = new XYSeries(field)
77121
ceee2a01322e clarified signature: more explicit types;
wenzelm
parents: 77119
diff changeset
   283
      content.foreach(e => series.add(e.time, ML_Statistics.field_scale(field, e.get(field))))
50689
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   284
      data.addSeries(series)
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   285
    }
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   286
  }
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   287
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75380
diff changeset
   288
  def chart(title: String, selected_fields: List[String]): JFreeChart = {
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   289
    val data = new XYSeriesCollection
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   290
    update_data(data, selected_fields)
50689
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   291
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   292
    ChartFactory.createXYLineChart(title, "time", "value", data,
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   293
      PlotOrientation.VERTICAL, true, true, true)
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   294
  }
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   295
65051
f094e27e4902 tuned signature;
wenzelm
parents: 64045
diff changeset
   296
  def chart(fields: ML_Statistics.Fields): JFreeChart =
77117
9a22256b0a27 clarified signature: more explicit types;
wenzelm
parents: 77115
diff changeset
   297
    chart(fields.title, fields.names)
50691
20beafe66748 added standard_frames convenience;
wenzelm
parents: 50690
diff changeset
   298
65053
460f0fd2f77a clarified defaults;
wenzelm
parents: 65051
diff changeset
   299
  def show_frames(fields: List[ML_Statistics.Fields] = ML_Statistics.main_fields): Unit =
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 69832
diff changeset
   300
    fields.map(chart).foreach(c =>
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 53189
diff changeset
   301
      GUI_Thread.later {
50854
2b15227b17e8 add icon for toplevel windows;
wenzelm
parents: 50777
diff changeset
   302
        new Frame {
51615
072a7249e1ac separate module "GUI", to keep this out of the way of generic Isabelle_System operations, notably for non-Isabelle/jEdit applications;
wenzelm
parents: 51432
diff changeset
   303
          iconImage = GUI.isabelle_image()
65851
c103358a5559 tuned signature;
wenzelm
parents: 65477
diff changeset
   304
          title = heading
50854
2b15227b17e8 add icon for toplevel windows;
wenzelm
parents: 50777
diff changeset
   305
          contents = Component.wrap(new ChartPanel(c))
2b15227b17e8 add icon for toplevel windows;
wenzelm
parents: 50777
diff changeset
   306
          visible = true
2b15227b17e8 add icon for toplevel windows;
wenzelm
parents: 50777
diff changeset
   307
        }
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   308
      })
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
   309
}