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