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

Expose the scheduling API to Rust #34

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions seastar/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ static CXX_BRIDGES: &[&str] = &[
"src/smp.rs",
"src/distributed.rs",
"src/file.rs",
"src/scheduling.rs",
];

static CXX_CPP_SOURCES: &[&str] = &[
Expand All @@ -26,6 +27,7 @@ static CXX_CPP_SOURCES: &[&str] = &[
"src/smp.cc",
"src/distributed.cc",
"src/file.cc",
"src/scheduling.cc",
];

fn main() {
Expand Down
58 changes: 58 additions & 0 deletions seastar/src/clocks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::cxx_async_futures::VoidFuture;
use crate::SchedulingGroup;
use core::cmp::Ordering;
use cxx::UniquePtr;
use std::fmt;
Expand Down Expand Up @@ -37,6 +38,13 @@ mod ffi {
fn manual_sleep(nanos: i64) -> VoidFuture;
}

#[namespace = "seastar_ffi::scheduling"]
unsafe extern "C++" {
include!("seastar/src/scheduling.hh");

type scheduling_group = crate::scheduling_group;
}

#[namespace = "seastar_ffi::timer::steady_clock"]
unsafe extern "C++" {
include!("seastar/src/timer.hh");
Expand All @@ -52,6 +60,14 @@ mod ffi {
dropper: unsafe fn(*mut u8),
);

unsafe fn sct_set_callback_under_group(
timer: Pin<&mut steady_clock_timer>,
callback: *mut u8,
caller: unsafe fn(*mut u8),
dropper: unsafe fn(*mut u8),
sg: &scheduling_group,
);

fn sct_arm_at(timer: Pin<&mut steady_clock_timer>, at: i64);
fn sct_arm_at_periodic(timer: Pin<&mut steady_clock_timer>, at: i64, period: i64);

Expand Down Expand Up @@ -80,6 +96,14 @@ mod ffi {
dropper: unsafe fn(*mut u8),
);

unsafe fn lct_set_callback_under_group(
timer: Pin<&mut lowres_clock_timer>,
callback: *mut u8,
caller: unsafe fn(*mut u8),
dropper: unsafe fn(*mut u8),
sg: &scheduling_group,
);

fn lct_arm_at(timer: Pin<&mut lowres_clock_timer>, at: i64);
fn lct_arm_at_periodic(timer: Pin<&mut lowres_clock_timer>, at: i64, period: i64);

Expand Down Expand Up @@ -108,6 +132,14 @@ mod ffi {
dropper: unsafe fn(*mut u8),
);

unsafe fn mct_set_callback_under_group(
timer: Pin<&mut manual_clock_timer>,
callback: *mut u8,
caller: unsafe fn(*mut u8),
dropper: unsafe fn(*mut u8),
sg: &scheduling_group,
);

fn mct_arm_at(timer: Pin<&mut manual_clock_timer>, at: i64);
fn mct_arm_at_periodic(timer: Pin<&mut manual_clock_timer>, at: i64, period: i64);

Expand Down Expand Up @@ -569,6 +601,14 @@ mod clock_implementation {
dropper: fn(*mut u8),
);

fn set_callback_under_group(
timer: &mut Self::CppTimer,
callback: *mut u8,
caller: fn(*mut u8),
dropper: fn(*mut u8),
sg: &SchedulingGroup,
);

fn arm_at(timer: &mut Self::CppTimer, at: i64);

fn arm_at_periodic(timer: &mut Self::CppTimer, at: i64, period: i64);
Expand Down Expand Up @@ -614,6 +654,24 @@ macro_rules! timer_impl {
}
}

fn set_callback_under_group(
timer: &mut Self::CppTimer,
callback: *mut u8,
caller: fn(*mut u8),
dropper: fn(*mut u8),
sg: &SchedulingGroup,
) {
unsafe {
[<$ffi_pref _set_callback_under_group>](
timer.pin_mut(),
callback,
caller,
dropper,
&sg.inner,
);
}
}

fn arm_at(timer: &mut Self::CppTimer, at: i64) {
[<$ffi_pref _arm_at>](timer.pin_mut(), at);
}
Expand Down
2 changes: 2 additions & 0 deletions seastar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod ffi_utils;
mod file;
mod gate;
mod preempt;
mod scheduling;
#[doc(hidden)]
pub mod seastar_test_guard;
mod sleep;
Expand All @@ -30,6 +31,7 @@ pub use distributed::*;
pub use file::*;
pub use gate::*;
pub use preempt::*;
pub use scheduling::*;
pub use sleep::*;
pub use smp::*;
pub use spawn::*;
Expand Down
56 changes: 56 additions & 0 deletions seastar/src/scheduling.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "scheduling.hh"

namespace seastar_ffi {
namespace scheduling {

std::shared_ptr<scheduling_group> new_sg() {
return std::make_unique<scheduling_group>();
}

bool sg_active(const scheduling_group& sg) {
return sg.active();
}

rust::str sg_name(const scheduling_group& sg) {
const seastar::sstring& sg_name = sg.name();
return rust::Str(sg_name.begin(), sg_name.size());
}

bool sg_is_main(const scheduling_group& sg) {
return sg.is_main();
}

void sg_set_shares(const std::shared_ptr<scheduling_group>& sg, float shares) {
sg->set_shares(shares);
}

bool sg_equal(const scheduling_group& sg1, const scheduling_group& sg2) {
return sg1 == sg2;
}

VoidFuture create_sg(std::shared_ptr<scheduling_group>& sg, rust::str name, float shares) {
seastar::sstring s_name(name.begin(), name.size());
scheduling_group new_sg = co_await seastar::create_scheduling_group(s_name, shares);
sg = std::make_shared<scheduling_group>(std::move(new_sg));
}

VoidFuture destroy_sg(const std::shared_ptr<scheduling_group>& sg) {
co_await seastar::destroy_scheduling_group(*sg);
}

VoidFuture rename_sg(const std::shared_ptr<scheduling_group>& sg, rust::str new_name) {
seastar::sstring s_name(new_name.begin(), new_name.size());
co_await seastar::rename_scheduling_group(*sg, s_name);
}

uint32_t max_sg() {
return (uint32_t) seastar::max_scheduling_groups();
}

std::shared_ptr<scheduling_group> current_sg() {
scheduling_group sg = seastar::current_scheduling_group();
return std::make_shared<scheduling_group>(std::move(sg));
}

} // namespace scheduling
} // namespace seastar_ffi
34 changes: 34 additions & 0 deletions seastar/src/scheduling.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include "cxx_async_futures.hh"
#include <seastar/core/scheduling.hh>

namespace seastar_ffi {
namespace scheduling {

using scheduling_group = seastar::scheduling_group;

std::shared_ptr<scheduling_group> new_sg();

bool sg_active(const scheduling_group& sg);

rust::str sg_name(const scheduling_group& sg);

bool sg_is_main(const scheduling_group& sg);

void sg_set_shares(const std::shared_ptr<scheduling_group>& sg, float shares);

bool sg_equal(const scheduling_group& sg1, const scheduling_group& sg2);

VoidFuture create_sg(std::shared_ptr<scheduling_group>& sg, rust::str name, float shares);

VoidFuture destroy_sg(const std::shared_ptr<scheduling_group>& sg);

VoidFuture rename_sg(const std::shared_ptr<scheduling_group>& sg, rust::str new_name);

uint32_t max_sg();

std::shared_ptr<scheduling_group> current_sg();

} // namespace scheduling
} // namespace seastar_ffi
Loading