src/Pure/Tools/build_process.scala
author wenzelm
Tue, 21 Feb 2023 14:30:07 +0100
changeset 77343 db479840d6ad
parent 77339 c81abb66a97f
child 77344 de7eae726f8e
permissions -rw-r--r--
clarified signature;
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 {
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
    16
  /* static context */
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    17
77244
2e5a3955bc69 clarified signature and terminology;
wenzelm
parents: 77241
diff changeset
    18
  object Session_Context {
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    19
    def empty(session: String, timeout: Time): Session_Context =
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    20
      new Session_Context(session, timeout, Time.zero, Nil)
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    21
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    22
    def apply(
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    23
      session: String,
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    24
      timeout: Time,
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    25
      store: Sessions.Store,
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    26
      progress: Progress = new Progress
77244
2e5a3955bc69 clarified signature and terminology;
wenzelm
parents: 77241
diff changeset
    27
    ): Session_Context = {
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    28
      store.try_open_database(session) match {
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    29
        case None => empty(session, timeout)
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    30
        case Some(db) =>
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    31
          def ignore_error(msg: String) = {
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    32
            progress.echo_warning("Ignoring bad database " + db +
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    33
              " for session " + quote(session) + (if (msg == "") "" else ":\n" + msg))
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    34
            empty(session, timeout)
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    35
          }
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    36
          try {
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    37
            val command_timings = store.read_command_timings(db, session)
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    38
            val elapsed =
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    39
              store.read_session_timing(db, session) match {
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    40
                case Markup.Elapsed(s) => Time.seconds(s)
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    41
                case _ => Time.zero
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    42
              }
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    43
            new Session_Context(session, timeout, elapsed, command_timings)
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    44
          }
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    45
          catch {
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    46
            case ERROR(msg) => ignore_error(msg)
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    47
            case exn: java.lang.Error => ignore_error(Exn.message(exn))
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    48
            case _: XML.Error => ignore_error("XML.Error")
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    49
          }
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    50
          finally { db.close() }
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    51
      }
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    52
    }
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    53
  }
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    54
77244
2e5a3955bc69 clarified signature and terminology;
wenzelm
parents: 77241
diff changeset
    55
  final class Session_Context(
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    56
    val session: String,
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    57
    val timeout: Time,
77244
2e5a3955bc69 clarified signature and terminology;
wenzelm
parents: 77241
diff changeset
    58
    val old_time: Time,
2e5a3955bc69 clarified signature and terminology;
wenzelm
parents: 77241
diff changeset
    59
    val old_command_timings: List[Properties.T]
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    60
  ) {
77244
2e5a3955bc69 clarified signature and terminology;
wenzelm
parents: 77241
diff changeset
    61
    def is_empty: Boolean = old_time.is_zero && old_command_timings.isEmpty
77240
2ff94ba22140 tuned signature: more operations;
wenzelm
parents: 77239
diff changeset
    62
77245
1e2670d9dc18 tuned message: old_time not sufficiently prominent nor accurate to be printed;
wenzelm
parents: 77244
diff changeset
    63
    override def toString: String = session
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    64
  }
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    65
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    66
  object Context {
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    67
    def apply(
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    68
      store: Sessions.Store,
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
    69
      deps: Sessions.Deps,
77315
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    70
      progress: Progress = new Progress,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    71
      build_heap: Boolean = false,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    72
      numa_shuffling: Boolean = false,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    73
      max_jobs: Int = 1,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    74
      fresh_build: Boolean = false,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    75
      no_build: Boolean = false,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    76
      verbose: Boolean = false,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
    77
      session_setup: (String, Session) => Unit = (_, _) => ()
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    78
    ): Context = {
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
    79
      val sessions_structure = deps.sessions_structure
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    80
      val build_graph = sessions_structure.build_graph
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    81
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    82
      val sessions =
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    83
        Map.from(
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    84
          for (name <- build_graph.keys_iterator)
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    85
          yield {
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    86
            val timeout = sessions_structure(name).timeout
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    87
            name -> Build_Process.Session_Context(name, timeout, store, progress = progress)
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    88
          })
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    89
77248
wenzelm
parents: 77247
diff changeset
    90
      val sessions_time = {
wenzelm
parents: 77247
diff changeset
    91
        val maximals = build_graph.maximals.toSet
wenzelm
parents: 77247
diff changeset
    92
        def descendants_time(name: String): Double = {
wenzelm
parents: 77247
diff changeset
    93
          if (maximals.contains(name)) sessions(name).old_time.seconds
wenzelm
parents: 77247
diff changeset
    94
          else {
wenzelm
parents: 77247
diff changeset
    95
            val descendants = build_graph.all_succs(List(name)).toSet
wenzelm
parents: 77247
diff changeset
    96
            val g = build_graph.restrict(descendants)
wenzelm
parents: 77247
diff changeset
    97
            (0.0 :: g.maximals.flatMap { desc =>
wenzelm
parents: 77247
diff changeset
    98
              val ps = g.all_preds(List(desc))
wenzelm
parents: 77247
diff changeset
    99
              if (ps.exists(p => !sessions.isDefinedAt(p))) None
wenzelm
parents: 77247
diff changeset
   100
              else Some(ps.map(p => sessions(p).old_time.seconds).sum)
wenzelm
parents: 77247
diff changeset
   101
            }).max
wenzelm
parents: 77247
diff changeset
   102
          }
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
   103
        }
77248
wenzelm
parents: 77247
diff changeset
   104
        Map.from(
wenzelm
parents: 77247
diff changeset
   105
          for (name <- sessions.keysIterator)
wenzelm
parents: 77247
diff changeset
   106
          yield name -> descendants_time(name)).withDefaultValue(0.0)
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
   107
      }
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
   108
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   109
      val ordering =
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   110
        new Ordering[String] {
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   111
          def compare(name1: String, name2: String): Int =
77248
wenzelm
parents: 77247
diff changeset
   112
            sessions_time(name2) compare sessions_time(name1) match {
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   113
              case 0 =>
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
   114
                sessions(name2).timeout compare sessions(name1).timeout match {
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   115
                  case 0 => name1 compare name2
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   116
                  case ord => ord
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   117
                }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   118
              case ord => ord
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   119
            }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   120
        }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   121
77329
1b7c5d4b97a8 misc tuning and clarification;
wenzelm
parents: 77317
diff changeset
   122
      val numa_nodes = NUMA.nodes(enabled = numa_shuffling)
77317
b8ec3c0455db clarified modules: NUMA is managed by Build_Process;
wenzelm
parents: 77315
diff changeset
   123
      new Context(store, deps, sessions, ordering, progress, numa_nodes,
b8ec3c0455db clarified modules: NUMA is managed by Build_Process;
wenzelm
parents: 77315
diff changeset
   124
        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
   125
        no_build = no_build, verbose = verbose, session_setup)
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   126
    }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   127
  }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   128
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   129
  final class Context private(
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   130
    val store: Sessions.Store,
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   131
    val deps: Sessions.Deps,
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   132
    sessions: Map[String, Session_Context],
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   133
    val ordering: Ordering[String],
77315
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   134
    val progress: Progress,
77317
b8ec3c0455db clarified modules: NUMA is managed by Build_Process;
wenzelm
parents: 77315
diff changeset
   135
    val numa_nodes: List[Int],
77315
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   136
    val build_heap: Boolean,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   137
    val max_jobs: Int,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   138
    val fresh_build: Boolean,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   139
    val no_build: Boolean,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   140
    val verbose: Boolean,
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   141
    val session_setup: (String, Session) => Unit
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   142
  ) {
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   143
    def sessions_structure: Sessions.Structure = deps.sessions_structure
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   144
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   145
    def apply(session: String): Session_Context =
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
   146
      sessions.getOrElse(session, Session_Context.empty(session, Time.zero))
77255
b810e99b5afb clarified static build_context vs. dynamic queue;
wenzelm
parents: 77254
diff changeset
   147
77315
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   148
    def do_store(session: String): Boolean =
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   149
      build_heap || Sessions.is_pure(session) || !sessions_structure.build_graph.is_maximal(session)
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   150
  }
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   151
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   152
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   153
  /* dynamic state */
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   154
77313
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   155
  case class Entry(name: String, deps: List[String]) {
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   156
    def is_ready: Boolean = deps.isEmpty
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   157
    def resolve(dep: String): Entry =
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   158
      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
   159
  }
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   160
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   161
  case class Result(
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   162
    current: Boolean,
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   163
    output_heap: SHA1.Shasum,
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   164
    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
   165
  ) {
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   166
    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
   167
  }
77312
6a6231370432 clarified signature (see also 68a7ad1385bc);
wenzelm
parents: 77310
diff changeset
   168
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   169
  sealed case class State(
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   170
    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
   171
    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
   172
    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
   173
    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
   174
  ) {
77343
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   175
    def numa_next(numa_nodes: List[Int]): (Option[Int], State) =
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   176
      if (numa_nodes.isEmpty) (None, this)
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   177
      else {
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   178
        val available = numa_nodes.zipWithIndex
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   179
        val used = Set.from(for (job <- running.valuesIterator; i <- job.numa_node) yield i)
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   180
        val candidates = available.drop(numa_index) ::: available.take(numa_index)
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   181
        val (n, i) =
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   182
          candidates.find({ case (n, i) => i == numa_index && !used(n) }) orElse
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   183
          candidates.find({ case (n, _) => !used(n) }) getOrElse candidates.head
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   184
        (Some(n), copy(numa_index = (i + 1) % available.length))
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   185
      }
77337
1ab68d4370ec tuned signature;
wenzelm
parents: 77336
diff changeset
   186
77335
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   187
    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
   188
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   189
    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
   190
      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
   191
        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
   192
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   193
    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
   194
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   195
    def 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
   196
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   197
    def finished_running(): List[Build_Job.Build_Session] =
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   198
      List.from(
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   199
        running.valuesIterator.flatMap {
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   200
          case job: Build_Job.Build_Session if job.is_finished => Some(job)
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   201
          case _ => None
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   202
        })
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   203
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   204
    def 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
   205
      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
   206
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   207
    def 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
   208
      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
   209
77336
e9beaaf955bf tuned signature;
wenzelm
parents: 77335
diff changeset
   210
    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
   211
      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
   212
      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
   213
      output_heap: SHA1.Shasum,
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   214
      process_result: 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
   215
    ): State = {
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   216
      val result = Build_Process.Result(current, output_heap, 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
   217
      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
   218
    }
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   219
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   220
    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
   221
      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
   222
  }
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   223
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   224
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   225
  /* main process */
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   226
77312
6a6231370432 clarified signature (see also 68a7ad1385bc);
wenzelm
parents: 77310
diff changeset
   227
  def session_finished(session_name: String, process_result: Process_Result): String =
6a6231370432 clarified signature (see also 68a7ad1385bc);
wenzelm
parents: 77310
diff changeset
   228
    "Finished " + session_name + " (" + process_result.timing.message_resources + ")"
6a6231370432 clarified signature (see also 68a7ad1385bc);
wenzelm
parents: 77310
diff changeset
   229
6a6231370432 clarified signature (see also 68a7ad1385bc);
wenzelm
parents: 77310
diff changeset
   230
  def session_timing(session_name: String, build_log: Build_Log.Session_Info): String = {
6a6231370432 clarified signature (see also 68a7ad1385bc);
wenzelm
parents: 77310
diff changeset
   231
    val props = build_log.session_timing
6a6231370432 clarified signature (see also 68a7ad1385bc);
wenzelm
parents: 77310
diff changeset
   232
    val threads = Markup.Session_Timing.Threads.unapply(props) getOrElse 1
6a6231370432 clarified signature (see also 68a7ad1385bc);
wenzelm
parents: 77310
diff changeset
   233
    val timing = Markup.Timing_Properties.get(props)
6a6231370432 clarified signature (see also 68a7ad1385bc);
wenzelm
parents: 77310
diff changeset
   234
    "Timing " + session_name + " (" + threads + " threads, " + timing.message_factor + ")"
6a6231370432 clarified signature (see also 68a7ad1385bc);
wenzelm
parents: 77310
diff changeset
   235
  }
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   236
}
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   237
77331
38643c64b1e2 clarified signature: support meaningful subclasses for Build.Engine implementations;
wenzelm
parents: 77329
diff changeset
   238
