Skip to contentSkip to conversation

osdep: Cache getpagesize() call in qemu_real_host_page_size()

PATCH In review Philippe Mathieu-Daudé 2 participants 4 messages Nov 3, 2025 – Nov 6, 2025

lore
Share & tools
lore
#0 lore raw

cover osdep: Cache getpagesize() call in qemu_real_host_page_size()

Cache getpagesize() call once, so we don't have to worry how often we can call qemu_real_host_page_size() and qemu_real_host_page_mask().

Philippe Mathieu-Daudé (2):
osdep: Un-inline qemu_real_host_page_size()
osdep: Cache getpagesize() call in qemu_real_host_page_size()

 include/qemu/osdep.h |  6 +-----
 util/osdep.c         | 11 +++++++++++
 2 files changed, 12 insertions(+), 5 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/2 osdep: Un-inline qemu_real_host_page_size()

---
 include/qemu/osdep.h | 6 +-----
 util/osdep.c         | 5 +++++
 2 files changed, 6 insertions(+), 5 deletions(-)
include/qemu/osdep.h +1 -5
index cf8d7cf7e61..fd714014eaf 100644
@@ -763,14 +763,10 @@ bool qemu_finish_async_prealloc_mem(Error **errp);
  */
 char *qemu_get_pid_name(pid_t pid);
 
+uintptr_t qemu_real_host_page_size(void);
 /* Using intptr_t ensures that qemu_*_page_mask is sign-extended even
  * when intptr_t is 32-bit and we are aligning a long long.
  */
-static inline uintptr_t qemu_real_host_page_size(void)
-{
-    return getpagesize();
-}
-
 static inline intptr_t qemu_real_host_page_mask(void)
 {
     return -(intptr_t)qemu_real_host_page_size();
util/osdep.c +5 -0
index 770369831bc..44fad13dcc7 100644
@@ -613,3 +613,8 @@ int qemu_fdatasync(int fd)
     return fsync(fd);
 #endif
 }
+
+uintptr_t qemu_real_host_page_size(void)
+{
+    return getpagesize();
+}
Signature · 1 line
2.51.0
#2 lore raw

PATCH 2/2 osdep: Cache getpagesize() call in qemu_real_host_page_size()

In reply to Philippe Mathieu-Daudé

Since we can not know whether a libc implementation of getpagesize() calls syscalls -- potentially slow --, but we know the host page size won't change during runtime, we can cache its value.

---
 util/osdep.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
util/osdep.c +7 -1
index 44fad13dcc7..9b50d00dbda 100644
@@ -616,5 +616,11 @@ int qemu_fdatasync(int fd)
 
 uintptr_t qemu_real_host_page_size(void)
 {
-    return getpagesize();
+    static uintptr_t real_host_page_size;
+
+    if (!real_host_page_size) {
+        real_host_page_size = getpagesize();
+    }
+
+    return real_host_page_size;
 }
Signature · 1 line
2.51.0
Reviewed-by on the v1 series
#3 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 7 lines · Philippe Mathieu-Daudé

Cache getpagesize() call once, so we don't have to worry how often we can call qemu_real_host_page_size() and qemu_real_host_page_mask().

Philippe Mathieu-Daudé (2):
osdep: Un-inline qemu_real_host_page_size()
osdep: Cache getpagesize() call in qemu_real_host_page_size()

That's a step up, certainly. I had been thinking of making it a faux const variable, akin to how we manage page-vary-common.c, and a high priority constructor.

With less efford you could mark the function __attribute__((const)), so that the compiler will CSE calls.

r~