X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libbcachefs%2Futil.h;h=3956043895de8b18b2f5a88d14224948d15923b7;hb=9fc4b5d675cd6dc0b2503abe95b1c761d8d05abe;hp=2c9e91023bb96a6c80bc4d81e43cce075b046549;hpb=3e2e3d468eed1d5ebbb4c6309d2eaebd081912c5;p=bcachefs-tools-debian diff --git a/libbcachefs/util.h b/libbcachefs/util.h index 2c9e910..3956043 100644 --- a/libbcachefs/util.h +++ b/libbcachefs/util.h @@ -17,37 +17,16 @@ #include #include #include +#include + +#include "darray.h" struct closure; #ifdef CONFIG_BCACHEFS_DEBUG - #define EBUG_ON(cond) BUG_ON(cond) -#define atomic_dec_bug(v) BUG_ON(atomic_dec_return(v) < 0) -#define atomic_inc_bug(v, i) BUG_ON(atomic_inc_return(v) <= i) -#define atomic_sub_bug(i, v) BUG_ON(atomic_sub_return(i, v) < 0) -#define atomic_add_bug(i, v) BUG_ON(atomic_add_return(i, v) < 0) -#define atomic_long_dec_bug(v) BUG_ON(atomic_long_dec_return(v) < 0) -#define atomic_long_sub_bug(i, v) BUG_ON(atomic_long_sub_return(i, v) < 0) -#define atomic64_dec_bug(v) BUG_ON(atomic64_dec_return(v) < 0) -#define atomic64_inc_bug(v, i) BUG_ON(atomic64_inc_return(v) <= i) -#define atomic64_sub_bug(i, v) BUG_ON(atomic64_sub_return(i, v) < 0) -#define atomic64_add_bug(i, v) BUG_ON(atomic64_add_return(i, v) < 0) - -#else /* DEBUG */ - +#else #define EBUG_ON(cond) -#define atomic_dec_bug(v) atomic_dec(v) -#define atomic_inc_bug(v, i) atomic_inc(v) -#define atomic_sub_bug(i, v) atomic_sub(i, v) -#define atomic_add_bug(i, v) atomic_add(i, v) -#define atomic_long_dec_bug(v) atomic_long_dec(v) -#define atomic_long_sub_bug(i, v) atomic_long_sub(i, v) -#define atomic64_dec_bug(v) atomic64_dec(v) -#define atomic64_inc_bug(v, i) atomic64_inc(v) -#define atomic64_sub_bug(i, v) atomic64_sub(i, v) -#define atomic64_add_bug(i, v) atomic64_add(i, v) - #endif #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ @@ -210,9 +189,11 @@ do { \ \ BUG_ON(_i >= (h)->used); \ (h)->used--; \ - heap_swap(h, _i, (h)->used, set_backpointer); \ - heap_sift_up(h, _i, cmp, set_backpointer); \ - heap_sift_down(h, _i, cmp, set_backpointer); \ + if ((_i) < (h)->used) { \ + heap_swap(h, _i, (h)->used, set_backpointer); \ + heap_sift_up(h, _i, cmp, set_backpointer); \ + heap_sift_down(h, _i, cmp, set_backpointer); \ + } \ } while (0) #define heap_pop(h, d, cmp, set_backpointer) \ @@ -235,124 +216,39 @@ do { \ #define ANYSINT_MAX(t) \ ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1) -enum printbuf_units { - PRINTBUF_UNITS_RAW, - PRINTBUF_UNITS_BYTES, - PRINTBUF_UNITS_HUMAN_READABLE, -}; - -struct printbuf { - char *buf; - unsigned size; - unsigned pos; - unsigned last_newline; - unsigned last_field; - unsigned indent; - enum printbuf_units units:8; - u8 atomic; - bool allocation_failure:1; - u8 tabstop; - u8 tabstops[4]; -}; - -#define PRINTBUF ((struct printbuf) { NULL }) - -static inline void printbuf_exit(struct printbuf *buf) -{ - kfree(buf->buf); - buf->buf = ERR_PTR(-EINTR); /* poison value */ -} - -static inline void printbuf_reset(struct printbuf *buf) -{ - buf->pos = 0; - buf->last_newline = 0; - buf->last_field = 0; - buf->indent = 0; - buf->tabstop = 0; -} - -static inline size_t printbuf_remaining(struct printbuf *buf) -{ - return buf->size - buf->pos; -} - -static inline size_t printbuf_linelen(struct printbuf *buf) -{ - return buf->pos - buf->last_newline; -} +#include "printbuf.h" -void bch2_pr_buf(struct printbuf *out, const char *fmt, ...); +#define prt_vprintf(_out, ...) bch2_prt_vprintf(_out, __VA_ARGS__) +#define prt_printf(_out, ...) bch2_prt_printf(_out, __VA_ARGS__) +#define printbuf_str(_buf) bch2_printbuf_str(_buf) +#define printbuf_exit(_buf) bch2_printbuf_exit(_buf) -#define pr_buf(_out, ...) bch2_pr_buf(_out, __VA_ARGS__) +#define printbuf_tabstops_reset(_buf) bch2_printbuf_tabstops_reset(_buf) +#define printbuf_tabstop_pop(_buf) bch2_printbuf_tabstop_pop(_buf) +#define printbuf_tabstop_push(_buf, _n) bch2_printbuf_tabstop_push(_buf, _n) -static inline void pr_char(struct printbuf *out, char c) -{ - bch2_pr_buf(out, "%c", c); -} +#define printbuf_indent_add(_out, _n) bch2_printbuf_indent_add(_out, _n) +#define printbuf_indent_sub(_out, _n) bch2_printbuf_indent_sub(_out, _n) -static inline void pr_indent_push(struct printbuf *buf, unsigned spaces) -{ - buf->indent += spaces; - while (spaces--) - pr_char(buf, ' '); -} +#define prt_newline(_out) bch2_prt_newline(_out) +#define prt_tab(_out) bch2_prt_tab(_out) +#define prt_tab_rjust(_out) bch2_prt_tab_rjust(_out) -static inline void pr_indent_pop(struct printbuf *buf, unsigned spaces) -{ - if (buf->last_newline + buf->indent == buf->pos) { - buf->pos -= spaces; - buf->buf[buf->pos] = 0; - } - buf->indent -= spaces; -} - -static inline void pr_newline(struct printbuf *buf) -{ - unsigned i; - - pr_char(buf, '\n'); +#define prt_bytes_indented(...) bch2_prt_bytes_indented(__VA_ARGS__) +#define prt_u64(_out, _v) prt_printf(_out, "%llu", (u64) (_v)) +#define prt_human_readable_u64(...) bch2_prt_human_readable_u64(__VA_ARGS__) +#define prt_human_readable_s64(...) bch2_prt_human_readable_s64(__VA_ARGS__) +#define prt_units_u64(...) bch2_prt_units_u64(__VA_ARGS__) +#define prt_units_s64(...) bch2_prt_units_s64(__VA_ARGS__) +#define prt_string_option(...) bch2_prt_string_option(__VA_ARGS__) +#define prt_bitflags(...) bch2_prt_bitflags(__VA_ARGS__) - buf->last_newline = buf->pos; - - for (i = 0; i < buf->indent; i++) - pr_char(buf, ' '); - - buf->last_field = buf->pos; - buf->tabstop = 0; -} - -static inline void pr_tab(struct printbuf *buf) -{ - BUG_ON(buf->tabstop > ARRAY_SIZE(buf->tabstops)); - - while (printbuf_remaining(buf) > 1 && - printbuf_linelen(buf) < buf->tabstops[buf->tabstop]) - pr_char(buf, ' '); - - buf->last_field = buf->pos; - buf->tabstop++; -} - -void bch2_pr_tab_rjust(struct printbuf *); - -static inline void pr_tab_rjust(struct printbuf *buf) -{ - bch2_pr_tab_rjust(buf); -} - -void bch2_pr_units(struct printbuf *, s64, s64); -#define pr_units(...) bch2_pr_units(__VA_ARGS__) - -static inline void pr_sectors(struct printbuf *out, u64 v) -{ - bch2_pr_units(out, v, v << 9); -} +void bch2_pr_time_units(struct printbuf *, u64); #ifdef __KERNEL__ static inline void pr_time(struct printbuf *out, u64 time) { - pr_buf(out, "%llu", time); + prt_printf(out, "%llu", time); } #else #include @@ -363,16 +259,16 @@ static inline void pr_time(struct printbuf *out, u64 _time) struct tm *tm = localtime(&time); size_t err = strftime(time_str, sizeof(time_str), "%c", tm); if (!err) - pr_buf(out, "(formatting error)"); + prt_printf(out, "(formatting error)"); else - pr_buf(out, "%s", time_str); + prt_printf(out, "%s", time_str); } #endif #ifdef __KERNEL__ static inline void uuid_unparse_lower(u8 *uuid, char *out) { - sprintf(out, "%plU", uuid); + sprintf(out, "%pUb", uuid); } #else #include @@ -383,7 +279,7 @@ static inline void pr_uuid(struct printbuf *out, u8 *uuid) char uuid_str[40]; uuid_unparse_lower(uuid, uuid_str); - pr_buf(out, uuid_str); + prt_printf(out, "%s", uuid_str); } int bch2_strtoint_h(const char *, int *); @@ -449,7 +345,7 @@ static inline int bch2_strtoul_h(const char *cp, long *res) }) #define snprint(out, var) \ - pr_buf(out, \ + prt_printf(out, \ type_is(var, int) ? "%i\n" \ : type_is(var, unsigned) ? "%u\n" \ : type_is(var, long) ? "%li\n" \ @@ -459,60 +355,71 @@ static inline int bch2_strtoul_h(const char *cp, long *res) : type_is(var, char *) ? "%s\n" \ : "%i\n", var) -void bch2_hprint(struct printbuf *, s64); - bool bch2_is_zero(const void *, size_t); -void bch2_string_opt_to_text(struct printbuf *, - const char * const [], size_t); - -void bch2_flags_to_text(struct printbuf *, const char * const[], u64); u64 bch2_read_flag_list(char *, const char * const[]); +void bch2_prt_u64_binary(struct printbuf *, u64, unsigned); + +void bch2_print_string_as_lines(const char *prefix, const char *lines); + +typedef DARRAY(unsigned long) bch_stacktrace; +int bch2_save_backtrace(bch_stacktrace *stack, struct task_struct *); +void bch2_prt_backtrace(struct printbuf *, bch_stacktrace *); +int bch2_prt_task_backtrace(struct printbuf *, struct task_struct *); + #define NR_QUANTILES 15 #define QUANTILE_IDX(i) inorder_to_eytzinger0(i, NR_QUANTILES) #define QUANTILE_FIRST eytzinger0_first(NR_QUANTILES) #define QUANTILE_LAST eytzinger0_last(NR_QUANTILES) -struct quantiles { - struct quantile_entry { +struct bch2_quantiles { + struct bch2_quantile_entry { u64 m; u64 step; } entries[NR_QUANTILES]; }; -struct time_stat_buffer { +struct bch2_time_stat_buffer { unsigned nr; - struct time_stat_buffer_entry { + struct bch2_time_stat_buffer_entry { u64 start; u64 end; } entries[32]; }; -struct time_stats { +struct bch2_time_stats { spinlock_t lock; - u64 count; /* all fields are in nanoseconds */ - u64 average_duration; - u64 average_frequency; u64 max_duration; + u64 min_duration; + u64 max_freq; + u64 min_freq; u64 last_event; - struct quantiles quantiles; + struct bch2_quantiles quantiles; - struct time_stat_buffer __percpu *buffer; + struct mean_and_variance duration_stats; + struct mean_and_variance_weighted duration_stats_weighted; + struct mean_and_variance freq_stats; + struct mean_and_variance_weighted freq_stats_weighted; + struct bch2_time_stat_buffer __percpu *buffer; }; -void __bch2_time_stats_update(struct time_stats *stats, u64, u64); +#ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT +void __bch2_time_stats_update(struct bch2_time_stats *stats, u64, u64); +#else +static inline void __bch2_time_stats_update(struct bch2_time_stats *stats, u64 start, u64 end) {} +#endif -static inline void bch2_time_stats_update(struct time_stats *stats, u64 start) +static inline void bch2_time_stats_update(struct bch2_time_stats *stats, u64 start) { __bch2_time_stats_update(stats, start, local_clock()); } -void bch2_time_stats_to_text(struct printbuf *, struct time_stats *); +void bch2_time_stats_to_text(struct printbuf *, struct bch2_time_stats *); -void bch2_time_stats_exit(struct time_stats *); -void bch2_time_stats_init(struct time_stats *); +void bch2_time_stats_exit(struct bch2_time_stats *); +void bch2_time_stats_init(struct bch2_time_stats *); #define ewma_add(ewma, val, weight) \ ({ \ @@ -712,6 +619,20 @@ static inline void memmove_u64s_down(void *dst, const void *src, __memmove_u64s_down(dst, src, u64s); } +static inline void __memmove_u64s_down_small(void *dst, const void *src, + unsigned u64s) +{ + memcpy_u64s_small(dst, src, u64s); +} + +static inline void memmove_u64s_down_small(void *dst, const void *src, + unsigned u64s) +{ + EBUG_ON(dst > src); + + __memmove_u64s_down_small(dst, src, u64s); +} + static inline void __memmove_u64s_up_small(void *_dst, const void *_src, unsigned u64s) { @@ -803,6 +724,31 @@ do { \ #define array_remove_item(_array, _nr, _pos) \ array_remove_items(_array, _nr, _pos, 1) +static inline void __move_gap(void *array, size_t element_size, + size_t nr, size_t size, + size_t old_gap, size_t new_gap) +{ + size_t gap_end = old_gap + size - nr; + + if (new_gap < old_gap) { + size_t move = old_gap - new_gap; + + memmove(array + element_size * (gap_end - move), + array + element_size * (old_gap - move), + element_size * move); + } else if (new_gap > old_gap) { + size_t move = new_gap - old_gap; + + memmove(array + element_size * old_gap, + array + element_size * gap_end, + element_size * move); + } +} + +/* Move the gap in a gap buffer: */ +#define move_gap(_array, _nr, _size, _old_gap, _new_gap) \ + __move_gap(_array, sizeof(_array[0]), _nr, _size, _old_gap, _new_gap) + #define bubble_sort(_base, _nr, _cmp) \ do { \ ssize_t _i, _end; \