[libvirt] [PATCH v3] Add macro for handling exponential backoff loops.

Ján Tomko jtomko at redhat.com
Fri Apr 15 08:01:50 UTC 2016


On Sat, Apr 09, 2016 at 08:39:58PM +0100, Richard W.M. Jones wrote:
> In a few places in libvirt we busy-wait for events, for example qemu
> creating a monitor socket.  This is problematic because:
> 
>  - We need to choose a sufficiently small polling period so that
>    libvirt doesn't add unnecessary delays.
> 
>  - We need to choose a sufficiently large polling period so that
>    the effect of busy-waiting doesn't affect the system.
> 
> The solution to this conflict is to use an exponential backoff.
> 
> This patch adds a macro VIR_TIME_WHILE_WITH_BACKOFF to hide the
> details, and modifies a few places where we currently busy-wait.
> ---
>  src/fdstream.c           | 10 +++++----
>  src/libvirt_private.syms |  2 ++
>  src/qemu/qemu_agent.c    | 10 +++++----
>  src/qemu/qemu_monitor.c  | 10 +++++----
>  src/util/virtime.c       | 55 ++++++++++++++++++++++++++++++++++++++++++++++++
>  src/util/virtime.h       | 34 ++++++++++++++++++++++++++++++
>  6 files changed, 109 insertions(+), 12 deletions(-)
> 

> diff --git a/src/util/virtime.h b/src/util/virtime.h
> index 8ebad38..b0d9f89 100644
> --- a/src/util/virtime.h
> +++ b/src/util/virtime.h
> @@ -64,4 +64,38 @@ char *virTimeStringThen(unsigned long long when);
>  int virTimeLocalOffsetFromUTC(long *offset)
>      ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
>  
> +/**
> + * VIR_TIME_WHILE_WITH_BACKOFF:
> + * @var: Timeout variable (with type virTimeBackOffVar).
> + *
> + * You must initialize @var first by calling this function, which
> + * also starts the timer:
> + *
> + * virTimeBackOffStart(&var, first, timeout);
> + *
> + * This macro is a while loop that runs the body of the code
> + * repeatedly, with an exponential backoff.  It first waits for first
> + * milliseconds, then runs the body, then waits for 2*first ms, then
> + * runs the body again.  Then 4*first ms, and so on.
> + *
> + * When timeout milliseconds is reached, the while loop ends.
> + *
> + * The body should use "break" or "goto" when whatever condition it is
> + * testing for succeeds (or there is an unrecoverable error).
> + */
> +#define VIR_TIME_WHILE_WITH_BACKOFF(var)        \
> +    while (virTimeBackOffCondition(&(var)))

This still unnecessarily hides the while keyword.

ACK to the rest.

Jan




More information about the libvir-list mailing list