use Boottime clock on linux

Better to advance while suspended, that way a stale content retention
lock will expire while suspended.

The fallback the Monotonic to support older kernels assumes that there
are not systems where Boottime sometimes succeeds and sometimes fails.
If that ever happened, the clock would probably not monotonically
advance as it read from different clocks on different calls! I don't see
any indication in clock_gettime(2) errono list that it can fail
intermittently so probably this is ok.
This commit is contained in:
Joey Hess 2024-07-03 18:44:38 -04:00
parent 5b6150e5d5
commit e9bd03fc4b
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38

View file

@ -14,11 +14,23 @@ import qualified System.Clock as Clock
#else #else
import qualified System.Posix.Clock as Clock import qualified System.Posix.Clock as Clock
#endif #endif
#ifdef linux_HOST_OS
import Utility.Exception
#endif
newtype MonotonicTimestamp = MonotonicTimestamp Integer newtype MonotonicTimestamp = MonotonicTimestamp Integer
deriving (Show, Eq, Ord) deriving (Show, Eq, Ord)
-- On linux, this uses a clock that advances while the system is suspended,
-- except for on very old kernels (eg 2.6.32).
-- On other systems, that is not available, and the monotonic clock will
-- not advance while suspended.
currentMonotonicTimestamp :: IO MonotonicTimestamp currentMonotonicTimestamp :: IO MonotonicTimestamp
currentMonotonicTimestamp = currentMonotonicTimestamp =
(MonotonicTimestamp . fromIntegral . Clock.sec) (MonotonicTimestamp . fromIntegral . Clock.sec) <$>
<$> Clock.getTime Clock.Monotonic #ifdef linux_HOST_OS
(tryNonAsync (Clock.getTime Clock.Boottime)
>>= either (const $ Clock.getTime Clock.Monotonic) return)
#else
Clock.getTime Clock.Monotonic
#endif