[libvirt] [PATCH 2/6] Use virTimeMs when appropriate

Jiri Denemark jdenemar at redhat.com
Tue Jun 7 13:01:56 UTC 2011


---
 src/qemu/qemu_domain.c    |   24 ++++++++----------------
 src/qemu/qemu_driver.c    |   11 ++---------
 src/qemu/qemu_migration.c |   31 ++++++++++---------------------
 src/util/event_poll.c     |   45 ++++++++++++++++-----------------------------
 4 files changed, 36 insertions(+), 75 deletions(-)

diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 332c09e..5f18ad3 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -45,8 +45,6 @@
 
 #define QEMU_NAMESPACE_HREF "http://libvirt.org/schemas/domain/qemu/1.0"
 
-#define timeval_to_ms(tv)       (((tv).tv_sec * 1000ull) + ((tv).tv_usec / 1000))
-
 
 static void qemuDomainEventDispatchFunc(virConnectPtr conn,
                                         virDomainEventPtr event,
@@ -492,15 +490,12 @@ void qemuDomainSetNamespaceHooks(virCapsPtr caps)
 int qemuDomainObjBeginJob(virDomainObjPtr obj)
 {
     qemuDomainObjPrivatePtr priv = obj->privateData;
-    struct timeval now;
+    unsigned long long now;
     unsigned long long then;
 
-    if (gettimeofday(&now, NULL) < 0) {
-        virReportSystemError(errno, "%s",
-                             _("cannot get time of day"));
+    if (virTimeMs(&now) < 0)
         return -1;
-    }
-    then = timeval_to_ms(now) + QEMU_JOB_WAIT_TIME;
+    then = now + QEMU_JOB_WAIT_TIME;
 
     virDomainObjRef(obj);
 
@@ -520,7 +515,7 @@ int qemuDomainObjBeginJob(virDomainObjPtr obj)
     priv->jobActive = QEMU_JOB_UNSPECIFIED;
     priv->jobSignals = 0;
     memset(&priv->jobSignalsData, 0, sizeof(priv->jobSignalsData));
-    priv->jobStart = timeval_to_ms(now);
+    priv->jobStart = now;
     memset(&priv->jobInfo, 0, sizeof(priv->jobInfo));
 
     return 0;
@@ -536,15 +531,12 @@ int qemuDomainObjBeginJobWithDriver(struct qemud_driver *driver,
                                     virDomainObjPtr obj)
 {
     qemuDomainObjPrivatePtr priv = obj->privateData;
-    struct timeval now;
+    unsigned long long now;
     unsigned long long then;
 
-    if (gettimeofday(&now, NULL) < 0) {
-        virReportSystemError(errno, "%s",
-                             _("cannot get time of day"));
+    if (virTimeMs(&now) < 0)
         return -1;
-    }
-    then = timeval_to_ms(now) + QEMU_JOB_WAIT_TIME;
+    then = now + QEMU_JOB_WAIT_TIME;
 
     virDomainObjRef(obj);
     qemuDriverUnlock(driver);
@@ -568,7 +560,7 @@ int qemuDomainObjBeginJobWithDriver(struct qemud_driver *driver,
     priv->jobActive = QEMU_JOB_UNSPECIFIED;
     priv->jobSignals = 0;
     memset(&priv->jobSignalsData, 0, sizeof(priv->jobSignalsData));
-    priv->jobStart = timeval_to_ms(now);
+    priv->jobStart = now;
     memset(&priv->jobInfo, 0, sizeof(priv->jobInfo));
 
     virDomainObjUnlock(obj);
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 2957467..470573e 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -113,8 +113,6 @@
 
 #define QEMU_NB_BLKIO_PARAM  1
 
-#define timeval_to_ms(tv)       (((tv).tv_sec * 1000ull) + ((tv).tv_usec / 1000))
-
 static void processWatchdogEvent(void *data, void *opaque);
 
 static int qemudShutdown(void);
@@ -6843,8 +6841,6 @@ static int qemuDomainGetJobInfo(virDomainPtr dom,
 
     if (virDomainObjIsActive(vm)) {
         if (priv->jobActive) {
-            struct timeval now;
-
             memcpy(info, &priv->jobInfo, sizeof(*info));
 
             /* Refresh elapsed time again just to ensure it
@@ -6852,12 +6848,9 @@ static int qemuDomainGetJobInfo(virDomainPtr dom,
              * of incoming migration which we don't currently
              * monitor actively in the background thread
              */
-            if (gettimeofday(&now, NULL) < 0) {
-                virReportSystemError(errno, "%s",
-                                     _("cannot get time of day"));
+            if (virTimeMs(&info->timeElapsed) < 0)
                 goto cleanup;
-            }
-            info->timeElapsed = timeval_to_ms(now) - priv->jobStart;
+            info->timeElapsed -= priv->jobStart;
         } else {
             memset(info, 0, sizeof(*info));
             info->type = VIR_DOMAIN_JOB_NONE;
diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
index f7eaa1c..5d7494b 100644
--- a/src/qemu/qemu_migration.c
+++ b/src/qemu/qemu_migration.c
@@ -46,8 +46,6 @@
 
 #define VIR_FROM_THIS VIR_FROM_QEMU
 
-#define timeval_to_ms(tv)       (((tv).tv_sec * 1000ull) + ((tv).tv_usec / 1000))
-
 enum qemuMigrationCookieFlags {
     QEMU_MIGRATION_COOKIE_FLAG_GRAPHICS,
     QEMU_MIGRATION_COOKIE_FLAG_LOCKSTATE,
@@ -831,7 +829,6 @@ qemuMigrationUpdateJobStatus(struct qemud_driver *driver,
     unsigned long long memProcessed;
     unsigned long long memRemaining;
     unsigned long long memTotal;
-    struct timeval now;
 
     if (!virDomainObjIsActive(vm)) {
         qemuReportError(VIR_ERR_INTERNAL_ERROR, _("%s: %s"),
@@ -852,13 +849,9 @@ qemuMigrationUpdateJobStatus(struct qemud_driver *driver,
         return -1;
     }
 
-    if (gettimeofday(&now, NULL) < 0) {
-        priv->jobInfo.type = VIR_DOMAIN_JOB_FAILED;
-        virReportSystemError(errno, "%s",
-                             _("cannot get time of day"));
+    if (virTimeMs(&priv->jobInfo.timeElapsed) < 0)
         return -1;
-    }
-    priv->jobInfo.timeElapsed = timeval_to_ms(now) - priv->jobStart;
+    priv->jobInfo.timeElapsed -= priv->jobStart;
 
     switch (status) {
     case QEMU_MONITOR_MIGRATION_STATUS_INACTIVE:
@@ -1069,18 +1062,16 @@ qemuMigrationPrepareTunnel(struct qemud_driver *driver,
     int internalret;
     int dataFD[2] = { -1, -1 };
     qemuDomainObjPrivatePtr priv = NULL;
-    struct timeval now;
+    unsigned long long now;
     qemuMigrationCookiePtr mig = NULL;
+
     VIR_DEBUG("driver=%p, dconn=%p, cookiein=%s, cookieinlen=%d, "
               "cookieout=%p, cookieoutlen=%p, st=%p, dname=%s, dom_xml=%s",
               driver, dconn, NULLSTR(cookiein), cookieinlen,
               cookieout, cookieoutlen, st, NULLSTR(dname), dom_xml);
 
-    if (gettimeofday(&now, NULL) < 0) {
-        virReportSystemError(errno, "%s",
-                             _("cannot get time of day"));
+    if (virTimeMs(&now) < 0)
         return -1;
-    }
 
     /* Parse the domain XML. */
     if (!(def = virDomainDefParseString(driver->caps, dom_xml,
@@ -1190,7 +1181,7 @@ endjob:
         virDomainObjIsActive(vm)) {
         priv->jobActive = QEMU_JOB_MIGRATION_IN;
         priv->jobInfo.type = VIR_DOMAIN_JOB_UNBOUNDED;
-        priv->jobStart = timeval_to_ms(now);
+        priv->jobStart = now;
     }
 
 cleanup:
@@ -1229,8 +1220,9 @@ qemuMigrationPrepareDirect(struct qemud_driver *driver,
     int ret = -1;
     int internalret;
     qemuDomainObjPrivatePtr priv = NULL;
-    struct timeval now;
+    unsigned long long now;
     qemuMigrationCookiePtr mig = NULL;
+
     VIR_DEBUG("driver=%p, dconn=%p, cookiein=%s, cookieinlen=%d, "
               "cookieout=%p, cookieoutlen=%p, uri_in=%s, uri_out=%p, "
               "dname=%s, dom_xml=%s",
@@ -1238,11 +1230,8 @@ qemuMigrationPrepareDirect(struct qemud_driver *driver,
               cookieout, cookieoutlen, NULLSTR(uri_in), uri_out,
               NULLSTR(dname), dom_xml);
 
-    if (gettimeofday(&now, NULL) < 0) {
-        virReportSystemError(errno, "%s",
-                             _("cannot get time of day"));
+    if (virTimeMs(&now) < 0)
         return -1;
-    }
 
     /* The URI passed in may be NULL or a string "tcp://somehostname:port".
      *
@@ -1413,7 +1402,7 @@ endjob:
         virDomainObjIsActive(vm)) {
         priv->jobActive = QEMU_JOB_MIGRATION_IN;
         priv->jobInfo.type = VIR_DOMAIN_JOB_UNBOUNDED;
-        priv->jobStart = timeval_to_ms(now);
+        priv->jobStart = now;
     }
 
 cleanup:
diff --git a/src/util/event_poll.c b/src/util/event_poll.c
index df2b28c..285ba50 100644
--- a/src/util/event_poll.c
+++ b/src/util/event_poll.c
@@ -201,11 +201,12 @@ int virEventPollRemoveHandle(int watch) {
 int virEventPollAddTimeout(int frequency,
                            virEventTimeoutCallback cb,
                            void *opaque,
-                           virFreeCallback ff) {
-    struct timeval now;
+                           virFreeCallback ff)
+{
+    unsigned long long now;
     int ret;
     EVENT_DEBUG("Adding timer %d with %d ms freq", nextTimer, frequency);
-    if (gettimeofday(&now, NULL) < 0) {
+    if (virTimeMs(&now) < 0) {
         return -1;
     }
 
@@ -227,9 +228,7 @@ int virEventPollAddTimeout(int frequency,
     eventLoop.timeouts[eventLoop.timeoutsCount].opaque = opaque;
     eventLoop.timeouts[eventLoop.timeoutsCount].deleted = 0;
     eventLoop.timeouts[eventLoop.timeoutsCount].expiresAt =
-        frequency >= 0 ? frequency +
-        (((unsigned long long)now.tv_sec)*1000) +
-        (((unsigned long long)now.tv_usec)/1000) : 0;
+        frequency >= 0 ? frequency + now : 0;
 
     eventLoop.timeoutsCount++;
     ret = nextTimer-1;
@@ -238,8 +237,9 @@ int virEventPollAddTimeout(int frequency,
     return ret;
 }
 
-void virEventPollUpdateTimeout(int timer, int frequency) {
-    struct timeval tv;
+void virEventPollUpdateTimeout(int timer, int frequency)
+{
+    unsigned long long now;
     int i;
     EVENT_DEBUG("Updating timer %d timeout with %d ms freq", timer, frequency);
 
@@ -248,7 +248,7 @@ void virEventPollUpdateTimeout(int timer, int frequency) {
         return;
     }
 
-    if (gettimeofday(&tv, NULL) < 0) {
+    if (virTimeMs(&now) < 0) {
         return;
     }
 
@@ -257,9 +257,7 @@ void virEventPollUpdateTimeout(int timer, int frequency) {
         if (eventLoop.timeouts[i].timer == timer) {
             eventLoop.timeouts[i].frequency = frequency;
             eventLoop.timeouts[i].expiresAt =
-                frequency >= 0 ? frequency +
-                (((unsigned long long)tv.tv_sec)*1000) +
-                (((unsigned long long)tv.tv_usec)/1000) : 0;
+                frequency >= 0 ? frequency + now : 0;
             virEventPollInterruptLocked();
             break;
         }
@@ -321,18 +319,12 @@ static int virEventPollCalculateTimeout(int *timeout) {
 
     /* Calculate how long we should wait for a timeout if needed */
     if (then > 0) {
-        struct timeval tv;
+        unsigned long long now;
 
-        if (gettimeofday(&tv, NULL) < 0) {
-            virReportSystemError(errno, "%s",
-                                 _("Unable to get current time"));
+        if (virTimeMs(&now) < 0)
             return -1;
-        }
-
-        *timeout = then -
-            ((((unsigned long long)tv.tv_sec)*1000) +
-             (((unsigned long long)tv.tv_usec)/1000));
 
+        *timeout = then - now;
         if (*timeout < 0)
             *timeout = 0;
     } else {
@@ -397,21 +389,16 @@ static struct pollfd *virEventPollMakePollFDs(int *nfds) {
  *
  * Returns 0 upon success, -1 if an error occurred
  */
-static int virEventPollDispatchTimeouts(void) {
-    struct timeval tv;
+static int virEventPollDispatchTimeouts(void)
+{
     unsigned long long now;
     int i;
     /* Save this now - it may be changed during dispatch */
     int ntimeouts = eventLoop.timeoutsCount;
     VIR_DEBUG("Dispatch %d", ntimeouts);
 
-    if (gettimeofday(&tv, NULL) < 0) {
-        virReportSystemError(errno, "%s",
-                             _("Unable to get current time"));
+    if (virTimeMs(&now) < 0)
         return -1;
-    }
-    now = (((unsigned long long)tv.tv_sec)*1000) +
-        (((unsigned long long)tv.tv_usec)/1000);
 
     for (i = 0 ; i < ntimeouts ; i++) {
         if (eventLoop.timeouts[i].deleted || eventLoop.timeouts[i].frequency < 0)
-- 
1.7.5.3




More information about the libvir-list mailing list