Skip to content

Commit 955ba7b

Browse files
author
Kent Overstreet
committed
bcachefs: bch_dev_usage_full
All the fastpaths that need device usage don't need the sector totals or fragmentation, just bucket counts. Split bch_dev_usage up into two different versions, the normal one with just bucket counts. This is also a stack usage improvement, since we have a bch_dev_usage on the stack in the allocation path. Signed-off-by: Kent Overstreet <[email protected]>
1 parent 9180ad2 commit 955ba7b

File tree

8 files changed

+51
-25
lines changed

8 files changed

+51
-25
lines changed

fs/bcachefs/alloc_background.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,11 @@ static inline u64 should_invalidate_buckets(struct bch_dev *ca,
321321
{
322322
u64 want_free = ca->mi.nbuckets >> 7;
323323
u64 free = max_t(s64, 0,
324-
u.d[BCH_DATA_free].buckets
325-
+ u.d[BCH_DATA_need_discard].buckets
324+
u.buckets[BCH_DATA_free]
325+
+ u.buckets[BCH_DATA_need_discard]
326326
- bch2_dev_buckets_reserved(ca, BCH_WATERMARK_stripe));
327327

328-
return clamp_t(s64, want_free - free, 0, u.d[BCH_DATA_cached].buckets);
328+
return clamp_t(s64, want_free - free, 0, u.buckets[BCH_DATA_cached]);
329329
}
330330

331331
void bch2_dev_do_invalidates(struct bch_dev *);

fs/bcachefs/alloc_foreground.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ static noinline void trace_bucket_alloc2(struct bch_fs *c, struct bch_dev *ca,
469469
prt_printf(&buf, "watermark\t%s\n", bch2_watermarks[watermark]);
470470
prt_printf(&buf, "data type\t%s\n", __bch2_data_types[data_type]);
471471
prt_printf(&buf, "blocking\t%u\n", cl != NULL);
472-
prt_printf(&buf, "free\t%llu\n", usage->d[BCH_DATA_free].buckets);
472+
prt_printf(&buf, "free\t%llu\n", usage->buckets[BCH_DATA_free]);
473473
prt_printf(&buf, "avail\t%llu\n", dev_buckets_free(ca, *usage, watermark));
474474
prt_printf(&buf, "copygc_wait\t%lu/%lli\n",
475475
bch2_copygc_wait_amount(c),
@@ -524,10 +524,10 @@ static struct open_bucket *bch2_bucket_alloc_trans(struct btree_trans *trans,
524524
bch2_dev_usage_read_fast(ca, usage);
525525
avail = dev_buckets_free(ca, *usage, watermark);
526526

527-
if (usage->d[BCH_DATA_need_discard].buckets > avail)
527+
if (usage->buckets[BCH_DATA_need_discard] > avail)
528528
bch2_dev_do_discards(ca);
529529

530-
if (usage->d[BCH_DATA_need_gc_gens].buckets > avail)
530+
if (usage->buckets[BCH_DATA_need_gc_gens] > avail)
531531
bch2_gc_gens_async(c);
532532

533533
if (should_invalidate_buckets(ca, *usage))
@@ -1669,7 +1669,7 @@ void bch2_fs_alloc_debug_to_text(struct printbuf *out, struct bch_fs *c)
16691669
void bch2_dev_alloc_debug_to_text(struct printbuf *out, struct bch_dev *ca)
16701670
{
16711671
struct bch_fs *c = ca->fs;
1672-
struct bch_dev_usage stats = bch2_dev_usage_read(ca);
1672+
struct bch_dev_usage_full stats = bch2_dev_usage_full_read(ca);
16731673
unsigned nr[BCH_DATA_NR];
16741674

16751675
memset(nr, 0, sizeof(nr));
@@ -1692,7 +1692,8 @@ void bch2_dev_alloc_debug_to_text(struct printbuf *out, struct bch_dev *ca)
16921692
printbuf_tabstop_push(out, 16);
16931693

16941694
prt_printf(out, "open buckets\t%i\r\n", ca->nr_open_buckets);
1695-
prt_printf(out, "buckets to invalidate\t%llu\r\n", should_invalidate_buckets(ca, stats));
1695+
prt_printf(out, "buckets to invalidate\t%llu\r\n",
1696+
should_invalidate_buckets(ca, bch2_dev_usage_read(ca)));
16961697
}
16971698

16981699
static noinline void bch2_print_allocator_stuck(struct bch_fs *c)

fs/bcachefs/bcachefs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,8 @@ struct bch_dev {
562562
unsigned long *bucket_backpointer_mismatches;
563563
unsigned long *bucket_backpointer_empty;
564564

565-
struct bch_dev_usage __percpu *usage;
565+
struct bch_dev_usage_full __percpu
566+
*usage;
566567

567568
/* Allocator: */
568569
u64 alloc_cursor[3];

fs/bcachefs/buckets.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
#include <linux/preempt.h>
3030

3131
void bch2_dev_usage_read_fast(struct bch_dev *ca, struct bch_dev_usage *usage)
32+
{
33+
for (unsigned i = 0; i < BCH_DATA_NR; i++)
34+
usage->buckets[i] = percpu_u64_get(&ca->usage->d[i].buckets);
35+
}
36+
37+
void bch2_dev_usage_full_read_fast(struct bch_dev *ca, struct bch_dev_usage_full *usage)
3238
{
3339
memset(usage, 0, sizeof(*usage));
3440
acc_u64s_percpu((u64 *) usage, (u64 __percpu *) ca->usage, dev_usage_u64s());
@@ -75,7 +81,7 @@ bch2_fs_usage_read_short(struct bch_fs *c)
7581

7682
void bch2_dev_usage_to_text(struct printbuf *out,
7783
struct bch_dev *ca,
78-
struct bch_dev_usage *usage)
84+
struct bch_dev_usage_full *usage)
7985
{
8086
if (out->nr_tabstops < 5) {
8187
printbuf_tabstops_reset(out);
@@ -1331,7 +1337,7 @@ void bch2_dev_buckets_free(struct bch_dev *ca)
13311337

13321338
int bch2_dev_buckets_alloc(struct bch_fs *c, struct bch_dev *ca)
13331339
{
1334-
ca->usage = alloc_percpu(struct bch_dev_usage);
1340+
ca->usage = alloc_percpu(struct bch_dev_usage_full);
13351341
if (!ca->usage)
13361342
return -BCH_ERR_ENOMEM_usage_init;
13371343

fs/bcachefs/buckets.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,16 @@ static inline struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *ca)
172172
return ret;
173173
}
174174

175-
void bch2_dev_usage_to_text(struct printbuf *, struct bch_dev *, struct bch_dev_usage *);
175+
void bch2_dev_usage_full_read_fast(struct bch_dev *, struct bch_dev_usage_full *);
176+
static inline struct bch_dev_usage_full bch2_dev_usage_full_read(struct bch_dev *ca)
177+
{
178+
struct bch_dev_usage_full ret;
179+
180+
bch2_dev_usage_full_read_fast(ca, &ret);
181+
return ret;
182+
}
183+
184+
void bch2_dev_usage_to_text(struct printbuf *, struct bch_dev *, struct bch_dev_usage_full *);
176185

177186
static inline u64 bch2_dev_buckets_reserved(struct bch_dev *ca, enum bch_watermark watermark)
178187
{
@@ -207,7 +216,7 @@ static inline u64 dev_buckets_free(struct bch_dev *ca,
207216
enum bch_watermark watermark)
208217
{
209218
return max_t(s64, 0,
210-
usage.d[BCH_DATA_free].buckets -
219+
usage.buckets[BCH_DATA_free]-
211220
ca->nr_open_buckets -
212221
bch2_dev_buckets_reserved(ca, watermark));
213222
}
@@ -217,10 +226,10 @@ static inline u64 __dev_buckets_available(struct bch_dev *ca,
217226
enum bch_watermark watermark)
218227
{
219228
return max_t(s64, 0,
220-
usage.d[BCH_DATA_free].buckets
221-
+ usage.d[BCH_DATA_cached].buckets
222-
+ usage.d[BCH_DATA_need_gc_gens].buckets
223-
+ usage.d[BCH_DATA_need_discard].buckets
229+
usage.buckets[BCH_DATA_free]
230+
+ usage.buckets[BCH_DATA_cached]
231+
+ usage.buckets[BCH_DATA_need_gc_gens]
232+
+ usage.buckets[BCH_DATA_need_discard]
224233
- ca->nr_open_buckets
225234
- bch2_dev_buckets_reserved(ca, watermark));
226235
}

fs/bcachefs/buckets_types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ struct bucket_gens {
5454
u8 b[] __counted_by(nbuckets);
5555
};
5656

57+
/* Only info on bucket countns: */
5758
struct bch_dev_usage {
59+
u64 buckets[BCH_DATA_NR];
60+
};
61+
62+
struct bch_dev_usage_full {
5863
struct bch_dev_usage_type {
5964
u64 buckets;
6065
u64 sectors; /* _compressed_ sectors: */

fs/bcachefs/chardev.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ static ssize_t bch2_data_job_read(struct file *file, char __user *buf,
350350
if (ctx->arg.op == BCH_DATA_OP_scrub) {
351351
struct bch_dev *ca = bch2_dev_tryget(c, ctx->arg.scrub.dev);
352352
if (ca) {
353-
struct bch_dev_usage u;
354-
bch2_dev_usage_read_fast(ca, &u);
353+
struct bch_dev_usage_full u;
354+
bch2_dev_usage_full_read_fast(ca, &u);
355355
for (unsigned i = BCH_DATA_btree; i < ARRAY_SIZE(u.d); i++)
356356
if (ctx->arg.scrub.data_types & BIT(i))
357357
e.p.sectors_total += u.d[i].sectors;
@@ -473,7 +473,7 @@ static long bch2_ioctl_dev_usage(struct bch_fs *c,
473473
struct bch_ioctl_dev_usage __user *user_arg)
474474
{
475475
struct bch_ioctl_dev_usage arg;
476-
struct bch_dev_usage src;
476+
struct bch_dev_usage_full src;
477477
struct bch_dev *ca;
478478
unsigned i;
479479

@@ -493,7 +493,7 @@ static long bch2_ioctl_dev_usage(struct bch_fs *c,
493493
if (IS_ERR(ca))
494494
return PTR_ERR(ca);
495495

496-
src = bch2_dev_usage_read(ca);
496+
src = bch2_dev_usage_full_read(ca);
497497

498498
arg.state = ca->mi.state;
499499
arg.bucket_size = ca->mi.bucket_size;
@@ -514,7 +514,7 @@ static long bch2_ioctl_dev_usage_v2(struct bch_fs *c,
514514
struct bch_ioctl_dev_usage_v2 __user *user_arg)
515515
{
516516
struct bch_ioctl_dev_usage_v2 arg;
517-
struct bch_dev_usage src;
517+
struct bch_dev_usage_full src;
518518
struct bch_dev *ca;
519519
int ret = 0;
520520

@@ -534,7 +534,7 @@ static long bch2_ioctl_dev_usage_v2(struct bch_fs *c,
534534
if (IS_ERR(ca))
535535
return PTR_ERR(ca);
536536

537-
src = bch2_dev_usage_read(ca);
537+
src = bch2_dev_usage_full_read(ca);
538538

539539
arg.state = ca->mi.state;
540540
arg.bucket_size = ca->mi.bucket_size;

fs/bcachefs/movinggc.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,19 @@ unsigned long bch2_copygc_wait_amount(struct bch_fs *c)
280280
s64 wait = S64_MAX, fragmented_allowed, fragmented;
281281

282282
for_each_rw_member(c, ca) {
283-
struct bch_dev_usage usage = bch2_dev_usage_read(ca);
283+
struct bch_dev_usage_full usage_full = bch2_dev_usage_full_read(ca);
284+
struct bch_dev_usage usage;
285+
286+
for (unsigned i = 0; i < BCH_DATA_NR; i++)
287+
usage.buckets[i] = usage_full.d[i].buckets;
284288

285289
fragmented_allowed = ((__dev_buckets_available(ca, usage, BCH_WATERMARK_stripe) *
286290
ca->mi.bucket_size) >> 1);
287291
fragmented = 0;
288292

289293
for (unsigned i = 0; i < BCH_DATA_NR; i++)
290294
if (data_type_movable(i))
291-
fragmented += usage.d[i].fragmented;
295+
fragmented += usage_full.d[i].fragmented;
292296

293297
wait = min(wait, max(0LL, fragmented_allowed - fragmented));
294298
}

0 commit comments

Comments
 (0)