| author | wenzelm |
| Mon, 07 Dec 2020 20:26:09 +0100 | |
| changeset 72847 | 9dda93a753b1 |
| parent 72844 | 240c8a0f6337 |
| child 72848 | d5d0e36eda16 |
| permissions | -rw-r--r-- |
| 72662 | 1 |
/* Title: Pure/Tools/build_job.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Build job running prover process, with rudimentary PIDE session. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
import scala.collection.mutable |
|
11 |
||
12 |
||
13 |
class Build_Job(progress: Progress, |
|
14 |
session_name: String, |
|
15 |
val info: Sessions.Info, |
|
16 |
deps: Sessions.Deps, |
|
17 |
store: Sessions.Store, |
|
18 |
do_store: Boolean, |
|
19 |
verbose: Boolean, |
|
20 |
val numa_node: Option[Int], |
|
21 |
command_timings0: List[Properties.T]) |
|
22 |
{
|
|
23 |
val options: Options = NUMA.policy_options(info.options, numa_node) |
|
24 |
||
25 |
private val sessions_structure = deps.sessions_structure |
|
26 |
||
27 |
private val future_result: Future[Process_Result] = |
|
28 |
Future.thread("build", uninterruptible = true) {
|
|
29 |
val parent = info.parent.getOrElse("")
|
|
30 |
val base = deps(parent) |
|
|
72816
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents:
72783
diff
changeset
|
31 |
val result_base = deps(session_name) |
| 72662 | 32 |
|
33 |
val env = |
|
34 |
Isabelle_System.settings() + |
|
35 |
("ISABELLE_ML_DEBUGGER" -> options.bool("ML_debugger").toString)
|
|
36 |
||
37 |
val is_pure = Sessions.is_pure(session_name) |
|
38 |
||
39 |
val use_prelude = if (is_pure) Thy_Header.ml_roots.map(_._1) else Nil |
|
40 |
||
41 |
val eval_store = |
|
42 |
if (do_store) {
|
|
43 |
(if (info.theories.nonEmpty) List("ML_Heap.share_common_data ()") else Nil) :::
|
|
44 |
List("ML_Heap.save_child " +
|
|
45 |
ML_Syntax.print_string_bytes(File.platform_path(store.output_heap(session_name)))) |
|
46 |
} |
|
47 |
else Nil |
|
48 |
||
49 |
val resources = new Resources(sessions_structure, base, command_timings = command_timings0) |
|
50 |
val session = |
|
51 |
new Session(options, resources) {
|
|
52 |
override val xml_cache: XML.Cache = store.xml_cache |
|
53 |
override val xz_cache: XZ.Cache = store.xz_cache |
|
|
72816
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents:
72783
diff
changeset
|
54 |
|
|
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents:
72783
diff
changeset
|
55 |
override def build_blobs_info(name: Document.Node.Name): Command.Blobs_Info = |
|
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents:
72783
diff
changeset
|
56 |
{
|
|
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents:
72783
diff
changeset
|
57 |
result_base.load_commands.get(name.expand) match {
|
|
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents:
72783
diff
changeset
|
58 |
case Some(spans) => |
|
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents:
72783
diff
changeset
|
59 |
val syntax = result_base.theory_syntax(name) |
|
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents:
72783
diff
changeset
|
60 |
Command.build_blobs_info(syntax, name, spans) |
|
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents:
72783
diff
changeset
|
61 |
case None => Command.Blobs_Info.none |
|
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents:
72783
diff
changeset
|
62 |
} |
|
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents:
72783
diff
changeset
|
63 |
} |
| 72662 | 64 |
} |
| 72730 | 65 |
def make_rendering(snapshot: Document.Snapshot): Rendering = |
66 |
new Rendering(snapshot, options, session) {
|
|
67 |
override def model: Document.Model = ??? |
|
68 |
} |
|
| 72662 | 69 |
|
70 |
object Build_Session_Errors |
|
71 |
{
|
|
72 |
private val promise: Promise[List[String]] = Future.promise |
|
73 |
||
74 |
def result: Exn.Result[List[String]] = promise.join_result |
|
75 |
def cancel: Unit = promise.cancel |
|
76 |
def apply(errs: List[String]) |
|
77 |
{
|
|
78 |
try { promise.fulfill(errs) }
|
|
79 |
catch { case _: IllegalStateException => }
|
|
80 |
} |
|
81 |
} |
|
82 |
||
83 |
val export_consumer = |
|
84 |
Export.consumer(store.open_database(session_name, output = true), cache = store.xz_cache) |
|
85 |
||
86 |
val stdout = new StringBuilder(1000) |
|
87 |
val stderr = new StringBuilder(1000) |
|
88 |
val messages = new mutable.ListBuffer[XML.Elem] |
|
89 |
val command_timings = new mutable.ListBuffer[Properties.T] |
|
90 |
val theory_timings = new mutable.ListBuffer[Properties.T] |
|
91 |
val session_timings = new mutable.ListBuffer[Properties.T] |
|
92 |
val runtime_statistics = new mutable.ListBuffer[Properties.T] |
|
93 |
val task_statistics = new mutable.ListBuffer[Properties.T] |
|
94 |
||
95 |
def fun( |
|
96 |
name: String, |
|
97 |
acc: mutable.ListBuffer[Properties.T], |
|
98 |
unapply: Properties.T => Option[Properties.T]): (String, Session.Protocol_Function) = |
|
99 |
{
|
|
100 |
name -> ((msg: Prover.Protocol_Output) => |
|
101 |
unapply(msg.properties) match {
|
|
102 |
case Some(props) => acc += props; true |
|
103 |
case _ => false |
|
104 |
}) |
|
105 |
} |
|
106 |
||
107 |
session.init_protocol_handler(new Session.Protocol_Handler |
|
108 |
{
|
|
109 |
override def exit() { Build_Session_Errors.cancel }
|
|
110 |
||
111 |
private def build_session_finished(msg: Prover.Protocol_Output): Boolean = |
|
112 |
{
|
|
113 |
val (rc, errors) = |
|
114 |
try {
|
|
115 |
val (rc, errs) = |
|
116 |
{
|
|
117 |
import XML.Decode._ |
|
118 |
pair(int, list(x => x))(Symbol.decode_yxml(msg.text)) |
|
119 |
} |
|
120 |
val errors = |
|
121 |
for (err <- errs) yield {
|
|
122 |
val prt = Protocol_Message.expose_no_reports(err) |
|
123 |
Pretty.string_of(prt, metric = Symbol.Metric) |
|
124 |
} |
|
125 |
(rc, errors) |
|
126 |
} |
|
127 |
catch { case ERROR(err) => (2, List(err)) }
|
|
128 |
||
129 |
session.protocol_command("Prover.stop", rc.toString)
|
|
130 |
Build_Session_Errors(errors) |
|
131 |
true |
|
132 |
} |
|
133 |
||
134 |
private def loading_theory(msg: Prover.Protocol_Output): Boolean = |
|
135 |
msg.properties match {
|
|
|
72692
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents:
72683
diff
changeset
|
136 |
case Markup.Loading_Theory(Markup.Name(name)) => |
| 72662 | 137 |
progress.theory(Progress.Theory(name, session = session_name)) |
|
72692
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents:
72683
diff
changeset
|
138 |
false |
| 72662 | 139 |
case _ => false |
140 |
} |
|
141 |
||
142 |
private def export(msg: Prover.Protocol_Output): Boolean = |
|
143 |
msg.properties match {
|
|
144 |
case Protocol.Export(args) => |
|
145 |
export_consumer(session_name, args, msg.bytes) |
|
146 |
true |
|
147 |
case _ => false |
|
148 |
} |
|
149 |
||
150 |
override val functions = |
|
151 |
List( |
|
152 |
Markup.Build_Session_Finished.name -> build_session_finished, |
|
153 |
Markup.Loading_Theory.name -> loading_theory, |
|
154 |
Markup.EXPORT -> export, |
|
155 |
fun(Markup.Theory_Timing.name, theory_timings, Markup.Theory_Timing.unapply), |
|
156 |
fun(Markup.Session_Timing.name, session_timings, Markup.Session_Timing.unapply), |
|
157 |
fun(Markup.Task_Statistics.name, task_statistics, Markup.Task_Statistics.unapply)) |
|
158 |
}) |
|
159 |
||
| 72711 | 160 |
session.command_timings += Session.Consumer("command_timings")
|
161 |
{
|
|
162 |
case Session.Command_Timing(props) => |
|
163 |
for {
|
|
164 |
elapsed <- Markup.Elapsed.unapply(props) |
|
165 |
elapsed_time = Time.seconds(elapsed) |
|
166 |
if elapsed_time.is_relevant && elapsed_time >= options.seconds("command_timing_threshold")
|
|
167 |
} command_timings += props.filter(Markup.command_timing_property) |
|
168 |
} |
|
169 |
||
| 72662 | 170 |
session.runtime_statistics += Session.Consumer("ML_statistics")
|
171 |
{
|
|
172 |
case Session.Runtime_Statistics(props) => runtime_statistics += props |
|
173 |
} |
|
174 |
||
|
72721
79f5e843e5ec
clarified signature: prefer high-level Snapshot over low-level Command.State;
wenzelm
parents:
72717
diff
changeset
|
175 |
session.finished_theories += Session.Consumer[Document.Snapshot]("finished_theories")
|
|
72692
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents:
72683
diff
changeset
|
176 |
{
|
|
72721
79f5e843e5ec
clarified signature: prefer high-level Snapshot over low-level Command.State;
wenzelm
parents:
72717
diff
changeset
|
177 |
case snapshot => |
| 72730 | 178 |
val rendering = make_rendering(snapshot) |
179 |
||
| 72844 | 180 |
def export(name: String, xml: XML.Body, compress: Boolean = true) |
| 72717 | 181 |
{
|
|
72721
79f5e843e5ec
clarified signature: prefer high-level Snapshot over low-level Command.State;
wenzelm
parents:
72717
diff
changeset
|
182 |
val theory_name = snapshot.node_name.theory |
| 72844 | 183 |
val args = |
184 |
Protocol.Export.Args(theory_name = theory_name, name = name, compress = compress) |
|
| 72783 | 185 |
val bytes = Bytes(Symbol.encode(YXML.string_of_body(xml))) |
| 72730 | 186 |
if (!bytes.is_empty) export_consumer(session_name, args, bytes) |
| 72717 | 187 |
} |
| 72844 | 188 |
def export_text(name: String, text: String, compress: Boolean = true): Unit = |
189 |
export(name, List(XML.Text(text)), compress = compress) |
|
| 72730 | 190 |
|
| 72844 | 191 |
for (command <- snapshot.snippet_command) {
|
192 |
export_text(Export.DOCUMENT_ID, command.id.toString, compress = false) |
|
193 |
} |
|
194 |
||
195 |
export_text(Export.FILES, |
|
196 |
cat_lines(snapshot.node_files.map(_.symbolic.node)), compress = false) |
|
|
72816
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents:
72783
diff
changeset
|
197 |
|
|
72817
1c378ab75d48
clarified signature, notably access to blob files;
wenzelm
parents:
72816
diff
changeset
|
198 |
for ((xml, i) <- snapshot.xml_markup_blobs().zipWithIndex) {
|
|
1c378ab75d48
clarified signature, notably access to blob files;
wenzelm
parents:
72816
diff
changeset
|
199 |
export(Export.MARKUP + (i + 1), xml) |
|
1c378ab75d48
clarified signature, notably access to blob files;
wenzelm
parents:
72816
diff
changeset
|
200 |
} |
| 72723 | 201 |
export(Export.MARKUP, snapshot.xml_markup()) |
| 72725 | 202 |
export(Export.MESSAGES, snapshot.messages.map(_._1)) |
| 72730 | 203 |
|
204 |
val citations = Library.distinct(rendering.citations(Text.Range.full).map(_.info)) |
|
205 |
export_text(Export.CITATIONS, cat_lines(citations)) |
|
|
72692
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents:
72683
diff
changeset
|
206 |
} |
|
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
wenzelm
parents:
72683
diff
changeset
|
207 |
|
| 72662 | 208 |
session.all_messages += Session.Consumer[Any]("build_session_output")
|
209 |
{
|
|
210 |
case msg: Prover.Output => |
|
211 |
val message = msg.message |
|
212 |
if (msg.is_stdout) {
|
|
213 |
stdout ++= Symbol.encode(XML.content(message)) |
|
214 |
} |
|
215 |
else if (msg.is_stderr) {
|
|
216 |
stderr ++= Symbol.encode(XML.content(message)) |
|
217 |
} |
|
218 |
else if (Protocol.is_exported(message)) {
|
|
219 |
messages += message |
|
220 |
} |
|
221 |
else if (msg.is_exit) {
|
|
222 |
val err = |
|
223 |
"Prover terminated" + |
|
224 |
(msg.properties match {
|
|
225 |
case Markup.Process_Result(result) => ": " + result.print_rc |
|
226 |
case _ => "" |
|
227 |
}) |
|
228 |
Build_Session_Errors(List(err)) |
|
229 |
} |
|
230 |
case _ => |
|
231 |
} |
|
232 |
||
233 |
val eval_main = Command_Line.ML_tool("Isabelle_Process.init_build ()" :: eval_store)
|
|
234 |
||
235 |
val process = |
|
236 |
Isabelle_Process(session, options, sessions_structure, store, |
|
237 |
logic = parent, raw_ml_system = is_pure, |
|
238 |
use_prelude = use_prelude, eval_main = eval_main, |
|
239 |
cwd = info.dir.file, env = env) |
|
240 |
||
241 |
val build_errors = |
|
242 |
Isabelle_Thread.interrupt_handler(_ => process.terminate) {
|
|
243 |
Exn.capture { process.await_startup } match {
|
|
244 |
case Exn.Res(_) => |
|
245 |
val resources_yxml = resources.init_session_yxml |
|
246 |
val args_yxml = |
|
247 |
YXML.string_of_body( |
|
248 |
{
|
|
249 |
import XML.Encode._ |
|
250 |
pair(string, list(pair(Options.encode, list(pair(string, properties)))))( |
|
251 |
(session_name, info.theories)) |
|
252 |
}) |
|
253 |
session.protocol_command("build_session", resources_yxml, args_yxml)
|
|
254 |
Build_Session_Errors.result |
|
255 |
case Exn.Exn(exn) => Exn.Res(List(Exn.message(exn))) |
|
256 |
} |
|
257 |
} |
|
258 |
||
259 |
val process_result = |
|
260 |
Isabelle_Thread.interrupt_handler(_ => process.terminate) { process.await_shutdown }
|
|
261 |
||
| 72697 | 262 |
session.stop() |
263 |
||
| 72662 | 264 |
val export_errors = |
265 |
export_consumer.shutdown(close = true).map(Output.error_message_text) |
|
266 |
||
| 72699 | 267 |
val (document_output, document_errors) = |
| 72662 | 268 |
try {
|
| 72675 | 269 |
if (build_errors.isInstanceOf[Exn.Res[_]] && process_result.ok && info.documents.nonEmpty) |
270 |
{
|
|
|
72715
2615b8c05337
clarified signature --- avoid repeated open_database on server;
wenzelm
parents:
72711
diff
changeset
|
271 |
using(store.open_database_context(deps.sessions_structure))(db_context => |
|
2615b8c05337
clarified signature --- avoid repeated open_database on server;
wenzelm
parents:
72711
diff
changeset
|
272 |
{
|
|
2615b8c05337
clarified signature --- avoid repeated open_database on server;
wenzelm
parents:
72711
diff
changeset
|
273 |
val documents = |
|
2615b8c05337
clarified signature --- avoid repeated open_database on server;
wenzelm
parents:
72711
diff
changeset
|
274 |
Presentation.build_documents(session_name, deps, db_context, |
|
2615b8c05337
clarified signature --- avoid repeated open_database on server;
wenzelm
parents:
72711
diff
changeset
|
275 |
output_sources = info.document_output, |
|
2615b8c05337
clarified signature --- avoid repeated open_database on server;
wenzelm
parents:
72711
diff
changeset
|
276 |
output_pdf = info.document_output, |
|
2615b8c05337
clarified signature --- avoid repeated open_database on server;
wenzelm
parents:
72711
diff
changeset
|
277 |
progress = progress, |
|
2615b8c05337
clarified signature --- avoid repeated open_database on server;
wenzelm
parents:
72711
diff
changeset
|
278 |
verbose = verbose) |
|
2615b8c05337
clarified signature --- avoid repeated open_database on server;
wenzelm
parents:
72711
diff
changeset
|
279 |
db_context.output_database(session_name)(db => |
|
2615b8c05337
clarified signature --- avoid repeated open_database on server;
wenzelm
parents:
72711
diff
changeset
|
280 |
documents.foreach(_.write(db, session_name))) |
|
2615b8c05337
clarified signature --- avoid repeated open_database on server;
wenzelm
parents:
72711
diff
changeset
|
281 |
(documents.flatMap(_.log_lines), Nil) |
|
2615b8c05337
clarified signature --- avoid repeated open_database on server;
wenzelm
parents:
72711
diff
changeset
|
282 |
}) |
| 72662 | 283 |
} |
| 72699 | 284 |
(Nil, Nil) |
| 72662 | 285 |
} |
| 72699 | 286 |
catch { case Exn.Interrupt.ERROR(msg) => (Nil, List(msg)) }
|
| 72662 | 287 |
|
288 |
val result = |
|
289 |
{
|
|
|
72738
a4d7da18ac5c
store timings for used_theories in canonical order, with reconstructed store.read_theories;
wenzelm
parents:
72730
diff
changeset
|
290 |
val theory_timing = |
|
a4d7da18ac5c
store timings for used_theories in canonical order, with reconstructed store.read_theories;
wenzelm
parents:
72730
diff
changeset
|
291 |
theory_timings.iterator.map( |
|
a4d7da18ac5c
store timings for used_theories in canonical order, with reconstructed store.read_theories;
wenzelm
parents:
72730
diff
changeset
|
292 |
{ case props @ Markup.Name(name) => name -> props }).toMap
|
|
a4d7da18ac5c
store timings for used_theories in canonical order, with reconstructed store.read_theories;
wenzelm
parents:
72730
diff
changeset
|
293 |
val used_theory_timings = |
|
a4d7da18ac5c
store timings for used_theories in canonical order, with reconstructed store.read_theories;
wenzelm
parents:
72730
diff
changeset
|
294 |
for { (name, _) <- deps(session_name).used_theories }
|
|
a4d7da18ac5c
store timings for used_theories in canonical order, with reconstructed store.read_theories;
wenzelm
parents:
72730
diff
changeset
|
295 |
yield theory_timing.getOrElse(name.theory, Markup.Name(name.theory)) |
|
a4d7da18ac5c
store timings for used_theories in canonical order, with reconstructed store.read_theories;
wenzelm
parents:
72730
diff
changeset
|
296 |
|
| 72662 | 297 |
val more_output = |
298 |
Library.trim_line(stdout.toString) :: |
|
299 |
messages.toList.map(message => |
|
300 |
Symbol.encode(Protocol.message_text(List(message), metric = Symbol.Metric))) ::: |
|
301 |
command_timings.toList.map(Protocol.Command_Timing_Marker.apply) ::: |
|
|
72738
a4d7da18ac5c
store timings for used_theories in canonical order, with reconstructed store.read_theories;
wenzelm
parents:
72730
diff
changeset
|
302 |
used_theory_timings.map(Protocol.Theory_Timing_Marker.apply) ::: |
| 72662 | 303 |
session_timings.toList.map(Protocol.Session_Timing_Marker.apply) ::: |
304 |
runtime_statistics.toList.map(Protocol.ML_Statistics_Marker.apply) ::: |
|
305 |
task_statistics.toList.map(Protocol.Task_Statistics_Marker.apply) ::: |
|
| 72699 | 306 |
document_output |
| 72662 | 307 |
|
|
72726
ec6a27bbdab8
proper return code for more errors (amending d892f6d66402);
wenzelm
parents:
72725
diff
changeset
|
308 |
process_result.output(more_output) |
|
ec6a27bbdab8
proper return code for more errors (amending d892f6d66402);
wenzelm
parents:
72725
diff
changeset
|
309 |
.error(Library.trim_line(stderr.toString)) |
|
ec6a27bbdab8
proper return code for more errors (amending d892f6d66402);
wenzelm
parents:
72725
diff
changeset
|
310 |
.errors_rc(export_errors ::: document_errors) |
| 72662 | 311 |
} |
312 |
||
313 |
build_errors match {
|
|
314 |
case Exn.Res(build_errs) => |
|
315 |
val errs = build_errs ::: document_errors |
|
316 |
if (errs.isEmpty) result |
|
317 |
else {
|
|
318 |
result.error_rc.output( |
|
319 |
errs.flatMap(s => split_lines(Output.error_message_text(s))) ::: |
|
320 |
errs.map(Protocol.Error_Message_Marker.apply)) |
|
321 |
} |
|
322 |
case Exn.Exn(Exn.Interrupt()) => |
|
323 |
if (result.ok) result.copy(rc = Exn.Interrupt.return_code) else result |
|
324 |
case Exn.Exn(exn) => throw exn |
|
325 |
} |
|
326 |
} |
|
327 |
||
328 |
def terminate: Unit = future_result.cancel |
|
329 |
def is_finished: Boolean = future_result.is_finished |
|
330 |
||
331 |
private val timeout_request: Option[Event_Timer.Request] = |
|
332 |
{
|
|
333 |
if (info.timeout > Time.zero) |
|
334 |
Some(Event_Timer.request(Time.now() + info.timeout) { terminate })
|
|
335 |
else None |
|
336 |
} |
|
337 |
||
338 |
def join: (Process_Result, Option[String]) = |
|
339 |
{
|
|
340 |
val result1 = future_result.join |
|
341 |
||
342 |
val was_timeout = |
|
343 |
timeout_request match {
|
|
344 |
case None => false |
|
345 |
case Some(request) => !request.cancel |
|
346 |
} |
|
347 |
||
348 |
val result2 = |
|
349 |
if (result1.interrupted) {
|
|
350 |
if (was_timeout) result1.error(Output.error_message_text("Timeout")).was_timeout
|
|
351 |
else result1.error(Output.error_message_text("Interrupt"))
|
|
352 |
} |
|
353 |
else result1 |
|
354 |
||
355 |
val heap_digest = |
|
356 |
if (result2.ok && do_store && store.output_heap(session_name).is_file) |
|
357 |
Some(Sessions.write_heap_digest(store.output_heap(session_name))) |
|
358 |
else None |
|
359 |
||
360 |
(result2, heap_digest) |
|
361 |
} |
|
362 |
} |