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