diff mbox series

[3/4] Replace qemu_gettimeofday() with g_get_real_time()

Message ID 20220304152704.3466036-4-marcandre.lureau@redhat.com
State Superseded
Headers show
Series RFC: remove qemu_gettimeofday() | expand

Commit Message

Marc-André Lureau March 4, 2022, 3:27 p.m. UTC
From: Marc-André Lureau <marcandre.lureau@redhat.com>

GLib g_get_real_time() is an alternative to gettimeofday().

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>
---
 blockdev.c                 |  8 ++++----
 hw/rtc/m41t80.c            |  6 +++---
 hw/virtio/virtio-balloon.c |  9 +--------
 qapi/qmp-event.c           | 12 +++++-------
 qemu-img.c                 |  8 ++++----
 qga/commands-posix.c       | 11 +----------
 target/m68k/m68k-semi.c    | 22 ++++++++++------------
 target/nios2/nios2-semi.c  | 24 +++++++++++-------------
 8 files changed, 39 insertions(+), 61 deletions(-)

Comments

Laurent Vivier March 4, 2022, 4:08 p.m. UTC | #1
Le 04/03/2022 à 16:27, marcandre.lureau@redhat.com a écrit :
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
> 
> GLib g_get_real_time() is an alternative to gettimeofday().
> 
> 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>
> ---
>   blockdev.c                 |  8 ++++----
>   hw/rtc/m41t80.c            |  6 +++---
>   hw/virtio/virtio-balloon.c |  9 +--------
>   qapi/qmp-event.c           | 12 +++++-------
>   qemu-img.c                 |  8 ++++----
>   qga/commands-posix.c       | 11 +----------
>   target/m68k/m68k-semi.c    | 22 ++++++++++------------
>   target/nios2/nios2-semi.c  | 24 +++++++++++-------------
>   8 files changed, 39 insertions(+), 61 deletions(-)
> 
...
> diff --git a/qga/commands-posix.c b/qga/commands-posix.c
> index 75dbaab68ea9..3311a4ca0167 100644
> --- a/qga/commands-posix.c
> +++ b/qga/commands-posix.c
> @@ -138,16 +138,7 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
>   
>   int64_t qmp_guest_get_time(Error **errp)
>   {
> -   int ret;
> -   qemu_timeval tq;
> -
> -   ret = qemu_gettimeofday(&tq);
> -   if (ret < 0) {
> -       error_setg_errno(errp, errno, "Failed to get time");
> -       return -1;
> -   }
> -
> -   return tq.tv_sec * 1000000000LL + tq.tv_usec * 1000;
> +    return g_get_real_time();

now you return microseconds, before it was nanoseconds.

qga/qapi-schema.json:

##
# @guest-get-time:
#
# Get the information about guest's System Time relative to
# the Epoch of 1970-01-01 in UTC.
#
# Returns: Time in nanoseconds.
#
# Since: 1.5
##
{ 'command': 'guest-get-time',
   'returns': 'int' }

Except this problem:

Reviewed-by: Laurent Vivier <laurent@vivier.eu>
Laurent Vivier March 4, 2022, 4:13 p.m. UTC | #2
Le 04/03/2022 à 17:08, Laurent Vivier a écrit :
> Le 04/03/2022 à 16:27, marcandre.lureau@redhat.com a écrit :
>> From: Marc-André Lureau <marcandre.lureau@redhat.com>
>>
>> GLib g_get_real_time() is an alternative to gettimeofday().
>>
>> 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>
>> ---
>>   blockdev.c                 |  8 ++++----
>>   hw/rtc/m41t80.c            |  6 +++---
>>   hw/virtio/virtio-balloon.c |  9 +--------
>>   qapi/qmp-event.c           | 12 +++++-------
>>   qemu-img.c                 |  8 ++++----
>>   qga/commands-posix.c       | 11 +----------
>>   target/m68k/m68k-semi.c    | 22 ++++++++++------------
>>   target/nios2/nios2-semi.c  | 24 +++++++++++-------------
>>   8 files changed, 39 insertions(+), 61 deletions(-)
>>
> ...
>> diff --git a/qga/commands-posix.c b/qga/commands-posix.c
>> index 75dbaab68ea9..3311a4ca0167 100644
>> --- a/qga/commands-posix.c
>> +++ b/qga/commands-posix.c
>> @@ -138,16 +138,7 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
>>   int64_t qmp_guest_get_time(Error **errp)
>>   {
>> -   int ret;
>> -   qemu_timeval tq;
>> -
>> -   ret = qemu_gettimeofday(&tq);
>> -   if (ret < 0) {
>> -       error_setg_errno(errp, errno, "Failed to get time");
>> -       return -1;
>> -   }
>> -
>> -   return tq.tv_sec * 1000000000LL + tq.tv_usec * 1000;
>> +    return g_get_real_time();
> 
> now you return microseconds, before it was nanoseconds.
> 
> qga/qapi-schema.json:
> 
> ##
> # @guest-get-time:
> #
> # Get the information about guest's System Time relative to
> # the Epoch of 1970-01-01 in UTC.
> #
> # Returns: Time in nanoseconds.
> #
> # Since: 1.5
> ##
> { 'command': 'guest-get-time',
>    'returns': 'int' }
> 
> Except this problem:
> 
> Reviewed-by: Laurent Vivier <laurent@vivier.eu>

Perhaps you can also remove the windows version from qga/commands-win32.c and move the function to 
qga/commands.c ?

Thanks,
Laurent
Marc-André Lureau March 4, 2022, 4:48 p.m. UTC | #3
Hi

On Fri, Mar 4, 2022 at 8:10 PM Laurent Vivier <laurent@vivier.eu> wrote:

> Le 04/03/2022 à 16:27, marcandre.lureau@redhat.com a écrit :
> > From: Marc-André Lureau <marcandre.lureau@redhat.com>
> >
> > GLib g_get_real_time() is an alternative to gettimeofday().
> >
> > 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>
> > ---
> >   blockdev.c                 |  8 ++++----
> >   hw/rtc/m41t80.c            |  6 +++---
> >   hw/virtio/virtio-balloon.c |  9 +--------
> >   qapi/qmp-event.c           | 12 +++++-------
> >   qemu-img.c                 |  8 ++++----
> >   qga/commands-posix.c       | 11 +----------
> >   target/m68k/m68k-semi.c    | 22 ++++++++++------------
> >   target/nios2/nios2-semi.c  | 24 +++++++++++-------------
> >   8 files changed, 39 insertions(+), 61 deletions(-)
> >
> ...
> > diff --git a/qga/commands-posix.c b/qga/commands-posix.c
> > index 75dbaab68ea9..3311a4ca0167 100644
> > --- a/qga/commands-posix.c
> > +++ b/qga/commands-posix.c
> > @@ -138,16 +138,7 @@ void qmp_guest_shutdown(bool has_mode, const char
> *mode, Error **errp)
> >
> >   int64_t qmp_guest_get_time(Error **errp)
> >   {
> > -   int ret;
> > -   qemu_timeval tq;
> > -
> > -   ret = qemu_gettimeofday(&tq);
> > -   if (ret < 0) {
> > -       error_setg_errno(errp, errno, "Failed to get time");
> > -       return -1;
> > -   }
> > -
> > -   return tq.tv_sec * 1000000000LL + tq.tv_usec * 1000;
> > +    return g_get_real_time();
>
> now you return microseconds, before it was nanoseconds.
>
>
my bad, will fix it in v2, thanks for noticing!


> qga/qapi-schema.json:
>
> ##
> # @guest-get-time:
> #
> # Get the information about guest's System Time relative to
> # the Epoch of 1970-01-01 in UTC.
> #
> # Returns: Time in nanoseconds.
> #
> # Since: 1.5
> ##
> { 'command': 'guest-get-time',
>    'returns': 'int' }
>
> Except this problem:
>
> Reviewed-by: Laurent Vivier <laurent@vivier.eu>
>
>
BALATON Zoltan March 4, 2022, 8:39 p.m. UTC | #4
On Fri, 4 Mar 2022, marcandre.lureau@redhat.com wrote:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
>
> GLib g_get_real_time() is an alternative to gettimeofday().
>
> 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>
> ---
> blockdev.c                 |  8 ++++----
> hw/rtc/m41t80.c            |  6 +++---
> hw/virtio/virtio-balloon.c |  9 +--------
> qapi/qmp-event.c           | 12 +++++-------
> qemu-img.c                 |  8 ++++----
> qga/commands-posix.c       | 11 +----------
> target/m68k/m68k-semi.c    | 22 ++++++++++------------
> target/nios2/nios2-semi.c  | 24 +++++++++++-------------
> 8 files changed, 39 insertions(+), 61 deletions(-)
>
> diff --git a/blockdev.c b/blockdev.c
> index 42e098b458b1..4b07dbfbdefc 100644
> --- a/blockdev.c
> +++ b/blockdev.c
> @@ -1235,7 +1235,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;
> @@ -1305,9 +1305,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 a00971a67e1c..e045c864bb44 100644
> --- a/hw/rtc/m41t80.c
> +++ b/hw/rtc/m41t80.c

This part

Acked-by: BALATON Zoltan <balaton@eik.bme.hu>

but why don't you use g_get_current_time() instead which seems to be a 
more direct replacement for gettimeofday, then you don't have to do maths 
with G_USEC_PER_SEC.

Regards.
BALATON Zoltan

> @@ -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 e6c1b0aa46fe..b1bada84cecc 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 19d3cd003833..025716b3ec37 100644
> --- a/qapi/qmp-event.c
> +++ b/qapi/qmp-event.c
> @@ -20,15 +20,13 @@
>
> 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);
> +    ts = qdict_from_jsonf_nofail("{ 'seconds': %" G_GINT64_FORMAT
> +                                 ", 'microseconds': %" G_GINT64_FORMAT "}",
> +                                 rt / G_USEC_PER_SEC,
> +                                 rt % G_USEC_PER_SEC);
>     qdict_put(qdict, "timestamp", ts);
> }
>
> diff --git a/qemu-img.c b/qemu-img.c
> index 6fe2466032f9..e26773909684 100644
> --- a/qemu-img.c
> +++ b/qemu-img.c
> @@ -3304,11 +3304,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 */
> @@ -3405,9 +3405,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/qga/commands-posix.c b/qga/commands-posix.c
> index 75dbaab68ea9..3311a4ca0167 100644
> --- a/qga/commands-posix.c
> +++ b/qga/commands-posix.c
> @@ -138,16 +138,7 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
>
> int64_t qmp_guest_get_time(Error **errp)
> {
> -   int ret;
> -   qemu_timeval tq;
> -
> -   ret = qemu_gettimeofday(&tq);
> -   if (ret < 0) {
> -       error_setg_errno(errp, errno, "Failed to get time");
> -       return -1;
> -   }
> -
> -   return tq.tv_sec * 1000000000LL + tq.tv_usec * 1000;
> +    return g_get_real_time();
> }
>
> void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
> diff --git a/target/m68k/m68k-semi.c b/target/m68k/m68k-semi.c
> index c5c164e096c8..54f735743165 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();
> +            if (!(p = lock_user(VERIFY_WRITE,
> +                                arg0, sizeof(struct gdb_timeval), 0))) {
> +                /* 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 5a7ad0c7108d..4600bf0a39d9 100644
> --- a/target/nios2/nios2-semi.c
> +++ b/target/nios2/nios2-semi.c
> @@ -400,20 +400,18 @@ 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;
>
Marc-André Lureau March 4, 2022, 8:45 p.m. UTC | #5
Hi

On Sat, Mar 5, 2022 at 12:40 AM BALATON Zoltan <balaton@eik.bme.hu> wrote:

> On Fri, 4 Mar 2022, marcandre.lureau@redhat.com wrote:
> > From: Marc-André Lureau <marcandre.lureau@redhat.com>
> >
> > GLib g_get_real_time() is an alternative to gettimeofday().
> >
> > 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>
> > ---
> > blockdev.c                 |  8 ++++----
> > hw/rtc/m41t80.c            |  6 +++---
> > hw/virtio/virtio-balloon.c |  9 +--------
> > qapi/qmp-event.c           | 12 +++++-------
> > qemu-img.c                 |  8 ++++----
> > qga/commands-posix.c       | 11 +----------
> > target/m68k/m68k-semi.c    | 22 ++++++++++------------
> > target/nios2/nios2-semi.c  | 24 +++++++++++-------------
> > 8 files changed, 39 insertions(+), 61 deletions(-)
> >
> > diff --git a/blockdev.c b/blockdev.c
> > index 42e098b458b1..4b07dbfbdefc 100644
> > --- a/blockdev.c
> > +++ b/blockdev.c
> > @@ -1235,7 +1235,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;
> > @@ -1305,9 +1305,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 a00971a67e1c..e045c864bb44 100644
> > --- a/hw/rtc/m41t80.c
> > +++ b/hw/rtc/m41t80.c
>
> This part
>
> Acked-by: BALATON Zoltan <balaton@eik.bme.hu>
>
> but why don't you use g_get_current_time() instead which seems to be a
> more direct replacement for gettimeofday, then you don't have to do maths
> with G_USEC_PER_SEC.>
>
>
As per glib docs, "g_get_current_time has been deprecated since version
2.62 and should not be used in newly-written code. GTimeVal is not
year-2038-safe. Use g_get_real_time() instead."

thanks




> Regards.
> BALATON Zoltan
>
> > @@ -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 e6c1b0aa46fe..b1bada84cecc 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 19d3cd003833..025716b3ec37 100644
> > --- a/qapi/qmp-event.c
> > +++ b/qapi/qmp-event.c
> > @@ -20,15 +20,13 @@
> >
> > 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);
> > +    ts = qdict_from_jsonf_nofail("{ 'seconds': %" G_GINT64_FORMAT
> > +                                 ", 'microseconds': %" G_GINT64_FORMAT
> "}",
> > +                                 rt / G_USEC_PER_SEC,
> > +                                 rt % G_USEC_PER_SEC);
> >     qdict_put(qdict, "timestamp", ts);
> > }
> >
> > diff --git a/qemu-img.c b/qemu-img.c
> > index 6fe2466032f9..e26773909684 100644
> > --- a/qemu-img.c
> > +++ b/qemu-img.c
> > @@ -3304,11 +3304,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 */
> > @@ -3405,9 +3405,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/qga/commands-posix.c b/qga/commands-posix.c
> > index 75dbaab68ea9..3311a4ca0167 100644
> > --- a/qga/commands-posix.c
> > +++ b/qga/commands-posix.c
> > @@ -138,16 +138,7 @@ void qmp_guest_shutdown(bool has_mode, const char
> *mode, Error **errp)
> >
> > int64_t qmp_guest_get_time(Error **errp)
> > {
> > -   int ret;
> > -   qemu_timeval tq;
> > -
> > -   ret = qemu_gettimeofday(&tq);
> > -   if (ret < 0) {
> > -       error_setg_errno(errp, errno, "Failed to get time");
> > -       return -1;
> > -   }
> > -
> > -   return tq.tv_sec * 1000000000LL + tq.tv_usec * 1000;
> > +    return g_get_real_time();
> > }
> >
> > void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
> > diff --git a/target/m68k/m68k-semi.c b/target/m68k/m68k-semi.c
> > index c5c164e096c8..54f735743165 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();
> > +            if (!(p = lock_user(VERIFY_WRITE,
> > +                                arg0, sizeof(struct gdb_timeval), 0))) {
> > +                /* 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 5a7ad0c7108d..4600bf0a39d9 100644
> > --- a/target/nios2/nios2-semi.c
> > +++ b/target/nios2/nios2-semi.c
> > @@ -400,20 +400,18 @@ 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;
> >
diff mbox series

Patch

diff --git a/blockdev.c b/blockdev.c
index 42e098b458b1..4b07dbfbdefc 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -1235,7 +1235,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;
@@ -1305,9 +1305,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 a00971a67e1c..e045c864bb44 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 e6c1b0aa46fe..b1bada84cecc 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 19d3cd003833..025716b3ec37 100644
--- a/qapi/qmp-event.c
+++ b/qapi/qmp-event.c
@@ -20,15 +20,13 @@ 
 
 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);
+    ts = qdict_from_jsonf_nofail("{ 'seconds': %" G_GINT64_FORMAT
+                                 ", 'microseconds': %" G_GINT64_FORMAT "}",
+                                 rt / G_USEC_PER_SEC,
+                                 rt % G_USEC_PER_SEC);
     qdict_put(qdict, "timestamp", ts);
 }
 
diff --git a/qemu-img.c b/qemu-img.c
index 6fe2466032f9..e26773909684 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -3304,11 +3304,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 */
@@ -3405,9 +3405,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/qga/commands-posix.c b/qga/commands-posix.c
index 75dbaab68ea9..3311a4ca0167 100644
--- a/qga/commands-posix.c
+++ b/qga/commands-posix.c
@@ -138,16 +138,7 @@  void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)
 
 int64_t qmp_guest_get_time(Error **errp)
 {
-   int ret;
-   qemu_timeval tq;
-
-   ret = qemu_gettimeofday(&tq);
-   if (ret < 0) {
-       error_setg_errno(errp, errno, "Failed to get time");
-       return -1;
-   }
-
-   return tq.tv_sec * 1000000000LL + tq.tv_usec * 1000;
+    return g_get_real_time();
 }
 
 void qmp_guest_set_time(bool has_time, int64_t time_ns, Error **errp)
diff --git a/target/m68k/m68k-semi.c b/target/m68k/m68k-semi.c
index c5c164e096c8..54f735743165 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();
+            if (!(p = lock_user(VERIFY_WRITE,
+                                arg0, sizeof(struct gdb_timeval), 0))) {
+                /* 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 5a7ad0c7108d..4600bf0a39d9 100644
--- a/target/nios2/nios2-semi.c
+++ b/target/nios2/nios2-semi.c
@@ -400,20 +400,18 @@  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;