From f4c2fcf0c53654d99ba0293d0b3fc65c1688b1a4 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sun, 6 Oct 2019 21:37:38 -0400 Subject: [PATCH] Finally able to terminate a driver. Need to write a test for it, but it's working locally. --- examples/do_terminate.rs | 7 +++++-- src/client/cnc_descriptor.rs | 7 +++++++ src/client/concurrent/ring_buffer.rs | 4 ++-- src/util.rs | 11 ++++++++--- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/examples/do_terminate.rs b/examples/do_terminate.rs index 81e818c..00b30bb 100644 --- a/examples/do_terminate.rs +++ b/examples/do_terminate.rs @@ -6,6 +6,7 @@ use aeron_rs::util::IndexT; use memmap::MmapOptions; use std::fs::OpenOptions; use std::mem::size_of; +use aeron_rs::client::cnc_descriptor; fn main() { let path = ClientContext::default_aeron_path(); @@ -22,8 +23,8 @@ fn main() { println!("MMap len: {}", mmap.len()); // When creating the buffer, we need to offset by the CnC metadata - let cnc_metadata_len = size_of::(); - println!("Buffer len: {}", mmap[cnc_metadata_len..].len()); + let cnc_metadata_len = cnc_descriptor::META_DATA_LENGTH; + println!("Buffer start: {}", cnc_metadata_len); // Read metadata to get buffer length let buffer_len = { @@ -31,6 +32,7 @@ fn main() { let metadata = atomic_buffer.overlay::(0).unwrap(); metadata.to_driver_buffer_length }; + println!("Buffer len: {}", buffer_len); let buffer_end = cnc_metadata_len + buffer_len as usize; let atomic_buffer = AtomicBuffer::wrap(&mut mmap[cnc_metadata_len..buffer_end]); @@ -43,6 +45,7 @@ fn main() { let mut source_buffer = AtomicBuffer::wrap(&mut terminate_bytes); let client_id = ring_buffer.next_correlation_id(); source_buffer.put_i64_ordered(0, client_id).unwrap(); + source_buffer.put_i64_ordered(8, -1).unwrap(); let term_id: i32 = 0x0E; ring_buffer diff --git a/src/client/cnc_descriptor.rs b/src/client/cnc_descriptor.rs index fbe07bc..410ab20 100644 --- a/src/client/cnc_descriptor.rs +++ b/src/client/cnc_descriptor.rs @@ -19,6 +19,9 @@ //! +-----------------------------+ //! ``` +use crate::util::bit; +use std::mem::size_of; + /// The CnC file metadata header. Layout: /// /// ```text @@ -61,6 +64,10 @@ pub struct MetaDataDefinition { _pid: i64, } +/// Length of the metadata block in a CnC file. Note that it's not equivalent +/// to the actual struct length. +pub const META_DATA_LENGTH: usize = bit::align_usize(size_of::(), bit::CACHE_LINE_LENGTH * 2); + /// Version code for the Aeron CnC file format pub const CNC_VERSION: i32 = crate::sematic_version_compose(0, 0, 16); diff --git a/src/client/concurrent/ring_buffer.rs b/src/client/concurrent/ring_buffer.rs index 4cbbaa8..fbc0982 100644 --- a/src/client/concurrent/ring_buffer.rs +++ b/src/client/concurrent/ring_buffer.rs @@ -5,9 +5,9 @@ use crate::util::{bit, AeronError, IndexT, Result}; /// Description of the Ring Buffer schema. pub mod buffer_descriptor { use crate::client::concurrent::atomic_buffer::AtomicBuffer; - use crate::util::bit::is_power_of_two; + use crate::util::bit::{is_power_of_two, CACHE_LINE_LENGTH}; use crate::util::AeronError::IllegalArgument; - use crate::util::{IndexT, Result, CACHE_LINE_LENGTH}; + use crate::util::{IndexT, Result}; // QUESTION: Why are these offsets so large when we only ever use i64 types? diff --git a/src/util.rs b/src/util.rs index db0dfdc..534f5c5 100644 --- a/src/util.rs +++ b/src/util.rs @@ -6,9 +6,6 @@ // QUESTION: Can this just be updated to be `usize` in Rust? pub type IndexT = i32; -/// Length of the data blocks used by the CPU cache sub-system in bytes -pub const CACHE_LINE_LENGTH: usize = 64; - /// Error types from operations in the Aeron client. Synonymous with the exceptions /// generated by the C++ client. #[derive(Debug, PartialEq)] @@ -29,6 +26,9 @@ pub mod bit { use crate::util::IndexT; use num::PrimInt; + /// Length of the data blocks used by the CPU cache sub-system in bytes + pub const CACHE_LINE_LENGTH: usize = 64; + /// Helper method for quick verification that `IndexT` is a positive power of two /// /// ```rust @@ -56,4 +56,9 @@ pub mod bit { { (val + (alignment - T::one())) & !(alignment - T::one()) } + + /// Align a `usize` value. See `align` for similar functionality on general types. + pub const fn align_usize(val: usize, alignment: usize) -> usize { + (val + (alignment - 1)) & !(alignment - 1) + } }