src/HOL/Tools/Sledgehammer/async_manager.ML
author blanchet
Fri, 18 Nov 2011 11:47:12 +0100
changeset 46444 22d003b5b32e
parent 44995 ef876972fdc1
child 48400 5f35a95c0645
permissions -rw-r--r--
less offensive terminology
blanchet@41290
     1
(*  Title:      HOL/Tools/Sledgehammer/async_manager.ML
blanchet@37583
     2
    Author:     Fabian Immler, TU Muenchen
blanchet@37583
     3
    Author:     Makarius
blanchet@37583
     4
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@37583
     5
blanchet@37583
     6
Central manager for asynchronous diagnosis tool threads.
blanchet@37583
     7
*)
blanchet@37583
     8
blanchet@37583
     9
signature ASYNC_MANAGER =
blanchet@37583
    10
sig
blanchet@43846
    11
  val implode_desc : string * string -> string
blanchet@43846
    12
  val break_into_chunks : string -> string list
blanchet@37584
    13
  val launch :
blanchet@43846
    14
    string -> Time.time -> Time.time -> string * string
blanchet@43846
    15
    -> (unit -> bool * string) -> unit
blanchet@37585
    16
  val kill_threads : string -> string -> unit
blanchet@37585
    17
  val running_threads : string -> string -> unit
blanchet@37585
    18
  val thread_messages : string -> string -> int option -> unit
blanchet@37583
    19
end;
blanchet@37583
    20
blanchet@37583
    21
structure Async_Manager : ASYNC_MANAGER =
blanchet@37583
    22
struct
blanchet@37583
    23
blanchet@37583
    24
(** preferences **)
blanchet@37583
    25
blanchet@37583
    26
val message_store_limit = 20;
blanchet@37583
    27
val message_display_limit = 5;
blanchet@37583
    28
blanchet@37583
    29
blanchet@37583
    30
(** thread management **)
blanchet@37583
    31
blanchet@43846
    32
val implode_desc = op ^ o apfst quote
blanchet@43846
    33
blanchet@43846
    34
fun implode_message (workers, work) =
blanchet@43870
    35
  space_implode " " (Try.serial_commas "and" (map quote workers)) ^ work
blanchet@43846
    36
blanchet@43846
    37
blanchet@37583
    38
(* data structures over threads *)
blanchet@37583
    39
blanchet@37583
    40
structure Thread_Heap = Heap
blanchet@37583
    41
(
blanchet@37583
    42
  type elem = Time.time * Thread.thread;
blanchet@37583
    43
  fun ord ((a, _), (b, _)) = Time.compare (a, b);
blanchet@37583
    44
);
blanchet@37583
    45
blanchet@37583
    46
fun lookup_thread xs = AList.lookup Thread.equal xs;
blanchet@37583
    47
fun delete_thread xs = AList.delete Thread.equal xs;
blanchet@37583
    48
fun update_thread xs = AList.update Thread.equal xs;
blanchet@37583
    49
blanchet@37583
    50
blanchet@37583
    51
(* state of thread manager *)
blanchet@37583
    52
blanchet@37583
    53
type state =
blanchet@37585
    54
  {manager: Thread.thread option,
blanchet@37585
    55
   timeout_heap: Thread_Heap.T,
blanchet@43846
    56
   active:
blanchet@43846
    57
     (Thread.thread
blanchet@43846
    58
      * (string * Time.time * Time.time * (string * string))) list,
blanchet@43846
    59
   canceling:  (Thread.thread * (string * Time.time * (string * string))) list,
blanchet@43846
    60
   messages: (bool * (string * (string * string))) list,
blanchet@43846
    61
   store: (string * (string * string)) list}
blanchet@37583
    62
blanchet@37583
    63
fun make_state manager timeout_heap active canceling messages store : state =
blanchet@37583
    64
  {manager = manager, timeout_heap = timeout_heap, active = active,
blanchet@37583
    65
   canceling = canceling, messages = messages, store = store}
blanchet@37583
    66
blanchet@37583
    67
