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