src/Pure/Tools/build_log.scala
author wenzelm
Wed Oct 05 14:15:54 2016 +0200 (2016-10-05)
changeset 64054 1fc9ab31720d
parent 64053 7ece2e14fd6c
child 64061 1bbea2b55d22
permissions -rw-r--r--
clarified modules;
wenzelm@64045
     1
/*  Title:      Pure/Tools/build_log.scala
wenzelm@64045
     2
    Author:     Makarius
wenzelm@64045
     3
wenzelm@64045
     4
Build log parsing for historic versions, back to "build_history_base".
wenzelm@64045
     5
*/
wenzelm@64045
     6
wenzelm@64045
     7
package isabelle
wenzelm@64045
     8
wenzelm@64045
     9
wenzelm@64054
    10
import scala.collection.mutable
wenzelm@64054
    11
import scala.util.matching.Regex
wenzelm@64054
    12
wenzelm@64054
    13
wenzelm@64045
    14
object Build_Log
wenzelm@64045
    15
{
wenzelm@64045
    16
  /* inlined properties (YXML) */
wenzelm@64045
    17
wenzelm@64045
    18
  object Props
wenzelm@64045
    19
  {
wenzelm@64053
    20
    def print(props: Properties.T): String = YXML.string_of_body(XML.Encode.properties(props))
wenzelm@64045
    21
    def parse(text: String): Properties.T = XML.Decode.properties(YXML.parse_body(text))
wenzelm@64045
    22
wenzelm@64045
    23
    def parse_lines(prefix: String, lines: List[String]): List[Properties.T] =
wenzelm@64045
    24
      for (line <- lines; s <- Library.try_unprefix(prefix, line)) yield parse(s)
wenzelm@64045
    25
wenzelm@64045
    26
    def find_parse_line(prefix: String, lines: List[String]): Option[Properties.T] =
wenzelm@64045
    27
      lines.find(_.startsWith(prefix)).map(line => parse(line.substring(prefix.length)))
wenzelm@64045
    28
  }
wenzelm@64045
    29
wenzelm@64045
    30
wenzelm@64045
    31
  /* session log: produced by "isabelle build" */
wenzelm@64045
    32
wenzelm@64045
    33
  sealed case class Session_Info(
wenzelm@64045
    34
    session_name: String,
wenzelm@64045
    35
    session_timing: Properties.T,
wenzelm@64045
    36
    command_timings: List[Properties.T],
wenzelm@64045
    37
    ml_statistics: List[Properties.T],
wenzelm@64045
    38
    task_statistics: List[Properties.T])
wenzelm@64045
    39
wenzelm@64045
    40
  val SESSION_NAME = "\fSession.name = "
wenzelm@64045
    41
wenzelm@64045
    42
  def parse_session_info(name0: String, lines: List[String], full: Boolean): Session_Info =
wenzelm@64045
    43
  {
wenzelm@64045
    44
    val xml_cache = new XML.Cache()
wenzelm@64045
    45
    def parse_lines(prfx: String): List[Properties.T] =
wenzelm@64045
    46
      Props.parse_lines(prfx, lines).map(xml_cache.props(_))
wenzelm@64045
    47
wenzelm@64045
    48
    val session_name =
wenzelm@64045
    49
      lines.find(_.startsWith(SESSION_NAME)).map(_.substring(SESSION_NAME.length)) match {
wenzelm@64045
    50
        case None => name0
wenzelm@64045
    51
        case Some(name) if name0 == "" || name0 == name => name
wenzelm@64045
    52
        case Some(name) =>
wenzelm@64045
    53
          error("Session log for " + quote(name0) + " is actually from " + quote(name))
wenzelm@64045
    54
      }
wenzelm@64045
    55
    val session_timing = Props.find_parse_line("\fTiming = ", lines) getOrElse Nil
wenzelm@64045
    56
    val command_timings = parse_lines("\fcommand_timing = ")
wenzelm@64045
    57
    val ml_statistics = if (full) parse_lines("\fML_statistics = ") else Nil
wenzelm@64045
    58
    val task_statistics = if (full) parse_lines("\ftask_statistics = ") else Nil
wenzelm@64045
    59
wenzelm@64045
    60
    Session_Info(session_name, session_timing, command_timings, ml_statistics, task_statistics)
wenzelm@64045
    61
  }
wenzelm@64054
    62
wenzelm@64054
    63
wenzelm@64054
    64
  /* main log: produced by isatest, afp-test, jenkins etc. */
wenzelm@64054
    65
wenzelm@64054
    66
  sealed case class Info(
wenzelm@64054
    67
    ml_options: List[(String, String)],
wenzelm@64054
    68
    finished: Map[String, Timing],
wenzelm@64054
    69
    timing: Map[String, Timing],
wenzelm@64054
    70
    threads: Map[String, Int])
wenzelm@64054
    71
  {
wenzelm@64054
    72
    val sessions: Set[String] = finished.keySet ++ timing.keySet
wenzelm@64054
    73
wenzelm@64054
    74
    override def toString: String =
wenzelm@64054
    75
      sessions.toList.sorted.mkString("Build_Log.Info(", ", ", ")")
wenzelm@64054
    76
  }
wenzelm@64054
    77
wenzelm@64054
    78
  private val Session_Finished1 =
wenzelm@64054
    79
    new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time, (\d+):(\d+):(\d+) cpu time.*$""")
wenzelm@64054
    80
  private val Session_Finished2 =
wenzelm@64054
    81
    new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time.*$""")
wenzelm@64054
    82
  private val Session_Timing =
wenzelm@64054
    83
    new Regex("""^Timing (\S+) \((\d) threads, (\d+\.\d+)s elapsed time, (\d+\.\d+)s cpu time, (\d+\.\d+)s GC time.*$""")
wenzelm@64054
    84
wenzelm@64054
    85
  private object ML_Option
wenzelm@64054
    86
  {
wenzelm@64054
    87
    def unapply(s: String): Option[(String, String)] =
wenzelm@64054
    88
      s.indexOf('=') match {
wenzelm@64054
    89
        case -1 => None
wenzelm@64054
    90
        case i =>
wenzelm@64054
    91
          val a = s.substring(0, i)
wenzelm@64054
    92
          Library.try_unquote(s.substring(i + 1)) match {
wenzelm@64054
    93
            case Some(b) if Build.ml_options.contains(a) => Some((a, b))
wenzelm@64054
    94
            case _ => None
wenzelm@64054
    95
          }
wenzelm@64054
    96
      }
wenzelm@64054
    97
  }
wenzelm@64054
    98
wenzelm@64054
    99
  def parse_info(text: String): Info =
wenzelm@64054
   100
  {
wenzelm@64054
   101
    val ml_options = new mutable.ListBuffer[(String, String)]
wenzelm@64054
   102
    var finished = Map.empty[String, Timing]
wenzelm@64054
   103
    var timing = Map.empty[String, Timing]
wenzelm@64054
   104
    var threads = Map.empty[String, Int]
wenzelm@64054
   105
wenzelm@64054
   106
    for (line <- split_lines(text)) {
wenzelm@64054
   107
      line match {
wenzelm@64054
   108
        case Session_Finished1(name,
wenzelm@64054
   109
            Value.Int(e1), Value.Int(e2), Value.Int(e3),
wenzelm@64054
   110
            Value.Int(c1), Value.Int(c2), Value.Int(c3)) =>
wenzelm@64054
   111
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@64054
   112
          val cpu = Time.hms(c1, c2, c3)
wenzelm@64054
   113
          finished += (name -> Timing(elapsed, cpu, Time.zero))
wenzelm@64054
   114
        case Session_Finished2(name,
wenzelm@64054
   115
            Value.Int(e1), Value.Int(e2), Value.Int(e3)) =>
wenzelm@64054
   116
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@64054
   117
          finished += (name -> Timing(elapsed, Time.zero, Time.zero))
wenzelm@64054
   118
        case Session_Timing(name,
wenzelm@64054
   119
            Value.Int(t), Value.Double(e), Value.Double(c), Value.Double(g)) =>
wenzelm@64054
   120
          val elapsed = Time.seconds(e)
wenzelm@64054
   121
          val cpu = Time.seconds(c)
wenzelm@64054
   122
          val gc = Time.seconds(g)
wenzelm@64054
   123
          timing += (name -> Timing(elapsed, cpu, gc))
wenzelm@64054
   124
          threads += (name -> t)
wenzelm@64054
   125
        case ML_Option(a, b) => ml_options += (a -> b)
wenzelm@64054
   126
        case _ =>
wenzelm@64054
   127
      }
wenzelm@64054
   128
    }
wenzelm@64054
   129
wenzelm@64054
   130
    Info(ml_options.toList, finished, timing, threads)
wenzelm@64054
   131
  }
wenzelm@64045
   132
}