author | wenzelm |
Sat, 11 Feb 2023 20:54:24 +0100 | |
changeset 77246 | 173c2fb78290 |
parent 77245 | 1e2670d9dc18 |
child 77247 | 6ed94a0ec723 |
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 { |
18 |
def empty(session: String): Session_Context = new Session_Context(session, Time.zero, Nil) |
|
77238 | 19 |
|
77246 | 20 |
def apply( |
77239 | 21 |
session: String, |
22 |
store: Sessions.Store, |
|
23 |
progress: Progress = new Progress |
|
77244 | 24 |
): Session_Context = { |
77239 | 25 |
store.try_open_database(session) match { |
26 |
case None => empty(session) |
|
77238 | 27 |
case Some(db) => |
28 |
def ignore_error(msg: String) = { |
|
29 |
progress.echo_warning("Ignoring bad database " + db + |
|
77239 | 30 |
" for session " + quote(session) + (if (msg == "") "" else ":\n" + msg)) |
31 |
empty(session) |
|
77238 | 32 |
} |
33 |
try { |
|
77239 | 34 |
val command_timings = store.read_command_timings(db, session) |
35 |
val elapsed = |
|
36 |
store.read_session_timing(db, session) match { |
|
37 |
case Markup.Elapsed(s) => Time.seconds(s) |
|
38 |
case _ => Time.zero |
|
77238 | 39 |
} |
77244 | 40 |
new Session_Context(session, elapsed, command_timings) |
77238 | 41 |
} |
42 |
catch { |
|
43 |
case ERROR(msg) => ignore_error(msg) |
|
44 |
case exn: java.lang.Error => ignore_error(Exn.message(exn)) |
|
45 |
case _: XML.Error => ignore_error("XML.Error") |
|
46 |
} |
|
47 |
finally { db.close() } |
|
48 |
} |
|
49 |
} |
|
50 |
} |
|
77239 | 51 |
|
77244 | 52 |
final class Session_Context( |
77239 | 53 |
val session: String, |
77244 | 54 |
val old_time: Time, |
55 |
val old_command_timings: List[Properties.T] |
|
77239 | 56 |
) { |
77244 | 57 |
def is_empty: Boolean = old_time.is_zero && old_command_timings.isEmpty |
77240 | 58 |
|
77245
1e2670d9dc18
tuned message: old_time not sufficiently prominent nor accurate to be printed;
wenzelm
parents:
77244
diff
changeset
|
59 |
override def toString: String = session |
77239 | 60 |
} |
77246 | 61 |
|
62 |
object Context { |
|
63 |
private def make_session_timing( |
|
64 |
sessions_structure: Sessions.Structure, |
|
65 |
timing: Map[String, Double] |
|
66 |
) : Map[String, Double] = { |
|
67 |
val maximals = sessions_structure.build_graph.maximals.toSet |
|
68 |
def desc_timing(session_name: String): Double = { |
|
69 |
if (maximals.contains(session_name)) timing(session_name) |
|
70 |
else { |
|
71 |
val descendants = sessions_structure.build_descendants(List(session_name)).toSet |
|
72 |
val g = sessions_structure.build_graph.restrict(descendants) |
|
73 |
(0.0 :: g.maximals.flatMap { desc => |
|
74 |
val ps = g.all_preds(List(desc)) |
|
75 |
if (ps.exists(p => !timing.isDefinedAt(p))) None |
|
76 |
else Some(ps.map(timing(_)).sum) |
|
77 |
}).max |
|
78 |
} |
|
79 |
} |
|
80 |
timing.keySet.iterator.map(name => (name -> desc_timing(name))).toMap.withDefaultValue(0.0) |
|
81 |
} |
|
82 |
||
83 |
def apply( |
|
84 |
sessions_structure: Sessions.Structure, |
|
85 |
store: Sessions.Store, |
|
86 |
progress: Progress = new Progress |
|
87 |
): Context = { |
|
88 |
val sessions = |
|
89 |
Map.from( |
|
90 |
for (session <- sessions_structure.build_graph.keys_iterator) |
|
91 |
yield session -> Build_Process.Session_Context(session, store, progress = progress)) |
|
92 |
||
93 |
val session_timing = |
|
94 |
make_session_timing(sessions_structure, |
|
95 |
Map.from(for ((name, context) <- sessions.iterator) yield name -> context.old_time.seconds)) |
|
96 |
||
97 |
val ordering = |
|
98 |
new Ordering[String] { |
|
99 |
def compare(name1: String, name2: String): Int = |
|
100 |
session_timing(name2) compare session_timing(name1) match { |
|
101 |
case 0 => |
|
102 |
sessions_structure(name2).timeout compare sessions_structure(name1).timeout match { |
|
103 |
case 0 => name1 compare name2 |
|
104 |
case ord => ord |
|
105 |
} |
|
106 |
case ord => ord |
|
107 |
} |
|
108 |
} |
|
109 |
||
110 |
new Context(sessions, ordering) |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
final class Context private( |
|
115 |
sessions: Map[String, Session_Context], |
|
116 |
val ordering: Ordering[String] |
|
117 |
) { |
|
118 |
def apply(session: String): Session_Context = |
|
119 |
sessions.getOrElse(session, Session_Context.empty(session)) |
|
120 |
} |
|
77238 | 121 |
} |