Commit ac0b0dcb authored by jan.koester's avatar jan.koester
Browse files

test

parent a3d69f7a
Loading
Loading
Loading
Loading
+35 −4
Changes for src/posix/udp.cpp: 35 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

#include <memory>
#include "socket.h"
#include "exception.h"
#include "error.h"
@@ -600,7 +601,37 @@ namespace {
// GRO-enabled socket. Safe to shrink only if GRO support were removed, or
// reworked to size per-socket instead of per-thread.
constexpr int kRecvMaxBatch = 64;
thread_local std::vector<uint8_t> g_recv_flat_buf(kRecvMaxBatch * 65535);
// Allocated WITHOUT initialisation: std::vector<uint8_t>(n) zero-filled all
// 4MB and so made every page resident -- on every thread that ever received
// (each ThreadPool worker: up to 2x cores, i.e. ~270MB RSS on a 32-core box
// for a single QUIC listener). A fresh malloc of this size is a new mmap; only
// the pages the kernel actually writes datagrams into become resident, and
// recvmmsg typically fills one slot per call.
constexpr size_t kRecvFlatBufSize = static_cast<size_t>(kRecvMaxBatch) * 65535;
thread_local std::unique_ptr<uint8_t[]> t_recv_flat_buf;

static uint8_t *recvFlatBuf() {
    if (!t_recv_flat_buf) t_recv_flat_buf.reset(new uint8_t[kRecvFlatBufSize]);
    return t_recv_flat_buf.get();
}

} // namespace (reopened below)

// Gives the pages of this thread's receive buffer back to the OS. Under load a
// thread's 4MB fills (multi-datagram recvmmsg calls, 64KB GRO slots) and stays
// resident forever otherwise; ThreadPool workers call this when they go idle.
// Must run on the owning thread: views returned by the last recvBatch*Views()
// call on this thread become invalid -- which their contract already says
// happens with the thread's next receive.
void releaseThreadScratch() {
    if (!t_recv_flat_buf) return;
    const uintptr_t page = static_cast<uintptr_t>(sysconf(_SC_PAGESIZE));
    uintptr_t begin = (reinterpret_cast<uintptr_t>(t_recv_flat_buf.get()) + page - 1) & ~(page - 1);
    uintptr_t end = (reinterpret_cast<uintptr_t>(t_recv_flat_buf.get()) + kRecvFlatBufSize) & ~(page - 1);
    if (end > begin) ::madvise(reinterpret_cast<void *>(begin), end - begin, MADV_DONTNEED);
}

namespace {
thread_local struct iovec g_recv_iovecs[kRecvMaxBatch];
thread_local struct mmsghdr g_recv_msgs[kRecvMaxBatch];
thread_local sockaddr_storage g_recv_peer_addrs[kRecvMaxBatch];
@@ -629,7 +660,7 @@ int recvBatchRaw(SOCKET sock, int batch, bool gro_enabled) {
    // family-correct length -- never a raw whole-struct read that could
    // observe stale padding from a previous call.
    for (int i = 0; i < batch; ++i) {
        g_recv_iovecs[i].iov_base = g_recv_flat_buf.data() + i * 65535;
        g_recv_iovecs[i].iov_base = recvFlatBuf() + i * 65535;
        g_recv_iovecs[i].iov_len  = 65535;
        g_recv_msgs[i].msg_hdr.msg_iov     = &g_recv_iovecs[i];
        g_recv_msgs[i].msg_hdr.msg_iovlen  = 1;
@@ -705,7 +736,7 @@ size_t udp::recvBatchAddr(std::vector<std::vector<uint8_t>>& out,
        size_t total_len = g_recv_msgs[i].msg_len;
        if (total_len == 0) continue;

        uint8_t* base = g_recv_flat_buf.data() + i * 65535;
        uint8_t* base = recvFlatBuf() + i * 65535;
        uint16_t gro_seg = recvBatchGroSegSize(i, _gro_enabled);

        if (gro_seg > 0 && total_len > gro_seg) {
@@ -791,7 +822,7 @@ size_t udp::recvBatchAddrViews(std::vector<DatagramView>& out,
        size_t total_len = g_recv_msgs[i].msg_len;
        if (total_len == 0) continue;

        uint8_t* base = g_recv_flat_buf.data() + i * 65535;
        uint8_t* base = recvFlatBuf() + i * 65535;
        uint16_t gro_seg = recvBatchGroSegSize(i, _gro_enabled);

        if (gro_seg > 0 && total_len > gro_seg) {