u/trace_marker: Add json writing helpers

This commit is contained in:
Jakob Bornecrantz 2021-04-07 19:45:54 +01:00 committed by Jakob Bornecrantz
parent c42198b86f
commit 52d1c4d834
2 changed files with 99 additions and 0 deletions
src/xrt/auxiliary/util

View file

@ -16,6 +16,7 @@
#include "u_trace_marker.h"
#ifdef XRT_OS_LINUX
#include <inttypes.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
@ -113,3 +114,71 @@ void
u_trace_data(int fd, enum u_trace_data_type type, void *data, size_t size)
{}
#endif
/*
*
* Writing functions.
*
*/
void
u_trace_maker_write_json_metadata(FILE *file, uint32_t pid, uint32_t tid, const char *name)
{
fprintf(file,
",\n"
"\t\t{\n"
"\t\t\t\"ph\": \"M\",\n"
"\t\t\t\"name\": \"thread_name\",\n"
"\t\t\t\"pid\": %u,\n"
"\t\t\t\"tid\": %u,\n"
"\t\t\t\"args\": {\n"
"\t\t\t\t\"name\": \"%s\"\n"
"\t\t\t}\n"
"\t\t}",
pid, tid, name);
}
void
u_trace_maker_write_json_begin(FILE *file, //
uint32_t pid, //
uint32_t tid, //
const char *name, //
const char *cat, //
uint64_t when_ns) //
{
// clang-format off
fprintf(file,
",\n"
"\t\t{\n"
"\t\t\t\"ph\": \"B\",\n"
"\t\t\t\"name\": \"%s\",\n"
"\t\t\t\"cat\": \"%s\",\n"
"\t\t\t\"ts\": %" PRIu64 ".%03" PRIu64 ",\n"
"\t\t\t\"pid\": %u,\n"
"\t\t\t\"tid\": %u,\n"
"\t\t\t\"args\": {}\n"
"\t\t}",
name, cat, when_ns / 1000, when_ns % 1000, pid, tid);
// clang-format on
}
void
u_trace_maker_write_json_end(FILE *file, //
uint32_t pid, //
uint32_t tid, //
uint64_t when_ns) //
{
// clang-format off
fprintf(file,
",\n"
"\t\t{\n"
"\t\t\t\"ph\": \"E\",\n"
"\t\t\t\"ts\": %" PRIu64 ".%03" PRIu64 ",\n"
"\t\t\t\"pid\": %u,\n"
"\t\t\t\"tid\": %u,\n"
"\t\t\t\"args\": {}\n"
"\t\t}",
when_ns / 1000, when_ns % 1000, pid, tid);
// clang-format on
}

View file

@ -47,6 +47,36 @@ extern int u_trace_comp_fd;
#define COMP_TRACE_DATA(type, data) U_TRACE_DATA(u_trace_comp_fd, type, data)
/*
*
* JSON dumper helper files.
*
*/
void
u_trace_maker_write_json_metadata( //
FILE *file, //
uint32_t pid, //
uint32_t tid, //
const char *name); //
void
u_trace_maker_write_json_begin( //
FILE *file, //
uint32_t pid, //
uint32_t tid, //
const char *name, //
const char *cat, //
uint64_t when_ns); //
void
u_trace_maker_write_json_end( //
FILE *file, //
uint32_t pid, //
uint32_t tid, //
uint64_t when_ns); //
/*
*
* Functions implemented by other modules.