From patchwork Fri Apr 13 11:19:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Wang X-Patchwork-Id: 897909 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 40MwL36h4Qz9rxx for ; Fri, 13 Apr 2018 21:20:11 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 9EB4B3E7812 for ; Fri, 13 Apr 2018 13:20:07 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-5.smtp.seeweb.it (in-5.smtp.seeweb.it [IPv6:2001:4b78:1:20::5]) by picard.linux.it (Postfix) with ESMTP id EEEF93E77FD for ; Fri, 13 Apr 2018 13:20:05 +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-5.smtp.seeweb.it (Postfix) with ESMTPS id F26466007AE for ; Fri, 13 Apr 2018 13:20:03 +0200 (CEST) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A5443406AE2C; Fri, 13 Apr 2018 11:20:01 +0000 (UTC) Received: from dhcp-12-102.nay.redhat.com (dhcp-12-102.nay.redhat.com [10.66.12.102]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0DDF52026DFD; Fri, 13 Apr 2018 11:19:59 +0000 (UTC) From: Li Wang To: ltp@lists.linux.it Date: Fri, 13 Apr 2018 19:19:56 +0800 Message-Id: <20180413111956.15837-1-liwang@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Fri, 13 Apr 2018 11:20:01 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Fri, 13 Apr 2018 11:20:01 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.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-5.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=-0.0 required=7.0 tests=SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-5.smtp.seeweb.it Subject: [LTP] [PATCH v2] 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 | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/include/tst_common.h b/include/tst_common.h index e4466d5..3c01e26 100644 --- a/include/tst_common.h +++ b/include/tst_common.h @@ -35,4 +35,30 @@ #define LTP_ALIGN(x, a) __LTP_ALIGN_MASK(x, (typeof(x))(a) - 1) #define __LTP_ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) +/* + * Exponential backoff usleep for function repeat + * @FUNC: the function which will be retried + * @ERET: an expected return value from the FUNC + */ +#define TST_RETRY_FUNC(FUNC, ERET) \ + TST_RETRY_FN_EXP_BACKOFF(FUNC, ERET, 1) + +#define TST_RETRY_FN_EXP_BACKOFF(FUNC, ERET, MAX_DELAY) \ +do { 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"); \ + } \ + } \ +} while(0) + #endif /* TST_COMMON_H__ */