Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 07167f0

Browse files
nkrkvcristianoc
andauthored
Bench on Linux (#581)
* Make benchmarking work on Linux * Rename `mac_osx_time.c` to less platform-specific `time.c` * Semi missing. Co-authored-by: Cristiano Calcagno <cristianoc@users.noreply.github.com>
1 parent 2074447 commit 07167f0

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

benchmarks/dune

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22
(name benchmark)
33
(public_name bench)
44
(enabled_if
5-
(= %{system} macosx))
5+
(or
6+
(= %{system} macosx)
7+
; or one of Linuxes (see https://github.com/ocaml/ocaml/issues/10613)
8+
(= %{system} linux)
9+
(= %{system} linux_elf)
10+
(= %{system} elf)
11+
(= %{system} linux_eabihf)
12+
(= %{system} linux_eabi)))
613
(flags
714
(-open Syntax -open Compilerlibs406))
815
(foreign_stubs
916
(language c)
10-
(names mac_osx_time))
17+
(names time))
1118
(libraries syntax compilerlibs406))
1219

1320
(data_only_dirs data)

benchmarks/mac_osx_time.c

Lines changed: 0 additions & 16 deletions
This file was deleted.

benchmarks/time.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <caml/mlvalues.h>
2+
#include <caml/alloc.h>
3+
4+
//
5+
// Platform-specific includes
6+
//
7+
#if (defined(__MACH__) && defined(__APPLE__))
8+
#include <mach/mach_time.h>
9+
#elif defined(__linux__)
10+
#include <time.h>
11+
#endif
12+
13+
//
14+
// Platform-specific globals
15+
//
16+
#if (defined(__MACH__) && defined(__APPLE__))
17+
static mach_timebase_info_data_t info;
18+
#endif
19+
20+
//
21+
// Exported functions
22+
//
23+
CAMLprim value caml_mach_initialize(value unit) {
24+
#if (defined(__MACH__) && defined(__APPLE__))
25+
mach_timebase_info(&info);
26+
#endif
27+
28+
return Val_unit;
29+
}
30+
31+
CAMLprim value caml_mach_absolute_time(value unit) {
32+
uint64_t result = 0;
33+
34+
#if (defined(__MACH__) && defined(__APPLE__))
35+
uint64_t now = mach_absolute_time();
36+
result = (now * info.numer) / info.denom;
37+
#elif defined(__linux__)
38+
struct timespec now;
39+
clock_gettime(CLOCK_MONOTONIC, &now);
40+
result = now.tv_sec * 1000 + now.tv_nsec / 1000000;
41+
#endif
42+
43+
return caml_copy_int64(result);
44+
}

0 commit comments

Comments
 (0)