blob: 21ee2351d690621a2b781faa9c8e1b913aa18b99 [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 // Constructors and Destructors
Kate Stoneb9c1b512016-09-06 20:57:50 +000025 DNBTimer(bool threadSafe) : m_mutexAP() {
26 if (threadSafe)
27 m_mutexAP.reset(new PThreadMutex(PTHREAD_MUTEX_RECURSIVE));
28 Reset();
29 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
Kate Stoneb9c1b512016-09-06 20:57:50 +000031 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 Lattner30fdc8d2010-06-08 16:52:24 +000038
Kate Stoneb9c1b512016-09-06 20:57:50 +000039 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 Lattner30fdc8d2010-06-08 16:52:24 +000047
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 ~DNBTimer() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049
Kate Stoneb9c1b512016-09-06 20:57:50 +000050 bool IsThreadSafe() const { return m_mutexAP.get() != NULL; }
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 // Reset the time value to now
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 void Reset() {
53 PTHREAD_MUTEX_LOCKER(locker, m_mutexAP.get());
54 gettimeofday(&m_timeval, NULL);
55 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 // Get the total mircoseconds since Jan 1, 1970
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 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 Lattner30fdc8d2010-06-08 16:52:24 +000062
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 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 Stoneb9c1b512016-09-06 20:57:50 +000068 // Return the number of microseconds elapsed between now and the
69 // m_timeval
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 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 Lattner30fdc8d2010-06-08 16:52:24 +000084
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 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 Lattner30fdc8d2010-06-08 16:52:24 +000092
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 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 Lattner30fdc8d2010-06-08 16:52:24 +0000128protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 // Classes that inherit from DNBTimer can see and modify these
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 std::unique_ptr<PThreadMutex> m_mutexAP;
131 struct timeval m_timeval;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132};
133
134#endif // #ifndef __DNBTimer_h__