src/Pure/Admin/build_status.scala
author wenzelm
Wed Nov 01 17:29:14 2017 +0100 (19 months ago)
changeset 66980 8947cf58cb86
parent 66882 2d98b0141d89
child 66981 e76c6cb0d461
permissions -rw-r--r--
tuned output;
     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(
    25     description: String, history: Int = 0, afp: Boolean = false, slow: Boolean = false, sql: String)
    26   {
    27     def days(options: Options): Int = options.int("build_log_history") max history
    28 
    29     def stretch(options: Options): Double =
    30       (days(options) max default_history min (default_history * 5)).toDouble / default_history
    31 
    32     def select(options: Options, columns: List[SQL.Column], only_sessions: Set[String]): SQL.Source =
    33     {
    34       Build_Log.Data.universal_table.select(columns, distinct = true,
    35         sql = "WHERE " +
    36           Build_Log.Data.pull_date(afp) + " > " + Build_Log.Data.recent_time(days(options)) +
    37           " AND " +
    38           SQL.member(Build_Log.Data.status.ident,
    39             List(
    40               Build_Log.Session_Status.finished.toString,
    41               Build_Log.Session_Status.failed.toString)) +
    42           (if (only_sessions.isEmpty) ""
    43            else " AND " + SQL.member(Build_Log.Data.session_name.ident, only_sessions)) +
    44           " AND " + SQL.enclose(sql) +
    45           " ORDER BY " + Build_Log.Data.pull_date(afp))
    46     }
    47   }
    48 
    49 
    50   /* build status */
    51 
    52   def build_status(options: Options,
    53     progress: Progress = No_Progress,
    54     profiles: List[Profile] = default_profiles,
    55     only_sessions: Set[String] = Set.empty,
    56     verbose: Boolean = false,
    57     target_dir: Path = default_target_dir,
    58     ml_statistics: Boolean = false,
    59     image_size: (Int, Int) = default_image_size)
    60   {
    61     val data =
    62       read_data(options, progress = progress, profiles = profiles,
    63         only_sessions = only_sessions, ml_statistics = ml_statistics, verbose = verbose)
    64 
    65     present_data(data, progress = progress, target_dir = target_dir, image_size = image_size)
    66   }
    67 
    68 
    69   /* read data */
    70 
    71   sealed case class Data(date: Date, entries: List[Data_Entry])
    72   sealed case class Data_Entry(
    73     name: String, hosts: List[String], stretch: Double, sessions: List[Session])
    74   {
    75     def failed_sessions: List[Session] =
    76       sessions.filter(_.head.failed).sortBy(_.name)
    77   }
    78   sealed case class Session(
    79     name: String, threads: Int, entries: List[Entry], ml_statistics: ML_Statistics)
    80   {
    81     require(entries.nonEmpty)
    82 
    83     def head: Entry = entries.head
    84     def order: Long = - head.timing.elapsed.ms
    85 
    86     def finished_entries: List[Entry] = entries.filter(_.finished)
    87     def finished_entries_size: Int =
    88       finished_entries.map(entry => entry.pull_date.unix_epoch).toSet.size
    89 
    90     def check_timing: Boolean = finished_entries_size >= 3
    91     def check_heap: Boolean =
    92       finished_entries_size >= 3 &&
    93       finished_entries.forall(entry =>
    94         entry.maximum_heap > 0 ||
    95         entry.average_heap > 0 ||
    96         entry.stored_heap > 0)
    97   }
    98   sealed case class Entry(
    99     chapter: String,
   100     pull_date: Date,
   101     isabelle_version: String,
   102     afp_version: String,
   103     timing: Timing,
   104     ml_timing: Timing,
   105     maximum_heap: Long,
   106     average_heap: Long,
   107     stored_heap: Long,
   108     status: Build_Log.Session_Status.Value,
   109     errors: List[String])
   110   {
   111     def finished: Boolean = status == Build_Log.Session_Status.finished
   112     def failed: Boolean = status == Build_Log.Session_Status.failed
   113 
   114     def present_errors(name: String): XML.Body =
   115     {
   116       if (errors.isEmpty)
   117         HTML.text(name + print_version(isabelle_version, afp_version, chapter))
   118       else {
   119         HTML.tooltip_errors(HTML.text(name), errors.map(s => HTML.text(Symbol.decode(s)))) ::
   120           HTML.text(print_version(isabelle_version, afp_version, chapter))
   121       }
   122     }
   123   }
   124 
   125   sealed case class Image(name: String, width: Int, height: Int)
   126   {
   127     def path: Path = Path.basic(name)
   128   }
   129 
   130   def print_version(
   131     isabelle_version: String, afp_version: String = "", chapter: String = "AFP"): String =
   132   {
   133     val body =
   134       proper_string(isabelle_version).map("Isabelle/" + _).toList :::
   135       (if (chapter == "AFP") proper_string(afp_version).map("AFP/" + _) else None).toList
   136     if (body.isEmpty) "" else body.mkString(" (", ", ", ")")
   137   }
   138 
   139   def read_data(options: Options,
   140     progress: Progress = No_Progress,
   141     profiles: List[Profile] = default_profiles,
   142     only_sessions: Set[String] = Set.empty,
   143     ml_statistics: Boolean = false,
   144     verbose: Boolean = false): Data =
   145   {
   146     val date = Date.now()
   147     var data_hosts = Map.empty[String, Set[String]]
   148     var data_stretch = Map.empty[String, Double]
   149     var data_entries = Map.empty[String, Map[String, Session]]
   150 
   151     def get_hosts(data_name: String): Set[String] =
   152       data_hosts.getOrElse(data_name, Set.empty)
   153 
   154     val store = Build_Log.store(options)
   155     using(store.open_database())(db =>
   156     {
   157       for (profile <- profiles.sortBy(_.description)) {
   158         progress.echo("input " + quote(profile.description))
   159 
   160         val afp = profile.afp
   161         val columns =
   162           List(
   163             Build_Log.Data.pull_date(afp),
   164             Build_Log.Prop.build_host,
   165             Build_Log.Prop.isabelle_version,
   166             Build_Log.Prop.afp_version,
   167             Build_Log.Settings.ISABELLE_BUILD_OPTIONS,
   168             Build_Log.Settings.ML_PLATFORM,
   169             Build_Log.Data.session_name,
   170             Build_Log.Data.chapter,
   171             Build_Log.Data.groups,
   172             Build_Log.Data.threads,
   173             Build_Log.Data.timing_elapsed,
   174             Build_Log.Data.timing_cpu,
   175             Build_Log.Data.timing_gc,
   176             Build_Log.Data.ml_timing_elapsed,
   177             Build_Log.Data.ml_timing_cpu,
   178             Build_Log.Data.ml_timing_gc,
   179             Build_Log.Data.heap_size,
   180             Build_Log.Data.status,
   181             Build_Log.Data.errors) :::
   182           (if (ml_statistics) List(Build_Log.Data.ml_statistics) else Nil)
   183 
   184         val Threads_Option = """threads\s*=\s*(\d+)""".r
   185 
   186         val sql = profile.select(options, columns, only_sessions)
   187         if (verbose) progress.echo(sql)
   188 
   189         db.using_statement(sql)(stmt =>
   190         {
   191           val res = stmt.execute_query()
   192           while (res.next()) {
   193             val session_name = res.string(Build_Log.Data.session_name)
   194             val chapter = res.string(Build_Log.Data.chapter)
   195             val groups = split_lines(res.string(Build_Log.Data.groups))
   196             val threads =
   197             {
   198               val threads1 =
   199                 res.string(Build_Log.Settings.ISABELLE_BUILD_OPTIONS) match {
   200                   case Threads_Option(Value.Int(i)) => i
   201                   case _ => 1
   202                 }
   203               val threads2 = res.get_int(Build_Log.Data.threads).getOrElse(1)
   204               threads1 max threads2
   205             }
   206             val ml_platform = res.string(Build_Log.Settings.ML_PLATFORM)
   207             val data_name =
   208               profile.description +
   209                 (if (ml_platform.startsWith("x86_64")) ", 64bit" else "") +
   210                 (if (threads == 1) "" else ", " + threads + " threads")
   211 
   212             res.get_string(Build_Log.Prop.build_host).foreach(host =>
   213               data_hosts += (data_name -> (get_hosts(data_name) + host)))
   214 
   215             data_stretch += (data_name -> profile.stretch(options))
   216 
   217             val isabelle_version = res.string(Build_Log.Prop.isabelle_version)
   218             val afp_version = res.string(Build_Log.Prop.afp_version)
   219 
   220             val ml_stats =
   221               ML_Statistics(
   222                 if (ml_statistics)
   223                   Properties.uncompress(res.bytes(Build_Log.Data.ml_statistics))
   224                 else Nil,
   225                 heading = session_name + print_version(isabelle_version, afp_version, chapter))
   226 
   227             val entry =
   228               Entry(
   229                 chapter = chapter,
   230                 pull_date = res.date(Build_Log.Data.pull_date(afp)),
   231                 isabelle_version = isabelle_version,
   232                 afp_version = afp_version,
   233                 timing =
   234                   res.timing(
   235                     Build_Log.Data.timing_elapsed,
   236                     Build_Log.Data.timing_cpu,
   237                     Build_Log.Data.timing_gc),
   238                 ml_timing =
   239                   res.timing(
   240                     Build_Log.Data.ml_timing_elapsed,
   241                     Build_Log.Data.ml_timing_cpu,
   242                     Build_Log.Data.ml_timing_gc),
   243                 maximum_heap = ml_stats.maximum_heap_size,
   244                 average_heap = ml_stats.average_heap_size,
   245                 stored_heap = ML_Statistics.heap_scale(res.long(Build_Log.Data.heap_size)),
   246                 status = Build_Log.Session_Status.withName(res.string(Build_Log.Data.status)),
   247                 errors = Build_Log.uncompress_errors(res.bytes(Build_Log.Data.errors)))
   248 
   249             val sessions = data_entries.getOrElse(data_name, Map.empty)
   250             val entries = sessions.get(session_name).map(_.entries) getOrElse Nil
   251             val session = Session(session_name, threads, entry :: entries, ml_stats)
   252 
   253             if ((!afp || chapter == "AFP") && (!profile.slow || groups.contains("slow"))) {
   254               data_entries += (data_name -> (sessions + (session_name -> session)))
   255             }
   256           }
   257         })
   258       }
   259     })
   260 
   261     val sorted_entries =
   262       (for {
   263         (name, sessions) <- data_entries.toList
   264         sorted_sessions <- proper_list(sessions.toList.map(_._2).sortBy(_.order))
   265       }
   266       yield {
   267         val hosts = get_hosts(name).toList.sorted
   268         val stretch = data_stretch(name)
   269         Data_Entry(name, hosts, stretch, sorted_sessions)
   270       }).sortBy(_.name)
   271 
   272     Data(date, sorted_entries)
   273   }
   274 
   275 
   276   /* present data */
   277 
   278   def present_data(data: Data,
   279     progress: Progress = No_Progress,
   280     target_dir: Path = default_target_dir,
   281     image_size: (Int, Int) = default_image_size)
   282   {
   283     def clean_name(name: String): String =
   284       name.flatMap(c => if (c == ' ' || c == '/') "_" else if (c == ',') "" else c.toString)
   285 
   286     def print_heap(x: Long): Option[String] =
   287       if (x == 0L) None else Some(x.toString + " M")
   288 
   289     HTML.write_document(target_dir, "index.html",
   290       List(HTML.title("Isabelle build status")),
   291       List(HTML.chapter("Isabelle build status"),
   292         HTML.par(
   293           List(HTML.description(
   294             List(HTML.text("status date:") -> HTML.text(data.date.toString))))),
   295         HTML.par(
   296           List(HTML.itemize(data.entries.map({ case data_entry =>
   297             List(
   298               HTML.link(clean_name(data_entry.name) + "/index.html",
   299                 HTML.text(data_entry.name))) :::
   300             (data_entry.failed_sessions match {
   301               case Nil => Nil
   302               case sessions =>
   303                 HTML.break :::
   304                 List(HTML.span(HTML.error_message, HTML.text("Failed sessions:"))) :::
   305                 List(HTML.itemize(sessions.map(s => s.head.present_errors(s.name))))
   306             })
   307           }))))))
   308 
   309     for (data_entry <- data.entries) {
   310       val data_name = data_entry.name
   311 
   312       val (image_width, image_height) = image_size
   313       val image_width_stretch = (image_width * data_entry.stretch).toInt
   314 
   315       progress.echo("output " + quote(data_name))
   316 
   317       val dir = target_dir + Path.basic(clean_name(data_name))
   318       Isabelle_System.mkdirs(dir)
   319 
   320       val session_plots =
   321         Par_List.map((session: Session) =>
   322           Isabelle_System.with_tmp_file(session.name, "data") { data_file =>
   323             Isabelle_System.with_tmp_file(session.name, "gnuplot") { gnuplot_file =>
   324 
   325               def plot_name(kind: String): String = session.name + "_" + kind + ".png"
   326 
   327               File.write(data_file,
   328                 cat_lines(
   329                   session.finished_entries.map(entry =>
   330                     List(entry.pull_date.unix_epoch,
   331                       entry.timing.elapsed.minutes,
   332                       entry.timing.resources.minutes,
   333                       entry.ml_timing.elapsed.minutes,
   334                       entry.ml_timing.resources.minutes,
   335                       entry.maximum_heap,
   336                       entry.average_heap,
   337                       entry.stored_heap).mkString(" "))))
   338 
   339               val max_time =
   340                 ((0.0 /: session.finished_entries){ case (m, entry) =>
   341                   m.max(entry.timing.elapsed.minutes).
   342                     max(entry.timing.resources.minutes).
   343                     max(entry.ml_timing.elapsed.minutes).
   344                     max(entry.ml_timing.resources.minutes) } max 0.1) * 1.1
   345               val timing_range = "[0:" + max_time + "]"
   346 
   347               def gnuplot(plot_name: String, plots: List[String], range: String): Image =
   348               {
   349                 val image = Image(plot_name, image_width_stretch, image_height)
   350 
   351                 File.write(gnuplot_file, """
   352 set terminal png size """ + image.width + "," + image.height + """
   353 set output """ + quote(File.standard_path(dir + image.path)) + """
   354 set xdata time
   355 set timefmt "%s"
   356 set format x "%d-%b"
   357 set xlabel """ + quote(session.name) + """ noenhanced
   358 set key left bottom
   359 plot [] """ + range + " " +
   360                 plots.map(s => quote(data_file.implode) + " " + s).mkString(", ") + "\n")
   361 
   362                 val result =
   363                   Isabelle_System.bash("\"$ISABELLE_GNUPLOT\" " + File.bash_path(gnuplot_file))
   364                 if (!result.ok)
   365                   result.error("Gnuplot failed for " + data_name + "/" + plot_name).check
   366 
   367                 image
   368               }
   369 
   370               val timing_plots =
   371               {
   372                 val plots1 =
   373                   List(
   374                     """ using 1:2 smooth sbezier title "elapsed time (smooth)" """,
   375                     """ using 1:2 smooth csplines title "elapsed time" """)
   376                 val plots2 =
   377                   List(
   378                     """ using 1:3 smooth sbezier title "cpu time (smooth)" """,
   379                     """ using 1:3 smooth csplines title "cpu time" """)
   380                 if (session.threads == 1) plots1 else plots1 ::: plots2
   381               }
   382 
   383               val ml_timing_plots =
   384                 List(
   385                   """ using 1:4 smooth sbezier title "ML elapsed time (smooth)" """,
   386                   """ using 1:4 smooth csplines title "ML elapsed time" """,
   387                   """ using 1:5 smooth sbezier title "ML cpu time (smooth)" """,
   388                   """ using 1:5 smooth csplines title "ML cpu time" """)
   389 
   390               val heap_plots =
   391                 List(
   392                   """ using 1:6 smooth sbezier title "maximum heap (smooth)" """,
   393                   """ using 1:6 smooth csplines title "maximum heap" """,
   394                   """ using 1:7 smooth sbezier title "average heap (smooth)" """,
   395                   """ using 1:7 smooth csplines title "average heap" """,
   396                   """ using 1:8 smooth sbezier title "stored heap (smooth)" """,
   397                   """ using 1:8 smooth csplines title "stored heap" """)
   398 
   399               def jfreechart(plot_name: String, fields: ML_Statistics.Fields): Image =
   400               {
   401                 val image = Image(plot_name, image_width, image_height)
   402                 val chart =
   403                   session.ml_statistics.chart(
   404                     fields._1 + ": " + session.ml_statistics.heading, fields._2)
   405                 Graphics_File.write_chart_png(
   406                   (dir + image.path).file, chart, image.width, image.height)
   407                 image
   408               }
   409 
   410               val images =
   411                 (if (session.check_timing)
   412                   List(
   413                     gnuplot(plot_name("timing"), timing_plots, timing_range),
   414                     gnuplot(plot_name("ml_timing"), ml_timing_plots, timing_range))
   415                  else Nil) :::
   416                 (if (session.check_heap)
   417                   List(gnuplot(plot_name("heap"), heap_plots, "[0:]"))
   418                  else Nil) :::
   419                 (if (session.ml_statistics.content.nonEmpty)
   420                   List(jfreechart(plot_name("heap_chart"), ML_Statistics.heap_fields)) :::
   421                   (if (session.threads > 1)
   422                     List(
   423                       jfreechart(plot_name("tasks_chart"), ML_Statistics.tasks_fields),
   424                       jfreechart(plot_name("workers_chart"), ML_Statistics.workers_fields))
   425                    else Nil)
   426                  else Nil)
   427 
   428               session.name -> images
   429             }
   430           }, data_entry.sessions).toMap
   431 
   432       HTML.write_document(dir, "index.html",
   433         List(HTML.title("Isabelle build status for " + data_name)),
   434         HTML.chapter("Isabelle build status for " + data_name) ::
   435         HTML.par(
   436           List(HTML.description(
   437             List(
   438               HTML.text("status date:") -> HTML.text(data.date.toString),
   439               HTML.text("build host:") -> HTML.text(commas(data_entry.hosts)))))) ::
   440         HTML.par(
   441           List(HTML.itemize(
   442             data_entry.sessions.map(session =>
   443               HTML.link("#session_" + session.name, HTML.text(session.name)) ::
   444               HTML.text(" (" + session.head.timing.message_resources + ")"))))) ::
   445         data_entry.sessions.flatMap(session =>
   446           List(
   447             HTML.section(HTML.id("session_" + session.name), session.name),
   448             HTML.par(
   449               HTML.description(
   450                 List(
   451                   HTML.text("timing:") -> HTML.text(session.head.timing.message_resources),
   452                   HTML.text("ML timing:") -> HTML.text(session.head.ml_timing.message_resources)) :::
   453                 print_heap(session.head.maximum_heap).map(s =>
   454                   HTML.text("maximum heap:") -> HTML.text(s)).toList :::
   455                 print_heap(session.head.average_heap).map(s =>
   456                   HTML.text("average heap:") -> HTML.text(s)).toList :::
   457                 print_heap(session.head.stored_heap).map(s =>
   458                   HTML.text("stored heap:") -> HTML.text(s)).toList :::
   459                 proper_string(session.head.isabelle_version).map(s =>
   460                   HTML.text("Isabelle version:") -> HTML.text(s)).toList :::
   461                 proper_string(session.head.afp_version).map(s =>
   462                   HTML.text("AFP version:") -> HTML.text(s)).toList) ::
   463               session_plots.getOrElse(session.name, Nil).map(image =>
   464                 HTML.size(image.width / 2, image.height / 2)(HTML.image(image.name)))))))
   465     }
   466   }
   467 
   468 
   469   /* Isabelle tool wrapper */
   470 
   471   val isabelle_tool =
   472     Isabelle_Tool("build_status", "present recent build status information from database", args =>
   473     {
   474       var target_dir = default_target_dir
   475       var ml_statistics = false
   476       var only_sessions = Set.empty[String]
   477       var options = Options.init()
   478       var image_size = default_image_size
   479       var verbose = false
   480 
   481       val getopts = Getopts("""
   482 Usage: isabelle build_status [OPTIONS]
   483 
   484   Options are:
   485     -D DIR       target directory (default """ + default_target_dir + """)
   486     -M           include full ML statistics
   487     -S SESSIONS  only given SESSIONS (comma separated)
   488     -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
   489     -s WxH       size of PNG image (default """ + image_size._1 + "x" + image_size._2 + """)
   490     -v           verbose
   491 
   492   Present performance statistics from build log database, which is specified
   493   via system options build_log_database_host, build_log_database_user,
   494   build_log_history etc.
   495 """,
   496         "D:" -> (arg => target_dir = Path.explode(arg)),
   497         "M" -> (_ => ml_statistics = true),
   498         "S:" -> (arg => only_sessions = space_explode(',', arg).toSet),
   499         "o:" -> (arg => options = options + arg),
   500         "s:" -> (arg =>
   501           space_explode('x', arg).map(Value.Int.parse(_)) match {
   502             case List(w, h) if w > 0 && h > 0 => image_size = (w, h)
   503             case _ => error("Error bad PNG image size: " + quote(arg))
   504           }),
   505         "v" -> (_ => verbose = true))
   506 
   507       val more_args = getopts(args)
   508       if (more_args.nonEmpty) getopts.usage()
   509 
   510       val progress = new Console_Progress
   511 
   512       build_status(options, progress = progress, only_sessions = only_sessions, verbose = verbose,
   513         target_dir = target_dir, ml_statistics = ml_statistics, image_size = image_size)
   514 
   515   }, admin = true)
   516 }