src/Pure/System/isabelle_process.ML
author wenzelm
Mon Mar 04 15:03:46 2013 +0100 (2013-03-04 ago)
changeset 51332 8707df0b0255
parent 51044 890f502f0e89
child 51423 e5f9a6d9ca82
permissions -rw-r--r--
refined parallel_proofs = 2: fork whole Isar sub-proofs, not just terminal ones;
refined parallel_proofs = 3: fork terminal proofs, as poor man's parallelization in interactive mode;
wenzelm@30173
     1
(*  Title:      Pure/System/isabelle_process.ML
wenzelm@25528
     2
    Author:     Makarius
wenzelm@25528
     3
wenzelm@38445
     4
Isabelle process wrapper, based on private fifos for maximum
wenzelm@48712
     5
robustness and performance, or local socket for maximum portability.
wenzelm@39528
     6
wenzelm@39528
     7
Startup phases:
wenzelm@48712
     8
  - raw Posix process startup with uncontrolled output on stdout/stderr
wenzelm@48712
     9
  - stderr \002: ML running
wenzelm@48712
    10
  - stdin/stdout/stderr freely available (raw ML loop)
wenzelm@48712
    11
  - protocol thread initialization
wenzelm@48712
    12
  - rendezvous on system channel
wenzelm@48712
    13
  - message INIT: channels ready
wenzelm@25528
    14
*)
wenzelm@25528
    15
wenzelm@25528
    16
signature ISABELLE_PROCESS =
wenzelm@25528
    17
sig
wenzelm@42897
    18
  val is_active: unit -> bool
wenzelm@46119
    19
  val protocol_command: string -> (string list -> unit) -> unit
wenzelm@50505
    20
  val tracing_messages: int Unsynchronized.ref;
wenzelm@50505
    21
  val reset_tracing: unit -> unit
wenzelm@43684
    22
  val crashes: exn list Synchronized.var
wenzelm@45029
    23
  val init_fifos: string -> string -> unit
wenzelm@45028
    24
  val init_socket: string -> unit
wenzelm@25528
    25
end;
wenzelm@25528
    26
wenzelm@31797
    27
structure Isabelle_Process: ISABELLE_PROCESS =
wenzelm@25528
    28
struct
wenzelm@25528
    29
wenzelm@42897
    30
(* print mode *)
wenzelm@25554
    31
wenzelm@25748
    32
val isabelle_processN = "isabelle_process";
wenzelm@25748
    33
wenzelm@42897
    34
fun is_active () = Print_Mode.print_mode_active isabelle_processN;
wenzelm@42897
    35
wenzelm@26550
    36
val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
wenzelm@28036
    37
val _ = Markup.add_mode isabelle_processN YXML.output_markup;
wenzelm@25841
    38
wenzelm@25841
    39
wenzelm@46119
    40
(* protocol commands *)
wenzelm@38270
    41
wenzelm@38270
    42
local
wenzelm@38270
    43
wenzelm@43684
    44
val commands =
wenzelm@43684
    45
  Synchronized.var "Isabelle_Process.commands" (Symtab.empty: (string list -> unit) Symtab.table);
wenzelm@38270
    46
wenzelm@38270
    47
in
wenzelm@38270
    48
wenzelm@46119
    49
fun protocol_command name cmd =
wenzelm@43684
    50
  Synchronized.change commands (fn cmds =>
wenzelm@38270
    51
   (if not (Symtab.defined cmds name) then ()
wenzelm@38270
    52
    else warning ("Redefining Isabelle process command " ^ quote name);
wenzelm@43684
    53
    Symtab.update (name, cmd) cmds));
wenzelm@38270
    54
wenzelm@46119
    55
fun run_command name args =
wenzelm@43684
    56
  (case Symtab.lookup (Synchronized.value commands) name of
wenzelm@38270
    57
    NONE => error ("Undefined Isabelle process command " ^ quote name)
wenzelm@46119
    58
  | SOME cmd =>
wenzelm@46119
    59
      (Runtime.debugging cmd args handle exn =>
wenzelm@48056
    60
        error ("Isabelle process protocol failure: " ^ quote name ^ "\n" ^
wenzelm@48056
    61
          ML_Compiler.exn_message exn)));
