Replace qemu_gettimeofday() with g_get_real_time()

GLib g_get_real_time() is an alternative to gettimeofday() which allows
to simplify our code.

For semihosting, a few bits are lost on POSIX host, but this shouldn't
be a big concern.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Laurent Vivier <laurent@vivier.eu>
Message-Id: <20220307070401.171986-5-marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Marc-André Lureau 2022-03-07 11:04:00 +04:00 committed by Paolo Bonzini
parent 287698e50f
commit f793dde091
7 changed files with 35 additions and 50 deletions

View File

@ -1258,7 +1258,7 @@ static void internal_snapshot_prepare(BlkActionState *common,
BlockDriverState *bs; BlockDriverState *bs;
QEMUSnapshotInfo old_sn, *sn; QEMUSnapshotInfo old_sn, *sn;
bool ret; bool ret;
qemu_timeval tv; int64_t rt;
BlockdevSnapshotInternal *internal; BlockdevSnapshotInternal *internal;
InternalSnapshotState *state; InternalSnapshotState *state;
AioContext *aio_context; AioContext *aio_context;
@ -1328,9 +1328,9 @@ static void internal_snapshot_prepare(BlkActionState *common,
/* 3. take the snapshot */ /* 3. take the snapshot */
sn = &state->sn; sn = &state->sn;
pstrcpy(sn->name, sizeof(sn->name), name); pstrcpy(sn->name, sizeof(sn->name), name);
qemu_gettimeofday(&tv); rt = g_get_real_time();
sn->date_sec = tv.tv_sec; sn->date_sec = rt / G_USEC_PER_SEC;
sn->date_nsec = tv.tv_usec * 1000; sn->date_nsec = (rt % G_USEC_PER_SEC) * 1000;
sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
if (replay_mode != REPLAY_MODE_NONE) { if (replay_mode != REPLAY_MODE_NONE) {
sn->icount = replay_get_current_icount(); sn->icount = replay_get_current_icount();

View File

@ -47,7 +47,7 @@ static uint8_t m41t80_recv(I2CSlave *i2c)
{ {
M41t80State *s = M41T80(i2c); M41t80State *s = M41T80(i2c);
struct tm now; struct tm now;
qemu_timeval tv; int64_t rt;
if (s->addr < 0) { if (s->addr < 0) {
s->addr = 0; s->addr = 0;
@ -57,8 +57,8 @@ static uint8_t m41t80_recv(I2CSlave *i2c)
} }
switch (s->addr++) { switch (s->addr++) {
case 0: case 0:
qemu_gettimeofday(&tv); rt = g_get_real_time();
return to_bcd(tv.tv_usec / 10000); return to_bcd((rt % G_USEC_PER_SEC) / 10000);
case 1: case 1:
return to_bcd(now.tm_sec); return to_bcd(now.tm_sec);
case 2: case 2:

View File

@ -452,7 +452,6 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
VirtQueueElement *elem; VirtQueueElement *elem;
VirtIOBalloonStat stat; VirtIOBalloonStat stat;
size_t offset = 0; size_t offset = 0;
qemu_timeval tv;
elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
if (!elem) { if (!elem) {
@ -484,13 +483,7 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
s->stats[tag] = val; s->stats[tag] = val;
} }
s->stats_vq_offset = offset; s->stats_vq_offset = offset;
s->stats_last_update = g_get_real_time() / G_USEC_PER_SEC;
if (qemu_gettimeofday(&tv) < 0) {
warn_report("%s: failed to get time of day", __func__);
goto out;
}
s->stats_last_update = tv.tv_sec;
out: out:
if (balloon_stats_enabled(s)) { if (balloon_stats_enabled(s)) {

View File

@ -20,15 +20,12 @@
static void timestamp_put(QDict *qdict) static void timestamp_put(QDict *qdict)
{ {
int err;
QDict *ts; QDict *ts;
qemu_timeval tv; int64_t rt = g_get_real_time();
err = qemu_gettimeofday(&tv);
/* Put -1 to indicate failure of getting host time */
ts = qdict_from_jsonf_nofail("{ 'seconds': %lld, 'microseconds': %lld }", ts = qdict_from_jsonf_nofail("{ 'seconds': %lld, 'microseconds': %lld }",
err < 0 ? -1LL : (long long)tv.tv_sec, (long long)rt / G_USEC_PER_SEC,
err < 0 ? -1LL : (long long)tv.tv_usec); (long long)rt % G_USEC_PER_SEC);
qdict_put(qdict, "timestamp", ts); qdict_put(qdict, "timestamp", ts);
} }

View File

@ -3305,11 +3305,11 @@ static int img_snapshot(int argc, char **argv)
char *filename, *snapshot_name = NULL; char *filename, *snapshot_name = NULL;
int c, ret = 0, bdrv_oflags; int c, ret = 0, bdrv_oflags;
int action = 0; int action = 0;
qemu_timeval tv;
bool quiet = false; bool quiet = false;
Error *err = NULL; Error *err = NULL;
bool image_opts = false; bool image_opts = false;
bool force_share = false; bool force_share = false;
int64_t rt;
bdrv_oflags = BDRV_O_RDWR; bdrv_oflags = BDRV_O_RDWR;
/* Parse commandline parameters */ /* Parse commandline parameters */
@ -3406,9 +3406,9 @@ static int img_snapshot(int argc, char **argv)
memset(&sn, 0, sizeof(sn)); memset(&sn, 0, sizeof(sn));
pstrcpy(sn.name, sizeof(sn.name), snapshot_name); pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
qemu_gettimeofday(&tv); rt = g_get_real_time();
sn.date_sec = tv.tv_sec; sn.date_sec = rt / G_USEC_PER_SEC;
sn.date_nsec = tv.tv_usec * 1000; sn.date_nsec = (rt % G_USEC_PER_SEC) * 1000;
ret = bdrv_snapshot_create(bs, &sn); ret = bdrv_snapshot_create(bs, &sn);
if (ret) { if (ret) {

View File

@ -378,19 +378,17 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
arg0, arg1); arg0, arg1);
return; return;
} else { } else {
qemu_timeval tv;
struct gdb_timeval *p; struct gdb_timeval *p;
result = qemu_gettimeofday(&tv); int64_t rt = g_get_real_time();
if (result == 0) { p = lock_user(VERIFY_WRITE, arg0, sizeof(struct gdb_timeval), 0);
if (!(p = lock_user(VERIFY_WRITE, if (!p) {
arg0, sizeof(struct gdb_timeval), 0))) { /* FIXME - check error code? */
/* FIXME - check error code? */ result = -1;
result = -1; } else {
} else { result = 0;
p->tv_sec = cpu_to_be32(tv.tv_sec); p->tv_sec = cpu_to_be32(rt / G_USEC_PER_SEC);
p->tv_usec = cpu_to_be64(tv.tv_usec); p->tv_usec = cpu_to_be64(rt % G_USEC_PER_SEC);
unlock_user(p, arg0, sizeof(struct gdb_timeval)); unlock_user(p, arg0, sizeof(struct gdb_timeval));
}
} }
} }
break; break;

View File

@ -400,20 +400,17 @@ void do_nios2_semihosting(CPUNios2State *env)
arg0, 0); arg0, 0);
return; return;
} else { } else {
qemu_timeval tv;
struct gdb_timeval *p; struct gdb_timeval *p;
result = qemu_gettimeofday(&tv); int64_t rt = g_get_real_time();
if (result == 0) { p = lock_user(VERIFY_WRITE, arg0, sizeof(struct gdb_timeval), 0);
p = lock_user(VERIFY_WRITE, arg0, sizeof(struct gdb_timeval), if (!p) {
0); result = -1;
if (!p) { errno = EFAULT;
result = -1; } else {
errno = EFAULT; result = 0;
} else { p->tv_sec = cpu_to_be32(rt / G_USEC_PER_SEC);
p->tv_sec = cpu_to_be32(tv.tv_sec); p->tv_usec = cpu_to_be64(rt % G_USEC_PER_SEC);
p->tv_usec = cpu_to_be64(tv.tv_usec); unlock_user(p, arg0, sizeof(struct gdb_timeval));
unlock_user(p, arg0, sizeof(struct gdb_timeval));
}
} }
} }
break; break;