src/Pure/Tools/build.ML
author wenzelm
Sun Dec 18 15:53:27 2016 +0100 (2016-12-18 ago)
changeset 64599 80ef54198f44
parent 64308 b00508facb4f
child 65058 3e9f382fb67e
permissions -rw-r--r--
dummy fork to produce ML_statistics even in sequential mode (e.g. for heap size);
wenzelm@50686
     1
(*  Title:      Pure/Tools/build.ML
wenzelm@48418
     2
    Author:     Makarius
wenzelm@48418
     3
wenzelm@48418
     4
Build Isabelle sessions.
wenzelm@48418
     5
*)
wenzelm@48418
     6
wenzelm@48418
     7
signature BUILD =
wenzelm@48418
     8
sig
wenzelm@48731
     9
  val build: string -> unit
wenzelm@48418
    10
end;
wenzelm@48418
    11
wenzelm@48418
    12
structure Build: BUILD =
wenzelm@48418
    13
struct
wenzelm@48418
    14
wenzelm@51662
    15
(* command timings *)
wenzelm@51662
    16
wenzelm@56615
    17
type timings = ((string * Time.time) Inttab.table) Symtab.table;  (*file -> offset -> name, time*)
wenzelm@51662
    18
wenzelm@51662
    19
val empty_timings: timings = Symtab.empty;
wenzelm@51662
    20
wenzelm@51662
    21
fun update_timings props =
wenzelm@51662
    22
  (case Markup.parse_command_timing_properties props of
wenzelm@51662
    23
    SOME ({file, offset, name}, time) =>
wenzelm@51666
    24
      Symtab.map_default (file, Inttab.empty)
wenzelm@62826
    25
        (Inttab.map_default (offset, (name, time)) (fn (_, t) => (name, t + time)))
wenzelm@51662
    26
  | NONE => I);
wenzelm@51662
    27
wenzelm@51662
    28
fun approximative_id name pos =
wenzelm@51662
    29
  (case (Position.file_of pos, Position.offset_of pos) of
wenzelm@51662
    30
    (SOME file, SOME offset) =>
wenzelm@51662
    31
      if name = "" then NONE else SOME {file = file, offset = offset, name = name}
wenzelm@51662
    32
  | _ => NONE);
wenzelm@51662
    33
wenzelm@51662
    34
