src/Pure/Tools/build_log.scala
author wenzelm
Thu Oct 06 11:13:12 2016 +0200 (2016-10-06)
changeset 64062 a7352cbde7d7
parent 64061 1bbea2b55d22
child 64063 2c5039363ea3
permissions -rw-r--r--
misc tuning and clarification;
     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, split_lines(Library.trim_line(text)).map(Library.trim_line(_)))
    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   val ml_settings = List("ML_PLATFORM", "ML_HOME", "ML_SYSTEM", "ML_OPTIONS")
   138 
   139   object AFP
   140   {
   141     val Date_Format =
   142       Date.Format.make_patterns(List("EEE MMM d HH:mm:ss VV yyyy", "EEE MMM d HH:mm:ss O yyyy"),
   143         // workaround for jdk-8u102
   144         s => Word.implode(Word.explode(s).map({ case "CEST" => "GMT+2" case a => a })))
   145 
   146     val Test_Start = new Regex("""^Start test for .+ at (.+), (\w+)$""")
   147     val Test_End = new Regex("""^End test on (.+), \w+, elapsed time:.*$""")
   148     val Isabelle_Version = new Regex("""^Isabelle version: .* -- hg id (\w+)$""")
   149     val AFP_Version = new Regex("""^AFP version: .* -- hg id (\w+)$""")
   150     val settings = "ISABELLE_BUILD_OPTIONS" :: ml_settings
   151   }
   152 
   153   private def parse_header(log_file: Log_File): Header =
   154   {
   155     log_file.lines match {
   156       case AFP.Test_Start(start, hostname) :: _ =>
   157         (start, log_file.lines.last) match {
   158           case (AFP.Date_Format(start_date), AFP.Test_End(AFP.Date_Format(end_date))) =>
   159             val isabelle_version =
   160               log_file.find_match(AFP.Isabelle_Version) getOrElse
   161                 log_file.err("missing Isabelle version")
   162             val afp_version =
   163               log_file.find_match(AFP.AFP_Version) getOrElse
   164                 log_file.err("missing AFP version")
   165 
   166             Header(Header_Kind.AFP_TEST,
   167               List(
   168                 Field.build_host -> hostname,
   169                 Field.build_start -> start_date.toString,
   170                 Field.build_end -> end_date.toString,
   171                 Field.isabelle_version -> isabelle_version,
   172                 Field.afp_version -> afp_version),
   173               log_file.get_settings(AFP.settings))
   174 
   175           case _ => log_file.err("cannot detect start/end date in afp-test log")
   176         }
   177       case _ => log_file.err("cannot detect log header format")
   178     }
   179   }
   180 
   181   object Session_Status extends Enumeration
   182   {
   183     val UNKNOWN = Value("unknown")
   184     val FINISHED = Value("finished")
   185     val FAILED = Value("failed")
   186     val CANCELLED = Value("cancelled")
   187   }
   188 
   189 
   190   /* main log: produced by isatest, afp-test, jenkins etc. */
   191 
   192   sealed case class Info(
   193     ml_options: List[(String, String)],
   194     finished: Map[String, Timing],
   195     timing: Map[String, Timing],
   196     threads: Map[String, Int])
   197   {
   198     val sessions: Set[String] = finished.keySet ++ timing.keySet
   199 
   200     override def toString: String =
   201       sessions.toList.sorted.mkString("Build_Log.Info(", ", ", ")")
   202   }
   203 
   204   private val Session_Finished1 =
   205     new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time, (\d+):(\d+):(\d+) cpu time.*$""")
   206   private val Session_Finished2 =
   207     new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time.*$""")
   208   private val Session_Timing =
   209     new Regex("""^Timing (\S+) \((\d) threads, (\d+\.\d+)s elapsed time, (\d+\.\d+)s cpu time, (\d+\.\d+)s GC time.*$""")
   210 
   211   private object ML_Option
   212   {
   213     def unapply(s: String): Option[(String, String)] =
   214       s.indexOf('=') match {
   215         case -1 => None
   216         case i =>
   217           val a = s.substring(0, i)
   218           Library.try_unquote(s.substring(i + 1)) match {
   219             case Some(b) if Build.ml_options.contains(a) => Some((a, b))
   220             case _ => None
   221           }
   222       }
   223   }
   224 
   225   private def parse_info(log_file: Log_File): Info =
   226   {
   227     val ml_options = new mutable.ListBuffer[(String, String)]
   228     var finished = Map.empty[String, Timing]
   229     var timing = Map.empty[String, Timing]
   230     var threads = Map.empty[String, Int]
   231 
   232     for (line <- log_file.lines) {
   233       line match {
   234         case Session_Finished1(name,
   235             Value.Int(e1), Value.Int(e2), Value.Int(e3),
   236             Value.Int(c1), Value.Int(c2), Value.Int(c3)) =>
   237           val elapsed = Time.hms(e1, e2, e3)
   238           val cpu = Time.hms(c1, c2, c3)
   239           finished += (name -> Timing(elapsed, cpu, Time.zero))
   240         case Session_Finished2(name,
   241             Value.Int(e1), Value.Int(e2), Value.Int(e3)) =>
   242           val elapsed = Time.hms(e1, e2, e3)
   243           finished += (name -> Timing(elapsed, Time.zero, Time.zero))
   244         case Session_Timing(name,
   245             Value.Int(t), Value.Double(e), Value.Double(c), Value.Double(g)) =>
   246           val elapsed = Time.seconds(e)
   247           val cpu = Time.seconds(c)
   248           val gc = Time.seconds(g)
   249           timing += (name -> Timing(elapsed, cpu, gc))
   250           threads += (name -> t)
   251         case ML_Option(a, b) => ml_options += (a -> b)
   252         case _ =>
   253       }
   254     }
   255 
   256     Info(ml_options.toList, finished, timing, threads)
   257   }
   258 }