From a29229f862d5bea1fe09222f04bbda834c08d887 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Wed, 11 Oct 2023 12:02:45 +0200 Subject: [PATCH 1/2] fmt/pl: add pl_alloc_str --- include/re_fmt.h | 1 + src/fmt/pl.c | 42 ++++++++++++++++++++++++++++++++++++++++++ test/fmt.c | 18 ++++++++++++++++++ test/test.c | 1 + test/test.h | 1 + 5 files changed, 63 insertions(+) diff --git a/include/re_fmt.h b/include/re_fmt.h index 13d0783ba..af4cd1841 100644 --- a/include/re_fmt.h +++ b/include/re_fmt.h @@ -29,6 +29,7 @@ struct pl { extern const struct pl pl_null; +struct pl *pl_alloc_str(const char *str); void pl_set_str(struct pl *pl, const char *str); void pl_set_mbuf(struct pl *pl, const struct mbuf *mb); int32_t pl_i32(const struct pl *pl); diff --git a/src/fmt/pl.c b/src/fmt/pl.c index 32edd193b..72ebfa4e8 100644 --- a/src/fmt/pl.c +++ b/src/fmt/pl.c @@ -21,6 +21,48 @@ const struct pl pl_null = {NULL, 0}; +static void pl_alloc_destruct(void *arg) +{ + struct pl *pl = arg; + + mem_deref((void *)pl->p); +} + + +/** + * Allocate a pointer-length object from a NULL-terminated string + * + * @param str NULL-terminated string + * + * @return Allocated Pointer-length object or NULL + */ +struct pl *pl_alloc_str(const char *str) +{ + struct pl *pl; + + if (!str) + return NULL; + + size_t sz = strlen(str); + + pl = mem_zalloc(sizeof(struct pl), pl_alloc_destruct); + if (!pl) + return NULL; + + pl->p = mem_alloc(sz, NULL); + if (!pl->p) { + mem_deref(pl); + return NULL; + } + + memcpy((void *)pl->p, str, sz); + + pl->l = sz; + + return pl; +} + + /** * Initialise a pointer-length object from a NULL-terminated string * diff --git a/test/fmt.c b/test/fmt.c index 79a93aecc..6eb9ef807 100644 --- a/test/fmt.c +++ b/test/fmt.c @@ -138,6 +138,24 @@ int test_fmt_pl(void) } +int test_fmt_pl_alloc_str(void) +{ + int err = 0; + char test_str[] = "Test String"; + + struct pl *pl = pl_alloc_str(test_str); + if (!pl) + return ENOMEM; + + TEST_MEMCMP(test_str, str_len(test_str), pl->p, pl->l); + +out: + mem_deref(pl); + + return err; +} + + int test_fmt_pl_i32(void) { const struct { diff --git a/test/test.c b/test/test.c index 86a3a278d..daec37bb1 100644 --- a/test/test.c +++ b/test/test.c @@ -79,6 +79,7 @@ static const struct test tests[] = { TEST(test_fmt_human_time), TEST(test_fmt_param), TEST(test_fmt_pl), + TEST(test_fmt_pl_alloc_str), TEST(test_fmt_pl_float), TEST(test_fmt_pl_i32), TEST(test_fmt_pl_i64), diff --git a/test/test.h b/test/test.h index a11cfdc5a..1eb31009e 100644 --- a/test/test.h +++ b/test/test.h @@ -181,6 +181,7 @@ int test_fmt_hexdump(void); int test_fmt_human_time(void); int test_fmt_param(void); int test_fmt_pl(void); +int test_fmt_pl_alloc_str(void); int test_fmt_pl_float(void); int test_fmt_pl_i32(void); int test_fmt_pl_i64(void); From b05e7164e0a302155ffccd68bec1839c805432e5 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Tue, 10 Oct 2023 21:22:30 +0200 Subject: [PATCH 2/2] trace: add id handling Add identifier handling for custom id tracking like: {"cat":"jbuf","pid":490156,"tid":490156,"ts":1415394,"ph":"I","name":"flush","id":"call1_video"} --- include/re_trace.h | 38 ++++++++++++++++++++++++++------------ src/trace/trace.c | 36 ++++++++++++++++++++---------------- 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/include/re_trace.h b/include/re_trace.h index e3e9710e2..96fe48702 100644 --- a/include/re_trace.h +++ b/include/re_trace.h @@ -3,6 +3,8 @@ * JSON traces (chrome://tracing) */ +struct pl; + typedef enum { RE_TRACE_ARG_NONE, RE_TRACE_ARG_INT, @@ -14,37 +16,46 @@ typedef enum { int re_trace_init(const char *json_file); int re_trace_close(void); int re_trace_flush(void); -void re_trace_event(const char *cat, const char *name, char ph, void *id, - int32_t async_id, re_trace_arg_type arg_type, - const char *arg_name, void *arg_value); +void re_trace_event(const char *cat, const char *name, char ph, struct pl *id, + re_trace_arg_type arg_type, const char *arg_name, + void *arg_value); #ifdef RE_TRACE_ENABLED #define RE_TRACE_BEGIN(c, n) \ - re_trace_event(c, n, 'B', 0, 0, RE_TRACE_ARG_NONE, NULL, NULL) + re_trace_event(c, n, 'B', NULL, RE_TRACE_ARG_NONE, NULL, NULL) #define RE_TRACE_END(c, n) \ - re_trace_event(c, n, 'E', 0, 0, RE_TRACE_ARG_NONE, NULL, NULL) + re_trace_event(c, n, 'E', NULL, RE_TRACE_ARG_NONE, NULL, NULL) #define RE_TRACE_ID_BEGIN(c, n, id) \ - re_trace_event(c, n, 'B', 0, id, RE_TRACE_ARG_NONE, NULL, NULL) + re_trace_event(c, n, 'B', id, RE_TRACE_ARG_NONE, NULL, NULL) #define RE_TRACE_ID_END(c, n, id) \ - re_trace_event(c, n, 'E', 0, id, RE_TRACE_ARG_NONE, NULL, NULL) + re_trace_event(c, n, 'E', id, RE_TRACE_ARG_NONE, NULL, NULL) #define RE_TRACE_INSTANT(c, n) \ - re_trace_event(c, n, 'I', 0, 0, RE_TRACE_ARG_NONE, NULL, NULL) + re_trace_event(c, n, 'I', NULL, RE_TRACE_ARG_NONE, NULL, NULL) #define RE_TRACE_INSTANT_C(c, n, vname, str) \ - re_trace_event(c, n, 'I', 0, 0, RE_TRACE_ARG_STRING_CONST, \ + re_trace_event(c, n, 'I', NULL, RE_TRACE_ARG_STRING_CONST, \ vname, (void *)(str)) #define RE_TRACE_INSTANT_I(c, n, i) \ - re_trace_event(c, n, 'I', 0, 0, RE_TRACE_ARG_INT, \ + re_trace_event(c, n, 'I', NULL, RE_TRACE_ARG_INT, \ + n, (void *)(intptr_t)i) + +#define RE_TRACE_ID_INSTANT(c, n, id) \ + re_trace_event(c, n, 'I', id, RE_TRACE_ARG_NONE, NULL, NULL) +#define RE_TRACE_ID_INSTANT_C(c, n, vname, str, id) \ + re_trace_event(c, n, 'I', id, RE_TRACE_ARG_STRING_CONST, \ + vname, (void *)(str)) +#define RE_TRACE_ID_INSTANT_I(c, n, i, id) \ + re_trace_event(c, n, 'I', id, RE_TRACE_ARG_INT, \ n, (void *)(intptr_t)i) #define RE_TRACE_PROCESS_NAME(n) \ - re_trace_event("", "process_name", 'M', 0, 0, \ + re_trace_event("", "process_name", 'M', NULL, \ RE_TRACE_ARG_STRING_COPY, \ "name", (void *)(n)) #define RE_TRACE_THREAD_NAME(n) \ - re_trace_event("", "thread_name", 'M', 0, 0, \ + re_trace_event("", "thread_name", 'M', NULL, \ RE_TRACE_ARG_STRING_COPY, \ "name", (void *)(n)) @@ -57,6 +68,9 @@ void re_trace_event(const char *cat, const char *name, char ph, void *id, #define RE_TRACE_INSTANT(c, n) #define RE_TRACE_INSTANT_C(c, n, str) #define RE_TRACE_INSTANT_I(c, n, i) +#define RE_TRACE_ID_INSTANT(c, n, id) +#define RE_TRACE_ID_INSTANT_C(c, n, str, id) +#define RE_TRACE_ID_INSTANT_I(c, n, i, id) #define RE_TRACE_PROCESS_NAME(n) #define RE_TRACE_THREAD_NAME(n) diff --git a/src/trace/trace.c b/src/trace/trace.c index d927953d7..18114c460 100644 --- a/src/trace/trace.c +++ b/src/trace/trace.c @@ -49,7 +49,7 @@ struct trace_event { const char *name; const char *cat; - void *id; + struct pl *id; uint64_t ts; int pid; unsigned long tid; @@ -243,8 +243,9 @@ int re_trace_flush(void) int i, flush_count; struct trace_event *event_tmp; struct trace_event *e; - char json_arg[256]; - char name[128]; + char json_arg[256] = {0}; + char name[128] = {0}; + char id_str[128] = {0}; #ifndef RE_TRACE_ENABLED return 0; @@ -291,12 +292,20 @@ int re_trace_flush(void) re_snprintf(name, sizeof(name), "\"name\":\"%s\"", e->name); + if (e->id) { + re_snprintf(id_str, sizeof(id_str), "\"id\":\"%r\"", + e->id); + mem_deref(e->id); + } + (void)re_fprintf(trace.f, - "%s{\"cat\":\"%s\",\"pid\":%i,\"tid\":%lu,\"ts\":%llu," - "\"ph\":\"%c\",%s%s}", + "%s{\"cat\":\"%s\",\"pid\":%i,\"tid\":%lu,\"ts\":%Lu," + "\"ph\":\"%c\",%s%s%s}", trace.new ? "" : ",\n", e->cat, e->pid, e->tid, e->ts - trace.start_time, - e->ph, name, str_isset(json_arg) ? json_arg : ""); + e->ph, name, + e->id ? id_str : "", + str_isset(json_arg) ? json_arg : ""); trace.new = false; } @@ -305,9 +314,9 @@ int re_trace_flush(void) } -void re_trace_event(const char *cat, const char *name, char ph, void *id, - int32_t custom_id, re_trace_arg_type arg_type, - const char *arg_name, void *arg_value) +void re_trace_event(const char *cat, const char *name, char ph, struct pl *id, + re_trace_arg_type arg_type, const char *arg_name, + void *arg_value) { struct trace_event *e; @@ -329,17 +338,12 @@ void re_trace_event(const char *cat, const char *name, char ph, void *id, mtx_unlock(&trace.lock); e->ts = tmr_jiffies_usec(); - e->id = id; + e->id = mem_ref(id); e->ph = ph; e->cat = cat; e->name = name; e->pid = get_process_id(); - if (custom_id) { - e->tid = custom_id; - } - else { - e->tid = get_thread_id(); - } + e->tid = get_thread_id(); e->arg_type = arg_type; e->arg_name = arg_name;