Skip to content

Commit

Permalink
chore(build): uzfs build use submodule to build
Browse files Browse the repository at this point in the history
Signed-off-by: sundengyu <dengyu.sun@iomesh.com>
  • Loading branch information
sundengyu committed Jul 2, 2024
1 parent 9530edd commit b6d8344
Show file tree
Hide file tree
Showing 16 changed files with 1,012 additions and 1,281 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Cargo.lock
target
uzfs-sys/src/bindings.rs
zfs
src/bindings.rs
download
install
libuzfs.pc
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "zfs"]
path = zfs
url = git@github.com:iomesh/zfs.git
27 changes: 8 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,26 @@ edition = "2021"
[lib]
crate-type = ["staticlib", "rlib"]

[workspace]
members = ["uzfs-sys"]

[build-dependencies]
bindgen = "0.60"
pkg-config = "0.3"
uzfs-sys = { path = "uzfs-sys", version = "0.1.0" }
pkg-config = "0.3.26"

[workspace.dependencies]
tokio-test = "0.4.2"
[dependencies]
tokio = { version = "1.32.0", features = ["macros", "rt-multi-thread", "signal", "time", "sync"] }
futures = "0.3.28"
dashmap = "5.4.0"
serial_test = { version = "1.0.0" }
once_cell = "1.13.1"
rand = "0.8.5"

[dependencies]
libc = "0.2"
backtrace = "0.3"
cstr-argument = "0.1"
uzfs-sys = { path = "uzfs-sys", version = "0.1.0" }
tempfile = "3.3.0"
prometheus-client = { git = "https://github.com/iomesh/prometheus_client_rust.git", tag = "v0.22.0-1"}
tokio.workspace = true
once_cell.workspace = true
strum = "0.24"
strum_macros = "0.24"

[dev-dependencies]
rand.workspace = true
tokio-test = "0.4.2"
rand = "0.8.5"
serial_test = { version = "1.0.0" }
tempfile = "3.3.0"
petgraph = "0.6.3"
serial_test.workspace = true
dashmap.workspace = true
tokio-test.workspace = true
nix = { version = "0.27.1", features = ["process"] }
23 changes: 17 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
all: build
top_srcdir=`pwd`
scripts_dir=${top_srcdir}/scripts

all: build_libuzfs_src build

build_libuzfs_src:
@bash ${scripts_dir}/build_libuzfs_src.sh ${top_srcdir} ${ENABLE_DEBUG} ${ENABLE_ASAN}

clean_libuzfs_src:
@bash ${scripts_dir}/clean_libuzfs_src.sh ${top_srcdir}

build_uzfs_sys:
cargo build -p uzfs_sys

build_uzfs:
cargo build -p uzfs

build:
cargo build --all
Expand All @@ -9,9 +24,5 @@ test:
cargo test --target x86_64-unknown-linux-gnu uzfs_sync_test -- --ignored --nocapture
cargo test --target x86_64-unknown-linux-gnu --workspace -- --nocapture

clean:
clean: clean_libuzfs_src
cargo clean
make -C uzfs-sys clean

bench:
cargo bench --all
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,8 @@ ZFS_PKG=zfs-${ZFS_TAG}
ZFS_ZIP=${ZFS_PKG}.zip
INSTALL_DIR=${TOP_SRCDIR}/install
LIBUZFS=${INSTALL_DIR}/lib/libuzfs.a
DOWNLOAD_DIR=${TOP_SRCDIR}/download
DOWNLOAD_URL=https://github.com/iomesh/zfs/archive/refs/tags/${ZFS_TAG}.zip
LIBUZFS_PC=${INSTALL_DIR}/lib/pkgconfig/libuzfs.pc

download_src() {
file=${DOWNLOAD_DIR}/${ZFS_ZIP}

[ -f ${file} ] && unzip -tq ${file} || {
wget -q -c ${DOWNLOAD_URL} -O $file || {
echo "download $file failure"
exit 1
}
}
}

