| 1 | (************************************************************************) |
| 2 | (* * The Rocq Prover / The Rocq Development Team *) |
| 3 | (* v * Copyright INRIA, CNRS and contributors *) |
| 4 | (* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 | (* \VV/ **************************************************************) |
| 6 | (* // * This file is distributed under the terms of the *) |
| 7 | (* * GNU Lesser General Public License Version 2.1 *) |
| 8 | (* * (see LICENSE file for the text of the license) *) |
| 9 | (************************************************************************) |
| 10 | |
| 11 | (* Default flags for workers *) |
| 12 | val async_proofs_flags_for_workers : string list ref |
| 13 | |
| 14 | (** This file provides an API for defining and managing a queue of |
| 15 | tasks to be done by external workers. |
| 16 | |
| 17 | A queue of items of type [task] is maintained, then for each task, |
| 18 | a request is generated, then sent to a worker using marshalling. |
| 19 | |
| 20 | The workers will then eventually return a result, using marshalling |
| 21 | again: |
| 22 | ____ ____ ____ ________ |
| 23 | | T1 | T2 | T3 | => [request ] => | Worker | |
| 24 | |____|____|____| <= [response] <= |________| |
| 25 | | Master Proc. | |
| 26 | \--------------/ |
| 27 | |
| 28 | Thus [request] and [response] must be safely marshallable. |
| 29 | |
| 30 | Operations for managing the task queue are provide, see below |
| 31 | for more details. |
| 32 | |
| 33 | *) |
| 34 | |
| 35 | (** The [Task] module type defines an abstract message-processing |
| 36 | queue. *) |
| 37 | module type Task = sig |
| 38 | |
| 39 | (** Main description of a task. Elements are stored in the "master" |
| 40 | process, and then converted into a request. |
| 41 | *) |
| 42 | type task |
| 43 | |
| 44 | (** [competence] stores the information about what kind of work a |
| 45 | worker has completed / has available. *) |
| 46 | type competence |
| 47 | |
| 48 | (** A worker_status is: |
| 49 | |
| 50 | - [`Fresh] when a worker is born. |
| 51 | |
| 52 | - [`Old of competence]: When a worker ends a job it can either die |
| 53 | (and be replaced by a fresh new worker) or hang there as an [`Old] |
| 54 | worker. In such case some data can be carried by the [`Old] |
| 55 | constructor, typically used to implement [request_of_task]. |
| 56 | |
| 57 | This allows to implement both one-shot workers and "persistent" |
| 58 | ones. E.g. par: is implement using workers that don't |
| 59 | "reboot". Proof workers do reboot mainly because the vm has some |
| 60 | C state that cannot be cleared, so you have a real memory leak if |
| 61 | you don't reboot the worker. *) |
| 62 | type worker_status = Fresh | Old of competence |
| 63 | |
| 64 | (** Type of input and output data for workers. |
| 65 | |
| 66 | The data must be marshallable as it send through the network |
| 67 | using [Marshal] . *) |
| 68 | type request |
| 69 | type response |
| 70 | |
| 71 | (** UID of the task kind *) |
| 72 | val name : string |
| 73 | |
| 74 | (** Extra arguments of the task kind *) |
| 75 | val extra_env : unit -> string array |
| 76 | |
| 77 | (** {5 Master API, it is run by the master, on a thread} *) |
| 78 | |
| 79 | (** [request_of_task status t] takes the [status] of the worker |
| 80 | and a task [t] and creates the corresponding [Some request] to be |
| 81 | sent to the worker or it is not valid anymore [None]. *) |
| 82 | val request_of_task : worker_status -> task -> request option |
| 83 | |
| 84 | (** [task_match status tid] Allows to discard tasks based on the |
| 85 | worker status. *) |
| 86 | val task_match : worker_status -> task -> bool |
| 87 | |
| 88 | (** [use_response status t out] |
| 89 | |
| 90 | For a response [out] to a task [t] with [status] we can choose |
| 91 | to end the worker of to keep it alive with some data and |
| 92 | immediately inject extra tasks in the queue. |
| 93 | |
| 94 | For example, the proof worker runs a proof and finds an error, |
| 95 | the response signals that, e.g. |
| 96 | |
| 97 | [ReponseError {state = 34; msg = "oops"}] |
| 98 | |
| 99 | When the manager uses such a response he can tell the worker to |
| 100 | stay there and inject into the queue an extra task requesting |
| 101 | state 33 (to implement efficient proof repair). *) |
| 102 | val use_response : worker_status -> task -> response -> |
| 103 | [ `Stay of competence * task list | `End ] |
| 104 | |
| 105 | (** [on_marshal_error err_msg tid] notifies of marshaling failure. *) |
| 106 | val on_marshal_error : string -> task -> unit |
| 107 | |
| 108 | (** [on_task_cancellation_or_expiration_or_slave_death tid] |
| 109 | |
| 110 | These functions are meant to parametrize the worker manager on |
| 111 | the actions to be taken when things go wrong or are cancelled |
| 112 | (you can kill a worker in RocqIDE, or using kill -9...) |
| 113 | |
| 114 | E.g. master can decide to inhabit the (delegate) Future.t with a |
| 115 | closure (to be run in master), i.e. make the document still |
| 116 | checkable. This is what I do for marshaling errors. *) |
| 117 | val on_task_cancellation_or_expiration_or_slave_death : task option -> unit |
| 118 | |
| 119 | (** [forward_feedback fb] sends fb to all the workers. *) |
| 120 | val forward_feedback : Feedback.feedback -> unit |
| 121 | |
| 122 | (** {5 Worker API, it is run by worker, on a different fresh |
| 123 | process} *) |
| 124 | |
| 125 | (** [perform in] synchronously processes a request [in] *) |
| 126 | val perform : request -> response |
| 127 | |
| 128 | (** debugging *) |
| 129 | val name_of_task : task -> string |
| 130 | val name_of_request : request -> string |
| 131 | |
| 132 | end |
| 133 | |
| 134 | (** [cancel_switch] to be flipped to true by anyone to signal the task |
| 135 | is not relevant anymore. When the STM performs an undo/edit-at, it |
| 136 | crawls the document and flips these flags (the Qed node carries a |
| 137 | pointer to the flag IIRC). |
| 138 | *) |
| 139 | type cancel_switch = bool ref |
| 140 | |
| 141 | (** Client-side functor. [MakeQueue T] creates a task queue for task [T] *) |
| 142 | module MakeQueue(T : Task) () : sig |
| 143 | |
| 144 | (** [queue] is the abstract queue type. *) |
| 145 | type queue |
| 146 | |
| 147 | (** [create n pri] will initialize the queue with [n] workers having |
| 148 | priority [pri]. If [n] is 0, the queue won't spawn any process, |
| 149 | working in a lazy local manner. [not imposed by the this API] *) |
| 150 | val create : spawn_args:string list -> int -> CoqworkmgrApi.priority -> queue |
| 151 | |
| 152 | (** [destroy q] Deallocates [q], cancelling all pending tasks. *) |
| 153 | val destroy : queue -> unit |
| 154 | |
| 155 | (** [n_workers q] returns the number of workers of [q] *) |
| 156 | val n_workers : queue -> int |
| 157 | |
| 158 | (** [enqueue_task q t ~cancel_switch] schedules [t] for execution in |
| 159 | [q]. [cancel_switch] can be flipped to true to cancel the task. *) |
| 160 | val enqueue_task : queue -> T.task -> cancel_switch:cancel_switch -> unit |
| 161 | |
| 162 | (** [join q] blocks until the task queue is empty *) |
| 163 | val join : queue -> unit |
| 164 | |
| 165 | (** [cancel_all q] Cancels all tasks *) |
| 166 | val cancel_all : queue -> unit |
| 167 | |
| 168 | (** [cancel_worker q wid] cancels a particular worker [wid] *) |
| 169 | val cancel_worker : queue -> WorkerPool.worker_id -> unit |
| 170 | |
| 171 | (** [set_order q cmp] reorders [q] using ordering [cmp] *) |
| 172 | val set_order : queue -> (T.task -> T.task -> int) -> unit |
| 173 | |
| 174 | (** [broadcast q] |
| 175 | |
| 176 | This is nasty. Workers can be picky, e.g. pick tasks only when |
| 177 | they are "on screen". Of course the screen is scrolled, and that |
| 178 | changes the potential choice of workers to pick up a task or |
| 179 | not. |
| 180 | |
| 181 | This function wakes up the workers (the managers) that give a |
| 182 | look (again) to the tasks in the queue. |
| 183 | |
| 184 | The STM calls it when the perspective (as in PIDE) changes. |
| 185 | |
| 186 | A problem here is that why task_match has access to the |
| 187 | competence data in order to decide if the task is palatable to |
| 188 | the worker or not... such data is local to the worker (manager). |
| 189 | The perspective is global, so it does not quite fit this |
| 190 | picture. This API to make all managers reconsider the tasks in |
| 191 | the queue is the best I could came up with. |
| 192 | |
| 193 | This API is crucial to Coqoon (or any other UI that invokes |
| 194 | Stm.finish eagerly but wants the workers to "focus" on the visible |
| 195 | part of the document). |
| 196 | *) |
| 197 | val broadcast : queue -> unit |
| 198 | |
| 199 | (** [snapshot q] Takes a snapshot (non destructive but waits until |
| 200 | all workers are enqueued) *) |
| 201 | val snapshot : queue -> T.task list |
| 202 | |
| 203 | (** [clear q] Clears [q], only if the worker prool is empty *) |
| 204 | val clear : queue -> unit |
| 205 | |
| 206 | (** [with_n_workers n pri f] creates a queue, runs the function, destroys |
| 207 | the queue. The user should call join *) |
| 208 | val with_n_workers : spawn_args:string list -> int -> CoqworkmgrApi.priority -> (queue -> 'a) -> 'a |
| 209 | |
| 210 | end |
| 211 | |
| 212 | (** Server-side functor. [MakeWorker T] creates the server task |
| 213 | dispatcher. *) |
| 214 | module MakeWorker(_ : Task) () : sig |
| 215 | |
| 216 | (** [init_stdout ()] is called at [Coqtop.toploop_init] time. *) |
| 217 | val init_stdout : unit -> unit |
| 218 | |
| 219 | (** [main_loop ()] is called at [Coqtop.toploop_run] time. *) |
| 220 | val main_loop : unit -> unit |
| 221 | |
| 222 | end |
| 223 | |
| 224 | (** convenience exception to marshall to master *) |
| 225 | exception RemoteException of Pp.t |