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