src/Pure/System/isabelle_process.ML
author wenzelm
Mon Oct 01 20:17:30 2012 +0200 (2012-10-01 ago)
changeset 49677 c4e2762a265c
parent 49661 ac48def96b69
child 50117 32755e357a51
permissions -rw-r--r--
more direct message header: eliminated historic encoding via single letter;
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@49647
    20
  val tracing_limit: int Unsynchronized.ref;
wenzelm@49647
    21
  val reset_tracing_limits: 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@49647
    66
(* tracing limit *)
wenzelm@49647
    67
wenzelm@49647
    68
val tracing_limit = Unsynchronized.ref 500000;
wenzelm@49647
    69
wenzelm@49647
    70
val tracing_limits = Synchronized.var "tracing_limits" (Inttab.empty: int Inttab.table);
wenzelm@49647
    71
wenzelm@49647
    72
fun reset_tracing_limits () = Synchronized.change tracing_limits (K Inttab.empty);
wenzelm@49647
    73
wenzelm@49647
    74
fun update_tracing_limits msg =
wenzelm@49647
    75
  (case Position.get_id (Position.thread_data ()) of
wenzelm@49647
    76
    NONE => ()
wenzelm@49647
    77
  | SOME id =>
wenzelm@49647
    78
      Synchronized.change tracing_limits (fn tab =>
wenzelm@49647
    79
        let
wenzelm@49647
    80
          val i = Markup.parse_int id;
wenzelm@49647
    81
          val n = the_default 0 (Inttab.lookup tab i) + size msg;
wenzelm@49647
    82
          val _ = if n > ! tracing_limit then raise Output.TRACING_LIMIT n else ();
wenzelm@49647
    83
        in Inttab.update (i, n) tab end));
wenzelm@49647
    84
wenzelm@49647
    85
wenzelm@40134
    86
(* message channels *)
wenzelm@25841
    87
wenzelm@25841
    88
local
wenzelm@25810
    89
wenzelm@40134
    90
fun chunk s = [string_of_int (size s), "\n", s];
wenzelm@26574
    91
wenzelm@49677
    92
fun message do_flush mbox name raw_props body =
wenzelm@43771
    93
  let
wenzelm@43772
    94
    val robust_props = map (pairself YXML.embed_controls) raw_props;
wenzelm@49677
    95
    val header = YXML.string_of (XML.Elem ((name, robust_props), []));
wenzelm@44731
    96
  in Mailbox.send mbox (chunk header @ chunk body, do_flush) end;
wenzelm@25554
    97
wenzelm@49677
    98
fun standard_message mbox opt_serial name body =
wenzelm@43771
    99
  if body = "" then ()
wenzelm@43771
   100
  else
wenzelm@49677
   101
    message false mbox name
wenzelm@45666
   102
      ((case opt_serial of SOME i => cons (Isabelle_Markup.serialN, string_of_int i) | _ => I)
wenzelm@43771
   103
        (Position.properties_of (Position.thread_data ()))) body;
wenzelm@25554
   104
wenzelm@45029
   105
fun message_output mbox channel =
wenzelm@28188
   106
  let
wenzelm@45029
   107
    fun flush () = ignore (try System_Channel.flush channel);
wenzelm@40134
   108
    fun loop receive =
wenzelm@40134
   109
      (case receive mbox of
wenzelm@44731
   110
        SOME (msg, do_flush) =>
wenzelm@45029
   111
         (List.app (fn s => System_Channel.output channel s) msg;
wenzelm@44731
   112
          if do_flush then flush () else ();
wenzelm@40301
   113
          loop (Mailbox.receive_timeout (seconds 0.02)))
wenzelm@44731
   114
      | NONE => (flush (); loop (SOME o Mailbox.receive)));
wenzelm@40134
   115
  in fn () => loop (SOME o Mailbox.receive) end;
wenzelm@28188
   116
wenzelm@28188
   117
in
wenzelm@28188
   118
wenzelm@48712
   119
fun init_channels channel =
wenzelm@28189
   120
  let
wenzelm@40134
   121
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
wenzelm@40134
   122
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
wenzelm@40134
   123
wenzelm@44731
   124
    val mbox = Mailbox.create () : (string list * bool) Mailbox.T;
wenzelm@45029
   125
    val _ = Simple_Thread.fork false (message_output mbox channel);
wenzelm@28044
   126
  in
wenzelm@49677
   127
    Output.Private_Hooks.status_fn := standard_message mbox NONE Isabelle_Markup.statusN;
wenzelm@49677
   128
    Output.Private_Hooks.report_fn := standard_message mbox NONE Isabelle_Markup.reportN;
wenzelm@49677
   129
    Output.Private_Hooks.writeln_fn :=
wenzelm@49677
   130
      (fn s => standard_message mbox (SOME (serial ())) Isabelle_Markup.writelnN s);
wenzelm@49647
   131
    Output.Private_Hooks.tracing_fn :=
wenzelm@49677
   132
      (fn s =>
wenzelm@49677
   133
        (update_tracing_limits s;
wenzelm@49677
   134
          standard_message mbox (SOME (serial ())) Isabelle_Markup.tracingN s));
