Skip to content

Commit

Permalink
make PCLOCK a device
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgray committed Jul 27, 2016
1 parent 439c7d4 commit b9b225e
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 48 deletions.
4 changes: 2 additions & 2 deletions _oasis
Expand Up @@ -23,7 +23,7 @@ Library "mirage-clock-unix"
CSources: clock_stubs.c
CCOpt: -O2 -g -Wall
Findlibname: mirage-clock-unix
BuildDepends: mirage-types
BuildDepends: mirage-types, lwt

Library "mirage-clock-xen"
Build$: flag(xen)
Expand All @@ -39,7 +39,7 @@ Executable "portable"
Install: false
Path: lib_test
MainIs: portable.ml
BuildDepends: mirage-clock-unix, cstruct, mirage-types
BuildDepends: mirage-clock-unix, cstruct, mirage-types, lwt.unix

Test "portable-test"
Command: $portable
Expand Down
8 changes: 7 additions & 1 deletion _tags
@@ -1,5 +1,5 @@
# OASIS_START
# DO NOT EDIT (digest: 18fc54a17d287ef21746fd3ab54ea843)
# DO NOT EDIT (digest: 50e3721c7ecf0608a819c188f5b7bcfd)
# Ignore VCS directories, you can use the same kind of rule outside
# OASIS_START/STOP if you want to exclude directories that contains
# useless stuff for the build process
Expand All @@ -19,16 +19,22 @@ true: annot, bin_annot
<unix/*.ml{,i,y}>: oasis_library_mirage_clock_unix_ccopt
"unix/clock_stubs.c": oasis_library_mirage_clock_unix_ccopt
<unix/mirage-clock-unix.{cma,cmxa}>: use_libmirage-clock-unix_stubs
<unix/*.ml{,i,y}>: pkg_lwt
<unix/*.ml{,i,y}>: pkg_mirage-types
"unix/clock_stubs.c": pkg_lwt
"unix/clock_stubs.c": pkg_mirage-types
# Library mirage-clock-xen
"xen/mirage-clock-xen.cmxs": use_mirage-clock-xen
<xen/*.ml{,i,y}>: pkg_mirage-types
# Executable portable
<lib_test/portable.{native,byte}>: pkg_cstruct
<lib_test/portable.{native,byte}>: pkg_lwt
<lib_test/portable.{native,byte}>: pkg_lwt.unix
<lib_test/portable.{native,byte}>: pkg_mirage-types
<lib_test/portable.{native,byte}>: use_mirage-clock-unix
<lib_test/*.ml{,i,y}>: pkg_cstruct
<lib_test/*.ml{,i,y}>: pkg_lwt
<lib_test/*.ml{,i,y}>: pkg_lwt.unix
<lib_test/*.ml{,i,y}>: pkg_mirage-types
<lib_test/*.ml{,i,y}>: use_mirage-clock-unix
# OASIS_STOP
39 changes: 23 additions & 16 deletions lib_test/portable.ml
@@ -1,26 +1,33 @@
module C = Pclock
module M = Mclock
let (>>=) = Lwt.bind

let print_time () =
let d, ps = C.now_d_ps () in
let print_time c =
let d, ps = Pclock.now_d_ps c in
Printf.printf "The time is %d days and %Ld picoseconds since the epoch.\n" d ps

let print_offset () = match C.current_tz_offset_s () with
let print_offset c = match Pclock.current_tz_offset_s c with
| Some offset -> Printf.printf "The offset from UTC is %d minutes.\n" offset
| None -> Printf.printf "Clock UTC offset unavailable\n"

let print_period () = match C.period_d_ps () with
let print_period c = match Pclock.period_d_ps c with
| Some (d, ps) -> Printf.printf "The clock period is: %Ld picoseconds\n" ps
| None -> Printf.printf "Clock period unavailable\n"

let print_mtime () =
Printf.printf "Monotonic clock says: %Ld nanoseconds\n" (M.elapsed_ns ())
let print_mtime c =
Printf.printf "Monotonic clock says: %Ld nanoseconds\n" (Mclock.elapsed_ns c)

let _ =
print_mtime ();
print_time ();
print_time ();
print_time ();
print_offset ();
print_period ();
print_mtime ();
let main () =
let mclock = Mclock.connect () in
Pclock.connect () >>= function
`Ok clock -> (
print_mtime mclock;
print_time clock;
print_time clock;
print_time clock;
print_offset clock;
print_period clock;
print_mtime mclock;
Lwt.return_unit
)
| `Error _ -> Lwt.return_unit

let _ = Lwt_main.run (main ())
1 change: 0 additions & 1 deletion lib_test/portable.mli

This file was deleted.

14 changes: 9 additions & 5 deletions setup.ml
@@ -1,7 +1,7 @@
(* setup.ml generated for the first time by OASIS v0.4.5 *)

(* OASIS_START *)
(* DO NOT EDIT (digest: 9ba5d8750a33c1bb86e02eb4b433ac85) *)
(* DO NOT EDIT (digest: 9bcfd26db55cac1ff98e65fe2a68f611) *)
(*
Regenerated by OASIS v0.4.6
Visit http://oasis.forge.ocamlcore.org for more information and
Expand Down Expand Up @@ -6964,7 +6964,10 @@ let setup_t =
bs_path = "unix";
bs_compiled_object = Best;
bs_build_depends =
[FindlibPackage ("mirage-types", None)];
[
FindlibPackage ("mirage-types", None);
FindlibPackage ("lwt", None)
];
bs_build_tools = [ExternalTool "ocamlbuild"];
bs_c_sources = ["clock_stubs.c"];
bs_data_files = [];
Expand Down Expand Up @@ -7038,7 +7041,8 @@ let setup_t =
[
InternalLibrary "mirage-clock-unix";
FindlibPackage ("cstruct", None);
FindlibPackage ("mirage-types", None)
FindlibPackage ("mirage-types", None);
FindlibPackage ("lwt.unix", None)
];
bs_build_tools = [ExternalTool "ocamlbuild"];
bs_c_sources = [];
Expand Down Expand Up @@ -7083,14 +7087,14 @@ let setup_t =
};
oasis_fn = Some "_oasis";
oasis_version = "0.4.6";
oasis_digest = Some " \131bËʱÞ\1327\n\128{ܨÖË";
oasis_digest = Some "]õIR\\Öľ\150Ïç9\131\000ã\000";
oasis_exec = None;
oasis_setup_args = [];
setup_update = false
};;

let setup () = BaseSetup.setup setup_t;;

# 7095 "setup.ml"
# 7099 "setup.ml"
(* OASIS_STOP *)
let () = setup ();;
4 changes: 2 additions & 2 deletions unix/META
@@ -1,8 +1,8 @@
# OASIS_START
# DO NOT EDIT (digest: aed631109c0e0ea131345feb63468c5c)
# DO NOT EDIT (digest: 010e47cbabd0a2dbc2c1b40fb4ac99cd)
version = "1.1.0"
description = "Posix clocks for Mirage"
requires = "mirage-types"
requires = "mirage-types lwt"
archive(byte) = "mirage-clock-unix.cma"
archive(byte, plugin) = "mirage-clock-unix.cma"
archive(native) = "mirage-clock-unix.cmxa"
Expand Down
14 changes: 11 additions & 3 deletions unix/pclock.ml
Expand Up @@ -15,19 +15,27 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

type t = unit
type id = string
type 'a io = 'a Lwt.t
type error = unit

let ps_count_in_s = 1_000_000_000_000L

external posix_clock_gettime_s_ns : unit -> int * int = "ocaml_posix_clock_gettime_s_ns"

let now_d_ps () =
let connect _ = Lwt.return (`Ok ())
let disconnect _t = Lwt.return ()

let now_d_ps t =
let secs, ns = posix_clock_gettime_s_ns () in
let days = secs / 86_400 in
let rem_s = secs mod 86_400 in
let frac_ps = Int64.(mul (of_int ns) 1000L) in
let rem_ps = Int64.(mul (of_int rem_s) ps_count_in_s) in
(days, (Int64.add rem_ps frac_ps))

let current_tz_offset_s () =
let current_tz_offset_s t =
let now = Unix.gettimeofday () in
let utc = Unix.gmtime now in
let local = Unix.localtime now in
Expand All @@ -48,7 +56,7 @@ let current_tz_offset_s () =

external posix_clock_period_ns : unit -> int64 = "ocaml_posix_clock_period_ns"

let period_d_ps () =
let period_d_ps t =
let period_ns = posix_clock_period_ns () in
match period_ns with
| 0L -> None
Expand Down
21 changes: 3 additions & 18 deletions unix/pclock.mli
Expand Up @@ -17,22 +17,7 @@
(** {1 POSIX clock}
Clock counting time since the Unix epoch. Subject to adjustment by e.g. NTP. *)
include V1_LWT.PCLOCK
with type 'a io = 'a Lwt.t

include V1.PCLOCK

val now_d_ps : unit -> int * int64
(** [now_d_ps ()] is [(d, ps)] representing the POSIX time occuring
at [d] * 86'400e12 + [ps] POSIX picoseconds from the epoch
1970-01-01 00:00:00 UTC. [ps] is in the range
\[[0];[86_399_999_999_999_999L]\]. By definition this time
is always on the UTC timeline.*)

val current_tz_offset_s : unit -> int option
(** [current_tz_offset_s ()] is the system's current local time
zone offset to UTC in seconds, if know. This is the duration
local time - UTC time in seconds. *)

val period_d_ps : unit -> (int * int64) option
(** [period_d_ps ()] is if available [Some (d, ps)] representing the
clock's picosecond period [d] * 86'400e12 + [ps]. [ps] is in the
range \[[0];[86_399_999_999_999_999L]\]. *)
val connect : unit -> [`Ok of t | `Error of error] io

0 comments on commit b9b225e

Please sign in to comment.