Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- DNBTimer.h ----------------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Created by Greg Clayton on 12/13/07. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef __DNBTimer_h__ |
| 14 | #define __DNBTimer_h__ |
| 15 | |
Greg Clayton | e01e07b | 2013-04-18 18:10:51 +0000 | [diff] [blame] | 16 | #include "DNBDefs.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "PThreadMutex.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 18 | #include <memory> |
| 19 | #include <stdint.h> |
| 20 | #include <sys/time.h> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | class DNBTimer { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | //------------------------------------------------------------------ |
| 25 | // Constructors and Destructors |
| 26 | //------------------------------------------------------------------ |
| 27 | DNBTimer(bool threadSafe) : m_mutexAP() { |
| 28 | if (threadSafe) |
| 29 | m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE)); |
| 30 | Reset(); |
| 31 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | DNBTimer(const DNBTimer &rhs) : m_mutexAP() { |
| 34 | // Create a new mutex to make this timer thread safe as well if |
| 35 | // the timer we are copying is thread safe |
| 36 | if (rhs.IsThreadSafe()) |
| 37 | m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE)); |
| 38 | m_timeval = rhs.m_timeval; |
| 39 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | DNBTimer &operator=(const DNBTimer &rhs) { |
| 42 | // Create a new mutex to make this timer thread safe as well if |
| 43 | // the timer we are copying is thread safe |
| 44 | if (rhs.IsThreadSafe()) |
| 45 | m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE)); |
| 46 | m_timeval = rhs.m_timeval; |
| 47 | return *this; |
| 48 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | ~DNBTimer() {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | bool IsThreadSafe() const { return m_mutexAP.get() != NULL; } |
| 53 | //------------------------------------------------------------------ |
| 54 | // Reset the time value to now |
| 55 | //------------------------------------------------------------------ |
| 56 | void Reset() { |
| 57 | PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get()); |
| 58 | gettimeofday(&m_timeval, NULL); |
| 59 | } |
| 60 | //------------------------------------------------------------------ |
| 61 | // Get the total mircoseconds since Jan 1, 1970 |
| 62 | //------------------------------------------------------------------ |
| 63 | uint64_t TotalMicroSeconds() const { |
| 64 | PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get()); |
| 65 | return (uint64_t)(m_timeval.tv_sec) * 1000000ull + |
| 66 | (uint64_t)m_timeval.tv_usec; |
| 67 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 68 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | void GetTime(uint64_t &sec, uint32_t &usec) const { |
| 70 | PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get()); |
| 71 | sec = m_timeval.tv_sec; |
| 72 | usec = m_timeval.tv_usec; |
| 73 | } |
| 74 | //------------------------------------------------------------------ |
| 75 | // Return the number of microseconds elapsed between now and the |
| 76 | // m_timeval |
| 77 | //------------------------------------------------------------------ |
| 78 | uint64_t ElapsedMicroSeconds(bool update) { |
| 79 | PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get()); |
| 80 | struct timeval now; |
| 81 | gettimeofday(&now, NULL); |
| 82 | uint64_t now_usec = |
| 83 | (uint64_t)(now.tv_sec) * 1000000ull + (uint64_t)now.tv_usec; |
| 84 | uint64_t this_usec = |
| 85 | (uint64_t)(m_timeval.tv_sec) * 1000000ull + (uint64_t)m_timeval.tv_usec; |
| 86 | uint64_t elapsed = now_usec - this_usec; |
| 87 | // Update the timer time value if requeseted |
| 88 | if (update) |
| 89 | m_timeval = now; |
| 90 | return elapsed; |
| 91 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | static uint64_t GetTimeOfDay() { |
| 94 | struct timeval now; |
| 95 | gettimeofday(&now, NULL); |
| 96 | uint64_t now_usec = |
| 97 | (uint64_t)(now.tv_sec) * 1000000ull + (uint64_t)now.tv_usec; |
| 98 | return now_usec; |
| 99 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | static void OffsetTimeOfDay(struct timespec *ts, |
| 102 | __darwin_time_t sec_offset = 0, |
| 103 | long nsec_offset = 0) { |
| 104 | if (ts == NULL) |
| 105 | return; |
| 106 | // Get the current time in a timeval structure |
| 107 | struct timeval now; |
| 108 | gettimeofday(&now, NULL); |
| 109 | // Morph it into a timespec |
| 110 | TIMEVAL_TO_TIMESPEC(&now, ts); |
| 111 | // Offset the timespec if requested |
| 112 | if (sec_offset != 0 || nsec_offset != 0) { |
| 113 | // Offset the nano seconds |
| 114 | ts->tv_nsec += nsec_offset; |
| 115 | // Offset the seconds taking into account a nano-second overflow |
| 116 | ts->tv_sec = ts->tv_sec + ts->tv_nsec / 1000000000 + sec_offset; |
| 117 | // Trim the nanoseconds back there was an overflow |
| 118 | ts->tv_nsec = ts->tv_nsec % 1000000000; |
| 119 | } |
| 120 | } |
| 121 | static bool TimeOfDayLaterThan(struct timespec &ts) { |
| 122 | struct timespec now; |
| 123 | OffsetTimeOfDay(&now); |
| 124 | if (now.tv_sec > ts.tv_sec) |
| 125 | return true; |
| 126 | else if (now.tv_sec < ts.tv_sec) |
| 127 | return false; |
| 128 | else { |
| 129 | if (now.tv_nsec > ts.tv_nsec) |
| 130 | return true; |
| 131 | else |
| 132 | return false; |
| 133 | } |
| 134 | } |
| 135 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 136 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 137 | //------------------------------------------------------------------ |
| 138 | // Classes that inherit from DNBTimer can see and modify these |
| 139 | //------------------------------------------------------------------ |
| 140 | std::unique_ptr<PThreadMutex> m_mutexAP; |
| 141 | struct timeval m_timeval; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | #endif // #ifndef __DNBTimer_h__ |