src/Pure/Tools/build_log.scala
author wenzelm
Fri Oct 07 18:07:10 2016 +0200 (2016-10-07)
changeset 64088 210aabe359ab
parent 64087 a77c57235bae
child 64089 10d719dbb3ee
permissions -rw-r--r--
more permissive for old logs;
     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(path: Path): Log_File =
    55     {
    56       val (path_name, ext) = path.expand.split_ext
    57       val text =
    58         if (ext == "gz") File.read_gzip(path)
    59         else if (ext == "xz") File.read_xz(path)
    60         else File.read(path)
    61       apply(path_name.base.implode, text)
    62     }
    63 
    64     def apply(name: String, lines: List[String]): Log_File =
    65       new Log_File(name, lines)
    66 
    67     def apply(name: String, text: String): Log_File =
    68       Log_File(name, Library.trim_split_lines(text))
    69   }
    70 
    71   class Log_File private(val name: String, val lines: List[String])
    72   {
    73     log_file =>
    74 
    75     override def toString: String = name
    76 
    77     def text: String = cat_lines(lines)
    78 
    79     def err(msg: String): Nothing =
    80       error("Error in log file " + quote(name) + ": " + msg)
    81 
    82 
    83     /* inlined content */
    84 
    85     def find[A](f: String => Option[A]): Option[A] =
    86       lines.iterator.map(f).find(_.isDefined).map(_.get)
    87 
    88     def find_match(regex: Regex): Option[String] =
    89       lines.iterator.map(regex.unapplySeq(_)).find(res => res.isDefined && res.get.length == 1).
    90         map(res => res.get.head)
    91 
    92 
    93     /* settings */
    94 
    95     def get_setting(a: String): Settings.Entry =
    96       Settings.Entry.unapply(
    97         lines.find(_.startsWith(a + "=")) getOrElse err("missing " + a)).get
    98 
    99     def get_settings(as: List[String]): Settings.T = as.map(get_setting(_))
   100 
   101 
   102     /* properties (YXML) */
   103 
   104     val xml_cache = new XML.Cache()
   105 
   106     def parse_props(text: String): Properties.T =
   107       xml_cache.props(XML.Decode.properties(YXML.parse_body(text)))
   108 
   109     def filter_props(prefix: String): List[Properties.T] =
   110       for (line <- lines; s <- Library.try_unprefix(prefix, line)) yield parse_props(s)
   111 
   112     def find_line(prefix: String): Option[String] =
   113       find(Library.try_unprefix(prefix, _))
   114 
   115     def find_props(prefix: String): Option[Properties.T] =
   116       find_line(prefix).map(parse_props(_))
   117 
   118 
   119     /* parse various formats */
   120 
   121     def parse_session_info(
   122         default_name: String = "",
   123         command_timings: Boolean = false,
   124         ml_statistics: Boolean = false,
   125         task_statistics: Boolean = false): Session_Info =
   126       Build_Log.parse_session_info(
   127         log_file, default_name, command_timings, ml_statistics, task_statistics)
   128 
   129     def parse_header(): Header = Build_Log.parse_header(log_file)
   130 
   131     def parse_build_info(): Build_Info = Build_Log.parse_build_info(log_file)
   132   }
   133 
   134 
   135   /* session log: produced by "isabelle build" */
   136 
   137   sealed case class Session_Info(
   138     session_name: String,
   139     session_timing: Properties.T,
   140     command_timings: List[Properties.T],
   141     ml_statistics: List[Properties.T],
   142     task_statistics: List[Properties.T])
   143 
   144   private def parse_session_info(
   145     log_file: Log_File,
   146     default_name: String,
   147     command_timings: Boolean,
   148     ml_statistics: Boolean,
   149     task_statistics: Boolean): Session_Info =
   150   {
   151     val xml_cache = new XML.Cache()
   152 
   153     val session_name =
   154       log_file.find_line("\fSession.name = ") match {
   155         case None => default_name
   156         case Some(name) if default_name == "" || default_name == name => name
   157         case Some(name) => log_file.err("log from different session " + quote(name))
   158       }
   159     val session_timing = log_file.find_props("\fTiming = ") getOrElse Nil
   160     val command_timings_ =
   161       if (command_timings) log_file.filter_props("\fcommand_timing = ") else Nil
   162     val ml_statistics_ =
   163       if (ml_statistics) log_file.filter_props("\fML_statistics = ") else Nil
   164     val task_statistics_ =
   165       if (task_statistics) log_file.filter_props("\ftask_statistics = ") else Nil
   166 
   167     Session_Info(session_name, session_timing, command_timings_, ml_statistics_, task_statistics_)
   168   }
   169 
   170 
   171   /* header and meta data */
   172 
   173   object Header_Kind extends Enumeration
   174   {
   175     val ISATEST = Value("isatest")
   176     val AFP_TEST = Value("afp-test")
   177     val JENKINS = Value("jenkins")
   178   }
   179 
   180   sealed case class Header(
   181     kind: Header_Kind.Value, props: Properties.T, settings: List[(String, String)])
   182 
   183   object Field
   184   {
   185     val build_host = "build_host"
   186     val build_start = "build_start"
   187     val build_end = "build_end"
   188     val isabelle_version = "isabelle_version"
   189     val afp_version = "afp_version"
   190   }
   191 
   192   object AFP
   193   {
   194     val Date_Format =
   195       Date.Format.make_patterns(List("EEE MMM d HH:mm:ss VV yyyy", "EEE MMM d HH:mm:ss O yyyy"),
   196         // workaround for jdk-8u102
   197         s => Word.implode(Word.explode(s).map({ case "CEST" => "GMT+2" case a => a })))
   198 
   199     val Test_Start = new Regex("""^Start test (?:for \S+)? at (.+), (\S+)$""")
   200     val Test_End = new Regex("""^End test on (.+), \S+, elapsed time:.*$""")
   201     val Isabelle_Version = new Regex("""^Isabelle version: .* -- hg id (\S+)$""")
   202     val AFP_Version = new Regex("""^AFP version: .* -- hg id (\S+)$""")
   203   }
   204 
   205   private def parse_header(log_file: Log_File): Header =
   206   {
   207     log_file.lines match {
   208       case AFP.Test_Start(start, hostname) :: _ =>
   209         (start, log_file.lines.last) match {
   210           case (AFP.Date_Format(start_date), AFP.Test_End(AFP.Date_Format(end_date))) =>
   211             val isabelle_version =
   212               log_file.find_match(AFP.Isabelle_Version).map((Field.isabelle_version, _))
   213             val afp_version =
   214               log_file.find_match(AFP.AFP_Version).map((Field.afp_version, _))
   215 
   216             Header(Header_Kind.AFP_TEST,
   217               List(
   218                 Field.build_host -> hostname,
   219                 Field.build_start -> start_date.toString,
   220                 Field.build_end -> end_date.toString) :::
   221               isabelle_version.toList :::
   222               afp_version.toList,
   223               log_file.get_settings(Settings.all_settings))
   224 
   225           case _ => log_file.err("cannot detect start/end date in afp-test log")
   226         }
   227       case _ => log_file.err("cannot detect log header format")
   228     }
   229   }
   230 
   231 
   232   /* build info: produced by isabelle build */
   233 
   234   object Session_Status extends Enumeration
   235   {
   236     val EXISTING = Value("existing")
   237     val FINISHED = Value("finished")
   238     val FAILED = Value("failed")
   239     val CANCELLED = Value("cancelled")
   240   }
   241 
   242   sealed case class Session_Entry(
   243     chapter: String,
   244     groups: List[String],
   245     threads: Option[Int],
   246     timing: Option[Timing],
   247     ml_timing: Option[Timing],
   248     status: Session_Status.Value)
   249   {
   250     def finished: Boolean = status == Session_Status.FINISHED
   251   }
   252 
   253   sealed case class Build_Info(sessions: Map[String, Session_Entry])
   254   {
   255     def session(name: String): Session_Entry = sessions(name)
   256     def get_session(name: String): Option[Session_Entry] = sessions.get(name)
   257 
   258     def finished(name: String): Boolean =
   259       get_session(name) match {
   260         case Some(entry) => entry.finished
   261         case None => false
   262       }
   263 
   264     def timing(name: String): Timing =
   265       (for (entry <- get_session(name); t <- entry.timing) yield t) getOrElse Timing.zero
   266 
   267     def ml_timing(name: String): Timing =
   268       (for (entry <- get_session(name); t <- entry.ml_timing) yield t) getOrElse Timing.zero
   269   }
   270 
   271   private def parse_build_info(log_file: Log_File): Build_Info =
   272   {
   273     object Chapter_Name
   274     {
   275       def unapply(s: String): Some[(String, String)] =
   276         space_explode('/', s) match {
   277           case List(chapter, name) => Some((chapter, name))
   278           case _ => Some(("", s))
   279         }
   280     }
   281 
   282     val Session_No_Groups = new Regex("""^Session (\S+)$""")
   283     val Session_Groups = new Regex("""^Session (\S+) \((.*)\)$""")
   284     val Session_Finished1 =
   285       new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time, (\d+):(\d+):(\d+) cpu time.*$""")
   286     val Session_Finished2 =
   287       new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time.*$""")
   288     val Session_Timing =
   289       new Regex("""^Timing (\S+) \((\d) threads, (\d+\.\d+)s elapsed time, (\d+\.\d+)s cpu time, (\d+\.\d+)s GC time.*$""")
   290     val Session_Started = new Regex("""^(?:Running|Building) (\S+) \.\.\.$""")
   291     val Session_Failed = new Regex("""^(\S+) FAILED""")
   292     val Session_Cancelled = new Regex("""^(\S+) CANCELLED""")
   293 
   294     var chapter = Map.empty[String, String]
   295     var groups = Map.empty[String, List[String]]
   296     var threads = Map.empty[String, Int]
   297     var timing = Map.empty[String, Timing]
   298     var ml_timing = Map.empty[String, Timing]
   299     var started = Set.empty[String]
   300     var failed = Set.empty[String]
   301     var cancelled = Set.empty[String]
   302     def all_sessions: Set[String] =
   303       chapter.keySet ++ groups.keySet ++ threads.keySet ++
   304       timing.keySet ++ ml_timing.keySet ++ failed ++ cancelled ++ started
   305 
   306 
   307     for (line <- log_file.lines) {
   308       line match {
   309         case Session_No_Groups(Chapter_Name(chapt, name)) =>
   310           chapter += (name -> chapt)
   311           groups += (name -> Nil)
   312         case Session_Groups(Chapter_Name(chapt, name), grps) =>
   313           chapter += (name -> chapt)
   314           groups += (name -> Word.explode(grps))
   315         case Session_Started(name) =>
   316           started += name
   317         case Session_Finished1(name,
   318             Value.Int(e1), Value.Int(e2), Value.Int(e3),
   319             Value.Int(c1), Value.Int(c2), Value.Int(c3)) =>
   320           val elapsed = Time.hms(e1, e2, e3)
   321           val cpu = Time.hms(c1, c2, c3)
   322           timing += (name -> Timing(elapsed, cpu, Time.zero))
   323         case Session_Finished2(name,
   324             Value.Int(e1), Value.Int(e2), Value.Int(e3)) =>
   325           val elapsed = Time.hms(e1, e2, e3)
   326           timing += (name -> Timing(elapsed, Time.zero, Time.zero))
   327         case Session_Timing(name,
   328             Value.Int(t), Value.Double(e), Value.Double(c), Value.Double(g)) =>
   329           val elapsed = Time.seconds(e)
   330           val cpu = Time.seconds(c)
   331           val gc = Time.seconds(g)
   332           ml_timing += (name -> Timing(elapsed, cpu, gc))
   333           threads += (name -> t)
   334         case _ =>
   335       }
   336     }
   337 
   338     val sessions =
   339       Map(
   340         (for (name <- all_sessions.toList) yield {
   341           val status =
   342             if (failed(name)) Session_Status.FAILED
   343             else if (cancelled(name)) Session_Status.CANCELLED
   344             else if (timing.isDefinedAt(name) || ml_timing.isDefinedAt(name))
   345               Session_Status.FINISHED
   346             else if (started(name)) Session_Status.FAILED
   347             else Session_Status.EXISTING
   348           val entry =
   349             Session_Entry(
   350               chapter.getOrElse(name, ""),
   351               groups.getOrElse(name, Nil),
   352               threads.get(name),
   353               timing.get(name),
   354               ml_timing.get(name),
   355               status)
   356           (name -> entry)
   357         }):_*)
   358     Build_Info(sessions)
   359   }
   360 }