src/Pure/Tools/build_stats.scala
author wenzelm
Sun Aug 14 23:35:16 2016 +0200 (2016-08-14)
changeset 63703 ec095a532a2b
parent 63702 fed1d4dab990
child 63706 76c2f833abf4
permissions -rw-r--r--
provide index.html;
tuned;
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@63700
    82
wenzelm@63703
    83
  def present_job(job: String, dir: Path,
wenzelm@63700
    84
    history_length: Int = default_history_length,
wenzelm@63700
    85
    size: (Int, Int) = default_size,
wenzelm@63700
    86
    only_sessions: Set[String] = default_only_sessions,
wenzelm@63703
    87
    elapsed_threshold: Time = default_elapsed_threshold): 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@63688
   118
              val finished = stats.finished(session)
wenzelm@63688
   119
              t.toString + " " + finished.cpu.minutes + " " + finished.elapsed.minutes
wenzelm@63688
   120
            }
wenzelm@63688
   121
          File.write(data_file, cat_lines(data))
wenzelm@63688
   122
wenzelm@63700
   123
          val data_file_name = quote(File.standard_path(data_file.getAbsolutePath))
wenzelm@63688
   124
          File.write(plot_file, """
wenzelm@63700
   125
set terminal png size """ + size._1 + "," + size._2 + """
wenzelm@63688
   126
set output """ + quote(File.standard_path(dir + Path.basic(session + ".png"))) + """
wenzelm@63688
   127
set xdata time
wenzelm@63688
   128
set timefmt "%s"
wenzelm@63688
   129
set format x "%d-%b"
wenzelm@63701
   130
set xlabel """ + quote(session) + """ noenhanced
wenzelm@63688
   131
set key left top
wenzelm@63688
   132
plot [] [0:] """ +
wenzelm@63700
   133
  data_file_name + """ using 1:2 smooth sbezier title "interpolated cpu time",""" +
wenzelm@63700
   134
  data_file_name + """ using 1:2 smooth csplines title "cpu time", """ +
wenzelm@63700
   135
  data_file_name + """ using 1:3 smooth sbezier title "interpolated elapsed time",""" +
wenzelm@63700
   136
  data_file_name + """ using 1:3 smooth csplines title "elapsed time"
wenzelm@63688
   137
""")
wenzelm@63688
   138
          val result = Isabelle_System.bash("\"$ISABELLE_GNUPLOT\" " + File.bash_path(plot_file))
wenzelm@63688
   139
          if (result.rc != 0) {
wenzelm@63688
   140
            Output.error_message("Session " + session + ": gnuplot error")
wenzelm@63688
   141
            result.print
wenzelm@63688
   142
          }
wenzelm@63688
   143
        }
wenzelm@63688
   144
      }
wenzelm@63688
   145
    }
wenzelm@63703
   146
wenzelm@63703
   147
    sessions.toList.sorted
wenzelm@63688
   148
  }
wenzelm@63688
   149
wenzelm@63688
   150
wenzelm@63688
   151
  /* Isabelle tool wrapper */
wenzelm@63688
   152
wenzelm@63703
   153
  private val html_header = """<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
wenzelm@63703
   154
<html>
wenzelm@63703
   155
<head><title>Performance statistics from session build output</title></head>
wenzelm@63703
   156
<body>
wenzelm@63703
   157
"""
wenzelm@63703
   158
  private val html_footer = """
wenzelm@63703
   159
</body>
wenzelm@63703
   160
</html>
wenzelm@63703
   161
"""
wenzelm@63703
   162
wenzelm@63688
   163
  val isabelle_tool =
