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