From patchwork Sat Apr 21 02:15:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Wang X-Patchwork-Id: 902271 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40Sbzz5Z4wz9s1X for ; Sat, 21 Apr 2018 12:20:42 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id D007E3E6B2D for ; Sat, 21 Apr 2018 04:20:39 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-7.smtp.seeweb.it (in-7.smtp.seeweb.it [217.194.8.7]) by picard.linux.it (Postfix) with ESMTP id 10DFE3E6B0F for ; Sat, 21 Apr 2018 04:20:37 +0200 (CEST) Received: from mx1.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-7.smtp.seeweb.it (Postfix) with ESMTPS id 94FA1200746 for ; Sat, 21 Apr 2018 04:20:34 +0200 (CEST) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7A541A3EF0; Sat, 21 Apr 2018 02:20:32 +0000 (UTC) Received: from dhcp-12-102.nay.redhat.com (unknown [10.66.12.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id B750FAFD69; Sat, 21 Apr 2018 02:20:29 +0000 (UTC) From: Li Wang To: ltp@lists.linux.it Date: Sat, 21 Apr 2018 10:15:59 +0800 Message-Id: <20180421021600.17549-1-liwang@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Sat, 21 Apr 2018 02:20:32 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Sat, 21 Apr 2018 02:20:32 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'liwang@redhat.com' RCPT:'' X-Virus-Scanned: clamav-milter 0.99.2 at in-7.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=-0.0 required=7.0 tests=SPF_HELO_PASS,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-7.smtp.seeweb.it Subject: [LTP] [PATCH v3 1/2] include: add an exponential backoff macro for function repeat X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.18 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Signed-off-by: Li Wang Cc: Richard Palethorpe Cc: Cyril Hrubis --- include/tst_common.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/include/tst_common.h b/include/tst_common.h index e4466d5..32adb74 100644 --- a/include/tst_common.h +++ b/include/tst_common.h @@ -35,4 +35,37 @@ #define LTP_ALIGN(x, a) __LTP_ALIGN_MASK(x, (typeof(x))(a) - 1) #define __LTP_ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) +/** + * TST_RETRY_FUNC() - Repeatedly retry a function with an increasing delay. + * @FUNC - The function which will be retried + * @ERET - The value returned from @FUNC on success + * + * This macro will call @FUNC in a loop with a delay between retries. If @FUNC + * returns @ERET then the loop exits. The delay between retries starts at one + * micro second and is then doubled each iteration until it exceeds one second + * (the total time sleeping will be aproximately one second as well). When the + * delay exceeds one second tst_brk() is called. + */ +#define TST_RETRY_FUNC(FUNC, ERET) \ + TST_RETRY_FN_EXP_BACKOFF(FUNC, ERET, 1) + +#define TST_RETRY_FN_EXP_BACKOFF(FUNC, ERET, MAX_DELAY) \ +({ int tst_delay = 1; \ + for (;;) { \ + typeof(FUNC) ret = FUNC; \ + if (ret == ERET) \ + break; \ + if (tst_delay < MAX_DELAY * 1000000) { \ + tst_res(TINFO, \ + #FUNC" returned %i, retrying" \ + " in %ius", ret, tst_delay); \ + usleep(tst_delay); \ + tst_delay *= 2; \ + } else { \ + tst_brk(TBROK, #FUNC" failed"); \ + } \ + } \ + ERET; \ +}) + #endif /* TST_COMMON_H__ */