From 37cb94d15488bc9ed1d20b0adc6744801112d65b Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 27 Jan 2013 17:16:13 +0900 Subject: [PATCH] src: Use clock_gettime instead of gettimeofday if available --- configure.ac | 11 +++++++++++ src/Makefile.am | 3 ++- src/spdycat.cc | 6 +++--- src/spdylay_ssl.cc | 26 ++++++++++++++++++++------ src/spdylay_ssl.h | 2 ++ 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/configure.ac b/configure.ac index a844669..627d002 100644 --- a/configure.ac +++ b/configure.ac @@ -72,6 +72,9 @@ AM_CONDITIONAL([HAVE_STDCXX_11], # Additional libraries required for tests. TESTS_LIBS= +# Additional libraries required for programs under src directory. +SRC_LIBS= + LIBS_OLD=$LIBS # Search for dlsym function, which is used in tests. Linux needs -ldl, # but netbsd does not need it. @@ -79,6 +82,13 @@ AC_SEARCH_LIBS([dlsym], [dl]) TESTS_LIBS=$LIBS $TESTS_LIBS LIBS=$LIBS_OLD +LIBS_OLD=$LIBS +AC_SEARCH_LIBS([clock_gettime], [rt], + [AC_DEFINE([HAVE_CLOCK_GETTIME], [1], + [Define to 1 if you have the `clock_gettime`.])]) +SRC_LIBS=$LIBS $SRC_LIBS +LIBS=$LIBS_OLD + # zlib PKG_CHECK_MODULES([ZLIB], [zlib >= 1.2.3]) LIBS="$ZLIB_LIBS $LIBS" @@ -242,6 +252,7 @@ if test "x$maintainer_mode" != "xno"; then fi AC_SUBST([TESTS_LIBS]) +AC_SUBST([SRC_LIBS]) AC_CONFIG_FILES([ Makefile diff --git a/src/Makefile.am b/src/Makefile.am index 17eef3e..4459db5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -26,7 +26,8 @@ if ENABLE_SRC AM_CFLAGS = -Wall AM_CPPFLAGS = -Wall -I$(srcdir)/../lib/includes -I$(builddir)/../lib/includes \ @OPENSSL_CFLAGS@ @XML_CPPFLAGS@ @LIBEVENT_OPENSSL_CFLAGS@ @DEFS@ -AM_LDFLAGS = @OPENSSL_LIBS@ @XML_LIBS@ @LIBEVENT_OPENSSL_LIBS@ -pthread +AM_LDFLAGS = @OPENSSL_LIBS@ @XML_LIBS@ @LIBEVENT_OPENSSL_LIBS@ @SRC_LIBS@ \ + -pthread LDADD = $(top_builddir)/lib/libspdylay.la bin_PROGRAMS = spdycat spdyd diff --git a/src/spdycat.cc b/src/spdycat.cc index fbfdd73..749772c 100644 --- a/src/spdycat.cc +++ b/src/spdycat.cc @@ -105,7 +105,7 @@ struct RequestStat { void record_time(timeval *tv) { - gettimeofday(tv, 0); + get_time(tv); } bool has_uri_field(const http_parser_url &u, http_parser_url_fields field) @@ -601,7 +601,7 @@ int spdy_evloop(int fd, SSL *ssl, int spdy_version, SpdySession& spdySession, timeval tv1, tv2; while(!sc.finish()) { if(config.timeout != -1) { - gettimeofday(&tv1, 0); + get_time(&tv1); } int nfds = poll(pollfds, npollfds, timeout); if(nfds == -1) { @@ -627,7 +627,7 @@ int spdy_evloop(int fd, SSL *ssl, int spdy_version, SpdySession& spdySession, break; } if(config.timeout != -1) { - gettimeofday(&tv2, 0); + get_time(&tv2); timeout -= time_delta(tv2, tv1); if (timeout <= 0) { std::cerr << "Requests to " << spdySession.hostport << " timed out." diff --git a/src/spdylay_ssl.cc b/src/spdylay_ssl.cc index 3075b77..03f996e 100644 --- a/src/spdylay_ssl.cc +++ b/src/spdylay_ssl.cc @@ -299,7 +299,7 @@ int nonblock_connect_to(const std::string& host, uint16_t port, int timeout) struct timeval tv1, tv2; struct pollfd pfd = {fd, POLLOUT, 0}; if(timeout != -1) { - gettimeofday(&tv1, 0); + get_time(&tv1); } r = poll(&pfd, 1, timeout); if(r == 0) { @@ -308,7 +308,7 @@ int nonblock_connect_to(const std::string& host, uint16_t port, int timeout) return -1; } else { if(timeout != -1) { - gettimeofday(&tv2, 0); + get_time(&tv2); timeout -= time_delta(tv2, tv1); if(timeout <= 0) { return -2; @@ -828,7 +828,7 @@ int ssl_nonblock_handshake(SSL *ssl, int fd, int& timeout) timeval tv1, tv2; while(1) { if(timeout != -1) { - gettimeofday(&tv1, 0); + get_time(&tv1); } int rv = poll(&pfd, 1, timeout); if(rv == 0) { @@ -843,7 +843,7 @@ int ssl_nonblock_handshake(SSL *ssl, int fd, int& timeout) return -1; } else if(rv < 0) { if(timeout != -1) { - gettimeofday(&tv2, 0); + get_time(&tv2); timeout -= time_delta(tv2, tv1); if(timeout <= 0) { return -2; @@ -892,12 +892,12 @@ timeval base_tv; void reset_timer() { - gettimeofday(&base_tv, 0); + get_time(&base_tv); } void get_timer(timeval* tv) { - gettimeofday(tv, 0); + get_time(tv); tv->tv_usec -= base_tv.tv_usec; tv->tv_sec -= base_tv.tv_sec; if(tv->tv_usec < 0) { @@ -906,4 +906,18 @@ void get_timer(timeval* tv) } } +int get_time(timeval *tv) +{ + 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(&base_tv, 0); +#endif // !HAVE_CLOCK_GETTIME + return rv; +} + } // namespace spdylay diff --git a/src/spdylay_ssl.h b/src/spdylay_ssl.h index 00fd4af..27a13af 100644 --- a/src/spdylay_ssl.h +++ b/src/spdylay_ssl.h @@ -150,6 +150,8 @@ void reset_timer(); void get_timer(timeval *tv); +int get_time(timeval *tv); + void print_timer(); enum {