src/Pure/Admin/build_status.scala
author wenzelm
Sun May 07 23:18:23 2017 +0200 (2017-05-07)
changeset 65764 1af6d544c2a3
parent 65763 dbadcc3fbe33
child 65765 c67bb109cd7b
permissions -rw-r--r--
clarified description vs. file name;
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@65757
    14
  private val default_image_size = (640, 480)
wenzelm@65736
    15
wenzelm@65736
    16
wenzelm@65736
    17
  /* data profiles */
wenzelm@63688
    18
wenzelm@65764
    19
  def clean_name(name: String): String =
wenzelm@65764
    20
    name.flatMap(c => if (c == ' ' || c == '/') "_" else if (c == ',') "" else c.toString)
wenzelm@65764
    21
wenzelm@65764
    22
  sealed case class Profile(description: String, sql: String)
wenzelm@65736
    23
  {
wenzelm@65736
    24
    def select(columns: List[SQL.Column], days: Int, only_sessions: Set[String]): SQL.Source =
wenzelm@65736
    25
    {
wenzelm@65736
    26
      val sql_sessions =
wenzelm@65736
    27
        if (only_sessions.isEmpty) ""
wenzelm@65736
    28
        else
wenzelm@65736
    29
          only_sessions.iterator.map(a => Build_Log.Data.session_name + " = " + SQL.string(a))
wenzelm@65736
    30
            .mkString("(", " OR ", ") AND ")
wenzelm@63700
    31
wenzelm@65736
    32
      Build_Log.Data.universal_table.select(columns, distinct = true,
wenzelm@65736
    33
        sql = "WHERE " +
wenzelm@65736
    34
          Build_Log.Data.pull_date + " > " + Build_Log.Data.recent_time(days) + " AND " +
wenzelm@65736
    35
          Build_Log.Data.status + " = " + SQL.string(Build_Log.Session_Status.finished.toString) +
wenzelm@65736
    36
          " AND " + sql_sessions + SQL.enclose(sql) +
wenzelm@65736
    37
          " ORDER BY " + Build_Log.Data.pull_date + " DESC")
wenzelm@65736
    38
    }
wenzelm@65736
    39
  }
wenzelm@65736
    40
wenzelm@65736
    41
  val standard_profiles: List[Profile] =
wenzelm@65747
    42
    Jenkins.build_status_profiles ::: Isabelle_Cronjob.build_status_profiles
wenzelm@65736
    43
wenzelm@65736
    44
wenzelm@65762
    45
  sealed case class Data(date: Date, entries: List[(String, List[Session])])
wenzelm@65762
    46
  sealed case class Session(name: String, entries: List[Entry])
wenzelm@65754
    47
  {
wenzelm@65762
    48
    def timing: Timing = if (entries.isEmpty) Timing.zero else entries.head.timing
wenzelm@65762
    49
    def ml_timing: Timing = if (entries.isEmpty) Timing.zero else entries.head.ml_timing
wenzelm@65762
    50
    def order: Long = - timing.elapsed.ms
wenzelm@65762
    51
    def check: Boolean = entries.length >= 3
wenzelm@65754
    52
  }
wenzelm@65762
    53
  sealed case class Entry(date: Date, timing: Timing, ml_timing: Timing)
wenzelm@65736
    54
wenzelm@63688
    55
wenzelm@65736
    56
  /* read data */
wenzelm@65736
    57
wenzelm@65736
    58
  def read_data(options: Options,
wenzelm@65736
    59
    profiles: List[Profile] = standard_profiles,
wenzelm@65736
    60
    progress: Progress = No_Progress,
wenzelm@65736
    61
    history_length: Int = default_history_length,
wenzelm@65736
    62
    only_sessions: Set[String] = Set.empty,
wenzelm@65750
    63
    verbose: Boolean = false): Data =
