format.attach: allow empty value to disable multi-part messages
[git.git] / trace2 / tr2_ctr.c
blob483ca7c308fa216055c1a6de293a1bfbf9de0bb2
1 #include "cache.h"
2 #include "thread-utils.h"
3 #include "trace2/tr2_tgt.h"
4 #include "trace2/tr2_tls.h"
5 #include "trace2/tr2_ctr.h"
7 /*
8 * A global counter block to aggregrate values from the partial sums
9 * from each thread.
11 static struct tr2_counter_block final_counter_block; /* access under tr2tls_mutex */
14 * Define metadata for each global counter.
16 * This array must match the "enum trace2_counter_id" and the values
17 * in "struct tr2_counter_block.counter[*]".
19 static struct tr2_counter_metadata tr2_counter_metadata[TRACE2_NUMBER_OF_COUNTERS] = {
20 [TRACE2_COUNTER_ID_TEST1] = {
21 .category = "test",
22 .name = "test1",
23 .want_per_thread_events = 0,
25 [TRACE2_COUNTER_ID_TEST2] = {
26 .category = "test",
27 .name = "test2",
28 .want_per_thread_events = 1,
31 /* Add additional metadata before here. */
34 void tr2_counter_increment(enum trace2_counter_id cid, uint64_t value)
36 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
37 struct tr2_counter *c = &ctx->counter_block.counter[cid];
39 c->value += value;
41 ctx->used_any_counter = 1;
42 if (tr2_counter_metadata[cid].want_per_thread_events)
43 ctx->used_any_per_thread_counter = 1;
46 void tr2_update_final_counters(void)
48 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
49 enum trace2_counter_id cid;
51 if (!ctx->used_any_counter)
52 return;
55 * Access `final_counter_block` requires holding `tr2tls_mutex`.
56 * We assume that our caller is holding the lock.
59 for (cid = 0; cid < TRACE2_NUMBER_OF_COUNTERS; cid++) {
60 struct tr2_counter *c_final = &final_counter_block.counter[cid];
61 const struct tr2_counter *c = &ctx->counter_block.counter[cid];
63 c_final->value += c->value;
67 void tr2_emit_per_thread_counters(tr2_tgt_evt_counter_t *fn_apply)
69 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
70 enum trace2_counter_id cid;
72 if (!ctx->used_any_per_thread_counter)
73 return;
76 * For each counter, if the counter wants per-thread events
77 * and this thread used it (the value is non-zero), emit it.
79 for (cid = 0; cid < TRACE2_NUMBER_OF_COUNTERS; cid++)
80 if (tr2_counter_metadata[cid].want_per_thread_events &&
81 ctx->counter_block.counter[cid].value)
82 fn_apply(&tr2_counter_metadata[cid],
83 &ctx->counter_block.counter[cid],
84 0);
87 void tr2_emit_final_counters(tr2_tgt_evt_counter_t *fn_apply)
89 enum trace2_counter_id cid;
92 * Access `final_counter_block` requires holding `tr2tls_mutex`.
93 * We assume that our caller is holding the lock.
96 for (cid = 0; cid < TRACE2_NUMBER_OF_COUNTERS; cid++)
97 if (final_counter_block.counter[cid].value)
98 fn_apply(&tr2_counter_metadata[cid],
99 &final_counter_block.counter[cid],