Git Product home page Git Product logo

io-uring-ipc's Introduction

io_uring IPC

Using IORING_OP_NOP it's possible to send arbitrary data (up to 64 bits) to another process. We can use this to share memory-pool entries, meaning, we can send data to another process. All the synchronization machinery is already provided -- for free -- by io_uring. You only need a shared memory allocator.

Using:

One of the limitations of this approach is that the yama security model prevents us from obtaining the file descriptor of the uring, you need either root privileges or PTRACE_MODE_ATTACH_REALCREDS, see pidfd_getfd(2).

Another option is to disable it completely.

echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

# or

sudo make disable-ptrace-scope

To send a message:

#include "hring.h"

char const* const addr = "hring_shm"; // will be used to search /dev/shm (must be unique)

struct hring h = { 0 };

hring_init(&h, addr, 4096, 32, 32 << 8); // creates an entry in /dev/shm with the following format:
//             ^           ^   ^
//             |           |   |
//             |           |   +---------------------+
//             |           |                         |
//             |           +---------------+         |
//             |                           |         |
//             |                           |         |
//             |                           |         |
//             v                           v         v
//             hring_shm:<pid>:<uring_fd>:<sq_size>:<cq_size>
//                        ^     ^
//                        |     |
//                        |     +----> uring file descriptor
//                        |
//                        +----------> process pid
hring_addr_t addr = hring_alloc(&h, sizeof(int));

size_t* msg = hring_deref(&h, addr);

*msg = 123;

hring_try_que(&h, addr);

hring_submit(&h, true);

To receive a message:

#include "hring.h"

char const* const addr = "hring_shm";

struct hring h;
hring_attach(&h, addr);

hring_deque_with_callback(&h, callback);
//                            ^
//                            |
//   +------------------------+   Try to dequeue all entries, calling callback for each.
//   |
//   v
void callback(struct hring* h, struct io_uring_cqe const* const cqe) {
   hring_addr_t addr = cqe->user_data;

   int* val = hring_deref(h, addr);

   // do something with val;

   hring_free(h, addr);
}

Building:

Just include hring.h.

make test

# or

make perf

Results

With a payload size of 8 bytes, sending 102400000 messages.

left = 94497280, deque   7902720(7.72%) messages, 0.06 GiB/s
left = 68429792, deque 26067488(25.46%) messages, 0.19 GiB/s
left = 42355424, deque 26074368(25.46%) messages, 0.19 GiB/s
left = 16253600, deque 26101824(25.49%) messages, 0.19 GiB/s
wait for child, sent an average of 25.15 msgs/usec, average latency of 39.76 ns
child exited with status = 0

io-uring-ipc's People

Contributors

mulling avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.