Skip to content

Commit

Permalink
xapi_globs: add duration type for arguments
Browse files Browse the repository at this point in the history
This allows to define in the code the exact amount of time, including the
measure of time used as base. This is usually minutes.

On top of that it allows for two type of configuration parameters: one that
retains backwards compatibility, using seconds as floats. They can be used to
define relatively short amounts of time (less that 104 days). For longer
amounts of time, seconds can be encoded as integers.

When printing the configuration, both the literal value used to configure the
global and the amount using time units is shown.

Signed-off-by: Pau Ruiz Safont <[email protected]>
  • Loading branch information
psafont committed Jun 26, 2024
1 parent 44223d1 commit e4e548a
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions ocaml/xapi/xapi_globs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,12 @@ let ignore_vtpm_unimplemented = ref false

let evacuation_batch_size = ref 10

type xapi_globs_spec_ty = Float of float ref | Int of int ref
type 'a xapi_globs_spec =
| Float of float ref
| Int of int ref
| ShortDurationFromSeconds of Mtime.Span.t ref
(** From float, max of 104 days *)
| LongDurationFromSeconds of Mtime.Span.t ref (** From int *)

let extauth_ad_backend = ref "winbind"

Expand Down Expand Up @@ -1120,13 +1125,41 @@ let options_of_xapi_globs_spec =
List.map
(fun (name, ty) ->
( name
, (match ty with Float x -> Arg.Set_float x | Int x -> Arg.Set_int x)
, ( match ty with
| Float x ->
Arg.Set_float x
| Int x ->
Arg.Set_int x
| ShortDurationFromSeconds x ->
Arg.Float
(fun y ->
match Clock.Timer.s_to_span y with
| Some y ->
x := y
| None ->
D.warn
"Ignoring argument '%s', invalid float being used: %f. \
(it only allows durations of less than 104 days)"
name y
)
| LongDurationFromSeconds x ->
Arg.Int (fun y -> x := Mtime.Span.(y * s))
)
, (fun () ->
match ty with
| Float x ->
string_of_float !x
| Int x ->
string_of_int !x
| ShortDurationFromSeconds x ->
let literal =
Mtime.Span.to_uint64_ns !x |> fun ns ->
Int64.div ns 1_000_000_000L |> Int64.to_int |> string_of_int
in
Fmt.str "%s (%a)" literal Mtime.Span.pp !x
| LongDurationFromSeconds x ->
let literal = Clock.Timer.span_to_s !x |> string_of_float in
Fmt.str "%s (%a)" literal Mtime.Span.pp !x
)
, Printf.sprintf "Set the value of '%s'" name
)
Expand Down

0 comments on commit e4e548a

Please sign in to comment.