-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathctrace.h
347 lines (317 loc) · 8.28 KB
/
ctrace.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#ifndef CTRACE_H
#define CTRACE_H
#include <time.h>
#include <stdint.h>
#include <inttypes.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <stdio.h>
#ifndef CTRACE_FILE_NAME
#define CTRACE_FILE_NAME "trace.json"
#endif // CTRACE_FILE_NAME
#ifdef CTRACE_THREAD_SUPPORTED
#include <pthread.h>
#define CURRENT_TIME_LOCK_VAR CTrace::Lock __my_lock__ (GetCurrentTimeLock ())
#define SUBMIT_LOCK_VAR CTrace::Lock __my_submit_lock__ (GetSubmitLock ())
#else
#define CURRENT_TIME_LOCK_VAR
#define SUBMIT_LOCK_VAR
#endif // CTRACE_THREAD_SUPPORTED
#ifndef CTRACE_OMIT_JITTER
#define CTRACE_OMIT_JITTER 0UL
#endif // CTRACE_OMIT_JITTER
class CTrace
{
public:
CTrace (const char *cat, const char *name);
~CTrace ();
void CommonInit ();
const char *cat_;
const char *name_;
int pid_;
int tid_;
uint64_t clock_;
uint64_t clock_real_;
#ifdef CTRACE_THREAD_SUPPORTED
uint64_t clock_thread_;
uint64_t clock_thread_real_;
#endif
static const int64_t kMillisecondsPerSecond = 1000;
static const int64_t kMicrosecondsPerMillisecond = 1000;
static const int64_t kMicrosecondsPerSecond = kMicrosecondsPerMillisecond
* kMillisecondsPerSecond;
static const int64_t kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60;
static const int64_t kMicrosecondsPerHour = kMicrosecondsPerMinute * 60;
static const int64_t kMicrosecondsPerDay = kMicrosecondsPerHour * 24;
static const int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7;
static const int64_t kNanosecondsPerMicrosecond = 1000;
static const int64_t kNanosecondsPerSecond = kNanosecondsPerMicrosecond
* kMicrosecondsPerSecond;
private:
static void Submit (const CTrace *);
static uint64_t &GetCurrentTime ();
#ifdef CTRACE_THREAD_SUPPORTED
static uint64_t GetCurrentThreadTime ();
static void SetCurrentThreadTime (uint64_t);
static pthread_key_t GetThreadTimeKey ();
struct Lock
{
Lock (pthread_mutex_t *mutex) : mutex_ (mutex)
{
pthread_mutex_lock (mutex_);
}
~Lock () { pthread_mutex_unlock (mutex_); }
pthread_mutex_t *mutex_;
};
static pthread_mutex_t *GetCurrentTimeLock ();
static pthread_mutex_t *GetSubmitLock ();
#endif // CTRACE_THREAD_SUPPORTED
};
#define C_TRACE_0(cat, name) CTrace __trace__ (cat, name)
#ifdef CTRACE_THREAD_SUPPORTED
inline uint64_t
CTrace::GetCurrentThreadTime ()
{
pthread_key_t key = GetThreadTimeKey ();
#ifdef __LP64__
return reinterpret_cast<uint64_t> (pthread_getspecific (key));
#else
uint64_t *pdata = static_cast<uint64_t *> (pthread_getspecific (key));
if (pdata)
{
return *pdata;
}
else
{
return 0;
}
#endif
}
inline void
CTrace::SetCurrentThreadTime (uint64_t time)
{
pthread_key_t key = GetThreadTimeKey ();
#ifdef __LP64__
pthread_setspecific (key, reinterpret_cast<const void *> (time));
#else
uint64_t *pdata = static_cast<uint64_t *> (pthread_getspecific (key));
if (pdata)
{
*pdata = time;
}
else
{
pdata = static_cast<uint64_t *> (malloc (sizeof (uint64_t)));
if (pdata)
{
*pdata = time;
pthread_setspecific (key, pdata);
}
}
#endif
}
inline pthread_key_t
CTrace::GetThreadTimeKey ()
{
static pthread_key_t key;
static bool inited = false;
if (!inited)
{
#ifdef __LP64__
pthread_key_create (&key, NULL);
#else
pthread_key_create (&key, free);
#endif
inited = true;
}
return key;
}
inline pthread_mutex_t *
CTrace::GetCurrentTimeLock ()
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
return &mutex;
}
inline pthread_mutex_t *
CTrace::GetSubmitLock ()
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
return &mutex;
}
#endif // CTRACE_THREAD_SUPPORTED
inline CTrace::CTrace (const char *cat, const char *name)
{
cat_ = cat;
name_ = name;
CommonInit ();
}
inline CTrace::~CTrace () { Submit (this); }
inline void
CTrace::CommonInit ()
{
pid_ = getpid ();
tid_ = syscall (__NR_gettid, 0);
struct timespec ts;
if (clock_gettime (CLOCK_MONOTONIC, &ts) != 0)
{
clock_ = 0;
}
else
{
clock_ = (static_cast<uint64_t> (ts.tv_sec)
* CTrace::kMicrosecondsPerSecond)
+ (static_cast<uint64_t> (ts.tv_nsec)
/ CTrace::kNanosecondsPerMicrosecond);
clock_real_ = clock_;
}
#ifdef CTRACE_THREAD_SUPPORTED
struct timespec ts_thread;
if (clock_gettime (CLOCK_THREAD_CPUTIME_ID, &ts_thread) != 0)
{
clock_thread_ = 0;
}
else
{
clock_thread_ = (static_cast<uint64_t> (ts_thread.tv_sec)
* CTrace::kMicrosecondsPerSecond)
+ (static_cast<uint64_t> (ts_thread.tv_nsec)
/ CTrace::kNanosecondsPerMicrosecond);
clock_thread_real_ = clock_thread_;
}
{
uint64_t current_thread = GetCurrentThreadTime ();
if (this->clock_thread_ <= current_thread)
this->clock_thread_ = current_thread + 1;
SetCurrentThreadTime (this->clock_thread_);
}
#endif // CTRACE_THREAD_SUPPORTED
{
CURRENT_TIME_LOCK_VAR;
uint64_t ¤t = GetCurrentTime ();
if (this->clock_ <= current)
this->clock_ = current + 1;
current = this->clock_;
}
}
inline uint64_t &
CTrace::GetCurrentTime ()
{
static uint64_t current = 0;
return current;
}
inline void
CTrace::Submit (const CTrace *This)
{
uint64_t dur, now;
timespec ts;
if (clock_gettime (CLOCK_MONOTONIC, &ts) != 0)
{
now = 0;
}
else
{
now = (static_cast<uint64_t> (ts.tv_sec)
* CTrace::kMicrosecondsPerSecond)
+ (static_cast<uint64_t> (ts.tv_nsec)
/ CTrace::kNanosecondsPerMicrosecond);
}
if (now <= This->clock_real_)
dur = 1;
else
dur = now - This->clock_real_;
if (dur < CTRACE_OMIT_JITTER)
return;
{
CURRENT_TIME_LOCK_VAR;
uint64_t ¤t = GetCurrentTime ();
if (dur + This->clock_ < current)
{
dur = current - This->clock_;
}
current = This->clock_ + dur;
}
#ifdef CTRACE_THREAD_SUPPORTED
timespec ts_thread;
uint64_t now_thread, dur_thread;
if (clock_gettime (CLOCK_THREAD_CPUTIME_ID, &ts_thread) != 0)
{
now_thread = 0;
}
else
{
now_thread = (static_cast<uint64_t> (ts_thread.tv_sec)
* CTrace::kMicrosecondsPerSecond)
+ (static_cast<uint64_t> (ts_thread.tv_nsec)
/ CTrace::kNanosecondsPerMicrosecond);
}
if (now_thread <= This->clock_thread_real_)
dur_thread = 1;
else
dur_thread = now_thread - This->clock_thread_real_;
{
uint64_t current = GetCurrentThreadTime ();
if (dur_thread + This->clock_thread_ < current)
{
dur_thread = current - This->clock_thread_;
}
SetCurrentThreadTime (This->clock_thread_ + dur_thread);
}
#endif // CTRACE_THREAD_SUPPORTED
{
SUBMIT_LOCK_VAR;
static FILE *f;
static bool isInit = false;
struct FileSink
{
FileSink () : f_ (NULL) {}
~FileSink ()
{
if (f_)
{
fprintf (f_, "]}");
fclose (f_);
}
}
FILE *f_;
};
static FileSink fsink;
static bool needComma = false;
if (!isInit)
{
f = fopen (CTRACE_FILE_NAME, "w");
if (!f)
return;
fprintf (f, "{\"traceEvents\": [");
fsink.f_ = f;
isInit = true;
}
if (!needComma)
{
needComma = true;
}
else
{
fprintf (f, ", ");
}
#ifdef CTRACE_THREAD_SUPPORTED
fprintf (f, "{\"cat\":\"%s\", \"pid\":%d, \"tid\":%d, \"ts\":%" PRIu64
", \"ph\":\"X\", \"name\":\"%s\", \"dur\":%" PRIu64
", \"tts\":%" PRIu64 ", \"tdur\":%" PRIu64 "}",
This->cat_, This->pid_, This->tid_, This->clock_, This->name_,
dur, This->clock_thread_, dur_thread);
#else
fprintf (f, "{\"cat\":\"%s\", \"pid\":%d, \"tid\":%d, \"ts\":%" PRIu64 ", "
"\"ph\":\"X\", \"name\":\"%s\", \"dur\": %" PRIu64 "}",
This->cat_, This->pid_, This->tid_, This->clock_, This->name_,
dur);
#endif // CTRACE_THREAD_SUPPORTED
static int flushCount = 0;
if (flushCount++ == 5)
{
fflush (f);
flushCount = 0;
}
}
}
#endif /* CTRACE_H */