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