| 1 | |
| 2 | open CoqworkmgrApi |
| 3 | |
| 4 | let debug = ref false |
| 5 | |
| 6 | type party = { |
| 7 | sock : Unix.file_descr; |
| 8 | cout : out_channel; |
| 9 | mutable tokens : int; |
| 10 | priority : priority; |
| 11 | } |
| 12 | |
| 13 | let answer party msg = |
| 14 | output_string party.cout (print_response msg); flush party.cout |
| 15 | |
| 16 | let mk_socket_channel () = |
| 17 | let open Unix in |
| 18 | let s = socket PF_INET SOCK_STREAM 0 in |
| 19 | bind s (ADDR_INET (inet_addr_loopback,0)); |
| 20 | listen s 1; |
| 21 | match getsockname s with |
| 22 | | ADDR_INET(host, port) -> |
| 23 | s, string_of_inet_addr host ^":"^ string_of_int port |
| 24 | | _ -> assert false |
| 25 | |
| 26 | module Queue : sig |
| 27 | type t |
| 28 | val is_empty : t -> bool |
| 29 | val push : int * party -> t -> unit |
| 30 | val pop : t -> int * party |
| 31 | val create : unit -> t |
| 32 | end = struct |
| 33 | type t = (int * party) list ref |
| 34 | let create () = ref [] |
| 35 | let is_empty q = !q = [] |
| 36 | let rec split acc = function |
| 37 | | [] -> List.rev acc, [] |
| 38 | | (_, { priority = Low }) :: _ as l -> List.rev acc, l |
| 39 | | x :: xs -> split (x :: acc) xs |
| 40 | let push (_,{ priority } as item) q = |
| 41 | if priority = Low then q := !q @ [item] |
| 42 | else |
| 43 | let high, low = split [] !q in |
| 44 | q := high @ (item :: low) |
| 45 | let pop q = match !q with x :: xs -> q := xs; x | _ -> assert false |
| 46 | end |
| 47 | |
| 48 | let read_fd fd s ~off ~len = |
| 49 | let rec loop () = |
| 50 | try Unix.read fd s off len |
| 51 | with Unix.Unix_error(Unix.EAGAIN,_,_) -> loop () |
| 52 | in |
| 53 | loop () |
| 54 | |
| 55 | let really_read_fd fd s off len = |
| 56 | let i = ref 0 in |
| 57 | while !i < len do |
| 58 | let off = off + !i in |
| 59 | let len = len - !i in |
| 60 | let r = read_fd fd s ~off ~len in |
| 61 | if r = 0 then raise End_of_file; |
| 62 | i := !i + r |
| 63 | done |
| 64 | |
| 65 | let raw_input_line fd = |
| 66 | try |
| 67 | let b = Buffer.create 80 in |
| 68 | let s = Bytes.make 1 '\000' in |
| 69 | let endl = Bytes.of_string "\n" in |
| 70 | let endr = Bytes.of_string "\r" in |
| 71 | while Bytes.compare s endl <> 0 do |
| 72 | really_read_fd fd s 0 1; |
| 73 | if Bytes.compare s endl <> 0 && Bytes.compare s endr <> 0 |
| 74 | then Buffer.add_bytes b s; |
| 75 | done; |
| 76 | Buffer.contents b |
| 77 | with Unix.Unix_error _ -> raise End_of_file |
| 78 | |
| 79 | let accept s = |
| 80 | let cs, _ = Unix.accept s in |
| 81 | let cout = Unix.out_channel_of_descr cs in |
| 82 | set_binary_mode_out cout true; |
| 83 | match parse_request (raw_input_line cs) with |
| 84 | | Hello p -> { sock=cs; cout; tokens=0; priority=p } |
| 85 | | _ -> (try Unix.close cs with _ -> ()); raise End_of_file |
| 86 | |
| 87 | let parties = ref [] |
| 88 | |
| 89 | let max_tokens = ref 2 |
| 90 | let cur_tokens = ref 0 |
| 91 | |
| 92 | let queue = Queue.create () |
| 93 | |
| 94 | let rec allocate n party = |
| 95 | let extra = min n (!max_tokens - !cur_tokens) in |
| 96 | cur_tokens := !cur_tokens + extra; |
| 97 | party.tokens <- party.tokens + extra; |
| 98 | answer party (Tokens extra) |
| 99 | |
| 100 | and de_allocate n party = |
| 101 | let back = min party.tokens n in |
| 102 | party.tokens <- party.tokens - back; |
| 103 | cur_tokens := min (!cur_tokens - back) !max_tokens; |
| 104 | eventually_dequeue () |
| 105 | |
| 106 | and eventually_dequeue () = |
| 107 | if Queue.is_empty queue || !cur_tokens >= !max_tokens then () |
| 108 | else |
| 109 | let req, party = Queue.pop queue in |
| 110 | if List.exists (fun { sock } -> sock = party.sock) !parties |
| 111 | then allocate req party |
| 112 | else eventually_dequeue () |
| 113 | |
| 114 | let chat s = |
| 115 | let party = |
| 116 | try List.find (fun { sock } -> sock = s) !parties |
| 117 | with Not_found -> Printf.eprintf "Internal error"; exit 1 in |
| 118 | try |
| 119 | match parse_request (raw_input_line party.sock) with |
| 120 | | Get n -> |
| 121 | if !cur_tokens < !max_tokens then allocate n party |
| 122 | else Queue.push (n,party) queue |
| 123 | | TryGet n -> |
| 124 | if !cur_tokens < !max_tokens then allocate n party |
| 125 | else answer party Noluck |
| 126 | | GiveBack n -> de_allocate n party |
| 127 | | Ping -> |
| 128 | answer party (Pong (!cur_tokens,!max_tokens,Unix.getpid ())); |
| 129 | raise End_of_file |
| 130 | | Hello _ -> raise End_of_file |
| 131 | with Failure _ | ParseError | Sys_error _ | End_of_file -> |
| 132 | (try Unix.close party.sock with _ -> ()); |
| 133 | parties := List.filter (fun { sock } -> sock <> s) !parties; |
| 134 | de_allocate party.tokens party; |
| 135 | eventually_dequeue () |
| 136 | |
| 137 | let check_alive s = |
| 138 | match CoqworkmgrApi.connect s with |
| 139 | | Some s -> |
| 140 | let cout = Unix.out_channel_of_descr s in |
| 141 | set_binary_mode_out cout true; |
| 142 | output_string cout (print_request (Hello Low)); flush cout; |
| 143 | output_string cout (print_request Ping); flush cout; |
| 144 | begin match Unix.select [s] [] [] 1.0 with |
| 145 | | [s],_,_ -> |
| 146 | let cin = Unix.in_channel_of_descr s in |
| 147 | set_binary_mode_in cin true; |
| 148 | begin match parse_response (input_line cin) with |
| 149 | | Pong (n,m,pid) -> n, m, pid |
| 150 | | _ -> raise Not_found |
| 151 | end |
| 152 | | _ -> raise Not_found |
| 153 | end |
| 154 | | _ -> raise Not_found |
| 155 | |
| 156 | let parse_args argv = |
| 157 | let args = [ |
| 158 | "-j",Arg.Set_int max_tokens, "max number of concurrent jobs"; |
| 159 | "-d",Arg.Set debug, "do not detach (debug)"] in |
| 160 | let usage = |
| 161 | "Prints on stdout an env variable assignment to be picked up by rocq\n"^ |
| 162 | "instances in order to limit the maximum number of concurrent workers.\n"^ |
| 163 | "The default value is 2.\n"^ |
| 164 | "Usage:" in |
| 165 | try |
| 166 | Arg.parse_argv argv args (fun extra -> |
| 167 | Arg.usage args ("Unexpected argument "^extra^".\n"^usage)) |
| 168 | usage |
| 169 | with |
| 170 | | Arg.Bad msg -> Printf.eprintf "%s" msg; exit 2 |
| 171 | | Arg.Help msg -> Printf.printf "%s" msg; exit 0 |
| 172 | |
| 173 | let main ~prog argv = |
| 174 | parse_args (Array.of_list (prog::argv)); |
| 175 | try |
| 176 | let sock = |
| 177 | let rocq = "ROCQWORKMGR_SOCK" in |
| 178 | let coq = "COQWORKMGR_SOCK" in |
| 179 | try Sys.getenv rocq with Not_found -> |
| 180 | let s = Sys.getenv coq in |
| 181 | Printf.eprintf "Deprecated environment variable %s, use %s instead.\n%!" coq rocq; |
| 182 | s |
| 183 | in |
| 184 | if !debug then Printf.eprintf "Contacting %s\n%!" sock; |
| 185 | let cur, max, pid = check_alive sock in |
| 186 | Printf.printf "ROCQWORKMGR_SOCK=%s\n%!" sock; |
| 187 | Printf.eprintf |
| 188 | "rocqworkmgr already up and running (pid=%d, socket=%s, j=%d/%d)\n%!" |
| 189 | pid sock cur max; |
| 190 | exit 0 |
| 191 | with Not_found | Failure _ | Invalid_argument _ | Unix.Unix_error _ -> |
| 192 | if !debug then Printf.eprintf "No running instance. Starting a new one\n%!"; |
| 193 | let master, str = mk_socket_channel () in |
| 194 | if not !debug then begin |
| 195 | let pid = Unix.fork () in |
| 196 | if pid <> 0 then begin |
| 197 | Printf.printf "ROCQWORKMGR_SOCK=%s\n%!" str; |
| 198 | exit 0 |
| 199 | end else begin |
| 200 | ignore(Unix.setsid ()); |
| 201 | Unix.close Unix.stdin; |
| 202 | Unix.close Unix.stdout; |
| 203 | end; |
| 204 | end else begin |
| 205 | Printf.printf "ROCQWORKMGR_SOCK=%s\n%!" str; |
| 206 | end; |
| 207 | Sys.catch_break true; |
| 208 | try |
| 209 | while true do |
| 210 | if !debug then |
| 211 | Printf.eprintf "Status: #parties=%d tokens=%d/%d \n%!" |
| 212 | (List.length !parties) !cur_tokens !max_tokens; |
| 213 | let socks = master :: List.map (fun { sock } -> sock) !parties in |
| 214 | let r, _, _ = Unix.select socks [] [] (-1.0) in |
| 215 | List.iter (fun s -> |
| 216 | if s = master then begin |
| 217 | try parties := accept master :: !parties |
| 218 | with _ -> () |
| 219 | end else chat s) |
| 220 | r |
| 221 | done; |
| 222 | exit 0 |
| 223 | with Sys.Break -> |
| 224 | if !parties <> [] then begin |
| 225 | Printf.eprintf "Some rocq processes still need me\n%!"; |
| 226 | exit 1; |
| 227 | end else |
| 228 | exit 0 |