2023-02-17 19:51:22 -05:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <pthread.h>
|
|
|
|
#undef _GNU_SOURCE
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
#include "trace.h"
|
|
|
|
|
2022-06-04 13:04:51 -04:00
|
|
|
#include "mem.h"
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
#include <assert.h>
|
2023-02-14 21:34:46 -05:00
|
|
|
#include <pthread.h>
|
2022-05-16 18:30:14 -04:00
|
|
|
#include <stdio.h>
|
2021-01-02 13:10:00 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-02-17 19:51:22 -05:00
|
|
|
#include <pthread.h>
|
2021-01-02 13:10:00 -05:00
|
|
|
#include <sqlite3.h>
|
2023-02-17 19:51:22 -05:00
|
|
|
#include <uv.h>
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2021-10-24 11:46:30 -04:00
|
|
|
#if !defined(_countof)
|
|
|
|
#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
|
|
|
|
#endif
|
|
|
|
|
2022-01-02 13:17:58 -05:00
|
|
|
enum
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
k_buffer_size = 4 * 1024 * 1024,
|
|
|
|
};
|
|
|
|
|
2021-10-24 11:46:30 -04:00
|
|
|
typedef struct _tf_trace_stack_t tf_trace_stack_t;
|
|
|
|
|
|
|
|
typedef struct _tf_trace_stack_t
|
|
|
|
{
|
|
|
|
const char* names[256];
|
|
|
|
int count;
|
|
|
|
tf_trace_stack_t* next;
|
|
|
|
} tf_trace_stack_t;
|
|
|
|
|
2023-02-17 19:51:22 -05:00
|
|
|
typedef struct _tf_trace_thread_t
|
|
|
|
{
|
|
|
|
pthread_t id;
|
|
|
|
tf_trace_stack_t* stack;
|
|
|
|
char name[64];
|
|
|
|
} tf_trace_thread_t;
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
typedef struct _tf_trace_t
|
|
|
|
{
|
|
|
|
char buffer[k_buffer_size];
|
2023-02-01 18:20:16 -05:00
|
|
|
char process_name[256];
|
2021-01-02 13:10:00 -05:00
|
|
|
int write_offset;
|
2021-10-24 11:46:30 -04:00
|
|
|
|
2023-02-17 19:51:22 -05:00
|
|
|
uv_mutex_t mutex;
|
2022-01-02 13:17:58 -05:00
|
|
|
tf_trace_write_callback_t* callback;
|
|
|
|
void* user_data;
|
|
|
|
|
2023-02-17 19:51:22 -05:00
|
|
|
uv_rwlock_t threads_lock;
|
|
|
|
tf_trace_thread_t** threads;
|
|
|
|
int threads_count;
|
2021-01-02 13:10:00 -05:00
|
|
|
} tf_trace_t;
|
|
|
|
|
2022-01-02 13:17:58 -05:00
|
|
|
static void _trace_append(tf_trace_t* trace, const char* buffer, size_t size, void* user_data)
|
|
|
|
{
|
2023-02-17 19:51:22 -05:00
|
|
|
uv_mutex_lock(&trace->mutex);
|
2022-01-02 13:17:58 -05:00
|
|
|
if (trace->write_offset + size + 2 >= k_buffer_size)
|
|
|
|
{
|
|
|
|
trace->buffer[trace->write_offset] = '\0';
|
|
|
|
trace->write_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trace->write_offset + size + 2 < k_buffer_size)
|
|
|
|
{
|
|
|
|
memcpy(trace->buffer + trace->write_offset, buffer, size);
|
|
|
|
trace->write_offset += size;
|
|
|
|
trace->buffer[trace->write_offset++] = '\n';
|
|
|
|
}
|
2023-02-17 19:51:22 -05:00
|
|
|
uv_mutex_unlock(&trace->mutex);
|
2022-01-02 13:17:58 -05:00
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_trace_t* tf_trace_create()
|
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_trace_t* trace = tf_malloc(sizeof(tf_trace_t));
|
2021-01-02 13:10:00 -05:00
|
|
|
memset(trace, 0, sizeof(*trace));
|
2022-01-02 13:17:58 -05:00
|
|
|
trace->callback = _trace_append;
|
2023-02-17 19:51:22 -05:00
|
|
|
uv_mutex_init(&trace->mutex);
|
|
|
|
uv_rwlock_init(&trace->threads_lock);
|
2021-01-02 13:10:00 -05:00
|
|
|
return trace;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tf_trace_destroy(tf_trace_t* trace)
|
|
|
|
{
|
2023-02-17 19:51:22 -05:00
|
|
|
for (int i = 0; i < trace->threads_count; i++)
|
2021-10-24 11:46:30 -04:00
|
|
|
{
|
2023-02-17 19:51:22 -05:00
|
|
|
tf_trace_thread_t* thread = trace->threads[i];
|
|
|
|
while (thread->stack)
|
|
|
|
{
|
|
|
|
tf_trace_stack_t* stack = thread->stack;
|
|
|
|
thread->stack = stack->next;
|
|
|
|
tf_free(stack);
|
|
|
|
}
|
|
|
|
tf_free(thread);
|
2021-10-24 11:46:30 -04:00
|
|
|
}
|
2023-02-17 19:51:22 -05:00
|
|
|
tf_free(trace->threads);
|
|
|
|
uv_rwlock_destroy(&trace->threads_lock);
|
|
|
|
uv_mutex_destroy(&trace->mutex);
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_free(trace);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 18:20:16 -05:00
|
|
|
void tf_trace_set_process_name(tf_trace_t* trace, const char* name)
|
|
|
|
{
|
|
|
|
snprintf(trace->process_name, sizeof(trace->process_name), "%s", name);
|
|
|
|
}
|
|
|
|
|
2022-01-02 13:17:58 -05:00
|
|
|
void tf_trace_raw(tf_trace_t* trace, const char* buffer, size_t size)
|
|
|
|
{
|
|
|
|
trace->callback(trace, buffer, size, trace->user_data);
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
static int64_t _trace_ts()
|
|
|
|
{
|
|
|
|
int64_t result = 0;
|
|
|
|
struct timespec ts;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
result = (ts.tv_sec * 1e9 + ts.tv_nsec) / 1e3;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tf_trace_counter(tf_trace_t* trace, const char* name, int argc, const char** arg_names, const int64_t* arg_values)
|
|
|
|
{
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!trace)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char line[1024];
|
|
|
|
int p = 0;
|
|
|
|
p += snprintf(line + p, sizeof(line) - p, "{\"ph\": \"C\", \"pid\": %d, \"ts\": %" PRId64 ", \"name\": \"%s\", \"args\": {", getpid(), _trace_ts(), name);
|
|
|
|
for (int i = 0; i < argc; i++)
|
|
|
|
{
|
|
|
|
p += snprintf(line + p, sizeof(line) - p, "\"%s\": %" PRId64 "%s", arg_names[i], arg_values[i], i == argc - 1 ? "}}," : ", ");
|
|
|
|
}
|
|
|
|
|
2022-01-02 13:17:58 -05:00
|
|
|
trace->callback(trace, line, p, trace->user_data);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2023-01-31 22:15:22 -05:00
|
|
|
static int _tf_trace_escape_name(char* out, size_t out_size, const char* name)
|
|
|
|
{
|
|
|
|
int p = 0;
|
|
|
|
for (const char* c = name; *c && (size_t)p < out_size; c++)
|
|
|
|
{
|
|
|
|
switch (*c)
|
|
|
|
{
|
|
|
|
case '"':
|
|
|
|
case '\\':
|
|
|
|
out[p++] = '\\';
|
|
|
|
if ((size_t)p + 1 < out_size)
|
|
|
|
{
|
|
|
|
out[p++] = *c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
out[p++] = '\\';
|
|
|
|
if ((size_t)p + 1 < out_size)
|
|
|
|
{
|
|
|
|
out[p++] = 't';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
out[p++] = '\\';
|
|
|
|
if ((size_t)p + 1 < out_size)
|
|
|
|
{
|
|
|
|
out[p++] = 'n';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
out[p++] = *c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2023-02-17 19:51:22 -05:00
|
|
|
static tf_trace_thread_t* _tf_trace_get_thread(tf_trace_t* trace, pthread_t self)
|
2021-01-02 13:10:00 -05:00
|
|
|
{
|
2023-02-17 19:51:22 -05:00
|
|
|
tf_trace_thread_t* found = NULL;
|
|
|
|
uv_rwlock_rdlock(&trace->threads_lock);
|
|
|
|
for (int i = 0; i < trace->threads_count; i++)
|
2021-10-10 17:51:38 -04:00
|
|
|
{
|
2023-02-17 19:51:22 -05:00
|
|
|
if (trace->threads[i]->id == self)
|
|
|
|
{
|
|
|
|
found = trace->threads[i];
|
|
|
|
break;
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2023-02-17 19:51:22 -05:00
|
|
|
uv_rwlock_rdunlock(&trace->threads_lock);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2023-02-17 19:51:22 -05:00
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
uv_rwlock_wrlock(&trace->threads_lock);
|
|
|
|
/* Maybe it was added while we changed from rd to rw. */
|
|
|
|
for (int i = 0; i < trace->threads_count; i++)
|
|
|
|
{
|
|
|
|
if (trace->threads[i]->id == self)
|
|
|
|
{
|
|
|
|
found = trace->threads[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
found = tf_malloc(sizeof(tf_trace_thread_t));
|
|
|
|
*found = (tf_trace_thread_t)
|
|
|
|
{
|
|
|
|
.id = self,
|
|
|
|
};
|
|
|
|
#if defined(__linux__)
|
|
|
|
pthread_getname_np(self, found->name, sizeof(found->name));
|
|
|
|
#endif
|
|
|
|
trace->threads = tf_resize_vec(trace->threads, sizeof(tf_trace_thread_t*) * (trace->threads_count + 1));
|
|
|
|
trace->threads[trace->threads_count++] = found;
|
|
|
|
}
|
|
|
|
uv_rwlock_wrunlock(&trace->threads_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _tf_push_stack(tf_trace_t* trace, pthread_t self, const char* name)
|
|
|
|
{
|
|
|
|
tf_trace_thread_t* thread = _tf_trace_get_thread(trace, self);
|
|
|
|
if (!thread->stack || thread->stack->count + 1 > (int)_countof(thread->stack->names))
|
2021-10-24 11:46:30 -04:00
|
|
|
{
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_trace_stack_t* stack = tf_malloc(sizeof(tf_trace_stack_t));
|
2021-10-24 11:46:30 -04:00
|
|
|
memset(stack, 0, sizeof(*stack));
|
2023-02-17 19:51:22 -05:00
|
|
|
stack->next = thread->stack;
|
|
|
|
thread->stack = stack;
|
2021-10-24 11:46:30 -04:00
|
|
|
}
|
2023-02-17 19:51:22 -05:00
|
|
|
tf_trace_stack_t* stack = thread->stack;
|
|
|
|
while (stack->count == 0 && stack->next && stack->next->count + 1 <= (int)_countof(thread->stack->names))
|
2021-10-24 11:46:30 -04:00
|
|
|
{
|
|
|
|
stack = stack->next;
|
|
|
|
}
|
|
|
|
stack->names[stack->count++] = name;
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2023-02-17 19:51:22 -05:00
|
|
|
static const char* _tf_pop_stack(tf_trace_t* trace, pthread_t self)
|
2021-01-02 13:10:00 -05:00
|
|
|
{
|
2023-02-17 19:51:22 -05:00
|
|
|
tf_trace_thread_t* thread = _tf_trace_get_thread(trace, self);
|
|
|
|
tf_trace_stack_t* stack = thread->stack;
|
2021-10-24 11:46:30 -04:00
|
|
|
while (stack && stack->count == 0)
|
|
|
|
{
|
|
|
|
stack = stack->next;
|
|
|
|
}
|
2021-12-21 14:33:02 -05:00
|
|
|
const char* name = NULL;
|
|
|
|
if (stack && stack->count > 0)
|
|
|
|
{
|
|
|
|
name = stack->names[stack->count - 1];
|
|
|
|
stack->count--;
|
|
|
|
}
|
2023-02-17 19:51:22 -05:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tf_trace_begin(tf_trace_t* trace, const char* name)
|
|
|
|
{
|
|
|
|
if (!trace)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_t self = pthread_self();
|
|
|
|
_tf_push_stack(trace, self, name);
|
|
|
|
|
|
|
|
char line[1024];
|
|
|
|
int p = snprintf(line, sizeof(line), "{\"ph\": \"B\", \"pid\": %d, \"tid\": \"0x%" PRIx64 "\", \"ts\": %" PRId64 ", \"name\": \"", getpid(), (int64_t)self, _trace_ts());
|
|
|
|
p += _tf_trace_escape_name(line + p, sizeof(line) - p, name);
|
|
|
|
p += snprintf(line + p, sizeof(line) - p, "\"},");
|
|
|
|
trace->callback(trace, line, p, trace->user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tf_trace_end(tf_trace_t* trace)
|
|
|
|
{
|
|
|
|
if (!trace)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_t self = pthread_self();
|
|
|
|
const char* name = _tf_pop_stack(trace, self);
|
2022-01-02 13:17:58 -05:00
|
|
|
if (!name)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-10-24 11:46:30 -04:00
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
char line[1024];
|
2023-02-17 19:51:22 -05:00
|
|
|
int p = snprintf(line, sizeof(line), "{\"ph\": \"E\", \"pid\": %d, \"tid\": \"0x%" PRIx64 "\", \"ts\": %" PRId64 ", \"name\": \"", getpid(), (int64_t)pthread_self(), _trace_ts());
|
2023-01-31 22:15:22 -05:00
|
|
|
p += _tf_trace_escape_name(line + p, sizeof(line) - p, name);
|
2021-10-24 11:46:30 -04:00
|
|
|
p += snprintf(line + p, sizeof(line) - p, "\"},");
|
2022-01-02 13:17:58 -05:00
|
|
|
trace->callback(trace, line, p, trace->user_data);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
char* tf_trace_export(tf_trace_t* trace)
|
|
|
|
{
|
2021-10-10 17:51:38 -04:00
|
|
|
if (!trace)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int k_extra_size = 1024;
|
2022-06-04 13:04:51 -04:00
|
|
|
char* buffer = tf_malloc(k_buffer_size + k_extra_size);
|
2023-02-17 19:51:22 -05:00
|
|
|
uv_mutex_lock(&trace->mutex);
|
2021-01-02 13:10:00 -05:00
|
|
|
const char* newline = strchr(trace->buffer + trace->write_offset, '\n');
|
|
|
|
int begin = newline ? newline - trace->buffer : 0;
|
|
|
|
size_t size = 0;
|
|
|
|
size += snprintf(buffer, k_buffer_size, "{\"displayTimeUnit\": \"ns\",\n\"traceEvents\": [\n");
|
2023-02-01 18:20:16 -05:00
|
|
|
if (*trace->process_name)
|
|
|
|
{
|
|
|
|
size += snprintf(buffer + size, k_buffer_size - size,
|
2023-02-17 19:51:22 -05:00
|
|
|
"{\"ph\":\"M\",\"pid\":%d,\"name\":\"process_name\",\"args\":{\"name\":\"%s\"}},\n",
|
2023-02-01 18:20:16 -05:00
|
|
|
getpid(),
|
|
|
|
trace->process_name);
|
|
|
|
}
|
2023-02-17 19:51:22 -05:00
|
|
|
uv_rwlock_rdlock(&trace->threads_lock);
|
|
|
|
for (int i = 0; i < trace->threads_count; i++)
|
|
|
|
{
|
|
|
|
tf_trace_thread_t* thread = trace->threads[i];
|
|
|
|
size += snprintf(buffer + size, k_buffer_size - size,
|
|
|
|
"{\"ph\":\"M\",\"pid\":%d,\"tid\":\"0x%" PRIx64 "\",\"name\":\"thread_name\",\"args\":{\"name\":\"%s\"}},\n",
|
|
|
|
getpid(),
|
|
|
|
thread->id,
|
|
|
|
thread->name);
|
|
|
|
}
|
|
|
|
uv_rwlock_rdunlock(&trace->threads_lock);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (begin)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
size_t this_size = strlen(trace->buffer + begin);
|
|
|
|
memcpy(buffer + size, trace->buffer + begin, this_size);
|
|
|
|
size += this_size;
|
|
|
|
}
|
|
|
|
memcpy(buffer + size, trace->buffer, trace->write_offset);
|
|
|
|
size += trace->write_offset;
|
2023-02-17 19:51:22 -05:00
|
|
|
uv_mutex_unlock(&trace->mutex);
|
2021-10-10 17:51:38 -04:00
|
|
|
if (size > 2 && buffer[size - 1] == '\n' && buffer[size - 2] == ',')
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
buffer[size - 2] = '\n';
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
size += snprintf(buffer + size, k_buffer_size - size, "]}\n");
|
|
|
|
assert(size < (size_t)k_buffer_size + k_extra_size);
|
2023-02-17 19:51:22 -05:00
|
|
|
buffer[size] = '\0';
|
2021-01-02 13:10:00 -05:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _tf_trace_sqlite_callback(unsigned int t, void* c, void* p, void* x)
|
|
|
|
{
|
|
|
|
tf_trace_t* trace = c;
|
2021-10-10 17:51:38 -04:00
|
|
|
switch (t)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
case SQLITE_TRACE_STMT:
|
|
|
|
{
|
|
|
|
const char* statement = x;
|
2021-10-10 17:51:38 -04:00
|
|
|
if (statement[0] != '-' || statement[1] != '-')
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
tf_trace_begin(trace, statement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SQLITE_TRACE_PROFILE:
|
|
|
|
tf_trace_end(trace);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-02 13:17:58 -05:00
|
|
|
void tf_trace_set_write_callback(tf_trace_t* trace, tf_trace_write_callback_t* callback, void* user_data)
|
|
|
|
{
|
|
|
|
trace->callback = callback;
|
|
|
|
trace->user_data = user_data;
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
void tf_trace_sqlite(tf_trace_t* trace, sqlite3* sqlite)
|
|
|
|
{
|
2021-10-10 17:51:38 -04:00
|
|
|
if (sqlite)
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
sqlite3_trace_v2(sqlite, SQLITE_TRACE_STMT | SQLITE_TRACE_PROFILE, _tf_trace_sqlite_callback, trace);
|
2021-10-10 17:51:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 13:10:00 -05:00
|
|
|
sqlite3_trace_v2(sqlite, 0, NULL, NULL);
|
|
|
|
}
|
|
|
|
}
|