123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325openCoreopenCore_profiler_disabledmoduleProfiler=structletis_enabled=trueletsafe_to_delay()=Common.maybe_do_slow_tasksCommon.Offline_profiler~reluctance:1;Protocol.Buffer.ensure_free2048letdump_stats()=Protocol.Writer.dump_stats()letconfigure?don't_require_core_profiler_env?offline_profiler_data_file?online_print_time_interval_secs:_?online_print_by_default:_()=Option.iterdon't_require_core_profiler_env~f:(fun()->Check_environment.don't_require_core_profiler_env());Option.iteroffline_profiler_data_file~f:(funfile->Protocol.set_current_output_filenamefile);;;endmoduleTimer=structtypetimer=Probe_id.ttypet=timertypeprobe=tletcreate~name=Check_environment.check_safety_exn();letid=Probe_id.create()inProtocol.Writer.write_new_singleidnameProbe_type.Timer;idletrecordid=letn=Common.nowCommon.Offline_profiler~reluctance:3()inProtocol.Writer.write_timer_atidnmoduleGroup=structtypet=Probe_id.tletcreate~name=Check_environment.check_safety_exn();letid=Probe_id.create()inProtocol.Writer.write_new_groupidnameProbe_type.Timer;idletadd_probegroup?(sources=[||])~name()=letid=Probe_id.create()in(* Note! sources : Point.t array = Point_id.t array *)Protocol.Writer.write_new_group_point~group_id:group~idnamesources;idletresetgroup=letn=Common.nowCommon.Offline_profiler~reluctance:2()inProtocol.Writer.write_group_resetgroupnendendlet%bench_module"Timer"=(modulestructlet()=Profiler.configure()~don't_require_core_profiler_env:()lettimer=Timer.create~name:"bench_timer"(* let group = Timer.Group.create "bench_timer_group" ()
* let group_probe = Timer.Group.add_probe group "bench_timer_group_probe" *)let%bench"at"=Timer.recordtimer(* BENCH "group_probe_at" = Timer.Group.Probe.at group_probe
*
* BENCH "group_reset" = Timer.Group.reset group *)let()=Protocol.Writer.set_at_exit_handler`Disableend)moduleProbe=structtypeprobe=Probe_id.ttypet=probeletcreate~name~units=Check_environment.check_safety_exn();letid=Probe_id.create()inProtocol.Writer.write_new_singleidname(Probe_type.Probeunits);idletrecordidvalue=letn=Common.nowCommon.Offline_profiler~reluctance:3()inProtocol.Writer.write_probe_atidnvaluemoduleGroup=structtypet=Probe_id.tletcreate~name~units=Check_environment.check_safety_exn();letid=Probe_id.create()inProtocol.Writer.write_new_groupidname(Probe_type.Probeunits);idletadd_probegroup?(sources=[||])~name()=letid=Probe_id.create()inProtocol.Writer.write_new_group_point~group_id:group~idnamesources;idletresetgroup=letn=Common.nowCommon.Offline_profiler~reluctance:2()inProtocol.Writer.write_group_resetgroupnendendlet%bench_module"Probe"=(modulestructlet()=Profiler.configure()~don't_require_core_profiler_env:()lettimer=Probe.create~name:"bench_probe"~units:Profiler_units.Seconds(* let group = Probe.Group.create "bench_probe_group" Profiler_units.Int
* let group_probe = Probe.Group.add_probe group "bench_probe_group_probe" *)let%bench"at"=Probe.recordtimer19827312(* BENCH "group_probe_at" = Probe.Group.Probe.at group_probe 123812
*
* BENCH "group_reset" = Probe.Group.reset group *)let()=Protocol.Writer.set_at_exit_handler`Disableend)moduleDelta_timer=structtypestate=Time_ns.ttypet={probe:Probe.t;mutablestate:state;mutableaccum:int}letcreate~name={probe=Probe.create~name~units:Profiler_units.Nanoseconds;state=Time_ns.epoch;accum=0}letdiffnstate=Time_ns.diffnstate|>Time_ns.Span.to_int_ns(* If we calibrate on start, we get back the time before we started calibrating,
and those 300ns will be included in the delta. *)letstateless_start_t=Common.nowCommon.Offline_profiler~reluctance:4()letstateless_stoptstate=(* Avoid calling Common.now () twice: *)letn=Common.nowCommon.Offline_profiler~reluctance:2()inletd=diffnstateinProtocol.Writer.write_probe_att.probendletstartt=letn=Common.nowCommon.Offline_profiler~reluctance:4()int.state<-nletaccumulatetn=t.accum<-t.accum+(diffnt.state);t.state<-nletwrite_probe_attn=Protocol.Writer.write_probe_att.probent.accum;t.accum<-0;t.state<-nletpauset=letn=Common.nowCommon.Offline_profiler~reluctance:4()inaccumulatetnletrecordt=letn=Common.nowCommon.Offline_profiler~reluctance:2()inwrite_probe_attnletstopt=letn=Common.nowCommon.Offline_profiler~reluctance:2()inaccumulatetn;write_probe_attnletwrap_synctfx=letstate=stateless_starttinletr=tryfxwithex->stateless_stoptstate;Exn.reraiseex"Core_profiler Delta_timer.wrap_sync"instateless_stoptstate;rletwrap_sync2tfxy=letstate=stateless_starttinletr=tryfxywithex->stateless_stoptstate;Exn.reraiseex"Core_profiler Delta_timer.wrap_sync2"instateless_stoptstate;rletwrap_sync3tfxyz=letstate=stateless_starttinletr=tryfxyzwithex->stateless_stoptstate;Exn.reraiseex"Core_profiler Delta_timer.wrap_sync3"instateless_stoptstate;rletwrap_sync4tfxyzw=letstate=stateless_starttinletr=tryfxyzwwithex->stateless_stoptstate;Exn.reraiseex"Core_profiler Delta_timer.wrap_sync4"instateless_stoptstate;r(* let wrap_async t f x =
* let open Async in
* let state = start_async t in
* try_with ~run:`Now (fun () -> f x) >>= fun res ->
* stop_async t state;
* match res with
* | Ok x -> return x
* | Error ex -> Exn.reraise ex "Core_profiler Delta_timer.wrap_async" *)endlet%bench_module"Delta_timer"=(modulestructlet()=Profiler.configure()~don't_require_core_profiler_env:()letdelta=Delta_timer.create~name:"unittest"letstarted=Delta_timer.stateless_startdeltalet%bench"start_async"=Delta_timer.stateless_startdeltalet%bench"stop_async"=Delta_timer.stateless_stopdeltastartedlet%bench"start"=Delta_timer.startdeltalet%bench"stop"=Delta_timer.stopdeltaend)let%bench_module"Delta_timer.wrap_sync"=(modulestructlet()=Profiler.configure()~don't_require_core_profiler_env:()letnop()=()letwrapped_nop=letdelta=Delta_timer.create~name:"nop"inDelta_timer.wrap_syncdeltanopletcount_256()=for_=1to256do()doneletwrapped_count_256=letdelta=Delta_timer.create~name:"count_256"inDelta_timer.wrap_syncdeltacount_256let%bench"nop"=nop()let%bench"wrapped_nop"=wrapped_nop()let%bench"count_256"=count_256()let%bench"wrapped_count_256"=wrapped_count_256()end)(* stateless Delta_probe does not support pausing *)moduleDelta_probe=structtypestate=inttypet={probe:Probe.t;mutablestate:state;mutableaccum:state}letcreate~name~units={probe=Probe.create~name~units;state=0;accum=0}letstateless_start_tvalue=valueletstateless_stoptstatevalue=Probe.recordt.probe(value-state)letstarttvalue=t.state<-valueletrecordt=Probe.recordt.probet.accum;t.accum<-0letpausetvalue=t.accum<-t.accum+(value-t.state)letstoptvalue=pausetvalue;recordt;endlet%bench_module"Delta_probe"=(modulestructlet()=Profiler.configure()~don't_require_core_profiler_env:()letdelta=Delta_probe.create~name:"unittest"~units:Profiler_units.Intletstarted=Delta_probe.stateless_startdelta123let%bench"start"=Delta_probe.startdelta123let%bench"stop"=Delta_probe.stopdelta456let%bench"start_async"=Delta_probe.stateless_startdelta123let%bench"stop_async"=Delta_probe.stateless_stopdeltastarted456end)