]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/util.c
Update bcachefs sources to 25de2b00dc bcachefs: Change check for invalid key types
[bcachefs-tools-debian] / libbcachefs / util.c
index bb8a495e2290a847d222e9cc0f91d87830bc7bea..38886bf62c6697285c7f18212a8343ab25292a92 100644 (file)
@@ -240,36 +240,6 @@ bool bch2_is_zero(const void *_p, size_t n)
        return true;
 }
 
-static void bch2_quantiles_update(struct quantiles *q, u64 v)
-{
-       unsigned i = 0;
-
-       while (i < ARRAY_SIZE(q->entries)) {
-               struct quantile_entry *e = q->entries + i;
-
-               if (unlikely(!e->step)) {
-                       e->m = v;
-                       e->step = max_t(unsigned, v / 2, 1024);
-               } else if (e->m > v) {
-                       e->m = e->m >= e->step
-                               ? e->m - e->step
-                               : 0;
-               } else if (e->m < v) {
-                       e->m = e->m + e->step > e->m
-                               ? e->m + e->step
-                               : U32_MAX;
-               }
-
-               if ((e->m > v ? e->m - v : v - e->m) < e->step)
-                       e->step = max_t(unsigned, e->step / 2, 1);
-
-               if (v >= e->m)
-                       break;
-
-               i = eytzinger0_child(i, v > e->m);
-       }
-}
-
 void bch2_prt_u64_binary(struct printbuf *out, u64 v, unsigned nr_bits)
 {
        while (nr_bits)
@@ -292,45 +262,96 @@ void bch2_print_string_as_lines(const char *prefix, const char *lines)
                if (!*p)
                        break;
                lines = p + 1;
-               prefix = KERN_CONT;
        }
        console_unlock();
 }
 
