From patchwork Fri Jul 3 12:38:37 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Khoronzhuk X-Patchwork-Id: 50630 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-la0-f72.google.com (mail-la0-f72.google.com [209.85.215.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id C1894214B3 for ; Fri, 3 Jul 2015 12:39:46 +0000 (UTC) Received: by lagx9 with SMTP id x9sf28220791lag.2 for ; Fri, 03 Jul 2015 05:39:45 -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=bAgVUpuYKHhKKb2z/H1L+UQLtt54uY28Z5VdpkNIouE=; b=Hn/vIQdeFeEivJA6gP9x/Ao3Hyda07leWT0mix747DHun/B+EpSyQb5TmrPBNjJH+w V1FEfTRScYF0uYUdJiDSWTVwdEGSEX6/8wvWq4ZmsdrsIfMYyxzY/wOcTXNJPel2UhmW +XbHClwDi5Zab0F13KWPJdwQJyxvCxdBS0QRzVVXb2MOO3CyM7a18ABakBpwZb1q/Ca4 NPR2EwRiBjgI/aB1Yxe1mj5EqqEHl74p4T5JKdjrDhDc6hvsKuNwabb03TJvNWH6xFen /nnWgM0zGj9vL43Sb0xJx79ymG5K247VMm5gMMZbnp7Qts9pT7WXl+fD8RuRsdqJRWIl Nl7A== X-Gm-Message-State: ALoCoQmiAVmE1ZXkaxQSUFTwtwAiZtHF5Krs0bJQgYk+mAfOOuLYHgRSzWHNXoragx682rTLA2fu X-Received: by 10.194.175.36 with SMTP id bx4mr22618834wjc.1.1435927185738; Fri, 03 Jul 2015 05:39:45 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.225.129 with SMTP id rk1ls493352lac.71.gmail; Fri, 03 Jul 2015 05:39:45 -0700 (PDT) X-Received: by 10.152.7.206 with SMTP id l14mr35492232laa.3.1435927185187; Fri, 03 Jul 2015 05:39:45 -0700 (PDT) Received: from mail-la0-f53.google.com (mail-la0-f53.google.com. [209.85.215.53]) by mx.google.com with ESMTPS id e3si7106483laa.104.2015.07.03.05.39.44 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Jul 2015 05:39:44 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.53 as permitted sender) client-ip=209.85.215.53; Received: by lagx9 with SMTP id x9so83760204lag.1 for ; Fri, 03 Jul 2015 05:39:44 -0700 (PDT) X-Received: by 10.152.6.69 with SMTP id y5mr35381463lay.72.1435927184854; Fri, 03 Jul 2015 05:39:44 -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 hn6csp1163116lbb; Fri, 3 Jul 2015 05:39:43 -0700 (PDT) X-Received: by 10.140.194.72 with SMTP id p69mr54011947qha.1.1435927183434; Fri, 03 Jul 2015 05:39:43 -0700 (PDT) Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id h107si10203863qgh.17.2015.07.03.05.39.42; Fri, 03 Jul 2015 05:39:43 -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 9C09961CDE; Fri, 3 Jul 2015 12:39:42 +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 36BC561835; Fri, 3 Jul 2015 12:39:15 +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 40BB961D86; Fri, 3 Jul 2015 12:39:11 +0000 (UTC) Received: from mail-la0-f47.google.com (mail-la0-f47.google.com [209.85.215.47]) by lists.linaro.org (Postfix) with ESMTPS id DC3E361FC3 for ; Fri, 3 Jul 2015 12:38:48 +0000 (UTC) Received: by lagx9 with SMTP id x9so83732621lag.1 for ; Fri, 03 Jul 2015 05:38:47 -0700 (PDT) X-Received: by 10.112.137.164 with SMTP id qj4mr35334118lbb.105.1435927127402; Fri, 03 Jul 2015 05:38:47 -0700 (PDT) Received: from khorivan.synapse.com ([195.238.92.128]) by mx.google.com with ESMTPSA id p9sm2303386laf.11.2015.07.03.05.38.45 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 03 Jul 2015 05:38:46 -0700 (PDT) From: Ivan Khoronzhuk To: lng-odp@lists.linaro.org Date: Fri, 3 Jul 2015 15:38:37 +0300 Message-Id: <1435927118-12397-2-git-send-email-ivan.khoronzhuk@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1435927118-12397-1-git-send-email-ivan.khoronzhuk@linaro.org> References: <1435927118-12397-1-git-send-email-ivan.khoronzhuk@linaro.org> X-Topics: timers patch Subject: [lng-odp] [Patch v2 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.53 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. After what some 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 a thread terminates a last timer it used. But a last timer saved in ttp for a 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 path must follow the next things: - An event can be in the following places: in a timer (waiting to be scheduled), in a queue for a thread to be scheduled, received in the main loop. - An event "holds" a timer, so when we receive an event we can delete it's timer. - a thread cannot delete timer w/o an event as it doesn't know who is owner of the event (and obvious the timer). - a thread shouldn't send events more than requested. - all threads have to be "held" in the loop till a 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 above, added several improvements: - don't send more timeouts that supposed to receive - free timer and tmo for a last received tmos = num of threads. - leave the main loop only if a 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);