| Index: net/tools/epoll_server/epoll_server.cc
|
| diff --git a/net/tools/epoll_server/epoll_server.cc b/net/tools/epoll_server/epoll_server.cc
|
| index a382dbae9943cac86d7864e62969359871c872f0..87d8c63c30e277036929e8f8ac054ab28522e438 100644
|
| --- a/net/tools/epoll_server/epoll_server.cc
|
| +++ b/net/tools/epoll_server/epoll_server.cc
|
| @@ -6,7 +6,7 @@
|
|
|
| #include <unistd.h> // For read, pipe, close and write.
|
| #include <stdlib.h> // for abort
|
| -#include <errno.h> // for errno and strerror_r
|
| +#include <errno.h> // for errno and strerror_r
|
| #include <algorithm>
|
| #include <utility>
|
| #include <vector>
|
| @@ -69,25 +69,25 @@ class ReadPipeCallback : public EpollCallbackInterface {
|
| data_read = read(fd, &data, sizeof(data));
|
| }
|
| }
|
| - virtual void OnShutdown(EpollServer *eps, int fd) OVERRIDE {}
|
| + virtual void OnShutdown(EpollServer* eps, int fd) OVERRIDE {}
|
| virtual void OnRegistration(EpollServer*, int, int) OVERRIDE {}
|
| - virtual void OnModification(int, int) OVERRIDE {} // COV_NF_LINE
|
| - virtual void OnUnregistration(int, bool) OVERRIDE {} // COV_NF_LINE
|
| + virtual void OnModification(int, int) OVERRIDE {} // COV_NF_LINE
|
| + virtual void OnUnregistration(int, bool) OVERRIDE {} // COV_NF_LINE
|
| };
|
|
|
| ////////////////////////////////////////////////////////////////////////////////
|
| ////////////////////////////////////////////////////////////////////////////////
|
|
|
| EpollServer::EpollServer()
|
| - : epoll_fd_(epoll_create(1024)),
|
| - timeout_in_us_(0),
|
| - recorded_now_in_us_(0),
|
| - ready_list_size_(0),
|
| - wake_cb_(new ReadPipeCallback),
|
| - read_fd_(-1),
|
| - write_fd_(-1),
|
| - in_wait_for_events_and_execute_callbacks_(false),
|
| - in_shutdown_(false) {
|
| + : epoll_fd_(epoll_create(1024)),
|
| + timeout_in_us_(0),
|
| + recorded_now_in_us_(0),
|
| + ready_list_size_(0),
|
| + wake_cb_(new ReadPipeCallback),
|
| + read_fd_(-1),
|
| + write_fd_(-1),
|
| + in_wait_for_events_and_execute_callbacks_(false),
|
| + in_shutdown_(false) {
|
| // ensure that the epoll_fd_ is valid.
|
| CHECK_NE(epoll_fd_, -1);
|
| LIST_INIT(&ready_list_);
|
| @@ -131,8 +131,7 @@ void EpollServer::CleanupTimeToAlarmCBMap() {
|
| // Call OnShutdown() on alarms. Note that the structure of the loop
|
| // is similar to the structure of loop in the function HandleAlarms()
|
| for (TimeToAlarmCBMap::iterator i = alarm_map_.begin();
|
| - i != alarm_map_.end();
|
| - ) {
|
| + i != alarm_map_.end();) {
|
| // Note that OnShutdown() can call UnregisterAlarm() on
|
| // other iterators. OnShutdown() should not call UnregisterAlarm()
|
| // on self because by definition the iterator is not valid any more.
|
| @@ -214,7 +213,6 @@ void EpollServer::RegisterFD(int fd, CB* cb, int event_mask) {
|
| cb_map_.insert(CBAndEventMask(cb, event_mask, fd));
|
| }
|
|
|
| -
|
| // set the FD to be non-blocking.
|
| SetNonblocking(fd);
|
|
|
| @@ -230,8 +228,8 @@ void EpollServer::SetNonblocking(int fd) {
|
| if (flags == -1) {
|
| int saved_errno = errno;
|
| char buf[kErrorBufferSize];
|
| - LOG(FATAL) << "Error " << saved_errno
|
| - << " doing fcntl(" << fd << ", F_GETFL, 0): "
|
| + LOG(FATAL) << "Error " << saved_errno << " doing fcntl(" << fd
|
| + << ", F_GETFL, 0): "
|
| << strerror_r(saved_errno, buf, sizeof(buf));
|
| }
|
| if (!(flags & O_NONBLOCK)) {
|
| @@ -241,9 +239,9 @@ void EpollServer::SetNonblocking(int fd) {
|
| // bad.
|
| int saved_errno = errno;
|
| char buf[kErrorBufferSize];
|
| - LOG(FATAL) << "Error " << saved_errno
|
| - << " doing fcntl(" << fd << ", F_SETFL, " << saved_flags << "): "
|
| - << strerror_r(saved_errno, buf, sizeof(buf));
|
| + LOG(FATAL) << "Error " << saved_errno << " doing fcntl(" << fd
|
| + << ", F_SETFL, " << saved_flags
|
| + << "): " << strerror_r(saved_errno, buf, sizeof(buf));
|
| }
|
| }
|
| }
|
| @@ -345,19 +343,17 @@ class TrueFalseGuard {
|
| DCHECK(*guarded_bool_ == false);
|
| *guarded_bool_ = true;
|
| }
|
| - ~TrueFalseGuard() {
|
| - *guarded_bool_ = false;
|
| - }
|
| + ~TrueFalseGuard() { *guarded_bool_ = false; }
|
| +
|
| private:
|
| bool* guarded_bool_;
|
| };
|
|
|
| void EpollServer::WaitForEventsAndExecuteCallbacks() {
|
| if (in_wait_for_events_and_execute_callbacks_) {
|
| - LOG(DFATAL) <<
|
| - "Attempting to call WaitForEventsAndExecuteCallbacks"
|
| - " when an ancestor to the current function is already"
|
| - " WaitForEventsAndExecuteCallbacks!";
|
| + LOG(DFATAL) << "Attempting to call WaitForEventsAndExecuteCallbacks"
|
| + " when an ancestor to the current function is already"
|
| + " WaitForEventsAndExecuteCallbacks!";
|
| // The line below is actually tested, but in coverage mode,
|
| // we never see it.
|
| return; // COV_NF_LINE
|
| @@ -365,9 +361,7 @@ void EpollServer::WaitForEventsAndExecuteCallbacks() {
|
| TrueFalseGuard recursion_guard(&in_wait_for_events_and_execute_callbacks_);
|
| if (alarm_map_.empty()) {
|
| // no alarms, this is business as usual.
|
| - WaitForEventsAndCallHandleEvents(timeout_in_us_,
|
| - events_,
|
| - events_size_);
|
| + WaitForEventsAndCallHandleEvents(timeout_in_us_, events_, events_size_);
|
| recorded_now_in_us_ = 0;
|
| return;
|
| }
|
| @@ -377,11 +371,11 @@ void EpollServer::WaitForEventsAndExecuteCallbacks() {
|
| // long-running alarms might install other long-running
|
| // alarms, etc. By storing it here now, we ensure that
|
| // a more reasonable amount of work is done here.
|
| - int64 now_in_us = NowInUsec();
|
| + int64 now_in_us = NowInUsec();
|
|
|
| // Get the first timeout from the alarm_map where it is
|
| // stored in absolute time.
|
| - int64 next_alarm_time_in_us = alarm_map_.begin()->first;
|
| + int64 next_alarm_time_in_us = alarm_map_.begin()->first;
|
| VLOG(4) << "next_alarm_time = " << next_alarm_time_in_us
|
| << " now = " << now_in_us
|
| << " timeout_in_us = " << timeout_in_us_;
|
| @@ -402,9 +396,7 @@ void EpollServer::WaitForEventsAndExecuteCallbacks() {
|
|
|
| // wait for events.
|
|
|
| - WaitForEventsAndCallHandleEvents(wait_time_in_us,
|
| - events_,
|
| - events_size_);
|
| + WaitForEventsAndCallHandleEvents(wait_time_in_us, events_, events_size_);
|
| CallAndReregisterAlarmEvents();
|
| recorded_now_in_us_ = 0;
|
| }
|
| @@ -439,8 +431,7 @@ void EpollServer::SetFDNotReady(int fd) {
|
|
|
| bool EpollServer::IsFDReady(int fd) const {
|
| FDToCBMap::const_iterator fd_i = cb_map_.find(CBAndEventMask(NULL, 0, fd));
|
| - return (cb_map_.end() != fd_i &&
|
| - fd_i->cb != NULL &&
|
| + return (cb_map_.end() != fd_i && fd_i->cb != NULL &&
|
| fd_i->entry.le_prev != NULL);
|
| }
|
|
|
| @@ -505,18 +496,30 @@ int64 EpollServer::ApproximateNowInUsec() const {
|
|
|
| std::string EpollServer::EventMaskToString(int event_mask) {
|
| std::string s;
|
| - if (event_mask & EPOLLIN) s += "EPOLLIN ";
|
| - if (event_mask & EPOLLPRI) s += "EPOLLPRI ";
|
| - if (event_mask & EPOLLOUT) s += "EPOLLOUT ";
|
| - if (event_mask & EPOLLRDNORM) s += "EPOLLRDNORM ";
|
| - if (event_mask & EPOLLRDBAND) s += "EPOLLRDBAND ";
|
| - if (event_mask & EPOLLWRNORM) s += "EPOLLWRNORM ";
|
| - if (event_mask & EPOLLWRBAND) s += "EPOLLWRBAND ";
|
| - if (event_mask & EPOLLMSG) s += "EPOLLMSG ";
|
| - if (event_mask & EPOLLERR) s += "EPOLLERR ";
|
| - if (event_mask & EPOLLHUP) s += "EPOLLHUP ";
|
| - if (event_mask & EPOLLONESHOT) s += "EPOLLONESHOT ";
|
| - if (event_mask & EPOLLET) s += "EPOLLET ";
|
| + if (event_mask & EPOLLIN)
|
| + s += "EPOLLIN ";
|
| + if (event_mask & EPOLLPRI)
|
| + s += "EPOLLPRI ";
|
| + if (event_mask & EPOLLOUT)
|
| + s += "EPOLLOUT ";
|
| + if (event_mask & EPOLLRDNORM)
|
| + s += "EPOLLRDNORM ";
|
| + if (event_mask & EPOLLRDBAND)
|
| + s += "EPOLLRDBAND ";
|
| + if (event_mask & EPOLLWRNORM)
|
| + s += "EPOLLWRNORM ";
|
| + if (event_mask & EPOLLWRBAND)
|
| + s += "EPOLLWRBAND ";
|
| + if (event_mask & EPOLLMSG)
|
| + s += "EPOLLMSG ";
|
| + if (event_mask & EPOLLERR)
|
| + s += "EPOLLERR ";
|
| + if (event_mask & EPOLLHUP)
|
| + s += "EPOLLHUP ";
|
| + if (event_mask & EPOLLONESHOT)
|
| + s += "EPOLLONESHOT ";
|
| + if (event_mask & EPOLLET)
|
| + s += "EPOLLET ";
|
| return s;
|
| }
|
|
|
| @@ -531,24 +534,20 @@ void EpollServer::LogStateOnCrash() {
|
| it != alarm_map_.end();
|
| ++it) {
|
| const bool skipped =
|
| - alarms_reregistered_and_should_be_skipped_.find(it->second)
|
| - != alarms_reregistered_and_should_be_skipped_.end();
|
| + alarms_reregistered_and_should_be_skipped_.find(it->second) !=
|
| + alarms_reregistered_and_should_be_skipped_.end();
|
| LOG(ERROR) << "Alarm " << it->second << " registered at time " << it->first
|
| << " and should be skipped = " << skipped;
|
| }
|
|
|
| LOG(ERROR) << cb_map_.size() << " fd callbacks registered.";
|
| - for (FDToCBMap::iterator it = cb_map_.begin();
|
| - it != cb_map_.end();
|
| - ++it) {
|
| + for (FDToCBMap::iterator it = cb_map_.begin(); it != cb_map_.end(); ++it) {
|
| LOG(ERROR) << "fd: " << it->fd << " with mask " << it->event_mask
|
| << " registered with cb: " << it->cb;
|
| }
|
| LOG(ERROR) << "----------------------/Epoll Server--------------------------";
|
| }
|
|
|
| -
|
| -
|
| ////////////////////////////////////////////////////////////////////////////////
|
| ////////////////////////////////////////////////////////////////////////////////
|
|
|
| @@ -594,8 +593,7 @@ void EpollServer::ModFD(int fd, int event_mask) const {
|
| #ifdef EPOLL_SERVER_EVENT_TRACING
|
| event_recorder_.RecordFDMaskEvent(fd, ee.events, "ModFD");
|
| #endif
|
| - VLOG(3) << "modifying fd= " << fd << " "
|
| - << EventMaskToString(ee.events);
|
| + VLOG(3) << "modifying fd= " << fd << " " << EventMaskToString(ee.events);
|
| if (epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, fd, &ee)) {
|
| int saved_errno = errno;
|
| char buf[kErrorBufferSize];
|
| @@ -614,7 +612,7 @@ void EpollServer::ModifyFD(int fd, int remove_event, int add_event) {
|
| }
|
|
|
| if (fd_i->cb != NULL) {
|
| - int & event_mask = fd_i->event_mask;
|
| + int& event_mask = fd_i->event_mask;
|
| VLOG(3) << "fd= " << fd
|
| << " event_mask before: " << EventMaskToString(event_mask);
|
| event_mask &= ~remove_event;
|
| @@ -648,10 +646,7 @@ void EpollServer::WaitForEventsAndCallHandleEvents(int64 timeout_in_us,
|
| }
|
| }
|
| const int timeout_in_ms = timeout_in_us / 1000;
|
| - int nfds = epoll_wait_impl(epoll_fd_,
|
| - events,
|
| - events_size,
|
| - timeout_in_ms);
|
| + int nfds = epoll_wait_impl(epoll_fd_, events, events_size, timeout_in_ms);
|
| VLOG(3) << "nfds=" << nfds;
|
|
|
| #ifdef EPOLL_SERVER_EVENT_TRACING
|
| @@ -704,7 +699,7 @@ void EpollServer::CallReadyListCallbacks() {
|
|
|
| event.out_ready_mask = 0;
|
| event.in_events =
|
| - cb_and_mask->events_asserted | cb_and_mask->events_to_fake;
|
| + cb_and_mask->events_asserted | cb_and_mask->events_to_fake;
|
| // TODO(fenix): get rid of the two separate fields in cb_and_mask.
|
| cb_and_mask->events_asserted = 0;
|
| cb_and_mask->events_to_fake = 0;
|
| @@ -739,8 +734,7 @@ void EpollServer::CallAndReregisterAlarmEvents() {
|
|
|
| // execute alarms.
|
| for (TimeToAlarmCBMap::iterator i = alarm_map_.begin();
|
| - i != alarm_map_.end();
|
| - ) {
|
| + i != alarm_map_.end();) {
|
| if (i->first > now_in_us) {
|
| break;
|
| }
|
| @@ -748,8 +742,8 @@ void EpollServer::CallAndReregisterAlarmEvents() {
|
| // Execute the OnAlarm() only if we did not register
|
| // it in this loop itself.
|
| const bool added_in_this_round =
|
| - alarms_reregistered_and_should_be_skipped_.find(cb)
|
| - != alarms_reregistered_and_should_be_skipped_.end();
|
| + alarms_reregistered_and_should_be_skipped_.find(cb) !=
|
| + alarms_reregistered_and_should_be_skipped_.end();
|
| if (added_in_this_round) {
|
| ++i;
|
| continue;
|
| @@ -767,9 +761,8 @@ void EpollServer::CallAndReregisterAlarmEvents() {
|
| // can be reexecuted in this loop, and hence we do not need to
|
| // worry about a recursive loop.
|
| DVLOG(3) << "Reregistering alarm "
|
| - << " " << cb
|
| - << " " << new_timeout_time_in_us
|
| - << " " << now_in_us;
|
| + << " " << cb << " " << new_timeout_time_in_us << " "
|
| + << now_in_us;
|
| if (new_timeout_time_in_us <= now_in_us) {
|
| alarms_reregistered_and_should_be_skipped_.insert(cb);
|
| }
|
|
|