bcachefs: Convert bch2_pd_controller_print_debug() to a printbuf
authorKent Overstreet <kent.overstreet@gmail.com>
Fri, 25 Feb 2022 18:17:48 +0000 (13:17 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:09:25 +0000 (17:09 -0400)
Fewer random on-stack char arrays.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
fs/bcachefs/util.c
fs/bcachefs/util.h

index a330fa30cd79707096bea8032f7f7f1e98d4177e..2296658b9f0deba14645eb387330f002d87c013b 100644 (file)
@@ -484,36 +484,44 @@ void bch2_pd_controller_init(struct bch_pd_controller *pd)
        pd->backpressure        = 1;
 }
 
-size_t bch2_pd_controller_print_debug(struct bch_pd_controller *pd, char *buf)
-{
-       /* 2^64 - 1 is 20 digits, plus null byte */
-       char rate[21];
-       char actual[21];
-       char target[21];
-       char proportional[21];
-       char derivative[21];
-       char change[21];
-       s64 next_io;
-
-       bch2_hprint(&PBUF(rate),        pd->rate.rate);
-       bch2_hprint(&PBUF(actual),      pd->last_actual);
-       bch2_hprint(&PBUF(target),      pd->last_target);
-       bch2_hprint(&PBUF(proportional), pd->last_proportional);
-       bch2_hprint(&PBUF(derivative),  pd->last_derivative);
-       bch2_hprint(&PBUF(change),      pd->last_change);
-
-       next_io = div64_s64(pd->rate.next - local_clock(), NSEC_PER_MSEC);
-
-       return sprintf(buf,
-                      "rate:\t\t%s/sec\n"
-                      "target:\t\t%s\n"
-                      "actual:\t\t%s\n"
-                      "proportional:\t%s\n"
-                      "derivative:\t%s\n"
-                      "change:\t\t%s/sec\n"
-                      "next io:\t%llims\n",
-                      rate, target, actual, proportional,
-                      derivative, change, next_io);
+void bch2_pd_controller_debug_to_text(struct printbuf *out, struct bch_pd_controller *pd)
+{
+       out->tabstops[0] = 20;
+
+       pr_buf(out, "rate:");
+       pr_tab(out);
+       bch2_hprint(out, pd->rate.rate);
+       pr_newline(out);
+
+       pr_buf(out, "target:");
+       pr_tab(out);
+       bch2_hprint(out, pd->last_target);
+       pr_newline(out);
+
+       pr_buf(out, "actual:");
+       pr_tab(out);
+       bch2_hprint(out, pd->last_actual);
+       pr_newline(out);
+
+       pr_buf(out, "proportional:");
+       pr_tab(out);
+       bch2_hprint(out, pd->last_proportional);
+       pr_newline(out);
+
+       pr_buf(out, "derivative:");
+       pr_tab(out);
+       bch2_hprint(out, pd->last_derivative);
+       pr_newline(out);
+
+       pr_buf(out, "change:");
+       pr_tab(out);
+       bch2_hprint(out, pd->last_change);
+       pr_newline(out);
+
+       pr_buf(out, "next io:");
+       pr_tab(out);
+       pr_buf(out, "%llims", div64_s64(pd->rate.next - local_clock(), NSEC_PER_MSEC));
+       pr_newline(out);
 }
 
 /* misc: */
index 426c3009f292734a261c905ced81368b8aca61cf..58427edcfaa4e861749560c35714a4f6de047aaa 100644 (file)
@@ -582,7 +582,7 @@ struct bch_pd_controller {
 
 void bch2_pd_controller_update(struct bch_pd_controller *, s64, s64, int);
 void bch2_pd_controller_init(struct bch_pd_controller *);
-size_t bch2_pd_controller_print_debug(struct bch_pd_controller *, char *);
+void bch2_pd_controller_debug_to_text(struct printbuf *, struct bch_pd_controller *);
 
 #define sysfs_pd_controller_attribute(name)                            \
        rw_attribute(name##_rate);                                      \
@@ -605,8 +605,10 @@ do {                                                                       \
        sysfs_print(name##_rate_d_term,         (var)->d_term);         \
        sysfs_print(name##_rate_p_term_inverse, (var)->p_term_inverse); \
                                                                        \
-       if (attr == &sysfs_##name##_rate_debug)                         \
-               return bch2_pd_controller_print_debug(var, buf);                \
+       if (attr == &sysfs_##name##_rate_debug) {                       \
+               bch2_pd_controller_debug_to_text(&out, var);            \
+               return out.pos - buf;                                   \
+       }                                                               \
 } while (0)
 
 #define sysfs_pd_controller_store(name, var)                           \