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