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

ffi: impl Sync, Send for MCache, DCache, FSeq #588

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 3 additions & 3 deletions config/with-optimization.mk
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
CPPFLAGS+=-O3 -ffast-math -fno-associative-math -fno-reciprocal-math
CPPFLAGS+=-DFD_HAS_OPTIMIZATION=1
FD_HAS_OPTIMIZATION:=1
# CPPFLAGS+=-O3 -ffast-math -fno-associative-math -fno-reciprocal-math
# CPPFLAGS+=-DFD_HAS_OPTIMIZATION=1
# FD_HAS_OPTIMIZATION:=1
File renamed without changes.
2 changes: 1 addition & 1 deletion Cargo.toml → ffi/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["ffi/rust/firedancer-sys", "ffi/rust/firedancer-diff"]
members = ["ffi/rust/firedancer", "ffi/rust/firedancer-sys", "ffi/rust/firedancer-diff"]
resolver = "2"

[profile.dev]
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions ffi/rust/firedancer-sys/src/tango/cnc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ pub use crate::generated::{
fd_cnc_leave,
fd_cnc_new,
fd_cnc_open,
fd_cnc_signal,
fd_cnc_signal_cstr,
fd_cnc_signal_query,
fd_cnc_strerror,
fd_cnc_t,
fd_cnc_type,
Expand Down
6 changes: 6 additions & 0 deletions ffi/rust/firedancer-sys/src/tango/fctl.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
pub use crate::generated::{
fd_fctl_cfg_done,
fd_fctl_cfg_rx_add,
fd_fctl_delete,
fd_fctl_join,
fd_fctl_leave,
fd_fctl_new,
fd_fctl_private_rx_t,
fd_fctl_rx_cr_return,
fd_fctl_t,
fd_fctl_tx_cr_update,
FD_FCTL_ALIGN,
FD_FCTL_RX_MAX_MAX,
};
1 change: 1 addition & 0 deletions ffi/rust/firedancer-sys/src/tango/fseq.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub use crate::generated::{
fd_fseq_align,
fd_fseq_app_laddr,
fd_fseq_app_laddr_const,
fd_fseq_delete,
fd_fseq_footprint,
Expand Down
3 changes: 3 additions & 0 deletions ffi/rust/firedancer-sys/src/tango/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ pub use fseq::*;
pub use mcache::*;
pub use tcache::*;
pub use xdp::*;

pub use crate::generated::fd_chunk_to_laddr;
pub use crate::generated::fd_chunk_to_laddr_const;
4 changes: 4 additions & 0 deletions ffi/rust/firedancer-sys/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
mod bits;
mod log;
mod pod;
mod rng;
mod shmem;
mod tempo;
mod tile;
mod wksp;

pub use bits::*;
pub use log::*;
pub use pod::*;
pub use rng::*;
pub use shmem::*;
pub use tempo::*;
pub use tile::*;
pub use wksp::*;

Expand Down
2 changes: 2 additions & 0 deletions ffi/rust/firedancer-sys/src/util/pod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub use crate::generated::{
fd_pod_cnt_subpod,
fd_pod_info_t,
fd_pod_join,
fd_pod_leave,
fd_pod_query,
fd_pod_query_buf,
fd_pod_query_char,
Expand Down
19 changes: 19 additions & 0 deletions ffi/rust/firedancer-sys/src/util/rng.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pub use crate::generated::{
fd_rng_delete,
fd_rng_idx,
fd_rng_idx_set,
fd_rng_int,
fd_rng_join,
fd_rng_leave,
fd_rng_long,
fd_rng_new,
fd_rng_schar,
fd_rng_seq,
fd_rng_seq_set,
fd_rng_short,
fd_rng_t,
fd_rng_uchar,
fd_rng_uint,
fd_rng_ulong,
fd_rng_ushort,
};
6 changes: 6 additions & 0 deletions ffi/rust/firedancer-sys/src/util/tempo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub use crate::generated::{
fd_tempo_async_min,
fd_tempo_async_reload,
fd_tempo_lazy_default,
fd_tempo_tick_per_ns,
};
1 change: 1 addition & 0 deletions ffi/rust/firedancer-sys/src/util/wksp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ pub use crate::generated::{
fd_wksp_pod_map,
fd_wksp_pod_unmap,
fd_wksp_t,
fd_wksp_private,
fd_wksp_unmap,
};
1 change: 1 addition & 0 deletions ffi/rust/firedancer-sys/wrapper_util.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "src/util/fd_util.h"
#include "src/util/wksp/fd_wksp_private.h"

#if FD_MCACHE_LG_INTERLEAVE
#error "FD_MCACHE_LG_INTERLEAVE unsupported"
Expand Down
11 changes: 11 additions & 0 deletions ffi/rust/firedancer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "firedancer"
version = "0.1.0"
edition = "2021"
authors = ["Firedancer Contributors <[email protected]>"]
description = "Safe high level wrappers for Firedancer"
license = "Apache-2.0"

[dependencies]
paste = "1.0.12"
firedancer-sys = { path = "../firedancer-sys" }
42 changes: 42 additions & 0 deletions ffi/rust/firedancer/src/bits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
macro_rules! align_up {
( $x:expr, $a:expr ) => {
($x + ($a - 1)) & !($a - 1)
};
}

macro_rules! layout {
( value=$value:expr, ) => { $value };
( value=$value:expr, ($align:expr, $size:expr), $($tail:tt)*) => {
layout!( value=align_up!($value, $align) + $size, $($tail)*)
};
( align=$align:expr, [ $($tail:tt)* ]) => {
align_up!(layout!(value = 0, $($tail)*), $align)
};
}

pub(crate) use {
align_up,
layout,
};

#[cfg(test)]
mod tests {
#[test]
fn test_align_up() {
let zeros = 0u64;
let ones = u64::MAX;

for i in 0..64 {
let align = 1u64 << i;
let lo = (1u64 << i) - 1;
let hi = !lo;

assert_eq!(align_up!(zeros, align), zeros);
assert_eq!(align_up!(ones, align), if i == 0 { ones } else { zeros });
for j in 0..64 {
let x = 1u64 << j;
assert_eq!(align_up!(x, align), (x + lo) & hi);
}
}
}
}
78 changes: 78 additions & 0 deletions ffi/rust/firedancer/src/cnc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use std::ffi::c_ulong;
use std::ptr::{
read_volatile,
write_volatile,
};
use firedancer_sys::*;

use crate::*;

pub struct Cnc {
laddr: *mut tango::fd_cnc_t,
diagnostic: *mut c_ulong,
_workspace: Workspace,
}

impl Drop for Cnc {
fn drop(&mut self) {
unsafe { tango::fd_cnc_leave(self.laddr) };
}
}

#[repr(u32)]
#[derive(Copy, Clone, Debug)]
pub enum CncSignal {
Run = tango::FD_CNC_SIGNAL_RUN,
Boot = tango::FD_CNC_SIGNAL_BOOT,
Fail = tango::FD_CNC_SIGNAL_FAIL,
Halt = tango::FD_CNC_SIGNAL_HALT,
}

#[repr(u32)]
#[derive(Copy, Clone, Debug)]
pub enum CncDiag {
InBackpressure = tango::FD_CNC_DIAG_IN_BACKP,
BackpressureCount = tango::FD_CNC_DIAG_BACKP_CNT,
}

impl Cnc {
pub unsafe fn join<T: Into<GlobalAddress>>(gaddr: T) -> Result<Self, ()> {
let workspace = Workspace::map(gaddr)?;
let laddr = tango::fd_cnc_join(workspace.laddr.as_ptr());
if laddr.is_null() {
Err(())
} else {
let diagnostic = tango::fd_cnc_app_laddr(laddr) as *mut c_ulong;
if diagnostic.is_null() {
Err(())
} else {
Ok(Self {
laddr,
diagnostic,
_workspace: workspace,
})
}
}
}

pub fn query(&self) -> u64 {
unsafe { tango::fd_cnc_signal_query(self.laddr) }
}

pub fn signal(&self, signal: u64) {
unsafe { tango::fd_cnc_signal(self.laddr, signal) }
}

pub unsafe fn set(&self, diag: u64, value: u64) {
write_volatile(self.diagnostic.offset(diag as isize), value)
}

pub unsafe fn increment(&self, diag: u64, value: u64) {
let offset = self.diagnostic.offset(diag as isize);
write_volatile(offset, read_volatile(offset) + value)
}

pub fn heartbeat(&self, now: i64) {
unsafe { tango::fd_cnc_heartbeat(self.laddr, now) }
}
}
42 changes: 42 additions & 0 deletions ffi/rust/firedancer/src/dcache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use firedancer_sys::*;

use std::ffi::c_void;

use crate::*;

pub struct DCache {
laddr: *mut u8,
wksp: *mut util::fd_wksp_t,
_workspace: Workspace,
}

impl Drop for DCache {
fn drop(&mut self) {
unsafe { tango::fd_dcache_leave(self.laddr) };
}
}

unsafe impl Sync for DCache {}
unsafe impl Send for DCache {}

impl DCache {
pub unsafe fn join<T: Into<GlobalAddress>>(gaddr: T) -> Result<Self, ()> {
let workspace = Workspace::map(gaddr)?;
let laddr = tango::fd_dcache_join(workspace.laddr.as_ptr());
if laddr.is_null() {
return Err(());
}

let wksp = util::fd_wksp_containing(laddr as *const c_void);
Ok(Self {
laddr,
wksp,
_workspace: workspace,
})
}

pub unsafe fn slice<'a>(&self, chunk: u64, offset: u64, len: u64) -> &'a[u8] {
let laddr = tango::fd_chunk_to_laddr_const(self.wksp as *const c_void, chunk);
std::slice::from_raw_parts(laddr.offset(offset as isize) as *const u8, len as usize)
}
}
99 changes: 99 additions & 0 deletions ffi/rust/firedancer/src/fctl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use std::ffi::c_void;
use std::marker::PhantomData;
use std::mem::{
align_of,
size_of,
};
use std::ptr::null_mut;

use firedancer_sys::*;

use crate::*;

macro_rules! footprint {
( $rx_max:expr ) => {
layout!(
align = tango::FD_FCTL_ALIGN as usize,
[
(
align_of::<tango::fd_fctl_t>(),
size_of::<tango::fd_fctl_t>()
),
(
align_of::<tango::fd_fctl_private_rx_t>(),
$rx_max * size_of::<tango::fd_fctl_private_rx_t>()
),
]
)
};
}

pub struct FCtl<'a, 'b> {
_stack: [u8; footprint!(1)],
shmem: *mut c_void,
fctl: *mut tango::fd_fctl_t,
_seq: PhantomData<&'a u64>,
_slow: PhantomData<&'b mut u64>,
}

impl<'a, 'b> Drop for FCtl<'a, 'b> {
fn drop(&mut self) {
unsafe { tango::fd_fctl_leave(self.fctl) };
unsafe { tango::fd_fctl_delete(self.shmem) };
}
}

impl<'a, 'b> FCtl<'a, 'b> {
pub fn new(
cr_burst: u64,
cr_max: u64,
cr_resume: u64,
cr_refill: u64,
fseq: &FSeq,
) -> Result<Self, ()> {
let mut stack = [0; footprint!(1usize)];
let shmem = unsafe { tango::fd_fctl_new(&mut stack as *mut _ as *mut c_void, 1) };
if shmem.is_null() {
return Err(());
}

let fctl = unsafe { tango::fd_fctl_join(shmem) };
if fctl.is_null() {
return Err(());
}

let fctl = unsafe {
tango::fd_fctl_cfg_rx_add(
fctl,
cr_max,
fseq.laddr,
fseq.diagnostic.offset(FSeqDiag::SlowCount as isize),
)
};
if fctl.is_null() {
return Err(());
}

let fctl = unsafe { tango::fd_fctl_cfg_done(fctl, cr_burst, cr_max, cr_resume, cr_refill) };

Ok(FCtl {
_stack: stack,
shmem,
fctl,
_seq: PhantomData,
_slow: PhantomData,
})
}

pub fn tx_cr_update(&self, cr_avail: u64, mcache: &MCache) -> u64 {
unsafe { tango::fd_fctl_tx_cr_update(self.fctl, cr_avail, mcache.sequence_number) }
}
}

pub fn housekeeping_default_interval_nanos(cr_max: u64) -> i64 {
unsafe { util::fd_tempo_lazy_default(cr_max) }
}

pub fn minimum_housekeeping_tick_interval(lazy: i64) -> u64 {
unsafe { util::fd_tempo_async_min(lazy, 1, util::fd_tempo_tick_per_ns(null_mut()) as f32) }
}
Loading
Loading