Skip to contentSkip to conversation

include: add an exponential backoff macro for function repeat

PATCH Superseded Li Wang 2 participants 3 messages Apr 13, 2018 – Apr 17, 2018

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH v2 1/1 include: add an exponential backoff macro for function repeat

Cc Richard Palethorpe <[email protected]>
Cc Cyril Hrubis <[email protected]>
---
 include/tst_common.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
include/tst_common.h +26 -0
index e4466d5..3c01e26 100644
@@ -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__ */
Signature · 1 line
2.9.3
#1 lore raw

PATCH v2 1/1 include: add an exponential backoff macro for function repeat

Hello,

Quoted 12 lines · Li Wang
Cc Richard Palethorpe <[email protected]>
Cc Cyril Hrubis <[email protected]>
---
 include/tst_common.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
include/tst_common.h +0 -0
index e4466d5..3c01e26 100644

include/tst_common.h line 39

Quoted 5 lines
include/tst_common.h +2 -0
@@ -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

How about:

/**
* 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.
*/

include/tst_common.h line 51

Quoted 12 lines
include/tst_common.h +12 -0
+ * @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;					\

Maybe it should return ret somehow?

Quoted 13 lines
include/tst_common.h +12 -0
+		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__ */
Signature · 2 lines
Thank you,
Richard.
author
#2 lore raw

PATCH v2 1/1 include: add an exponential backoff macro for function repeat

Richard Palethorpe <[email protected]> wrote:

How about:

Quoted 17 lines

/**
* 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.
*/

​
I like this description.
​

Quoted 16 lines

include/tst_common.h line 51

Quoted 13 lines

+ * @FUNC: the function which will be retried
+ * @ERET: an expected return value from the FUNC
+ */

include/tst_common.h +9 -0
+#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;                                  \

Maybe it should return ret somehow?

​Maybe yes!

And, if we do that, the macro should probably be rewritten ​as:

​#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;                            \
})

​

Signature · 5 lines
Li Wang
[email protected]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20180417/29a46f38/attachment-0001.html>