From patchwork Fri Dec 4 17:51:29 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Khoronzhuk X-Patchwork-Id: 57715 Delivered-To: patch@linaro.org Received: by 10.112.155.196 with SMTP id vy4csp702104lbb; Fri, 4 Dec 2015 09:54:34 -0800 (PST) X-Received: by 10.55.82.193 with SMTP id g184mr20255475qkb.65.1449251674172; Fri, 04 Dec 2015 09:54:34 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id l130si15132598qhc.36.2015.12.04.09.54.33; Fri, 04 Dec 2015 09:54:34 -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.20150623.gappssmtp.com Received: by lists.linaro.org (Postfix, from userid 109) id CBE1661B1D; Fri, 4 Dec 2015 17:54:33 +0000 (UTC) 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 68F5A62D6C; Fri, 4 Dec 2015 17:52:34 +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 2585E62D74; Fri, 4 Dec 2015 17:52:29 +0000 (UTC) Received: from mail-lf0-f54.google.com (mail-lf0-f54.google.com [209.85.215.54]) by lists.linaro.org (Postfix) with ESMTPS id 5115562D6C for ; Fri, 4 Dec 2015 17:51:49 +0000 (UTC) Received: by lffu14 with SMTP id u14so117094066lff.1 for ; Fri, 04 Dec 2015 09:51:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=/ujnDSO3LIb5UR/r6QZIoghfTW5OsHFzPbokCgr6YT8=; b=Angr/w1uphua9Ug27h0ombctDwDMgDH0XVMCU3OkiPpmtuSjjRFIW7A5q1aaBEc+p8 kVb4Yg65mWEvUdQnOohYwqVhknFv+u2ngb7uP9aTZ2YDfx/Uu5jowo1VeP7Kt5j+qvYZ P1QPV0nVOmEn9DywVIsS/MiVa49klXi2u21ZiS/ub/z16vlO/evreV62t3vmDBxZvomC cHNeIh8vE+XkV4asx6sz1zlKjb9uyYDyLkvo7Aby5VycW/XfFdQighcrlNJMBigzpLdd 2eFhA1G+WChaHpY0/cd2MqPJ5wCOj51jcZW2tX44wyhW+JBUo9cY6AQWMFeQ+v0jdw9i TDew== 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=/ujnDSO3LIb5UR/r6QZIoghfTW5OsHFzPbokCgr6YT8=; b=QGiWrwmyiFxY/r3f8FMykG/DPCRQVoeW5HHne8ERzwRoV1V3txIJ44CnEsUdfIqG2e Sgs6V9mnP8hBgW0JCfBesQxQTNWtbRFIpeQLylqnvyf5rO6h56ph02qRufb+c4JMKyJv ursKdkyXgCO2dAJuAxaFvdPQ89r9L0LyzSRTZ0c0A7Old/yB458jzRq4r2SaoA0R1AZ/ Z/CvwcVBhOpZR2vdIA5Diz6WZhUToYbATtTQWT98Oj1DnK4pHYusZX8lJpt4Ig3TXs76 pcPKp/AMHZiwZFWp/fvqKjHPjh9FKDVTPPMzBlL+aXo0FNNRFXWeGB64kfUqZ/1m+X5w 8Jlg== X-Gm-Message-State: ALoCoQl49VaKuhcho3jU7iNTxNOioRSJjbciGMBXi+plPOS+grEvHS8wA+AOIXxykFrHyMg+Yn1b X-Received: by 10.25.218.9 with SMTP id r9mr8801649lfg.138.1449251507970; Fri, 04 Dec 2015 09:51:47 -0800 (PST) Received: from localhost.localdomain ([212.90.60.8]) by smtp.gmail.com with ESMTPSA id rm5sm2483518lbb.20.2015.12.04.09.51.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 04 Dec 2015 09:51:47 -0800 (PST) From: Ivan Khoronzhuk To: lng-odp@lists.linaro.org Date: Fri, 4 Dec 2015 19:51:29 +0200 Message-Id: <1449251494-23210-5-git-send-email-ivan.khoronzhuk@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1449251494-23210-1-git-send-email-ivan.khoronzhuk@linaro.org> References: <1449251494-23210-1-git-send-email-ivan.khoronzhuk@linaro.org> X-Topics: patch Subject: [lng-odp] [API-NEXT PATCH v7 4/9] linux-generic: odp_time: don't use cpu cycle API to get time 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" The linux-generic time API implementation shouldn't depend on cpu cycle API wich is not stable enough to measure time period due to dynamic frequency scaling. Reviewed-by: Petri Savolainen Signed-off-by: Ivan Khoronzhuk --- platform/linux-generic/Makefile.am | 1 - .../linux-generic/include/odp/plat/time_types.h | 6 +- platform/linux-generic/include/odp_cpu_internal.h | 29 ------ platform/linux-generic/odp_cpu.c | 6 +- platform/linux-generic/odp_time.c | 105 +++++++++++++-------- 5 files changed, 73 insertions(+), 74 deletions(-) delete mode 100644 platform/linux-generic/include/odp_cpu_internal.h diff --git a/platform/linux-generic/Makefile.am b/platform/linux-generic/Makefile.am index 102f11e..ba220c0 100644 --- a/platform/linux-generic/Makefile.am +++ b/platform/linux-generic/Makefile.am @@ -110,7 +110,6 @@ noinst_HEADERS = \ ${srcdir}/include/odp_timer_internal.h \ ${srcdir}/include/odp_timer_wheel_internal.h \ ${srcdir}/include/odp_traffic_mngr_internal.h \ - ${srcdir}/include/odp_cpu_internal.h \ ${srcdir}/Makefile.inc __LIB__libodp_la_SOURCES = \ diff --git a/platform/linux-generic/include/odp/plat/time_types.h b/platform/linux-generic/include/odp/plat/time_types.h index 9ba1508..e5765ec 100644 --- a/platform/linux-generic/include/odp/plat/time_types.h +++ b/platform/linux-generic/include/odp/plat/time_types.h @@ -21,9 +21,11 @@ extern "C" { * @{ **/ -typedef uint64_t odp_time_t; +typedef struct timespec odp_time_t; -#define ODP_TIME_NULL ((odp_time_t)0) +odp_time_t odp_time_null(void); + +#define ODP_TIME_NULL odp_time_null() /** * @} diff --git a/platform/linux-generic/include/odp_cpu_internal.h b/platform/linux-generic/include/odp_cpu_internal.h deleted file mode 100644 index 664e2df..0000000 --- a/platform/linux-generic/include/odp_cpu_internal.h +++ /dev/null @@ -1,29 +0,0 @@ -/* Copyright (c) 2015, Linaro Limited - * All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef ODP_CPU_INTERNAL_H_ -#define ODP_CPU_INTERNAL_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -static inline -uint64_t _odp_cpu_cycles_diff(uint64_t c2, uint64_t c1) -{ - if (odp_likely(c2 >= c1)) - return c2 - c1; - - return c2 + (odp_cpu_cycles_max() - c1) + 1; -} - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/platform/linux-generic/odp_cpu.c b/platform/linux-generic/odp_cpu.c index 636f811..4bd6a9a 100644 --- a/platform/linux-generic/odp_cpu.c +++ b/platform/linux-generic/odp_cpu.c @@ -6,9 +6,11 @@ #include #include -#include uint64_t odp_cpu_cycles_diff(uint64_t c2, uint64_t c1) { - return _odp_cpu_cycles_diff(c2, c1); + if (odp_likely(c2 >= c1)) + return c2 - c1; + + return c2 + (odp_cpu_cycles_max() - c1) + 1; } diff --git a/platform/linux-generic/odp_time.c b/platform/linux-generic/odp_time.c index a2e4046..89bbf0c 100644 --- a/platform/linux-generic/odp_time.c +++ b/platform/linux-generic/odp_time.c @@ -6,81 +6,106 @@ #define _POSIX_C_SOURCE 200809L +#include #include #include -#include -#include -#include +#include -#define GIGA 1000000000 - -static inline -uint64_t time_to_tick(odp_time_t time) +odp_time_t odp_time_local(void) { - return (uint64_t)time; -} + int ret; + struct timespec time; -static inline -odp_time_t tick_to_time(uint64_t tick) -{ - return (odp_time_t)tick; -} + ret = clock_gettime(CLOCK_MONOTONIC_RAW, &time); + if (odp_unlikely(ret != 0)) + ODP_ABORT("clock_gettime failed\n"); -odp_time_t odp_time_local(void) -{ - return tick_to_time(odp_cpu_cycles()); + return time; } odp_time_t odp_time_diff(odp_time_t t2, odp_time_t t1) { - return tick_to_time(_odp_cpu_cycles_diff(t2, t1)); + uint64_t ns1, ns2; + struct timespec time; + + ns1 = odp_time_to_ns(t1); + ns2 = odp_time_to_ns(t2); + if (ns2 < ns1) + return (struct timespec) {0, 1}; + + time.tv_sec = t2.tv_sec - t1.tv_sec; + time.tv_nsec = t2.tv_nsec - t1.tv_nsec; + + if (time.tv_nsec < 0) { + time.tv_nsec += ODP_TIME_SEC_IN_NS; + --time.tv_sec; + } + + return time; } uint64_t odp_time_to_ns(odp_time_t time) { - uint64_t hz = odp_cpu_hz_max(); - uint64_t tick = time_to_tick(time); + uint64_t ns; - if (tick > (UINT64_MAX / GIGA)) - return (tick / hz) * GIGA; + ns = time.tv_sec * ODP_TIME_SEC_IN_NS; + ns += time.tv_nsec; - return (tick * GIGA) / hz; + return ns; } - odp_time_t odp_time_local_from_ns(uint64_t ns) { - uint64_t hz = odp_cpu_hz_max(); + struct timespec time; - if (ns > (UINT64_MAX / hz)) - return tick_to_time((ns / GIGA) * hz); + time.tv_sec = ns / ODP_TIME_SEC_IN_NS; + time.tv_nsec = ns % ODP_TIME_SEC_IN_NS; - return tick_to_time((ns * hz) / GIGA); + return time; } int odp_time_cmp(odp_time_t t2, odp_time_t t1) { - uint64_t tick1 = time_to_tick(t1); - uint64_t tick2 = time_to_tick(t2); + if (t2.tv_sec < t1.tv_sec) + return -1; - if (tick1 < tick2) + if (t2.tv_sec > t1.tv_sec) return 1; - if (tick1 > tick2) - return -1; - - return 0; + return t2.tv_nsec - t1.tv_nsec; } odp_time_t odp_time_sum(odp_time_t t1, odp_time_t t2) { - uint64_t tick1 = time_to_tick(t1); - uint64_t tick2 = time_to_tick(t2); + struct timespec 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; +} + +uint64_t odp_time_to_u64(odp_time_t time) +{ + int ret; + struct timespec tres; + uint64_t resolution; + + ret = clock_getres(CLOCK_MONOTONIC_RAW, &tres); + if (odp_unlikely(ret != 0)) + ODP_ABORT("clock_getres failed\n"); + + resolution = (uint64_t)tres.tv_nsec; - return tick_to_time(tick1 + tick2); + return odp_time_to_ns(time) / resolution; } -uint64_t odp_time_to_u64(odp_time_t hdl) +odp_time_t odp_time_null(void) { - return time_to_tick(hdl); + return (struct timespec) {0, 0}; }