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