summaryrefslogtreecommitdiffstats
path: root/darwin/Platform.c
diff options
context:
space:
mode:
authorAlexander Momchilov <alexandermomchilov@gmail.com>2021-08-22 10:47:11 -0400
committerAlexander Momchilov <alexandermomchilov@gmail.com>2021-08-25 11:55:05 -0400
commit59d0c5b26a55a68be059f2ac32a7c083b2ff01ee (patch)
tree740efac73ae33c02ca512f46c3ee2f8119c73ef8 /darwin/Platform.c
parentfa48c484cc6db90736789b9ff811fd5bc8dc119d (diff)
Refactor Darwin platform unit conversion helpers
Diffstat (limited to 'darwin/Platform.c')
-rw-r--r--darwin/Platform.c29
1 files changed, 22 insertions, 7 deletions
diff --git a/darwin/Platform.c b/darwin/Platform.c
index 2630e2d4..c5a11df2 100644
--- a/darwin/Platform.c
+++ b/darwin/Platform.c
@@ -120,9 +120,9 @@ const MeterClass* const Platform_meterTypes[] = {
NULL
};
-double Platform_timebaseToNS = 1.0;
+static double Platform_nanosecondsPerMachTick = 1.0;
-long Platform_clockTicksPerSec = -1;
+static double Platform_schedulerTicksPerNS = -1;
void Platform_init(void) {
// Check if we can determine the timebase used on this system.
@@ -130,18 +130,33 @@ void Platform_init(void) {
#ifdef HAVE_MACH_TIMEBASE_INFO
mach_timebase_info_data_t info;
mach_timebase_info(&info);
- Platform_timebaseToNS = (double)info.numer / (double)info.denom;
+ Platform_nanosecondsPerMachTick = (double)info.numer / (double)info.denom;
#else
- Platform_timebaseToNS = 1.0;
+ Platform_nanosecondsPerMachTick = 1.0;
#endif
- // Determine the number of clock ticks per second
+ // Determine the number of scheduler clock ticks per second
errno = 0;
- Platform_clockTicksPerSec = sysconf(_SC_CLK_TCK);
+ long scheduler_ticks_per_sec = sysconf(_SC_CLK_TCK);
- if (errno || Platform_clockTicksPerSec < 1) {
+ if (errno || scheduler_ticks_per_sec < 1) {
CRT_fatalError("Unable to retrieve clock tick rate");
}
+
+ const double nanos_per_sec = 1e9;
+ Platform_schedulerTicksPerNS = nanos_per_sec / scheduler_ticks_per_sec;
+}
+
+// Converts ticks in the Mach "timebase" to nanoseconds.
+// See `mach_timebase_info`, as used to define the `Platform_nanosecondsPerMachTick` constant.
+uint64_t Platform_machTicksToNanoseconds(uint64_t mach_ticks) {
+ return (uint64_t) ((double) mach_ticks * Platform_nanosecondsPerMachTick);
+}
+
+// Converts "scheduler ticks" to nanoseconds.
+// See `sysconf(_SC_CLK_TCK)`, as used to define the `Platform_schedulerTicksPerNS` constant.
+double Platform_schedulerTicksToNanoseconds(const double scheduler_ticks) {
+ return scheduler_ticks * Platform_schedulerTicksPerNS;
}
void Platform_done(void) {

© 2014-2024 Faster IT GmbH | imprint | privacy policy