src/Pure/Tools/ml_statistics.scala
author wenzelm
Fri, 17 Apr 2015 22:15:35 +0200
changeset 60125 2944cc4f4f56
parent 57869 9665f79a7181
child 60610 f52b4b0c10c4
permissions -rw-r--r--
tuned spelling;
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 =
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    37
    ("Future tasks", List("tasks_ready", "tasks_pending", "tasks_running", "tasks_passive"))
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    38
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    39
  val workers_fields =
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    40
    ("Worker threads", List("workers_total", "workers_active", "workers_waiting"))
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    41
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    42
  val GC_fields = ("GCs", List("partial_GCs", "full_GCs"))
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    43
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    44
  val heap_fields =
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    45
    ("Heap", List("size_heap", "size_allocation", "size_allocation_free",
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    46
      "size_heap_free_last_full_GC", "size_heap_free_last_GC"))
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    47
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    48
  val threads_fields =
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    49
    ("Threads", List("threads_total", "threads_in_ML", "threads_wait_condvar",
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    50
      "threads_wait_IO", "threads_wait_mutex", "threads_wait_signal"))
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    51
51432
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    52
  val time_fields = ("Time", List("time_CPU", "time_GC"))
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    53
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    54
  val speed_fields = ("Speed", List("speed_CPU", "speed_GC"))
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    55
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    56
  val standard_fields =
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    57
    List(tasks_fields, workers_fields, GC_fields, heap_fields, threads_fields,
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    58
      time_fields, speed_fields)
50685
293e8ec4dfc8 ML runtime statistics: read properties from build log;
wenzelm
parents:
diff changeset
    59
}
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    60
50982
a7aa17a1f721 use inlined session name as title for charts;
wenzelm
parents: 50981
diff changeset
    61
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
    62
{
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    63
  val Now = new Properties.Double("now")
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
    64
  def now(props: Properties.T): Double = Now.unapply(props).get
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    65
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
    66
  require(stats.forall(props => Now.unapply(props).isDefined))
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
    67
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
    68
  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
    69
  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
    70
50689
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
    71
  val fields: Set[String] =
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    72
    SortedSet.empty[String] ++
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    73
      (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
    74
        yield x)
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    75
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    76
  val content: List[ML_Statistics.Entry] =
51432
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    77
  {
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    78
    var last_edge = Map.empty[String, (Double, Double, Double)]
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    79
    val result = new mutable.ListBuffer[ML_Statistics.Entry]
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    80
    for (props <- stats) {
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
    81
      val time = now(props) - time_start
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    82
      require(time >= 0.0)
51432
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    83
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    84
      // rising edges -- relative speed
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    85
      val speeds =
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    86
        for ((key, value) <- props; a <- Library.try_unprefix("time", key)) yield {
52888
wenzelm
parents: 51615
diff changeset
    87
          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
    88
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    89
          val x1 = time
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    90
          val y1 = java.lang.Double.parseDouble(value)
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    91
          val s1 = if (x1 == x0) 0.0 else (y1 - y0) / (x1 - x0)
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    92
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    93
          val b = ("speed" + a).intern
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    94
          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
    95
        }
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    96
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
    97
      val data =
51432
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
    98
        SortedMap.empty[String, Double] ++ speeds ++
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
    99
          (for ((x, y) <- props.iterator if x != Now.name)
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
   100
            yield (x, java.lang.Double.parseDouble(y)))
51432
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
   101
      result += ML_Statistics.Entry(time, data)
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
   102
    }
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
   103
    result.toList
903be59d9665 simplified time_CPU and time_GC;
wenzelm
parents: 51240
diff changeset
   104
  }
50689
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   105
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   106
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   107
  /* charts */
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   108
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   109
  def update_data(data: XYSeriesCollection, selected_fields: Iterable[String])
50689
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   110
  {
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   111
    data.removeAllSeries
50689
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   112
    for {
50690
03c4d75e8e32 some grouping of standard fields;
wenzelm
parents: 50689
diff changeset
   113
      field <- selected_fields.iterator
50689
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   114
      series = new XYSeries(field)
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   115
    } {
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   116
      content.foreach(entry => series.add(entry.time, entry.data(field)))
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   117
      data.addSeries(series)
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   118
    }
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   119
  }
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
  def chart(title: String, selected_fields: Iterable[String]): JFreeChart =
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   122
  {
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   123
    val data = new XYSeriesCollection
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   124
    update_data(data, selected_fields)
50689
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   125
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   126
    ChartFactory.createXYLineChart(title, "time", "value", data,
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   127
      PlotOrientation.VERTICAL, true, true, true)
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   128
  }
0607d557d073 some support for chart drawing;
wenzelm
parents: 50688
diff changeset
   129
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   130
  def chart(arg: (String, Iterable[String])): JFreeChart = chart(arg._1, arg._2)
50691
20beafe66748 added standard_frames convenience;
wenzelm
parents: 50690
diff changeset
   131
50981
1791a90a94fb tuned signature;
wenzelm
parents: 50974
diff changeset
   132
  def show_standard_frames(): Unit =
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   133
    ML_Statistics.standard_fields.map(chart(_)).foreach(c =>
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 53189
diff changeset
   134
      GUI_Thread.later {
50854
2b15227b17e8 add icon for toplevel windows;
wenzelm
parents: 50777
diff changeset
   135
        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
   136
          iconImage = GUI.isabelle_image()
50982
a7aa17a1f721 use inlined session name as title for charts;
wenzelm
parents: 50981
diff changeset
   137
          title = name
50854
2b15227b17e8 add icon for toplevel windows;
wenzelm
parents: 50777
diff changeset
   138
          contents = Component.wrap(new ChartPanel(c))
2b15227b17e8 add icon for toplevel windows;
wenzelm
parents: 50777
diff changeset
   139
          visible = true
2b15227b17e8 add icon for toplevel windows;
wenzelm
parents: 50777
diff changeset
   140
        }
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50691
diff changeset
   141
      })
50688
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
   142
}
f02864682307 some support for ML statistics content interpretation;
wenzelm
parents: 50685
diff changeset
   143