val global_state = Synchronized.var "async_manager"
blanchet@37583
    68
  (make_state NONE Thread_Heap.empty [] [] [] []);
blanchet@37583
    69
blanchet@37583
    70
blanchet@37583
    71
(* unregister thread *)
blanchet@37583
    72
blanchet@43846
    73
fun unregister (urgent, message) thread =
blanchet@37583
    74
  Synchronized.change global_state
blanchet@37583
    75
  (fn state as {manager, timeout_heap, active, canceling, messages, store} =>
blanchet@37583
    76
    (case lookup_thread active thread of
blanchet@43846
    77
      SOME (tool, _, _, desc as (worker, its_desc)) =>
blanchet@37583
    78
        let
blanchet@37583
    79
          val active' = delete_thread thread active;
blanchet@37583
    80
          val now = Time.now ()
blanchet@37585
    81
          val canceling' = (thread, (tool, now, desc)) :: canceling
blanchet@43846
    82
          val message' = (worker, its_desc ^ "\n" ^ message)
blanchet@43846
    83
          val messages' = (urgent, (tool, message')) :: messages
blanchet@37585
    84
          val store' = (tool, message') ::
blanchet@37583
    85
            (if length store <= message_store_limit then store
blanchet@37583
    86
             else #1 (chop message_store_limit store));
blanchet@37583
    87
        in make_state manager timeout_heap active' canceling' messages' store' end
blanchet@37583
    88
    | NONE => state));
blanchet@37583
    89
blanchet@37583
    90
blanchet@37583
    91
(* main manager thread -- only one may exist *)
blanchet@37583
    92
wenzelm@40553
    93
val min_wait_time = seconds 0.3;
wenzelm@40553
    94
val max_wait_time = seconds 10.0;
blanchet@37583
    95
blanchet@37583
    96
fun replace_all bef aft =
blanchet@37583
    97
  let
blanchet@37583
    98
    fun aux seen "" = String.implode (rev seen)
blanchet@37583
    99
      | aux seen s =
blanchet@37583
   100
        if String.isPrefix bef s then
blanchet@37583
   101
          aux seen "" ^ aft ^ aux [] (unprefix bef s)
blanchet@37583
   102
        else
blanchet@37583
   103
          aux (String.sub (s, 0) :: seen) (String.extract (s, 1, NONE))
blanchet@37583
   104
  in aux [] end
blanchet@37583
   105
blanchet@37583
   106
(* This is a workaround for Proof General's off-by-a-few sendback display bug,
blanchet@37583
   107
   whereby "pr" in "proof" is not highlighted. *)
blanchet@43846
   108
val break_into_chunks = space_explode "\000" o replace_all "\n\n" "\000"
blanchet@37583
   109
blanchet@37585
   110
fun print_new_messages () =
blanchet@43846
   111
  Synchronized.change_result global_state
blanchet@43846
   112
      (fn {manager, timeout_heap, active, canceling, messages, store} =>
blanchet@43846
   113
          messages
blanchet@43896
   114
          |> List.partition
blanchet@43896
   115
                 (fn (urgent, _) =>
blanchet@43896
   116
                     (null active andalso null canceling) orelse urgent)
blanchet@43846
   117
          ||> (fn postponed_messages =>
blanchet@43846
   118
                  make_state manager timeout_heap active canceling
blanchet@43846
   119
                                     postponed_messages store))
blanchet@43846
   120
  |> map (fn (_, (tool, (worker, work))) => ((tool, work), worker))
blanchet@43846
   121
  |> AList.group (op =)
blanchet@43846
   122
  |> List.app (fn ((tool, work), workers) =>
blanchet@43846
   123
                  tool ^ ": " ^
blanchet@43846
   124
                  implode_message (workers |> sort string_ord, work)
blanchet@43846
   125
                  |> break_into_chunks
blanchet@43846
   126
                  |> List.app Output.urgent_message)
blanchet@37583
   127
blanchet@39250
   128
