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