Skip to contentSkip to conversation

kvm/ppc/book3s: Move struct kvmppc_vcore from kvm_host.h to kvm_book3s.h

PATCH Superseded Suraj Jitindar Singh 2 participants 7 messages Jul 22, 2016 – Jul 25, 2016

Subsystems (12)
Arm (32-bit) x86 KVM PowerPC +8 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH v5 1/5 kvm/ppc/book3s: Move struct kvmppc_vcore from kvm_host.h to kvm_book3s.h

The next commit will introduce a member to the kvmppc_vcore struct which references MAX_SMT_THREADS which is defined in kvm_book3s_asm.h, however this file isn't included in kvm_host.h directly. Thus compiling for certain platforms such as pmac32_defconfig and ppc64e_defconfig with KVM fails due to MAX_SMT_THREADS not being defined.

Move the struct kvmppc_vcore definition to kvm_book3s.h which explicitly includes kvm_book3s_asm.h.

---
Change Log:

V1 -> V2:
- Added patch to series

---
 arch/powerpc/include/asm/kvm_book3s.h | 35 +++++++++++++++++++++++++++++++++++
 arch/powerpc/include/asm/kvm_host.h   | 35 -----------------------------------
 2 files changed, 35 insertions(+), 35 deletions(-)
arch/powerpc/include/asm/kvm_book3s.h +35 -0
index 8f39796..a50c5fe 100644
@@ -69,6 +69,41 @@ struct hpte_cache {
 	int pagesize;
 };
 