fun check_thread_manager () = Synchronized.change global_state
blanchet@37583
   129
  (fn state as {manager, timeout_heap, active, canceling, messages, store} =>
blanchet@37583
   130
    if (case manager of SOME thread => Thread.isActive thread | NONE => false) then state
blanchet@37583
   131
    else let val manager = SOME (Toplevel.thread false (fn () =>
blanchet@37583
   132
      let
blanchet@37583
   133
        fun time_limit timeout_heap =
blanchet@37583
   134
          (case try Thread_Heap.min timeout_heap of
blanchet@37583
   135
            NONE => Time.+ (Time.now (), max_wait_time)
blanchet@37583
   136
          | SOME (time, _) => time);
blanchet@37583
   137
blanchet@37583
   138
        (*action: find threads whose timeout is reached, and interrupt canceling threads*)
blanchet@37583
   139
        fun action {manager, timeout_heap, active, canceling, messages, store} =
blanchet@37583
   140
          let val (timeout_threads, timeout_heap') =
blanchet@37583
   141
            Thread_Heap.upto (Time.now (), Thread.self ()) timeout_heap;
blanchet@37583
   142
          in
blanchet@37585
   143
            if null timeout_threads andalso null canceling then
blanchet@37585
   144
              NONE
blanchet@37583
   145
            else
blanchet@37583
   146
              let
wenzelm@44995
   147
                val _ = List.app (Simple_Thread.interrupt_unsynchronized o #1) canceling
blanchet@37583
   148
                val canceling' = filter (Thread.isActive o #1) canceling
blanchet@37583
   149
                val state' = make_state manager timeout_heap' active canceling' messages store;
blanchet@37583
   150
              in SOME (map #2 timeout_threads, state') end
blanchet@37583
   151
          end;
blanchet@37583
   152
      in
blanchet@37583
   153
        while Synchronized.change_result global_state
blanchet@37583
   154
          (fn state as {timeout_heap, active, canceling, messages, store, ...} =>
blanchet@37583
   155
            if null active andalso null canceling andalso null messages
blanchet@37583
   156
            then (false, make_state NONE timeout_heap active canceling messages store)
blanchet@37583
   157
            else (true, state))
blanchet@37583
   158
        do
blanchet@37583
   159
          (Synchronized.timed_access global_state (SOME o time_limit o #timeout_heap) action
blanchet@37583
   160
            |> these
blanchet@43896
   161
            |> List.app (unregister (false, "Timed out."));
blanchet@37585
   162
            print_new_messages ();
blanchet@37583
   163
            (*give threads some time to respond to interrupt*)
blanchet@37583
   164
            OS.Process.sleep min_wait_time)
blanchet@37583
   165
      end))
blanchet@37583
   166
    in make_state manager timeout_heap active canceling messages store end)
blanchet@37583
   167
blanchet@37583
   168
blanchet@37583
   169
(* register thread *)
blanchet@37583
   170
blanchet@39250
   171
fun register tool birth_time death_time desc thread =
blanchet@37583
   172
 (Synchronized.change global_state
blanchet@37583
   173
    (fn {manager, timeout_heap, active, canceling, messages, store} =>
blanchet@37583
   174
      let
blanchet@37583
   175
        val timeout_heap' = Thread_Heap.insert (death_time, thread) timeout_heap;
blanchet@37585
   176
        val active' = update_thread (thread, (tool, birth_time, death_time, desc)) active;
blanchet@37583
   177
        val state' = make_state manager timeout_heap' active' canceling messages store;
blanchet@37583
   178
      in state' end);
blanchet@39250
   179
  check_thread_manager ())
blanchet@37583
   180
blanchet@37583
   181
blanchet@39250
   182
fun launch tool birth_time death_time desc f =
blanchet@37584
   183
  (Toplevel.thread true
blanchet@37584
   184
       (fn () =>
blanchet@37584
   185
           let
blanchet@37584
   186
             val self = Thread.self ()
blanchet@39250
   187
             val _ = register tool birth_time death_time desc self
blanchet@43846
   188
           in unregister (f ()) self end);
blanchet@37584
   189
   ())
blanchet@37584
   190
blanchet@37583
   191
blanchet@37583
   192
(** user commands **)
blanchet@37583
   193
blanchet@37583
   194
(* kill threads *)
blanchet@37583
   195
blanchet@43846
   196
fun kill_threads tool das_wort_worker = Synchronized.change global_state
blanchet@37583
   197
  (fn {manager, timeout_heap, active, canceling, messages, store} =>
blanchet@37583
   198
    let
blanchet@37585
   199
      val killing =
blanchet@37585
   200
        map_filter (fn (th, (tool', _, _, desc)) =>
blanchet@37585
   201
                       if tool' = tool then SOME (th, (tool', Time.now (), desc))
blanchet@37585
   202
                       else NONE) active
blanchet@37583
   203
      val state' = make_state manager timeout_heap [] (killing @ canceling) messages store;
blanchet@43846
   204
      val _ =
blanchet@43846
   205
        if null killing then ()
blanchet@46444
   206
        else Output.urgent_message ("Interrupted active " ^ das_wort_worker ^ "s.")
blanchet@37583
   207
    in state' end);
blanchet@37583
   208
blanchet@37583
   209
blanchet@37583
   210
(* running threads *)
blanchet@37583
   211
blanchet@37585
   212
fun seconds time = string_of_int (Time.toSeconds time) ^ " s"
blanchet@37583
   213
blanchet@43846
   214
fun running_threads tool das_wort_worker =
blanchet@37583
   215
  let
blanchet@37583
   216
    val {active, canceling, ...} = Synchronized.value global_state;
blanchet@37583
   217
blanchet@37583
   218
    val now = Time.now ();
blanchet@37585
   219
    fun running_info (_, (tool', birth_time, death_time, desc)) =
blanchet@37585
   220
      if tool' = tool then
blanchet@37585
   221
        SOME ("Running: " ^ seconds (Time.- (now, birth_time)) ^ " -- " ^
blanchet@43846
   222
              seconds (Time.- (death_time, now)) ^ " to live:\n" ^
blanchet@43846
   223
              implode_desc desc)
blanchet@37585
   224
      else
blanchet@37585
   225
        NONE
blanchet@37585
   226
    fun canceling_info (_, (tool', death_time, desc)) =
blanchet@37585
   227
      if tool' = tool then
blanchet@43846
   228
        SOME ("Trying to interrupt " ^ das_wort_worker ^ " since " ^
blanchet@43846
   229
              seconds (Time.- (now, death_time)) ^ ":\n" ^ implode_desc desc)
blanchet@37585
   230
      else
blanchet@37585
   231
        NONE
blanchet@37583
   232
    val running =
blanchet@37585
   233
      case map_filter running_info active of
blanchet@43846
   234
        [] => ["No " ^ das_wort_worker ^ "s running."]
blanchet@43846
   235
      | ss => "Running " ^ das_wort_worker ^ "s " :: ss
blanchet@37583
   236
    val interrupting =
blanchet@37585
   237
      case map_filter canceling_info canceling of
blanchet@37583
   238
        [] => []
blanchet@43846
   239
      | ss => "Interrupting " ^ das_wort_worker ^ "s " :: ss
wenzelm@40392
   240
  in Output.urgent_message (space_implode "\n\n" (running @ interrupting)) end
blanchet@37583
   241
blanchet@43846
   242
fun thread_messages tool das_wort_worker opt_limit =
blanchet@37583
   243
  let
blanchet@37583
   244
    val limit = the_default message_display_limit opt_limit;
blanchet@37585
   245
    val tool_store = Synchronized.value global_state
blanchet@37585
   246
                     |> #store |> filter (curry (op =) tool o fst)
blanchet@37583
   247
    val header =
blanchet@43846
   248
      "Recent " ^ das_wort_worker ^ " messages" ^
blanchet@37585
   249
        (if length tool_store <= limit then ":"
blanchet@37585
   250
         else " (" ^ string_of_int limit ^ " displayed):");
blanchet@43846
   251
    val ss = tool_store |> chop limit |> #1 |> map (implode_desc o snd)
blanchet@43846
   252
  in List.app Output.urgent_message (header :: maps break_into_chunks ss) end
blanchet@37583
   253
blanchet@37583
   254
end;