1
#include "git-compat-util.h"
2
#include "trace2/tr2_tgt.h"
3
#include "trace2/tr2_tls.h"
4
#include "trace2/tr2_tmr.h"
7
#define MY_MAX(a, b) ((a) > (b) ? (a) : (b))
8
#define MY_MIN(a, b) ((a) < (b) ? (a) : (b))
14
static struct tr2_timer_block final_timer_block;
22
static struct tr2_timer_metadata tr2_timer_metadata[TRACE2_NUMBER_OF_TIMERS] = {
23
[TRACE2_TIMER_ID_TEST1] = {
26
.want_per_thread_events = 0,
28
[TRACE2_TIMER_ID_TEST2] = {
31
.want_per_thread_events = 1,
37
void tr2_start_timer(enum trace2_timer_id tid)
39
struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
40
struct tr2_timer *t = &ctx->timer_block.timer[tid];
43
if (t->recursion_count > 1)
46
t->start_ns = getnanotime();
49
void tr2_stop_timer(enum trace2_timer_id tid)
51
struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
52
struct tr2_timer *t = &ctx->timer_block.timer[tid];
56
assert(t->recursion_count > 0);
59
if (t->recursion_count)
62
ns_now = getnanotime();
63
ns_interval = ns_now - t->start_ns;
65
t->total_ns += ns_interval;
73
if (!t->interval_count) {
74
t->min_ns = ns_interval;
75
t->max_ns = ns_interval;
77
t->min_ns = MY_MIN(ns_interval, t->min_ns);
78
t->max_ns = MY_MAX(ns_interval, t->max_ns);
83
ctx->used_any_timer = 1;
84
if (tr2_timer_metadata[tid].want_per_thread_events)
85
ctx->used_any_per_thread_timer = 1;
88
void tr2_update_final_timers(void)
90
struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
91
enum trace2_timer_id tid;
93
if (!ctx->used_any_timer)
101
for (tid = 0; tid < TRACE2_NUMBER_OF_TIMERS; tid++) {
102
struct tr2_timer *t_final = &final_timer_block.timer[tid];
103
struct tr2_timer *t = &ctx->timer_block.timer[tid];
105
if (t->recursion_count) {
125
if (!t->interval_count)
128
t_final->total_ns += t->total_ns;
136
if (!t_final->interval_count) {
137
t_final->min_ns = t->min_ns;
138
t_final->max_ns = t->max_ns;
140
t_final->min_ns = MY_MIN(t_final->min_ns, t->min_ns);
141
t_final->max_ns = MY_MAX(t_final->max_ns, t->max_ns);
144
t_final->interval_count += t->interval_count;
148
void tr2_emit_per_thread_timers(tr2_tgt_evt_timer_t *fn_apply)
150
struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
151
enum trace2_timer_id tid;
153
if (!ctx->used_any_per_thread_timer)
160
for (tid = 0; tid < TRACE2_NUMBER_OF_TIMERS; tid++)
161
if (tr2_timer_metadata[tid].want_per_thread_events &&
162
ctx->timer_block.timer[tid].interval_count)
163
fn_apply(&tr2_timer_metadata[tid],
164
&ctx->timer_block.timer[tid],
168
void tr2_emit_final_timers(tr2_tgt_evt_timer_t *fn_apply)
170
enum trace2_timer_id tid;
177
for (tid = 0; tid < TRACE2_NUMBER_OF_TIMERS; tid++)
178
if (final_timer_block.timer[tid].interval_count)
179
fn_apply(&tr2_timer_metadata[tid],
180
&final_timer_block.timer[tid],