Skip to content

Instantly share code, notes, and snippets.

@toolslive
Last active December 19, 2015 05:59
Show Gist options
  • Save toolslive/5908501 to your computer and use it in GitHub Desktop.
Save toolslive/5908501 to your computer and use it in GitHub Desktop.
oversleeping lwt threads?
open Lwt
let n = 1_000_000_000
let time f =
let t0 = Unix.gettimeofday () in
let a = f () in
let t1 = Unix.gettimeofday () in
a,(t1-.t0)
let waste_cycles () =
for vjvj = 0 to n do
ignore (1 + 1)
done
let loop until =
let bs = 4096 in
let buffer = String.create bs in
let rec inner ic =
if (Unix.gettimeofday ()) < until
then
begin
Lwt_log.debug "hey" >>= fun () ->
Lwt_io.read_into ic buffer 0 bs >>= fun bytes_read ->
Lwt_io.printf "." >>= fun () ->
Lwt_preemptive.detach (fun () -> time waste_cycles) ()
>>= fun (_,d) ->
Lwt_io.printlf "wasting_cycles took: %f" d >>= fun () ->
inner ic
end
else Lwt.return "loop finished first"
in
Lwt_io.with_file ~mode:Lwt_io.input "/dev/zero" inner
let race loop_duration =
let time = Unix.gettimeofday () in
let make_loop () = loop (time +. loop_duration) in
Lwt.pick [
make_loop ();
make_loop ();
make_loop ();
make_loop ();
make_loop ();
make_loop ();
make_loop ();
make_loop ();
make_loop ();
make_loop ();
make_loop ();
make_loop ();
begin
print_endline "going to sleep";
let t0 = Unix.gettimeofday() in
Lwt_unix.sleep 0.1 >>= fun () ->
let t1 = Unix.gettimeofday() in
let d = t1 -. t0 in
let msg = Printf.sprintf "sleep finished first (%f)" d in
Lwt.return msg
end
]
>>= fun s ->
Lwt_io.printl s >>= fun () ->
Lwt_io.printlf "pick took %f" (Unix.gettimeofday () -. time)
>>= fun () ->
Lwt.return ();;
let main () =
Lwt_main.run (race 1.);
Lwt_main.run (race 2.);
Lwt_main.run (race 3.);
Lwt_main.run (race 4.)
in
main ()
@toolslive
Copy link
Author

this is a sample run:
"""
./overslept.native
going to sleep
.wasting_cycles took: 0.613341
.wasting_cycles took: 0.610212
.wasting_cycles took: 0.611088
.wasting_cycles took: 0.610588
.wasting_cycles took: 0.606665
.wasting_cycles took: 0.606437
.wasting_cycles took: 0.606513
.wasting_cycles took: 0.608152
.wasting_cycles took: 0.610700
.wasting_cycles took: 0.610404
.wasting_cycles took: 0.606866
.wasting_cycles took: 0.606766
loop finished first
pick took 7.309896
going to sleep
.wasting_cycles took: 0.617117
.wasting_cycles took: 0.612003
.wasting_cycles took: 0.614682
.wasting_cycles took: 0.610438
.wasting_cycles took: 0.615222
.wasting_cycles took: 0.614679
.wasting_cycles took: 0.616298
.wasting_cycles took: 0.616601
.wasting_cycles took: 0.615361
.wasting_cycles took: 0.613952
.wasting_cycles took: 0.617605
.wasting_cycles took: 0.617004
going to sleep
loop finished first
pick took 7.382970
.wasting_cycles took: 0.618455
.wasting_cycles took: 0.617348
.wasting_cycles took: 0.653185
.wasting_cycles took: 0.616722
.wasting_cycles took: 0.609029
.wasting_cycles took: 0.608665
.wasting_cycles took: 0.606755
.wasting_cycles took: 0.606418
.wasting_cycles took: 0.606247
.wasting_cycles took: 0.606453
.wasting_cycles took: 0.606747
.wasting_cycles took: 0.606712
going to sleep
loop finished first
pick took 7.364616
.wasting_cycles took: 0.610054
.wasting_cycles took: 0.606610
.wasting_cycles took: 0.606066
.wasting_cycles took: 0.610830
.wasting_cycles took: 0.606513
.wasting_cycles took: 0.608504
.wasting_cycles took: 0.607910
.wasting_cycles took: 0.606809
.wasting_cycles took: 0.607453
.wasting_cycles took: 0.607469
.wasting_cycles took: 0.607701
.wasting_cycles took: 0.606453
loop finished first
pick took 7.294549
"""

@toolslive
Copy link
Author

with preemptive_detach, it's a bit better, but still akward.
"""
./overslept.native
going to sleep
.sleep finished first (0.620626)
pick took 0.620738
wasting_cycles took: 0.619400
............going to sleep
wasting_cycles took: 0.619840
wasting_cycles took: 0.619497
wasting_cycles took: 0.627495
wasting_cycles took: 0.626590
............sleep finished first (1.885461)
wasting_cycles took: 0.642846
wasting_cycles took: 0.619656
wasting_cycles took: 0.621365
pick took 3.752946
going to sleep
wasting_cycles took: 0.623798
wasting_cycles took: 0.618940
wasting_cycles took: 0.623775
wasting_cycles took: 0.618194
............sleep finished first (1.915955)
wasting_cycles took: 0.626672
wasting_cycles took: 0.645118
wasting_cycles took: 0.642509
pick took 3.164240
wasting_cycles took: 0.629577
going to sleep
............sleep finished first (2.494716)
wasting_cycles took: 0.620260
wasting_cycles took: 0.625525
wasting_cycles took: 0.626764
pick took 2.494976
wasting_cycles took: 0.620831
....wasting_cycles took: 0.625636
wasting_cycles took: 0.622160
wasting_cycles took: 0.629618
"""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment