Skip to content

Commit

Permalink
CP-52821: xapi_periodic_scheduler: use Mtime.span instead of Mtime.t
Browse files Browse the repository at this point in the history
Avoids dealing with overflow

Signed-off-by: Edwin Török <[email protected]>
  • Loading branch information
edwintorok committed Dec 5, 2024
1 parent aa5d0f4 commit dcb6d53
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
6 changes: 3 additions & 3 deletions ocaml/xapi/ipq.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*)
(* Imperative priority queue *)

type 'a event = {ev: 'a; time: Mtime.t}
type 'a event = {ev: 'a; time: Mtime.span}

type 'a t = {mutable size: int; mutable data: 'a event array}

Expand Down Expand Up @@ -49,7 +49,7 @@ let add h x =
(* moving [x] up in the heap *)
let rec moveup i =
let fi = (i - 1) / 2 in
if i > 0 && Mtime.is_later d.(fi).time ~than:x.time then (
if i > 0 && Mtime.Span.is_longer d.(fi).time ~than:x.time then (
d.(i) <- d.(fi) ;
moveup fi
) else
Expand All @@ -76,7 +76,7 @@ let remove h s =
let j' = j + 1 in
if j' < n && d.(j').time < d.(j).time then j' else j
in
if Mtime.is_earlier d.(j).time ~than:x.time then (
if Mtime.Span.is_shorter d.(j).time ~than:x.time then (
d.(i) <- d.(j) ;
movedown j
) else
Expand Down
18 changes: 5 additions & 13 deletions ocaml/xapi/xapi_periodic_scheduler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,13 @@ let (queue : t Ipq.t) = Ipq.create 50

let lock = Mutex.create ()

let add_span clock span =
match Mtime.add_span clock span with
| Some t ->
t
| None ->
raise
Api_errors.(Server_error (internal_error, ["clock overflow"; __LOC__]))

let add_to_queue_span ?(signal = true) name ty start_span newfunc =
with_lock lock (fun () ->
let ( ++ ) = add_span in
let ( ++ ) = Mtime.Span.add in
Ipq.add queue
{
Ipq.ev= {func= newfunc; ty; name}
; Ipq.time= Mtime_clock.now () ++ start_span
; Ipq.time= Mtime_clock.elapsed () ++ start_span
}
) ;
if signal then Delay.signal delay
Expand Down Expand Up @@ -90,8 +82,8 @@ let loop () =
(* Doesn't happen often - the queue isn't usually empty *)
else
let next = with_lock lock (fun () -> Ipq.maximum queue) in
let now = Mtime_clock.now () in
if Mtime.is_earlier next.Ipq.time ~than:now then (
let now = Mtime_clock.elapsed () in
if Mtime.Span.is_shorter next.Ipq.time ~than:now then (
let todo =
(with_lock lock (fun () -> Ipq.pop_maximum queue)).Ipq.ev
in
Expand All @@ -103,7 +95,7 @@ let loop () =
add_to_queue ~signal:false todo.name todo.ty timer todo.func
) else (* Sleep until next event. *)
let sleep =
Mtime.(span next.Ipq.time now)
Mtime.(Span.abs_diff next.Ipq.time now)
|> Mtime.Span.add Mtime.Span.(1 * ms)
|> Scheduler.span_to_s
in
Expand Down

0 comments on commit dcb6d53

Please sign in to comment.