src/Pure/Tools/build_stats.scala
author wenzelm
Wed Oct 05 14:15:54 2016 +0200 (2016-10-05)
changeset 64054 1fc9ab31720d
parent 63984 6ba87450894d
child 64062 a7352cbde7d7
permissions -rw-r--r--
clarified modules;
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
object Build_Stats
wenzelm@63686
    11
{
wenzelm@63688
    12
  /* presentation */
wenzelm@63688
    13
wenzelm@63700
    14
  private val default_history_length = 100
wenzelm@63700
    15
  private val default_size = (800, 600)
wenzelm@63700
    16
  private val default_only_sessions = Set.empty[String]
wenzelm@63700
    17
  private val default_elapsed_threshold = Time.zero
wenzelm@63706
    18
  private val default_ml_timing: Option[Boolean] = None
wenzelm@63700
    19
wenzelm@63703
    20
  def present_job(job: String, dir: Path,
wenzelm@63700
    21
    history_length: Int = default_history_length,
wenzelm@63700
    22
    size: (Int, Int) = default_size,
wenzelm@63700
    23
    only_sessions: Set[String] = default_only_sessions,
wenzelm@63706
    24
    elapsed_threshold: Time = default_elapsed_threshold,
wenzelm@63706
    25
    ml_timing: Option[Boolean] = default_ml_timing): List[String] =
wenzelm@63688
    26
  {
wenzelm@64054
    27
    val job_infos = CI_API.build_job_builds(job).sortBy(_.timestamp).reverse.take(history_length)
wenzelm@64054
    28
    if (job_infos.isEmpty) error("No build infos for job " + quote(job))
wenzelm@63688
    29
wenzelm@64054
    30
    val all_infos =
wenzelm@64054
    31
      Par_List.map((job_info: CI_API.Job_Info) =>
wenzelm@64054
    32
        (job_info.timestamp / 1000, Build_Log.parse_info(Url.read(job_info.output))), job_infos)
wenzelm@63688
    33
    val all_sessions =
wenzelm@64054
    34
      (Set.empty[String] /: all_infos)(
wenzelm@64054
    35
        { case (s, (_, info)) => s ++ info.sessions })
wenzelm@63688
    36
wenzelm@64054
    37
    def check_threshold(info: Build_Log.Info, session: String): Boolean =
wenzelm@64054
    38
      info.finished.get(session) match {
wenzelm@63700
    39
        case Some(t) => t.elapsed >= elapsed_threshold
wenzelm@63700
    40
        case None => false
wenzelm@63700
    41
      }
wenzelm@63700
    42
wenzelm@63703
    43
    val sessions =
wenzelm@63703
    44
      for {
wenzelm@63703
    45
        session <- (if (only_sessions.isEmpty) all_sessions else all_sessions & only_sessions)
wenzelm@64054
    46
        if all_infos.filter({ case (_, info) => check_threshold(info, session) }).length >= 3
wenzelm@63703
    47
      } yield session
wenzelm@63703
    48
wenzelm@63703
    49
    Isabelle_System.mkdirs(dir)
wenzelm@63703
    50
    for (session <- sessions) {
wenzelm@63688
    51
      Isabelle_System.with_tmp_file(session, "png") { data_file =>
wenzelm@63688
    52
        Isabelle_System.with_tmp_file(session, "gnuplot") { plot_file =>
wenzelm@63688
    53
          val data =
wenzelm@64054
    54
            for { (t, info) <- all_infos if info.finished.isDefinedAt(session) }
wenzelm@63688
    55
            yield {
wenzelm@64054
    56
              val finished = info.finished.getOrElse(session, Timing.zero)
wenzelm@64054
    57
              val timing = info.timing.getOrElse(session, Timing.zero)
wenzelm@63706
    58
              List(t.toString, finished.elapsed.minutes, finished.cpu.minutes,
wenzelm@63706
    59
                timing.elapsed.minutes, timing.cpu.minutes, timing.gc.minutes).mkString(" ")
wenzelm@63688
    60
            }
wenzelm@63688
    61
          File.write(data_file, cat_lines(data))
wenzelm@63688
    62
wenzelm@63706
    63
          val plots1 =
wenzelm@63706
    64
            List(
wenzelm@63707
    65
              """ using 1:3 smooth sbezier title "cpu time (smooth)" """,
wenzelm@63707
    66
              """ using 1:3 smooth csplines title "cpu time" """,
wenzelm@63706
    67
              """ using 1:2 smooth sbezier title "elapsed time (smooth)" """,
wenzelm@63707
    68
              """ using 1:2 smooth csplines title "elapsed time" """)
wenzelm@63706
    69
          val plots2 =
wenzelm@63706
    70
            List(
wenzelm@63707
    71
              """ using 1:5 smooth sbezier title "ML cpu time (smooth)" """,
wenzelm@63707
    72
              """ using 1:5 smooth csplines title "ML cpu time" """,
wenzelm@63706
    73
              """ using 1:4 smooth sbezier title "ML elapsed time (smooth)" """,
wenzelm@63706
    74
              """ using 1:4 smooth csplines title "ML elapsed time" """,
wenzelm@63706
    75
              """ using 1:6 smooth sbezier title "ML gc time (smooth)" """,
wenzelm@63706
    76
              """ using 1:6 smooth csplines title "ML gc time" """)
wenzelm@63706
    77
          val plots =
wenzelm@63706
    78
            ml_timing match {
wenzelm@63706
    79
              case None => plots1
wenzelm@63706
    80
              case Some(false) => plots1 ::: plots2
wenzelm@63706
    81
              case Some(true) => plots2
wenzelm@63706
    82
            }
wenzelm@63706
    83
wenzelm@63706
    84
          val data_file_name = File.standard_path(data_file.getAbsolutePath)
wenzelm@63688
    85
          File.write(plot_file, """
wenzelm@63700
    86
set terminal png size """ + size._1 + "," + size._2 + """
wenzelm@63688
    87
set output """ + quote(File.standard_path(dir + Path.basic(session + ".png"))) + """
wenzelm@63688
    88
set xdata time
wenzelm@63688
    89
set timefmt "%s"
wenzelm@63688
    90
set format x "%d-%b"
wenzelm@63701
    91
set xlabel """ + quote(session) + """ noenhanced
wenzelm@63688
    92
set key left top
wenzelm@63706
    93
plot [] [0:] """ + plots.map(s => quote(data_file_name) + " " + s).mkString(", ") + "\n")
wenzelm@63688
    94
          val result = Isabelle_System.bash("\"$ISABELLE_GNUPLOT\" " + File.bash_path(plot_file))
wenzelm@63688
    95
          if (result.rc != 0) {
wenzelm@63688
    96
            Output.error_message("Session " + session + ": gnuplot error")
wenzelm@63688
    97
            result.print
wenzelm@63688
    98
          }
wenzelm@63688
    99
        }
wenzelm@63688
   100
      }
wenzelm@63688
   101
    }
wenzelm@63703
   102
wenzelm@63703
   103
    sessions.toList.sorted
wenzelm@63688
   104
  }
wenzelm@63688
   105
wenzelm@63688
   106
wenzelm@63688
   107
  /* Isabelle tool wrapper */
wenzelm@63688
   108
wenzelm@63703
   109
  private val html_header = """<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
wenzelm@63703
   110
<html>
wenzelm@63703
   111
<head><title>Performance statistics from session build output</title></head>
wenzelm@63703
   112
<body>
wenzelm@63703
   113
"""
wenzelm@63703
   114
  private val html_footer = """
wenzelm@63703
   115
</body>
wenzelm@63703
   116
</html>
wenzelm@63703
   117
"""
wenzelm@63703
   118
wenzelm@63688
   119
  val isabelle_tool =
wenzelm@63688
   120
    Isabelle_Tool("build_stats", "present statistics from session build output", args =>
wenzelm@63688
   121
    {
wenzelm@63703
   122
      var target_dir = Path.explode("stats")
wenzelm@63706
   123
      var ml_timing = default_ml_timing
wenzelm@63700
   124
      var only_sessions = default_only_sessions
wenzelm@63700
   125
      var elapsed_threshold = default_elapsed_threshold
wenzelm@63700
   126
      var history_length = default_history_length
wenzelm@63700
   127
      var size = default_size
wenzelm@63688
   128
wenzelm@63688
   129
      val getopts = Getopts("""
wenzelm@63700
   130
Usage: isabelle build_stats [OPTIONS] [JOBS ...]
wenzelm@63688
   131
wenzelm@63688
   132
  Options are:
wenzelm@63688
   133
    -D DIR       target directory (default "stats")
wenzelm@63706
   134
    -M           only ML timing
wenzelm@63700
   135
    -S SESSIONS  only given SESSIONS (comma separated)
wenzelm@63700
   136
    -T THRESHOLD only sessions with elapsed time >= THRESHOLD (minutes)
wenzelm@63688
   137
    -l LENGTH    length of history (default 100)
wenzelm@63706
   138
    -m           include ML timing
wenzelm@63700
   139
    -s WxH       size of PNG image (default 800x600)
wenzelm@63688
   140
wenzelm@63700
   141
  Present statistics from session build output of the given JOBS, from Jenkins
wenzelm@63700
   142
  continuous build service specified as URL via ISABELLE_JENKINS_ROOT.
wenzelm@63688
   143
""",
wenzelm@63688
   144
        "D:" -> (arg => target_dir = Path.explode(arg)),
wenzelm@63706
   145
        "M" -> (_ => ml_timing = Some(true)),
wenzelm@63700
   146
        "S:" -> (arg => only_sessions = space_explode(',', arg).toSet),
wenzelm@63805
   147
        "T:" -> (arg => elapsed_threshold = Time.minutes(Value.Double.parse(arg))),
wenzelm@63805
   148
        "l:" -> (arg => history_length = Value.Int.parse(arg)),
wenzelm@63706
   149
        "m" -> (_ => ml_timing = Some(false)),
wenzelm@63700
   150
        "s:" -> (arg =>
wenzelm@63805
   151
          space_explode('x', arg).map(Value.Int.parse(_)) match {
wenzelm@63700
   152
            case List(w, h) if w > 0 && h > 0 => size = (w, h)
wenzelm@63700
   153
            case _ => error("Error bad PNG image size: " + quote(arg))
wenzelm@63700
   154
          }))
wenzelm@63688
   155
wenzelm@63700
   156
      val jobs = getopts(args)
wenzelm@63688
   157
      val all_jobs = CI_API.build_jobs()
wenzelm@63700
   158
      val bad_jobs = jobs.filterNot(all_jobs.contains(_)).sorted
wenzelm@63700
   159
wenzelm@63700
   160
      if (jobs.isEmpty)
wenzelm@63984
   161
        error("No build jobs given. Available jobs: " + all_jobs.sorted.mkString(" "))
wenzelm@63700
   162
wenzelm@63700
   163
      if (bad_jobs.nonEmpty)
wenzelm@63984
   164
        error("Unknown build jobs: " + bad_jobs.mkString(" ") +
wenzelm@63984
   165
          "\nAvailable jobs: " + all_jobs.sorted.mkString(" "))
wenzelm@63688
   166
wenzelm@63688
   167
      for (job <- jobs) {
wenzelm@63703
   168
        val dir = target_dir + Path.basic(job)
wenzelm@63703
   169
        Output.writeln(dir.implode)
wenzelm@63706
   170
        val sessions =
wenzelm@63706
   171
          present_job(job, dir, history_length, size, only_sessions, elapsed_threshold, ml_timing)
wenzelm@63703
   172
        File.write(dir + Path.basic("index.html"),
wenzelm@63703
   173
          html_header + "\n<h1>" + HTML.output(job) + "</h1>\n" +
wenzelm@63703
   174
          cat_lines(
wenzelm@63703
   175
            sessions.map(session =>
wenzelm@63703
   176
              """<br/><img src=""" + quote(HTML.output(session + ".png")) + """><br/>""")) +
wenzelm@63703
   177
          "\n" + html_footer)
wenzelm@63688
   178
      }
wenzelm@63703
   179
wenzelm@63703
   180
      File.write(target_dir + Path.basic("index.html"),
wenzelm@63703
   181
        html_header + "\n<ul>\n" +
wenzelm@63703
   182
        cat_lines(
wenzelm@63703
   183
          jobs.map(job => """<li> <a href=""" + quote(HTML.output(job + "/index.html")) + """>""" +
wenzelm@63703
   184
            HTML.output(job) + """</a> </li>""")) +
wenzelm@63703
   185
        "\n</ul>\n" + html_footer)
wenzelm@63703
   186
  })
wenzelm@63686
   187
}