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

Shm ci #593

Merged
merged 6 commits into from
Aug 23, 2024
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
11 changes: 7 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]
shm: [false, true]
unstable: [false, true]

steps:
- uses: actions/checkout@v4
Expand All @@ -50,7 +52,7 @@ jobs:
shell: bash
run: |
mkdir -p build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/local
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/local -DZENOHC_BUILD_WITH_SHARED_MEMORY=${{ matrix.shm }} -DZENOHC_BUILD_WITH_UNSTABLE_API=${{ matrix.unstable }}
cmake --build . --target install --config Release

- name: Install valgrind
Expand All @@ -61,22 +63,22 @@ jobs:
shell: bash
run: |
cd build
cmake .. -DZENOHC_LIB_STATIC=FALSE -DCMAKE_BUILD_TYPE=Release
cmake .. -DZENOHC_LIB_STATIC=FALSE -DCMAKE_BUILD_TYPE=Release -DZENOHC_BUILD_WITH_SHARED_MEMORY=${{ matrix.shm }} -DZENOHC_BUILD_WITH_UNSTABLE_API=${{ matrix.unstable }}
cmake --build . --target tests --config Release
ctest -C Release --output-on-failure -E "(unit_z_api_alignment_test|build_z_build_static)"

- name: Build cmake tests with C++ compiler to make sure that C API is C++ compatible
shell: bash
run: |
cd build
cmake .. -DZENOHC_LIB_STATIC=TRUE -DCMAKE_BUILD_TYPE=Debug -DZENOHC_BUILD_TESTS_WITH_CXX=TRUE
cmake .. -DZENOHC_LIB_STATIC=TRUE -DCMAKE_BUILD_TYPE=Debug -DZENOHC_BUILD_TESTS_WITH_CXX=TRUE -DZENOHC_BUILD_WITH_SHARED_MEMORY=${{ matrix.shm }} -DZENOHC_BUILD_WITH_UNSTABLE_API=${{ matrix.unstable }}
cmake --build . --target tests --config Debug

- name: Run cmake tests with zenoh-c as static library
shell: bash
run: |
cd build
cmake .. -DZENOHC_LIB_STATIC=TRUE -DCMAKE_BUILD_TYPE=Release
cmake .. -DZENOHC_LIB_STATIC=TRUE -DCMAKE_BUILD_TYPE=Release -DZENOHC_BUILD_WITH_SHARED_MEMORY=${{ matrix.shm }} -DZENOHC_BUILD_WITH_UNSTABLE_API=${{ matrix.unstable }}
cmake --build . --target tests --config Release
ctest -C Release --output-on-failure -E "(unit_z_api_alignment_test|build_z_build_shared)"

Expand Down Expand Up @@ -106,6 +108,7 @@ jobs:
run: cargo test --verbose --release --features=logger-autoinit

