generated from rust-vmm/crate-template
-
Notifications
You must be signed in to change notification settings - Fork 26
Interval tree initial documentation and structures defintions #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andreeaflorescu
merged 3 commits into
rust-vmm:main
from
AlexandruCihodaru:interval_tree_doc
Feb 10, 2022
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # Design | ||
|
|
||
| ## Allocation engine | ||
|
|
||
| This implementation uses an interval tree that is specialized for allocation of | ||
| memory-mapped I/O and port I/O address space. The fields of the structures | ||
| defined will have semantic meaning for this context (e.g. node state to indicate | ||
| if a node in the tree is assigned or not to a device). | ||
|
|
||
| We offer three options for placing a memory slot in the managed address space: | ||
|
|
||
| 1. `LastMatch` -> When using this allocation policy the allocator will try to | ||
| insert the range described by the constraint at the first available position | ||
| starting from the end of the managed address space. | ||
| 2. `FirstMatch` -> When using this allocation policy the allocator will try to | ||
| insert the range described by the constraint at the first available position | ||
| starting from the beginning of the managed address space. | ||
| 3. `ExactMatch(u64)` -> When using this allocation policy the allocator will try | ||
| to insert the range at the exact position described by the constraint, otherwise | ||
| it will return an error. | ||
|
|
||
| Struct `Constraint` is used to describe the overall information of the resource | ||
| needed to be allocated. This structure is also used by IntervalTree to know where | ||
| and how to allocate the resource. The fields that are mandatory for allocating | ||
| a new memory slot are size of the slot, alignment of the slot and allocation policy. | ||
| Optionally the user can specify a range where the allocator will place the allocated | ||
| memory slot. | ||
|
|
||
| ## Interval tree | ||
|
|
||
| An interval tree is a tree data structure used for storing information about intervals. | ||
| Specifically, it allows one to efficiently identify intervals that are overlapping | ||
| with a given point, or another interval. We considered that this characteristic | ||
| makes this data structure appropriate to be used as an allocation engine for | ||
| memory slots inside an address space. The time complexity of an interval tree, | ||
| namely O(log n+m) for queries, O(log n) for creation and O(log n) for insertion | ||
| and deletion of nodes. The key of each node of the tree is represented using a | ||
| struct named `Range` that contains the bounds of the address space. Each node in | ||
| the tree can have two states, either `Free` or `Allocated`. Beside the information | ||
| presented above the representation of a node also contains references two the | ||
| children node of the current node. | ||
|
|
||
| ## Usage | ||
|
|
||
| To use the `IntervalTree` implementation as an address allocator one should first | ||
| create an interval tree object and give an address space as a root node. After, | ||
| the user should create a constraint with the size for the resource. Optionally | ||
| the constraint could also contain the maximum, minimum and alignment for the | ||
| constraint. | ||
|
|
||
| ## State transition | ||
|
|
||
| At the beginning, the interval tree will contain just one node that will represent | ||
| the whole address space, the state of this node will be `NodeState::Free`. | ||
|
|
||
|  | ||
|
|
||
| When we allocate a memory slot, one of the nodes that have the state `NodeState::Free` | ||
| will be split accordingly. A new node that has as the key a range representing the | ||
| allocated memory slot will be inserted in the tree. | ||
|
|
||
|  | ||
|
|
||
| When one of the allocated nodes is freed its state will be changed from `NodeState::Allocated` | ||
| to `NodeState::Free` if there are two adjacent nodes that are not allocated then | ||
| they will be merged in a single node. | ||
|
|
||
|  | ||
|
|
||
| ## License | ||
|
|
||
| **!!!NOTICE**: The BSD-3-Clause license is not included in this template. | ||
| The license needs to be manually added because the text of the license file | ||
| also includes the copyright. The copyright can be different for different | ||
| crates. If the crate contains code from CrosVM, the crate must add the | ||
| CrosVM copyright which can be found | ||
| [here](https://chromium.googlesource.com/chromiumos/platform/crosvm/+/master/LICENSE). | ||
| For crates developed from scratch, the copyright is different and depends on | ||
| the contributors. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright (C) 2022 Alibaba Cloud. All rights reserved. | ||
| // Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2 | ||
|
|
||
| /// Policy for resource allocation. | ||
| #[derive(Copy, Clone, Debug, PartialEq)] | ||
| pub enum AllocPolicy { | ||
| /// Allocate the first matched entry. | ||
| FirstMatch, | ||
| /// Allocate first matched entry from the end of the range. | ||
| LastMatch, | ||
| /// Allocate a memory slot starting with the specified address | ||
| /// if it is available. | ||
| ExactMatch(u64), | ||
| } | ||
|
|
||
| impl Default for AllocPolicy { | ||
| fn default() -> Self { | ||
| AllocPolicy::FirstMatch | ||
| } | ||
| } | ||
|
|
||
| /// Struct to describe resource allocation constraints. | ||
| #[derive(Copy, Clone, Debug, PartialEq)] | ||
| pub struct Constraint { | ||
| /// Size to allocate. | ||
| pub size: u64, | ||
| /// Range where the allocated resource will be placed. | ||
| pub desired_range: Range, | ||
| /// Alignment for the allocated resource. | ||
| pub align: u64, | ||
| /// Resource allocation policy. | ||
| pub policy: AllocPolicy, | ||
| } | ||
|
|
||
| /// A closed interval range [min, max] used to describe a | ||
| /// memory slot that will be assigned to a device by the VMM. | ||
| /// This structure represents the key of the Node object in | ||
| /// the interval tree implementation. | ||
| #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Hash, Ord)] | ||
| pub struct Range { | ||
andreeaflorescu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pub min: u64, | ||
| pub max: u64, | ||
| } | ||
|
|
||
| /// Node state for interval tree nodes. | ||
| /// | ||
| /// Valid state transition: | ||
| /// - None -> Free: IntervalTree::insert() | ||
| /// - Free -> Allocated: IntervalTree::allocate() | ||
| /// - Allocated -> Free: IntervalTree::free() | ||
| /// - * -> None: IntervalTree::delete() | ||
| #[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord)] | ||
| pub enum NodeState { | ||
| /// Node is free. | ||
| Free, | ||
| /// Node is allocated. | ||
| Allocated, | ||
| } | ||
AlexandruCihodaru marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// Internal tree node to implement interval tree. | ||
| #[derive(Debug, PartialEq)] | ||
| struct InnerNode { | ||
| /// Interval handled by this node. | ||
| key: Range, | ||
| /// NodeState, can be Free or Allocated. | ||
| node_state: NodeState, | ||
| /// Optional left child of current node. | ||
| left: Option<Box<InnerNode>>, | ||
| /// Optional right child of current node. | ||
| right: Option<Box<InnerNode>>, | ||
| /// Cached height of the node. | ||
| height: u64, | ||
| /// Cached maximum valued covered by this node. | ||
| max_key: u64, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| // Copyright (C) 2022 Alibaba Cloud. All rights reserved. | ||
| // Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2 | ||
|
|
||
| mod interval_tree; | ||
|
|
||
| pub use interval_tree::{AllocPolicy, NodeState, Range}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.