Skip to content

Commit 1b50749

Browse files
authored
Optimize allocation throttling
Remove mc_lock use from metaslab_class_throttle_*(). The math there is based on refcounts and so atomic, so the only race possible there is between zfs_refcount_count() and zfs_refcount_add(). But in most cases metaslab_class_throttle_reserve() is called with the allocator lock held, which covers the race. In cases where the lock is not held, GANG_ALLOCATION() or METASLAB_MUST_RESERVE are set, and so we do not use zfs_refcount_count(). And even if we assume some other non-existing scenario, the worst that may happen from this race is few more I/Os get to allocation earlier, that is not a problem. Move locks and data of different allocators into different cache lines to avoid false sharing. Group spa_alloc_* arrays together into single array of aligned struct spa_alloc spa_allocs. Align struct metaslab_class_allocator. Reviewed-by: Paul Dagnelie <pcd@delphix.com> Reviewed-by: Ryan Moeller <ryan@iXsystems.com> Reviewed-by: Don Brady <don.brady@delphix.com> Signed-off-by: Alexander Motin <mav@FreeBSD.org> Sponsored-By: iXsystems, Inc. Closes openzfs#12314
1 parent bc93935 commit 1b50749

File tree

6 files changed

+45
-58
lines changed

6 files changed

+45
-58
lines changed

include/sys/metaslab_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ typedef struct metaslab_class_allocator {
157157
*/
158158
uint64_t mca_alloc_max_slots;
159159
zfs_refcount_t mca_alloc_slots;
160-
} metaslab_class_allocator_t;
160+
} ____cacheline_aligned metaslab_class_allocator_t;
161161

162162
/*
163163
* A metaslab class encompasses a category of allocatable top-level vdevs.

include/sys/spa_impl.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@
5757
extern "C" {
5858
#endif
5959

60+
typedef struct spa_alloc {
61+
kmutex_t spaa_lock;
62+
avl_tree_t spaa_tree;
63+
} ____cacheline_aligned spa_alloc_t;
64+
6065
typedef struct spa_error_entry {
6166
zbookmark_phys_t se_bookmark;
6267
char *se_name;
@@ -250,13 +255,11 @@ struct spa {
250255
list_t spa_config_dirty_list; /* vdevs with dirty config */
251256
list_t spa_state_dirty_list; /* vdevs with dirty state */
252257
/*
253-
* spa_alloc_locks and spa_alloc_trees are arrays, whose lengths are
254-
* stored in spa_alloc_count. There is one tree and one lock for each
255-
* allocator, to help improve allocation performance in write-heavy
256-
* workloads.
258+
* spa_allocs is an array, whose lengths is stored in spa_alloc_count.
259+
* There is one tree and one lock for each allocator, to help improve
260+
* allocation performance in write-heavy workloads.
257261
*/
258-
kmutex_t *spa_alloc_locks;
259-
avl_tree_t *spa_alloc_trees;
262+
spa_alloc_t *spa_allocs;
260263
int spa_alloc_count;
261264

262265
spa_aux_vdev_t spa_spares; /* hot spares */

