Loading src/event/kqueue.cpp +55 −20 Changes for src/event/kqueue.cpp: 55 added lines, 20 removed lines. Original line number Diff line number Diff line Loading @@ -134,6 +134,17 @@ namespace netplus { } } // Submit any pending changes AND wait for events in a // single kevent() call. This halves the number of // syscalls in the hot path compared to flush()+kevent(). int submit_and_wait(struct kevent* out, int maxevents, struct timespec* timeout) { struct kevent* ch = _count > 0 ? _changes : nullptr; int nch = _count; _count = 0; return kevent(_kqfd, ch, nch, out, maxevents, timeout); } private: int _kqfd; struct kevent _changes[MAX_CHANGES]; Loading Loading @@ -173,13 +184,17 @@ namespace netplus { if (needWrite) { // Data pending → arm WRITE only. // When WRITE fires, Step 4 sends the data. // Step 3 also checks hasBufferedData() so any // buffered TLS records are processed too. // Step 1 also does unconditional prime_recv so // any incoming data is processed in the same cycle. _batch->add_change(_fd, EVFILT_WRITE, EV_ADD | EV_ONESHOT); } else { // Idle → arm READ for next request / data. _batch->add_change(_fd, EVFILT_READ, EV_ADD | EV_ONESHOT); } // Flush immediately so other threads blocked in // kevent() see the re-armed fd right away — matches // epoll's immediate epoll_ctl() in EpollArmGuard. _batch->flush(); } catch (...) { // Connection was closed concurrently — skip re-arm } Loading Loading @@ -284,11 +299,12 @@ namespace netplus { return getEventFlags(pos); } int waitEventHandler(int timeout) override { // Flush any pending kqueue changes (new fds, re-arms) // before blocking — otherwise the kernel never sees them. _batch.flush(); // Flush any pending batch entries (re-arms, new fds) // so other threads sharing this kqueue fd can see them // immediately. Called after each event in the loop. void flushPendingChanges() { _batch.flush(); } int waitEventHandler(int timeout) override { struct timespec ts{}; struct timespec* pts = nullptr; Loading @@ -298,7 +314,10 @@ namespace netplus { pts = &ts; } int evn = kevent(_kqfd, nullptr, 0, _Events.get(), // Submit any pending changes AND wait for events in // one syscall (half the kernel transitions). int evn = _batch.submit_and_wait( _Events.get(), _ServerSocket->getMaxconnections(), pts); if (evn < 0) { Loading Loading @@ -371,6 +390,11 @@ namespace netplus { _batch.add_change(fd, EVFILT_WRITE, EV_ADD | EV_ONESHOT); } // Flush immediately so the new connection is active // in kqueue for all threads — matches epoll's // immediate epoll_ctl(EPOLL_CTL_ADD) behavior. _batch.flush(); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) return; Loading Loading @@ -408,11 +432,17 @@ namespace netplus { } if (!needClose) { // Step 1: EVFILT_READ - buffer transport data if (events & KQUEUE_EV_READ) { // Step 1: Buffer transport data (non-blocking). // Always attempt — even on WRITE events the peer // may have sent data while we were write-blocked. // Avoids an extra kevent round-trip for keep-alive // and TLS handshake phases. bool gotRawData = false; { buffer buf(BLOCKSIZE); try { c->csock->prime_recv(buf, 0); size_t n = c->csock->prime_recv(buf, 0); if (n > 0) gotRawData = true; } catch (NetException& e) { if (e.getErrorType() != NetException::Note) throw; } Loading Loading @@ -468,15 +498,15 @@ namespace netplus { // Drain all available TLS records to avoid stalling // when multiple records arrive in a single TCP segment. // Edge-triggered: drain completely before returning bool hasAppData = (events & KQUEUE_EV_READ) || bool hasAppData = gotRawData || c->csock->hasBufferedData(); while (hasAppData && c->csock->getHandshakeDone() && !needClose) { buffer buf(BLOCKSIZE); recvBuf.size = BLOCKSIZE; size_t rcv = 0; bool wouldBlock = false; try { rcv = c->csock->recvData(buf, 0); rcv = c->csock->recvData(recvBuf, 0); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { wouldBlock = true; Loading @@ -489,7 +519,7 @@ namespace netplus { // True EOF — peer closed the connection needClose = true; } else if (rcv > 0) { c->RecvData.append(buf.data.buf, rcv); c->RecvData.append(recvBuf.data.buf, rcv); evconnection->RequestEvent(*c, tid, args); } Loading @@ -500,8 +530,8 @@ namespace netplus { // without waiting for another kqueue event. bool gotMore = false; try { buffer prBuf(BLOCKSIZE); size_t pr = c->csock->prime_recv(prBuf, 0); recvBuf.size = BLOCKSIZE; size_t pr = c->csock->prime_recv(recvBuf, 0); if (pr > 0) gotMore = true; } catch (NetException&) { // EAGAIN / would-block — no more kernel data Loading Loading @@ -686,14 +716,19 @@ namespace netplus { if (flags & KQUEUE_EV_EOF) continue; pollptr.ConnectEventHandler(tid, args); continue; } } else { int fd = pollptr.getEventFd(i); int flags = pollptr.getEventFlags(i); pollptr.IoEventHandler(fd, flags, tid, args); } // Flush re-arms immediately after each // event so other threads (and this // thread's next kevent) see the filter // right away — matches epoll's per-event // epoll_ctl(MOD) latency. pollptr.flushPendingChanges(); } } catch (NetException& e) { std::cerr << "Poll Error: " << e.what() << std::endl; usleep(10000); // 10ms sleep on error to reduce CPU thrashing Loading Loading
src/event/kqueue.cpp +55 −20 Changes for src/event/kqueue.cpp: 55 added lines, 20 removed lines. Original line number Diff line number Diff line Loading @@ -134,6 +134,17 @@ namespace netplus { } } // Submit any pending changes AND wait for events in a // single kevent() call. This halves the number of // syscalls in the hot path compared to flush()+kevent(). int submit_and_wait(struct kevent* out, int maxevents, struct timespec* timeout) { struct kevent* ch = _count > 0 ? _changes : nullptr; int nch = _count; _count = 0; return kevent(_kqfd, ch, nch, out, maxevents, timeout); } private: int _kqfd; struct kevent _changes[MAX_CHANGES]; Loading Loading @@ -173,13 +184,17 @@ namespace netplus { if (needWrite) { // Data pending → arm WRITE only. // When WRITE fires, Step 4 sends the data. // Step 3 also checks hasBufferedData() so any // buffered TLS records are processed too. // Step 1 also does unconditional prime_recv so // any incoming data is processed in the same cycle. _batch->add_change(_fd, EVFILT_WRITE, EV_ADD | EV_ONESHOT); } else { // Idle → arm READ for next request / data. _batch->add_change(_fd, EVFILT_READ, EV_ADD | EV_ONESHOT); } // Flush immediately so other threads blocked in // kevent() see the re-armed fd right away — matches // epoll's immediate epoll_ctl() in EpollArmGuard. _batch->flush(); } catch (...) { // Connection was closed concurrently — skip re-arm } Loading Loading @@ -284,11 +299,12 @@ namespace netplus { return getEventFlags(pos); } int waitEventHandler(int timeout) override { // Flush any pending kqueue changes (new fds, re-arms) // before blocking — otherwise the kernel never sees them. _batch.flush(); // Flush any pending batch entries (re-arms, new fds) // so other threads sharing this kqueue fd can see them // immediately. Called after each event in the loop. void flushPendingChanges() { _batch.flush(); } int waitEventHandler(int timeout) override { struct timespec ts{}; struct timespec* pts = nullptr; Loading @@ -298,7 +314,10 @@ namespace netplus { pts = &ts; } int evn = kevent(_kqfd, nullptr, 0, _Events.get(), // Submit any pending changes AND wait for events in // one syscall (half the kernel transitions). int evn = _batch.submit_and_wait( _Events.get(), _ServerSocket->getMaxconnections(), pts); if (evn < 0) { Loading Loading @@ -371,6 +390,11 @@ namespace netplus { _batch.add_change(fd, EVFILT_WRITE, EV_ADD | EV_ONESHOT); } // Flush immediately so the new connection is active // in kqueue for all threads — matches epoll's // immediate epoll_ctl(EPOLL_CTL_ADD) behavior. _batch.flush(); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) return; Loading Loading @@ -408,11 +432,17 @@ namespace netplus { } if (!needClose) { // Step 1: EVFILT_READ - buffer transport data if (events & KQUEUE_EV_READ) { // Step 1: Buffer transport data (non-blocking). // Always attempt — even on WRITE events the peer // may have sent data while we were write-blocked. // Avoids an extra kevent round-trip for keep-alive // and TLS handshake phases. bool gotRawData = false; { buffer buf(BLOCKSIZE); try { c->csock->prime_recv(buf, 0); size_t n = c->csock->prime_recv(buf, 0); if (n > 0) gotRawData = true; } catch (NetException& e) { if (e.getErrorType() != NetException::Note) throw; } Loading Loading @@ -468,15 +498,15 @@ namespace netplus { // Drain all available TLS records to avoid stalling // when multiple records arrive in a single TCP segment. // Edge-triggered: drain completely before returning bool hasAppData = (events & KQUEUE_EV_READ) || bool hasAppData = gotRawData || c->csock->hasBufferedData(); while (hasAppData && c->csock->getHandshakeDone() && !needClose) { buffer buf(BLOCKSIZE); recvBuf.size = BLOCKSIZE; size_t rcv = 0; bool wouldBlock = false; try { rcv = c->csock->recvData(buf, 0); rcv = c->csock->recvData(recvBuf, 0); } catch (NetException& e) { if (e.getErrorType() == NetException::Note) { wouldBlock = true; Loading @@ -489,7 +519,7 @@ namespace netplus { // True EOF — peer closed the connection needClose = true; } else if (rcv > 0) { c->RecvData.append(buf.data.buf, rcv); c->RecvData.append(recvBuf.data.buf, rcv); evconnection->RequestEvent(*c, tid, args); } Loading @@ -500,8 +530,8 @@ namespace netplus { // without waiting for another kqueue event. bool gotMore = false; try { buffer prBuf(BLOCKSIZE); size_t pr = c->csock->prime_recv(prBuf, 0); recvBuf.size = BLOCKSIZE; size_t pr = c->csock->prime_recv(recvBuf, 0); if (pr > 0) gotMore = true; } catch (NetException&) { // EAGAIN / would-block — no more kernel data Loading Loading @@ -686,14 +716,19 @@ namespace netplus { if (flags & KQUEUE_EV_EOF) continue; pollptr.ConnectEventHandler(tid, args); continue; } } else { int fd = pollptr.getEventFd(i); int flags = pollptr.getEventFlags(i); pollptr.IoEventHandler(fd, flags, tid, args); } // Flush re-arms immediately after each // event so other threads (and this // thread's next kevent) see the filter // right away — matches epoll's per-event // epoll_ctl(MOD) latency. pollptr.flushPendingChanges(); } } catch (NetException& e) { std::cerr << "Poll Error: " << e.what() << std::endl; usleep(10000); // 10ms sleep on error to reduce CPU thrashing Loading