]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/util.h
New upstream release
[bcachefs-tools-debian] / libbcachefs / util.h
index 473c96968121b3c13f6990755b7b25778a786bcd..5fa29dab393bb6492163a36cfc3ea7c101059335 100644 (file)
@@ -11,7 +11,6 @@
 #include <linux/sched/clock.h>
 #include <linux/llist.h>
 #include <linux/log2.h>
-#include <linux/printbuf.h>
 #include <linux/percpu.h>
 #include <linux/preempt.h>
 #include <linux/ratelimit.h>
@@ -20,6 +19,8 @@
 #include <linux/workqueue.h>
 #include <linux/mean_and_variance.h>
 
+#include "darray.h"
+
 struct closure;
 
 #ifdef CONFIG_BCACHEFS_DEBUG
@@ -59,12 +60,13 @@ static inline void vpfree(void *p, size_t size)
                free_pages((unsigned long) p, get_order(size));
 }
 
-static inline void *vpmalloc(size_t size, gfp_t gfp_mask)
+static inline void *vpmalloc_noprof(size_t size, gfp_t gfp_mask)
 {
-       return (void *) __get_free_pages(gfp_mask|__GFP_NOWARN,
-                                        get_order(size)) ?:
-               __vmalloc(size, gfp_mask);
+       return (void *) get_free_pages_noprof(gfp_mask|__GFP_NOWARN,
+                                             get_order(size)) ?:
+               __vmalloc_noprof(size, gfp_mask);
 }
+#define vpmalloc(_size, _gfp)  alloc_hooks(vpmalloc_noprof(_size, _gfp))
 
 static inline void kvpfree(void *p, size_t size)
 {
@@ -74,12 +76,13 @@ static inline void kvpfree(void *p, size_t size)
                vpfree(p, size);
 }
 
-static inline void *kvpmalloc(size_t size, gfp_t gfp_mask)
+static inline void *kvpmalloc_noprof(size_t size, gfp_t gfp_mask)
 {
        return size < PAGE_SIZE
-               ? kmalloc(size, gfp_mask)
-               : vpmalloc(size, gfp_mask);
+               ? kmalloc_noprof(size, gfp_mask)
+               : vpmalloc_noprof(size, gfp_mask);
 }
+#define kvpmalloc(_size, _gfp) alloc_hooks(kvpmalloc_noprof(_size, _gfp))
 
 int mempool_init_kvpmalloc_pool(mempool_t *, int, size_t);
 
@@ -215,6 +218,34 @@ do {                                                                       \
 #define ANYSINT_MAX(t)                                                 \
        ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
 
+#include "printbuf.h"
+
+#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 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)
+
+#define printbuf_indent_add(_out, _n)  bch2_printbuf_indent_add(_out, _n)
+#define printbuf_indent_sub(_out, _n)  bch2_printbuf_indent_sub(_out, _n)
+
+#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)
+
+#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__)
+
+void bch2_pr_time_units(struct printbuf *, u64);
 
 #ifdef __KERNEL__
 static inline void pr_time(struct printbuf *out, u64 time)
@@ -333,29 +364,33 @@ 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);
-int bch2_prt_backtrace(struct printbuf *, struct task_struct *);
+
+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;
        /* all fields are in nanoseconds */
        u64             max_duration;
@@ -363,26 +398,30 @@ struct time_stats {
        u64             max_freq;
        u64             min_freq;
        u64             last_event;
-       struct quantiles quantiles;
+       struct bch2_quantiles quantiles;
 
        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 time_stat_buffer __percpu *buffer;
+       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)                                    \
 ({                                                                     \
@@ -493,7 +532,9 @@ static inline unsigned fract_exp_two(unsigned x, unsigned fract_bits)
 }
 
 void bch2_bio_map(struct bio *bio, void *base, size_t);
-int bch2_bio_alloc_pages(struct bio *, size_t, gfp_t);
+int bch2_bio_alloc_pages_noprof(struct bio *, size_t, gfp_t);
+#define bch2_bio_alloc_pages(_bio, _size, _gfp)                                \
+       alloc_hooks(bch2_bio_alloc_pages_noprof(_bio, _size, _gfp))
 
 static inline sector_t bdev_sectors(struct block_device *bdev)
 {
@@ -506,6 +547,26 @@ do {                                                                       \
        submit_bio(bio);                                                \
 } while (0)
 
+#define kthread_wait(cond)                                             \
+({                                                                     \
+       int _ret = 0;                                                   \
+                                                                       \
+       while (1) {                                                     \
+               set_current_state(TASK_INTERRUPTIBLE);                  \
+               if (kthread_should_stop()) {                            \
+                       _ret = -1;                                      \
+                       break;                                          \
+               }                                                       \
+                                                                       \
+               if (cond)                                               \
+                       break;                                          \
+                                                                       \
+               schedule();                                             \
+       }                                                               \
+       set_current_state(TASK_RUNNING);                                \
+       _ret;                                                           \
+})
+
 #define kthread_wait_freezable(cond)                                   \
 ({                                                                     \
        int _ret = 0;                                                   \
@@ -582,6 +643,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)
 {
@@ -766,4 +841,6 @@ static inline int u8_cmp(u8 l, u8 r)
        return cmp_int(l, r);
 }
 
+#include <linux/uuid.h>
+
 #endif /* _BCACHEFS_UTIL_H */