[libvirt] [PATCH 0/3] Makes the code more compact

These three patches introduce two new functions and one macro respectively, which make the code clean and compact. Hu Tao (3): Add a macro timeval_to_ms to compute micro seconds from timeval Add a new function doStopCPUs Add a new function doStartCPUs src/qemu/qemu_driver.c | 151 ++++++++++++++++++------------------------------ 1 files changed, 57 insertions(+), 94 deletions(-) -- 1.7.3 -- Thanks, Hu Tao

--- src/qemu/qemu_driver.c | 24 ++++++++++-------------- 1 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index b9c5003..8b3546d 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -96,6 +96,8 @@ #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)) + /* Only 1 job is allowed at any time * A job includes *all* monitor commands, even those just querying * information, not merely actions */ @@ -376,8 +378,7 @@ static int qemuDomainObjBeginJob(virDomainObjPtr obj) _("cannot get time of day")); return -1; } - then = (now.tv_sec * 1000ull) + (now.tv_usec / 1000); - then += QEMU_JOB_WAIT_TIME; + then = timeval_to_ms(now) + QEMU_JOB_WAIT_TIME; virDomainObjRef(obj); @@ -396,7 +397,7 @@ static int qemuDomainObjBeginJob(virDomainObjPtr obj) priv->jobActive = QEMU_JOB_UNSPECIFIED; priv->jobSignals = 0; memset(&priv->jobSignalsData, 0, sizeof(priv->jobSignalsData)); - priv->jobStart = (now.tv_sec * 1000ull) + (now.tv_usec / 1000); + priv->jobStart = timeval_to_ms(now); memset(&priv->jobInfo, 0, sizeof(priv->jobInfo)); return 0; @@ -422,8 +423,7 @@ static int qemuDomainObjBeginJobWithDriver(struct qemud_driver *driver, _("cannot get time of day")); return -1; } - then = (now.tv_sec * 1000ull) + (now.tv_usec / 1000); - then += QEMU_JOB_WAIT_TIME; + then = timeval_to_ms(now) + QEMU_JOB_WAIT_TIME; virDomainObjRef(obj); qemuDriverUnlock(driver); @@ -444,7 +444,7 @@ static int qemuDomainObjBeginJobWithDriver(struct qemud_driver *driver, priv->jobActive = QEMU_JOB_UNSPECIFIED; priv->jobSignals = 0; memset(&priv->jobSignalsData, 0, sizeof(priv->jobSignalsData)); - priv->jobStart = (now.tv_sec * 1000ull) + (now.tv_usec / 1000); + priv->jobStart = timeval_to_ms(now); memset(&priv->jobInfo, 0, sizeof(priv->jobInfo)); virDomainObjUnlock(obj); @@ -5469,9 +5469,7 @@ qemuDomainWaitForMigrationComplete(struct qemud_driver *driver, virDomainObjPtr _("cannot get time of day")); goto cleanup; } - priv->jobInfo.timeElapsed = - ((now.tv_sec * 1000ull) + (now.tv_usec / 1000)) - - priv->jobStart; + priv->jobInfo.timeElapsed = timeval_to_ms(now) - priv->jobStart; switch (status) { case QEMU_MONITOR_MIGRATION_STATUS_INACTIVE: @@ -11154,7 +11152,7 @@ endjob: virDomainObjIsActive(vm)) { priv->jobActive = QEMU_JOB_MIGRATION_IN; priv->jobInfo.type = VIR_DOMAIN_JOB_UNBOUNDED; - priv->jobStart = (now.tv_sec * 1000ull) + (now.tv_usec / 1000); + priv->jobStart = timeval_to_ms(now); } cleanup: @@ -11382,7 +11380,7 @@ endjob: virDomainObjIsActive(vm)) { priv->jobActive = QEMU_JOB_MIGRATION_IN; priv->jobInfo.type = VIR_DOMAIN_JOB_UNBOUNDED; - priv->jobStart = (now.tv_sec * 1000ull) + (now.tv_usec / 1000); + priv->jobStart = timeval_to_ms(now); } cleanup: @@ -12371,9 +12369,7 @@ static int qemuDomainGetJobInfo(virDomainPtr dom, _("cannot get time of day")); goto cleanup; } - info->timeElapsed = - ((now.tv_sec * 1000ull) + (now.tv_usec / 1000)) - - priv->jobStart; + info->timeElapsed = timeval_to_ms(now) - priv->jobStart; } else { memset(info, 0, sizeof(*info)); info->type = VIR_DOMAIN_JOB_NONE; -- 1.7.3 -- Thanks, Hu Tao

