src/Pure/Tools/build_log.scala
author wenzelm
Sun Oct 09 15:28:18 2016 +0200 (2016-10-09)
changeset 64119 8094eaa38d4b
parent 64117 c2b41b073d8a
child 64120 6c5039016321
permissions -rw-r--r--
inline session ML statistics into main build log;
tuned;
wenzelm@64045
     1
/*  Title:      Pure/Tools/build_log.scala
wenzelm@64045
     2
    Author:     Makarius
wenzelm@64045
     3
wenzelm@64110
     4
Build log parsing for current and historic formats.
wenzelm@64045
     5
*/
wenzelm@64045
     6
wenzelm@64045
     7
package isabelle
wenzelm@64045
     8
wenzelm@64045
     9
wenzelm@64100
    10
import java.io.{File => JFile}
wenzelm@64100
    11
import java.time.ZoneId
wenzelm@64110
    12
import java.time.format.{DateTimeFormatter, DateTimeParseException}
wenzelm@64096
    13
import java.util.Locale
wenzelm@64061
    14
wenzelm@64054
    15
import scala.collection.mutable
wenzelm@64054
    16
import scala.util.matching.Regex
wenzelm@64054
    17
wenzelm@64054
    18
wenzelm@64045
    19
object Build_Log
wenzelm@64045
    20
{
wenzelm@64101
    21
  /** directory content **/
wenzelm@64101
    22
wenzelm@64101
    23
  def is_log(file: JFile): Boolean =
wenzelm@64101
    24
    List(".log", ".log.gz", ".log.xz").exists(ext => file.getName.endsWith(ext))
wenzelm@64101
    25
wenzelm@64101
    26
  def isatest_files(dir: Path): List[JFile] =
wenzelm@64101
    27
    File.find_files(dir.file, file => is_log(file) && file.getName.startsWith("isatest-makeall-"))
wenzelm@64101
    28
wenzelm@64101
    29
  def afp_test_files(dir: Path): List[JFile] =
wenzelm@64101
    30
    File.find_files(dir.file, file => is_log(file) && file.getName.startsWith("afp-test-devel-"))
wenzelm@64101
    31
wenzelm@64101
    32
wenzelm@64101
    33
wenzelm@64081
    34
  /** settings **/
wenzelm@64080
    35
wenzelm@64081
    36
  object Settings
wenzelm@64081
    37
  {
wenzelm@64081
    38
    val build_settings = List("ISABELLE_BUILD_OPTIONS")
wenzelm@64081
    39
    val ml_settings = List("ML_PLATFORM", "ML_HOME", "ML_SYSTEM", "ML_OPTIONS")
wenzelm@64081
    40
    val all_settings = build_settings ::: ml_settings
wenzelm@64081
    41
wenzelm@64081
    42
    type Entry = (String, String)
wenzelm@64081
    43
    type T = List[Entry]
wenzelm@64080
    44
wenzelm@64081
    45
    object Entry
wenzelm@64081
    46
    {
wenzelm@64081
    47
      def unapply(s: String): Option[Entry] =
wenzelm@64081
    48
        s.indexOf('=') match {
wenzelm@64081
    49
          case -1 => None
wenzelm@64081
    50
          case i =>
wenzelm@64081
    51
            val a = s.substring(0, i)
wenzelm@64081
    52
            val b = Library.perhaps_unquote(s.substring(i + 1))
wenzelm@64081
    53
            Some((a, b))
wenzelm@64081
    54
        }
wenzelm@64081
    55
      def apply(a: String, b: String): String = a + "=" + quote(b)
wenzelm@64081
    56
      def getenv(a: String): String = apply(a, Isabelle_System.getenv(a))
wenzelm@64081
    57
    }
wenzelm@64080
    58
wenzelm@64081
    59
    def show(): String =
wenzelm@64081
    60
      cat_lines(
wenzelm@64081
    61
        build_settings.map(Entry.getenv(_)) ::: List("") ::: ml_settings.map(Entry.getenv(_)))
wenzelm@64080
    62
  }
wenzelm@64080
    63
wenzelm@64080
    64
wenzelm@64100
    65
wenzelm@64062
    66
  /** log file **/
wenzelm@64045
    67
wenzelm@64062
    68
  object Log_File
wenzelm@64062
    69
  {
wenzelm@64062
    70
    def apply(name: String, lines: List[String]): Log_File =
wenzelm@64062
    71
      new Log_File(name, lines)
wenzelm@64062
    72
wenzelm@64062
    73
    def apply(name: String, text: String): Log_File =
wenzelm@64063
    74
      Log_File(name, Library.trim_split_lines(text))
wenzelm@64090
    75
wenzelm@64090
    76
    def apply(file: JFile): Log_File =
wenzelm@64090
    77
    {
wenzelm@64090
    78
      val name = file.getName
wenzelm@64090
    79
      val (base_name, text) =
wenzelm@64090
    80
        Library.try_unsuffix(".gz", name) match {
wenzelm@64090
    81
          case Some(base_name) => (base_name, File.read_gzip(file))
wenzelm@64090
    82
          case None =>
wenzelm@64090
    83
            Library.try_unsuffix(".xz", name) match {
wenzelm@64090
    84
              case Some(base_name) => (base_name, File.read_xz(file))
wenzelm@64090
    85
              case None => (name, File.read(file))
wenzelm@64090
    86
            }
wenzelm@64090
    87
          }
wenzelm@64090
    88
      apply(base_name, text)
wenzelm@64090
    89
    }
wenzelm@64090
    90
wenzelm@64090
    91
    def apply(path: Path): Log_File = apply(path.file)
wenzelm@64101
    92
wenzelm@64110
    93
wenzelm@64110
    94
    /* date format */
wenzelm@64110
    95
wenzelm@64101
    96
    val Date_Format =
wenzelm@64101
    97
    {
wenzelm@64101
    98
      val fmts =
wenzelm@64101
    99
        Date.Formatter.variants(
wenzelm@64116
   100
          List("EEE MMM d HH:mm:ss O yyyy", "EEE MMM d HH:mm:ss VV yyyy"),
wenzelm@64104
   101
          List(Locale.ENGLISH, Locale.GERMAN)) :::
wenzelm@64110
   102
        List(
wenzelm@64110
   103
          DateTimeFormatter.RFC_1123_DATE_TIME,
wenzelm@64110
   104
          Date.Formatter.pattern("EEE MMM d HH:mm:ss yyyy").withZone(ZoneId.of("Europe/Berlin")))
wenzelm@64101
   105
wenzelm@64104
   106
      def tune_timezone(s: String): String =
wenzelm@64104
   107
        s match {
wenzelm@64101
   108
          case "CET" | "MET" => "GMT+1"
wenzelm@64101
   109
          case "CEST" | "MEST" => "GMT+2"
wenzelm@64104
   110
          case "EST" => "Europe/Berlin"
wenzelm@64104
   111
          case _ => s
wenzelm@64104
   112
        }
wenzelm@64104
   113
      def tune_weekday(s: String): String =
wenzelm@64104
   114
        s match {
wenzelm@64104
   115
          case "Die" => "Di"
wenzelm@64104
   116
          case "Mit" => "Mi"
wenzelm@64104
   117
          case "Don" => "Do"
wenzelm@64104
   118
          case "Fre" => "Fr"
wenzelm@64104
   119
          case "Sam" => "Sa"
wenzelm@64104
   120
          case "Son" => "So"
wenzelm@64104
   121
          case _ => s
wenzelm@64104
   122
        }
wenzelm@64104
   123
wenzelm@64104
   124
      def tune(s: String): String =
wenzelm@64104
   125
        Word.implode(
wenzelm@64104
   126
          Word.explode(s) match {
wenzelm@64104
   127
            case a :: "M\uFFFDr" :: bs => tune_weekday(a) :: "Mär" :: bs.map(tune_timezone(_))
wenzelm@64104
   128
            case a :: bs => tune_weekday(a) :: bs.map(tune_timezone(_))
wenzelm@64104
   129
            case Nil => Nil
wenzelm@64104
   130
          }
wenzelm@64104
   131
        )
wenzelm@64101
   132
wenzelm@64101
   133
      Date.Format.make(fmts, tune)
wenzelm@64101
   134
    }
wenzelm@64117
   135
wenzelm@64117
   136
wenzelm@64117
   137
    /* inlined content */
wenzelm@64117
   138
wenzelm@64119
   139
    def print_props(marker: String, props: Properties.T): String =
wenzelm@64119
   140
      marker + YXML.string_of_body(XML.Encode.properties(props))
wenzelm@64102
   141
  }
wenzelm@64102
   142
wenzelm@64102
   143
  class Log_File private(val name: String, val lines: List[String])
wenzelm@64102
   144
  {
wenzelm@64102
   145
    log_file =>
wenzelm@64102
   146
wenzelm@64102
   147
    override def toString: String = name
wenzelm@64102
   148
wenzelm@64102
   149
    def text: String = cat_lines(lines)
wenzelm@64102
   150
wenzelm@64102
   151
    def err(msg: String): Nothing =
wenzelm@64102
   152
      error("Error in log file " + quote(name) + ": " + msg)
wenzelm@64102
   153
wenzelm@64102
   154
wenzelm@64102
   155
    /* date format */
wenzelm@64101
   156
wenzelm@64101
   157
    object Strict_Date
wenzelm@64101
   158
    {
wenzelm@64101
   159
      def unapply(s: String): Some[Date] =
wenzelm@64102
   160
        try { Some(Log_File.Date_Format.parse(s)) }
wenzelm@64101
   161
        catch { case exn: DateTimeParseException => log_file.err(exn.getMessage) }
wenzelm@64101
   162
    }
wenzelm@64101
   163
wenzelm@64101
   164
wenzelm@64062
   165
    /* inlined content */
wenzelm@64062
   166
wenzelm@64062
   167
    def find[A](f: String => Option[A]): Option[A] =
wenzelm@64062
   168
      lines.iterator.map(f).find(_.isDefined).map(_.get)
wenzelm@64062
   169
wenzelm@64062
   170
    def find_match(regex: Regex): Option[String] =
wenzelm@64062
   171
      lines.iterator.map(regex.unapplySeq(_)).find(res => res.isDefined && res.get.length == 1).
wenzelm@64062
   172
        map(res => res.get.head)
wenzelm@64062
   173
wenzelm@64062
   174
wenzelm@64062
   175
    /* settings */
wenzelm@64062
   176
wenzelm@64091
   177
    def get_setting(a: String): Option[Settings.Entry] =
wenzelm@64091
   178
      lines.find(_.startsWith(a + "=")) match {
wenzelm@64091
   179
        case Some(line) => Settings.Entry.unapply(line)
wenzelm@64091
   180
        case None => None
wenzelm@64091
   181
      }
wenzelm@64045
   182
wenzelm@64091
   183
    def get_settings(as: List[String]): Settings.T =
wenzelm@64091
   184
      for { a <- as; entry <- get_setting(a) } yield entry
wenzelm@64062
   185
wenzelm@64062
   186
wenzelm@64062
   187
    /* properties (YXML) */
wenzelm@64062
   188
wenzelm@64062
   189
    val xml_cache = new XML.Cache()
wenzelm@64062
   190
wenzelm@64062
   191
    def parse_props(text: String): Properties.T =
wenzelm@64062
   192
      xml_cache.props(XML.Decode.properties(YXML.parse_body(text)))
wenzelm@64062
   193
wenzelm@64119
   194
    def filter_props(marker: String): List[Properties.T] =
wenzelm@64119
   195
      for (line <- lines; s <- Library.try_unprefix(marker, line)) yield parse_props(s)
wenzelm@64045
   196
wenzelm@64119
   197
    def find_line(marker: String): Option[String] =
wenzelm@64119
   198
      find(Library.try_unprefix(marker, _))
wenzelm@64062
   199
wenzelm@64119
   200
    def find_props(marker: String): Option[Properties.T] =
wenzelm@64119
   201
      find_line(marker).map(parse_props(_))
wenzelm@64062
   202
wenzelm@64062
   203
wenzelm@64062
   204
    /* parse various formats */
wenzelm@64062
   205
wenzelm@64105
   206
    def parse_meta_info(): Meta_Info = Build_Log.parse_meta_info(log_file)
wenzelm@64105
   207
wenzelm@64105
   208
    def parse_build_info(): Build_Info = Build_Log.parse_build_info(log_file)
wenzelm@64105
   209
wenzelm@64082
   210
    def parse_session_info(
wenzelm@64085
   211
        default_name: String = "",
wenzelm@64082
   212
        command_timings: Boolean = false,
wenzelm@64082
   213
        ml_statistics: Boolean = false,
wenzelm@64082
   214
        task_statistics: Boolean = false): Session_Info =
wenzelm@64082
   215
      Build_Log.parse_session_info(
wenzelm@64085
   216
        log_file, default_name, command_timings, ml_statistics, task_statistics)
wenzelm@64045
   217
  }
wenzelm@64045
   218
wenzelm@64045
   219
wenzelm@64098
   220
wenzelm@64105
   221
  /** meta info **/
wenzelm@64045
   222
wenzelm@64099
   223
  object Field
wenzelm@64045
   224
  {
wenzelm@64108
   225
    val build_engine = "build_engine"
wenzelm@64099
   226
    val build_host = "build_host"
wenzelm@64099
   227
    val build_start = "build_start"
wenzelm@64099
   228
    val build_end = "build_end"
wenzelm@64099
   229
    val isabelle_version = "isabelle_version"
wenzelm@64099
   230
    val afp_version = "afp_version"
wenzelm@64045
   231
  }
wenzelm@64054
   232
wenzelm@64108
   233
  object Meta_Info
wenzelm@64099
   234
  {
wenzelm@64108
   235
    val empty: Meta_Info = Meta_Info(Nil, Nil)
wenzelm@64099
   236
  }
wenzelm@64098
   237
wenzelm@64108
   238
  sealed case class Meta_Info(props: Properties.T, settings: List[(String, String)])
wenzelm@64103
   239
  {
wenzelm@64103
   240
    def is_empty: Boolean = props.isEmpty && settings.isEmpty
wenzelm@64103
   241
  }
wenzelm@64061
   242
wenzelm@64095
   243
  object Isatest
wenzelm@64095
   244
  {
wenzelm@64108
   245
    val engine = "isatest"
wenzelm@64109
   246
    val Start = new Regex("""^------------------- starting test --- (.+) --- (.+)$""")
wenzelm@64109
   247
    val End = new Regex("""^------------------- test (?:successful|FAILED) --- (.+) --- .*$""")
wenzelm@64095
   248
    val Isabelle_Version = new Regex("""^Isabelle version: (\S+)$""")
wenzelm@64095
   249
    val No_AFP_Version = new Regex("""$.""")
wenzelm@64095
   250
  }
wenzelm@64095
   251
wenzelm@64109
   252
  object AFP_Test
wenzelm@64061
   253
  {
wenzelm@64108
   254
    val engine = "afp-test"
wenzelm@64109
   255
    val Start = new Regex("""^Start test(?: for .+)? at ([^,]+), (.*)$""")
wenzelm@64109
   256
    val Start_Old = new Regex("""^Start test(?: for .+)? at ([^,]+)$""")
wenzelm@64109
   257
    val End = new Regex("""^End test on (.+), .+, elapsed time:.*$""")
wenzelm@64087
   258
    val Isabelle_Version = new Regex("""^Isabelle version: .* -- hg id (\S+)$""")
wenzelm@64087
   259
    val AFP_Version = new Regex("""^AFP version: .* -- hg id (\S+)$""")
wenzelm@64104
   260
    val Bad_Init = new Regex("""^cp:.*: Disc quota exceeded$""")
wenzelm@64061
   261
  }
wenzelm@64061
   262
wenzelm@64110
   263
  object Jenkins
wenzelm@64110
   264
  {
wenzelm@64110
   265
    val engine = "jenkins"
wenzelm@64110
   266
    val Start = new Regex("""^Started .*$""")
wenzelm@64110
   267
    val Start_Date = new Regex("""^Build started at (.+)$""")
wenzelm@64110
   268
    val No_End = new Regex("""$.""")
wenzelm@64110
   269
    val Isabelle_Version = new Regex("""^Isabelle id (\S+)$""")
wenzelm@64110
   270
    val AFP_Version = new Regex("""^AFP id (\S+)$""")
wenzelm@64110
   271
    val CONFIGURATION = "=== CONFIGURATION ==="
wenzelm@64110
   272
    val BUILD = "=== BUILD ==="
wenzelm@64110
   273
    val FINISHED = "Finished: "
wenzelm@64110
   274
  }
wenzelm@64110
   275
wenzelm@64105
   276
  private def parse_meta_info(log_file: Log_File): Meta_Info =
wenzelm@64061
   277
  {
wenzelm@64108
   278
    def parse(engine: String, host: String, start: Date,
wenzelm@64109
   279
      End: Regex, Isabelle_Version: Regex, AFP_Version: Regex): Meta_Info =
wenzelm@64091
   280
    {
wenzelm@64108
   281
      val build_engine = if (engine == "") Nil else List(Field.build_engine -> engine)
wenzelm@64108
   282
      val build_host = if (host == "") Nil else List(Field.build_host -> host)
wenzelm@64108
   283
wenzelm@64108
   284
      val start_date = List(Field.build_start -> start.toString)
wenzelm@64091
   285
      val end_date =
wenzelm@64091
   286
        log_file.lines.last match {
wenzelm@64109
   287
          case End(log_file.Strict_Date(end_date)) =>
wenzelm@64091
   288
            List(Field.build_end -> end_date.toString)
wenzelm@64091
   289
          case _ => Nil
wenzelm@64091
   290
        }
wenzelm@64091
   291
wenzelm@64091
   292
      val isabelle_version =
wenzelm@64095
   293
        log_file.find_match(Isabelle_Version).map(Field.isabelle_version -> _)
wenzelm@64091
   294
      val afp_version =
wenzelm@64095
   295
        log_file.find_match(AFP_Version).map(Field.afp_version -> _)
wenzelm@64062
   296
wenzelm@64108
   297
      Meta_Info(build_engine ::: build_host :::
wenzelm@64108
   298
          start_date ::: end_date ::: isabelle_version.toList ::: afp_version.toList,
wenzelm@64091
   299
        log_file.get_settings(Settings.all_settings))
wenzelm@64091
   300
    }
wenzelm@64091
   301
wenzelm@64091
   302
    log_file.lines match {
wenzelm@64119
   303
      case line :: _ if line.startsWith(Build_History.META_INFO_MARKER) =>
wenzelm@64119
   304
        Meta_Info(log_file.find_props(Build_History.META_INFO_MARKER).get,
wenzelm@64117
   305
          log_file.get_settings(Settings.all_settings))
wenzelm@64117
   306
wenzelm@64109
   307
      case Isatest.Start(log_file.Strict_Date(start), host) :: _ =>
wenzelm@64109
   308
        parse(Isatest.engine, host, start, Isatest.End,
wenzelm@64108
   309
          Isatest.Isabelle_Version, Isatest.No_AFP_Version)
wenzelm@64099
   310
wenzelm@64109
   311
      case AFP_Test.Start(log_file.Strict_Date(start), host) :: _ =>
wenzelm@64109
   312
        parse(AFP_Test.engine, host, start, AFP_Test.End,
wenzelm@64109
   313
          AFP_Test.Isabelle_Version, AFP_Test.AFP_Version)
wenzelm@64099
   314
wenzelm@64109
   315
      case AFP_Test.Start_Old(log_file.Strict_Date(start)) :: _ =>
wenzelm@64109
   316
        parse(AFP_Test.engine, "", start, AFP_Test.End,
wenzelm@64109
   317
          AFP_Test.Isabelle_Version, AFP_Test.AFP_Version)
wenzelm@64099
   318
wenzelm@64110
   319
      case Jenkins.Start() :: _
wenzelm@64110
   320
      if log_file.lines.contains(Jenkins.CONFIGURATION) ||
wenzelm@64110
   321
         log_file.lines.last.startsWith(Jenkins.FINISHED) =>
wenzelm@64110
   322
        log_file.lines.dropWhile(_ != Jenkins.BUILD) match {
wenzelm@64110
   323
          case Jenkins.BUILD :: _ :: Jenkins.Start_Date(log_file.Strict_Date(start)) :: _ =>
wenzelm@64111
   324
            parse(Jenkins.engine, "", start.to(ZoneId.of("Europe/Berlin")), Jenkins.No_End,
wenzelm@64110
   325
              Jenkins.Isabelle_Version, Jenkins.AFP_Version)
wenzelm@64110
   326
          case _ => Meta_Info.empty
wenzelm@64110
   327
        }
wenzelm@64110
   328
wenzelm@64105
   329
      case line :: _ if line.startsWith("\0") => Meta_Info.empty
wenzelm@64109
   330
      case List(Isatest.End(_)) => Meta_Info.empty
wenzelm@64109
   331
      case _ :: AFP_Test.Bad_Init() :: _ => Meta_Info.empty
wenzelm@64105
   332
      case Nil => Meta_Info.empty
wenzelm@64104
   333
wenzelm@64110
   334
      case _ => log_file.err("cannot detect log file format")
wenzelm@64061
   335
    }
wenzelm@64061
   336
  }
wenzelm@64061
   337
wenzelm@64085
   338
wenzelm@64098
   339
wenzelm@64119
   340
  /** build info: produced by isabelle build or build_history **/
wenzelm@64119
   341
wenzelm@64119
   342
  val ML_STATISTICS_MARKER = "\fML_statistics = "
wenzelm@64119
   343
  val SESSION_NAME = "session_name"
wenzelm@64085
   344
wenzelm@64061
   345
  object Session_Status extends Enumeration
wenzelm@64061
   346
  {
wenzelm@64085
   347
    val EXISTING = Value("existing")
wenzelm@64061
   348
    val FINISHED = Value("finished")
wenzelm@64061
   349
    val FAILED = Value("failed")
wenzelm@64061
   350
    val CANCELLED = Value("cancelled")
wenzelm@64061
   351
  }
wenzelm@64061
   352
wenzelm@64085
   353
  sealed case class Session_Entry(
wenzelm@64085
   354
    chapter: String,
wenzelm@64085
   355
    groups: List[String],
wenzelm@64085
   356
    threads: Option[Int],
wenzelm@64089
   357
    timing: Timing,
wenzelm@64089
   358
    ml_timing: Timing,
wenzelm@64119
   359
    ml_statistics: List[Properties.T],
wenzelm@64085
   360
    status: Session_Status.Value)
wenzelm@64085
   361
  {
wenzelm@64085
   362
    def finished: Boolean = status == Session_Status.FINISHED
wenzelm@64085
   363
  }
wenzelm@64054
   364
wenzelm@64085
   365
  sealed case class Build_Info(sessions: Map[String, Session_Entry])
wenzelm@64054
   366
  {
wenzelm@64085
   367
    def session(name: String): Session_Entry = sessions(name)
wenzelm@64085
   368
    def get_session(name: String): Option[Session_Entry] = sessions.get(name)
wenzelm@64054
   369
wenzelm@64089
   370
    def get_default[A](name: String, f: Session_Entry => A, x: A): A =
wenzelm@64085
   371
      get_session(name) match {
wenzelm@64089
   372
        case Some(entry) => f(entry)
wenzelm@64089
   373
        case None => x
wenzelm@64085
   374
      }
wenzelm@64085
   375
wenzelm@64119
   376
    def finished_sessions: List[String] = sessions.keySet.iterator.filter(finished(_)).toList
wenzelm@64089
   377
    def finished(name: String): Boolean = get_default(name, _.finished, false)
wenzelm@64089
   378
    def timing(name: String): Timing = get_default(name, _.timing, Timing.zero)
wenzelm@64089
   379
    def ml_timing(name: String): Timing = get_default(name, _.ml_timing, Timing.zero)
wenzelm@64054
   380
  }
wenzelm@64054
   381
wenzelm@64085
   382
  private def parse_build_info(log_file: Log_File): Build_Info =
wenzelm@64085
   383
  {
wenzelm@64085
   384
    object Chapter_Name
wenzelm@64085
   385
    {
wenzelm@64085
   386
      def unapply(s: String): Some[(String, String)] =
wenzelm@64085
   387
        space_explode('/', s) match {
wenzelm@64085
   388
          case List(chapter, name) => Some((chapter, name))
wenzelm@64085
   389
          case _ => Some(("", s))
wenzelm@64085
   390
        }
wenzelm@64085
   391
    }
wenzelm@64054
   392
wenzelm@64085
   393
    val Session_No_Groups = new Regex("""^Session (\S+)$""")
wenzelm@64085
   394
    val Session_Groups = new Regex("""^Session (\S+) \((.*)\)$""")
wenzelm@64085
   395
    val Session_Finished1 =
wenzelm@64085
   396
      new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time, (\d+):(\d+):(\d+) cpu time.*$""")
wenzelm@64085
   397
    val Session_Finished2 =
wenzelm@64085
   398
      new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time.*$""")
wenzelm@64085
   399
    val Session_Timing =
wenzelm@64085
   400
      new Regex("""^Timing (\S+) \((\d) threads, (\d+\.\d+)s elapsed time, (\d+\.\d+)s cpu time, (\d+\.\d+)s GC time.*$""")
wenzelm@64086
   401
    val Session_Started = new Regex("""^(?:Running|Building) (\S+) \.\.\.$""")
wenzelm@64085
   402
    val Session_Failed = new Regex("""^(\S+) FAILED""")
wenzelm@64085
   403
    val Session_Cancelled = new Regex("""^(\S+) CANCELLED""")
wenzelm@64085
   404
wenzelm@64085
   405
    var chapter = Map.empty[String, String]
wenzelm@64085
   406
    var groups = Map.empty[String, List[String]]
wenzelm@64085
   407
    var threads = Map.empty[String, Int]
wenzelm@64054
   408
    var timing = Map.empty[String, Timing]
wenzelm@64085
   409
    var ml_timing = Map.empty[String, Timing]
wenzelm@64086
   410
    var started = Set.empty[String]
wenzelm@64085
   411
    var failed = Set.empty[String]
wenzelm@64085
   412
    var cancelled = Set.empty[String]
wenzelm@64119
   413
    var ml_statistics = Map.empty[String, List[Properties.T]]
wenzelm@64119
   414
wenzelm@64085
   415
    def all_sessions: Set[String] =
wenzelm@64119
   416
      chapter.keySet ++ groups.keySet ++ threads.keySet ++ timing.keySet ++
wenzelm@64119
   417
      ml_timing.keySet ++ failed ++ cancelled ++ started ++ ml_statistics.keySet
wenzelm@64085
   418
wenzelm@64054
   419
wenzelm@64062
   420
    for (line <- log_file.lines) {
wenzelm@64054
   421
      line match {
wenzelm@64085
   422
        case Session_No_Groups(Chapter_Name(chapt, name)) =>
wenzelm@64085
   423
          chapter += (name -> chapt)
wenzelm@64085
   424
          groups += (name -> Nil)
wenzelm@64119
   425
wenzelm@64085
   426
        case Session_Groups(Chapter_Name(chapt, name), grps) =>
wenzelm@64085
   427
          chapter += (name -> chapt)
wenzelm@64085
   428
          groups += (name -> Word.explode(grps))
wenzelm@64119
   429
wenzelm@64086
   430
        case Session_Started(name) =>
wenzelm@64086
   431
          started += name
wenzelm@64119
   432
wenzelm@64054
   433
        case Session_Finished1(name,
wenzelm@64054
   434
            Value.Int(e1), Value.Int(e2), Value.Int(e3),
wenzelm@64054
   435
            Value.Int(c1), Value.Int(c2), Value.Int(c3)) =>
wenzelm@64054
   436
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@64054
   437
          val cpu = Time.hms(c1, c2, c3)
wenzelm@64085
   438
          timing += (name -> Timing(elapsed, cpu, Time.zero))
wenzelm@64119
   439
wenzelm@64054
   440
        case Session_Finished2(name,
wenzelm@64054
   441
            Value.Int(e1), Value.Int(e2), Value.Int(e3)) =>
wenzelm@64054
   442
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@64085
   443
          timing += (name -> Timing(elapsed, Time.zero, Time.zero))
wenzelm@64119
   444
wenzelm@64054
   445
        case Session_Timing(name,
wenzelm@64054
   446
            Value.Int(t), Value.Double(e), Value.Double(c), Value.Double(g)) =>
wenzelm@64054
   447
          val elapsed = Time.seconds(e)
wenzelm@64054
   448
          val cpu = Time.seconds(c)
wenzelm@64054
   449
          val gc = Time.seconds(g)
wenzelm@64085
   450
          ml_timing += (name -> Timing(elapsed, cpu, gc))
wenzelm@64054
   451
          threads += (name -> t)
wenzelm@64119
   452
wenzelm@64119
   453
        case _ if line.startsWith(ML_STATISTICS_MARKER) =>
wenzelm@64119
   454
          val (name, props) =
wenzelm@64119
   455
            Library.try_unprefix(ML_STATISTICS_MARKER, line).map(log_file.parse_props(_)) match {
wenzelm@64119
   456
              case Some((SESSION_NAME, session_name) :: props) => (session_name, props)
wenzelm@64119
   457
              case _ => log_file.err("malformed ML_statistics " + quote(line))
wenzelm@64119
   458
            }
wenzelm@64119
   459
          ml_statistics = ml_statistics + (name -> (props :: ml_statistics.getOrElse(name, Nil)))
wenzelm@64119
   460
wenzelm@64054
   461
        case _ =>
wenzelm@64054
   462
      }
wenzelm@64054
   463
    }
wenzelm@64054
   464
wenzelm@64085
   465
    val sessions =
wenzelm@64085
   466
      Map(
wenzelm@64085
   467
        (for (name <- all_sessions.toList) yield {
wenzelm@64085
   468
          val status =
wenzelm@64085
   469
            if (failed(name)) Session_Status.FAILED
wenzelm@64085
   470
            else if (cancelled(name)) Session_Status.CANCELLED
wenzelm@64086
   471
            else if (timing.isDefinedAt(name) || ml_timing.isDefinedAt(name))
wenzelm@64086
   472
              Session_Status.FINISHED
wenzelm@64086
   473
            else if (started(name)) Session_Status.FAILED
wenzelm@64085
   474
            else Session_Status.EXISTING
wenzelm@64085
   475
          val entry =
wenzelm@64085
   476
            Session_Entry(
wenzelm@64085
   477
              chapter.getOrElse(name, ""),
wenzelm@64085
   478
              groups.getOrElse(name, Nil),
wenzelm@64085
   479
              threads.get(name),
wenzelm@64089
   480
              timing.getOrElse(name, Timing.zero),
wenzelm@64089
   481
              ml_timing.getOrElse(name, Timing.zero),
wenzelm@64119
   482
              ml_statistics.getOrElse(name, Nil).reverse,
wenzelm@64085
   483
              status)
wenzelm@64085
   484
          (name -> entry)
wenzelm@64085
   485
        }):_*)
wenzelm@64085
   486
    Build_Info(sessions)
wenzelm@64054
   487
  }
