bcachefs: Btree write buffer
This adds a new method of doing btree updates - a straight write buffer,
implemented as a flat fixed size array.
This is only useful when we don't need to read from the btree in order
to do the update, and when reading is infrequent - perfect for the LRU
btree.
This will make LRU btree updates fast enough that we'll be able to use
it for persistently indexing buckets by fragmentation, which will be a
massive boost to copygc performance.
Changes:
- A new btree_insert_type enum, for btree_insert_entries. Specifies
btree, btree key cache, or btree write buffer.
- bch2_trans_update_buffered(): updates via the btree write buffer
don't need a btree path, so we need a new update path.
- Transaction commit path changes:
The update to the btree write buffer both mutates global, and can
fail if there isn't currently room. Therefore we do all write buffer
updates in the transaction all at once, and also if it fails we have
to revert filesystem usage counter changes.
If there isn't room we flush the write buffer in the transaction
commit error path and retry.
- A new persistent option, for specifying the number of entries in the
write buffer.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2023-01-03 22:00:50 -07:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
#ifndef _BCACHEFS_BTREE_WRITE_BUFFER_H
|
|
|
|
#define _BCACHEFS_BTREE_WRITE_BUFFER_H
|
|
|
|
|
2023-11-02 15:57:19 -07:00
|
|
|
#include "bkey.h"
|
2023-12-27 09:33:21 -07:00
|
|
|
#include "disk_accounting.h"
|
2023-11-02 15:57:19 -07:00
|
|
|
|
|
|
|
static inline bool bch2_btree_write_buffer_should_flush(struct bch_fs *c)
|
|
|
|
{
|
|
|
|
struct btree_write_buffer *wb = &c->btree_write_buffer;
|
|
|
|
|
|
|
|
return wb->inc.keys.nr + wb->flushing.keys.nr > wb->inc.keys.size / 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool bch2_btree_write_buffer_must_wait(struct bch_fs *c)
|
|
|
|
{
|
|
|
|
struct btree_write_buffer *wb = &c->btree_write_buffer;
|
|
|
|
|
|
|
|
return wb->inc.keys.nr > wb->inc.keys.size * 3 / 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct btree_trans;
|
bcachefs: Btree write buffer
This adds a new method of doing btree updates - a straight write buffer,
implemented as a flat fixed size array.
This is only useful when we don't need to read from the btree in order
to do the update, and when reading is infrequent - perfect for the LRU
btree.
This will make LRU btree updates fast enough that we'll be able to use
it for persistently indexing buckets by fragmentation, which will be a
massive boost to copygc performance.
Changes:
- A new btree_insert_type enum, for btree_insert_entries. Specifies
btree, btree key cache, or btree write buffer.
- bch2_trans_update_buffered(): updates via the btree write buffer
don't need a btree path, so we need a new update path.
- Transaction commit path changes:
The update to the btree write buffer both mutates global, and can
fail if there isn't currently room. Therefore we do all write buffer
updates in the transaction all at once, and also if it fails we have
to revert filesystem usage counter changes.
If there isn't room we flush the write buffer in the transaction
commit error path and retry.
- A new persistent option, for specifying the number of entries in the
write buffer.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2023-01-03 22:00:50 -07:00
|
|
|
int bch2_btree_write_buffer_flush_sync(struct btree_trans *);
|
2024-11-07 19:48:33 -07:00
|
|
|
bool bch2_btree_write_buffer_flush_going_ro(struct bch_fs *);
|
2023-11-02 15:57:19 -07:00
|
|
|
int bch2_btree_write_buffer_flush_nocheck_rw(struct btree_trans *);
|
2023-11-02 17:36:00 -07:00
|
|
|
int bch2_btree_write_buffer_tryflush(struct btree_trans *);
|
bcachefs: Btree write buffer
This adds a new method of doing btree updates - a straight write buffer,
implemented as a flat fixed size array.
This is only useful when we don't need to read from the btree in order
to do the update, and when reading is infrequent - perfect for the LRU
btree.
This will make LRU btree updates fast enough that we'll be able to use
it for persistently indexing buckets by fragmentation, which will be a
massive boost to copygc performance.
Changes:
- A new btree_insert_type enum, for btree_insert_entries. Specifies
btree, btree key cache, or btree write buffer.
- bch2_trans_update_buffered(): updates via the btree write buffer
don't need a btree path, so we need a new update path.
- Transaction commit path changes:
The update to the btree write buffer both mutates global, and can
fail if there isn't currently room. Therefore we do all write buffer
updates in the transaction all at once, and also if it fails we have
to revert filesystem usage counter changes.
If there isn't room we flush the write buffer in the transaction
commit error path and retry.
- A new persistent option, for specifying the number of entries in the
write buffer.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2023-01-03 22:00:50 -07:00
|
|
|
|
2024-06-29 15:32:01 -07:00
|
|
|
struct bkey_buf;
|
|
|
|
int bch2_btree_write_buffer_maybe_flush(struct btree_trans *, struct bkey_s_c, struct bkey_buf *);
|
|
|
|
|
2023-11-02 15:57:19 -07:00
|
|
|
struct journal_keys_to_wb {
|
|
|
|
struct btree_write_buffer_keys *wb;
|
|
|
|
size_t room;
|
|
|
|
u64 seq;
|
|
|
|
};
|
|
|
|
|
2023-12-27 09:33:21 -07:00
|
|
|
static inline int wb_key_cmp(const void *_l, const void *_r)
|
|
|
|
{
|
|
|
|
const struct btree_write_buffered_key *l = _l;
|
|
|
|
const struct btree_write_buffered_key *r = _r;
|
|
|
|
|
|
|
|
return cmp_int(l->btree, r->btree) ?: bpos_cmp(l->k.k.p, r->k.k.p);
|
|
|
|
}
|
|
|
|
|
|
|
|
int bch2_accounting_key_to_wb_slowpath(struct bch_fs *,
|
|
|
|
enum btree_id, struct bkey_i_accounting *);
|
|
|
|
|
|
|
|
static inline int bch2_accounting_key_to_wb(struct bch_fs *c,
|
|
|
|
enum btree_id btree, struct bkey_i_accounting *k)
|
|
|
|
{
|
|
|
|
struct btree_write_buffer *wb = &c->btree_write_buffer;
|
|
|
|
struct btree_write_buffered_key search;
|
|
|
|
search.btree = btree;
|
|
|
|
search.k.k.p = k->k.p;
|
|
|
|
|
|
|
|
unsigned idx = eytzinger0_find(wb->accounting.data, wb->accounting.nr,
|
|
|
|
sizeof(wb->accounting.data[0]),
|
|
|
|
wb_key_cmp, &search);
|
|
|
|
|
|
|
|
if (idx >= wb->accounting.nr)
|
|
|
|
return bch2_accounting_key_to_wb_slowpath(c, btree, k);
|
|
|
|
|
|
|
|
struct bkey_i_accounting *dst = bkey_i_to_accounting(&wb->accounting.data[idx].k);
|
|
|
|
bch2_accounting_accumulate(dst, accounting_i_to_s_c(k));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-27 18:26:30 -07:00
|
|
|
int bch2_journal_key_to_wb_slowpath(struct bch_fs *,
|
2023-11-02 15:57:19 -07:00
|
|
|
struct journal_keys_to_wb *,
|
|
|
|
enum btree_id, struct bkey_i *);
|
|
|
|
|
2023-12-27 09:33:21 -07:00
|
|
|
static inline int __bch2_journal_key_to_wb(struct bch_fs *c,
|
2023-11-02 15:57:19 -07:00
|
|
|
struct journal_keys_to_wb *dst,
|
|
|
|
enum btree_id btree, struct bkey_i *k)
|
|
|
|
{
|
|
|
|
if (unlikely(!dst->room))
|
2023-12-27 18:26:30 -07:00
|
|
|
return bch2_journal_key_to_wb_slowpath(c, dst, btree, k);
|
2023-11-02 15:57:19 -07:00
|
|
|
|
|
|
|
struct btree_write_buffered_key *wb_k = &darray_top(dst->wb->keys);
|
|
|
|
wb_k->journal_seq = dst->seq;
|
|
|
|
wb_k->btree = btree;
|
|
|
|
bkey_copy(&wb_k->k, k);
|
|
|
|
dst->wb->keys.nr++;
|
|
|
|
dst->room--;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-27 09:33:21 -07:00
|
|
|
static inline int bch2_journal_key_to_wb(struct bch_fs *c,
|
|
|
|
struct journal_keys_to_wb *dst,
|
|
|
|
enum btree_id btree, struct bkey_i *k)
|
|
|
|
{
|
|
|
|
EBUG_ON(!dst->seq);
|
|
|
|
|
|
|
|
return k->k.type == KEY_TYPE_accounting
|
|
|
|
? bch2_accounting_key_to_wb(c, btree, bkey_i_to_accounting(k))
|
|
|
|
: __bch2_journal_key_to_wb(c, dst, btree, k);
|
|
|
|
}
|
|
|
|
|
2023-11-02 15:57:19 -07:00
|
|
|
void bch2_journal_keys_to_write_buffer_start(struct bch_fs *, struct journal_keys_to_wb *, u64);
|
2023-12-27 09:33:21 -07:00
|
|
|
int bch2_journal_keys_to_write_buffer_end(struct bch_fs *, struct journal_keys_to_wb *);
|
bcachefs: Btree write buffer
This adds a new method of doing btree updates - a straight write buffer,
implemented as a flat fixed size array.
This is only useful when we don't need to read from the btree in order
to do the update, and when reading is infrequent - perfect for the LRU
btree.
This will make LRU btree updates fast enough that we'll be able to use
it for persistently indexing buckets by fragmentation, which will be a
massive boost to copygc performance.
Changes:
- A new btree_insert_type enum, for btree_insert_entries. Specifies
btree, btree key cache, or btree write buffer.
- bch2_trans_update_buffered(): updates via the btree write buffer
don't need a btree path, so we need a new update path.
- Transaction commit path changes:
The update to the btree write buffer both mutates global, and can
fail if there isn't currently room. Therefore we do all write buffer
updates in the transaction all at once, and also if it fails we have
to revert filesystem usage counter changes.
If there isn't room we flush the write buffer in the transaction
commit error path and retry.
- A new persistent option, for specifying the number of entries in the
write buffer.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2023-01-03 22:00:50 -07:00
|
|
|
|
2023-11-02 15:57:19 -07:00
|
|
|
int bch2_btree_write_buffer_resize(struct bch_fs *, size_t);
|
bcachefs: Btree write buffer
This adds a new method of doing btree updates - a straight write buffer,
implemented as a flat fixed size array.
This is only useful when we don't need to read from the btree in order
to do the update, and when reading is infrequent - perfect for the LRU
btree.
This will make LRU btree updates fast enough that we'll be able to use
it for persistently indexing buckets by fragmentation, which will be a
massive boost to copygc performance.
Changes:
- A new btree_insert_type enum, for btree_insert_entries. Specifies
btree, btree key cache, or btree write buffer.
- bch2_trans_update_buffered(): updates via the btree write buffer
don't need a btree path, so we need a new update path.
- Transaction commit path changes:
The update to the btree write buffer both mutates global, and can
fail if there isn't currently room. Therefore we do all write buffer
updates in the transaction all at once, and also if it fails we have
to revert filesystem usage counter changes.
If there isn't room we flush the write buffer in the transaction
commit error path and retry.
- A new persistent option, for specifying the number of entries in the
write buffer.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2023-01-03 22:00:50 -07:00
|
|
|
void bch2_fs_btree_write_buffer_exit(struct bch_fs *);
|
|
|
|
int bch2_fs_btree_write_buffer_init(struct bch_fs *);
|
|
|
|
|
|
|
|
#endif /* _BCACHEFS_BTREE_WRITE_BUFFER_H */
|