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