[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 04/53] Replace qemu_gettimeofday() with g_get_real_time()
From: |
Paolo Bonzini |
Subject: |
[PULL 04/53] Replace qemu_gettimeofday() with g_get_real_time() |
Date: |
Tue, 19 Apr 2022 07:50:20 +0200 |
From: Marc-André Lureau <marcandre.lureau@redhat.com>
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>
---
blockdev.c | 8 ++++----
hw/rtc/m41t80.c | 6 +++---
hw/virtio/virtio-balloon.c | 9 +--------
qapi/qmp-event.c | 9 +++------
qemu-img.c | 8 ++++----
target/m68k/m68k-semi.c | 22 ++++++++++------------
target/nios2/nios2-semi.c | 23 ++++++++++-------------
7 files changed, 35 insertions(+), 50 deletions(-)
diff --git a/blockdev.c b/blockdev.c
index e46e831212..9230888e34 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -1258,7 +1258,7 @@ static void internal_snapshot_prepare(BlkActionState
*common,
BlockDriverState *bs;
QEMUSnapshotInfo old_sn, *sn;
bool ret;
- qemu_timeval tv;
+ int64_t rt;
BlockdevSnapshotInternal *internal;
InternalSnapshotState *state;
AioContext *aio_context;
@@ -1328,9 +1328,9 @@ static void internal_snapshot_prepare(BlkActionState
*common,
/* 3. take the snapshot */
sn = &state->sn;
pstrcpy(sn->name, sizeof(sn->name), name);
- qemu_gettimeofday(&tv);
- sn->date_sec = tv.tv_sec;
- sn->date_nsec = tv.tv_usec * 1000;
+ rt = g_get_real_time();
+ sn->date_sec = rt / G_USEC_PER_SEC;
+ sn->date_nsec = (rt % G_USEC_PER_SEC) * 1000;
sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
if (replay_mode != REPLAY_MODE_NONE) {
sn->icount = replay_get_current_icount();
diff --git a/hw/rtc/m41t80.c b/hw/rtc/m41t80.c
index a00971a67e..e045c864bb 100644
--- a/hw/rtc/m41t80.c
+++ b/hw/rtc/m41t80.c
@@ -47,7 +47,7 @@ static uint8_t m41t80_recv(I2CSlave *i2c)
{
M41t80State *s = M41T80(i2c);
struct tm now;
- qemu_timeval tv;
+ int64_t rt;
if (s->addr < 0) {
s->addr = 0;
@@ -57,8 +57,8 @@ static uint8_t m41t80_recv(I2CSlave *i2c)
}
switch (s->addr++) {
case 0:
- qemu_gettimeofday(&tv);
- return to_bcd(tv.tv_usec / 10000);
+ rt = g_get_real_time();
+ return to_bcd((rt % G_USEC_PER_SEC) / 10000);
case 1:
return to_bcd(now.tm_sec);
case 2:
diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c
index 163d244eb4..8f1b38ef5c 100644
--- a/hw/virtio/virtio-balloon.c
+++ b/hw/virtio/virtio-balloon.c
@@ -452,7 +452,6 @@ static void virtio_balloon_receive_stats(VirtIODevice
*vdev, VirtQueue *vq)
VirtQueueElement *elem;
VirtIOBalloonStat stat;
size_t offset = 0;
- qemu_timeval tv;
elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
if (!elem) {
@@ -484,13 +483,7 @@ static void virtio_balloon_receive_stats(VirtIODevice
*vdev, VirtQueue *vq)
s->stats[tag] = val;
}
s->stats_vq_offset = offset;
-
- if (qemu_gettimeofday(&tv) < 0) {
- warn_report("%s: failed to get time of day", __func__);
- goto out;
- }
-
- s->stats_last_update = tv.tv_sec;
+ s->stats_last_update = g_get_real_time() / G_USEC_PER_SEC;
out:
if (balloon_stats_enabled(s)) {
diff --git a/qapi/qmp-event.c b/qapi/qmp-event.c
index 19d3cd0038..0fe0d0a5a6 100644
--- a/qapi/qmp-event.c
+++ b/qapi/qmp-event.c
@@ -20,15 +20,12 @@
static void timestamp_put(QDict *qdict)
{
- int err;
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 }",
- err < 0 ? -1LL : (long long)tv.tv_sec,
- err < 0 ? -1LL : (long long)tv.tv_usec);
+ (long long)rt / G_USEC_PER_SEC,
+ (long long)rt % G_USEC_PER_SEC);
qdict_put(qdict, "timestamp", ts);
}
diff --git a/qemu-img.c b/qemu-img.c
index 1caddfb23a..cf63db7655 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -3305,11 +3305,11 @@ static int img_snapshot(int argc, char **argv)
char *filename, *snapshot_name = NULL;
int c, ret = 0, bdrv_oflags;
int action = 0;
- qemu_timeval tv;
bool quiet = false;
Error *err = NULL;
bool image_opts = false;
bool force_share = false;
+ int64_t rt;
bdrv_oflags = BDRV_O_RDWR;
/* Parse commandline parameters */
@@ -3406,9 +3406,9 @@ static int img_snapshot(int argc, char **argv)
memset(&sn, 0, sizeof(sn));
pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
- qemu_gettimeofday(&tv);
- sn.date_sec = tv.tv_sec;
- sn.date_nsec = tv.tv_usec * 1000;
+ rt = g_get_real_time();
+ sn.date_sec = rt / G_USEC_PER_SEC;
+ sn.date_nsec = (rt % G_USEC_PER_SEC) * 1000;
ret = bdrv_snapshot_create(bs, &sn);
if (ret) {
diff --git a/target/m68k/m68k-semi.c b/target/m68k/m68k-semi.c
index c5c164e096..37343d47e2 100644
--- a/target/m68k/m68k-semi.c
+++ b/target/m68k/m68k-semi.c
@@ -378,19 +378,17 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
arg0, arg1);
return;
} else {
- qemu_timeval tv;
struct gdb_timeval *p;
- result = qemu_gettimeofday(&tv);
- if (result == 0) {
- if (!(p = lock_user(VERIFY_WRITE,
- arg0, sizeof(struct gdb_timeval), 0))) {
- /* FIXME - check error code? */
- result = -1;
- } else {
- p->tv_sec = cpu_to_be32(tv.tv_sec);
- p->tv_usec = cpu_to_be64(tv.tv_usec);
- unlock_user(p, arg0, sizeof(struct gdb_timeval));
- }
+ int64_t rt = g_get_real_time();
+ p = lock_user(VERIFY_WRITE, arg0, sizeof(struct gdb_timeval), 0);
+ if (!p) {
+ /* FIXME - check error code? */
+ result = -1;
+ } else {
+ result = 0;
+ p->tv_sec = cpu_to_be32(rt / G_USEC_PER_SEC);
+ p->tv_usec = cpu_to_be64(rt % G_USEC_PER_SEC);
+ unlock_user(p, arg0, sizeof(struct gdb_timeval));
}
}
break;
diff --git a/target/nios2/nios2-semi.c b/target/nios2/nios2-semi.c
index 5a7ad0c710..3decf6924c 100644
--- a/target/nios2/nios2-semi.c
+++ b/target/nios2/nios2-semi.c
@@ -400,20 +400,17 @@ void do_nios2_semihosting(CPUNios2State *env)
arg0, 0);
return;
} else {
- qemu_timeval tv;
struct gdb_timeval *p;
- result = qemu_gettimeofday(&tv);
- if (result == 0) {
- p = lock_user(VERIFY_WRITE, arg0, sizeof(struct gdb_timeval),
- 0);
- if (!p) {
- result = -1;
- errno = EFAULT;
- } else {
- p->tv_sec = cpu_to_be32(tv.tv_sec);
- p->tv_usec = cpu_to_be64(tv.tv_usec);
- unlock_user(p, arg0, sizeof(struct gdb_timeval));
- }
+ int64_t rt = g_get_real_time();
+ p = lock_user(VERIFY_WRITE, arg0, sizeof(struct gdb_timeval), 0);
+ if (!p) {
+ result = -1;
+ errno = EFAULT;
+ } else {
+ result = 0;
+ p->tv_sec = cpu_to_be32(rt / G_USEC_PER_SEC);
+ p->tv_usec = cpu_to_be64(rt % G_USEC_PER_SEC);
+ unlock_user(p, arg0, sizeof(struct gdb_timeval));
}
}
break;
--
2.35.1
- [PULL 27/53] include: move cpu_exec* declarations to cpu-common.h, (continued)
- [PULL 27/53] include: move cpu_exec* declarations to cpu-common.h, Paolo Bonzini, 2022/04/19
- [PULL 37/53] build-sys: drop ntddscsi.h check, Paolo Bonzini, 2022/04/19
- [PULL 38/53] build-sys: simplify AF_VSOCK check, Paolo Bonzini, 2022/04/19
- [PULL 41/53] thread-posix: use monotonic clock for QemuCond and QemuSemaphore, Paolo Bonzini, 2022/04/19
- [PULL 40/53] thread-posix: remove the posix semaphore support, Paolo Bonzini, 2022/04/19
- [PULL 44/53] hyperv: SControl is optional to enable SynIc, Paolo Bonzini, 2022/04/19
- [PULL 52/53] target/i386: do not access beyond the low 128 bits of SSE registers, Paolo Bonzini, 2022/04/19
- [PULL 30/53] include: move qemu_get_vm_name() to sysemu.h, Paolo Bonzini, 2022/04/19
- [PULL 05/53] oslib: drop qemu_gettimeofday(), Paolo Bonzini, 2022/04/19
- [PULL 03/53] qga: replace qemu_gettimeofday() with g_get_real_time(), Paolo Bonzini, 2022/04/19
- [PULL 04/53] Replace qemu_gettimeofday() with g_get_real_time(),
Paolo Bonzini <=
- [PULL 13/53] osdep: poison {HOST,TARGET}_WORDS_BIGENDIAN, Paolo Bonzini, 2022/04/19
- [PULL 09/53] meson: remove test-qdev-global-props dependency on testqapi, Paolo Bonzini, 2022/04/19
- [PULL 10/53] char: move qemu_openpty_raw from util/ to char/, Paolo Bonzini, 2022/04/19
- [PULL 17/53] qga: replace deprecated g_get_current_time(), Paolo Bonzini, 2022/04/19
- [PULL 12/53] Replace TARGET_WORDS_BIGENDIAN, Paolo Bonzini, 2022/04/19
- [PULL 02/53] qtest: replace gettimeofday with GTimer, Paolo Bonzini, 2022/04/19
- [PULL 15/53] tests: replace free_all() usage with g_auto, Paolo Bonzini, 2022/04/19
- [PULL 31/53] include: move os_*() to os-foo.h, Paolo Bonzini, 2022/04/19
- [PULL 33/53] Move CPU softfloat unions to cpu-float.h, Paolo Bonzini, 2022/04/19
- [PULL 34/53] Move fcntl_setfl() to oslib-posix, Paolo Bonzini, 2022/04/19