From patchwork Wed Feb 8 02:37:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wang X-Patchwork-Id: 93599 Delivered-To: patch@linaro.org Received: by 10.140.20.99 with SMTP id 90csp2485102qgi; Tue, 7 Feb 2017 18:38:43 -0800 (PST) X-Received: by 10.55.7.2 with SMTP id 2mr20688488qkh.228.1486521523312; Tue, 07 Feb 2017 18:38:43 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id w58si4491353qtc.245.2017.02.07.18.38.42; Tue, 07 Feb 2017 18:38:43 -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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 97C3662FE1; Wed, 8 Feb 2017 02:38:42 +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=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, 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 CD80360F53; Wed, 8 Feb 2017 02:38:14 +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 D839962DE5; Wed, 8 Feb 2017 02:38:07 +0000 (UTC) Received: from mail-pf0-f177.google.com (mail-pf0-f177.google.com [209.85.192.177]) by lists.linaro.org (Postfix) with ESMTPS id 80D3862FCE for ; Wed, 8 Feb 2017 02:37:16 +0000 (UTC) Received: by mail-pf0-f177.google.com with SMTP id f144so37945494pfa.2 for ; Tue, 07 Feb 2017 18:37:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=kQ3GL/jt6j92+1oBGJbkT2fQiU7oXKSszOgnHsewsBo=; b=tUl8WYsD21pe+SRKnvFQWbNDIODnhAVQN1ajnR0sazv5cxLbRy5S2SgKGCukWRppZ0 FW7fdhoU4FRw1L9M16xvF3BE4O54sbe/AWbunoLA7OgHRBivfmF/mPzoZ0a2qNolUPJM YL4G7W+pq+dYvf5rseMtHd3yzLBFyjhfqYaJNdja4qNK9ypAO5jD4qANbBv3h3DLc0F0 2CS2uEEvShGyeM3/DtkejMBBr8M+ofHj3C+rohaQt/pjiP0e8oa3N7/iCN+L9AQrn93+ 4w3TvBfM90YkcxVjch6VP+Qpayk3drgDGOBi/yH10G5e/O3Sj6Rbro32EWJfYrhWFYfp 2BNQ== X-Gm-Message-State: AIkVDXIaGLx2IduEB+FqvPgNq6JiCB2XOD5iqaR35aHj5ewT+Z5P1zZ9SiihFrcmC/bFcaRY2so= X-Received: by 10.84.141.1 with SMTP id 1mr30767879plu.33.1486521435653; Tue, 07 Feb 2017 18:37:15 -0800 (PST) Received: from ubuntu.heyii.co (ubuntu.heyii.co. [45.32.66.203]) by smtp.googlemail.com with ESMTPSA id i82sm14714084pfk.52.2017.02.07.18.37.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 07 Feb 2017 18:37:15 -0800 (PST) From: Kevin Wang To: lng-odp@lists.linaro.org Date: Wed, 8 Feb 2017 02:37:02 +0000 Message-Id: <1486521424-11172-3-git-send-email-kevin.wang@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1486521424-11172-1-git-send-email-kevin.wang@linaro.org> References: <1486521424-11172-1-git-send-email-kevin.wang@linaro.org> Subject: [lng-odp] [API-NEXT PATCHv4 2/4] linux-generic: timer: implement odp_timer_capability() 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: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" Implement a new internal function timer_res_init() to detect the max timer resolution without overrun at the ODP init stage. It will check timer resolution from 1ms to 100us, 10us...1ns until the timer is overrun. Signed-off-by: Kevin Wang --- platform/linux-generic/odp_timer.c | 95 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) -- 1.9.1 diff --git a/platform/linux-generic/odp_timer.c b/platform/linux-generic/odp_timer.c index 53fec08..9b884ea 100644 --- a/platform/linux-generic/odp_timer.c +++ b/platform/linux-generic/odp_timer.c @@ -70,6 +70,9 @@ static _odp_atomic_flag_t locks[NUM_LOCKS]; /* Multiple locks per cache line! */ #define IDX2LOCK(idx) (&locks[(idx) % NUM_LOCKS]) #endif +/* Timer resolution in nanoseconds */ +static uint64_t timer_res; + /****************************************************************************** * Translation between timeout buffer and timeout header *****************************************************************************/ @@ -188,6 +191,7 @@ typedef struct odp_timer_pool_s { #define MAX_TIMER_POOLS 255 /* Leave one for ODP_TIMER_INVALID */ #define INDEX_BITS 24 +#define TIMER_RES_TEST_LOOP_COUNT 10 static odp_atomic_u32_t num_timer_pools; static odp_timer_pool *timer_pool[MAX_TIMER_POOLS]; @@ -738,6 +742,81 @@ static void *timer_thread(void *arg) return NULL; } +/* Get the max timer resolution without overrun and fill in timer_res variable. + * + * Set timer's interval with candidate resolutions to get the max resolution + * that the timer would not be overrun. + * The candidate resolution value is from 1ms to 100us, 10us...1ns etc. + */ +static int timer_res_init(void) +{ + struct sigevent sigev; + timer_t timerid; + uint64_t res, sec, nsec; + struct itimerspec ispec; + sigset_t sigset; + siginfo_t si; + int loop_cnt; + struct timespec tmo; + + sigev.sigev_notify = SIGEV_THREAD_ID; + sigev._sigev_un._tid = (pid_t)syscall(SYS_gettid); + sigev.sigev_signo = SIGUSR1; + + /* Create timer */ + if (timer_create(CLOCK_MONOTONIC, &sigev, &timerid)) + ODP_ABORT("timer_create() returned error %s\n", + strerror(errno)); + + /* Timer resolution start from 1ms */ + res = ODP_TIME_MSEC_IN_NS; + /* Set initial value of timer_res */ + timer_res = res; + sigemptyset(&sigset); + /* Add SIGUSR1 to sigset */ + sigaddset(&sigset, SIGUSR1); + sigprocmask(SIG_BLOCK, &sigset, NULL); + + while (res > 0) { + /* Loop for 10 times to test the result */ + loop_cnt = TIMER_RES_TEST_LOOP_COUNT; + sec = res / ODP_TIME_SEC_IN_NS; + nsec = res - sec * ODP_TIME_SEC_IN_NS; + + memset(&ispec, 0, sizeof(ispec)); + ispec.it_interval.tv_sec = (time_t)sec; + ispec.it_interval.tv_nsec = (long)nsec; + ispec.it_value.tv_sec = (time_t)sec; + ispec.it_value.tv_nsec = (long)nsec; + + if (timer_settime(timerid, 0, &ispec, NULL)) + ODP_ABORT("timer_settime() returned error %s\n", + strerror(errno)); + /* Set signal wait timeout to 10*res */ + tmo.tv_sec = 0; + tmo.tv_nsec = res * 10; + while (loop_cnt--) { + if (sigtimedwait(&sigset, &si, &tmo) > 0) { + if (timer_getoverrun(timerid)) + /* overrun at this resolution */ + /* goto the end */ + goto timer_res_init_done; + } + } + /* Set timer_res */ + timer_res = res; + /* Test the next timer resolution candidate */ + res /= 10; + } +timer_res_init_done: + if (timer_delete(timerid) != 0) + ODP_ABORT("timer_delete() returned error %s\n", + strerror(errno)); + sigemptyset(&sigset); + sigprocmask(SIG_BLOCK, &sigset, NULL); + return 0; +} + static void itimer_init(odp_timer_pool *tp) { struct sigevent sigev; @@ -795,6 +874,20 @@ static void itimer_fini(odp_timer_pool *tp) * Some parameter checks and error messages * No modificatios of internal state *****************************************************************************/ +int odp_timer_capability(odp_timer_clk_src_t clk_src, + odp_timer_capability_t *capa) +{ + int ret = 0; + + if (clk_src == ODP_CLOCK_CPU) { + capa->res_ns = timer_res; + } else { + ODP_ERR("ODP timer system doesn't support external clock source currently\n"); + ret = -1; + } + return ret; +} + odp_timer_pool_t odp_timer_pool_create(const char *name, const odp_timer_pool_param_t *param) @@ -1003,6 +1096,8 @@ int odp_timer_init_global(void) #endif odp_atomic_init_u32(&num_timer_pools, 0); + timer_res_init(); + block_sigalarm(); return 0;