From 959d378f2aeea202aa09b79382c18ea017a79dce Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 17 Jan 2016 11:19:19 +0900 Subject: [PATCH] nghttpx: Optimize accesslog write --- src/shrpx_client_handler.cc | 49 +++++++++++++++++++------------------ src/shrpx_log.cc | 8 ++++++ src/shrpx_log.h | 13 ++++++---- src/template.h | 5 ++++ 4 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/shrpx_client_handler.cc b/src/shrpx_client_handler.cc index 67bf9e50..e9cc311b 100644 --- a/src/shrpx_client_handler.cc +++ b/src/shrpx_client_handler.cc @@ -835,25 +835,27 @@ void ClientHandler::write_accesslog(Downstream *downstream) { upstream_accesslog( get_config()->accesslog_format, LogSpec{ - downstream, ipaddr_.c_str(), http2::to_method_string(req.method), + downstream, ipaddr_, http2::to_method_string(req.method), req.method == HTTP_CONNECT - ? req.authority.c_str() + ? req.authority : (get_config()->http2_proxy || get_config()->client_proxy) - ? construct_absolute_request_uri(req).c_str() - : req.path.empty() ? req.method == HTTP_OPTIONS ? "*" : "-" - : req.path.c_str(), + ? construct_absolute_request_uri(req) + : req.path.empty() + ? req.method == HTTP_OPTIONS + ? StringAdaptor::from_lit("*") + : StringAdaptor::from_lit("-") + : req.path, - alpn_.c_str(), - nghttp2::ssl::get_tls_session_info(&tls_info, conn_.tls.ssl), + alpn_, nghttp2::ssl::get_tls_session_info(&tls_info, conn_.tls.ssl), std::chrono::system_clock::now(), // time_now downstream->get_request_start_time(), // request_start_time std::chrono::high_resolution_clock::now(), // request_end_time req.http_major, req.http_minor, resp.http_status, - downstream->response_sent_body_length, port_.c_str(), - get_config()->port, get_config()->pid, + downstream->response_sent_body_length, port_, get_config()->port, + get_config()->pid, }); } @@ -863,21 +865,20 @@ void ClientHandler::write_accesslog(int major, int minor, unsigned int status, auto highres_now = std::chrono::high_resolution_clock::now(); nghttp2::ssl::TLSSessionInfo tls_info; - upstream_accesslog(get_config()->accesslog_format, - LogSpec{ - nullptr, ipaddr_.c_str(), - "-", // method - "-", // path, - alpn_.c_str(), nghttp2::ssl::get_tls_session_info( - &tls_info, conn_.tls.ssl), - time_now, - highres_now, // request_start_time TODO is - // there a better value? - highres_now, // request_end_time - major, minor, // major, minor - status, body_bytes_sent, port_.c_str(), - get_config()->port, get_config()->pid, - }); + upstream_accesslog( + get_config()->accesslog_format, + LogSpec{ + nullptr, ipaddr_, + StringAdaptor::from_lit("-"), // method + StringAdaptor::from_lit("-"), // path, + alpn_, nghttp2::ssl::get_tls_session_info(&tls_info, conn_.tls.ssl), + time_now, + highres_now, // request_start_time TODO is + // there a better value? + highres_now, // request_end_time + major, minor, // major, minor + status, body_bytes_sent, port_, get_config()->port, get_config()->pid, + }); } ClientHandler::ReadBuf *ClientHandler::get_rb() { return &rb_; } diff --git a/src/shrpx_log.cc b/src/shrpx_log.cc index 10c398b4..e880caa0 100644 --- a/src/shrpx_log.cc +++ b/src/shrpx_log.cc @@ -182,6 +182,14 @@ std::pair copy(const std::string &src, size_t avail, } } // namespace +namespace { +template +std::pair copy(const StringAdaptor &src, size_t avail, + OutputIterator oitr) { + return copy(src.c_str(), src.size(), avail, oitr); +} +} // namespace + namespace { template std::pair copy_l(const char(&src)[N], size_t avail, diff --git a/src/shrpx_log.h b/src/shrpx_log.h index 2fae8d04..018206ab 100644 --- a/src/shrpx_log.h +++ b/src/shrpx_log.h @@ -36,6 +36,9 @@ #include "shrpx_log_config.h" #include "ssl.h" +#include "template.h" + +using namespace nghttp2; #define ENABLE_LOG 1 @@ -139,10 +142,10 @@ struct LogFragment { struct LogSpec { Downstream *downstream; - const char *remote_addr; - const char *method; - const char *path; - const char *alpn; + StringAdaptor remote_addr; + StringAdaptor method; + StringAdaptor path; + StringAdaptor alpn; const nghttp2::ssl::TLSSessionInfo *tls_info; std::chrono::system_clock::time_point time_now; std::chrono::high_resolution_clock::time_point request_start_time; @@ -150,7 +153,7 @@ struct LogSpec { int major, minor; unsigned int status; int64_t body_bytes_sent; - const char *remote_port; + StringAdaptor remote_port; uint16_t server_port; pid_t pid; }; diff --git a/src/template.h b/src/template.h index 25c0828e..d0314679 100644 --- a/src/template.h +++ b/src/template.h @@ -254,6 +254,11 @@ struct StringAdaptor { StringAdaptor(const T &s) : base(s.c_str()), len(s.size()) {} StringAdaptor(const char *s) : base(s), len(strlen(s)) {} + StringAdaptor(const char *s, size_t n) : base(s), len(n) {} + + template static StringAdaptor from_lit(const char(&s)[N]) { + return StringAdaptor(s, N - 1); + } const char *c_str() const { return base; } size_t size() const { return len; }