shuveb / io_uring-by-example

A companion repository for the io_uring by Example article series
MIT License
372 stars 56 forks source link

copy_file_contents() #13

Open fullnitrous opened 3 years ago

fullnitrous commented 3 years ago

Reading the comment above the function copy_file_contents() it says the following.

/*
 * Once a static file is identified to be served, this function is used to read the file
 * and write it over the client socket using Linux's sendfile() system call. This saves us
 * the hassle of transferring file buffers from kernel to user space and back.
 * */

But the function itself loads the entire file at once into userspace memory. The function does not use sendfile().

Lennie commented 2 years ago

Also the best way is probably to use io_uring for that too, from liburing:

This splice operation can be used to implement sendfile by splicing to an intermediate pipe first, then splice to the final destination. In fact, the implementation of sendfile in kernel uses splice internally.

And in case it wasn't clear, a comment explains it:

in the io_uring case, you should be able to build a splice-based sendfile yourself using a pipe you create via pipe(2) to act as the buffer. Or do similar via fixed buffers in the ring instead of splice.

https://lwn.net/Articles/868193/