src/Pure/Admin/build_log.scala
author wenzelm
Fri Apr 28 14:29:23 2017 +0200 (2017-04-28)
changeset 65607 c937984c70e9
parent 65605 a6447eb6bc38
child 65608 d526ba7b0a2d
permissions -rw-r--r--
tuned signature;
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@65599
    14
import java.sql.PreparedStatement
wenzelm@64061
    15
wenzelm@65600
    16
import scala.collection.immutable.SortedMap
wenzelm@64054
    17
import scala.collection.mutable
wenzelm@64054
    18
import scala.util.matching.Regex
wenzelm@64054
    19
wenzelm@64054
    20
wenzelm@64045
    21
object Build_Log
wenzelm@64045
    22
{
wenzelm@64298
    23
  /** content **/
wenzelm@64101
    24
wenzelm@64298
    25
  /* properties */
wenzelm@64150
    26
wenzelm@64298
    27
  object Prop
wenzelm@64298
    28
  {
wenzelm@64303
    29
    val separator = '\u000b'
wenzelm@64303
    30
wenzelm@64300
    31
    def multiple(name: String, args: List[String]): Properties.T =
wenzelm@64300
    32
      if (args.isEmpty) Nil
wenzelm@64303
    33
      else List(name -> args.mkString(separator.toString))
wenzelm@64299
    34
wenzelm@65601
    35
    def multiple_lines(s: String): String =
wenzelm@65601
    36
      cat_lines(Library.space_explode(separator, s))
wenzelm@65601
    37
wenzelm@65591
    38
    val build_tags = SQL.Column.string("build_tags")  // multiple
wenzelm@65591
    39
    val build_args = SQL.Column.string("build_args")  // multiple
wenzelm@65591
    40
    val build_group_id = SQL.Column.string("build_group_id")
wenzelm@65591
    41
    val build_id = SQL.Column.string("build_id")
wenzelm@65591
    42
    val build_engine = SQL.Column.string("build_engine")
wenzelm@65591
    43
    val build_host = SQL.Column.string("build_host")
wenzelm@65591
    44
    val build_start = SQL.Column.date("build_start")
wenzelm@65591
    45
    val build_end = SQL.Column.date("build_end")
wenzelm@65591
    46
    val isabelle_version = SQL.Column.string("isabelle_version")
wenzelm@65591
    47
    val afp_version = SQL.Column.string("afp_version")
wenzelm@65591
    48
wenzelm@65591
    49
    val columns: List[SQL.Column] =
wenzelm@65591
    50
      List(build_tags, build_args, build_group_id, build_id, build_engine,
wenzelm@65591
    51
        build_host, build_start, build_end, isabelle_version, afp_version)
wenzelm@64298
    52
  }
wenzelm@64150
    53
wenzelm@64150
    54
wenzelm@64298
    55
  /* settings */
wenzelm@64080
    56
wenzelm@64081
    57
  object Settings
wenzelm@64081
    58
  {
wenzelm@64081
    59
    val build_settings = List("ISABELLE_BUILD_OPTIONS")
wenzelm@64081
    60
    val ml_settings = List("ML_PLATFORM", "ML_HOME", "ML_SYSTEM", "ML_OPTIONS")
wenzelm@64081
    61
    val all_settings = build_settings ::: ml_settings
wenzelm@64081
    62
wenzelm@64081
    63
    type Entry = (String, String)
wenzelm@64081
    64
    type T = List[Entry]
wenzelm@64080
    65
wenzelm@64081
    66
    object Entry
wenzelm@64081
    67
    {
wenzelm@64081
    68
      def unapply(s: String): Option[Entry] =
wenzelm@64081
    69
        s.indexOf('=') match {
wenzelm@64081
    70
          case -1 => None
wenzelm@64081
    71
          case i =>
wenzelm@64081
    72
            val a = s.substring(0, i)
wenzelm@64081
    73
            val b = Library.perhaps_unquote(s.substring(i + 1))
wenzelm@64081
    74
            Some((a, b))
wenzelm@64081
    75
        }
wenzelm@64081
    76
      def apply(a: String, b: String): String = a + "=" + quote(b)
wenzelm@64081
    77
      def getenv(a: String): String = apply(a, Isabelle_System.getenv(a))
wenzelm@64081
    78
    }
wenzelm@64080
    79
wenzelm@64081
    80
    def show(): String =
wenzelm@64081
    81
      cat_lines(
wenzelm@64081
    82
        build_settings.map(Entry.getenv(_)) ::: List("") ::: ml_settings.map(Entry.getenv(_)))
wenzelm@64080
    83
  }
wenzelm@64080
    84
wenzelm@64080
    85
wenzelm@64298
    86
  /* file names */
wenzelm@64298
    87
wenzelm@64298
    88
  def log_date(date: Date): String =
wenzelm@64298
    89
    String.format(Locale.ROOT, "%s.%05d",
wenzelm@64298
    90
      DateTimeFormatter.ofPattern("yyyy-MM-dd").format(date.rep),
wenzelm@64298
    91
      new java.lang.Long((date.time - date.midnight.time).ms / 1000))
wenzelm@64298
    92
wenzelm@64298
    93
  def log_subdir(date: Date): Path =
wenzelm@64298
    94
    Path.explode("log") + Path.explode(date.rep.getYear.toString)
wenzelm@64298
    95
wenzelm@64298
    96
  def log_filename(engine: String, date: Date, more: List[String] = Nil): Path =
wenzelm@64298
    97
    Path.explode((engine :: log_date(date) :: more).mkString("", "_", ".log"))
wenzelm@64298
    98
wenzelm@64298
    99
wenzelm@64100
   100
wenzelm@64062
   101
  /** log file **/
wenzelm@64045
   102
wenzelm@64155
   103
  def print_date(date: Date): String = Log_File.Date_Format(date)
wenzelm@64155
   104
wenzelm@64062
   105
  object Log_File
wenzelm@64062
   106
  {
wenzelm@65607
   107
    /* log file */
wenzelm@65607
   108
wenzelm@64062
   109
    def apply(name: String, lines: List[String]): Log_File =
wenzelm@64062
   110
      new Log_File(name, lines)
wenzelm@64062
   111
wenzelm@64062
   112
    def apply(name: String, text: String): Log_File =
wenzelm@64063
   113
      Log_File(name, Library.trim_split_lines(text))
wenzelm@64090
   114
wenzelm@64090
   115
    def apply(file: JFile): Log_File =
wenzelm@64090
   116
    {
wenzelm@64090
   117
      val name = file.getName
wenzelm@64090
   118
      val (base_name, text) =
wenzelm@64090
   119
        Library.try_unsuffix(".gz", name) match {
wenzelm@64090
   120
          case Some(base_name) => (base_name, File.read_gzip(file))
wenzelm@64090
   121
          case None =>
wenzelm@64090
   122
            Library.try_unsuffix(".xz", name) match {
wenzelm@64090
   123
              case Some(base_name) => (base_name, File.read_xz(file))
wenzelm@64090
   124
              case None => (name, File.read(file))
wenzelm@64090
   125
            }
wenzelm@64090
   126
          }
wenzelm@64090
   127
      apply(base_name, text)
wenzelm@64090
   128
    }
wenzelm@64090
   129
wenzelm@64090
   130
    def apply(path: Path): Log_File = apply(path.file)
wenzelm@64101
   131
wenzelm@64110
   132
wenzelm@65607
   133
    /* log file collections */
wenzelm@65607
   134
wenzelm@65607
   135
    val suffixes: List[String] = List(".log", ".log.gz", ".log.xz")
wenzelm@65607
   136
wenzelm@65607
   137
    def is_log(file: JFile,
wenzelm@65607
   138
      prefixes: List[String] =
wenzelm@65607
   139
        List(Build_History.log_prefix, Isatest.log_prefix, AFP_Test.log_prefix)): Boolean =
wenzelm@65607
   140
    {
wenzelm@65607
   141
      val name = file.getName
wenzelm@65607
   142
      prefixes.exists(name.startsWith(_)) &&
wenzelm@65607
   143
      suffixes.exists(name.endsWith(_))
wenzelm@65607
   144
    }
wenzelm@65607
   145
wenzelm@65607
   146
    def find_files(dirs: Iterable[Path]): List[JFile] =
wenzelm@65607
   147
      dirs.iterator.flatMap(dir => File.find_files(dir.file, is_log(_))).toList
wenzelm@65607
   148
wenzelm@65607
   149
wenzelm@64110
   150
    /* date format */
wenzelm@64110
   151
wenzelm@64101
   152
    val Date_Format =
wenzelm@64101
   153
    {
wenzelm@64101
   154
      val fmts =
wenzelm@64101
   155
        Date.Formatter.variants(
wenzelm@64116
   156
          List("EEE MMM d HH:mm:ss O yyyy", "EEE MMM d HH:mm:ss VV yyyy"),
wenzelm@64104
   157
          List(Locale.ENGLISH, Locale.GERMAN)) :::
wenzelm@64110
   158
        List(
wenzelm@64110
   159
          DateTimeFormatter.RFC_1123_DATE_TIME,
wenzelm@64110
   160
          Date.Formatter.pattern("EEE MMM d HH:mm:ss yyyy").withZone(ZoneId.of("Europe/Berlin")))
wenzelm@64101
   161
wenzelm@64104
   162
      def tune_timezone(s: String): String =
wenzelm@64104
   163
        s match {
wenzelm@64101
   164
          case "CET" | "MET" => "GMT+1"
wenzelm@64101
   165
          case "CEST" | "MEST" => "GMT+2"
wenzelm@64104
   166
          case "EST" => "Europe/Berlin"
wenzelm@64104
   167
          case _ => s
wenzelm@64104
   168
        }
wenzelm@64104
   169
      def tune_weekday(s: String): String =
wenzelm@64104
   170
        s match {
wenzelm@64104
   171
          case "Die" => "Di"
wenzelm@64104
   172
          case "Mit" => "Mi"
wenzelm@64104
   173
          case "Don" => "Do"
wenzelm@64104
   174
          case "Fre" => "Fr"
wenzelm@64104
   175
          case "Sam" => "Sa"
wenzelm@64104
   176
          case "Son" => "So"
wenzelm@64104
   177
          case _ => s
wenzelm@64104
   178
        }
wenzelm@64104
   179
wenzelm@64104
   180
      def tune(s: String): String =
wenzelm@64104
   181
        Word.implode(
wenzelm@64104
   182
          Word.explode(s) match {
wenzelm@64104
   183
            case a :: "M\uFFFDr" :: bs => tune_weekday(a) :: "Mär" :: bs.map(tune_timezone(_))
wenzelm@64104
   184
            case a :: bs => tune_weekday(a) :: bs.map(tune_timezone(_))
wenzelm@64104
   185
            case Nil => Nil
wenzelm@64104
   186
          }
wenzelm@64104
   187
        )
wenzelm@64101
   188
wenzelm@64101
   189
      Date.Format.make(fmts, tune)
wenzelm@64101
   190
    }
wenzelm@64117
   191
wenzelm@64117
   192
wenzelm@64117
   193
    /* inlined content */
wenzelm@64117
   194
wenzelm@64119
   195
    def print_props(marker: String, props: Properties.T): String =
wenzelm@64119
   196
      marker + YXML.string_of_body(XML.Encode.properties(props))
wenzelm@64102
   197
  }
wenzelm@64102
   198
wenzelm@64102
   199
  class Log_File private(val name: String, val lines: List[String])
wenzelm@64102
   200
  {
wenzelm@64102
   201
    log_file =>
wenzelm@64102
   202
wenzelm@64102
   203
    override def toString: String = name
wenzelm@64102
   204
wenzelm@64102
   205
    def text: String = cat_lines(lines)
wenzelm@64102
   206
wenzelm@64102
   207
    def err(msg: String): Nothing =
wenzelm@64102
   208
      error("Error in log file " + quote(name) + ": " + msg)
wenzelm@64102
   209
wenzelm@64102
   210
wenzelm@64102
   211
    /* date format */
wenzelm@64101
   212
wenzelm@64101
   213
    object Strict_Date
wenzelm@64101
   214
    {
wenzelm@64101
   215
      def unapply(s: String): Some[Date] =
wenzelm@64102
   216
        try { Some(Log_File.Date_Format.parse(s)) }
wenzelm@64101
   217
        catch { case exn: DateTimeParseException => log_file.err(exn.getMessage) }
wenzelm@64101
   218
    }
wenzelm@64101
   219
wenzelm@64101
   220
wenzelm@64062
   221
    /* inlined content */
wenzelm@64062
   222
wenzelm@64062
   223
    def find[A](f: String => Option[A]): Option[A] =
wenzelm@64062
   224
      lines.iterator.map(f).find(_.isDefined).map(_.get)
wenzelm@64062
   225
wenzelm@64196
   226
    def find_line(marker: String): Option[String] =
wenzelm@64196
   227
      find(Library.try_unprefix(marker, _))
wenzelm@64196
   228
wenzelm@64062
   229
    def find_match(regex: Regex): Option[String] =
wenzelm@64062
   230
      lines.iterator.map(regex.unapplySeq(_)).find(res => res.isDefined && res.get.length == 1).
wenzelm@64062
   231
        map(res => res.get.head)
wenzelm@64062
   232
wenzelm@64062
   233
wenzelm@64062
   234
    /* settings */
wenzelm@64062
   235
wenzelm@64091
   236
    def get_setting(a: String): Option[Settings.Entry] =
wenzelm@64091
   237
      lines.find(_.startsWith(a + "=")) match {
wenzelm@64091
   238
        case Some(line) => Settings.Entry.unapply(line)
wenzelm@64091
   239
        case None => None
wenzelm@64091
   240
      }
wenzelm@64045
   241
wenzelm@64091
   242
    def get_settings(as: List[String]): Settings.T =
wenzelm@64091
   243
      for { a <- as; entry <- get_setting(a) } yield entry
wenzelm@64062
   244
wenzelm@64062
   245
wenzelm@64062
   246
    /* properties (YXML) */
wenzelm@64062
   247
wenzelm@64062
   248
    val xml_cache = new XML.Cache()
wenzelm@64062
   249
wenzelm@64062
   250
    def parse_props(text: String): Properties.T =
wenzelm@64062
   251
      xml_cache.props(XML.Decode.properties(YXML.parse_body(text)))
wenzelm@64062
   252
wenzelm@64119
   253
    def filter_props(marker: String): List[Properties.T] =
wenzelm@64196
   254
      for {
wenzelm@64196
   255
        line <- lines
wenzelm@64196
   256
        s <- Library.try_unprefix(marker, line)
wenzelm@64196
   257
        if YXML.detect(s)
wenzelm@64196
   258
      } yield parse_props(s)
wenzelm@64062
   259
wenzelm@64119
   260
    def find_props(marker: String): Option[Properties.T] =
wenzelm@64196
   261
      find_line(marker) match {
wenzelm@64196
   262
        case Some(text) if YXML.detect(text) => Some(parse_props(text))
wenzelm@64196
   263
        case _ => None
wenzelm@64196
   264
      }
wenzelm@64062
   265
wenzelm@64062
   266
wenzelm@64062
   267
    /* parse various formats */
wenzelm@64062
   268
wenzelm@64105
   269
    def parse_meta_info(): Meta_Info = Build_Log.parse_meta_info(log_file)
wenzelm@64105
   270
wenzelm@64105
   271
    def parse_build_info(): Build_Info = Build_Log.parse_build_info(log_file)
wenzelm@64105
   272
wenzelm@64082
   273
    def parse_session_info(
wenzelm@64082
   274
        command_timings: Boolean = false,
wenzelm@64082
   275
        ml_statistics: Boolean = false,
wenzelm@64082
   276
        task_statistics: Boolean = false): Session_Info =
wenzelm@65318
   277
      Build_Log.parse_session_info(log_file, command_timings, ml_statistics, task_statistics)
wenzelm@64045
   278
  }
wenzelm@64045
   279
wenzelm@64045
   280
wenzelm@64098
   281
wenzelm@65276
   282
  /** digested meta info: produced by Admin/build_history in log.xz file **/
wenzelm@64045
   283
wenzelm@64108
   284
  object Meta_Info
wenzelm@64099
   285
  {
wenzelm@64108
   286
    val empty: Meta_Info = Meta_Info(Nil, Nil)
wenzelm@65605
   287
wenzelm@65605
   288
    val log_filename = SQL.Column.string("log_filename", primary_key = true)
wenzelm@65605
   289
    val settings = SQL.Column.bytes("settings")
wenzelm@65605
   290
wenzelm@65605
   291
    val table =
wenzelm@65605
   292
      SQL.Table("isabelle_build_log_meta_info", log_filename :: Prop.columns ::: List(settings))
wenzelm@64099
   293
  }
wenzelm@64098
   294
wenzelm@64108
   295
  sealed case class Meta_Info(props: Properties.T, settings: List[(String, String)])
wenzelm@64103
   296
  {
wenzelm@64103
   297
    def is_empty: Boolean = props.isEmpty && settings.isEmpty
wenzelm@65599
   298
wenzelm@65599
   299
    def get(c: SQL.Column): Option[String] = Properties.get(props, c.name)
wenzelm@65599
   300
    def get_date(c: SQL.Column): Option[Date] = get(c).map(Log_File.Date_Format.parse(_))
wenzelm@64103
   301
  }
wenzelm@64061
   302
wenzelm@64095
   303
  object Isatest
wenzelm@64095
   304
  {
wenzelm@65588
   305
    val log_prefix = "isatest-makeall-"
wenzelm@64108
   306
    val engine = "isatest"
wenzelm@64109
   307
    val Start = new Regex("""^------------------- starting test --- (.+) --- (.+)$""")
wenzelm@64109
   308
    val End = new Regex("""^------------------- test (?:successful|FAILED) --- (.+) --- .*$""")
wenzelm@64095
   309
    val Isabelle_Version = new Regex("""^Isabelle version: (\S+)$""")
wenzelm@64095
   310
    val No_AFP_Version = new Regex("""$.""")
wenzelm@64095
   311
  }
wenzelm@64095
   312
wenzelm@64109
   313
  object AFP_Test
wenzelm@64061
   314
  {
wenzelm@65588
   315
    val log_prefix = "afp-test-devel-"
wenzelm@64108
   316
    val engine = "afp-test"
wenzelm@64109
   317
    val Start = new Regex("""^Start test(?: for .+)? at ([^,]+), (.*)$""")
wenzelm@64109
   318
    val Start_Old = new Regex("""^Start test(?: for .+)? at ([^,]+)$""")
wenzelm@64109
   319
    val End = new Regex("""^End test on (.+), .+, elapsed time:.*$""")
wenzelm@64087
   320
    val Isabelle_Version = new Regex("""^Isabelle version: .* -- hg id (\S+)$""")
wenzelm@64087
   321
    val AFP_Version = new Regex("""^AFP version: .* -- hg id (\S+)$""")
wenzelm@64104
   322
    val Bad_Init = new Regex("""^cp:.*: Disc quota exceeded$""")
wenzelm@64061
   323
  }
wenzelm@64061
   324
wenzelm@64110
   325
  object Jenkins
wenzelm@64110
   326
  {
wenzelm@64110
   327
    val engine = "jenkins"
wenzelm@64110
   328
    val Start = new Regex("""^Started .*$""")
wenzelm@64110
   329
    val Start_Date = new Regex("""^Build started at (.+)$""")
wenzelm@64110
   330
    val No_End = new Regex("""$.""")
wenzelm@64110
   331
    val Isabelle_Version = new Regex("""^Isabelle id (\S+)$""")
wenzelm@64110
   332
    val AFP_Version = new Regex("""^AFP id (\S+)$""")
wenzelm@64110
   333
    val CONFIGURATION = "=== CONFIGURATION ==="
wenzelm@64110
   334
    val BUILD = "=== BUILD ==="
wenzelm@64110
   335
    val FINISHED = "Finished: "
wenzelm@64110
   336
  }
wenzelm@64110
   337
wenzelm@64105
   338
  private def parse_meta_info(log_file: Log_File): Meta_Info =
wenzelm@64061
   339
  {
wenzelm@64108
   340
    def parse(engine: String, host: String, start: Date,
wenzelm@64109
   341
      End: Regex, Isabelle_Version: Regex, AFP_Version: Regex): Meta_Info =
wenzelm@64091
   342
    {
wenzelm@64296
   343
      val build_id =
wenzelm@64296
   344
      {
wenzelm@64296
   345
        val prefix = if (host != "") host else if (engine != "") engine else ""
wenzelm@64296
   346
        (if (prefix == "") "build" else prefix) + ":" + start.time.ms
wenzelm@64296
   347
      }
wenzelm@65591
   348
      val build_engine = if (engine == "") Nil else List(Prop.build_engine.name -> engine)
wenzelm@65591
   349
      val build_host = if (host == "") Nil else List(Prop.build_host.name -> host)
wenzelm@64108
   350
wenzelm@65599
   351
      val start_date = List(Prop.build_start.name -> print_date(start))
wenzelm@64091
   352
      val end_date =
wenzelm@64091
   353
        log_file.lines.last match {
wenzelm@64109
   354
          case End(log_file.Strict_Date(end_date)) =>
wenzelm@65599
   355
            List(Prop.build_end.name -> print_date(end_date))
wenzelm@64091
   356
          case _ => Nil
wenzelm@64091
   357
        }
wenzelm@64091
   358
wenzelm@64091
   359
      val isabelle_version =
wenzelm@65591
   360
        log_file.find_match(Isabelle_Version).map(Prop.isabelle_version.name -> _)
wenzelm@64091
   361
      val afp_version =
wenzelm@65591
   362
        log_file.find_match(AFP_Version).map(Prop.afp_version.name -> _)
wenzelm@64062
   363
wenzelm@65591
   364
      Meta_Info((Prop.build_id.name -> build_id) :: build_engine ::: build_host :::
wenzelm@64108
   365
          start_date ::: end_date ::: isabelle_version.toList ::: afp_version.toList,
wenzelm@64091
   366
        log_file.get_settings(Settings.all_settings))
wenzelm@64091
   367
    }
wenzelm@64091
   368
wenzelm@64091
   369
    log_file.lines match {
wenzelm@64119
   370
      case line :: _ if line.startsWith(Build_History.META_INFO_MARKER) =>
wenzelm@64119
   371
        Meta_Info(log_file.find_props(Build_History.META_INFO_MARKER).get,
wenzelm@64117
   372
          log_file.get_settings(Settings.all_settings))
wenzelm@64117
   373
wenzelm@64109
   374
      case Isatest.Start(log_file.Strict_Date(start), host) :: _ =>
wenzelm@64109
   375
        parse(Isatest.engine, host, start, Isatest.End,
wenzelm@64108
   376
          Isatest.Isabelle_Version, Isatest.No_AFP_Version)
wenzelm@64099
   377
wenzelm@64109
   378
      case AFP_Test.Start(log_file.Strict_Date(start), host) :: _ =>
wenzelm@64109
   379
        parse(AFP_Test.engine, host, start, AFP_Test.End,
wenzelm@64109
   380
          AFP_Test.Isabelle_Version, AFP_Test.AFP_Version)
wenzelm@64099
   381
wenzelm@64109
   382
      case AFP_Test.Start_Old(log_file.Strict_Date(start)) :: _ =>
wenzelm@64109
   383
        parse(AFP_Test.engine, "", start, AFP_Test.End,
wenzelm@64109
   384
          AFP_Test.Isabelle_Version, AFP_Test.AFP_Version)
wenzelm@64099
   385
wenzelm@64110
   386
      case Jenkins.Start() :: _
wenzelm@64110
   387
      if log_file.lines.contains(Jenkins.CONFIGURATION) ||
wenzelm@64110
   388
         log_file.lines.last.startsWith(Jenkins.FINISHED) =>
wenzelm@64110
   389
        log_file.lines.dropWhile(_ != Jenkins.BUILD) match {
wenzelm@64110
   390
          case Jenkins.BUILD :: _ :: Jenkins.Start_Date(log_file.Strict_Date(start)) :: _ =>
wenzelm@64111
   391
            parse(Jenkins.engine, "", start.to(ZoneId.of("Europe/Berlin")), Jenkins.No_End,
wenzelm@64110
   392
              Jenkins.Isabelle_Version, Jenkins.AFP_Version)
wenzelm@64110
   393
          case _ => Meta_Info.empty
wenzelm@64110
   394
        }
wenzelm@64110
   395
wenzelm@64341
   396
      case line :: _ if line.startsWith("\u0000") => Meta_Info.empty
wenzelm@64109
   397
      case List(Isatest.End(_)) => Meta_Info.empty
wenzelm@64109
   398
      case _ :: AFP_Test.Bad_Init() :: _ => Meta_Info.empty
wenzelm@64105
   399
      case Nil => Meta_Info.empty
wenzelm@64104
   400
wenzelm@64110
   401
      case _ => log_file.err("cannot detect log file format")
wenzelm@64061
   402
    }
wenzelm@64061
   403
  }
wenzelm@64061
   404
wenzelm@64085
   405
wenzelm@64098
   406
wenzelm@65276
   407
  /** build info: toplevel output of isabelle build or Admin/build_history **/
wenzelm@64119
   408
wenzelm@64119
   409
  val ML_STATISTICS_MARKER = "\fML_statistics = "
wenzelm@64119
   410
  val SESSION_NAME = "session_name"
wenzelm@64085
   411
wenzelm@64061
   412
  object Session_Status extends Enumeration
wenzelm@64061
   413
  {
wenzelm@64085
   414
    val EXISTING = Value("existing")
wenzelm@64061
   415
    val FINISHED = Value("finished")
wenzelm@64061
   416
    val FAILED = Value("failed")
wenzelm@64061
   417
    val CANCELLED = Value("cancelled")
wenzelm@64061
   418
  }
wenzelm@64061
   419
wenzelm@65599
   420
  object Session_Entry
wenzelm@65599
   421
  {
wenzelm@65599
   422
    val encode: XML.Encode.T[Session_Entry] = (entry: Session_Entry) =>
wenzelm@65599
   423
    {
wenzelm@65599
   424
      import XML.Encode._
wenzelm@65599
   425
      pair(string, pair(list(string), pair(option(int), pair(Timing.encode, pair(Timing.encode,
wenzelm@65599
   426
        pair(list(properties), pair(option(long), string)))))))(
wenzelm@65599
   427
        entry.chapter, (entry.groups, (entry.threads, (entry.timing, (entry.ml_timing,
wenzelm@65599
   428
        (entry.ml_statistics, (entry.heap_size, entry.status.toString)))))))
wenzelm@65599
   429
    }
wenzelm@65599
   430
    val decode: XML.Decode.T[Session_Entry] = (body: XML.Body) =>
wenzelm@65599
   431
    {
wenzelm@65599
   432
      import XML.Decode._
wenzelm@65599
   433
      val (chapter, (groups, (threads, (timing, (ml_timing, (ml_statistics, (heap_size, status))))))) =
wenzelm@65599
   434
        pair(string, pair(list(string), pair(option(int), pair(Timing.decode, pair(Timing.decode,
wenzelm@65599
   435
          pair(list(properties), pair(option(long), string)))))))(body)
wenzelm@65599
   436
      Session_Entry(chapter, groups, threads, timing, ml_timing, ml_statistics, heap_size,
wenzelm@65599
   437
        Session_Status.withName(status))
wenzelm@65599
   438
    }
wenzelm@65599
   439
  }
wenzelm@65599
   440
wenzelm@64085
   441
  sealed case class Session_Entry(
wenzelm@64085
   442
    chapter: String,
wenzelm@64085
   443
    groups: List[String],
wenzelm@64085
   444
    threads: Option[Int],
wenzelm@64089
   445
    timing: Timing,
wenzelm@64089
   446
    ml_timing: Timing,
wenzelm@64119
   447
    ml_statistics: List[Properties.T],
wenzelm@64120
   448
    heap_size: Option[Long],
wenzelm@64085
   449
    status: Session_Status.Value)
wenzelm@64085
   450
  {
wenzelm@64085
   451
    def finished: Boolean = status == Session_Status.FINISHED
wenzelm@64085
   452
  }
wenzelm@64054
   453
wenzelm@65599
   454
  object Build_Info
wenzelm@65599
   455
  {
wenzelm@65605
   456
    val build_info = SQL.Column.bytes("build_info")
wenzelm@65605
   457
    val table = SQL.Table("isabelle_build_log_build_info", List(Meta_Info.log_filename, build_info))
wenzelm@65605
   458
wenzelm@65599
   459
    def encode: XML.Encode.T[Build_Info] = (info: Build_Info) =>
wenzelm@65599
   460
    {
wenzelm@65599
   461
      import XML.Encode._
wenzelm@65599
   462
      list(pair(string, Session_Entry.encode))(info.sessions.toList)
wenzelm@65599
   463
    }
wenzelm@65599
   464
    def decode: XML.Decode.T[Build_Info] = (body: XML.Body) =>
wenzelm@65599
   465
    {
wenzelm@65599
   466
      import XML.Decode._
wenzelm@65599
   467
      Build_Info(list(pair(string, Session_Entry.decode))(body).toMap)
wenzelm@65599
   468
    }
wenzelm@65599
   469
  }
wenzelm@65599
   470
wenzelm@64085
   471
  sealed case class Build_Info(sessions: Map[String, Session_Entry])
wenzelm@64054
   472
  {
wenzelm@64085
   473
    def session(name: String): Session_Entry = sessions(name)
wenzelm@64085
   474
    def get_session(name: String): Option[Session_Entry] = sessions.get(name)
wenzelm@64054
   475
wenzelm@64089
   476
    def get_default[A](name: String, f: Session_Entry => A, x: A): A =
wenzelm@64085
   477
      get_session(name) match {
wenzelm@64089
   478
        case Some(entry) => f(entry)
wenzelm@64089
   479
        case None => x
wenzelm@64085
   480
      }
wenzelm@64085
   481
wenzelm@64119
   482
    def finished_sessions: List[String] = sessions.keySet.iterator.filter(finished(_)).toList
wenzelm@64089
   483
    def finished(name: String): Boolean = get_default(name, _.finished, false)
wenzelm@64089
   484
    def timing(name: String): Timing = get_default(name, _.timing, Timing.zero)
wenzelm@64089
   485
    def ml_timing(name: String): Timing = get_default(name, _.ml_timing, Timing.zero)
wenzelm@65052
   486
    def ml_statistics(name: String): ML_Statistics =
wenzelm@65052
   487
      get_default(name, entry => ML_Statistics(name, entry.ml_statistics), ML_Statistics.empty)
wenzelm@64054
   488
  }
wenzelm@64054
   489
wenzelm@64085
   490
  private def parse_build_info(log_file: Log_File): Build_Info =
wenzelm@64085
   491
  {
wenzelm@64085
   492
    object Chapter_Name
wenzelm@64085
   493
    {
wenzelm@64085
   494
      def unapply(s: String): Some[(String, String)] =
wenzelm@64085
   495
        space_explode('/', s) match {
wenzelm@64085
   496
          case List(chapter, name) => Some((chapter, name))
wenzelm@64085
   497
          case _ => Some(("", s))
wenzelm@64085
   498
        }
wenzelm@64085
   499
    }
wenzelm@64054
   500
wenzelm@64085
   501
    val Session_No_Groups = new Regex("""^Session (\S+)$""")
wenzelm@64085
   502
    val Session_Groups = new Regex("""^Session (\S+) \((.*)\)$""")
wenzelm@64085
   503
    val Session_Finished1 =
wenzelm@64085
   504
      new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time, (\d+):(\d+):(\d+) cpu time.*$""")
wenzelm@64085
   505
    val Session_Finished2 =
wenzelm@64085
   506
      new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time.*$""")
wenzelm@64085
   507
    val Session_Timing =
wenzelm@64085
   508
      new Regex("""^Timing (\S+) \((\d) threads, (\d+\.\d+)s elapsed time, (\d+\.\d+)s cpu time, (\d+\.\d+)s GC time.*$""")
wenzelm@64086
   509
    val Session_Started = new Regex("""^(?:Running|Building) (\S+) \.\.\.$""")
wenzelm@64085
   510
    val Session_Failed = new Regex("""^(\S+) FAILED""")
wenzelm@64085
   511
    val Session_Cancelled = new Regex("""^(\S+) CANCELLED""")
wenzelm@64120
   512
    val Heap = new Regex("""^Heap (\S+) \((\d+) bytes\)$""")
wenzelm@64085
   513
wenzelm@64085
   514
    var chapter = Map.empty[String, String]
wenzelm@64085
   515
    var groups = Map.empty[String, List[String]]
wenzelm@64085
   516
    var threads = Map.empty[String, Int]
wenzelm@64054
   517
    var timing = Map.empty[String, Timing]
wenzelm@64085
   518
    var ml_timing = Map.empty[String, Timing]
wenzelm@64086
   519
    var started = Set.empty[String]
wenzelm@64085
   520
    var failed = Set.empty[String]
wenzelm@64085
   521
    var cancelled = Set.empty[String]
wenzelm@64119
   522
    var ml_statistics = Map.empty[String, List[Properties.T]]
wenzelm@64120
   523
    var heap_sizes = Map.empty[String, Long]
wenzelm@64119
   524
wenzelm@64085
   525
    def all_sessions: Set[String] =
wenzelm@64120
   526
      chapter.keySet ++ groups.keySet ++ threads.keySet ++ timing.keySet ++ ml_timing.keySet ++
wenzelm@64120
   527
      failed ++ cancelled ++ started ++ ml_statistics.keySet ++ heap_sizes.keySet
wenzelm@64085
   528
wenzelm@64054
   529
wenzelm@64062
   530
    for (line <- log_file.lines) {
wenzelm@64054
   531
      line match {
wenzelm@64085
   532
        case Session_No_Groups(Chapter_Name(chapt, name)) =>
wenzelm@64085
   533
          chapter += (name -> chapt)
wenzelm@64085
   534
          groups += (name -> Nil)
wenzelm@64119
   535
wenzelm@64085
   536
        case Session_Groups(Chapter_Name(chapt, name), grps) =>
wenzelm@64085
   537
          chapter += (name -> chapt)
wenzelm@64085
   538
          groups += (name -> Word.explode(grps))
wenzelm@64119
   539
wenzelm@64086
   540
        case Session_Started(name) =>
wenzelm@64086
   541
          started += name
wenzelm@64119
   542
wenzelm@64054
   543
        case Session_Finished1(name,
wenzelm@64054
   544
            Value.Int(e1), Value.Int(e2), Value.Int(e3),
wenzelm@64054
   545
            Value.Int(c1), Value.Int(c2), Value.Int(c3)) =>
wenzelm@64054
   546
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@64054
   547
          val cpu = Time.hms(c1, c2, c3)
wenzelm@64085
   548
          timing += (name -> Timing(elapsed, cpu, Time.zero))
wenzelm@64119
   549
wenzelm@64054
   550
        case Session_Finished2(name,
wenzelm@64054
   551
            Value.Int(e1), Value.Int(e2), Value.Int(e3)) =>
wenzelm@64054
   552
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@64085
   553
          timing += (name -> Timing(elapsed, Time.zero, Time.zero))
wenzelm@64119
   554
wenzelm@64054
   555
        case Session_Timing(name,
wenzelm@64054
   556
            Value.Int(t), Value.Double(e), Value.Double(c), Value.Double(g)) =>
wenzelm@64054
   557
          val elapsed = Time.seconds(e)
wenzelm@64054
   558
          val cpu = Time.seconds(c)
wenzelm@64054
   559
          val gc = Time.seconds(g)
wenzelm@64085
   560
          ml_timing += (name -> Timing(elapsed, cpu, gc))
wenzelm@64054
   561
          threads += (name -> t)
wenzelm@64119
   562
wenzelm@64120
   563
        case Heap(name, Value.Long(size)) =>
wenzelm@64120
   564
          heap_sizes += (name -> size)
wenzelm@64120
   565
wenzelm@65590
   566
        case _ if line.startsWith(ML_STATISTICS_MARKER) && YXML.detect(line) =>
wenzelm@64119
   567
          val (name, props) =
wenzelm@64119
   568
            Library.try_unprefix(ML_STATISTICS_MARKER, line).map(log_file.parse_props(_)) match {
wenzelm@64119
   569
              case Some((SESSION_NAME, session_name) :: props) => (session_name, props)
wenzelm@64119
   570
              case _ => log_file.err("malformed ML_statistics " + quote(line))
wenzelm@64119
   571
            }
wenzelm@64120
   572
          ml_statistics += (name -> (props :: ml_statistics.getOrElse(name, Nil)))
wenzelm@64119
   573
wenzelm@64054
   574
        case _ =>
wenzelm@64054
   575
      }
wenzelm@64054
   576
    }
wenzelm@64054
   577
wenzelm@64085
   578
    val sessions =
wenzelm@64085
   579
      Map(
wenzelm@64085
   580
        (for (name <- all_sessions.toList) yield {
wenzelm@64085
   581
          val status =
wenzelm@64085
   582
            if (failed(name)) Session_Status.FAILED
wenzelm@64085
   583
            else if (cancelled(name)) Session_Status.CANCELLED
wenzelm@64086
   584
            else if (timing.isDefinedAt(name) || ml_timing.isDefinedAt(name))
wenzelm@64086
   585
              Session_Status.FINISHED
wenzelm@64086
   586
            else if (started(name)) Session_Status.FAILED
wenzelm@64085
   587
            else Session_Status.EXISTING
wenzelm@64085
   588
          val entry =
wenzelm@64085
   589
            Session_Entry(
wenzelm@64085
   590
              chapter.getOrElse(name, ""),
wenzelm@64085
   591
              groups.getOrElse(name, Nil),
wenzelm@64085
   592
              threads.get(name),
wenzelm@64089
   593
              timing.getOrElse(name, Timing.zero),
wenzelm@64089
   594
              ml_timing.getOrElse(name, Timing.zero),
wenzelm@64119
   595
              ml_statistics.getOrElse(name, Nil).reverse,
wenzelm@64120
   596
              heap_sizes.get(name),
wenzelm@64085
   597
              status)
wenzelm@64085
   598
          (name -> entry)
wenzelm@64085
   599
        }):_*)
wenzelm@64085
   600
    Build_Info(sessions)
wenzelm@64054
   601
  }
