src/Pure/Tools/build_log.scala
author wenzelm
Fri Oct 07 13:58:10 2016 +0200 (2016-10-07)
changeset 64082 d57c7295f601
parent 64081 38bb09ed965b
child 64083 fef1a0a59c12
permissions -rw-r--r--
clarified signature;
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@64061
    10
import java.time.ZonedDateTime
wenzelm@64061
    11
import java.time.format.{DateTimeFormatter, DateTimeParseException}
wenzelm@64061
    12
wenzelm@64054
    13
import scala.collection.mutable
wenzelm@64054
    14
import scala.util.matching.Regex
wenzelm@64054
    15
wenzelm@64054
    16
wenzelm@64045
    17
object Build_Log
wenzelm@64045
    18
{
wenzelm@64081
    19
  /** settings **/
wenzelm@64080
    20
wenzelm@64081
    21
  object Settings
wenzelm@64081
    22
  {
wenzelm@64081
    23
    val build_settings = List("ISABELLE_BUILD_OPTIONS")
wenzelm@64081
    24
    val ml_settings = List("ML_PLATFORM", "ML_HOME", "ML_SYSTEM", "ML_OPTIONS")
wenzelm@64081
    25
    val all_settings = build_settings ::: ml_settings
wenzelm@64081
    26
wenzelm@64081
    27
    type Entry = (String, String)
wenzelm@64081
    28
    type T = List[Entry]
wenzelm@64080
    29
wenzelm@64081
    30
    object Entry
wenzelm@64081
    31
    {
wenzelm@64081
    32
      def unapply(s: String): Option[Entry] =
wenzelm@64081
    33
        s.indexOf('=') match {
wenzelm@64081
    34
          case -1 => None
wenzelm@64081
    35
          case i =>
wenzelm@64081
    36
            val a = s.substring(0, i)
wenzelm@64081
    37
            val b = Library.perhaps_unquote(s.substring(i + 1))
wenzelm@64081
    38
            Some((a, b))
wenzelm@64081
    39
        }
wenzelm@64081
    40
      def apply(a: String, b: String): String = a + "=" + quote(b)
wenzelm@64081
    41
      def getenv(a: String): String = apply(a, Isabelle_System.getenv(a))
wenzelm@64081
    42
    }
wenzelm@64080
    43
wenzelm@64081
    44
    def show(): String =
wenzelm@64081
    45
      cat_lines(
wenzelm@64081
    46
        build_settings.map(Entry.getenv(_)) ::: List("") ::: ml_settings.map(Entry.getenv(_)))
wenzelm@64080
    47
  }
wenzelm@64080
    48
wenzelm@64080
    49
wenzelm@64062
    50
  /** log file **/
wenzelm@64045
    51
wenzelm@64062
    52
  object Log_File
wenzelm@64062
    53
  {
wenzelm@64062
    54
    def apply(name: String, lines: List[String]): Log_File =
wenzelm@64062
    55
      new Log_File(name, lines)
wenzelm@64062
    56
wenzelm@64062
    57
    def apply(name: String, text: String): Log_File =
wenzelm@64063
    58
      Log_File(name, Library.trim_split_lines(text))
wenzelm@64062
    59
  }
wenzelm@64062
    60
wenzelm@64062
    61
  class Log_File private(val name: String, val lines: List[String])
wenzelm@64045
    62
  {
wenzelm@64062
    63
    log_file =>
wenzelm@64062
    64
wenzelm@64062
    65
    override def toString: String = name
wenzelm@64062
    66
wenzelm@64062
    67
    def text: String = cat_lines(lines)
wenzelm@64062
    68
wenzelm@64062
    69
    def err(msg: String): Nothing =
wenzelm@64062
    70
      error("Error in log file " + quote(name) + ": " + msg)
wenzelm@64062
    71
wenzelm@64062
    72
wenzelm@64062
    73
    /* inlined content */
wenzelm@64062
    74
wenzelm@64062
    75
    def find[A](f: String => Option[A]): Option[A] =
wenzelm@64062
    76
      lines.iterator.map(f).find(_.isDefined).map(_.get)
wenzelm@64062
    77
wenzelm@64062
    78
    def find_match(regex: Regex): Option[String] =
wenzelm@64062
    79
      lines.iterator.map(regex.unapplySeq(_)).find(res => res.isDefined && res.get.length == 1).
wenzelm@64062
    80
        map(res => res.get.head)
wenzelm@64062
    81
wenzelm@64062
    82
wenzelm@64062
    83
    /* settings */
wenzelm@64062
    84
wenzelm@64081
    85
    def get_setting(a: String): Settings.Entry =
wenzelm@64081
    86
      Settings.Entry.unapply(
wenzelm@64081
    87
        lines.find(_.startsWith(a + "=")) getOrElse err("missing " + a)).get
wenzelm@64045
    88
wenzelm@64081
    89
    def get_settings(as: List[String]): Settings.T = as.map(get_setting(_))
wenzelm@64062
    90
wenzelm@64062
    91
wenzelm@64062
    92
    /* properties (YXML) */
wenzelm@64062
    93
wenzelm@64062
    94
    val xml_cache = new XML.Cache()
wenzelm@64062
    95
wenzelm@64062
    96
    def parse_props(text: String): Properties.T =
wenzelm@64062
    97
      xml_cache.props(XML.Decode.properties(YXML.parse_body(text)))
wenzelm@64062
    98
wenzelm@64062
    99
    def filter_props(prefix: String): List[Properties.T] =
wenzelm@64062
   100
      for (line <- lines; s <- Library.try_unprefix(prefix, line)) yield parse_props(s)
wenzelm@64045
   101
wenzelm@64062
   102
    def find_line(prefix: String): Option[String] =
wenzelm@64062
   103
      find(Library.try_unprefix(prefix, _))
wenzelm@64062
   104
wenzelm@64062
   105
    def find_props(prefix: String): Option[Properties.T] =
wenzelm@64062
   106
      find_line(prefix).map(parse_props(_))
wenzelm@64062
   107
wenzelm@64062
   108
wenzelm@64062
   109
    /* parse various formats */
wenzelm@64062
   110
wenzelm@64082
   111
    def parse_session_info(
wenzelm@64082
   112
        session_name: String,
wenzelm@64082
   113
        command_timings: Boolean = false,
wenzelm@64082
   114
        ml_statistics: Boolean = false,
wenzelm@64082
   115
        task_statistics: Boolean = false): Session_Info =
wenzelm@64082
   116
      Build_Log.parse_session_info(
wenzelm@64082
   117
        log_file, session_name, command_timings, ml_statistics, task_statistics)
wenzelm@64062
   118
wenzelm@64062
   119
    def parse_header: Header = Build_Log.parse_header(log_file)
wenzelm@64062
   120
wenzelm@64062
   121
    def parse_info: Info = Build_Log.parse_info(log_file)
wenzelm@64045
   122
  }
wenzelm@64045
   123
wenzelm@64045
   124
wenzelm@64045
   125
  /* session log: produced by "isabelle build" */
wenzelm@64045
   126
wenzelm@64045
   127
  sealed case class Session_Info(
wenzelm@64045
   128
    session_name: String,
wenzelm@64045
   129
    session_timing: Properties.T,
wenzelm@64045
   130
    command_timings: List[Properties.T],
wenzelm@64045
   131
    ml_statistics: List[Properties.T],
wenzelm@64045
   132
    task_statistics: List[Properties.T])
wenzelm@64045
   133
wenzelm@64082
   134
  private def parse_session_info(
wenzelm@64082
   135
    log_file: Log_File,
wenzelm@64082
   136
    default_name: String,
wenzelm@64082
   137
    command_timings: Boolean,
wenzelm@64082
   138
    ml_statistics: Boolean,
wenzelm@64082
   139
    task_statistics: Boolean): Session_Info =
wenzelm@64045
   140
  {
wenzelm@64045
   141
    val xml_cache = new XML.Cache()
wenzelm@64045
   142
wenzelm@64045
   143
    val session_name =
wenzelm@64062
   144
      log_file.find_line("\fSession.name = ") match {
wenzelm@64082
   145
        case None => default_name
wenzelm@64082
   146
        case Some(name) if default_name == "" || default_name == name => name
wenzelm@64062
   147
        case Some(name) => log_file.err("log from different session " + quote(name))
wenzelm@64045
   148
      }
wenzelm@64062
   149
    val session_timing = log_file.find_props("\fTiming = ") getOrElse Nil
wenzelm@64082
   150
    val command_timings_ =
wenzelm@64082
   151
      if (command_timings) log_file.filter_props("\fcommand_timing = ") else Nil
wenzelm@64082
   152
    val ml_statistics_ =
wenzelm@64082
   153
      if (ml_statistics) log_file.filter_props("\fML_statistics = ") else Nil
wenzelm@64082
   154
    val task_statistics_ =
wenzelm@64082
   155
      if (task_statistics) log_file.filter_props("\ftask_statistics = ") else Nil
wenzelm@64045
   156
wenzelm@64082
   157
    Session_Info(session_name, session_timing, command_timings_, ml_statistics_, task_statistics_)
wenzelm@64045
   158
  }
wenzelm@64054
   159
wenzelm@64054
   160
wenzelm@64062
   161
  /* header and meta data */
wenzelm@64061
   162
wenzelm@64061
   163
  object Header_Kind extends Enumeration
wenzelm@64061
   164
  {
wenzelm@64061
   165
    val ISATEST = Value("isatest")
wenzelm@64061
   166
    val AFP_TEST = Value("afp-test")
wenzelm@64061
   167
    val JENKINS = Value("jenkins")
wenzelm@64061
   168
  }
wenzelm@64061
   169
wenzelm@64081
   170
  sealed case class Header(
wenzelm@64081
   171
    kind: Header_Kind.Value, props: Properties.T, settings: List[(String, String)])
wenzelm@64061
   172
wenzelm@64061
   173
  object Field
wenzelm@64061
   174
  {
wenzelm@64061
   175
    val build_host = "build_host"
wenzelm@64061
   176
    val build_start = "build_start"
wenzelm@64061
   177
    val build_end = "build_end"
wenzelm@64061
   178
    val isabelle_version = "isabelle_version"
wenzelm@64061
   179
    val afp_version = "afp_version"
wenzelm@64061
   180
  }
wenzelm@64061
   181
wenzelm@64061
   182
  object AFP
wenzelm@64061
   183
  {
wenzelm@64061
   184
    val Date_Format =
wenzelm@64061
   185
      Date.Format.make_patterns(List("EEE MMM d HH:mm:ss VV yyyy", "EEE MMM d HH:mm:ss O yyyy"),
wenzelm@64061
   186
        // workaround for jdk-8u102
wenzelm@64061
   187
        s => Word.implode(Word.explode(s).map({ case "CEST" => "GMT+2" case a => a })))
wenzelm@64062
   188
wenzelm@64061
   189
    val Test_Start = new Regex("""^Start test for .+ at (.+), (\w+)$""")
wenzelm@64061
   190
    val Test_End = new Regex("""^End test on (.+), \w+, elapsed time:.*$""")
wenzelm@64061
   191
    val Isabelle_Version = new Regex("""^Isabelle version: .* -- hg id (\w+)$""")
wenzelm@64061
   192
    val AFP_Version = new Regex("""^AFP version: .* -- hg id (\w+)$""")
wenzelm@64061
   193
  }
wenzelm@64061
   194
wenzelm@64062
   195
  private def parse_header(log_file: Log_File): Header =
wenzelm@64061
   196
  {
wenzelm@64062
   197
    log_file.lines match {
wenzelm@64062
   198
      case AFP.Test_Start(start, hostname) :: _ =>
wenzelm@64062
   199
        (start, log_file.lines.last) match {
wenzelm@64062
   200
          case (AFP.Date_Format(start_date), AFP.Test_End(AFP.Date_Format(end_date))) =>
wenzelm@64062
   201
            val isabelle_version =
wenzelm@64062
   202
              log_file.find_match(AFP.Isabelle_Version) getOrElse
wenzelm@64062
   203
                log_file.err("missing Isabelle version")
wenzelm@64062
   204
            val afp_version =
wenzelm@64062
   205
              log_file.find_match(AFP.AFP_Version) getOrElse
wenzelm@64062
   206
                log_file.err("missing AFP version")
wenzelm@64061
   207
wenzelm@64062
   208
            Header(Header_Kind.AFP_TEST,
wenzelm@64061
   209
              List(
wenzelm@64061
   210
                Field.build_host -> hostname,
wenzelm@64061
   211
                Field.build_start -> start_date.toString,
wenzelm@64062
   212
                Field.build_end -> end_date.toString,
wenzelm@64062
   213
                Field.isabelle_version -> isabelle_version,
wenzelm@64062
   214
                Field.afp_version -> afp_version),
wenzelm@64081
   215
              log_file.get_settings(Settings.all_settings))
wenzelm@64062
   216
wenzelm@64062
   217
          case _ => log_file.err("cannot detect start/end date in afp-test log")
wenzelm@64061
   218
        }
wenzelm@64062
   219
      case _ => log_file.err("cannot detect log header format")
wenzelm@64061
   220
    }
wenzelm@64061
   221
  }
wenzelm@64061
   222
wenzelm@64061
   223
  object Session_Status extends Enumeration
wenzelm@64061
   224
  {
wenzelm@64061
   225
    val UNKNOWN = Value("unknown")
wenzelm@64061
   226
    val FINISHED = Value("finished")
wenzelm@64061
   227
    val FAILED = Value("failed")
wenzelm@64061
   228
    val CANCELLED = Value("cancelled")
wenzelm@64061
   229
  }
wenzelm@64061
   230
wenzelm@64061
   231
wenzelm@64054
   232
  /* main log: produced by isatest, afp-test, jenkins etc. */
wenzelm@64054
   233
wenzelm@64054
   234
  sealed case class Info(
wenzelm@64081
   235
    settings: List[(String, String)],
wenzelm@64054
   236
    finished: Map[String, Timing],
wenzelm@64054
   237
    timing: Map[String, Timing],
wenzelm@64054
   238
    threads: Map[String, Int])
wenzelm@64054
   239
  {
wenzelm@64054
   240
    val sessions: Set[String] = finished.keySet ++ timing.keySet
wenzelm@64054
   241
wenzelm@64054
   242
    override def toString: String =
wenzelm@64054
   243
      sessions.toList.sorted.mkString("Build_Log.Info(", ", ", ")")
wenzelm@64054
   244
  }
wenzelm@64054
   245
wenzelm@64054
   246
  private val Session_Finished1 =
wenzelm@64054
   247
    new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time, (\d+):(\d+):(\d+) cpu time.*$""")
wenzelm@64054
   248
  private val Session_Finished2 =
wenzelm@64054
   249
    new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time.*$""")
