mirror of
https://git.proxmox.com/git/mirror_zfs.git
synced 2024-12-28 03:49:38 +03:00
93e28d661e
= Motivation At Delphix we've seen a lot of customer systems where fragmentation is over 75% and random writes take a performance hit because a lot of time is spend on I/Os that update on-disk space accounting metadata. Specifically, we seen cases where 20% to 40% of sync time is spend after sync pass 1 and ~30% of the I/Os on the system is spent updating spacemaps. The problem is that these pools have existed long enough that we've touched almost every metaslab at least once, and random writes scatter frees across all metaslabs every TXG, thus appending to their spacemaps and resulting in many I/Os. To give an example, assuming that every VDEV has 200 metaslabs and our writes fit within a single spacemap block (generally 4K) we have 200 I/Os. Then if we assume 2 levels of indirection, we need 400 additional I/Os and since we are talking about metadata for which we keep 2 extra copies for redundancy we need to triple that number, leading to a total of 1800 I/Os per VDEV every TXG. We could try and decrease the number of metaslabs so we have less I/Os per TXG but then each metaslab would cover a wider range on disk and thus would take more time to be loaded in memory from disk. In addition, after it's loaded, it's range tree would consume more memory. Another idea would be to just increase the spacemap block size which would allow us to fit more entries within an I/O block resulting in fewer I/Os per metaslab and a speedup in loading time. The problem is still that we don't deal with the number of I/Os going up as the number of metaslabs is increasing and the fact is that we generally write a lot to a few metaslabs and a little to the rest of them. Thus, just increasing the block size would actually waste bandwidth because we won't be utilizing our bigger block size. = About this patch This patch introduces the Log Spacemap project which provides the solution to the above problem while taking into account all the aforementioned tradeoffs. The details on how it achieves that can be found in the references sections below and in the code (see Big Theory Statement in spa_log_spacemap.c). Even though the change is fairly constraint within the metaslab and lower-level SPA codepaths, there is a side-change that is user-facing. The change is that VDEV IDs from VDEV holes will no longer be reused. To give some background and reasoning for this, when a log device is removed and its VDEV structure was replaced with a hole (or was compacted; if at the end of the vdev array), its vdev_id could be reused by devices added after that. Now with the pool-wide space maps recording the vdev ID, this behavior can cause problems (e.g. is this entry referring to a segment in the new vdev or the removed log?). Thus, to simplify things the ID reuse behavior is gone and now vdev IDs for top-level vdevs are truly unique within a pool. = Testing The illumos implementation of this feature has been used internally for a year and has been in production for ~6 months. For this patch specifically there don't seem to be any regressions introduced to ZTS and I have been running zloop for a week without any related problems. = Performance Analysis (Linux Specific) All performance results and analysis for illumos can be found in the links of the references. Redoing the same experiments in Linux gave similar results. Below are the specifics of the Linux run. After the pool reached stable state the percentage of the time spent in pass 1 per TXG was 64% on average for the stock bits while the log spacemap bits stayed at 95% during the experiment (graph: sdimitro.github.io/img/linux-lsm/PercOfSyncInPassOne.png). Sync times per TXG were 37.6 seconds on average for the stock bits and 22.7 seconds for the log spacemap bits (related graph: sdimitro.github.io/img/linux-lsm/SyncTimePerTXG.png). As a result the log spacemap bits were able to push more TXGs, which is also the reason why all graphs quantified per TXG have more entries for the log spacemap bits. Another interesting aspect in terms of txg syncs is that the stock bits had 22% of their TXGs reach sync pass 7, 55% reach sync pass 8, and 20% reach 9. The log space map bits reached sync pass 4 in 79% of their TXGs, sync pass 7 in 19%, and sync pass 8 at 1%. This emphasizes the fact that not only we spend less time on metadata but we also iterate less times to convergence in spa_sync() dirtying objects. [related graphs: stock- sdimitro.github.io/img/linux-lsm/NumberOfPassesPerTXGStock.png lsm- sdimitro.github.io/img/linux-lsm/NumberOfPassesPerTXGLSM.png] Finally, the improvement in IOPs that the userland gains from the change is approximately 40%. There is a consistent win in IOPS as you can see from the graphs below but the absolute amount of improvement that the log spacemap gives varies within each minute interval. sdimitro.github.io/img/linux-lsm/StockVsLog3Days.png sdimitro.github.io/img/linux-lsm/StockVsLog10Hours.png = Porting to Other Platforms For people that want to port this commit to other platforms below is a list of ZoL commits that this patch depends on: Make zdb results for checkpoint tests consistentdb587941c5
Update vdev_is_spacemap_addressable() for new spacemap encoding419ba59145
Simplify spa_sync by breaking it up to smaller functions8dc2197b7b
Factor metaslab_load_wait() in metaslab_load()b194fab0fb
Rename range_tree_verify to range_tree_verify_not_presentdf72b8bebe
Change target size of metaslabs from 256GB to 16GBc853f382db
zdb -L should skip leak detection altogether21e7cf5da8
vs_alloc can underflow in L2ARC vdevs7558997d2f
Simplify log vdev removal code6c926f426a
Get rid of space_map_update() for ms_synced_length425d3237ee
Introduce auxiliary metaslab histograms928e8ad47d
Error path in metaslab_load_impl() forgets to drop ms_sync_lock8eef997679
= References Background, Motivation, and Internals of the Feature - OpenZFS 2017 Presentation: youtu.be/jj2IxRkl5bQ - Slides: slideshare.net/SerapheimNikolaosDim/zfs-log-spacemaps-project Flushing Algorithm Internals & Performance Results (Illumos Specific) - Blogpost: sdimitro.github.io/post/zfs-lsm-flushing/ - OpenZFS 2018 Presentation: youtu.be/x6D2dHRjkxw - Slides: slideshare.net/SerapheimNikolaosDim/zfs-log-spacemap-flushing-algorithm Upstream Delphix Issues: DLPX-51539, DLPX-59659, DLPX-57783, DLPX-61438, DLPX-41227, DLPX-59320 DLPX-63385 Reviewed-by: Sean Eric Fagan <sef@ixsystems.com> Reviewed-by: Matt Ahrens <matt@delphix.com> Reviewed-by: George Wilson <gwilson@delphix.com> Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov> Signed-off-by: Serapheim Dimitropoulos <serapheim@delphix.com> Closes #8442
746 lines
19 KiB
C
746 lines
19 KiB
C
/*
|
|
* CDDL HEADER START
|
|
*
|
|
* The contents of this file are subject to the terms of the
|
|
* Common Development and Distribution License (the "License").
|
|
* You may not use this file except in compliance with the License.
|
|
*
|
|
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
|
* or http://www.opensolaris.org/os/licensing.
|
|
* See the License for the specific language governing permissions
|
|
* and limitations under the License.
|
|
*
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
* If applicable, add the following below this CDDL HEADER, with the
|
|
* fields enclosed by brackets "[]" replaced with your own identifying
|
|
* information: Portions Copyright [yyyy] [name of copyright owner]
|
|
*
|
|
* CDDL HEADER END
|
|
*/
|
|
/*
|
|
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
|
|
* Use is subject to license terms.
|
|
*/
|
|
/*
|
|
* Copyright (c) 2013, 2019 by Delphix. All rights reserved.
|
|
*/
|
|
|
|
#include <sys/zfs_context.h>
|
|
#include <sys/spa.h>
|
|
#include <sys/dmu.h>
|
|
#include <sys/dnode.h>
|
|
#include <sys/zio.h>
|
|
#include <sys/range_tree.h>
|
|
|
|
/*
|
|
* Range trees are tree-based data structures that can be used to
|
|
* track free space or generally any space allocation information.
|
|
* A range tree keeps track of individual segments and automatically
|
|
* provides facilities such as adjacent extent merging and extent
|
|
* splitting in response to range add/remove requests.
|
|
*
|
|
* A range tree starts out completely empty, with no segments in it.
|
|
* Adding an allocation via range_tree_add to the range tree can either:
|
|
* 1) create a new extent
|
|
* 2) extend an adjacent extent
|
|
* 3) merge two adjacent extents
|
|
* Conversely, removing an allocation via range_tree_remove can:
|
|
* 1) completely remove an extent
|
|
* 2) shorten an extent (if the allocation was near one of its ends)
|
|
* 3) split an extent into two extents, in effect punching a hole
|
|
*
|
|
* A range tree is also capable of 'bridging' gaps when adding
|
|
* allocations. This is useful for cases when close proximity of
|
|
* allocations is an important detail that needs to be represented
|
|
* in the range tree. See range_tree_set_gap(). The default behavior
|
|
* is not to bridge gaps (i.e. the maximum allowed gap size is 0).
|
|
*
|
|
* In order to traverse a range tree, use either the range_tree_walk()
|
|
* or range_tree_vacate() functions.
|
|
*
|
|
* To obtain more accurate information on individual segment
|
|
* operations that the range tree performs "under the hood", you can
|
|
* specify a set of callbacks by passing a range_tree_ops_t structure
|
|
* to the range_tree_create function. Any callbacks that are non-NULL
|
|
* are then called at the appropriate times.
|
|
*
|
|
* The range tree code also supports a special variant of range trees
|
|
* that can bridge small gaps between segments. This kind of tree is used
|
|
* by the dsl scanning code to group I/Os into mostly sequential chunks to
|
|
* optimize disk performance. The code here attempts to do this with as
|
|
* little memory and computational overhead as possible. One limitation of
|
|
* this implementation is that segments of range trees with gaps can only
|
|
* support removing complete segments.
|
|
*/
|
|
|
|
kmem_cache_t *range_seg_cache;
|
|
|
|
/* Generic ops for managing an AVL tree alongside a range tree */
|
|
struct range_tree_ops rt_avl_ops = {
|
|
.rtop_create = rt_avl_create,
|
|
.rtop_destroy = rt_avl_destroy,
|
|
.rtop_add = rt_avl_add,
|
|
.rtop_remove = rt_avl_remove,
|
|
.rtop_vacate = rt_avl_vacate,
|
|
};
|
|
|
|
void
|
|
range_tree_init(void)
|
|
{
|
|
ASSERT(range_seg_cache == NULL);
|
|
range_seg_cache = kmem_cache_create("range_seg_cache",
|
|
sizeof (range_seg_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
|
|
}
|
|
|
|
void
|
|
range_tree_fini(void)
|
|
{
|
|
kmem_cache_destroy(range_seg_cache);
|
|
range_seg_cache = NULL;
|
|
}
|
|
|
|
void
|
|
range_tree_stat_verify(range_tree_t *rt)
|
|
{
|
|
range_seg_t *rs;
|
|
uint64_t hist[RANGE_TREE_HISTOGRAM_SIZE] = { 0 };
|
|
int i;
|
|
|
|
for (rs = avl_first(&rt->rt_root); rs != NULL;
|
|
rs = AVL_NEXT(&rt->rt_root, rs)) {
|
|
uint64_t size = rs->rs_end - rs->rs_start;
|
|
int idx = highbit64(size) - 1;
|
|
|
|
hist[idx]++;
|
|
ASSERT3U(hist[idx], !=, 0);
|
|
}
|
|
|
|
for (i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
|
|
if (hist[i] != rt->rt_histogram[i]) {
|
|
zfs_dbgmsg("i=%d, hist=%px, hist=%llu, rt_hist=%llu",
|
|
i, hist, hist[i], rt->rt_histogram[i]);
|
|
}
|
|
VERIFY3U(hist[i], ==, rt->rt_histogram[i]);
|
|
}
|
|
}
|
|
|
|
static void
|
|
range_tree_stat_incr(range_tree_t *rt, range_seg_t *rs)
|
|
{
|
|
uint64_t size = rs->rs_end - rs->rs_start;
|
|
int idx = highbit64(size) - 1;
|
|
|
|
ASSERT(size != 0);
|
|
ASSERT3U(idx, <,
|
|
sizeof (rt->rt_histogram) / sizeof (*rt->rt_histogram));
|
|
|
|
rt->rt_histogram[idx]++;
|
|
ASSERT3U(rt->rt_histogram[idx], !=, 0);
|
|
}
|
|
|
|
static void
|
|
range_tree_stat_decr(range_tree_t *rt, range_seg_t *rs)
|
|
{
|
|
uint64_t size = rs->rs_end - rs->rs_start;
|
|
int idx = highbit64(size) - 1;
|
|
|
|
ASSERT(size != 0);
|
|
ASSERT3U(idx, <,
|
|
sizeof (rt->rt_histogram) / sizeof (*rt->rt_histogram));
|
|
|
|
ASSERT3U(rt->rt_histogram[idx], !=, 0);
|
|
rt->rt_histogram[idx]--;
|
|
}
|
|
|
|
/*
|
|
* NOTE: caller is responsible for all locking.
|
|
*/
|
|
static int
|
|
range_tree_seg_compare(const void *x1, const void *x2)
|
|
{
|
|
const range_seg_t *r1 = (const range_seg_t *)x1;
|
|
const range_seg_t *r2 = (const range_seg_t *)x2;
|
|
|
|
ASSERT3U(r1->rs_start, <=, r1->rs_end);
|
|
ASSERT3U(r2->rs_start, <=, r2->rs_end);
|
|
|
|
return ((r1->rs_start >= r2->rs_end) - (r1->rs_end <= r2->rs_start));
|
|
}
|
|
|
|
range_tree_t *
|
|
range_tree_create_impl(range_tree_ops_t *ops, void *arg,
|
|
int (*avl_compare) (const void *, const void *), uint64_t gap)
|
|
{
|
|
range_tree_t *rt = kmem_zalloc(sizeof (range_tree_t), KM_SLEEP);
|
|
|
|
avl_create(&rt->rt_root, range_tree_seg_compare,
|
|
sizeof (range_seg_t), offsetof(range_seg_t, rs_node));
|
|
|
|
rt->rt_ops = ops;
|
|
rt->rt_gap = gap;
|
|
rt->rt_arg = arg;
|
|
rt->rt_avl_compare = avl_compare;
|
|
|
|
if (rt->rt_ops != NULL && rt->rt_ops->rtop_create != NULL)
|
|
rt->rt_ops->rtop_create(rt, rt->rt_arg);
|
|
|
|
return (rt);
|
|
}
|
|
|
|
range_tree_t *
|
|
range_tree_create(range_tree_ops_t *ops, void *arg)
|
|
{
|
|
return (range_tree_create_impl(ops, arg, NULL, 0));
|
|
}
|
|
|
|
void
|
|
range_tree_destroy(range_tree_t *rt)
|
|
{
|
|
VERIFY0(rt->rt_space);
|
|
|
|
if (rt->rt_ops != NULL && rt->rt_ops->rtop_destroy != NULL)
|
|
rt->rt_ops->rtop_destroy(rt, rt->rt_arg);
|
|
|
|
avl_destroy(&rt->rt_root);
|
|
kmem_free(rt, sizeof (*rt));
|
|
}
|
|
|
|
void
|
|
range_tree_adjust_fill(range_tree_t *rt, range_seg_t *rs, int64_t delta)
|
|
{
|
|
ASSERT3U(rs->rs_fill + delta, !=, 0);
|
|
ASSERT3U(rs->rs_fill + delta, <=, rs->rs_end - rs->rs_start);
|
|
|
|
if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
|
|
rt->rt_ops->rtop_remove(rt, rs, rt->rt_arg);
|
|
rs->rs_fill += delta;
|
|
if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL)
|
|
rt->rt_ops->rtop_add(rt, rs, rt->rt_arg);
|
|
}
|
|
|
|
static void
|
|
range_tree_add_impl(void *arg, uint64_t start, uint64_t size, uint64_t fill)
|
|
{
|
|
range_tree_t *rt = arg;
|
|
avl_index_t where;
|
|
range_seg_t rsearch, *rs_before, *rs_after, *rs;
|
|
uint64_t end = start + size, gap = rt->rt_gap;
|
|
uint64_t bridge_size = 0;
|
|
boolean_t merge_before, merge_after;
|
|
|
|
ASSERT3U(size, !=, 0);
|
|
ASSERT3U(fill, <=, size);
|
|
|
|
rsearch.rs_start = start;
|
|
rsearch.rs_end = end;
|
|
rs = avl_find(&rt->rt_root, &rsearch, &where);
|
|
|
|
if (gap == 0 && rs != NULL &&
|
|
rs->rs_start <= start && rs->rs_end >= end) {
|
|
zfs_panic_recover("zfs: allocating allocated segment"
|
|
"(offset=%llu size=%llu) of (offset=%llu size=%llu)\n",
|
|
(longlong_t)start, (longlong_t)size,
|
|
(longlong_t)rs->rs_start,
|
|
(longlong_t)rs->rs_end - rs->rs_start);
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* If this is a gap-supporting range tree, it is possible that we
|
|
* are inserting into an existing segment. In this case simply
|
|
* bump the fill count and call the remove / add callbacks. If the
|
|
* new range will extend an existing segment, we remove the
|
|
* existing one, apply the new extent to it and re-insert it using
|
|
* the normal code paths.
|
|
*/
|
|
if (rs != NULL) {
|
|
ASSERT3U(gap, !=, 0);
|
|
if (rs->rs_start <= start && rs->rs_end >= end) {
|
|
range_tree_adjust_fill(rt, rs, fill);
|
|
return;
|
|
}
|
|
|
|
avl_remove(&rt->rt_root, rs);
|
|
if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
|
|
rt->rt_ops->rtop_remove(rt, rs, rt->rt_arg);
|
|
|
|
range_tree_stat_decr(rt, rs);
|
|
rt->rt_space -= rs->rs_end - rs->rs_start;
|
|
|
|
fill += rs->rs_fill;
|
|
start = MIN(start, rs->rs_start);
|
|
end = MAX(end, rs->rs_end);
|
|
size = end - start;
|
|
|
|
range_tree_add_impl(rt, start, size, fill);
|
|
|
|
kmem_cache_free(range_seg_cache, rs);
|
|
return;
|
|
}
|
|
|
|
ASSERT3P(rs, ==, NULL);
|
|
|
|
/*
|
|
* Determine whether or not we will have to merge with our neighbors.
|
|
* If gap != 0, we might need to merge with our neighbors even if we
|
|
* aren't directly touching.
|
|
*/
|
|
rs_before = avl_nearest(&rt->rt_root, where, AVL_BEFORE);
|
|
rs_after = avl_nearest(&rt->rt_root, where, AVL_AFTER);
|
|
|
|
merge_before = (rs_before != NULL && rs_before->rs_end >= start - gap);
|
|
merge_after = (rs_after != NULL && rs_after->rs_start <= end + gap);
|
|
|
|
if (merge_before && gap != 0)
|
|
bridge_size += start - rs_before->rs_end;
|
|
if (merge_after && gap != 0)
|
|
bridge_size += rs_after->rs_start - end;
|
|
|
|
if (merge_before && merge_after) {
|
|
avl_remove(&rt->rt_root, rs_before);
|
|
if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL) {
|
|
rt->rt_ops->rtop_remove(rt, rs_before, rt->rt_arg);
|
|
rt->rt_ops->rtop_remove(rt, rs_after, rt->rt_arg);
|
|
}
|
|
|
|
range_tree_stat_decr(rt, rs_before);
|
|
range_tree_stat_decr(rt, rs_after);
|
|
|
|
rs_after->rs_fill += rs_before->rs_fill + fill;
|
|
rs_after->rs_start = rs_before->rs_start;
|
|
kmem_cache_free(range_seg_cache, rs_before);
|
|
rs = rs_after;
|
|
} else if (merge_before) {
|
|
if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
|
|
rt->rt_ops->rtop_remove(rt, rs_before, rt->rt_arg);
|
|
|
|
range_tree_stat_decr(rt, rs_before);
|
|
|
|
rs_before->rs_fill += fill;
|
|
rs_before->rs_end = end;
|
|
rs = rs_before;
|
|
} else if (merge_after) {
|
|
if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
|
|
rt->rt_ops->rtop_remove(rt, rs_after, rt->rt_arg);
|
|
|
|
range_tree_stat_decr(rt, rs_after);
|
|
|
|
rs_after->rs_fill += fill;
|
|
rs_after->rs_start = start;
|
|
rs = rs_after;
|
|
} else {
|
|
rs = kmem_cache_alloc(range_seg_cache, KM_SLEEP);
|
|
|
|
rs->rs_fill = fill;
|
|
rs->rs_start = start;
|
|
rs->rs_end = end;
|
|
avl_insert(&rt->rt_root, rs, where);
|
|
}
|
|
|
|
if (gap != 0)
|
|
ASSERT3U(rs->rs_fill, <=, rs->rs_end - rs->rs_start);
|
|
else
|
|
ASSERT3U(rs->rs_fill, ==, rs->rs_end - rs->rs_start);
|
|
|
|
if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL)
|
|
rt->rt_ops->rtop_add(rt, rs, rt->rt_arg);
|
|
|
|
range_tree_stat_incr(rt, rs);
|
|
rt->rt_space += size + bridge_size;
|
|
}
|
|
|
|
void
|
|
range_tree_add(void *arg, uint64_t start, uint64_t size)
|
|
{
|
|
range_tree_add_impl(arg, start, size, size);
|
|
}
|
|
|
|
static void
|
|
range_tree_remove_impl(range_tree_t *rt, uint64_t start, uint64_t size,
|
|
boolean_t do_fill)
|
|
{
|
|
avl_index_t where;
|
|
range_seg_t rsearch, *rs, *newseg;
|
|
uint64_t end = start + size;
|
|
boolean_t left_over, right_over;
|
|
|
|
VERIFY3U(size, !=, 0);
|
|
VERIFY3U(size, <=, rt->rt_space);
|
|
|
|
rsearch.rs_start = start;
|
|
rsearch.rs_end = end;
|
|
rs = avl_find(&rt->rt_root, &rsearch, &where);
|
|
|
|
/* Make sure we completely overlap with someone */
|
|
if (rs == NULL) {
|
|
zfs_panic_recover("zfs: freeing free segment "
|
|
"(offset=%llu size=%llu)",
|
|
(longlong_t)start, (longlong_t)size);
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* Range trees with gap support must only remove complete segments
|
|
* from the tree. This allows us to maintain accurate fill accounting
|
|
* and to ensure that bridged sections are not leaked. If we need to
|
|
* remove less than the full segment, we can only adjust the fill count.
|
|
*/
|
|
if (rt->rt_gap != 0) {
|
|
if (do_fill) {
|
|
if (rs->rs_fill == size) {
|
|
start = rs->rs_start;
|
|
end = rs->rs_end;
|
|
size = end - start;
|
|
} else {
|
|
range_tree_adjust_fill(rt, rs, -size);
|
|
return;
|
|
}
|
|
} else if (rs->rs_start != start || rs->rs_end != end) {
|
|
zfs_panic_recover("zfs: freeing partial segment of "
|
|
"gap tree (offset=%llu size=%llu) of "
|
|
"(offset=%llu size=%llu)",
|
|
(longlong_t)start, (longlong_t)size,
|
|
(longlong_t)rs->rs_start,
|
|
(longlong_t)rs->rs_end - rs->rs_start);
|
|
return;
|
|
}
|
|
}
|
|
|
|
VERIFY3U(rs->rs_start, <=, start);
|
|
VERIFY3U(rs->rs_end, >=, end);
|
|
|
|
left_over = (rs->rs_start != start);
|
|
right_over = (rs->rs_end != end);
|
|
|
|
range_tree_stat_decr(rt, rs);
|
|
|
|
if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
|
|
rt->rt_ops->rtop_remove(rt, rs, rt->rt_arg);
|
|
|
|
if (left_over && right_over) {
|
|
newseg = kmem_cache_alloc(range_seg_cache, KM_SLEEP);
|
|
newseg->rs_start = end;
|
|
newseg->rs_end = rs->rs_end;
|
|
newseg->rs_fill = newseg->rs_end - newseg->rs_start;
|
|
range_tree_stat_incr(rt, newseg);
|
|
|
|
rs->rs_end = start;
|
|
|
|
avl_insert_here(&rt->rt_root, newseg, rs, AVL_AFTER);
|
|
if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL)
|
|
rt->rt_ops->rtop_add(rt, newseg, rt->rt_arg);
|
|
} else if (left_over) {
|
|
rs->rs_end = start;
|
|
} else if (right_over) {
|
|
rs->rs_start = end;
|
|
} else {
|
|
avl_remove(&rt->rt_root, rs);
|
|
kmem_cache_free(range_seg_cache, rs);
|
|
rs = NULL;
|
|
}
|
|
|
|
if (rs != NULL) {
|
|
/*
|
|
* The fill of the leftover segment will always be equal to
|
|
* the size, since we do not support removing partial segments
|
|
* of range trees with gaps.
|
|
*/
|
|
rs->rs_fill = rs->rs_end - rs->rs_start;
|
|
range_tree_stat_incr(rt, rs);
|
|
|
|
if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL)
|
|
rt->rt_ops->rtop_add(rt, rs, rt->rt_arg);
|
|
}
|
|
|
|
rt->rt_space -= size;
|
|
}
|
|
|
|
void
|
|
range_tree_remove(void *arg, uint64_t start, uint64_t size)
|
|
{
|
|
range_tree_remove_impl(arg, start, size, B_FALSE);
|
|
}
|
|
|
|
void
|
|
range_tree_remove_fill(range_tree_t *rt, uint64_t start, uint64_t size)
|
|
{
|
|
range_tree_remove_impl(rt, start, size, B_TRUE);
|
|
}
|
|
|
|
void
|
|
range_tree_resize_segment(range_tree_t *rt, range_seg_t *rs,
|
|
uint64_t newstart, uint64_t newsize)
|
|
{
|
|
int64_t delta = newsize - (rs->rs_end - rs->rs_start);
|
|
|
|
range_tree_stat_decr(rt, rs);
|
|
if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
|
|
rt->rt_ops->rtop_remove(rt, rs, rt->rt_arg);
|
|
|
|
rs->rs_start = newstart;
|
|
rs->rs_end = newstart + newsize;
|
|
|
|
range_tree_stat_incr(rt, rs);
|
|
if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL)
|
|
rt->rt_ops->rtop_add(rt, rs, rt->rt_arg);
|
|
|
|
rt->rt_space += delta;
|
|
}
|
|
|
|
static range_seg_t *
|
|
range_tree_find_impl(range_tree_t *rt, uint64_t start, uint64_t size)
|
|
{
|
|
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, NULL));
|
|
}
|
|
|
|
range_seg_t *
|
|
range_tree_find(range_tree_t *rt, uint64_t start, uint64_t size)
|
|
{
|
|
range_seg_t *rs = range_tree_find_impl(rt, start, size);
|
|
if (rs != NULL && rs->rs_start <= start && rs->rs_end >= start + size)
|
|
return (rs);
|
|
return (NULL);
|
|
}
|
|
|
|
void
|
|
range_tree_verify_not_present(range_tree_t *rt, uint64_t off, uint64_t size)
|
|
{
|
|
range_seg_t *rs = range_tree_find(rt, off, size);
|
|
if (rs != NULL)
|
|
panic("segment already in tree; rs=%p", (void *)rs);
|
|
}
|
|
|
|
boolean_t
|
|
range_tree_contains(range_tree_t *rt, uint64_t start, uint64_t size)
|
|
{
|
|
return (range_tree_find(rt, start, size) != NULL);
|
|
}
|
|
|
|
/*
|
|
* Ensure that this range is not in the tree, regardless of whether
|
|
* it is currently in the tree.
|
|
*/
|
|
void
|
|
range_tree_clear(range_tree_t *rt, uint64_t start, uint64_t size)
|
|
{
|
|
range_seg_t *rs;
|
|
|
|
if (size == 0)
|
|
return;
|
|
|
|
while ((rs = range_tree_find_impl(rt, start, size)) != NULL) {
|
|
uint64_t free_start = MAX(rs->rs_start, start);
|
|
uint64_t free_end = MIN(rs->rs_end, start + size);
|
|
range_tree_remove(rt, free_start, free_end - free_start);
|
|
}
|
|
}
|
|
|
|
void
|
|
range_tree_swap(range_tree_t **rtsrc, range_tree_t **rtdst)
|
|
{
|
|
range_tree_t *rt;
|
|
|
|
ASSERT0(range_tree_space(*rtdst));
|
|
ASSERT0(avl_numnodes(&(*rtdst)->rt_root));
|
|
|
|
rt = *rtsrc;
|
|
*rtsrc = *rtdst;
|
|
*rtdst = rt;
|
|
}
|
|
|
|
void
|
|
range_tree_vacate(range_tree_t *rt, range_tree_func_t *func, void *arg)
|
|
{
|
|
range_seg_t *rs;
|
|
void *cookie = NULL;
|
|
|
|
if (rt->rt_ops != NULL && rt->rt_ops->rtop_vacate != NULL)
|
|
rt->rt_ops->rtop_vacate(rt, rt->rt_arg);
|
|
|
|
while ((rs = avl_destroy_nodes(&rt->rt_root, &cookie)) != NULL) {
|
|
if (func != NULL)
|
|
func(arg, rs->rs_start, rs->rs_end - rs->rs_start);
|
|
kmem_cache_free(range_seg_cache, rs);
|
|
}
|
|
|
|
bzero(rt->rt_histogram, sizeof (rt->rt_histogram));
|
|
rt->rt_space = 0;
|
|
}
|
|
|
|
void
|
|
range_tree_walk(range_tree_t *rt, range_tree_func_t *func, void *arg)
|
|
{
|
|
for (range_seg_t *rs = avl_first(&rt->rt_root); rs;
|
|
rs = AVL_NEXT(&rt->rt_root, rs)) {
|
|
func(arg, rs->rs_start, rs->rs_end - rs->rs_start);
|
|
}
|
|
}
|
|
|
|
range_seg_t *
|
|
range_tree_first(range_tree_t *rt)
|
|
{
|
|
return (avl_first(&rt->rt_root));
|
|
}
|
|
|
|
uint64_t
|
|
range_tree_space(range_tree_t *rt)
|
|
{
|
|
return (rt->rt_space);
|
|
}
|
|
|
|
uint64_t
|
|
range_tree_numsegs(range_tree_t *rt)
|
|
{
|
|
return ((rt == NULL) ? 0 : avl_numnodes(&rt->rt_root));
|
|
}
|
|
|
|
boolean_t
|
|
range_tree_is_empty(range_tree_t *rt)
|
|
{
|
|
ASSERT(rt != NULL);
|
|
return (range_tree_space(rt) == 0);
|
|
}
|
|
|
|
/* Generic range tree functions for maintaining segments in an AVL tree. */
|
|
void
|
|
rt_avl_create(range_tree_t *rt, void *arg)
|
|
{
|
|
avl_tree_t *tree = arg;
|
|
|
|
avl_create(tree, rt->rt_avl_compare, sizeof (range_seg_t),
|
|
offsetof(range_seg_t, rs_pp_node));
|
|
}
|
|
|
|
void
|
|
rt_avl_destroy(range_tree_t *rt, void *arg)
|
|
{
|
|
avl_tree_t *tree = arg;
|
|
|
|
ASSERT0(avl_numnodes(tree));
|
|
avl_destroy(tree);
|
|
}
|
|
|
|
void
|
|
rt_avl_add(range_tree_t *rt, range_seg_t *rs, void *arg)
|
|
{
|
|
avl_tree_t *tree = arg;
|
|
avl_add(tree, rs);
|
|
}
|
|
|
|
void
|
|
rt_avl_remove(range_tree_t *rt, range_seg_t *rs, void *arg)
|
|
{
|
|
avl_tree_t *tree = arg;
|
|
avl_remove(tree, rs);
|
|
}
|
|
|
|
void
|
|
rt_avl_vacate(range_tree_t *rt, void *arg)
|
|
{
|
|
/*
|
|
* Normally one would walk the tree freeing nodes along the way.
|
|
* Since the nodes are shared with the range trees we can avoid
|
|
* walking all nodes and just reinitialize the avl tree. The nodes
|
|
* will be freed by the range tree, so we don't want to free them here.
|
|
*/
|
|
rt_avl_create(rt, arg);
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
/*
|
|
* Remove any overlapping ranges between the given segment [start, end)
|
|
* from removefrom. Add non-overlapping leftovers to addto.
|
|
*/
|
|
void
|
|
range_tree_remove_xor_add_segment(uint64_t start, uint64_t end,
|
|
range_tree_t *removefrom, range_tree_t *addto)
|
|
{
|
|
avl_index_t where;
|
|
range_seg_t starting_rs = {
|
|
.rs_start = start,
|
|
.rs_end = start + 1
|
|
};
|
|
|
|
range_seg_t *curr = avl_find(&removefrom->rt_root,
|
|
&starting_rs, &where);
|
|
|
|
if (curr == NULL)
|
|
curr = avl_nearest(&removefrom->rt_root, where, AVL_AFTER);
|
|
|
|
range_seg_t *next;
|
|
for (; curr != NULL; curr = next) {
|
|
next = AVL_NEXT(&removefrom->rt_root, curr);
|
|
|
|
if (start == end)
|
|
return;
|
|
VERIFY3U(start, <, end);
|
|
|
|
/* there is no overlap */
|
|
if (end <= curr->rs_start) {
|
|
range_tree_add(addto, start, end - start);
|
|
return;
|
|
}
|
|
|
|
uint64_t overlap_start = MAX(curr->rs_start, start);
|
|
uint64_t overlap_end = MIN(curr->rs_end, end);
|
|
uint64_t overlap_size = overlap_end - overlap_start;
|
|
ASSERT3S(overlap_size, >, 0);
|
|
range_tree_remove(removefrom, overlap_start, overlap_size);
|
|
|
|
if (start < overlap_start)
|
|
range_tree_add(addto, start, overlap_start - start);
|
|
|
|
start = overlap_end;
|
|
}
|
|
VERIFY3P(curr, ==, NULL);
|
|
|
|
if (start != end) {
|
|
VERIFY3U(start, <, end);
|
|
range_tree_add(addto, start, end - start);
|
|
} else {
|
|
VERIFY3U(start, ==, end);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* For each entry in rt, if it exists in removefrom, remove it
|
|
* from removefrom. Otherwise, add it to addto.
|
|
*/
|
|
void
|
|
range_tree_remove_xor_add(range_tree_t *rt, range_tree_t *removefrom,
|
|
range_tree_t *addto)
|
|
{
|
|
for (range_seg_t *rs = avl_first(&rt->rt_root); rs;
|
|
rs = AVL_NEXT(&rt->rt_root, rs)) {
|
|
range_tree_remove_xor_add_segment(rs->rs_start, rs->rs_end,
|
|
removefrom, addto);
|
|
}
|
|
}
|