aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Goulet <dgoulet@torproject.org>2022-10-14 09:12:23 -0400
committerDavid Goulet <dgoulet@torproject.org>2022-10-14 09:12:23 -0400
commite86833ade650104d610327432f2e646d13f6a2a2 (patch)
tree30ebec419c80eed7900ae17a00f76e3eab5f7d8c
parentc8d8fa0d3639058544ffe153c1b83e0ed80bf43a (diff)
parente531d4d1b9753e80b56a28805b01c014a1fe5d51 (diff)
downloadtor-e86833ade650104d610327432f2e646d13f6a2a2.tar.gz
tor-e86833ade650104d610327432f2e646d13f6a2a2.zip
Merge branch 'maint-0.4.5' into maint-0.4.7
-rw-r--r--changes/bug406846
-rw-r--r--src/lib/time/compat_time.c11
2 files changed, 13 insertions, 4 deletions
diff --git a/changes/bug40684 b/changes/bug40684
new file mode 100644
index 0000000000..8c751ede2c
--- /dev/null
+++ b/changes/bug40684
@@ -0,0 +1,6 @@
+ o Major bugfixes (OSX):
+ - Fix coarse-time computation on Apple platforms (like Mac M1) where
+ the Mach absolute time ticks do not correspond directly to
+ nanoseconds. Previously, we computed our shift value wrong, which
+ led us to give incorrect timing results.
+ Fixes bug 40684; bugfix on 0.3.3.1-alpha.
diff --git a/src/lib/time/compat_time.c b/src/lib/time/compat_time.c
index 380fbee1d5..eb716259c4 100644
--- a/src/lib/time/compat_time.c
+++ b/src/lib/time/compat_time.c
@@ -253,11 +253,14 @@ monotime_init_internal(void)
tor_assert(mach_time_info.denom != 0);
{
- // approximate only.
- uint64_t ns_per_tick = mach_time_info.numer / mach_time_info.denom;
- uint64_t ms_per_tick = ns_per_tick * ONE_MILLION;
+ // We want to compute this, approximately:
+ // uint64_t ns_per_tick = mach_time_info.numer / mach_time_info.denom;
+ // uint64_t ticks_per_ms = ONE_MILLION / ns_per_tick;
+ // This calculation multiplies first, though, to improve accuracy.
+ uint64_t ticks_per_ms = (ONE_MILLION * mach_time_info.denom)
+ / mach_time_info.numer;
// requires that tor_log2(0) == 0.
- monotime_shift = tor_log2(ms_per_tick);
+ monotime_shift = tor_log2(ticks_per_ms);
}
{
// For converting ticks to milliseconds in a 32-bit-friendly way, we