src/Pure/Tools/ml_statistics.scala
author wenzelm
Sun, 04 Sep 2016 15:44:20 +0200
changeset 63778 e06e899b78d0
parent 60610 f52b4b0c10c4
child 64041 fd454d9e97c4
permissions -rw-r--r--
clarified modules; tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51240
a7a04b449e8b updated headers;
wenzelm
parents: 50982
diff changeset
     1
/*  Title:      Pure/Tools/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
51432
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    10
import scala.collection.mutable
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    11
import scala.collection.immutable.{SortedSet, SortedMap}
50691
20beafe66748 added standard_frames convenience;
wenzelm
parents: 50690
diff changeset
    12
import scala.swing.{Frame, Component}
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    13
50689
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
    14
import org.jfree.data.xy.{XYSeries, XYSeriesCollection}
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
    15
import org.jfree.chart.{JFreeChart, ChartPanel, ChartFactory}
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
    16
import org.jfree.chart.plot.PlotOrientation
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
    17
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    18
50685
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    19
object ML_Statistics
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    20
{
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    21
  /* content interpretation */
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    22
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    23
  final case class Entry(time: Double, data: Map[String, Double])
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    24
50982
a7aa17a1f721 use inlined session name as title for charts;
wenzelm
parents: 50981
diff changeset
    25
  def apply(name: String, stats: List[Properties.T]): ML_Statistics =
a7aa17a1f721 use inlined session name as title for charts;
wenzelm
parents: 50981
diff changeset
    26
    new ML_Statistics(name, stats)
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    27
50982
a7aa17a1f721 use inlined session name as title for charts;
wenzelm
parents: 50981
diff changeset
    28
  def apply(info: Build.Log_Info): ML_Statistics =
a7aa17a1f721 use inlined session name as title for charts;
wenzelm
parents: 50981
diff changeset
    29
    apply(info.name, info.stats)
a7aa17a1f721 use inlined session name as title for charts;
wenzelm
parents: 50981
diff changeset
    30
a7aa17a1f721 use inlined session name as title for charts;
wenzelm
parents: 50981
diff changeset
    31
  val empty = apply("", Nil)
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
    32
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    33
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    34
  /* standard fields */
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    35
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    36
  val tasks_fields =
60610
f52b4b0c10c4 improved scheduling for urgent tasks, using farm of replacement threads (may lead to factor 2 overloading, but CPUs are usually hyperthreaded);
wenzelm
parents: 57869
diff changeset
    37
    ("Future tasks",
f52b4b0c10c4 improved scheduling for urgent tasks, using farm of replacement threads (may lead to factor 2 overloading, but CPUs are usually hyperthreaded);
wenzelm
parents: 57869
diff changeset
    38
      List("tasks_ready", "tasks_pending", "tasks_running", "tasks_passive", "tasks_urgent"))
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    39
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    40
  val workers_fields =
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    41
    ("Worker threads", List("workers_total", "workers_active", "workers_waiting"))
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    42
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    43
  val GC_fields = ("GCs", List("partial_GCs", "full_GCs"))
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    44
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    45
  val heap_fields =
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    46
    ("Heap", List("size_heap", "size_allocation", "size_allocation_free",
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    47
      "size_heap_free_last_full_GC", "size_heap_free_last_GC"))
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    48
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    49
  val threads_fields =
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    50
    ("Threads", List("threads_total", "threads_in_ML", "threads_wait_condvar",
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    51
      "threads_wait_IO", "threads_wait_mutex", "threads_wait_signal"))
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    52
51432
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    53
  val time_fields = ("Time", List("time_CPU", "time_GC"))
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    54
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    55
  val speed_fields = ("Speed", List("speed_CPU", "speed_GC"))
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    56
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    57
  val standard_fields =
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    58
    List(tasks_fields, workers_fields, GC_fields, heap_fields, threads_fields,
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    59
      time_fields, speed_fields)
50685
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    60
}
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    61
50982
a7aa17a1f721 use inlined session name as title for charts;
wenzelm
parents: 50981
diff changeset
    62
