From patchwork Thu Jan 8 21:35:23 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ola Liljedahl X-Patchwork-Id: 42894 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-we0-f200.google.com (mail-we0-f200.google.com [74.125.82.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 740D02055F for ; Thu, 8 Jan 2015 21:35:46 +0000 (UTC) Received: by mail-we0-f200.google.com with SMTP id u56sf2502742wes.3 for ; Thu, 08 Jan 2015 13:35:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:subject:precedence:list-id:list-unsubscribe:list-archive :list-post:list-help:list-subscribe:mime-version:content-type :content-transfer-encoding:errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list; bh=2ezklm/doXz2bpWHBq2ISmuU/RpGTSxsbuEy6cpcKac=; b=m0LlFvRsSLxmXqhRsHJCBPCeNE1Q+Dj2BED4NfxhFTbSkQF5tsFivBw7B3Ge8QTOmf k7VsPC6dA+j5zVcVBeAZir+7F1OzB/5CWB8Es5/HUTi0N6Rw1NgS+pDlCzmnCunNRxgS HB9/zpzKW6WY4uz2wf2IqgmGxnbB35+8mv1FN2cvl2v7M9RIFcF3sRohKeGdI9TqgMkB wcNtRCLdZrh5qlxWqqpU7wfGjLosczfbX7OFJlcxAdC4CxfO/o7mpdge8HkOggvaLkqq 6SMLy9QUxGKuQkGCWdWTwWqPeZb71p1PTKneqPlJt7+UhudsIdHlr0IyNFLiGa5KnjLU KOtA== X-Gm-Message-State: ALoCoQlVlbFsGxMDxod9dcYnUv4PafqL0BE5Aokw2K0ucfOyjw+hEP8l0mrCQ1xl8GTGBM3eiREi X-Received: by 10.112.89.36 with SMTP id bl4mr1486420lbb.1.1420752945743; Thu, 08 Jan 2015 13:35:45 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.7.7 with SMTP id f7ls365472laa.100.gmail; Thu, 08 Jan 2015 13:35:45 -0800 (PST) X-Received: by 10.152.36.1 with SMTP id m1mr17637950laj.95.1420752945513; Thu, 08 Jan 2015 13:35:45 -0800 (PST) Received: from mail-la0-f43.google.com (mail-la0-f43.google.com. [209.85.215.43]) by mx.google.com with ESMTPS id i8si10852966lam.48.2015.01.08.13.35.45 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 08 Jan 2015 13:35:45 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.43 as permitted sender) client-ip=209.85.215.43; Received: by mail-la0-f43.google.com with SMTP id s18so11540549lam.2 for ; Thu, 08 Jan 2015 13:35:45 -0800 (PST) X-Received: by 10.112.131.1 with SMTP id oi1mr17520864lbb.2.1420752945390; Thu, 08 Jan 2015 13:35:45 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.9.200 with SMTP id c8csp136426lbb; Thu, 8 Jan 2015 13:35:43 -0800 (PST) X-Received: by 10.140.98.168 with SMTP id o37mr19060744qge.39.1420752942922; Thu, 08 Jan 2015 13:35:42 -0800 (PST) Received: from ip-10-35-177-41.ec2.internal (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTPS id j4si8804044qao.97.2015.01.08.13.35.41 (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 08 Jan 2015 13:35:42 -0800 (PST) Received-SPF: none (google.com: lng-odp-bounces@lists.linaro.org does not designate permitted sender hosts) client-ip=54.225.227.206; Received: from localhost ([127.0.0.1] helo=ip-10-35-177-41.ec2.internal) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1Y9Kjy-0001Ei-Kw; Thu, 08 Jan 2015 21:35:38 +0000 Received: from mail-la0-f51.google.com ([209.85.215.51]) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1Y9Kjh-0001DZ-Mh for lng-odp@lists.linaro.org; Thu, 08 Jan 2015 21:35:21 +0000 Received: by mail-la0-f51.google.com with SMTP id ms9so11512761lab.10 for ; Thu, 08 Jan 2015 13:35:16 -0800 (PST) X-Received: by 10.112.167.228 with SMTP id zr4mr17845230lbb.20.1420752915961; Thu, 08 Jan 2015 13:35:15 -0800 (PST) Received: from macmini.lan (78-82-118-111.tn.glocalnet.net. [78.82.118.111]) by mx.google.com with ESMTPSA id ss8sm1402002lbb.22.2015.01.08.13.35.14 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 08 Jan 2015 13:35:15 -0800 (PST) From: Ola Liljedahl To: lng-odp@lists.linaro.org Date: Thu, 8 Jan 2015 22:35:23 +0100 Message-Id: <1420752923-24198-4-git-send-email-ola.liljedahl@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1420752923-24198-1-git-send-email-ola.liljedahl@linaro.org> References: <1420752923-24198-1-git-send-email-ola.liljedahl@linaro.org> X-Topics: timers patch Subject: [lng-odp] [PATCHv4 3/3] test: odp_timer.h: cunit test X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: 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-bounces@lists.linaro.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: ola.liljedahl@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.43 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 Cunit test program test/validation/odp_timer.c for the updated timer API. Signed-off-by: Ola Liljedahl --- (This document/code contribution attached is provided under the terms of agreement LES-LTM-21309) Updated due to minor API changes. test/validation/.gitignore | 1 + test/validation/Makefile.am | 5 +- test/validation/odp_timer.c | 353 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 357 insertions(+), 2 deletions(-) create mode 100644 test/validation/odp_timer.c diff --git a/test/validation/.gitignore b/test/validation/.gitignore index d08db73..23b76ab 100644 --- a/test/validation/.gitignore +++ b/test/validation/.gitignore @@ -8,3 +8,4 @@ odp_shm odp_system odp_pktio odp_buffer +odp_timer diff --git a/test/validation/Makefile.am b/test/validation/Makefile.am index c0545b7..841ccbd 100644 --- a/test/validation/Makefile.am +++ b/test/validation/Makefile.am @@ -6,9 +6,9 @@ AM_LDFLAGS += -static TESTS_ENVIRONMENT = ODP_PLATFORM=${with_platform} if ODP_CUNIT_ENABLED -TESTS = odp_init odp_queue odp_crypto odp_shm odp_schedule odp_pktio_run odp_buffer odp_system +TESTS = odp_init odp_queue odp_crypto odp_shm odp_schedule odp_pktio_run odp_buffer odp_system odp_timer check_PROGRAMS = ${bin_PROGRAMS} -bin_PROGRAMS = odp_init odp_queue odp_crypto odp_shm odp_schedule odp_pktio odp_buffer odp_system +bin_PROGRAMS = odp_init odp_queue odp_crypto odp_shm odp_schedule odp_pktio odp_buffer odp_system odp_timer odp_crypto_CFLAGS = $(AM_CFLAGS) -I$(srcdir)/crypto odp_buffer_CFLAGS = $(AM_CFLAGS) -I$(srcdir)/buffer endif @@ -27,3 +27,4 @@ dist_odp_buffer_SOURCES = buffer/odp_buffer_pool_test.c \ buffer/odp_packet_test.c \ odp_buffer.c common/odp_cunit_common.c dist_odp_system_SOURCES = odp_system.c common/odp_cunit_common.c +dist_odp_timer_SOURCES = odp_timer.c common/odp_cunit_common.c diff --git a/test/validation/odp_timer.c b/test/validation/odp_timer.c new file mode 100644 index 0000000..21c4c36 --- /dev/null +++ b/test/validation/odp_timer.c @@ -0,0 +1,353 @@ +/* Copyright (c) 2014, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +/** + * @file + */ + +#include +#include +#include +#include +#include +#include +#include "odp_cunit_common.h" + +/** @private Timeout range in milliseconds (ms) */ +#define RANGE_MS 2000 + +/** @private Number of timers per thread */ +#define NTIMERS 2000 + +/** @private Barrier for thread synchronisation */ +static odp_barrier_t test_barrier; + +/** @private Timeout buffer pool handle used by all threads */ +static odp_buffer_pool_t tbp; + +/** @private Timer pool handle used by all threads */ +static odp_timer_pool_t tp; + +/** @private min() function */ +static int min(int a, int b) +{ + return a < b ? a : b; +} + +/* @private Timer helper structure */ +struct test_timer { + odp_timer_t tim; /* Timer handle */ + odp_buffer_t buf; /* Timeout buffer */ + odp_buffer_t buf2; /* Copy of buffer handle */ + uint64_t tick; /* Expiration tick or TICK_INVALID */ +}; + +#define TICK_INVALID (~(uint64_t)0) + +/* @private Handle a received (timeout) buffer */ +static void handle_tmo(odp_buffer_t buf, bool stale, uint64_t prev_tick) +{ + /* Use assert() for internal correctness checks of test program */ + assert(buf != ODP_BUFFER_INVALID); + if (odp_buffer_type(buf) != ODP_BUFFER_TYPE_TIMEOUT) { + /* Not a timeout buffer */ + CU_FAIL("Unexpected buffer type received"); + return; + } + /* Read the metadata from the timeout */ + odp_timeout_t tmo = odp_timeout_from_buf(buf); + odp_timer_t tim = odp_timeout_timer(tmo); + uint64_t tick = odp_timeout_tick(tmo); + struct test_timer *ttp = odp_timeout_user_ptr(tmo); + + if (tim == ODP_TIMER_INVALID) + CU_FAIL("odp_timeout_timer() invalid timer"); + if (ttp == NULL) + CU_FAIL("odp_timeout_user_ptr() null user ptr"); + + if (ttp->buf2 != buf) + CU_FAIL("odp_timeout_user_ptr() wrong user ptr"); + if (ttp->tim != tim) + CU_FAIL("odp_timeout_timer() wrong timer"); + if (stale) { + if (odp_timeout_fresh(tmo)) + CU_FAIL("Wrong status (fresh) for stale timeout"); + /* Stale timeout => local timer must have invalid tick */ + if (ttp->tick != TICK_INVALID) + CU_FAIL("Stale timeout for active timer"); + } else { + if (!odp_timeout_fresh(tmo)) + CU_FAIL("Wrong status (stale) for fresh timeout"); + /* Fresh timeout => local timer must have matching tick */ + if (ttp->tick != tick) { + printf("Wrong tick: expected %"PRIu64" actual %"PRIu64"\n", + ttp->tick, tick); + CU_FAIL("odp_timeout_tick() wrong tick"); + } + /* Check that timeout was delivered 'timely' */ + if (tick > odp_timer_current_tick(tp)) + CU_FAIL("Timeout delivered early"); + if (tick < prev_tick) { + printf("Too late tick: %"PRIu64" prev_tick %"PRIu64"\n", + tick, prev_tick); + CU_FAIL("Timeout delivered late"); + } + } + + /* Use assert() for correctness check of test program itself */ + assert(ttp->buf == ODP_BUFFER_INVALID); + ttp->buf = buf; +} + +/* @private Worker thread entrypoint which performs timer alloc/set/cancel/free + * tests */ +static void *worker_entrypoint(void *arg) +{ + int thr = odp_thread_id(); + uint32_t i; + unsigned seed = thr; + (void)arg; + + odp_queue_t queue = odp_queue_create("timer_queue", + ODP_QUEUE_TYPE_POLL, + NULL); + if (queue == ODP_QUEUE_INVALID) + CU_FAIL_FATAL("Queue create failed"); + + struct test_timer *tt = malloc(sizeof(struct test_timer) * NTIMERS); + if (tt == NULL) + perror("malloc"), abort(); + + /* Prepare all timers */ + for (i = 0; i < NTIMERS; i++) { + tt[i].tim = odp_timer_alloc(tp, queue, &tt[i]); + if (tt[i].tim == ODP_TIMER_INVALID) + CU_FAIL_FATAL("Failed to allocate timer"); + tt[i].buf = odp_buffer_alloc(tbp); + if (tt[i].buf == ODP_BUFFER_INVALID) + CU_FAIL_FATAL("Failed to allocate timeout buffer"); + tt[i].buf2 = tt[i].buf; + tt[i].tick = TICK_INVALID; + } + + odp_barrier_wait(&test_barrier); + + /* Initial set all timers with a random expiration time */ + uint32_t nset = 0; + for (i = 0; i < NTIMERS; i++) { + uint64_t tck = odp_timer_current_tick(tp) + 1 + + odp_timer_ns_to_tick(tp, + (rand_r(&seed) % RANGE_MS) + * 1000000ULL); + odp_timer_set_t rc; + rc = odp_timer_set_abs(tt[i].tim, tck, &tt[i].buf); + if (rc != ODP_TIMER_SUCCESS) { + CU_FAIL("Failed to set timer"); + } else { + tt[i].tick = tck; + nset++; + } + } + + /* Step through wall time, 1ms at a time and check for expired timers */ + uint32_t nrcv = 0; + uint32_t nreset = 0; + uint32_t ncancel = 0; + uint32_t ntoolate = 0; + uint32_t ms; + uint64_t prev_tick = odp_timer_current_tick(tp); + for (ms = 0; ms < 7 * RANGE_MS / 10; ms++) { + odp_buffer_t buf; + while ((buf = odp_queue_deq(queue)) != ODP_BUFFER_INVALID) { + /* Subtract one from prev_tick to allow for timeouts + * to be delivered a tick late */ + handle_tmo(buf, false, prev_tick - 1); + nrcv++; + } + prev_tick = odp_timer_current_tick(tp); + i = rand_r(&seed) % NTIMERS; + if (tt[i].buf == ODP_BUFFER_INVALID && + (rand_r(&seed) % 2 == 0)) { + /* Timer active, cancel it */ + int rc = odp_timer_cancel(tt[i].tim, &tt[i].buf); + if (rc != 0) + /* Cancel failed, timer already expired */ + ntoolate++; + tt[i].tick = TICK_INVALID; + ncancel++; + } else { + if (tt[i].buf != ODP_BUFFER_INVALID) + /* Timer inactive => set */ + nset++; + else + /* Timer active => reset */ + nreset++; + uint64_t tck = 1 + odp_timer_ns_to_tick(tp, + (rand_r(&seed) % RANGE_MS) * 1000000ULL); + odp_timer_set_t rc; + uint64_t cur_tick; + /* Loop until we manage to read cur_tick and set a + * relative timer in the same tick */ + do { + cur_tick = odp_timer_current_tick(tp); + rc = odp_timer_set_rel(tt[i].tim, + tck, &tt[i].buf); + } while (cur_tick != odp_timer_current_tick(tp)); + if (rc == ODP_TIMER_TOOEARLY || + rc == ODP_TIMER_TOOLATE) { + CU_FAIL("Failed to set timer (tooearly/toolate)"); + } else if (rc != ODP_TIMER_SUCCESS) { + /* Set/reset failed, timer already expired */ + ntoolate++; + } + /* Save expected expiration tick */ + tt[i].tick = cur_tick + tck; + } + if (usleep(1000/*1ms*/) < 0) + perror("usleep"), abort(); + } + + /* Cancel and free all timers */ + uint32_t nstale = 0; + for (i = 0; i < NTIMERS; i++) { + (void)odp_timer_cancel(tt[i].tim, &tt[i].buf); + tt[i].tick = TICK_INVALID; + if (tt[i].buf == ODP_BUFFER_INVALID) + /* Cancel too late, timer already expired and + * timoeut buffer enqueued */ + nstale++; + if (odp_timer_free(tt[i].tim) != ODP_BUFFER_INVALID) + CU_FAIL("odp_timer_free"); + } + + printf("Thread %u: %u timers set\n", thr, nset); + printf("Thread %u: %u timers reset\n", thr, nreset); + printf("Thread %u: %u timers cancelled\n", thr, ncancel); + printf("Thread %u: %u timers reset/cancelled too late\n", + thr, ntoolate); + printf("Thread %u: %u timeouts received\n", thr, nrcv); + printf("Thread %u: %u stale timeout(s) after odp_timer_free()\n", + thr, nstale); + + /* Delay some more to ensure timeouts for expired timers can be + * received */ + usleep(1000/*1ms*/); + while (nstale != 0) { + odp_buffer_t buf = odp_queue_deq(queue); + if (buf != ODP_BUFFER_INVALID) { + handle_tmo(buf, true, 0/*Dont' care for stale tmo's*/); + nstale--; + } else { + CU_FAIL("Failed to receive stale timeout"); + break; + } + } + /* Check if there any more (unexpected) buffers */ + odp_buffer_t buf = odp_queue_deq(queue); + if (buf != ODP_BUFFER_INVALID) + CU_FAIL("Unexpected buffer received"); + + printf("Thread %u: exiting\n", thr); + return NULL; +} + +/* @private Timer test case entrypoint */ +static void test_odp_timer_all(void) +{ + odp_buffer_pool_param_t params; + odp_timer_pool_param_t tparam; + int num_workers = min(odp_sys_cpu_count(), MAX_WORKERS); + + /* Create timeout buffer pools */ + params.buf_size = 0; + params.buf_align = ODP_CACHE_LINE_SIZE; + params.num_bufs = (NTIMERS + 1) * num_workers; + params.buf_type = ODP_BUFFER_TYPE_TIMEOUT; + tbp = odp_buffer_pool_create("tmo_pool", ODP_SHM_INVALID, ¶ms); + if (tbp == ODP_BUFFER_POOL_INVALID) + CU_FAIL_FATAL("Timeout buffer pool create failed"); + +#define NAME "timer_pool" +#define RES (10 * ODP_TIME_MSEC / 3) +#define MIN (10 * ODP_TIME_MSEC / 3) +#define MAX (1000000 * ODP_TIME_MSEC) + /* Create a timer pool */ + tparam.res_ns = RES; + tparam.min_tmo = MIN; + tparam.max_tmo = MAX; + tparam.num_timers = num_workers * NTIMERS; + tparam.private = 0; + tparam.clk_src = ODP_CLOCK_CPU; + tp = odp_timer_pool_create(NAME, &tparam); + if (tp == ODP_TIMER_POOL_INVALID) + CU_FAIL_FATAL("Timer pool create failed"); + + /* Start all created timer pools */ + odp_timer_pool_start(); + + odp_timer_pool_info_t tpinfo; + if (odp_timer_pool_info(tp, &tpinfo) != 0) + CU_FAIL("odp_timer_pool_info"); + CU_ASSERT(strcmp(tpinfo.name, NAME) == 0); + CU_ASSERT(tpinfo.param.res_ns == RES); + CU_ASSERT(tpinfo.param.min_tmo == MIN); + CU_ASSERT(tpinfo.param.max_tmo == MAX); + printf("Timer pool\n"); + printf("----------\n"); + printf(" name: %s\n", tpinfo.name); + printf(" resolution: %"PRIu64" ns (%"PRIu64" us)\n", + tpinfo.param.res_ns, tpinfo.param.res_ns / 1000); + printf(" min tmo: %"PRIu64" ns\n", tpinfo.param.min_tmo); + printf(" max tmo: %"PRIu64" ns\n", tpinfo.param.max_tmo); + printf("\n"); + + printf("#timers..: %u\n", NTIMERS); + printf("Tmo range: %u ms (%"PRIu64" ticks)\n", RANGE_MS, + odp_timer_ns_to_tick(tp, 1000000ULL * RANGE_MS)); + printf("\n"); + + uint64_t tick; + for (tick = 0; tick < 1000000000000ULL; tick += 1000000ULL) { + uint64_t ns = odp_timer_tick_to_ns(tp, tick); + uint64_t t2 = odp_timer_ns_to_tick(tp, ns); + if (tick != t2) + CU_FAIL("Invalid conversion tick->ns->tick"); + } + + /* Initialize barrier used by worker threads for synchronization */ + odp_barrier_init(&test_barrier, num_workers); + + /* Create and start worker threads */ + pthrd_arg thrdarg; + thrdarg.testcase = 0; + thrdarg.numthrds = num_workers; + odp_cunit_thread_create(worker_entrypoint, &thrdarg); + + /* Wait for worker threads to exit */ + odp_cunit_thread_exit(&thrdarg); + + /* Check some statistics after the test */ + if (odp_timer_pool_info(tp, &tpinfo) != 0) + CU_FAIL("odp_timer_pool_info"); + CU_ASSERT(tpinfo.param.num_timers == (unsigned)num_workers * NTIMERS); + CU_ASSERT(tpinfo.cur_timers == 0); + CU_ASSERT(tpinfo.hwm_timers == (unsigned)num_workers * NTIMERS); + + /* Destroy timer pool, all timers must have been freed */ + odp_timer_pool_destroy(tp); + + CU_PASS("ODP timer test"); +} + +CU_TestInfo test_odp_timer[] = { + {"test_odp_timer_all", test_odp_timer_all}, + CU_TEST_INFO_NULL, +}; + +CU_SuiteInfo odp_testsuites[] = { + {"Timer", NULL, NULL, NULL, NULL, test_odp_timer}, + CU_SUITE_INFO_NULL, +};