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