src/Pure/Tools/build_stats.scala
author wenzelm
Tue Aug 16 15:55:11 2016 +0200 (2016-08-16)
changeset 63707 b7aab1a6cf0d
parent 63706 76c2f833abf4
child 63708 77323d963ca4
permissions -rw-r--r--
clarified presentation order, according to typical amounts;
wenzelm@63686
     1
/*  Title:      Pure/Tools/build_stats.scala
wenzelm@63686
     2
    Author:     Makarius
wenzelm@63686
     3
wenzelm@63686
     4
Statistics from session build output.
wenzelm@63686
     5
*/
wenzelm@63686
     6
wenzelm@63686
     7
package isabelle
wenzelm@63686
     8
wenzelm@63686
     9
wenzelm@63686
    10
import scala.collection.mutable
wenzelm@63686
    11
import scala.util.matching.Regex
wenzelm@63686
    12
wenzelm@63686
    13
wenzelm@63686
    14
object Build_Stats
wenzelm@63686
    15
{
wenzelm@63688
    16
  /* parse build output */
wenzelm@63688
    17
wenzelm@63702
    18
  private val Session_Finished1 =
wenzelm@63686
    19
    new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time, (\d+):(\d+):(\d+) cpu time.*$""")
wenzelm@63702
    20
  private val Session_Finished2 =
wenzelm@63702
    21
    new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time.*$""")
wenzelm@63686
    22
  private val Session_Timing =
wenzelm@63686
    23
    new Regex("""^Timing (\S+) \((\d) threads, (\d+\.\d+)s elapsed time, (\d+\.\d+)s cpu time, (\d+\.\d+)s GC time.*$""")
wenzelm@63686
    24
wenzelm@63686
    25
  private object ML_Option
wenzelm@63686
    26
  {
wenzelm@63686
    27
    def unapply(s: String): Option[(String, String)] =
wenzelm@63686
    28
      s.indexOf('=') match {
wenzelm@63686
    29
        case -1 => None
wenzelm@63686
    30
        case i =>
wenzelm@63686
    31
          val a = s.substring(0, i)
wenzelm@63686
    32
          Library.try_unquote(s.substring(i + 1)) match {
wenzelm@63686
    33
            case Some(b) if Build.ml_options.contains(a) => Some((a, b))
wenzelm@63686
    34
            case _ => None
wenzelm@63686
    35
          }
wenzelm@63686
    36
      }
wenzelm@63686
    37
  }
wenzelm@63686
    38
wenzelm@63686
    39
  def parse(text: String): Build_Stats =
wenzelm@63686
    40
  {
wenzelm@63686
    41
    import Properties.Value
wenzelm@63686
    42
wenzelm@63686
    43
    val ml_options = new mutable.ListBuffer[(String, String)]
wenzelm@63686
    44
    var finished = Map.empty[String, Timing]
wenzelm@63686
    45
    var timing = Map.empty[String, Timing]
wenzelm@63686
    46
    var threads = Map.empty[String, Int]
wenzelm@63686
    47
wenzelm@63686
    48
    for (line <- split_lines(text)) {
wenzelm@63686
    49
      line match {
wenzelm@63702
    50
        case Session_Finished1(name,
wenzelm@63686
    51
            Value.Int(e1), Value.Int(e2), Value.Int(e3),
wenzelm@63686
    52
            Value.Int(c1), Value.Int(c2), Value.Int(c3)) =>
wenzelm@63700
    53
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@63700
    54
          val cpu = Time.hms(c1, c2, c3)
wenzelm@63686
    55
          finished += (name -> Timing(elapsed, cpu, Time.zero))
wenzelm@63702
    56
        case Session_Finished2(name,
wenzelm@63702
    57
            Value.Int(e1), Value.Int(e2), Value.Int(e3)) =>
wenzelm@63702
    58
          val elapsed = Time.hms(e1, e2, e3)
wenzelm@63702
    59
          finished += (name -> Timing(elapsed, Time.zero, Time.zero))
wenzelm@63686
    60
        case Session_Timing(name,
wenzelm@63686
    61
            Value.Int(t), Value.Double(e), Value.Double(c), Value.Double(g)) =>
wenzelm@63686
    62
          val elapsed = Time.seconds(e)
wenzelm@63686
    63
          val cpu = Time.seconds(c)
wenzelm@63686
    64
          val gc = Time.seconds(g)
wenzelm@63686
    65
          timing += (name -> Timing(elapsed, cpu, gc))
wenzelm@63686
    66
          threads += (name -> t)
wenzelm@63686
    67
        case ML_Option(option) => ml_options += option
wenzelm@63686
    68
        case _ =>
wenzelm@63686
    69
      }
wenzelm@63686
    70
    }
wenzelm@63686
    71
wenzelm@63686
    72
    Build_Stats(ml_options.toList, finished, timing, threads)
wenzelm@63686
    73
  }
wenzelm@63688
    74
wenzelm@63688
    75
wenzelm@63688
    76
  /* presentation */
wenzelm@63688
    77
wenzelm@63700
    78
  private val default_history_length = 100
wenzelm@63700
    79
  private val default_size = (800, 600)
wenzelm@63700
    80
  private val default_only_sessions = Set.empty[String]
wenzelm@63700
    81
  private val default_elapsed_threshold = Time.zero
wenzelm@63706
    82
  private val default_ml_timing: Option[Boolean] = None
wenzelm@63700
    83
wenzelm@63703
    84
  def present_job(job: String, dir: Path,
wenzelm@63700
    85
    history_length: Int = default_history_length,
wenzelm@63700
    86
    size: (Int, Int) = default_size,
wenzelm@63700
    87
    only_sessions: Set[String] = default_only_sessions,
wenzelm@63706
    88
    elapsed_threshold: Time = default_elapsed_threshold,
wenzelm@63706
    89
    ml_timing: Option[Boolean] = default_ml_timing): List[String] =
wenzelm@63688
    90
  {
wenzelm@63688
    91
    val build_infos = CI_API.build_job_builds(job).sortBy(_.timestamp).reverse.take(history_length)
wenzelm@63688
    92
    if (build_infos.isEmpty) error("No build infos for job " + quote(job))
wenzelm@63688
    93
wenzelm@63688
    94
    val all_build_stats =
wenzelm@63688
    95
      Par_List.map((info: CI_API.Build_Info) =>
wenzelm@63688
    96
        (info.timestamp / 1000, parse(Url.read(info.output))), build_infos)
wenzelm@63688
    97
    val all_sessions =
wenzelm@63688
    98
      (Set.empty[String] /: all_build_stats)(
wenzelm@63688
    99
        { case (s, (_, stats)) => s ++ stats.sessions })
wenzelm@63688
   100
wenzelm@63700
   101
    def check_threshold(stats: Build_Stats, session: String): Boolean =
wenzelm@63700
   102
      stats.finished.get(session) match {
wenzelm@63700
   103
        case Some(t) => t.elapsed >= elapsed_threshold
wenzelm@63700
   104
        case None => false
wenzelm@63700
   105
      }
wenzelm@63700
   106
wenzelm@63703
   107
    val sessions =
wenzelm@63703
   108
      for {
wenzelm@63703
   109
        session <- (if (only_sessions.isEmpty) all_sessions else all_sessions & only_sessions)
wenzelm@63703
   110
        if all_build_stats.filter({ case (_, stats) => check_threshold(stats, session) }).length >= 3
wenzelm@63703
   111
      } yield session
wenzelm@63703
   112
wenzelm@63703
   113
    Isabelle_System.mkdirs(dir)
wenzelm@63703
   114
    for (session <- sessions) {
wenzelm@63688
   115
      Isabelle_System.with_tmp_file(session, "png") { data_file =>
wenzelm@63688
   116
        Isabelle_System.with_tmp_file(session, "gnuplot") { plot_file =>
wenzelm@63688
   117
          val data =
wenzelm@63688
   118
            for { (t, stats) <- all_build_stats if stats.finished.isDefinedAt(session) }
wenzelm@63688
   119
            yield {
wenzelm@63688
   120
              val finished = stats.finished(session)
wenzelm@63706
   121
              val timing = stats.timing(session)
wenzelm@63706
   122
              List(t.toString, finished.elapsed.minutes, finished.cpu.minutes,
wenzelm@63706
   123
                timing.elapsed.minutes, timing.cpu.minutes, timing.gc.minutes).mkString(" ")
wenzelm@63688
   124
            }
wenzelm@63688
   125
          File.write(data_file, cat_lines(data))
wenzelm@63688
   126
wenzelm@63706
   127
          val plots1 =
wenzelm@63706
   128
            List(
wenzelm@63707
   129
              """ using 1:3 smooth sbezier title "cpu time (smooth)" """,
wenzelm@63707
   130
              """ using 1:3 smooth csplines title "cpu time" """,
wenzelm@63706
   131
              """ using 1:2 smooth sbezier title "elapsed time (smooth)" """,
wenzelm@63707
   132
              """ using 1:2 smooth csplines title "elapsed time" """)
wenzelm@63706
   133
          val plots2 =
wenzelm@63706
   134
            List(
wenzelm@63707
   135
              """ using 1:5 smooth sbezier title "ML cpu time (smooth)" """,
wenzelm@63707
   136
              """ using 1:5 smooth csplines title "ML cpu time" """,
wenzelm@63706
   137
              """ using 1:4 smooth sbezier title "ML elapsed time (smooth)" """,
wenzelm@63706
   138
              """ using 1:4 smooth csplines title "ML elapsed time" """,
wenzelm@63706
   139
              """ using 1:6 smooth sbezier title "ML gc time (smooth)" """,
wenzelm@63706
   140
              """ using 1:6 smooth csplines title "ML gc time" """)
wenzelm@63706
   141
          val plots =
wenzelm@63706
   142
            ml_timing match {
wenzelm@63706
   143
              case None => plots1
wenzelm@63706
   144
              case Some(false) => plots1 ::: plots2
wenzelm@63706
   145
              case Some(true) => plots2
wenzelm@63706
   146
            }
wenzelm@63706
   147
wenzelm@63706
   148
          val data_file_name = File.standard_path(data_file.getAbsolutePath)
wenzelm@63688
   149
          File.write(plot_file, """
wenzelm@63700
   150
set terminal png size """ + size._1 + "," + size._2 + """
wenzelm@63688
   151
set output """ + quote(File.standard_path(dir + Path.basic(session + ".png"))) + """
wenzelm@63688
   152
set xdata time
wenzelm@63688
   153
set timefmt "%s"
wenzelm@63688
   154
set format x "%d-%b"
wenzelm@63701
   155
set xlabel """ + quote(session) + """ noenhanced
wenzelm@63688
   156
set key left top
wenzelm@63706
   157
plot [] [0:] """ + plots.map(s => quote(data_file_name) + " " + s).mkString(", ") + "\n")
wenzelm@63688
   158
          val result = Isabelle_System.bash("\"$ISABELLE_GNUPLOT\" " + File.bash_path(plot_file))
wenzelm@63688
   159
          if (result.rc != 0) {
wenzelm@63688
   160
            Output.error_message("Session " + session + ": gnuplot error")
wenzelm@63688
   161
            result.print
wenzelm@63688
   162
          }
wenzelm@63688
   163
        }
wenzelm@63688
   164
      }
wenzelm@63688
   165
    }
wenzelm@63703
   166
wenzelm@63703
   167
    sessions.toList.sorted
wenzelm@63688
   168
  }