module/zfs/metaslab.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5611,31 +5611,21 @@ metaslab_class_throttle_reserve(metaslab_class_t *mc, int slots, int allocator,
56115611
zio_t *zio, int flags)
56125612
{
56135613
metaslab_class_allocator_t *mca = &mc->mc_allocator[allocator];
5614-
uint64_t available_slots = 0;
5615-
boolean_t slot_reserved = B_FALSE;
56165614
uint64_t max = mca->mca_alloc_max_slots;
56175615

56185616
ASSERT(mc->mc_alloc_throttle_enabled);
5619-
mutex_enter(&mc->mc_lock);
5620-
5621-
uint64_t reserved_slots = zfs_refcount_count(&mca->mca_alloc_slots);
5622-
if (reserved_slots < max)
5623-
available_slots = max - reserved_slots;
5624-
5625-
if (slots <= available_slots || GANG_ALLOCATION(flags) ||
5626-
flags & METASLAB_MUST_RESERVE) {
5617+
if (GANG_ALLOCATION(flags) || (flags & METASLAB_MUST_RESERVE) ||
5618+
zfs_refcount_count(&mca->mca_alloc_slots) + slots <= max) {
56275619
/*
56285620
* We reserve the slots individually so that we can unreserve
56295621
* them individually when an I/O completes.
56305622
*/
56315623
for (int d = 0; d < slots; d++)
56325624
zfs_refcount_add(&mca->mca_alloc_slots, zio);
56335625
zio->io_flags |= ZIO_FLAG_IO_ALLOCATING;
5634-
slot_reserved = B_TRUE;
5626+
return (B_TRUE);
56355627
}
5636-
5637-
mutex_exit(&mc->mc_lock);
5638-
return (slot_reserved);
5628+
return (B_FALSE);
56395629
}
56405630

56415631
void
@@ -5645,10 +5635,8 @@ metaslab_class_throttle_unreserve(metaslab_class_t *mc, int slots,
56455635
metaslab_class_allocator_t *mca = &mc->mc_allocator[allocator];
56465636

56475637
ASSERT(mc->mc_alloc_throttle_enabled);
5648-
mutex_enter(&mc->mc_lock);
56495638
for (int d = 0; d < slots; d++)
56505639
zfs_refcount_remove(&mca->mca_alloc_slots, zio);
5651-
mutex_exit(&mc->mc_lock);
56525640
}
56535641

56545642
static int

module/zfs/spa.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9197,9 +9197,9 @@ spa_sync(spa_t *spa, uint64_t txg)
91979197
spa->spa_sync_pass = 0;
91989198

91999199
for (int i = 0; i < spa->spa_alloc_count; i++) {
9200-
mutex_enter(&spa->spa_alloc_locks[i]);
9201-
VERIFY0(avl_numnodes(&spa->spa_alloc_trees[i]));
9202-
mutex_exit(&spa->spa_alloc_locks[i]);
9200+
mutex_enter(&spa->spa_allocs[i].spaa_lock);
9201+
VERIFY0(avl_numnodes(&spa->spa_allocs[i].spaa_tree));
9202+
mutex_exit(&spa->spa_allocs[i].spaa_lock);
92039203
}
92049204

92059205
/*
@@ -9309,9 +9309,9 @@ spa_sync(spa_t *spa, uint64_t txg)
93099309
dsl_pool_sync_done(dp, txg);
93109310

93119311
for (int i = 0; i < spa->spa_alloc_count; i++) {
9312-
mutex_enter(&spa->spa_alloc_locks[i]);
9313-
VERIFY0(avl_numnodes(&spa->spa_alloc_trees[i]));
9314-
mutex_exit(&spa->spa_alloc_locks[i]);
9312+
mutex_enter(&spa->spa_allocs[i].spaa_lock);
9313+
VERIFY0(avl_numnodes(&spa->spa_allocs[i].spaa_tree));
9314+
mutex_exit(&spa->spa_allocs[i].spaa_lock);
93159315
}
93169316

93179317
/*

module/zfs/spa_misc.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -700,13 +700,12 @@ spa_add(const char *name, nvlist_t *config, const char *altroot)
700700
spa->spa_root = spa_strdup(altroot);
701701

702702
spa->spa_alloc_count = spa_allocators;
703-
spa->spa_alloc_locks = kmem_zalloc(spa->spa_alloc_count *
704-
sizeof (kmutex_t), KM_SLEEP);
705-
spa->spa_alloc_trees = kmem_zalloc(spa->spa_alloc_count *
706-
sizeof (avl_tree_t), KM_SLEEP);
703+
spa->spa_allocs = kmem_zalloc(spa->spa_alloc_count *
704+
sizeof (spa_alloc_t), KM_SLEEP);
707705
for (int i = 0; i < spa->spa_alloc_count; i++) {
708-
mutex_init(&spa->spa_alloc_locks[i], NULL, MUTEX_DEFAULT, NULL);
709-
avl_create(&spa->spa_alloc_trees[i], zio_bookmark_compare,
706+
mutex_init(&spa->spa_allocs[i].spaa_lock, NULL, MUTEX_DEFAULT,
707+
NULL);
708+
avl_create(&spa->spa_allocs[i].spaa_tree, zio_bookmark_compare,
710709
sizeof (zio_t), offsetof(zio_t, io_alloc_node));
711710
}
712711
avl_create(&spa->spa_metaslabs_by_flushed, metaslab_sort_by_flushed,
@@ -799,13 +798,11 @@ spa_remove(spa_t *spa)
799798
}
800799

801800
for (int i = 0; i < spa->spa_alloc_count; i++) {
802-
avl_destroy(&spa->spa_alloc_trees[i]);
803-
mutex_destroy(&spa->spa_alloc_locks[i]);
801+
avl_destroy(&spa->spa_allocs[i].spaa_tree);
802+
mutex_destroy(&spa->spa_allocs[i].spaa_lock);
804803
}
805-
kmem_free(spa->spa_alloc_locks, spa->spa_alloc_count *
806-
sizeof (kmutex_t));
807-
kmem_free(spa->spa_alloc_trees, spa->spa_alloc_count *
808-
sizeof (avl_tree_t));
804+
kmem_free(spa->spa_allocs, spa->spa_alloc_count *
805+
sizeof (spa_alloc_t));
809806

810807
avl_destroy(&spa->spa_metaslabs_by_flushed);
811808
avl_destroy(&spa->spa_sm_logs_by_txg);

module/zfs/zio.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -877,8 +877,7 @@ zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
877877
zio->io_bookmark = *zb;
878878

879879
if (pio != NULL) {
880-
if (zio->io_metaslab_class == NULL)
881-
zio->io_metaslab_class = pio->io_metaslab_class;
880+
zio->io_metaslab_class = pio->io_metaslab_class;
882881
if (zio->io_logical == NULL)
883882
zio->io_logical = pio->io_logical;
884883
if (zio->io_child_type == ZIO_CHILD_GANG)
@@ -3380,9 +3379,9 @@ zio_io_to_allocate(spa_t *spa, int allocator)
33803379
{
33813380
zio_t *zio;
33823381

3383-
ASSERT(MUTEX_HELD(&spa->spa_alloc_locks[allocator]));
3382+
ASSERT(MUTEX_HELD(&spa->spa_allocs[allocator].spaa_lock));
33843383

3385-
zio = avl_first(&spa->spa_alloc_trees[allocator]);
3384+
zio = avl_first(&spa->spa_allocs[allocator].spaa_tree);
33863385
if (zio == NULL)
33873386
return (NULL);
33883387

@@ -3394,11 +3393,11 @@ zio_io_to_allocate(spa_t *spa, int allocator)
33943393
*/
33953394
ASSERT3U(zio->io_allocator, ==, allocator);
33963395
if (!metaslab_class_throttle_reserve(zio->io_metaslab_class,
3397-
zio->io_prop.zp_copies, zio->io_allocator, zio, 0)) {
3396+
zio->io_prop.zp_copies, allocator, zio, 0)) {
33983397
return (NULL);
33993398
}
34003399

