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