]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/util.h
Update upstream source from tag 'v1.6.3'
[bcachefs-tools-debian] / libbcachefs / util.h
index 54e309d94b9bebeb43c0503b145b6ad38b0771c5..1b3aced8d83caf63f867553d87ab091fab807a54 100644 (file)
@@ -5,22 +5,22 @@
 #include <linux/bio.h>
 #include <linux/blkdev.h>
 #include <linux/closure.h>
+#include <linux/darray.h>
 #include <linux/errno.h>
 #include <linux/freezer.h>
 #include <linux/kernel.h>
-#include <linux/sched/clock.h>
 #include <linux/llist.h>
 #include <linux/log2.h>
 #include <linux/percpu.h>
 #include <linux/preempt.h>
 #include <linux/ratelimit.h>
+#include <linux/sched/clock.h>
 #include <linux/slab.h>
+#include <linux/time_stats.h>
 #include <linux/vmalloc.h>
 #include <linux/workqueue.h>
 #include <linux/mean_and_variance.h>
 
-#include "darray.h"
-
 struct closure;
 
 #ifdef CONFIG_BCACHEFS_DEBUG
@@ -52,38 +52,6 @@ static inline size_t buf_pages(void *p, size_t len)
                            PAGE_SIZE);
 }
 
-static inline void vpfree(void *p, size_t size)
-{
-       if (is_vmalloc_addr(p))
-               vfree(p);
-       else
-               free_pages((unsigned long) p, get_order(size));
-}
-
-static inline void *vpmalloc(size_t size, gfp_t gfp_mask)
-{
-       return (void *) __get_free_pages(gfp_mask|__GFP_NOWARN,
-                                        get_order(size)) ?:
-               __vmalloc(size, gfp_mask);
-}
-
-static inline void kvpfree(void *p, size_t size)
-{
-       if (size < PAGE_SIZE)
-               kfree(p);
-       else
-               vpfree(p, size);
-}
-
-static inline void *kvpmalloc(size_t size, gfp_t gfp_mask)
-{
-       return size < PAGE_SIZE
-               ? kmalloc(size, gfp_mask)
-               : vpmalloc(size, gfp_mask);
-}
-
-int mempool_init_kvpmalloc_pool(mempool_t *, int, size_t);
-
 #define HEAP(type)                                                     \
 struct {                                                               \
        size_t size, used;                                              \
@@ -96,13 +64,13 @@ struct {                                                            \
 ({                                                                     \
        (heap)->used = 0;                                               \
        (heap)->size = (_size);                                         \
-       (heap)->data = kvpmalloc((heap)->size * sizeof((heap)->data[0]),\
+       (heap)->data = kvmalloc((heap)->size * sizeof((heap)->data[0]),\
                                 (gfp));                                \
 })
 
 #define free_heap(heap)                                                        \
 do {                                                                   \
-       kvpfree((heap)->data, (heap)->size * sizeof((heap)->data[0]));  \
+       kvfree((heap)->data);                                           \
        (heap)->data = NULL;                                            \
 } while (0)
 
@@ -242,6 +210,7 @@ do {                                                                        \
 #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__)
+#define prt_bitflags_vector(...)       bch2_prt_bitflags_vector(__VA_ARGS__)
 
 void bch2_pr_time_units(struct printbuf *, u64);
 void bch2_prt_datetime(struct printbuf *, time64_t);
@@ -340,92 +309,26 @@ bool bch2_is_zero(const void *, size_t);
 
 u64 bch2_read_flag_list(char *, const char * const[]);
 
-void bch2_prt_u64_binary(struct printbuf *, u64, unsigned);
+void bch2_prt_u64_base2_nbits(struct printbuf *, u64, unsigned);
+void bch2_prt_u64_base2(struct printbuf *, u64);
 
 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 *);
+int bch2_save_backtrace(bch_stacktrace *stack, struct task_struct *, unsigned, gfp_t);
 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 bch2_quantiles {
-       struct bch2_quantile_entry {
-               u64     m;
-               u64     step;
-       }               entries[NR_QUANTILES];
-};
-
-struct bch2_time_stat_buffer {
-       unsigned        nr;
-       struct bch2_time_stat_buffer_entry {
-               u64     start;
-               u64     end;
-       }               entries[32];
-};
-
-struct bch2_time_stats {
-       spinlock_t      lock;
-       /* all fields are in nanoseconds */
-       u64             min_duration;
-       u64             max_duration;
-       u64             total_duration;
-       u64             max_freq;
-       u64             min_freq;
-       u64             last_event;
-       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 bch2_time_stat_buffer __percpu *buffer;
-};
-
-#ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
-void __bch2_time_stats_update(struct bch2_time_stats *stats, u64, u64);
-
-static inline void bch2_time_stats_update(struct bch2_time_stats *stats, u64 start)
-{
-       __bch2_time_stats_update(stats, start, local_clock());
-}
+int bch2_prt_task_backtrace(struct printbuf *, struct task_struct *, unsigned, gfp_t);
 
-static inline bool track_event_change(struct bch2_time_stats *stats,
-                                     u64 *start, bool v)
+static inline void prt_bdevname(struct printbuf *out, struct block_device *bdev)
 {
-       if (v != !!*start) {
-               if (!v) {
-                       bch2_time_stats_update(stats, *start);
-                       *start = 0;
-               } else {
-                       *start = local_clock() ?: 1;
-                       return true;
-               }
-       }
-
-       return false;
-}
+#ifdef __KERNEL__
+       prt_printf(out, "%pg", bdev);
 #else
-static inline void __bch2_time_stats_update(struct bch2_time_stats *stats, u64 start, u64 end) {}
-static inline void bch2_time_stats_update(struct bch2_time_stats *stats, u64 start) {}
-static inline bool track_event_change(struct bch2_time_stats *stats,
-                                     u64 *start, bool v)
-{
-       bool ret = v && !*start;
-       *start = v;
-       return ret;
-}
+       prt_str(out, bdev->name);
 #endif
+}
 
-void bch2_time_stats_to_text(struct printbuf *, struct bch2_time_stats *);
-
-void bch2_time_stats_exit(struct bch2_time_stats *);
-void bch2_time_stats_init(struct bch2_time_stats *);
+void bch2_time_stats_to_text(struct printbuf *, struct time_stats *);
 
 #define ewma_add(ewma, val, weight)                                    \
 ({                                                                     \
@@ -726,34 +629,6 @@ static inline void memset_u64s_tail(void *s, int c, unsigned bytes)
        memset(s + bytes, c, rem);
 }
 
-void sort_cmp_size(void *base, size_t num, size_t size,
-         int (*cmp_func)(const void *, const void *, size_t),
-         void (*swap_func)(void *, void *, size_t));
-
-/* just the memmove, doesn't update @_nr */
-#define __array_insert_item(_array, _nr, _pos)                         \
-       memmove(&(_array)[(_pos) + 1],                                  \
-               &(_array)[(_pos)],                                      \
-               sizeof((_array)[0]) * ((_nr) - (_pos)))
-
-#define array_insert_item(_array, _nr, _pos, _new_item)                        \
-do {                                                                   \
-       __array_insert_item(_array, _nr, _pos);                         \
-       (_nr)++;                                                        \
-       (_array)[(_pos)] = (_new_item);                                 \
-} while (0)
-
-#define array_remove_items(_array, _nr, _pos, _nr_to_remove)           \
-do {                                                                   \
-       (_nr) -= (_nr_to_remove);                                       \
-       memmove(&(_array)[(_pos)],                                      \
-               &(_array)[(_pos) + (_nr_to_remove)],                    \
-               sizeof((_array)[0]) * ((_nr) - (_pos)));                \
-} while (0)
-
-#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)
@@ -854,4 +729,14 @@ static inline int cmp_le32(__le32 l, __le32 r)
 
 #include <linux/uuid.h>
 
+#define QSTR(n) { { { .len = strlen(n) } }, .name = n }
+
+static inline bool qstr_eq(const struct qstr l, const struct qstr r)
+{
+       return l.len == r.len && !memcmp(l.name, r.name, l.len);
+}
+
+void bch2_darray_str_exit(darray_str *);
+int bch2_split_devs(const char *, darray_str *);
+
 #endif /* _BCACHEFS_UTIL_H */