Skip to content

Commit

Permalink
feat: make runtime shutdowns fast
Browse files Browse the repository at this point in the history
  • Loading branch information
leostera committed Oct 19, 2024
1 parent c2d5266 commit 0c728e3
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 32 deletions.
2 changes: 1 addition & 1 deletion bench/http_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ let main () =
let _ = List.init 99 (fun _ -> spawn_link acceptor) in
acceptor ()

let () = Riot.run ~workers:0 @@ main
let () = Riot.run ~config:(Config.make ~workers:0 ()) @@ main
11 changes: 11 additions & 0 deletions packages/riot-runtime/Config.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type t = { rnd : Random.State.t; max_workers : int; workers : int }

let make ?workers () =
let max_workers = Int.max 0 (Stdlib.Domain.recommended_domain_count () - 2) in
let workers =
match workers with Some w -> Int.min w max_workers | None -> max_workers
in
let rnd = Random.State.make_self_init () in
{ rnd; max_workers; workers }

let default () = make ()
5 changes: 3 additions & 2 deletions packages/riot-runtime/riot_runtime.ml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module Log = Log
module Config = Config
module Core = Core
module Import = Import
module Util = Util
module Log = Log
module Scheduler = Scheduler
module Time = Time
module Util = Util

let set_log_level = Log.set_log_level
31 changes: 14 additions & 17 deletions packages/riot/riot.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ include Riot_stdlib

open struct
open Riot_runtime
module Config = Config
module Log = Log
module Core = Core
module Import = Import
Expand All @@ -10,6 +11,8 @@ open struct
module Time = Time
end

module Config = Config

open Logger.Make (struct
let namespace = [ "riot" ]
end)
Expand All @@ -23,30 +26,25 @@ let shutdown ?(status = 0) () =

let started = ref false

let run ?(rnd = Random.State.make_self_init ()) ?workers main =
let run ?(config = Config.default ()) main =
if !started then raise Riot_already_started else started := true;

let max_workers = Int.max 0 (Stdlib.Domain.recommended_domain_count () - 2) in
let workers =
match workers with Some w -> Int.min w max_workers | None -> max_workers
in
let Config.{ workers; rnd; _ } = config in

Log.debug (fun f -> f "Initializing Riot runtime...");
Printexc.record_backtrace true;
Core.Pid.reset ();
Scheduler.Uid.reset ();

let sch0 = Scheduler.make ~rnd () in
let pool, domains = Scheduler.Pool.make ~main:sch0 ~domains:workers () in
let pool, _domains = Scheduler.Pool.make ~main:sch0 ~domains:workers () in

Scheduler.set_current_scheduler sch0;
Scheduler.Pool.set_pool pool;

let _pid = _spawn ~pool ~scheduler:sch0 main in
Scheduler.run pool sch0 ();

Log.debug (fun f -> f "Riot runtime shutting down...");
List.iter Stdlib.Domain.join domains;
Log.debug (fun f -> f "Riot runtime shutdown");
Stdlib.exit pool.status

Expand All @@ -58,16 +56,15 @@ let on_error (error : [ `Msg of string ]) =
Log.error (fun f -> f "Riot raised an error: %s\n" error_string);
1

let run_with_status ?(rnd = Random.State.make_self_init ()) ?workers ~on_error
main =
run ~rnd ?workers (fun _ ->
let status =
match main () with Ok code -> code | Error reason -> on_error reason
in
shutdown ~status ())
let run_with_status ?config ~on_error main =
run ?config @@ fun _ ->
let status =
match main () with Ok code -> code | Error reason -> on_error reason
in
shutdown ~status ()

let start ?rnd ?workers ~apps () =
run ?rnd ?workers @@ fun () ->
let start ?config ~apps () =
run ?config @@ fun () ->
let child_specs =
List.map
(fun (module App : Application.Intf) ->
Expand Down
18 changes: 10 additions & 8 deletions packages/riot/riot.mli
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,20 @@ val receive_any : ?after:int64 -> ?ref:unit Ref.t -> unit -> Message.t
val shutdown : ?status:int -> unit -> unit
(** Gracefully shuts down the runtime. Any non-yielding process will block this. *)

val run : ?rnd:Random.State.t -> ?workers:int -> (unit -> unit) -> unit
module Config : sig
type t = { rnd : Random.State.t; max_workers : int; workers : int }

val default : unit -> t
val make : ?workers:int -> unit -> t
end

val run : ?config:Config.t -> (unit -> unit) -> unit
(** Start the Riot runtime using function [main] to boot the system *)

val on_error : [ `Msg of string ] -> int

val run_with_status :
?rnd:Random.State.t ->
?workers:int ->
?config:Config.t ->
on_error:('error -> int) ->
(unit -> (int, 'error) result) ->
unit
Expand All @@ -320,11 +326,7 @@ val run_with_status :
*)

val start :
?rnd:Random.State.t ->
?workers:int ->
apps:(module Application.Intf) list ->
unit ->
unit
?config:Config.t -> apps:(module Application.Intf) list -> unit -> unit
(** Start the Riot runtime with a series of applications.
Each application will be started in the same order as specified, and
Expand Down
2 changes: 1 addition & 1 deletion test/add_monitor_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ let main () =
sleep 0.2;
raise Fail

let () = Riot.run ~workers:1 @@ main
let () = Riot.run ~config:(Config.make ~workers:1 ()) @@ main
2 changes: 1 addition & 1 deletion test/process_priority_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ let main () =
That behavior _is expected_.
*)
let () = Riot.run ~workers:0 @@ main
let () = Riot.run ~config:(Config.make ~workers:0 ()) @@ main
2 changes: 1 addition & 1 deletion test/selective_receive_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ let main () =
sleep 1.;
Stdlib.exit 1

let () = Riot.run ~workers:0 @@ main
let () = Riot.run ~config:(Config.make ~workers:0 ()) @@ main
2 changes: 1 addition & 1 deletion test/send_after_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ let main () =
sleep 0.1;
shutdown ~status:1 ()

let () = Riot.run ~workers:0 @@ main
let () = Riot.run ~config:(Config.make ~workers:0 ()) @@ main

0 comments on commit 0c728e3

Please sign in to comment.