Signed-off-by: Daniel Hill <[email protected]>
---
fs/bcachefs/printbuf.c | 14 ++++++++++++++
fs/bcachefs/printbuf.h | 1 +
2 files changed, 15 insertions(+)
diff --git a/fs/bcachefs/printbuf.c b/fs/bcachefs/printbuf.c
index b27d22925929..8e1f5b0bc2e9 100644
--- a/fs/bcachefs/printbuf.c
+++ b/fs/bcachefs/printbuf.c
@@ -446,3 +446,17 @@ void bch2_prt_bitflags_vector(struct printbuf *out,
bch2_prt_printf(out, "%s", list[i]);
}
}
+
+/* format a ratio of two u64s as a percentage (to three decimal places)
+ * supplying a default value for when the denominator is zero
+ */
+
+void bch2_prt_percent_rational_u64(struct printbuf *out,
+ u64 numerator, u64 denominator, u64 def)
+{
+ u64 rem = 0;
+
+ bch2_prt_printf(out, "%3llu.%03llu%%",
+ denominator ? div64_u64_rem(numerator * 100,
denominator, &rem) : def,
+ div64_u64(rem * 100, denominator));
+}
diff --git a/fs/bcachefs/printbuf.h b/fs/bcachefs/printbuf.h
index 9a4a56c40937..e8a87d17793b 100644
--- a/fs/bcachefs/printbuf.h
+++ b/fs/bcachefs/printbuf.h
@@ -126,6 +126,7 @@ void bch2_prt_string_option(struct printbuf *, const char *
const[], size_t);
void bch2_prt_bitflags(struct printbuf *, const char * const[], u64);
void bch2_prt_bitflags_vector(struct printbuf *, const char * const[],
unsigned long *, unsigned);
+void bch2_prt_percent_rational_u64(struct printbuf *, u64, u64, u64);
/* Initializer for a heap allocated printbuf: */
#define PRINTBUF ((struct printbuf) { .heap_allocated = true })
--
2.43.0