wenzelm@49677
   135
    Output.Private_Hooks.warning_fn :=
wenzelm@49677
   136
      (fn s => standard_message mbox (SOME (serial ())) Isabelle_Markup.warningN s);
wenzelm@49677
   137
    Output.Private_Hooks.error_fn :=
wenzelm@49677
   138
      (fn (i, s) => standard_message mbox (SOME i) Isabelle_Markup.errorN s);
wenzelm@49677
   139
    Output.Private_Hooks.protocol_message_fn := message true mbox Isabelle_Markup.protocolN;
wenzelm@40133
   140
    Output.Private_Hooks.urgent_message_fn := ! Output.Private_Hooks.writeln_fn;
wenzelm@40133
   141
    Output.Private_Hooks.prompt_fn := ignore;
wenzelm@49677
   142
    message true mbox Isabelle_Markup.initN [] (Session.welcome ())
wenzelm@28044
   143
  end;
wenzelm@25841
   144
wenzelm@28188
   145
end;
wenzelm@28188
   146
wenzelm@25841
   147
wenzelm@39234
   148
(* protocol loop -- uninterruptible *)
wenzelm@38270
   149
wenzelm@43684
   150
val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
wenzelm@38270
   151
wenzelm@38270
   152
local
wenzelm@38270
   153
wenzelm@38270
   154
fun recover crash =
wenzelm@43684
   155
  (Synchronized.change crashes (cons crash);
wenzelm@38270
   156
    warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
wenzelm@38270
   157
wenzelm@45029
   158
fun read_chunk channel len =
wenzelm@38270
   159
  let
wenzelm@38270
   160
    val n =
wenzelm@38270
   161
      (case Int.fromString len of
wenzelm@38270
   162
        SOME n => n
wenzelm@38270
   163
      | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
wenzelm@45029
   164
    val chunk = System_Channel.inputN channel n;
wenzelm@38270
   165
    val m = size chunk;
wenzelm@38270
   166
  in
wenzelm@38270
   167
    if m = n then chunk
wenzelm@38270
   168
    else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
wenzelm@38270
   169
  end;
wenzelm@38270
   170
wenzelm@45029
   171
fun read_command channel =
wenzelm@45029
   172
  (case System_Channel.input_line channel of
wenzelm@38270
   173
    NONE => raise Runtime.TERMINATE
wenzelm@45029
   174
  | SOME line => map (read_chunk channel) (space_explode "," line));
wenzelm@38270
   175
wenzelm@38270
   176
in
wenzelm@38270
   177
wenzelm@45029
   178
fun loop channel =
wenzelm@38270
   179
  let val continue =
wenzelm@45029
   180
    (case read_command channel of
wenzelm@38270
   181
      [] => (Output.error_msg "Isabelle process: no input"; true)
wenzelm@38270
   182
    | name :: args => (run_command name args; true))
wenzelm@38270
   183
    handle Runtime.TERMINATE => false
wenzelm@38270
   184
      | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
wenzelm@45029
   185
  in if continue then loop channel else () end;
wenzelm@38270
   186
wenzelm@38270
   187
end;
wenzelm@38270
   188
wenzelm@38270
   189
wenzelm@25554
   190
(* init *)
wenzelm@25554
   191
wenzelm@49566
   192
val default_modes1 = [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN];
wenzelm@49566
   193
val default_modes2 =
wenzelm@49566
   194
  [Symbol.xsymbolsN, isabelle_processN, Pretty.symbolicN, Graph_Display.graphview_reportN];
wenzelm@49566
   195
wenzelm@45029
   196
fun init rendezvous = ignore (Simple_Thread.fork false (fn () =>
wenzelm@38253
   197
  let
wenzelm@40301
   198
    val _ = OS.Process.sleep (seconds 0.5);  (*yield to raw ML toplevel*)
wenzelm@46548
   199
    val _ = Output.physical_stderr Symbol.STX;
wenzelm@39528
   200
wenzelm@49661
   201
    (* FIXME proper system options *)
wenzelm@49661
   202
    val _ = Printer.show_markup_default := true;
wenzelm@47396
   203
    val _ = quick_and_dirty := false;
wenzelm@49108
   204
    val _ = Goal.parallel_proofs := 4;
wenzelm@44988
   205
    val _ =
wenzelm@44988
   206
      if Multithreading.max_threads_value () < 2
wenzelm@44988
   207
      then Multithreading.max_threads := 2 else ();
wenzelm@39528
   208
    val _ = Context.set_thread_data NONE;
wenzelm@43671
   209
    val _ =
wenzelm@43671
   210
      Unsynchronized.change print_mode
wenzelm@49566
   211
        (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
wenzelm@39528
   212
wenzelm@45029
   213
    val channel = rendezvous ();
wenzelm@48712
   214
    val _ = init_channels channel;
wenzelm@45029
   215
  in loop channel end));
wenzelm@25528
   216
wenzelm@45029
   217
fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
wenzelm@45029
   218
fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
wenzelm@45028
   219
wenzelm@25528
   220
end;
wenzelm@39528
   221