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 ) | ` In...
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_ap...
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 t...
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 | Duratio...
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 . ...
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 . D...
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...
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 ( ) -> ...
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 :...
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_fi...
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 sched...
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 ( ) ...
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 ( bas...
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 . e...
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 ? st...
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 . fi...
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 ...
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 Tim...
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_cy...
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 ...
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 : Calibrat...
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...
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