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