Skip to content

Improve shm api #1950

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

yellowhatter
Copy link
Contributor

@yellowhatter yellowhatter commented May 19, 2025

Series of SHM API improvements:

  • try_resize and try_relayout API to resize owned SHM buffer
  • improve owned and unowned shm buffer types isolation (now strictly follows str and String relation semantic)
  • introduce OwnedShmBuf trait to aggregate API related to owned shm buffer types
  • move some private impls from public API files
  • introduce alignment constants: ALIGN_1_BYTE .. ALIGN_8_BYTES to better express alignment in code
  • default Alignment is now ALIGN_1_BYTE (fixes the issue with user not able to allocate arbitrary-sized buffers on default-initialized POSIX SHM provider)
  • fix some tests
  • add tests for buffer resize

- improved relation between owned and borrowed shm buffers
- add alignment constants
- default alignment now is 1
- fix bug in shm and session tests
@yellowhatter yellowhatter self-assigned this May 19, 2025
@yellowhatter yellowhatter added enhancement Existing things could work better rust Pull requests that update rust code labels May 19, 2025
Copy link

codecov bot commented May 26, 2025

Codecov Report

Attention: Patch coverage is 55.55556% with 40 lines in your changes missing coverage. Please review.

Please upload report for BASE (main@e6579fe). Learn more about missing BASE report.

Files with missing lines Patch % Lines
commons/zenoh-shm/src/api/buffer/zshm.rs 36.36% 14 Missing ⚠️
commons/zenoh-shm/src/api/buffer/zshmmut.rs 23.52% 13 Missing ⚠️
commons/zenoh-shm/src/lib.rs 71.73% 13 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1950   +/-   ##
=======================================
  Coverage        ?   70.84%           
=======================================
  Files           ?      365           
  Lines           ?    61664           
  Branches        ?        0           
=======================================
  Hits            ?    43684           
  Misses          ?    17980           
  Partials        ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yellowhatter yellowhatter requested a review from YuanYuYuan May 26, 2025 17:00
@yellowhatter yellowhatter marked this pull request as ready for review May 26, 2025 17:00
@yellowhatter yellowhatter changed the title WIP: Improve shm api Improve shm api May 27, 2025
@@ -35,9 +38,19 @@ impl ShmBuf for ZShm {
}
}

impl OwnedShmBuf for ZShm {
fn try_resize(&mut self, new_size: NonZeroUsize) -> Option<()> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to document its safety.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

fn try_relayout(&mut self, new_layout: MemoryLayout) -> Result<(), BufferRelayoutError> {
unsafe { self.0.try_relayout(new_layout) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document the safety.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Self(data)
impl OwnedShmBuf for ZShmMut {
fn try_resize(&mut self, new_size: NonZeroUsize) -> Option<()> {
unsafe { self.0.try_resize(new_size) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document the safety.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

fn eq(&self, other: &zshmmut) -> bool {
self.0 == other.0 .0
fn try_relayout(&mut self, new_layout: MemoryLayout) -> Result<(), BufferRelayoutError> {
unsafe { self.0.try_relayout(new_layout) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document the safety.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -84,7 +84,7 @@ async fn open_session_multicast(endpoint01: &str, endpoint02: &str) -> (Session,
.endpoints
.set(vec![endpoint01.parse().unwrap()])
.unwrap();
config.scouting.multicast.set_enabled(Some(true)).unwrap();
config.scouting.multicast.set_enabled(Some(false)).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it relevant to this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is small test bugfix - too small to deserve separate PR

});
}

#[test]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion. We can use tokio::test macro as we do in other tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! Fixed

});
}

#[test]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion. We can use tokio::test macro as we do in other tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! Fixed

});
}

#[test]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion. We can use tokio::test macro as we do in other tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! Fixed

});
}

#[test]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion. We can use tokio::test macro as we do in other tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! Fixed


const TIMEOUT: Duration = Duration::from_secs(60);
const SLEEP: Duration = Duration::from_secs(1);

const MSG_COUNT: usize = 1_00;
const MSG_SIZE: [usize; 2] = [1_024, 100_000];

async fn open_session_unicast(endpoints: &[&str]) -> (Session, Session) {
async fn open_session_unicast<const NO_SHM_FOR_SECOND_PEER: bool>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity. Is there any reason to use a const generic rather than a plain bool argument?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, no reason, I just thought that this is more readable (nobody cares about endpoints list, but testing modes are more interesting)

@yellowhatter yellowhatter requested a review from YuanYuYuan June 10, 2025 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Existing things could work better rust Pull requests that update rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants