From patchwork Fri Dec 18 12:01:52 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Khoronzhuk X-Patchwork-Id: 58646 Delivered-To: patch@linaro.org Received: by 10.112.89.199 with SMTP id bq7csp978621lbb; Fri, 18 Dec 2015 04:03:53 -0800 (PST) X-Received: by 10.140.232.4 with SMTP id d4mr4572601qhc.27.1450440233633; Fri, 18 Dec 2015 04:03:53 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id e13si15911923qka.120.2015.12.18.04.03.53; Fri, 18 Dec 2015 04:03:53 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dkim=neutral (body hash did not verify) header.i=@linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 37576616A4; Fri, 18 Dec 2015 12:03:53 +0000 (UTC) Authentication-Results: lists.linaro.org; dkim=fail reason="verification failed; unprotected key" header.d=linaro.org header.i=@linaro.org header.b=Q39gSBj8; dkim-adsp=none (unprotected policy); dkim-atps=neutral X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id D11F76161B; Fri, 18 Dec 2015 12:02:55 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id E72906160F; Fri, 18 Dec 2015 12:02:50 +0000 (UTC) Received: from mail-lb0-f180.google.com (mail-lb0-f180.google.com [209.85.217.180]) by lists.linaro.org (Postfix) with ESMTPS id 688A86160F for ; Fri, 18 Dec 2015 12:02:08 +0000 (UTC) Received: by mail-lb0-f180.google.com with SMTP id yq9so42913700lbb.3 for ; Fri, 18 Dec 2015 04:02:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=YH0GVVKxl4svd7iXUG10ZJX/EZjj/RXxH48mbEdExy0=; b=Q39gSBj8Bka+8aEKVQ2II9KAov+m08h3pFmjT7qlK6zbOLUFLzLYp79qabtkbijClm XtpydZRHqBJhx8JQpSuj8K58zMwt89+/fLhVglFxr2ZaMLLbpJ6WS1BBokoidqEmrr4S gL71SPUs2Q7XqgfKWt9gtDq6Rd70qMcRmteuU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=YH0GVVKxl4svd7iXUG10ZJX/EZjj/RXxH48mbEdExy0=; b=UtiacRIvvhbe0DOWsDpNrlHLwkEsu5Zmv1qX17cfOHRpXXi8SAcsOEjAd64ZALytxq SuSF98o2upccjZq3ZnrLVyg6JZoEni5sFwpCKUzYUNY42PI3iodt7HGRbrXQ+qMHzdpl 2/LLymKQxXI+wC+1SCd3ZMLnTsSgJXPxDXlatzL09jh/Dr81LwolTcISlsI3X0c2xBL+ kzZHJtgp5MgtWMZT5o2qLSYqvwfZTu+Zu8D4nsGFd6sCmr66wtglIVUweczRW4bNngNp zNM9zHXD/vzyi4pKbbhXgw6Gr+G0PiMKl0+McxOQhk+IEoi+9AtzGBPzN/xXcyMIyWaW DhzQ== X-Gm-Message-State: ALoCoQkMIx+uG470qZNWxOfxjoeFpOtjjIJbMe63AEnN3TySk9eLgrv4qEeKLqvbzmf0KBU1uCzgTuBvi4fYCIdvPDDtT2CMJQ== X-Received: by 10.112.201.132 with SMTP id ka4mr1160390lbc.71.1450440127139; Fri, 18 Dec 2015 04:02:07 -0800 (PST) Received: from localhost.localdomain ([195.238.92.128]) by smtp.gmail.com with ESMTPSA id e63sm2731250lfe.5.2015.12.18.04.02.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 18 Dec 2015 04:02:05 -0800 (PST) From: Ivan Khoronzhuk To: lng-odp@lists.linaro.org Date: Fri, 18 Dec 2015 14:01:52 +0200 Message-Id: <1450440117-5588-2-git-send-email-ivan.khoronzhuk@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1450440117-5588-1-git-send-email-ivan.khoronzhuk@linaro.org> References: <1450440117-5588-1-git-send-email-ivan.khoronzhuk@linaro.org> X-Topics: patch Cc: petri.savolainen@nokia.com Subject: [lng-odp] [API-NEXT PATCH v5 1/6] api: time: add resolution and wait API calls X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" This patch adds odp_time_local_res() and odp_time_wait_until(), odp_time_wait_ns() APIs. Signed-off-by: Ivan Khoronzhuk --- include/odp/api/time.h | 25 ++++++++++ platform/linux-generic/odp_time.c | 98 ++++++++++++++++++++++++++++++--------- 2 files changed, 101 insertions(+), 22 deletions(-) diff --git a/include/odp/api/time.h b/include/odp/api/time.h index 9865d81..1f94710 100644 --- a/include/odp/api/time.h +++ b/include/odp/api/time.h @@ -101,6 +101,31 @@ odp_time_t odp_time_local_from_ns(uint64_t ns); int odp_time_cmp(odp_time_t t2, odp_time_t t1); /** + * Local time resolution in hertz + * + * @return Local time resolution in hertz + */ +uint64_t odp_time_local_res(void); + +/** + * Wait until the specified (wall clock) time has been reached + * + * The thread potentially busy loop the entire wait time. + * + * @param time Time to reach before continue + */ +void odp_time_wait_until(odp_time_t time); + +/** + * Wait the specified number of nanoseconds + * + * The thread potentially busy loop the entire wait time. + * + * @param ns Time in nanoseconds to wait + */ +void odp_time_wait_ns(uint64_t ns); + +/** * Get printable value for an odp_time_t * * @param time time to be printed diff --git a/platform/linux-generic/odp_time.c b/platform/linux-generic/odp_time.c index 9b64b37..a3ab2e3 100644 --- a/platform/linux-generic/odp_time.c +++ b/platform/linux-generic/odp_time.c @@ -29,8 +29,7 @@ uint64_t time_to_ns(odp_time_t time) return ns; } -static inline -odp_time_t time_diff(odp_time_t t2, odp_time_t t1) +static inline odp_time_t time_diff(odp_time_t t2, odp_time_t t1) { odp_time_t time; @@ -45,7 +44,7 @@ odp_time_t time_diff(odp_time_t t2, odp_time_t t1) return time; } -odp_time_t odp_time_local(void) +static inline odp_time_t time_local(void) { int ret; _odp_time_t time; @@ -57,17 +56,33 @@ odp_time_t odp_time_local(void) return time_diff(time.ex, start_time); } -odp_time_t odp_time_diff(odp_time_t t2, odp_time_t t1) +static inline int time_cmp(odp_time_t t2, odp_time_t t1) { - return time_diff(t2, t1); + if (t2.tv_sec < t1.tv_sec) + return -1; + + if (t2.tv_sec > t1.tv_sec) + return 1; + + return t2.tv_nsec - t1.tv_nsec; } -uint64_t odp_time_to_ns(odp_time_t time) +static inline odp_time_t time_sum(odp_time_t t1, odp_time_t t2) { - return time_to_ns(time); + odp_time_t time; + + time.tv_sec = t2.tv_sec + t1.tv_sec; + time.tv_nsec = t2.tv_nsec + t1.tv_nsec; + + if (time.tv_nsec >= (long)ODP_TIME_SEC_IN_NS) { + time.tv_nsec -= ODP_TIME_SEC_IN_NS; + ++time.tv_sec; + } + + return time; } -odp_time_t odp_time_local_from_ns(uint64_t ns) +static inline odp_time_t time_local_from_ns(uint64_t ns) { odp_time_t time; @@ -77,30 +92,69 @@ odp_time_t odp_time_local_from_ns(uint64_t ns) return time; } -int odp_time_cmp(odp_time_t t2, odp_time_t t1) +static inline void time_wait_until(odp_time_t time) { - if (t2.tv_sec < t1.tv_sec) - return -1; + odp_time_t cur; - if (t2.tv_sec > t1.tv_sec) - return 1; + do { + cur = time_local(); + } while (time_cmp(time, cur) > 0); +} - return t2.tv_nsec - t1.tv_nsec; +odp_time_t odp_time_local(void) +{ + return time_local(); +} + +odp_time_t odp_time_diff(odp_time_t t2, odp_time_t t1) +{ + return time_diff(t2, t1); +} + +uint64_t odp_time_to_ns(odp_time_t time) +{ + return time_to_ns(time); +} + +odp_time_t odp_time_local_from_ns(uint64_t ns) +{ + return time_local_from_ns(ns); +} + +int odp_time_cmp(odp_time_t t2, odp_time_t t1) +{ + return time_cmp(t2, t1); } odp_time_t odp_time_sum(odp_time_t t1, odp_time_t t2) { - odp_time_t time; + return time_sum(t1, t2); +} - time.tv_sec = t2.tv_sec + t1.tv_sec; - time.tv_nsec = t2.tv_nsec + t1.tv_nsec; +uint64_t odp_time_local_res(void) +{ + int ret; + struct timespec tres; - if (time.tv_nsec >= (long)ODP_TIME_SEC_IN_NS) { - time.tv_nsec -= ODP_TIME_SEC_IN_NS; - ++time.tv_sec; - } + ret = clock_getres(CLOCK_MONOTONIC_RAW, &tres); + if (odp_unlikely(ret != 0)) + ODP_ABORT("clock_getres failed\n"); - return time; + return ODP_TIME_SEC_IN_NS / (uint64_t)tres.tv_nsec; +} + +void odp_time_wait_ns(uint64_t ns) +{ + odp_time_t cur = time_local(); + odp_time_t wait = time_local_from_ns(ns); + odp_time_t end_time = time_sum(cur, wait); + + time_wait_until(end_time); +} + +void odp_time_wait_until(odp_time_t time) +{ + return time_wait_until(time); } uint64_t odp_time_to_u64(odp_time_t time)