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