author | wenzelm |
Wed, 15 Jul 2020 12:04:48 +0200 | |
changeset 72035 | 25d5ef16401a |
parent 71601 | 97ccf48c2f0c |
child 72037 | aa6a36c730c9 |
permissions | -rw-r--r-- |
65477 | 1 |
/* Title: Pure/ML/ml_statistics.scala |
50685 | 2 |
Author: Makarius |
3 |
||
4 |
ML runtime statistics. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
65858 | 10 |
import scala.annotation.tailrec |
51432 | 11 |
import scala.collection.mutable |
50688
f02864682307
some support for ML statistics content interpretation;
wenzelm
parents:
50685
diff
changeset
|
12 |
import scala.collection.immutable.{SortedSet, SortedMap} |
50691 | 13 |
import scala.swing.{Frame, Component} |
50688
f02864682307
some support for ML statistics content interpretation;
wenzelm
parents:
50685
diff
changeset
|
14 |
|
50689 | 15 |
import org.jfree.data.xy.{XYSeries, XYSeriesCollection} |
16 |
import org.jfree.chart.{JFreeChart, ChartPanel, ChartFactory} |
|
17 |
import org.jfree.chart.plot.PlotOrientation |
|
18 |
||
50688
f02864682307
some support for ML statistics content interpretation;
wenzelm
parents:
50685
diff
changeset
|
19 |
|
50685 | 20 |
object ML_Statistics |
21 |
{ |
|
65855
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
22 |
/* properties */ |
50690 | 23 |
|
65855
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
24 |
val Now = new Properties.Double("now") |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
25 |
def now(props: Properties.T): Double = Now.unapply(props).get |
50697
82e9178e6a98
improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents:
50691
diff
changeset
|
26 |
|
50690 | 27 |
|
72035 | 28 |
/* monitor process */ |
29 |
||
30 |
def monitor(pid: Long, |
|
31 |
delay: Time = Time.seconds(0.5), |
|
32 |
consume: Properties.T => Unit = Console.println) |
|
33 |
{ |
|
34 |
def progress_stdout(line: String) |
|
35 |
{ |
|
36 |
val props = |
|
37 |
Library.space_explode(',', line).flatMap((entry: String) => |
|
38 |
Library.space_explode('=', entry) match { |
|
39 |
case List(a, b) => Some((a, b)) |
|
40 |
case _ => None |
|
41 |
}) |
|
42 |
if (props.nonEmpty) consume(props) |
|
43 |
} |
|
44 |
||
45 |
Bash.process("exec \"$POLYML_EXE\" -q --use " + |
|
46 |
File.bash_path(Path.explode("~~/src/Pure/ML/ml_statistics.ML")) + |
|
47 |
" --eval " + Bash.string("ML_Statistics.monitor " + ML_Syntax.print_long(pid) + " " + |
|
48 |
ML_Syntax.print_double(delay.seconds))) |
|
49 |
.result(progress_stdout = progress_stdout, strict = false).check |
|
50 |
} |
|
51 |
||
52 |
||
69832 | 53 |
/* memory fields (mega bytes) */ |
54 |
||
55 |
def mem_print(x: Long): Option[String] = |
|
56 |
if (x == 0L) None else Some(x.toString + " M") |
|
57 |
||
58 |
def mem_scale(x: Long): Long = x / 1024 / 1024 |
|
50690 | 59 |
|
69832 | 60 |
def mem_field_scale(name: String, x: Double): Double = |
61 |
if (heap_fields._2.contains(name) || program_fields._2.contains(name)) |
|
62 |
mem_scale(x.toLong).toDouble |
|
63 |
else x |
|
64 |
||
65 |
val CODE_SIZE = "size_code" |
|
66 |
val STACK_SIZE = "size_stacks" |
|
65858 | 67 |
val HEAP_SIZE = "size_heap" |
68 |
||
65866 | 69 |
|
70 |
/* standard fields */ |
|
71 |
||
65864 | 72 |
type Fields = (String, List[String]) |
65051 | 73 |
|
74 |
val tasks_fields: 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
|
75 |
("Future tasks", |
68129 | 76 |
List("tasks_ready", "tasks_pending", "tasks_running", "tasks_passive", |
77 |
"tasks_urgent", "tasks_total")) |
|
57869 | 78 |
|
65051 | 79 |
val workers_fields: Fields = |
57869 | 80 |
("Worker threads", List("workers_total", "workers_active", "workers_waiting")) |
81 |
||
65051 | 82 |
val GC_fields: Fields = |
69822
8c587dd44f51
updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents:
68129
diff
changeset
|
83 |
("GCs", List("partial_GCs", "full_GCs", "share_passes")) |
50690 | 84 |
|
65051 | 85 |
val heap_fields: Fields = |
65858 | 86 |
("Heap", List(HEAP_SIZE, "size_allocation", "size_allocation_free", |
50690 | 87 |
"size_heap_free_last_full_GC", "size_heap_free_last_GC")) |
88 |
||
69822
8c587dd44f51
updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents:
68129
diff
changeset
|
89 |
val program_fields: Fields = |
8c587dd44f51
updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents:
68129
diff
changeset
|
90 |
("Program", List("size_code", "size_stacks")) |
8c587dd44f51
updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents:
68129
diff
changeset
|
91 |
|
65051 | 92 |
val threads_fields: Fields = |
50690 | 93 |
("Threads", List("threads_total", "threads_in_ML", "threads_wait_condvar", |
94 |
"threads_wait_IO", "threads_wait_mutex", "threads_wait_signal")) |
|
95 |
||
65051 | 96 |
val time_fields: Fields = |
69822
8c587dd44f51
updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents:
68129
diff
changeset
|
97 |
("Time", List("time_elapsed", "time_elapsed_GC", "time_CPU", "time_GC")) |
51432 | 98 |
|
65051 | 99 |
val speed_fields: Fields = |
100 |
("Speed", List("speed_CPU", "speed_GC")) |
|
50690 | 101 |
|
69822
8c587dd44f51
updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents:
68129
diff
changeset
|
102 |
private val time_speed = Map("time_CPU" -> "speed_CPU", "time_GC" -> "speed_GC") |
8c587dd44f51
updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents:
68129
diff
changeset
|
103 |
|
65053 | 104 |
|
105 |
val all_fields: List[Fields] = |
|
69822
8c587dd44f51
updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents:
68129
diff
changeset
|
106 |
List(tasks_fields, workers_fields, GC_fields, heap_fields, program_fields, threads_fields, |
57869 | 107 |
time_fields, speed_fields) |
65053 | 108 |
|
109 |
val main_fields: List[Fields] = |
|
110 |
List(tasks_fields, workers_fields, heap_fields) |
|
65855
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
111 |
|
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
112 |
|
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
113 |
/* content interpretation */ |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
114 |
|
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
115 |
final case class Entry(time: Double, data: Map[String, Double]) |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
116 |
{ |
65858 | 117 |
def get(field: String): Double = data.getOrElse(field, 0.0) |
65855
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
118 |
} |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
119 |
|
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
120 |
val empty: ML_Statistics = apply(Nil) |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
121 |
|
67760
553d9ad7d679
more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents:
65866
diff
changeset
|
122 |
def apply(ml_statistics: List[Properties.T], heading: String = "", |
553d9ad7d679
more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents:
65866
diff
changeset
|
123 |
domain: String => Boolean = (key: String) => true): ML_Statistics = |
65855
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
124 |
{ |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
125 |
require(ml_statistics.forall(props => Now.unapply(props).isDefined)) |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
126 |
|
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
127 |
val time_start = if (ml_statistics.isEmpty) 0.0 else now(ml_statistics.head) |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
128 |
val duration = if (ml_statistics.isEmpty) 0.0 else now(ml_statistics.last) - time_start |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
129 |
|
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
130 |
val fields = |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
131 |
SortedSet.empty[String] ++ |
67760
553d9ad7d679
more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents:
65866
diff
changeset
|
132 |
(for { |
553d9ad7d679
more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents:
65866
diff
changeset
|
133 |
props <- ml_statistics.iterator |
553d9ad7d679
more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents:
65866
diff
changeset
|
134 |
(x, _) <- props.iterator |
553d9ad7d679
more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents:
65866
diff
changeset
|
135 |
if x != Now.name && domain(x) } yield x) |
65855
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
136 |
|
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
137 |
val content = |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
138 |
{ |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
139 |
var last_edge = Map.empty[String, (Double, Double, Double)] |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
140 |
val result = new mutable.ListBuffer[ML_Statistics.Entry] |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
141 |
for (props <- ml_statistics) { |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
142 |
val time = now(props) - time_start |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
143 |
require(time >= 0.0) |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
144 |
|
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
145 |
// rising edges -- relative speed |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
146 |
val speeds = |
67760
553d9ad7d679
more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents:
65866
diff
changeset
|
147 |
(for { |
553d9ad7d679
more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents:
65866
diff
changeset
|
148 |
(key, value) <- props.iterator |
69822
8c587dd44f51
updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents:
68129
diff
changeset
|
149 |
key1 <- time_speed.get(key) |
8c587dd44f51
updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents:
68129
diff
changeset
|
150 |
if domain(key1) |
8c587dd44f51
updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents:
68129
diff
changeset
|
151 |
} yield { |
8c587dd44f51
updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents:
68129
diff
changeset
|
152 |
val (x0, y0, s0) = last_edge.getOrElse(key, (0.0, 0.0, 0.0)) |
65855
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
153 |
|
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
154 |
val x1 = time |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
155 |
val y1 = java.lang.Double.parseDouble(value) |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
156 |
val s1 = if (x1 == x0) 0.0 else (y1 - y0) / (x1 - x0) |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
157 |
|
67760
553d9ad7d679
more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents:
65866
diff
changeset
|
158 |
if (y1 > y0) { |
69822
8c587dd44f51
updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents:
68129
diff
changeset
|
159 |
last_edge += (key -> (x1, y1, s1)) |
8c587dd44f51
updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents:
68129
diff
changeset
|
160 |
(key1, s1.toString) |
67760
553d9ad7d679
more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents:
65866
diff
changeset
|
161 |
} |
69822
8c587dd44f51
updated to polyml-5.8-20190220 (pre-release of Poly/ML 5.8);
wenzelm
parents:
68129
diff
changeset
|
162 |
else (key1, s0.toString) |
67760
553d9ad7d679
more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents:
65866
diff
changeset
|
163 |
}).toList |
65855
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
164 |
|
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
165 |
val data = |
67760
553d9ad7d679
more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents:
65866
diff
changeset
|
166 |
SortedMap.empty[String, Double] ++ |
553d9ad7d679
more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents:
65866
diff
changeset
|
167 |
(for { |
553d9ad7d679
more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents:
65866
diff
changeset
|
168 |
(x, y) <- props.iterator ++ speeds.iterator |
553d9ad7d679
more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents:
65866
diff
changeset
|
169 |
if x != Now.name && domain(x) |
553d9ad7d679
more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents:
65866
diff
changeset
|
170 |
z = java.lang.Double.parseDouble(y) if z != 0.0 |
69832 | 171 |
} yield { (x.intern, mem_field_scale(x, z)) }) |
65866 | 172 |
|
65855
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
173 |
result += ML_Statistics.Entry(time, data) |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
174 |
} |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
175 |
result.toList |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
176 |
} |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
177 |
|
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
178 |
new ML_Statistics(heading, fields, content, time_start, duration) |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
179 |
} |
50685 | 180 |
} |
50688
f02864682307
some support for ML statistics content interpretation;
wenzelm
parents:
50685
diff
changeset
|
181 |
|
65855
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
182 |
final class ML_Statistics private( |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
183 |
val heading: String, |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
184 |
val fields: Set[String], |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
185 |
val content: List[ML_Statistics.Entry], |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
186 |
val time_start: Double, |
33b3e76114f8
store processed content instead of somewhat bulky properties;
wenzelm
parents:
65854
diff
changeset
|
187 |
val duration: Double) |
50688
f02864682307
some support for ML statistics content interpretation;
wenzelm
parents:
50685
diff
changeset
|
188 |
{ |
65858 | 189 |
/* content */ |
190 |
||
191 |
def maximum(field: String): Double = |
|
192 |
(0.0 /: content)({ case (m, e) => m max e.get(field) }) |
|
193 |
||
194 |
def average(field: String): Double = |
|
195 |
{ |
|
196 |
@tailrec def sum(t0: Double, list: List[ML_Statistics.Entry], acc: Double): Double = |
|
197 |
list match { |
|
198 |
case Nil => acc |
|
199 |
case e :: es => |
|
200 |
val t = e.time |
|
201 |
sum(t, es, (t - t0) * e.get(field) + acc) |
|
202 |
} |
|
203 |
content match { |
|
204 |
case Nil => 0.0 |
|
205 |
case List(e) => e.get(field) |
|
206 |
case e :: es => sum(e.time, es, 0.0) / duration |
|
207 |
} |
|
208 |
} |
|
209 |
||
50689 | 210 |
|
211 |
/* charts */ |
|
212 |
||
65864 | 213 |
def update_data(data: XYSeriesCollection, selected_fields: List[String]) |
50689 | 214 |
{ |
50697
82e9178e6a98
improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents:
50691
diff
changeset
|
215 |
data.removeAllSeries |
69832 | 216 |
for (field <- selected_fields) { |
217 |
val series = new XYSeries(field) |
|
67760
553d9ad7d679
more compact ML_Statistics, to make build_status work with less than 2GB heap;
wenzelm
parents:
65866
diff
changeset
|
218 |
content.foreach(entry => series.add(entry.time, entry.get(field))) |
50689 | 219 |
data.addSeries(series) |
220 |
} |
|
50697
82e9178e6a98
improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents:
50691
diff
changeset
|
221 |
} |
82e9178e6a98
improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents:
50691
diff
changeset
|
222 |
|
65864 | 223 |
def chart(title: String, selected_fields: List[String]): JFreeChart = |
50697
82e9178e6a98
improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents:
50691
diff
changeset
|
224 |
{ |
82e9178e6a98
improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents:
50691
diff
changeset
|
225 |
val data = new XYSeriesCollection |
82e9178e6a98
improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents:
50691
diff
changeset
|
226 |
update_data(data, selected_fields) |
50689 | 227 |
|
228 |
ChartFactory.createXYLineChart(title, "time", "value", data, |
|
229 |
PlotOrientation.VERTICAL, true, true, true) |
|
230 |
} |
|
231 |
||
65051 | 232 |
def chart(fields: ML_Statistics.Fields): JFreeChart = |
233 |
chart(fields._1, fields._2) |
|
50691 | 234 |
|
65053 | 235 |
def show_frames(fields: List[ML_Statistics.Fields] = ML_Statistics.main_fields): Unit = |
71601 | 236 |
fields.map(chart).foreach(c => |
57612
990ffb84489b
clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents:
53189
diff
changeset
|
237 |
GUI_Thread.later { |
50854 | 238 |
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
|
239 |
iconImage = GUI.isabelle_image() |
65851 | 240 |
title = heading |
50854 | 241 |
contents = Component.wrap(new ChartPanel(c)) |
242 |
visible = true |
|
243 |
} |
|
50697
82e9178e6a98
improved Monitor_Dockable, based on ML_Statistics operations;
wenzelm
parents:
50691
diff
changeset
|
244 |
}) |
50688
f02864682307
some support for ML statistics content interpretation;
wenzelm
parents:
50685
diff
changeset
|
245 |
} |