src/Pure/Admin/build_stats.scala
author wenzelm
Sat May 06 00:12:46 2017 +0200 (2017-05-06)
changeset 65736 2e7230b66a32
parent 65733 45b8446a8b52
child 65737 0729c09be90c
permissions -rw-r--r--
performance statistics from build log database;
wenzelm@64161
     1
/*  Title:      Pure/Admin/build_stats.scala
wenzelm@63686
     2
    Author:     Makarius
wenzelm@63686
     3
wenzelm@65736
     4
Performance statistics from build log database.
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@65736
    12
  private val default_target_dir = Path.explode("stats")
wenzelm@65736
    13
  private val default_history_length = 30
wenzelm@65736
    14
  private val default_image_size = (800, 600)
wenzelm@65736
    15
wenzelm@65736
    16
wenzelm@65736
    17
  /* data profiles */
wenzelm@63688
    18
wenzelm@65736
    19
  sealed case class Profile(name: String, sql: String)
wenzelm@65736
    20
  {
wenzelm@65736
    21
    def select(columns: List[SQL.Column], days: Int, only_sessions: Set[String]): SQL.Source =
wenzelm@65736
    22
    {
wenzelm@65736
    23
      val sql_sessions =
wenzelm@65736
    24
        if (only_sessions.isEmpty) ""
wenzelm@65736
    25
        else
wenzelm@65736
    26
          only_sessions.iterator.map(a => Build_Log.Data.session_name + " = " + SQL.string(a))
wenzelm@65736
    27
            .mkString("(", " OR ", ") AND ")
wenzelm@63700
    28
wenzelm@65736
    29
      Build_Log.Data.universal_table.select(columns, distinct = true,
wenzelm@65736
    30
        sql = "WHERE " +
wenzelm@65736
    31
          Build_Log.Data.pull_date + " > " + Build_Log.Data.recent_time(days) + " AND " +
wenzelm@65736
    32
          Build_Log.Data.status + " = " + SQL.string(Build_Log.Session_Status.finished.toString) +
wenzelm@65736
    33
          " AND " + sql_sessions + SQL.enclose(sql) +
wenzelm@65736
    34
          " ORDER BY " + Build_Log.Data.pull_date + " DESC")
wenzelm@65736
    35
    }
wenzelm@65736
    36
  }
wenzelm@65736
    37
wenzelm@65736
    38
  val standard_profiles: List[Profile] =
wenzelm@65736
    39
    Jenkins.build_log_profiles :::
wenzelm@65736
    40
    Isabelle_Cronjob.remote_builds.flatten.toList.map(r => Profile(r.name, r.sql))
wenzelm@65736
    41
wenzelm@65736
    42
  sealed case class Entry(date: Date, timing: Timing, ml_timing: Timing)
wenzelm@63688
    43
  {
wenzelm@65736
    44
    def check(elapsed_threshold: Time): Boolean =
wenzelm@65736
    45
      !timing.is_zero && timing.elapsed >= elapsed_threshold
wenzelm@65736
    46
  }
wenzelm@65736
    47
wenzelm@65736
    48
  type Data = Map[String, Map[String, List[Entry]]]
wenzelm@65736
    49
wenzelm@63688
    50
wenzelm@65736
    51
  /* read data */
wenzelm@65736
    52
wenzelm@65736
    53
  def read_data(options: Options,
wenzelm@65736
    54
    profiles: List[Profile] = standard_profiles,
wenzelm@65736
    55
    progress: Progress = No_Progress,
wenzelm@65736
    56
    history_length: Int = default_history_length,
wenzelm@65736
    57
    only_sessions: Set[String] = Set.empty,
wenzelm@65736
    58
    elapsed_threshold: Time = Time.zero): Data =
