Skip to content

Commit

Permalink
bcache: reduce cache_set devices iteration by devices_max_used
Browse files Browse the repository at this point in the history
Member devices of struct cache_set is used to reference all attached
bcache devices to this cache set. If it is treated as array of pointers,
size of devices[] is indicated by member nr_uuids of struct cache_set.

nr_uuids is calculated in drivers/md/super.c:bch_cache_set_alloc(),
	bucket_bytes(c) / sizeof(struct uuid_entry)
Bucket size is determined by user space tool "make-bcache", by default it
is 1024 sectors (defined in bcache-tools/make-bcache.c:main()). So default
nr_uuids value is 4096 from the above calculation.

Every time when bcache code iterates bcache devices of a cache set, all
the 4096 pointers are checked even only 1 bcache device is attached to the
cache set, that's a wast of time and unncessary.

This patch adds a member devices_max_used to struct cache_set. Its value
is 1 + the maximum used index of devices[] in a cache set. When iterating
all valid bcache devices of a cache set, use c->devices_max_used in
for-loop may reduce a lot of useless checking.

Personally, my motivation of this patch is not for performance, I use it
in bcache debugging, which helps me to narrow down the scape to check
valid bcached devices of a cache set.

Signed-off-by: Coly Li <[email protected]>
Reviewed-by: Michael Lyle <[email protected]>
Reviewed-by: Tang Junhui <[email protected]>
Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
Coly Li authored and axboe committed Jan 8, 2018
1 parent b40503e commit 2831231
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions drivers/md/bcache/bcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ struct cache_set {
int caches_loaded;

struct bcache_device **devices;
unsigned devices_max_used;
struct list_head cached_devs;
uint64_t cached_dev_sectors;
struct closure caching;
Expand Down
2 changes: 1 addition & 1 deletion drivers/md/bcache/btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -1679,7 +1679,7 @@ static void bch_btree_gc_finish(struct cache_set *c)

/* don't reclaim buckets to which writeback keys point */
rcu_read_lock();
for (i = 0; i < c->nr_uuids; i++) {
for (i = 0; i < c->devices_max_used; i++) {
struct bcache_device *d = c->devices[i];
struct cached_dev *dc;
struct keybuf_key *w, *n;
Expand Down
9 changes: 6 additions & 3 deletions drivers/md/bcache/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,9 @@ static void bcache_device_attach(struct bcache_device *d, struct cache_set *c,
d->c = c;
c->devices[id] = d;

if (id >= c->devices_max_used)
c->devices_max_used = id + 1;

closure_get(&c->caching);
}

Expand Down Expand Up @@ -1267,7 +1270,7 @@ static int flash_devs_run(struct cache_set *c)
struct uuid_entry *u;

for (u = c->uuids;
u < c->uuids + c->nr_uuids && !ret;
u < c->uuids + c->devices_max_used && !ret;
u++)
if (UUID_FLASH_ONLY(u))
ret = flash_dev_run(c, u);
Expand Down Expand Up @@ -1433,7 +1436,7 @@ static void __cache_set_unregister(struct closure *cl)

mutex_lock(&bch_register_lock);

for (i = 0; i < c->nr_uuids; i++)
for (i = 0; i < c->devices_max_used; i++)
if (c->devices[i]) {
if (!UUID_FLASH_ONLY(&c->uuids[i]) &&
test_bit(CACHE_SET_UNREGISTERING, &c->flags)) {
Expand Down Expand Up @@ -1496,7 +1499,7 @@ struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
c->bucket_bits = ilog2(sb->bucket_size);
c->block_bits = ilog2(sb->block_size);
c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry);

c->devices_max_used = 0;
c->btree_pages = bucket_pages(c);
if (c->btree_pages > BTREE_MAX_PAGES)
c->btree_pages = max_t(int, c->btree_pages / 4,
Expand Down
2 changes: 1 addition & 1 deletion drivers/md/bcache/writeback.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static inline uint64_t bcache_flash_devs_sectors_dirty(struct cache_set *c)

mutex_lock(&bch_register_lock);

for (i = 0; i < c->nr_uuids; i++) {
for (i = 0; i < c->devices_max_used; i++) {
struct bcache_device *d = c->devices[i];

if (!d || !UUID_FLASH_ONLY(&c->uuids[i]))
Expand Down

0 comments on commit 2831231

Please sign in to comment.