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