From 0a649e2499eb1c9e4cd656064e8dc83b8038bb8b Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 16 Sep 2013 17:36:24 +0900 Subject: [PATCH] src: Use std::chrono instead of gettimeofday and clock_gettime --- src/app_helper.cc | 39 +++++------------------- src/app_helper.h | 15 ++++++++-- src/nghttp.cc | 76 +++++++++++++++++++++-------------------------- 3 files changed, 54 insertions(+), 76 deletions(-) diff --git a/src/app_helper.cc b/src/app_helper.cc index 9035c3e4..61896d2b 100644 --- a/src/app_helper.cc +++ b/src/app_helper.cc @@ -164,11 +164,10 @@ void print_nv(nghttp2_nv *nva, size_t nvlen) void print_timer() { - timeval tv; - get_timer(&tv); + auto millis = get_timer(); printf("%s[%3ld.%03ld]%s", ansi_esc("\033[33m"), - (long int)tv.tv_sec, tv.tv_usec/1000, + (long int)(millis.count()/1000), (long int)(millis.count()%1000), ansi_escend()); } @@ -438,45 +437,23 @@ int on_data_send_callback return 0; } -int64_t time_delta(const timeval& a, const timeval& b) -{ - int64_t res = (a.tv_sec - b.tv_sec) * 1000; - res += (a.tv_usec - b.tv_usec) / 1000; - return res; -} - namespace { -timeval base_tv; +std::chrono::steady_clock::time_point base_tv; } // namespace void reset_timer() { - get_time(&base_tv); + base_tv = std::chrono::steady_clock::now(); } -void get_timer(timeval* tv) +std::chrono::milliseconds get_timer() { - get_time(tv); - tv->tv_usec -= base_tv.tv_usec; - tv->tv_sec -= base_tv.tv_sec; - if(tv->tv_usec < 0) { - tv->tv_usec += 1000000; - --tv->tv_sec; - } + return time_delta(std::chrono::steady_clock::now(), base_tv); } -int get_time(timeval *tv) +std::chrono::steady_clock::time_point get_time() { - int rv; -#ifdef HAVE_CLOCK_GETTIME - timespec ts; - rv = clock_gettime(CLOCK_MONOTONIC, &ts); - tv->tv_sec = ts.tv_sec; - tv->tv_usec = ts.tv_nsec/1000; -#else // !HAVE_CLOCK_GETTIME - rv = gettimeofday(tv, 0); -#endif // !HAVE_CLOCK_GETTIME - return rv; + return std::chrono::steady_clock::now(); } } // namespace nghttp2 diff --git a/src/app_helper.h b/src/app_helper.h index 6860e433..5d1624b7 100644 --- a/src/app_helper.h +++ b/src/app_helper.h @@ -31,7 +31,9 @@ #include #include #include + #include +#include #include @@ -74,13 +76,20 @@ int on_data_send_callback // Returns difference between |a| and |b| in milliseconds, assuming // |a| is more recent than |b|. -int64_t time_delta(const timeval& a, const timeval& b); +template +std::chrono::milliseconds time_delta(const TimePoint& a, const TimePoint& b) +{ + return std::chrono::duration_cast(a - b); +} +// Resets timer void reset_timer(); -void get_timer(timeval *tv); +// Returns the duration since timer reset. +std::chrono::milliseconds get_timer(); -int get_time(timeval *tv); +// Returns current time point. +std::chrono::steady_clock::time_point get_time(); void print_timer(); diff --git a/src/nghttp.cc b/src/nghttp.cc index 42400953..1a0d9a56 100644 --- a/src/nghttp.cc +++ b/src/nghttp.cc @@ -50,6 +50,7 @@ #include #include #include +#include #include #include @@ -111,28 +112,21 @@ struct Config { }; } // namespace -namespace { -struct RequestStat { - timeval on_syn_stream_time; - timeval on_syn_reply_time; - timeval on_complete_time; - RequestStat() - { - on_syn_stream_time.tv_sec = -1; - on_syn_stream_time.tv_usec = -1; - on_syn_reply_time.tv_sec = -1; - on_syn_reply_time.tv_usec = -1; - on_complete_time.tv_sec = -1; - on_complete_time.tv_usec = -1; - } +enum StatStage { + STAT_INITIAL, + STAT_ON_REQUEST, + STAT_ON_RESPONSE, + STAT_ON_COMPLETE }; -} // namespace namespace { -void record_time(timeval *tv) -{ - get_time(tv); -} +struct RequestStat { + std::chrono::steady_clock::time_point on_request_time; + std::chrono::steady_clock::time_point on_response_time; + std::chrono::steady_clock::time_point on_complete_time; + StatStage stage; + RequestStat():stage(STAT_INITIAL) {} +}; } // namespace namespace { @@ -318,31 +312,29 @@ struct Request { } } - void record_syn_stream_time() + void record_request_time() { - record_time(&stat.on_syn_stream_time); + stat.stage = STAT_ON_REQUEST; + stat.on_request_time = get_time(); } - void record_syn_reply_time() + void record_response_time() { - record_time(&stat.on_syn_reply_time); + stat.stage = STAT_ON_RESPONSE; + stat.on_response_time = get_time(); } void record_complete_time() { - record_time(&stat.on_complete_time); + stat.stage = STAT_ON_COMPLETE; + stat.on_complete_time = get_time(); } }; } // namespace namespace { struct SessionStat { - timeval on_handshake_time; - SessionStat() - { - on_handshake_time.tv_sec = -1; - on_handshake_time.tv_usec = -1; - } + std::chrono::steady_clock::time_point on_handshake_time; }; } // namespace @@ -821,7 +813,7 @@ struct HttpClient { } void record_handshake_time() { - record_time(&stat.on_handshake_time); + stat.on_handshake_time = get_time(); } }; } // namespace @@ -1026,7 +1018,7 @@ void check_stream_id(nghttp2_session *session, int32_t stream_id, stream_id); assert(req); client->streams[stream_id] = req; - req->record_syn_stream_time(); + req->record_request_time(); } } // namespace @@ -1101,7 +1093,7 @@ int on_frame_recv_callback2 // If this is the HTTP Upgrade with OPTIONS method to avoid POST, // req is nullptr. if(req) { - req->record_syn_reply_time(); + req->record_response_time(); } } check_response_header(session, frame, user_data); @@ -1141,23 +1133,23 @@ void print_stats(const HttpClient& client) std::cout << " Status: " << req->status << std::endl; std::cout << " Delta (ms) from handshake(HEADERS):" << std::endl; - if(req->stat.on_syn_reply_time.tv_sec >= 0) { - std::cout << " SYN_REPLY: " - << time_delta(req->stat.on_syn_reply_time, - client.stat.on_handshake_time) + if(req->stat.stage >= STAT_ON_RESPONSE) { + std::cout << " response HEADERS: " + << time_delta(req->stat.on_response_time, + client.stat.on_handshake_time).count() << "(" - << time_delta(req->stat.on_syn_reply_time, - req->stat.on_syn_stream_time) + << time_delta(req->stat.on_response_time, + req->stat.on_request_time).count() << ")" << std::endl; } - if(req->stat.on_complete_time.tv_sec >= 0) { + if(req->stat.stage >= STAT_ON_COMPLETE) { std::cout << " Completed: " << time_delta(req->stat.on_complete_time, - client.stat.on_handshake_time) + client.stat.on_handshake_time).count() << "(" << time_delta(req->stat.on_complete_time, - req->stat.on_syn_stream_time) + req->stat.on_request_time).count() << ")" << std::endl; }