The nodeinfo is reporting incorrect number of cpus and incorrect host
topology on PPC64 KVM hosts. The KVM hypervisor on PPC64 needs only
the primary thread in a core to be online, and the secondaries offlined.
While scheduling a guest in, the kvm scheduler wakes up the secondaries to
run in guest context.
The host scheduling of the guests happen at the core level(as only primary
thread is online). The kvm scheduler exploits as many threads of the core
as needed by guest. Further, starting POWER8, the processor allows splitting
a physical core into multiple subcores with 2 or 4 threads each. Again, only
the primary thread in a subcore is online in the host. The KVM-PPC
scheduler allows guests to exploit all the offline threads in the subcore,
by bringing them online when needed.
(Kernel patches on split-core
http://www.spinics.net/lists/kvm-ppc/msg09121.html)
Recently with dynamic micro-threading changes in ppc-kvm, makes sure
to utilize all the offline cpus across guests, and across guests with
different cpu topologies.
(
https://www.mail-archive.com/kvm@vger.kernel.org/msg115978.html)
Since the offline cpus are brought online in the guest context, it is safe
to count them as online. Nodeinfo today discounts these offline cpus from
cpu count/topology calclulation, and the nodeinfo output is not of any help
and the host appears overcommited when it is actually not.
The patch carefully counts those offline threads whose primary threads are
online. The host topology displayed by the nodeinfo is also fixed when the
host is in valid kvm state.
Test results with and without fix:
http://ur1.ca/mx25y
Signed-off-by: Shivaprasad G Bhat <sbhat(a)linux.vnet.ibm.com>
---
src/nodeinfo.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 85 insertions(+)
diff --git a/src/nodeinfo.c b/src/nodeinfo.c
index 2fafe2d..1a27080 100644
--- a/src/nodeinfo.c
+++ b/src/nodeinfo.c
@@ -32,6 +32,8 @@
#include <sys/utsname.h>
#include <sched.h>
#include "conf/domain_conf.h"
+#include <fcntl.h>
+#include <sys/ioctl.h>
#if defined(__FreeBSD__) || defined(__APPLE__)
# include <sys/time.h>
@@ -428,6 +430,10 @@ virNodeParseNode(const char *node,
unsigned int cpu;
int online;
int direrr;
+ int kvmfd = -1;
+ int lastonline;
+ int threads_per_subcore = 1;
+ bool valid_ppc64_kvmhost_mode = false;
*threads = 0;
*cores = 0;
@@ -464,6 +470,66 @@ virNodeParseNode(const char *node,
sock_max++;
+ if (ARCH_IS_PPC64(arch)) {
+ /* PPC-KVM needs the secondary threads of a core to be offline on the
+ * host. The kvm schedular brings the secondary threads online in the
+ * guest context. Moreover, P8 processor has split-core capability
+ * where, there can be 1,2 or 4 subcores per core. The primaries of the
+ * subcores alone will be online on the host for a subcore in the
+ * host. Even though the actual threads per core for P8 processor is 8,
+ * depending on the subcores_per_core = 1, 2 or 4, the threads per
+ * subcore will vary accordingly to 8, 4 and 2 repectively.
+ * So, On host threads_per_core what is arrived at from sysfs in the
+ * current logic is actually the subcores_per_core. Threads per subcore
+ * can only be obtained from the kvm device. For example, on P8 wih 1
+ * core having 8 threads, sub_cores_percore=4, the threads 0,2,4 & 6
+ * will be online. The sysfs reflects this and in the current logic
+ * variable 'threads' will be 4 which is nothing but subcores_per_core.
+ * If the user tampers the cpu online/offline states using chcpu or other
+ * means, then it is an unsupported configuration for kvm.
+ * The code below tries to keep in mind
+ * - when the libvirtd is run inside a KVM guest or Phyp based guest.
+ * - Or on the kvm host where user manually tampers the cpu states to
+ * offline/online randomly.
+ */
+# if HAVE_LINUX_KVM_H
+# include <linux/kvm.h>
+ kvmfd = open("/dev/kvm", O_RDONLY);
+ if (kvmfd >= 0) {
+# ifdef KVM_CAP_PPC_SMT
+ valid_ppc64_kvmhost_mode = true;
+ /* For Phyp and KVM based guests the ioctl for KVM_CAP_PPC_SMT
+ * returns zero and both primary and secondary threads will be
+ * online. Dont try to alter or interpret anything here.
+ */
+ threads_per_subcore = ioctl(kvmfd, KVM_CHECK_EXTENSION, KVM_CAP_PPC_SMT);
+ if (threads_per_subcore == 0)
+ valid_ppc64_kvmhost_mode = false;
+# endif
+ }
+ VIR_FORCE_CLOSE(kvmfd);
+# endif
+ rewinddir(cpudir);
+ lastonline = -1;
+ while (valid_ppc64_kvmhost_mode &&
+ ((direrr = virDirRead(cpudir, &cpudirent, node)) > 0)) {
+ if (sscanf(cpudirent->d_name, "cpu%u", &cpu) != 1)
+ continue;
+ if ((online = virNodeGetCpuValue(node, cpu, "online", 1)) < 0)
+ goto cleanup;
+
+ if (online) {
+ if (lastonline != -1 &&
+ (cpu - lastonline) < threads_per_subcore) {
+ valid_ppc64_kvmhost_mode = false; /* if any of secondaries
+ * online */
+ break;
+ }
+ lastonline = cpu;
+ }
+ }
+ }
+
/* allocate cpu maps for each socket */
if (VIR_ALLOC_N(core_maps, sock_max) < 0)
goto cleanup;
@@ -473,6 +539,7 @@ virNodeParseNode(const char *node,
/* iterate over all CPU's in the node */
rewinddir(cpudir);
+ lastonline = -1;
while ((direrr = virDirRead(cpudir, &cpudirent, node)) > 0) {
if (sscanf(cpudirent->d_name, "cpu%u", &cpu) != 1)
continue;
@@ -480,6 +547,17 @@ virNodeParseNode(const char *node,
if ((online = virNodeGetCpuValue(node, cpu, "online", 1)) < 0)
goto cleanup;
+ if (ARCH_IS_PPC64(arch) && valid_ppc64_kvmhost_mode) {
+ if (online) {
+ lastonline = cpu;
+ } else if (lastonline != -1 &&
+ (cpu-lastonline) < threads_per_subcore) {
+ processors++; /* Count only those secondaries whose primary
+ subcore is online */
+ continue;
+ }
+ }
+
if (!online) {
(*offline)++;
continue;
@@ -528,6 +606,13 @@ virNodeParseNode(const char *node,
*cores = core;
}
+ if (ARCH_IS_PPC64(arch) && valid_ppc64_kvmhost_mode) {
+ /* The actual host threads per core is
+ * multiple of the subcores_per_core(i.e variable *threads in this case)
+ * and threads_per_subcore.
+ */
+ *threads = *threads * threads_per_subcore;
+ }
ret = processors;
cleanup: