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