author | wenzelm |
Sun, 26 Feb 2023 21:55:20 +0100 | |
changeset 77387 | cd10b8edfdf5 |
parent 77385 | 4a7c42c84743 |
child 77390 | ff43a524aa5d |
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 |
77257 | 12 |
import scala.annotation.tailrec |
77246 | 13 |
|
14 |
||
77238 | 15 |
object Build_Process { |
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
16 |
/* static context */ |
77238 | 17 |
|
77244 | 18 |
object Session_Context { |
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
19 |
def empty(session: String, timeout: Time): Session_Context = |
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
20 |
new Session_Context(session, timeout, Time.zero, Nil) |
77238 | 21 |
|
77246 | 22 |
def apply( |
77239 | 23 |
session: String, |
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
24 |
timeout: Time, |
77239 | 25 |
store: Sessions.Store, |
26 |
progress: Progress = new Progress |
|
77244 | 27 |
): Session_Context = { |
77239 | 28 |
store.try_open_database(session) match { |
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
29 |
case None => empty(session, timeout) |
77238 | 30 |
case Some(db) => |
31 |
def ignore_error(msg: String) = { |
|
32 |
progress.echo_warning("Ignoring bad database " + db + |
|
77239 | 33 |
" for session " + quote(session) + (if (msg == "") "" else ":\n" + msg)) |
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
34 |
empty(session, timeout) |
77238 | 35 |
} |
36 |
try { |
|
77239 | 37 |
val command_timings = store.read_command_timings(db, session) |
38 |
val elapsed = |
|
39 |
store.read_session_timing(db, session) match { |
|
40 |
case Markup.Elapsed(s) => Time.seconds(s) |
|
41 |
case _ => Time.zero |
|
77238 | 42 |
} |
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
43 |
new Session_Context(session, timeout, elapsed, command_timings) |
77238 | 44 |
} |
45 |
catch { |
|
46 |
case ERROR(msg) => ignore_error(msg) |
|
47 |
case exn: java.lang.Error => ignore_error(Exn.message(exn)) |
|
48 |
case _: XML.Error => ignore_error("XML.Error") |
|
49 |
} |
|
50 |
finally { db.close() } |
|
51 |
} |
|
52 |
} |
|
53 |
} |
|
77239 | 54 |
|
77244 | 55 |
final class Session_Context( |
77239 | 56 |
val session: String, |
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
57 |
val timeout: Time, |
77244 | 58 |
val old_time: Time, |
59 |
val old_command_timings: List[Properties.T] |
|
77239 | 60 |
) { |
77244 | 61 |
def is_empty: Boolean = old_time.is_zero && old_command_timings.isEmpty |
77240 | 62 |
|
77245
1e2670d9dc18
tuned message: old_time not sufficiently prominent nor accurate to be printed;
wenzelm
parents:
77244
diff
changeset
|
63 |
override def toString: String = session |
77239 | 64 |
} |
77246 | 65 |
|
66 |
object Context { |
|
67 |
def apply( |
|
68 |
store: Sessions.Store, |
|
77257 | 69 |
deps: Sessions.Deps, |
77315
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
70 |
progress: Progress = new Progress, |
77378
f047804f4860
clarified Build_Process.Context: cover all static information;
wenzelm
parents:
77375
diff
changeset
|
71 |
hostname: String = Isabelle_System.hostname(), |
f047804f4860
clarified Build_Process.Context: cover all static information;
wenzelm
parents:
77375
diff
changeset
|
72 |
numa_shuffling: Boolean = false, |
77315
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
73 |
build_heap: Boolean = false, |
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
74 |
max_jobs: Int = 1, |
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
75 |
fresh_build: Boolean = false, |
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
76 |
no_build: Boolean = false, |
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
77 |
verbose: Boolean = false, |
77378
f047804f4860
clarified Build_Process.Context: cover all static information;
wenzelm
parents:
77375
diff
changeset
|
78 |
session_setup: (String, Session) => Unit = (_, _) => (), |
f047804f4860
clarified Build_Process.Context: cover all static information;
wenzelm
parents:
77375
diff
changeset
|
79 |
instance: String = UUID.random().toString |
77246 | 80 |
): Context = { |
77257 | 81 |
val sessions_structure = deps.sessions_structure |
77247 | 82 |
val build_graph = sessions_structure.build_graph |
83 |
||
77246 | 84 |
val sessions = |
85 |
Map.from( |
|
77247 | 86 |
for (name <- build_graph.keys_iterator) |
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
87 |
yield { |
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
88 |
val timeout = sessions_structure(name).timeout |
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
89 |
name -> Build_Process.Session_Context(name, timeout, store, progress = progress) |
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
90 |
}) |
77247 | 91 |
|
77248 | 92 |
val sessions_time = { |
93 |
val maximals = build_graph.maximals.toSet |
|
94 |
def descendants_time(name: String): Double = { |
|
95 |
if (maximals.contains(name)) sessions(name).old_time.seconds |
|
96 |
else { |
|
97 |
val descendants = build_graph.all_succs(List(name)).toSet |
|
98 |
val g = build_graph.restrict(descendants) |
|
99 |
(0.0 :: g.maximals.flatMap { desc => |
|
100 |
val ps = g.all_preds(List(desc)) |
|
101 |
if (ps.exists(p => !sessions.isDefinedAt(p))) None |
|
102 |
else Some(ps.map(p => sessions(p).old_time.seconds).sum) |
|
103 |
}).max |
|
104 |
} |
|
77247 | 105 |
} |
77248 | 106 |
Map.from( |
107 |
for (name <- sessions.keysIterator) |
|
108 |
yield name -> descendants_time(name)).withDefaultValue(0.0) |
|
77247 | 109 |
} |
110 |
||
77246 | 111 |
val ordering = |
112 |
new Ordering[String] { |
|
113 |
def compare(name1: String, name2: String): Int = |
|
77248 | 114 |
sessions_time(name2) compare sessions_time(name1) match { |
77246 | 115 |
case 0 => |
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
116 |
sessions(name2).timeout compare sessions(name1).timeout match { |
77246 | 117 |
case 0 => name1 compare name2 |
118 |
case ord => ord |
|
119 |
} |
|
120 |
case ord => ord |
|
121 |
} |
|
122 |
} |
|
123 |
||
77329 | 124 |
val numa_nodes = NUMA.nodes(enabled = numa_shuffling) |
77378
f047804f4860
clarified Build_Process.Context: cover all static information;
wenzelm
parents:
77375
diff
changeset
|
125 |
new Context(instance, store, deps, sessions, ordering, progress, hostname, numa_nodes, |
77317
b8ec3c0455db
clarified modules: NUMA is managed by Build_Process;
wenzelm
parents:
77315
diff
changeset
|
126 |
build_heap = build_heap, max_jobs = max_jobs, fresh_build = fresh_build, |
b8ec3c0455db
clarified modules: NUMA is managed by Build_Process;
wenzelm
parents:
77315
diff
changeset
|
127 |
no_build = no_build, verbose = verbose, session_setup) |
77246 | 128 |
} |
129 |
} |
|
130 |
||
131 |
final class Context private( |
|
77378
f047804f4860
clarified Build_Process.Context: cover all static information;
wenzelm
parents:
77375
diff
changeset
|
132 |
val instance: String, |
77257 | 133 |
val store: Sessions.Store, |
134 |
val deps: Sessions.Deps, |
|
77246 | 135 |
sessions: Map[String, Session_Context], |
77257 | 136 |
val ordering: Ordering[String], |
77315
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
137 |
val progress: Progress, |
77378
f047804f4860
clarified Build_Process.Context: cover all static information;
wenzelm
parents:
77375
diff
changeset
|
138 |
val hostname: String, |
77317
b8ec3c0455db
clarified modules: NUMA is managed by Build_Process;
wenzelm
parents:
77315
diff
changeset
|
139 |
val numa_nodes: List[Int], |
77315
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
140 |
val build_heap: Boolean, |
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
141 |
val max_jobs: Int, |
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
142 |
val fresh_build: Boolean, |
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
143 |
val no_build: Boolean, |
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
144 |
val verbose: Boolean, |
77378
f047804f4860
clarified Build_Process.Context: cover all static information;
wenzelm
parents:
77375
diff
changeset
|
145 |
val session_setup: (String, Session) => Unit, |
77246 | 146 |
) { |
77257 | 147 |
def sessions_structure: Sessions.Structure = deps.sessions_structure |
148 |
||
77246 | 149 |
def apply(session: String): Session_Context = |
77249
f3f1b7ad1d0d
clarified data structure: more direct access to timeout;
wenzelm
parents:
77248
diff
changeset
|
150 |
sessions.getOrElse(session, Session_Context.empty(session, Time.zero)) |
77255
b810e99b5afb
clarified static build_context vs. dynamic queue;
wenzelm
parents:
77254
diff
changeset
|
151 |
|
77315
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
152 |
def do_store(session: String): Boolean = |
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
153 |
build_heap || Sessions.is_pure(session) || !sessions_structure.build_graph.is_maximal(session) |
77246 | 154 |
} |
77257 | 155 |
|
156 |
||
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
157 |
/* dynamic state */ |
77257 | 158 |
|
77344 | 159 |
case class Entry(name: String, deps: List[String], info: JSON.Object.T = JSON.Object.empty) { |
77313
f8aa1647d156
more elementary data structures, to fit better to SQL database;
wenzelm
parents:
77312
diff
changeset
|
160 |
def is_ready: Boolean = deps.isEmpty |
f8aa1647d156
more elementary data structures, to fit better to SQL database;
wenzelm
parents:
77312
diff
changeset
|
161 |
def resolve(dep: String): Entry = |
f8aa1647d156
more elementary data structures, to fit better to SQL database;
wenzelm
parents:
77312
diff
changeset
|
162 |
if (deps.contains(dep)) copy(deps = deps.filterNot(_ == dep)) else this |
f8aa1647d156
more elementary data structures, to fit better to SQL database;
wenzelm
parents:
77312
diff
changeset
|
163 |
} |
f8aa1647d156
more elementary data structures, to fit better to SQL database;
wenzelm
parents:
77312
diff
changeset
|
164 |
|
77259
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
165 |
case class Result( |
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
166 |
current: Boolean, |
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
167 |
output_heap: SHA1.Shasum, |
77372 | 168 |
node_info: Build_Job.Node_Info, |
77259
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
169 |
process_result: Process_Result |
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
170 |
) { |
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
171 |
def ok: Boolean = process_result.ok |
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
172 |
} |
77312 | 173 |
|
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
174 |
sealed case class State( |
77372 | 175 |
serial: Long = 0, |
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
176 |
numa_index: Int = 0, |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
177 |
pending: List[Entry] = Nil, |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
178 |
running: Map[String, Build_Job] = Map.empty, |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
179 |
results: Map[String, Build_Process.Result] = Map.empty |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
180 |
) { |
77343 | 181 |
def numa_next(numa_nodes: List[Int]): (Option[Int], State) = |
182 |
if (numa_nodes.isEmpty) (None, this) |
|
183 |
else { |
|
184 |
val available = numa_nodes.zipWithIndex |
|
77372 | 185 |
val used = |
186 |
Set.from(for (job <- running.valuesIterator; i <- job.node_info.numa_node) yield i) |
|
77343 | 187 |
val candidates = available.drop(numa_index) ::: available.take(numa_index) |
188 |
val (n, i) = |
|
189 |
candidates.find({ case (n, i) => i == numa_index && !used(n) }) orElse |
|
190 |
candidates.find({ case (n, _) => !used(n) }) getOrElse candidates.head |
|
191 |
(Some(n), copy(numa_index = (i + 1) % available.length)) |
|
192 |
} |
|
77337 | 193 |
|
77335 | 194 |
def finished: Boolean = pending.isEmpty |
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
195 |
|
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
196 |
def remove_pending(name: String): State = |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
197 |
copy(pending = pending.flatMap( |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
198 |
entry => if (entry.name == name) None else Some(entry.resolve(name)))) |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
199 |
|
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
200 |
def is_running(name: String): Boolean = running.isDefinedAt(name) |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
201 |
|
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
202 |
def stop_running(): Unit = running.valuesIterator.foreach(_.terminate()) |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
203 |
|
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
204 |
def finished_running(): List[Build_Job.Build_Session] = |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
205 |
List.from( |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
206 |
running.valuesIterator.flatMap { |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
207 |
case job: Build_Job.Build_Session if job.is_finished => Some(job) |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
208 |
case _ => None |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
209 |
}) |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
210 |
|
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
211 |
def add_running(name: String, job: Build_Job): State = |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
212 |
copy(running = running + (name -> job)) |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
213 |
|
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
214 |
def remove_running(name: String): State = |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
215 |
copy(running = running - name) |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
216 |
|
77336 | 217 |
def make_result( |
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
218 |
name: String, |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
219 |
current: Boolean, |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
220 |
output_heap: SHA1.Shasum, |
77372 | 221 |
process_result: Process_Result, |
222 |
node_info: Build_Job.Node_Info = Build_Job.Node_Info.none |
|
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
223 |
): State = { |
77372 | 224 |
val result = Build_Process.Result(current, output_heap, node_info, process_result) |
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
225 |
copy(results = results + (name -> result)) |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
226 |
} |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
227 |
|
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
228 |
def get_results(names: List[String]): List[Build_Process.Result] = |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
229 |
names.map(results.apply) |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
230 |
} |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
231 |
|
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
232 |
|
77372 | 233 |
/* SQL data model */ |
234 |
||
235 |
object Data { |
|
236 |
val database = Path.explode("$ISABELLE_HOME_USER/build.db") |
|
237 |
||
238 |
def make_table(name: String, columns: List[SQL.Column], body: String = ""): SQL.Table = |
|
239 |
SQL.Table("isabelle_build" + if_proper(name, "_" + name), columns, body = body) |
|
240 |
||
241 |
object Generic { |
|
242 |
val instance = SQL.Column.string("instance") |
|
243 |
val name = SQL.Column.string("name") |
|
244 |
||
245 |
def sql_equal(instance: String = "", name: String = ""): SQL.Source = |
|
246 |
SQL.and( |
|
247 |
if_proper(instance, Generic.instance.equal(instance)), |
|
248 |
if_proper(name, Generic.name.equal(name))) |
|
249 |
||
250 |
def sql_member(instance: String = "", names: Iterable[String] = Nil): SQL.Source = |
|
251 |
SQL.and( |
|
252 |
if_proper(instance, Generic.instance.equal(instance)), |
|
77375 | 253 |
if_proper(names, Generic.name.member(names))) |
77372 | 254 |
} |
255 |
||
256 |
object Config { |
|
257 |
val instance = Generic.instance.make_primary_key |
|
77387
cd10b8edfdf5
clarified db content: avoid redundancy of historic ML_IDENTIFIER;
wenzelm
parents:
77385
diff
changeset
|
258 |
val ml_platform = SQL.Column.string("ml_platform") |
77372 | 259 |
val options = SQL.Column.string("options") |
260 |
||
77387
cd10b8edfdf5
clarified db content: avoid redundancy of historic ML_IDENTIFIER;
wenzelm
parents:
77385
diff
changeset
|
261 |
val table = make_table("", List(instance, ml_platform, options)) |
77372 | 262 |
} |
263 |
||
264 |
object State { |
|
265 |
val instance = Generic.instance.make_primary_key |
|
266 |
val serial = SQL.Column.long("serial") |
|
267 |
val numa_index = SQL.Column.int("numa_index") |
|
268 |
||
269 |
val table = make_table("state", List(instance, serial, numa_index)) |
|
270 |
} |
|
271 |
||
272 |
object Pending { |
|
273 |
val name = Generic.name.make_primary_key |
|
274 |
val deps = SQL.Column.string("deps") |
|
275 |
val info = SQL.Column.string("info") |
|
276 |
||
277 |
val table = make_table("pending", List(name, deps, info)) |
|
278 |
} |
|
279 |
||
280 |
object Running { |
|
281 |
val name = Generic.name.make_primary_key |
|
282 |
val hostname = SQL.Column.string("hostname") |
|
283 |
val numa_node = SQL.Column.int("numa_node") |
|
284 |
||
285 |
val table = make_table("running", List(name, hostname, numa_node)) |
|
286 |
} |
|
287 |
||
288 |
object Results { |
|
289 |
val name = Generic.name.make_primary_key |
|
290 |
val hostname = SQL.Column.string("hostname") |
|
291 |
val numa_node = SQL.Column.string("numa_node") |
|
292 |
val rc = SQL.Column.int("rc") |
|
293 |
val out = SQL.Column.string("out") |
|
294 |
val err = SQL.Column.string("err") |
|
295 |
val timing_elapsed = SQL.Column.long("timing_elapsed") |
|
296 |
val timing_cpu = SQL.Column.long("timing_cpu") |
|
297 |
val timing_gc = SQL.Column.long("timing_gc") |
|
298 |
||
299 |
val table = |
|
300 |
make_table("results", |
|
301 |
List(name, hostname, numa_node, rc, out, err, timing_elapsed, timing_cpu, timing_gc)) |
|
302 |
} |
|
303 |
||
304 |
def read_pending(db: SQL.Database): List[Entry] = |
|
77381
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
305 |
db.using_statement(Pending.table.select(sql = SQL.order_by(List(Pending.name)))) { stmt => |
77372 | 306 |
List.from( |
307 |
stmt.execute_query().iterator { res => |
|
308 |
val name = res.string(Pending.name) |
|
309 |
val deps = res.string(Pending.deps) |
|
310 |
val info = res.string(Pending.info) |
|
311 |
Entry(name, split_lines(deps), info = JSON.Object.parse(info)) |
|
312 |
}) |
|
313 |
} |
|
314 |
||
315 |
def update_pending(db: SQL.Database, pending: List[Entry]): Boolean = { |
|
316 |
val old_pending = read_pending(db) |
|
317 |
val (delete, insert) = Library.symmetric_difference(old_pending, pending) |
|
318 |
||
319 |
if (delete.nonEmpty) { |
|
320 |
db.using_statement( |
|
77381
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
321 |
Pending.table.delete( |
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
322 |
sql = SQL.where(Generic.sql_member(names = delete.map(_.name)))))(_.execute()) |
77372 | 323 |
} |
324 |
||
325 |
for (entry <- insert) { |
|
326 |
db.using_statement(Pending.table.insert()) { stmt => |
|
327 |
stmt.string(1) = entry.name |
|
328 |
stmt.string(2) = cat_lines(entry.deps) |
|
329 |
stmt.string(3) = JSON.Format(entry.info) |
|
330 |
stmt.execute() |
|
331 |
} |
|
332 |
} |
|
333 |
||
334 |
delete.nonEmpty || insert.nonEmpty |
|
335 |
} |
|
336 |
||
337 |
def read_running(db: SQL.Database): List[Build_Job.Abstract] = |
|
77381
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
338 |
db.using_statement(Running.table.select(sql = SQL.order_by(List(Running.name)))) { stmt => |
77372 | 339 |
List.from( |
340 |
stmt.execute_query().iterator { res => |
|
341 |
val name = res.string(Running.name) |
|
342 |
val hostname = res.string(Running.hostname) |
|
343 |
val numa_node = res.get_int(Running.numa_node) |
|
344 |
Build_Job.Abstract(name, Build_Job.Node_Info(hostname, numa_node)) |
|
345 |
}) |
|
346 |
} |
|
347 |
||
348 |
def update_running(db: SQL.Database, running: Map[String, Build_Job]): Boolean = { |
|
349 |
val old_running = read_running(db) |
|
350 |
val abs_running = running.valuesIterator.map(_.make_abstract).toList |
|
351 |
||
352 |
val (delete, insert) = Library.symmetric_difference(old_running, abs_running) |
|
353 |
||
354 |
if (delete.nonEmpty) { |
|
355 |
db.using_statement( |
|
77381
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
356 |
Running.table.delete( |
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
357 |
sql = SQL.where(Generic.sql_member(names = delete.map(_.job_name)))))(_.execute()) |
77372 | 358 |
} |
359 |
||
360 |
for (job <- insert) { |
|
361 |
db.using_statement(Running.table.insert()) { stmt => |
|
362 |
stmt.string(1) = job.job_name |
|
363 |
stmt.string(2) = job.node_info.hostname |
|
364 |
stmt.int(3) = job.node_info.numa_node |
|
365 |
stmt.execute() |
|
366 |
} |
|
367 |
} |
|
368 |
||
369 |
delete.nonEmpty || insert.nonEmpty |
|
370 |
} |
|
371 |
||
372 |
def read_results(db: SQL.Database, names: List[String] = Nil): Map[String, Build_Job.Result] = |
|
373 |
db.using_statement( |
|
77381
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
374 |
Results.table.select(sql = if_proper(names, Results.name.where_member(names)))) { stmt => |
77372 | 375 |
Map.from( |
376 |
stmt.execute_query().iterator { res => |
|
377 |
val name = res.string(Results.name) |
|
378 |
val hostname = res.string(Results.hostname) |
|
379 |
val numa_node = res.get_int(Results.numa_node) |
|
380 |
val rc = res.int(Results.rc) |
|
381 |
val out = res.string(Results.out) |
|
382 |
val err = res.string(Results.err) |
|
383 |
val timing_elapsed = res.long(Results.timing_elapsed) |
|
384 |
val timing_cpu = res.long(Results.timing_cpu) |
|
385 |
val timing_gc = res.long(Results.timing_gc) |
|
386 |
val node_info = Build_Job.Node_Info(hostname, numa_node) |
|
387 |
val process_result = |
|
388 |
Process_Result(rc, |
|
389 |
out_lines = split_lines(out), |
|
390 |
err_lines = split_lines(err), |
|
391 |
timing = Timing(Time.ms(timing_elapsed), Time.ms(timing_cpu), Time.ms(timing_gc))) |
|
392 |
name -> Build_Job.Result(node_info, process_result) |
|
393 |
}) |
|
394 |
} |
|
395 |
||
396 |
def read_results_name(db: SQL.Database): Set[String] = |
|
77381
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
397 |
db.using_statement(Results.table.select(List(Results.name)))(stmt => |
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
398 |
Set.from(stmt.execute_query().iterator(_.string(Results.name)))) |
77372 | 399 |
|
400 |
def update_results(db: SQL.Database, results: Map[String, Build_Process.Result]): Boolean = { |
|
401 |
val old_results = read_results_name(db) |
|
77385 | 402 |
val insert = results.iterator.filterNot(p => old_results.contains(p._1)).toList |
77372 | 403 |
|
404 |
for ((name, result) <- insert) { |
|
405 |
val node_info = result.node_info |
|
406 |
val process_result = result.process_result |
|
407 |
db.using_statement(Results.table.insert()) { stmt => |
|
408 |
stmt.string(1) = name |
|
409 |
stmt.string(2) = node_info.hostname |
|
410 |
stmt.int(3) = node_info.numa_node |
|
411 |
stmt.int(4) = process_result.rc |
|
412 |
stmt.string(5) = cat_lines(process_result.out_lines) |
|
413 |
stmt.string(6) = cat_lines(process_result.err_lines) |
|
414 |
stmt.long(7) = process_result.timing.elapsed.ms |
|
415 |
stmt.long(8) = process_result.timing.cpu.ms |
|
416 |
stmt.long(9) = process_result.timing.gc.ms |
|
417 |
stmt.execute() |
|
418 |
} |
|
419 |
} |
|
420 |
||
421 |
insert.nonEmpty |
|
422 |
} |
|
423 |
||
424 |
def write_config(db: SQL.Database, instance: String, hostname: String, options: Options): Unit = |
|
425 |
db.using_statement(Config.table.insert()) { stmt => |
|
426 |
stmt.string(1) = instance |
|
77387
cd10b8edfdf5
clarified db content: avoid redundancy of historic ML_IDENTIFIER;
wenzelm
parents:
77385
diff
changeset
|
427 |
stmt.string(2) = Isabelle_System.getenv("ML_PLATFORM") |
77374
268bf61631ec
more robust options in "prefs" format: avoid odd control character;
wenzelm
parents:
77372
diff
changeset
|
428 |
stmt.string(3) = options.make_prefs(Options.init(prefs = "")) |
77372 | 429 |
stmt.execute() |
430 |
} |
|
431 |
||
432 |
def read_state(db: SQL.Database, instance: String): (Long, Int) = |
|
433 |
db.using_statement( |
|
77381
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
434 |
State.table.select(sql = SQL.where(Generic.sql_equal(instance = instance))) |
77372 | 435 |
) { stmt => |
77381
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
436 |
(stmt.execute_query().iterator { res => |
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
437 |
val serial = res.long(State.serial) |
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
438 |
val numa_index = res.int(State.numa_index) |
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
439 |
(serial, numa_index) |
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
440 |
}).nextOption.getOrElse(error("No build state instance " + instance + " in database " + db)) |
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
441 |
} |
77372 | 442 |
|
77381
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
443 |
def write_state(db: SQL.Database, instance: String, serial: Long, numa_index: Int): Unit = |
77372 | 444 |
db.using_statement(State.table.insert()) { stmt => |
445 |
stmt.string(1) = instance |
|
446 |
stmt.long(2) = serial |
|
447 |
stmt.int(3) = numa_index |
|
448 |
stmt.execute() |
|
449 |
} |
|
450 |
||
77379
bd0028d1ece6
clarified init_database vs. update_database: implicitly assume fresh "instance";
wenzelm
parents:
77378
diff
changeset
|
451 |
def reset_state(db: SQL.Database, instance: String): Unit = |
bd0028d1ece6
clarified init_database vs. update_database: implicitly assume fresh "instance";
wenzelm
parents:
77378
diff
changeset
|
452 |
db.using_statement( |
77381
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77380
diff
changeset
|
453 |
State.table.delete(sql = SQL.where(Generic.sql_equal(instance = instance))))(_.execute()) |
77379
bd0028d1ece6
clarified init_database vs. update_database: implicitly assume fresh "instance";
wenzelm
parents:
77378
diff
changeset
|
454 |
|
77378
f047804f4860
clarified Build_Process.Context: cover all static information;
wenzelm
parents:
77375
diff
changeset
|
455 |
def init_database(db: SQL.Database, build_context: Build_Process.Context): Unit = { |
77372 | 456 |
val tables = |
457 |
List(Config.table, State.table, Pending.table, Running.table, Results.table) |
|
458 |
||
459 |
for (table <- tables) db.create_table(table) |
|
460 |
||
461 |
val old_pending = Data.read_pending(db) |
|
462 |
if (old_pending.nonEmpty) { |
|
463 |
error("Cannot init build process, because of unfinished " + |
|
464 |
commas_quote(old_pending.map(_.name))) |
|
465 |
} |
|
466 |
||
467 |
for (table <- tables) db.using_statement(table.delete())(_.execute()) |
|
468 |
||
77378
f047804f4860
clarified Build_Process.Context: cover all static information;
wenzelm
parents:
77375
diff
changeset
|
469 |
write_config(db, build_context.instance, build_context.hostname, build_context.store.options) |
f047804f4860
clarified Build_Process.Context: cover all static information;
wenzelm
parents:
77375
diff
changeset
|
470 |
write_state(db, build_context.instance, 0, 0) |
77372 | 471 |
} |
472 |
||
473 |
def update_database(db: SQL.Database, instance: String, state: State): State = { |
|
474 |
val ch1 = update_pending(db, state.pending) |
|
475 |
val ch2 = update_running(db, state.running) |
|
476 |
val ch3 = update_results(db, state.results) |
|
477 |
||
77380 | 478 |
val (serial0, _) = read_state(db, instance) |
77372 | 479 |
val serial = if (ch1 || ch2 || ch3) serial0 + 1 else serial0 |
77379
bd0028d1ece6
clarified init_database vs. update_database: implicitly assume fresh "instance";
wenzelm
parents:
77378
diff
changeset
|
480 |
if (serial != serial0) { |
bd0028d1ece6
clarified init_database vs. update_database: implicitly assume fresh "instance";
wenzelm
parents:
77378
diff
changeset
|
481 |
reset_state(db, instance) |
bd0028d1ece6
clarified init_database vs. update_database: implicitly assume fresh "instance";
wenzelm
parents:
77378
diff
changeset
|
482 |
write_state(db, instance, serial, state.numa_index) |
bd0028d1ece6
clarified init_database vs. update_database: implicitly assume fresh "instance";
wenzelm
parents:
77378
diff
changeset
|
483 |
} |
77372 | 484 |
|
485 |
state.copy(serial = serial) |
|
486 |
} |
|
487 |
} |
|
488 |
||
489 |
||
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
490 |
/* main process */ |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
491 |
|
77312 | 492 |
def session_finished(session_name: String, process_result: Process_Result): String = |
493 |
"Finished " + session_name + " (" + process_result.timing.message_resources + ")" |
|
494 |
||
495 |
def session_timing(session_name: String, build_log: Build_Log.Session_Info): String = { |
|
496 |
val props = build_log.session_timing |
|
497 |
val threads = Markup.Session_Timing.Threads.unapply(props) getOrElse 1 |
|
498 |
val timing = Markup.Timing_Properties.get(props) |
|
499 |
"Timing " + session_name + " (" + threads + " threads, " + timing.message_factor + ")" |
|
500 |
} |
|
77259
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
501 |
} |
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
502 |
|
77372 | 503 |
class Build_Process(protected val build_context: Build_Process.Context) extends AutoCloseable { |
77338
0a91c697a18a
tuned signature: avoid warnings in IntelliJ IDEA;
wenzelm
parents:
77337
diff
changeset
|
504 |
protected val store: Sessions.Store = build_context.store |
0a91c697a18a
tuned signature: avoid warnings in IntelliJ IDEA;
wenzelm
parents:
77337
diff
changeset
|
505 |
protected val build_options: Options = store.options |
0a91c697a18a
tuned signature: avoid warnings in IntelliJ IDEA;
wenzelm
parents:
77337
diff
changeset
|
506 |
protected val build_deps: Sessions.Deps = build_context.deps |
0a91c697a18a
tuned signature: avoid warnings in IntelliJ IDEA;
wenzelm
parents:
77337
diff
changeset
|
507 |
protected val progress: Progress = build_context.progress |
0a91c697a18a
tuned signature: avoid warnings in IntelliJ IDEA;
wenzelm
parents:
77337
diff
changeset
|
508 |
protected val verbose: Boolean = build_context.verbose |
77259
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
509 |
|
77338
0a91c697a18a
tuned signature: avoid warnings in IntelliJ IDEA;
wenzelm
parents:
77337
diff
changeset
|
510 |
protected val log: Logger = |
77287 | 511 |
build_options.string("system_log") match { |
512 |
case "" => No_Logger |
|
513 |
case "-" => Logger.make(progress) |
|
514 |
case log_file => Logger.make(Some(Path.explode(log_file))) |
|
515 |
} |
|
516 |
||
77372 | 517 |
protected val database: Option[SQL.Database] = |
518 |
if (!build_options.bool("build_database") || true /*FIXME*/) None |
|
519 |
else if (store.database_server) Some(store.open_database_server()) |
|
77383
cb75171d8c9f
clarified permissions of build.db, following server.db;
wenzelm
parents:
77381
diff
changeset
|
520 |
else { |
cb75171d8c9f
clarified permissions of build.db, following server.db;
wenzelm
parents:
77381
diff
changeset
|
521 |
val db = SQLite.open_database(Build_Process.Data.database) |
cb75171d8c9f
clarified permissions of build.db, following server.db;
wenzelm
parents:
77381
diff
changeset
|
522 |
try { Isabelle_System.chmod("600", Build_Process.Data.database) } |
cb75171d8c9f
clarified permissions of build.db, following server.db;
wenzelm
parents:
77381
diff
changeset
|
523 |
catch { case exn: Throwable => db.close(); throw exn } |
cb75171d8c9f
clarified permissions of build.db, following server.db;
wenzelm
parents:
77381
diff
changeset
|
524 |
Some(db) |
cb75171d8c9f
clarified permissions of build.db, following server.db;
wenzelm
parents:
77381
diff
changeset
|
525 |
} |
77372 | 526 |
|
77380 | 527 |
def close(): Unit = database.foreach(_.close()) |
77372 | 528 |
|
77259
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
529 |
// global state |
77338
0a91c697a18a
tuned signature: avoid warnings in IntelliJ IDEA;
wenzelm
parents:
77337
diff
changeset
|
530 |
protected var _state: Build_Process.State = init_state() |
77333 | 531 |
|
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
532 |
protected def init_state(): Build_Process.State = |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
533 |
Build_Process.State(pending = |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
534 |
(for ((name, (_, (preds, _))) <- build_context.sessions_structure.build_graph.iterator) |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
535 |
yield Build_Process.Entry(name, preds.toList)).toList) |
77259
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
536 |
|
77335 | 537 |
protected def finished(): Boolean = synchronized { _state.finished } |
77259
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
538 |
|
77331
38643c64b1e2
clarified signature: support meaningful subclasses for Build.Engine implementations;
wenzelm
parents:
77329
diff
changeset
|
539 |
protected def next_pending(): Option[String] = synchronized { |
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
540 |
if (_state.running.size < (build_context.max_jobs max 1)) { |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
541 |
_state.pending.filter(entry => entry.is_ready && !_state.is_running(entry.name)) |
77313
f8aa1647d156
more elementary data structures, to fit better to SQL database;
wenzelm
parents:
77312
diff
changeset
|
542 |
.sortBy(_.name)(build_context.ordering) |
f8aa1647d156
more elementary data structures, to fit better to SQL database;
wenzelm
parents:
77312
diff
changeset
|
543 |
.headOption.map(_.name) |
77296
eeaa2872320b
clarified signature: more explicit synchronized operations;
wenzelm
parents:
77295
diff
changeset
|
544 |
} |
eeaa2872320b
clarified signature: more explicit synchronized operations;
wenzelm
parents:
77295
diff
changeset
|
545 |
else None |
77289
c7d893278aec
proper synchronized access to mutable state, to support concurrency eventually;
wenzelm
parents:
77288
diff
changeset
|
546 |
} |
77259
61fc2afe4c8b
clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents:
77258
diff
changeset
|
547 |
|
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
548 |
protected def stop_running(): Unit = synchronized { _state.stop_running() } |
77295
ab13ac27c74a
clarified signature: more explicit synchronized operations;
wenzelm
parents:
77294
diff
changeset
|
549 |
|
77331
38643c64b1e2
clarified signature: support meaningful subclasses for Build.Engine implementations;
wenzelm
parents:
77329
diff
changeset
|
550 |
protected def finished_running(): List[Build_Job.Build_Session] = synchronized { |
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
551 |
_state.finished_running() |
77296
eeaa2872320b
clarified signature: more explicit synchronized operations;
wenzelm
parents:
77295
diff
changeset
|
552 |
} |
eeaa2872320b
clarified signature: more explicit synchronized operations;
wenzelm
parents:
77295
diff
changeset
|
553 |
|
77331
38643c64b1e2
clarified signature: support meaningful subclasses for Build.Engine implementations;
wenzelm
parents:
77329
diff
changeset
|
554 |
protected def finish_job(job: Build_Job.Build_Session): Unit = { |
77296
eeaa2872320b
clarified signature: more explicit synchronized operations;
wenzelm
parents:
77295
diff
changeset
|
555 |
val session_name = job.session_name |
77260 | 556 |
val process_result = job.join |
77294 | 557 |
val output_heap = job.finish |
77257 | 558 |
|
77260 | 559 |
val log_lines = process_result.out_lines.filterNot(Protocol_Message.Marker.test) |
560 |
val process_result_tail = { |
|
561 |
val tail = job.info.options.int("process_output_tail") |
|
562 |
process_result.copy( |
|
563 |
out_lines = |
|
564 |
"(more details via \"isabelle log -H Error " + session_name + "\")" :: |
|
565 |
(if (tail == 0) log_lines else log_lines.drop(log_lines.length - tail max 0))) |
|
566 |
} |
|
77257 | 567 |
|
77260 | 568 |
val build_log = |
569 |
Build_Log.Log_File(session_name, process_result.out_lines). |
|
570 |
parse_session_info( |
|
571 |
command_timings = true, |
|
572 |
theory_timings = true, |
|
573 |
ml_statistics = true, |
|
574 |
task_statistics = true) |
|
575 |
||
576 |
// write log file |
|
577 |
if (process_result.ok) { |
|
578 |
File.write_gzip(store.output_log_gz(session_name), terminate_lines(log_lines)) |
|
579 |
} |
|
580 |
else File.write(store.output_log(session_name), terminate_lines(log_lines)) |
|
77257 | 581 |
|
77260 | 582 |
// write database |
583 |
using(store.open_database(session_name, output = true))(db => |
|
584 |
store.write_session_info(db, session_name, job.session_sources, |
|
585 |
build_log = |
|
586 |
if (process_result.timeout) build_log.error("Timeout") else build_log, |
|
587 |
build = |
|
77285 | 588 |
Sessions.Build_Info(build_deps.sources_shasum(session_name), job.input_heaps, |
77260 | 589 |
output_heap, process_result.rc, UUID.random().toString))) |
77257 | 590 |
|
77260 | 591 |
// messages |
592 |
process_result.err_lines.foreach(progress.echo) |
|
77257 | 593 |
|
77260 | 594 |
if (process_result.ok) { |
77312 | 595 |
if (verbose) progress.echo(Build_Process.session_timing(session_name, build_log)) |
596 |
progress.echo(Build_Process.session_finished(session_name, process_result)) |
|
77260 | 597 |
} |
598 |
else { |
|
599 |
progress.echo(session_name + " FAILED") |
|
600 |
if (!process_result.interrupted) progress.echo(process_result_tail.out) |
|
601 |
} |
|
77257 | 602 |
|
77289
c7d893278aec
proper synchronized access to mutable state, to support concurrency eventually;
wenzelm
parents:
77288
diff
changeset
|
603 |
synchronized { |
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
604 |
_state = _state. |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
605 |
remove_pending(session_name). |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
606 |
remove_running(session_name). |
77372 | 607 |
make_result(session_name, false, output_heap, process_result_tail, node_info = job.node_info) |
77289
c7d893278aec
proper synchronized access to mutable state, to support concurrency eventually;
wenzelm
parents:
77288
diff
changeset
|
608 |
} |
77260 | 609 |
} |
77257 | 610 |
|
77331
38643c64b1e2
clarified signature: support meaningful subclasses for Build.Engine implementations;
wenzelm
parents:
77329
diff
changeset
|
611 |
protected def start_job(session_name: String): Unit = { |
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
612 |
val ancestor_results = synchronized { |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
613 |
_state.get_results( |
77295
ab13ac27c74a
clarified signature: more explicit synchronized operations;
wenzelm
parents:
77294
diff
changeset
|
614 |
build_deps.sessions_structure.build_requirements(List(session_name)). |
ab13ac27c74a
clarified signature: more explicit synchronized operations;
wenzelm
parents:
77294
diff
changeset
|
615 |
filterNot(_ == session_name)) |
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
616 |
} |
77260 | 617 |
val input_heaps = |
618 |
if (ancestor_results.isEmpty) { |
|
619 |
SHA1.shasum_meta_info(SHA1.digest(Path.explode("$POLYML_EXE"))) |
|
620 |
} |
|
621 |
else SHA1.flat_shasum(ancestor_results.map(_.output_heap)) |
|
77257 | 622 |
|
77315
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
623 |
val do_store = build_context.do_store(session_name) |
77260 | 624 |
val (current, output_heap) = { |
625 |
store.try_open_database(session_name) match { |
|
626 |
case Some(db) => |
|
627 |
using(db)(store.read_build(_, session_name)) match { |
|
628 |
case Some(build) => |
|
629 |
val output_heap = store.find_heap_shasum(session_name) |
|
630 |
val current = |
|
77315
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
631 |
!build_context.fresh_build && |
77260 | 632 |
build.ok && |
633 |
build.sources == build_deps.sources_shasum(session_name) && |
|
634 |
build.input_heaps == input_heaps && |
|
635 |
build.output_heap == output_heap && |
|
636 |
!(do_store && output_heap.is_empty) |
|
637 |
(current, output_heap) |
|
638 |
case None => (false, SHA1.no_shasum) |
|
639 |
} |
|
640 |
case None => (false, SHA1.no_shasum) |
|
641 |
} |
|
642 |
} |
|
643 |
val all_current = current && ancestor_results.forall(_.current) |
|
77257 | 644 |
|
77260 | 645 |
if (all_current) { |
77289
c7d893278aec
proper synchronized access to mutable state, to support concurrency eventually;
wenzelm
parents:
77288
diff
changeset
|
646 |
synchronized { |
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
647 |
_state = _state. |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
648 |
remove_pending(session_name). |
77336 | 649 |
make_result(session_name, true, output_heap, Process_Result.ok) |
77289
c7d893278aec
proper synchronized access to mutable state, to support concurrency eventually;
wenzelm
parents:
77288
diff
changeset
|
650 |
} |
77260 | 651 |
} |
77315
f34559b24277
clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents:
77314
diff
changeset
|
652 |
else if (build_context.no_build) { |
77260 | 653 |
progress.echo_if(verbose, "Skipping " + session_name + " ...") |
77289
c7d893278aec
proper synchronized access to mutable state, to support concurrency eventually;
wenzelm
parents:
77288
diff
changeset
|
654 |
synchronized { |
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
655 |
_state = _state. |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
656 |
remove_pending(session_name). |
77336 | 657 |
make_result(session_name, false, output_heap, Process_Result.error) |
77289
c7d893278aec
proper synchronized access to mutable state, to support concurrency eventually;
wenzelm
parents:
77288
diff
changeset
|
658 |
} |
77260 | 659 |
} |
660 |
else if (ancestor_results.forall(_.ok) && !progress.stopped) { |
|
661 |
progress.echo((if (do_store) "Building " else "Running ") + session_name + " ...") |
|
662 |
||
663 |
store.clean_output(session_name) |
|
664 |
using(store.open_database(session_name, output = true))( |
|
665 |
store.init_session_info(_, session_name)) |
|
77257 | 666 |
|
77260 | 667 |
val session_background = build_deps.background(session_name) |
668 |
val resources = |
|
669 |
new Resources(session_background, log = log, |
|
670 |
command_timings = build_context(session_name).old_command_timings) |
|
77257 | 671 |
|
77291 | 672 |
val job = |
673 |
synchronized { |
|
77343 | 674 |
val (numa_node, state1) = _state.numa_next(build_context.numa_nodes) |
77378
f047804f4860
clarified Build_Process.Context: cover all static information;
wenzelm
parents:
77375
diff
changeset
|
675 |
val node_info = Build_Job.Node_Info(build_context.hostname, numa_node) |
77343 | 676 |
val job = |
77297
226a880d0423
clarified types: support a variety of Build_Job instances;
wenzelm
parents:
77296
diff
changeset
|
677 |
new Build_Job.Build_Session(progress, session_background, store, do_store, |
77372 | 678 |
resources, build_context.session_setup, input_heaps, node_info) |
77343 | 679 |
_state = state1.add_running(session_name, job) |
680 |
job |
|
77291 | 681 |
} |
682 |
job.start() |
|
77260 | 683 |
} |
684 |
else { |
|
685 |
progress.echo(session_name + " CANCELLED") |
|
77289
c7d893278aec
proper synchronized access to mutable state, to support concurrency eventually;
wenzelm
parents:
77288
diff
changeset
|
686 |
synchronized { |
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
687 |
_state = _state. |
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
688 |
remove_pending(session_name). |
77336 | 689 |
make_result(session_name, false, output_heap, Process_Result.undefined) |
77289
c7d893278aec
proper synchronized access to mutable state, to support concurrency eventually;
wenzelm
parents:
77288
diff
changeset
|
690 |
} |
77260 | 691 |
} |
692 |
} |
|
77257 | 693 |
|
77372 | 694 |
protected def setup_database(): Unit = |
695 |
for (db <- database) { |
|
696 |
synchronized { |
|
697 |
db.transaction { |
|
77378
f047804f4860
clarified Build_Process.Context: cover all static information;
wenzelm
parents:
77375
diff
changeset
|
698 |
Build_Process.Data.init_database(db, build_context) |
77372 | 699 |
} |
700 |
} |
|
701 |
db.rebuild() |
|
702 |
} |
|
703 |
protected def sync_database(): Unit = |
|
704 |
for (db <- database) { |
|
705 |
synchronized { |
|
706 |
db.transaction { |
|
77378
f047804f4860
clarified Build_Process.Context: cover all static information;
wenzelm
parents:
77375
diff
changeset
|
707 |
_state = Build_Process.Data.update_database(db, build_context.instance, _state) |
77372 | 708 |
} |
709 |
} |
|
710 |
} |
|
711 |
||
77331
38643c64b1e2
clarified signature: support meaningful subclasses for Build.Engine implementations;
wenzelm
parents:
77329
diff
changeset
|
712 |
protected def sleep(): Unit = |
77260 | 713 |
Isabelle_Thread.interrupt_handler(_ => progress.stop()) { |
714 |
build_options.seconds("editor_input_delay").sleep() |
|
715 |
} |
|
77257 | 716 |
|
77310 | 717 |
def run(): Map[String, Process_Result] = { |
77335 | 718 |
if (finished()) { |
719 |
progress.echo_warning("Nothing to build") |
|
720 |
Map.empty[String, Process_Result] |
|
721 |
} |
|
722 |
else { |
|
77372 | 723 |
setup_database() |
77335 | 724 |
while (!finished()) { |
77310 | 725 |
if (progress.stopped) stop_running() |
726 |
||
727 |
for (job <- finished_running()) finish_job(job) |
|
77260 | 728 |
|
77310 | 729 |
next_pending() match { |
77372 | 730 |
case Some(name) => |
731 |
start_job(name) |
|
732 |
case None => |
|
733 |
sync_database() |
|
734 |
sleep() |
|
77310 | 735 |
} |
736 |
} |
|
737 |
synchronized { |
|
77334
0231e62956a6
clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents:
77333
diff
changeset
|
738 |
for ((name, result) <- _state.results) yield name -> result.process_result |
77257 | 739 |
} |
740 |
} |
|
741 |
} |
|
77238 | 742 |
} |