trace.util: add Unix_util and Mock_

This commit is contained in:
Simon Cruanes 2026-01-15 20:42:19 -05:00
parent 85ef7f4587
commit 7b0197e6c2
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
5 changed files with 54 additions and 0 deletions

View file

@ -16,6 +16,11 @@
(mtime mtime.clock.jsoo -> time_util.mtime.ml) (mtime mtime.clock.jsoo -> time_util.mtime.ml)
(unix -> time_util.unix.ml) (unix -> time_util.unix.ml)
(-> time_util.dummy.ml)) (-> time_util.dummy.ml))
(select
unix_util.ml
from
(unix -> unix_util.real.ml)
(-> unix_util.dummy.ml))
(select (select
domain_util.ml domain_util.ml
from from

46
src/util/mock_.ml Normal file
View file

@ -0,0 +1,46 @@
(** Mocking for tests *)
module Inner = struct
let mock = ref false
let get_now_ns_ref = ref Time_util.get_time_ns
let get_tid_ref = ref Thread_util.get_tid
let get_pid_ref = ref Unix_util.get_pid
(** used to mock timing *)
let make_time_mock () : unit -> int64 =
let time_ = ref 0 in
let get_now_ns () : int64 =
let x = !time_ in
incr time_;
Int64.(mul (of_int x) 1000L)
in
get_now_ns
end
(** Now, in nanoseconds. Uses {!get_now_ns_ref} *)
let[@inline] now_ns () : int64 =
if !Inner.mock then
!Inner.get_now_ns_ref ()
else
Time_util.get_time_ns ()
(** Current thread's ID. Uses {!get_tid_ref} *)
let[@inline] get_tid () : int =
if !Inner.mock then
!Inner.get_tid_ref ()
else
Thread_util.get_tid ()
let[@inline] get_pid () : int =
if !Inner.mock then
!Inner.get_pid_ref ()
else
Unix_util.get_pid ()
let mock_all () : unit =
Inner.mock := true;
(Inner.get_pid_ref := fun () -> 2);
(Inner.get_tid_ref := fun () -> 3);
Inner.get_now_ns_ref := Inner.make_time_mock ();
()

View file

@ -0,0 +1 @@
let get_pid () = -1

1
src/util/unix_util.mli Normal file
View file

@ -0,0 +1 @@
val get_pid : unit -> int

View file

@ -0,0 +1 @@
let get_pid = Unix.getpid