src/Pure/Tools/build_log.scala
author wenzelm
Fri Oct 07 13:58:10 2016 +0200 (2016-10-07)
changeset 64082 d57c7295f601
parent 64081 38bb09ed965b
child 64083 fef1a0a59c12
permissions -rw-r--r--
clarified signature;
     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(
   112         session_name: String,
   113         command_timings: Boolean = false,
   114         ml_statistics: Boolean = false,
   115         task_statistics: Boolean = false): Session_Info =
   116       Build_Log.parse_session_info(
   117         log_file, session_name, command_timings, ml_statistics, task_statistics)
   118 
   119     def parse_header: Header = Build_Log.parse_header(log_file)
   120 
   121     def parse_info: Info = Build_Log.parse_info(log_file)
   122   }
   123 
   124 
   125   /* session log: produced by "isabelle build" */
   126 
   127   sealed case class Session_Info(
   128     session_name: String,
   129     session_timing: Properties.T,
   130     command_timings: List[Properties.T],
   131     ml_statistics: List[Properties.T],
   132     task_statistics: List[Properties.T])
   133 
   134   private def parse_session_info(
   135     log_file: Log_File,
   136     default_name: String,
   137     command_timings: Boolean,
   138     ml_statistics: Boolean,
   139     task_statistics: Boolean): Session_Info =
   140   {
   141     val xml_cache = new XML.Cache()
   142 
   143     val session_name =
   144       log_file.find_line("\fSession.name = ") match {
   145         case None => default_name
   146         case Some(name) if default_name == "" || default_name == name => name
   147         case Some(name) => log_file.err("log from different session " + quote(name))
   148       }
   149     val session_timing = log_file.find_props("\fTiming = ") getOrElse Nil
   150     val command_timings_ =
   151       if (command_timings) log_file.filter_props("\fcommand_timing = ") else Nil
   152     val ml_statistics_ =
   153       if (ml_statistics) log_file.filter_props("\fML_statistics = ") else Nil
   154     val task_statistics_ =
   155       if (task_statistics) log_file.filter_props("\ftask_statistics = ") else Nil
   156 
   157     Session_Info(session_name, session_timing, command_timings_, ml_statistics_, task_statistics_)
   158   }
   159 
   160 
   161   /* header and meta data */
   162 
   163   object Header_Kind extends Enumeration
   164   {
   165     val ISATEST = Value("isatest")
   166     val AFP_TEST = Value("afp-test")
   167     val JENKINS = Value("jenkins")
   168   }
   169 
   170   sealed case class Header(
   171     kind: Header_Kind.Value, props: Properties.T, settings: List[(String, String)])
   172 
   173   object Field
   174   {
   175     val build_host = "build_host"
   176     val build_start = "build_start"
   177     val build_end = "build_end"
   178     val isabelle_version = "isabelle_version"
   179     val afp_version = "afp_version"
   180   }
   181 
   182   object AFP
   183   {
   184     val Date_Format =
   185       Date.Format.make_patterns(List("EEE MMM d HH:mm:ss VV yyyy", "EEE MMM d HH:mm:ss O yyyy"),
   186         // workaround for jdk-8u102
   187         s => Word.implode(Word.explode(s).map({ case "CEST" => "GMT+2" case a => a })))
   188 
   189     val Test_Start = new Regex("""^Start test for .+ at (.+), (\w+)$""")
   190     val Test_End = new Regex("""^End test on (.+), \w+, elapsed time:.*$""")
   191     val Isabelle_Version = new Regex("""^Isabelle version: .* -- hg id (\w+)$""")
   192     val AFP_Version = new Regex("""^AFP version: .* -- hg id (\w+)$""")
   193   }
   194 
   195   private def parse_header(log_file: Log_File): Header =
   196   {
   197     log_file.lines match {
   198       case AFP.Test_Start(start, hostname) :: _ =>
   199         (start, log_file.lines.last) match {
   200           case (AFP.Date_Format(start_date), AFP.Test_End(AFP.Date_Format(end_date))) =>
   201             val isabelle_version =
   202               log_file.find_match(AFP.Isabelle_Version) getOrElse
   203                 log_file.err("missing Isabelle version")
   204             val afp_version =
   205               log_file.find_match(AFP.AFP_Version) getOrElse
   206                 log_file.err("missing AFP version")
   207 
   208             Header(Header_Kind.AFP_TEST,
   209               List(
   210                 Field.build_host -> hostname,
   211                 Field.build_start -> start_date.toString,
   212                 Field.build_end -> end_date.toString,
   213                 Field.isabelle_version -> isabelle_version,
   214                 Field.afp_version -> afp_version),
   215               log_file.get_settings(Settings.all_settings))
   216 
   217           case _ => log_file.err("cannot detect start/end date in afp-test log")
   218         }
   219       case _ => log_file.err("cannot detect log header format")
   220     }
   221   }
   222 
   223   object Session_Status extends Enumeration
   224   {
   225     val UNKNOWN = Value("unknown")
   226     val FINISHED = Value("finished")
   227     val FAILED = Value("failed")
   228     val CANCELLED = Value("cancelled")
   229   }
   230 
   231 
   232   /* main log: produced by isatest, afp-test, jenkins etc. */
   233 
   234   sealed case class Info(
   235     settings: List[(String, String)],
   236     finished: Map[String, Timing],
   237     timing: Map[String, Timing],
   238     threads: Map[String, Int])
   239   {
   240     val sessions: Set[String] = finished.keySet ++ timing.keySet
   241 
   242     override def toString: String =
   243       sessions.toList.sorted.mkString("Build_Log.Info(", ", ", ")")
   244   }
   245 
   246   private val Session_Finished1 =
   247     new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time, (\d+):(\d+):(\d+) cpu time.*$""")
   248   private val Session_Finished2 =
   249     new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time.*$""")
   250   private val Session_Timing =
   251     new Regex("""^Timing (\S+) \((\d) threads, (\d+\.\d+)s elapsed time, (\d+\.\d+)s cpu time, (\d+\.\d+)s GC time.*$""")
   252 
   253   private def parse_info(log_file: Log_File): Info =
   254   {
   255     val settings = new mutable.ListBuffer[(String, String)]
   256     var finished = Map.empty[String, Timing]
   257     var timing = Map.empty[String, Timing]
   258     var threads = Map.empty[String, Int]
   259 
   260     for (line <- log_file.lines) {
   261       line match {
   262         case Session_Finished1(name,
   263             Value.Int(e1), Value.Int(e2), Value.Int(e3),
   264             Value.Int(c1), Value.Int(c2), Value.Int(c3)) =>
   265           val elapsed = Time.hms(e1, e2, e3)
   266           val cpu = Time.hms(c1, c2, c3)
   267           finished += (name -> Timing(elapsed, cpu, Time.zero))
   268         case Session_Finished2(name,
   269             Value.Int(e1), Value.Int(e2), Value.Int(e3)) =>
   270           val elapsed = Time.hms(e1, e2, e3)
   271           finished += (name -> Timing(elapsed, Time.zero, Time.zero))
   272         case Session_Timing(name,
   273             Value.Int(t), Value.Double(e), Value.Double(c), Value.Double(g)) =>
   274           val elapsed = Time.seconds(e)
   275           val cpu = Time.seconds(c)
   276           val gc = Time.seconds(g)
   277           timing += (name -> Timing(elapsed, cpu, gc))
   278           threads += (name -> t)
   279         case Settings.Entry(a, b) if Settings.all_settings.contains(a) =>
   280           settings += (a -> b)
   281         case _ =>
   282       }
   283     }
   284 
   285     Info(settings.toList, finished, timing, threads)
   286   }
   287 }