wenzelm@65736
    64
  {
wenzelm@65754
    65
    val date = Date.now()
wenzelm@65762
    66
    var data_entries = Map.empty[String, Map[String, Session]]
wenzelm@63688
    67
wenzelm@65736
    68
    val store = Build_Log.store(options)
wenzelm@65736
    69
    using(store.open_database())(db =>
wenzelm@64089
    70
    {
wenzelm@65764
    71
      for (profile <- profiles.sortBy(_.description)) {
wenzelm@65764
    72
        progress.echo("input " + quote(profile.description))
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@65764
    98
            val threads =
wenzelm@65764
    99
            {
wenzelm@65764
   100
              val threads1 =
wenzelm@65764
   101
                res.string(Build_Log.Settings.ISABELLE_BUILD_OPTIONS) match {
wenzelm@65764
   102
                  case Threads_Option(Value.Int(i)) => i
wenzelm@65764
   103
                  case _ => 1
wenzelm@65764
   104
                }
wenzelm@65764
   105
              val threads2 = res.get_int(Build_Log.Data.threads).getOrElse(1)
wenzelm@65764
   106
              threads1 max threads2
wenzelm@65764
   107
            }
wenzelm@65752
   108
wenzelm@65762
   109
            val data_name =
wenzelm@65764
   110
              profile.description +
wenzelm@65764
   111
                (if (ml_platform.startsWith("x86_64")) ", 64bit" else "") +
wenzelm@65764
   112
                (if (threads == 1) "" else ", " + threads + " threads")
wenzelm@63703
   113
wenzelm@65762
   114
            val name = res.string(Build_Log.Data.session_name)
wenzelm@65736
   115
            val entry =
wenzelm@65740
   116
              Entry(res.date(Build_Log.Data.pull_date),
wenzelm@65741
   117
                res.timing(
wenzelm@65741
   118
                  Build_Log.Data.timing_elapsed,
wenzelm@65741
   119
                  Build_Log.Data.timing_cpu,
wenzelm@65741
   120
                  Build_Log.Data.timing_gc),
wenzelm@65741
   121
                res.timing(
wenzelm@65741
   122
                  Build_Log.Data.ml_timing_elapsed,
wenzelm@65741
   123
                  Build_Log.Data.ml_timing_cpu,
wenzelm@65741
   124
                  Build_Log.Data.ml_timing_gc))
wenzelm@65736
   125
wenzelm@65762
   126
            val sessions = data_entries.getOrElse(data_name, Map.empty)
wenzelm@65762
   127
            val entries = sessions.get(name).map(_.entries) getOrElse Nil
wenzelm@65762
   128
            data_entries += (data_name -> (sessions + (name -> Session(name, entry :: entries))))
wenzelm@65736
   129
          }
wenzelm@65736
   130
        })
wenzelm@65736
   131
      }
wenzelm@65736
   132
    })
wenzelm@65736
   133
wenzelm@65754
   134
    Data(date,
wenzelm@65762
   135
      (for {
wenzelm@65762
   136
        (data_name, sessions) <- data_entries.toList
wenzelm@65762
   137
        sorted_sessions <- proper_list(sessions.toList.map(_._2).filter(_.check).sortBy(_.order))
wenzelm@65762
   138
      } yield (data_name, sorted_sessions)).sortBy(_._1))
wenzelm@65736
   139
  }
wenzelm@65736
   140
wenzelm@65736
   141
wenzelm@65736
   142
  /* present data */
wenzelm@65736
   143
wenzelm@65736
   144
  def present_data(data: Data,
wenzelm@65736
   145
    progress: Progress = No_Progress,
wenzelm@65736
   146
    target_dir: Path = default_target_dir,
wenzelm@65759
   147
    image_size: (Int, Int) = default_image_size)
wenzelm@65736
   148
  {
wenzelm@65762
   149
    for ((data_name, sessions) <- data.entries) {
wenzelm@65764
   150
      val dir = target_dir + Path.basic(clean_name(data_name))
wenzelm@65764
   151
wenzelm@65742
   152
      progress.echo("output " + dir)
wenzelm@65736
   153
      Isabelle_System.mkdirs(dir)
wenzelm@65736
   154
wenzelm@65764
   155
      val session_plots =
wenzelm@65764
   156
        Par_List.map((session: Session) =>
wenzelm@65764
   157
          Isabelle_System.with_tmp_file(session.name, "data") { data_file =>
wenzelm@65764
   158
            Isabelle_System.with_tmp_file(session.name, "gnuplot") { gnuplot_file =>
wenzelm@63706
   159
wenzelm@65764
   160
              File.write(data_file,
wenzelm@65764
   161
                cat_lines(
wenzelm@65764
   162
                  session.entries.map(entry =>
wenzelm@65764
   163
                    List(entry.date.unix_epoch.toString,
wenzelm@65764
   164
                      entry.timing.elapsed.minutes,
wenzelm@65764
   165
                      entry.timing.resources.minutes,
wenzelm@65764
   166
                      entry.ml_timing.elapsed.minutes,
wenzelm@65764
   167
                      entry.ml_timing.resources.minutes).mkString(" "))))
wenzelm@65736
   168
wenzelm@65764
   169
              val max_time =
wenzelm@65764
   170
                ((0.0 /: session.entries){ case (m, entry) =>
wenzelm@65764
   171
                  m.max(entry.timing.elapsed.minutes).
wenzelm@65764
   172
                    max(entry.timing.resources.minutes).
wenzelm@65764
   173
                    max(entry.ml_timing.elapsed.minutes).
wenzelm@65764
   174
                    max(entry.ml_timing.resources.minutes) } max 0.1) * 1.1
wenzelm@65763
   175
wenzelm@65764
   176
              def gnuplot(plots: List[String], kind: String): String =
wenzelm@65764
   177
              {
wenzelm@65764
   178
                val plot_name = session.name + "_" + kind + ".png"
wenzelm@65764
   179
wenzelm@65764
   180
                File.write(gnuplot_file, """
wenzelm@65759
   181
set terminal png size """ + image_size._1 + "," + image_size._2 + """
wenzelm@65764
   182
set output """ + quote(File.standard_path(dir + Path.basic(plot_name))) + """
wenzelm@65759
   183
set xdata time
wenzelm@65759
   184
set timefmt "%s"
wenzelm@65759
   185
set format x "%d-%b"
wenzelm@65762
   186
set xlabel """ + quote(session.name) + """ noenhanced
wenzelm@65763
   187
set key left bottom
wenzelm@65763
   188
plot [] [0:""" + max_time + "] " +
wenzelm@65764
   189
                  plots.map(s => quote(data_file.implode) + " " + s).mkString(", ") + "\n")
wenzelm@65759
   190
wenzelm@65764
   191
                val result =
wenzelm@65764
   192
                  Isabelle_System.bash("\"$ISABELLE_GNUPLOT\" " + File.bash_path(gnuplot_file))
wenzelm@65764
   193
                if (!result.ok)
wenzelm@65764
   194
                  result.error("Gnuplot failed for " + data_name + "/" + plot_name).check
wenzelm@65764
   195
wenzelm@65764
   196
                plot_name
wenzelm@65764
   197
              }
wenzelm@65759
   198
wenzelm@65764
   199
              val timing_plots =
wenzelm@65764
   200
                List(
wenzelm@65764
   201
                  """ using 1:3 smooth sbezier title "cpu time (smooth)" """,
wenzelm@65764
   202
                  """ using 1:3 smooth csplines title "cpu time" """,
wenzelm@65764
   203
                  """ using 1:2 smooth sbezier title "elapsed time (smooth)" """,
wenzelm@65764
   204
                  """ using 1:2 smooth csplines title "elapsed time" """)
wenzelm@65764
   205
              val ml_timing_plots =
wenzelm@65764
   206
                List(
wenzelm@65764
   207
                  """ using 1:5 smooth sbezier title "ML cpu time (smooth)" """,
wenzelm@65764
   208
                  """ using 1:5 smooth csplines title "ML cpu time" """,
wenzelm@65764
   209
                  """ using 1:4 smooth sbezier title "ML elapsed time (smooth)" """,
wenzelm@65764
   210
                  """ using 1:4 smooth csplines title "ML elapsed time" """)
wenzelm@65736
   211
wenzelm@65764
   212
              session.name ->
wenzelm@65764
   213
                List(gnuplot(timing_plots, "timing"), gnuplot(ml_timing_plots, "ml_timing"))
wenzelm@65764
   214
            }
wenzelm@65764
   215
          }, sessions).toMap
wenzelm@65754
   216
wenzelm@65736
   217
      File.write(dir + Path.basic("index.html"),
wenzelm@65754
   218
        HTML.output_document(
wenzelm@65760
   219
          List(HTML.title("Isabelle build status for " + data_name)),
wenzelm@65760
   220
          HTML.chapter("Isabelle build status for " + data_name + " (" + data.date + ")") ::
wenzelm@65754
   221
          HTML.itemize(
wenzelm@65762
   222
            sessions.map(session =>
wenzelm@65762
   223
              HTML.link("#session_" + session.name, HTML.text(session.name)) ::
wenzelm@65762
   224
              HTML.text(" (" + session.timing.message_resources + ")"))) ::
wenzelm@65762
   225
          sessions.flatMap(session =>
wenzelm@65754
   226
            List(
wenzelm@65762
   227
              HTML.section(session.name) + HTML.id("session_" + session.name),
wenzelm@65755
   228
              HTML.par(
wenzelm@65764
   229
                HTML.itemize(List(
wenzelm@65764
   230
                  HTML.bold(HTML.text("timing: ")) :: HTML.text(session.timing.message_resources),
wenzelm@65764
   231
                  HTML.bold(HTML.text("ML timing: ")) ::
wenzelm@65764
   232
                    HTML.text(session.ml_timing.message_resources))) ::
wenzelm@65764
   233
                session_plots.getOrElse(session.name, Nil).map(HTML.image(_)))))))
wenzelm@63688
   234
    }
wenzelm@63703
   235
wenzelm@65736
   236
    File.write(target_dir + Path.basic("index.html"),
wenzelm@65754
   237
      HTML.output_document(
wenzelm@65760
   238
        List(HTML.title("Isabelle build status")),
wenzelm@65760
   239
        List(HTML.chapter("Isabelle build status (" + data.date + ")"),
wenzelm@65764
   240
          HTML.itemize(data.entries.map({ case (data_name, _) =>
wenzelm@65764
   241
            List(HTML.link(clean_name(data_name) + "/index.html", HTML.text(data_name))) })))))
wenzelm@63688
   242
  }
wenzelm@63688
   243
wenzelm@63688
   244
wenzelm@63688
   245
  /* Isabelle tool wrapper */
wenzelm@63688
   246
wenzelm@63688
   247
  val isabelle_tool =
wenzelm@65743
   248
    Isabelle_Tool("build_status", "present recent build status information from database", args =>
wenzelm@63688
   249
    {
wenzelm@65736
   250
      var target_dir = default_target_dir
wenzelm@65736
   251
      var only_sessions = Set.empty[String]
wenzelm@63700
   252
      var history_length = default_history_length
wenzelm@65736
   253
      var options = Options.init()
wenzelm@65736
   254
      var image_size = default_image_size
wenzelm@65750
   255
      var verbose = false
wenzelm@63688
   256
wenzelm@63688
   257
      val getopts = Getopts("""
wenzelm@65743
   258
Usage: isabelle build_status [OPTIONS]
wenzelm@63688
   259
wenzelm@63688
   260
  Options are:
wenzelm@65737
   261
    -D DIR       target directory (default """ + default_target_dir + """)
wenzelm@63700
   262
    -S SESSIONS  only given SESSIONS (comma separated)
wenzelm@65737
   263
    -l LENGTH    length of history (default """ + default_history_length + """)
wenzelm@65736
   264
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
wenzelm@65737
   265
    -s WxH       size of PNG image (default """ + image_size._1 + "x" + image_size._2 + """)
wenzelm@65750
   266
    -v           verbose
wenzelm@63688
   267
wenzelm@65736
   268
  Present performance statistics from build log database, which is specified
wenzelm@65736
   269
  via system options build_log_database_host, build_log_database_user etc.
wenzelm@63688
   270
""",
wenzelm@63688
   271
        "D:" -> (arg => target_dir = Path.explode(arg)),
wenzelm@63700
   272
        "S:" -> (arg => only_sessions = space_explode(',', arg).toSet),
wenzelm@63805
   273
        "l:" -> (arg => history_length = Value.Int.parse(arg)),
wenzelm@65736
   274
        "o:" -> (arg => options = options + arg),
wenzelm@63700
   275
        "s:" -> (arg =>
wenzelm@63805
   276
          space_explode('x', arg).map(Value.Int.parse(_)) match {
wenzelm@65736
   277
            case List(w, h) if w > 0 && h > 0 => image_size = (w, h)
wenzelm@63700
   278
            case _ => error("Error bad PNG image size: " + quote(arg))
wenzelm@65750
   279
          }),
wenzelm@65750
   280
        "v" -> (_ => verbose = true))
wenzelm@63688
   281
wenzelm@65736
   282
      val more_args = getopts(args)
wenzelm@65736
   283
      if (more_args.nonEmpty) getopts.usage()
wenzelm@63700
   284
wenzelm@65736
   285
      val progress = new Console_Progress
wenzelm@63688
   286
wenzelm@65736
   287
      val data =
wenzelm@65745
   288
        read_data(options, progress = progress, history_length = history_length,
wenzelm@65758
   289
          only_sessions = only_sessions, verbose = verbose)
wenzelm@63703
   290
wenzelm@65759
   291
      present_data(data, progress = progress, target_dir = target_dir, image_size = image_size)
wenzelm@65736
   292
wenzelm@64161
   293
  }, admin = true)
wenzelm@63686
   294
}