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