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