From patchwork Fri Nov 6 10:16:09 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Khoronzhuk X-Patchwork-Id: 56103 Delivered-To: patch@linaro.org Received: by 10.112.61.134 with SMTP id p6csp919045lbr; Fri, 6 Nov 2015 02:16:29 -0800 (PST) X-Received: by 10.140.20.213 with SMTP id 79mr12686197qgj.39.1446804988940; Fri, 06 Nov 2015 02:16:28 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id o85si8016346qki.46.2015.11.06.02.16.28; Fri, 06 Nov 2015 02:16:28 -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 084AF61CE1; Fri, 6 Nov 2015 10:16:28 +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 0F2A76198B; Fri, 6 Nov 2015 10:16:21 +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 C0E4561A1B; Fri, 6 Nov 2015 10:16:18 +0000 (UTC) Received: from mail-lb0-f169.google.com (mail-lb0-f169.google.com [209.85.217.169]) by lists.linaro.org (Postfix) with ESMTPS id 9483C61983 for ; Fri, 6 Nov 2015 10:16:17 +0000 (UTC) Received: by lbbes7 with SMTP id es7so54366626lbb.2 for ; Fri, 06 Nov 2015 02:16:16 -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=e6P6TaEmoWUcKzvPGmQZBrwkhG5iQhVI7JJ01cN6fBE=; b=JiPrA0ObZXEBiCEVFoG5h8SGMIQipC1JFvnzJIRClZuYRW3cIxlqWWx2BHZicacvsm a0YqgC5zWtJD/Nh6+EsFktUVuf8mz3M1EbfMNxNc28+slBzU9Bfp+PSdR/8XdgLkP1Zr STvA19wGJ3PGbFs1yjkHuy6I76jdDJKSgXoTGPlSCPyo6KMc2RNl6OrmRljjx9bklZbq BmfwwmBVXVvqgA42VswN+/6m8K3tMKqjnEujz4SJX/l6+viQVVhVJGDlyhA9EPz2YiMB ZUcDT8HzB452WgnOcOTI6AU5wIc9AkIKSabjwlpQEtVns2jcigRtbDqyzaSuE/BMH5sC 35nw== 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=e6P6TaEmoWUcKzvPGmQZBrwkhG5iQhVI7JJ01cN6fBE=; b=Uv6NncPVn9eoEq1JudZiADyxE4TnKSBs8R7/uPCZRakFobztYJO05JaLAqqvt6KUxs qSUTQZRSM/VtVhX6vWRSqPsAhIYIRAm4v5bdYhStOXTyO5tSGebWZDv2frQVpt0I1Fjk 1nJZcc+JWK0HkcS/bfEj/GTYVd15VJT97GJw+uOqk+vkm2XQUlv97vB/TU33hctMgiRy i075gXsKIRQUzNT/6UA0HA/TSDme72EgEgOXKlMSANvCZ83GHqV9q2RjEd5sdlVo/daZ FsoV7MdTc9DBef3eNVka/w9bFASADpmEoyy2igv1f2eVdA/OLAtX6FEm+f8TfuMEmbpe efPQ== X-Gm-Message-State: ALoCoQlW2b/OXfhp4Sa736YcgE2tFcby9kj+r7So8MliBa1NotsE+E08S7EXqMw6gBjGcNOnQwWV X-Received: by 10.112.235.65 with SMTP id uk1mr6430107lbc.118.1446804976430; Fri, 06 Nov 2015 02:16:16 -0800 (PST) Received: from localhost.localdomain ([195.238.92.128]) by smtp.gmail.com with ESMTPSA id x142sm1231301lfd.42.2015.11.06.02.16.15 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 06 Nov 2015 02:16:15 -0800 (PST) From: Ivan Khoronzhuk To: lng-odp@lists.linaro.org Date: Fri, 6 Nov 2015 12:16:09 +0200 Message-Id: <1446804969-24647-1-git-send-email-ivan.khoronzhuk@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: References: X-Topics: patch Subject: [lng-odp] [temp PATCH] 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" This will be looking like this patch. It allows to get rid of cpu_freq_max ... a little. But temp freq actually breaks CPU cycle API that emulates number of cpu cycles are base on cpu_hz_max....but any way it's not correct... The linux-generic time API implementation shouldn't depend on cpu cycle API wich is not stable enough to measure time period. Signed-off-by: Ivan Khoronzhuk --- platform/linux-generic/Makefile.am | 1 - platform/linux-generic/include/odp_cpu_internal.h | 29 ---------- platform/linux-generic/odp_cpu.c | 6 ++- platform/linux-generic/odp_time.c | 66 +++++++++++++++++------ 4 files changed, 54 insertions(+), 48 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 a6b6029..3a2603f 100644 --- a/platform/linux-generic/Makefile.am +++ b/platform/linux-generic/Makefile.am @@ -147,7 +147,6 @@ noinst_HEADERS = \ ${srcdir}/include/odp_schedule_internal.h \ ${srcdir}/include/odp_spin_internal.h \ ${srcdir}/include/odp_timer_internal.h \ - ${srcdir}/include/odp_cpu_internal.h \ ${srcdir}/Makefile.inc __LIB__libodp_la_SOURCES = \ 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..486996e 100644 --- a/platform/linux-generic/odp_time.c +++ b/platform/linux-generic/odp_time.c @@ -6,13 +6,10 @@ #define _POSIX_C_SOURCE 200809L +#include #include #include -#include -#include -#include - -#define GIGA 1000000000 +#include static inline uint64_t time_to_tick(odp_time_t time) @@ -28,34 +25,71 @@ odp_time_t tick_to_time(uint64_t tick) odp_time_t odp_time_local(void) { - return tick_to_time(odp_cpu_cycles()); + int ret; + struct timespec time, tres; + uint64_t sec, nsec, ticks, resolution; + + ret = clock_gettime(CLOCK_MONOTONIC_RAW, &time); + if (odp_unlikely(ret != 0)) + ODP_ABORT("clock_gettime failed\n"); + + sec = (uint64_t)time.tv_sec; + nsec = (uint64_t)time.tv_nsec; + + ret = clock_getres(CLOCK_MONOTONIC_RAW, &tres); + if (odp_unlikely(ret != 0)) + ODP_ABORT("clock_getres failed\n"); + + resolution = (uint64_t)tres.tv_nsec; + ticks = sec * ODP_TIME_SEC_IN_NS + nsec; + + return ticks / resolution; } 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 tick1 = time_to_tick(t1); + uint64_t tick2 = time_to_tick(t2); + + /* + * It's supposed that CLOCK_MONOTONIC_RAW never overflows, + * and t2 is got after t1 + */ + if (odp_unlikely(tick2 < tick1)) + ODP_ABORT("incorrect time order\n"); + + return tick_to_time(tick2 - tick1); } uint64_t odp_time_to_ns(odp_time_t time) { - uint64_t hz = odp_cpu_hz_max(); + int ret; + uint64_t resolution; + struct timespec tres; uint64_t tick = time_to_tick(time); - if (tick > (UINT64_MAX / GIGA)) - return (tick / hz) * GIGA; + ret = clock_getres(CLOCK_MONOTONIC_RAW, &tres); + if (odp_unlikely(ret != 0)) + ODP_ABORT("clock_getres failed\n"); - return (tick * GIGA) / hz; -} + resolution = (uint64_t)tres.tv_nsec; + return tick * resolution; +} odp_time_t odp_time_local_from_ns(uint64_t ns) { - uint64_t hz = odp_cpu_hz_max(); + 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"); - if (ns > (UINT64_MAX / hz)) - return tick_to_time((ns / GIGA) * hz); + resolution = (uint64_t)tres.tv_nsec; - return tick_to_time((ns * hz) / GIGA); + return tick_to_time(ns / resolution); } int odp_time_cmp(odp_time_t t2, odp_time_t t1)