Skip to contentSkip to conversation

qemu-sem-posix: use monotonic clock instead

PATCH In review Longpeng(Mike) via 2 participants 5 messages Feb 22, 2022 – Feb 23, 2022 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

v2 cover qemu-sem-posix: use monotonic clock instead

The qemu_sem_timedwait() uses system time as default, it would be affected by changes to the system time. In the real scenario, the time that goes faster or slower is a common case and the NTP service could help us to sync time periodically.

This patchset uses monotonic clock instead of the realtime clock, this could make sure we would not be affected by the system time anymore.

Changes v1(RFC) -> v2:
Patch 2:
- clean the code [Paolo]
- use pthread_condattr_setclock when initializing qemu-cond. [Paolo]
Patch 3:
- new added, make the qemu-sem code neater. [Longpeng]

Longpeng (Mike) (3):
sem-posix: remove the posix semaphore support
sem-posix: use monotonic clock instead
sem-posix: refactor qemu-sem with qemu-cond and qemu-mutex

 include/qemu/thread-posix.h |   9 +--
 meson.build                 |  12 ++-
 util/qemu-thread-posix.c    | 178 +++++++++++++++-----------------------------
 3 files changed, 73 insertions(+), 126 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 1/3 sem-posix: remove the posix semaphore support

POSIX specifies an absolute time for sem_timedwait(), it would be affected if the system time is changing, but there is not a relative time or monotonic clock version of sem_timedwait, so we cannot gain from POSIX semaphore any more.

An alternative way is to use sem_trywait + usleep, maybe we can remove CONFIG_SEM_TIMEDWAIT in this way? No, because some systems (e.g. mac os) mark the sem_xxx API as deprecated.

So maybe remove the usage of POSIX semaphore and turn to use the pthread variant for all systems looks better.

---
 include/qemu/thread-posix.h |  4 ----
 meson.build                 |  1 -
 util/qemu-thread-posix.c    | 54 ---------------------------------------------
 3 files changed, 59 deletions(-)
include/qemu/thread-posix.h +0 -4
index b792e6e..5466608 100644
@@ -27,13 +27,9 @@ struct QemuCond {
 };
 
 struct QemuSemaphore {
-#ifndef CONFIG_SEM_TIMEDWAIT
     pthread_mutex_t lock;
     pthread_cond_t cond;
     unsigned int count;
-#else
-    sem_t sem;
-#endif
     bool initialized;
 };
 
meson.build +0 -1
index 762d7ce..3ccb110 100644
@@ -1557,7 +1557,6 @@ config_host_data.set('CONFIG_POSIX_FALLOCATE', cc.has_function('posix_fallocate'
 config_host_data.set('CONFIG_POSIX_MEMALIGN', cc.has_function('posix_memalign'))
 config_host_data.set('CONFIG_PPOLL', cc.has_function('ppoll'))
 config_host_data.set('CONFIG_PREADV', cc.has_function('preadv', prefix: '#include <sys/uio.h>'))
-config_host_data.set('CONFIG_SEM_TIMEDWAIT', cc.has_function('sem_timedwait', dependencies: threads))
 config_host_data.set('CONFIG_SENDFILE', cc.has_function('sendfile'))
 config_host_data.set('CONFIG_SETNS', cc.has_function('setns') and cc.has_function('unshare'))
 config_host_data.set('CONFIG_SYNCFS', cc.has_function('syncfs'))
util/qemu-thread-posix.c +0 -54

114 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#2 lore raw

PATCH v2 2/3 sem-posix: use monotonic clock instead

In reply to Longpeng(Mike) via

Use CLOCK_MONOTONIC, so the timeout isn't affected by changes to the system time. It depends on the pthread_condattr_setclock(), while some systems(e.g. mac os) does not support it, so the behavior won't change in these systems.

---
 meson.build              | 11 +++++++++++
 util/qemu-thread-posix.c | 49 +++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 53 insertions(+), 7 deletions(-)
meson.build +11 -0
index 3ccb110..2bab94f 100644
@@ -1688,6 +1688,17 @@ config_host_data.set('CONFIG_PTHREAD_SETNAME_NP_WO_TID', cc.links(gnu_source_pre
     pthread_create(&thread, 0, f, 0);
     return 0;
   }''', dependencies: threads))
+config_host_data.set('CONFIG_PTHREAD_CONDATTR_SETCLOCK', cc.links(gnu_source_prefix + '''
+  #include <pthread.h>
+  #include <time.h>
+
+  int main(void)
+  {
+    pthread_condattr_t attr
+    pthread_condattr_init(&attr);
+    pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+    return 0;
+  }''', dependencies: threads))
 
 config_host_data.set('CONFIG_SIGNALFD', cc.links(gnu_source_prefix + '''
   #include <sys/signalfd.h>
util/qemu-thread-posix.c +42 -7

81 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#3 lore raw

PATCH v2 3/3 sem-posix: refactor qemu-sem with qemu-cond and qemu-mutex

In reply to Longpeng(Mike) via

Now, qemu-sem is based on the pthread_cond only, we can use qemu-cond and qemu-mutex to make the code neater and the mutex trace can be supported in qemu-sem naturally.

---
 include/qemu/thread-posix.h |   5 +--
 util/qemu-thread-posix.c    | 103 ++++++++++++++------------------------------
 2 files changed, 34 insertions(+), 74 deletions(-)
include/qemu/thread-posix.h +2 -3
index 5466608..5f2f3d1 100644
@@ -27,10 +27,9 @@ struct QemuCond {
 };
 
 struct QemuSemaphore {
-    pthread_mutex_t lock;
-    pthread_cond_t cond;
+    QemuMutex mutex;
+    QemuCond cond;
     unsigned int count;
-    bool initialized;
 };
 
 struct QemuEvent {
util/qemu-thread-posix.c +32 -71

159 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#4 lore raw
In reply to Longpeng(Mike) via
Quoted 20 lines · Longpeng(Mike) via

The qemu_sem_timedwait() uses system time as default, it would be affected by changes to the system time. In the real scenario, the time that goes faster or slower is a common case and the NTP service could help us to sync time periodically.

This patchset uses monotonic clock instead of the realtime clock, this could make sure we would not be affected by the system time anymore.

Changes v1(RFC) -> v2:
Patch 2:
- clean the code [Paolo]
- use pthread_condattr_setclock when initializing qemu-cond. [Paolo]
Patch 3:
- new added, make the qemu-sem code neater. [Longpeng]

Longpeng (Mike) (3):
sem-posix: remove the posix semaphore support
sem-posix: use monotonic clock instead
sem-posix: refactor qemu-sem with qemu-cond and qemu-mutex

Quoted 5 lines
  include/qemu/thread-posix.h |   9 +--
  meson.build                 |  12 ++-
  util/qemu-thread-posix.c    | 178 +++++++++++++++-----------------------------
  3 files changed, 73 insertions(+), 126 deletions(-)

Queued, thanks! I also sent a small optimization patch on top.

Paolo