wenzelm@65736
    59
  {
wenzelm@65736
    60
    var data: Data = Map.empty
wenzelm@63688
    61
wenzelm@65736
    62
    val store = Build_Log.store(options)
wenzelm@65736
    63
    using(store.open_database())(db =>
wenzelm@64089
    64
    {
wenzelm@65736
    65
      for (profile <- profiles) {
wenzelm@65736
    66
        progress.echo("database query " + profile.name)
wenzelm@65736
    67
        val columns =
wenzelm@65736
    68
          List(
wenzelm@65736
    69
            Build_Log.Data.pull_date,
wenzelm@65736
    70
            Build_Log.Settings.ML_PLATFORM,
wenzelm@65736
    71
            Build_Log.Data.session_name,
wenzelm@65736
    72
            Build_Log.Data.threads,
wenzelm@65736
    73
            Build_Log.Data.timing_elapsed,
wenzelm@65736
    74
            Build_Log.Data.timing_cpu,
wenzelm@65736
    75
            Build_Log.Data.timing_gc,
wenzelm@65736
    76
            Build_Log.Data.ml_timing_elapsed,
wenzelm@65736
    77
            Build_Log.Data.ml_timing_cpu,
wenzelm@65736
    78
            Build_Log.Data.ml_timing_gc)
wenzelm@63700
    79
wenzelm@65736
    80
        db.using_statement(profile.select(columns, history_length, only_sessions))(stmt =>
wenzelm@65736
    81
        {
wenzelm@65736
    82
          val rs = stmt.executeQuery
wenzelm@65736
    83
          while (rs.next) {
wenzelm@65736
    84
            val ml_platform = db.string(rs, Build_Log.Settings.ML_PLATFORM)
wenzelm@65736
    85
            val threads = db.get_int(rs, Build_Log.Data.threads)
wenzelm@65736
    86
            val name =
wenzelm@65736
    87
              profile.name +
wenzelm@65736
    88
                "_m" + (if (ml_platform.startsWith("x86_64")) "64" else "32") +
wenzelm@65736
    89
                "_M" + threads.getOrElse(1)
wenzelm@63703
    90
wenzelm@65736
    91
            val session = db.string(rs, Build_Log.Data.session_name)
wenzelm@65736
    92
            val entry =
wenzelm@65736
    93
              Entry(db.date(rs, Build_Log.Data.pull_date),
wenzelm@65736
    94
                Timing(
wenzelm@65736
    95
                  Time.ms(db.long(rs, Build_Log.Data.timing_elapsed)),
wenzelm@65736
    96
                  Time.ms(db.long(rs, Build_Log.Data.timing_cpu)),
wenzelm@65736
    97
                  Time.ms(db.long(rs, Build_Log.Data.timing_gc))),
wenzelm@65736
    98
                Timing(
wenzelm@65736
    99
                  Time.ms(db.long(rs, Build_Log.Data.ml_timing_elapsed)),
wenzelm@65736
   100
                  Time.ms(db.long(rs, Build_Log.Data.ml_timing_cpu)),
wenzelm@65736
   101
                  Time.ms(db.long(rs, Build_Log.Data.ml_timing_gc))))
wenzelm@65736
   102
wenzelm@65736
   103
            val session_entries = data.getOrElse(name, Map.empty)
wenzelm@65736
   104
            val entries = session_entries.getOrElse(session, Nil)
wenzelm@65736
   105
            data += (name -> (session_entries + (session -> (entry :: entries))))
wenzelm@65736
   106
          }
wenzelm@65736
   107
        })
wenzelm@65736
   108
      }
wenzelm@65736
   109
    })
wenzelm@65736
   110
wenzelm@65736
   111
    for {
wenzelm@65736
   112
      (name, session_entries) <- data
wenzelm@65736
   113
      session_entries1 <-
wenzelm@65736
   114
        {
wenzelm@65736
   115
          val session_entries1 =
wenzelm@65736
   116
            for {
wenzelm@65736
   117
              (session, entries) <- session_entries
wenzelm@65736
   118
              if entries.filter(_.check(elapsed_threshold)).length >= 3
wenzelm@65736
   119
            } yield (session, entries)
wenzelm@65736
   120
          if (session_entries1.isEmpty) None
wenzelm@65736
   121
          else Some(session_entries1)
wenzelm@65736
   122
        }
wenzelm@65736
   123
    } yield (name, session_entries1)
wenzelm@65736
   124
  }
wenzelm@65736
   125
wenzelm@65736
   126
wenzelm@65736
   127
  /* present data */
wenzelm@65736
   128
wenzelm@65736
   129
  private val html_header = """<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
wenzelm@65736
   130
<html>
wenzelm@65736
   131
<head><title>Performance statistics from build log database</title></head>
wenzelm@65736
   132
<body>
wenzelm@65736
   133
"""
wenzelm@65736
   134
  private val html_footer = """
wenzelm@65736
   135
</body>
wenzelm@65736
   136
</html>
wenzelm@65736
   137
"""
wenzelm@63688
   138
wenzelm@65736
   139
  def present_data(data: Data,
wenzelm@65736
   140
    progress: Progress = No_Progress,
wenzelm@65736
   141
    target_dir: Path = default_target_dir,
wenzelm@65736
   142
    image_size: (Int, Int) = default_image_size,
wenzelm@65736
   143
    ml_timing: Option[Boolean] = None)
