src/Pure/Tools/build_log.scala
author wenzelm
Wed Oct 05 22:09:53 2016 +0200 (2016-10-05)
changeset 64061 1bbea2b55d22
parent 64054 1fc9ab31720d
child 64062 a7352cbde7d7
permissions -rw-r--r--
some support for header and data fields, notably from afp-test;
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@64045
    19
  /* inlined properties (YXML) */
wenzelm@64045
    20
wenzelm@64045
    21
  object Props
wenzelm@64045
    22
  {
wenzelm@64053
    23
    def print(props: Properties.T): String = YXML.string_of_body(XML.Encode.properties(props))
wenzelm@64045
    24
    def parse(text: String): Properties.T = XML.Decode.properties(YXML.parse_body(text))
wenzelm@64045
    25
wenzelm@64045
    26
    def parse_lines(prefix: String, lines: List[String]): List[Properties.T] =
wenzelm@64045
    27
      for (line <- lines; s <- Library.try_unprefix(prefix, line)) yield parse(s)
wenzelm@64045
    28
wenzelm@64045
    29
    def find_parse_line(prefix: String, lines: List[String]): Option[Properties.T] =
wenzelm@64045
    30
      lines.find(_.startsWith(prefix)).map(line => parse(line.substring(prefix.length)))
wenzelm@64045
    31
  }
wenzelm@64045
    32
wenzelm@64045
    33
wenzelm@64045
    34
  /* session log: produced by "isabelle build" */
wenzelm@64045
    35
wenzelm@64045
    36
  sealed case class Session_Info(
wenzelm@64045
    37
    session_name: String,
wenzelm@64045
    38
    session_timing: Properties.T,
wenzelm@64045
    39
    command_timings: List[Properties.T],
wenzelm@64045
    40
    ml_statistics: List[Properties.T],
wenzelm@64045
    41
    task_statistics: List[Properties.T])
wenzelm@64045
    42
wenzelm@64045
    43
  val SESSION_NAME = "\fSession.name = "
wenzelm@64045
    44
wenzelm@64045
    45
  def parse_session_info(name0: String, lines: List[String], full: Boolean): Session_Info =
wenzelm@64045
    46
  {
wenzelm@64045
    47
    val xml_cache = new XML.Cache()
wenzelm@64045
    48
    def parse_lines(prfx: String): List[Properties.T] =
wenzelm@64045
    49
      Props.parse_lines(prfx, lines).map(xml_cache.props(_))
wenzelm@64045
    50
wenzelm@64045
    51
    val session_name =
wenzelm@64045
    52
      lines.find(_.startsWith(SESSION_NAME)).map(_.substring(SESSION_NAME.length)) match {
wenzelm@64045
    53
        case None => name0
wenzelm@64045
    54
        case Some(name) if name0 == "" || name0 == name => name
wenzelm@64045
    55
        case Some(name) =>
wenzelm@64045
    56
          error("Session log for " + quote(name0) + " is actually from " + quote(name))
wenzelm@64045
    57
      }
wenzelm@64045
    58
    val session_timing = Props.find_parse_line("\fTiming = ", lines) getOrElse Nil
wenzelm@64045
    59
    val command_timings = parse_lines("\fcommand_timing = ")
wenzelm@64045
    60
    val ml_statistics = if (full) parse_lines("\fML_statistics = ") else Nil
wenzelm@64045
    61
    val task_statistics = if (full) parse_lines("\ftask_statistics = ") else Nil
wenzelm@64045
    62
wenzelm@64045
    63
    Session_Info(session_name, session_timing, command_timings, ml_statistics, task_statistics)
wenzelm@64045
    64
  }
wenzelm@64054
    65
wenzelm@64054
    66
wenzelm@64061
    67
  /* header and data fields */
wenzelm@64061
    68
wenzelm@64061
    69
  object Header_Kind extends Enumeration
wenzelm@64061
    70
  {
wenzelm@64061
    71
    val ISATEST = Value("isatest")
wenzelm@64061
    72
    val AFP_TEST = Value("afp-test")
wenzelm@64061
    73
    val JENKINS = Value("jenkins")
wenzelm@64061
    74
  }
wenzelm@64061
    75
wenzelm@64061
    76
  sealed case class Header(kind: Header_Kind.Value, props: Properties.T, settings: List[String])
wenzelm@64061
    77
wenzelm@64061
    78
  object Field