unzip_src() {
unzip -q ${DOWNLOAD_DIR}/${ZFS_ZIP} -d ${DOWNLOAD_DIR} || {
echo "unzip ${ZFS_ZIP} failure"
exit 1
}

mv ${DOWNLOAD_DIR}/${ZFS_PKG} ${ZFS_DIR}
}

CFLAGS="-fPIC -O2 -ftls-model=initial-exec -fno-omit-frame-pointer -g"
if [ "${ENABLE_DEBUG}" = "yes" ]; then
CFLAGS="-fPIC -fno-omit-frame-pointer"
Expand All @@ -52,7 +30,7 @@ build_libuzfs_lib() {
}

setup_libuzfs_pc() {
mv ${LIBUZFS_PC} ${TOP_SRCDIR}/libuzfs.pc
cp ${LIBUZFS_PC} ${TOP_SRCDIR}/libuzfs.pc
}


Expand All @@ -65,16 +43,6 @@ setup_libuzfs_pc() {
exit 0
}

[ ! -d ${ZFS_DIR} ] && [ -f ${DOWNLOAD_DIR}/${ZFS_ZIP} ] && {
unzip_src
build_libuzfs_lib
setup_libuzfs_pc
exit 0
}

rm -rf ${ZFS_DIR} && rm -rf ${DOWNLOAD_DIR} && mkdir ${DOWNLOAD_DIR}

download_src
unzip_src
git submodule update --init
build_libuzfs_lib
setup_libuzfs_pc
File renamed without changes.
File renamed without changes.
39 changes: 21 additions & 18 deletions uzfs-sys/src/coroutine.rs → src/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ use std::{
};
use tokio::runtime::Runtime;

static id_task_handle_map: OnceCell<DashMap<u64, tokio::task::JoinHandle<()>>> = OnceCell::new();
static id_runtime_map: OnceCell<DashMap<u64, std::thread::JoinHandle<()>>> = OnceCell::new();
static uzfs_runtime: OnceCell<Runtime> = OnceCell::new();
static ID_TASK_HANDLE_MAP: OnceCell<DashMap<u64, tokio::task::JoinHandle<()>>> = OnceCell::new();
static ID_RUNTIME_MAP: OnceCell<DashMap<u64, std::thread::JoinHandle<()>>> = OnceCell::new();
static UZFS_RUNTIME: OnceCell<Runtime> = OnceCell::new();
// start from 1 to pass null pointer check
static current_task_id: AtomicU64 = AtomicU64::new(1);
static CURRENT_TASK_ID: AtomicU64 = AtomicU64::new(1);

pub static add_backtrace: OnceCell<fn(u64, String)> = OnceCell::new();
pub static remove_backtrace: OnceCell<fn(u64)> = OnceCell::new();
pub static add_creation_pos: OnceCell<fn(u64, String)> = OnceCell::new();
pub static remove_creation_pos: OnceCell<fn(u64)> = OnceCell::new();
pub static ADD_BACKTRACE: OnceCell<fn(u64, String)> = OnceCell::new();
pub static REMOVE_BACKTRACE: OnceCell<fn(u64)> = OnceCell::new();
pub static ADD_CREATION_POS: OnceCell<fn(u64, String)> = OnceCell::new();
pub static REMOVE_CREATION_POS: OnceCell<fn(u64)> = OnceCell::new();