wenzelm@65736
   144
  {
wenzelm@65736
   145
    val data_entries = data.toList.sortBy(_._1)
wenzelm@65736
   146
    for ((name, session_entries) <- data_entries) {
wenzelm@65736
   147
      val dir = target_dir + Path.explode(name)
wenzelm@65736
   148
      progress.echo("directory " + dir)
wenzelm@65736
   149
      Isabelle_System.mkdirs(dir)
wenzelm@65736
   150
wenzelm@65736
   151
      for ((session, entries) <- session_entries) {
wenzelm@65736
   152
        Isabelle_System.with_tmp_file(session, "data") { data_file =>
wenzelm@65736
   153
          Isabelle_System.with_tmp_file(session, "gnuplot") { gnuplot_file =>
wenzelm@63706
   154
wenzelm@65736
   155
            File.write(data_file,
wenzelm@65736
   156
              cat_lines(
wenzelm@65736
   157
                entries.map(entry =>
wenzelm@65736
   158
                  List(entry.date.unix_epoch.toString,
wenzelm@65736
   159
                    entry.timing.elapsed.minutes,
wenzelm@65736
   160
                    entry.timing.cpu.minutes,
wenzelm@65736
   161
                    entry.ml_timing.elapsed.minutes,
wenzelm@65736
   162
                    entry.ml_timing.cpu.minutes,
wenzelm@65736
   163
                    entry.ml_timing.gc.minutes).mkString(" "))))
wenzelm@65736
   164
wenzelm@65736
   165
            val plots1 =
wenzelm@65736
   166
              List(
wenzelm@65736
   167
                """ using 1:3 smooth sbezier title "cpu time (smooth)" """,
wenzelm@65736
   168
                """ using 1:3 smooth csplines title "cpu time" """,
wenzelm@65736
   169
                """ using 1:2 smooth sbezier title "elapsed time (smooth)" """,
wenzelm@65736
   170
                """ using 1:2 smooth csplines title "elapsed time" """)
wenzelm@65736
   171
            val plots2 =
wenzelm@65736
   172
              List(
wenzelm@65736
   173
                """ using 1:5 smooth sbezier title "ML cpu time (smooth)" """,
wenzelm@65736
   174
                """ using 1:5 smooth csplines title "ML cpu time" """,
wenzelm@65736
   175
                """ using 1:4 smooth sbezier title "ML elapsed time (smooth)" """,
wenzelm@65736
   176
                """ using 1:4 smooth csplines title "ML elapsed time" """,
wenzelm@65736
   177
                """ using 1:6 smooth sbezier title "ML gc time (smooth)" """,
wenzelm@65736
   178
                """ using 1:6 smooth csplines title "ML gc time" """)
wenzelm@65736
   179
            val plots =
wenzelm@65736
   180
              ml_timing match {
wenzelm@65736
   181
                case None => plots1
wenzelm@65736
   182
                case Some(false) => plots1 ::: plots2
wenzelm@65736
   183
                case Some(true) => plots2
wenzelm@65736
   184
              }
wenzelm@65736
   185
wenzelm@65736
   186
            File.write(gnuplot_file, """
wenzelm@65736
   187
set terminal png size """ + image_size._1 + "," + image_size._2 + """
wenzelm@63688
   188
set output """ + quote(File.standard_path(dir + Path.basic(session + ".png"))) + """
wenzelm@63688
   189
set xdata time
wenzelm@63688
   190
set timefmt "%s"
wenzelm@63688
   191
set format x "%d-%b"
wenzelm@63701
   192
set xlabel """ + quote(session) + """ noenhanced
wenzelm@63688
   193
set key left top
wenzelm@64220
   194
plot [] [0:] """ + plots.map(s => quote(data_file.implode) + " " + s).mkString(", ") + "\n")
wenzelm@65736
   195
wenzelm@65736
   196
            val result =
wenzelm@65736
   197
              Isabelle_System.bash("\"$ISABELLE_GNUPLOT\" " + File.bash_path(gnuplot_file))
wenzelm@65736
   198
            if (result.rc != 0)
wenzelm@65736
   199
              result.error("Gnuplot error in " + name + "/" + session).check
wenzelm@63688
   200
          }
wenzelm@63688
   201
        }
wenzelm@63688
   202
      }
wenzelm@65736
   203
wenzelm@65736
   204
      File.write(dir + Path.basic("index.html"),
wenzelm@65736
   205
        html_header + "\n<h1>" + HTML.output(name) + "</h1>\n" +
wenzelm@65736
   206
        cat_lines(
wenzelm@65736
   207
          session_entries.toList.map(_._1).sorted.map(session =>
wenzelm@65736
   208
            """<br/><img src=""" + quote(HTML.output(session + ".png")) + """><br/>""")) +
wenzelm@65736
   209
        "\n" + html_footer)
wenzelm@63688
   210
    }
wenzelm@63703
   211
wenzelm@65736
   212
    File.write(target_dir + Path.basic("index.html"),
wenzelm@65736
   213
      html_header + "\n<ul>\n" +
wenzelm@65736
   214
      cat_lines(
wenzelm@65736
   215
        data_entries.map(_._1).map(name =>
wenzelm@65736
   216
          """<li> <a href=""" + quote(HTML.output(name + "/index.html")) + """>""" +
wenzelm@65736
   217
            HTML.output(name) + """</a> </li>""")) +
wenzelm@65736
   218
      "\n</ul>\n" + html_footer)
wenzelm@63688
   219
  }
