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

refactor: use mmap to replace socket for ipc #88

Closed
wants to merge 1 commit into from

Conversation

silver-ymz
Copy link
Member

WIP
Close #70

Current problem:
mmap-sync doesn't provide a way to check if the writer has completed a write operation. Performing a read operation before starting a write can even cause a panic. Maybe we should try using self-managed mmap and condvar.

Signed-off-by: silver-ymz <yinmingzhuo@gmail.com>
reader
.write(&[0u8; 1], Duration::from_secs(1))
.expect("Failed to write.");
unsafe { reader.read::<[u8; 1]>(false) }.expect("Failed to read.");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the writing is done if no exception raised? Because the mmap-sync is sync

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the writing is done if no exception raised?

Yes. But the other side don't know when the writing is done.

Copy link
Member

Choose a reason for hiding this comment

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

Is this the common pattern for stream based interface? For socket interface, receiver also don't know when the write operation is done by the writer?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the reader need to spawn a spin-loop thread to actively read from mmap to a buffer, and the ChannelTrait read is designed to read from the buffer when needed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Is this the common pattern for stream based interface? For socket interface, receiver also don't know when the write operation is done by the writer?

For socket interface, the reader will be blocked until the write operation is done by the writer. But mmap-sync won't block anything. For read operations other than the first read, we can use spin-loop to block it mannually. But for the first time, it will directly panic.

I think the reader need to spawn a spin-loop thread to actively read from mmap to a buffer

Using the way, we will give up the advantage of zero-copy with mmap.

Copy link
Contributor

Choose a reason for hiding this comment

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

For read operations other than the first read, we can use spin-loop to block it mannually.

Since the mmap is the only IPC data link, I dont think there are any method for writer to "notify" the receiver that the writting is done.

Using the way, we will give up the advantage of zero-copy with mmap.

The writer doesn't check if the sent data is consumed, so the next data will overwrite the previous one and causes "lossing packet"

Copy link
Member Author

Choose a reason for hiding this comment

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

Since the mmap is the only IPC data link, I dont think there are any method for writer to "notify" the receiver that the writting is done.

We can store extra state data in an additional mmap file. Because the memory is shared between 2 processes, all sync primitives can be used, like locks, etc.

The writer doesn't check if the sent data is consumed, so the next data will overwrite the previous one and causes "lossing packet".

We can use write data in different place, also it's possible to check if the sent data is consumed. mmap-sync use RCU to implement similar function.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for your explanation. @usamoi will implement this feature, and we're single producer, single consumer so it can be much easier.
Spin-lock is not a good solution since it consumes lots of cpu resources.

Copy link
Member

Choose a reason for hiding this comment

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

@silver-ymz silver-ymz closed this Nov 13, 2023
@silver-ymz silver-ymz deleted the mmap branch November 19, 2023 01:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

optimization: Use mmap instead of socket for intra process communication
3 participants