author | wenzelm |
Wed, 12 Mar 2025 11:39:00 +0100 | |
changeset 82265 | 4b875a4c83b0 |
parent 82144 | e8959b23208b |
child 82794 | 3db393729a65 |
permissions | -rw-r--r-- |
56385 | 1 |
/* Title: Pure/PIDE/prover.scala |
2 |
Author: Makarius |
|
57923 | 3 |
Options: :folding=explicit: |
56385 | 4 |
|
57923 | 5 |
Prover process wrapping. |
56385 | 6 |
*/ |
7 |
||
8 |
package isabelle |
|
9 |
||
10 |
||
67835 | 11 |
import java.io.{InputStream, OutputStream, BufferedOutputStream, IOException} |
57915
448325de6e4f
more abstract Prover.System_Process, which allows to bypass Isabelle_System.Managed_Process;
wenzelm
parents:
57906
diff
changeset
|
12 |
|
448325de6e4f
more abstract Prover.System_Process, which allows to bypass Isabelle_System.Managed_Process;
wenzelm
parents:
57906
diff
changeset
|
13 |
|
75393 | 14 |
object Prover { |
56385 | 15 |
/* messages */ |
16 |
||
17 |
sealed abstract class Message |
|
62556
c115e69f457f
more abstract Session.start, without prover command-line;
wenzelm
parents:
62310
diff
changeset
|
18 |
type Receiver = Message => Unit |
56385 | 19 |
|
80462
7a1f9e571046
clarified signature: more explicit XML.Body types, more uniform Symbol.encode_yxml;
wenzelm
parents:
80357
diff
changeset
|
20 |
class Input(val name: String, val args: List[XML.Body]) extends Message { |
56385 | 21 |
override def toString: String = |
22 |
XML.Elem(Markup(Markup.PROVER_COMMAND, List((Markup.NAME, name))), |
|
80462
7a1f9e571046
clarified signature: more explicit XML.Body types, more uniform Symbol.encode_yxml;
wenzelm
parents:
80357
diff
changeset
|
23 |
args.flatMap(arg => List(XML.newline, XML.elem(Markup.PROVER_ARG, arg)))).toString |
56385 | 24 |
} |
25 |
||
75393 | 26 |
class Output(val message: XML.Elem) extends Message { |
56385 | 27 |
def kind: String = message.markup.name |
28 |
def properties: Properties.T = message.markup.properties |
|
29 |
def body: XML.Body = message.body |
|
30 |
||
71601 | 31 |
def is_init: Boolean = kind == Markup.INIT |
32 |
def is_exit: Boolean = kind == Markup.EXIT |
|
33 |
def is_stdout: Boolean = kind == Markup.STDOUT |
|
34 |
def is_stderr: Boolean = kind == Markup.STDERR |
|
35 |
def is_system: Boolean = kind == Markup.SYSTEM |
|
36 |
def is_status: Boolean = kind == Markup.STATUS |
|
37 |
def is_report: Boolean = kind == Markup.REPORT |
|
38 |
def is_syslog: Boolean = is_init || is_exit || is_system || is_stderr |
|
56385 | 39 |
|
75393 | 40 |
override def toString: String = { |
56385 | 41 |
val res = |
42 |
if (is_status || is_report) message.body.map(_.toString).mkString |
|
71649 | 43 |
else Pretty.string_of(message.body, metric = Symbol.Metric) |
56385 | 44 |
if (properties.isEmpty) |
73559 | 45 |
kind + " [[" + res + "]]" |
56385 | 46 |
else |
73559 | 47 |
kind + " " + |
73712 | 48 |
(properties.map(Properties.Eq.apply)).mkString("{", ",", "}") + " [[" + res + "]]" |
56385 | 49 |
} |
50 |
} |
|
51 |
||
73560 | 52 |
class Malformed(msg: String) extends Exn.User_Error("Malformed prover message: " + msg) |
53 |
def bad_header(print: String): Nothing = throw new Malformed("bad message header\n" + print) |
|
54 |
def bad_chunks(): Nothing = throw new Malformed("bad message chunks") |
|
73559 | 55 |
|
56 |
def the_chunk(chunks: List[Bytes], print: => String): Bytes = |
|
57 |
chunks match { |
|
58 |
case List(chunk) => chunk |
|
73560 | 59 |
case _ => throw new Malformed("single chunk expected: " + print) |
73559 | 60 |
} |
61 |
||
62 |
class Protocol_Output(props: Properties.T, val chunks: List[Bytes]) |
|
75393 | 63 |
extends Output(XML.Elem(Markup(Markup.PROTOCOL, props), Nil)) { |
73559 | 64 |
def chunk: Bytes = the_chunk(chunks, toString) |
65 |
lazy val text: String = chunk.text |
|
56385 | 66 |
} |
67 |
} |
|
68 |
||
56393
22f533e6a049
more abstract Prover.Syntax, as proposed by Carst Tankink;
wenzelm
parents:
56387
diff
changeset
|
69 |
|
65345
2fdd4431b30e
clarified YXML vs. symbol encoding: operate on whole message;
wenzelm
parents:
65317
diff
changeset
|
70 |
class Prover( |
62556
c115e69f457f
more abstract Session.start, without prover command-line;
wenzelm
parents:
62310
diff
changeset
|
71 |
receiver: Prover.Receiver, |
73031
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
71969
diff
changeset
|
72 |
cache: XML.Cache, |
65316 | 73 |
channel: System_Channel, |
75393 | 74 |
process: Bash.Process |
75 |
) extends Protocol { |
|
57923 | 76 |
/** receiver output **/ |
57916 | 77 |
|
75393 | 78 |
private def system_output(text: String): Unit = { |
57916 | 79 |
receiver(new Prover.Output(XML.Elem(Markup(Markup.SYSTEM, Nil), List(XML.Text(text))))) |
80 |
} |
|
81 |
||
75393 | 82 |
private def protocol_output(props: Properties.T, chunks: List[Bytes]): Unit = { |
73562 | 83 |
receiver(new Prover.Protocol_Output(props, chunks)) |
57916 | 84 |
} |
85 |
||
75393 | 86 |
private def output(kind: String, props: Properties.T, body: XML.Body): Unit = { |
59713 | 87 |
val main = XML.Elem(Markup(kind, props), Protocol_Message.clean_reports(body)) |
88 |
val reports = Protocol_Message.reports(props, body) |
|
73031
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
71969
diff
changeset
|
89 |
for (msg <- main :: reports) receiver(new Prover.Output(cache.elem(msg))) |
57916 | 90 |
} |
91 |
||
75393 | 92 |
private def exit_message(result: Process_Result): Unit = { |
65317 | 93 |
output(Markup.EXIT, Markup.Process_Result(result), |
71747 | 94 |
List(XML.Text(result.print_return_code))) |
57916 | 95 |
} |
96 |
||
97 |
||
56387 | 98 |
|
57916 | 99 |
/** process manager **/ |
100 |
||
65317 | 101 |
private val process_result: Future[Process_Result] = |
102 |
Future.thread("process_result") { |
|
74141 | 103 |
val rc = process.join() |
65317 | 104 |
val timing = process.get_timing |
105 |
Process_Result(rc, timing = timing) |
|
106 |
} |
|
57916 | 107 |
|
75393 | 108 |
private def terminate_process(): Unit = { |
73367 | 109 |
try { process.terminate() } |
57916 | 110 |
catch { |
111 |
case exn @ ERROR(_) => system_output("Failed to terminate prover process: " + exn.getMessage) |
|
112 |
} |
|
56387 | 113 |
} |
114 |
||
75393 | 115 |
private val process_manager = Isabelle_Thread.fork(name = "process_manager") { |
71641
c1409b9c2b22
proper startup for Pure: its use_prelude produces stdout before stderr protocol init;
wenzelm
parents:
71601
diff
changeset
|
116 |
val stdout = physical_output(false) |
c1409b9c2b22
proper startup for Pure: its use_prelude produces stdout before stderr protocol init;
wenzelm
parents:
71601
diff
changeset
|
117 |
|
75393 | 118 |
val (startup_failed, startup_errors) = { |
57916 | 119 |
var finished: Option[Boolean] = None |
120 |
val result = new StringBuilder(100) |
|
65316 | 121 |
while (finished.isEmpty && (process.stderr.ready || !process_result.is_finished)) { |
122 |
while (finished.isEmpty && process.stderr.ready) { |
|
57916 | 123 |
try { |
65316 | 124 |
val c = process.stderr.read |
57916 | 125 |
if (c == 2) finished = Some(true) |
126 |
else result += c.toChar |
|
127 |
} |
|
128 |
catch { case _: IOException => finished = Some(false) } |
|
129 |
} |
|
73702
7202e12cb324
tuned signature --- following hints by IntelliJ IDEA;
wenzelm
parents:
73562
diff
changeset
|
130 |
Time.seconds(0.05).sleep() |
57916 | 131 |
} |
132 |
(finished.isEmpty || !finished.get, result.toString.trim) |
|
133 |
} |
|
134 |
if (startup_errors != "") system_output(startup_errors) |
|
135 |
||
136 |
if (startup_failed) { |
|
137 |
terminate_process() |
|
138 |
process_result.join |
|
71641
c1409b9c2b22
proper startup for Pure: its use_prelude produces stdout before stderr protocol init;
wenzelm
parents:
71601
diff
changeset
|
139 |
stdout.join |
77243 | 140 |
exit_message(Process_Result.startup_failure) |
57916 | 141 |
} |
142 |
else { |
|
65316 | 143 |
val (command_stream, message_stream) = channel.rendezvous() |
57916 | 144 |
|
145 |
command_input_init(command_stream) |
|
146 |
val stderr = physical_output(true) |
|
147 |
val message = message_output(message_stream) |
|
148 |
||
65317 | 149 |
val result = process_result.join |
57916 | 150 |
system_output("process terminated") |
151 |
command_input_close() |
|
74140 | 152 |
for (thread <- List(stdout, stderr, message)) thread.join() |
57916 | 153 |
system_output("process_manager terminated") |
65317 | 154 |
exit_message(result) |
57916 | 155 |
} |
69572
09a6a7c04b45
more robust system channel via options that are private to the user;
wenzelm
parents:
68805
diff
changeset
|
156 |
channel.shutdown() |
57916 | 157 |
} |
158 |
||
159 |
||
160 |
/* management methods */ |
|
161 |
||
73340 | 162 |
def join(): Unit = process_manager.join() |
57916 | 163 |
|
75393 | 164 |
def terminate(): Unit = { |
62310
ab836dc7410e
more gentle termination (like Bash.multi_kill without signal) to give prover a chance to conclude;
wenzelm
parents:
62307
diff
changeset
|
165 |
system_output("Terminating prover process") |
57916 | 166 |
command_input_close() |
62310
ab836dc7410e
more gentle termination (like Bash.multi_kill without signal) to give prover a chance to conclude;
wenzelm
parents:
62307
diff
changeset
|
167 |
|
ab836dc7410e
more gentle termination (like Bash.multi_kill without signal) to give prover a chance to conclude;
wenzelm
parents:
62307
diff
changeset
|
168 |
var count = 10 |
ab836dc7410e
more gentle termination (like Bash.multi_kill without signal) to give prover a chance to conclude;
wenzelm
parents:
62307
diff
changeset
|
169 |
while (!process_result.is_finished && count > 0) { |
73702
7202e12cb324
tuned signature --- following hints by IntelliJ IDEA;
wenzelm
parents:
73562
diff
changeset
|
170 |
Time.seconds(0.1).sleep() |
62310
ab836dc7410e
more gentle termination (like Bash.multi_kill without signal) to give prover a chance to conclude;
wenzelm
parents:
62307
diff
changeset
|
171 |
count -= 1 |
ab836dc7410e
more gentle termination (like Bash.multi_kill without signal) to give prover a chance to conclude;
wenzelm
parents:
62307
diff
changeset
|
172 |
} |
ab836dc7410e
more gentle termination (like Bash.multi_kill without signal) to give prover a chance to conclude;
wenzelm
parents:
62307
diff
changeset
|
173 |
if (!process_result.is_finished) terminate_process() |
57916 | 174 |
} |
175 |
||
176 |
||
177 |
||
178 |
/** process streams **/ |
|
179 |
||
180 |
/* command input */ |
|
181 |
||
182 |
private var command_input: Option[Consumer_Thread[List[Bytes]]] = None |
|
183 |
||
73367 | 184 |
private def command_input_close(): Unit = command_input.foreach(_.shutdown()) |
57916 | 185 |
|
75393 | 186 |
private def command_input_init(raw_stream: OutputStream): Unit = { |
57916 | 187 |
val name = "command_input" |
188 |
val stream = new BufferedOutputStream(raw_stream) |
|
189 |
command_input = |
|
190 |
Some( |
|
191 |
Consumer_Thread.fork(name)( |
|
192 |
consume = |
|
193 |
{ |
|
194 |
case chunks => |
|
195 |
try { |
|
80357
fe123d033e76
clarified signature: pro-forma support for Bytes with size: Long;
wenzelm
parents:
77243
diff
changeset
|
196 |
Bytes(chunks.map(_.size).mkString("", ",", "\n")).write_stream(stream) |
64004 | 197 |
chunks.foreach(_.write_stream(stream)) |
57916 | 198 |
stream.flush |
199 |
true |
|
200 |
} |
|
201 |
catch { case e: IOException => system_output(name + ": " + e.getMessage); false } |
|
202 |
}, |
|
73367 | 203 |
finish = { case () => stream.close(); system_output(name + " terminated") } |
57916 | 204 |
) |
205 |
) |
|
206 |
} |
|
56387 | 207 |
|
208 |
||
57916 | 209 |
/* physical output */ |
210 |
||
75393 | 211 |
private def physical_output(err: Boolean): Thread = { |
57916 | 212 |
val (name, reader, markup) = |
65316 | 213 |
if (err) ("standard_error", process.stderr, Markup.STDERR) |
214 |
else ("standard_output", process.stdout, Markup.STDOUT) |
|
56387 | 215 |
|
71692 | 216 |
Isabelle_Thread.fork(name = name) { |
57916 | 217 |
try { |
82144 | 218 |
val result = new StringBuilder(100) |
57916 | 219 |
var finished = false |
220 |
while (!finished) { |
|
221 |
//{{{ |
|
222 |
var c = -1 |
|
223 |
var done = false |
|
71383 | 224 |
while (!done && (result.isEmpty || reader.ready)) { |
57916 | 225 |
c = reader.read |
226 |
if (c >= 0) result.append(c.asInstanceOf[Char]) |
|
227 |
else done = true |
|
228 |
} |
|
71383 | 229 |
if (result.nonEmpty) { |
65345
2fdd4431b30e
clarified YXML vs. symbol encoding: operate on whole message;
wenzelm
parents:
65317
diff
changeset
|
230 |
output(markup, Nil, List(XML.Text(Symbol.decode(result.toString)))) |
73367 | 231 |
result.clear() |
57916 | 232 |
} |
233 |
else { |
|
73367 | 234 |
reader.close() |
57916 | 235 |
finished = true |
236 |
} |
|
237 |
//}}} |
|
238 |
} |
|
239 |
} |
|
240 |
catch { case e: IOException => system_output(name + ": " + e.getMessage) } |
|
241 |
system_output(name + " terminated") |
|
242 |
} |
|
243 |
} |
|
56387 | 244 |
|
245 |
||
57916 | 246 |
/* message output */ |
247 |
||
75393 | 248 |
private def message_output(stream: InputStream): Thread = { |
80505
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
249 |
def decode_prop(bytes: Bytes): Properties.Entry = { |
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
250 |
val (a, b) = Properties.Eq.parse(bytes.text) |
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
251 |
(Symbol.decode(a), Symbol.decode(b)) |
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
252 |
} |
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
253 |
|
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
254 |
def decode_xml(bytes: Bytes): XML.Body = |
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
255 |
Symbol.decode_yxml_failsafe(bytes.text, cache = cache) |
56387 | 256 |
|
73559 | 257 |
val thread_name = "message_output" |
258 |
Isabelle_Thread.fork(name = thread_name) { |
|
259 |
try { |
|
260 |
var finished = false |
|
261 |
while (!finished) { |
|
262 |
Byte_Message.read_message(stream) match { |
|
263 |
case None => finished = true |
|
80505
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
264 |
case Some(k :: Value.Nat(props_length) :: rest) => |
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
265 |
val kind = k.text |
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
266 |
val props = rest.take(props_length).map(decode_prop) |
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
267 |
val chunks = rest.drop(props_length) |
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
268 |
if (kind == Markup.PROTOCOL) protocol_output(props, chunks) |
e3af424fdd1a
more robust message header: prefer explicit props_length/props_chunks over odd YXML.embed_controls;
wenzelm
parents:
80462
diff
changeset
|
269 |
else output(kind, props, chunks.flatMap(decode_xml)) |
73559 | 270 |
case Some(_) => Prover.bad_chunks() |
271 |
} |
|
57916 | 272 |
} |
273 |
} |
|
274 |
catch { |
|
275 |
case e: IOException => system_output("Cannot read message:\n" + e.getMessage) |
|
73560 | 276 |
case e: Prover.Malformed => system_output(e.getMessage) |
57916 | 277 |
} |
73367 | 278 |
stream.close() |
56387 | 279 |
|
73559 | 280 |
system_output(thread_name + " terminated") |
57916 | 281 |
} |
282 |
} |
|
283 |
||
284 |
||
285 |
||
286 |
/** protocol commands **/ |
|
287 |
||
70666 | 288 |
var trace: Boolean = false |
289 |
||
70661 | 290 |
def protocol_command_raw(name: String, args: List[Bytes]): Unit = |
57916 | 291 |
command_input match { |
74253 | 292 |
case Some(thread) if thread.is_active() => |
70666 | 293 |
if (trace) { |
80357
fe123d033e76
clarified signature: pro-forma support for Bytes with size: Long;
wenzelm
parents:
77243
diff
changeset
|
294 |
val payload = args.foldLeft(0L) { case (n, b) => n + b.size } |
70666 | 295 |
Output.writeln( |
296 |
"protocol_command " + name + ", args = " + args.length + ", payload = " + payload) |
|
297 |
} |
|
298 |
thread.send(Bytes(name) :: args) |
|
68805 | 299 |
case _ => error("Inactive prover input thread for command " + quote(name)) |
57916 | 300 |
} |
301 |
||
80462
7a1f9e571046
clarified signature: more explicit XML.Body types, more uniform Symbol.encode_yxml;
wenzelm
parents:
80357
diff
changeset
|
302 |
def protocol_command_args(name: String, args: List[XML.Body]): Unit = { |
70661 | 303 |
receiver(new Prover.Input(name, args)) |
80462
7a1f9e571046
clarified signature: more explicit XML.Body types, more uniform Symbol.encode_yxml;
wenzelm
parents:
80357
diff
changeset
|
304 |
protocol_command_raw(name, args.map(arg => Bytes(Symbol.encode_yxml(arg)))) |
57916 | 305 |
} |
70661 | 306 |
|
80462
7a1f9e571046
clarified signature: more explicit XML.Body types, more uniform Symbol.encode_yxml;
wenzelm
parents:
80357
diff
changeset
|
307 |
def protocol_command(name: String, args: XML.Body*): Unit = |
70661 | 308 |
protocol_command_args(name, args.toList) |
56387 | 309 |
} |