wenzelm@64061
    79
  {
wenzelm@64061
    80
    val build_host = "build_host"
wenzelm@64061
    81
    val build_start = "build_start"
wenzelm@64061
    82
    val build_end = "build_end"
wenzelm@64061
    83
    val isabelle_version = "isabelle_version"
wenzelm@64061
    84
    val afp_version = "afp_version"
wenzelm@64061
    85
  }
wenzelm@64061
    86
wenzelm@64061
    87
  object AFP
wenzelm@64061
    88
  {
wenzelm@64061
    89
    val Date_Format =
wenzelm@64061
    90
      Date.Format.make_patterns(List("EEE MMM d HH:mm:ss VV yyyy", "EEE MMM d HH:mm:ss O yyyy"),
wenzelm@64061
    91
        // workaround for jdk-8u102
wenzelm@64061
    92
        s => Word.implode(Word.explode(s).map({ case "CEST" => "GMT+2" case a => a })))
wenzelm@64061
    93
    val Test_Start = new Regex("""^Start test for .+ at (.+), (\w+)$""")
wenzelm@64061
    94
    val Test_End = new Regex("""^End test on (.+), \w+, elapsed time:.*$""")
wenzelm@64061
    95
    val Isabelle_Version = new Regex("""^Isabelle version: .* -- hg id (\w+)$""")
wenzelm@64061
    96
    val AFP_Version = new Regex("""^AFP version: .* -- hg id (\w+)$""")
wenzelm@64061
    97
    val settings =
wenzelm@64061
    98
      List("ISABELLE_BUILD_OPTIONS=", "ML_PLATFORM=", "ML_HOME=", "ML_SYSTEM=", "ML_OPTIONS=")
wenzelm@64061
    99
  }
wenzelm@64061
   100
wenzelm@64061
   101
  def parse_header(lines: List[String]): Header =
wenzelm@64061
   102
  {
wenzelm@64061
   103
    val proper_lines = lines.filterNot(line => line.forall(Character.isWhitespace(_)))
wenzelm@64061
   104
wenzelm@64061
   105
    def err(msg: String): Nothing = error(cat_lines((msg + ":") :: lines.take(10)))
wenzelm@64061
   106
wenzelm@64061
   107
    proper_lines match {
wenzelm@64061
   108
      case AFP.Test_Start(start, hostname) :: _ =>
wenzelm@64061
   109
        (start, proper_lines.last) match {
wenzelm@64061
   110
          case (AFP.Date_Format(start_date), AFP.Test_End(AFP.Date_Format(end_date))) =>
wenzelm@64061
   111
            val props =
wenzelm@64061
   112
              List(
wenzelm@64061
   113
                Field.build_host -> hostname,
wenzelm@64061
   114
                Field.build_start -> start_date.toString,
wenzelm@64061
   115
                Field.build_end -> end_date.toString) :::
wenzelm@64061
   116
              lines.collectFirst(
wenzelm@64061
   117
                { case AFP.Isabelle_Version(id) => Field.isabelle_version -> id }).toList :::
wenzelm@64061
   118
              lines.collectFirst(
wenzelm@64061
   119
                { case AFP.AFP_Version(id) => Field.afp_version -> id }).toList
wenzelm@64061
   120
            val settings = lines.filter(line => AFP.settings.exists(line.startsWith(_)))
wenzelm@64061
   121
            Header(Header_Kind.AFP_TEST, props, settings)
wenzelm@64061
   122
          case _ => err("Malformed start/end date in afp-test log")
wenzelm@64061
   123
        }
wenzelm@64061
   124
      case _ => err("Failed to detect build log header")
wenzelm@64061
   125
    }
wenzelm@64061
   126
  }
wenzelm@64061
   127
wenzelm@64061
   128
  object Session_Status extends Enumeration
wenzelm@64061
   129
  {
wenzelm@64061
   130
    val UNKNOWN = Value("unknown")
wenzelm@64061
   131
    val FINISHED = Value("finished")
wenzelm@64061
   132
    val FAILED = Value("failed")
wenzelm@64061
   133
    val CANCELLED = Value("cancelled")
wenzelm@64061
   134
  }
wenzelm@64061
   135
wenzelm@64061
   136
wenzelm@64054
   137
  /* main log: produced by isatest, afp-test, jenkins etc. */
wenzelm@64054
   138
