Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2021 The Bitcoin Core developers 3 : // Distributed under the MIT software license, see the accompanying 4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 : 6 : #ifndef BITCOIN_UTIL_TIME_H 7 : #define BITCOIN_UTIL_TIME_H 8 : 9 : #include <compat/compat.h> 10 : 11 : #include <atomic> 12 : #include <chrono> // IWYU pragma: export 13 : #include <cstdint> 14 : #include <string> 15 : 16 : using namespace std::chrono_literals; 17 : 18 : /** Mockable clock in the context of tests, otherwise the system clock */ 19 : struct NodeClock : public std::chrono::system_clock { 20 : using time_point = std::chrono::time_point<NodeClock>; 21 : /** Return current system time or mocked time, if set */ 22 : static time_point now() noexcept; 23 : static std::time_t to_time_t(const time_point&) = delete; // unused 24 : static time_point from_time_t(std::time_t) = delete; // unused 25 : }; 26 : using NodeSeconds = std::chrono::time_point<NodeClock, std::chrono::seconds>; 27 : 28 : using SteadyClock = std::chrono::steady_clock; 29 : using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>; 30 : using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>; 31 : using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>; 32 : 33 : using SystemClock = std::chrono::system_clock; 34 : 35 : /** 36 : * Thread-safe cleanup interval throttler. 37 : * Ensures cleanup functions don't run more frequently than specified interval. 38 : */ 39 : template<typename ClockType = NodeClock> 40 184 : class CleanupThrottler { 41 : private: 42 184 : std::atomic<typename ClockType::time_point> nextCleanup{typename ClockType::time_point{}}; 43 : 44 : public: 45 : /** 46 : * Attempt to claim a cleanup slot. 47 : * @param interval Minimum time between cleanups 48 : * @return true if cleanup should proceed, false if throttled 49 : */ 50 0 : bool TryCleanup(typename ClockType::duration interval) { 51 0 : auto now = ClockType::now(); 52 0 : auto expected = nextCleanup.load(std::memory_order_relaxed); 53 : 54 : // Cleanup not yet due 55 0 : if (now < expected) { 56 0 : return false; 57 : } 58 : 59 : // Atomically claim the cleanup slot 60 0 : return nextCleanup.compare_exchange_strong( 61 : expected, 62 0 : now + interval, 63 : std::memory_order_relaxed 64 : ); 65 0 : } 66 : }; 67 : 68 : void UninterruptibleSleep(const std::chrono::microseconds& n); 69 : 70 : /** 71 : * Helper to count the seconds of a duration/time_point. 72 : * 73 : * All durations/time_points should be using std::chrono and calling this should generally 74 : * be avoided in code. Though, it is still preferred to an inline t.count() to 75 : * protect against a reliance on the exact type of t. 76 : * 77 : * This helper is used to convert durations/time_points before passing them over an 78 : * interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI) 79 : */ 80 : template <typename Dur1, typename Dur2> 81 32729 : constexpr auto Ticks(Dur2 d) 82 : { 83 32729 : return std::chrono::duration_cast<Dur1>(d).count(); 84 : } 85 : template <typename Duration, typename Timepoint> 86 32307 : constexpr auto TicksSinceEpoch(Timepoint t) 87 : { 88 32307 : return Ticks<Duration>(t.time_since_epoch()); 89 : } 90 2018947 : constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); } 91 95563 : constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); } 92 98677 : constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); } 93 : 94 : using HoursDouble = std::chrono::duration<double, std::chrono::hours::period>; 95 : using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>; 96 : using MillisecondsDouble = std::chrono::duration<double, std::chrono::milliseconds::period>; 97 : 98 : /** 99 : * DEPRECATED 100 : * Use either ClockType::now() or Now<TimePointType>() if a cast is needed. 101 : * ClockType is 102 : * - std::chrono::steady_clock for steady time 103 : * - std::chrono::system_clock for system time 104 : * - NodeClock for mockable system time 105 : */ 106 : int64_t GetTime(); 107 : 108 : /** Returns the system time (not mockable) */ 109 : int64_t GetTimeMicros(); 110 : /** 111 : * DEPRECATED 112 : * Use SetMockTime with chrono type 113 : * 114 : * @param[in] nMockTimeIn Time in seconds. 115 : */ 116 : void SetMockTime(int64_t nMockTimeIn); 117 : 118 : /** For testing. Set e.g. with the setmocktime rpc, or -mocktime argument */ 119 : void SetMockTime(std::chrono::seconds mock_time_in); 120 : 121 : /** For testing */ 122 : std::chrono::seconds GetMockTime(); 123 : 124 : /** 125 : * Return the current time point cast to the given precision. Only use this 126 : * when an exact precision is needed, otherwise use T::clock::now() directly. 127 : */ 128 : template <typename T> 129 943849 : T Now() 130 : { 131 943849 : return std::chrono::time_point_cast<typename T::duration>(T::clock::now()); 132 : } 133 : /** DEPRECATED, see GetTime */ 134 : template <typename T> 135 405913 : T GetTime() 136 : { 137 405913 : return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch(); 138 : } 139 : 140 : /** 141 : * ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date,Time} 142 : * helper functions if possible. 143 : */ 144 : std::string FormatISO8601DateTime(int64_t nTime); 145 : std::string FormatISO8601Date(int64_t nTime); 146 : std::string FormatISO8601Time(int64_t nTime); 147 : 148 : /** 149 : * Convert milliseconds to a struct timeval for e.g. select. 150 : */ 151 : struct timeval MillisToTimeval(int64_t nTimeout); 152 : struct timespec MillisToTimespec(int64_t nTimeout); 153 : 154 : /** 155 : * Convert milliseconds to a struct timeval for e.g. select. 156 : */ 157 : struct timeval MillisToTimeval(std::chrono::milliseconds ms); 158 : struct timespec MillisToTimespec(std::chrono::milliseconds ms); 159 : 160 : #endif // BITCOIN_UTIL_TIME_H