final class ML_Statistics private(val name: String, val stats: List[Properties.T])
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    63
{
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    64
  val Now = new Properties.Double("now")
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
    65
  def now(props: Properties.T): Double = Now.unapply(props).get
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    66
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
    67
  require(stats.forall(props => Now.unapply(props).isDefined))
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
    68
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
    69
  val time_start = if (stats.isEmpty) 0.0 else now(stats.head)
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
    70
  val duration = if (stats.isEmpty) 0.0 else now(stats.last) - time_start
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    71
50689
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
    72
  val fields: Set[String] =
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    73
    SortedSet.empty[String] ++
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    74
      (for (props <- stats.iterator; (x, _) <- props.iterator if x != Now.name)
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    75
        yield x)
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    76
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    77
  val content: List[ML_Statistics.Entry] =
51432
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    78
  {
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    79
    var last_edge = Map.empty[String, (Double, Double, Double)]
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    80
    val result = new mutable.ListBuffer[ML_Statistics.Entry]
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    81
    for (props <- stats) {
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
    82
      val time = now(props) - time_start
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    83
      require(time >= 0.0)
51432
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    84
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    85
      // rising edges -- relative speed
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    86
      val speeds =
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    87
        for ((key, value) <- props; a <- Library.try_unprefix("time", key)) yield {
52888
wenzelm
parents: 51615
diff changeset
    88
          val (x0, y0, s0) = last_edge.getOrElse(a, (0.0, 0.0, 0.0))
51432
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    89
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    90
          val x1 = time
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    91
          val y1 = java.lang.Double.parseDouble(value)
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    92
          val s1 = if (x1 == x0) 0.0 else (y1 - y0) / (x1 - x0)
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    93
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    94
          val b = ("speed" + a).intern
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    95
          if (y1 > y0) { last_edge += (a -> (x1, y1, s1)); (b, s1) } else (b, s0)
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    96
        }
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    97
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    98
      val data =
51432
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    99
        SortedMap.empty[String, Double] ++ speeds ++
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
   100
          (for ((x, y) <- props.iterator if x != Now.name)
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
   101
            yield (x, java.lang.Double.parseDouble(y)))
51432
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
   102
      result += ML_Statistics.Entry(time, data)
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
   103
    }
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
   104
    result.toList
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
   105
  }
50689
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   106
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   107
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   108
  /* charts */
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   109
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   110
  def update_data(data: XYSeriesCollection, selected_fields: Iterable[String])
50689
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   111
  {
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   112
    data.removeAllSeries
50689
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   113
    for {
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
   114
      field <- selected_fields.iterator
50689
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   115
      series = new XYSeries(field)
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   116
    } {
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   117
      content.foreach(entry => series.add(entry.time, entry.data(field)))
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   118
      data.addSeries(series)
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   119
    }
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   120
  }
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   121
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   122
  def chart(title: String, selected_fields: Iterable[String]): JFreeChart =
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   123
  {
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   124
    val data = new XYSeriesCollection
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   125
    update_data(data, selected_fields)
50689
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   126
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   127
    ChartFactory.createXYLineChart(title, "time", "value", data,
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   128
      PlotOrientation.VERTICAL, true, true, true)
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   129
  }
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   130
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   131
  def chart(arg: (String, Iterable[String])): JFreeChart = chart(arg._1, arg._2)
50691
20beafe66748 added standard_frames convenience;
wenzelm
parents: 50690
diff changeset
   132
50981
1791a90a94fb tuned signature;
wenzelm
parents: 50974
diff changeset
   133
  def show_standard_frames(): Unit =
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   134
    ML_Statistics.standard_fields.map(chart(_)).foreach(c =>
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 53189
diff changeset
   135
      GUI_Thread.later {
50854
2b15227b17e8 add icon for toplevel windows;
wenzelm
parents: 50777
diff changeset
   136
        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
   137
          iconImage = GUI.isabelle_image()
50982
a7aa17a1f721 use inlined session name as title for charts;
wenzelm
parents: 50981
diff changeset
   138
          title = name
50854
2b15227b17e8 add icon for toplevel windows;
wenzelm
parents: 50777
diff changeset
   139
          contents = Component.wrap(new ChartPanel(c))
2b15227b17e8 add icon for toplevel windows;
wenzelm
parents: 50777
diff changeset
   140
          visible = true
2b15227b17e8 add icon for toplevel windows;
wenzelm
parents: 50777
diff changeset
   141
        }
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   142
      })
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
   143
}
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
   144