forked from mrpi/redis-cplusplus-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TimeTrace.h
184 lines (159 loc) · 6.99 KB
/
TimeTrace.h
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* Copyright (c) 2014-2016 Stanford University
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef PERFUTIL_TIMETRACE_H
#define PERFUTIL_TIMETRACE_H
#include <string>
#include <vector>
#include <mutex>
#include <xmmintrin.h>
#include "Atomic.h"
#include "Cycles.h"
namespace PerfUtils {
// A macro to disallow the copy constructor and operator= functions
#ifndef DISALLOW_COPY_AND_ASSIGN
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&) = delete; \
TypeName& operator=(const TypeName&) = delete;
#endif
/**
* This class implements a circular buffer of entries, each of which
* consists of a fine-grain timestamp and a short descriptive string.
* It's typically used to record times at various points in an operation,
* in order to find performance bottlenecks. It can record a trace relatively
* efficiently, and then either return the trace either as a string or
* print it to the system log.
*
* This class is thread-safe.
*/
class TimeTrace {
public:
class Buffer;
static std::string getTrace();
static void setOutputFileName(const char *filename) {
TimeTrace::filename = filename;
}
static void print();
/**
* Record an event in a thread-local buffer, creating a new buffer
* if this is the first record for this thread.
*
* \param timestamp
* Identifies the time at which the event occurred.
* \param format
* A format string for snprintf that will be used, along with
* arg0..arg3, to generate a human-readable message describing what
* happened, when the time trace is printed. The message is generated
* by calling snprintf as follows:
* snprintf(buffer, size, format, arg0, arg1, arg2, arg3)
* where format and arg0..arg3 are the corresponding arguments to this
* method. This pointer is stored in the time trace, so the caller must
* ensure that its contents will not change over its lifetime in the
* trace.
* \param arg0
* Argument to use when printing a message about this event.
* \param arg1
* Argument to use when printing a message about this event.
* \param arg2
* Argument to use when printing a message about this event.
* \param arg3
* Argument to use when printing a message about this event.
*/
static inline void record(uint64_t timestamp, const char* format,
uint32_t arg0 = 0, uint32_t arg1 = 0, uint32_t arg2 = 0,
uint32_t arg3 = 0) {
// if (threadBuffer == NULL) {
// createThreadBuffer();
// }
// threadBuffer->record(timestamp, format, arg0, arg1, arg2, arg3);
}
static inline void record(const char* format, uint32_t arg0 = 0,
uint32_t arg1 = 0, uint32_t arg2 = 0, uint32_t arg3 = 0) {
// record(RAMCloud::Cycles::rdtsc(), format, arg0, arg1, arg2, arg3);
}
static void reset();
protected:
TimeTrace();
static void createThreadBuffer();
static void printInternal(std::vector<TimeTrace::Buffer*>* traces,
std::string* s);
// Points to a private per-thread TimeTrace::Buffer object; NULL means
// no such object has been created yet for the current thread.
static __thread Buffer* threadBuffer;
// Holds pointers to all of the thread-private TimeTrace objects created
// so far. Entries never get deleted from this object.
static std::vector<Buffer*> threadBuffers;
// Provides mutual exclusion on threadBuffers.
static std::mutex mutex;
// The name of the file to write records into. If it is null, then we will
// write to stdout
static const char* filename;
/**
* This structure holds one entry in the TimeTrace.
*/
struct Event {
uint64_t timestamp; // Time when a particular event occurred.
const char* format; // Format string describing the event.
// NULL means that this entry is unused.
uint32_t arg0; // Argument that may be referenced by format
// when printing out this event.
uint32_t arg1; // Argument that may be referenced by format
// when printing out this event.
uint32_t arg2; // Argument that may be referenced by format
// when printing out this event.
uint32_t arg3; // Argument that may be referenced by format
// when printing out this event.
};
public:
/**
* Represents a sequence of events, typically consisting of all those
* generated by one thread. Has a fixed capacity, so slots are re-used
* on a circular basis. This class is not thread-safe.
*/
class Buffer {
public:
Buffer();
~Buffer();
std::string getTrace();
void print();
void printToLog();
void record(uint64_t timestamp, const char* format, uint32_t arg0 = 0,
uint32_t arg1 = 0, uint32_t arg2 = 0, uint32_t arg3 = 0);
void record(const char* format, uint32_t arg0 = 0, uint32_t arg1 = 0,
uint32_t arg2 = 0, uint32_t arg3 = 0) {
record(RAMCloud::Cycles::rdtsc(), format, arg0, arg1, arg2, arg3);
}
void reset();
protected:
// Determines the number of events we can retain as an exponent of 2
static const uint8_t BUFFER_SIZE_EXP = 13;
// Total number of events that we can retain any given time.
static const uint32_t BUFFER_SIZE = 1 << BUFFER_SIZE_EXP;
// Bit mask used to implement a circular event buffer
static const uint32_t BUFFER_MASK = BUFFER_SIZE - 1;
// Index within events of the slot to use for the next call to the
// record method.
int nextIndex;
// Count of number of calls to printInternal that are currently active
// for this buffer; if nonzero, then it isn't safe to log new
// entries, since this could interfere with readers.
Atomic<int> activeReaders;
// Holds information from the most recent calls to the record method.
TimeTrace::Event events[BUFFER_SIZE];
friend class TimeTrace;
DISALLOW_COPY_AND_ASSIGN(Buffer)
};
};
} // namespace PerfUtils
#endif // PERFUTIL_TIMETRACE_H