Skip to content
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

[crates] Add page-cache crate #268

Merged
merged 1 commit into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/composite_action/prebuild/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ runs:
docker exec ${{ inputs.container-name }} bash -c "git config --global --add safe.directory /root/occlum/deps/grpc-rust";
docker exec ${{ inputs.container-name }} bash -c "git config --global --add safe.directory /root/occlum/deps/io-uring";
docker exec ${{ inputs.container-name }} bash -c "git config --global --add safe.directory /root/occlum/deps/itoa-sgx";
docker exec ${{ inputs.container-name }} bash -c "git config --global --add safe.directory /root/occlum/deps/lru-rs";
docker exec ${{ inputs.container-name }} bash -c "git config --global --add safe.directory /root/occlum/deps/resolv-conf";
docker exec ${{ inputs.container-name }} bash -c "git config --global --add safe.directory /root/occlum/deps/rust-hash-wheel-timer";
docker exec ${{ inputs.container-name }} bash -c "git config --global --add safe.directory /root/occlum/deps/rust-sgx-sdk";
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@
[submodule "deps/resolv-conf"]
path = deps/resolv-conf
url = https://github.com/tailhook/resolv-conf.git
[submodule "deps/lru-rs"]
path = deps/lru-rs
url = https://github.com/liqinggd/lru-rs.git
1 change: 1 addition & 0 deletions deps/lru-rs
Submodule lru-rs added at cd2422
2 changes: 2 additions & 0 deletions src/libos/crates/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"keyable-arc",
"new-self-ref-arc",
"object-id",
"page-cache",
"sgx-disk",
"sgx-untrusted-alloc",
"vdso-time"
Expand All @@ -27,6 +28,7 @@ default-members = [
"keyable-arc",
"new-self-ref-arc",
"object-id",
"page-cache",
"sgx-disk",
"vdso-time"
]
Expand Down
1 change: 1 addition & 0 deletions src/libos/crates/block-device/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub use self::block_io::{
BioReq, BioReqBuilder, BioReqOnCompleteFn, BioReqOnDropFn, BioResp, BioSubmission, BioType,
};
pub use self::util::anymap::{Any, AnyMap};
pub use self::util::block_range::{BlockRange, BlockRangeIter};

// This crate assumes the machine is 64-bit to use u64 and usize interchangably.
use static_assertions::assert_eq_size;
Expand Down
125 changes: 125 additions & 0 deletions src/libos/crates/block-device/src/util/block_range.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//! BlockRangeIter is an iterator for blocks within a specific range.
//! BlockRange describes sub-range information about each block.
use crate::prelude::*;

/// Given a range and iterate sub-range for each block.
#[derive(Debug)]
pub struct BlockRangeIter {
pub begin: usize,
pub end: usize,
pub block_size: usize,
}

/// Describe the range for one block.
#[derive(Debug, Eq, PartialEq)]
pub struct BlockRange {
pub block_id: BlockId,
pub begin: usize,
pub end: usize,
pub block_size: usize,
}

impl Iterator for BlockRangeIter {
type Item = BlockRange;

fn next(&mut self) -> Option<<Self as Iterator>::Item> {
// Exit condition
if self.begin >= self.end {
return None;
}

// Construct sub-range of next block
let sub_range = {
let block_id = self.begin / self.block_size;
let begin = self.begin % self.block_size;
let end = if block_id == self.end / self.block_size {
self.end % self.block_size
} else {
self.block_size
};
let block_size = self.block_size;
BlockRange {
block_id,
begin,
end,
block_size,
}
};

self.begin += sub_range.len();
Some(sub_range)
}
}

impl BlockRange {
/// Return block length.
pub fn len(&self) -> usize {
self.end - self.begin
}

/// Whether the range covers a whole block.
pub fn is_full(&self) -> bool {
self.len() == self.block_size
}

/// Whether the range is empty.
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Describe the begin offset of this block from the whole range.
pub fn origin_begin(&self) -> usize {
self.block_id * self.block_size + self.begin
}

/// Describe the end offset of this block from the whole range.
pub fn origin_end(&self) -> usize {
self.block_id * self.block_size + self.end
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn block_range_iter() {
let mut iter = BlockRangeIter {
begin: 0x125,
end: 0x2025,
block_size: BLOCK_SIZE,
};

assert_eq!(
iter.next(),
Some(BlockRange {
block_id: 0,
begin: 0x125,
end: 0x1000,
block_size: BLOCK_SIZE,
})
);

assert_eq!(
iter.next(),
Some(BlockRange {
block_id: 1,
begin: 0,
end: 0x1000,
block_size: BLOCK_SIZE,
})
);

assert_eq!(
iter.next(),
Some(BlockRange {
block_id: 2,
begin: 0,
end: 0x25,
block_size: BLOCK_SIZE,
})
);

assert_eq!(iter.next(), None);
}
}
1 change: 1 addition & 0 deletions src/libos/crates/block-device/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::prelude::*;

pub mod anymap;
pub mod block_range;
pub mod test;

/// Equivalent to `Box::into_inner`. The latter method is not available in
Expand Down
36 changes: 36 additions & 0 deletions src/libos/crates/page-cache/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
name = "page-cache"
version = "0.1.0"
authors = ["Song Shaowei <songshaowei.ssw@antgroup.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["libc", "lru/default"]
sgx = ["sgx_types", "sgx_tstd", "sgx_trts", "sgx_libc", "async-io/sgx", "async-rt/sgx", "lru/sgx"]

[dependencies]
async-trait = "0.1.52"
lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
libc = { version = "0.2", optional = true }
log = "0.4"
lru = { path = "../../../../deps/lru-rs", default-features = false }
spin = "0.7"

async-io = { path = "../async-io" }
async-rt = { path = "../async-rt" }
block-device = { path = "../block-device" }
errno = { path = "../errno" }
object-id = { path = "../object-id" }

sgx_types = { path = "../../../../deps/rust-sgx-sdk/sgx_types", optional = true }
sgx_tstd = { path = "../../../../deps/rust-sgx-sdk/sgx_tstd", optional = true, features = ["backtrace"] }
sgx_trts = { path = "../../../../deps/rust-sgx-sdk/sgx_trts", optional = true }
sgx_libc = { path = "../../../../deps/rust-sgx-sdk/sgx_libc", optional = true }

[dev-dependencies]
async-rt = { path = "../async-rt", features = ["auto_run"] }

[lib]
doctest = false
Loading