author | wenzelm |
Sat, 11 Feb 2023 22:59:23 +0100 | |
changeset 77254 | 8d34f53871b4 |
parent 77249 | f3f1b7ad1d0d |
child 77255 | b810e99b5afb |
permissions | -rw-r--r-- |
77238 | 1 |
/* Title: Pure/Tools/build_process.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Build process for sessions, with build database, optional heap, and |
|
5 |
optional presentation. |
|
6 |
*/ |
|
7 |
||
8 |
package isabelle |
|
9 |
||
10 |
||
77246 | 11 |
import scala.math.Ordering |
12 |
||
13 |
||
77238 | 14 |
object Build_Process { |
77244 | 15 |
/* static information */ |
77238 | 16 |
|
77244 | 17 |
object Session_Context { |
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
18 |
def empty(session: String, timeout: Time): Session_Context = |
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
19 |
new Session_Context(session, timeout, Time.zero, Nil) |
77238 | 20 |
|
77246 | 21 |
def apply( |
77239 | 22 |
session: String, |
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
23 |
timeout: Time, |
77239 | 24 |
store: Sessions.Store, |
25 |
progress: Progress = new Progress |
|
77244 | 26 |
): Session_Context = { |
77239 | 27 |
store.try_open_database(session) match { |
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
28 |
case None => empty(session, timeout) |
77238 | 29 |
case Some(db) => |
30 |
def ignore_error(msg: String) = { |
|
31 |
progress.echo_warning("Ignoring bad database " + db + |
|
77239 | 32 |
" for session " + quote(session) + (if (msg == "") "" else ":\n" + msg)) |
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
33 |
empty(session, timeout) |
77238 | 34 |
} |
35 |
try { |
|
77239 | 36 |
val command_timings = store.read_command_timings(db, session) |
37 |
val elapsed = |
|
38 |
store.read_session_timing(db, session) match { |
|
39 |
case Markup.Elapsed(s) => Time.seconds(s) |
|
40 |
case _ => Time.zero |
|
77238 | 41 |
} |
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
42 |
new Session_Context(session, timeout, elapsed, command_timings) |
77238 | 43 |
} |
44 |
catch { |
|
45 |
case ERROR(msg) => ignore_error(msg) |
|
46 |
case exn: java.lang.Error => ignore_error(Exn.message(exn)) |
|
47 |
case _: XML.Error => ignore_error("XML.Error") |
|
48 |
} |
|
49 |
finally { db.close() } |
|
50 |
} |
|
51 |
} |
|
52 |
} |
|
77239 | 53 |
|
77244 | 54 |
final class Session_Context( |
77239 | 55 |
val session: String, |
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
56 |
val timeout: Time, |
77244 | 57 |
val old_time: Time, |
58 |
val old_command_timings: List[Properties.T] |
|
77239 | 59 |
) { |
77244 | 60 |
def is_empty: Boolean = old_time.is_zero && old_command_timings.isEmpty |
77240 | 61 |
|
77245
1e2670d9dc18
tuned message: old_time not sufficiently prominent nor accurate to be printed;
wenzelm
parents:
77244
diff
changeset
|
62 |
override def toString: String = session |
77239 | 63 |
} |
77246 | 64 |
|
65 |
object Context { |
|
66 |
def apply( |
|
67 |
sessions_structure: Sessions.Structure, |
|
68 |
store: Sessions.Store, |
|
69 |
progress: Progress = new Progress |
|
70 |
): Context = { |
|
77247 | 71 |
val build_graph = sessions_structure.build_graph |
72 |
||
77246 | 73 |
val sessions = |
74 |
Map.from( |
|
77247 | 75 |
for (name <- build_graph.keys_iterator) |
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
76 |
yield { |
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
77 |
val timeout = sessions_structure(name).timeout |
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
78 |
name -> Build_Process.Session_Context(name, timeout, store, progress = progress) |
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
79 |
}) |
77247 | 80 |
|
77248 | 81 |
val sessions_time = { |
82 |
val maximals = build_graph.maximals.toSet |
|
83 |
def descendants_time(name: String): Double = { |
|
84 |
if (maximals.contains(name)) sessions(name).old_time.seconds |
|
85 |
else { |
|
86 |
val descendants = build_graph.all_succs(List(name)).toSet |
|
87 |
val g = build_graph.restrict(descendants) |
|
88 |
(0.0 :: g.maximals.flatMap { desc => |
|
89 |
val ps = g.all_preds(List(desc)) |
|
90 |
if (ps.exists(p => !sessions.isDefinedAt(p))) None |
|
91 |
else Some(ps.map(p => sessions(p).old_time.seconds).sum) |
|
92 |
}).max |
|
93 |
} |
|
77247 | 94 |
} |
77248 | 95 |
Map.from( |
96 |
for (name <- sessions.keysIterator) |
|
97 |
yield name -> descendants_time(name)).withDefaultValue(0.0) |
|
77247 | 98 |
} |
99 |
||
77246 | 100 |
val ordering = |
101 |
new Ordering[String] { |
|
102 |
def compare(name1: String, name2: String): Int = |
|
77248 | 103 |
sessions_time(name2) compare sessions_time(name1) match { |
77246 | 104 |
case 0 => |
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
105 |
sessions(name2).timeout compare sessions(name1).timeout match { |
77246 | 106 |
case 0 => name1 compare name2 |
107 |
case ord => ord |
|
108 |
} |
|
109 |
case ord => ord |
|
110 |
} |
|
111 |
} |
|
112 |
||
77254
8d34f53871b4
clarified signature: make dynamic Queue from static Context;
wenzelm
parents:
77249
diff
changeset
|
113 |
new Context(sessions_structure, sessions, ordering) |
77246 | 114 |
} |
115 |
} |
|
116 |
||
117 |
final class Context private( |
|
77254
8d34f53871b4
clarified signature: make dynamic Queue from static Context;
wenzelm
parents:
77249
diff
changeset
|
118 |
val sessions_structure: Sessions.Structure, |
77246 | 119 |
sessions: Map[String, Session_Context], |
120 |
val ordering: Ordering[String] |
|
121 |
) { |
|
122 |
def apply(session: String): Session_Context = |
|
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
123 |
sessions.getOrElse(session, Session_Context.empty(session, Time.zero)) |
77246 | 124 |
} |
77238 | 125 |
} |