wenzelm@63688
   164
    Isabelle_Tool("build_stats", "present statistics from session build output", args =>
wenzelm@63688
   165
    {
wenzelm@63703
   166
      var target_dir = Path.explode("stats")
wenzelm@63700
   167
      var only_sessions = default_only_sessions
wenzelm@63700
   168
      var elapsed_threshold = default_elapsed_threshold
wenzelm@63700
   169
      var history_length = default_history_length
wenzelm@63700
   170
      var size = default_size
wenzelm@63688
   171
wenzelm@63688
   172
      val getopts = Getopts("""
wenzelm@63700
   173
Usage: isabelle build_stats [OPTIONS] [JOBS ...]
wenzelm@63688
   174
wenzelm@63688
   175
  Options are:
wenzelm@63688
   176
    -D DIR       target directory (default "stats")
wenzelm@63700
   177
    -S SESSIONS  only given SESSIONS (comma separated)
wenzelm@63700
   178
    -T THRESHOLD only sessions with elapsed time >= THRESHOLD (minutes)
wenzelm@63688
   179
    -l LENGTH    length of history (default 100)
wenzelm@63700
   180
    -s WxH       size of PNG image (default 800x600)
wenzelm@63688
   181
wenzelm@63700
   182
  Present statistics from session build output of the given JOBS, from Jenkins
wenzelm@63700
   183
  continuous build service specified as URL via ISABELLE_JENKINS_ROOT.
wenzelm@63688
   184
""",
wenzelm@63688
   185
        "D:" -> (arg => target_dir = Path.explode(arg)),
wenzelm@63700
   186
        "S:" -> (arg => only_sessions = space_explode(',', arg).toSet),
wenzelm@63700
   187
        "T:" -> (arg => elapsed_threshold = Time.minutes(Properties.Value.Double.parse(arg))),
wenzelm@63700
   188
        "l:" -> (arg => history_length = Properties.Value.Int.parse(arg)),
wenzelm@63700
   189
        "s:" -> (arg =>
wenzelm@63700
   190
          space_explode('x', arg).map(Properties.Value.Int.parse(_)) match {
wenzelm@63700
   191
            case List(w, h) if w > 0 && h > 0 => size = (w, h)
wenzelm@63700
   192
            case _ => error("Error bad PNG image size: " + quote(arg))
wenzelm@63700
   193
          }))
wenzelm@63688
   194
wenzelm@63700
   195
      val jobs = getopts(args)
wenzelm@63688
   196
      val all_jobs = CI_API.build_jobs()
wenzelm@63700
   197
      val bad_jobs = jobs.filterNot(all_jobs.contains(_)).sorted
wenzelm@63700
   198
wenzelm@63700
   199
      if (jobs.isEmpty)
wenzelm@63700
   200
        error("No build jobs given. Available jobs: " + commas(all_jobs))
wenzelm@63700
   201
wenzelm@63700
   202
      if (bad_jobs.nonEmpty)
wenzelm@63700
   203
        error("Unknown build jobs: " + commas(bad_jobs) +
wenzelm@63700
   204
          "\nAvailable jobs: " + commas(all_jobs.sorted))
wenzelm@63688
   205
wenzelm@63688
   206
      for (job <- jobs) {
wenzelm@63703
   207
        val dir = target_dir + Path.basic(job)
wenzelm@63703
   208
        Output.writeln(dir.implode)
wenzelm@63703
   209
        val sessions = present_job(job, dir, history_length, size, only_sessions, elapsed_threshold)
wenzelm@63703
   210
        File.write(dir + Path.basic("index.html"),
wenzelm@63703
   211
          html_header + "\n<h1>" + HTML.output(job) + "</h1>\n" +
wenzelm@63703
   212
          cat_lines(
wenzelm@63703
   213
            sessions.map(session =>
wenzelm@63703
   214
              """<br/><img src=""" + quote(HTML.output(session + ".png")) + """><br/>""")) +
wenzelm@63703
   215
          "\n" + html_footer)
wenzelm@63688
   216
      }
wenzelm@63703
   217
wenzelm@63703
   218
      File.write(target_dir + Path.basic("index.html"),
wenzelm@63703
   219
        html_header + "\n<ul>\n" +
wenzelm@63703
   220
        cat_lines(
wenzelm@63703
   221
          jobs.map(job => """<li> <a href=""" + quote(HTML.output(job + "/index.html")) + """>""" +
wenzelm@63703
   222
            HTML.output(job) + """</a> </li>""")) +
wenzelm@63703
   223
        "\n</ul>\n" + html_footer)
wenzelm@63703
   224
  })
wenzelm@63686
   225
}
wenzelm@63686
   226
wenzelm@63686
   227
sealed case class Build_Stats(
wenzelm@63686
   228
  ml_options: List[(String, String)],
wenzelm@63686
   229
  finished: Map[String, Timing],
wenzelm@63686
   230
  timing: Map[String, Timing],
wenzelm@63686
   231
  threads: Map[String, Int])
wenzelm@63686
   232
{
wenzelm@63686
   233
  val sessions: Set[String] = finished.keySet ++ timing.keySet
wenzelm@63686
   234
wenzelm@63686
   235
  override def toString: String =
wenzelm@63686
   236
    sessions.toList.sorted.mkString("Build_Stats(", ", ", ")")
wenzelm@63686
   237
}