src/Pure/System/isabelle_process.ML
author wenzelm
Sat Sep 29 19:28:03 2012 +0200 (2012-09-29 ago)
changeset 49661 ac48def96b69
parent 49647 21ae8500d261
child 49677 c4e2762a265c
permissions -rw-r--r--
enable show_markup by default (approx. double output size);
     1 (*  Title:      Pure/System/isabelle_process.ML
     2     Author:     Makarius
     3 
     4 Isabelle process wrapper, based on private fifos for maximum
     5 robustness and performance, or local socket for maximum portability.
     6 
     7 Startup phases:
     8   - raw Posix process startup with uncontrolled output on stdout/stderr
     9   - stderr \002: ML running
    10   - stdin/stdout/stderr freely available (raw ML loop)
    11   - protocol thread initialization
    12   - rendezvous on system channel
    13   - message INIT: channels ready
    14 *)
    15 
    16 signature ISABELLE_PROCESS =
    17 sig
    18   val is_active: unit -> bool
    19   val protocol_command: string -> (string list -> unit) -> unit
    20   val tracing_limit: int Unsynchronized.ref;
    21   val reset_tracing_limits: unit -> unit
    22   val crashes: exn list Synchronized.var
    23   val init_fifos: string -> string -> unit
    24   val init_socket: string -> unit
    25 end;
    26 
    27 structure Isabelle_Process: ISABELLE_PROCESS =
    28 struct
    29 
    30 (* print mode *)
    31 
    32 val isabelle_processN = "isabelle_process";
    33 
    34 fun is_active () = Print_Mode.print_mode_active isabelle_processN;
    35 
    36 val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
    37 val _ = Markup.add_mode isabelle_processN YXML.output_markup;
    38 
    39 
    40 (* protocol commands *)
    41 
    42 local
    43 
    44 val commands =
    45   Synchronized.var "Isabelle_Process.commands" (Symtab.empty: (string list -> unit) Symtab.table);
    46 
    47 in
    48 
    49 fun protocol_command name cmd =
    50   Synchronized.change commands (fn cmds =>
    51    (if not (Symtab.defined cmds name) then ()
    52     else warning ("Redefining Isabelle process command " ^ quote name);
    53     Symtab.update (name, cmd) cmds));
    54 
    55 fun run_command name args =
    56   (case Symtab.lookup (Synchronized.value commands) name of
    57     NONE => error ("Undefined Isabelle process command " ^ quote name)
    58   | SOME cmd =>
    59       (Runtime.debugging cmd args handle exn =>
    60         error ("Isabelle process protocol failure: " ^ quote name ^ "\n" ^
    61           ML_Compiler.exn_message exn)));
    62 
    63 end;
    64 
    65 
    66 (* tracing limit *)
    67 
    68 val tracing_limit = Unsynchronized.ref 500000;
    69 
    70 val tracing_limits = Synchronized.var "tracing_limits" (Inttab.empty: int Inttab.table);
    71 
    72 fun reset_tracing_limits () = Synchronized.change tracing_limits (K Inttab.empty);
    73 
    74 fun update_tracing_limits msg =
    75   (case Position.get_id (Position.thread_data ()) of
    76     NONE => ()
    77   | SOME id =>
    78       Synchronized.change tracing_limits (fn tab =>
    79         let
    80           val i = Markup.parse_int id;
    81           val n = the_default 0 (Inttab.lookup tab i) + size msg;
    82           val _ = if n > ! tracing_limit then raise Output.TRACING_LIMIT n else ();
    83         in Inttab.update (i, n) tab end));
    84 
    85 
    86 (* message channels *)
    87 
    88 local
    89 
    90 fun chunk s = [string_of_int (size s), "\n", s];
    91 
    92 fun message do_flush mbox ch raw_props body =
    93   let
    94     val robust_props = map (pairself YXML.embed_controls) raw_props;
    95     val header = YXML.string_of (XML.Elem ((ch, robust_props), []));
    96   in Mailbox.send mbox (chunk header @ chunk body, do_flush) end;
    97 
    98 fun standard_message mbox opt_serial ch body =
    99   if body = "" then ()
   100   else
   101     message false mbox ch
   102       ((case opt_serial of SOME i => cons (Isabelle_Markup.serialN, string_of_int i) | _ => I)
   103         (Position.properties_of (Position.thread_data ()))) body;
   104 
   105 fun message_output mbox channel =
   106   let
   107     fun flush () = ignore (try System_Channel.flush channel);
   108     fun loop receive =
   109       (case receive mbox of
   110         SOME (msg, do_flush) =>
   111          (List.app (fn s => System_Channel.output channel s) msg;
   112           if do_flush then flush () else ();
   113           loop (Mailbox.receive_timeout (seconds 0.02)))
   114       | NONE => (flush (); loop (SOME o Mailbox.receive)));
   115   in fn () => loop (SOME o Mailbox.receive) end;
   116 
   117 in
   118 
   119 fun init_channels channel =
   120   let
   121     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
   122     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
   123 
   124     val mbox = Mailbox.create () : (string list * bool) Mailbox.T;
   125     val _ = Simple_Thread.fork false (message_output mbox channel);
   126   in
   127     Output.Private_Hooks.status_fn := standard_message mbox NONE "B";
   128     Output.Private_Hooks.report_fn := standard_message mbox NONE "C";
   129     Output.Private_Hooks.writeln_fn := (fn s => standard_message mbox (SOME (serial ())) "D" s);
   130     Output.Private_Hooks.tracing_fn :=
   131       (fn s => (update_tracing_limits s; standard_message mbox (SOME (serial ())) "E" s));
   132     Output.Private_Hooks.warning_fn := (fn s => standard_message mbox (SOME (serial ())) "F" s);
   133     Output.Private_Hooks.error_fn := (fn (i, s) => standard_message mbox (SOME i) "G" s);
   134     Output.Private_Hooks.protocol_message_fn := message true mbox "H";
   135     Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
   136     Output.Private_Hooks.prompt_fn := ignore;
   137     message true mbox "A" [] (Session.welcome ())
   138   end;
   139 
   140 end;
   141 
   142 
   143 (* protocol loop -- uninterruptible *)
   144 
   145 val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
   146 
   147 local
   148 
   149 fun recover crash =
   150   (Synchronized.change crashes (cons crash);
   151     warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
   152 
   153 fun read_chunk channel len =
   154   let
   155     val n =
   156       (case Int.fromString len of
   157         SOME n => n
   158       | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
   159     val chunk = System_Channel.inputN channel n;
   160     val m = size chunk;
   161   in
   162     if m = n then chunk
   163     else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
   164   end;
   165 
   166 fun read_command channel =
   167   (case System_Channel.input_line channel of
   168     NONE => raise Runtime.TERMINATE
   169   | SOME line => map (read_chunk channel) (space_explode "," line));
   170 
   171 in
   172 
   173 fun loop channel =
   174   let val continue =
   175     (case read_command channel of
   176       [] => (Output.error_msg "Isabelle process: no input"; true)
   177     | name :: args => (run_command name args; true))
   178     handle Runtime.TERMINATE => false
   179       | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
   180   in if continue then loop channel else () end;
   181 
   182 end;
   183 
   184 
   185 (* init *)
   186 
   187 val default_modes1 = [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN];
   188 val default_modes2 =
   189   [Symbol.xsymbolsN, isabelle_processN, Pretty.symbolicN, Graph_Display.graphview_reportN];
   190 
   191 fun init rendezvous = ignore (Simple_Thread.fork false (fn () =>
   192   let
   193     val _ = OS.Process.sleep (seconds 0.5);  (*yield to raw ML toplevel*)
   194     val _ = Output.physical_stderr Symbol.STX;
   195 
   196     (* FIXME proper system options *)
   197     val _ = Printer.show_markup_default := true;
   198     val _ = quick_and_dirty := false;
   199     val _ = Goal.parallel_proofs := 4;
   200     val _ =
   201       if Multithreading.max_threads_value () < 2
   202       then Multithreading.max_threads := 2 else ();
   203     val _ = Context.set_thread_data NONE;
   204     val _ =
   205       Unsynchronized.change print_mode
   206         (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
   207 
   208     val channel = rendezvous ();
   209     val _ = init_channels channel;
   210   in loop channel end));
   211 
   212 fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
   213 fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
   214 
   215 end;
   216