text
stringlengths
12
786k
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 | Non...
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 ; ...
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 ) ; Timi...
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 . ...
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 . c...
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 ...
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...
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_er...
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 )...
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 ...
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 ...
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 . cre...
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 : fl...
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 initiali...
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 ) ; ; l...
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 ...
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
let ( ) = add " t " ( fun ( ) -> let s1 = " 2005 - 05 - 25 12 : 46 - 4 : 00 " in let s2 = " 2005 - 05 - 25 12 : 46 : 15 - 4 : 00 " in let s3 = " 2005 - 05 - 25 12 : 46 : 15 . 232 - 4 : 00 " in let s4 = " 2005 - 05 - 25 12 : 46 : 15 ....
let ( ) = add " Ofday_string_conversion " ( fun ( ) -> let rand_state = Random . State . make [ | 1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7 ] | in for _ = 0 to 100_000 do let secs = Random . State . int rand_state 86_400_000 in let ofday = Ofday . of_span_since_start_of_day_ex...
let ( ) = add " date " ( fun ( ) -> let zone = ( force Time . Zone . local ) in let start = Time . of_date_ofday ~ zone ( Date . create_exn ~ y : 1999 ~ m : Month . Jan ~ d : 1 ) Ofday . start_of_day in let day = Span . of_day 1 . in let number_of_days = match Word_...
module Old_date_impl = struct let of_tm tm = Date . create_exn ~ y ( : tm . Unix . tm_year + 1900 ) ~ m ( : Month . of_int_exn ( tm . Unix . tm_mon + 1 ) ) ~ d : tm . Unix . tm_mday ; ; let to_tm t = { Unix . tm_sec = 0 ; tm_min = 0 ; tm_hour = 12 ; tm_mday ...
let ( ) = add " day_of_week " ( fun ( ) -> " exhaustive " @? Old_date_impl . exhaustive_day_of_week_test ( ) )
let ( ) = add " add_days " ( fun ( ) -> " one " @? ( Date . add_days ( Date . of_string " 2008 - 11 - 02 " ) 1 = Date . of_string " 2008 - 11 - 03 " ) ; " two " @? ( Date . add_days ( Date . of_string " 2008 - 11 - 02 " ) 2 = Date . of...
let ( ) = add " add_months " ( fun ( ) -> " zero " @? ( Date . add_months ( Date . of_string " 2009 - 02 - 28 " ) 0 = Date . of_string " 2009 - 02 - 28 " ) ; " one " @? ( Date . add_months ( Date . of_string " 2009 - 01 - 30 " ) 1 = Date...
let ( ) = add " add_weekdays_rounding_forward " ( fun ( ) -> let test lbl d1 n d2 = lbl @? ( Date . add_weekdays_rounding_forward ( Date . of_string d1 ) n = Date . of_string d2 ) in test " one " " 2009 - 01 - 01 " 1 " 2009 - 01 - 02 " ; test " one_weekend " ...
let ( ) = add " add_weekdays_rounding_backward " ( fun ( ) -> let test lbl d1 n d2 = lbl @? ( Date . add_weekdays_rounding_backward ( Date . of_string d1 ) n = Date . of_string d2 ) in test " one " " 2009 - 01 - 01 " 1 " 2009 - 01 - 02 " ; test " one_weekend "...
let ( ) = add " add_business_days_rounding_forward " ( fun ( ) -> let test lbl d1 n d2 = let is_holiday d = List . mem ~ equal : Date . equal ( List . map [ " 2009 - 01 - 01 " ; " 2009 - 03 - 01 " ; " 2009 - 03 - 02 " ; ] ~ f : Date . of_string ) d ...
let ( ) = add " add_business_days_rounding_backward " ( fun ( ) -> let test lbl d1 n d2 = let is_holiday d = List . mem ~ equal : Date . equal ( List . map [ " 2009 - 01 - 01 " ; " 2009 - 03 - 01 " ; " 2009 - 03 - 02 " ; ] ~ f : Date . of_string ) d...
let ( ) = add " span_scale " ( fun ( ) -> " ms " @? speq ( Time . Span . scale ( sec 10 . ) 0 . 001 ) ( Time . Span . of_ms 10 . ) ; " min " @? speq ( Time . Span . scale ( sec 10 . ) 60 . ) ( Time . Span . of_min 10 . ) ; " hr "...
let roundtrip s = let t = Span . of_string s in ( " string roundtrip " ^ s ) @? Span . ( ) = t ( Time . Span . of_string ( Time . Span . to_string t ) ) ; ;
let ( ) = let assert_raises f = try f ( ) ; raise Finished with | Finished -> assert false | _ -> ( ) in let extensions = [ " ms " ; " s " ; " m " ; " h " ] in add " roundtrip span <-> string " ( fun ( ) -> List . iter extensions ~ f ( : fun ext -> let ...
let test = " time " >::: ! test_list
module Alco = struct let union_empty ( ) = Alcotest . ( check ( list ( pair span_testable span_testable ) ) ) " same list " [ ] ( Time . union [ ] |> Resolver . resolve |> CCResult . get_exn |> CCList . of_seq ) let inter_empty ( ) = Alcotest . ( check ( list ( ...
module Qc = struct let to_of_sexp = QCheck . Test . make ~ count : 100_000 ~ name " : to_of_sexp " time ( fun t -> let t ' = t |> To_sexp . to_sexp |> Of_sexp . of_sexp in Time . equal t t ' ) let union_order_does_not_matter = QCheck . Test . make ~ count : 10 ~ name " : uni...
let remove_1st key list = let rec remove = function [ ] -> [ ] | a :: l -> if a == key then l else a ( :: remove l ) in remove list
let debug_time_travel = ref false
let insert_checkpoint ( { c_time = time } as checkpoint ) = let rec traverse = function [ ] -> [ checkpoint ] | ( ( { c_time = t } as a ) :: l ) as l ' -> if t > time then a ( :: traverse l ) else if t = time then raise Exit else checkpoint :: l ' in checkpoints := trave...
let remove_checkpoint checkpoint = checkpoints := remove_1st checkpoint ! checkpoints
let wait_for_connection checkpoint = try Exec . unprotect ( function ( ) -> let old_controller = Input_handling . current_controller ! connection in execute_with_other_controller ( function fd -> old_controller fd ; if checkpoint . c_valid = true then exit_main_loop ( ) ) ! connection main_...
let set_current_checkpoint checkpoint = if ! debug_time_travel then prerr_endline ( " Select : " ^ ( Int . to_string checkpoint . c_pid ) ) ; if not checkpoint . c_valid then wait_for_connection checkpoint ; current_checkpoint := checkpoint ; let dead_frags = List . filter ( fun frag ...
let kill_checkpoint checkpoint = if ! debug_time_travel then prerr_endline ( " Kill : " ^ ( Int . to_string checkpoint . c_pid ) ) ; if checkpoint . c_pid > 0 then ( if not checkpoint . c_valid then wait_for_connection checkpoint ; stop checkpoint . c_fd ; if checkpoint . c_parent ...
let cut t = let rec cut_t = function [ ] -> ( [ ] , [ ] ) | ( { c_time = t ' } as a :: l ) as l ' -> if t ' <= t then ( [ ] , l ' ) else let ( b , e ) = cut_t l in ( a :: b , e ) in cut_t
let cut2 t0 t l = let rec cut2_t0 t = function [ ] -> [ ] | l -> let ( after , before ) = cut ( t0 -- t -- _1 ) l in let l = cut2_t0 ( t ++ t ) before in after :: l in let ( after , before ) = cut ( t0 -- _1 ) l in after ( :: cut2_t0 t before )
let chk_merge2 cont = let rec chk_merge2_cont = function [ ] -> cont | [ a ] -> let ( accepted , rejected ) = cont in ( a :: accepted , rejected ) | a :: l -> let ( accepted , rejected ) = chk_merge2_cont l in ( accepted , a :: rejected ) in chk_merge2_cont
let rec chk_merge = function [ ] -> ( [ ] , [ ] ) | l :: tail -> chk_merge2 ( chk_merge tail ) l
let new_checkpoint_list checkpoint_count accepted rejected = if List . length accepted >= checkpoint_count then let ( k , l ) = list_truncate2 checkpoint_count accepted in ( k , l @ rejected ) else let ( k , l ) = list_truncate2 ( checkpoint_count - List . length accepted ) rejected in (...
let clean_checkpoints time checkpoint_count = let ( after , before ) = cut time ! checkpoints in let ( accepted , rejected ) = chk_merge ( cut2 time ! checkpoint_small_step before ) in let ( kept , lost ) = new_checkpoint_list checkpoint_count accepted after in List . iter kill_checkpoint ...
let find_checkpoint_before time = let rec find = function [ ] -> print_string " Can ' t go that far in the past " ; ! print_newline ( ) ; if yes_or_no " Reload program " then begin load_program ( ) ; find ! checkpoints end else raise Toplevel | { c_time = t } as a :: l -> if t ...
let duplicate_current_checkpoint ( ) = let checkpoint = ! current_checkpoint in if not checkpoint . c_valid then wait_for_connection checkpoint ; let new_checkpoint = { c_time = checkpoint . c_time ; c_pid = 0 ; c_fd = checkpoint . c_fd ; c_valid = false ; c_report = checkpoint . c_rep...
let interrupted = ref false
let last_breakpoint = ref None
let last_debug_info = ref None
let rec do_go_dynlink steps = match do_go steps with | { rep_type = Code_loaded frag ; rep_event_count = steps } as report -> begin match ! last_debug_info with | Some di -> Symbols . add_symbols frag di ; Symbols . set_all_events frag ; last_debug_info := None | None -> assert false end ; if...
let rec stop_on_event report = match report with { rep_type = Breakpoint ; rep_program_pointer = pc ; rep_stack_pointer = sp } -> last_breakpoint := Some ( pc , sp ) ; Symbols . update_current_event ( ) ; begin match ! current_event with None -> find_event ( ) | Some _ -> ( ) ...