Commit 83309b63 authored by Tatsuhiro Tsujikawa's avatar Tatsuhiro Tsujikawa

nghttpx: Reduce epoll_ctl call

parent 23dd428d
...@@ -256,7 +256,7 @@ ClientHandler::ClientHandler(bufferevent *bev, ...@@ -256,7 +256,7 @@ ClientHandler::ClientHandler(bufferevent *bev,
CLOG(FATAL, this) << "bufferevent_add_to_rate_limit_group() failed"; CLOG(FATAL, this) << "bufferevent_add_to_rate_limit_group() failed";
} }
bufferevent_enable(bev_, EV_READ | EV_WRITE); util::bev_enable_unless(bev_, EV_READ | EV_WRITE);
bufferevent_setwatermark(bev_, EV_READ, 0, SHRPX_READ_WATERMARK); bufferevent_setwatermark(bev_, EV_READ, 0, SHRPX_READ_WATERMARK);
set_upstream_timeouts(&get_config()->upstream_read_timeout, set_upstream_timeouts(&get_config()->upstream_read_timeout,
&get_config()->upstream_write_timeout); &get_config()->upstream_write_timeout);
...@@ -298,7 +298,7 @@ ClientHandler::~ClientHandler() ...@@ -298,7 +298,7 @@ ClientHandler::~ClientHandler()
bufferevent_remove_from_rate_limit_group(bev_); bufferevent_remove_from_rate_limit_group(bev_);
bufferevent_disable(bev_, EV_READ | EV_WRITE); util::bev_disable_unless(bev_, EV_READ | EV_WRITE);
bufferevent_free(bev_); bufferevent_free(bev_);
if(ssl_) { if(ssl_) {
......
...@@ -89,7 +89,7 @@ int Http2Session::disconnect() ...@@ -89,7 +89,7 @@ int Http2Session::disconnect()
} }
if(bev_) { if(bev_) {
int fd = bufferevent_getfd(bev_); int fd = bufferevent_getfd(bev_);
bufferevent_disable(bev_, EV_READ | EV_WRITE); util::bev_disable_unless(bev_, EV_READ | EV_WRITE);
bufferevent_free(bev_); bufferevent_free(bev_);
bev_ = nullptr; bev_ = nullptr;
if(fd != -1) { if(fd != -1) {
...@@ -219,7 +219,7 @@ int Http2Session::init_notification() ...@@ -219,7 +219,7 @@ int Http2Session::init_notification()
close(sockpair[1]); close(sockpair[1]);
return -1; return -1;
} }
bufferevent_enable(rdbev_, EV_READ); util::bev_enable_unless(rdbev_, EV_READ);
bufferevent_setcb(rdbev_, notify_readcb, nullptr, notify_eventcb, this); bufferevent_setcb(rdbev_, notify_readcb, nullptr, notify_eventcb, this);
return 0; return 0;
} }
...@@ -417,7 +417,7 @@ int Http2Session::initiate_connection() ...@@ -417,7 +417,7 @@ int Http2Session::initiate_connection()
close(fd); close(fd);
return SHRPX_ERR_NETWORK; return SHRPX_ERR_NETWORK;
} }
bufferevent_enable(bev_, EV_READ); util::bev_enable_unless(bev_, EV_READ);
bufferevent_set_timeouts(bev_, &get_config()->downstream_read_timeout, bufferevent_set_timeouts(bev_, &get_config()->downstream_read_timeout,
&get_config()->downstream_write_timeout); &get_config()->downstream_write_timeout);
...@@ -534,7 +534,7 @@ int Http2Session::initiate_connection() ...@@ -534,7 +534,7 @@ int Http2Session::initiate_connection()
} }
bufferevent_setwatermark(bev_, EV_READ, 0, SHRPX_READ_WATERMARK); bufferevent_setwatermark(bev_, EV_READ, 0, SHRPX_READ_WATERMARK);
bufferevent_enable(bev_, EV_READ); util::bev_enable_unless(bev_, EV_READ);
bufferevent_setcb(bev_, readcb, writecb, eventcb, this); bufferevent_setcb(bev_, readcb, writecb, eventcb, this);
// Set timeout for HTTP2 session // Set timeout for HTTP2 session
reset_timeouts(); reset_timeouts();
......
...@@ -54,7 +54,7 @@ HttpDownstreamConnection::HttpDownstreamConnection ...@@ -54,7 +54,7 @@ HttpDownstreamConnection::HttpDownstreamConnection
HttpDownstreamConnection::~HttpDownstreamConnection() HttpDownstreamConnection::~HttpDownstreamConnection()
{ {
if(bev_) { if(bev_) {
bufferevent_disable(bev_, EV_READ | EV_WRITE); util::bev_disable_unless(bev_, EV_READ | EV_WRITE);
bufferevent_free(bev_); bufferevent_free(bev_);
} }
// Downstream and DownstreamConnection may be deleted // Downstream and DownstreamConnection may be deleted
...@@ -127,7 +127,7 @@ int HttpDownstreamConnection::attach_downstream(Downstream *downstream) ...@@ -127,7 +127,7 @@ int HttpDownstreamConnection::attach_downstream(Downstream *downstream)
response_htp_.data = downstream_; response_htp_.data = downstream_;
bufferevent_setwatermark(bev_, EV_READ, 0, SHRPX_READ_WATERMARK); bufferevent_setwatermark(bev_, EV_READ, 0, SHRPX_READ_WATERMARK);
bufferevent_enable(bev_, EV_READ); util::bev_enable_unless(bev_, EV_READ);
bufferevent_setcb(bev_, bufferevent_setcb(bev_,
upstream->get_downstream_readcb(), upstream->get_downstream_readcb(),
upstream->get_downstream_writecb(), upstream->get_downstream_writecb(),
...@@ -369,7 +369,7 @@ void HttpDownstreamConnection::detach_downstream(Downstream *downstream) ...@@ -369,7 +369,7 @@ void HttpDownstreamConnection::detach_downstream(Downstream *downstream)
} }
downstream_ = nullptr; downstream_ = nullptr;
ioctrl_.force_resume_read(); ioctrl_.force_resume_read();
bufferevent_enable(bev_, EV_READ); util::bev_enable_unless(bev_, EV_READ);
bufferevent_setcb(bev_, 0, 0, idle_eventcb, this); bufferevent_setcb(bev_, 0, 0, idle_eventcb, this);
// On idle state, just enable read timeout. Normally idle downstream // On idle state, just enable read timeout. Normally idle downstream
// connection will get EOF from the downstream server and closed. // connection will get EOF from the downstream server and closed.
......
...@@ -26,6 +26,10 @@ ...@@ -26,6 +26,10 @@
#include <algorithm> #include <algorithm>
#include "util.h"
using namespace nghttp2;
namespace shrpx { namespace shrpx {
IOControl::IOControl(bufferevent *bev) IOControl::IOControl(bufferevent *bev)
...@@ -45,7 +49,7 @@ void IOControl::pause_read(IOCtrlReason reason) ...@@ -45,7 +49,7 @@ void IOControl::pause_read(IOCtrlReason reason)
{ {
rdbits_ |= reason; rdbits_ |= reason;
if(bev_) { if(bev_) {
bufferevent_disable(bev_, EV_READ); util::bev_disable_unless(bev_, EV_READ);
} }
} }
...@@ -54,7 +58,7 @@ bool IOControl::resume_read(IOCtrlReason reason) ...@@ -54,7 +58,7 @@ bool IOControl::resume_read(IOCtrlReason reason)
rdbits_ &= ~reason; rdbits_ &= ~reason;
if(rdbits_ == 0) { if(rdbits_ == 0) {
if(bev_) { if(bev_) {
bufferevent_enable(bev_, EV_READ); util::bev_enable_unless(bev_, EV_READ);
} }
return true; return true;
} else { } else {
...@@ -66,7 +70,7 @@ void IOControl::force_resume_read() ...@@ -66,7 +70,7 @@ void IOControl::force_resume_read()
{ {
rdbits_ = 0; rdbits_ = 0;
if(bev_) { if(bev_) {
bufferevent_enable(bev_, EV_READ); util::bev_enable_unless(bev_, EV_READ);
} }
} }
......
...@@ -112,7 +112,7 @@ void worker_writecb(bufferevent *bev, void *ptr) ...@@ -112,7 +112,7 @@ void worker_writecb(bufferevent *bev, void *ptr)
// Disable bev so that this won' be called accidentally in the // Disable bev so that this won' be called accidentally in the
// future. // future.
bufferevent_disable(bev, EV_READ | EV_WRITE); util::bev_disable_unless(bev, EV_READ | EV_WRITE);
} }
} // namespace } // namespace
#endif // NOTHREADS #endif // NOTHREADS
......
...@@ -113,7 +113,7 @@ void Worker::run() ...@@ -113,7 +113,7 @@ void Worker::run()
http2session.get(), http2session.get(),
http1_connect_blocker.get()); http1_connect_blocker.get());
bufferevent_enable(bev.get(), EV_READ); util::bev_enable_unless(bev.get(), EV_READ);
bufferevent_setcb(bev.get(), readcb, nullptr, eventcb, receiver.get()); bufferevent_setcb(bev.get(), readcb, nullptr, eventcb, receiver.get());
event_base_loop(evbase.get(), 0); event_base_loop(evbase.get(), 0);
......
...@@ -657,6 +657,24 @@ char* get_exec_path(int argc, char **const argv, const char *cwd) ...@@ -657,6 +657,24 @@ char* get_exec_path(int argc, char **const argv, const char *cwd)
return path; return path;
} }
void bev_enable_unless(bufferevent *bev, int events)
{
if((bufferevent_get_enabled(bev) & events) == events) {
return;
}
bufferevent_enable(bev, events);
}
void bev_disable_unless(bufferevent *bev, int events)
{
if((bufferevent_get_enabled(bev) & events) == 0) {
return;
}
bufferevent_disable(bev, events);
}
} // namespace util } // namespace util
} // namespace nghttp2 } // namespace nghttp2
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include <memory> #include <memory>
#include <event2/buffer.h> #include <event2/buffer.h>
#include <event2/bufferevent.h>
#include "http-parser/http_parser.h" #include "http-parser/http_parser.h"
...@@ -481,6 +482,12 @@ std::string ascii_dump(const uint8_t *data, size_t len); ...@@ -481,6 +482,12 @@ std::string ascii_dump(const uint8_t *data, size_t len);
// it. // it.
char* get_exec_path(int argc, char **const argv, const char *cwd); char* get_exec_path(int argc, char **const argv, const char *cwd);
// These functions are provided to reduce epoll_ctl syscall. Avoid
// calling bufferevent_enable/disable() unless it is required by
// sniffing current enabled events.
void bev_enable_unless(bufferevent *bev, int events);
void bev_disable_unless(bufferevent *bev, int events);
} // namespace util } // namespace util
} // namespace nghttp2 } // namespace nghttp2
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment