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