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