Skip to content

Commit 2831231

Browse files
Coly Liaxboe
authored andcommitted
bcache: reduce cache_set devices iteration by devices_max_used
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 <colyli@suse.de> Reviewed-by: Michael Lyle <mlyle@lyle.org> Reviewed-by: Tang Junhui <tang.junhui@zte.com.cn> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent b40503e commit 2831231

File tree

4 files changed

+9
-5
lines changed

4 files changed

+9
-5
lines changed

drivers/md/bcache/bcache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ struct cache_set {
497497
int caches_loaded;
498498

499499
struct bcache_device **devices;
500+
unsigned devices_max_used;
500501
struct list_head cached_devs;
501502
uint64_t cached_dev_sectors;
502503
struct closure caching;

drivers/md/bcache/btree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,7 @@ static void bch_btree_gc_finish(struct cache_set *c)
16791679

16801680
/* don't reclaim buckets to which writeback keys point */
16811681
rcu_read_lock();
1682-
for (i = 0; i < c->nr_uuids; i++) {
1682+
for (i = 0; i < c->devices_max_used; i++) {
16831683
struct bcache_device *d = c->devices[i];
16841684
struct cached_dev *dc;
16851685
struct keybuf_key *w, *n;

drivers/md/bcache/super.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,9 @@ static void bcache_device_attach(struct bcache_device *d, struct cache_set *c,
721721
d->c = c;
722722
c->devices[id] = d;
723723

724+
if (id >= c->devices_max_used)
725+
c->devices_max_used = id + 1;
726+
724727
closure_get(&c->caching);
725728
}
726729

@@ -1267,7 +1270,7 @@ static int flash_devs_run(struct cache_set *c)
12671270
struct uuid_entry *u;
12681271

12691272
for (u = c->uuids;
1270-
u < c->uuids + c->nr_uuids && !ret;
1273+
u < c->uuids + c->devices_max_used && !ret;
12711274
u++)
12721275
if (UUID_FLASH_ONLY(u))
12731276
ret = flash_dev_run(c, u);
@@ -1433,7 +1436,7 @@ static void __cache_set_unregister(struct closure *cl)
14331436

14341437
mutex_lock(&bch_register_lock);
14351438

1436-
for (i = 0; i < c->nr_uuids; i++)
1439+
for (i = 0; i < c->devices_max_used; i++)
14371440
if (c->devices[i]) {
14381441
if (!UUID_FLASH_ONLY(&c->uuids[i]) &&
14391442
test_bit(CACHE_SET_UNREGISTERING, &c->flags)) {
@@ -1496,7 +1499,7 @@ struct cache_set *bch_cache_set_alloc(struct cache_sb *sb)
14961499
c->bucket_bits = ilog2(sb->bucket_size);
14971500
c->block_bits = ilog2(sb->block_size);
14981501
c->nr_uuids = bucket_bytes(c) / sizeof(struct uuid_entry);
1499-
1502+
c->devices_max_used = 0;
15001503
c->btree_pages = bucket_pages(c);
15011504
if (c->btree_pages > BTREE_MAX_PAGES)
15021505
c->btree_pages = max_t(int, c->btree_pages / 4,

drivers/md/bcache/writeback.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static inline uint64_t bcache_flash_devs_sectors_dirty(struct cache_set *c)
2424

2525
mutex_lock(&bch_register_lock);
2626

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

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

0 commit comments

Comments
 (0)