poco icon indicating copy to clipboard operation
poco copied to clipboard

Use sendfile system call on Linux in HTTPServerResponseImpl::sendFile

Open carun opened this issue 4 years ago • 3 comments

At the moment, HTTPServerResponseImpl::sendFile uses StreamCopied::copyStream which in turn uses intermediate buffer copies. This is inefficient on Linux as the buffers are copied from user space to user space and again from user space to kernel space.

https://github.com/pocoproject/poco/blob/3fc3e5f5b8462f7666952b43381383a79b8b5d92/Net/src/HTTPServerResponseImpl.cpp#L131

https://github.com/pocoproject/poco/blob/3fc3e5f5b8462f7666952b43381383a79b8b5d92/Foundation/src/StreamCopier.cpp#L36

As the size of the file increases this will become evident as the kernel buffer cache will get filled up pretty quickly and we have to resort to running

$ free -m
              total        used        free      shared  buff/cache   available
Mem:          32115        1859       12880           1       17374       29812
Swap:          3935         167        3768

$ echo 3 | sudo tee /proc/sys/vm/drop_caches
3

$ free -m
              total        used        free      shared  buff/cache   available
Mem:          32115        1866       29841           1         407       29816
Swap:          3935         167        3768

Linux has sendfile system call. This can be used to speed up the sends and also minimize the memory usage of the process and in turn reduce the kernel buffer cache.

carun avatar May 21 '21 02:05 carun

It would require exposing the file descriptors, either at the HTTPResponse (from HTTPSession) level, or at the streams layer. Plus reliably detecting if sendfile() is available (since 2.2, so I think practically can be relied on being there for linux).

aleks-f avatar May 21 '21 08:05 aleks-f

This issue is stale because it has been open for 365 days with no activity.

github-actions[bot] avatar May 24 '22 03:05 github-actions[bot]

This issue was closed because it has been inactive for 60 days since being marked as stale.

github-actions[bot] avatar Jul 30 '22 03:07 github-actions[bot]

This issue was closed because it has been inactive for 60 days since being marked as stale.

github-actions[bot] avatar Sep 29 '22 03:09 github-actions[bot]