-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtime_trace.h
210 lines (182 loc) · 6.88 KB
/
time_trace.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* Copyright (c) 2020-2021 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 TIMETRACE_H
#define TIMETRACE_H
#include <string>
#include <vector>
// Change 1 -> 0 in the following line to disable time tracing globally.
#define ENABLE_TIME_TRACE 0
/**
* This class implements a circular buffer of entries, each of which
* consists of a fine-grain timestamp, a short descriptive string, and
* a few additional values. 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 (< 10ns as of 7/2020),
* and then either return the trace either as a string or print it to
* a file.
*
* This class is thread-safe. By default, trace information is recorded
* separately for each thread in order to avoid synchronization and cache
* consistency overheads; the thread-local traces are merged when the
* timetrace is printed, so the existence of multiple trace buffers is
* normally invisible.
*
* The TimeTrace class should never be constructed; it offers only
* static methods.
*
* If you want to use a single trace buffer rather than per-thread
* buffers, see the subclass TimeTrace::Buffer below.
*/
class TimeTrace {
public:
static void freeze();
static double getCyclesPerSec();
static std::string getTrace();
static int printToFile(const char *name);
static void record2(const char* format, uint64_t arg0 = 0,
uint64_t arg1 = 0, uint64_t arg2 = 0, uint64_t arg3 = 0);
static double toSeconds(uint64_t cycles);
// Nonzero means that the timetrace is already frozen.
static int frozen;
protected:
/**
* Holds one entry in a TimeTrace::Buffer.
*/
struct Event {
/* See documentation for record method. */
uint64_t timestamp;
const char* format;
uint64_t arg0;
uint64_t arg1;
uint64_t arg2;
uint64_t arg3;
};
/**
* Represents a sequence of events generated by a single 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();
void record(uint64_t timestamp, const char* format,
uint64_t arg0 = 0, uint64_t arg1 = 0,
uint64_t arg2 = 0, uint64_t arg3 = 0);
void reset();
public:
// Name that identifies this buffer/thread.
std::string name;
// Determines the number of events we can retain, as an exponent of 2.
static const uint8_t BUFFER_SIZE_EXP = 16;
// Total number of events that we can retain at 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 @record.
int nextIndex;
// Number of thread_buffer objects that reference this buffer. When
// this count becomes 0, the buffer can be deleted in the next call
// to time_trace::cleanup.
int refCount;
// Holds information from the most recent calls to record.
TimeTrace::Event events[BUFFER_SIZE];
friend class TimeTrace;
};
/**
* Stores a pointer to a Buffer; used to automatically allocate
* buffers on thread creation and recycle them on thread exit.
*/
class BufferPtr {
public:
BufferPtr();
~BufferPtr();
Buffer* operator->()
{
return buffer;
}
Buffer *buffer;
};
// Points to a private per-thread TimeTrace::Buffer.
static thread_local BufferPtr tb;
// Holds pointers to all of the existing thread-private buffers.
// Entries get deleted only by free_unused.
static std::vector<Buffer*> threadBuffers;
// Buffers that have been previously used by a thread, but the
// thread has exited so the buffer is available for re-use.
static std::vector<Buffer*> freeBuffers;
public:
/**
* Record an event in a thread-local buffer.
* \param timestamp
* The time at which the event occurred (as returned by rdtsc()).
* \param format
* A format string for snprintf that will be used when the time trace
* is printed, along with arg0..arg3, to generate a human-readable
* message describing what happened. 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,
uint64_t arg0 = 0, uint64_t arg1 = 0,
uint64_t arg2 = 0, uint64_t arg3 = 0) {
#if ENABLE_TIME_TRACE
tb->record(timestamp, format, arg0, arg1, arg2, arg3);
#endif
}
static inline void record(const char* format, uint64_t arg0 = 0,
uint64_t arg1 = 0, uint64_t arg2 = 0, uint64_t arg3 = 0) {
#if ENABLE_TIME_TRACE
record(rdtsc(), format, arg0, arg1, arg2, arg3);
#endif
}
/**
* Return the current value of the fine-grain CPU cycle counter
* (accessed via the RDTSC instruction).
*/
inline static uint64_t rdtsc(void)
{
uint32_t lo, hi;
__asm__ __volatile__("rdtsc" : "=a" (lo), "=d" (hi));
return (((uint64_t)hi << 32) | lo);
}
protected:
TimeTrace();
static void printInternal(std::string* s, FILE *f);
};
extern void (*recordFunc)(const char* format, uint64_t arg0,
uint64_t arg1, uint64_t arg2, uint64_t arg3);
#define tt TimeTrace::record
/**
* Records time trace record indirectly through recordFunc; used to
* add time tracing to the gRPC core.
*/
inline void tt2(const char* format, uint64_t arg0 = 0,
uint64_t arg1 = 0, uint64_t arg2 = 0, uint64_t arg3 = 0)
{
(*recordFunc)(format, arg0, arg1, arg2, arg3);
}
#endif // TIMETRACE_H