erpc-io / eRPC

Efficient RPCs for datacenter networks
https://erpc.io/
Other
835 stars 137 forks source link

Any idea how to receive a message more efficient in eRPC, using epoll? #88

Closed shenweihai1 closed 1 year ago

shenweihai1 commented 1 year ago

In eRPC, from the server perspective, we use run_event_loop_once to check if there is a response received from another endpoint with busy loop. Here is an example of how to use eRPC to wait for a request from another eRPC endpoint.

void callback(req) { 
  // deal with req
  // ...

  // construct a response back to the client side
  resp=[value]
  rpc.enqueue(resp);
}

// this is a daemon thread
void server_background() {
  while(true) {
    // if there is a request received, the function callback will be invoked
    rpc.run_event_loop_once();  
  }
}

In this example, there is a busy loop in it which wastes a lot of cpu time, what can i do to avoid busy loop? All high-performance rpc frameworks use the event_driven way to solve this issue, I think the only way to solve this issue could also be using the event-driven method. To support event-driven, we should use epoll_wait and epoll_ctl system calls in the kernel via a file descriptor, but it is slow compared to the kernel-bypass way, why don't I use TCP/IP directly instead of eRPC, right? So does anyone have any idea how to implement it in an event-driven way or any example?

Or any idea can help

ankalia commented 1 year ago

Hi Weihai. Currently there isn't a way in eRPC to use event-driven I/O instead of polling-based I/O.

One possibility is that if you're running an RPC server with many server-side threads, you can use just one "foreground" polling thread for the packet transport. The other application handling threads can be "background" threads (e.g., see the example below). The background threads can be put sleep when there is no work available from the "foreground" thread.

https://github.com/erpc-io/eRPC/blob/5c2343b4968a2f740b730fd29100f3acf9c1ff69/apps/masstree_analytics/masstree_analytics.cc#L410

https://github.com/erpc-io/eRPC/blob/5c2343b4968a2f740b730fd29100f3acf9c1ff69/src/nexus_impl/nexus_bg_thread.cc#L21

shenweihai1 commented 1 year ago

Thank you so much for your advice @ankalia