src/Pure/Admin/build_status.scala
changeset 65743 4847ca570454
parent 65742 b9e0f25ba16a
child 65745 a124fbf8b2b9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/Admin/build_status.scala	Sat May 06 19:23:33 2017 +0200
     1.3 @@ -0,0 +1,278 @@
     1.4 +/*  Title:      Pure/Admin/build_status.scala
     1.5 +    Author:     Makarius
     1.6 +
     1.7 +Present recent build status information from database.
     1.8 +*/
     1.9 +
    1.10 +package isabelle
    1.11 +
    1.12 +
    1.13 +object Build_Status
    1.14 +{
    1.15 +  private val default_target_dir = Path.explode("build_status")
    1.16 +  private val default_history_length = 30
    1.17 +  private val default_image_size = (800, 600)
    1.18 +
    1.19 +
    1.20 +  /* data profiles */
    1.21 +
    1.22 +  sealed case class Profile(name: String, sql: String)
    1.23 +  {
    1.24 +    def select(columns: List[SQL.Column], days: Int, only_sessions: Set[String]): SQL.Source =
    1.25 +    {
    1.26 +      val sql_sessions =
    1.27 +        if (only_sessions.isEmpty) ""
    1.28 +        else
    1.29 +          only_sessions.iterator.map(a => Build_Log.Data.session_name + " = " + SQL.string(a))
    1.30 +            .mkString("(", " OR ", ") AND ")
    1.31 +
    1.32 +      Build_Log.Data.universal_table.select(columns, distinct = true,
    1.33 +        sql = "WHERE " +
    1.34 +          Build_Log.Data.pull_date + " > " + Build_Log.Data.recent_time(days) + " AND " +
    1.35 +          Build_Log.Data.status + " = " + SQL.string(Build_Log.Session_Status.finished.toString) +
    1.36 +          " AND " + sql_sessions + SQL.enclose(sql) +
    1.37 +          " ORDER BY " + Build_Log.Data.pull_date + " DESC")
    1.38 +    }
    1.39 +  }
    1.40 +
    1.41 +  val standard_profiles: List[Profile] =
    1.42 +    Jenkins.build_log_profiles :::
    1.43 +    Isabelle_Cronjob.remote_builds.flatten.toList.map(r => Profile(r.name, r.sql))
    1.44 +
    1.45 +  sealed case class Entry(date: Date, timing: Timing, ml_timing: Timing)
    1.46 +  {
    1.47 +    def check(elapsed_threshold: Time): Boolean =
    1.48 +      !timing.is_zero && timing.elapsed >= elapsed_threshold
    1.49 +  }
    1.50 +
    1.51 +  type Data = Map[String, Map[String, List[Entry]]]
    1.52 +
    1.53 +
    1.54 +  /* read data */
    1.55 +
    1.56 +  def read_data(options: Options,
    1.57 +    profiles: List[Profile] = standard_profiles,
    1.58 +    progress: Progress = No_Progress,
    1.59 +    history_length: Int = default_history_length,
    1.60 +    only_sessions: Set[String] = Set.empty,
    1.61 +    elapsed_threshold: Time = Time.zero): Data =
    1.62 +  {
    1.63 +    var data: Data = Map.empty
    1.64 +
    1.65 +    val store = Build_Log.store(options)
    1.66 +    using(store.open_database())(db =>
    1.67 +    {
    1.68 +      for (profile <- profiles) {
    1.69 +        progress.echo("input " + quote(profile.name))
    1.70 +        val columns =
    1.71 +          List(
    1.72 +            Build_Log.Data.pull_date,
    1.73 +            Build_Log.Settings.ML_PLATFORM,
    1.74 +            Build_Log.Data.session_name,
    1.75 +            Build_Log.Data.threads,
    1.76 +            Build_Log.Data.timing_elapsed,
    1.77 +            Build_Log.Data.timing_cpu,
    1.78 +            Build_Log.Data.timing_gc,
    1.79 +            Build_Log.Data.ml_timing_elapsed,
    1.80 +            Build_Log.Data.ml_timing_cpu,
    1.81 +            Build_Log.Data.ml_timing_gc)
    1.82 +
    1.83 +        db.using_statement(profile.select(columns, history_length, only_sessions))(stmt =>
    1.84 +        {
    1.85 +          val res = stmt.execute_query()
    1.86 +          while (res.next()) {
    1.87 +            val ml_platform = res.string(Build_Log.Settings.ML_PLATFORM)
    1.88 +            val threads = res.get_int(Build_Log.Data.threads)
    1.89 +            val name =
    1.90 +              profile.name +
    1.91 +                "_m" + (if (ml_platform.startsWith("x86_64")) "64" else "32") +
    1.92 +                "_M" + threads.getOrElse(1)
    1.93 +
    1.94 +            val session = res.string(Build_Log.Data.session_name)
    1.95 +            val entry =
    1.96 +              Entry(res.date(Build_Log.Data.pull_date),
    1.97 +                res.timing(
    1.98 +                  Build_Log.Data.timing_elapsed,
    1.99 +                  Build_Log.Data.timing_cpu,
   1.100 +                  Build_Log.Data.timing_gc),
   1.101 +                res.timing(
   1.102 +                  Build_Log.Data.ml_timing_elapsed,
   1.103 +                  Build_Log.Data.ml_timing_cpu,
   1.104 +                  Build_Log.Data.ml_timing_gc))
   1.105 +
   1.106 +            val session_entries = data.getOrElse(name, Map.empty)
   1.107 +            val entries = session_entries.getOrElse(session, Nil)
   1.108 +            data += (name -> (session_entries + (session -> (entry :: entries))))
   1.109 +          }
   1.110 +        })
   1.111 +      }
   1.112 +    })
   1.113 +
   1.114 +    for {
   1.115 +      (name, session_entries) <- data
   1.116 +      session_entries1 <-
   1.117 +        {
   1.118 +          val session_entries1 =
   1.119 +            for {
   1.120 +              (session, entries) <- session_entries
   1.121 +              if entries.filter(_.check(elapsed_threshold)).length >= 3
   1.122 +            } yield (session, entries)
   1.123 +          if (session_entries1.isEmpty) None
   1.124 +          else Some(session_entries1)
   1.125 +        }
   1.126 +    } yield (name, session_entries1)
   1.127 +  }
   1.128 +
   1.129 +
   1.130 +  /* present data */
   1.131 +
   1.132 +  private val html_header = """<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
   1.133 +<html>
   1.134 +<head><title>Build status</title></head>
   1.135 +<body>
   1.136 +"""
   1.137 +  private val html_footer = """
   1.138 +</body>
   1.139 +</html>
   1.140 +"""
   1.141 +
   1.142 +  def present_data(data: Data,
   1.143 +    progress: Progress = No_Progress,
   1.144 +    target_dir: Path = default_target_dir,
   1.145 +    image_size: (Int, Int) = default_image_size,
   1.146 +    ml_timing: Option[Boolean] = None)
   1.147 +  {
   1.148 +    val data_entries = data.toList.sortBy(_._1)
   1.149 +    for ((name, session_entries) <- data_entries) {
   1.150 +      val dir = target_dir + Path.explode(name)
   1.151 +      progress.echo("output " + dir)
   1.152 +      Isabelle_System.mkdirs(dir)
   1.153 +
   1.154 +      for ((session, entries) <- session_entries) {
   1.155 +        Isabelle_System.with_tmp_file(session, "data") { data_file =>
   1.156 +          Isabelle_System.with_tmp_file(session, "gnuplot") { gnuplot_file =>
   1.157 +
   1.158 +            File.write(data_file,
   1.159 +              cat_lines(
   1.160 +                entries.map(entry =>
   1.161 +                  List(entry.date.unix_epoch.toString,
   1.162 +                    entry.timing.elapsed.minutes,
   1.163 +                    entry.timing.cpu.minutes,
   1.164 +                    entry.ml_timing.elapsed.minutes,
   1.165 +                    entry.ml_timing.cpu.minutes,
   1.166 +                    entry.ml_timing.gc.minutes).mkString(" "))))
   1.167 +
   1.168 +            val plots1 =
   1.169 +              List(
   1.170 +                """ using 1:3 smooth sbezier title "cpu time (smooth)" """,
   1.171 +                """ using 1:3 smooth csplines title "cpu time" """,
   1.172 +                """ using 1:2 smooth sbezier title "elapsed time (smooth)" """,
   1.173 +                """ using 1:2 smooth csplines title "elapsed time" """)
   1.174 +            val plots2 =
   1.175 +              List(
   1.176 +                """ using 1:5 smooth sbezier title "ML cpu time (smooth)" """,
   1.177 +                """ using 1:5 smooth csplines title "ML cpu time" """,
   1.178 +                """ using 1:4 smooth sbezier title "ML elapsed time (smooth)" """,
   1.179 +                """ using 1:4 smooth csplines title "ML elapsed time" """,
   1.180 +                """ using 1:6 smooth sbezier title "ML gc time (smooth)" """,
   1.181 +                """ using 1:6 smooth csplines title "ML gc time" """)
   1.182 +            val plots =
   1.183 +              ml_timing match {
   1.184 +                case None => plots1
   1.185 +                case Some(false) => plots1 ::: plots2
   1.186 +                case Some(true) => plots2
   1.187 +              }
   1.188 +
   1.189 +            File.write(gnuplot_file, """
   1.190 +set terminal png size """ + image_size._1 + "," + image_size._2 + """
   1.191 +set output """ + quote(File.standard_path(dir + Path.basic(session + ".png"))) + """
   1.192 +set xdata time
   1.193 +set timefmt "%s"
   1.194 +set format x "%d-%b"
   1.195 +set xlabel """ + quote(session) + """ noenhanced
   1.196 +set key left top
   1.197 +plot [] [0:] """ + plots.map(s => quote(data_file.implode) + " " + s).mkString(", ") + "\n")
   1.198 +
   1.199 +            val result =
   1.200 +              Isabelle_System.bash("\"$ISABELLE_GNUPLOT\" " + File.bash_path(gnuplot_file))
   1.201 +            if (result.rc != 0)
   1.202 +              result.error("Gnuplot failed for " + name + "/" + session).check
   1.203 +          }
   1.204 +        }
   1.205 +      }
   1.206 +
   1.207 +      File.write(dir + Path.basic("index.html"),
   1.208 +        html_header + "\n<h1>" + HTML.output(name) + "</h1>\n" +
   1.209 +        cat_lines(
   1.210 +          session_entries.toList.map(_._1).sorted.map(session =>
   1.211 +            """<br/><img src=""" + quote(HTML.output(session + ".png")) + """><br/>""")) +
   1.212 +        "\n" + html_footer)
   1.213 +    }
   1.214 +
   1.215 +    File.write(target_dir + Path.basic("index.html"),
   1.216 +      html_header + "\n<ul>\n" +
   1.217 +      cat_lines(
   1.218 +        data_entries.map(_._1).map(name =>
   1.219 +          """<li> <a href=""" + quote(HTML.output(name + "/index.html")) + """>""" +
   1.220 +            HTML.output(name) + """</a> </li>""")) +
   1.221 +      "\n</ul>\n" + html_footer)
   1.222 +  }
   1.223 +
   1.224 +
   1.225 +  /* Isabelle tool wrapper */
   1.226 +
   1.227 +  val isabelle_tool =
   1.228 +    Isabelle_Tool("build_status", "present recent build status information from database", args =>
   1.229 +    {
   1.230 +      var target_dir = default_target_dir
   1.231 +      var ml_timing: Option[Boolean] = None
   1.232 +      var only_sessions = Set.empty[String]
   1.233 +      var elapsed_threshold = Time.zero
   1.234 +      var history_length = default_history_length
   1.235 +      var options = Options.init()
   1.236 +      var image_size = default_image_size
   1.237 +
   1.238 +      val getopts = Getopts("""
   1.239 +Usage: isabelle build_status [OPTIONS]
   1.240 +
   1.241 +  Options are:
   1.242 +    -D DIR       target directory (default """ + default_target_dir + """)
   1.243 +    -M           only ML timing
   1.244 +    -S SESSIONS  only given SESSIONS (comma separated)
   1.245 +    -T THRESHOLD only sessions with elapsed time >= THRESHOLD (minutes)
   1.246 +    -l LENGTH    length of history (default """ + default_history_length + """)
   1.247 +    -m           include ML timing
   1.248 +    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
   1.249 +    -s WxH       size of PNG image (default """ + image_size._1 + "x" + image_size._2 + """)
   1.250 +
   1.251 +  Present performance statistics from build log database, which is specified
   1.252 +  via system options build_log_database_host, build_log_database_user etc.
   1.253 +""",
   1.254 +        "D:" -> (arg => target_dir = Path.explode(arg)),
   1.255 +        "M" -> (_ => ml_timing = Some(true)),
   1.256 +        "S:" -> (arg => only_sessions = space_explode(',', arg).toSet),
   1.257 +        "T:" -> (arg => elapsed_threshold = Time.minutes(Value.Double.parse(arg))),
   1.258 +        "l:" -> (arg => history_length = Value.Int.parse(arg)),
   1.259 +        "m" -> (_ => ml_timing = Some(false)),
   1.260 +        "o:" -> (arg => options = options + arg),
   1.261 +        "s:" -> (arg =>
   1.262 +          space_explode('x', arg).map(Value.Int.parse(_)) match {
   1.263 +            case List(w, h) if w > 0 && h > 0 => image_size = (w, h)
   1.264 +            case _ => error("Error bad PNG image size: " + quote(arg))
   1.265 +          }))
   1.266 +
   1.267 +      val more_args = getopts(args)
   1.268 +      if (more_args.nonEmpty) getopts.usage()
   1.269 +
   1.270 +      val progress = new Console_Progress
   1.271 +
   1.272 +      val data =
   1.273 +        read_data(options, profiles = standard_profiles, progress = progress,
   1.274 +          history_length = history_length, only_sessions = only_sessions,
   1.275 +          elapsed_threshold = elapsed_threshold)
   1.276 +
   1.277 +      present_data(data, progress = progress, target_dir = target_dir,
   1.278 +        image_size = image_size, ml_timing = ml_timing)
   1.279 +
   1.280 +  }, admin = true)
   1.281 +}