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