src/Pure/Tools/build_process.scala
author wenzelm
Sat, 11 Feb 2023 22:59:23 +0100
changeset 77254 8d34f53871b4
parent 77249 f3f1b7ad1d0d
child 77255 b810e99b5afb
permissions -rw-r--r--
clarified signature: make dynamic Queue from static Context;
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
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    12
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    13
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    14
object Build_Process {
77244
2e5a3955bc69 clarified signature and terminology;
wenzelm
parents: 77241
diff changeset
    15
  /* static information */
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    16
77244
2e5a3955bc69 clarified signature and terminology;
wenzelm
parents: 77241
diff changeset
    17
  object Session_Context {
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    18
    def empty(session: String, timeout: Time): Session_Context =
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    19
      new Session_Context(session, timeout, Time.zero, Nil)
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    20
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    21
    def apply(
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    22
      session: String,
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    23
      timeout: Time,
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    24
      store: Sessions.Store,
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    25
      progress: Progress = new Progress
77244
2e5a3955bc69 clarified signature and terminology;
wenzelm
parents: 77241
diff changeset
    26
    ): Session_Context = {
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    27
      store.try_open_database(session) match {
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    28
        case None => empty(session, timeout)
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    29
        case Some(db) =>
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    30
          def ignore_error(msg: String) = {
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    31
            progress.echo_warning("Ignoring bad database " + db +
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    32
              " for session " + quote(session) + (if (msg == "") "" else ":\n" + msg))
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    33
            empty(session, timeout)
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    34
          }
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    35
          try {
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    36
            val command_timings = store.read_command_timings(db, session)
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    37
            val elapsed =
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    38
              store.read_session_timing(db, session) match {
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    39
                case Markup.Elapsed(s) => Time.seconds(s)
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    40
                case _ => Time.zero
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    41
              }
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    42
            new Session_Context(session, timeout, elapsed, command_timings)
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    43
          }
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    44
          catch {
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    45
            case ERROR(msg) => ignore_error(msg)
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    46
            case exn: java.lang.Error => ignore_error(Exn.message(exn))
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    47
            case _: XML.Error => ignore_error("XML.Error")
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    48
          }
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    49
          finally { db.close() }
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    50
      }
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    51
    }
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
    52
  }
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    53
77244
2e5a3955bc69 clarified signature and terminology;
wenzelm
parents: 77241
diff changeset
    54
  final class Session_Context(
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    55
    val session: String,
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    56
    val timeout: Time,
77244
2e5a3955bc69 clarified signature and terminology;
wenzelm
parents: 77241
diff changeset
    57
    val old_time: Time,
2e5a3955bc69 clarified signature and terminology;
wenzelm
parents: 77241
diff changeset
    58
    val old_command_timings: List[Properties.T]
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    59
  ) {
77244
2e5a3955bc69 clarified signature and terminology;
wenzelm
parents: 77241
diff changeset
    60
    def is_empty: Boolean = old_time.is_zero && old_command_timings.isEmpty
77240
2ff94ba22140 tuned signature: more operations;
wenzelm
parents: 77239
diff changeset
    61
77245
1e2670d9dc18 tuned message: old_time not sufficiently prominent nor accurate to be printed;
wenzelm
parents: 77244
diff changeset
    62
    override def toString: String = session
77239
b9bf4c0bd47d clarified signature: more explicit types;
wenzelm
parents: 77238
diff changeset
    63
  }
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    64
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    65
  object Context {
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    66
    def apply(
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    67
      sessions_structure: Sessions.Structure,
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    68
      store: Sessions.Store,
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    69
      progress: Progress = new Progress
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    70
    ): Context = {
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    71
      val build_graph = sessions_structure.build_graph
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    72
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    73
      val sessions =
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
    74
        Map.from(
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    75
          for (name <- build_graph.keys_iterator)
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    76
          yield {
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    77
            val timeout = sessions_structure(name).timeout
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    78
            name -> Build_Process.Session_Context(name, timeout, store, progress = progress)
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
    79
          })
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    80
77248
wenzelm
parents: 77247
diff changeset
    81
      val sessions_time = {
wenzelm
parents: 77247
diff changeset
    82
        val maximals = build_graph.maximals.toSet
wenzelm
parents: 77247
diff changeset
    83
        def descendants_time(name: String): Double = {
wenzelm
parents: 77247
diff changeset
    84
          if (maximals.contains(name)) sessions(name).old_time.seconds
wenzelm
parents: 77247
diff changeset
    85
          else {
wenzelm
parents: 77247
diff changeset
    86
            val descendants = build_graph.all_succs(List(name)).toSet
wenzelm
parents: 77247
diff changeset
    87
            val g = build_graph.restrict(descendants)
wenzelm
parents: 77247
diff changeset
    88
            (0.0 :: g.maximals.flatMap { desc =>
wenzelm
parents: 77247
diff changeset
    89
              val ps = g.all_preds(List(desc))
wenzelm
parents: 77247
diff changeset
    90
              if (ps.exists(p => !sessions.isDefinedAt(p))) None
wenzelm
parents: 77247
diff changeset
    91
              else Some(ps.map(p => sessions(p).old_time.seconds).sum)
wenzelm
parents: 77247
diff changeset
    92
            }).max
wenzelm
parents: 77247
diff changeset
    93
          }
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    94
        }
77248
wenzelm
parents: 77247
diff changeset
    95
        Map.from(
wenzelm
parents: 77247
diff changeset
    96
          for (name <- sessions.keysIterator)
wenzelm
parents: 77247
diff changeset
    97
          yield name -> descendants_time(name)).withDefaultValue(0.0)
77247
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    98
      }
6ed94a0ec723 misc tuning and clarification;
wenzelm
parents: 77246
diff changeset
    99
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   100
      val ordering =
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   101
        new Ordering[String] {
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   102
          def compare(name1: String, name2: String): Int =
77248
wenzelm
parents: 77247
diff changeset
   103
            sessions_time(name2) compare sessions_time(name1) match {
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   104
              case 0 =>
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
   105
                sessions(name2).timeout compare sessions(name1).timeout match {
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   106
                  case 0 => name1 compare name2
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   107
                  case ord => ord
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   108
                }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   109
              case ord => ord
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   110
            }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   111
        }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   112
77254
8d34f53871b4 clarified signature: make dynamic Queue from static Context;
wenzelm
parents: 77249
diff changeset
   113
      new Context(sessions_structure, sessions, ordering)
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   114
    }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   115
  }
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   116
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   117
  final class Context private(
77254
8d34f53871b4 clarified signature: make dynamic Queue from static Context;
wenzelm
parents: 77249
diff changeset
   118
    val sessions_structure: Sessions.Structure,
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   119
    sessions: Map[String, Session_Context],
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   120
    val ordering: Ordering[String]
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   121
  ) {
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   122
    def apply(session: String): Session_Context =
77249
f3f1b7ad1d0d clarified data structure: more direct access to timeout;
wenzelm
parents: 77248
diff changeset
   123
      sessions.getOrElse(session, Session_Context.empty(session, Time.zero))
77246
173c2fb78290 clarified modules;
wenzelm
parents: 77245
diff changeset
   124
  }
77238
02308a0ddf30 clarified modules;
wenzelm
parents:
diff changeset
   125
}