flush is usually called once but in some cases most of the time is spent in so we cannot ignore it. Here is an example run with this change: $ ./nbdkit --foreground \ --unix /tmp/nbd.sock \ --exportname '' \ --filter stats \ file file=/var/tmp/dst.img \ statsfile=/dev/stderr \ --run 'qemu-img convert -p -n -f raw -O raw -T none /var/tmp/fedora-30.img nbd:unix:/tmp/nbd.sock' (100.00/100%) elapsed time: 2.107 s write: 1271 ops, 1.14 GiB, 0.356 s, 3262.38 MiB/s zero: 1027 ops, 4.86 GiB, 0.013 s, 390318.00 MiB/s extents: 1 ops, 2.00 GiB, 0.000 s, 68266650.39 MiB/s flush: 2 ops, 1.021 s --- filters/stats/stats.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/filters/stats/stats.c b/filters/stats/stats.c index 86439e7..25e425b 100644 --- a/filters/stats/stats.c +++ b/filters/stats/stats.c @@ -66,6 +66,7 @@ static uint64_t trim_ops, trim_bytes, trim_usecs; static uint64_t zero_ops, zero_bytes, zero_usecs; static uint64_t extents_ops, extents_bytes, extents_usecs; static uint64_t cache_ops, cache_bytes, cache_usecs; +static uint64_t flush_ops, flush_usecs; static inline double calc_mibps (uint64_t bytes, int64_t usecs) @@ -96,6 +97,9 @@ print_stats (int64_t usecs) if (cache_ops > 0) fprintf (fp, "cache: %" PRIu64 " ops, %.2f GiB, %.3f s, %.2f MiB/s\n", cache_ops, cache_bytes / GiB, cache_usecs / USEC, calc_mibps (cache_bytes, cache_usecs)); + if (flush_ops > 0) + fprintf (fp, "flush: %" PRIu64 " ops, %.3f s\n", + flush_ops, flush_usecs / USEC); fflush (fp); } @@ -254,6 +258,30 @@ stats_trim (struct nbdkit_next_ops *next_ops, void *nxdata, return r; } +/* Flush. */ +static int +stats_flush (struct nbdkit_next_ops *next_ops, void *nxdata, + void *handle, uint32_t flags, + int *err) +{ + struct timeval start, end; + int64_t usecs; + int r; + + gettimeofday (&start, NULL); + r = next_ops->flush (nxdata, flags, err); + if (r == 0) { + gettimeofday (&end, NULL); + usecs = tvdiff_usec(&start, &end); + + ACQUIRE_LOCK_FOR_CURRENT_SCOPE (&lock); + flush_ops++; + flush_usecs += usecs; + } + + return r; +} + /* Zero. */ static int stats_zero (struct nbdkit_next_ops *next_ops, void *nxdata, @@ -343,6 +371,7 @@ static struct nbdkit_filter filter = { .pread = stats_pread, .pwrite = stats_pwrite, .trim = stats_trim, + .flush = stats_flush, .zero = stats_zero, .extents = stats_extents, .cache = stats_cache, -- 2.21.0