src/Pure/Tools/build_stats.scala
author wenzelm
Sat Aug 13 23:45:29 2016 +0200 (2016-08-13)
changeset 63688 cc57255bf6ae
parent 63686 66f217416da7
child 63700 2a95d904672e
permissions -rw-r--r--
gnuplot presentation similar to former isatest-statistics;
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@63686
    51
          val elapsed = Time.hours_minutes_seconds(e1, e2, e3)
wenzelm@63686
    52
          val cpu = Time.hours_minutes_seconds(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@63688
    72
  def present(job: String, history_length: Int, target_dir: Path)
wenzelm@63688
    73
  {
wenzelm@63688
    74
    val build_infos = CI_API.build_job_builds(job).sortBy(_.timestamp).reverse.take(history_length)
wenzelm@63688
    75
    if (build_infos.isEmpty) error("No build infos for job " + quote(job))
wenzelm@63688
    76
wenzelm@63688
    77
    val dir = target_dir + Path.basic(job)
wenzelm@63688
    78
    Isabelle_System.mkdirs(dir)
wenzelm@63688
    79
wenzelm@63688
    80
    val all_build_stats =
wenzelm@63688
    81
      Par_List.map((info: CI_API.Build_Info) =>
wenzelm@63688
    82
        (info.timestamp / 1000, parse(Url.read(info.output))), build_infos)
wenzelm@63688
    83
    val all_sessions =
wenzelm@63688
    84
      (Set.empty[String] /: all_build_stats)(
wenzelm@63688
    85
        { case (s, (_, stats)) => s ++ stats.sessions })
wenzelm@63688
    86
wenzelm@63688
    87
    for {
wenzelm@63688
    88
      session <- all_sessions
wenzelm@63688
    89
      if all_build_stats.filter({ case (_, stats) => stats.finished.isDefinedAt(session) }).length > 3
wenzelm@63688
    90
    } {
wenzelm@63688
    91
      Isabelle_System.with_tmp_file(session, "png") { data_file =>
wenzelm@63688
    92
        Isabelle_System.with_tmp_file(session, "gnuplot") { plot_file =>
wenzelm@63688
    93
          val data_file_name = File.standard_path(data_file.getAbsolutePath)
wenzelm@63688
    94
          val data =
wenzelm@63688
    95
            for { (t, stats) <- all_build_stats if stats.finished.isDefinedAt(session) }
wenzelm@63688
    96
            yield {
wenzelm@63688
    97
              val finished = stats.finished(session)
wenzelm@63688
    98
              t.toString + " " + finished.cpu.minutes + " " + finished.elapsed.minutes
wenzelm@63688
    99
            }
wenzelm@63688
   100
          File.write(data_file, cat_lines(data))
wenzelm@63688
   101
wenzelm@63688
   102
          File.write(plot_file, """
wenzelm@63688
   103
set terminal png size 1024,768
wenzelm@63688
   104
set output """ + quote(File.standard_path(dir + Path.basic(session + ".png"))) + """
wenzelm@63688
   105
set xdata time
wenzelm@63688
   106
set timefmt "%s"
wenzelm@63688
   107
set format x "%d-%b"
wenzelm@63688
   108
set xlabel """ + quote(session) + """
wenzelm@63688
   109
set key left top
wenzelm@63688
   110
plot [] [0:] """ +
wenzelm@63688
   111
  quote(data_file_name) + """ using 1:2 smooth sbezier title "interpolated cpu time",""" +
wenzelm@63688
   112
  quote(data_file_name) + """ using 1:2 smooth csplines title "cpu time", """ +
wenzelm@63688
   113
  quote(data_file_name) + """ using 1:3 smooth sbezier title "interpolated elapsed time",""" +
wenzelm@63688
   114
  quote(data_file_name) + """ using 1:3 smooth csplines title "elapsed time"
wenzelm@63688
   115
""")
wenzelm@63688
   116
          val result = Isabelle_System.bash("\"$ISABELLE_GNUPLOT\" " + File.bash_path(plot_file))
wenzelm@63688
   117
          if (result.rc != 0) {
wenzelm@63688
   118
            Output.error_message("Session " + session + ": gnuplot error")
wenzelm@63688
   119
            result.print
wenzelm@63688
   120
          }
wenzelm@63688
   121
        }
wenzelm@63688
   122
      }
wenzelm@63688
   123
    }
wenzelm@63688
   124
  }
wenzelm@63688
   125
wenzelm@63688
   126
wenzelm@63688
   127
  /* Isabelle tool wrapper */
wenzelm@63688
   128
wenzelm@63688
   129
  val isabelle_tool =
wenzelm@63688
   130
    Isabelle_Tool("build_stats", "present statistics from session build output", args =>
wenzelm@63688
   131
    {
wenzelm@63688
   132
      var target_dir = Path.explode("stats")
wenzelm@63688
   133
      var only_jobs = Set.empty[String]
wenzelm@63688
   134
      var history_length = 100
wenzelm@63688
   135
wenzelm@63688
   136
      val getopts = Getopts("""
wenzelm@63688
   137
Usage: isabelle build_stats [OPTIONS]
wenzelm@63688
   138
wenzelm@63688
   139
  Options are:
wenzelm@63688
   140
    -D DIR       target directory (default "stats")
wenzelm@63688
   141
    -J JOB       select named JOB (default: all jobs, multiple -J options possible)
wenzelm@63688
   142
    -l LENGTH    length of history (default 100)
wenzelm@63688
   143
wenzelm@63688
   144
  Present statistics from session build output, from Jenkins continuous build
wenzelm@63688
   145
  service specified as URL via ISABELLE_JENKINS_ROOT.
wenzelm@63688
   146
""",
wenzelm@63688
   147
        "D:" -> (arg => target_dir = Path.explode(arg)),
wenzelm@63688
   148
        "J:" -> (arg => only_jobs += arg),
wenzelm@63688
   149
        "l:" -> (arg => history_length = Properties.Value.Int.parse(arg)))
wenzelm@63688
   150
wenzelm@63688
   151
      val more_args = getopts(args)
wenzelm@63688
   152
      if (!more_args.isEmpty) getopts.usage()
wenzelm@63688
   153
wenzelm@63688
   154
      val all_jobs = CI_API.build_jobs()
wenzelm@63688
   155
      val jobs =
wenzelm@63688
   156
        if (only_jobs.isEmpty) all_jobs
wenzelm@63688
   157
        else {
wenzelm@63688
   158
          val bad = (only_jobs -- all_jobs).toList.sorted
wenzelm@63688
   159
          if (bad.isEmpty) only_jobs.toList
wenzelm@63688
   160
          else
wenzelm@63688
   161
            error("Unknown build jobs: " + commas_quote(bad) +
wenzelm@63688
   162
              "\nPossible jobs: " + commas_quote(all_jobs.sorted))
wenzelm@63688
   163
        }
wenzelm@63688
   164
wenzelm@63688
   165
      for (job <- jobs) {
wenzelm@63688
   166
        Output.writeln((target_dir + Path.basic(job)).implode)
wenzelm@63688
   167
        present(job, history_length, target_dir)
wenzelm@63688
   168
      }
wenzelm@63688
   169
    })
wenzelm@63686
   170
}
wenzelm@63686
   171
wenzelm@63686
   172
sealed case class Build_Stats(
wenzelm@63686
   173
  ml_options: List[(String, String)],
wenzelm@63686
   174
  finished: Map[String, Timing],
wenzelm@63686
   175
  timing: Map[String, Timing],
wenzelm@63686
   176
  threads: Map[String, Int])
wenzelm@63686
   177
{
wenzelm@63686
   178
  val sessions: Set[String] = finished.keySet ++ timing.keySet
wenzelm@63686
   179
wenzelm@63686
   180
  override def toString: String =
wenzelm@63686
   181
    sessions.toList.sorted.mkString("Build_Stats(", ", ", ")")
wenzelm@63686
   182
}