From patchwork Fri Jul 3 12:00:38 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Khoronzhuk X-Patchwork-Id: 50629 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f70.google.com (mail-wg0-f70.google.com [74.125.82.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id DA8EA22916 for ; Fri, 3 Jul 2015 12:02:56 +0000 (UTC) Received: by wgjx7 with SMTP id x7sf29368548wgj.3 for ; Fri, 03 Jul 2015 05:02:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to: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=5r7ZTJ2wt2o2QarHsc7leZ/bFgbT43evn9AVKgKcyQE=; b=aUB1/Ei+JvWJn4csKjmcnbkUgCGLOBU+ZX8fCiG6naV88ElXWuQJtpMeJUaU3piPfz fADAs2SHP0XLNeX2mI+2yFkDBOFrMpdvtxkpkR5wKm23afIUTuztp4E1zHUVTYpFdLFh /NKu2X+PeZtXYobGlAkibVFRSLQWNXjGp+GWkWQYLATaRetjYcceY7YBJCAxgG6+QkIr iD93QFLZ/zaBWDivipRHgiHAs1ZeWHyrB8JP7UQECzzV1/lBq510vSrhEb/5jAvb62MB BFAd6zRH5Kt6IQu6KOWhPKM9ZcVibpL+vF033I6IkboNu/xMRCwB/tTAkIpIWL8FoLAG GqxQ== X-Gm-Message-State: ALoCoQnRCE9yJ3Ez9rOktiRGzqeSA0YEf2Rs9pY0Zyq2cRntRzk7/Z3pUO0/5AZgKFHXNKX2NVpJ X-Received: by 10.152.5.2 with SMTP id o2mr22546674lao.0.1435924975848; Fri, 03 Jul 2015 05:02:55 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.224.162 with SMTP id rd2ls451716lac.84.gmail; Fri, 03 Jul 2015 05:02:55 -0700 (PDT) X-Received: by 10.112.140.9 with SMTP id rc9mr35826604lbb.14.1435924975500; Fri, 03 Jul 2015 05:02:55 -0700 (PDT) Received: from mail-la0-f52.google.com (mail-la0-f52.google.com. [209.85.215.52]) by mx.google.com with ESMTPS id h3si7026900lbj.145.2015.07.03.05.02.55 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Jul 2015 05:02:55 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.52 as permitted sender) client-ip=209.85.215.52; Received: by lagx9 with SMTP id x9so82655932lag.1 for ; Fri, 03 Jul 2015 05:02:55 -0700 (PDT) X-Received: by 10.112.42.50 with SMTP id k18mr32602465lbl.76.1435924975285; Fri, 03 Jul 2015 05:02:55 -0700 (PDT) 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.108.230 with SMTP id hn6csp1138700lbb; Fri, 3 Jul 2015 05:02:54 -0700 (PDT) X-Received: by 10.140.41.134 with SMTP id z6mr49615500qgz.40.1435924973672; Fri, 03 Jul 2015 05:02:53 -0700 (PDT) Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id i195si10080460qhc.17.2015.07.03.05.02.52; Fri, 03 Jul 2015 05:02:53 -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; Received: by lists.linaro.org (Postfix, from userid 109) id C480E61FC3; Fri, 3 Jul 2015 12:02:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252.ec2.internal 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 ip-10-142-244-252.ec2.internal (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 5074D6204D; Fri, 3 Jul 2015 12:02:04 +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 7AB0F61B33; Fri, 3 Jul 2015 12:01:57 +0000 (UTC) Received: from mail-la0-f46.google.com (mail-la0-f46.google.com [209.85.215.46]) by lists.linaro.org (Postfix) with ESMTPS id 2E13E61B2F for ; Fri, 3 Jul 2015 12:00:51 +0000 (UTC) Received: by lagx9 with SMTP id x9so82589677lag.1 for ; Fri, 03 Jul 2015 05:00:49 -0700 (PDT) X-Received: by 10.112.10.166 with SMTP id j6mr32246856lbb.120.1435924849386; Fri, 03 Jul 2015 05:00:49 -0700 (PDT) Received: from khorivan.synapse.com ([195.238.92.128]) by mx.google.com with ESMTPSA id aj4sm2284300lbd.5.2015.07.03.05.00.47 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 03 Jul 2015 05:00:48 -0700 (PDT) From: Ivan Khoronzhuk To: lng-odp@lists.linaro.org Date: Fri, 3 Jul 2015 15:00:38 +0300 Message-Id: <1435924839-11521-2-git-send-email-ivan.khoronzhuk@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1435924839-11521-1-git-send-email-ivan.khoronzhuk@linaro.org> References: <1435924839-11521-1-git-send-email-ivan.khoronzhuk@linaro.org> X-Topics: timers patch Subject: [lng-odp] [Patch 1/2] example: timer: delete races while termination X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 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" X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: ivan.khoronzhuk@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.52 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 Current implementation has at least two races that lead to several issues: - gbls->remain can overflow. One thread can decrement remain counter to 0. While another can decrement it once again and it will be > 0 once again. After what the thread will loop very long time ... - Several threads can terminate the same timer and as result the same event. After out from the main loop the thread terminates the last timer it used. But the last timer saved in ttp for one thread can be received in another thread. So after leaving the main loop two threads can hold the same timer. - Some timer cannot be freed as several threads try to delete the same timer, as result one of the timer/tmo stay not freed after termination. - The test can send more events that requested. The receiving of requested number of tmos doesn't mean the test sent the same number. It rather sent more. This patch is intended to fix above drawbacks. The termination patch must follow the next things: - The event can be in the following places: in the timer (waiting to be scheduled), in the some queue for some thread to be scheduled, received in the main loop. - each event "holds" the timer, so when we receive event we can delete timer. - the thread cannot delete timer w/o event as it doesn't know who is owner of this event (and obvious the timer). - the thread shouldn't send events more than requested. - all threads have to be "held" in the loop till the last received event. The scheduler can assign event for any of the threads, so one thread can receive two last events for example. According to the above, added several improvements: - don't send more timeouts that supposed to receive - free timer and tmo for the last received tmos = num of threads. - leave the main loop only if the last tmo/timer is free. Signed-off-by: Ivan Khoronzhuk --- example/timer/odp_timer_test.c | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/example/timer/odp_timer_test.c b/example/timer/odp_timer_test.c index 5e4306e..584a581 100644 --- a/example/timer/odp_timer_test.c +++ b/example/timer/odp_timer_test.c @@ -47,6 +47,7 @@ typedef struct { odp_timer_pool_t tp; /**< Timer pool handle*/ odp_atomic_u32_t remain; /**< Number of timeouts to receive*/ struct test_timer tt[256]; /**< Array of all timer helper structs*/ + uint32_t num_workers; /**< Number of threads */ } test_globals_t; /** @private Timer set status ASCII strings */ @@ -139,16 +140,18 @@ static void test_abs_timeouts(int thr, test_globals_t *gbls) ttp->ev = odp_timeout_to_event(tmo); tick = odp_timer_current_tick(gbls->tp); - while ((int)odp_atomic_load_u32(&gbls->remain) > 0) { + while (1) { odp_event_t ev; odp_timer_set_t rc; - tick += period; - rc = odp_timer_set_abs(ttp->tim, tick, &ttp->ev); - if (odp_unlikely(rc != ODP_TIMER_SUCCESS)) { - /* Too early or too late timeout requested */ - EXAMPLE_ABORT("odp_timer_set_abs() failed: %s\n", - timerset2str(rc)); + if (ttp) { + tick += period; + rc = odp_timer_set_abs(ttp->tim, tick, &ttp->ev); + if (odp_unlikely(rc != ODP_TIMER_SUCCESS)) { + /* Too early or too late timeout requested */ + EXAMPLE_ABORT("odp_timer_set_abs() failed: %s\n", + timerset2str(rc)); + } } /* Get the next expired timeout. @@ -185,18 +188,17 @@ static void test_abs_timeouts(int thr, test_globals_t *gbls) } EXAMPLE_DBG(" [%i] timeout, tick %"PRIu64"\n", thr, tick); - odp_atomic_dec_u32(&gbls->remain); - } + uint32_t rx_num = odp_atomic_fetch_dec_u32(&gbls->remain); + if (!rx_num) + EXAMPLE_ABORT("Unexpected timeout received (timer %x, tick %"PRIu64")\n", + ttp->tim, tick); + else if (rx_num > gbls->num_workers) + continue; - /* Cancel and free last timer used */ - (void)odp_timer_cancel(ttp->tim, &ttp->ev); - if (ttp->ev != ODP_EVENT_INVALID) odp_timeout_free(odp_timeout_from_event(ttp->ev)); - else - EXAMPLE_ERR("Lost timeout event at timer cancel\n"); - /* Since we have cancelled the timer, there is no timeout event to - * return from odp_timer_free() */ - (void)odp_timer_free(ttp->tim); + odp_timer_free(ttp->tim); + ttp = NULL; + } /* Remove any prescheduled events */ remove_prescheduled_events(); @@ -483,6 +485,8 @@ int main(int argc, char *argv[]) printf("\n"); + gbls->num_workers = num_workers; + /* Initialize number of timeouts to receive */ odp_atomic_init_u32(&gbls->remain, gbls->args.tmo_count * num_workers);