]> git.ipfire.org Git - thirdparty/git.git/blob - trace2/tr2_ctr.c
commit -a -m: allow the top-level tree to become empty again
[thirdparty/git.git] / trace2 / tr2_ctr.c
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"
6
7 /*
8 * A global counter block to aggregrate values from the partial sums
9 * from each thread.
10 */
11 static struct tr2_counter_block final_counter_block; /* access under tr2tls_mutex */
12
13 /*
14 * Define metadata for each global counter.
15 *
16 * This array must match the "enum trace2_counter_id" and the values
17 * in "struct tr2_counter_block.counter[*]".
18 */
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,
24 },
25 [TRACE2_COUNTER_ID_TEST2] = {
26 .category = "test",
27 .name = "test2",
28 .want_per_thread_events = 1,
29 },
30
31 /* Add additional metadata before here. */
32 };
33
34 void tr2_counter_increment(enum trace2_counter_id cid, uint64_t value)
35 {
36 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
37 struct tr2_counter *c = &ctx->counter_block.counter[cid];
38
39 c->value += value;
40
41 ctx->used_any_counter = 1;
42 if (tr2_counter_metadata[cid].want_per_thread_events)
43 ctx->used_any_per_thread_counter = 1;
44 }
45
46 void tr2_update_final_counters(void)
47 {
48 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
49 enum trace2_counter_id cid;
50
51 if (!ctx->used_any_counter)
52 return;
53
54 /*
55 * Access `final_counter_block` requires holding `tr2tls_mutex`.
56 * We assume that our caller is holding the lock.
57 */
58
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];
62
63 c_final->value += c->value;
64 }
65 }
66
67 void tr2_emit_per_thread_counters(tr2_tgt_evt_counter_t *fn_apply)
68 {
69 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
70 enum trace2_counter_id cid;
71
72 if (!ctx->used_any_per_thread_counter)
73 return;
74
75 /*
76 * For each counter, if the counter wants per-thread events
77 * and this thread used it (the value is non-zero), emit it.
78 */
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);
85 }
86
87 void tr2_emit_final_counters(tr2_tgt_evt_counter_t *fn_apply)
88 {
89 enum trace2_counter_id cid;
90
91 /*
92 * Access `final_counter_block` requires holding `tr2tls_mutex`.
93 * We assume that our caller is holding the lock.
94 */
95
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],
100 1);
101 }