src/Tools/jEdit/src/monitor_dockable.scala
author wenzelm
Sat, 13 Aug 2022 23:04:53 +0200
changeset 75853 f981111768ec
parent 75839 29441f2bfe81
child 76492 e228be7cd375
permissions -rw-r--r--
clarified signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/monitor_dockable.scala
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
     3
72147
2375b38a42f8 misc tuning, based on hints by IntelliJ IDEA;
wenzelm
parents: 72146
diff changeset
     4
Monitor for runtime statistics.
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
     5
*/
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
     6
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
     8
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
     9
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    10
import isabelle._
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    11
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    12
import java.awt.BorderLayout
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    13
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    14
import scala.collection.immutable.Queue
75853
f981111768ec clarified signature;
wenzelm
parents: 75839
diff changeset
    15
import scala.swing.TextField
f981111768ec clarified signature;
wenzelm
parents: 75839
diff changeset
    16
import scala.swing.event.ValueChanged
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    17
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50433
diff changeset
    18
import org.jfree.chart.ChartPanel
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50433
diff changeset
    19
import org.jfree.data.xy.XYSeriesCollection
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    20
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    21
import org.gjt.sp.jedit.View
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    22
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    23
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    24
class Monitor_Dockable(view: View, position: String) extends Dockable(view, position) {
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    25
  /* chart data -- owned by GUI thread */
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    26
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    27
  private var statistics = Queue.empty[Properties.T]
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    28
  private var statistics_length = 0
56770
e160ae47db94 mane delayed events outside of Swing thread -- triggers no longer require Swing_Thread.later;
wenzelm
parents: 56715
diff changeset
    29
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    30
  private def add_statistics(stats: Properties.T): Unit = {
75447
d1417d9c6deb tuned signature: avoid problems with scala3;
wenzelm
parents: 75393
diff changeset
    31
    statistics = statistics.appended(stats)
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    32
    statistics_length += 1
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    33
    limit_data.text match {
63805
c272680df665 clarified modules;
wenzelm
parents: 61751
diff changeset
    34
      case Value.Int(limit) =>
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    35
        while (statistics_length > limit) {
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    36
          statistics = statistics.dequeue._2
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    37
          statistics_length -= 1
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    38
        }
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    39
      case _ =>
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    40
    }
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    41
  }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    42
  private def clear_statistics(): Unit = {
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    43
    statistics = Queue.empty
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    44
    statistics_length = 0
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
    45
  }
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    46
72147
2375b38a42f8 misc tuning, based on hints by IntelliJ IDEA;
wenzelm
parents: 72146
diff changeset
    47
  private var data_name = ML_Statistics.all_fields.head._1
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50433
diff changeset
    48
  private val chart = ML_Statistics.empty.chart(null, Nil)
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50433
diff changeset
    49
  private val data = chart.getXYPlot.getDataset.asInstanceOf[XYSeriesCollection]
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    50
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    51
  private def update_chart(): Unit = {
65053
460f0fd2f77a clarified defaults;
wenzelm
parents: 63805
diff changeset
    52
    ML_Statistics.all_fields.find(_._1 == data_name) match {
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    53
      case None =>
65851
c103358a5559 tuned signature;
wenzelm
parents: 65053
diff changeset
    54
      case Some((_, fields)) => ML_Statistics(statistics.toList).update_data(data, fields)
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50433
diff changeset
    55
    }
72147
2375b38a42f8 misc tuning, based on hints by IntelliJ IDEA;
wenzelm
parents: 72146
diff changeset
    56
  }
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
    57
61751
aa7b748bd96c more reactive GUI;
wenzelm
parents: 61724
diff changeset
    58
  private val input_delay =
72147
2375b38a42f8 misc tuning, based on hints by IntelliJ IDEA;
wenzelm
parents: 72146
diff changeset
    59
    Delay.first(PIDE.options.seconds("editor_input_delay"), gui = true) { update_chart() }
61751
aa7b748bd96c more reactive GUI;
wenzelm
parents: 61724
diff changeset
    60
aa7b748bd96c more reactive GUI;
wenzelm
parents: 61724
diff changeset
    61
  private val update_delay =
72147
2375b38a42f8 misc tuning, based on hints by IntelliJ IDEA;
wenzelm
parents: 72146
diff changeset
    62
    Delay.first(PIDE.options.seconds("editor_chart_delay"), gui = true) { update_chart() }
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    63
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    64
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    65
  /* controls */
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    66
75839
29441f2bfe81 clarified signature: more explicit types;
wenzelm
parents: 75838
diff changeset
    67
  private val select_data = new GUI.Selector[String](ML_Statistics.all_fields.map(_._1)) {
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    68
    tooltip = "Select visualized data collection"
75839
29441f2bfe81 clarified signature: more explicit types;
wenzelm
parents: 75838
diff changeset
    69
    override def changed(): Unit = { data_name = selection.item; update_chart() }
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    70
  }
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    71
72145
25db9c4209ee tuned GUI;
wenzelm
parents: 72135
diff changeset
    72
  private val limit_data = new TextField("200", 5) {
25db9c4209ee tuned GUI;
wenzelm
parents: 72135
diff changeset
    73
    tooltip = "Limit for accumulated data"
72147
2375b38a42f8 misc tuning, based on hints by IntelliJ IDEA;
wenzelm
parents: 72146
diff changeset
    74
    verifier = {
2375b38a42f8 misc tuning, based on hints by IntelliJ IDEA;
wenzelm
parents: 72146
diff changeset
    75
      case Value.Int(x) => x > 0
2375b38a42f8 misc tuning, based on hints by IntelliJ IDEA;
wenzelm
parents: 72146
diff changeset
    76
      case _ => false
2375b38a42f8 misc tuning, based on hints by IntelliJ IDEA;
wenzelm
parents: 72146
diff changeset
    77
    }
72145
25db9c4209ee tuned GUI;
wenzelm
parents: 72135
diff changeset
    78
    reactions += { case ValueChanged(_) => input_delay.invoke() }
25db9c4209ee tuned GUI;
wenzelm
parents: 72135
diff changeset
    79
  }
25db9c4209ee tuned GUI;
wenzelm
parents: 72135
diff changeset
    80
75853
f981111768ec clarified signature;
wenzelm
parents: 75839
diff changeset
    81
  private val reset_data = new GUI.Button("Reset") {
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    82
    tooltip = "Reset accumulated data"
75853
f981111768ec clarified signature;
wenzelm
parents: 75839
diff changeset
    83
    override def clicked(): Unit = { clear_statistics(); update_chart() }
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    84
  }
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    85
75853
f981111768ec clarified signature;
wenzelm
parents: 75839
diff changeset
    86
  private val full_gc = new GUI.Button("GC") {
72146
d8dd3aa6dae9 clarified GUI;
wenzelm
parents: 72145
diff changeset
    87
    tooltip = "Full garbage collection of ML heap"
75853
f981111768ec clarified signature;
wenzelm
parents: 75839
diff changeset
    88
    override def clicked(): Unit = PIDE.session.protocol_command("ML_Heap.full_gc")
72146
d8dd3aa6dae9 clarified GUI;
wenzelm
parents: 72145
diff changeset
    89
  }
d8dd3aa6dae9 clarified GUI;
wenzelm
parents: 72145
diff changeset
    90
75853
f981111768ec clarified signature;
wenzelm
parents: 75839
diff changeset
    91
  private val share_common_data = new GUI.Button("Sharing") {
72146
d8dd3aa6dae9 clarified GUI;
wenzelm
parents: 72145
diff changeset
    92
    tooltip = "Share common data of ML heap"
75853
f981111768ec clarified signature;
wenzelm
parents: 75839
diff changeset
    93
    override def clicked(): Unit = PIDE.session.protocol_command("ML_Heap.share_common_data")
72146
d8dd3aa6dae9 clarified GUI;
wenzelm
parents: 72145
diff changeset
    94
  }
d8dd3aa6dae9 clarified GUI;
wenzelm
parents: 72145
diff changeset
    95
d8dd3aa6dae9 clarified GUI;
wenzelm
parents: 72145
diff changeset
    96
  private val controls =
d8dd3aa6dae9 clarified GUI;
wenzelm
parents: 72145
diff changeset
    97
    Wrap_Panel(List(select_data, limit_data, reset_data, full_gc, share_common_data))
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    98
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    99
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
   100
  /* layout */
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
   101
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50433
diff changeset
   102
  set_content(new ChartPanel(chart))
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
   103
  add(controls.peer, BorderLayout.NORTH)
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
   104
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
   105
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 55618
diff changeset
   106
  /* main */
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
   107
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 55618
diff changeset
   108
  private val main =
72135
f67e83608745 removed pointless GUI controls for ML_statistics --- no longer part of prover protocol (see also 38a64cc17403);
wenzelm
parents: 71704
diff changeset
   109
    Session.Consumer[Session.Runtime_Statistics](getClass.getName) {
72147
2375b38a42f8 misc tuning, based on hints by IntelliJ IDEA;
wenzelm
parents: 72146
diff changeset
   110
      stats =>
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
   111
        add_statistics(stats.props)
61751
aa7b748bd96c more reactive GUI;
wenzelm
parents: 61724
diff changeset
   112
        update_delay.invoke()
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 55618
diff changeset
   113
    }
53177
dcac8d837b9c more uniform treatment of Swing_Thread context switch: prefer asynchronous Swing_Thread.later from actor;
wenzelm
parents: 50982
diff changeset
   114
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   115
  override def init(): Unit = {
71652
721f143a679b clarified signature;
wenzelm
parents: 66591
diff changeset
   116
    PIDE.session.runtime_statistics += main
60074
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   117
  }
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   118
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   119
  override def exit(): Unit = {
71652
721f143a679b clarified signature;
wenzelm
parents: 66591
diff changeset
   120
    PIDE.session.runtime_statistics -= main
60074
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   121
  }
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
   122
}