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