src/Pure/Admin/build_status.scala
author wenzelm
Sun May 07 13:42:20 2017 +0200 (2017-05-07)
changeset 65752 ed7b5cd3a7f2
parent 65751 426d4bf3b9bb
child 65754 05c6a29c9132
permissions -rw-r--r--
more uniform threads value, notably for Pure session;
     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 = (800, 600)
    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     def check(elapsed_threshold: Time): Boolean =
    44       !timing.is_zero && timing.elapsed >= elapsed_threshold
    45   }
    46 
    47   type Data = Map[String, Map[String, List[Entry]]]
    48 
    49 
    50   /* read data */
    51 
    52   def read_data(options: Options,
    53     profiles: List[Profile] = standard_profiles,
    54     progress: Progress = No_Progress,
    55     history_length: Int = default_history_length,
    56     only_sessions: Set[String] = Set.empty,
    57     elapsed_threshold: Time = Time.zero,
    58     verbose: Boolean = false): Data =
    59   {
    60     var data: Data = Map.empty
    61 
    62     val store = Build_Log.store(options)
    63     using(store.open_database())(db =>
    64     {
    65       for (profile <- profiles.sortBy(_.name)) {
    66         progress.echo("input " + quote(profile.name))
    67         val columns =
    68           List(
    69             Build_Log.Data.pull_date,
    70             Build_Log.Settings.ISABELLE_BUILD_OPTIONS,
    71             Build_Log.Settings.ML_PLATFORM,
    72             Build_Log.Data.session_name,
    73             Build_Log.Data.threads,
    74             Build_Log.Data.timing_elapsed,
    75             Build_Log.Data.timing_cpu,
    76             Build_Log.Data.timing_gc,
    77             Build_Log.Data.ml_timing_elapsed,
    78             Build_Log.Data.ml_timing_cpu,
    79             Build_Log.Data.ml_timing_gc)
    80 
    81         val Threads_Option = """threads\s*=\s*(\d+)""".r
    82 
    83         val sql = profile.select(columns, history_length, only_sessions)
    84         if (verbose) progress.echo(sql)
    85 
    86         db.using_statement(sql)(stmt =>
    87         {
    88           val res = stmt.execute_query()
    89           while (res.next()) {
    90             val ml_platform = res.string(Build_Log.Settings.ML_PLATFORM)
    91 
    92             val threads_option =
    93               res.string(Build_Log.Settings.ISABELLE_BUILD_OPTIONS) match {
    94                 case Threads_Option(Value.Int(i)) => i
    95                 case _ => 1
    96               }
    97             val threads = res.get_int(Build_Log.Data.threads).getOrElse(1)
    98 
    99             val name =
   100               profile.name +
   101                 "_m" + (if (ml_platform.startsWith("x86_64")) "64" else "32") +
   102                 "_M" + (threads_option max threads)
   103 
   104             val session = res.string(Build_Log.Data.session_name)
   105             val entry =
   106               Entry(res.date(Build_Log.Data.pull_date),
   107                 res.timing(
   108                   Build_Log.Data.timing_elapsed,
   109                   Build_Log.Data.timing_cpu,
   110                   Build_Log.Data.timing_gc),
   111                 res.timing(
   112                   Build_Log.Data.ml_timing_elapsed,
   113                   Build_Log.Data.ml_timing_cpu,
   114                   Build_Log.Data.ml_timing_gc))
   115 
   116             val session_entries = data.getOrElse(name, Map.empty)
   117             val entries = session_entries.getOrElse(session, Nil)
   118             data += (name -> (session_entries + (session -> (entry :: entries))))
   119           }
   120         })
   121       }
   122     })
   123 
   124     for {
   125       (name, session_entries) <- data
   126       session_entries1 <-
   127         {
   128           val session_entries1 =
   129             for {
   130               (session, entries) <- session_entries
   131               if entries.filter(_.check(elapsed_threshold)).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   private val html_header = """<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
   143 <html>
   144 <head><title>Build status</title></head>
   145 <body>
   146 """
   147   private val html_footer = """
   148 </body>
   149 </html>
   150 """
   151 
   152   def present_data(data: Data,
   153     progress: Progress = No_Progress,
   154     target_dir: Path = default_target_dir,
   155     image_size: (Int, Int) = default_image_size,
   156     ml_timing: Option[Boolean] = None)
   157   {
   158     val data_entries = data.toList.sortBy(_._1)
   159     for ((name, session_entries) <- data_entries) {
   160       val dir = target_dir + Path.explode(name)
   161       progress.echo("output " + dir)
   162       Isabelle_System.mkdirs(dir)
   163 
   164       for ((session, entries) <- session_entries) {
   165         Isabelle_System.with_tmp_file(session, "data") { data_file =>
   166           Isabelle_System.with_tmp_file(session, "gnuplot") { gnuplot_file =>
   167 
   168             File.write(data_file,
   169               cat_lines(
   170                 entries.map(entry =>
   171                   List(entry.date.unix_epoch.toString,
   172                     entry.timing.elapsed.minutes,
   173                     entry.timing.cpu.minutes,
   174                     entry.ml_timing.elapsed.minutes,
   175                     entry.ml_timing.cpu.minutes,
   176                     entry.ml_timing.gc.minutes).mkString(" "))))
   177 
   178             val plots1 =
   179               List(
   180                 """ using 1:3 smooth sbezier title "cpu time (smooth)" """,
   181                 """ using 1:3 smooth csplines title "cpu time" """,
   182                 """ using 1:2 smooth sbezier title "elapsed time (smooth)" """,
   183                 """ using 1:2 smooth csplines title "elapsed time" """)
   184             val plots2 =
   185               List(
   186                 """ using 1:5 smooth sbezier title "ML cpu time (smooth)" """,
   187                 """ using 1:5 smooth csplines title "ML cpu time" """,
   188                 """ using 1:4 smooth sbezier title "ML elapsed time (smooth)" """,
   189                 """ using 1:4 smooth csplines title "ML elapsed time" """,
   190                 """ using 1:6 smooth sbezier title "ML gc time (smooth)" """,
   191                 """ using 1:6 smooth csplines title "ML gc time" """)
   192             val plots =
   193               ml_timing match {
   194                 case None => plots1
   195                 case Some(false) => plots1 ::: plots2
   196                 case Some(true) => plots2
   197               }
   198 
   199             File.write(gnuplot_file, """
   200 set terminal png size """ + image_size._1 + "," + image_size._2 + """
   201 set output """ + quote(File.standard_path(dir + Path.basic(session + ".png"))) + """
   202 set xdata time
   203 set timefmt "%s"
   204 set format x "%d-%b"
   205 set xlabel """ + quote(session) + """ noenhanced
   206 set key left top
   207 plot [] [0:] """ + plots.map(s => quote(data_file.implode) + " " + s).mkString(", ") + "\n")
   208 
   209             val result =
   210               Isabelle_System.bash("\"$ISABELLE_GNUPLOT\" " + File.bash_path(gnuplot_file))
   211             if (result.rc != 0)
   212               result.error("Gnuplot failed for " + name + "/" + session).check
   213           }
   214         }
   215       }
   216 
   217       File.write(dir + Path.basic("index.html"),
   218         html_header + "\n<h1>" + HTML.output(name) + "</h1>\n" +
   219         cat_lines(
   220           session_entries.toList.map(_._1).sorted.map(session =>
   221             """<br/><img src=""" + quote(HTML.output(session + ".png")) + """><br/>""")) +
   222         "\n" + html_footer)
   223     }
   224 
   225     File.write(target_dir + Path.basic("index.html"),
   226       html_header + "\n<ul>\n" +
   227       cat_lines(
   228         data_entries.map(_._1).map(name =>
   229           """<li> <a href=""" + quote(HTML.output(name + "/index.html")) + """>""" +
   230             HTML.output(name) + """</a> </li>""")) +
   231       "\n</ul>\n" + html_footer)
   232   }
   233 
   234 
   235   /* Isabelle tool wrapper */
   236 
   237   val isabelle_tool =
   238     Isabelle_Tool("build_status", "present recent build status information from database", args =>
   239     {
   240       var target_dir = default_target_dir
   241       var ml_timing: Option[Boolean] = None
   242       var only_sessions = Set.empty[String]
   243       var elapsed_threshold = Time.zero
   244       var history_length = default_history_length
   245       var options = Options.init()
   246       var image_size = default_image_size
   247       var verbose = false
   248 
   249       val getopts = Getopts("""
   250 Usage: isabelle build_status [OPTIONS]
   251 
   252   Options are:
   253     -D DIR       target directory (default """ + default_target_dir + """)
   254     -M           only ML timing
   255     -S SESSIONS  only given SESSIONS (comma separated)
   256     -T THRESHOLD only sessions with elapsed time >= THRESHOLD (minutes)
   257     -l LENGTH    length of history (default """ + default_history_length + """)
   258     -m           include ML timing
   259     -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
   260     -s WxH       size of PNG image (default """ + image_size._1 + "x" + image_size._2 + """)
   261     -v           verbose
   262 
   263   Present performance statistics from build log database, which is specified
   264   via system options build_log_database_host, build_log_database_user etc.
   265 """,
   266         "D:" -> (arg => target_dir = Path.explode(arg)),
   267         "M" -> (_ => ml_timing = Some(true)),
   268         "S:" -> (arg => only_sessions = space_explode(',', arg).toSet),
   269         "T:" -> (arg => elapsed_threshold = Time.minutes(Value.Double.parse(arg))),
   270         "l:" -> (arg => history_length = Value.Int.parse(arg)),
   271         "m" -> (_ => ml_timing = Some(false)),
   272         "o:" -> (arg => options = options + arg),
   273         "s:" -> (arg =>
   274           space_explode('x', arg).map(Value.Int.parse(_)) match {
   275             case List(w, h) if w > 0 && h > 0 => image_size = (w, h)
   276             case _ => error("Error bad PNG image size: " + quote(arg))
   277           }),
   278         "v" -> (_ => verbose = true))
   279 
   280       val more_args = getopts(args)
   281       if (more_args.nonEmpty) getopts.usage()
   282 
   283       val progress = new Console_Progress
   284 
   285       val data =
   286         read_data(options, progress = progress, history_length = history_length,
   287           only_sessions = only_sessions, elapsed_threshold = elapsed_threshold, verbose = verbose)
   288 
   289       present_data(data, progress = progress, target_dir = target_dir,
   290         image_size = image_size, ml_timing = ml_timing)
   291 
   292   }, admin = true)
   293 }