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