src/Pure/Admin/build_status.scala
author wenzelm
Sun May 07 17:10:03 2017 +0200 (2017-05-07)
changeset 65758 d79126bb5d07
parent 65757 a6522bb9acfa
child 65759 a2b041a36523
permissions -rw-r--r--
removed threshold: redundant due to sorting;
     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     ml_timing: Option[Boolean] = None)
   147   {
   148     val data_entries = data.sorted_entries
   149 
   150     for ((data_name, session_entries) <- data_entries) {
   151       val dir = target_dir + Path.explode(data_name)
   152       progress.echo("output " + dir)
   153       Isabelle_System.mkdirs(dir)
   154 
   155       Par_List.map[(String, List[isabelle.Build_Status.Entry]), Process_Result](
   156         { case (session, entries) =>
   157           Isabelle_System.with_tmp_file(session, "data") { data_file =>
   158             Isabelle_System.with_tmp_file(session, "gnuplot") { gnuplot_file =>
   159 
   160               File.write(data_file,
   161                 cat_lines(
   162                   entries.map(entry =>
   163                     List(entry.date.unix_epoch.toString,
   164                       entry.timing.elapsed.minutes,
   165                       entry.timing.cpu.minutes,
   166                       entry.ml_timing.elapsed.minutes,
   167                       entry.ml_timing.cpu.minutes,
   168                       entry.ml_timing.gc.minutes).mkString(" "))))
   169 
   170               val plots1 =
   171                 List(
   172                   """ using 1:3 smooth sbezier title "cpu time (smooth)" """,
   173                   """ using 1:3 smooth csplines title "cpu time" """,
   174                   """ using 1:2 smooth sbezier title "elapsed time (smooth)" """,
   175                   """ using 1:2 smooth csplines title "elapsed time" """)
   176               val plots2 =
   177                 List(
   178                   """ using 1:5 smooth sbezier title "ML cpu time (smooth)" """,
   179                   """ using 1:5 smooth csplines title "ML cpu time" """,
   180                   """ using 1:4 smooth sbezier title "ML elapsed time (smooth)" """,
   181                   """ using 1:4 smooth csplines title "ML elapsed time" """,
   182                   """ using 1:6 smooth sbezier title "ML gc time (smooth)" """,
   183                   """ using 1:6 smooth csplines title "ML gc time" """)
   184               val plots =
   185                 ml_timing match {
   186                   case None => plots1
   187                   case Some(false) => plots1 ::: plots2
   188                   case Some(true) => plots2
   189                 }
   190 
   191               File.write(gnuplot_file, """
   192 set terminal png size """ + image_size._1 + "," + image_size._2 + """
   193 set output """ + quote(File.standard_path(dir + Path.basic(session + ".png"))) + """
   194 set xdata time
   195 set timefmt "%s"
   196 set format x "%d-%b"
   197 set xlabel """ + quote(session) + """ noenhanced
   198 set key left top
   199 plot [] [0:] """ + plots.map(s => quote(data_file.implode) + " " + s).mkString(", ") + "\n")
   200 
   201               val result =
   202                 Isabelle_System.bash("\"$ISABELLE_GNUPLOT\" " + File.bash_path(gnuplot_file))
   203               if (result.ok) result
   204               else result.error("Gnuplot failed for " + data_name + "/" + session)
   205             }
   206           }
   207         }, session_entries).foreach(_.check)
   208 
   209       val sessions = session_entries.toList.sortBy(_._2.head.timing.elapsed.ms).reverse
   210       val heading = "Build status for " + data_name + " (" + data.date + ")"
   211 
   212       File.write(dir + Path.basic("index.html"),
   213         HTML.output_document(
   214           List(HTML.title(heading)),
   215           HTML.chapter(heading) ::
   216           HTML.itemize(
   217             sessions.map({ case (name, entries) =>
   218               HTML.link("#session_" + name, HTML.text(name)) ::
   219               HTML.text(" (" + entries.head.timing.message_resources + ")") })) ::
   220           sessions.flatMap({ case (name, entries) =>
   221             List(
   222               HTML.section(name) + HTML.id("session_" + name),
   223               HTML.par(
   224                 List(
   225                   HTML.itemize(List(
   226                     HTML.bold(HTML.text("timing: ")) ::
   227                       HTML.text(entries.head.timing.message_resources),
   228                     HTML.bold(HTML.text("ML timing: ")) ::
   229                       HTML.text(entries.head.ml_timing.message_resources))),
   230                   HTML.image(name + ".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 ml_timing: Option[Boolean] = None
   251       var only_sessions = Set.empty[String]
   252       var history_length = default_history_length
   253       var options = Options.init()
   254       var image_size = default_image_size
   255       var verbose = false
   256 
   257       val getopts = Getopts("""
   258 Usage: isabelle build_status [OPTIONS]
   259 
   260   Options are:
   261     -D DIR       target directory (default """ + default_target_dir + """)
   262     -M           only ML timing
   263     -S SESSIONS  only given SESSIONS (comma separated)
   264     -l LENGTH    length of history (default """ + default_history_length + """)
   265     -m           include ML timing
   266     -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
   267     -s WxH       size of PNG image (default """ + image_size._1 + "x" + image_size._2 + """)
   268     -v           verbose
   269 
   270   Present performance statistics from build log database, which is specified
   271   via system options build_log_database_host, build_log_database_user etc.
   272 """,
   273         "D:" -> (arg => target_dir = Path.explode(arg)),
   274         "M" -> (_ => ml_timing = Some(true)),
   275         "S:" -> (arg => only_sessions = space_explode(',', arg).toSet),
   276         "l:" -> (arg => history_length = Value.Int.parse(arg)),
   277         "m" -> (_ => ml_timing = Some(false)),
   278         "o:" -> (arg => options = options + arg),
   279         "s:" -> (arg =>
   280           space_explode('x', arg).map(Value.Int.parse(_)) match {
   281             case List(w, h) if w > 0 && h > 0 => image_size = (w, h)
   282             case _ => error("Error bad PNG image size: " + quote(arg))
   283           }),
   284         "v" -> (_ => verbose = true))
   285 
   286       val more_args = getopts(args)
   287       if (more_args.nonEmpty) getopts.usage()
   288 
   289       val progress = new Console_Progress
   290 
   291       val data =
   292         read_data(options, progress = progress, history_length = history_length,
   293           only_sessions = only_sessions, verbose = verbose)
   294 
   295       present_data(data, progress = progress, target_dir = target_dir,
   296         image_size = image_size, ml_timing = ml_timing)
   297 
   298   }, admin = true)
   299 }