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