wenzelm@64054
   139
  sealed case class Info(
wenzelm@64054
   140
    ml_options: List[(String, String)],
wenzelm@64054
   141
    finished: Map[String, Timing],
wenzelm@64054
   142
    timing: Map[String, Timing],
wenzelm@64054
   143
    threads: Map[String, Int])
wenzelm@64054
   144
  {
wenzelm@64054
   145
    val sessions: Set[String] = finished.keySet ++ timing.keySet
wenzelm@64054
   146
wenzelm@64054
   147
    override def toString: String =
wenzelm@64054
   148
      sessions.toList.sorted.mkString("Build_Log.Info(", ", ", ")")
wenzelm@64054
   149
  }
wenzelm@64054
   150
wenzelm@64054
   151
  private val Session_Finished1 =
wenzelm@64054
   152
    new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time, (\d+):(\d+):(\d+) cpu time.*$""")
wenzelm@64054
   153
  private val Session_Finished2 =
wenzelm@64054
   154
    new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time.*$""")
wenzelm@64054
   155
  private val Session_Timing =
wenzelm@64054
   156
    new Regex("""^Timing (\S+) \((\d) threads, (\d+\.\d+)s elapsed time, (\d+\.\d+)s cpu time, (\d+\.\d+)s GC time.*$""")
wenzelm@64054
   157
wenzelm@64054
   158
  private object ML_Option
wenzelm@64054
   159
  {
wenzelm@64054
   160
    def unapply(s: String): Option[(String, String)] =
wenzelm@64054
   161
      s.indexOf('=') match {
wenzelm@64054
   162
        case -1 => None
wenzelm@64054
   163
        case i =>
wenzelm@64054
   164
          val a = s.substring(0, i)
wenzelm@64054
   165
          Library.try_unquote(s.substring(i + 1)) match {
wenzelm@64054
   166
            case Some(b) if Build.ml_options.contains(a) => Some((a, b))
wenzelm@64054
   167
            case _ => None
wenzelm@64054
   168
          }
wenzelm@64054
   169
      }
wenzelm@64054
   170
  }
wenzelm@64054
   171
wenzelm@64054
   172
  def parse_info(text: String): Info =
wenzelm@64054
   173
  {
wenzelm@64054
   174
    val ml_options = new mutable.ListBuffer[(String, String)]
wenzelm@64054
   175
    var finished = Map.empty[String, Timing]
wenzelm@64054
   176
    var timing = Map.empty[String, Timing]
wenzelm@64054
   177
    var threads = Map.empty[String, Int]
wenzelm@64054
   178
wenzelm@64054
   179
    for (line <- split_lines(text)) {
wenzelm@64054
   180
      line match {
wenzelm@64054
   181
        case Session_Finished1(name,
wenzelm@64054
   182
            Value.Int(e1), Value.Int(e2), Value.Int(e3),
wenzelm@64054
   183
            Value.Int(c1), Value.Int(c2), Value.Int(c3)) =>
wenzelm@64054
   184
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@64054
   185
          val cpu = Time.hms(c1, c2, c3)
wenzelm@64054
   186
          finished += (name -> Timing(elapsed, cpu, Time.zero))
wenzelm@64054
   187
        case Session_Finished2(name,
wenzelm@64054
   188
            Value.Int(e1), Value.Int(e2), Value.Int(e3)) =>
wenzelm@64054
   189
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@64054
   190
          finished += (name -> Timing(elapsed, Time.zero, Time.zero))
wenzelm@64054
   191
        case Session_Timing(name,
wenzelm@64054
   192
            Value.Int(t), Value.Double(e), Value.Double(c), Value.Double(g)) =>
wenzelm@64054
   193
          val elapsed = Time.seconds(e)
wenzelm@64054
   194
          val cpu = Time.seconds(c)
wenzelm@64054
   195
          val gc = Time.seconds(g)
wenzelm@64054
   196
          timing += (name -> Timing(elapsed, cpu, gc))
wenzelm@64054
   197
          threads += (name -> t)
wenzelm@64054
   198
        case ML_Option(a, b) => ml_options += (a -> b)
wenzelm@64054
   199
        case _ =>
wenzelm@64054
   200
      }
wenzelm@64054
   201
    }
wenzelm@64054
   202
wenzelm@64054
   203
    Info(ml_options.toList, finished, timing, threads)
wenzelm@64054
   204
  }
wenzelm@64045
   205
}