#[cfg(debug_assertions)]
const MAX_BACKTRACE_DEPTH: u32 = 20;
Expand Down Expand Up @@ -55,14 +55,14 @@ impl UzfsCoroutineFuture {
// FIXME(sundengyu): pin arg to the address of arg to prevent future swap
#[allow(unused_variables)]
pub fn new(func: CoroutineFunc, arg: usize, foreground: bool, backtrace: bool) -> Self {
let task_id = current_task_id.fetch_add(1, Ordering::Relaxed);
let task_id = CURRENT_TASK_ID.fetch_add(1, Ordering::Relaxed);

#[cfg(not(debug_assertions))]
let record_backtrace = None;

#[cfg(debug_assertions)]
let record_backtrace = if backtrace {
if let Some(add_creation_pos_func) = add_creation_pos.get() {
if let Some(add_creation_pos_func) = ADD_CREATION_POS.get() {
add_creation_pos_func(task_id, UzfsCoroutineFuture::get_creation_pos(foreground));
}
Some(UzfsCoroutineFuture::record_backtrace as unsafe extern "C" fn(u64))
Expand Down Expand Up @@ -154,7 +154,7 @@ impl UzfsCoroutineFuture {
#[cfg(debug_assertions)]
unsafe extern "C" fn record_backtrace(task_id: u64) {
use std::fmt::Write;
if let Some(add_backtrace_func) = add_backtrace.get() {
if let Some(add_backtrace_func) = ADD_BACKTRACE.get() {
let mut bt_string = String::from("");
let mut depth = 0;
let mut begin_record = false;
Expand Down Expand Up @@ -205,17 +205,20 @@ impl Future for UzfsCoroutineFuture {
}

match unsafe { libuzfs_run_coroutine(self.uc) } {
#[allow(non_upper_case_globals)]
run_state_RUN_STATE_PENDING => {
waker_arg.state = CoroutineState::Pending;
Poll::Pending
}
#[allow(non_upper_case_globals)]
run_state_RUN_STATE_YIELDED => {
// use better way like context::defer to yield
let fut = tokio::task::yield_now();
tokio::pin!(fut);
let _ = fut.poll(cx);
Poll::Pending
}
#[allow(non_upper_case_globals)]
run_state_RUN_STATE_DONE => {
waker_arg.state = CoroutineState::Done;
Poll::Ready(())
Expand All @@ -239,10 +242,10 @@ impl Drop for UzfsCoroutineFuture {

#[cfg(debug_assertions)]
if self.backtrace {
if let Some(remove_backtrace_func) = remove_backtrace.get() {
if let Some(remove_backtrace_func) = REMOVE_BACKTRACE.get() {
remove_backtrace_func(self.task_id);
}
if let Some(remove_creation_pos_func) = remove_creation_pos.get() {
if let Some(remove_creation_pos_func) = REMOVE_CREATION_POS.get() {
remove_creation_pos_func(self.task_id);
}
}
Expand Down Expand Up @@ -277,12 +280,12 @@ pub unsafe extern "C" fn thread_create(
.unwrap();

if joinable != 0 {
id_runtime_map
ID_RUNTIME_MAP
.get_or_init(DashMap::new)
.insert(task_id, handle);
}
} else {
let handle = uzfs_runtime
let handle = UZFS_RUNTIME
.get_or_init(|| {
tokio::runtime::Builder::new_multi_thread()
.thread_name("uzfs-background")
Expand All @@ -292,7 +295,7 @@ pub unsafe extern "C" fn thread_create(
.spawn(coroutine);

if joinable != 0 {
id_task_handle_map
ID_TASK_HANDLE_MAP
.get_or_init(DashMap::new)
.insert(task_id, handle);
}
Expand All @@ -308,12 +311,12 @@ pub unsafe extern "C" fn thread_exit() {

#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn thread_join(task_id: u64) {
if let Some(map) = id_task_handle_map.get() {
if let Some(map) = ID_TASK_HANDLE_MAP.get() {
if map.remove(&task_id).is_some() {
panic!("async join not supported !!!");
}
}

let handle = id_runtime_map.get().unwrap().remove(&task_id).unwrap().1;
let handle = ID_RUNTIME_MAP.get().unwrap().remove(&task_id).unwrap().1;
handle.join().unwrap();
}
Loading

0 comments on commit b6d8344

Please sign in to comment.