wenzelm@38270
    62
wenzelm@38270
    63
end;
wenzelm@38270
    64
wenzelm@38270
    65
wenzelm@50505
    66
(* restricted tracing messages *)
wenzelm@49647
    67
wenzelm@50505
    68
val tracing_messages = Unsynchronized.ref 100;
wenzelm@49647
    69
wenzelm@50505
    70
val command_tracing_messages =
wenzelm@50505
    71
  Synchronized.var "command_tracing_messages" (Inttab.empty: int Inttab.table);
wenzelm@49647
    72
wenzelm@50505
    73
fun reset_tracing () =
wenzelm@50505
    74
  Synchronized.change command_tracing_messages (K Inttab.empty);
wenzelm@49647
    75
wenzelm@50505
    76
fun update_tracing () =
wenzelm@50911
    77
  (case Position.parse_id (Position.thread_data ()) of
wenzelm@49647
    78
    NONE => ()
wenzelm@49647
    79
  | SOME id =>
wenzelm@50505
    80
      let
wenzelm@50505
    81
        val (n, ok) =
wenzelm@50505
    82
          Synchronized.change_result command_tracing_messages (fn tab =>
wenzelm@50505
    83
            let
wenzelm@50911
    84
              val n = the_default 0 (Inttab.lookup tab id) + 1;
wenzelm@50505
    85
              val ok = n <= ! tracing_messages;
wenzelm@50911
    86
            in ((n, ok), Inttab.update (id, n) tab) end);
wenzelm@50505
    87
      in
wenzelm@50505
    88
        if ok then ()
wenzelm@50505
    89
        else
wenzelm@50505
    90
          let
wenzelm@50505
    91
            val (text, promise) = Active.dialog_text ();
wenzelm@50505
    92
            val _ =
wenzelm@50505
    93
              writeln ("Tracing paused.  " ^ text "Stop" ^ ", or continue with next " ^
wenzelm@51044
    94
                text "100" ^ ", " ^ text "1000" ^ ", " ^ text "10000" ^ " messages?")
wenzelm@50505
    95
            val m = Markup.parse_int (Future.join promise)
wenzelm@50505
    96
              handle Fail _ => error "Stopped";
wenzelm@50505
    97
          in
wenzelm@50505
    98
            Synchronized.change command_tracing_messages
wenzelm@50911
    99
              (Inttab.map_default (id, 0) (fn k => k - m))
wenzelm@50505
   100
          end
wenzelm@50505
   101
      end);
wenzelm@49647
   102
wenzelm@49647
   103
wenzelm@40134
   104
(* message channels *)
wenzelm@25841
   105
wenzelm@25841
   106
local
wenzelm@25810
   107
wenzelm@40134
   108
fun chunk s = [string_of_int (size s), "\n", s];
wenzelm@26574
   109
wenzelm@49677
   110
fun message do_flush mbox name raw_props body =
wenzelm@43771
   111
  let
wenzelm@43772
   112
    val robust_props = map (pairself YXML.embed_controls) raw_props;
wenzelm@49677
   113
    val header = YXML.string_of (XML.Elem ((name, robust_props), []));
wenzelm@44731
   114
  in Mailbox.send mbox (chunk header @ chunk body, do_flush) end;
wenzelm@25554
   115
wenzelm@49677
   116
fun standard_message mbox opt_serial name body =
wenzelm@43771
   117
  if body = "" then ()
wenzelm@43771
   118
  else
wenzelm@49677
   119
    message false mbox name
wenzelm@50254
   120
      ((case opt_serial of SOME i => cons (Markup.serialN, Markup.print_int i) | _ => I)
wenzelm@43771
   121
        (Position.properties_of (Position.thread_data ()))) body;
wenzelm@25554
   122
