src/Pure/Tools/build_stats.scala
author wenzelm
Sun Aug 14 22:35:38 2016 +0200 (2016-08-14)
changeset 63701 3744d2cf4d2f
parent 63700 2a95d904672e
child 63702 fed1d4dab990
permissions -rw-r--r--
proper display of "_";
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@63686
    18
  private val Session_Finished =
wenzelm@63686
    19
    new Regex("""^Finished (\S+) \((\d+):(\d+):(\d+) elapsed time, (\d+):(\d+):(\d+) cpu time.*$""")
wenzelm@63686
    20
  private val Session_Timing =
wenzelm@63686
    21
    new Regex("""^Timing (\S+) \((\d) threads, (\d+\.\d+)s elapsed time, (\d+\.\d+)s cpu time, (\d+\.\d+)s GC time.*$""")
wenzelm@63686
    22
wenzelm@63686
    23
  private object ML_Option
wenzelm@63686
    24
  {
wenzelm@63686
    25
    def unapply(s: String): Option[(String, String)] =
wenzelm@63686
    26
      s.indexOf('=') match {
wenzelm@63686
    27
        case -1 => None
wenzelm@63686
    28
        case i =>
wenzelm@63686
    29
          val a = s.substring(0, i)
wenzelm@63686
    30
          Library.try_unquote(s.substring(i + 1)) match {
wenzelm@63686
    31
            case Some(b) if Build.ml_options.contains(a) => Some((a, b))
wenzelm@63686
    32
            case _ => None
wenzelm@63686
    33
          }
wenzelm@63686
    34
      }
wenzelm@63686
    35
  }
wenzelm@63686
    36
wenzelm@63686
    37
  def parse(text: String): Build_Stats =
wenzelm@63686
    38
  {
wenzelm@63686
    39
    import Properties.Value
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@63686
    48
        case Session_Finished(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@63686
    54
        case Session_Timing(name,
wenzelm@63686
    55
            Value.Int(t), Value.Double(e), Value.Double(c), Value.Double(g)) =>
wenzelm@63686
    56
          val elapsed = Time.seconds(e)
wenzelm@63686
    57
          val cpu = Time.seconds(c)
wenzelm@63686
    58
          val gc = Time.seconds(g)
wenzelm@63686
    59
          timing += (name -> Timing(elapsed, cpu, gc))
wenzelm@63686
    60
          threads += (name -> t)
wenzelm@63686
    61
        case ML_Option(option) => ml_options += option
wenzelm@63686
    62
        case _ =>
wenzelm@63686
    63
      }
wenzelm@63686
    64
    }
wenzelm@63686
    65
wenzelm@63686
    66
    Build_Stats(ml_options.toList, finished, timing, threads)
wenzelm@63686
    67
  }
wenzelm@63688
    68
wenzelm@63688
    69
wenzelm@63688
    70
  /* presentation */
wenzelm@63688
    71
wenzelm@63700
    72
  private val default_target_dir = Path.explode("stats")
wenzelm@63700
    73
  private val default_history_length = 100
wenzelm@63700
    74
  private val default_size = (800, 600)
wenzelm@63700
    75
  private val default_only_sessions = Set.empty[String]
wenzelm@63700
    76
  private val default_elapsed_threshold = Time.zero
wenzelm@63700
    77
wenzelm@63700
    78
  def present(job: String,
wenzelm@63700
    79
    target_dir: Path = default_target_dir,
wenzelm@63700
    80
    history_length: Int = default_history_length,
wenzelm@63700
    81
    size: (Int, Int) = default_size,
wenzelm@63700
    82
    only_sessions: Set[String] = default_only_sessions,
wenzelm@63700
    83
    elapsed_threshold: Time = default_elapsed_threshold)
