/src/git/trace2/tr2_ctr.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "git-compat-util.h" |
2 | | #include "trace2/tr2_tgt.h" |
3 | | #include "trace2/tr2_tls.h" |
4 | | #include "trace2/tr2_ctr.h" |
5 | | |
6 | | /* |
7 | | * A global counter block to aggregrate values from the partial sums |
8 | | * from each thread. |
9 | | */ |
10 | | static struct tr2_counter_block final_counter_block; /* access under tr2tls_mutex */ |
11 | | |
12 | | /* |
13 | | * Define metadata for each global counter. |
14 | | * |
15 | | * This array must match the "enum trace2_counter_id" and the values |
16 | | * in "struct tr2_counter_block.counter[*]". |
17 | | */ |
18 | | static struct tr2_counter_metadata tr2_counter_metadata[TRACE2_NUMBER_OF_COUNTERS] = { |
19 | | [TRACE2_COUNTER_ID_TEST1] = { |
20 | | .category = "test", |
21 | | .name = "test1", |
22 | | .want_per_thread_events = 0, |
23 | | }, |
24 | | [TRACE2_COUNTER_ID_TEST2] = { |
25 | | .category = "test", |
26 | | .name = "test2", |
27 | | .want_per_thread_events = 1, |
28 | | }, |
29 | | [TRACE2_COUNTER_ID_PACKED_REFS_JUMPS] = { |
30 | | .category = "packed-refs", |
31 | | .name = "jumps_made", |
32 | | .want_per_thread_events = 0, |
33 | | }, |
34 | | [TRACE2_COUNTER_ID_FSYNC_WRITEOUT_ONLY] = { |
35 | | .category = "fsync", |
36 | | .name = "writeout-only", |
37 | | .want_per_thread_events = 0, |
38 | | }, |
39 | | [TRACE2_COUNTER_ID_FSYNC_HARDWARE_FLUSH] = { |
40 | | .category = "fsync", |
41 | | .name = "hardware-flush", |
42 | | .want_per_thread_events = 0, |
43 | | }, |
44 | | |
45 | | /* Add additional metadata before here. */ |
46 | | }; |
47 | | |
48 | | void tr2_counter_increment(enum trace2_counter_id cid, uint64_t value) |
49 | 0 | { |
50 | 0 | struct tr2tls_thread_ctx *ctx = tr2tls_get_self(); |
51 | 0 | struct tr2_counter *c = &ctx->counter_block.counter[cid]; |
52 | |
|
53 | 0 | c->value += value; |
54 | |
|
55 | 0 | ctx->used_any_counter = 1; |
56 | 0 | if (tr2_counter_metadata[cid].want_per_thread_events) |
57 | 0 | ctx->used_any_per_thread_counter = 1; |
58 | 0 | } |
59 | | |
60 | | void tr2_update_final_counters(void) |
61 | 0 | { |
62 | 0 | struct tr2tls_thread_ctx *ctx = tr2tls_get_self(); |
63 | 0 | enum trace2_counter_id cid; |
64 | |
|
65 | 0 | if (!ctx->used_any_counter) |
66 | 0 | return; |
67 | | |
68 | | /* |
69 | | * Access `final_counter_block` requires holding `tr2tls_mutex`. |
70 | | * We assume that our caller is holding the lock. |
71 | | */ |
72 | | |
73 | 0 | for (cid = 0; cid < TRACE2_NUMBER_OF_COUNTERS; cid++) { |
74 | 0 | struct tr2_counter *c_final = &final_counter_block.counter[cid]; |
75 | 0 | const struct tr2_counter *c = &ctx->counter_block.counter[cid]; |
76 | |
|
77 | 0 | c_final->value += c->value; |
78 | 0 | } |
79 | 0 | } |
80 | | |
81 | | void tr2_emit_per_thread_counters(tr2_tgt_evt_counter_t *fn_apply) |
82 | 0 | { |
83 | 0 | struct tr2tls_thread_ctx *ctx = tr2tls_get_self(); |
84 | 0 | enum trace2_counter_id cid; |
85 | |
|
86 | 0 | if (!ctx->used_any_per_thread_counter) |
87 | 0 | return; |
88 | | |
89 | | /* |
90 | | * For each counter, if the counter wants per-thread events |
91 | | * and this thread used it (the value is non-zero), emit it. |
92 | | */ |
93 | 0 | for (cid = 0; cid < TRACE2_NUMBER_OF_COUNTERS; cid++) |
94 | 0 | if (tr2_counter_metadata[cid].want_per_thread_events && |
95 | 0 | ctx->counter_block.counter[cid].value) |
96 | 0 | fn_apply(&tr2_counter_metadata[cid], |
97 | 0 | &ctx->counter_block.counter[cid], |
98 | 0 | 0); |
99 | 0 | } |
100 | | |
101 | | void tr2_emit_final_counters(tr2_tgt_evt_counter_t *fn_apply) |
102 | 0 | { |
103 | 0 | enum trace2_counter_id cid; |
104 | | |
105 | | /* |
106 | | * Access `final_counter_block` requires holding `tr2tls_mutex`. |
107 | | * We assume that our caller is holding the lock. |
108 | | */ |
109 | |
|
110 | 0 | for (cid = 0; cid < TRACE2_NUMBER_OF_COUNTERS; cid++) |
111 | 0 | if (final_counter_block.counter[cid].value) |
112 | 0 | fn_apply(&tr2_counter_metadata[cid], |
113 | 0 | &final_counter_block.counter[cid], |
114 | 0 | 1); |
115 | 0 | } |