wenzelm@64054
   250
  private val Session_Timing =
wenzelm@64054
   251
    new Regex("""^Timing (\S+) \((\d) threads, (\d+\.\d+)s elapsed time, (\d+\.\d+)s cpu time, (\d+\.\d+)s GC time.*$""")
wenzelm@64054
   252
wenzelm@64062
   253
  private def parse_info(log_file: Log_File): Info =
wenzelm@64054
   254
  {
wenzelm@64081
   255
    val settings = new mutable.ListBuffer[(String, String)]
wenzelm@64054
   256
    var finished = Map.empty[String, Timing]
wenzelm@64054
   257
    var timing = Map.empty[String, Timing]
wenzelm@64054
   258
    var threads = Map.empty[String, Int]
wenzelm@64054
   259
wenzelm@64062
   260
    for (line <- log_file.lines) {
wenzelm@64054
   261
      line match {
wenzelm@64054
   262
        case Session_Finished1(name,
wenzelm@64054
   263
            Value.Int(e1), Value.Int(e2), Value.Int(e3),
wenzelm@64054
   264
            Value.Int(c1), Value.Int(c2), Value.Int(c3)) =>
wenzelm@64054
   265
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@64054
   266
          val cpu = Time.hms(c1, c2, c3)
wenzelm@64054
   267
          finished += (name -> Timing(elapsed, cpu, Time.zero))
wenzelm@64054
   268
        case Session_Finished2(name,
wenzelm@64054
   269
            Value.Int(e1), Value.Int(e2), Value.Int(e3)) =>
wenzelm@64054
   270
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@64054
   271
          finished += (name -> Timing(elapsed, Time.zero, Time.zero))
wenzelm@64054
   272
        case Session_Timing(name,
wenzelm@64054
   273
            Value.Int(t), Value.Double(e), Value.Double(c), Value.Double(g)) =>
wenzelm@64054
   274
          val elapsed = Time.seconds(e)
wenzelm@64054
   275
          val cpu = Time.seconds(c)
wenzelm@64054
   276
          val gc = Time.seconds(g)
wenzelm@64054
   277
          timing += (name -> Timing(elapsed, cpu, gc))
wenzelm@64054
   278
          threads += (name -> t)
wenzelm@64081
   279
        case Settings.Entry(a, b) if Settings.all_settings.contains(a) =>
wenzelm@64081
   280
          settings += (a -> b)
wenzelm@64054
   281
        case _ =>
wenzelm@64054
   282
      }
wenzelm@64054
   283
    }
wenzelm@64054
   284
wenzelm@64081
   285
    Info(settings.toList, finished, timing, threads)
wenzelm@64054
   286
  }
wenzelm@64045
   287
}