class Build_Process(protected val build_context: Build_Process.Context) {
77338
0a91c697a18a tuned signature: avoid warnings in IntelliJ IDEA;
wenzelm
parents: 77337
diff changeset
   239
  protected val store: Sessions.Store = build_context.store
0a91c697a18a tuned signature: avoid warnings in IntelliJ IDEA;
wenzelm
parents: 77337
diff changeset
   240
  protected val build_options: Options = store.options
0a91c697a18a tuned signature: avoid warnings in IntelliJ IDEA;
wenzelm
parents: 77337
diff changeset
   241
  protected val build_deps: Sessions.Deps = build_context.deps
0a91c697a18a tuned signature: avoid warnings in IntelliJ IDEA;
wenzelm
parents: 77337
diff changeset
   242
  protected val progress: Progress = build_context.progress
0a91c697a18a tuned signature: avoid warnings in IntelliJ IDEA;
wenzelm
parents: 77337
diff changeset
   243
  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
   244
77338
0a91c697a18a tuned signature: avoid warnings in IntelliJ IDEA;
wenzelm
parents: 77337
diff changeset
   245
  protected val log: Logger =
77287
wenzelm
parents: 77285
diff changeset
   246
    build_options.string("system_log") match {
wenzelm
parents: 77285
diff changeset
   247
      case "" => No_Logger
wenzelm
parents: 77285
diff changeset
   248
      case "-" => Logger.make(progress)
wenzelm
parents: 77285
diff changeset
   249
      case log_file => Logger.make(Some(Path.explode(log_file)))
wenzelm
parents: 77285
diff changeset
   250
    }
wenzelm
parents: 77285
diff changeset
   251
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   252
  // global state
77338
0a91c697a18a tuned signature: avoid warnings in IntelliJ IDEA;
wenzelm
parents: 77337
diff changeset
   253
  protected var _state: Build_Process.State = init_state()
77333
0bec014c0f9f tuned signature;
wenzelm
parents: 77332
diff changeset
   254
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   255
  protected def init_state(): Build_Process.State =
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   256
    Build_Process.State(pending =
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   257
      (for ((name, (_, (preds, _))) <- build_context.sessions_structure.build_graph.iterator)
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   258
        yield Build_Process.Entry(name, preds.toList)).toList)
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   259
77335
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   260
  protected def finished(): Boolean = synchronized { _state.finished }
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   261
77331
38643c64b1e2 clarified signature: support meaningful subclasses for Build.Engine implementations;
wenzelm
parents: 77329
diff changeset
   262
  protected def next_pending(): Option[String] = synchronized {
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   263
    if (_state.running.size < (build_context.max_jobs max 1)) {
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   264
      _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
   265
        .sortBy(_.name)(build_context.ordering)
f8aa1647d156 more elementary data structures, to fit better to SQL database;
wenzelm
parents: 77312
diff changeset
   266
        .headOption.map(_.name)
77296
eeaa2872320b clarified signature: more explicit synchronized operations;
wenzelm
parents: 77295
diff changeset
   267
    }
eeaa2872320b clarified signature: more explicit synchronized operations;
wenzelm
parents: 77295
diff changeset
   268
    else None
77289
c7d893278aec proper synchronized access to mutable state, to support concurrency eventually;
wenzelm
parents: 77288
diff changeset
   269
  }
77259
61fc2afe4c8b clarified signature: prefer stateful object-oriented style, to make it fit better into physical world;
wenzelm
parents: 77258
diff changeset
   270
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   271
  protected def stop_running(): Unit = synchronized { _state.stop_running() }
77295
ab13ac27c74a clarified signature: more explicit synchronized operations;
wenzelm
parents: 77294
diff changeset
   272
77331
38643c64b1e2 clarified signature: support meaningful subclasses for Build.Engine implementations;
wenzelm
parents: 77329
diff changeset
   273
  protected def finished_running(): List[Build_Job.Build_Session] = synchronized {
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   274
    _state.finished_running()
77296
eeaa2872320b clarified signature: more explicit synchronized operations;
wenzelm
parents: 77295
diff changeset
   275
  }
eeaa2872320b clarified signature: more explicit synchronized operations;
wenzelm
parents: 77295
diff changeset
   276
77331
38643c64b1e2 clarified signature: support meaningful subclasses for Build.Engine implementations;
wenzelm
parents: 77329
diff changeset
   277
  protected def finish_job(job: Build_Job.Build_Session): Unit = {
77296
eeaa2872320b clarified signature: more explicit synchronized operations;
wenzelm
parents: 77295
diff changeset
   278
    val session_name = job.session_name
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   279
    val process_result = job.join
77294
3f2b1419f598 clarified modules (again);
wenzelm
parents: 77293
diff changeset
   280
    val output_heap = job.finish
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   281
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   282
    val log_lines = process_result.out_lines.filterNot(Protocol_Message.Marker.test)
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   283
    val process_result_tail = {
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   284
      val tail = job.info.options.int("process_output_tail")
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   285
      process_result.copy(
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   286
        out_lines =
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   287
          "(more details via \"isabelle log -H Error " + session_name + "\")" ::
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   288
          (if (tail == 0) log_lines else log_lines.drop(log_lines.length - tail max 0)))
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   289
    }
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   290
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   291
    val build_log =
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   292
      Build_Log.Log_File(session_name, process_result.out_lines).
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   293
        parse_session_info(
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   294
          command_timings = true,
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   295
          theory_timings = true,
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   296
          ml_statistics = true,
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   297
          task_statistics = true)
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   298
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   299
    // write log file
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   300
    if (process_result.ok) {
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   301
      File.write_gzip(store.output_log_gz(session_name), terminate_lines(log_lines))
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   302
    }
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   303
    else File.write(store.output_log(session_name), terminate_lines(log_lines))
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   304
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   305
    // write database
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   306
    using(store.open_database(session_name, output = true))(db =>
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   307
      store.write_session_info(db, session_name, job.session_sources,
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   308
        build_log =
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   309
          if (process_result.timeout) build_log.error("Timeout") else build_log,
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   310
        build =
77285
f04672649483 clarified signature;
wenzelm
parents: 77284
diff changeset
   311
          Sessions.Build_Info(build_deps.sources_shasum(session_name), job.input_heaps,
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   312
            output_heap, process_result.rc, UUID.random().toString)))
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   313
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   314
    // messages
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   315
    process_result.err_lines.foreach(progress.echo)
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   316
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   317
    if (process_result.ok) {
77312
6a6231370432 clarified signature (see also 68a7ad1385bc);
wenzelm
parents: 77310
diff changeset
   318
      if (verbose) progress.echo(Build_Process.session_timing(session_name, build_log))
6a6231370432 clarified signature (see also 68a7ad1385bc);
wenzelm
parents: 77310
diff changeset
   319
      progress.echo(Build_Process.session_finished(session_name, process_result))
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   320
    }
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   321
    else {
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   322
      progress.echo(session_name + " FAILED")
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   323
      if (!process_result.interrupted) progress.echo(process_result_tail.out)
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   324
    }
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   325
77289
c7d893278aec proper synchronized access to mutable state, to support concurrency eventually;
wenzelm
parents: 77288
diff changeset
   326
    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
   327
      _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
   328
        remove_pending(session_name).
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   329
        remove_running(session_name).
77336
e9beaaf955bf tuned signature;
wenzelm
parents: 77335
diff changeset
   330
        make_result(session_name, false, output_heap, process_result_tail)
77289
c7d893278aec proper synchronized access to mutable state, to support concurrency eventually;
wenzelm
parents: 77288
diff changeset
   331
    }
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   332
  }
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   333
77331
38643c64b1e2 clarified signature: support meaningful subclasses for Build.Engine implementations;
wenzelm
parents: 77329
diff changeset
   334
  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
   335
    val ancestor_results = synchronized {
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   336
      _state.get_results(
77295
ab13ac27c74a clarified signature: more explicit synchronized operations;
wenzelm
parents: 77294
diff changeset
   337
        build_deps.sessions_structure.build_requirements(List(session_name)).
ab13ac27c74a clarified signature: more explicit synchronized operations;
wenzelm
parents: 77294
diff changeset
   338
          filterNot(_ == session_name))
77334
0231e62956a6 clarified state: more explicit type as plain value, which is also easier to sync with external db;
wenzelm
parents: 77333
diff changeset
   339
    }
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   340
    val input_heaps =
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   341
      if (ancestor_results.isEmpty) {
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   342
        SHA1.shasum_meta_info(SHA1.digest(Path.explode("$POLYML_EXE")))
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   343
      }
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   344
      else SHA1.flat_shasum(ancestor_results.map(_.output_heap))
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   345
77315
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   346
    val do_store = build_context.do_store(session_name)
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   347
    val (current, output_heap) = {
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   348
      store.try_open_database(session_name) match {
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   349
        case Some(db) =>
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   350
          using(db)(store.read_build(_, session_name)) match {
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   351
            case Some(build) =>
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   352
              val output_heap = store.find_heap_shasum(session_name)
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   353
              val current =
77315
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   354
                !build_context.fresh_build &&
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   355
                build.ok &&
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   356
                build.sources == build_deps.sources_shasum(session_name) &&
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   357
                build.input_heaps == input_heaps &&
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   358
                build.output_heap == output_heap &&
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   359
                !(do_store && output_heap.is_empty)
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   360
              (current, output_heap)
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   361
            case None => (false, SHA1.no_shasum)
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   362
          }
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   363
        case None => (false, SHA1.no_shasum)
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   364
      }
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   365
    }
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   366
    val all_current = current && ancestor_results.forall(_.current)
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   367
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   368
    if (all_current) {
77289
c7d893278aec proper synchronized access to mutable state, to support concurrency eventually;
wenzelm
parents: 77288
diff changeset
   369
      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
   370
        _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
   371
          remove_pending(session_name).
77336
e9beaaf955bf tuned signature;
wenzelm
parents: 77335
diff changeset
   372
          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
   373
      }
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   374
    }