wenzelm@64099
   602
wenzelm@64099
   603
wenzelm@64099
   604
wenzelm@65276
   605
  /** session info: produced by isabelle build as session log.gz file **/
wenzelm@64099
   606
wenzelm@64099
   607
  sealed case class Session_Info(
wenzelm@64099
   608
    session_timing: Properties.T,
wenzelm@64099
   609
    command_timings: List[Properties.T],
wenzelm@64099
   610
    ml_statistics: List[Properties.T],
wenzelm@64099
   611
    task_statistics: List[Properties.T])
wenzelm@64099
   612
wenzelm@64099
   613
  private def parse_session_info(
wenzelm@64099
   614
    log_file: Log_File,
wenzelm@64099
   615
    command_timings: Boolean,
wenzelm@64099
   616
    ml_statistics: Boolean,
wenzelm@64099
   617
    task_statistics: Boolean): Session_Info =
wenzelm@64099
   618
  {
wenzelm@65290
   619
    Session_Info(
wenzelm@65290
   620
      session_timing = log_file.find_props("\fTiming = ") getOrElse Nil,
wenzelm@65290
   621
      command_timings = if (command_timings) log_file.filter_props("\fcommand_timing = ") else Nil,
wenzelm@65290
   622
      ml_statistics = if (ml_statistics) log_file.filter_props(ML_STATISTICS_MARKER) else Nil,
wenzelm@65290
   623
      task_statistics = if (task_statistics) log_file.filter_props("\ftask_statistics = ") else Nil)
wenzelm@64099
   624
  }
