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