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