wenzelm@65595
   625
wenzelm@65595
   626
wenzelm@65595
   627
wenzelm@65595
   628
  /** persistent store **/
wenzelm@65595
   629
wenzelm@65595
   630
  def store(options: Options): Store = new Store(options)
wenzelm@65595
   631
wenzelm@65595
   632
  class Store private[Build_Log](options: Options) extends Properties.Store
wenzelm@65595
   633
  {
wenzelm@65595
   634
    def open_database(
wenzelm@65595
   635
      user: String = options.string("build_log_database_user"),
wenzelm@65595
   636
      password: String = options.string("build_log_database_password"),
wenzelm@65595
   637
      database: String = options.string("build_log_database_name"),
wenzelm@65595
   638
      host: String = options.string("build_log_database_host"),
wenzelm@65595
   639
      port: Int = options.int("build_log_database_port"),
wenzelm@65595
   640
      ssh_host: String = options.string("build_log_ssh_host"),
wenzelm@65595
   641
      ssh_user: String = options.string("build_log_ssh_user"),
wenzelm@65595
   642
      ssh_port: Int = options.int("build_log_ssh_port")): PostgreSQL.Database =
wenzelm@65595
   643
    {
wenzelm@65595
   644
      PostgreSQL.open_database(
wenzelm@65595
   645
        user = user, password = password, database = database, host = host, port = port,
wenzelm@65595
   646
        ssh =
wenzelm@65595
   647
          if (ssh_host == "") None
wenzelm@65595
   648
          else Some(SSH.init_context(options).open_session(ssh_host, ssh_user, port)))
wenzelm@65595
   649
    }
wenzelm@65599
   650
wenzelm@65599
   651
    def compress_build_info(build_info: Build_Info, options: XZ.Options = XZ.options()): Bytes =
wenzelm@65599
   652
      Bytes(YXML.string_of_body(Build_Info.encode(build_info))).compress(options)
wenzelm@65599
   653
wenzelm@65599
   654
    def uncompress_build_info(bytes: Bytes): Build_Info =
wenzelm@65599
   655
      Build_Info.decode(xml_cache.body(YXML.parse_body(bytes.uncompress().text)))
wenzelm@65599
   656
wenzelm@65605
   657
    def filter_files(db: SQL.Database, table: SQL.Table, files: List[JFile]): List[JFile] =
wenzelm@65600
   658
    {
wenzelm@65605
   659
      val key = Meta_Info.log_filename
wenzelm@65600
   660
      val known_files =
wenzelm@65605
   661
        using(db.select_statement(table, List(key)))(stmt =>
wenzelm@65605
   662
          SQL.iterator(stmt.executeQuery)(rs => db.string(rs, key)).toSet)
wenzelm@65600
   663
wenzelm@65600
   664
      val unique_files =
wenzelm@65600
   665
        (Map.empty[String, JFile] /: files.iterator)({ case (m, file) =>
wenzelm@65600
   666
          val name = file.getName
wenzelm@65600
   667
          if (known_files(name)) m else m + (name -> file)
wenzelm@65600
   668
        })
wenzelm@65600
   669
wenzelm@65600
   670
      unique_files.iterator.map(_._2).toList
wenzelm@65600
   671
    }
wenzelm@65600
   672
wenzelm@65605
   673
    def write_meta_info(db: SQL.Database, files: List[JFile])
wenzelm@65599
   674
    {
wenzelm@65599
   675
      db.transaction {
wenzelm@65605
   676
        db.create_table(Meta_Info.table)
wenzelm@65599
   677
wenzelm@65605
   678
        using(db.insert_statement(Meta_Info.table))(stmt =>
wenzelm@65599
   679
        {
wenzelm@65605
   680
          for (file <- filter_files(db, Meta_Info.table, files)) {
wenzelm@65605
   681
            val meta_info = Log_File(file).parse_meta_info()
wenzelm@65599
   682
wenzelm@65599
   683
            db.set_string(stmt, 1, file.getName)
wenzelm@65599
   684
            for ((c, i) <- Prop.columns.zipWithIndex) {
wenzelm@65599
   685
              if (c.T == SQL.Type.Date)
wenzelm@65599
   686
                db.set_date(stmt, i + 2, meta_info.get_date(c).orNull)
wenzelm@65599
   687
              else
wenzelm@65601
   688
                db.set_string(stmt, i + 2, meta_info.get(c).map(Prop.multiple_lines(_)).orNull)
wenzelm@65599
   689
            }
wenzelm@65605
   690
            db.set_bytes(stmt, Meta_Info.table.columns.length, encode_properties(meta_info.settings))
wenzelm@65599
   691
wenzelm@65605
   692
            stmt.execute()
wenzelm@65605
   693
          }
wenzelm@65605
   694
        })
wenzelm@65605
   695
      }
wenzelm@65605
   696
    }
wenzelm@65605
   697
wenzelm@65605
   698
    def write_build_info(db: SQL.Database, files: List[JFile])
wenzelm@65605
   699
    {
wenzelm@65605
   700
      db.transaction {
wenzelm@65605
   701
        db.create_table(Build_Info.table)
wenzelm@65605
   702
wenzelm@65605
   703
        using(db.insert_statement(Build_Info.table))(stmt =>
wenzelm@65605
   704
        {
wenzelm@65605
   705
          for (file <- filter_files(db, Build_Info.table, files)) {
wenzelm@65605
   706
            val build_info = Log_File(file).parse_build_info()
wenzelm@65605
   707
wenzelm@65605
   708
            db.set_string(stmt, 1, file.getName)
wenzelm@65605
   709
            db.set_bytes(stmt, 2, compress_build_info(build_info))
wenzelm@65599
   710
wenzelm@65599
   711
            stmt.execute()
wenzelm@65599
   712
          }
wenzelm@65599
   713
        })
wenzelm@65599
   714
      }
wenzelm@65599
   715
    }
wenzelm@65595
   716
  }
wenzelm@64045
   717
}