13 #pragma warning (disable:4996)
14 #define WIN32_LEAN_AND_MEAN
16 #define __thread __declspec(thread)
17 #define pthread_mutex_t CRITICAL_SECTION
18 #define pthread_mutex_init(a, b) InitializeCriticalSection(a)
19 #define pthread_mutex_lock(a) EnterCriticalSection(a)
20 #define pthread_mutex_unlock(a) LeaveCriticalSection(a)
21 #define pthread_mutex_destroy(a) DeleteCriticalSection(a)
31 #define ARRAY_SIZE(x) sizeof(x)/sizeof(x[0])
63 #define STRING_POOL_SIZE 100
73 return (
int)GetCurrentThreadId();
76 static uint64_t _frequency = 0;
77 static uint64_t _starttime = 0;
80 static int64_t prev = 0;
81 if (_frequency == 0) {
82 QueryPerformanceFrequency((LARGE_INTEGER*)&_frequency);
83 QueryPerformanceCounter((LARGE_INTEGER*)&_starttime);
86 QueryPerformanceCounter((LARGE_INTEGER*)&time);
87 int64_t now =
static_cast<int64_t
>( 1.0e6 * ((double)(time - _starttime) / (double)_frequency));
88 if( now <= prev) now = prev + 1;
94 static BOOL WINAPI CtrlHandler(DWORD fdwCtrlType) {
95 if (
is_tracing && fdwCtrlType == CTRL_C_EVENT) {
96 printf(
"Ctrl-C detected! Flushing trace and shutting down.\n\n");
105 SetConsoleCtrlHandler(&CtrlHandler, TRUE);
111 return (
int)(intptr_t)pthread_self();
114 #if defined(BLACKBERRY)
116 static int64_t prev = 0;
117 struct timespec time;
118 clock_gettime(CLOCK_MONOTONIC, &time);
119 int64_t now = time.tv_sec*1000000 + time.tv_nsec / 1000;
120 if( now <= prev) now = prev + 1;
127 static int64_t prev = 0;
129 gettimeofday(&tv,
nullptr);
130 int64_t now = 1000000*tv.tv_sec + tv.tv_usec;
131 if( now <= prev) now = prev + 1;
135 #endif // !BLACKBERRY
139 printf(
"Ctrl-C detected! Flushing trace and shutting down.\n\n");
141 fwrite(
"\n]}\n", 1, 4,
file);
153 signal(SIGINT, SIG_IGN);
165 file = fopen(json_file,
"wb");
166 const char *header =
"{\"traceEvents\":[\n";
167 fwrite(header, 1, strlen(header),
file);
170 pthread_mutex_init(&
mutex, 0);
180 fwrite(
"\n]}\n", 1, 4,
file);
182 pthread_mutex_destroy(&
mutex);
198 str_pool[i] = (
char *)malloc(strlen(str) + 1);
206 return "string pool full";
234 pthread_mutex_lock(&
mutex);
238 for (
int i = 0; i <
count; i++) {
249 if (strlen(raw->
a_str) > 700) {
250 ((
char*)raw->
a_str)[700] = 0;
265 snprintf(id_buf,
ARRAY_SIZE(id_buf),
",\"id\":\"0x%08x\"", (uint32_t)(uintptr_t)raw->
id);
274 const char *cat = raw->
cat;
279 int cat_len = (int)strlen(cat);
282 for (
int a = 0; a < cat_len; a++)
284 temp[a] = cat[a] ==
'\\' ?
'/' : cat[a];
291 len = snprintf(linebuf,
ARRAY_SIZE(linebuf),
"%s{\"cat\":\"%s\",\"pid\":%i,\"tid\":%i,\"ts\":%" PRId64
",\"ph\":\"%c\",\"name\":\"%s\",\"args\":{%s}%s}",
294 fwrite(linebuf, 1, len,
file);
300 pthread_mutex_unlock(&
mutex);
314 #if 0 && _WIN32 // TODO: This needs testing
315 int bufPos = InterlockedIncrement(&
count);
318 pthread_mutex_lock(&
mutex);
321 pthread_mutex_unlock(&
mutex);
330 memcpy(&x,
id,
sizeof(int64_t));
332 ev->
a_double =
static_cast<double>(ts - x);
351 #if 0 && _WIN32 // TODO: This needs testing
352 int bufPos = InterlockedIncrement(&
count);
355 pthread_mutex_lock(&
mutex);
358 pthread_mutex_unlock(&
mutex);