Search code examples
linuxperformancesocketsnas

Linux: what is the most efficient way of reading a file from a NAS and sending over a socket


I would like to write a server that reads in a file from a NAS and sends it out over a socket. What is the fastest way of doing this?

Thanks!


Solution

  • I think standard CIFS mounts support mmap(2) on the files (if I read correctly, direct mode must be off).

    If so, your fastest option is probably to open(2) files as normal, and use sendfile(2) to send the file data over your UDP sockets. (sendfile(2) requires the file to mappable, which isn't always guaranteed, but the CIFS client code in the kernel (fs/cifs/file.c:cifs_file_strict_mmap()) appears to support mmap(2).)

    Pat Patterson reports an 8% speedup with sendfile(2) vs write(2). But if it works, it'd save you the hassle of handling AIO operations yourself -- the kernel would be in charge of requesting memory pages from the file, sending them over the socket when the socket buffers allow, and hopefully allow your application code to be short and sweet.