Source file miou_unix.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
module Logs = Miou.Logs.Make (struct
let src = "unix"
end)
type file_descr = { fd: Unix.file_descr; non_blocking: bool }
let of_file_descr ?(non_blocking = true) fd =
if non_blocking then Unix.set_nonblock fd else Unix.clear_nonblock fd;
{ fd; non_blocking }
let to_file_descr { fd; _ } = fd
let tcpv4 () =
let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
Unix.set_nonblock fd; { fd; non_blocking= true }
let tcpv6 () =
let fd = Unix.socket Unix.PF_INET6 Unix.SOCK_STREAM 0 in
Unix.set_nonblock fd; { fd; non_blocking= true }
let bind_and_listen ?(backlog = 64) ?(reuseaddr = true) ?(reuseport = true)
{ fd; _ } sockaddr =
Unix.setsockopt fd Unix.SO_REUSEADDR reuseaddr;
Unix.setsockopt fd Unix.SO_REUSEPORT reuseport;
Unix.bind fd sockaddr;
Unix.listen fd backlog
module File_descrs = struct
type 'a t = { mutable contents: (Unix.file_descr * 'a) list }
let find tbl fd = List.assq fd tbl.contents
let remove tbl fd =
let contents =
List.fold_left
(fun acc (k, v) -> if k == fd then acc else (k, v) :: acc)
[] tbl.contents
in
tbl.contents <- contents
let iter f tbl = List.iter (fun (k, v) -> f k v) tbl.contents
let replace tbl fd v' =
let contents =
List.fold_left
(fun acc (k, v) -> if k == fd then (k, v') :: acc else (k, v) :: acc)
[] tbl.contents
in
tbl.contents <- contents
let add tbl k v = tbl.contents <- (k, v) :: tbl.contents
let clear tbl = tbl.contents <- []
let create _ = { contents= [] }
end
type elt = { time: float; syscall: Miou.syscall; mutable cancelled: bool }
module Heapq = Miou.Pqueue.Make (struct
type t = elt
let dummy = { time= 0.0; syscall= Obj.magic (); cancelled= false }
let compare { time= a; _ } { time= b; _ } = Float.compare a b
end)
type domain = {
readers: Miou.syscall list File_descrs.t
; writers: Miou.syscall list File_descrs.t
; sleepers: Heapq.t
; revert: (Miou.uid, Unix.file_descr) Hashtbl.t
}
let clean domain uids =
let clean uid' (fd : Unix.file_descr) tbl =
match File_descrs.find tbl fd with
| exception Not_found -> ()
| syscalls -> (
match List.filter (fun s -> uid' <> Miou.uid s) syscalls with
| [] -> File_descrs.remove tbl fd
| syscalls -> File_descrs.replace tbl fd syscalls)
in
let clean uid =
match Hashtbl.find domain.revert uid with
| fd ->
clean uid fd domain.readers;
clean uid fd domain.writers
| exception Not_found -> ()
in
List.iter clean uids;
List.iter (Hashtbl.remove domain.revert) uids;
let clean ({ syscall; _ } as elt) =
if List.exists (( = ) (Miou.uid syscall)) uids then elt.cancelled <- true
in
Heapq.iter clean domain.sleepers
let rec drop_heapq heapq =
try Heapq.delete_min_exn heapq; drop_heapq heapq with _ -> ()
let domain =
let rec split_from_parent v =
File_descrs.clear v.readers;
File_descrs.clear v.writers;
drop_heapq v.sleepers;
Hashtbl.clear v.revert;
make ()
and make () =
{
readers= File_descrs.create 0x100
; writers= File_descrs.create 0x100
; sleepers= Heapq.create ()
; revert= Hashtbl.create 0x100
}
in
let key = Stdlib.Domain.DLS.new_key ~split_from_parent make in
fun () -> Stdlib.Domain.DLS.get key
let append tbl fd syscall =
try
let syscalls = File_descrs.find tbl fd in
File_descrs.replace tbl fd (syscall :: syscalls)
with Not_found -> File_descrs.add tbl fd [ syscall ]
let blocking_read fd =
let syscall = Miou.syscall () in
let uid = Miou.uid syscall in
let domain = domain () in
Hashtbl.replace domain.revert uid fd;
Logs.debug (fun m -> m "append [%d] as a reader" (Obj.magic fd));
append domain.readers fd syscall;
Miou.suspend syscall
let blocking_write fd =
let syscall = Miou.syscall () in
let uid = Miou.uid syscall in
let domain = domain () in
Hashtbl.replace domain.revert uid fd;
append domain.writers fd syscall;
Miou.suspend syscall
let rec unsafe_read ({ fd; non_blocking } as file_descr) off len buf =
if non_blocking then
match Unix.read fd buf off len with
| exception Unix.(Unix_error (EINTR, _, _)) ->
unsafe_read file_descr off len buf
| exception Unix.(Unix_error ((EAGAIN | EWOULDBLOCK), _, _)) ->
blocking_read fd;
unsafe_read file_descr off len buf
| value -> value
else
let rec go () =
match Unix.read fd buf off len with
| exception Unix.(Unix_error (EINTR, _, _)) -> go ()
| value -> value
in
blocking_read fd; go ()
let read file_descr ?(off = 0) ?len buf =
let len = match len with None -> Bytes.length buf - off | Some len -> len in
if off < 0 || len < 0 || off > Bytes.length buf - len then
invalid_arg "Miou_unix.read";
unsafe_read file_descr off len buf
let rec really_read_go file_descr off len buf =
let len' = unsafe_read file_descr off len buf in
if len' == 0 then raise End_of_file
else if len - len' > 0 then
really_read_go file_descr (off + len') (len - len') buf
let really_read file_descr ?(off = 0) ?len buf =
let len = match len with None -> Bytes.length buf - off | Some len -> len in
if off < 0 || len < 0 || off > Bytes.length buf - len then
invalid_arg "Miou_unix.really_read";
if len > 0 then really_read_go file_descr off len buf
let rec unsafe_write ({ fd; non_blocking } as file_descr) off len str =
if non_blocking then
match Unix.write fd (Bytes.unsafe_of_string str) off len with
| exception Unix.(Unix_error (EINTR, _, _)) ->
unsafe_write file_descr off len str
| exception Unix.(Unix_error ((EAGAIN | EWOULDBLOCK), _, _)) ->
blocking_write fd;
unsafe_write file_descr off len str
| len' when len' < len ->
unsafe_write file_descr (off + len') (len - len') str
| _ -> ()
else
let rec go () =
match Unix.write fd (Bytes.unsafe_of_string str) off len with
| exception Unix.(Unix_error (EINTR, _, _)) -> go ()
| len' when len' < len ->
unsafe_write file_descr (off + len') (len - len') str
| _ -> ()
in
blocking_write fd; go ()
let write file_descr ?(off = 0) ?len str =
let len =
match len with None -> String.length str - off | Some len -> len
in
if off < 0 || len < 0 || off > String.length str - len then
invalid_arg "Miou_unix.write";
unsafe_write file_descr off len str
let rec accept ?cloexec ({ fd; non_blocking } as file_descr) =
if non_blocking then (
match Unix.accept ?cloexec fd with
| exception Unix.(Unix_error (EINTR, _, _)) -> accept ?cloexec file_descr
| exception Unix.(Unix_error ((EAGAIN | EWOULDBLOCK), _, _)) ->
blocking_read fd; accept ?cloexec file_descr
| fd, sockaddr ->
Unix.set_nonblock fd;
let file_descr = { fd; non_blocking= true } in
(file_descr, sockaddr))
else
let rec go () =
match Unix.accept ?cloexec fd with
| exception Unix.(Unix_error (EINTR, _, _)) -> go ()
| fd, sockaddr ->
Unix.set_nonblock fd;
let file_descr = { fd; non_blocking= true } in
(file_descr, sockaddr)
in
blocking_read fd; go ()
let rec connect ({ fd; non_blocking } as file_descr) sockaddr =
if not non_blocking then
invalid_arg
"Miou_unix.connect: we expect a file descriptor in the non-blocking mode";
match Unix.connect fd sockaddr with
| () -> ()
| exception Unix.(Unix_error (EINTR, _, _)) -> connect file_descr sockaddr
| exception Unix.(Unix_error (EINPROGRESS, _, _)) -> (
blocking_write fd;
match Unix.getsockopt_error fd with
| None -> ()
| Some err -> raise (Unix.Unix_error (err, "connect", "")))
let close { fd; _ } = Unix.close fd
let sleep until =
let syscall = Miou.syscall () in
let domain = domain () in
let elt =
{ time= Unix.gettimeofday () +. until; syscall; cancelled= false }
in
Heapq.insert elt domain.sleepers;
Miou.suspend syscall
module Ownership = struct
type old = file_descr
type file_descr = {
fd: Unix.file_descr
; non_blocking: bool
; resource: Miou.Ownership.t
}
let bind_and_listen ?backlog { fd; non_blocking; resource } sockaddr =
Miou.Ownership.check resource;
bind_and_listen ?backlog { fd; non_blocking } sockaddr
let read { fd; non_blocking; resource } ?off ?len buf =
Miou.Ownership.check resource;
read { fd; non_blocking } ?off ?len buf
let really_read { fd; non_blocking; resource } ?off ?len buf =
Miou.Ownership.check resource;
really_read { fd; non_blocking } ?off ?len buf
let write { fd; non_blocking; resource } ?off ?len str =
Miou.Ownership.check resource;
write { fd; non_blocking } ?off ?len str
let accept ?cloexec { fd; non_blocking; resource } =
Miou.Ownership.check resource;
let ({ fd; non_blocking } : old), sockaddr =
accept ?cloexec { fd; non_blocking }
in
let resource = Miou.Ownership.create ~finally:Unix.close fd in
Miou.Ownership.own resource;
({ fd; non_blocking; resource }, sockaddr)
let connect { fd; non_blocking; resource } sockaddr =
Miou.Ownership.check resource;
connect { fd; non_blocking } sockaddr
let close { fd; resource; _ } =
Miou.Ownership.disown resource;
Unix.close fd
let of_file_descr ?(non_blocking = true) fd =
let resource = Miou.Ownership.create ~finally:Unix.close fd in
if non_blocking then Unix.set_nonblock fd else Unix.clear_nonblock fd;
Miou.Ownership.own resource;
{ fd; non_blocking; resource }
let to_file_descr { fd; _ } = fd
let resource { resource; _ } = resource
let tcpv4 () =
let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
let resource = Miou.Ownership.create ~finally:Unix.close fd in
Unix.set_nonblock fd;
Miou.Ownership.own resource;
{ fd; non_blocking= true; resource }
let tcpv6 () =
let fd = Unix.socket Unix.PF_INET6 Unix.SOCK_STREAM 0 in
let resource = Miou.Ownership.create ~finally:Unix.close fd in
Unix.set_nonblock fd;
Miou.Ownership.own resource;
{ fd; non_blocking= true; resource }
end
let rec sleeper domain =
match Heapq.find_min_exn domain.sleepers with
| exception Heapq.Empty -> None
| { cancelled= true; _ } ->
Heapq.delete_min_exn domain.sleepers;
sleeper domain
| { time; _ } -> Some time
let in_the_past t = t = 0. || t <= Unix.gettimeofday ()
let rec collect domain signals =
match Heapq.find_min_exn domain.sleepers with
| exception Heapq.Empty -> signals
| { cancelled= true; _ } ->
Heapq.delete_min_exn domain.sleepers;
collect domain signals
| { time; syscall; _ } when in_the_past time ->
Heapq.delete_min_exn domain.sleepers;
collect domain (Miou.signal syscall :: signals)
| _ -> signals
let file_descrs tbl =
let res = ref [] in
File_descrs.iter (fun k _ -> res := k :: !res) tbl;
!res
let transmit_fds signals revert tbl fds =
let fold acc fd =
match File_descrs.find tbl fd with
| [] -> File_descrs.remove tbl fd; acc
| syscalls ->
let transmit syscall =
Hashtbl.remove revert (Miou.uid syscall);
Miou.signal syscall
in
Logs.debug (fun m -> m "delete [%d]" (Obj.magic fd));
File_descrs.remove tbl fd;
List.rev_append (List.rev_map transmit syscalls) acc
| exception Not_found -> acc
in
List.fold_left fold signals fds
let interrupted fd fds = List.exists (( = ) fd) fds
let intr fd =
let buf = Bytes.create 0x100 in
match Unix.read fd buf 0 (Bytes.length buf) with
| _ -> ()
| exception Unix.(Unix_error (EAGAIN, _, _)) -> ()
let select uid interrupt ~block cancelled_syscalls =
let domain = domain () in
clean domain cancelled_syscalls;
let rds = file_descrs domain.readers in
let wrs = file_descrs domain.writers in
let timeout =
match (sleeper domain, block) with
| None, true -> -1.0
| (None | Some _), false -> 0.0
| Some value, true ->
let value = value -. Unix.gettimeofday () in
Float.max value 0.0
in
Logs.debug (fun m ->
m "[%a] [readers:%dw, writers:%dw, sleepers:%dw, revert:%dw]"
Miou.Domain.Uid.pp uid
Obj.(reachable_words (repr domain.readers))
Obj.(reachable_words (repr domain.writers))
Obj.(reachable_words (repr domain.sleepers))
Obj.(reachable_words (repr domain.revert)));
Logs.debug (fun m -> m "select(%f)" timeout);
match Unix.select (interrupt :: rds) wrs [] timeout with
| exception Unix.(Unix_error (EINTR, _, _)) ->
Logs.debug (fun m ->
m "[%a] interrupted by the system" Miou.Domain.Uid.pp uid);
collect domain []
| [], [], _ -> collect domain []
| rds, wrs, _ ->
if interrupted interrupt rds then begin
Logs.debug (fun m ->
m "[%a] interrupted by Miou" Miou.Domain.Uid.pp uid);
intr interrupt
end;
let signals = collect domain [] in
let signals = transmit_fds signals domain.revert domain.readers rds in
let signals = transmit_fds signals domain.revert domain.writers wrs in
signals
let signal = Bytes.make 1 '\000'
let interrupt oc () =
match Unix.single_write oc signal 0 1 with
| n -> assert (n = 1)
| exception Unix.(Unix_error (EAGAIN, _, _)) -> ()
let events domain =
let ic, oc = Unix.pipe ~cloexec:true () in
Unix.set_nonblock ic;
Unix.set_nonblock oc;
let select = select domain ic in
let finaliser () = Unix.close ic; Unix.close oc in
{ Miou.interrupt= interrupt oc; select; finaliser }
let run ?g ?domains fn = Miou.run ~events ?g ?domains fn