Commit 8e0dc298 authored by jan.koester's avatar jan.koester
Browse files

speed

parent d3b00904
Loading
Loading
Loading
Loading
+21 −0
Changes for src/event/epoll.cpp: 21 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -42,6 +42,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <fcntl.h>
#include <errno.h>
#include <cstring>
#include <pthread.h>
#include <sched.h>

#include "config.h"
#include "socket.h"
@@ -890,6 +892,25 @@ namespace netplus {
                e[NetException::Critical] << "runEventloop: pthread_create failed";
                throw e;
            }

            // Best-effort CPU pinning: `threads` defaults to the core count
            // (see the constructor above), so worker i -> CPU i is a natural
            // 1:1 mapping that keeps a worker's L2/L3 working set (connection
            // state it touches repeatedly across epoll_wait cycles) from
            // migrating between cores. Deliberately non-fatal: under a
            // cgroup/cpuset (containers, cloud vCPUs) or a machine with fewer
            // online CPUs than `threads`, the target CPU may not be in this
            // thread's allowed set — pthread_setaffinity_np then fails with
            // EINVAL/ESRCH, which is fine, the thread simply stays
            // unpinned/scheduler-migratable exactly as it was before this
            // change.
            {
                cpu_set_t cpuset;
                CPU_ZERO(&cpuset);
                CPU_SET(i % std::max(1, static_cast<int>(std::thread::hardware_concurrency())), &cpuset);
                pthread_setaffinity_np(tid, sizeof(cpuset), &cpuset);
            }

            threadpool.push_back(tid);
        }

+25 −1
Changes for src/event/kqueue.cpp: 25 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -378,6 +378,14 @@ namespace netplus {
                        if (select(_ServerSocket->fd() + 1, &readfds, nullptr, nullptr, &tv) <= 0)
                            break;
                    } while (true);

                    // Re-arm the EV_ONESHOT listener registration (see the
                    // ADD in runEventloop) now that this drain round is
                    // done, so the next datagram wakes a thread again —
                    // mirrors epoll.cpp's EpollArmGuard rearm for the same
                    // listener fd.
                    _batch.add_change(_ServerSocket->fd(), EVFILT_READ, EV_ADD | EV_ONESHOT);
                    _batch.flush();
                    return;
                }

@@ -903,7 +911,23 @@ MAINWORKERLOOP:
        }

        struct kevent setevent;
        EV_SET(&setevent, ss->fd(), EVFILT_READ, EV_ADD, 0, 0, nullptr);
        // QUIC multiplexes every client over this one UDP socket for the
        // connection's whole lifetime — unlike TCP/SSL, where only accept()
        // shares the listener and established connections get their own
        // EV_ONESHOT'd fd. All `threads` worker threads share this single
        // kqueue fd (`_pollFD`, registered once here), so without EV_ONESHOT
        // every incoming datagram would wake all of them (level-triggered,
        // shared kqueue). EV_ONESHOT + explicit re-arm after each drain round
        // (see ConnectEventHandler's QUIC branch) limits servicing to one
        // thread at a time — mirrors epoll.cpp's EPOLLONESHOT handling for
        // the same reason (see that file's comment for the measured 10x
        // regression when this was briefly relaxed to allow concurrent
        // servicing).
        int listen_flags = EV_ADD;
        if (ss->_Type == sockettype::QUIC) {
            listen_flags |= EV_ONESHOT;
        }
        EV_SET(&setevent, ss->fd(), EVFILT_READ, listen_flags, 0, 0, nullptr);

        if (kevent(_pollFD, &setevent, 1, nullptr, 0, nullptr) < 0) {
            NetException e;