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