src/Pure/Tools/ml_statistics.scala
author wenzelm
Fri, 18 Jan 2013 22:38:34 +0100
changeset 51996 1791a90a94fb
parent 51989 src/Pure/ML/ml_statistics.scala@55f8bd61b029
child 51997 a7aa17a1f721
permissions -rw-r--r--
tuned signature;
     1 /*  Title:      Pure/ML/ml_statistics.ML
     2     Author:     Makarius
     3 
     4 ML runtime statistics.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import scala.collection.immutable.{SortedSet, SortedMap}
    11 import scala.swing.{Frame, Component}
    12 
    13 import org.jfree.data.xy.{XYSeries, XYSeriesCollection}
    14 import org.jfree.chart.{JFreeChart, ChartPanel, ChartFactory}
    15 import org.jfree.chart.plot.PlotOrientation
    16 
    17 
    18 object ML_Statistics
    19 {
    20   /* content interpretation */
    21 
    22   final case class Entry(time: Double, data: Map[String, Double])
    23 
    24   def apply(stats: List[Properties.T]): ML_Statistics = new ML_Statistics(stats)
    25   def apply(path: Path): ML_Statistics = apply(Build.parse_log(File.read_gzip(path)).stats)
    26 
    27   val empty = apply(Nil)
    28 
    29 
    30   /* standard fields */
    31 
    32   val GC_fields = ("GCs", List("partial_GCs", "full_GCs"))
    33 
    34   val heap_fields =
    35     ("Heap", List("size_heap", "size_allocation", "size_allocation_free",
    36       "size_heap_free_last_full_GC", "size_heap_free_last_GC"))
    37 
    38   val threads_fields =
    39     ("Threads", List("threads_total", "threads_in_ML", "threads_wait_condvar",
    40       "threads_wait_IO", "threads_wait_mutex", "threads_wait_signal"))
    41 
    42   val time_fields =
    43     ("Time", List("time_GC_system", "time_GC_user", "time_non_GC_system", "time_non_GC_user"))
    44 
    45   val tasks_fields =
    46     ("Future tasks",
    47       List("tasks_proof", "tasks_ready", "tasks_pending", "tasks_running", "tasks_passive"))
    48 
    49   val workers_fields =
    50     ("Worker threads", List("workers_total", "workers_active", "workers_waiting"))
    51 
    52   val standard_fields =
    53     List(GC_fields, heap_fields, threads_fields, time_fields, tasks_fields, workers_fields)
    54 }
    55 
    56 final class ML_Statistics private(val stats: List[Properties.T])
    57 {
    58   val Now = new Properties.Double("now")
    59   def now(props: Properties.T): Double = Now.unapply(props).get
    60 
    61   require(stats.forall(props => Now.unapply(props).isDefined))
    62 
    63   val time_start = if (stats.isEmpty) 0.0 else now(stats.head)
    64   val duration = if (stats.isEmpty) 0.0 else now(stats.last) - time_start
    65 
    66   val fields: Set[String] =
    67     SortedSet.empty[String] ++
    68       (for (props <- stats.iterator; (x, _) <- props.iterator if x != Now.name)
    69         yield x)
    70 
    71   val content: List[ML_Statistics.Entry] =
    72     stats.map(props => {
    73       val time = now(props) - time_start
    74       require(time >= 0.0)
    75       val data =
    76         SortedMap.empty[String, Double] ++
    77           (for ((x, y) <- props.iterator if x != Now.name)
    78             yield (x, java.lang.Double.parseDouble(y)))
    79       ML_Statistics.Entry(time, data)
    80     })
    81 
    82 
    83   /* charts */
    84 
    85   def update_data(data: XYSeriesCollection, selected_fields: Iterable[String])
    86   {
    87     data.removeAllSeries
    88     for {
    89       field <- selected_fields.iterator
    90       series = new XYSeries(field)
    91     } {
    92       content.foreach(entry => series.add(entry.time, entry.data(field)))
    93       data.addSeries(series)
    94     }
    95   }
    96 
    97   def chart(title: String, selected_fields: Iterable[String]): JFreeChart =
    98   {
    99     val data = new XYSeriesCollection
   100     update_data(data, selected_fields)
   101 
   102     ChartFactory.createXYLineChart(title, "time", "value", data,
   103       PlotOrientation.VERTICAL, true, true, true)
   104   }
   105 
   106   def chart(arg: (String, Iterable[String])): JFreeChart = chart(arg._1, arg._2)
   107 
   108   def show_standard_frames(): Unit =
   109     ML_Statistics.standard_fields.map(chart(_)).foreach(c =>
   110       Swing_Thread.later {
   111         new Frame {
   112           iconImage = Isabelle_System.get_icon().getImage
   113           title = "Statistics"
   114           contents = Component.wrap(new ChartPanel(c))
   115           visible = true
   116         }
   117       })
   118 }
   119