#include "virfile.h"
#include "configmake.h"
#include "virstring.h"
+#include "virtime.h"
#define VIR_FROM_THIS VIR_FROM_STREAMS
bool abstract)
{
struct sockaddr_un sa;
- size_t i = 0;
- int timeout = 3;
+ virTimeBackOffVar timeout;
int ret;
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
goto error;
}
- do {
+ if (virTimeBackOffStart(&timeout, 1, 3*1000 /* ms */) < 0)
+ goto error;
+ while (virTimeBackOffWait(&timeout)) {
ret = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
if (ret == 0)
break;
}
goto error;
- } while ((++i <= timeout*5) && (usleep(.2 * 1000000) <= 0));
+ }
if (virFDStreamOpenInternal(st, fd, NULL, -1, 0) < 0)
goto error;
{
struct sockaddr_un addr;
int monfd;
- int timeout = 3; /* In seconds */
- int ret;
- size_t i = 0;
+ virTimeBackOffVar timeout;
+ int ret = -1;
*inProgress = false;
goto error;
}
- do {
+ if (virTimeBackOffStart(&timeout, 1, 3*1000 /* ms */) < 0)
+ goto error;
+ while (virTimeBackOffWait(&timeout)) {
ret = connect(monfd, (struct sockaddr *) &addr, sizeof(addr));
if (ret == 0)
_("failed to connect to monitor socket"));
goto error;
- } while ((++i <= timeout*5) && (usleep(.2 * 1000000) <= 0));
+ }
if (ret != 0) {
virReportSystemError(errno, "%s",
#include "virobject.h"
#include "virprobe.h"
#include "virstring.h"
+#include "virtime.h"
#ifdef WITH_DTRACE_PROBES
# include "libvirt_qemu_probes.h"
{
struct sockaddr_un addr;
int monfd;
- int timeout = 30; /* In seconds */
- int ret;
- size_t i = 0;
+ virTimeBackOffVar timeout;
+ int ret = -1;
if ((monfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
virReportSystemError(errno,
goto error;
}
- do {
+ if (virTimeBackOffStart(&timeout, 1, 30*1000 /* ms */) < 0)
+ goto error;
+ while (virTimeBackOffWait(&timeout)) {
ret = connect(monfd, (struct sockaddr *) &addr, sizeof(addr));
if (ret == 0)
_("failed to connect to monitor socket"));
goto error;
- } while ((++i <= timeout*5) && (usleep(.2 * 1000000) <= 0));
+ }
if (ret != 0) {
virReportSystemError(errno, "%s",
#include <config.h>
#include <stdio.h>
+#include <unistd.h>
#include <sys/time.h>
#include "virtime.h"
#include "viralloc.h"
#include "virerror.h"
+#include "virlog.h"
#define VIR_FROM_THIS VIR_FROM_NONE
+VIR_LOG_INIT("util.time");
+
/* We prefer clock_gettime if available because that is officially
* async signal safe according to POSIX. Many platforms lack it
* though, so fallback to gettimeofday everywhere else
*offset = current - utc;
return 0;
}
+
+/**
+ * virTimeBackOffStart:
+ * @var: Timeout variable (with type virTimeBackOffVar).
+ * @first: Initial time to wait (milliseconds).
+ * @timeout: Timeout (milliseconds).
+ *
+ * Initialize the timeout variable @var and start the timer running.
+ *
+ * Returns 0 on success, -1 on error and raises a libvirt error.
+ */
+int
+virTimeBackOffStart(virTimeBackOffVar *var,
+ unsigned long long first, unsigned long long timeout)
+{
+ if (virTimeMillisNow(&var->start_t) < 0)
+ return -1;
+
+ var->next = first;
+ var->limit_t = var->start_t + timeout;
+ return 0;
+}
+
+/**
+ * virTimeBackOffWait
+ * @var: Timeout variable (with type virTimeBackOffVar *).
+ *
+ * You must initialize @var first by calling the following function,
+ * which also starts the timer:
+ *
+ * if (virTimeBackOffStart(&var, first, timeout) < 0) {
+ * // handle errors
+ * }
+ *
+ * Then you use a while loop:
+ *
+ * while (virTimeBackOffWait(&var)) {
+ * //...
+ * }
+ *
+ * The 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).
+ */
+bool
+virTimeBackOffWait(virTimeBackOffVar *var)
+{
+ unsigned long long t, next;
+
+ ignore_value(virTimeMillisNowRaw(&t));
+
+ VIR_DEBUG("t=%llu, limit=%llu", t, var->limit_t);
+
+ if (t > var->limit_t)
+ return 0; /* ends the while loop */
+
+ next = var->next;
+ var->next *= 2;
+
+ /* If sleeping would take us beyond the limit, then shorten the
+ * sleep. This is so we always run the body just before the final
+ * timeout.
+ */
+ if (t + next > var->limit_t)
+ next = var->limit_t - t;
+
+ VIR_DEBUG("sleeping for %llu ms", next);
+
+ usleep(next * 1000);
+ return 1;
+}
int virTimeLocalOffsetFromUTC(long *offset)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
+typedef struct {
+ unsigned long long start_t;
+ unsigned long long next;
+ unsigned long long limit_t;
+} virTimeBackOffVar;
+
+int virTimeBackOffStart(virTimeBackOffVar *var,
+ unsigned long long first, unsigned long long timeout);
+
+bool virTimeBackOffWait(virTimeBackOffVar *var);
+
#endif