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