qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 11/32] bsd-user: Get number of cpus.


From: Karim Taha
Subject: [PATCH 11/32] bsd-user: Get number of cpus.
Date: Sun, 27 Aug 2023 17:57:25 +0200

From: Kyle Evans <kevans@FreeBSD.org>

Signed-off-by: Kyle Evans <kevans@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/bsd-proc.c | 39 +++++++++++++++++++++++++++++++++++++++
 bsd-user/bsd-proc.h |  2 ++
 2 files changed, 41 insertions(+)

diff --git a/bsd-user/bsd-proc.c b/bsd-user/bsd-proc.c
index 49c0fb67d0..dd6bad6de3 100644
--- a/bsd-user/bsd-proc.c
+++ b/bsd-user/bsd-proc.c
@@ -185,3 +185,42 @@ int host_to_target_waitstatus(int status)
     return status;
 }
 
+int bsd_get_ncpu(void)
+{
+    static int ncpu = -1;
+
+    if (ncpu != -1) {
+        return ncpu;
+    }
+    if (ncpu == -1) {
+        cpuset_t mask;
+
+        CPU_ZERO(&mask);
+
+        if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, 
sizeof(mask),
+                               &mask) == 0) {
+            ncpu = CPU_COUNT(&mask);
+        }
+    }
+#ifdef _SC_NPROCESSORS_ONLN
+    if (ncpu == -1)
+        ncpu = sysconf(_SC_NPROCESSORS_ONLN);
+#endif
+#if defined(CTL_HW) && defined(HW_NCPU)
+    if (ncpu == -1) {
+        int mib[2] = {CTL_HW, HW_NCPU};
+        size_t sz;
+
+        sz = sizeof(ncpu);
+        if (sysctl(mib, 2, &ncpu, &sz, NULL, NULL) == -1) {
+            ncpu = -1;
+        }
+    }
+#endif
+    if (ncpu == -1) {
+        gemu_log("XXX Missing bsd_get_ncpu() implementation\n");
+        ncpu = 1;
+    }
+    return ncpu;
+}
+
diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index 048773a75d..b6225e520e 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -26,6 +26,8 @@
 #include "gdbstub/syscalls.h"
 #include "qemu/plugin.h"
 
+int bsd_get_ncpu(void);
+
 /* exit(2) */
 static inline abi_long do_bsd_exit(void *cpu_env, abi_long arg1)
 {
-- 
2.40.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]