wenzelm@63688
    84
  {
wenzelm@63688
    85
    val build_infos = CI_API.build_job_builds(job).sortBy(_.timestamp).reverse.take(history_length)
wenzelm@63688
    86
    if (build_infos.isEmpty) error("No build infos for job " + quote(job))
wenzelm@63688
    87
wenzelm@63688
    88
    val dir = target_dir + Path.basic(job)
wenzelm@63688
    89
    Isabelle_System.mkdirs(dir)
wenzelm@63688
    90
wenzelm@63688
    91
    val all_build_stats =
wenzelm@63688
    92
      Par_List.map((info: CI_API.Build_Info) =>
wenzelm@63688
    93
        (info.timestamp / 1000, parse(Url.read(info.output))), build_infos)
wenzelm@63688
    94
    val all_sessions =
wenzelm@63688
    95
      (Set.empty[String] /: all_build_stats)(
wenzelm@63688
    96
        { case (s, (_, stats)) => s ++ stats.sessions })
wenzelm@63688
    97
wenzelm@63700
    98
    def check_threshold(stats: Build_Stats, session: String): Boolean =
wenzelm@63700
    99
      stats.finished.get(session) match {
wenzelm@63700
   100
        case Some(t) => t.elapsed >= elapsed_threshold
wenzelm@63700
   101
        case None => false
wenzelm@63700
   102
      }
wenzelm@63700
   103
wenzelm@63688
   104
    for {
wenzelm@63700
   105
      session <- (if (only_sessions.isEmpty) all_sessions else all_sessions & only_sessions)
wenzelm@63700
   106
      if all_build_stats.filter({ case (_, stats) => check_threshold(stats, session) }).length >= 3
wenzelm@63688
   107
    } {
wenzelm@63688
   108
      Isabelle_System.with_tmp_file(session, "png") { data_file =>
wenzelm@63688
   109
        Isabelle_System.with_tmp_file(session, "gnuplot") { plot_file =>
wenzelm@63688
   110
          val data =
wenzelm@63688
   111
            for { (t, stats) <- all_build_stats if stats.finished.isDefinedAt(session) }
wenzelm@63688
   112
            yield {
wenzelm@63688
   113
              val finished = stats.finished(session)
wenzelm@63688
   114
              t.toString + " " + finished.cpu.minutes + " " + finished.elapsed.minutes
wenzelm@63688
   115
            }
wenzelm@63688
   116
          File.write(data_file, cat_lines(data))
wenzelm@63688
   117
wenzelm@63700
   118
          val data_file_name = quote(File.standard_path(data_file.getAbsolutePath))
wenzelm@63688
   119
          File.write(plot_file, """
wenzelm@63700
   120
set terminal png size """ + size._1 + "," + size._2 + """
wenzelm@63688
   121
set output """ + quote(File.standard_path(dir + Path.basic(session + ".png"))) + """
wenzelm@63688
   122
set xdata time
wenzelm@63688
   123
set timefmt "%s"
wenzelm@63688
   124
set format x "%d-%b"
wenzelm@63701
   125
set xlabel """ + quote(session) + """ noenhanced
wenzelm@63688
   126
set key left top
wenzelm@63688
   127
plot [] [0:] """ +
wenzelm@63700
   128
  data_file_name + """ using 1:2 smooth sbezier title "interpolated cpu time",""" +
wenzelm@63700
   129
  data_file_name + """ using 1:2 smooth csplines title "cpu time", """ +
wenzelm@63700
   130
  data_file_name + """ using 1:3 smooth sbezier title "interpolated elapsed time",""" +
wenzelm@63700
   131
  data_file_name + """ using 1:3 smooth csplines title "elapsed time"
wenzelm@63688
   132
""")
wenzelm@63688
   133
          val result = Isabelle_System.bash("\"$ISABELLE_GNUPLOT\" " + File.bash_path(plot_file))
wenzelm@63688
   134
          if (result.rc != 0) {
wenzelm@63688
   135
            Output.error_message("Session " + session + ": gnuplot error")
wenzelm@63688
   136
            result.print
wenzelm@63688
   137
          }
wenzelm@63688
   138
        }
wenzelm@63688
   139
      }
wenzelm@63688
   140
    }
wenzelm@63688
   141
  }
wenzelm@63688
   142
wenzelm@63688
   143
wenzelm@63688
   144
  /* Isabelle tool wrapper */
wenzelm@63688
   145
wenzelm@63688
   146
  val isabelle_tool =