wenzelm@64099
   488
wenzelm@64099
   489
wenzelm@64099
   490
wenzelm@64099
   491
  /** session info: produced by "isabelle build" **/
wenzelm@64099
   492
wenzelm@64099
   493
  sealed case class Session_Info(
wenzelm@64099
   494
    session_name: String,
wenzelm@64099
   495
    session_timing: Properties.T,
wenzelm@64099
   496
    command_timings: List[Properties.T],
wenzelm@64099
   497
    ml_statistics: List[Properties.T],
wenzelm@64099
   498
    task_statistics: List[Properties.T])
wenzelm@64099
   499
wenzelm@64099
   500
  private def parse_session_info(
wenzelm@64099
   501
    log_file: Log_File,
wenzelm@64099
   502
    default_name: String,
wenzelm@64099
   503
    command_timings: Boolean,
wenzelm@64099
   504
    ml_statistics: Boolean,
wenzelm@64099
   505
    task_statistics: Boolean): Session_Info =
wenzelm@64099
   506
  {
wenzelm@64099
   507
    val xml_cache = new XML.Cache()
wenzelm@64099
   508
wenzelm@64099
   509
    val session_name =
wenzelm@64099
   510
      log_file.find_line("\fSession.name = ") match {
wenzelm@64099
   511
        case None => default_name
wenzelm@64099
   512
        case Some(name) if default_name == "" || default_name == name => name
wenzelm@64099
   513
        case Some(name) => log_file.err("log from different session " + quote(name))
wenzelm@64099
   514
      }
wenzelm@64099
   515
    val session_timing = log_file.find_props("\fTiming = ") getOrElse Nil
wenzelm@64099
   516
    val command_timings_ =
wenzelm@64099
   517
      if (command_timings) log_file.filter_props("\fcommand_timing = ") else Nil
wenzelm@64099
   518
    val ml_statistics_ =
wenzelm@64119
   519
      if (ml_statistics) log_file.filter_props(ML_STATISTICS_MARKER) else Nil
wenzelm@64099
   520
    val task_statistics_ =
wenzelm@64099
   521
      if (task_statistics) log_file.filter_props("\ftask_statistics = ") else Nil
wenzelm@64099
   522
wenzelm@64099
   523
    Session_Info(session_name, session_timing, command_timings_, ml_statistics_, task_statistics_)
wenzelm@64099
   524
  }
wenzelm@64045
   525
}