wrapper: use trace2 counters to collect fsync stats

As mentioned in the thread starting at [1], trace2 counters should be used to count events instead of ad-hoc static variables. Convert the two fsync static variables to trace2 counters, reducing the coupling between wrapper.c and the trace2 subsystem. Adjust t/t5351 to match the trace2 counter output format. The counters are not per-thread because the ones being replaced also were not. [1] https://lore.kernel.org/git/20230627195251.1973421-2-calvinwan@google.com/ Signed-off-by: Beat Bolli <dev+git@drbeat.li> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Beat Bolli committed Jul 20, 2023 at 18:48 UTC a27eecea75b3858b4052b191143f144a7e966869
6 files changed +19 -26
t/t5351-unpack-large-objects.sh
+3 -3
@@ -55,7 +55,7 @@ check_fsync_events () {
55
56 cat >expect &&
57 sed -n \
58 - -e '/^{"event":"data",.*"category":"fsync",/ {
58 + -e '/^{"event":"counter",.*"category":"fsync",/ {
59 s/.*"category":"fsync",//;
60 s/}$//;
61 p;
@@ -78,8 +78,8 @@ test_expect_success 'unpack big object in stream (core.fsyncmethod=batch)' '
78 flush_count=1
79 fi &&
80 check_fsync_events trace2.txt <<-EOF &&
81 - "key":"fsync/writeout-only","value":"6"
82 - "key":"fsync/hardware-flush","value":"$flush_count"
81 + "name":"writeout-only","count":6
82 + "name":"hardware-flush","count":$flush_count
83 EOF
84
85 test_dir_is_empty dest.git/objects/pack &&
trace2.c
-1
@@ -276,7 +276,6 @@ void trace2_cmd_exit_fl(const char *file, int line, int code)
276 if (!trace2_enabled)
277 return;
278
279 - trace_git_fsync_stats();
279 trace2_collect_process_info(TRACE2_PROCESS_INFO_EXIT);
280
281 tr2main_exit_code = code;
trace2.h
+4
@@ -552,6 +552,10 @@ enum trace2_counter_id {
552 TRACE2_COUNTER_ID_TEST1 = 0, /* emits summary event only */
553 TRACE2_COUNTER_ID_TEST2, /* emits summary and thread events */
554
555 + /* counts number of fsyncs */
556 + TRACE2_COUNTER_ID_FSYNC_WRITEOUT_ONLY,
557 + TRACE2_COUNTER_ID_FSYNC_HARDWARE_FLUSH,
558 +
559 /* Add additional counter definitions before here. */
560 TRACE2_NUMBER_OF_COUNTERS
561 };
trace2/tr2_ctr.c
+10
@@ -27,6 +27,16 @@ static struct tr2_counter_metadata tr2_counter_metadata[TRACE2_NUMBER_OF_COUNTER
27 .name = "test2",
28 .want_per_thread_events = 1,
29 },
30 + [TRACE2_COUNTER_ID_FSYNC_WRITEOUT_ONLY] = {
31 + .category = "fsync",
32 + .name = "writeout-only",
33 + .want_per_thread_events = 0,
34 + },
35 + [TRACE2_COUNTER_ID_FSYNC_HARDWARE_FLUSH] = {
36 + .category = "fsync",
37 + .name = "hardware-flush",
38 + .want_per_thread_events = 0,
39 + },
40
41 /* Add additional metadata before here. */
42 };
wrapper.c
+2 -17
@@ -10,9 +10,6 @@
10 #include "strbuf.h"
11 #include "trace2.h"
12
13 -static intmax_t count_fsync_writeout_only;
14 -static intmax_t count_fsync_hardware_flush;
15 -
13 #ifdef HAVE_RTLGENRANDOM
14 /* This is required to get access to RtlGenRandom. */
15 #define SystemFunction036 NTAPI SystemFunction036
@@ -551,7 +548,7 @@ int git_fsync(int fd, enum fsync_action action)
548 {
549 switch (action) {
550 case FSYNC_WRITEOUT_ONLY:
554 - count_fsync_writeout_only += 1;
551 + trace2_counter_add(TRACE2_COUNTER_ID_FSYNC_WRITEOUT_ONLY, 1);
552
553 #ifdef __APPLE__
554 /*
@@ -583,7 +580,7 @@ int git_fsync(int fd, enum fsync_action action)
580 return -1;
581
582 case FSYNC_HARDWARE_FLUSH:
586 - count_fsync_hardware_flush += 1;
583 + trace2_counter_add(TRACE2_COUNTER_ID_FSYNC_HARDWARE_FLUSH, 1);
584
585 /*
586 * On macOS, a special fcntl is required to really flush the
@@ -600,18 +597,6 @@ int git_fsync(int fd, enum fsync_action action)
597 }
598 }
599
603 -static void log_trace_fsync_if(const char *key, intmax_t value)
604 -{
605 - if (value)
606 - trace2_data_intmax("fsync", the_repository, key, value);
607 -}
608 -
609 -void trace_git_fsync_stats(void)
610 -{
611 - log_trace_fsync_if("fsync/writeout-only", count_fsync_writeout_only);
612 - log_trace_fsync_if("fsync/hardware-flush", count_fsync_hardware_flush);
613 -}
614 -
600 static int warn_if_unremovable(const char *op, const char *file, int rc)
601 {
602 int err;
wrapper.h
-5
@@ -87,11 +87,6 @@ enum fsync_action {
87 */
88 int git_fsync(int fd, enum fsync_action action);
89
90 -/*
91 - * Writes out trace statistics for fsync using the trace2 API.
92 - */
93 -void trace_git_fsync_stats(void);
94 -
90 /*
91 * Preserves errno, prints a message, but gives no warning for ENOENT.
92 * Returns 0 on success, which includes trying to unlink an object that does