wenzelm@45029
   123
fun message_output mbox channel =
wenzelm@28188
   124
  let
wenzelm@45029
   125
    fun flush () = ignore (try System_Channel.flush channel);
wenzelm@40134
   126
    fun loop receive =
wenzelm@40134
   127
      (case receive mbox of
wenzelm@44731
   128
        SOME (msg, do_flush) =>
wenzelm@45029
   129
         (List.app (fn s => System_Channel.output channel s) msg;
wenzelm@44731
   130
          if do_flush then flush () else ();
wenzelm@40301
   131
          loop (Mailbox.receive_timeout (seconds 0.02)))
wenzelm@44731
   132
      | NONE => (flush (); loop (SOME o Mailbox.receive)));
wenzelm@40134
   133
  in fn () => loop (SOME o Mailbox.receive) end;
wenzelm@28188
   134
wenzelm@28188
   135
in
wenzelm@28188
   136
wenzelm@48712
   137
fun init_channels channel =
wenzelm@28189
   138
  let
wenzelm@40134
   139
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
wenzelm@40134
   140
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
wenzelm@40134
   141
wenzelm@44731
   142
    val mbox = Mailbox.create () : (string list * bool) Mailbox.T;
wenzelm@45029
   143
    val _ = Simple_Thread.fork false (message_output mbox channel);
wenzelm@28044
   144
  in
wenzelm@50201
   145
    Output.Private_Hooks.status_fn := standard_message mbox NONE Markup.statusN;
wenzelm@50201
   146
    Output.Private_Hooks.report_fn := standard_message mbox NONE Markup.reportN;
wenzelm@50503
   147
    Output.Private_Hooks.result_fn :=
wenzelm@50503
   148
      (fn (i, s) => standard_message mbox (SOME i) Markup.resultN s);
wenzelm@49677
   149
    Output.Private_Hooks.writeln_fn :=
wenzelm@50201
   150
      (fn s => standard_message mbox (SOME (serial ())) Markup.writelnN s);
wenzelm@49647
   151
    Output.Private_Hooks.tracing_fn :=
wenzelm@50505
   152
      (fn s => (update_tracing (); standard_message mbox (SOME (serial ())) Markup.tracingN s));
wenzelm@49677
   153
    Output.Private_Hooks.warning_fn :=
wenzelm@50201
   154
      (fn s => standard_message mbox (SOME (serial ())) Markup.warningN s);
wenzelm@49677
   155
    Output.Private_Hooks.error_fn :=
wenzelm@50201
   156
      (fn (i, s) => standard_message mbox (SOME i) Markup.errorN s);
wenzelm@50201
   157
    Output.Private_Hooks.protocol_message_fn := message true mbox Markup.protocolN;
wenzelm@40133
   158
    Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
wenzelm@40133
   159
    Output.Private_Hooks.prompt_fn := ignore;
wenzelm@50201
   160
    message true mbox Markup.initN [] (Session.welcome ())
wenzelm@28044
   161
  end;
wenzelm@25841
   162
wenzelm@28188
   163
end;
wenzelm@28188
   164
wenzelm@25841
   165
wenzelm@39234
   166
(* protocol loop -- uninterruptible *)
wenzelm@38270
   167
wenzelm@43684
   168
val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
wenzelm@38270
   169
wenzelm@38270
   170
local
wenzelm@38270
   171
wenzelm@38270
   172