3401-
avl_remove(&spa->spa_alloc_trees[allocator], zio);
3400+
avl_remove(&spa->spa_allocs[allocator].spaa_tree, zio);
34023401
ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE);
34033402

34043403
return (zio);
@@ -3422,8 +3421,8 @@ zio_dva_throttle(zio_t *zio)
34223421
return (zio);
34233422
}
34243423

3424+
ASSERT(zio->io_type == ZIO_TYPE_WRITE);
34253425
ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
3426-
34273426
ASSERT3U(zio->io_queued_timestamp, >, 0);
34283427
ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE);
34293428

@@ -3435,14 +3434,14 @@ zio_dva_throttle(zio_t *zio)
34353434
* into 2^20 block regions, and then hash based on the objset, object,
34363435
* level, and region to accomplish both of these goals.
34373436
*/
3438-
zio->io_allocator = cityhash4(bm->zb_objset, bm->zb_object,
3437+
int allocator = (uint_t)cityhash4(bm->zb_objset, bm->zb_object,
34393438
bm->zb_level, bm->zb_blkid >> 20) % spa->spa_alloc_count;
3440-
mutex_enter(&spa->spa_alloc_locks[zio->io_allocator]);
3441-
ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3439+
zio->io_allocator = allocator;
34423440
zio->io_metaslab_class = mc;
3443-
avl_add(&spa->spa_alloc_trees[zio->io_allocator], zio);
3444-
nio = zio_io_to_allocate(spa, zio->io_allocator);
3445-
mutex_exit(&spa->spa_alloc_locks[zio->io_allocator]);
3441+
mutex_enter(&spa->spa_allocs[allocator].spaa_lock);
3442+
avl_add(&spa->spa_allocs[allocator].spaa_tree, zio);
3443+
nio = zio_io_to_allocate(spa, allocator);
3444+
mutex_exit(&spa->spa_allocs[allocator].spaa_lock);
34463445
return (nio);
34473446
}
34483447

@@ -3451,9 +3450,9 @@ zio_allocate_dispatch(spa_t *spa, int allocator)
34513450
{
34523451
zio_t *zio;
34533452

3454-
mutex_enter(&spa->spa_alloc_locks[allocator]);
3453+
mutex_enter(&spa->spa_allocs[allocator].spaa_lock);
34553454
zio = zio_io_to_allocate(spa, allocator);
3456-
mutex_exit(&spa->spa_alloc_locks[allocator]);
3455+
mutex_exit(&spa->spa_allocs[allocator].spaa_lock);
34573456
if (zio == NULL)
34583457
return;
34593458

@@ -3643,8 +3642,8 @@ zio_alloc_zil(spa_t *spa, objset_t *os, uint64_t txg, blkptr_t *new_bp,
36433642
* some parallelism.
36443643
*/
36453644
int flags = METASLAB_FASTWRITE | METASLAB_ZIL;
3646-
int allocator = cityhash4(0, 0, 0, os->os_dsl_dataset->ds_object) %
3647-
spa->spa_alloc_count;
3645+
int allocator = (uint_t)cityhash4(0, 0, 0,
3646+
os->os_dsl_dataset->ds_object) % spa->spa_alloc_count;
36483647
error = metaslab_alloc(spa, spa_log_class(spa), size, new_bp, 1,
36493648
txg, NULL, flags, &io_alloc_list, NULL, allocator);
36503649
*slog = (error == 0);

0 commit comments

Comments
 (0)