Remove qemu_gettimeofday()
v3 cover Remove qemu_gettimeofday()
From: Marc-André Lureau <[email protected]>
Hi,
Here is a few patches to replace qemu_gettimeofday() helper with functions provided by GLib.
v3:
- fix wrong qdict_from_jsonf() format change
v2:
- fix return of get-time in nanoseconds
- qga: replace with a common get-time function for qga posix/win32
- split qga patch
- add r-b tags, drop RFC
Marc-André Lureau (5):
m68k/nios2-semi: fix gettimeofday() result check
qtest: replace gettimeofday with GTimer
qga: replace qemu_gettimeofday() with g_get_real_time() Replace qemu_gettimeofday() with g_get_real_time()
oslib: drop qemu_gettimeofday()
include/sysemu/os-posix.h | 3 --- include/sysemu/os-win32.h | 6 ------ blockdev.c | 8 ++++---- hw/rtc/m41t80.c | 6 +++--- hw/virtio/virtio-balloon.c | 9 +-------- qapi/qmp-event.c | 9 +++------ qemu-img.c | 8 ++++---- qga/commands-posix.c | 14 -------------- qga/commands-win32.c | 19 ------------------- qga/commands.c | 5 +++++ softmmu/qtest.c | 39 ++++++++++---------------------------- target/m68k/m68k-semi.c | 22 ++++++++++----------- target/nios2/nios2-semi.c | 23 ++++++++++------------ util/oslib-win32.c | 20 ------------------- 14 files changed, 50 insertions(+), 141 deletions(-)
Signature · 1 line
2.35.1.273.ge6ebfd0e8cbb
PATCH v3 1/5 m68k/nios2-semi: fix gettimeofday() result check
From: Marc-André Lureau <[email protected]>
gettimeofday() returns 0 for success.
--- target/m68k/m68k-semi.c | 2 +- target/nios2/nios2-semi.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
target/m68k/m68k-semi.c +1 -1
@@ -381,7 +381,7 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)qemu_timeval tv;struct gdb_timeval *p; result =qemu_gettimeofday (&tv); -if (result !=0 ) { +if (result ==0 ) {if (!(p =lock_user (VERIFY_WRITE , arg0,sizeof (struct gdb_timeval ),0 ))) {/* FIXME - check error code? */
target/nios2/nios2-semi.c +1 -1
@@ -403,7 +403,7 @@ void do_nios2_semihosting(CPUNios2State *env)qemu_timeval tv;struct gdb_timeval *p; result =qemu_gettimeofday (&tv); -if (result !=0 ) { +if (result ==0 ) { p =lock_user (VERIFY_WRITE , arg0,sizeof (struct gdb_timeval ),0 );if (!p) {
Signature · 1 line
2.35.1.273.ge6ebfd0e8cbb
PATCH v3 2/5 qtest: replace gettimeofday with GTimer
In reply to [email protected]From: Marc-André Lureau <[email protected]>
glib provides a convenience helper to measure elapsed time. It isn't subject to wall-clock time changes.
Note that this changes the initial OPENED time, which used to print the current time.
--- softmmu/qtest.c | 39 ++++++++++----------------------------- 1 file changed, 10 insertions(+), 29 deletions(-)
softmmu/qtest.c +10 -29
@@ -58,12 +58,12 @@ static FILE *qtest_log_fp;static QTest *qtest;static GString *inbuf;static int irq_levels[MAX_IRQ ]; -static qemu_timeval start_time; +static GTimer *timer;static bool qtest_opened;static void (*qtest_server_send)(void *,const char *);static void *qtest_server_send_opaque; -#define FMT_timeval "%ld.%06ld" +#define FMT_timeval "%.06f"/** * DOC: QTest Protocol
@@ -264,28 +264,13 @@ static int hex2nib(char ch) } } -static void qtest_get_time (qemu_timeval *tv) -{ -qemu_gettimeofday (tv); - tv->tv_sec -= start_time.tv_sec; - tv->tv_usec -= start_time.tv_usec; -if (tv->tv_usec <0 ) { - tv->tv_usec +=1000000 ; - tv->tv_sec -=1 ; - } -} -static void qtest_send_prefix (CharBackend *chr) { -qemu_timeval tv; -if (!qtest_log_fp || !qtest_opened) {return ; } -qtest_get_time (&tv); -fprintf (qtest_log_fp,"[S +" FMT_timeval"] " , - (long ) tv.tv_sec, (long ) tv.tv_usec); +fprintf (qtest_log_fp,"[S +" FMT_timeval"] " ,g_timer_elapsed (timer,NULL )); } static void GCC_FMT_ATTR(1, 2) qtest_log_send(const char *fmt, ...)
@@ -386,12 +371,9 @@ static void qtest_process_command(CharBackend *chr, gchar **words) command = words[0 ];if (qtest_log_fp) { -qemu_timeval tv;int i; -qtest_get_time (&tv); -fprintf (qtest_log_fp,"[R +" FMT_timeval"]" , - (long ) tv.tv_sec, (long ) tv.tv_usec); +fprintf (qtest_log_fp,"[R +" FMT_timeval"]" ,g_timer_elapsed (timer,NULL ));for (i =0 ; words[i]; i++) {fprintf (qtest_log_fp," %s" , words[i]); }
@@ -846,21 +828,20 @@ static void qtest_event(void *opaque, QEMUChrEvent event)for (i =0 ; i <ARRAY_SIZE (irq_levels); i++) { irq_levels[i] =0 ; } -qemu_gettimeofday (&start_time); + +g_clear_pointer (&timer, g_timer_destroy); + timer =g_timer_new (); qtest_opened = true;if (qtest_log_fp) { -fprintf (qtest_log_fp,"[I " FMT_timeval"] OPENED\n" , - (long ) start_time.tv_sec, (long ) start_time.tv_usec); +fprintf (qtest_log_fp,"[I " FMT_timeval"] OPENED\n" ,g_timer_elapsed (timer,NULL )); }break ;case CHR_EVENT_CLOSED : qtest_opened = false;if (qtest_log_fp) { -qemu_timeval tv; -qtest_get_time (&tv); -fprintf (qtest_log_fp,"[I +" FMT_timeval"] CLOSED\n" , - (long ) tv.tv_sec, (long ) tv.tv_usec); +fprintf (qtest_log_fp,"[I +" FMT_timeval"] CLOSED\n" ,g_timer_elapsed (timer,NULL )); } +g_clear_pointer (&timer, g_timer_destroy);break ;default :break ;
Signature · 1 line
2.35.1.273.ge6ebfd0e8cbb
Re: [PATCH v3 2/5] qtest: replace gettimeofday with GTimer
Quoted 7 lines · On 07/03/2022 08.03, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
glib provides a convenience helper to measure elapsed time. It isn't subject to wall-clock time changes.
Note that this changes the initial OPENED time, which used to print the current time.
[...]
Quoted 5 lines
softmmu/qtest.c +0
-1@@ -846,21 +828,20 @@ static void qtest_event(void *opaque, QEMUChrEvent event)for (i =0 ; i <ARRAY_SIZE (irq_levels); i++) { irq_levels[i] =0 ; } -qemu_gettimeofday (&start_time);
softmmu/qtest.c static void qtest_event(void *opaque, QEMUChrEvent event) · line 838
Quoted 10 lines
softmmu/qtest.c +4
-2+ +g_clear_pointer (&timer, g_timer_destroy); + timer =g_timer_new (); qtest_opened = true;if (qtest_log_fp) { -fprintf (qtest_log_fp,"[I " FMT_timeval"] OPENED\n" , - (long ) start_time.tv_sec, (long ) start_time.tv_usec); +fprintf (qtest_log_fp,"[I " FMT_timeval"] OPENED\n" ,g_timer_elapsed (timer,NULL )); }break ;
The new timestamp here is quite unuseful now, of course ... could you replace it with g_get_current_time() instead?
Thomas
Re: [PATCH v3 2/5] qtest: replace gettimeofday with GTimer
Hi
Quoted 22 lines · Thomas Huth
Quoted 7 lines · On 07/03/2022 08.03, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
glib provides a convenience helper to measure elapsed time. It isn't subject to wall-clock time changes.
Note that this changes the initial OPENED time, which used to print the current time.
[...]
Review static void qtest_event(void *opaque, QEMUChrEvent
Quoted 1 line
(quoted diff) +0
-0@@ -846,21 +828,20 @@ static void qtest_event(void *opaque, QEMUChrEventevent)
softmmu/qtest.c static void qtest_event(void *opaque, QEMUChrEvent event)
Quoted 11 lines
softmmu/qtest.c +3
-3for (i =0 ; i <ARRAY_SIZE (irq_levels); i++) { irq_levels[i] =0 ; } -qemu_gettimeofday (&start_time); + +g_clear_pointer (&timer, g_timer_destroy); + timer =g_timer_new (); qtest_opened = true;if (qtest_log_fp) { - fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n", - (long) start_time.tv_sec, (long)
Quoted 9 lines
start_time.tv_usec);
Review static void qtest_event(void *opaque, QEMUChrEvent · line 836
Quoted 1 line
(quoted diff) +1
-0+ fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n",
g_timer_elapsed(timer, NULL));Review static void qtest_event(void *opaque, QEMUChrEvent · line 838
Quoted 2 lines
(quoted diff) +0
-0} break;The new timestamp here is quite unuseful now, of course ... could you replace it with
g_get_current_time()instead?
Eventually, but I wonder why this (and only this) particular timestamp should be the current time.
Signature · 1 line
Marc-André Lureau
Re: [PATCH v3 2/5] qtest: replace gettimeofday with GTimer
Quoted 38 lines · Marc-André Lureau
Hi
On Mon, Mar 7, 2022 at 11:46 AM Thomas Huth <[email protected] <mailto:[email protected]>> wrote:
On 07/03/2022 08.03, [email protected] <mailto:[email protected]> wrote: > From: Marc-André Lureau <[email protected] <mailto:[email protected]>> > > glib provides a convenience helper to measure elapsed time. It isn't > subject to wall-clock time changes. > > Note that this changes the initial OPENED time, which used to print the > current time. [...] > @@ -846,21 +828,20 @@ static void qtest_event(void *opaque, QEMUChrEvent event) > for (i = 0; i < ARRAY_SIZE(irq_levels); i++) { > irq_levels[i] = 0; > } > - qemu_gettimeofday(&start_time); > + > + g_clear_pointer(&timer, g_timer_destroy); > + timer = g_timer_new(); > qtest_opened = true; > if (qtest_log_fp) { > - fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n", > - (long) start_time.tv_sec, (long) start_time.tv_usec); > + fprintf(qtest_log_fp, "[I " FMT_timeval "] OPENED\n", g_timer_elapsed(timer, NULL)); > } > break; The new timestamp here is quite unuseful now, of course ... could you replace it with g_get_current_time() instead?
Quoted 1 line
Eventually, but I wonder why this (and only this) particular timestamp should be the current time.
I assume it was meant as a possibility to sync the times in this log with other things that are going on on the host system in parallel. If you only have the relative time stamps in the log here, you cannot compare the events to other logs anymore. (having said that, I wonder why it doesn't simply always use the current wall time and uses the relative time instead, but maybe there is also a reason for that...)
Thomas
PATCH v3 3/5 qga: replace qemu_gettimeofday() with g_get_real_time()
In reply to [email protected]From: Marc-André Lureau <[email protected]>
GLib g_get_real_time() is an alternative to gettimeofday() which allows to simplify our code.
--- qga/commands-posix.c | 14 -------------- qga/commands-win32.c | 19 ------------------- qga/commands.c | 5 +++++ 3 files changed, 5 insertions(+), 33 deletions(-)
qga/commands-posix.c +0 -14
@@ -136,20 +136,6 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **errp)/* succeeded */ } -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 ; -} -void qmp_guest_set_time (bool has_time,int64_t time_ns,Error **errp) {int ret;
qga/commands-win32.c +0 -19
@@ -1751,25 +1751,6 @@ static int64_t filetime_to_ns(const FILETIME *tf) - W32_FT_OFFSET) * 100; } -int64_t qmp_guest_get_time (Error **errp) -{ -SYSTEMTIME ts = {0 }; -FILETIME tf; - -GetSystemTime (&ts); -if (ts.wYear <1601 || ts.wYear >30827 ) { -error_setg (errp,"Failed to get time" ); -return -1 ; - } - -if (!SystemTimeToFileTime (&ts, &tf)) { -error_setg (errp,"Failed to convert system time: %d" , (int )GetLastError ()); -return -1 ; - } - -return filetime_to_ns (&tf); -} -void qmp_guest_set_time (bool has_time,int64_t time_ns,Error **errp) {Error *local_err =NULL ;
qga/commands.c +5 -0
@@ -585,3 +585,8 @@ GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,return read_data; } + +int64_t qmp_guest_get_time (Error **errp) +{ +return g_get_real_time () *1000 ; +}
Signature · 1 line
2.35.1.273.ge6ebfd0e8cbb
Re: [PATCH v3 3/5] qga: replace qemu_gettimeofday() with g_get_real_time()
Quoted 5 lines · On 07/03/2022 08.03, [email protected] wrote:
From: Marc-André Lureau <[email protected]>
GLib
g_get_real_time()is an alternative togettimeofday()which allows to simplify our code.
Quoted 7 lines
Signed-off-by Marc-André Lureau <[email protected]>Reviewed-by Laurent Vivier <[email protected]>--- qga/commands-posix.c | 14 -------------- qga/commands-win32.c | 19 ------------------- qga/commands.c | 5 +++++ 3 files changed, 5 insertions(+), 33 deletions(-)
PATCH v3 4/5 Replace qemu_gettimeofday() with g_get_real_time()
In reply to [email protected]From: Marc-André Lureau <[email protected]>
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.
--- 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(-)
blockdev.c +4 -4
@@ -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 ();
hw/rtc/m41t80.c +3 -3
@@ -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 :
hw/virtio/virtio-balloon.c +1 -8
@@ -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)) {
qapi/qmp-event.c +3 -6
@@ -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); }
qemu-img.c +4 -4
@@ -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) {
target/m68k/m68k-semi.c +10 -12
@@ -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 ;
target/nios2/nios2-semi.c +10 -13
@@ -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 ;
Signature · 1 line
2.35.1.273.ge6ebfd0e8cbb
PATCH v3 5/5 oslib: drop qemu_gettimeofday()
In reply to [email protected]From: Marc-André Lureau <[email protected]>
No longer used after the previous patches.
--- include/sysemu/os-posix.h | 3 --- include/sysemu/os-win32.h | 6 ------ util/oslib-win32.c | 20 -------------------- 3 files changed, 29 deletions(-)
include/sysemu/os-posix.h +0 -3
@@ -52,9 +52,6 @@ int os_mlock(void);#define closesocket (s) close(s)#define ioctlsocket (s, r, v) ioctl(s, r, v) -typedef struct timeval qemu_timeval ; -#define qemu_gettimeofday (tp) gettimeofday(tp, NULL) -int os_set_daemonize (bool d);bool is_daemonized (void );
include/sysemu/os-win32.h +0 -6
@@ -71,12 +71,6 @@ int getpagesize(void);# define EPROTONOSUPPORT EINVAL#endif -typedef struct { -long tv_sec; -long tv_usec; -}qemu_timeval ; -int qemu_gettimeofday (qemu_timeval *tp); -static inline int os_set_daemonize (bool d) {if (d) {
util/oslib-win32.c +0 -20
@@ -265,26 +265,6 @@ void qemu_set_cloexec(int fd) { } -/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */ -#define _W32_FT_OFFSET (116444736000000000ULL) - -int qemu_gettimeofday (qemu_timeval *tp) -{ -union { -unsigned long long ns100;/*time since 1 Jan 1601 in 100ns units */ -FILETIME ft; - } _now; - -if (tp) { -GetSystemTimeAsFileTime (&_now.ft); - tp->tv_usec=(long )((_now.ns100 /10ULL ) %1000000ULL ); - tp->tv_sec= (long )((_now.ns100 - _W32_FT_OFFSET) /10000000ULL ); - } -/* Always return 0 as per Open Group Base Specifications Issue 6. -Do not set errno on error. */ -return 0 ; -} -int qemu_get_thread_id (void ) {return GetCurrentThreadId ();
Signature · 1 line
2.35.1.273.ge6ebfd0e8cbb