src/Pure/Tools/build_log.scala
author wenzelm
Fri Oct 07 18:41:54 2016 +0200 (2016-10-07)
changeset 64090 5a68280112b3
parent 64089 10d719dbb3ee
child 64091 f8dfba90e73f
permissions -rw-r--r--
more operations;
wenzelm@64045
     1
/*  Title:      Pure/Tools/build_log.scala
wenzelm@64045
     2
    Author:     Makarius
wenzelm@64045
     3
wenzelm@64045
     4
Build log parsing for historic versions, back to "build_history_base".
wenzelm@64045
     5
*/
wenzelm@64045
     6
wenzelm@64045
     7
package isabelle
wenzelm@64045
     8
wenzelm@64045
     9
wenzelm@64090
    10
import java.io.{File => JFile}
wenzelm@64061
    11
import java.time.ZonedDateTime
wenzelm@64061
    12
import java.time.format.{DateTimeFormatter, DateTimeParseException}
wenzelm@64061
    13
wenzelm@64054
    14
import scala.collection.mutable
wenzelm@64054
    15
import scala.util.matching.Regex
wenzelm@64054
    16
wenzelm@64054
    17
wenzelm@64045
    18
object Build_Log
wenzelm@64045
    19
{
wenzelm@64081
    20
  /** settings **/
wenzelm@64080
    21
wenzelm@64081
    22
  object Settings
wenzelm@64081
    23
  {
wenzelm@64081
    24
    val build_settings = List("ISABELLE_BUILD_OPTIONS")
wenzelm@64081
    25
    val ml_settings = List("ML_PLATFORM", "ML_HOME", "ML_SYSTEM", "ML_OPTIONS")
wenzelm@64081
    26
    val all_settings = build_settings ::: ml_settings
wenzelm@64081
    27
wenzelm@64081
    28
    type Entry = (String, String)
wenzelm@64081
    29
    type T = List[Entry]
wenzelm@64080
    30
wenzelm@64081
    31
    object Entry
wenzelm@64081
    32
    {
wenzelm@64081
    33
      def unapply(s: String): Option[Entry] =
wenzelm@64081
    34
        s.indexOf('=') match {
wenzelm@64081
    35
          case -1 => None
wenzelm@64081
    36
          case i =>
wenzelm@64081
    37
            val a = s.substring(0, i)
wenzelm@64081
    38
            val b = Library.perhaps_unquote(s.substring(i + 1))
wenzelm@64081
    39
            Some((a, b))
wenzelm@64081
    40
        }
wenzelm@64081
    41
      def apply(a: String, b: String): String = a + "=" + quote(b)
wenzelm@64081
    42
      def getenv(a: String): String = apply(a, Isabelle_System.getenv(a))
wenzelm@64081
    43
    }
wenzelm@64080
    44
wenzelm@64081
    45
    def show(): String =
wenzelm@64081
    46
      cat_lines(
wenzelm@64081
    47
        build_settings.map(Entry.getenv(_)) ::: List("") ::: ml_settings.map(Entry.getenv(_)))
wenzelm@64080
    48
  }
wenzelm@64080
    49
wenzelm@64080
    50
wenzelm@64062
    51
  /** log file **/
wenzelm@64045
    52
wenzelm@64062
    53
  object Log_File
wenzelm@64062
    54
  {
wenzelm@64062
    55
    def apply(name: String, lines: List[String]): Log_File =
wenzelm@64062
    56
      new Log_File(name, lines)
wenzelm@64062
    57
wenzelm@64062
    58
    def apply(name: String, text: String): Log_File =
wenzelm@64063
    59
      Log_File(name, Library.trim_split_lines(text))
wenzelm@64090
    60
wenzelm@64090
    61
    def apply(file: JFile): Log_File =
wenzelm@64090
    62
    {
wenzelm@64090
    63
      val name = file.getName
wenzelm@64090
    64
      val (base_name, text) =
wenzelm@64090
    65
        Library.try_unsuffix(".gz", name) match {
wenzelm@64090
    66
          case Some(base_name) => (base_name, File.read_gzip(file))
wenzelm@64090
    67
          case None =>
wenzelm@64090
    68
            Library.try_unsuffix(".xz", name) match {
wenzelm@64090
    69
              case Some(base_name) => (base_name, File.read_xz(file))
wenzelm@64090
    70
              case None => (name, File.read(file))
wenzelm@64090
    71
            }
wenzelm@64090
    72
          }
wenzelm@64090
    73
      apply(base_name, text)
wenzelm@64090
    74
    }
wenzelm@64090
    75
wenzelm@64090
    76
    def apply(path: Path): Log_File = apply(path.file)
wenzelm@64062
    77
  }
wenzelm@64062
    78
wenzelm@64062
    79
  class Log_File private(val name: String, val lines: List[String])
wenzelm@64045
    80
  {
wenzelm@64062
    81
    log_file =>
wenzelm@64062
    82
wenzelm@64062
    83
    override def toString: String = name
wenzelm@64062
    84
wenzelm@64062
    85
    def text: String = cat_lines(lines)
wenzelm@64062
    86
wenzelm@64062
    87
    def err(msg: String): Nothing =
wenzelm@64062
    88
      error("Error in log file " + quote(name) + ": " + msg)
wenzelm@64062
    89
wenzelm@64062
    90
wenzelm@64062
    91
    /* inlined content */
wenzelm@64062
    92
wenzelm@64062
    93
    def find[A](f: String => Option[A]): Option[A] =
wenzelm@64062
    94
      lines.iterator.map(f).find(_.isDefined).map(_.get)
wenzelm@64062
    95
wenzelm@64062
    96
    def find_match(regex: Regex): Option[String] =
wenzelm@64062
    97
      lines.iterator.map(regex.unapplySeq(_)).find(res => res.isDefined && res.get.length == 1).
wenzelm@64062
    98
        map(res => res.get.head)
wenzelm@64062
    99
wenzelm@64062
   100
wenzelm@64062
   101
    /* settings */
wenzelm@64062
   102
wenzelm@64081
   103
    def get_setting(a: String): Settings.Entry =
wenzelm@64081
   104
      Settings.Entry.unapply(
wenzelm@64081
   105
        lines.find(_.startsWith(a + "=")) getOrElse err("missing " + a)).get
wenzelm@64045
   106
wenzelm@64081
   107
    def get_settings(as: List[String]): Settings.T = as.map(get_setting(_))
wenzelm@64062
   108
wenzelm@64062
   109
wenzelm@64062
   110
    /* properties (YXML) */
wenzelm@64062
   111
wenzelm@64062
   112
    val xml_cache = new XML.Cache()
wenzelm@64062
   113
wenzelm@64062
   114
    def parse_props(text: String): Properties.T =
wenzelm@64062
   115
      xml_cache.props(XML.Decode.properties(YXML.parse_body(text)))
wenzelm@64062
   116
wenzelm@64062
   117
    def filter_props(prefix: String): List[Properties.T] =
wenzelm@64062
   118
      for (line <- lines; s <- Library.try_unprefix(prefix, line)) yield parse_props(s)
wenzelm@64045
   119
wenzelm@64062
   120
    def find_line(prefix: String): Option[String] =
wenzelm@64062
   121
      find(Library.try_unprefix(prefix, _))
wenzelm@64062
   122
wenzelm@64062
   123
    def find_props(prefix: String): Option[Properties.T] =
wenzelm@64062
   124
      find_line(prefix).map(parse_props(_))
wenzelm@64062
   125
wenzelm@64062
   126
wenzelm@64062
   127
    /* parse various formats */
wenzelm@64062
   128
wenzelm@64082
   129
    def parse_session_info(
wenzelm@64085
   130
        default_name: String = "",
wenzelm@64082
   131
        command_timings: Boolean = false,
wenzelm@64082
   132
        ml_statistics: Boolean = false,
wenzelm@64082
   133
        task_statistics: Boolean = false): Session_Info =
wenzelm@64082
   134
      Build_Log.parse_session_info(
wenzelm@64085
   135
        log_file, default_name, command_timings, ml_statistics, task_statistics)
wenzelm@64062
   136
wenzelm@64085
   137
    def parse_header(): Header = Build_Log.parse_header(log_file)
wenzelm@64062
   138
wenzelm@64085
   139
    def parse_build_info(): Build_Info = Build_Log.parse_build_info(log_file)
wenzelm@64045
   140
  }
wenzelm@64045
   141
wenzelm@64045
   142
wenzelm@64045
   143
  /* session log: produced by "isabelle build" */
wenzelm@64045
   144
wenzelm@64045
   145
  sealed case class Session_Info(
wenzelm@64045
   146
    session_name: String,
wenzelm@64045
   147
    session_timing: Properties.T,
wenzelm@64045
   148
    command_timings: List[Properties.T],
wenzelm@64045
   149
    ml_statistics: List[Properties.T],
wenzelm@64045
   150
    task_statistics: List[Properties.T])
wenzelm@64045
   151
wenzelm@64082
   152
  private def parse_session_info(
wenzelm@64082
   153
    log_file: Log_File,
wenzelm@64082
   154
    default_name: String,
wenzelm@64082
   155
    command_timings: Boolean,
wenzelm@64082
   156
    ml_statistics: Boolean,
wenzelm@64082
   157
    task_statistics: Boolean): Session_Info =
wenzelm@64045
   158
  {
wenzelm@64045
   159
    val xml_cache = new XML.Cache()
wenzelm@64045
   160
wenzelm@64045
   161
    val session_name =
wenzelm@64062
   162
      log_file.find_line("\fSession.name = ") match {
wenzelm@64082
   163
        case None => default_name
wenzelm@64082
   164
        case Some(name) if default_name == "" || default_name == name => name
wenzelm@64062
   165
        case Some(name) => log_file.err("log from different session " + quote(name))
wenzelm@64045
   166
      }
wenzelm@64062
   167
    val session_timing = log_file.find_props("\fTiming = ") getOrElse Nil
wenzelm@64082
   168
    val command_timings_ =
wenzelm@64082
   169
      if (command_timings) log_file.filter_props("\fcommand_timing = ") else Nil
wenzelm@64082
   170
    val ml_statistics_ =
wenzelm@64082
   171
      if (ml_statistics) log_file.filter_props("\fML_statistics = ") else Nil
wenzelm@64082
   172
    val task_statistics_ =
wenzelm@64082
   173
      if (task_statistics) log_file.filter_props("\ftask_statistics = ") else Nil
wenzelm@64045
   174
wenzelm@64082
   175
    Session_Info(session_name, session_timing, command_timings_, ml_statistics_, task_statistics_)
wenzelm@64045
   176
  }
wenzelm@64054
   177
wenzelm@64054
   178
wenzelm@64062
   179
  /* header and meta data */
wenzelm@64061
   180
wenzelm@64061
   181
  object Header_Kind extends Enumeration
wenzelm@64061
   182
  {
wenzelm@64061
   183
    val ISATEST = Value("isatest")
wenzelm@64061
   184
    val AFP_TEST = Value("afp-test")
wenzelm@64061
   185
    val JENKINS = Value("jenkins")
wenzelm@64061
   186
  }
wenzelm@64061
   187
wenzelm@64081
   188
  sealed case class Header(
wenzelm@64081
   189
    kind: Header_Kind.Value, props: Properties.T, settings: List[(String, String)])
wenzelm@64061
   190
wenzelm@64061
   191
  object Field
wenzelm@64061
   192
  {
wenzelm@64061
   193
    val build_host = "build_host"
wenzelm@64061
   194
    val build_start = "build_start"
wenzelm@64061
   195
    val build_end = "build_end"
wenzelm@64061
   196
    val isabelle_version = "isabelle_version"
wenzelm@64061
   197
    val afp_version = "afp_version"
wenzelm@64061
   198
  }
wenzelm@64061
   199
wenzelm@64061
   200
  object AFP
wenzelm@64061
   201
  {
wenzelm@64061
   202
    val Date_Format =
wenzelm@64061
   203
      Date.Format.make_patterns(List("EEE MMM d HH:mm:ss VV yyyy", "EEE MMM d HH:mm:ss O yyyy"),
wenzelm@64061
   204
        // workaround for jdk-8u102
wenzelm@64061
   205
        s => Word.implode(Word.explode(s).map({ case "CEST" => "GMT+2" case a => a })))
wenzelm@64062
   206
wenzelm@64088
   207
    val Test_Start = new Regex("""^Start test (?:for \S+)? at (.+), (\S+)$""")
wenzelm@64087
   208
    val Test_End = new Regex("""^End test on (.+), \S+, elapsed time:.*$""")
wenzelm@64087
   209
    val Isabelle_Version = new Regex("""^Isabelle version: .* -- hg id (\S+)$""")
wenzelm@64087
   210
    val AFP_Version = new Regex("""^AFP version: .* -- hg id (\S+)$""")
wenzelm@64061
   211
  }
wenzelm@64061
   212
wenzelm@64062
   213
  private def parse_header(log_file: Log_File): Header =
wenzelm@64061
   214
  {
wenzelm@64062
   215
    log_file.lines match {
wenzelm@64062
   216
      case AFP.Test_Start(start, hostname) :: _ =>
wenzelm@64062
   217
        (start, log_file.lines.last) match {
wenzelm@64062
   218
          case (AFP.Date_Format(start_date), AFP.Test_End(AFP.Date_Format(end_date))) =>
wenzelm@64062
   219
            val isabelle_version =
wenzelm@64088
   220
              log_file.find_match(AFP.Isabelle_Version).map((Field.isabelle_version, _))
wenzelm@64062
   221
            val afp_version =
wenzelm@64088
   222
              log_file.find_match(AFP.AFP_Version).map((Field.afp_version, _))
wenzelm@64061
   223
wenzelm@64062
   224
            Header(Header_Kind.AFP_TEST,
wenzelm@64061
   225
              List(
wenzelm@64061
   226
                Field.build_host -> hostname,
wenzelm@64061
   227
                Field.build_start -> start_date.toString,
wenzelm@64088
   228
                Field.build_end -> end_date.toString) :::
wenzelm@64088
   229
              isabelle_version.toList :::
wenzelm@64088
   230
              afp_version.toList,
wenzelm@64081
   231
              log_file.get_settings(Settings.all_settings))
wenzelm@64062
   232
wenzelm@64062
   233
          case _ => log_file.err("cannot detect start/end date in afp-test log")
wenzelm@64061
   234
        }
wenzelm@64062
   235
      case _ => log_file.err("cannot detect log header format")
wenzelm@64061
   236
    }
wenzelm@64061
   237
  }
wenzelm@64061
   238
wenzelm@64085
   239
wenzelm@64085
   240
  /* build info: produced by isabelle build */
wenzelm@64085
   241
wenzelm@64061
   242
  object Session_Status extends Enumeration
wenzelm@64061
   243
  {
wenzelm@64085
   244
    val EXISTING = Value("existing")
wenzelm@64061
   245
    val FINISHED = Value("finished")
wenzelm@64061
   246
    val FAILED = Value("failed")
wenzelm@64061
   247
    val CANCELLED = Value("cancelled")
wenzelm@64061
   248
  }
wenzelm@64061
   249
wenzelm@64085
   250
  sealed case class Session_Entry(
wenzelm@64085
   251
    chapter: String,
wenzelm@64085
   252
    groups: List[String],
wenzelm@64085
   253
    threads: Option[Int],
wenzelm@64089
   254
    timing: Timing,
wenzelm@64089
   255
    ml_timing: Timing,
wenzelm@64085
   256
    status: Session_Status.Value)
wenzelm@64085
   257
  {
wenzelm@64085
   258
    def finished: Boolean = status == Session_Status.FINISHED
wenzelm@64085
   259
  }
wenzelm@64054
   260
wenzelm@64085
   261
  sealed case class Build_Info(sessions: Map[String, Session_Entry])
wenzelm@64054
   262
  {
wenzelm@64085
   263
    def session(name: String): Session_Entry = sessions(name)
wenzelm@64085
   264
    def get_session(name: String): Option[Session_Entry] = sessions.get(name)
wenzelm@64054
   265
wenzelm@64089
   266
    def get_default[A](name: String, f: Session_Entry => A, x: A): A =
wenzelm@64085
   267
      get_session(name) match {
wenzelm@64089
   268
        case Some(entry) => f(entry)
wenzelm@64089
   269
        case None => x
wenzelm@64085
   270
      }
wenzelm@64085
   271
wenzelm@64089
   272
    def finished(name: String): Boolean = get_default(name, _.finished, false)
wenzelm@64089
   273
    def timing(name: String): Timing = get_default(name, _.timing, Timing.zero)
wenzelm@64089
   274
    def ml_timing(name: String): Timing = get_default(name, _.ml_timing, Timing.zero)
wenzelm@64054
   275
  }
wenzelm@64054
   276
wenzelm@64085
   277
  private def parse_build_info(log_file: Log_File): Build_Info =
wenzelm@64085
   278
  {
wenzelm@64085
   279
    object Chapter_Name
wenzelm@64085
   280
    {
wenzelm@64085
   281
      def unapply(s: String): Some[(String, String)] =
wenzelm@64085
   282
        space_explode('/', s) match {
wenzelm@64085
   283
          case List(chapter, name) => Some((chapter, name))
wenzelm@64085
   284
          case _ => Some(("", s))
wenzelm@64085
   285
        }
wenzelm@64085
   286
    }
wenzelm@64054
   287
wenzelm@64085
   288
    val Session_No_Groups = new Regex("""^Session (\S+)$""")
wenzelm@64085
   289
    val Session_Groups = new Regex("""^Session (\S+) \((.*)\)$""")
wenzelm@64085
   290
    val Session_Finished1 =
wenzelm@64085
   291
      new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time, (\d+):(\d+):(\d+) cpu time.*$""")
wenzelm@64085
   292
    val Session_Finished2 =
wenzelm@64085
   293
      new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time.*$""")
wenzelm@64085
   294
    val Session_Timing =
wenzelm@64085
   295
      new Regex("""^Timing (\S+) \((\d) threads, (\d+\.\d+)s elapsed time, (\d+\.\d+)s cpu time, (\d+\.\d+)s GC time.*$""")
wenzelm@64086
   296
    val Session_Started = new Regex("""^(?:Running|Building) (\S+) \.\.\.$""")
wenzelm@64085
   297
    val Session_Failed = new Regex("""^(\S+) FAILED""")
wenzelm@64085
   298
    val Session_Cancelled = new Regex("""^(\S+) CANCELLED""")
wenzelm@64085
   299
wenzelm@64085
   300
    var chapter = Map.empty[String, String]
wenzelm@64085
   301
    var groups = Map.empty[String, List[String]]
wenzelm@64085
   302
    var threads = Map.empty[String, Int]
wenzelm@64054
   303
    var timing = Map.empty[String, Timing]
wenzelm@64085
   304
    var ml_timing = Map.empty[String, Timing]
wenzelm@64086
   305
    var started = Set.empty[String]
wenzelm@64085
   306
    var failed = Set.empty[String]
wenzelm@64085
   307
    var cancelled = Set.empty[String]
wenzelm@64085
   308
    def all_sessions: Set[String] =
wenzelm@64085
   309
      chapter.keySet ++ groups.keySet ++ threads.keySet ++
wenzelm@64086
   310
      timing.keySet ++ ml_timing.keySet ++ failed ++ cancelled ++ started
wenzelm@64085
   311
wenzelm@64054
   312
wenzelm@64062
   313
    for (line <- log_file.lines) {
wenzelm@64054
   314
      line match {
wenzelm@64085
   315
        case Session_No_Groups(Chapter_Name(chapt, name)) =>
wenzelm@64085
   316
          chapter += (name -> chapt)
wenzelm@64085
   317
          groups += (name -> Nil)
wenzelm@64085
   318
        case Session_Groups(Chapter_Name(chapt, name), grps) =>
wenzelm@64085
   319
          chapter += (name -> chapt)
wenzelm@64085
   320
          groups += (name -> Word.explode(grps))
wenzelm@64086
   321
        case Session_Started(name) =>
wenzelm@64086
   322
          started += name
wenzelm@64054
   323
        case Session_Finished1(name,
wenzelm@64054
   324
            Value.Int(e1), Value.Int(e2), Value.Int(e3),
wenzelm@64054
   325
            Value.Int(c1), Value.Int(c2), Value.Int(c3)) =>
wenzelm@64054
   326
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@64054
   327
          val cpu = Time.hms(c1, c2, c3)
wenzelm@64085
   328
          timing += (name -> Timing(elapsed, cpu, Time.zero))
wenzelm@64054
   329
        case Session_Finished2(name,
wenzelm@64054
   330
            Value.Int(e1), Value.Int(e2), Value.Int(e3)) =>
wenzelm@64054
   331
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@64085
   332
          timing += (name -> Timing(elapsed, Time.zero, Time.zero))
wenzelm@64054
   333
        case Session_Timing(name,
wenzelm@64054
   334
            Value.Int(t), Value.Double(e), Value.Double(c), Value.Double(g)) =>
wenzelm@64054
   335
          val elapsed = Time.seconds(e)
wenzelm@64054
   336
          val cpu = Time.seconds(c)
wenzelm@64054
   337
          val gc = Time.seconds(g)
wenzelm@64085
   338
          ml_timing += (name -> Timing(elapsed, cpu, gc))
wenzelm@64054
   339
          threads += (name -> t)
wenzelm@64054
   340
        case _ =>
wenzelm@64054
   341
      }
wenzelm@64054
   342
    }
wenzelm@64054
   343
wenzelm@64085
   344
    val sessions =
wenzelm@64085
   345
      Map(
wenzelm@64085
   346
        (for (name <- all_sessions.toList) yield {
wenzelm@64085
   347
          val status =
wenzelm@64085
   348
            if (failed(name)) Session_Status.FAILED
wenzelm@64085
   349
            else if (cancelled(name)) Session_Status.CANCELLED
wenzelm@64086
   350
            else if (timing.isDefinedAt(name) || ml_timing.isDefinedAt(name))
wenzelm@64086
   351
              Session_Status.FINISHED
wenzelm@64086
   352
            else if (started(name)) Session_Status.FAILED
wenzelm@64085
   353
            else Session_Status.EXISTING
wenzelm@64085
   354
          val entry =
wenzelm@64085
   355
            Session_Entry(
wenzelm@64085
   356
              chapter.getOrElse(name, ""),
wenzelm@64085
   357
              groups.getOrElse(name, Nil),
wenzelm@64085
   358
              threads.get(name),
wenzelm@64089
   359
              timing.getOrElse(name, Timing.zero),
wenzelm@64089
   360
              ml_timing.getOrElse(name, Timing.zero),
wenzelm@64085
   361
              status)
wenzelm@64085
   362
          (name -> entry)
wenzelm@64085
   363
        }):_*)
wenzelm@64085
   364
    Build_Info(sessions)
wenzelm@64054
   365
  }
wenzelm@64045
   366
}