From patchwork Tue Jun 7 20:46:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Uvarov X-Patchwork-Id: 69578 Delivered-To: patch@linaro.org Received: by 10.140.106.246 with SMTP id e109csp2168176qgf; Tue, 7 Jun 2016 13:47:09 -0700 (PDT) X-Received: by 10.141.1.86 with SMTP id c83mr1572679qhd.12.1465332429206; Tue, 07 Jun 2016 13:47:09 -0700 (PDT) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id c8si15159297qgf.84.2016.06.07.13.47.08; Tue, 07 Jun 2016 13:47:09 -0700 (PDT) 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 dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id CEAB668042; Tue, 7 Jun 2016 20:47:08 +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.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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 351CA68018; Tue, 7 Jun 2016 20:47:03 +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 C7DA368038; Tue, 7 Jun 2016 20:47:01 +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 462A1617AC for ; Tue, 7 Jun 2016 20:47:00 +0000 (UTC) Received: by mail-lf0-f54.google.com with SMTP id u74so16033862lff.2 for ; Tue, 07 Jun 2016 13:47:00 -0700 (PDT) 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; bh=cI3q5CWHdlHxiWPY8koZAqXAhiJAGZ8+h9u+NUUgVDo=; b=BIt7hwVyrnp4EHfCcugp195zKkCaskIgakHi83lFSxO/MdMxA8C62bedz3OnDiixh2 2X/v510yHk3JtKEUmpzBxSXMyqiMm3gXdbMiLJhLuAowH8v4S/Ssbh+M/FR80VD/Q8N3 p8HmN9UzVGnnuSusZPS1U7kH7rsBIW13iuegNdXh7wnIKfhrYeZ11y6uY9FYjN7ZTdOV 9Ru3mTQwXw/W76cidkkPDpmNa5RHG6pWTlx5ecw7ButdxMhmsH0FLhGTlWu88ZQntCjO kGV/I9SCge0M3OHKSBxU8s8i1sk0+k7m8zWUR768E4jZbW8Wg4DxjNqj9agGskDa3Gj6 SZgw== X-Gm-Message-State: ALyK8tIb0fTsLOaobgodeEwZfJPe/RA64ePqX1qrb/iDfs8VDKIm9ghstVsSW3odIgu0xDdRDvA= X-Received: by 10.25.207.70 with SMTP id f67mr2862579lfg.62.1465332419139; Tue, 07 Jun 2016 13:46:59 -0700 (PDT) Received: from localhost.localdomain ([83.220.237.94]) by smtp.gmail.com with ESMTPSA id f6sm2540979lbt.31.2016.06.07.13.46.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 07 Jun 2016 13:46:58 -0700 (PDT) From: Maxim Uvarov To: lng-odp@lists.linaro.org Date: Tue, 7 Jun 2016 23:46:52 +0300 Message-Id: <1465332412-30587-1-git-send-email-maxim.uvarov@linaro.org> X-Mailer: git-send-email 2.7.1.250.gff4ea60 X-Topics: timers patch Subject: [lng-odp] [PATCH] example: timer simple 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" Example for simple timer usage. https://bugs.linaro.org/show_bug.cgi?id=2090 Signed-off-by: Maxim Uvarov --- example/timer/.gitignore | 1 + example/timer/Makefile.am | 12 ++- example/timer/odp_timer_simple.c | 167 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 3 deletions(-) create mode 100644 example/timer/odp_timer_simple.c diff --git a/example/timer/.gitignore b/example/timer/.gitignore index eb59265..f53a0df 100644 --- a/example/timer/.gitignore +++ b/example/timer/.gitignore @@ -1 +1,2 @@ odp_timer_test +odp_timer_simple diff --git a/example/timer/Makefile.am b/example/timer/Makefile.am index fcb67a9..1c733d3 100644 --- a/example/timer/Makefile.am +++ b/example/timer/Makefile.am @@ -1,10 +1,16 @@ include $(top_srcdir)/example/Makefile.inc -bin_PROGRAMS = odp_timer_test$(EXEEXT) +bin_PROGRAMS = odp_timer_test$(EXEEXT) \ + odp_timer_simple$(EXEEXT) odp_timer_test_LDFLAGS = $(AM_LDFLAGS) -static odp_timer_test_CFLAGS = $(AM_CFLAGS) -I${top_srcdir}/example +dist_odp_timer_test_SOURCES = odp_timer_test.c + +odp_timer_simple_LDFLAGS = $(AM_LDFLAGS) -static +odp_timer_simple_CFLAGS = $(AM_CFLAGS) -I${top_srcdir}/example +dist_odp_timer_simple_SOURCES = odp_timer_simple.c + +TESTS = odp_timer_simple noinst_HEADERS = \ $(top_srcdir)/example/example_debug.h - -dist_odp_timer_test_SOURCES = odp_timer_test.c diff --git a/example/timer/odp_timer_simple.c b/example/timer/odp_timer_simple.c new file mode 100644 index 0000000..d4917c3 --- /dev/null +++ b/example/timer/odp_timer_simple.c @@ -0,0 +1,167 @@ +/* Copyright (c) 2016, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +/** + * @file + * + * @example odp_timer_simple.c ODP simple example to schedule timer + * action for 1 second. + */ + +#include +#include +#include + +/* ODP main header */ +#include + +int main(int argc ODP_UNUSED, char *argv[] ODP_UNUSED) +{ + odp_instance_t instance; + odp_pool_param_t params; + odp_timer_pool_param_t tparams; + odp_pool_t tmo_pool; + odp_timer_pool_t timer_pool; + odp_queue_param_t qparam; + odp_queue_t queue; + odp_event_t ev = ODP_EVENT_INVALID; + odp_timer_t tim; + uint64_t sched_tmo; + int i, rc; + uint64_t period; + uint64_t tick; + odp_timeout_t tmo; + int ret = 0; + + /* + * Init ODP app + */ + if (odp_init_global(&instance, NULL, NULL)) + goto err_global; + + if (odp_init_local(instance, ODP_THREAD_CONTROL)) + goto err_local; + + /* + * Create pool for timeouts + */ + odp_pool_param_init(¶ms); + params.tmo.num = 10; + params.type = ODP_POOL_TIMEOUT; + + tmo_pool = odp_pool_create("msg_pool", ¶ms); + if (tmo_pool == ODP_POOL_INVALID) { + ret += 1; + goto err_tp; + } + + /* + * Create pool of timeouts + */ + tparams.res_ns = 10 * ODP_TIME_USEC_IN_NS; + tparams.min_tmo = 10 * ODP_TIME_USEC_IN_NS; + tparams.max_tmo = 1 * ODP_TIME_SEC_IN_NS; + tparams.num_timers = 1; /* One timer per worker */ + tparams.priv = 0; /* Shared */ + tparams.clk_src = ODP_CLOCK_CPU; + timer_pool = odp_timer_pool_create("timer_pool", &tparams); + if (timer_pool == ODP_TIMER_POOL_INVALID) { + ret += 1; + goto err; + } + + /* + * Create a queue for timer test + */ + odp_queue_param_init(&qparam); + qparam.type = ODP_QUEUE_TYPE_SCHED; + qparam.sched.prio = ODP_SCHED_PRIO_DEFAULT; + qparam.sched.sync = ODP_SCHED_SYNC_PARALLEL; + qparam.sched.group = ODP_SCHED_GROUP_ALL; + + queue = odp_queue_create("timer_queue", &qparam); + if (queue == ODP_QUEUE_INVALID) { + ret += 1; + goto err; + } + + tim = odp_timer_alloc(timer_pool, queue, NULL); + if (tim == ODP_TIMER_INVALID) { + EXAMPLE_ERR("Failed to allocate timer\n"); + ret += 1; + goto err; + } + + tmo = odp_timeout_alloc(tmo_pool); + if (tmo == ODP_TIMEOUT_INVALID) { + EXAMPLE_ERR("Failed to allocate timeout\n"); + return -1; + } + + ev = odp_timeout_to_event(tmo); + + /* Calculate period for timer in uint64_t value, in current case + * we will schedule timer for 1 second */ + period = odp_timer_ns_to_tick(timer_pool, 1 * ODP_TIME_SEC_IN_NS); + + /* Get current tick uint64_t value */ + tick = odp_timer_current_tick(timer_pool); + + rc = odp_timer_set_abs(tim, tick + period, &ev); + if (odp_unlikely(rc != ODP_TIMER_SUCCESS)) { + /* Too early or too late timeout requested */ + EXAMPLE_ABORT("odp_timer_set_abs() failed: %d\n", + rc); + } + + /* Wait time to return from odp_schedule() if there are no + * events + */ + sched_tmo = odp_schedule_wait_time(2 * ODP_TIME_SEC_IN_NS); + + for (i = 0; i < 5; i++) { + odp_time_t time; + + /* Program timeout action on current tick + period */ + tick = odp_timer_current_tick(timer_pool); + rc = odp_timer_set_abs(tim, tick + period, &ev); + /* Too early or too late timeout requested */ + if (odp_unlikely(rc != ODP_TIMER_SUCCESS)) + EXAMPLE_ABORT("odp_timer_set_abs() failed: %d\n", + rc); + + /* Wait for 2 seconds for timeout action to be generated */ + ev = odp_schedule(&queue, sched_tmo); + if (ev == ODP_EVENT_INVALID) + EXAMPLE_ABORT("Invalid event\n"); + if (odp_event_type(ev) != ODP_EVENT_TIMEOUT) + EXAMPLE_ABORT("Unexpected event type (%u) received\n", + odp_event_type(ev)); + + time = odp_time_global(); + printf("timer tick %d, time ns %" PRIu64 "\n", + i, odp_time_to_ns(time)); + + /* Do not free current event, just go back to loop and program + * timeout to next second. + */ + } + + /* Destroy created resources */ + odp_timer_cancel(tim, &ev); + odp_timer_free(tim); + odp_event_free(ev); + + ret += odp_queue_destroy(queue); +err: + odp_timer_pool_destroy(timer_pool); +err_tp: + ret += odp_pool_destroy(tmo_pool); + ret += odp_term_local(); +err_local: + ret += odp_term_global(instance); +err_global: + return ret; +}