77315
f34559b24277 clarified signature: move all parameters into Build_Process.Context;
wenzelm
parents: 77314
diff changeset
   375
    else if (build_context.no_build) {
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   376
      progress.echo_if(verbose, "Skipping " + session_name + " ...")
77289
c7d893278aec proper synchronized access to mutable state, to support concurrency eventually;
wenzelm
parents: 77288
diff changeset
   377
      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
   378
        _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
   379
          remove_pending(session_name).
77336
e9beaaf955bf tuned signature;
wenzelm
parents: 77335
diff changeset
   380
          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
   381
      }
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   382
    }
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   383
    else if (ancestor_results.forall(_.ok) && !progress.stopped) {
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   384
      progress.echo((if (do_store) "Building " else "Running ") + session_name + " ...")
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   385
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   386
      store.clean_output(session_name)
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   387
      using(store.open_database(session_name, output = true))(
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   388
        store.init_session_info(_, session_name))
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   389
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   390
      val session_background = build_deps.background(session_name)
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   391
      val resources =
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   392
        new Resources(session_background, log = log,
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   393
          command_timings = build_context(session_name).old_command_timings)
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   394
77291
f7e413e8d269 more robust: first register job, then start job;
wenzelm
parents: 77290
diff changeset
   395
      val job =
f7e413e8d269 more robust: first register job, then start job;
wenzelm
parents: 77290
diff changeset
   396
        synchronized {
77343
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   397
          val (numa_node, state1) = _state.numa_next(build_context.numa_nodes)
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   398
          val job =
77297
226a880d0423 clarified types: support a variety of Build_Job instances;
wenzelm
parents: 77296
diff changeset
   399
            new Build_Job.Build_Session(progress, session_background, store, do_store,
77343
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   400
              resources, build_context.session_setup, input_heaps, numa_node)
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   401
          _state = state1.add_running(session_name, job)
db479840d6ad clarified signature;
wenzelm
parents: 77339
diff changeset
   402
          job
77291
f7e413e8d269 more robust: first register job, then start job;
wenzelm
parents: 77290
diff changeset
   403
        }
f7e413e8d269 more robust: first register job, then start job;
wenzelm
parents: 77290
diff changeset
   404
      job.start()
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   405
    }
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   406
    else {
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   407
      progress.echo(session_name + " CANCELLED")
77289
c7d893278aec proper synchronized access to mutable state, to support concurrency eventually;
wenzelm
parents: 77288
diff changeset
   408
      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
   409
        _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
   410
          remove_pending(session_name).
77336
e9beaaf955bf tuned signature;
wenzelm
parents: 77335
diff changeset
   411
          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
   412
      }
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   413
    }
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   414
  }
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   415
77331
38643c64b1e2 clarified signature: support meaningful subclasses for Build.Engine implementations;
wenzelm
parents: 77329
diff changeset
   416
  protected def sleep(): Unit =
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   417
    Isabelle_Thread.interrupt_handler(_ => progress.stop()) {
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   418
      build_options.seconds("editor_input_delay").sleep()
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   419
    }
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   420
77310
6754b5eceb12 clarified modules;
wenzelm
parents: 77297
diff changeset
   421
  def run(): Map[String, Process_Result] = {
77335
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   422
    if (finished()) {
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   423
      progress.echo_warning("Nothing to build")
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   424
      Map.empty[String, Process_Result]
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   425
    }
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   426
    else {
05b97b54cb3b tuned signature;
wenzelm
parents: 77334
diff changeset
   427
      while (!finished()) {
77310
6754b5eceb12 clarified modules;
wenzelm
parents: 77297
diff changeset
   428
        if (progress.stopped) stop_running()
6754b5eceb12 clarified modules;
wenzelm
parents: 77297
diff changeset
   429
6754b5eceb12 clarified modules;
wenzelm
parents: 77297
diff changeset
   430
        for (job <- finished_running()) finish_job(job)
77260
58397b40df2b clarified main operations;
wenzelm
parents: 77259
diff changeset
   431
77310
6754b5eceb12 clarified modules;
wenzelm
parents: 77297
diff changeset
   432
        next_pending() match {
77339
c81abb66a97f tuned signature;
wenzelm
parents: 77338
diff changeset
   433
          case Some(name) => start_job(name)
77310
6754b5eceb12 clarified modules;
wenzelm
parents: 77297
diff changeset
   434
          case None => sleep()
6754b5eceb12 clarified modules;
wenzelm
parents: 77297
diff changeset
   435
        }
6754b5eceb12 clarified modules;
wenzelm
parents: 77297
diff changeset
   436
      }
6754b5eceb12 clarified modules;
wenzelm
parents: 77297
diff changeset
   437
      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
   438
        for ((name, result) <- _state.results) yield name -> result.process_result
77257
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   439
      }
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   440
    }
68a7ad1385bc clarified modules;
wenzelm
parents: 77256
diff changeset
   441
  }
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
   442
}