Use virTimeMs when appropriate

This commit is contained in:
Jiri Denemark 2011-06-01 12:35:18 +02:00
parent ef6e99dc24
commit a231016b69
4 changed files with 36 additions and 78 deletions

View File

@ -45,8 +45,6 @@
#define QEMU_NAMESPACE_HREF "http://libvirt.org/schemas/domain/qemu/1.0" #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, static void qemuDomainEventDispatchFunc(virConnectPtr conn,
virDomainEventPtr event, virDomainEventPtr event,
@ -492,15 +490,12 @@ void qemuDomainSetNamespaceHooks(virCapsPtr caps)
int qemuDomainObjBeginJob(virDomainObjPtr obj) int qemuDomainObjBeginJob(virDomainObjPtr obj)
{ {
qemuDomainObjPrivatePtr priv = obj->privateData; qemuDomainObjPrivatePtr priv = obj->privateData;
struct timeval now; unsigned long long now;
unsigned long long then; unsigned long long then;
if (gettimeofday(&now, NULL) < 0) { if (virTimeMs(&now) < 0)
virReportSystemError(errno, "%s",
_("cannot get time of day"));
return -1; return -1;
} then = now + QEMU_JOB_WAIT_TIME;
then = timeval_to_ms(now) + QEMU_JOB_WAIT_TIME;
virDomainObjRef(obj); virDomainObjRef(obj);
@ -520,7 +515,7 @@ int qemuDomainObjBeginJob(virDomainObjPtr obj)
priv->jobActive = QEMU_JOB_UNSPECIFIED; priv->jobActive = QEMU_JOB_UNSPECIFIED;
priv->jobSignals = 0; priv->jobSignals = 0;
memset(&priv->jobSignalsData, 0, sizeof(priv->jobSignalsData)); memset(&priv->jobSignalsData, 0, sizeof(priv->jobSignalsData));
priv->jobStart = timeval_to_ms(now); priv->jobStart = now;
memset(&priv->jobInfo, 0, sizeof(priv->jobInfo)); memset(&priv->jobInfo, 0, sizeof(priv->jobInfo));
return 0; return 0;
@ -536,15 +531,12 @@ int qemuDomainObjBeginJobWithDriver(struct qemud_driver *driver,
virDomainObjPtr obj) virDomainObjPtr obj)
{ {
qemuDomainObjPrivatePtr priv = obj->privateData; qemuDomainObjPrivatePtr priv = obj->privateData;
struct timeval now; unsigned long long now;
unsigned long long then; unsigned long long then;
if (gettimeofday(&now, NULL) < 0) { if (virTimeMs(&now) < 0)
virReportSystemError(errno, "%s",
_("cannot get time of day"));
return -1; return -1;
} then = now + QEMU_JOB_WAIT_TIME;
then = timeval_to_ms(now) + QEMU_JOB_WAIT_TIME;
virDomainObjRef(obj); virDomainObjRef(obj);
qemuDriverUnlock(driver); qemuDriverUnlock(driver);
@ -568,7 +560,7 @@ int qemuDomainObjBeginJobWithDriver(struct qemud_driver *driver,
priv->jobActive = QEMU_JOB_UNSPECIFIED; priv->jobActive = QEMU_JOB_UNSPECIFIED;
priv->jobSignals = 0; priv->jobSignals = 0;
memset(&priv->jobSignalsData, 0, sizeof(priv->jobSignalsData)); memset(&priv->jobSignalsData, 0, sizeof(priv->jobSignalsData));
priv->jobStart = timeval_to_ms(now); priv->jobStart = now;
memset(&priv->jobInfo, 0, sizeof(priv->jobInfo)); memset(&priv->jobInfo, 0, sizeof(priv->jobInfo));
virDomainObjUnlock(obj); virDomainObjUnlock(obj);

View File

@ -113,8 +113,6 @@
#define QEMU_NB_BLKIO_PARAM 1 #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 void processWatchdogEvent(void *data, void *opaque);
static int qemudShutdown(void); static int qemudShutdown(void);
@ -6841,8 +6839,6 @@ static int qemuDomainGetJobInfo(virDomainPtr dom,
if (virDomainObjIsActive(vm)) { if (virDomainObjIsActive(vm)) {
if (priv->jobActive) { if (priv->jobActive) {
struct timeval now;
memcpy(info, &priv->jobInfo, sizeof(*info)); memcpy(info, &priv->jobInfo, sizeof(*info));
/* Refresh elapsed time again just to ensure it /* Refresh elapsed time again just to ensure it
@ -6850,12 +6846,9 @@ static int qemuDomainGetJobInfo(virDomainPtr dom,
* of incoming migration which we don't currently * of incoming migration which we don't currently
* monitor actively in the background thread * monitor actively in the background thread
*/ */
if (gettimeofday(&now, NULL) < 0) { if (virTimeMs(&info->timeElapsed) < 0)
virReportSystemError(errno, "%s",
_("cannot get time of day"));
goto cleanup; goto cleanup;
} info->timeElapsed -= priv->jobStart;
info->timeElapsed = timeval_to_ms(now) - priv->jobStart;
} else { } else {
memset(info, 0, sizeof(*info)); memset(info, 0, sizeof(*info));
info->type = VIR_DOMAIN_JOB_NONE; info->type = VIR_DOMAIN_JOB_NONE;

View File

@ -46,8 +46,6 @@
#define VIR_FROM_THIS VIR_FROM_QEMU #define VIR_FROM_THIS VIR_FROM_QEMU
#define timeval_to_ms(tv) (((tv).tv_sec * 1000ull) + ((tv).tv_usec / 1000))
enum qemuMigrationCookieFlags { enum qemuMigrationCookieFlags {
QEMU_MIGRATION_COOKIE_FLAG_GRAPHICS, QEMU_MIGRATION_COOKIE_FLAG_GRAPHICS,
QEMU_MIGRATION_COOKIE_FLAG_LOCKSTATE, QEMU_MIGRATION_COOKIE_FLAG_LOCKSTATE,
@ -831,7 +829,6 @@ qemuMigrationUpdateJobStatus(struct qemud_driver *driver,
unsigned long long memProcessed; unsigned long long memProcessed;
unsigned long long memRemaining; unsigned long long memRemaining;
unsigned long long memTotal; unsigned long long memTotal;
struct timeval now;
if (!virDomainObjIsActive(vm)) { if (!virDomainObjIsActive(vm)) {
qemuReportError(VIR_ERR_INTERNAL_ERROR, _("%s: %s"), qemuReportError(VIR_ERR_INTERNAL_ERROR, _("%s: %s"),
@ -847,18 +844,11 @@ qemuMigrationUpdateJobStatus(struct qemud_driver *driver,
&memTotal); &memTotal);
qemuDomainObjExitMonitorWithDriver(driver, vm); qemuDomainObjExitMonitorWithDriver(driver, vm);
if (ret < 0) { if (ret < 0 || virTimeMs(&priv->jobInfo.timeElapsed) < 0) {
priv->jobInfo.type = VIR_DOMAIN_JOB_FAILED; priv->jobInfo.type = VIR_DOMAIN_JOB_FAILED;
return -1; return -1;
} }
priv->jobInfo.timeElapsed -= priv->jobStart;
if (gettimeofday(&now, NULL) < 0) {
priv->jobInfo.type = VIR_DOMAIN_JOB_FAILED;
virReportSystemError(errno, "%s",
_("cannot get time of day"));
return -1;
}
priv->jobInfo.timeElapsed = timeval_to_ms(now) - priv->jobStart;
switch (status) { switch (status) {
case QEMU_MONITOR_MIGRATION_STATUS_INACTIVE: case QEMU_MONITOR_MIGRATION_STATUS_INACTIVE:
@ -1069,18 +1059,16 @@ qemuMigrationPrepareTunnel(struct qemud_driver *driver,
int internalret; int internalret;
int dataFD[2] = { -1, -1 }; int dataFD[2] = { -1, -1 };
qemuDomainObjPrivatePtr priv = NULL; qemuDomainObjPrivatePtr priv = NULL;
struct timeval now; unsigned long long now;
qemuMigrationCookiePtr mig = NULL; qemuMigrationCookiePtr mig = NULL;
VIR_DEBUG("driver=%p, dconn=%p, cookiein=%s, cookieinlen=%d, " VIR_DEBUG("driver=%p, dconn=%p, cookiein=%s, cookieinlen=%d, "
"cookieout=%p, cookieoutlen=%p, st=%p, dname=%s, dom_xml=%s", "cookieout=%p, cookieoutlen=%p, st=%p, dname=%s, dom_xml=%s",
driver, dconn, NULLSTR(cookiein), cookieinlen, driver, dconn, NULLSTR(cookiein), cookieinlen,
cookieout, cookieoutlen, st, NULLSTR(dname), dom_xml); cookieout, cookieoutlen, st, NULLSTR(dname), dom_xml);
if (gettimeofday(&now, NULL) < 0) { if (virTimeMs(&now) < 0)
virReportSystemError(errno, "%s",
_("cannot get time of day"));
return -1; return -1;
}
/* Parse the domain XML. */ /* Parse the domain XML. */
if (!(def = virDomainDefParseString(driver->caps, dom_xml, if (!(def = virDomainDefParseString(driver->caps, dom_xml,
@ -1190,7 +1178,7 @@ endjob:
virDomainObjIsActive(vm)) { virDomainObjIsActive(vm)) {
priv->jobActive = QEMU_JOB_MIGRATION_IN; priv->jobActive = QEMU_JOB_MIGRATION_IN;
priv->jobInfo.type = VIR_DOMAIN_JOB_UNBOUNDED; priv->jobInfo.type = VIR_DOMAIN_JOB_UNBOUNDED;
priv->jobStart = timeval_to_ms(now); priv->jobStart = now;
} }
cleanup: cleanup:
@ -1229,8 +1217,9 @@ qemuMigrationPrepareDirect(struct qemud_driver *driver,
int ret = -1; int ret = -1;
int internalret; int internalret;
qemuDomainObjPrivatePtr priv = NULL; qemuDomainObjPrivatePtr priv = NULL;
struct timeval now; unsigned long long now;
qemuMigrationCookiePtr mig = NULL; qemuMigrationCookiePtr mig = NULL;
VIR_DEBUG("driver=%p, dconn=%p, cookiein=%s, cookieinlen=%d, " VIR_DEBUG("driver=%p, dconn=%p, cookiein=%s, cookieinlen=%d, "
"cookieout=%p, cookieoutlen=%p, uri_in=%s, uri_out=%p, " "cookieout=%p, cookieoutlen=%p, uri_in=%s, uri_out=%p, "
"dname=%s, dom_xml=%s", "dname=%s, dom_xml=%s",
@ -1238,11 +1227,8 @@ qemuMigrationPrepareDirect(struct qemud_driver *driver,
cookieout, cookieoutlen, NULLSTR(uri_in), uri_out, cookieout, cookieoutlen, NULLSTR(uri_in), uri_out,
NULLSTR(dname), dom_xml); NULLSTR(dname), dom_xml);
if (gettimeofday(&now, NULL) < 0) { if (virTimeMs(&now) < 0)
virReportSystemError(errno, "%s",
_("cannot get time of day"));
return -1; return -1;
}
/* The URI passed in may be NULL or a string "tcp://somehostname:port". /* The URI passed in may be NULL or a string "tcp://somehostname:port".
* *
@ -1413,7 +1399,7 @@ endjob:
virDomainObjIsActive(vm)) { virDomainObjIsActive(vm)) {
priv->jobActive = QEMU_JOB_MIGRATION_IN; priv->jobActive = QEMU_JOB_MIGRATION_IN;
priv->jobInfo.type = VIR_DOMAIN_JOB_UNBOUNDED; priv->jobInfo.type = VIR_DOMAIN_JOB_UNBOUNDED;
priv->jobStart = timeval_to_ms(now); priv->jobStart = now;
} }
cleanup: cleanup:

View File

@ -201,11 +201,12 @@ int virEventPollRemoveHandle(int watch) {
int virEventPollAddTimeout(int frequency, int virEventPollAddTimeout(int frequency,
virEventTimeoutCallback cb, virEventTimeoutCallback cb,
void *opaque, void *opaque,
virFreeCallback ff) { virFreeCallback ff)
struct timeval now; {
unsigned long long now;
int ret; int ret;
EVENT_DEBUG("Adding timer %d with %d ms freq", nextTimer, frequency); EVENT_DEBUG("Adding timer %d with %d ms freq", nextTimer, frequency);
if (gettimeofday(&now, NULL) < 0) { if (virTimeMs(&now) < 0) {
return -1; return -1;
} }
@ -227,9 +228,7 @@ int virEventPollAddTimeout(int frequency,
eventLoop.timeouts[eventLoop.timeoutsCount].opaque = opaque; eventLoop.timeouts[eventLoop.timeoutsCount].opaque = opaque;
eventLoop.timeouts[eventLoop.timeoutsCount].deleted = 0; eventLoop.timeouts[eventLoop.timeoutsCount].deleted = 0;
eventLoop.timeouts[eventLoop.timeoutsCount].expiresAt = eventLoop.timeouts[eventLoop.timeoutsCount].expiresAt =
frequency >= 0 ? frequency + frequency >= 0 ? frequency + now : 0;
(((unsigned long long)now.tv_sec)*1000) +
(((unsigned long long)now.tv_usec)/1000) : 0;
eventLoop.timeoutsCount++; eventLoop.timeoutsCount++;
ret = nextTimer-1; ret = nextTimer-1;
@ -238,8 +237,9 @@ int virEventPollAddTimeout(int frequency,
return ret; return ret;
} }
void virEventPollUpdateTimeout(int timer, int frequency) { void virEventPollUpdateTimeout(int timer, int frequency)
struct timeval tv; {
unsigned long long now;
int i; int i;
EVENT_DEBUG("Updating timer %d timeout with %d ms freq", timer, frequency); EVENT_DEBUG("Updating timer %d timeout with %d ms freq", timer, frequency);
@ -248,7 +248,7 @@ void virEventPollUpdateTimeout(int timer, int frequency) {
return; return;
} }
if (gettimeofday(&tv, NULL) < 0) { if (virTimeMs(&now) < 0) {
return; return;
} }
@ -257,9 +257,7 @@ void virEventPollUpdateTimeout(int timer, int frequency) {
if (eventLoop.timeouts[i].timer == timer) { if (eventLoop.timeouts[i].timer == timer) {
eventLoop.timeouts[i].frequency = frequency; eventLoop.timeouts[i].frequency = frequency;
eventLoop.timeouts[i].expiresAt = eventLoop.timeouts[i].expiresAt =
frequency >= 0 ? frequency + frequency >= 0 ? frequency + now : 0;
(((unsigned long long)tv.tv_sec)*1000) +
(((unsigned long long)tv.tv_usec)/1000) : 0;
virEventPollInterruptLocked(); virEventPollInterruptLocked();
break; break;
} }
@ -321,18 +319,12 @@ static int virEventPollCalculateTimeout(int *timeout) {
/* Calculate how long we should wait for a timeout if needed */ /* Calculate how long we should wait for a timeout if needed */
if (then > 0) { if (then > 0) {
struct timeval tv; unsigned long long now;
if (gettimeofday(&tv, NULL) < 0) { if (virTimeMs(&now) < 0)
virReportSystemError(errno, "%s",
_("Unable to get current time"));
return -1; return -1;
}
*timeout = then -
((((unsigned long long)tv.tv_sec)*1000) +
(((unsigned long long)tv.tv_usec)/1000));
*timeout = then - now;
if (*timeout < 0) if (*timeout < 0)
*timeout = 0; *timeout = 0;
} else { } else {
@ -397,21 +389,16 @@ static struct pollfd *virEventPollMakePollFDs(int *nfds) {
* *
* Returns 0 upon success, -1 if an error occurred * Returns 0 upon success, -1 if an error occurred
*/ */
static int virEventPollDispatchTimeouts(void) { static int virEventPollDispatchTimeouts(void)
struct timeval tv; {
unsigned long long now; unsigned long long now;
int i; int i;
/* Save this now - it may be changed during dispatch */ /* Save this now - it may be changed during dispatch */
int ntimeouts = eventLoop.timeoutsCount; int ntimeouts = eventLoop.timeoutsCount;
VIR_DEBUG("Dispatch %d", ntimeouts); VIR_DEBUG("Dispatch %d", ntimeouts);
if (gettimeofday(&tv, NULL) < 0) { if (virTimeMs(&now) < 0)
virReportSystemError(errno, "%s",
_("Unable to get current time"));
return -1; return -1;
}
now = (((unsigned long long)tv.tv_sec)*1000) +
(((unsigned long long)tv.tv_usec)/1000);
for (i = 0 ; i < ntimeouts ; i++) { for (i = 0 ; i < ntimeouts ; i++) {
if (eventLoop.timeouts[i].deleted || eventLoop.timeouts[i].frequency < 0) if (eventLoop.timeouts[i].deleted || eventLoop.timeouts[i].frequency < 0)