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