From patchwork Tue Nov 28 22:02:06 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Bodireddy, Bhanuprakash" X-Patchwork-Id: 842339 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 3ymdGW0xLZz9ryv for ; Wed, 29 Nov 2017 09:13:22 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 3897CBE4; Tue, 28 Nov 2017 22:13:18 +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 CBF93BCE for ; Tue, 28 Nov 2017 22:13:16 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 5DBA4403 for ; Tue, 28 Nov 2017 22:13:16 +0000 (UTC) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 28 Nov 2017 14:13:15 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,468,1505804400"; d="scan'208";a="13090365" Received: from silpixa00393942.ir.intel.com (HELO silpixa00393942.ger.corp.intel.com) ([10.237.223.42]) by orsmga002.jf.intel.com with ESMTP; 28 Nov 2017 14:13:14 -0800 From: Bhanuprakash Bodireddy To: dev@openvswitch.org Date: Tue, 28 Nov 2017 22:02:06 +0000 Message-Id: <1511906526-36128-1-git-send-email-bhanuprakash.bodireddy@intel.com> X-Mailer: git-send-email 2.4.11 X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_RP_MATCHES_RCVD autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Subject: [ovs-dev] [PATCH v3] util: Add high resolution sleep support. 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 introduces xnanosleep() for the threads needing high resolution sleep timeouts. usleep() that provides microsecond granularity is deprecated and threads wanting sub-second(ms,us,ns) granularity can use this implementation. Signed-off-by: Bhanuprakash Bodireddy Acked-by: Alin Gabriel Serdean --- v2 -> v3 * Replace NSEC_PER_SEC Macro with (1000 * 1000 * 1000) lib/timeval.c | 19 +++++++++++++++++++ lib/timeval.h | 1 + lib/util.c | 35 +++++++++++++++++++++++++++++++++++ lib/util.h | 1 + 4 files changed, 56 insertions(+) diff --git a/lib/timeval.c b/lib/timeval.c index b60bf30..193c7ba 100644 --- a/lib/timeval.c +++ b/lib/timeval.c @@ -514,6 +514,25 @@ msec_to_timespec(long long int ms, struct timespec *ts) ts->tv_nsec = (ms % 1000) * 1000 * 1000; } +void +nsec_to_timespec(long long int nsec, struct timespec *ts) +{ + if (!nsec) { + ts->tv_sec = ts->tv_nsec = 0; + return; + } + ts->tv_sec = nsec / (1000 * 1000 * 1000); + + nsec = nsec % (1000 * 1000 * 1000); + /* This is to handle dates before epoch. */ + if (OVS_UNLIKELY(nsec < 0)) { + nsec += 1000 * 1000 * 1000; + ts->tv_sec--; + } + + ts->tv_nsec = nsec; +} + static void timewarp_work(void) { diff --git a/lib/timeval.h b/lib/timeval.h index c3dbb51..08d7a9e 100644 --- a/lib/timeval.h +++ b/lib/timeval.h @@ -73,6 +73,7 @@ size_t strftime_msec(char *s, size_t max, const char *format, const struct tm_msec *); void xgettimeofday(struct timeval *); void xclock_gettime(clock_t, struct timespec *); +void nsec_to_timespec(long long int , struct timespec *); int get_cpu_usage(void); diff --git a/lib/util.c b/lib/util.c index 9e6edd2..62f5fa2 100644 --- a/lib/util.c +++ b/lib/util.c @@ -2205,6 +2205,41 @@ xsleep(unsigned int seconds) ovsrcu_quiesce_end(); } +/* High resolution sleep. */ +void +xnanosleep(uint64_t nanoseconds) +{ + ovsrcu_quiesce_start(); +#ifndef _WIN32 + int retval; + struct timespec ts_sleep; + nsec_to_timespec(nanoseconds, &ts_sleep); + + int error = 0; + do { + retval = nanosleep(&ts_sleep, NULL); + error = retval < 0 ? errno : 0; + } while (error == EINTR); +#else + HANDLE timer = CreateWaitableTimer(NULL, FALSE, NULL); + if (timer) { + LARGE_INTEGER duetime; + duetime.QuadPart = -nanoseconds; + if (SetWaitableTimer(timer, &duetime, 0, NULL, NULL, FALSE)) { + WaitForSingleObject(timer, INFINITE); + } else { + VLOG_ERR_ONCE("SetWaitableTimer Failed (%s)", + ovs_lasterror_to_string()); + } + CloseHandle(timer); + } else { + VLOG_ERR_ONCE("CreateWaitableTimer Failed (%s)", + ovs_lasterror_to_string()); + } +#endif + ovsrcu_quiesce_end(); +} + /* Determine whether standard output is a tty or not. This is useful to decide * whether to use color output or not when --color option for utilities is set * to `auto`. diff --git a/lib/util.h b/lib/util.h index 3c43c2c..d355313 100644 --- a/lib/util.h +++ b/lib/util.h @@ -502,6 +502,7 @@ ovs_u128_and(const ovs_u128 a, const ovs_u128 b) } void xsleep(unsigned int seconds); +void xnanosleep(uint64_t nanoseconds); bool is_stdout_a_tty(void);