From b7c63ab83ae2b991ff8350bc0aa6427588629287 Mon Sep 17 00:00:00 2001 From: Markus Handell Date: Tue, 26 May 2020 18:09:55 +0200 Subject: [PATCH] PhysicalSocketServer: remove heap-based epoll_event handling. This change deletes heap-based handling of dynamic number of epoll events handled per call to epoll, with the assumption that PSS load is likely not dominated by the epoll syscalls. This simplifies the logic in the code and removes a heap allocation. Bug: webrtc:11567 Change-Id: I34fbe1fa8bf0a037bf849a4adac1a0a333c9dd86 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/175563 Commit-Queue: Markus Handell Reviewed-by: Karl Wiberg Cr-Commit-Position: refs/heads/master@{#31358} --- rtc_base/physical_socket_server.cc | 22 +--------------------- rtc_base/physical_socket_server.h | 10 +++++++++- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/rtc_base/physical_socket_server.cc b/rtc_base/physical_socket_server.cc index cf65300b4a..3a95331669 100644 --- a/rtc_base/physical_socket_server.cc +++ b/rtc_base/physical_socket_server.cc @@ -1365,12 +1365,6 @@ bool PhysicalSocketServer::WaitSelect(int cmsWait, bool process_io) { #if defined(WEBRTC_USE_EPOLL) -// Initial number of events to process with one call to "epoll_wait". -static const size_t kInitialEpollEvents = 128; - -// Maximum number of events to process with one call to "epoll_wait". -static const size_t kMaxEpollEvents = 8192; - void PhysicalSocketServer::AddEpoll(Dispatcher* pdispatcher) { RTC_DCHECK(epoll_fd_ != INVALID_SOCKET); int fd = pdispatcher->GetDescriptor(); @@ -1437,20 +1431,13 @@ bool PhysicalSocketServer::WaitEpoll(int cmsWait) { tvStop = TimeAfter(cmsWait); } - if (epoll_events_.empty()) { - // The initial space to receive events is created only if epoll is used. - epoll_events_.resize(kInitialEpollEvents); - } - fWait_ = true; - while (fWait_) { // Wait then call handlers as appropriate // < 0 means error // 0 means timeout // > 0 means count of descriptors ready - int n = epoll_wait(epoll_fd_, &epoll_events_[0], - static_cast(epoll_events_.size()), + int n = epoll_wait(epoll_fd_, epoll_events_.data(), epoll_events_.size(), static_cast(tvWait)); if (n < 0) { if (errno != EINTR) { @@ -1483,13 +1470,6 @@ bool PhysicalSocketServer::WaitEpoll(int cmsWait) { } } - if (static_cast(n) == epoll_events_.size() && - epoll_events_.size() < kMaxEpollEvents) { - // We used the complete space to receive events, increase size for future - // iterations. - epoll_events_.resize(std::max(epoll_events_.size() * 2, kMaxEpollEvents)); - } - if (cmsWait != kForever) { tvWait = TimeDiff(tvStop, TimeMillis()); if (tvWait < 0) { diff --git a/rtc_base/physical_socket_server.h b/rtc_base/physical_socket_server.h index 2f53d4d4fe..e21e53b8ec 100644 --- a/rtc_base/physical_socket_server.h +++ b/rtc_base/physical_socket_server.h @@ -16,6 +16,7 @@ #define WEBRTC_USE_EPOLL 1 #endif +#include #include #include #include @@ -81,6 +82,9 @@ class RTC_EXPORT PhysicalSocketServer : public SocketServer { void Update(Dispatcher* dispatcher); private: + // The number of events to process with one call to "epoll_wait". + static constexpr size_t kNumEpollEvents = 128; + typedef std::set DispatcherSet; void AddRemovePendingDispatchers() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); @@ -95,8 +99,12 @@ class RTC_EXPORT PhysicalSocketServer : public SocketServer { bool WaitEpoll(int cms); bool WaitPoll(int cms, Dispatcher* dispatcher); + // This array is accessed in isolation by a thread calling into Wait(). + // It's useless to use a SequenceChecker to guard it because a socket + // server can outlive the thread it's bound to, forcing the Wait call + // to have to reset the sequence checker on Wait calls. + std::array epoll_events_; const int epoll_fd_ = INVALID_SOCKET; - std::vector epoll_events_; #endif // WEBRTC_USE_EPOLL DispatcherSet dispatchers_ RTC_GUARDED_BY(crit_); DispatcherSet pending_add_dispatchers_ RTC_GUARDED_BY(crit_);