Skip to content

Commit

Permalink
9486 reduce memory used by device removal on fragmented pools
Browse files Browse the repository at this point in the history
Device removal allocates a new location for each allocated segment on
the disk that's being removed.  Each allocation results in one entry in
the mapping table, which maps from old location + length to new
location.  When a fragmented disk is removed, this can result in a large
number of mapping entries, and thus a large amount of memory consumed by
the mapping table.  In the worst real-world cases, we've seen around 1GB
of RAM per 1TB of storage removed.

We can improve on this situation by allocating larger segments, which
span across both allocated and free regions of the device being removed.
By including free regions in the allocation (and thus mapping), we
reduce the number of mapping entries.  For example, if we have a 4K
allocation followed by 1K free and then 4K allocated, we would allocate
4+1+4 = 9KB, and then move the entire region (including allocated and
free parts).  In this case we used one mapping where previously we would
have used two, but often the ratio is much higher (up to 20:1 in
real-world use).  We then need to mark the regions that were free on the
removing device as free in the new locations, and also obsolete in the
mapping entry.

This method preserves the fragmentation of the removing device, rather
than consolidating its allocated space into a small number of chunks
where possible.  But it results in drastic reduction of memory used by
the mapping table - around 20x in the most-fragmented cases.

In the most fragmented real-world cases, this reduces memory used by the
mapping from ~1GB to ~50MB of RAM per 1TB of storage removed.  Less
fragmented cases will typically also see around 50-100MB of RAM per 1TB
of storage.

External-issue: DLPX-57962
  • Loading branch information
ahrens committed Apr 17, 2018
1 parent 3f4c0b6 commit 07152e1
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 30 deletions.
23 changes: 21 additions & 2 deletions usr/src/uts/common/fs/zfs/range_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,14 @@ range_tree_remove(void *arg, uint64_t start, uint64_t size)
static range_seg_t *
range_tree_find_impl(range_tree_t *rt, uint64_t start, uint64_t size)
{
avl_index_t where;
range_seg_t rsearch;
uint64_t end = start + size;

VERIFY(size != 0);

rsearch.rs_start = start;
rsearch.rs_end = end;
return (avl_find(&rt->rt_root, &rsearch, &where));
return (avl_find(&rt->rt_root, &rsearch, NULL));
}

static range_seg_t *
Expand Down Expand Up @@ -407,3 +406,23 @@ range_tree_is_empty(range_tree_t *rt)
ASSERT(rt != NULL);
return (range_tree_space(rt) == 0);
}

uint64_t
range_tree_min(range_tree_t *rt)
{
range_seg_t *rs = avl_first(&rt->rt_root);
return (rs != NULL ? rs->rs_start : 0);
}

uint64_t
range_tree_max(range_tree_t *rt)
{
range_seg_t *rs = avl_last(&rt->rt_root);
return (rs != NULL ? rs->rs_end : 0);
}

uint64_t
range_tree_span(range_tree_t *rt)
{
return (range_tree_max(rt) - range_tree_min(rt));
}
3 changes: 3 additions & 0 deletions usr/src/uts/common/fs/zfs/sys/range_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ boolean_t range_tree_is_empty(range_tree_t *rt);
void range_tree_verify(range_tree_t *rt, uint64_t start, uint64_t size);
void range_tree_swap(range_tree_t **rtsrc, range_tree_t **rtdst);
void range_tree_stat_verify(range_tree_t *rt);
uint64_t range_tree_min(range_tree_t *rt);
uint64_t range_tree_max(range_tree_t *rt);
uint64_t range_tree_span(range_tree_t *rt);

void range_tree_add(void *arg, uint64_t start, uint64_t size);
void range_tree_remove(void *arg, uint64_t start, uint64_t size);
Expand Down
3 changes: 3 additions & 0 deletions usr/src/uts/common/fs/zfs/sys/vdev_removal.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ extern void spa_vdev_remove_suspend(spa_t *);
extern int spa_vdev_remove_cancel(spa_t *);
extern void spa_vdev_removal_destroy(spa_vdev_removal_t *svr);

extern int vdev_removal_max_span;
extern int zfs_remove_max_segment;

#ifdef __cplusplus
}
#endif
Expand Down
23 changes: 17 additions & 6 deletions usr/src/uts/common/fs/zfs/vdev_label.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,22 +390,33 @@ vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
* histograms.
*/
uint64_t seg_count = 0;
uint64_t to_alloc = vd->vdev_stat.vs_alloc;

/*
* There are the same number of allocated segments
* as free segments, so we will have at least one
* entry per free segment.
* entry per free segment. However, small free
* segments (smaller than vdev_removal_max_span)
* will be combined with adjacent allocated segments
* as a single mapping.
*/
for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
seg_count += vd->vdev_mg->mg_histogram[i];
if (1ULL << (i + 1) < vdev_removal_max_span) {
to_alloc +=
vd->vdev_mg->mg_histogram[i] <<
i + 1;
} else {
seg_count +=
vd->vdev_mg->mg_histogram[i];
}
}

/*
* The maximum length of a mapping is SPA_MAXBLOCKSIZE,
* so we need at least one entry per SPA_MAXBLOCKSIZE
* of allocated data.
* The maximum length of a mapping is
* zfs_remove_max_segment, so we need at least one entry
* per zfs_remove_max_segment of allocated data.
*/
seg_count += vd->vdev_stat.vs_alloc / SPA_MAXBLOCKSIZE;
seg_count += to_alloc / zfs_remove_max_segment;

fnvlist_add_uint64(nv, ZPOOL_CONFIG_INDIRECT_SIZE,
seg_count *
Expand Down
Loading

0 comments on commit 07152e1

Please sign in to comment.