| author | Fabian Huch <huch@in.tum.de> |
| Wed, 13 Mar 2024 11:54:06 +0100 | |
| changeset 79880 | a3d53f2bc41d |
| parent 79718 | fba02e281b44 |
| child 79887 | 17220dc05991 |
| permissions | -rw-r--r-- |
| 79502 | 1 |
/* Title: Pure/Build/build_job.scala |
| 72662 | 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 |
||
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
13 |
trait Build_Job {
|
|
77486
032c76e04475
clarified execution context: main work happens within Future.thread;
wenzelm
parents:
77485
diff
changeset
|
14 |
def cancel(): Unit = () |
| 77298 | 15 |
def is_finished: Boolean = false |
| 78530 | 16 |
def join: Option[Build_Job.Result] = None |
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
17 |
} |
|
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
18 |
|
| 75393 | 19 |
object Build_Job {
|
| 78530 | 20 |
sealed case class Result(process_result: Process_Result, output_shasum: SHA1.Shasum) |
21 |
||
22 |
||
| 77396 | 23 |
/* build session */ |
24 |
||
| 77474 | 25 |
def start_session( |
| 78421 | 26 |
build_context: Build.Context, |
|
78237
c2c59de57df9
clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents:
78213
diff
changeset
|
27 |
session_context: Session_Context, |
| 77505 | 28 |
progress: Progress, |
29 |
log: Logger, |
|
| 78372 | 30 |
server: SSH.Server, |
| 77474 | 31 |
session_background: Sessions.Background, |
|
78237
c2c59de57df9
clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents:
78213
diff
changeset
|
32 |
sources_shasum: SHA1.Shasum, |
| 77474 | 33 |
input_shasum: SHA1.Shasum, |
|
78237
c2c59de57df9
clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents:
78213
diff
changeset
|
34 |
node_info: Host.Node_Info, |
|
c2c59de57df9
clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents:
78213
diff
changeset
|
35 |
store_heap: Boolean |
| 77505 | 36 |
): Session_Job = {
|
| 78372 | 37 |
new Session_Job(build_context, session_context, progress, log, server, |
|
78237
c2c59de57df9
clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents:
78213
diff
changeset
|
38 |
session_background, sources_shasum, input_shasum, node_info, store_heap) |
| 77505 | 39 |
} |
| 77474 | 40 |
|
| 77442 | 41 |
object Session_Context {
|
42 |
def load( |
|
| 78374 | 43 |
database_server: Option[SQL.Database], |
|
77529
40ccee0fe19a
separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents:
77521
diff
changeset
|
44 |
build_uuid: String, |
|
77444
0c704aba71e3
clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents:
77442
diff
changeset
|
45 |
name: String, |
|
0c704aba71e3
clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents:
77442
diff
changeset
|
46 |
deps: List[String], |
|
0c704aba71e3
clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents:
77442
diff
changeset
|
47 |
ancestors: List[String], |
|
77610
3b09ae9e40cb
clarified session prefs (or "options" within the database);
wenzelm
parents:
77587
diff
changeset
|
48 |
session_prefs: String, |
| 77458 | 49 |
sources_shasum: SHA1.Shasum, |
| 77442 | 50 |
timeout: Time, |
| 78178 | 51 |
store: Store, |
| 78374 | 52 |
progress: Progress = new Progress |
| 77442 | 53 |
): Session_Context = {
|
| 77450 | 54 |
def default: Session_Context = |
| 77496 | 55 |
Session_Context( |
|
77610
3b09ae9e40cb
clarified session prefs (or "options" within the database);
wenzelm
parents:
77587
diff
changeset
|
56 |
name, deps, ancestors, session_prefs, sources_shasum, timeout, |
|
3b09ae9e40cb
clarified session prefs (or "options" within the database);
wenzelm
parents:
77587
diff
changeset
|
57 |
Time.zero, Bytes.empty, build_uuid) |
|
77444
0c704aba71e3
clarified signature: reduce explicit access to static Sessions.Structure;
wenzelm
parents:
77442
diff
changeset
|
58 |
|
| 78374 | 59 |
def read(db: SQL.Database): Session_Context = {
|
60 |
def ignore_error(msg: String) = {
|
|
61 |
progress.echo_warning( |
|
62 |
"Ignoring bad database " + db + " for session " + quote(name) + |
|
63 |
if_proper(msg, ":\n" + msg)) |
|
64 |
default |
|
65 |
} |
|
66 |
try {
|
|
67 |
val command_timings = store.read_command_timings(db, name) |
|
68 |
val elapsed = |
|
69 |
store.read_session_timing(db, name) match {
|
|
70 |
case Markup.Elapsed(s) => Time.seconds(s) |
|
71 |
case _ => Time.zero |
|
72 |
} |
|
73 |
new Session_Context( |
|
74 |
name, deps, ancestors, session_prefs, sources_shasum, timeout, |
|
75 |
elapsed, command_timings, build_uuid) |
|
76 |
} |
|
77 |
catch {
|
|
78 |
case ERROR(msg) => ignore_error(msg) |
|
79 |
case exn: java.lang.Error => ignore_error(Exn.message(exn)) |
|
80 |
case _: XML.Error => ignore_error("XML.Error")
|
|
81 |
} |
|
82 |
} |
|
83 |
||
84 |
database_server match {
|
|
85 |
case Some(db) => if (store.session_info_exists(db)) read(db) else default |
|
86 |
case None => using_option(store.try_open_database(name))(read) getOrElse default |
|
| 77442 | 87 |
} |
88 |
} |
|
89 |
} |
|
90 |
||
| 77496 | 91 |
sealed case class Session_Context( |
92 |
name: String, |
|
93 |
deps: List[String], |
|
94 |
ancestors: List[String], |
|
|
77610
3b09ae9e40cb
clarified session prefs (or "options" within the database);
wenzelm
parents:
77587
diff
changeset
|
95 |
session_prefs: String, |
| 77496 | 96 |
sources_shasum: SHA1.Shasum, |
97 |
timeout: Time, |
|
98 |
old_time: Time, |
|
99 |
old_command_timings_blob: Bytes, |
|
|
77529
40ccee0fe19a
separate static build_uuid from dynamic worker_uuid, to allow multiple worker processes participate in one build process;
wenzelm
parents:
77521
diff
changeset
|
100 |
build_uuid: String |
|
77652
5f706f7c624b
more thorough synchronization of internal "_state" vs. external "_database";
wenzelm
parents:
77651
diff
changeset
|
101 |
) extends Library.Named |
| 77442 | 102 |
|
| 77474 | 103 |
class Session_Job private[Build_Job]( |
| 78421 | 104 |
build_context: Build.Context, |
|
78237
c2c59de57df9
clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents:
78213
diff
changeset
|
105 |
session_context: Session_Context, |
| 77505 | 106 |
progress: Progress, |
107 |
log: Logger, |
|
| 78372 | 108 |
server: SSH.Server, |
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
109 |
session_background: Sessions.Background, |
|
78237
c2c59de57df9
clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents:
78213
diff
changeset
|
110 |
sources_shasum: SHA1.Shasum, |
|
77460
ccca589d7027
tuned signature: support general Build_Job instances;
wenzelm
parents:
77458
diff
changeset
|
111 |
input_shasum: SHA1.Shasum, |
|
78237
c2c59de57df9
clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents:
78213
diff
changeset
|
112 |
node_info: Host.Node_Info, |
|
c2c59de57df9
clarified static Build_Process.Context vs. dynamic Build_Process.State;
wenzelm
parents:
78213
diff
changeset
|
113 |
store_heap: Boolean |
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
114 |
) extends Build_Job {
|
|
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
115 |
def session_name: String = session_background.session_name |
|
77349
5a84de89170d
more operations to support management of jobs, e.g. from external database;
wenzelm
parents:
77298
diff
changeset
|
116 |
|
| 78530 | 117 |
private val future_result: Future[Option[Result]] = |
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
118 |
Future.thread("build", uninterruptible = true) {
|
|
78359
cb0a90df4871
prefer asynchronous operations: reduce time spent within synchronized_database("Build_Process.start_job");
wenzelm
parents:
78280
diff
changeset
|
119 |
val info = session_background.sessions_structure(session_name) |
|
78842
eb572f7b6689
prefer extensible next_node_info in build process over process_options in build engine (which needs the final node info anyway);
Fabian Huch <huch@in.tum.de>
parents:
78674
diff
changeset
|
120 |
val options = Host.node_options(info.options, node_info) |
|
78359
cb0a90df4871
prefer asynchronous operations: reduce time spent within synchronized_database("Build_Process.start_job");
wenzelm
parents:
78280
diff
changeset
|
121 |
val store = build_context.store |
|
cb0a90df4871
prefer asynchronous operations: reduce time spent within synchronized_database("Build_Process.start_job");
wenzelm
parents:
78280
diff
changeset
|
122 |
|
| 78372 | 123 |
using_optional(store.maybe_open_database_server(server = server)) { database_server =>
|
124 |
store.clean_output(database_server, session_name, session_init = true) |
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
125 |
|
| 78372 | 126 |
val session_sources = |
127 |
Store.Sources.load(session_background.base, cache = store.cache.compress) |
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
128 |
|
| 78372 | 129 |
val env = |
130 |
Isabelle_System.settings( |
|
131 |
List("ISABELLE_ML_DEBUGGER" -> options.bool("ML_debugger").toString))
|
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
132 |
|
| 78372 | 133 |
val session_heaps = |
134 |
session_background.info.parent match {
|
|
135 |
case None => Nil |
|
136 |
case Some(logic) => ML_Process.session_heaps(store, session_background, logic = logic) |
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
137 |
} |
|
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
138 |
|
| 78372 | 139 |
val use_prelude = if (session_heaps.isEmpty) Thy_Header.ml_roots.map(_._1) else Nil |
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
140 |
|
| 78372 | 141 |
val eval_store = |
142 |
if (store_heap) {
|
|
143 |
(if (info.theories.nonEmpty) List("ML_Heap.share_common_data ()") else Nil) :::
|
|
144 |
List("ML_Heap.save_child " +
|
|
145 |
ML_Syntax.print_string_bytes(File.platform_path(store.output_heap(session_name)))) |
|
146 |
} |
|
147 |
else Nil |
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
148 |
|
| 78372 | 149 |
def session_blobs(node_name: Document.Node.Name): List[(Command.Blob, Document.Blobs.Item)] = |
150 |
session_background.base.theory_load_commands.get(node_name.theory) match {
|
|
151 |
case None => Nil |
|
152 |
case Some(spans) => |
|
153 |
val syntax = session_background.base.theory_syntax(node_name) |
|
154 |
val master_dir = Path.explode(node_name.master_dir) |
|
155 |
for (span <- spans; file <- span.loaded_files(syntax).files) |
|
156 |
yield {
|
|
157 |
val src_path = Path.explode(file) |
|
158 |
val blob_name = Document.Node.Name(File.symbolic_path(master_dir + src_path)) |
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
159 |
|
| 78372 | 160 |
val bytes = session_sources(blob_name.node).bytes |
161 |
val text = bytes.text |
|
162 |
val chunk = Symbol.Text_Chunk(text) |
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
163 |
|
| 78372 | 164 |
Command.Blob(blob_name, src_path, Some((SHA1.digest(bytes), chunk))) -> |
165 |
Document.Blobs.Item(bytes, text, chunk, changed = false) |
|
166 |
} |
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
167 |
} |
|
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
168 |
|
|
77486
032c76e04475
clarified execution context: main work happens within Future.thread;
wenzelm
parents:
77485
diff
changeset
|
169 |
|
| 78372 | 170 |
/* session */ |
|
77486
032c76e04475
clarified execution context: main work happens within Future.thread;
wenzelm
parents:
77485
diff
changeset
|
171 |
|
| 78372 | 172 |
val resources = |
173 |
new Resources(session_background, log = log, |
|
174 |
command_timings = |
|
175 |
Properties.uncompress(session_context.old_command_timings_blob, cache = store.cache)) |
|
| 77485 | 176 |
|
| 78372 | 177 |
val session = |
178 |
new Session(options, resources) {
|
|
179 |
override val cache: Term.Cache = store.cache |
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
180 |
|
| 78372 | 181 |
override def build_blobs_info(node_name: Document.Node.Name): Command.Blobs_Info = |
182 |
Command.Blobs_Info.make(session_blobs(node_name)) |
|
183 |
||
184 |
override def build_blobs(node_name: Document.Node.Name): Document.Blobs = |
|
185 |
Document.Blobs.make(session_blobs(node_name)) |
|
186 |
} |
|
187 |
||
188 |
object Build_Session_Errors {
|
|
189 |
private val promise: Promise[List[String]] = Future.promise |
|
190 |
||
191 |
def result: Exn.Result[List[String]] = promise.join_result |
|
192 |
def cancel(): Unit = promise.cancel() |
|
193 |
def apply(errs: List[String]): Unit = {
|
|
194 |
try { promise.fulfill(errs) }
|
|
195 |
catch { case _: IllegalStateException => }
|
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
196 |
} |
|
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
197 |
} |
|
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
198 |
|
| 78372 | 199 |
val export_consumer = |
200 |
Export.consumer(store.open_database(session_name, output = true, server = server), |
|
201 |
store.cache, progress = progress) |
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
202 |
|
| 78372 | 203 |
val stdout = new StringBuilder(1000) |
204 |
val stderr = new StringBuilder(1000) |
|
205 |
val command_timings = new mutable.ListBuffer[Properties.T] |
|
206 |
val theory_timings = new mutable.ListBuffer[Properties.T] |
|
207 |
val session_timings = new mutable.ListBuffer[Properties.T] |
|
208 |
val runtime_statistics = new mutable.ListBuffer[Properties.T] |
|
209 |
val task_statistics = new mutable.ListBuffer[Properties.T] |
|
210 |
||
211 |
def fun( |
|
212 |
name: String, |
|
213 |
acc: mutable.ListBuffer[Properties.T], |
|
214 |
unapply: Properties.T => Option[Properties.T] |
|
215 |
): (String, Session.Protocol_Function) = {
|
|
216 |
name -> ((msg: Prover.Protocol_Output) => |
|
217 |
unapply(msg.properties) match {
|
|
218 |
case Some(props) => acc += props; true |
|
219 |
case _ => false |
|
220 |
}) |
|
| 77485 | 221 |
} |
222 |
||
| 78372 | 223 |
session.init_protocol_handler(new Session.Protocol_Handler {
|
224 |
override def exit(): Unit = Build_Session_Errors.cancel() |
|
225 |
||
226 |
private def build_session_finished(msg: Prover.Protocol_Output): Boolean = {
|
|
227 |
val (rc, errors) = |
|
228 |
try {
|
|
229 |
val (rc, errs) = {
|
|
230 |
import XML.Decode._ |
|
231 |
pair(int, list(x => x))(Symbol.decode_yxml(msg.text)) |
|
232 |
} |
|
233 |
val errors = |
|
234 |
for (err <- errs) yield {
|
|
235 |
val prt = Protocol_Message.expose_no_reports(err) |
|
236 |
Pretty.string_of(prt, metric = Symbol.Metric) |
|
237 |
} |
|
238 |
(rc, errors) |
|
239 |
} |
|
240 |
catch { case ERROR(err) => (Process_Result.RC.failure, List(err)) }
|
|
241 |
||
242 |
session.protocol_command("Prover.stop", rc.toString)
|
|
243 |
Build_Session_Errors(errors) |
|
244 |
true |
|
245 |
} |
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
246 |
|
| 78372 | 247 |
private def loading_theory(msg: Prover.Protocol_Output): Boolean = |
248 |
msg.properties match {
|
|
249 |
case Markup.Loading_Theory(Markup.Name(name)) => |
|
250 |
progress.theory(Progress.Theory(name, session = session_name)) |
|
251 |
false |
|
252 |
case _ => false |
|
253 |
} |
|
254 |
||
255 |
private def export_(msg: Prover.Protocol_Output): Boolean = |
|
256 |
msg.properties match {
|
|
257 |
case Protocol.Export(args) => |
|
258 |
export_consumer.make_entry(session_name, args, msg.chunk) |
|
259 |
true |
|
260 |
case _ => false |
|
261 |
} |
|
262 |
||
263 |
override val functions: Session.Protocol_Functions = |
|
264 |
List( |
|
265 |
Markup.Build_Session_Finished.name -> build_session_finished, |
|
266 |
Markup.Loading_Theory.name -> loading_theory, |
|
267 |
Markup.EXPORT -> export_, |
|
268 |
fun(Markup.Theory_Timing.name, theory_timings, Markup.Theory_Timing.unapply), |
|
269 |
fun(Markup.Session_Timing.name, session_timings, Markup.Session_Timing.unapply), |
|
270 |
fun(Markup.Task_Statistics.name, task_statistics, Markup.Task_Statistics.unapply)) |
|
271 |
}) |
|
272 |
||
273 |
session.command_timings += Session.Consumer("command_timings") {
|
|
274 |
case Session.Command_Timing(props) => |
|
275 |
for {
|
|
276 |
elapsed <- Markup.Elapsed.unapply(props) |
|
277 |
elapsed_time = Time.seconds(elapsed) |
|
| 79692 | 278 |
if elapsed_time.is_relevant && |
279 |
elapsed_time >= options.seconds("command_timing_threshold")
|
|
| 78372 | 280 |
} command_timings += props.filter(Markup.command_timing_property) |
281 |
} |
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
282 |
|
| 78372 | 283 |
session.runtime_statistics += Session.Consumer("ML_statistics") {
|
284 |
case Session.Runtime_Statistics(props) => runtime_statistics += props |
|
285 |
} |
|
286 |
||
287 |
session.finished_theories += Session.Consumer[Document.Snapshot]("finished_theories") {
|
|
288 |
case snapshot => |
|
289 |
if (!progress.stopped) {
|
|
290 |
def export_(name: String, xml: XML.Body, compress: Boolean = true): Unit = {
|
|
291 |
if (!progress.stopped) {
|
|
292 |
val theory_name = snapshot.node_name.theory |
|
293 |
val args = |
|
| 79692 | 294 |
Protocol.Export.Args( |
295 |
theory_name = theory_name, name = name, compress = compress) |
|
| 78372 | 296 |
val body = Bytes(Symbol.encode(YXML.string_of_body(xml))) |
297 |
export_consumer.make_entry(session_name, args, body) |
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
298 |
} |
| 78372 | 299 |
} |
300 |
def export_text(name: String, text: String, compress: Boolean = true): Unit = |
|
301 |
export_(name, List(XML.Text(text)), compress = compress) |
|
302 |
||
303 |
for (command <- snapshot.snippet_command) {
|
|
304 |
export_text(Export.DOCUMENT_ID, command.id.toString, compress = false) |
|
305 |
} |
|
306 |
||
307 |
export_text(Export.FILES, |
|
308 |
cat_lines(snapshot.node_files.map(name => File.symbolic_path(name.path))), |
|
309 |
compress = false) |
|
310 |
||
311 |
for ((blob_name, i) <- snapshot.node_files.tail.zipWithIndex) {
|
|
312 |
val xml = snapshot.switch(blob_name).xml_markup() |
|
313 |
export_(Export.MARKUP + (i + 1), xml) |
|
314 |
} |
|
315 |
export_(Export.MARKUP, snapshot.xml_markup()) |
|
316 |
export_(Export.MESSAGES, snapshot.messages.map(_._1)) |
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
317 |
} |
|
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
318 |
} |
| 78372 | 319 |
|
320 |
session.all_messages += Session.Consumer[Any]("build_session_output") {
|
|
321 |
case msg: Prover.Output => |
|
322 |
val message = msg.message |
|
323 |
if (msg.is_system) resources.log(Protocol.message_text(message)) |
|
324 |
||
325 |
if (msg.is_stdout) {
|
|
326 |
stdout ++= Symbol.encode(XML.content(message)) |
|
327 |
} |
|
328 |
else if (msg.is_stderr) {
|
|
329 |
stderr ++= Symbol.encode(XML.content(message)) |
|
330 |
} |
|
331 |
else if (msg.is_exit) {
|
|
332 |
val err = |
|
333 |
"Prover terminated" + |
|
334 |
(msg.properties match {
|
|
335 |
case Markup.Process_Result(result) => ": " + result.print_rc |
|
336 |
case _ => "" |
|
337 |
}) |
|
338 |
Build_Session_Errors(List(err)) |
|
339 |
} |
|
340 |
case _ => |
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
341 |
} |
|
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
342 |
|
| 78372 | 343 |
build_context.session_setup(session_name, session) |
344 |
||
345 |
val eval_main = Command_Line.ML_tool("Isabelle_Process.init_build ()" :: eval_store)
|
|
346 |
||
|
77486
032c76e04475
clarified execution context: main work happens within Future.thread;
wenzelm
parents:
77485
diff
changeset
|
347 |
|
| 78372 | 348 |
/* process */ |
349 |
||
350 |
val process = |
|
351 |
Isabelle_Process.start(options, session, session_background, session_heaps, |
|
352 |
use_prelude = use_prelude, eval_main = eval_main, cwd = info.dir.file, env = env) |
|
353 |
||
354 |
val timeout_request: Option[Event_Timer.Request] = |
|
355 |
if (info.timeout_ignored) None |
|
356 |
else Some(Event_Timer.request(Time.now() + info.timeout) { process.terminate() })
|
|
|
77486
032c76e04475
clarified execution context: main work happens within Future.thread;
wenzelm
parents:
77485
diff
changeset
|
357 |
|
| 78372 | 358 |
val build_errors = |
359 |
Isabelle_Thread.interrupt_handler(_ => process.terminate()) {
|
|
360 |
Exn.capture { process.await_startup() } match {
|
|
361 |
case Exn.Res(_) => |
|
362 |
val resources_yxml = resources.init_session_yxml |
|
363 |
val encode_options: XML.Encode.T[Options] = |
|
364 |
options => session.prover_options(options).encode |
|
365 |
val args_yxml = |
|
366 |
YXML.string_of_body( |
|
367 |
{
|
|
368 |
import XML.Encode._ |
|
369 |
pair(string, list(pair(encode_options, list(pair(string, properties)))))( |
|
370 |
(session_name, info.theories)) |
|
371 |
}) |
|
372 |
session.protocol_command("build_session", resources_yxml, args_yxml)
|
|
373 |
Build_Session_Errors.result |
|
374 |
case Exn.Exn(exn) => Exn.Res(List(Exn.message(exn))) |
|
375 |
} |
|
376 |
} |
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
377 |
|
| 78372 | 378 |
val result0 = |
379 |
Isabelle_Thread.interrupt_handler(_ => process.terminate()) { process.await_shutdown() }
|
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
380 |
|
| 78372 | 381 |
val was_timeout = |
382 |
timeout_request match {
|
|
383 |
case None => false |
|
384 |
case Some(request) => !request.cancel() |
|
385 |
} |
|
386 |
||
387 |
session.stop() |
|
388 |
||
389 |
val export_errors = |
|
390 |
export_consumer.shutdown(close = true).map(Output.error_message_text) |
|
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
391 |
|
| 78372 | 392 |
val (document_output, document_errors) = |
393 |
try {
|
|
| 78674 | 394 |
if (Exn.is_res(build_errors) && result0.ok && info.documents.nonEmpty) {
|
| 78379 | 395 |
using(Export.open_database_context(store, server = server)) { database_context =>
|
| 78372 | 396 |
val documents = |
397 |
using(database_context.open_session(session_background)) {
|
|
398 |
session_context => |
|
399 |
Document_Build.build_documents( |
|
400 |
Document_Build.context(session_context, progress = progress), |
|
401 |
output_sources = info.document_output, |
|
402 |
output_pdf = info.document_output) |
|
403 |
} |
|
| 79692 | 404 |
using(database_context.open_database(session_name, output = true))( |
405 |
session_database => |
|
406 |
documents.foreach(_.write(session_database.db, session_name))) |
|
| 78372 | 407 |
(documents.flatMap(_.log_lines), Nil) |
408 |
} |
|
| 77484 | 409 |
} |
| 78372 | 410 |
else (Nil, Nil) |
411 |
} |
|
412 |
catch {
|
|
413 |
case exn: Document_Build.Build_Error => (exn.log_lines, exn.log_errors) |
|
414 |
case Exn.Interrupt.ERROR(msg) => (Nil, List(msg)) |
|
415 |
} |
|
| 77485 | 416 |
|
|
77486
032c76e04475
clarified execution context: main work happens within Future.thread;
wenzelm
parents:
77485
diff
changeset
|
417 |
|
| 78372 | 418 |
/* process result */ |
|
77486
032c76e04475
clarified execution context: main work happens within Future.thread;
wenzelm
parents:
77485
diff
changeset
|
419 |
|
| 78372 | 420 |
val result1 = {
|
421 |
val theory_timing = |
|
422 |
theory_timings.iterator.flatMap( |
|
423 |
{
|
|
424 |
case props @ Markup.Name(name) => Some(name -> props) |
|
425 |
case _ => None |
|
426 |
}).toMap |
|
427 |
val used_theory_timings = |
|
428 |
for { (name, _) <- session_background.base.used_theories }
|
|
429 |
yield theory_timing.getOrElse(name.theory, Markup.Name(name.theory)) |
|
|
77486
032c76e04475
clarified execution context: main work happens within Future.thread;
wenzelm
parents:
77485
diff
changeset
|
430 |
|
| 78372 | 431 |
val more_output = |
432 |
Library.trim_line(stdout.toString) :: |
|
433 |
command_timings.toList.map(Protocol.Command_Timing_Marker.apply) ::: |
|
434 |
used_theory_timings.map(Protocol.Theory_Timing_Marker.apply) ::: |
|
435 |
session_timings.toList.map(Protocol.Session_Timing_Marker.apply) ::: |
|
436 |
runtime_statistics.toList.map(Protocol.ML_Statistics_Marker.apply) ::: |
|
437 |
task_statistics.toList.map(Protocol.Task_Statistics_Marker.apply) ::: |
|
438 |
document_output |
|
|
77486
032c76e04475
clarified execution context: main work happens within Future.thread;
wenzelm
parents:
77485
diff
changeset
|
439 |
|
| 78372 | 440 |
result0.output(more_output) |
441 |
.error(Library.trim_line(stderr.toString)) |
|
442 |
.errors_rc(export_errors ::: document_errors) |
|
443 |
} |
|
|
77486
032c76e04475
clarified execution context: main work happens within Future.thread;
wenzelm
parents:
77485
diff
changeset
|
444 |
|
| 78372 | 445 |
val result2 = |
446 |
build_errors match {
|
|
447 |
case Exn.Res(build_errs) => |
|
448 |
val errs = build_errs ::: document_errors |
|
449 |
if (errs.nonEmpty) {
|
|
450 |
result1.error_rc.output( |
|
451 |
errs.flatMap(s => split_lines(Output.error_message_text(s))) ::: |
|
452 |
errs.map(Protocol.Error_Message_Marker.apply)) |
|
453 |
} |
|
| 79692 | 454 |
else if (progress.stopped && result1.ok) {
|
455 |
result1.copy(rc = Process_Result.RC.interrupt) |
|
456 |
} |
|
| 78372 | 457 |
else result1 |
458 |
case Exn.Exn(Exn.Interrupt()) => |
|
459 |
if (result1.ok) result1.copy(rc = Process_Result.RC.interrupt) |
|
460 |
else result1 |
|
461 |
case Exn.Exn(exn) => throw exn |
|
462 |
} |
|
463 |
||
464 |
val process_result = |
|
465 |
if (result2.ok) result2 |
|
466 |
else if (was_timeout) result2.error(Output.error_message_text("Timeout")).timeout_rc
|
|
467 |
else if (result2.interrupted) result2.error(Output.error_message_text("Interrupt"))
|
|
468 |
else result2 |
|
469 |
||
|
79691
d298c5b65d8e
clarified store_session: heap requires process_result.ok, but log_db is always stored;
wenzelm
parents:
79682
diff
changeset
|
470 |
val store_session = |
|
d298c5b65d8e
clarified store_session: heap requires process_result.ok, but log_db is always stored;
wenzelm
parents:
79682
diff
changeset
|
471 |
store.output_session(session_name, store_heap = process_result.ok && store_heap) |
|
d298c5b65d8e
clarified store_session: heap requires process_result.ok, but log_db is always stored;
wenzelm
parents:
79682
diff
changeset
|
472 |
|
| 78372 | 473 |
|
474 |
/* output heap */ |
|
|
77486
032c76e04475
clarified execution context: main work happens within Future.thread;
wenzelm
parents:
77485
diff
changeset
|
475 |
|
|
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79678
diff
changeset
|
476 |
val output_shasum = |
|
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79678
diff
changeset
|
477 |
store_session.heap match {
|
|
79691
d298c5b65d8e
clarified store_session: heap requires process_result.ok, but log_db is always stored;
wenzelm
parents:
79682
diff
changeset
|
478 |
case Some(path) => SHA1.shasum(ML_Heap.write_file_digest(path), session_name) |
|
d298c5b65d8e
clarified store_session: heap requires process_result.ok, but log_db is always stored;
wenzelm
parents:
79682
diff
changeset
|
479 |
case None => SHA1.no_shasum |
| 78372 | 480 |
} |
481 |
||
482 |
val log_lines = process_result.out_lines.filterNot(Protocol_Message.Marker.test) |
|
|
77486
032c76e04475
clarified execution context: main work happens within Future.thread;
wenzelm
parents:
77485
diff
changeset
|
483 |
|
| 78372 | 484 |
val build_log = |
|
78985
24e686fe043e
clarified Log_File.cache: reuse existing Store.cache / Build_Log.Store.cache;
wenzelm
parents:
78842
diff
changeset
|
485 |
Build_Log.Log_File(session_name, process_result.out_lines, cache = store.cache). |
| 78372 | 486 |
parse_session_info( |
487 |
command_timings = true, |
|
488 |
theory_timings = true, |
|
489 |
ml_statistics = true, |
|
490 |
task_statistics = true) |
|
491 |
||
492 |
// write log file |
|
493 |
if (process_result.ok) {
|
|
494 |
File.write_gzip(store.output_log_gz(session_name), terminate_lines(log_lines)) |
|
495 |
} |
|
496 |
else File.write(store.output_log(session_name), terminate_lines(log_lines)) |
|
|
77486
032c76e04475
clarified execution context: main work happens within Future.thread;
wenzelm
parents:
77485
diff
changeset
|
497 |
|
| 78372 | 498 |
// write database |
| 78374 | 499 |
def write_info(db: SQL.Database): Unit = |
| 78372 | 500 |
store.write_session_info(db, session_name, session_sources, |
501 |
build_log = |
|
502 |
if (process_result.timeout) build_log.error("Timeout") else build_log,
|
|
503 |
build = |
|
504 |
Store.Build_Info( |
|
505 |
sources = sources_shasum, |
|
506 |
input_heaps = input_shasum, |
|
507 |
output_heap = output_shasum, |
|
508 |
process_result.rc, |
|
| 78374 | 509 |
build_context.build_uuid)) |
|
78529
0e79fa88cab6
build_worker is stopped independently from master build_process;
wenzelm
parents:
78435
diff
changeset
|
510 |
|
|
0e79fa88cab6
build_worker is stopped independently from master build_process;
wenzelm
parents:
78435
diff
changeset
|
511 |
val valid = |
|
0e79fa88cab6
build_worker is stopped independently from master build_process;
wenzelm
parents:
78435
diff
changeset
|
512 |
if (progress.stopped_local) false |
|
0e79fa88cab6
build_worker is stopped independently from master build_process;
wenzelm
parents:
78435
diff
changeset
|
513 |
else {
|
|
0e79fa88cab6
build_worker is stopped independently from master build_process;
wenzelm
parents:
78435
diff
changeset
|
514 |
database_server match {
|
|
0e79fa88cab6
build_worker is stopped independently from master build_process;
wenzelm
parents:
78435
diff
changeset
|
515 |
case Some(db) => write_info(db) |
|
0e79fa88cab6
build_worker is stopped independently from master build_process;
wenzelm
parents:
78435
diff
changeset
|
516 |
case None => using(store.open_database(session_name, output = true))(write_info) |
|
0e79fa88cab6
build_worker is stopped independently from master build_process;
wenzelm
parents:
78435
diff
changeset
|
517 |
} |
|
0e79fa88cab6
build_worker is stopped independently from master build_process;
wenzelm
parents:
78435
diff
changeset
|
518 |
true |
|
0e79fa88cab6
build_worker is stopped independently from master build_process;
wenzelm
parents:
78435
diff
changeset
|
519 |
} |
| 78372 | 520 |
|
|
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79678
diff
changeset
|
521 |
using_optional(store.maybe_open_heaps_database(database_server, server = server)) {
|
|
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79678
diff
changeset
|
522 |
heaps_database => |
|
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79678
diff
changeset
|
523 |
for (db <- database_server orElse heaps_database) {
|
|
79691
d298c5b65d8e
clarified store_session: heap requires process_result.ok, but log_db is always stored;
wenzelm
parents:
79682
diff
changeset
|
524 |
val slice = Space.MiB(options.real("build_database_slice"))
|
| 79693 | 525 |
ML_Heap.store(db, store_session, slice, |
526 |
cache = store.cache.compress, progress = progress) |
|
|
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79678
diff
changeset
|
527 |
} |
|
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79678
diff
changeset
|
528 |
} |
|
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79678
diff
changeset
|
529 |
|
| 78372 | 530 |
// messages |
531 |
process_result.err_lines.foreach(progress.echo(_)) |
|
532 |
||
533 |
if (process_result.ok) {
|
|
534 |
val props = build_log.session_timing |
|
535 |
val threads = Markup.Session_Timing.Threads.unapply(props) getOrElse 1 |
|
536 |
val timing = Markup.Timing_Properties.get(props) |
|
537 |
progress.echo( |
|
538 |
"Timing " + session_name + " (" + threads + " threads, " + timing.message_factor + ")",
|
|
539 |
verbose = true) |
|
540 |
progress.echo( |
|
541 |
"Finished " + session_name + " (" + process_result.timing.message_resources + ")")
|
|
542 |
} |
|
543 |
else {
|
|
544 |
progress.echo( |
|
| 79692 | 545 |
session_name + " FAILED (see also \"isabelle build_log -H Error " + |
546 |
session_name + "\")") |
|
| 78372 | 547 |
if (!process_result.interrupted) {
|
548 |
val tail = info.options.int("process_output_tail")
|
|
| 79692 | 549 |
val suffix = |
550 |
if (tail == 0) log_lines else log_lines.drop(log_lines.length - tail max 0) |
|
551 |
val prefix = |
|
552 |
if (log_lines.length == suffix.length) Nil else List("...")
|
|
| 78372 | 553 |
progress.echo(Library.trim_line(cat_lines(prefix ::: suffix))) |
554 |
} |
|
555 |
} |
|
556 |
||
| 78530 | 557 |
if (valid) Some(Result(process_result.copy(out_lines = log_lines), output_shasum)) |
558 |
else None |
|
|
77486
032c76e04475
clarified execution context: main work happens within Future.thread;
wenzelm
parents:
77485
diff
changeset
|
559 |
} |
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
560 |
} |
|
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
561 |
|
|
77486
032c76e04475
clarified execution context: main work happens within Future.thread;
wenzelm
parents:
77485
diff
changeset
|
562 |
override def cancel(): Unit = future_result.cancel() |
| 77298 | 563 |
override def is_finished: Boolean = future_result.is_finished |
| 78530 | 564 |
override def join: Option[Result] = future_result.join |
|
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77294
diff
changeset
|
565 |
} |
|
72848
d5d0e36eda16
read theory with PIDE markup from session database;
wenzelm
parents:
72844
diff
changeset
|
566 |
} |