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