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