wenzelm@63688
   147
    Isabelle_Tool("build_stats", "present statistics from session build output", args =>
wenzelm@63688
   148
    {
wenzelm@63700
   149
      var target_dir = default_target_dir
wenzelm@63700
   150
      var only_sessions = default_only_sessions
wenzelm@63700
   151
      var elapsed_threshold = default_elapsed_threshold
wenzelm@63700
   152
      var history_length = default_history_length
wenzelm@63700
   153
      var size = default_size
wenzelm@63688
   154
wenzelm@63688
   155
      val getopts = Getopts("""
wenzelm@63700
   156
Usage: isabelle build_stats [OPTIONS] [JOBS ...]
wenzelm@63688
   157
wenzelm@63688
   158
  Options are:
wenzelm@63688
   159
    -D DIR       target directory (default "stats")
wenzelm@63700
   160
    -S SESSIONS  only given SESSIONS (comma separated)
wenzelm@63700
   161
    -T THRESHOLD only sessions with elapsed time >= THRESHOLD (minutes)
wenzelm@63688
   162
    -l LENGTH    length of history (default 100)
wenzelm@63700
   163
    -s WxH       size of PNG image (default 800x600)
wenzelm@63688
   164
wenzelm@63700
   165
  Present statistics from session build output of the given JOBS, from Jenkins
wenzelm@63700
   166
  continuous build service specified as URL via ISABELLE_JENKINS_ROOT.
wenzelm@63688
   167
""",
wenzelm@63688
   168
        "D:" -> (arg => target_dir = Path.explode(arg)),
wenzelm@63700
   169
        "S:" -> (arg => only_sessions = space_explode(',', arg).toSet),
wenzelm@63700
   170
        "T:" -> (arg => elapsed_threshold = Time.minutes(Properties.Value.Double.parse(arg))),
wenzelm@63700
   171
        "l:" -> (arg => history_length = Properties.Value.Int.parse(arg)),
wenzelm@63700
   172
        "s:" -> (arg =>
wenzelm@63700
   173
          space_explode('x', arg).map(Properties.Value.Int.parse(_)) match {
wenzelm@63700
   174
            case List(w, h) if w > 0 && h > 0 => size = (w, h)
wenzelm@63700
   175
            case _ => error("Error bad PNG image size: " + quote(arg))
wenzelm@63700
   176
          }))
wenzelm@63688
   177
wenzelm@63700
   178
      val jobs = getopts(args)
wenzelm@63688
   179
      val all_jobs = CI_API.build_jobs()
wenzelm@63700
   180
      val bad_jobs = jobs.filterNot(all_jobs.contains(_)).sorted
wenzelm@63700
   181
wenzelm@63700
   182
      if (jobs.isEmpty)
wenzelm@63700
   183
        error("No build jobs given. Available jobs: " + commas(all_jobs))
wenzelm@63700
   184
wenzelm@63700
   185
      if (bad_jobs.nonEmpty)
wenzelm@63700
   186
        error("Unknown build jobs: " + commas(bad_jobs) +
wenzelm@63700
   187
          "\nAvailable jobs: " + commas(all_jobs.sorted))
wenzelm@63688
   188
wenzelm@63688
   189
      for (job <- jobs) {
wenzelm@63688
   190
        Output.writeln((target_dir + Path.basic(job)).implode)
wenzelm@63700
   191
        present(job, target_dir, history_length, size, only_sessions, elapsed_threshold)
wenzelm@63688
   192
      }
wenzelm@63688
   193
    })
wenzelm@63686
   194
}
wenzelm@63686
   195
wenzelm@63686
   196
sealed case class Build_Stats(
wenzelm@63686
   197
  ml_options: List[(String, String)],
wenzelm@63686
   198
  finished: Map[String, Timing],
wenzelm@63686
   199
  timing: Map[String, Timing],
wenzelm@63686
   200
  threads: Map[String, Int])
wenzelm@63686
   201
{
wenzelm@63686
   202
  val sessions: Set[String] = finished.keySet ++ timing.keySet
wenzelm@63686
   203
wenzelm@63686
   204
  override def toString: String =
wenzelm@63686
   205
    sessions.toList.sorted.mkString("Build_Stats(", ", ", ")")
wenzelm@63686
   206
}