fun lookup_timings timings tr =
wenzelm@51662
    35
  (case approximative_id (Toplevel.name_of tr) (Toplevel.pos_of tr) of
wenzelm@51662
    36
    SOME {file, offset, name} =>
wenzelm@51662
    37
      (case Symtab.lookup timings file of
wenzelm@51662
    38
        SOME offsets =>
wenzelm@51662
    39
          (case Inttab.lookup offsets offset of
wenzelm@51662
    40
            SOME (name', time) => if name = name' then SOME time else NONE
wenzelm@51662
    41
          | NONE => NONE)
wenzelm@51662
    42
      | NONE => NONE)
wenzelm@51662
    43
  | NONE => NONE);
wenzelm@51662
    44
wenzelm@51662
    45
wenzelm@52052
    46
(* session timing *)
wenzelm@52052
    47
wenzelm@52052
    48
fun session_timing name verbose f x =
wenzelm@52052
    49
  let
wenzelm@52052
    50
    val start = Timing.start ();
wenzelm@52052
    51
    val y = f x;
wenzelm@52052
    52
    val timing = Timing.result start;
wenzelm@52052
    53
wenzelm@62925
    54
    val threads = string_of_int (Multithreading.max_threads ());
wenzelm@52052
    55
    val factor = Time.toReal (#cpu timing) / Time.toReal (#elapsed timing)
wenzelm@52052
    56
      |> Real.fmt (StringCvt.FIX (SOME 2));
wenzelm@52052
    57
wenzelm@52052
    58
    val timing_props =
wenzelm@52052
    59
      [("threads", threads)] @ Markup.timing_properties timing @ [("factor", factor)];
wenzelm@52052
    60
    val _ = writeln ("\fTiming = " ^ YXML.string_of_body (XML.Encode.properties timing_props));
wenzelm@52052
    61
    val _ =
wenzelm@52052
    62
      if verbose then
wenzelm@52052
    63
        Output.physical_stderr ("Timing " ^ name ^ " (" ^
wenzelm@52052
    64
          threads ^ " threads, " ^ Timing.message timing ^ ", factor " ^ factor ^ ")\n")
wenzelm@52052
    65
      else ();
wenzelm@52052
    66
  in y end;
wenzelm@52052
    67
wenzelm@52052
    68
wenzelm@50683
    69
(* protocol messages *)
wenzelm@50683
    70
wenzelm@51662
    71
fun inline_message a args =
wenzelm@51662
    72
  writeln ("\f" ^ a ^ " = " ^ YXML.string_of_body (XML.Encode.properties args));
wenzelm@50683
    73
wenzelm@50683
    74
fun protocol_message props output =
wenzelm@51216
    75
  (case props of
wenzelm@51216
    76
    function :: args =>
wenzelm@51662
    77
      if function = Markup.ML_statistics orelse function = Markup.task_statistics then
wenzelm@51662
    78
        inline_message (#2 function) args
wenzelm@51662
    79
      else if function = Markup.command_timing then
wenzelm@51662
    80
        let
wenzelm@51662
    81
          val name = the_default "" (Properties.get args Markup.nameN);
wenzelm@51662
    82
          val pos = Position.of_properties args;
wenzelm@51662
    83
          val {elapsed, ...} = Markup.parse_timing_properties args;
wenzelm@62793
    84
          val is_significant =
wenzelm@62793
    85
            Timing.is_relevant_time elapsed andalso
wenzelm@62826
    86
            elapsed >= Options.default_seconds "command_timing_threshold";
wenzelm@51662
    87
        in
wenzelm@62793
    88
          if is_significant then
wenzelm@59149
    89
            (case approximative_id name pos of
wenzelm@59149
    90
              SOME id => inline_message (#2 function) (Markup.command_timing_properties id elapsed)
wenzelm@59149
    91
            | NONE => ())
wenzelm@59149
    92
          else ()
wenzelm@51662
    93
        end
wenzelm@51216
    94
      else
wenzelm@51216
    95
        (case Markup.dest_loading_theory props of
wenzelm@51216
    96
          SOME name => writeln ("\floading_theory = " ^ name)
wenzelm@51661
    97
        | NONE => raise Output.Protocol_Message props)
wenzelm@51661
    98
  | [] => raise Output.Protocol_Message props);
wenzelm@51045
    99
wenzelm@50683
   100
wenzelm@50683
   101
(* build *)
wenzelm@50683
   102
wenzelm@61381
   103
fun build_theories symbols last_timing master_dir (options, thys) =
wenzelm@59366
   104
  let
wenzelm@59366
   105
    val condition = space_explode "," (Options.string options "condition");
wenzelm@59366
   106
    val conds = filter_out (can getenv_strict) condition;
wenzelm@59366
   107
  in
wenzelm@59366
   108
    if null conds then
wenzelm@63827
   109
      (if Options.bool options "checkpoint" then ML_Heap.share_common_data () else ();
wenzelm@63827
   110
        Options.set_default options;
wenzelm@62714
   111
        Isabelle_Process.init_options ();
wenzelm@64599
   112
        Future.fork I;
wenzelm@59366
   113
        (Thy_Info.use_theories {
wenzelm@59366
   114
          document = Present.document_enabled (Options.string options "document"),
wenzelm@61381
   115
          symbols = symbols,
wenzelm@59366
   116
          last_timing = last_timing,
wenzelm@59366
   117
          master_dir = master_dir}
wenzelm@64308
   118
        |>
wenzelm@64308
   119
          (case Options.string options "profiling" of
wenzelm@64308
   120
            "" => I
wenzelm@64308
   121
          | "time" => profile_time
wenzelm@64308
   122
          | "allocations" => profile_allocations
wenzelm@64308
   123
          | bad => error ("Bad profiling option: " ^ quote bad))
wenzelm@59366
   124
        |> Unsynchronized.setmp print_mode
wenzelm@62714
   125
            (space_explode "," (Options.string options "print_mode") @ print_mode_value ())) thys)
wenzelm@59366
   126
    else
wenzelm@59366
   127
      Output.physical_stderr ("Skipping theories " ^ commas_quote (map #1 thys) ^
wenzelm@59366
   128
        " (undefined " ^ commas conds ^ ")\n")
wenzelm@59366
   129
  end;
wenzelm@59366
   130
wenzelm@62630
   131
fun build args_file =
wenzelm@62630
   132
  let
wenzelm@62666
   133
    val _ = SHA1.test_samples ();
wenzelm@53212
   134
wenzelm@62630
   135
    val (symbol_codes, (command_timings, (do_output, (verbose, (browser_info,
wenzelm@62630
   136
          (document_files, (graph_file, (parent_name, (chapter, (name, theories)))))))))) =
wenzelm@62630
   137
      File.read (Path.explode args_file) |> YXML.parse_body |>
wenzelm@62630
   138
        let open XML.Decode in
wenzelm@62630
   139
          pair (list (pair string int)) (pair (list properties) (pair bool
wenzelm@62630
   140
            (pair bool (pair string (pair (list (pair string string)) (pair string
wenzelm@62630
   141
              (pair string (pair string (pair string
wenzelm@62630
   142
              ((list (pair Options.decode (list (string #> rpair Position.none))))))))))))))
wenzelm@62630
   143
        end;
wenzelm@48418
   144
wenzelm@62630
   145
    val symbols = HTML.make_symbols symbol_codes;
wenzelm@62713
   146
    val _ = Options.load_default ();
wenzelm@62713
   147
    val _ = Isabelle_Process.init_options ();
wenzelm@51941
   148
wenzelm@62630
   149
    val _ = writeln ("\fSession.name = " ^ name);
wenzelm@62630
   150
    val _ =
wenzelm@62630
   151
      Session.init
wenzelm@62630
   152
        symbols
wenzelm@62630
   153
        do_output
wenzelm@62630
   154
        (Options.default_bool "browser_info")
wenzelm@62630
   155
        (Path.explode browser_info)
wenzelm@62630
   156
        (Options.default_string "document")
wenzelm@62630
   157
        (Options.default_string "document_output")
wenzelm@62630
   158
        (Present.document_variants (Options.default_string "document_variants"))
wenzelm@62630
   159
        (map (apply2 Path.explode) document_files)
wenzelm@62630
   160
        (Path.explode graph_file)
wenzelm@62630
   161
        parent_name (chapter, name)
wenzelm@62630
   162
        verbose;
wenzelm@49911
   163
wenzelm@62630
   164
    val last_timing = lookup_timings (fold update_timings command_timings empty_timings);
wenzelm@51218
   165
wenzelm@62630
   166
    val res1 =
wenzelm@62630
   167
      theories |>
wenzelm@62630
   168
        (List.app (build_theories symbols last_timing Path.current)
wenzelm@62630
   169
          |> session_timing name verbose
wenzelm@62930
   170
          |> Unsynchronized.setmp Private_Output.protocol_message_fn protocol_message
wenzelm@62630
   171
          |> Exn.capture);
wenzelm@62630
   172
    val res2 = Exn.capture Session.finish ();
wenzelm@62630
   173
    val _ = Par_Exn.release_all [res1, res2];
wenzelm@48673
   174
wenzelm@62630
   175
    val _ = Options.reset_default ();
wenzelm@62630
   176
  in () end;
wenzelm@48418
   177
wenzelm@59366
   178
wenzelm@59366
   179
(* PIDE protocol *)
wenzelm@59366
   180
wenzelm@59366
   181
val _ =
wenzelm@59366
   182
  Isabelle_Process.protocol_command "build_theories"
wenzelm@61381
   183
    (fn [id, symbol_codes_yxml, master_dir, theories_yxml] =>
wenzelm@59366
   184
      let
wenzelm@61381
   185
        val symbols =
wenzelm@61381
   186
          YXML.parse_body symbol_codes_yxml
wenzelm@61381
   187
          |> let open XML.Decode in list (pair string int) end
wenzelm@61381
   188
          |> HTML.make_symbols;
wenzelm@59366
   189
        val theories =
wenzelm@59366
   190
          YXML.parse_body theories_yxml |>
wenzelm@59366
   191
            let open XML.Decode
wenzelm@59366
   192
            in list (pair Options.decode (list (string #> rpair Position.none))) end;
wenzelm@59369
   193
        val res1 =
wenzelm@59366
   194
          Exn.capture (fn () =>
wenzelm@61381
   195
            theories |> List.app (build_theories symbols (K NONE) (Path.explode master_dir))) ();
wenzelm@59369
   196
        val res2 = Exn.capture Session.shutdown ();
wenzelm@59369
   197
        val result =
wenzelm@59369
   198
          (Par_Exn.release_all [res1, res2]; "") handle exn =>
wenzelm@59369
   199
            (Runtime.exn_message exn handle _ (*sic!*) =>
wenzelm@59369
   200
              "Exception raised, but failed to print details!");
wenzelm@59369
   201
    in Output.protocol_message (Markup.build_theories_result id) [result] end);
wenzelm@59366
   202
wenzelm@48418
   203
end;