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 | // Constructors and Destructors |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | DNBTimer(bool threadSafe) : m_mutexAP() { |
| 26 | if (threadSafe) |
| 27 | m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE)); |
| 28 | Reset(); |
| 29 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 31 | DNBTimer(const DNBTimer &rhs) : m_mutexAP() { |
| 32 | // Create a new mutex to make this timer thread safe as well if |
| 33 | // the timer we are copying is thread safe |
| 34 | if (rhs.IsThreadSafe()) |
| 35 | m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE)); |
| 36 | m_timeval = rhs.m_timeval; |
| 37 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 39 | DNBTimer &operator=(const DNBTimer &rhs) { |
| 40 | // Create a new mutex to make this timer thread safe as well if |
| 41 | // the timer we are copying is thread safe |
| 42 | if (rhs.IsThreadSafe()) |
| 43 | m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE)); |
| 44 | m_timeval = rhs.m_timeval; |
| 45 | return *this; |
| 46 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 48 | ~DNBTimer() {} |
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 | bool IsThreadSafe() const { return m_mutexAP.get() != NULL; } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | // Reset the time value to now |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | void Reset() { |
| 53 | PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get()); |
| 54 | gettimeofday(&m_timeval, NULL); |
| 55 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | // Get the total mircoseconds since Jan 1, 1970 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 57 | uint64_t TotalMicroSeconds() const { |
| 58 | PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get()); |
| 59 | return (uint64_t)(m_timeval.tv_sec) * 1000000ull + |
| 60 | (uint64_t)m_timeval.tv_usec; |
| 61 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 62 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | void GetTime(uint64_t &sec, uint32_t &usec) const { |
| 64 | PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get()); |
| 65 | sec = m_timeval.tv_sec; |
| 66 | usec = m_timeval.tv_usec; |
| 67 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | // Return the number of microseconds elapsed between now and the |
| 69 | // m_timeval |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | uint64_t ElapsedMicroSeconds(bool update) { |
| 71 | PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get()); |
| 72 | struct timeval now; |
| 73 | gettimeofday(&now, NULL); |
| 74 | uint64_t now_usec = |
| 75 | (uint64_t)(now.tv_sec) * 1000000ull + (uint64_t)now.tv_usec; |
| 76 | uint64_t this_usec = |
| 77 | (uint64_t)(m_timeval.tv_sec) * 1000000ull + (uint64_t)m_timeval.tv_usec; |
| 78 | uint64_t elapsed = now_usec - this_usec; |
| 79 | // Update the timer time value if requeseted |
| 80 | if (update) |
| 81 | m_timeval = now; |
| 82 | return elapsed; |
| 83 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | static uint64_t GetTimeOfDay() { |
| 86 | struct timeval now; |
| 87 | gettimeofday(&now, NULL); |
| 88 | uint64_t now_usec = |
| 89 | (uint64_t)(now.tv_sec) * 1000000ull + (uint64_t)now.tv_usec; |
| 90 | return now_usec; |
| 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 void OffsetTimeOfDay(struct timespec *ts, |
| 94 | __darwin_time_t sec_offset = 0, |
| 95 | long nsec_offset = 0) { |
| 96 | if (ts == NULL) |
| 97 | return; |
| 98 | // Get the current time in a timeval structure |
| 99 | struct timeval now; |
| 100 | gettimeofday(&now, NULL); |
| 101 | // Morph it into a timespec |
| 102 | TIMEVAL_TO_TIMESPEC(&now, ts); |
| 103 | // Offset the timespec if requested |
| 104 | if (sec_offset != 0 || nsec_offset != 0) { |
| 105 | // Offset the nano seconds |
| 106 | ts->tv_nsec += nsec_offset; |
| 107 | // Offset the seconds taking into account a nano-second overflow |
| 108 | ts->tv_sec = ts->tv_sec + ts->tv_nsec / 1000000000 + sec_offset; |
| 109 | // Trim the nanoseconds back there was an overflow |
| 110 | ts->tv_nsec = ts->tv_nsec % 1000000000; |
| 111 | } |
| 112 | } |
| 113 | static bool TimeOfDayLaterThan(struct timespec &ts) { |
| 114 | struct timespec now; |
| 115 | OffsetTimeOfDay(&now); |
| 116 | if (now.tv_sec > ts.tv_sec) |
| 117 | return true; |
| 118 | else if (now.tv_sec < ts.tv_sec) |
| 119 | return false; |
| 120 | else { |
| 121 | if (now.tv_nsec > ts.tv_nsec) |
| 122 | return true; |
| 123 | else |
| 124 | return false; |
| 125 | } |
| 126 | } |
| 127 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 128 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | // Classes that inherit from DNBTimer can see and modify these |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 130 | std::unique_ptr<PThreadMutex> m_mutexAP; |
| 131 | struct timeval m_timeval; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | #endif // #ifndef __DNBTimer_h__ |