src/Tools/jEdit/src/monitor_dockable.scala
author wenzelm
Wed, 09 Nov 2022 19:42:21 +0100
changeset 76492 e228be7cd375
parent 75853 f981111768ec
child 76577 c662a56e77a8
permissions -rw-r--r--
clarified GUI.Selector, with support for separator as pseudo-entry; 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
76492
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75853
diff changeset
    67
  private val select_data =
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75853
diff changeset
    68
    new GUI.Selector(ML_Statistics.all_fields.map { case (a, _) => GUI.Selector.item(a) }) {
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75853
diff changeset
    69
      tooltip = "Select visualized data collection"
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75853
diff changeset
    70
      override def changed(): Unit = { data_name = selection.item.toString; update_chart() }
e228be7cd375 clarified GUI.Selector, with support for separator as pseudo-entry;
wenzelm
parents: 75853
diff changeset
    71
    }
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    72
72145
25db9c4209ee tuned GUI;
wenzelm
parents: 72135
diff changeset
    73
  private val limit_data = new TextField("200", 5) {
25db9c4209ee tuned GUI;
wenzelm
parents: 72135
diff changeset
    74
    tooltip = "Limit for accumulated data"
72147
2375b38a42f8 misc tuning, based on hints by IntelliJ IDEA;
wenzelm
parents: 72146
diff changeset
    75
    verifier = {
2375b38a42f8 misc tuning, based on hints by IntelliJ IDEA;
wenzelm
parents: 72146
diff changeset
    76
      case Value.Int(x) => x > 0
2375b38a42f8 misc tuning, based on hints by IntelliJ IDEA;
wenzelm
parents: 72146
diff changeset
    77
      case _ => false
2375b38a42f8 misc tuning, based on hints by IntelliJ IDEA;
wenzelm
parents: 72146
diff changeset
    78
    }
72145
25db9c4209ee tuned GUI;
wenzelm
parents: 72135
diff changeset
    79
    reactions += { case ValueChanged(_) => input_delay.invoke() }
25db9c4209ee tuned GUI;
wenzelm
parents: 72135
diff changeset
    80
  }
25db9c4209ee tuned GUI;
wenzelm
parents: 72135
diff changeset
    81
75853
f981111768ec clarified signature;
wenzelm
parents: 75839
diff changeset
    82
  private val reset_data = new GUI.Button("Reset") {
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    83
    tooltip = "Reset accumulated data"
75853
f981111768ec clarified signature;
wenzelm
parents: 75839
diff changeset
    84
    override def clicked(): Unit = { clear_statistics(); update_chart() }
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    85
  }
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    86
75853
f981111768ec clarified signature;
wenzelm
parents: 75839
diff changeset
    87
  private val full_gc = new GUI.Button("GC") {
72146
d8dd3aa6dae9 clarified GUI;
wenzelm
parents: 72145
diff changeset
    88
    tooltip = "Full garbage collection of ML heap"
75853
f981111768ec clarified signature;
wenzelm
parents: 75839
diff changeset
    89
    override def clicked(): Unit = PIDE.session.protocol_command("ML_Heap.full_gc")
72146
d8dd3aa6dae9 clarified GUI;
wenzelm
parents: 72145
diff changeset
    90
  }
d8dd3aa6dae9 clarified GUI;
wenzelm
parents: 72145
diff changeset
    91
75853
f981111768ec clarified signature;
wenzelm
parents: 75839
diff changeset
    92
  private val share_common_data = new GUI.Button("Sharing") {
72146
d8dd3aa6dae9 clarified GUI;
wenzelm
parents: 72145
diff changeset
    93
    tooltip = "Share common data of ML heap"
75853
f981111768ec clarified signature;
wenzelm
parents: 75839
diff changeset
    94
    override def clicked(): Unit = PIDE.session.protocol_command("ML_Heap.share_common_data")
72146
d8dd3aa6dae9 clarified GUI;
wenzelm
parents: 72145
diff changeset
    95
  }
d8dd3aa6dae9 clarified GUI;
wenzelm
parents: 72145
diff changeset
    96
d8dd3aa6dae9 clarified GUI;
wenzelm
parents: 72145
diff changeset
    97
  private val controls =
d8dd3aa6dae9 clarified GUI;
wenzelm
parents: 72145
diff changeset
    98
    Wrap_Panel(List(select_data, limit_data, reset_data, full_gc, share_common_data))
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
    99
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
   100
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
   101
  /* layout */
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
   102
50697
82e9178e6a98 improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents: 50433
diff changeset
   103
  set_content(new ChartPanel(chart))
57869
9665f79a7181 improved monitor panel;
wenzelm
parents: 57612
diff changeset
   104
  add(controls.peer, BorderLayout.NORTH)
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
   105
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
   106
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 55618
diff changeset
   107
  /* main */
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
   108
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 55618
diff changeset
   109
  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
   110
    Session.Consumer[Session.Runtime_Statistics](getClass.getName) {
72147
2375b38a42f8 misc tuning, based on hints by IntelliJ IDEA;
wenzelm
parents: 72146
diff changeset
   111
      stats =>
61724
4bfcc09a33e8 limit statistics, to avoid exhaustion of heap space or GUI time;
wenzelm
parents: 61590
diff changeset
   112
        add_statistics(stats.props)
61751
aa7b748bd96c more reactive GUI;
wenzelm
parents: 61724
diff changeset
   113
        update_delay.invoke()
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 55618
diff changeset
   114
    }
53177
dcac8d837b9c more uniform treatment of Swing_Thread context switch: prefer asynchronous Swing_Thread.later from actor;
wenzelm
parents: 50982
diff changeset
   115
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   116
  override def init(): Unit = {
71652
721f143a679b clarified signature;
wenzelm
parents: 66591
diff changeset
   117
    PIDE.session.runtime_statistics += main
60074
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   118
  }
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   119
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   120
  override def exit(): Unit = {
71652
721f143a679b clarified signature;
wenzelm
parents: 66591
diff changeset
   121
    PIDE.session.runtime_statistics -= main
60074
38a64cc17403 GUI controls for ML_statistics, for more digestible protocol dump;
wenzelm
parents: 57869
diff changeset
   122
  }
50433
9131dadb2bf7 basic monitor panel, using the powerful jfreechart library;
wenzelm
parents:
diff changeset
   123
}