From patchwork Tue Nov 14 20:42:30 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Bodireddy, Bhanuprakash" X-Patchwork-Id: 838030 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yc08b6hlTz9sBW for ; Wed, 15 Nov 2017 07:53:19 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id CF394B2B; Tue, 14 Nov 2017 20:53:15 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id A6D1EAEF for ; Tue, 14 Nov 2017 20:53:14 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 135EB8A for ; Tue, 14 Nov 2017 20:53:13 +0000 (UTC) Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 14 Nov 2017 12:53:13 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,396,1505804400"; d="scan'208";a="176222307" Received: from silpixa00393942.ir.intel.com (HELO silpixa00393942.ger.corp.intel.com) ([10.237.223.42]) by fmsmga006.fm.intel.com with ESMTP; 14 Nov 2017 12:53:12 -0800 From: Bhanuprakash Bodireddy To: dev@openvswitch.org Date: Tue, 14 Nov 2017 20:42:30 +0000 Message-Id: <1510692151-14322-1-git-send-email-bhanuprakash.bodireddy@intel.com> X-Mailer: git-send-email 2.4.11 X-Spam-Status: No, score=-5.0 required=5.0 tests=RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD autolearn=disabled version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: aserdean@ovn.org Subject: [ovs-dev] [PATCH v2 1/2] timeval: Introduce macros to convert timespec and timeval. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org This commit replaces the numbers with MSEC_PER_SEC, NSEC_PER_SEC and USEC_PER_MSEC macros when dealing with timespec and timeval. This commit doesn't change functionality. Signed-off-by: Bhanuprakash Bodireddy --- lib/timeval.c | 29 ++++++++++++++++------------- lib/timeval.h | 7 +++++++ lib/util.c | 6 +++--- ofproto/ofproto-dpif-ipfix.c | 2 +- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/lib/timeval.c b/lib/timeval.c index b60bf30..567c26e 100644 --- a/lib/timeval.c +++ b/lib/timeval.c @@ -266,7 +266,7 @@ time_alarm(unsigned int secs) time_init(); now = time_msec(); - msecs = secs * 1000LL; + msecs = secs * MSEC_PER_SEC * 1LL; deadline = now < LLONG_MAX - msecs ? now + msecs : LLONG_MAX; } @@ -372,25 +372,28 @@ time_poll(struct pollfd *pollfds, int n_pollfds, HANDLE *handles OVS_UNUSED, long long int timespec_to_msec(const struct timespec *ts) { - return (long long int) ts->tv_sec * 1000 + ts->tv_nsec / (1000 * 1000); + return (long long int) ts->tv_sec * MSEC_PER_SEC + + ts->tv_nsec / NSEC_PER_MSEC; } long long int timeval_to_msec(const struct timeval *tv) { - return (long long int) tv->tv_sec * 1000 + tv->tv_usec / 1000; + return (long long int) tv->tv_sec * MSEC_PER_SEC + + tv->tv_usec / USEC_PER_MSEC; } long long int timespec_to_usec(const struct timespec *ts) { - return (long long int) ts->tv_sec * 1000 * 1000 + ts->tv_nsec / 1000; + return (long long int) ts->tv_sec * USEC_PER_SEC + + ts->tv_nsec / NSEC_PER_USEC; } long long int timeval_to_usec(const struct timeval *tv) { - return (long long int) tv->tv_sec * 1000 * 1000 + tv->tv_usec; + return (long long int) tv->tv_sec * USEC_PER_SEC + tv->tv_usec; } /* Returns the monotonic time at which the "time" module was initialized, in @@ -510,8 +513,8 @@ xclock_gettime(clock_t id, struct timespec *ts) static void msec_to_timespec(long long int ms, struct timespec *ts) { - ts->tv_sec = ms / 1000; - ts->tv_nsec = (ms % 1000) * 1000 * 1000; + ts->tv_sec = ms / MSEC_PER_SEC; + ts->tv_nsec = (ms % 1000) * NSEC_PER_MSEC; } static void @@ -596,8 +599,8 @@ timespec_add(struct timespec *sum, tmp.tv_sec = a->tv_sec + b->tv_sec; tmp.tv_nsec = a->tv_nsec + b->tv_nsec; - if (tmp.tv_nsec >= 1000 * 1000 * 1000) { - tmp.tv_nsec -= 1000 * 1000 * 1000; + if (tmp.tv_nsec >= NSEC_PER_SEC) { + tmp.tv_nsec -= NSEC_PER_SEC; tmp.tv_sec++; } @@ -621,7 +624,7 @@ log_poll_interval(long long int last_wakeup) { long long int interval = time_msec() - last_wakeup; - if (interval >= 1000 && !is_warped(&monotonic_clock)) { + if (interval >= MSEC_PER_SEC && !is_warped(&monotonic_clock)) { const struct rusage *last_rusage = get_recent_rusage(); struct rusage rusage; @@ -713,7 +716,7 @@ refresh_rusage(void) if (!getrusage_thread(recent_rusage)) { long long int now = time_msec(); - if (now >= t->newer.when + 3 * 1000) { + if (now >= t->newer.when + 3 * MSEC_PER_SEC) { t->older = t->newer; t->newer.when = now; t->newer.cpu = (timeval_to_msec(&recent_rusage->ru_utime) + @@ -837,7 +840,7 @@ strftime_msec(char *s, size_t max, const char *format, struct tm_msec * localtime_msec(long long int now, struct tm_msec *result) { - time_t now_sec = now / 1000; + time_t now_sec = now / MSEC_PER_SEC; localtime_r(&now_sec, &result->tm); result->msec = now % 1000; return result; @@ -846,7 +849,7 @@ localtime_msec(long long int now, struct tm_msec *result) struct tm_msec * gmtime_msec(long long int now, struct tm_msec *result) { - time_t now_sec = now / 1000; + time_t now_sec = now / MSEC_PER_SEC; gmtime_r(&now_sec, &result->tm); result->msec = now % 1000; return result; diff --git a/lib/timeval.h b/lib/timeval.h index c3dbb51..5e2a731 100644 --- a/lib/timeval.h +++ b/lib/timeval.h @@ -40,6 +40,13 @@ BUILD_ASSERT_DECL(TYPE_IS_SIGNED(time_t)); #define TIME_MAX TYPE_MAXIMUM(time_t) #define TIME_MIN TYPE_MINIMUM(time_t) +#define MSEC_PER_SEC 1000L +#define USEC_PER_SEC 1000000L +#define NSEC_PER_SEC 1000000000L +#define USEC_PER_MSEC 1000L +#define NSEC_PER_MSEC 1000000L +#define NSEC_PER_USEC 1000L + #ifdef _WIN32 #define localtime_r(timep, result) localtime_s(result, timep) #define gmtime_r(timep, result) gmtime_s(result, timep) diff --git a/lib/util.c b/lib/util.c index 9e6edd2..17c2c99 100644 --- a/lib/util.c +++ b/lib/util.c @@ -586,7 +586,7 @@ get_boot_time(void) char line[128]; FILE *stream; - cache_expiration = time_msec() + 5 * 1000; + cache_expiration = time_msec() + 5 * MSEC_PER_SEC; stream = fopen(stat_file, "r"); if (!stream) { @@ -598,7 +598,7 @@ get_boot_time(void) while (fgets(line, sizeof line, stream)) { long long int btime; if (ovs_scan(line, "btime %lld", &btime)) { - boot_time = btime * 1000; + boot_time = btime * MSEC_PER_SEC; goto done; } } @@ -2198,7 +2198,7 @@ xsleep(unsigned int seconds) { ovsrcu_quiesce_start(); #ifdef _WIN32 - Sleep(seconds * 1000); + Sleep(seconds * MSEC_PER_SEC); #else sleep(seconds); #endif diff --git a/ofproto/ofproto-dpif-ipfix.c b/ofproto/ofproto-dpif-ipfix.c index 4d16878..dda7657 100644 --- a/ofproto/ofproto-dpif-ipfix.c +++ b/ofproto/ofproto-dpif-ipfix.c @@ -1869,7 +1869,7 @@ ipfix_update_stats(struct dpif_ipfix_exporter *exporter, static uint64_t ipfix_now(void) { - return time_wall_msec() * 1000ULL; + return time_wall_usec(); } /* Add an entry into a flow cache. The entry is either aggregated into