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