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