Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trace: add flush worker and optimize memory usage #967

Merged
merged 4 commits into from
Oct 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 58 additions & 8 deletions src/trace/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include <re_list.h>
#include <re_tmr.h>
#include <re_thread.h>
#include <re_atomic.h>
#include <re_sys.h>
#include <re_main.h>

#ifdef HAVE_PTHREAD
#include <pthread.h>
Expand All @@ -27,7 +29,22 @@
#include <unistd.h>
#endif

#define TRACE_BUFFER_SIZE 1000000
#define DEBUG_MODULE "trace"
#define DEBUG_LEVEL 5
#include <re_dbg.h>

#ifndef TRACE_BUFFER_SIZE
#define TRACE_BUFFER_SIZE 100000
#endif

#ifndef TRACE_FLUSH_THRESHOLD
#define TRACE_FLUSH_THRESHOLD 1000
#endif

#ifndef TRACE_FLUSH_TMR
#define TRACE_FLUSH_TMR 1000
#endif


struct trace_event {
const char *name;
Expand All @@ -47,15 +64,16 @@ struct trace_event {

/** Trace configuration */
static struct {
RE_ATOMIC bool init;
int process_id;
FILE *f;
int event_count;
struct trace_event *event_buffer;
struct trace_event *event_buffer_flush;
mtx_t lock;
bool init;
bool new;
uint64_t start_time;
struct tmr flush_tmr;
} trace = {
.init = false
};
Expand Down Expand Up @@ -90,6 +108,33 @@ static inline int get_process_id(void)
}


static int flush_worker(void *arg)
{
(void)arg;

mtx_lock(&trace.lock);
if (trace.event_count < TRACE_FLUSH_THRESHOLD) {
mtx_unlock(&trace.lock);
return 0;
}
mtx_unlock(&trace.lock);

re_trace_flush();

return 0;
}


static void flush_tmr(void *arg)
{
(void)arg;

re_thread_async(flush_worker, NULL, NULL);

tmr_start(&trace.flush_tmr, TRACE_FLUSH_TMR, flush_tmr, NULL);
}


/**
* Init new trace json file
*
Expand All @@ -108,7 +153,7 @@ int re_trace_init(const char *json_file)
if (!json_file)
return EINVAL;

if (trace.init)
if (re_atomic_rlx(&trace.init))
return EALREADY;

trace.event_buffer = mem_zalloc(
Expand Down Expand Up @@ -137,12 +182,15 @@ int re_trace_init(const char *json_file)
(void)fflush(trace.f);

trace.start_time = tmr_jiffies_usec();
trace.init = true;
re_atomic_rlx_set(&trace.init, true);
trace.new = true;

tmr_init(&trace.flush_tmr);
tmr_start(&trace.flush_tmr, TRACE_FLUSH_TMR, flush_tmr, NULL);

out:
if (err) {
trace.init = false;
re_atomic_rlx_set(&trace.init, false);
mem_deref(trace.event_buffer);
mem_deref(trace.event_buffer_flush);
}
Expand All @@ -164,12 +212,13 @@ int re_trace_close(void)
return 0;
#endif

tmr_cancel(&trace.flush_tmr);
re_trace_flush();
re_atomic_rlx_set(&trace.init, false);

trace.event_buffer = mem_deref(trace.event_buffer);
trace.event_buffer_flush = mem_deref(trace.event_buffer_flush);
mtx_destroy(&trace.lock);
trace.init = false;

(void)re_fprintf(trace.f, "\n\t]\n}\n");
if (trace.f)
Expand Down Expand Up @@ -201,7 +250,7 @@ int re_trace_flush(void)
return 0;
#endif

if (!trace.init)
if (!re_atomic_rlx(&trace.init))
return 0;

mtx_lock(&trace.lock);
Expand Down Expand Up @@ -266,11 +315,12 @@ void re_trace_event(const char *cat, const char *name, char ph, void *id,
return;
#endif

if (!trace.init)
if (!re_atomic_rlx(&trace.init))
return;

mtx_lock(&trace.lock);
if (trace.event_count >= TRACE_BUFFER_SIZE) {
DEBUG_WARNING("Increase TRACE_BUFFER_SIZE\n");
mtx_unlock(&trace.lock);
return;
}
Expand Down
Loading