trace: Use our own sequential thread ids to be more compatible with different trace viewers.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 22m14s

This commit is contained in:
Cory McWilliams 2025-01-24 19:00:40 -05:00
parent 82e876a892
commit e0444510f4

View File

@ -34,6 +34,7 @@ typedef struct _tf_trace_stack_t
typedef struct _tf_trace_thread_t typedef struct _tf_trace_thread_t
{ {
pthread_t id; pthread_t id;
int index;
tf_trace_stack_t* stack; tf_trace_stack_t* stack;
char name[64]; char name[64];
} tf_trace_thread_t; } tf_trace_thread_t;
@ -208,6 +209,7 @@ static tf_trace_thread_t* _tf_trace_get_thread(tf_trace_t* trace, pthread_t self
found = tf_malloc(sizeof(tf_trace_thread_t)); found = tf_malloc(sizeof(tf_trace_thread_t));
*found = (tf_trace_thread_t) { *found = (tf_trace_thread_t) {
.id = self, .id = self,
.index = trace->threads_count,
}; };
#if defined(__linux__) && !defined(__ANDROID__) #if defined(__linux__) && !defined(__ANDROID__)
pthread_getname_np(self, found->name, sizeof(found->name)); pthread_getname_np(self, found->name, sizeof(found->name));
@ -221,7 +223,7 @@ static tf_trace_thread_t* _tf_trace_get_thread(tf_trace_t* trace, pthread_t self
return found; return found;
} }
static void _tf_push_stack(tf_trace_t* trace, pthread_t self, const char* name, void* tag) static void _tf_push_stack(tf_trace_t* trace, pthread_t self, const char* name, void* tag, tf_trace_thread_t** out_thread)
{ {
tf_trace_thread_t* thread = _tf_trace_get_thread(trace, self); tf_trace_thread_t* thread = _tf_trace_get_thread(trace, self);
if (!thread->stack || thread->stack->count + 1 > tf_countof(thread->stack->names)) if (!thread->stack || thread->stack->count + 1 > tf_countof(thread->stack->names))
@ -239,9 +241,10 @@ static void _tf_push_stack(tf_trace_t* trace, pthread_t self, const char* name,
stack->names[stack->count] = name; stack->names[stack->count] = name;
stack->tags[stack->count] = tag; stack->tags[stack->count] = tag;
stack->count++; stack->count++;
*out_thread = thread;
} }
static const char* _tf_pop_stack(tf_trace_t* trace, pthread_t self, void* tag) static const char* _tf_pop_stack(tf_trace_t* trace, pthread_t self, void* tag, tf_trace_thread_t** out_thread)
{ {
tf_trace_thread_t* thread = _tf_trace_get_thread(trace, self); tf_trace_thread_t* thread = _tf_trace_get_thread(trace, self);
tf_trace_stack_t* stack = thread->stack; tf_trace_stack_t* stack = thread->stack;
@ -255,6 +258,7 @@ static const char* _tf_pop_stack(tf_trace_t* trace, pthread_t self, void* tag)
name = stack->names[stack->count - 1]; name = stack->names[stack->count - 1];
stack->count--; stack->count--;
} }
*out_thread = thread;
return name; return name;
} }
@ -265,11 +269,11 @@ static void _tf_trace_begin_tagged(tf_trace_t* trace, const char* name, void* ta
return; return;
} }
pthread_t self = pthread_self(); tf_trace_thread_t* thread = NULL;
_tf_push_stack(trace, self, name, tag); _tf_push_stack(trace, pthread_self(), name, tag, &thread);
char line[1024]; char line[1024];
int p = snprintf(line, sizeof(line), "{\"ph\": \"B\", \"pid\": %d, \"tid\": %" PRId64 ", \"ts\": %" PRId64 ", \"name\": \"", getpid(), (int64_t)self, _trace_ts()); int p = snprintf(line, sizeof(line), "{\"ph\": \"B\", \"pid\": %d, \"tid\": %d, \"ts\": %" PRId64 ", \"name\": \"", getpid(), thread->index, _trace_ts());
p += _tf_trace_escape_name(line + p, sizeof(line) - p - 4, name); p += _tf_trace_escape_name(line + p, sizeof(line) - p - 4, name);
p += snprintf(line + p, sizeof(line) - p, "\"},"); p += snprintf(line + p, sizeof(line) - p, "\"},");
p = tf_min(p, tf_countof(line)); p = tf_min(p, tf_countof(line));
@ -288,15 +292,15 @@ static void _tf_trace_end_tagged(tf_trace_t* trace, void* tag)
return; return;
} }
pthread_t self = pthread_self(); tf_trace_thread_t* thread = NULL;
const char* name = _tf_pop_stack(trace, self, tag); const char* name = _tf_pop_stack(trace, pthread_self(), tag, &thread);
if (!name) if (!name)
{ {
return; return;
} }
char line[1024]; char line[1024];
int p = snprintf(line, sizeof(line), "{\"ph\": \"E\", \"pid\": %d, \"tid\": %" PRId64 ", \"ts\": %" PRId64 ", \"name\": \"", getpid(), (int64_t)pthread_self(), _trace_ts()); int p = snprintf(line, sizeof(line), "{\"ph\": \"E\", \"pid\": %d, \"tid\": %d, \"ts\": %" PRId64 ", \"name\": \"", getpid(), thread->index, _trace_ts());
p += _tf_trace_escape_name(line + p, sizeof(line) - p - 4, name); p += _tf_trace_escape_name(line + p, sizeof(line) - p - 4, name);
p += snprintf(line + p, sizeof(line) - p, "\"},"); p += snprintf(line + p, sizeof(line) - p, "\"},");
p = tf_min(p, tf_countof(line)); p = tf_min(p, tf_countof(line));
@ -330,8 +334,8 @@ char* tf_trace_export(tf_trace_t* trace)
for (int i = 0; i < trace->threads_count; i++) for (int i = 0; i < trace->threads_count; i++)
{ {
tf_trace_thread_t* thread = trace->threads[i]; tf_trace_thread_t* thread = trace->threads[i];
size += snprintf(buffer + size, k_buffer_size - size, "{\"ph\":\"M\",\"pid\":%d,\"tid\":%" PRId64 ",\"name\":\"thread_name\",\"args\":{\"name\":\"%s\"}},\n", getpid(), size += snprintf(buffer + size, k_buffer_size - size, "{\"ph\":\"M\",\"pid\":%d,\"tid\":%d,\"name\":\"thread_name\",\"args\":{\"name\":\"%s\"}},\n", getpid(),
(uint64_t)thread->id, thread->name); thread->index, thread->name);
} }
uv_rwlock_rdunlock(&trace->threads_lock); uv_rwlock_rdunlock(&trace->threads_lock);
if (begin) if (begin)