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