src/Pure/Admin/build_status.scala
author wenzelm
Sun May 07 16:31:39 2017 +0200 (2017-05-07)
changeset 65755 21b4bfa6d204
parent 65754 05c6a29c9132
child 65756 2c6b5dd59db3
permissions -rw-r--r--
more HTML output;
wenzelm@65743
     1
/*  Title:      Pure/Admin/build_status.scala
wenzelm@63686
     2
    Author:     Makarius
wenzelm@63686
     3
wenzelm@65743
     4
Present recent build status information from database.
wenzelm@63686
     5
*/
wenzelm@63686
     6
wenzelm@63686
     7
package isabelle
wenzelm@63686
     8
wenzelm@63686
     9
wenzelm@65743
    10
object Build_Status
wenzelm@63686
    11
{
wenzelm@65743
    12
  private val default_target_dir = Path.explode("build_status")
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@65747
    39
    Jenkins.build_status_profiles ::: Isabelle_Cronjob.build_status_profiles
wenzelm@65736
    40
wenzelm@65736
    41
  sealed case class Entry(date: Date, timing: Timing, ml_timing: Timing)
wenzelm@63688
    42
  {
wenzelm@65736
    43
    def check(elapsed_threshold: Time): Boolean =
wenzelm@65736
    44
      !timing.is_zero && timing.elapsed >= elapsed_threshold
wenzelm@65736
    45
  }
wenzelm@65736
    46
wenzelm@65754
    47
  sealed case class Data(date: Date, entries: Map[String, Map[String, List[Entry]]])
wenzelm@65754
    48
  {
wenzelm@65754
    49
    def sorted_entries: List[(String, List[(String, List[Entry])])] =
wenzelm@65754
    50
      entries.toList.sortBy(_._1).map({ case (name, session_entries) =>
wenzelm@65754
    51
        (name, session_entries.toList.sortBy(_._2.head.timing.elapsed.ms).reverse) })
wenzelm@65754
    52
  }
wenzelm@65736
    53
wenzelm@63688
    54
wenzelm@65736
    55
  /* read data */
wenzelm@65736
    56
wenzelm@65736
    57
  def read_data(options: Options,
wenzelm@65736
    58
    profiles: List[Profile] = standard_profiles,
wenzelm@65736
    59
    progress: Progress = No_Progress,
wenzelm@65736
    60
    history_length: Int = default_history_length,
wenzelm@65736
    61
    only_sessions: Set[String] = Set.empty,
wenzelm@65750
    62
    elapsed_threshold: Time = Time.zero,
wenzelm@65750
    63
    verbose: Boolean = false): Data =
wenzelm@65736
    64
  {
wenzelm@65754
    65
    val date = Date.now()
wenzelm@65754
    66
    var data_entries = Map.empty[String, Map[String, List[Entry]]]
wenzelm@63688
    67
wenzelm@65736
    68
    val store = Build_Log.store(options)
wenzelm@65736
    69
    using(store.open_database())(db =>
wenzelm@64089
    70
    {
wenzelm@65751
    71
      for (profile <- profiles.sortBy(_.name)) {
wenzelm@65742
    72
        progress.echo("input " + quote(profile.name))
wenzelm@65736
    73
        val columns =
wenzelm@65736
    74
          List(
wenzelm@65736
    75
            Build_Log.Data.pull_date,
wenzelm@65752
    76
            Build_Log.Settings.ISABELLE_BUILD_OPTIONS,
wenzelm@65736
    77
            Build_Log.Settings.ML_PLATFORM,
wenzelm@65736
    78
            Build_Log.Data.session_name,
wenzelm@65736
    79
            Build_Log.Data.threads,
wenzelm@65736
    80
            Build_Log.Data.timing_elapsed,
wenzelm@65736
    81
            Build_Log.Data.timing_cpu,
wenzelm@65736
    82
            Build_Log.Data.timing_gc,
wenzelm@65736
    83
            Build_Log.Data.ml_timing_elapsed,
wenzelm@65736
    84
            Build_Log.Data.ml_timing_cpu,
wenzelm@65736
    85
            Build_Log.Data.ml_timing_gc)
wenzelm@63700
    86
wenzelm@65752
    87
        val Threads_Option = """threads\s*=\s*(\d+)""".r
wenzelm@65752
    88
wenzelm@65750
    89
        val sql = profile.select(columns, history_length, only_sessions)
wenzelm@65750
    90
        if (verbose) progress.echo(sql)
wenzelm@65750
    91
wenzelm@65750
    92
        db.using_statement(sql)(stmt =>
wenzelm@65736
    93
        {
wenzelm@65740
    94
          val res = stmt.execute_query()
wenzelm@65740
    95
          while (res.next()) {
wenzelm@65740
    96
            val ml_platform = res.string(Build_Log.Settings.ML_PLATFORM)
wenzelm@65752
    97
wenzelm@65752
    98
            val threads_option =
wenzelm@65752
    99
              res.string(Build_Log.Settings.ISABELLE_BUILD_OPTIONS) match {
wenzelm@65752
   100
                case Threads_Option(Value.Int(i)) => i
wenzelm@65752
   101
                case _ => 1
wenzelm@65752
   102
              }
wenzelm@65752
   103
            val threads = res.get_int(Build_Log.Data.threads).getOrElse(1)
wenzelm@65752
   104
wenzelm@65736
   105
            val name =
wenzelm@65736
   106
              profile.name +
wenzelm@65736
   107
                "_m" + (if (ml_platform.startsWith("x86_64")) "64" else "32") +
wenzelm@65752
   108
                "_M" + (threads_option max threads)
wenzelm@63703
   109
wenzelm@65740
   110
            val session = res.string(Build_Log.Data.session_name)
wenzelm@65736
   111
            val entry =
wenzelm@65740
   112
              Entry(res.date(Build_Log.Data.pull_date),
wenzelm@65741
   113
                res.timing(
wenzelm@65741
   114
                  Build_Log.Data.timing_elapsed,
wenzelm@65741
   115
                  Build_Log.Data.timing_cpu,
wenzelm@65741
   116
                  Build_Log.Data.timing_gc),
wenzelm@65741
   117
                res.timing(
wenzelm@65741
   118
                  Build_Log.Data.ml_timing_elapsed,
wenzelm@65741
   119
                  Build_Log.Data.ml_timing_cpu,
wenzelm@65741
   120
                  Build_Log.Data.ml_timing_gc))
wenzelm@65736
   121
wenzelm@65754
   122
            val session_entries = data_entries.getOrElse(name, Map.empty)
wenzelm@65736
   123
            val entries = session_entries.getOrElse(session, Nil)
wenzelm@65754
   124
            data_entries += (name -> (session_entries + (session -> (entry :: entries))))
wenzelm@65736
   125
          }
wenzelm@65736
   126
        })
wenzelm@65736
   127
      }
wenzelm@65736
   128
    })
wenzelm@65736
   129
wenzelm@65754
   130
    Data(date,
wenzelm@65754
   131
      for {
wenzelm@65754
   132
        (name, session_entries) <- data_entries
wenzelm@65754
   133
        session_entries1 <-
wenzelm@65754
   134
          {
wenzelm@65754
   135
            val session_entries1 =
wenzelm@65754
   136
              for {
wenzelm@65754
   137
                (session, entries) <- session_entries
wenzelm@65754
   138
                if entries.filter(_.check(elapsed_threshold)).length >= 3
wenzelm@65754
   139
              } yield (session, entries)
wenzelm@65754
   140
            if (session_entries1.isEmpty) None
wenzelm@65754
   141
            else Some(session_entries1)
wenzelm@65754
   142
          }
wenzelm@65754
   143
      } yield (name, session_entries1))
wenzelm@65736
   144
  }
wenzelm@65736
   145
wenzelm@65736
   146
wenzelm@65736
   147
  /* present data */
wenzelm@65736
   148
wenzelm@65736
   149
  def present_data(data: Data,
wenzelm@65736
   150
    progress: Progress = No_Progress,
wenzelm@65736
   151
    target_dir: Path = default_target_dir,
wenzelm@65736
   152
    image_size: (Int, Int) = default_image_size,
wenzelm@65736
   153
    ml_timing: Option[Boolean] = None)
wenzelm@65736
   154
  {
wenzelm@65754
   155
    val data_entries = data.sorted_entries
wenzelm@65754
   156
wenzelm@65754
   157
    for ((data_name, session_entries) <- data_entries) {
wenzelm@65754
   158
      val dir = target_dir + Path.explode(data_name)
wenzelm@65742
   159
      progress.echo("output " + dir)
wenzelm@65736
   160
      Isabelle_System.mkdirs(dir)
wenzelm@65736
   161
wenzelm@65736
   162
      for ((session, entries) <- session_entries) {
wenzelm@65736
   163
        Isabelle_System.with_tmp_file(session, "data") { data_file =>
wenzelm@65736
   164
          Isabelle_System.with_tmp_file(session, "gnuplot") { gnuplot_file =>
wenzelm@63706
   165
wenzelm@65736
   166
            File.write(data_file,
wenzelm@65736
   167
              cat_lines(
wenzelm@65736
   168
                entries.map(entry =>
wenzelm@65736
   169
                  List(entry.date.unix_epoch.toString,
wenzelm@65736
   170
                    entry.timing.elapsed.minutes,
wenzelm@65736
   171
                    entry.timing.cpu.minutes,
wenzelm@65736
   172
                    entry.ml_timing.elapsed.minutes,
wenzelm@65736
   173
                    entry.ml_timing.cpu.minutes,
wenzelm@65736
   174
                    entry.ml_timing.gc.minutes).mkString(" "))))
wenzelm@65736
   175
wenzelm@65736
   176
            val plots1 =
wenzelm@65736
   177
              List(
wenzelm@65736
   178
                """ using 1:3 smooth sbezier title "cpu time (smooth)" """,
wenzelm@65736
   179
                """ using 1:3 smooth csplines title "cpu time" """,
wenzelm@65736
   180
                """ using 1:2 smooth sbezier title "elapsed time (smooth)" """,
wenzelm@65736
   181
                """ using 1:2 smooth csplines title "elapsed time" """)
wenzelm@65736
   182
            val plots2 =
wenzelm@65736
   183
              List(
wenzelm@65736
   184
                """ using 1:5 smooth sbezier title "ML cpu time (smooth)" """,
wenzelm@65736
   185
                """ using 1:5 smooth csplines title "ML cpu time" """,
wenzelm@65736
   186
                """ using 1:4 smooth sbezier title "ML elapsed time (smooth)" """,
wenzelm@65736
   187
                """ using 1:4 smooth csplines title "ML elapsed time" """,
wenzelm@65736
   188
                """ using 1:6 smooth sbezier title "ML gc time (smooth)" """,
wenzelm@65736
   189
                """ using 1:6 smooth csplines title "ML gc time" """)
wenzelm@65736
   190
            val plots =
wenzelm@65736
   191
              ml_timing match {
wenzelm@65736
   192
                case None => plots1
wenzelm@65736
   193
                case Some(false) => plots1 ::: plots2
wenzelm@65736
   194
                case Some(true) => plots2
wenzelm@65736
   195
              }
wenzelm@65736
   196
wenzelm@65736
   197
            File.write(gnuplot_file, """
wenzelm@65736
   198
set terminal png size """ + image_size._1 + "," + image_size._2 + """
wenzelm@63688
   199
set output """ + quote(File.standard_path(dir + Path.basic(session + ".png"))) + """
wenzelm@63688
   200
set xdata time
wenzelm@63688
   201
set timefmt "%s"
wenzelm@63688
   202
set format x "%d-%b"
wenzelm@63701
   203
set xlabel """ + quote(session) + """ noenhanced
wenzelm@63688
   204
set key left top
wenzelm@64220
   205
plot [] [0:] """ + plots.map(s => quote(data_file.implode) + " " + s).mkString(", ") + "\n")
wenzelm@65736
   206
wenzelm@65736
   207
            val result =
wenzelm@65736
   208
              Isabelle_System.bash("\"$ISABELLE_GNUPLOT\" " + File.bash_path(gnuplot_file))
wenzelm@65736
   209
            if (result.rc != 0)
wenzelm@65754
   210
              result.error("Gnuplot failed for " + data_name + "/" + session).check
wenzelm@63688
   211
          }
wenzelm@63688
   212
        }
wenzelm@63688
   213
      }
wenzelm@65736
   214
wenzelm@65754
   215
      val sessions = session_entries.toList.sortBy(_._2.head.timing.elapsed.ms).reverse
wenzelm@65754
   216
      val heading = "Build status for " + data_name + " (" + data.date + ")"
wenzelm@65754
   217
wenzelm@65736
   218
      File.write(dir + Path.basic("index.html"),
wenzelm@65754
   219
        HTML.output_document(
wenzelm@65754
   220
          List(HTML.title(heading)),
wenzelm@65754
   221
          HTML.chapter(heading) ::
wenzelm@65754
   222
          HTML.itemize(
wenzelm@65754
   223
            sessions.map({ case (name, entries) =>
wenzelm@65754
   224
              HTML.link("#session_" + name, HTML.text(name)) ::
wenzelm@65754
   225
              HTML.text(" (" + entries.head.timing.message_resources + ")") })) ::
wenzelm@65754
   226
          sessions.flatMap({ case (name, entries) =>
wenzelm@65754
   227
            List(
wenzelm@65755
   228
              HTML.section(name) + HTML.id("session_" + name),
wenzelm@65755
   229
              HTML.par(
wenzelm@65755
   230
                List(
wenzelm@65755
   231
                  HTML.itemize(List(
wenzelm@65755
   232
                    HTML.bold(HTML.text("last timing: ")) ::
wenzelm@65755
   233
                      HTML.text(entries.head.timing.message_resources),
wenzelm@65755
   234
                    HTML.bold(HTML.text("last ML timing: ")) ::
wenzelm@65755
   235
                      HTML.text(entries.head.ml_timing.message_resources))),
wenzelm@65755
   236
                  HTML.image(name + ".png")))) })))
wenzelm@63688
   237
    }
wenzelm@63703
   238
wenzelm@65754
   239
    val heading = "Build status (" + data.date + ")"
wenzelm@65754
   240
wenzelm@65736
   241
    File.write(target_dir + Path.basic("index.html"),
wenzelm@65754
   242
      HTML.output_document(
wenzelm@65754
   243
        List(HTML.title(heading)),
wenzelm@65754
   244
        List(HTML.chapter(heading),
wenzelm@65754
   245
          HTML.itemize(data_entries.map({ case (name, _) =>
wenzelm@65754
   246
            List(HTML.link(name + "/index.html", HTML.text(name))) })))))
wenzelm@63688
   247
  }
wenzelm@63688
   248
wenzelm@63688
   249
wenzelm@63688
   250
  /* Isabelle tool wrapper */
wenzelm@63688
   251
wenzelm@63688
   252
  val isabelle_tool =
wenzelm@65743
   253
    Isabelle_Tool("build_status", "present recent build status information from database", args =>
wenzelm@63688
   254
    {
wenzelm@65736
   255
      var target_dir = default_target_dir
wenzelm@65736
   256
      var ml_timing: Option[Boolean] = None
wenzelm@65736
   257
      var only_sessions = Set.empty[String]
wenzelm@65736
   258
      var elapsed_threshold = Time.zero
wenzelm@63700
   259
      var history_length = default_history_length
wenzelm@65736
   260
      var options = Options.init()
wenzelm@65736
   261
      var image_size = default_image_size
wenzelm@65750
   262
      var verbose = false
wenzelm@63688
   263
wenzelm@63688
   264
      val getopts = Getopts("""
wenzelm@65743
   265
Usage: isabelle build_status [OPTIONS]
wenzelm@63688
   266
wenzelm@63688
   267
  Options are:
wenzelm@65737
   268
    -D DIR       target directory (default """ + default_target_dir + """)
wenzelm@63706
   269
    -M           only ML timing
wenzelm@63700
   270
    -S SESSIONS  only given SESSIONS (comma separated)
wenzelm@63700
   271
    -T THRESHOLD only sessions with elapsed time >= THRESHOLD (minutes)
wenzelm@65737
   272
    -l LENGTH    length of history (default """ + default_history_length + """)
wenzelm@63706
   273
    -m           include ML timing
wenzelm@65736
   274
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
wenzelm@65737
   275
    -s WxH       size of PNG image (default """ + image_size._1 + "x" + image_size._2 + """)
wenzelm@65750
   276
    -v           verbose
wenzelm@63688
   277
wenzelm@65736
   278
  Present performance statistics from build log database, which is specified
wenzelm@65736
   279
  via system options build_log_database_host, build_log_database_user etc.
wenzelm@63688
   280
""",
wenzelm@63688
   281
        "D:" -> (arg => target_dir = Path.explode(arg)),
wenzelm@63706
   282
        "M" -> (_ => ml_timing = Some(true)),
wenzelm@63700
   283
        "S:" -> (arg => only_sessions = space_explode(',', arg).toSet),
wenzelm@63805
   284
        "T:" -> (arg => elapsed_threshold = Time.minutes(Value.Double.parse(arg))),
wenzelm@63805
   285
        "l:" -> (arg => history_length = Value.Int.parse(arg)),
wenzelm@63706
   286
        "m" -> (_ => ml_timing = Some(false)),
wenzelm@65736
   287
        "o:" -> (arg => options = options + arg),
wenzelm@63700
   288
        "s:" -> (arg =>
wenzelm@63805
   289
          space_explode('x', arg).map(Value.Int.parse(_)) match {
wenzelm@65736
   290
            case List(w, h) if w > 0 && h > 0 => image_size = (w, h)
wenzelm@63700
   291
            case _ => error("Error bad PNG image size: " + quote(arg))
wenzelm@65750
   292
          }),
wenzelm@65750
   293
        "v" -> (_ => verbose = true))
wenzelm@63688
   294
wenzelm@65736
   295
      val more_args = getopts(args)
wenzelm@65736
   296
      if (more_args.nonEmpty) getopts.usage()
wenzelm@63700
   297
wenzelm@65736
   298
      val progress = new Console_Progress
wenzelm@63688
   299
wenzelm@65736
   300
      val data =
wenzelm@65745
   301
        read_data(options, progress = progress, history_length = history_length,
wenzelm@65750
   302
          only_sessions = only_sessions, elapsed_threshold = elapsed_threshold, verbose = verbose)
wenzelm@63703
   303
wenzelm@65736
   304
      present_data(data, progress = progress, target_dir = target_dir,
wenzelm@65736
   305
        image_size = image_size, ml_timing = ml_timing)
wenzelm@65736
   306
wenzelm@64161
   307
  }, admin = true)
wenzelm@63686
   308
}