fun recover crash =
wenzelm@43684
   173
  (Synchronized.change crashes (cons crash);
wenzelm@38270
   174
    warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
wenzelm@38270
   175
wenzelm@45029
   176
fun read_chunk channel len =
wenzelm@38270
   177
  let
wenzelm@38270
   178
    val n =
wenzelm@38270
   179
      (case Int.fromString len of
wenzelm@38270
   180
        SOME n => n
wenzelm@38270
   181
      | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
wenzelm@45029
   182
    val chunk = System_Channel.inputN channel n;
wenzelm@38270
   183
    val m = size chunk;
wenzelm@38270
   184
  in
wenzelm@38270
   185
    if m = n then chunk
wenzelm@38270
   186
    else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
wenzelm@38270
   187
  end;
wenzelm@38270
   188
wenzelm@45029
   189
fun read_command channel =
wenzelm@45029
   190
  (case System_Channel.input_line channel of
wenzelm@38270
   191
    NONE => raise Runtime.TERMINATE
wenzelm@45029
   192
  | SOME line => map (read_chunk channel) (space_explode "," line));
wenzelm@38270
   193
wenzelm@38270
   194
in
wenzelm@38270
   195
wenzelm@45029
   196
fun loop channel =
wenzelm@38270
   197
  let val continue =
wenzelm@45029
   198
    (case read_command channel of
wenzelm@38270
   199
      [] => (Output.error_msg "Isabelle process: no input"; true)
wenzelm@38270
   200
    | name :: args => (run_command name args; true))
wenzelm@38270
   201
    handle Runtime.TERMINATE => false
wenzelm@38270
   202
      | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
wenzelm@45029
   203
  in if continue then loop channel else () end;
wenzelm@38270
   204
wenzelm@38270
   205
end;
wenzelm@38270
   206
wenzelm@38270
   207
wenzelm@25554
   208
(* init *)
wenzelm@25554
   209
wenzelm@50715
   210
val default_modes1 =
wenzelm@50715
   211
  [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN, Graph_Display.active_graphN];
wenzelm@50715
   212
val default_modes2 = [Symbol.xsymbolsN, isabelle_processN, Pretty.symbolicN];
wenzelm@49566
   213
wenzelm@45029
   214
fun init rendezvous = ignore (Simple_Thread.fork false (fn () =>
wenzelm@38253
   215
  let
wenzelm@40301
   216
    val _ = OS.Process.sleep (seconds 0.5);  (*yield to raw ML toplevel*)
wenzelm@46548
   217
    val _ = Output.physical_stderr Symbol.STX;
wenzelm@39528
   218
wenzelm@49661
   219
    val _ = Printer.show_markup_default := true;
wenzelm@47396
   220
    val _ = quick_and_dirty := false;
wenzelm@39528
   221
    val _ = Context.set_thread_data NONE;
wenzelm@43671
   222
    val _ =
wenzelm@43671
   223
      Unsynchronized.change print_mode
wenzelm@49566
   224
        (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
wenzelm@39528
   225
wenzelm@45029
   226
    val channel = rendezvous ();
wenzelm@48712
   227
    val _ = init_channels channel;
wenzelm@45029
   228
  in loop channel end));
wenzelm@25528
   229
wenzelm@45029
   230
fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
wenzelm@45029
   231
fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
wenzelm@45028
   232
wenzelm@50117
   233
wenzelm@50117
   234
(* options *)
wenzelm@50117
   235
wenzelm@50117
   236
val _ =
wenzelm@50117
   237
  protocol_command "Isabelle_Process.options"
wenzelm@50117
   238
    (fn [options_yxml] =>
wenzelm@50117
   239
      let val options = Options.decode (YXML.parse_body options_yxml) in
wenzelm@50698
   240
        Future.ML_statistics := true;
wenzelm@50117
   241
        Multithreading.trace := Options.int options "threads_trace";
wenzelm@50117
   242
        Multithreading.max_threads := Options.int options "threads";
wenzelm@50117
   243
        if Multithreading.max_threads_value () < 2
wenzelm@50117
   244
        then Multithreading.max_threads := 2 else ();
wenzelm@51332
   245
        Goal.parallel_proofs := (if Options.int options "parallel_proofs" > 0 then 3 else 0);
wenzelm@50119
   246
        Goal.parallel_proofs_threshold := Options.int options "parallel_proofs_threshold";
wenzelm@50505
   247
        tracing_messages := Options.int options "editor_tracing_messages"
wenzelm@50117
   248
      end);
wenzelm@50117
   249
wenzelm@25528
   250
end;
wenzelm@39528
   251