-int bch2_prt_backtrace(struct printbuf *out, struct task_struct *task)
+int bch2_save_backtrace(bch_stacktrace *stack, struct task_struct *task)
 {
-       unsigned long entries[32];
-       unsigned i, nr_entries;
-       int ret;
+       unsigned nr_entries = 0;
+       int ret = 0;
 
-       ret = down_read_killable(&task->signal->exec_update_lock);
+       stack->nr = 0;
+       ret = darray_make_room(stack, 32);
        if (ret)
                return ret;
 
-       nr_entries = stack_trace_save_tsk(task, entries, ARRAY_SIZE(entries), 0);
-       for (i = 0; i < nr_entries; i++) {
-               prt_printf(out, "[<0>] %pB", (void *)entries[i]);
+       if (!down_read_trylock(&task->signal->exec_update_lock))
+               return -1;
+
+       do {
+               nr_entries = stack_trace_save_tsk(task, stack->data, stack->size, 0);
+       } while (nr_entries == stack->size &&
+                !(ret = darray_make_room(stack, stack->size * 2)));
+
+       stack->nr = nr_entries;
+       up_read(&task->signal->exec_update_lock);
+
+       return ret;
+}
+
+void bch2_prt_backtrace(struct printbuf *out, bch_stacktrace *stack)
+{
+       unsigned long *i;
+
+       darray_for_each(*stack, i) {
+               prt_printf(out, "[<0>] %pB", (void *) *i);
                prt_newline(out);
        }
+}
 
-       up_read(&task->signal->exec_update_lock);
-       return 0;
+int bch2_prt_task_backtrace(struct printbuf *out, struct task_struct *task)
+{
+       bch_stacktrace stack = { 0 };
+       int ret = bch2_save_backtrace(&stack, task);
+
+       bch2_prt_backtrace(out, &stack);
+       darray_exit(&stack);
+       return ret;
 }
 
 /* time stats: */
 
-static inline void bch2_time_stats_update_one(struct time_stats *stats,
+#ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
+static void bch2_quantiles_update(struct bch2_quantiles *q, u64 v)
+{
+       unsigned i = 0;
+
+       while (i < ARRAY_SIZE(q->entries)) {
+               struct bch2_quantile_entry *e = q->entries + i;
+
+               if (unlikely(!e->step)) {
+                       e->m = v;
+                       e->step = max_t(unsigned, v / 2, 1024);
+               } else if (e->m > v) {
+                       e->m = e->m >= e->step
+                               ? e->m - e->step
+                               : 0;
+               } else if (e->m < v) {
+                       e->m = e->m + e->step > e->m
+                               ? e->m + e->step
+                               : U32_MAX;
+               }
+
+               if ((e->m > v ? e->m - v : v - e->m) < e->step)
+                       e->step = max_t(unsigned, e->step / 2, 1);
+
+               if (v >= e->m)
+                       break;
+
+               i = eytzinger0_child(i, v > e->m);
+       }
+}
+
+static inline void bch2_time_stats_update_one(struct bch2_time_stats *stats,
                                              u64 start, u64 end)
 {
        u64 duration, freq;
 
        if (time_after64(end, start)) {
                duration = end - start;
-               stats->duration_stats = mean_and_variance_update_inlined(stats->duration_stats,
-                                                                duration);
-               stats->duration_stats_weighted = mean_and_variance_weighted_update(
-                       stats->duration_stats_weighted,
-                       duration);
+               mean_and_variance_update(&stats->duration_stats, duration);
+               mean_and_variance_weighted_update(&stats->duration_stats_weighted, duration);
                stats->max_duration = max(stats->max_duration, duration);
                stats->min_duration = min(stats->min_duration, duration);
                bch2_quantiles_update(&stats->quantiles, duration);
@@ -338,20 +359,18 @@ static inline void bch2_time_stats_update_one(struct time_stats *stats,
 
        if (time_after64(end, stats->last_event)) {
                freq = end - stats->last_event;
-               stats->freq_stats = mean_and_variance_update_inlined(stats->freq_stats, freq);
-               stats->freq_stats_weighted = mean_and_variance_weighted_update(
-                       stats->freq_stats_weighted,
-                       freq);
+               mean_and_variance_update(&stats->freq_stats, freq);
+               mean_and_variance_weighted_update(&stats->freq_stats_weighted, freq);
                stats->max_freq = max(stats->max_freq, freq);
                stats->min_freq = min(stats->min_freq, freq);
                stats->last_event = end;
        }
 }
 
-static noinline void bch2_time_stats_clear_buffer(struct time_stats *stats,
-                                                 struct time_stat_buffer *b)
+static noinline void bch2_time_stats_clear_buffer(struct bch2_time_stats *stats,
+                                                 struct bch2_time_stat_buffer *b)
 {
-       struct time_stat_buffer_entry *i;
+       struct bch2_time_stat_buffer_entry *i;
        unsigned long flags;
 
        spin_lock_irqsave(&stats->lock, flags);
@@ -364,7 +383,7 @@ static noinline void bch2_time_stats_clear_buffer(struct time_stats *stats,
        b->nr = 0;
 }
 
-void __bch2_time_stats_update(struct time_stats *stats, u64 start, u64 end)
+void __bch2_time_stats_update(struct bch2_time_stats *stats, u64 start, u64 end)
 {
        unsigned long flags;
 
@@ -379,17 +398,17 @@ void __bch2_time_stats_update(struct time_stats *stats, u64 start, u64 end)
                if (mean_and_variance_weighted_get_mean(stats->freq_stats_weighted) < 32 &&
                    stats->duration_stats.n > 1024)
                        stats->buffer =
-                               alloc_percpu_gfp(struct time_stat_buffer,
+                               alloc_percpu_gfp(struct bch2_time_stat_buffer,
                                                 GFP_ATOMIC);
                spin_unlock_irqrestore(&stats->lock, flags);
        } else {
-               struct time_stat_buffer *b;
+               struct bch2_time_stat_buffer *b;
 
                preempt_disable();
                b = this_cpu_ptr(stats->buffer);
 
                BUG_ON(b->nr >= ARRAY_SIZE(b->entries));
-               b->entries[b->nr++] = (struct time_stat_buffer_entry) {
+               b->entries[b->nr++] = (struct bch2_time_stat_buffer_entry) {
                        .start = start,
                        .end = end
                };
@@ -399,6 +418,7 @@ void __bch2_time_stats_update(struct time_stats *stats, u64 start, u64 end)
                preempt_enable();
        }
 }
+#endif
 
 static const struct time_unit {
        const char      *name;
@@ -408,8 +428,8 @@ static const struct time_unit {
        { "us",         NSEC_PER_USEC    },
        { "ms",         NSEC_PER_MSEC    },
        { "s",          NSEC_PER_SEC     },
-       { "m",          NSEC_PER_SEC * 60},
-       { "h",          NSEC_PER_SEC * 3600},
+       { "m",          (u64) NSEC_PER_SEC * 60},
+       { "h",          (u64) NSEC_PER_SEC * 3600},
        { "eon",        U64_MAX          },
 };
 
@@ -426,7 +446,14 @@ static const struct time_unit *pick_time_units(u64 ns)
        return u;
 }
 
-static void pr_time_units(struct printbuf *out, u64 ns)
+void bch2_pr_time_units(struct printbuf *out, u64 ns)
+{
+       const struct time_unit *u = pick_time_units(ns);
+
+       prt_printf(out, "%llu %s", div_u64(ns, u->nsecs), u->name);
+}
+
+static void bch2_pr_time_units_aligned(struct printbuf *out, u64 ns)
 {
        const struct time_unit *u = pick_time_units(ns);
 
@@ -441,11 +468,11 @@ static inline void pr_name_and_units(struct printbuf *out, const char *name, u64
 {
        prt_str(out, name);
        prt_tab(out);
-       pr_time_units(out, ns);
+       bch2_pr_time_units_aligned(out, ns);
        prt_newline(out);
 }
 
-void bch2_time_stats_to_text(struct printbuf *out, struct time_stats *stats)
+void bch2_time_stats_to_text(struct printbuf *out, struct bch2_time_stats *stats)
 {
        const struct time_unit *u;
        s64 f_mean = 0, d_mean = 0;
@@ -499,16 +526,16 @@ void bch2_time_stats_to_text(struct printbuf *out, struct time_stats *stats)
 
        prt_printf(out, "mean:");
        prt_tab(out);
-       pr_time_units(out, d_mean);
+       bch2_pr_time_units_aligned(out, d_mean);
        prt_tab(out);
-       pr_time_units(out, mean_and_variance_weighted_get_mean(stats->duration_stats_weighted));
+       bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_mean(stats->duration_stats_weighted));
        prt_newline(out);
 
        prt_printf(out, "stddev:");
        prt_tab(out);
-       pr_time_units(out, d_stddev);
+       bch2_pr_time_units_aligned(out, d_stddev);
        prt_tab(out);
-       pr_time_units(out, mean_and_variance_weighted_get_stddev(stats->duration_stats_weighted));
+       bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_stddev(stats->duration_stats_weighted));
 
        printbuf_indent_sub(out, 2);
        prt_newline(out);
@@ -522,16 +549,16 @@ void bch2_time_stats_to_text(struct printbuf *out, struct time_stats *stats)
 
        prt_printf(out, "mean:");
        prt_tab(out);
-       pr_time_units(out, f_mean);
+       bch2_pr_time_units_aligned(out, f_mean);
        prt_tab(out);
-       pr_time_units(out, mean_and_variance_weighted_get_mean(stats->freq_stats_weighted));
+       bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_mean(stats->freq_stats_weighted));
        prt_newline(out);
 
        prt_printf(out, "stddev:");
        prt_tab(out);
-       pr_time_units(out, f_stddev);
+       bch2_pr_time_units_aligned(out, f_stddev);
        prt_tab(out);
-       pr_time_units(out, mean_and_variance_weighted_get_stddev(stats->freq_stats_weighted));
+       bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_stddev(stats->freq_stats_weighted));
 
        printbuf_indent_sub(out, 2);
        prt_newline(out);
@@ -554,16 +581,16 @@ void bch2_time_stats_to_text(struct printbuf *out, struct time_stats *stats)
        }
 }
 
-void bch2_time_stats_exit(struct time_stats *stats)
+void bch2_time_stats_exit(struct bch2_time_stats *stats)
 {
        free_percpu(stats->buffer);
 }
 
-void bch2_time_stats_init(struct time_stats *stats)
+void bch2_time_stats_init(struct bch2_time_stats *stats)
 {
        memset(stats, 0, sizeof(*stats));
-       stats->duration_stats_weighted.w = 8;
-       stats->freq_stats_weighted.w = 8;
+       stats->duration_stats_weighted.weight = 8;
+       stats->freq_stats_weighted.weight = 8;
        stats->min_duration = U64_MAX;
        stats->min_freq = U64_MAX;
        spin_lock_init(&stats->lock);
@@ -732,7 +759,7 @@ void bch2_bio_map(struct bio *bio, void *base, size_t size)
 int bch2_bio_alloc_pages(struct bio *bio, size_t size, gfp_t gfp_mask)
 {
        while (size) {
-               struct page *page = alloc_page(gfp_mask);
+               struct page *page = alloc_pages(gfp_mask, 0);
                unsigned len = min_t(size_t, PAGE_SIZE, size);
 
                if (!page)