From patchwork Tue Feb 7 09:03:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wang X-Patchwork-Id: 93523 Delivered-To: patch@linaro.org Received: by 10.182.3.34 with SMTP id 2csp2139844obz; Tue, 7 Feb 2017 01:03:52 -0800 (PST) X-Received: by 10.237.34.125 with SMTP id o58mr12724365qtc.95.1486458232674; Tue, 07 Feb 2017 01:03:52 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id c75si2478561qkb.136.2017.02.07.01.03.52; Tue, 07 Feb 2017 01:03:52 -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 331C160DEE; Tue, 7 Feb 2017 09:03:52 +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 78ED560E96; Tue, 7 Feb 2017 09:03:24 +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 7D2FE608A8; Tue, 7 Feb 2017 09:03:21 +0000 (UTC) Received: from mail-pg0-f44.google.com (mail-pg0-f44.google.com [74.125.83.44]) by lists.linaro.org (Postfix) with ESMTPS id 3D9E2608A8 for ; Tue, 7 Feb 2017 09:03:20 +0000 (UTC) Received: by mail-pg0-f44.google.com with SMTP id 14so37174154pgg.1 for ; Tue, 07 Feb 2017 01:03:20 -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; bh=kQ3GL/jt6j92+1oBGJbkT2fQiU7oXKSszOgnHsewsBo=; b=CQJdU+6MB5tk4UJykYzgMSlUm7WVWV3ozx1jZ7cU+B9LrMI6bEY//yaeX+ljxlYkqf EzYSMDq+dQ7VVJ5f5QP0GSpxVV0Z3Miij2djHBrGkcDy6TrG5AqAyDT6rjLCHJ5nItRR SljvWwyaunq2UFfq1ew8872k5g+dgHckPGac3oDnSMKjRMpzdmVRMljRbGFJdGd3Noh7 i8BkZN3Y9AyqNYMbUWw7el35gumh9O0Z3TNUXaUgK+vchQjSt6bWqDrWXfFwFA2GEIhF mhqeggjpWhHKGvSXLap1pNJUphIc5bMDLzaZyQzAsE1VwTgLpsopCGXWUGD2AwTVdMpq P2/g== X-Gm-Message-State: AIkVDXIGScq7+rRHlfN1DYKDrpPT0oGZj4JysdBEiHkPcq9iGaQZxcmhSoLtppP/4QwOL/PUVk4= X-Received: by 10.98.74.202 with SMTP id c71mr18513321pfj.28.1486458199531; Tue, 07 Feb 2017 01:03:19 -0800 (PST) Received: from ubuntu.heyii.co (ubuntu.heyii.co. [45.32.66.203]) by smtp.googlemail.com with ESMTPSA id p15sm9070091pfk.58.2017.02.07.01.03.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 07 Feb 2017 01:03:19 -0800 (PST) From: Kevin Wang To: lng-odp@lists.linaro.org Date: Tue, 7 Feb 2017 09:03:16 +0000 Message-Id: <1486458196-31868-1-git-send-email-kevin.wang@linaro.org> X-Mailer: git-send-email 1.9.1 Subject: [lng-odp] [API-NEXT PATCHv3 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;