From c589d65f96ddff7e3d2fb7be74b8c9a77b8f663a Mon Sep 17 00:00:00 2001 From: lyinch Date: Thu, 17 Feb 2022 12:46:47 +0100 Subject: [PATCH 1/9] Add test and discussed fix --- library/std/src/sys/unix/time.rs | 6 ++++-- library/std/src/time/tests.rs | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/library/std/src/sys/unix/time.rs b/library/std/src/sys/unix/time.rs index 59ddd1aa92f81..13ad218ab391e 100644 --- a/library/std/src/sys/unix/time.rs +++ b/library/std/src/sys/unix/time.rs @@ -149,9 +149,11 @@ mod inner { impl Instant { pub fn now() -> Instant { extern "C" { - fn mach_absolute_time() -> u64; + //fn mach_absolute_time() -> u64; + fn clock_gettime_nsec_np(clock_type: i64) -> u64; } - Instant { t: unsafe { mach_absolute_time() } } + Instant { t: unsafe { clock_gettime_nsec_np(4) } } + //Instant { t: unsafe { mach_absolute_time() } } } pub fn checked_sub_instant(&self, other: &Instant) -> Option { diff --git a/library/std/src/time/tests.rs b/library/std/src/time/tests.rs index d1a69ff8697c6..7227ece5914de 100644 --- a/library/std/src/time/tests.rs +++ b/library/std/src/time/tests.rs @@ -12,6 +12,16 @@ macro_rules! assert_almost_eq { }}; } +#[test] +#[cfg(all(target_arch = "aarch64", target_os = "macos"))] +fn wtf() { + let t0 = Instant::now(); + let t1 = t0 + Duration::from_nanos(50); + let d = t1 - t0; + dbg!(t0, t1, d); + assert_eq!(t0 + d, t1); +} + #[test] fn instant_monotonic() { let a = Instant::now(); From 17d39d6ce182e3126a9293fe1a8e5e393206970c Mon Sep 17 00:00:00 2001 From: lyinch Date: Thu, 17 Feb 2022 12:54:08 +0100 Subject: [PATCH 2/9] Renamae test --- library/std/src/time/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/time/tests.rs b/library/std/src/time/tests.rs index 7227ece5914de..22e388d284fec 100644 --- a/library/std/src/time/tests.rs +++ b/library/std/src/time/tests.rs @@ -14,7 +14,7 @@ macro_rules! assert_almost_eq { #[test] #[cfg(all(target_arch = "aarch64", target_os = "macos"))] -fn wtf() { +fn macos_resolution_regression() { let t0 = Instant::now(); let t1 = t0 + Duration::from_nanos(50); let d = t1 - t0; From f3ef98f04017be12efe473ffd363f4eca2d49c1b Mon Sep 17 00:00:00 2001 From: lyinch Date: Thu, 17 Feb 2022 13:12:15 +0100 Subject: [PATCH 3/9] Use correct clock enum value --- library/std/src/sys/unix/time.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/library/std/src/sys/unix/time.rs b/library/std/src/sys/unix/time.rs index 13ad218ab391e..c14109c4f7328 100644 --- a/library/std/src/sys/unix/time.rs +++ b/library/std/src/sys/unix/time.rs @@ -149,11 +149,9 @@ mod inner { impl Instant { pub fn now() -> Instant { extern "C" { - //fn mach_absolute_time() -> u64; - fn clock_gettime_nsec_np(clock_type: i64) -> u64; + fn clock_gettime_nsec_np(clock_type: u32) -> u64; } - Instant { t: unsafe { clock_gettime_nsec_np(4) } } - //Instant { t: unsafe { mach_absolute_time() } } + Instant { t: unsafe { clock_gettime_nsec_np(8) } } } pub fn checked_sub_instant(&self, other: &Instant) -> Option { From 88f0c0d66de5d726bb2039519ed26a5102499d84 Mon Sep 17 00:00:00 2001 From: lyinch Date: Thu, 17 Feb 2022 13:21:15 +0100 Subject: [PATCH 4/9] Use correct clock enum value --- library/std/src/sys/unix/time.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/sys/unix/time.rs b/library/std/src/sys/unix/time.rs index c14109c4f7328..97345b6728c3f 100644 --- a/library/std/src/sys/unix/time.rs +++ b/library/std/src/sys/unix/time.rs @@ -151,7 +151,7 @@ mod inner { extern "C" { fn clock_gettime_nsec_np(clock_type: u32) -> u64; } - Instant { t: unsafe { clock_gettime_nsec_np(8) } } + Instant { t: unsafe { clock_gettime_nsec_np(4) } } } pub fn checked_sub_instant(&self, other: &Instant) -> Option { From d55e29981eafd5b46161f23091c3d999434706d6 Mon Sep 17 00:00:00 2001 From: lyinch Date: Thu, 17 Feb 2022 13:37:39 +0100 Subject: [PATCH 5/9] Add typedef for clock_id --- library/std/src/sys/unix/time.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/std/src/sys/unix/time.rs b/library/std/src/sys/unix/time.rs index 97345b6728c3f..13def89978a97 100644 --- a/library/std/src/sys/unix/time.rs +++ b/library/std/src/sys/unix/time.rs @@ -146,12 +146,14 @@ mod inner { type mach_timebase_info_t = *mut mach_timebase_info; type kern_return_t = libc::c_int; + pub type clockid_t = libc::clockid_t; + impl Instant { pub fn now() -> Instant { extern "C" { - fn clock_gettime_nsec_np(clock_type: u32) -> u64; + fn clock_gettime_nsec_np(clock_id: clockid_t) -> u64; } - Instant { t: unsafe { clock_gettime_nsec_np(4) } } + Instant { t: unsafe { clock_gettime_nsec_np(8) } } } pub fn checked_sub_instant(&self, other: &Instant) -> Option { From e565c33c0c063e23dce6ab57fdeaa76741be7e79 Mon Sep 17 00:00:00 2001 From: lyinch Date: Thu, 17 Feb 2022 15:36:08 +0100 Subject: [PATCH 6/9] Use unix implementation of Instant for macos aarch64 --- library/std/src/sys/unix/time.rs | 20 ++++++++++++-------- library/std/src/time/tests.rs | 1 - 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/library/std/src/sys/unix/time.rs b/library/std/src/sys/unix/time.rs index 13def89978a97..10dcab49f77ba 100644 --- a/library/std/src/sys/unix/time.rs +++ b/library/std/src/sys/unix/time.rs @@ -114,7 +114,7 @@ impl Hash for Timespec { } } -#[cfg(any(target_os = "macos", target_os = "ios"))] +#[cfg(any(all(target_os = "macos", not(target_arch = "aarch64")), target_os = "ios"))] mod inner { use crate::fmt; use crate::sync::atomic::{AtomicU64, Ordering}; @@ -146,14 +146,12 @@ mod inner { type mach_timebase_info_t = *mut mach_timebase_info; type kern_return_t = libc::c_int; - pub type clockid_t = libc::clockid_t; - impl Instant { pub fn now() -> Instant { extern "C" { - fn clock_gettime_nsec_np(clock_id: clockid_t) -> u64; + fn mach_absolute_time() -> u64; } - Instant { t: unsafe { clock_gettime_nsec_np(8) } } + Instant { t: unsafe { mach_absolute_time() } } } pub fn checked_sub_instant(&self, other: &Instant) -> Option { @@ -265,7 +263,7 @@ mod inner { } } -#[cfg(not(any(target_os = "macos", target_os = "ios")))] +#[cfg(not(any(all(target_os = "macos", not(target_arch = "aarch64")), target_os = "ios")))] mod inner { use crate::fmt; use crate::sys::cvt; @@ -287,7 +285,11 @@ mod inner { impl Instant { pub fn now() -> Instant { - Instant { t: now(libc::CLOCK_MONOTONIC) } + #[cfg(target_os = "macos")] + const clock_id: clock_t = 8; + #[cfg(not(target_os = "macos"))] + const clock_id: clock_t = libc::CLOCK_MONOTONIC; + Instant { t: now(clock_id) } } pub fn checked_sub_instant(&self, other: &Instant) -> Option { @@ -345,10 +347,12 @@ mod inner { } } - #[cfg(not(any(target_os = "dragonfly", target_os = "espidf")))] + #[cfg(not(any(target_os = "dragonfly", target_os = "espidf", target_os = "macos")))] pub type clock_t = libc::c_int; #[cfg(any(target_os = "dragonfly", target_os = "espidf"))] pub type clock_t = libc::c_ulong; + #[cfg(target_os = "macos")] + pub type clock_t = libc::clockid_t; fn now(clock: clock_t) -> Timespec { let mut t = Timespec { t: libc::timespec { tv_sec: 0, tv_nsec: 0 } }; diff --git a/library/std/src/time/tests.rs b/library/std/src/time/tests.rs index 22e388d284fec..3a1ecf7695ef0 100644 --- a/library/std/src/time/tests.rs +++ b/library/std/src/time/tests.rs @@ -18,7 +18,6 @@ fn macos_resolution_regression() { let t0 = Instant::now(); let t1 = t0 + Duration::from_nanos(50); let d = t1 - t0; - dbg!(t0, t1, d); assert_eq!(t0 + d, t1); } From abaaa9aff42c698765d58d8b602d441ea0b49e7d Mon Sep 17 00:00:00 2001 From: lyinch Date: Thu, 17 Feb 2022 22:12:44 +0100 Subject: [PATCH 7/9] Fix formatting issue --- library/std/src/sys/unix/time.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/sys/unix/time.rs b/library/std/src/sys/unix/time.rs index 10dcab49f77ba..50e15a47ae2a8 100644 --- a/library/std/src/sys/unix/time.rs +++ b/library/std/src/sys/unix/time.rs @@ -288,7 +288,7 @@ mod inner { #[cfg(target_os = "macos")] const clock_id: clock_t = 8; #[cfg(not(target_os = "macos"))] - const clock_id: clock_t = libc::CLOCK_MONOTONIC; + const clock_id: clock_t = libc::CLOCK_MONOTONIC; Instant { t: now(clock_id) } } From 4a296822f606dbd7b134910b9a94691805483e39 Mon Sep 17 00:00:00 2001 From: lyinch Date: Thu, 17 Feb 2022 22:43:58 +0100 Subject: [PATCH 8/9] Change test to run on all platformsms instead of only macos aarch64 --- library/std/src/time/tests.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/library/std/src/time/tests.rs b/library/std/src/time/tests.rs index 3a1ecf7695ef0..56a16465d7be8 100644 --- a/library/std/src/time/tests.rs +++ b/library/std/src/time/tests.rs @@ -13,7 +13,6 @@ macro_rules! assert_almost_eq { } #[test] -#[cfg(all(target_arch = "aarch64", target_os = "macos"))] fn macos_resolution_regression() { let t0 = Instant::now(); let t1 = t0 + Duration::from_nanos(50); From 283ccc9704e384ae47439d2ee76784a25e628c31 Mon Sep 17 00:00:00 2001 From: lyinch Date: Sat, 19 Feb 2022 16:42:15 +0100 Subject: [PATCH 9/9] Bump libc version and use CLOCK_UPTIME_RAW constant --- Cargo.lock | 4 ++-- library/std/Cargo.toml | 2 +- library/std/src/sys/unix/time.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fd65ed8d4a2d6..1082e8f2370ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1972,9 +1972,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" [[package]] name = "libc" -version = "0.2.116" +version = "0.2.119" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "565dbd88872dbe4cc8a46e527f26483c1d1f7afa6b884a3bd6cd893d4f98da74" +checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4" dependencies = [ "rustc-std-workspace-core", ] diff --git a/library/std/Cargo.toml b/library/std/Cargo.toml index 146096535dbbe..c9574ed54f771 100644 --- a/library/std/Cargo.toml +++ b/library/std/Cargo.toml @@ -15,7 +15,7 @@ cfg-if = { version = "0.1.8", features = ['rustc-dep-of-std'] } panic_unwind = { path = "../panic_unwind", optional = true } panic_abort = { path = "../panic_abort" } core = { path = "../core" } -libc = { version = "0.2.116", default-features = false, features = ['rustc-dep-of-std'] } +libc = { version = "0.2.119", default-features = false, features = ['rustc-dep-of-std'] } compiler_builtins = { version = "0.1.69" } profiler_builtins = { path = "../profiler_builtins", optional = true } unwind = { path = "../unwind" } diff --git a/library/std/src/sys/unix/time.rs b/library/std/src/sys/unix/time.rs index 50e15a47ae2a8..9a8634aa33327 100644 --- a/library/std/src/sys/unix/time.rs +++ b/library/std/src/sys/unix/time.rs @@ -286,7 +286,7 @@ mod inner { impl Instant { pub fn now() -> Instant { #[cfg(target_os = "macos")] - const clock_id: clock_t = 8; + const clock_id: clock_t = libc::CLOCK_UPTIME_RAW; #[cfg(not(target_os = "macos"))] const clock_id: clock_t = libc::CLOCK_MONOTONIC; Instant { t: now(clock_id) }