- name: Upload artifact
if: ${{ matrix.unstable == 'false' && matrix.shm == 'false' }}
uses: actions/upload-artifact@v4
with:
# Artifact name
Expand Down
2 changes: 1 addition & 1 deletion examples/z_get_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ int main(int argc, char** argv) {
// Allocate SHM Buffer
z_buf_layout_alloc_result_t alloc;
z_shm_provider_alloc(&alloc, z_loan(provider), value_len, alignment);
if (!z_check(alloc.buf)) {
if (alloc.status != ZC_BUF_LAYOUT_ALLOC_STATUS_OK) {
printf("Unexpected failure during SHM buffer allocation...");
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/z_ping_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ int main(int argc, char** argv) {
// Allocate SHM Buffer
z_buf_layout_alloc_result_t alloc;
z_shm_provider_alloc(&alloc, z_loan(provider), args.size, alignment);
if (!z_check(alloc.buf)) {
if (alloc.status != ZC_BUF_LAYOUT_ALLOC_STATUS_OK) {
printf("Unexpected failure during SHM buffer allocation...");
return -1;
}
Expand Down
31 changes: 24 additions & 7 deletions examples/z_pub_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,22 @@
#define DEFAULT_VALUE "Pub from C!"

struct args_t {
char* keyexpr; // -k
char* value; // -v
char* keyexpr; // -k
char* value; // -v
bool add_matching_listener; // --add-matching-listener
};
struct args_t parse_args(int argc, char** argv, z_owned_config_t* config);

#ifdef UNSTABLE
void matching_status_handler(const zc_matching_status_t* matching_status, void* arg) {
if (matching_status->matching) {
printf("Subscriber matched\n");
} else {
printf("No Subscribers matched\n");
}
}
#endif

int main(int argc, char** argv) {
z_owned_config_t config;
struct args_t args = parse_args(argc, argv, &config);
Expand All @@ -48,7 +59,7 @@ int main(int argc, char** argv) {
}
#ifdef UNSTABLE
zc_owned_matching_listener_t listener;
if (add_matching_listener) {
if (args.add_matching_listener) {
zc_owned_closure_matching_status_t callback;
z_closure(&callback, matching_status_handler, NULL, NULL);
zc_publisher_matching_listener_declare(&listener, z_loan(pub), z_move(callback));
Expand Down Expand Up @@ -76,7 +87,7 @@ int main(int argc, char** argv) {

z_buf_layout_alloc_result_t alloc;
z_shm_provider_alloc_gc_defrag_blocking(&alloc, z_loan(provider), buf_ok_size, alignment);
if (z_check(alloc.buf)) {
if (alloc.status == ZC_BUF_LAYOUT_ALLOC_STATUS_OK) {
{
uint8_t* buf = z_shm_mut_data_mut(z_loan_mut(alloc.buf));
sprintf((char*)buf, "[%4d] %s", idx, args.value);
Expand All @@ -97,7 +108,7 @@ int main(int argc, char** argv) {
}

#ifdef UNSTABLE
if (add_matching_listener) {
if (args.add_matching_listener) {
zc_publisher_matching_listener_undeclare(z_move(listener));
}
#endif
Expand Down Expand Up @@ -138,8 +149,13 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) {
if (!value) {
value = DEFAULT_VALUE;
}
const char* arg = parse_opt(argc, argv, "add-matching-listener", false);
bool add_matching_listener = false;
if (arg) {
add_matching_listener = true;
}
parse_zenoh_common_args(argc, argv, config);
const char* arg = check_unknown_opts(argc, argv);
arg = check_unknown_opts(argc, argv);
if (arg) {
printf("Unknown option %s\n", arg);
exit(-1);
Expand All @@ -151,5 +167,6 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) {
exit(-1);
}
free(pos_args);
return (struct args_t){.keyexpr = (char*)keyexpr, .value = (char*)value};
return (struct args_t){
.keyexpr = (char*)keyexpr, .value = (char*)value, .add_matching_listener = add_matching_listener};
}
2 changes: 1 addition & 1 deletion examples/z_pub_shm_thr.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ int main(int argc, char **argv) {
printf("Allocating single SHM buffer\n");
z_buf_layout_alloc_result_t alloc;
z_shm_provider_alloc(&alloc, z_loan(provider), len, alignment);
if (!z_check(alloc.buf)) {
if (alloc.status != ZC_BUF_LAYOUT_ALLOC_STATUS_OK) {
printf("Unexpected failure during SHM buffer allocation...\n");
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/z_queryable_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void query_handler(const z_loaned_query_t *query, void *context) {
z_alloc_alignment_t alignment = {0};
z_buf_layout_alloc_result_t alloc;
z_shm_provider_alloc_gc_defrag_blocking(&alloc, provider, value_len, alignment);
if (z_check(alloc.buf)) {
if (alloc.status == ZC_BUF_LAYOUT_ALLOC_STATUS_OK) {
{
uint8_t *buf = z_shm_mut_data_mut(z_loan_mut(alloc.buf));
memcpy(buf, value, value_len);
Expand Down
59 changes: 58 additions & 1 deletion include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,50 @@ typedef enum z_whatami_t {
Z_WHATAMI_PEER = 2,
Z_WHATAMI_CLIENT = 4,
} z_whatami_t;
/**
* Status of SHM buffer allocation operation
*/
#if (defined(SHARED_MEMORY) && defined(UNSTABLE))
typedef enum zc_buf_alloc_status_t {
#if (defined(SHARED_MEMORY) && defined(UNSTABLE))
/**
* Allocation ok
*/
ZC_BUF_ALLOC_STATUS_OK = 0,
#endif
#if (defined(SHARED_MEMORY) && defined(UNSTABLE))
/**
* Allocation error
*/
ZC_BUF_ALLOC_STATUS_ALLOC_ERROR = 1,
#endif
} zc_buf_alloc_status_t;
#endif
/**
* Status of SHM buffer layouting + allocation operation
*/
#if (defined(SHARED_MEMORY) && defined(UNSTABLE))
typedef enum zc_buf_layout_alloc_status_t {
#if (defined(SHARED_MEMORY) && defined(UNSTABLE))
/**
* Allocation ok
*/
ZC_BUF_LAYOUT_ALLOC_STATUS_OK = 0,
#endif
#if (defined(SHARED_MEMORY) && defined(UNSTABLE))
/**
* Allocation error
*/
ZC_BUF_LAYOUT_ALLOC_STATUS_ALLOC_ERROR = 1,
#endif
#if (defined(SHARED_MEMORY) && defined(UNSTABLE))
/**
* Layouting error
*/
ZC_BUF_LAYOUT_ALLOC_STATUS_LAYOUT_ERROR = 2,
#endif
} zc_buf_layout_alloc_status_t;
#endif
/**
* The locality of samples to be received by subscribers or targeted by publishers.
*/
Expand Down Expand Up @@ -268,8 +312,12 @@ typedef enum zc_reply_keyexpr_t {
ZC_REPLY_KEYEXPR_MATCHING_QUERY = 1,
} zc_reply_keyexpr_t;
#endif
/**
* A result of SHM buffer allocation operation
*/
#if (defined(SHARED_MEMORY) && defined(UNSTABLE))
typedef struct z_buf_alloc_result_t {
enum zc_buf_alloc_status_t status;
z_owned_shm_mut_t buf;
enum z_alloc_error_t error;
} z_buf_alloc_result_t;
Expand Down Expand Up @@ -569,6 +617,12 @@ typedef struct z_subscriber_options_t {
*/
enum z_reliability_t reliability;
#endif
#if !defined(UNSTABLE)
/**
* Dummy field to avoid having fieldless struct
*/
uint8_t _0;
#endif
} z_subscriber_options_t;
/**
* Options passed to the `z_delete()` function.
Expand Down Expand Up @@ -897,10 +951,13 @@ typedef struct zc_shm_client_callbacks_t {
bool (*attach_fn)(struct z_shm_segment_t *out_segment, z_segment_id_t segment_id, void *context);
} zc_shm_client_callbacks_t;
#endif
/**
* A result of SHM buffer layouting + allocation operation
*/
#if (defined(SHARED_MEMORY) && defined(UNSTABLE))
typedef struct z_buf_layout_alloc_result_t {
enum zc_buf_layout_alloc_status_t status;
z_owned_shm_mut_t buf;
bool error_is_alloc;
enum z_alloc_error_t alloc_error;
enum z_layout_error_t layout_error;
} z_buf_layout_alloc_result_t;
Expand Down
2 changes: 2 additions & 0 deletions splitguide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ zenoh_opaque.h:
- z_owned_alloc_layout_t!#shared-memory#unstable
- z_loaned_alloc_layout_t!#shared-memory#unstable
- z_buf_alloc_result_t!#shared-memory#unstable
- zc_buf_alloc_status_t!#shared-memory#unstable
- z_alloc_alignment_t!#shared-memory#unstable
- z_chunk_descriptor_t!#shared-memory#unstable
- z_allocated_chunk_t!#shared-memory#unstable
- zc_shm_segment_callbacks_t!#shared-memory#unstable
- z_shm_segment_t!#shared-memory#unstable
- zc_shm_client_callbacks_t!#shared-memory#unstable
- z_buf_layout_alloc_result_t!#shared-memory#unstable
- zc_buf_layout_alloc_status_t!#shared-memory#unstable
- zc_shm_provider_backend_callbacks_t!#shared-memory#unstable
- z_layout_error_t!#shared-memory#unstable
- z_alloc_error_t!#shared-memory#unstable
Expand Down
37 changes: 32 additions & 5 deletions src/shm/provider/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,20 @@ pub extern "C" fn z_chunk_alloc_result_drop(this_: &mut z_moved_chunk_alloc_resu
let _ = this_.take_rust_type();
}

/// Status of SHM buffer allocation operation
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub enum zc_buf_alloc_status_t {
/// Allocation ok
OK = 0,
/// Allocation error
ALLOC_ERROR = 1,
}

/// A result of SHM buffer allocation operation
#[repr(C)]
pub struct z_buf_alloc_result_t {
status: zc_buf_alloc_status_t,
buf: z_owned_shm_mut_t,
error: z_alloc_error_t,
}
Expand All @@ -262,6 +274,7 @@ impl From<BufAllocResult> for z_buf_alloc_result_t {
Ok(val) => {
buf.as_rust_type_mut_uninit().write(Some(val));
Self {
status: zc_buf_alloc_status_t::OK,
// SAFETY: this is safe because buf is gravestone-initialized above
buf: unsafe { buf.assume_init() },
error: z_alloc_error_t::OTHER,
Expand All @@ -270,6 +283,7 @@ impl From<BufAllocResult> for z_buf_alloc_result_t {
Err(error) => {
z_internal_shm_mut_null(&mut buf);
Self {
status: zc_buf_alloc_status_t::ALLOC_ERROR,
// SAFETY: this is safe because buf is gravestone-initialized above
buf: unsafe { buf.assume_init() },
error: error.into(),
Expand All @@ -279,10 +293,23 @@ impl From<BufAllocResult> for z_buf_alloc_result_t {
}
}

/// Status of SHM buffer layouting + allocation operation
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub enum zc_buf_layout_alloc_status_t {
/// Allocation ok
OK = 0,
/// Allocation error
ALLOC_ERROR = 1,
/// Layouting error
LAYOUT_ERROR = 2,
}

/// A result of SHM buffer layouting + allocation operation
#[repr(C)]
pub struct z_buf_layout_alloc_result_t {
status: zc_buf_layout_alloc_status_t,
buf: z_owned_shm_mut_t,
error_is_alloc: bool,
alloc_error: z_alloc_error_t,
layout_error: z_layout_error_t,
}
Expand All @@ -294,9 +321,9 @@ impl From<BufLayoutAllocResult> for z_buf_layout_alloc_result_t {
Ok(val) => {
buf.as_rust_type_mut_uninit().write(Some(val));
Self {
// SAFETY: this is safe because buf is gravestone-initialized above
status: zc_buf_layout_alloc_status_t::OK,
// SAFETY: this is safe because buf is initialized above
buf: unsafe { buf.assume_init() },
error_is_alloc: false,
alloc_error: z_alloc_error_t::OTHER,
layout_error: z_layout_error_t::PROVIDER_INCOMPATIBLE_LAYOUT,
}
Expand All @@ -306,18 +333,18 @@ impl From<BufLayoutAllocResult> for z_buf_layout_alloc_result_t {
match error {
zenoh::shm::ZLayoutAllocError::Alloc(alloc) => {
Self {
status: zc_buf_layout_alloc_status_t::ALLOC_ERROR,
// SAFETY: this is safe because buf is gravestone-initialized above
buf: unsafe { buf.assume_init() },
error_is_alloc: true,
alloc_error: alloc.into(),
layout_error: z_layout_error_t::PROVIDER_INCOMPATIBLE_LAYOUT,
}
}
zenoh::shm::ZLayoutAllocError::Layout(layout) => {
Self {
status: zc_buf_layout_alloc_status_t::LAYOUT_ERROR,
// SAFETY: this is safe because buf is gravestone-initialized above
buf: unsafe { buf.assume_init() },
error_is_alloc: false,
alloc_error: z_alloc_error_t::OTHER,
layout_error: layout.into(),
}
Expand Down
5 changes: 5 additions & 0 deletions src/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ pub struct z_subscriber_options_t {
/// The subscription reliability.
#[cfg(feature = "unstable")]
pub reliability: z_reliability_t,
/// Dummy field to avoid having fieldless struct
#[cfg(not(feature = "unstable"))]
pub _0: u8,
}

/// Constructs the default value for `z_subscriber_options_t`.
Expand All @@ -97,6 +100,8 @@ pub extern "C" fn z_subscriber_options_default(this_: &mut MaybeUninit<z_subscri
this_.write(z_subscriber_options_t {
#[cfg(feature = "unstable")]
reliability: Reliability::DEFAULT.into(),
#[cfg(not(feature = "unstable"))]
_0: 0,
});
}

Expand Down
4 changes: 2 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ foreach(file ${files})
get_filename_component(target ${file} NAME_WE)

# Exclude SHM tests if SHM feature is disabled
if(NOT(ZENOHC_BUILD_WITH_SHARED_MEMORY))
if(${target} MATCHES "^.*_shm.*$")
if(NOT(ZENOHC_BUILD_WITH_SHARED_MEMORY AND (ZENOHC_BUILD_WITH_UNSTABLE_API)))
if(${target} MATCHES "^.*_shm.*$")
continue()
endif()
endif()
Expand Down
Loading