wenzelm@63688
   220
wenzelm@63688
   221
wenzelm@63688
   222
  /* Isabelle tool wrapper */
wenzelm@63688
   223
wenzelm@63688
   224
  val isabelle_tool =
wenzelm@65736
   225
    Isabelle_Tool("build_stats", "present statistics from build log database", args =>
wenzelm@63688
   226
    {
wenzelm@65736
   227
      var target_dir = default_target_dir
wenzelm@65736
   228
      var ml_timing: Option[Boolean] = None
wenzelm@65736
   229
      var only_sessions = Set.empty[String]
wenzelm@65736
   230
      var elapsed_threshold = Time.zero
wenzelm@63700
   231
      var history_length = default_history_length
wenzelm@65736
   232
      var options = Options.init()
wenzelm@65736
   233
      var image_size = default_image_size
wenzelm@63688
   234
wenzelm@63688
   235
      val getopts = Getopts("""
wenzelm@65736
   236
Usage: isabelle build_stats [OPTIONS]
wenzelm@63688
   237
wenzelm@63688
   238
  Options are:
wenzelm@65736
   239
    -D DIR       target directory (default " + default_target_dir + ")
wenzelm@63706
   240
    -M           only ML timing
wenzelm@63700
   241
    -S SESSIONS  only given SESSIONS (comma separated)
wenzelm@63700
   242
    -T THRESHOLD only sessions with elapsed time >= THRESHOLD (minutes)
wenzelm@63688
   243
    -l LENGTH    length of history (default 100)
wenzelm@63706
   244
    -m           include ML timing
wenzelm@65736
   245
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
wenzelm@63700
   246
    -s WxH       size of PNG image (default 800x600)
wenzelm@63688
   247
wenzelm@65736
   248
  Present performance statistics from build log database, which is specified
wenzelm@65736
   249
  via system options build_log_database_host, build_log_database_user etc.
wenzelm@63688
   250
""",
wenzelm@63688
   251
        "D:" -> (arg => target_dir = Path.explode(arg)),
wenzelm@63706
   252
        "M" -> (_ => ml_timing = Some(true)),
wenzelm@63700
   253
        "S:" -> (arg => only_sessions = space_explode(',', arg).toSet),
wenzelm@63805
   254
        "T:" -> (arg => elapsed_threshold = Time.minutes(Value.Double.parse(arg))),
wenzelm@63805
   255
        "l:" -> (arg => history_length = Value.Int.parse(arg)),
wenzelm@63706
   256
        "m" -> (_ => ml_timing = Some(false)),
wenzelm@65736
   257
        "o:" -> (arg => options = options + arg),
wenzelm@63700
   258
        "s:" -> (arg =>
wenzelm@63805
   259
          space_explode('x', arg).map(Value.Int.parse(_)) match {
wenzelm@65736
   260
            case List(w, h) if w > 0 && h > 0 => image_size = (w, h)
wenzelm@63700
   261
            case _ => error("Error bad PNG image size: " + quote(arg))
wenzelm@63700
   262
          }))
wenzelm@63688
   263
wenzelm@65736
   264
      val more_args = getopts(args)
wenzelm@65736
   265
      if (more_args.nonEmpty) getopts.usage()
wenzelm@63700
   266
wenzelm@65736
   267
      val progress = new Console_Progress
wenzelm@63688
   268
wenzelm@65736
   269
      val data =
wenzelm@65736
   270
        read_data(options, profiles = standard_profiles, progress = progress,
wenzelm@65736
   271
          history_length = history_length, only_sessions = only_sessions,
wenzelm@65736
   272
          elapsed_threshold = elapsed_threshold)
wenzelm@63703
   273
wenzelm@65736
   274
      present_data(data, progress = progress, target_dir = target_dir,
wenzelm@65736
   275
        image_size = image_size, ml_timing = ml_timing)
wenzelm@65736
   276
wenzelm@64161
   277
  }, admin = true)
wenzelm@63686
   278
}