On 12/14/2010 12:40 AM, Hu Tao wrote:
--- src/qemu/qemu_driver.c | 24 ++++++++++-------------- 1 files changed, 10 insertions(+), 14 deletions(-)
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index b9c5003..8b3546d 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -96,6 +96,8 @@
#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))
ACK; pure refactoring. -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org

--- src/qemu/qemu_driver.c | 64 +++++++++++++++++------------------------------ 1 files changed, 23 insertions(+), 41 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 8b3546d..484bdeb 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -579,6 +579,22 @@ static void qemuDomainObjExitRemoteWithDriver(struct qemud_driver *driver, virDomainObjUnref(obj); } +static int doStopCPUs(struct qemud_driver *driver, virDomainObjPtr vm) +{ + int ret; + int oldState = vm->state; + qemuDomainObjPrivatePtr priv = vm->privateData; + + vm->state = VIR_DOMAIN_PAUSED; + qemuDomainObjEnterMonitorWithDriver(driver, vm); + ret = qemuMonitorStopCPUs(priv->mon); + qemuDomainObjExitMonitorWithDriver(driver, vm); + if (ret < 0) { + vm->state = oldState; + } + return ret; +} + void qemuDomainDefNamespaceFree(void *nsdata) { qemuDomainCmdlineDefPtr cmd = nsdata; @@ -4968,15 +4984,7 @@ static int qemudDomainSuspend(virDomainPtr dom) { goto endjob; } if (vm->state != VIR_DOMAIN_PAUSED) { - int rc; - int state = vm->state; - - vm->state = VIR_DOMAIN_PAUSED; - qemuDomainObjEnterMonitorWithDriver(driver, vm); - rc = qemuMonitorStopCPUs(priv->mon); - qemuDomainObjExitMonitorWithDriver(driver, vm); - if (rc < 0) { - vm->state = state; + if (doStopCPUs(driver, vm) < 0) { goto endjob; } event = virDomainEventNewFromObj(vm, @@ -5350,15 +5358,9 @@ static int qemuDomainMigrateOffline(struct qemud_driver *driver, virDomainObjPtr vm) { - qemuDomainObjPrivatePtr priv = vm->privateData; - int state = vm->state; int ret; - vm->state = VIR_DOMAIN_PAUSED; - qemuDomainObjEnterMonitorWithDriver(driver, vm); - ret = qemuMonitorStopCPUs(priv->mon); - qemuDomainObjExitMonitorWithDriver(driver, vm); - + ret = doStopCPUs(driver, vm); if (ret == 0) { virDomainEventPtr event; @@ -5367,8 +5369,7 @@ qemuDomainMigrateOffline(struct qemud_driver *driver, VIR_DOMAIN_EVENT_SUSPENDED_MIGRATED); if (event) qemuDomainEventQueue(driver, event); - } else - vm->state = state; + } return ret; } @@ -5630,14 +5631,8 @@ static int qemudDomainSaveFlag(struct qemud_driver *driver, virDomainPtr dom, /* Pause */ if (vm->state == VIR_DOMAIN_RUNNING) { header.was_running = 1; - vm->state = VIR_DOMAIN_PAUSED; - qemuDomainObjEnterMonitorWithDriver(driver, vm); - if (qemuMonitorStopCPUs(priv->mon) < 0) { - qemuDomainObjExitMonitorWithDriver(driver, vm); - vm->state = VIR_DOMAIN_RUNNING; + if (doStopCPUs(driver, vm) < 0) goto endjob; - } - qemuDomainObjExitMonitorWithDriver(driver, vm); if (!virDomainObjIsActive(vm)) { qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s", @@ -6224,14 +6219,8 @@ static int qemudDomainCoreDump(virDomainPtr dom, /* Pause domain for non-live dump */ if (!(flags & VIR_DUMP_LIVE) && vm->state == VIR_DOMAIN_RUNNING) { - vm->state = VIR_DOMAIN_PAUSED; - qemuDomainObjEnterMonitorWithDriver(driver, vm); - if (qemuMonitorStopCPUs(priv->mon) < 0) { - qemuDomainObjExitMonitorWithDriver(driver, vm); - vm->state = VIR_DOMAIN_RUNNING; + if (doStopCPUs(driver, vm) < 0) goto endjob; - } - qemuDomainObjExitMonitorWithDriver(driver, vm); paused = 1; if (!virDomainObjIsActive(vm)) { @@ -12980,16 +12969,9 @@ static int qemuDomainRevertToSnapshot(virDomainSnapshotPtr snapshot, /* qemu unconditionally starts the domain running again after * loadvm, so let's pause it to keep consistency */ - int state = vm->state; - priv = vm->privateData; - vm->state = VIR_DOMAIN_PAUSED; - qemuDomainObjEnterMonitorWithDriver(driver, vm); - rc = qemuMonitorStopCPUs(priv->mon); - qemuDomainObjExitMonitorWithDriver(driver, vm); - if (rc < 0) { - vm->state = state; + rc = doStopCPUs(driver, vm); + if (rc < 0) goto endjob; - } } event = virDomainEventNewFromObj(vm, -- 1.7.3 -- Thanks, Hu Tao

On 12/14/2010 12:40 AM, Hu Tao wrote:
--- src/qemu/qemu_driver.c | 64 +++++++++++++++++------------------------------ 1 files changed, 23 insertions(+), 41 deletions(-)
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 8b3546d..484bdeb 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -579,6 +579,22 @@ static void qemuDomainObjExitRemoteWithDriver(struct qemud_driver *driver, virDomainObjUnref(obj); }
+static int doStopCPUs(struct qemud_driver *driver, virDomainObjPtr vm) +{ + int ret; + int oldState = vm->state; + qemuDomainObjPrivatePtr priv = vm->privateData; + + vm->state = VIR_DOMAIN_PAUSED; + qemuDomainObjEnterMonitorWithDriver(driver, vm); + ret = qemuMonitorStopCPUs(priv->mon); + qemuDomainObjExitMonitorWithDriver(driver, vm); + if (ret < 0) { + vm->state = oldState; + } + return ret; +}
ACK. -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org

--- src/qemu/qemu_driver.c | 63 ++++++++++++++++++----------------------------- 1 files changed, 24 insertions(+), 39 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 484bdeb..c096890 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -579,6 +579,21 @@ static void qemuDomainObjExitRemoteWithDriver(struct qemud_driver *driver, virDomainObjUnref(obj); } +static int doStartCPUs(struct qemud_driver *driver, virDomainObjPtr vm, virConnectPtr conn) +{ + int ret; + qemuDomainObjPrivatePtr priv = vm->privateData; + + qemuDomainObjEnterMonitorWithDriver(driver, vm); + ret = qemuMonitorStartCPUs(priv->mon, conn); + if (ret == 0) { + vm->state = VIR_DOMAIN_RUNNING; + } + qemuDomainObjExitMonitorWithDriver(driver, vm); + + return ret; +} + static int doStopCPUs(struct qemud_driver *driver, virDomainObjPtr vm) { int ret; @@ -4245,19 +4260,18 @@ static int qemudStartVMDaemon(virConnectPtr conn, qemuDomainObjExitMonitorWithDriver(driver, vm); goto cleanup; } + qemuDomainObjExitMonitorWithDriver(driver, vm); if (!start_paused) { DEBUG0("Starting domain CPUs"); /* Allow the CPUS to start executing */ - if (qemuMonitorStartCPUs(priv->mon, conn) < 0) { + if (doStartCPUs(driver, vm, conn) < 0) { if (virGetLastError() == NULL) qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("resume operation failed")); - qemuDomainObjExitMonitorWithDriver(driver, vm); goto cleanup; } } - qemuDomainObjExitMonitorWithDriver(driver, vm); DEBUG0("Writing domain status to disk"); @@ -5037,17 +5051,12 @@ static int qemudDomainResume(virDomainPtr dom) { goto endjob; } if (vm->state == VIR_DOMAIN_PAUSED) { - qemuDomainObjPrivatePtr priv = vm->privateData; - qemuDomainObjEnterMonitorWithDriver(driver, vm); - if (qemuMonitorStartCPUs(priv->mon, dom->conn) < 0) { - qemuDomainObjExitMonitorWithDriver(driver, vm); + if (doStartCPUs(driver, vm, dom->conn) < 0) { if (virGetLastError() == NULL) qemuReportError(VIR_ERR_OPERATION_FAILED, "%s", _("resume operation failed")); goto endjob; } - qemuDomainObjExitMonitorWithDriver(driver, vm); - vm->state = VIR_DOMAIN_RUNNING; event = virDomainEventNewFromObj(vm, VIR_DOMAIN_EVENT_RESUMED, VIR_DOMAIN_EVENT_RESUMED_UNPAUSED); @@ -5861,13 +5870,9 @@ endjob: if (vm) { if (ret != 0) { if (header.was_running && virDomainObjIsActive(vm)) { - qemuDomainObjEnterMonitorWithDriver(driver, vm); - rc = qemuMonitorStartCPUs(priv->mon, dom->conn); - qemuDomainObjExitMonitorWithDriver(driver, vm); + rc = doStartCPUs(driver, vm, dom->conn); if (rc < 0) VIR_WARN0("Unable to resume guest CPUs after save failure"); - else - vm->state = VIR_DOMAIN_RUNNING; } if (cgroup != NULL) { @@ -6249,14 +6254,11 @@ endjob: will support synchronous operations so we always get here after the migration is complete. */ else if (resume && paused && virDomainObjIsActive(vm)) { - qemuDomainObjEnterMonitorWithDriver(driver, vm); - if (qemuMonitorStartCPUs(priv->mon, dom->conn) < 0) { + if (doStartCPUs(driver, vm, dom->conn) < 0) { if (virGetLastError() == NULL) qemuReportError(VIR_ERR_OPERATION_FAILED, "%s", _("resuming after dump failed")); } - qemuDomainObjExitMonitorWithDriver(driver, vm); - vm->state = VIR_DOMAIN_RUNNING; } if (qemuDomainObjEndJob(vm) == 0) @@ -6288,8 +6290,6 @@ static void processWatchdogEvent(void *data, void *opaque) char *dumpfile; int i; - qemuDomainObjPrivatePtr priv = wdEvent->vm->privateData; - i = virAsprintf(&dumpfile, "%s/%s-%u", driver->autoDumpPath, wdEvent->vm->def->name, @@ -6315,9 +6315,7 @@ static void processWatchdogEvent(void *data, void *opaque) qemuReportError(VIR_ERR_OPERATION_FAILED, "%s", _("Dump failed")); - qemuDomainObjEnterMonitorWithDriver(driver, wdEvent->vm); - ret = qemuMonitorStartCPUs(priv->mon, NULL); - qemuDomainObjExitMonitorWithDriver(driver, wdEvent->vm); + ret = doStartCPUs(driver, wdEvent->vm, NULL); if (ret < 0) qemuReportError(VIR_ERR_OPERATION_FAILED, @@ -7172,17 +7170,12 @@ qemudDomainSaveImageStartVM(virConnectPtr conn, /* If it was running before, resume it now. */ if (header->was_running) { - qemuDomainObjPrivatePtr priv = vm->privateData; - qemuDomainObjEnterMonitorWithDriver(driver, vm); - if (qemuMonitorStartCPUs(priv->mon, conn) < 0) { + if (doStartCPUs(driver, vm, conn) < 0) { if (virGetLastError() == NULL) qemuReportError(VIR_ERR_OPERATION_FAILED, "%s", _("failed to resume domain")); - qemuDomainObjExitMonitorWithDriver(driver,vm); goto out; } - qemuDomainObjExitMonitorWithDriver(driver, vm); - vm->state = VIR_DOMAIN_RUNNING; if (virDomainSaveStatus(driver->caps, driver->stateDir, vm) < 0) { VIR_WARN("Failed to save status on vm %s", vm->def->name); goto out; @@ -11936,8 +11929,7 @@ qemudDomainMigratePerform (virDomainPtr dom, endjob: if (resume && vm->state == VIR_DOMAIN_PAUSED) { /* we got here through some sort of failure; start the domain again */ - qemuDomainObjEnterMonitorWithDriver(driver, vm); - if (qemuMonitorStartCPUs(priv->mon, dom->conn) < 0) { + if (doStartCPUs(driver, vm, dom->conn) < 0) { /* Hm, we already know we are in error here. We don't want to * overwrite the previous error, though, so we just throw something * to the logs and hope for the best @@ -11945,9 +11937,7 @@ endjob: VIR_ERROR(_("Failed to resume guest %s after failure"), vm->def->name); } - qemuDomainObjExitMonitorWithDriver(driver, vm); - vm->state = VIR_DOMAIN_RUNNING; event = virDomainEventNewFromObj(vm, VIR_DOMAIN_EVENT_RESUMED, VIR_DOMAIN_EVENT_RESUMED_MIGRATED); @@ -12103,17 +12093,12 @@ qemudDomainMigrateFinish2 (virConnectPtr dconn, * >= 0.10.6 to work properly. This isn't strictly necessary on * older qemu's, but it also doesn't hurt anything there */ - qemuDomainObjEnterMonitorWithDriver(driver, vm); - if (qemuMonitorStartCPUs(priv->mon, dconn) < 0) { + if (doStartCPUs(driver, vm, dconn) < 0) { if (virGetLastError() == NULL) qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("resume operation failed")); - qemuDomainObjExitMonitorWithDriver(driver, vm); goto endjob; } - qemuDomainObjExitMonitorWithDriver(driver, vm); - - vm->state = VIR_DOMAIN_RUNNING; } event = virDomainEventNewFromObj(vm, -- 1.7.3 -- Thanks, Hu Tao

On 12/14/2010 12:40 AM, Hu Tao wrote:
--- src/qemu/qemu_driver.c | 63 ++++++++++++++++++----------------------------- 1 files changed, 24 insertions(+), 39 deletions(-)
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 484bdeb..c096890 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -579,6 +579,21 @@ static void qemuDomainObjExitRemoteWithDriver(struct qemud_driver *driver, virDomainObjUnref(obj); }
+static int doStartCPUs(struct qemud_driver *driver, virDomainObjPtr vm, virConnectPtr conn) +{ + int ret; + qemuDomainObjPrivatePtr priv = vm->privateData; + + qemuDomainObjEnterMonitorWithDriver(driver, vm); + ret = qemuMonitorStartCPUs(priv->mon, conn); + if (ret == 0) { + vm->state = VIR_DOMAIN_RUNNING; + } + qemuDomainObjExitMonitorWithDriver(driver, vm); + + return ret; +}
ACK, and series pushed. -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org
participants (2)
-
Eric Blake
-
Hu Tao