+/*
+ * Struct for a virtual core.
+ * Note: entry_exit_map combines a bitmap of threads that have entered
+ * in the bottom 8 bits and a bitmap of threads that have exited in the
+ * next 8 bits.  This is so that we can atomically set the entry bit
+ * iff the exit map is 0 without taking a lock.
+ */
+struct kvmppc_vcore {
+	int n_runnable;
+	int num_threads;
+	int entry_exit_map;
+	int napping_threads;
+	int first_vcpuid;
+	u16 pcpu;
+	u16 last_cpu;
+	u8 vcore_state;
+	u8 in_guest;
+	struct kvmppc_vcore *master_vcore;
+	struct list_head runnable_threads;
+	struct list_head preempt_list;
+	spinlock_t lock;
+	struct swait_queue_head wq;
+	spinlock_t stoltb_lock;	/* protects stolen_tb and preempt_tb */
+	u64 stolen_tb;
+	u64 preempt_tb;
+	struct kvm_vcpu *runner;
+	struct kvm *kvm;
+	u64 tb_offset;		/* guest timebase - host timebase */
+	ulong lpcr;
+	u32 arch_compat;
+	ulong pcr;
+	ulong dpdes;		/* doorbell state (POWER8) */
+	ulong conferring_threads;
+};
+
 struct kvmppc_vcpu_book3s {
 	struct kvmppc_sid_map sid_map[SID_MAP_NUM];
 	struct {
arch/powerpc/include/asm/kvm_host.h +0 -35
index ec35af3..19c6731 100644
@@ -275,41 +275,6 @@ struct kvm_arch {
 #endif
 };
 
-/*
- * Struct for a virtual core.
- * Note: entry_exit_map combines a bitmap of threads that have entered
- * in the bottom 8 bits and a bitmap of threads that have exited in the
- * next 8 bits.  This is so that we can atomically set the entry bit
- * iff the exit map is 0 without taking a lock.
- */
-struct kvmppc_vcore {
-	int n_runnable;
-	int num_threads;
-	int entry_exit_map;
-	int napping_threads;
-	int first_vcpuid;
-	u16 pcpu;
-	u16 last_cpu;
-	u8 vcore_state;
-	u8 in_guest;
-	struct kvmppc_vcore *master_vcore;
-	struct list_head runnable_threads;
-	struct list_head preempt_list;
-	spinlock_t lock;
-	struct swait_queue_head wq;
-	spinlock_t stoltb_lock;	/* protects stolen_tb and preempt_tb */
-	u64 stolen_tb;
-	u64 preempt_tb;
-	struct kvm_vcpu *runner;
-	struct kvm *kvm;
-	u64 tb_offset;		/* guest timebase - host timebase */
-	ulong lpcr;
-	u32 arch_compat;
-	ulong pcr;
-	ulong dpdes;		/* doorbell state (POWER8) */
-	ulong conferring_threads;
-};
-
 #define VCORE_ENTRY_MAP(vc)	((vc)->entry_exit_map & 0xff)
 #define VCORE_EXIT_MAP(vc)	((vc)->entry_exit_map >> 8)
 #define VCORE_IS_EXITING(vc)	(VCORE_EXIT_MAP(vc) != 0)
Signature · 1 line
2.5.5
#1 lore raw

PATCH v5 2/5 kvm/ppc/book3s_hv: Change vcore element runnable_threads from linked-list to array

The struct kvmppc_vcore is a structure used to store various information about a virtual core for a kvm guest. The runnable_threads element of the struct provides a list of all of the currently runnable vcpus on the core (those in the KVMPPC_VCPU_RUNNABLE state). The previous implementation of this list was a linked_list. The next patch requires that the list be able to be iterated over without holding the vcore lock.

Reimplement the runnable_threads list in the kvmppc_vcore struct as an array. Implement function to iterate over valid entries in the array and update access sites accordingly.

---
 arch/powerpc/include/asm/kvm_book3s.h |  2 +-
 arch/powerpc/include/asm/kvm_host.h   |  1 -
 arch/powerpc/kvm/book3s_hv.c          | 68 +++++++++++++++++++++--------------
 3 files changed, 43 insertions(+), 28 deletions(-)
arch/powerpc/include/asm/kvm_book3s.h +1 -1
index a50c5fe..151f817 100644
@@ -87,7 +87,7 @@ struct kvmppc_vcore {
 	u8 vcore_state;
 	u8 in_guest;
 	struct kvmppc_vcore *master_vcore;
-	struct list_head runnable_threads;
+	struct kvm_vcpu *runnable_threads[MAX_SMT_THREADS];
 	struct list_head preempt_list;
 	spinlock_t lock;
 	struct swait_queue_head wq;
arch/powerpc/include/asm/kvm_host.h +0 -1
index 19c6731..02d06e9 100644
@@ -633,7 +633,6 @@ struct kvm_vcpu_arch {
 	long pgfault_index;
 	unsigned long pgfault_hpte[2];
 
-	struct list_head run_list;
 	struct task_struct *run_task;
 	struct kvm_run *kvm_run;
 
arch/powerpc/kvm/book3s_hv.c +42 -26

177 lines not on this page · raw message

Signature · 1 line
2.5.5
#2 lore raw

PATCH v5 3/5 kvm/ppc/book3s_hv: Implement halt polling in the kvm_hv kernel module

In reply to Suraj Jitindar Singh

This patch introduces new halt polling functionality into the kvm_hv kernel module. When a vcore is idle it will poll for some period of time before scheduling itself out.

When all of the runnable vcpus on a vcore have ceded (and thus the vcore is idle) we schedule ourselves out to allow something else to run. In the event that we need to wake up very quickly (for example an interrupt arrives), we are required to wait until we get scheduled again.

Implement halt polling so that when a vcore is idle, and before scheduling ourselves, we poll for vcpus in the runnable_threads list which have pending exceptions or which leave the ceded state. If we poll successfully then we can get back into the guest very quickly without ever scheduling ourselves, otherwise we schedule ourselves out as before.

There exists generic halt_polling code in virt/kvm_main.c, however on powerpc the polling conditions are different to the generic case. It would be nice if we could just implement an arch specific kvm_check_block() function, but there is still other arch specific things which need to be done for kvm_hv (for example manipulating vcore states) which means that a separate implementation is the best option.

Testing of this patch with a TCP round robin test between two guests with virtio network interfaces has found a decrease in round trip time of ~15us on average. A performance gain is only seen when going out of and back into the guest often and quickly, otherwise there is no net benefit from the polling. The polling interval is adjusted such that when we are often scheduled out for long periods of time it is reduced, and when we often poll successfully it is increased. The rate at which the polling interval increases or decreases, and the maximum polling interval, can be set through module parameters.

Based on the implementation in the generic kvm module by Wanpeng Li and Paolo Bonzini, and on direction from Paul Mackerras.

---
 arch/powerpc/include/asm/kvm_book3s.h |   1 +
 arch/powerpc/include/asm/kvm_host.h   |   1 +
 arch/powerpc/kvm/book3s_hv.c          | 116 ++++++++++++++++++++++++++++++----
 arch/powerpc/kvm/trace_hv.h           |  22 +++++++
 4 files changed, 126 insertions(+), 14 deletions(-)
arch/powerpc/include/asm/kvm_book3s.h +1 -0
index 151f817..c261f52 100644
@@ -102,6 +102,7 @@ struct kvmppc_vcore {
 	ulong pcr;
 	ulong dpdes;		/* doorbell state (POWER8) */
 	ulong conferring_threads;
+	unsigned int halt_poll_ns;
 };
 
 struct kvmppc_vcpu_book3s {
arch/powerpc/include/asm/kvm_host.h +1 -0
index 02d06e9..610f393 100644
@@ -294,6 +294,7 @@ struct kvm_arch {
 #define VCORE_SLEEPING	3
 #define VCORE_RUNNING	4
 #define VCORE_EXITING	5
+#define VCORE_POLLING	6
 
 /*
  * Struct used to manage memory for a virtual processor area
arch/powerpc/kvm/book3s_hv.c +102 -14

146 lines not on this page · raw message

arch/powerpc/kvm/trace_hv.h +22 -0
index 33d9daf..fb21990 100644
@@ -432,6 +432,28 @@ TRACE_EVENT(kvmppc_vcore_blocked,
 		   __entry->runner_vcpu, __entry->n_runnable, __entry->tgid)
 );
 
+TRACE_EVENT(kvmppc_vcore_wakeup,
+	TP_PROTO(int do_sleep, __u64 ns),
+
+	TP_ARGS(do_sleep, ns),
+
+	TP_STRUCT__entry(
+		__field(__u64,  ns)
+		__field(int,    waited)
+		__field(pid_t,  tgid)
+	),
+
+	TP_fast_assign(
+		__entry->ns     = ns;
+		__entry->waited = do_sleep;
+		__entry->tgid   = current->tgid;
+	),
+
+	TP_printk("%s time %lld ns, tgid=%d",
+		__entry->waited ? "wait" : "poll",
+		__entry->ns, __entry->tgid)
+);
+
 TRACE_EVENT(kvmppc_run_vcpu_enter,
 	TP_PROTO(struct kvm_vcpu *vcpu),
 
Signature · 1 line
2.5.5
#3 lore raw

PATCH v5 4/5 kvm/stats: Add provisioning for ulong vm stats and u64 vcpu stats

In reply to Suraj Jitindar Singh

vms and vcpus have statistics associated with them which can be viewed within the debugfs. Currently it is assumed within the vcpu_stat_get() and vm_stat_get() functions that all of these statistics are represented as u32s, however the next patch adds some u64 vcpu statistics.

Change all vcpu statistics to u64 and modify vcpu_stat_get() accordingly. Since vcpu statistics are per vcpu, they will only be updated by a single vcpu at a time so this shouldn't present a problem on 32-bit machines which can't atomically increment 64-bit numbers. However vm statistics could potentially be updated by multiple vcpus from that vm at a time. To avoid the overhead of atomics make all vm statistics ulong such that they are 64-bit on 64-bit systems where they can be atomically incremented and are 32-bit on 32-bit systems which may not be able to atomically increment 64-bit numbers. Modify vm_stat_get() to expect ulongs.

---
Change Log:

V2 -> V3:
- Instead of implementing separate u32 and u64 functions keep the generic functions and modify them to expect u64s. Thus update all vm and vcpu statistics to u64s accordingly.
V3 -> V4:
- Change vm_stats from u64 to ulong

---
 arch/arm/include/asm/kvm_host.h     |  12 ++--
 arch/arm64/include/asm/kvm_host.h   |  12 ++--
 arch/mips/include/asm/kvm_host.h    |  46 ++++++-------
 arch/powerpc/include/asm/kvm_host.h |  60 ++++++++---------
 arch/s390/include/asm/kvm_host.h    | 128 ++++++++++++++++++------------------
 arch/x86/include/asm/kvm_host.h     |  72 ++++++++++----------
 virt/kvm/kvm_main.c                 |   4 +-
 7 files changed, 167 insertions(+), 167 deletions(-)
arch/arm/include/asm/kvm_host.h +6 -6
index 96387d4..c8e55b3b 100644
@@ -183,15 +183,15 @@ struct kvm_vcpu_arch {
 };
 
 struct kvm_vm_stat {
-	u32 remote_tlb_flush;
+	ulong remote_tlb_flush;
 };
 
 struct kvm_vcpu_stat {
-	u32 halt_successful_poll;
-	u32 halt_attempted_poll;
-	u32 halt_poll_invalid;
-	u32 halt_wakeup;
-	u32 hvc_exit_stat;
+	u64 halt_successful_poll;
+	u64 halt_attempted_poll;
+	u64 halt_poll_invalid;
+	u64 halt_wakeup;
+	u64 hvc_exit_stat;
 	u64 wfe_exit_stat;
 	u64 wfi_exit_stat;
 	u64 mmio_exit_user;
arch/arm64/include/asm/kvm_host.h +6 -6
index 49095fc..b14c8bc 100644
@@ -291,15 +291,15 @@ struct kvm_vcpu_arch {
 #endif
 
 struct kvm_vm_stat {
-	u32 remote_tlb_flush;
+	ulong remote_tlb_flush;
 };
 
 struct kvm_vcpu_stat {
-	u32 halt_successful_poll;
-	u32 halt_attempted_poll;
-	u32 halt_poll_invalid;
-	u32 halt_wakeup;
-	u32 hvc_exit_stat;
+	u64 halt_successful_poll;
+	u64 halt_attempted_poll;
+	u64 halt_poll_invalid;
+	u64 halt_wakeup;
+	u64 hvc_exit_stat;
 	u64 wfe_exit_stat;
 	u64 wfi_exit_stat;
 	u64 mmio_exit_user;
arch/mips/include/asm/kvm_host.h +23 -23

55 lines not on this page · raw message

arch/powerpc/include/asm/kvm_host.h +30 -30

71 lines not on this page · raw message

arch/s390/include/asm/kvm_host.h +64 -64

140 lines not on this page · raw message

arch/x86/include/asm/kvm_host.h +36 -36

81 lines not on this page · raw message

virt/kvm/kvm_main.c +2 -2
index 48bd520..37a6530 100644
@@ -3531,7 +3531,7 @@ static int vm_stat_get_per_vm(void *data, u64 *val)
 {
 	struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
 
-	*val = *(u32 *)((void *)stat_data->kvm + stat_data->offset);
+	*val = *(ulong *)((void *)stat_data->kvm + stat_data->offset);
 
 	return 0;
 }
@@ -3561,7 +3561,7 @@ static int vcpu_stat_get_per_vm(void *data, u64 *val)
 	*val = 0;
 
 	kvm_for_each_vcpu(i, vcpu, stat_data->kvm)
-		*val += *(u32 *)((void *)vcpu + stat_data->offset);
+		*val += *(u64 *)((void *)vcpu + stat_data->offset);
 
 	return 0;
 }
Signature · 1 line
2.5.5
#4 lore raw

PATCH v5 5/5 powerpc/kvm/stats: Implement existing and add new halt polling vcpu stats

In reply to Suraj Jitindar Singh

vcpu stats are used to collect information about a vcpu which can be viewed in the debugfs. For example halt_attempted_poll and halt_successful_poll are used to keep track of the number of times the vcpu attempts to and successfully polls. These stats are currently not used on powerpc.

Implement incrementation of the halt_attempted_poll and halt_successful_poll vcpu stats for powerpc. Since these stats are summed over all the vcpus for all running guests it doesn't matter which vcpu they are attributed to, thus we choose the current runner vcpu of the vcore.

Also add new vcpu stats: halt_poll_success_ns, halt_poll_fail_ns and halt_wait_ns to be used to accumulate the total time spend polling successfully, polling unsuccessfully and waiting respectively, and halt_successful_wait to accumulate the number of times the vcpu waits. Given that halt_poll_success_ns, halt_poll_fail_ns and halt_wait_ns are expressed in nanoseconds it is necessary to represent these as 64-bit quantities, otherwise they would overflow after only about 4 seconds.

Given that the total time spend either polling or waiting will be known and the number of times that each was done, it will be possible to determine the average poll and wait times. This will give the ability to tune the kvm module parameters based on the calculated average wait and poll times.

---
Change Log:

V3 -> V4:
- Instead of accounting just wait and poll time, separate these into successful_poll_time, failed_poll_time and wait_time.
V4 -> V5:
- Add single_task_running() check to polling loop

---
 arch/powerpc/include/asm/kvm_host.h |  4 ++++
 arch/powerpc/kvm/book3s.c           |  4 ++++
 arch/powerpc/kvm/book3s_hv.c        | 38 +++++++++++++++++++++++++++++++------
 3 files changed, 40 insertions(+), 6 deletions(-)
arch/powerpc/include/asm/kvm_host.h +4 -0
index f6304c5..f15ffc0 100644
@@ -114,8 +114,12 @@ struct kvm_vcpu_stat {
 	u64 emulated_inst_exits;
 	u64 dec_exits;
 	u64 ext_intr_exits;
+	u64 halt_poll_success_ns;
+	u64 halt_poll_fail_ns;
+	u64 halt_wait_ns;
 	u64 halt_successful_poll;
 	u64 halt_attempted_poll;
+	u64 halt_successful_wait;
 	u64 halt_poll_invalid;
 	u64 halt_wakeup;
 	u64 dbell_exits;
arch/powerpc/kvm/book3s.c +4 -0
index 47018fc..71eb8f3 100644
@@ -52,8 +52,12 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
 	{ "dec",         VCPU_STAT(dec_exits) },
 	{ "ext_intr",    VCPU_STAT(ext_intr_exits) },
 	{ "queue_intr",  VCPU_STAT(queue_intr) },
+	{ "halt_poll_success_ns",	VCPU_STAT(halt_poll_success_ns) },
+	{ "halt_poll_fail_ns",		VCPU_STAT(halt_poll_fail_ns) },
+	{ "halt_wait_ns",		VCPU_STAT(halt_wait_ns) },
 	{ "halt_successful_poll", VCPU_STAT(halt_successful_poll), },
 	{ "halt_attempted_poll", VCPU_STAT(halt_attempted_poll), },
+	{ "halt_successful_wait",	VCPU_STAT(halt_successful_wait) },
 	{ "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
 	{ "halt_wakeup", VCPU_STAT(halt_wakeup) },
 	{ "pf_storage",  VCPU_STAT(pf_storage) },
arch/powerpc/kvm/book3s_hv.c +29 -6
index a9de1d4..b1d9e88 100644
@@ -2679,15 +2679,16 @@ static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
  */
 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
 {
+	ktime_t cur, start_poll, start_wait;
 	int do_sleep = 1;
-	ktime_t cur, start;
 	u64 block_ns;
 	DECLARE_SWAITQUEUE(wait);
 
 	/* Poll for pending exceptions and ceded state */
-	cur = start = ktime_get();
+	cur = start_poll = ktime_get();
 	if (vc->halt_poll_ns) {
-		ktime_t stop = ktime_add_ns(start, vc->halt_poll_ns);
+		ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
+		++vc->runner->stat.halt_attempted_poll;
 
 		vc->vcore_state = VCORE_POLLING;
 		spin_unlock(&vc->lock);
@@ -2698,13 +2699,15 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
 				break;
 			}
 			cur = ktime_get();
-		} while (ktime_before(cur, stop));
+		} while (single_task_running() && ktime_before(cur, stop));
 
 		spin_lock(&vc->lock);
 		vc->vcore_state = VCORE_INACTIVE;
 
-		if (!do_sleep)
+		if (!do_sleep) {
+			++vc->runner->stat.halt_successful_poll;
 			goto out;
+		}
 	}
 
 	prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
@@ -2712,9 +2715,14 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
 	if (kvmppc_vcore_check_block(vc)) {
 		finish_swait(&vc->wq, &wait);
 		do_sleep = 0;
+		/* If we polled, count this as a successful poll */
+		if (vc->halt_poll_ns)
+			++vc->runner->stat.halt_successful_poll;
 		goto out;
 	}
 
+	start_wait = ktime_get();
+
 	vc->vcore_state = VCORE_SLEEPING;
 	trace_kvmppc_vcore_blocked(vc, 0);
 	spin_unlock(&vc->lock);
@@ -2723,11 +2731,29 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
 	spin_lock(&vc->lock);
 	vc->vcore_state = VCORE_INACTIVE;
 	trace_kvmppc_vcore_blocked(vc, 1);
+	++vc->runner->stat.halt_successful_wait;
 
 	cur = ktime_get();
 
 out:
-	block_ns = ktime_to_ns(cur) - ktime_to_ns(start);
+	block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
+
+	/* Attribute wait time */
+	if (do_sleep) {
+		vc->runner->stat.halt_wait_ns ++			ktime_to_ns(cur) - ktime_to_ns(start_wait);
+		/* Attribute failed poll time */
+		if (vc->halt_poll_ns)
+			vc->runner->stat.halt_poll_fail_ns ++				ktime_to_ns(start_wait) -
+				ktime_to_ns(start_poll);
+	} else {
+		/* Attribute successful poll time */
+		if (vc->halt_poll_ns)
+			vc->runner->stat.halt_poll_success_ns ++				ktime_to_ns(cur) -
+				ktime_to_ns(start_poll);
+	}
 
 	/* Adjust poll time */
 	if (halt_poll_max_ns) {
Signature · 1 line
2.5.5
#5 lore raw

Re: [PATCH V5 5/5] powerpc/kvm/stats: Implement existing and add new halt polling vcpu stats

Quoted 27 lines · Suraj Jitindar Singh

vcpu stats are used to collect information about a vcpu which can be viewed in the debugfs. For example halt_attempted_poll and halt_successful_poll are used to keep track of the number of times the vcpu attempts to and successfully polls. These stats are currently not used on powerpc.

Implement incrementation of the halt_attempted_poll and halt_successful_poll vcpu stats for powerpc. Since these stats are summed over all the vcpus for all running guests it doesn't matter which vcpu they are attributed to, thus we choose the current runner vcpu of the vcore.

Also add new vcpu stats: halt_poll_success_ns, halt_poll_fail_ns and halt_wait_ns to be used to accumulate the total time spend polling successfully, polling unsuccessfully and waiting respectively, and halt_successful_wait to accumulate the number of times the vcpu waits. Given that halt_poll_success_ns, halt_poll_fail_ns and halt_wait_ns are expressed in nanoseconds it is necessary to represent these as 64-bit quantities, otherwise they would overflow after only about 4 seconds.

Given that the total time spend either polling or waiting will be known and the number of times that each was done, it will be possible to determine the average poll and wait times. This will give the ability to tune the kvm module parameters based on the calculated average wait and poll times.

Quoted 8 lines

---
Change Log:

V3 -> V4:
- Instead of accounting just wait and poll time, separate these into successful_poll_time, failed_poll_time and wait_time.
V4 -> V5:
- Add single_task_running() check to polling loop

I was expecting to see this in PATCH 3/5 with the halt-polling implementation. But otherwise, looks good, and the net effect is the same.

Quoted 128 lines
---
 arch/powerpc/include/asm/kvm_host.h |  4 ++++
 arch/powerpc/kvm/book3s.c           |  4 ++++
 arch/powerpc/kvm/book3s_hv.c        | 38 +++++++++++++++++++++++++++++++------
 3 files changed, 40 insertions(+), 6 deletions(-)
arch/powerpc/include/asm/kvm_host.h +4 -0
index f6304c5..f15ffc0 100644
@@ -114,8 +114,12 @@ struct kvm_vcpu_stat {
        u64 emulated_inst_exits;
        u64 dec_exits;
        u64 ext_intr_exits;
+       u64 halt_poll_success_ns;
+       u64 halt_poll_fail_ns;
+       u64 halt_wait_ns;
        u64 halt_successful_poll;
        u64 halt_attempted_poll;
+       u64 halt_successful_wait;
        u64 halt_poll_invalid;
        u64 halt_wakeup;
        u64 dbell_exits;
arch/powerpc/kvm/book3s.c +4 -0
index 47018fc..71eb8f3 100644
@@ -52,8 +52,12 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
        { "dec",         VCPU_STAT(dec_exits) },
        { "ext_intr",    VCPU_STAT(ext_intr_exits) },
        { "queue_intr",  VCPU_STAT(queue_intr) },
+       { "halt_poll_success_ns",       VCPU_STAT(halt_poll_success_ns) },
+       { "halt_poll_fail_ns",          VCPU_STAT(halt_poll_fail_ns) },
+       { "halt_wait_ns",               VCPU_STAT(halt_wait_ns) },
        { "halt_successful_poll", VCPU_STAT(halt_successful_poll), },
        { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll), },
+       { "halt_successful_wait",       VCPU_STAT(halt_successful_wait) },
        { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
        { "halt_wakeup", VCPU_STAT(halt_wakeup) },
        { "pf_storage",  VCPU_STAT(pf_storage) },
arch/powerpc/kvm/book3s_hv.c +29 -7
index a9de1d4..b1d9e88 100644
@@ -2679,15 +2679,16 @@ static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
  */
 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
 {
+       ktime_t cur, start_poll, start_wait;
        int do_sleep = 1;
-       ktime_t cur, start;
        u64 block_ns;
        DECLARE_SWAITQUEUE(wait);
 
        /* Poll for pending exceptions and ceded state */
-       cur = start = ktime_get();
+       cur = start_poll = ktime_get();
        if (vc->halt_poll_ns) {
-               ktime_t stop = ktime_add_ns(start, vc->halt_poll_ns);
+               ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
+               ++vc->runner->stat.halt_attempted_poll;
 
                vc->vcore_state = VCORE_POLLING;
                spin_unlock(&vc->lock);
@@ -2698,13 +2699,15 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
                                break;
                        }
                        cur = ktime_get();
-               } while (ktime_before(cur, stop));
+               } while (single_task_running() && ktime_before(cur, stop));
 
                spin_lock(&vc->lock);
                vc->vcore_state = VCORE_INACTIVE;
 
-               if (!do_sleep)
+               if (!do_sleep) {
+                       ++vc->runner->stat.halt_successful_poll;
                        goto out;
+               }
        }
 
        prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
@@ -2712,9 +2715,14 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
        if (kvmppc_vcore_check_block(vc)) {
                finish_swait(&vc->wq, &wait);
                do_sleep = 0;
+               /* If we polled, count this as a successful poll */
+               if (vc->halt_poll_ns)
+                       ++vc->runner->stat.halt_successful_poll;
                goto out;
        }
 
+       start_wait = ktime_get();
+
        vc->vcore_state = VCORE_SLEEPING;
        trace_kvmppc_vcore_blocked(vc, 0);
        spin_unlock(&vc->lock);
@@ -2723,11 +2731,29 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
        spin_lock(&vc->lock);
        vc->vcore_state = VCORE_INACTIVE;
        trace_kvmppc_vcore_blocked(vc, 1);
+       ++vc->runner->stat.halt_successful_wait;
 
        cur = ktime_get();
 
 out:
-       block_ns = ktime_to_ns(cur) - ktime_to_ns(start);
+       block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
+
+       /* Attribute wait time */
+       if (do_sleep) {
+               vc->runner->stat.halt_wait_ns +> +                       ktime_to_ns(cur) - ktime_to_ns(start_wait);
+               /* Attribute failed poll time */
+               if (vc->halt_poll_ns)
+                       vc->runner->stat.halt_poll_fail_ns +> +                               ktime_to_ns(start_wait) -
+                               ktime_to_ns(start_poll);
+       } else {
+               /* Attribute successful poll time */
+               if (vc->halt_poll_ns)
+                       vc->runner->stat.halt_poll_success_ns +> +                               ktime_to_ns(cur) -
+                               ktime_to_ns(start_poll);
+       }
 
        /* Adjust poll time */
        if (halt_poll_max_ns) {
--

2.5.5

#6 lore raw

Re: [PATCH V5 5/5] powerpc/kvm/stats: Implement existing and add new halt polling vcpu stats

Quoted 32 lines · David Matlack
Quoted 30 lines · Suraj Jitindar Singh

vcpu stats are used to collect information about a vcpu which can be viewed in the debugfs. For example halt_attempted_poll and halt_successful_poll are used to keep track of the number of times the vcpu attempts to and successfully polls. These stats are currently not used on powerpc.

Implement incrementation of the halt_attempted_poll and halt_successful_poll vcpu stats for powerpc. Since these stats are summed over all the vcpus for all running guests it doesn't matter which vcpu they are attributed to, thus we choose the current runner vcpu of the vcore.

Also add new vcpu stats: halt_poll_success_ns, halt_poll_fail_ns and halt_wait_ns to be used to accumulate the total time spend polling successfully, polling unsuccessfully and waiting respectively, and halt_successful_wait to accumulate the number of times the vcpu waits. Given that halt_poll_success_ns, halt_poll_fail_ns and halt_wait_ns are expressed in nanoseconds it is necessary to represent these as 64-bit quantities, otherwise they would overflow after only about 4 seconds.

Given that the total time spend either polling or waiting will be known and the number of times that each was done, it will be possible to determine the average poll and wait times. This will give the ability to tune the kvm module parameters based on the calculated average wait and poll times.

---
Change Log:

Quoted 8 lines
Quoted 5 lines

V3 -> V4:
- Instead of accounting just wait and poll time, separate these into successful_poll_time, failed_poll_time and wait_time.
V4 -> V5:
- Add single_task_running() check to polling loop

I was expecting to see this in PATCH 3/5 with the halt-polling implementation. But otherwise, looks good, and the net effect is the same

That would have made more sense, obviously that was my Friday afternoon brain.

Quoted 130 lines

.

Quoted 128 lines
---
 arch/powerpc/include/asm/kvm_host.h |  4 ++++
 arch/powerpc/kvm/book3s.c           |  4 ++++
 arch/powerpc/kvm/book3s_hv.c        | 38 +++++++++++++++++++++++++++++++------
 3 files changed, 40 insertions(+), 6 deletions(-)
arch/powerpc/include/asm/kvm_host.h +4 -0
index f6304c5..f15ffc0 100644
@@ -114,8 +114,12 @@ struct kvm_vcpu_stat {
        u64 emulated_inst_exits;
        u64 dec_exits;
        u64 ext_intr_exits;
+       u64 halt_poll_success_ns;
+       u64 halt_poll_fail_ns;
+       u64 halt_wait_ns;
        u64 halt_successful_poll;
        u64 halt_attempted_poll;
+       u64 halt_successful_wait;
        u64 halt_poll_invalid;
        u64 halt_wakeup;
        u64 dbell_exits;
arch/powerpc/kvm/book3s.c +4 -0
index 47018fc..71eb8f3 100644
@@ -52,8 +52,12 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
        { "dec",         VCPU_STAT(dec_exits) },
        { "ext_intr",    VCPU_STAT(ext_intr_exits) },
        { "queue_intr",  VCPU_STAT(queue_intr) },
+       { "halt_poll_success_ns",       VCPU_STAT(halt_poll_success_ns) },
+       { "halt_poll_fail_ns",          VCPU_STAT(halt_poll_fail_ns) },
+       { "halt_wait_ns",               VCPU_STAT(halt_wait_ns) },
        { "halt_successful_poll", VCPU_STAT(halt_successful_poll), },
        { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll), },
+       { "halt_successful_wait",       VCPU_STAT(halt_successful_wait) },
        { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
        { "halt_wakeup", VCPU_STAT(halt_wakeup) },
        { "pf_storage",  VCPU_STAT(pf_storage) },
arch/powerpc/kvm/book3s_hv.c +29 -7
index a9de1d4..b1d9e88 100644
@@ -2679,15 +2679,16 @@ static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
  */
 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
 {
+       ktime_t cur, start_poll, start_wait;
        int do_sleep = 1;
-       ktime_t cur, start;
        u64 block_ns;
        DECLARE_SWAITQUEUE(wait);
 
        /* Poll for pending exceptions and ceded state */
-       cur = start = ktime_get();
+       cur = start_poll = ktime_get();
        if (vc->halt_poll_ns) {
-               ktime_t stop = ktime_add_ns(start, vc->halt_poll_ns);
+               ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
+               ++vc->runner->stat.halt_attempted_poll;
 
                vc->vcore_state = VCORE_POLLING;
                spin_unlock(&vc->lock);
@@ -2698,13 +2699,15 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
                                break;
                        }
                        cur = ktime_get();
-               } while (ktime_before(cur, stop));
+               } while (single_task_running() && ktime_before(cur, stop));
 
                spin_lock(&vc->lock);
                vc->vcore_state = VCORE_INACTIVE;
 
-               if (!do_sleep)
+               if (!do_sleep) {
+                       ++vc->runner->stat.halt_successful_poll;
                        goto out;
+               }
        }
 
        prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
@@ -2712,9 +2715,14 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
        if (kvmppc_vcore_check_block(vc)) {
                finish_swait(&vc->wq, &wait);
                do_sleep = 0;
+               /* If we polled, count this as a successful poll */
+               if (vc->halt_poll_ns)
+                       ++vc->runner->stat.halt_successful_poll;
                goto out;
        }
 
+       start_wait = ktime_get();
+
        vc->vcore_state = VCORE_SLEEPING;
        trace_kvmppc_vcore_blocked(vc, 0);
        spin_unlock(&vc->lock);
@@ -2723,11 +2731,29 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
        spin_lock(&vc->lock);
        vc->vcore_state = VCORE_INACTIVE;
        trace_kvmppc_vcore_blocked(vc, 1);
+       ++vc->runner->stat.halt_successful_wait;
 
        cur = ktime_get();
 
 out:
-       block_ns = ktime_to_ns(cur) - ktime_to_ns(start);
+       block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
+
+       /* Attribute wait time */
+       if (do_sleep) {
+               vc->runner->stat.halt_wait_ns +>> +                       ktime_to_ns(cur) - ktime_to_ns(start_wait);
+               /* Attribute failed poll time */
+               if (vc->halt_poll_ns)
+                       vc->runner->stat.halt_poll_fail_ns +>> +                               ktime_to_ns(start_wait) -
+                               ktime_to_ns(start_poll);
+       } else {
+               /* Attribute successful poll time */
+               if (vc->halt_poll_ns)
+                       vc->runner->stat.halt_poll_success_ns +>> +                               ktime_to_ns(cur) -
+                               ktime_to_ns(start_poll);
+       }
 
        /* Adjust poll time */
        if (halt_poll_max_ns) {
--

2.5.5