text
stringlengths 0
601k
|
---|
let timestamp_pre_constants = create_rewriter_constants " timestamp_pre "
|
let flush_constants = create_rewriter_constants " flush "
|
let attribute_names = [ duration_constants . attribute_name ; duration_lwt_constants . attribute_name ; timestamp_pre_constants . attribute_name ; flush_constants . attribute_name ; ]
|
let constants_of_rewriter = function | Duration _ -> duration_constants | Duration_lwt _ -> duration_lwt_constants | Timestamp_pre _ -> timestamp_pre_constants | Flush _ -> flush_constants
|
let labeled_rewriter_of_attr_name name = if name = duration_constants . attribute_name then Some duration else if name = duration_lwt_constants . attribute_name then Some duration_lwt else if name = timestamp_pre_constants . attribute_name then Some timestamp_pre else None
|
let locaction_of_rewriter = function | Duration ( _ , loc ) -> loc | Duration_lwt ( _ , loc ) -> loc | Timestamp_pre ( _ , loc ) -> loc | Flush loc -> loc
|
let error loc err = let open Format in let ( msg , hint ) = match err with | ` Too_many_Detection attribute_name -> ( sprintf " Attribute [ @% s ] detected several times on the same expression . " attribute_name , sprintf " A same expression can only be annotated once with [ @% s ] attribute . " attribute_name ) | ` Invalid_payload -> ( " Invalid or empty attribute payload . " , " This attribute payload must be of the form : < id > [ < expr ] >\ n \ \ where < id > should be a valid identifier \ n \ \ and < expr > an Ocaml expression evaluating to a string list . " ) | ` Non_empty_payload -> ( " Payload is not empty . " , " This attribute should not contain a payload . " ) in Location . raise_errorf ~ loc " time_measurement_ppx : % s \ nHint : % s " msg hint
|
let get_attribute_name attribute = attribute . attr_name . txt
|
let has_attribute_name attribute name = get_attribute_name attribute = name
|
let filter_attribute_with_name name = List . filter ( fun attr -> not @@ has_attribute_name attr name )
|
let filter_attribute_with_names names attributes = List . fold_left ( fun filtered name -> filter_attribute_with_name name filtered ) attributes names
|
let fresh_identifier = let counter = ref 0 in fun prefix -> let fresh = prefix ^ string_of_int ! counter in counter := ! counter + 1 ; fresh
|
let key_of_payload loc = function | PStr [ [ % stri [ % e ? { pexp_desc = Pexp_ident { txt = Lident label ; _ } ; _ } ] ] ] -> { label ; metadata = None } Tezos_time_measurement_runtime . Default . Time_measurement . duration ( " fun_app " , [ ] ) ( fun ( ) -> x ) ] } ) * | PStr [ [ % stri [ % e ? { pexp_desc = Pexp_apply ( { pexp_desc = Pexp_ident { txt = Lident label ; _ } ; _ } , [ ( Nolabel , expr ) ] ) ; _ ; } ] ] ; ] -> { label ; metadata = Some expr } Tezos_time_measurement_runtime . Default . Time_measurement . duration ( " fun_app " , [ " 123 " ] ) ( fun ( ) -> x ) ] } ) * | _ -> error loc ` Invalid_payload
|
let check_empty_payload loc = function | PStr [ ] -> ( ) | _ -> error loc ` Non_empty_payload
|
let rewriter_of_attribute ( { attr_payload ; attr_loc ; _ } as attribute ) = match get_attribute_name attribute |> labeled_rewriter_of_attr_name with | Some rwt -> let label = key_of_payload attr_loc attr_payload in Some ( rwt label attr_loc ) | None -> if has_attribute_name attribute flush_constants . attribute_name then ( check_empty_payload attr_loc attr_payload ; Some ( Flush attr_loc ) ) else None
|
let extract_rewriters = List . filter_map rewriter_of_attribute
|
let validate_rewriters rewriters = let duration_flag = ref false in let duration_lwt_flag = ref false in let timestamp_flag = ref false in let flush_flag = ref false in let check_visited flag name loc = if ! flag then error loc ( ` Too_many_Detection name ) else flag := true in let flag_of_rewriter = function | Duration _ -> duration_flag | Duration_lwt _ -> duration_lwt_flag | Timestamp_pre _ -> timestamp_flag | Flush _ -> flush_flag in List . iter ( fun rewriter -> let { attribute_name ; _ } = constants_of_rewriter rewriter in let flag = flag_of_rewriter rewriter in let loc = locaction_of_rewriter rewriter in check_visited flag attribute_name loc ) rewriters
|
let argument_of_key key loc = let label = Ast_helper . ( Exp . constant @@ Const . string key . label ) in let metadata = match key . metadata with Some expr -> expr | None -> [ % expr [ ] ] in [ % expr [ % e label ] , [ % e metadata ] ]
|
let ldot_of_non_empty_list ( x , xs ) = List . fold_left ( fun acc ident -> Ldot ( acc , ident ) ) ( Lident x ) xs
|
let time_measurement_longident name = ldot_of_non_empty_list ( " Tezos_time_measurement_runtime " , [ " Default " ; " Time_measurement " ; name ] )
|
let wrap_with_labelled_call expr loc key fn = let open Ast_helper in let fn_longident = time_measurement_longident fn in let fexpr = Exp . ident @@ { txt = fn_longident ; loc } in let last_arg = [ % expr fun ( ) -> [ % e expr ] ] in let args = [ ( Nolabel , argument_of_key key loc ) ; ( Nolabel , last_arg ) ] in Exp . apply fexpr args
|
let bind_with_flush_call expr loc = let open Ast_helper in let identifier = fresh_identifier " __flush__id__ " in let id_pattern = Pat . var { txt = identifier ; loc } in let id_expr = Exp . ident { txt = Lident identifier ; loc } in [ % expr [ % e expr ] >>= fun [ % p id_pattern ] -> Tezos_time_measurement_runtime . Default . Time_measurement . flush ( ) >|= fun ( ) -> [ % e id_expr ] ]
|
let rewrite rewriters initial_expr = let loc = initial_expr . pexp_loc in validate_rewriters rewriters ; List . fold_left ( fun expr rewriter -> let { fn_name ; _ } = constants_of_rewriter rewriter in match rewriter with | Duration ( key , _ ) | Duration_lwt ( key , _ ) | Timestamp_pre ( key , _ ) -> wrap_with_labelled_call expr loc key fn_name | Flush _ -> bind_with_flush_call expr loc ) initial_expr rewriters
|
let remove_attributes expr = { expr with pexp_attributes = filter_attribute_with_names attribute_names expr . pexp_attributes ; }
|
let mapper = object inherit Ast_traverse . map as super method ! expression e = let detected_rewriters = extract_rewriters e . pexp_attributes in remove_attributes e |> rewrite detected_rewriters |> super # expression end
|
let ( ) = Driver . register_transformation " time_measurement " ~ impl : mapper # structure
|
let ( ) = register_error_kind ` Permanent ~ id " : timestamp_add " ~ title " : Timestamp add " ~ description " : Overflow when adding timestamps . " ~ pp ( : fun ppf ( ) -> Format . fprintf ppf " Overflow when adding timestamps . " ) Data_encoding . empty ( function Timestamp_add -> Some ( ) | _ -> None ) ( fun ( ) -> Timestamp_add ) ; register_error_kind ` Permanent ~ id " : timestamp_sub " ~ title " : Timestamp sub " ~ description " : Subtracting timestamps resulted in negative period . " ~ pp ( : fun ppf ( ) -> Format . fprintf ppf " Subtracting timestamps resulted in negative period . " ) Data_encoding . empty ( function Timestamp_sub -> Some ( ) | _ -> None ) ( fun ( ) -> Timestamp_sub )
|
let of_seconds_string s = Option . map Time . of_seconds ( Int64 . of_string_opt s )
|
let to_seconds_string s = Int64 . to_string ( to_seconds s )
|
let ( +? ) x y = let span = Period_repr . to_seconds y in let t64 = Time . add x span in if x < Time . of_seconds 0L then ok t64 else if t64 < Time . of_seconds 0L then error Timestamp_add else ok t64
|
let ( -? ) x y = record_trace Timestamp_sub ( Period_repr . of_seconds ( Time . diff x y ) )
|
let ( - ) x y = Time . of_seconds Int64 . ( sub ( Time . to_seconds x ) ( Period_repr . to_seconds y ) )
|
let ( >>> ) = upon
|
module T1 = struct include Synchronous_time_source0 . T1 let sexp_of_t _ { id = _ ; advance_errors = _ ; am_advancing = _ ; events ; fired_events = _ ; handle_fired = _ ; is_wall_clock ; most_recently_fired = _ ; scheduler = _ } = if is_wall_clock then [ % message " < wall_clock " ] > else [ % message ( is_wall_clock : bool ) ( events : _ Timing_wheel . t ) ] ; ; end
|
module Read_write = struct type t = read_write T1 . t [ @@ deriving sexp_of ] let invariant = invariant let invariant_with_jobs = invariant_with_jobs end
|
type t = read T1 . t [ @@ deriving sexp_of ]
|
let read_only ( t : [ > read ] T1 . t ) = ( t :> t )
|
let alarm_precision t = Timing_wheel . alarm_precision t . events
|
let next_alarm_fires_at t = Timing_wheel . next_alarm_fires_at t . events
|
let timing_wheel_now t = Timing_wheel . now t . events
|
let id t = t . id
|
let now t = if t . is_wall_clock then Time_ns . now ( ) else timing_wheel_now t ; ;
|
let send_exn = Some Monitor . send_exn
|
let advance_directly t ~ to_ = Synchronous_time_source0 . advance_clock t ~ to_ ~ send_exn
|
let advance_directly_by t by = advance_directly t ~ to_ ( : Time_ns . after ( now t ) by )
|
let fire_past_alarms t = Synchronous_time_source0 . fire_past_alarms t ~ send_exn
|
let yield t = Bvar . wait ( Scheduler . yield t . scheduler )
|
let advance_by_alarms ? wait_for t ~ to_ = let run_queued_alarms ( ) = match wait_for with | None -> yield t | Some f -> f ( ) in let finish ( ) = advance_directly t ~ to_ ; fire_past_alarms t ; run_queued_alarms ( ) in let rec walk_alarms ( ) = match next_alarm_fires_at t with | None -> finish ( ) | Some next_alarm_fires_at -> if Time_ns . ( >= ) next_alarm_fires_at to_ then finish ( ) else ( advance_directly t ~ to_ : next_alarm_fires_at ; let queued_alarms_ran = run_queued_alarms ( ) in if Deferred . is_determined queued_alarms_ran then walk_alarms ( ) else ( let % bind ( ) = queued_alarms_ran in walk_alarms ( ) ) ) in let % bind ( ) = run_queued_alarms ( ) in walk_alarms ( ) ; ;
|
let advance_by_alarms_by ? wait_for t by = advance_by_alarms ? wait_for t ~ to_ ( : Time_ns . after ( now t ) by ) ; ;
|
let span_to_time t span = Time_ns . after ( now t ) span
|
let schedule_job t ~ at execution_context f a = let alarm = Timing_wheel . add t . events ~ at ( Job_or_event . of_job ( Scheduler . create_job t . scheduler execution_context f a ) ) in ( match t . scheduler . event_added_hook with | None -> ( ) | Some f -> f at ) ; alarm ; ;
|
let run_at_internal t time f a = let execution_context = Scheduler . current_execution_context t . scheduler in if Time_ns . ( > ) time ( Timing_wheel . now t . events ) then schedule_job t ~ at : time execution_context f a else ( Scheduler . enqueue t . scheduler execution_context f a ; Alarm . null ( ) ) ; ;
|
let run_at t time f a = ignore ( run_at_internal t time f a : _ Alarm . t )
|
let run_after t span f a = run_at t ( span_to_time t span ) f a
|
let at = let fill result = Ivar . fill result ( ) in fun t time -> if Time_ns . ( <= ) time ( Timing_wheel . now t . events ) then return ( ) else ( let result = Ivar . create ( ) in ignore ( run_at_internal t time fill result : _ Alarm . t ) ; Ivar . read result ) ; ;
|
let after t span = at t ( span_to_time t span )
|
let remove_alarm t alarm : unit = let job_or_event = Alarm . value t . events alarm in ( let open Job_or_event . Match in let ( K k ) = kind job_or_event in match k , project k job_or_event with | Job , job -> Scheduler . free_job t . scheduler job | Event , _ -> assert false ) ; Timing_wheel . remove t . events alarm ; ;
|
let remove_alarm_if_scheduled t alarm = if Timing_wheel . mem t . events alarm then remove_alarm t alarm ; ;
|
module Event = struct module Fired = struct type ( ' a , ' h ) t = | Aborted of ' a | Happened of ' h [ @@ deriving sexp_of ] end type ( ' a , ' h ) t = { mutable alarm : Job_or_event . t Alarm . t ; mutable fire : unit -> unit ; fired : ( ' a , ' h ) Fired . t Ivar . t ; mutable num_fires_to_skip : int ; mutable scheduled_at : Time_ns . t ; time_source : Synchronous_time_source . t } [ @@ deriving fields , sexp_of ] type t_unit = ( unit , unit ) t [ @@ deriving sexp_of ] let fired t = Ivar . read t . fired let invariant invariant_a invariant_h t = Invariant . invariant [ % here ] t [ % sexp_of : ( _ , _ ) t ] ( fun ( ) -> let events = t . time_source . events in let check f = Invariant . check_field t f in Fields . iter ~ alarm : ( check ( fun alarm -> if Ivar . is_full t . fired then assert ( not ( Timing_wheel . mem events alarm ) ) else if Timing_wheel . mem events alarm then assert ( Job_or_event . is_job ( Alarm . value events alarm ) ) ) ) ~ fire : ignore ~ fired : ( check ( fun ( fired : _ Fired . t Ivar . t ) -> match Deferred . peek ( Ivar . read fired ) with | None -> ( ) | Some ( Aborted a ) -> invariant_a a | Some ( Happened h ) -> invariant_h h ) ) ~ num_fires_to_skip : ( check ( fun num_fires_to_skip -> assert ( num_fires_to_skip >= 0 ) ) ) ~ scheduled_at : ( check ( fun scheduled_at -> if Timing_wheel . mem events t . alarm then [ % test_result : Time_ns . t ] scheduled_at ~ expect ( : Alarm . at events t . alarm ) ) ) ~ time_source : ignore ) ; ; module Status = struct type ( ' a , ' h ) t = | Aborted of ' a | Happened of ' h | Scheduled_at of Time_ns . t [ @@ deriving sexp_of ] end let status t : _ Status . t = match Deferred . peek ( Ivar . read t . fired ) with | None -> Scheduled_at t . scheduled_at | Some ( Aborted a ) -> Aborted a | Some ( Happened h ) -> Happened h ; ; module Abort_result = struct type ( ' a , ' h ) t = | Ok | Previously_aborted of ' a | Previously_happened of ' h [ @@ deriving sexp_of ] end let abort t a : _ Abort_result . t = if debug then Debug . log " Time_source . Event . abort " t [ % sexp_of : ( _ , _ ) t ] ; match Deferred . peek ( fired t ) with | Some ( Aborted a ) -> Previously_aborted a | Some ( Happened h ) -> Previously_happened h | None -> Ivar . fill t . fired ( Aborted a ) ; remove_alarm_if_scheduled t . time_source t . alarm ; Ok ; ; let abort_exn t a = match abort t a with | Ok -> ( ) | Previously_happened _ -> raise_s [ % message " Clock . Event . abort_exn failed to abort event that previously happened " ] | Previously_aborted _ -> raise_s [ % message " Clock . Event . abort_exn failed to abort event that previously aborted " ] ; ; let abort_if_possible t a = ignore ( abort t a : _ Abort_result . t ) let schedule t = t . alarm <- run_at_internal t . time_source t . scheduled_at t . fire ( ) module Reschedule_result = struct type ( ' a , ' h ) t = | Ok | Previously_aborted of ' a | Previously_happened of ' h [ @@ deriving sexp_of ] end let reschedule_at t at : _ Reschedule_result . t = if debug then Debug . log " Time_source . Event . reschedule_at " ( t , at ) [ % sexp_of : ( _ , _ ) t * Time_ns . t ] ; match Deferred . peek ( fired t ) with | Some ( Aborted a ) -> Previously_aborted a | Some ( Happened h ) -> Previously_happened h | None -> let events = t . time_source . events in let is_in_timing_wheel = Timing_wheel . mem events t . alarm in let am_trying_to_reschedule_in_the_future = Time_ns . ( > ) at ( Timing_wheel . now events ) in t . scheduled_at <- at ; ( match am_trying_to_reschedule_in_the_future , is_in_timing_wheel with | false , false -> ( ) | false , true -> t . time_source . handle_fired t . alarm ; Timing_wheel . remove events t . alarm | true , false -> t . num_fires_to_skip <- t . num_fires_to_skip + 1 ; schedule t | true , true -> Timing_wheel . reschedule events t . alarm ~ at ) ; Ok ; ; let reschedule_after t span = reschedule_at t ( span_to_time t . time_source span ) let run_at time_source scheduled_at f z = if debug then Debug . log " Time_source . Event . run_at " scheduled_at [ % sexp_of : Time_ns . t ] ; let t = { alarm = Alarm . null ( ) ; fire = ignore ; fired = Ivar . create ( ) ; num_fires_to_skip = 0 ; scheduled_at ; time_source = read_only time_source } in let fire ( ) = if Ivar . is_empty t . fired then if t . num_fires_to_skip > 0 then t . num_fires_to_skip <- t . num_fires_to_skip - 1 else ( let result = f z in if Ivar . is_empty t . fired then Ivar . fill t . fired ( Happened result ) ) in t . fire <- fire ; schedule t ; t ; ; let at time_source time = run_at time_source time ignore ( ) let run_after time_source span f a = run_at time_source ( span_to_time time_source span ) f a ; ; let after time_source span = at time_source ( span_to_time time_source span ) end
|
let at_times ( ? stop = Deferred . never ( ) ) t next_time = let tail = Tail . create ( ) in let rec loop ( ) = choose [ choice stop ( fun ( ) -> ` Stop ) ; choice ( at t ( next_time ( ) ) ) ( fun ( ) -> ` Tick ) ] >>> function | ` Stop -> Tail . close_exn tail | ` Tick -> Tail . extend tail ( ) ; loop ( ) in loop ( ) ; Tail . collect tail ; ;
|
let at_varying_intervals ? stop t compute_span = at_times t ? stop ( fun ( ) -> Time_ns . after ( now t ) ( compute_span ( ) ) ) ; ;
|
let at_intervals ? start ? stop t interval = let start = match start with | Some x -> x | None -> now t in at_times t ? stop ( fun ( ) -> Time_ns . next_multiple ~ base : start ~ after ( : now t ) ~ interval ( ) ) ; ;
|
module Continue = struct type t = | Immediately | After of Time_ns . Span . t | Next_multiple of Time_ns . t * Time_ns . Span . t let immediately = Immediately let at t time_source = match t with | Immediately -> Timing_wheel . now time_source . events | After span -> span_to_time time_source span | Next_multiple ( base , interval ) -> Time_ns . next_multiple ~ base ~ after ( : now time_source ) ~ interval ( ) ; ; end
|
let run_repeatedly ( ? start = return ( ) ) ? stop ( ? continue_on_error = true ) ( ? finished = Ivar . create ( ) ) t ~ f ~ continue = start >>> fun ( ) -> let alarm = ref ( Alarm . null ( ) ) in let stop = match stop with | None -> Deferred . never ( ) | Some stop -> upon stop ( fun ( ) -> if Timing_wheel . mem t . events ! alarm then ( remove_alarm t ! alarm ; Ivar . fill_if_empty finished ( ) ) ) ; stop in let rec run_f ( ) = if Deferred . is_determined stop then Ivar . fill_if_empty finished ( ) else if continue_on_error then Monitor . try_with f ~ run ` : Now ~ rest ` : Raise >>> continue_try_with else ( let d = f ( ) in if Deferred . is_determined d then continue_f ( ) else d >>> continue_f ) and continue_f ( ) = if Deferred . is_determined stop then Ivar . fill_if_empty finished ( ) else alarm := run_at_internal t ( Continue . at continue t ) run_f ( ) and continue_try_with or_error = ( match or_error with | Ok ( ) -> ( ) | Error error -> Monitor . send_exn ( Monitor . current ( ) ) error ) ; continue_f ( ) in run_f ( ) ; ;
|
let every ' ? start ? stop ? continue_on_error ? finished t span f = if Time_ns . Span . ( <= ) span Time_ns . Span . zero then raise_s [ % message " Time_source . every got nonpositive span " ( span : Time_ns . Span . t ) ] ; run_repeatedly t ? start ? stop ? continue_on_error ? finished ~ f ~ continue ( : After span ) ; ;
|
let every ? start ? stop ? continue_on_error t span f = every ' t ? start ? stop ? continue_on_error ? finished : None span ( fun ( ) -> f ( ) ; return ( ) ) ; ;
|
let run_at_intervals ' ? start ? stop ? continue_on_error t interval f = let now = now t in let base , start = match start with | None -> now , None | Some start -> ( start , Some ( at t ( Time_ns . next_multiple ( ) ~ base : start ~ after : now ~ can_equal_after : true ~ interval ) ) ) in run_repeatedly t ? start ? stop ? continue_on_error ~ f ~ continue ( : Next_multiple ( base , interval ) ) ; ;
|
let run_at_intervals ? start ? stop ? continue_on_error t interval f = run_at_intervals ' ? start ? stop ? continue_on_error t interval ( fun ( ) -> f ( ) ; return ( ) ) ; ;
|
let with_timeout t span d = let timeout = Event . after t span in choose [ choice d ( fun v -> ( match Event . abort timeout ( ) with | Ok | Previously_happened ( ) -> ( ) | Previously_aborted ( ) -> raise_s [ % message " Time_source . with_timeout bug : should only abort once " ] ) ; ` Result v ) ; choice ( Event . fired timeout ) ( function | Happened ( ) -> ` Timeout | Aborted ( ) -> raise_s [ % message " Time_source . with_timeout bug : both completed and timed out " ] ) ] ; ;
|
let of_synchronous t = t
|
let to_synchronous t = t
|
module type Time_source = sig module T1 : sig type ' - rw t [ @@ deriving sexp_of ] end module Read_write : sig type t = read_write T1 . t [ @@ deriving sexp_of ] include Invariant . S with type t := t val invariant_with_jobs : job : Job . t Invariant . t -> t Invariant . t end module Id : Unique_id . Id type t = read T1 . t [ @@ deriving sexp_of ] val id : _ T1 . t -> Id . t include Invariant . S with type t := t val invariant_with_jobs : job : Job . t Invariant . t -> t Invariant . t val read_only : [ > read ] T1 . t -> t val create : ? timing_wheel_config : Timing_wheel . Config . t -> now : Time_ns . t -> unit -> read_write T1 . t val wall_clock : unit -> t val alarm_precision : [ > read ] T1 . t -> Time_ns . Span . t val next_alarm_fires_at : [ > read ] T1 . t -> Time_ns . t option val now : [ > read ] T1 . t -> Time_ns . t val timing_wheel_now : [ > read ] T1 . t -> Time_ns . t val advance_directly : [ > write ] T1 . t -> to_ : Time_ns . t -> unit val advance : [ > write ] T1 . t -> to_ : Time_ns . t -> unit [ @@ deprecated " [ since 2019 - 06 ] Use [ advance_directly ] ( to preserve behavior ) or \ [ advance_by_alarms ] " ] val advance_directly_by : [ > write ] T1 . t -> Time_ns . Span . t -> unit val advance_by : [ > write ] T1 . t -> Time_ns . Span . t -> unit [ @@ deprecated " [ since 2019 - 06 ] Use [ advance_directly_by ] ( to preserve behavior ) or \ [ advance_by_alarms_by ] " ] val fire_past_alarms : [ > write ] T1 . t -> unit val advance_by_alarms : ? wait_for ( : unit -> unit Deferred . t ) -> [ > write ] T1 . t -> to_ : Time_ns . t -> unit Deferred . t val advance_by_alarms_by : ? wait_for ( : unit -> unit Deferred . t ) -> [ > write ] T1 . t -> Time_ns . Span . t -> unit Deferred . t module Continue : sig type t val immediately : t end val run_repeatedly : ? start : unit Deferred . t -> ? stop : unit Deferred . t -> ? continue_on_error : bool -> ? finished : unit Ivar . t -> [ > read ] T1 . t -> f ( : unit -> unit Deferred . t ) -> continue : Continue . t -> unit val run_at : [ > read ] T1 . t -> Time_ns . t -> ( ' a -> unit ) -> ' a -> unit val run_after : [ > read ] T1 . t -> Time_ns . Span . t -> ( ' a -> unit ) -> ' a -> unit val at : [ > read ] T1 . t -> Time_ns . t -> unit Deferred . t val after : [ > read ] T1 . t -> Time_ns . Span . t -> unit Deferred . t val with_timeout : [ > read ] T1 . t -> Time_ns . Span . t -> ' a Deferred . t -> [ ` Timeout | ` Result of ' a ] Deferred . t module Event : sig type ( ' a , ' h ) t [ @@ deriving sexp_of ] type t_unit = ( unit , unit ) t [ @@ deriving sexp_of ] include Invariant . S2 with type ( ' a , ' b ) t := ( ' a , ' b ) t val scheduled_at : ( _ , _ ) t -> Time_ns . t module Status : sig type ( ' a , ' h ) t = | Aborted of ' a | Happened of ' h | Scheduled_at of Time_ns . t [ @@ deriving sexp_of ] end val status : ( ' a , ' h ) t -> ( ' a , ' h ) Status . t val run_at : [ > read ] T1 . t -> Time_ns . t -> ( ' z -> ' h ) -> ' z -> ( _ , ' h ) t val run_after : [ > read ] T1 . t -> Time_ns . Span . t -> ( ' z -> ' h ) -> ' z -> ( _ , ' h ) t module Abort_result : sig type ( ' a , ' h ) t = | Ok | Previously_aborted of ' a | Previously_happened of ' h [ @@ deriving sexp_of ] end val abort : ( ' a , ' h ) t -> ' a -> ( ' a , ' h ) Abort_result . t val abort_exn : ( ' a , ' h ) t -> ' a -> unit val abort_if_possible : ( ' a , _ ) t -> ' a -> unit module Fired : sig type ( ' a , ' h ) t = | Aborted of ' a | Happened of ' h [ @@ deriving sexp_of ] end val fired : ( ' a , ' h ) t -> ( ' a , ' h ) Fired . t Deferred . t module Reschedule_result : sig type ( ' a , ' h ) t = | Ok | Previously_aborted of ' a | Previously_happened of ' h [ @@ deriving sexp_of ] end val reschedule_at : ( ' a , ' h ) t -> Time_ns . t -> ( ' a , ' h ) Reschedule_result . t val reschedule_after : ( ' a , ' h ) t -> Time_ns . Span . t -> ( ' a , ' h ) Reschedule_result . t val at : [ > read ] T1 . t -> Time_ns . t -> ( _ , unit ) t val after : [ > read ] T1 . t -> Time_ns . Span . t -> ( _ , unit ) t end val at_varying_intervals : ? stop : unit Deferred . t -> [ > read ] T1 . t -> ( unit -> Time_ns . Span . t ) -> unit Async_stream . t val at_intervals : ? start : Time_ns . t -> ? stop : unit Deferred . t -> [ > read ] T1 . t -> Time_ns . Span . t -> unit Async_stream . t val every ' : ? start : unit Deferred . t -> ? stop : unit Deferred . t -> ? continue_on_error : bool -> ? finished : unit Ivar . t -> [ > read ] T1 . t -> Time_ns . Span . t -> ( unit -> unit Deferred . t ) -> unit val every : ? start : unit Deferred . t -> ? stop : unit Deferred . t -> ? continue_on_error : bool -> [ > read ] T1 . t -> Time_ns . Span . t -> ( unit -> unit ) -> unit val run_at_intervals ' : ? start : Time_ns . t -> ? stop : unit Deferred . t -> ? continue_on_error : bool -> [ > read ] T1 . t -> Time_ns . Span . t -> ( unit -> unit Deferred . t ) -> unit val run_at_intervals : ? start : Time_ns . t -> ? stop : unit Deferred . t -> ? continue_on_error : bool -> [ > read ] T1 . t -> Time_ns . Span . t -> ( unit -> unit ) -> unit val of_synchronous : ' a Synchronous_time_source . T1 . t -> ' a T1 . t val to_synchronous : ' a T1 . t -> ' a Synchronous_time_source . T1 . t end
|
let run_cycles_until_no_jobs_remain = Kernel_scheduler . run_cycles_until_no_jobs_remain ( module struct let scheduler = Kernel_scheduler . t ( ) let time_source = scheduler . time_source |> Time_source . of_synchronous let % bench " ~ continue - on - error : false " = let iv = Ivar . create ( ) in let n = ref 0 in Time_source . run_repeatedly time_source ~ stop ( : Ivar . read iv ) ~ continue_on_error : false ~ f ( : fun ( ) -> if ! n >= 1_000 then Ivar . fill iv ( ) else incr n ; return ( ) ) ~ continue : Time_source . Continue . immediately ; run_cycles_until_no_jobs_remain ( ) ; ; let % bench " ~ continue_on_error : true " = let iv = Ivar . create ( ) in let n = ref 0 in Time_source . run_repeatedly time_source ~ stop ( : Ivar . read iv ) ~ continue_on_error : true ~ f ( : fun ( ) -> if ! n >= 1_000 then Ivar . fill iv ( ) else incr n ; return ( ) ) ~ continue : Time_source . Continue . immediately ; run_cycles_until_no_jobs_remain ( ) ; ; end ) ; ;
|
let ( ) = assert ( 0 . <= max_percent_change_from_real_slope ) ; assert ( max_percent_change_from_real_slope <= 1 . ) ; ;
|
let ewma ~ alpha ~ old ~ add = ( ( 1 . . - alpha ) . * old ) . + ( alpha . * add )
|
type t = Int63 . t [ @@ deriving bin_io , compare , sexp ]
|
type tsc = t [ @@ deriving bin_io , compare , sexp ]
|
let diff t1 t2 = Int63 . ( - ) t1 t2
|
let add t s = Int63 . ( + ) t s
|
let of_int63 t = t
|
let to_int63 t = t
|
let zero = Int63 . zero [ %% ifdef JSC_ARCH_SIXTYFOUR ]
|
module Calibrator = struct type float_fields = { mutable time : float ; mutable sec_per_cycle : float ; mutable monotonic_time : float ; mutable monotonic_sec_per_cycle : float ; mutable ewma_time_tsc : float ; mutable ewma_tsc_square : float ; mutable ewma_time : float ; mutable ewma_tsc : float ; mutable nanos_per_cycle : float ; mutable monotonic_nanos_per_cycle : float } [ @@ deriving bin_io , sexp ] type t = { mutable tsc : tsc ; mutable monotonic_until_tsc : tsc ; mutable time_nanos : Int63 . t ; mutable monotonic_time_nanos : Int63 . t ; floats : float_fields } [ @@ deriving bin_io , fields , sexp ] let tsc_to_seconds_since_epoch = let [ @ inline ] convert t tsc base mul = base . + ( mul . * Int63 . to_float ( diff tsc t . tsc ) ) in fun [ @ inline ] t tsc -> 0 . . + if tsc < t . monotonic_until_tsc then 0 . . + convert t tsc t . floats . monotonic_time t . floats . monotonic_sec_per_cycle else 0 . . + convert t tsc t . floats . time t . floats . sec_per_cycle ; ; let tsc_to_nanos_since_epoch = let convert t tsc base mul = Int63 . ( + ) base ( Float . int63_round_nearest_exn ( mul . * Int63 . to_float ( diff tsc t . tsc ) ) ) in fun t tsc -> if tsc < t . monotonic_until_tsc then convert t tsc t . monotonic_time_nanos t . floats . monotonic_nanos_per_cycle else convert t tsc t . time_nanos t . floats . nanos_per_cycle ; ; let alpha_for_interval time_diff = 0 . . + Float . max 0 . ( 1 . . - exp ( - 0 . 5 . * time_diff ) ) let catchup_cycles = 1E9 let initial_alpha = 1 . let iround_up_and_add int ~ if_iround_up_fails float = if Float . ( > ) float 0 . 0 then ( let float ' = Caml . ceil float in if Float . ( <= ) float ' Float . iround_ubound then Int63 . ( + ) int ( Int63 . of_float_unchecked float ' ) else if_iround_up_fails ) else if Float . ( >= ) float Float . iround_lbound then Int63 . ( + ) int ( Int63 . of_float_unchecked float ) else if_iround_up_fails ; ; let [ @ inline ] calibrate_using t ~ tsc ~ time ~ am_initializing = let estimated_time = 0 . . + tsc_to_seconds_since_epoch t tsc in let time_diff_est = time . - estimated_time in let time_diff = time . - t . floats . time in let tsc_diff = Int63 . to_float ( diff tsc t . tsc ) in let alpha = if am_initializing then initial_alpha else alpha_for_interval time_diff in t . floats . time <- time ; t . tsc <- tsc ; t . floats . ewma_time_tsc <- ewma ~ alpha ~ old : t . floats . ewma_time_tsc ~ add ( : tsc_diff . * time_diff ) ; t . floats . ewma_tsc_square <- ewma ~ alpha ~ old : t . floats . ewma_tsc_square ~ add ( : tsc_diff . * tsc_diff ) ; t . floats . ewma_tsc <- ewma ~ alpha ~ old : t . floats . ewma_tsc ~ add : tsc_diff ; t . floats . ewma_time <- ewma ~ alpha ~ old : t . floats . ewma_time ~ add : time_diff ; t . floats . sec_per_cycle <- t . floats . ewma_time_tsc . / t . floats . ewma_tsc_square ; t . floats . monotonic_time <- estimated_time ; if not am_initializing then ( let catchup_sec_per_cycle = t . floats . sec_per_cycle . + ( time_diff_est . / catchup_cycles ) in t . floats . monotonic_sec_per_cycle <- ( if Float . is_positive time_diff_est then 0 . . + Float . min catchup_sec_per_cycle ( t . floats . sec_per_cycle . * ( 1 . . + max_percent_change_from_real_slope ) ) else 0 . . + Float . max catchup_sec_per_cycle ( t . floats . sec_per_cycle . * ( 1 . . - max_percent_change_from_real_slope ) ) ) ; t . monotonic_until_tsc <- time_diff_est . / ( t . floats . monotonic_sec_per_cycle . - t . floats . sec_per_cycle ) |> iround_up_and_add tsc ~ if_iround_up_fails : Int63 . zero ) ; t . time_nanos <- Float . int63_round_nearest_exn ( t . floats . time . * 1E9 ) ; t . floats . nanos_per_cycle <- t . floats . sec_per_cycle . * 1E9 ; t . monotonic_time_nanos <- Float . int63_round_nearest_exn ( t . floats . monotonic_time . * 1E9 ) ; t . floats . monotonic_nanos_per_cycle <- t . floats . monotonic_sec_per_cycle . * 1E9 ; ; let now_float ( ) = 1E - 9 . * Int . to_float ( Time_ns . to_int_ns_since_epoch ( Time_ns . now ( ) ) ) ; ; let initialize t samples = List . iter samples ~ f ( : fun ( tsc , time ) -> calibrate_using t ~ tsc ~ time ~ am_initializing : true ) ; ; let collect_samples ~ num_samples ~ interval = assert ( Int . ( >= ) num_samples 1 ) ; let rec loop n sleep = let sample = now ( ) , now_float ( ) in if Int . ( = ) n 1 then [ sample ] else ( ignore ( Unix . nanosleep sleep ) ; sample :: loop ( n - 1 ) ( sleep . + interval ) ) in loop num_samples interval ; ; let create_using ~ tsc ~ time ~ samples = let t = { monotonic_until_tsc = Int63 . zero ; tsc ; time_nanos = Int63 . zero ; monotonic_time_nanos = Int63 . zero ; floats = { monotonic_time = time ; sec_per_cycle = 0 . ; monotonic_sec_per_cycle = 0 . ; time ; ewma_time_tsc = 0 . ; ewma_tsc_square = 0 . ; ewma_time = 0 . ; ewma_tsc = 0 . ; nanos_per_cycle = 0 . ; monotonic_nanos_per_cycle = 0 . } } in initialize t samples ; t ; ; let create ( ) = let time = now_float ( ) in let tsc = now ( ) in let samples = collect_samples ~ num_samples : 3 ~ interval : 0 . 0005 in create_using ~ tsc ~ time ~ samples ; ; let t = lazy ( create ( ) ) let cpu_mhz = Ok ( fun t -> 1 . . / ( t . floats . sec_per_cycle . * 1E6 ) ) let [ @ cold ] calibrate t = calibrate_using t ~ tsc ( : now ( ) ) ~ time ( : now_float ( ) ) ~ am_initializing : false ; ; module Private = struct let create_using = create_using let calibrate_using = calibrate_using let initialize = initialize let nanos_per_cycle t = t . floats . nanos_per_cycle end end [ %% else ]
|
module Calibrator = struct type t = unit [ @@ deriving bin_io , sexp ] let tsc_to_seconds_since_epoch _t tsc = Int63 . to_float tsc . * 1e - 9 let tsc_to_nanos_since_epoch _t tsc = tsc let create_using ~ tsc : _ ~ time : _ ~ samples : _ = ( ) let create ( ) = ( ) let initialize _t _samples = ( ) let calibrate_using _t ~ tsc : _ ~ time : _ ~ am_initializing : _ = ( ) let calibrate _ = ( ) let t = lazy ( create ( ) ) let cpu_mhz = Or_error . unimplemented " Time_stamp_counter . Calibrator . cpu_mhz is not defined for 32 - bit platforms " ; ; module Private = struct let create_using = create_using let calibrate_using = calibrate_using let initialize = initialize let nanos_per_cycle _ = 1 . end end [ %% endif ]
|
module Span = struct include Int63 module Private = struct let of_int63 t = t let to_int63 t = t end [ %% ifdef JSC_ARCH_SIXTYFOUR ] let to_ns t ( ~ calibrator : Calibrator . t ) = Float . int63_round_nearest_exn ( Int63 . to_float t . * calibrator . floats . nanos_per_cycle ) ; ; let of_ns ns ( ~ calibrator : Calibrator . t ) = try Float . int63_round_nearest_exn ( Int63 . to_float ns . / calibrator . floats . nanos_per_cycle ) with | exn -> raise_s [ % message " " ~ _ ( : exn : Exn . t ) ( calibrator : Calibrator . t ) ] ; ; [ %% else ] let to_ns t ~ calibrator : _ = t let of_ns ns ~ calibrator : _ = ns [ %% endif ] let to_time_span t ~ calibrator = Time . Span . of_ns ( Int63 . to_float ( to_ns t ~ calibrator ) ) end
|
let to_time t ~ calibrator = Calibrator . tsc_to_seconds_since_epoch calibrator t |> Time . Span . of_sec |> Time . of_span_since_epoch ; ;
|
let to_nanos_since_epoch t ~ calibrator = Calibrator . tsc_to_nanos_since_epoch calibrator t
|
let to_time_ns t ~ calibrator = Time_ns . of_int63_ns_since_epoch ( to_nanos_since_epoch ~ calibrator t ) ; ;
|
module Private = struct let ewma = ewma let of_int63 = of_int63 let max_percent_change_from_real_slope = max_percent_change_from_real_slope let to_nanos_since_epoch = to_nanos_since_epoch end
|
module TSC = Time_stamp_counter let calibrator = force TSC . calibrator in fun ( ) -> TSC . Calibrator . calibrate calibrator ; ; let calibrator = force TSC . calibrator in let c = TSC . now ( ) in fun ( ) -> ignore ( TSC . to_time c ~ calibrator ) ; ; let calibrator = force TSC . calibrator in fun ( ) -> TSC . to_time ( TSC . now ( ) ) ~ calibrator ; ; let calibrator = force TSC . calibrator in let c = TSC . now ( ) in fun ( ) -> ignore ( TSC . to_time_ns c ~ calibrator ) ; ; let calibrator = force TSC . calibrator in fun ( ) -> TSC . to_time_ns ( TSC . now ( ) ) ~ calibrator ; ; let calibrator = force TSC . calibrator in let c = Int63 . of_int_exn 123 in fun ( ) -> TSC . Span . of_ns c ~ calibrator ; ; let calibrator = force TSC . calibrator in let c = Int63 . of_int_exn 123 |> TSC . Span . of_ns ~ calibrator in fun ( ) -> TSC . Span . to_ns c ~ calibrator ; ;
|
let teq t1 t2 = if Sys . word_size = 64 then Float . iround ~ dir ` : Zero ( Time . Span . to_sec ( Time . to_span_since_epoch t1 ) . * 1000 . ) = Float . iround ~ dir ` : Zero ( Time . Span . to_sec ( Time . to_span_since_epoch t2 ) . * 1000 . ) else true
|
let speq s1 s2 = round ( Time . Span . to_ms s1 ) = round ( Time . Span . to_ms s2 )
|
let convtest ? tol f1 f2 = let x = float ( Random . int 1_000_000 ) . / 1000 . in let tol = match tol with | None -> Float . robust_comparison_tolerance | Some pct -> x . * pct in Float . abs ( f1 ( f2 x ) . - x ) <= tol
|
let mintime_str = " 0000 - 01 - 01 00 : 00 : 00 . 000000 "
|
let maxtime_str = " 3000 - 01 - 01 00 : 00 : 00 . 000000 "
|
let time_gen ( ) = Time . of_span_since_epoch ( Time . Span . of_sec ( Quickcheck_deprecated . fg ( ) ) )
|
let reasonable_time time = let time = Time . to_span_since_epoch time |> Time . Span . to_sec in time > 0 . && time < 100 . . * 52 . . * 24 . . * 60 . . * 60 .
|
let similar_time time time ' = let time = Time . to_span_since_epoch time |> Time . Span . to_sec in let time ' = Time . to_span_since_epoch time ' |> Time . Span . to_sec in Float . abs ( time . - time ' ) < 0 . 01
|
let test_list = ref [ ]
|
let add name test = test_list := ( name >:: test ) :: ! test_list
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.