wenzelm@63688
   169
wenzelm@63688
   170
wenzelm@63688
   171
  /* Isabelle tool wrapper */
wenzelm@63688
   172
wenzelm@63703
   173
  private val html_header = """<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
wenzelm@63703
   174
<html>
wenzelm@63703
   175
<head><title>Performance statistics from session build output</title></head>
wenzelm@63703
   176
<body>
wenzelm@63703
   177
"""
wenzelm@63703
   178
  private val html_footer = """
wenzelm@63703
   179
</body>
wenzelm@63703
   180
</html>
wenzelm@63703
   181
"""
wenzelm@63703
   182
wenzelm@63688
   183
  val isabelle_tool =
wenzelm@63688
   184
    Isabelle_Tool("build_stats", "present statistics from session build output", args =>
wenzelm@63688
   185
    {
wenzelm@63703
   186
      var target_dir = Path.explode("stats")
wenzelm@63706
   187
      var ml_timing = default_ml_timing
wenzelm@63700
   188
      var only_sessions = default_only_sessions
wenzelm@63700
   189
      var elapsed_threshold = default_elapsed_threshold
wenzelm@63700
   190
      var history_length = default_history_length
wenzelm@63700
   191
      var size = default_size
wenzelm@63688
   192
wenzelm@63688
   193
      val getopts = Getopts("""
wenzelm@63700
   194
Usage: isabelle build_stats [OPTIONS] [JOBS ...]
wenzelm@63688
   195
wenzelm@63688
   196
  Options are:
wenzelm@63688
   197
    -D DIR       target directory (default "stats")
wenzelm@63706
   198
    -M           only ML timing
wenzelm@63700
   199
    -S SESSIONS  only given SESSIONS (comma separated)
wenzelm@63700
   200
    -T THRESHOLD only sessions with elapsed time >= THRESHOLD (minutes)
wenzelm@63688
   201
    -l LENGTH    length of history (default 100)
wenzelm@63706
   202
    -m           include ML timing
wenzelm@63700
   203
    -s WxH       size of PNG image (default 800x600)
wenzelm@63688
   204
wenzelm@63700
   205
  Present statistics from session build output of the given JOBS, from Jenkins
wenzelm@63700
   206
  continuous build service specified as URL via ISABELLE_JENKINS_ROOT.
wenzelm@63688
   207
""",
wenzelm@63688
   208
        "D:" -> (arg => target_dir = Path.explode(arg)),
wenzelm@63706
   209
        "M" -> (_ => ml_timing = Some(true)),
wenzelm@63700
   210
        "S:" -> (arg => only_sessions = space_explode(',', arg).toSet),
wenzelm@63700
   211
        "T:" -> (arg => elapsed_threshold = Time.minutes(Properties.Value.Double.parse(arg))),
wenzelm@63700
   212
        "l:" -> (arg => history_length = Properties.Value.Int.parse(arg)),
wenzelm@63706
   213
        "m" -> (_ => ml_timing = Some(false)),
wenzelm@63700
   214
        "s:" -> (arg =>
wenzelm@63700
   215
          space_explode('x', arg).map(Properties.Value.Int.parse(_)) match {
wenzelm@63700
   216
            case List(w, h) if w > 0 && h > 0 => size = (w, h)
wenzelm@63700
   217
            case _ => error("Error bad PNG image size: " + quote(arg))
wenzelm@63700
   218
          }))
wenzelm@63688
   219
wenzelm@63700
   220
      val jobs = getopts(args)
wenzelm@63688
   221
      val all_jobs = CI_API.build_jobs()
wenzelm@63700
   222
      val bad_jobs = jobs.filterNot(all_jobs.contains(_)).sorted
wenzelm@63700
   223
wenzelm@63700
   224
      if (jobs.isEmpty)
wenzelm@63700
   225
        error("No build jobs given. Available jobs: " + commas(all_jobs))
wenzelm@63700
   226
wenzelm@63700
   227
      if (bad_jobs.nonEmpty)
wenzelm@63700
   228
        error("Unknown build jobs: " + commas(bad_jobs) +
wenzelm@63700
   229
          "\nAvailable jobs: " + commas(all_jobs.sorted))
wenzelm@63688
   230
wenzelm@63688
   231
      for (job <- jobs) {
wenzelm@63703
   232
        val dir = target_dir + Path.basic(job)
wenzelm@63703
   233
        Output.writeln(dir.implode)
wenzelm@63706
   234
        val sessions =
wenzelm@63706
   235
          present_job(job, dir, history_length, size, only_sessions, elapsed_threshold, ml_timing)
wenzelm@63703
   236
        File.write(dir + Path.basic("index.html"),
wenzelm@63703
   237
          html_header + "\n<h1>" + HTML.output(job) + "</h1>\n" +
wenzelm@63703
   238
          cat_lines(
wenzelm@63703
   239
            sessions.map(session =>
wenzelm@63703
   240
              """<br/><img src=""" + quote(HTML.output(session + ".png")) + """><br/>""")) +
wenzelm@63703
   241
          "\n" + html_footer)
wenzelm@63688
   242
      }
wenzelm@63703
   243
wenzelm@63703
   244
      File.write(target_dir + Path.basic("index.html"),
wenzelm@63703
   245
        html_header + "\n<ul>\n" +
wenzelm@63703
   246
        cat_lines(
wenzelm@63703
   247
          jobs.map(job => """<li> <a href=""" + quote(HTML.output(job + "/index.html")) + """>""" +
wenzelm@63703
   248
            HTML.output(job) + """</a> </li>""")) +
wenzelm@63703
   249
        "\n</ul>\n" + html_footer)
wenzelm@63703
   250
  })
wenzelm@63686
   251
}
wenzelm@63686
   252
wenzelm@63686
   253
sealed case class Build_Stats(
wenzelm@63686
   254
  ml_options: List[(String, String)],
wenzelm@63686
   255
  finished: Map[String, Timing],
wenzelm@63686
   256
  timing: Map[String, Timing],
wenzelm@63686
   257
  threads: Map[String, Int])
wenzelm@63686
   258
{
wenzelm@63686
   259
  val sessions: Set[String] = finished.keySet ++ timing.keySet
wenzelm@63686
   260
wenzelm@63686
   261
  override def toString: String =
wenzelm@63686
   262
    sessions.toList.sorted.mkString("Build_Stats(", ", ", ")")
wenzelm@63686
   263
}