dbuenzli / mtime Goto Github PK
View Code? Open in Web Editor NEWMonotonic wall-clock time for OCaml
Home Page: http://erratique.ch/software/mtime
License: ISC License
Monotonic wall-clock time for OCaml
Home Page: http://erratique.ch/software/mtime
License: ISC License
Remove the span_
prefix.
Hello,
opam install mtime failed for me on windows (mingw64).
I tried to compile manually. For some reason I did not understood, I had to do the following to achieve compilation:
And I solved my problem like this, but really do not understand:
The first pkg build gives:
$ ocaml.exe pkg/pkg.ml build --pinned false --with-js_of_ocaml false
mkdir 'C:\OCaml64\home\MatthieuDubuget\mtime.1.2.0\_build'
Le chemin d’accès spécifié est introuvable.
''ocamlfind ocamlopt unix.cmxa -I 'C:/OCaml64/home/MatthieuDubuget/.opam/ocaml-variants.4.12.0+flambda+mingw64c/lib\ocamlbuild' 'C:/OCaml64/home/MatthieuDubuget/.opam/ocaml-variants.4.12.0+flambda+mingw64c/lib\ocamlbuild/ocamlbuildlib.cmxa' -linkpkg myocamlbuild.ml 'C:/OCaml64/home/MatthieuDubuget/.opam/ocaml-variants.4.12.0+flambda+mingw64c/lib\ocamlbuild/ocamlbuild.cmx' -o myocamlbuild.exe
SANITIZE: a total of 3 files that should probably not be in your source tree
has been found. A script shell file
"C:\\OCaml64\\home\\MatthieuDubuget\\mtime.1.2.0\\_build/sanitize.sh" is
being created. Check this script and run it to remove unwanted files or use
other options (such as defining hygiene exceptions or using the -no-hygiene
option).
IMPORTANT: I cannot work with leftover compiled files.
ERROR: Leftover object files:
File myocamlbuild.o in _build has suffix .o
ERROR: Leftover OCaml compilation files:
File myocamlbuild.cmi in _build has suffix .cmi
File myocamlbuild.cmx in _build has suffix .cmx
Exiting due to hygiene violations.
pkg.ml: [ERROR] cmd ["ocamlbuild" "-use-ocamlfind" "-classic-display" "-j" "4" "-tag" "debug"
"-build-dir" "_build" "opam" "pkg/META" "CHANGES.md" "LICENSE.md"
"README.md" "src/mtime.a" "src/mtime.cmxs" "src/mtime.cmxa"
"src/mtime.cma" "src/mtime.cmx" "src/mtime.cmi" "src/mtime.mli"
"src/mtime_top.a" "src/mtime_top.cmxs" "src/mtime_top.cmxa"
"src/mtime_top.cma" "src/mtime_top.cmx" "src/mtime_top_init.ml"
"doc/index.mld" "src-os/mtime_clock.a" "src-os/mtime_clock.cmxs"
"src-os/mtime_clock.cmxa" "src-os/mtime_clock.cma"
"src-os/mtime_clock.cmx" "src-os/mtime_clock.cmi"
"src-os/mtime_clock.mli" "src-os/dllmtime_clock_stubs.dll"
"src-os/libmtime_clock_stubs.a" "test-os/min_os.ml"]: exited with 1
and the sanitize.sh is:
#!/bin/sh
# File generated by ocamlbuild
cd 'C:\OCaml64\home\MatthieuDubuget\mtime.1.2.0'
rm -f _build/myocamlbuild.cmi
rm -f _build/myocamlbuild.cmx
rm -f _build/myocamlbuild.o
# Also clean the script itself
rm -f 'C:\OCaml64\home\MatthieuDubuget\mtime.1.2.0\_build/sanitize.sh'
Should print units according to magnitude without too much floating point noise.
Same issue as dbuenzli/jsonc#3
I could not find the reason why 4.03 is now needed.
Issue to discuss the upcoming api overhaul.
mtime_stubs.c calculates time in nanoseconds using:
(now.tv_sec - start.tv_sec) * 1000000000 + (now.tv_nsec - start.tv_nsec)
This does arithmetic at type time_t
. If that's 32-bit, this will overflow in a few seconds. It should use 64-bit arithmetic explicitly, either with casts or temporaries, e.g:
(int64)(now.tv_sec - start.tv_sec) * (int64)1000000000 +
(int64)(now.tv_nsec - start.tv_nsec)
or
int64 time = now.tv_sec - start.tv_sec;
time *= 1000000000;
time += now.tv_nsec - start.tv_nsec
My system has a 64-bit time_t
, so I haven't actually tested any of this :)
It'd be nice to have @since 1.4
on, say, Mtime.Span.ms
. It's useful when trying to write correct lower bounds for opam.
I'm trying to convert back and forth between Mtime.t
and the timespec
structure as described in the C bindings here: https://github.com/dsheets/ocaml-unix-time/blob/master/lib/time.mli .
Currently, to convert from Mtime.t
to Timespec.t
, I only found the following way:
let timespec_of_mtime mtime =
let timestamp_ns = Mtime.to_uint64_ns mtime in
let span = Mtime.Span.of_uint64_ns timestamp_ns in
let sec = Mtime.Span.to_s span |> Int64.of_float in
let sec_span = Mtime.Span.of_uint64_ns sec in
let nsec = Mtime.Span.abs_diff span sec_span in
Timespec.{ sec; nsec = Mtime.Span.to_uint64_ns nsec |> Int64.to_int }
...which seems convoluted, and I'm a bit scared of a loss of precision by going through floats.
Could there be some additional conversion functions from timestamps directly, and maybe also functions to get the fractional part of a timestamp / span? (for example the rust time stdlib seems to contain some of these).
I'm using mtime
with ocamlopt
but it would be nice to have mtime
working with the toplevel or bytecode too.
Apparently it tries to load rt
as a bytecode library and not the system librt.so
:
$ ocamlc -version
4.02.3
$ ocamlfind ocamlc -package mtime.os x.ml -linkpkg -verbose
Effective set of compiler predicates: pkg_mtime.os,autolink,byte
+ ocamlc.opt -verbose -I /home/edwin/.opam/4.02.3/lib/mtime/os /home/edwin/.opam/4.02.3/lib/mtime/os/mtime.cma x.ml
File "x.ml", line 1:
Error: Error on dynamically loaded library: dllrt.so: dllrt.so: cannot open shared object file: No such file or directory
ocamlc.opt returned with exit code 2
$ utop
[...]
utop # #require "mtime.os.top";;
Cannot load required shared library dllrt. Reason: dllrt.so: dllrt.so: cannot open shared object file: No such file or directory. File "/home/edwin/.opam/4.02.3/lib/mtime/os/mtime_top_init.ml", line 1:
Error: Reference to undefined global `Mtime'
Using performance counters. PR welcome.
Using the min.ml
example in the documentation.
ocamlfind ocamlc -package mtime.os -linkpkg -custom -o min.byte min.ml
/tmp/camlprimfa1abf-4fadb0.o:(.data+0x9d8): undefined reference to `ocaml_mtime_elapsed_ns'
clang-3.9: error: linker command failed with exit code 1 (use -v to see invocation)
File "min.ml", line 1:
Error: Error while building custom runtime system
When trying to compile mtime.1.2.0
with the latest 5.1.0+trunk
, we observe the following error:
File "myocamlbuild.ml", line 1:
Error: /home/guest/sandmark/_opam/5.1.0+trunk/lib/ocamlbuild/ocamlbuild_plugin.cmx
was built with -for-pack Ocamlbuild_pack, but the
current unit Myocamlbuild is not
Command exited with code 2.
pkg.ml: [ERROR] cmd ['ocamlbuild' '-use-ocamlfind' '-classic-display' '-j' '4' '-tag' 'debug'
'-build-dir' '_build' 'opam' 'pkg/META' 'CHANGES.md' 'LICENSE.md'
'README.md' 'src/mtime.a' 'src/mtime.cmxa' 'src/mtime.cma'
'src/mtime.cmx' 'src/mtime.cmi' 'src/mtime.mli' 'src/mtime_top.a'
'src/mtime_top.cmxa' 'src/mtime_top.cma' 'src/mtime_top.cmx'
'src/mtime_top_init.ml' 'doc/index.mld' 'src-os/mtime_clock.a'
'src-os/mtime_clock.cmxa' 'src-os/mtime_clock.cma'
'src-os/mtime_clock.cmx' 'src-os/mtime_clock.cmi'
'src-os/mtime_clock.mli' 'src-os/dllmtime_clock_stubs.so'
'src-os/libmtime_clock_stubs.a' 'test-os/min_os.ml']: exited with 10
What is a possible fix for the same?
Would avoid the double _ns
, _s
functions. Accessing concrete values should then go through conversion functions.
The following fails in a 4.14 switch with flambda:
% cat test.ml
let _ = Mtime_clock.now_ns ()
% ocamlbuild -pkg mtime.clock -pkg mtime.clock.os test.native --
+ /home/fabian/.opam/4.14.0/bin/ocamlopt.opt -I /home/fabian/.opam/4.14.0/lib/mtime -I /home/fabian/.opam/4.14.0/lib/mtime/clock -I /home/fabian/.opam/4.14.0/lib/mtime/clock/os /home/fabian/.opam/4.14.0/lib/mtime/mtime.cmxa /home/fabian/.opam/4.14.0/lib/mtime/clock/os/mtime_clock.cmxa test.cmx -o test.native
File "_none_", line 1:
Error: Files test.cmx
and /home/fabian/.opam/4.14.0/lib/mtime/clock/os/mtime_clock.cmxa
make inconsistent assumptions over interface Mtime_clock
Command exited with code 2.
(removing -pkg mtime.clock
fixes the issue, but it looks like there are some conflicting files in the installation, so I figured I'd raise it just in case)
Seems that 0.8.3 broke windows support, see https://github.com/fdopen/opam-repository-mingw/blob/master/packages/mtime/mtime.0.8.3/files/mtime-0.8.3.patch for a possible fix.
/cc @fdopen
I'm trying to convert OCaml durations (floats) to Mtime.Span.t, but having some trouble. Int64.of_float
says:
If the truncated floating-point number is outside the range
[Int64.min_int, Int64.max_int]
, no exception is raised, and an unspecified, platform-dependent integer is returned.
So I want to check the number is in-range before converting. But max_span
seems to behave strangely:
utop # #require "fmt,mtime";;
utop # Mtime.Span.to_s (Mtime.Span.max_span);;
- : float = -1e-09
utop # Fmt.to_to_string Mtime.Span.pp Mtime.Span.max_span;;
- : string = "-0.001us"
What's the recommended way to convert a float in seconds to a span? Mtime's *
operator says:
{b Warning.} Does not check for overflow or that [n] is positive.
So I guess I can't use that.
It took me some trial and error and digging around to figure out how to build using the Mtime_clock
module. In particular, if using the jbuilder
build system, one has to list mtime.clock.os
as a dependency, rather than the obvious mtime.clock
. I think the same is true for oasis
.
It would be helpful to new users if this detail were listed somewhere, perhaps straight in the README.md
where Mtime_clock
is first mentioned, and also in the API docs for that module.
Would be nice to get a Span in seconds without having to go through a float.
Most network protocols talk in seconds, the most common operation is checking if something is too old on a second granularity. In this case the jitter is so high it doesn't matter if you round up or down.
Did I miss something or should I work on this ?
I get the following error log when I try to build mtime
with 4.14.0+trunk
and dune.2.9.0
#=== ERROR while compiling mtime.1.2.0 ========================================#
# context 2.0.8 | linux/x86_64 | ocaml-variants.4.14.0+trunk | https://opam.ocaml.org#1fc79772
# path ~/.opam/4.14.0+trunk/.opam-switch/build/mtime.1.2.0
# command ~/.opam/opam-init/hooks/sandbox.sh build ocaml pkg/pkg.ml build --pinned false --with-js_of_ocaml false
# exit-code 1
# env-file ~/.opam/log/mtime-2324156-d2c37b.env
# output-file ~/.opam/log/mtime-2324156-d2c37b.out
### output ###
# [...]
# Command exited with code 2.
# pkg.ml: [ERROR] cmd ['ocamlbuild' '-use-ocamlfind' '-classic-display' '-j' '4' '-tag' 'debug'
# '-build-dir' '_build' 'opam' 'pkg/META' 'CHANGES.md' 'LICENSE.md'
# 'README.md' 'src/mtime.a' 'src/mtime.cmxs' 'src/mtime.cmxa'
# 'src/mtime.cma' 'src/mtime.cmx' 'src/mtime.cmi' 'src/mtime.mli'
# 'src/mtime_top.a' 'src/mtime_top.cmxs' 'src/mtime_top.cmxa'
# 'src/mtime_top.cma' 'src/mtime_top.cmx' 'src/mtime_top_init.ml'
# 'doc/index.mld' 'src-os/mtime_clock.a' 'src-os/mtime_clock.cmxs'
# 'src-os/mtime_clock.cmxa' 'src-os/mtime_clock.cma'
# 'src-os/mtime_clock.cmx' 'src-os/mtime_clock.cmi'
# 'src-os/mtime_clock.mli' 'src-os/dllmtime_clock_stubs.so'
# 'src-os/libmtime_clock_stubs.a' 'test-os/min_os.ml']: exited with 10
E.g. mach_absolute_time
doesn't take into account if the OS what put to sleep. I believe this is also the case for CLOCK_MONOTONIC
.
This is somewhat redundant with Mtime.{ns,s}_of
functions but it can lead to cleaner code. For example adding three days:
let t' = t +. (3. *. Mtime.day_s)
(* vs *)
let t' = t +. (Mtime.s_of `Day 3.)
See e.g. #27
On various systems (including x86_32), mtime fails to link. It's easy to reproduce with dune by simply depending on mtime.clock.os
, but it fails with ocamlfind
too:
$ touch main.ml
$ ocamlfind ocamlc -output-complete-exe -linkpkg -package mtime.clock.os main.ml
/usr/bin/ld: cannot find -lmtime_clock_stubs
collect2: error: ld returned 1 exit status
File "main.ml", line 1:
Error: Error while building custom runtime system
I tried to run mtime's own tests, but:
$ ocaml pkg/pkg.ml test
pkg.ml: No tests to run.
Other packages with C stubs (e.g. cstruct
) do work.
(@mseri reported this as an Eio bug at ocaml-multicore/eio#407, but it doesn't seem like it has much to do with Eio)
Something like this patch would help cross-compilation:
--- a/myocamlbuild.ml 2018-03-04 11:03:00.253554294 +0000
+++ b/myocamlbuild.ml 2018-03-04 11:03:25.285553267 +0000
@@ -2,7 +2,11 @@
open Ocamlbuild_plugin
open Command
-let os = Ocamlbuild_pack.My_unix.run_and_read "uname -s"
+let os =
+ try
+ Sys.getenv "MTIME_OS"
+ with Not_found ->
+ Ocamlbuild_pack.My_unix.run_and_read "uname -s"
let system_support_lib = match os with
| "Linux\n" -> [A "-cclib"; A "-lrt"]
Related to #2.
Currently, only the result of subtracting absolute monotonic time stamps is exposed and not their absolute values. One usage scenario that this prevents is for a program to be able to measure the monotonic amount of time that occured between two runs of the program itself.
However the only function used in the backends that provides us a documented guarantee to be able to do that seems to be clock_gettime
(it gives the guarantee that the time stamps are system wide starting from an epoch cast in stone at startup time) and you'd still need a way to be able to detect if the operating system hasn't rebooted meanwhile. For sure JavaScript's performance.now ()
doesn't give us that as it seems to start from zero once the page load.
It seems that this would be better solved by looking up some kind of TAI clock (if only OSes would provide us with that).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.