blob: 9792c9b79ab3e7c9955ddc93f6a7f3632da0d6ba [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- DNBTimer.h ----------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Created by Greg Clayton on 12/13/07.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef __DNBTimer_h__
14#define __DNBTimer_h__
15
Greg Claytone01e07b2013-04-18 18:10:51 +000016#include "DNBDefs.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "PThreadMutex.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include <memory>
19#include <stdint.h>
20#include <sys/time.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021
Kate Stoneb9c1b512016-09-06 20:57:50 +000022class DNBTimer {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000024 //------------------------------------------------------------------
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 Lattner30fdc8d2010-06-08 16:52:24 +000032
Kate Stoneb9c1b512016-09-06 20:57:50 +000033 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 Lattner30fdc8d2010-06-08 16:52:24 +000040
Kate Stoneb9c1b512016-09-06 20:57:50 +000041 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 Lattner30fdc8d2010-06-08 16:52:24 +000049
Kate Stoneb9c1b512016-09-06 20:57:50 +000050 ~DNBTimer() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 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 Lattner30fdc8d2010-06-08 16:52:24 +000068
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 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 Lattner30fdc8d2010-06-08 16:52:24 +000092
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 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 Lattner30fdc8d2010-06-08 16:52:24 +0000100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 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 Lattner30fdc8d2010-06-08 16:52:24 +0000136protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 //------------------------------------------------------------------
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 Lattner30fdc8d2010-06-08 16:52:24 +0000142};
143
144#endif // #ifndef __DNBTimer_h__