[PATCH 6/7] virhostcpu.c: introduce virHostCPUGetAvailableCPUsBitmap()

Daniel Henrique Barboza danielhb413 at gmail.com
Fri Jun 26 22:10:43 UTC 2020


The idea is to have a function that calls virHostCPUGetOnlineBitmap()
but, instead of returning NULL if the host does not have CPU
offlining capabilities,  fall back to a bitmap containing all
present CPUs.

Next patch will use this helper in two other places.

Signed-off-by: Daniel Henrique Barboza <danielhb413 at gmail.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virhostcpu.c    | 30 ++++++++++++++++++++++++++++++
 src/util/virhostcpu.h    |  2 ++
 3 files changed, 33 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index ae0e253ab9..f120e200cb 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2211,6 +2211,7 @@ virHookPresent;
 
 
 # util/virhostcpu.h
+virHostCPUGetAvailableCPUsBitmap;
 virHostCPUGetCount;
 virHostCPUGetInfo;
 virHostCPUGetKVMMaxVCPUs;
diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c
index 615250d05d..8ca67e357d 100644
--- a/src/util/virhostcpu.c
+++ b/src/util/virhostcpu.c
@@ -1099,6 +1099,36 @@ virHostCPUGetMap(unsigned char **cpumap,
 }
 
 
+/* virHostCPUGetAvailableCPUsBitmap():
+ *
+ * Returns a virBitmap object with all available host CPUs.
+ *
+ * This is a glorified wrapper of virHostCPUGetOnlineBitmap()
+ * that, instead of returning NULL when 'ifndef __linux__' and
+ * the caller having to handle it outside the function, returns
+ * a virBitmap with all the possible CPUs in the host, up to
+ * virHostCPUGetCount(). */
+virBitmapPtr
+virHostCPUGetAvailableCPUsBitmap(void)
+{
+    g_autoptr(virBitmap) bitmap = NULL;
+
+    if (!(bitmap = virHostCPUGetOnlineBitmap())) {
+        int hostcpus;
+
+        if ((hostcpus = virHostCPUGetCount()) < 0)
+            return NULL;
+
+        if (!(bitmap = virBitmapNew(hostcpus)))
+            return NULL;
+
+        virBitmapSetAll(bitmap);
+    }
+
+    return g_steal_pointer(&bitmap);
+}
+
+
 #if HAVE_LINUX_KVM_H && defined(KVM_CAP_PPC_SMT)
 
 /* Get the number of threads per subcore.
diff --git a/src/util/virhostcpu.h b/src/util/virhostcpu.h
index 48b1431ca4..d07503857e 100644
--- a/src/util/virhostcpu.h
+++ b/src/util/virhostcpu.h
@@ -43,6 +43,8 @@ int virHostCPUGetStats(int cpuNum,
 bool virHostCPUHasBitmap(void);
 virBitmapPtr virHostCPUGetPresentBitmap(void);
 virBitmapPtr virHostCPUGetOnlineBitmap(void);
+virBitmapPtr virHostCPUGetAvailableCPUsBitmap(void);
+
 int virHostCPUGetCount(void);
 int virHostCPUGetThreadsPerSubcore(virArch arch) G_GNUC_NO_INLINE;
 
-- 
2.26.2




More information about the libvir-list mailing list