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