Skip to contentSkip to conversation

powerpc: stolen time accounting for VIRT_CPU_ACCOUNTING_GEN

PATCH Applied Nicholas Piggin 3 participants 7 messages Sep 2, 2022 – Oct 10, 2022

Subsystems (3)
lore
Share & tools
lore

Nicholas Piggin

author reviewer
#0 lore raw

v2 cover powerpc: stolen time accounting for VIRT_CPU_ACCOUNTING_GEN

pseries provides stolen time accounting when VIRT_CPU_ACCOUNTING_NATIVE is selected, but not when VIRT_CPU_ACCOUNTING_GEN is. We like GEN because it's less code in arch/powerpc, allows full nohz, and distros have moved to it, so this series adds stolen time accounting for GEN, and moves our pseries configs over to it.

Thanks,
Nick

Since v1:
- Move the KVM patches out of this series to make it smaller. I'll post them separately.
- Fix compilation bug in patch 2 due to missing header in patch.
- Add defconfig changes to patch 3.
- Add tidy up patch 4.
- Improve changelogs.

Nicholas Piggin (4):
powerpc/pseries: Add wait interval counter definitions to struct
lppaca
powerpc/pseries: Implement CONFIG_PARAVIRT_TIME_ACCOUNTING powerpc/64: Remove PPC64 special case for cputime accounting default powerpc/pseries: Move dtl scanning and steal time accounting to
pseries platform

 .../admin-guide/kernel-parameters.txt         |  6 +-
 arch/powerpc/configs/ppc64_defconfig          |  2 +
 arch/powerpc/configs/pseries_defconfig        |  2 +
 arch/powerpc/include/asm/cputime.h            |  2 +-
 arch/powerpc/include/asm/dtl.h                |  8 --
 arch/powerpc/include/asm/lppaca.h             | 10 +-
 arch/powerpc/include/asm/paravirt.h           | 12 +++
 arch/powerpc/include/asm/paravirt_api_clock.h |  1 +
 arch/powerpc/include/asm/time.h               |  5 +-
 arch/powerpc/kernel/time.c                    | 92 +------------------
 arch/powerpc/platforms/pseries/Kconfig        |  8 ++
 arch/powerpc/platforms/pseries/dtl.c          | 81 ++++++++++++++++
 arch/powerpc/platforms/pseries/lpar.c         | 11 +++
 arch/powerpc/platforms/pseries/setup.c        | 19 ++++
 init/Kconfig                                  |  3 +-
 15 files changed, 156 insertions(+), 106 deletions(-)
 create mode 100644 arch/powerpc/include/asm/paravirt_api_clock.h
Signature · 1 line
2.37.2
author reviewer
#1 lore raw

PATCH v2 1/4 powerpc/pseries: Add wait interval counter definitions to struct lppaca

The hypervisor exposes accumulated partition scheduling interval times in the VPA (lppaca). These can be used to implement a simple stolen time in the guest without complex and costly dtl scanning.

---
 arch/powerpc/include/asm/lppaca.h | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/lppaca.h +7 -3
index c390ec377bae..34d44cb17c87 100644
@@ -104,14 +104,18 @@ struct lppaca {
 	volatile __be32 dispersion_count; /* dispatch changed physical cpu */
 	volatile __be64 cmo_faults;	/* CMO page fault count */
 	volatile __be64 cmo_fault_time;	/* CMO page fault time */
-	u8	reserved10[104];
+	u8	reserved10[64];		/* [S]PURR expropriated/donated */
+	volatile __be64 enqueue_dispatch_tb; /* Total TB enqueue->dispatch */
+	volatile __be64 ready_enqueue_tb; /* Total TB ready->enqueue */
+	volatile __be64 wait_ready_tb;	/* Total TB wait->ready */
+	u8	reserved11[16];
 
 	/* cacheline 4-5 */
 
 	__be32	page_ins;		/* CMO Hint - # page ins by OS */
-	u8	reserved11[148];
+	u8	reserved12[148];
 	volatile __be64 dtl_idx;	/* Dispatch Trace Log head index */
-	u8	reserved12[96];
+	u8	reserved13[96];
 } ____cacheline_aligned;
 
 #define lppaca_of(cpu)	(*paca_ptrs[cpu]->lppaca_ptr)
Signature · 1 line
2.37.2
author reviewer
#2 lore raw

PATCH v2 2/4 powerpc/pseries: Implement CONFIG_PARAVIRT_TIME_ACCOUNTING

In reply to Nicholas Piggin

CONFIG_VIRT_CPU_ACCOUNTING_GEN under pseries does not provide stolen time accounting unless CONFIG_PARAVIRT_TIME_ACCOUNTING is enabled. Implement this using the VPA accumulated wait counters.

Note this will not work on current KVM hosts because KVM does not implement the VPA dispatch counters (yet). It could be implemented with the dispatch trace log as it is for VIRT_CPU_ACCOUNTING_NATIVE, but that is not necessary for the more limited accounting provided by PARAVIRT_TIME_ACCOUNTING, and it is more expensive, complex, and has downsides like potential log wrap.

From Shrikanth:

[...] it was tested on Power10 [PowerVM] Shared LPAR. system has two LPAR. we will call first one LPAR1 and second one as LPAR2. Test was carried out in SMT=1. Similar observation was seen in SMT=8 as well.

LPAR config header from each LPAR is below. LPAR1 is twice as big as LPAR2. Since Both are sharing the same underlying hardware, work stealing will happen when both the LPAR's are contending for the same resource.

LPAR1:
type=Shared mode=Uncapped smt=Off lcpu=40 cpus=40 ent=20.00 LPAR2:
type=Shared mode=Uncapped smt=Off lcpu=20 cpus=40 ent=10.00

mpstat was used to check for the utilization. stress-ng has been used as the workload. Few cases are tested. when the both LPAR are idle there is no steal time. when LPAR1 starts running at 100% which consumes all of the physical resource, steal time starts to get accounted. With LPAR1 running at 100% and LPAR2 starts running, steal time starts increasing. This is as expected. When the LPAR2 Load is increased further, steal time increases further.

Case 1: 0% LPAR1; 0% LPAR2

   %usr  %nice   %sys %iowait  %irq  %soft %steal %guest %gnice  %idle
   0.00   0.00   0.05   0.00   0.00   0.00   0.00   0.00   0.00  99.95

Case 2: 100% LPAR1; 0% LPAR2

   %usr  %nice   %sys %iowait  %irq  %soft %steal %guest %gnice  %idle
  97.68   0.00   0.00   0.00   0.00   0.00   2.32   0.00   0.00   0.00

Case 3: 100% LPAR1; 50% LPAR2

   %usr  %nice   %sys %iowait  %irq  %soft %steal %guest %gnice  %idle
  86.34   0.00   0.10   0.00   0.00   0.03  13.54   0.00   0.00   0.00

Case 4: 100% LPAR1; 100% LPAR2

   %usr  %nice   %sys %iowait  %irq  %soft %steal %guest %gnice  %idle
  78.54   0.00   0.07   0.00   0.00   0.02  21.36   0.00   0.00   0.00

Case 5: 50% LPAR1; 100% LPAR2

   %usr  %nice   %sys %iowait  %irq  %soft %steal %guest %gnice  %idle
  49.37   0.00   0.00   0.00   0.00   0.00   1.17   0.00   0.00  49.47

Patch is accounting for the steal time and basic tests are holding good.

---
 .../admin-guide/kernel-parameters.txt         |  6 +++---
 arch/powerpc/include/asm/paravirt.h           | 12 ++++++++++++
 arch/powerpc/include/asm/paravirt_api_clock.h |  1 +
 arch/powerpc/platforms/pseries/Kconfig        |  8 ++++++++
 arch/powerpc/platforms/pseries/lpar.c         | 11 +++++++++++
 arch/powerpc/platforms/pseries/setup.c        | 19 +++++++++++++++++++
 6 files changed, 54 insertions(+), 3 deletions(-)
 create mode 100644 arch/powerpc/include/asm/paravirt_api_clock.h
Documentation/admin-guide/kernel-parameters.txt +3 -3
index 426fa892d311..7172a91539f2 100644
@@ -3741,9 +3741,9 @@
 			[X86,PV_OPS] Disable paravirtualized VMware scheduler
 			clock and use the default one.
 
-	no-steal-acc	[X86,PV_OPS,ARM64] Disable paravirtualized steal time
-			accounting. steal time is computed, but won't
-			influence scheduler behaviour
+	no-steal-acc	[X86,PV_OPS,ARM64,PPC/PSERIES] Disable paravirtualized
+			steal time accounting. steal time is computed, but
+			won't influence scheduler behaviour
 
 	nolapic		[X86-32,APIC] Do not enable or use the local APIC.
 
arch/powerpc/include/asm/paravirt.h +12 -0
index eb7df559ae74..f5ba1a3c41f8 100644
@@ -21,6 +21,18 @@ static inline bool is_shared_processor(void)
 	return static_branch_unlikely(&shared_processor);
 }
 
+#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
+extern struct static_key paravirt_steal_enabled;
+extern struct static_key paravirt_steal_rq_enabled;
+
+u64 pseries_paravirt_steal_clock(int cpu);
+
+static inline u64 paravirt_steal_clock(int cpu)
+{
+	return pseries_paravirt_steal_clock(cpu);
+}
+#endif
+
 /* If bit 0 is set, the cpu has been ceded, conferred, or preempted */
 static inline u32 yield_count_of(int cpu)
 {
arch/powerpc/include/asm/paravirt_api_clock.h new file +1 -0
new file mode 100644
index 000000000000..65ac7cee0dad
@@ -0,0 +1 @@
+#include <asm/paravirt.h>
arch/powerpc/platforms/pseries/Kconfig +8 -0
index fb6499977f99..a3b4d99567cb 100644
@@ -23,13 +23,21 @@ config PPC_PSERIES
 	select SWIOTLB
 	default y
 
+config PARAVIRT
+	bool
+
 config PARAVIRT_SPINLOCKS
 	bool
 
+config PARAVIRT_TIME_ACCOUNTING
+	select PARAVIRT
+	bool
+
 config PPC_SPLPAR
 	bool "Support for shared-processor logical partitions"
 	depends on PPC_PSERIES
 	select PARAVIRT_SPINLOCKS if PPC_QUEUED_SPINLOCKS
+	select PARAVIRT_TIME_ACCOUNTING if VIRT_CPU_ACCOUNTING_GEN
 	default y
 	help
 	  Enabling this option will make the kernel run more efficiently
arch/powerpc/platforms/pseries/lpar.c +11 -0
index e6c117fb6491..97ef6499e501 100644
@@ -660,6 +660,17 @@ static int __init vcpudispatch_stats_procfs_init(void)
 }
 
 machine_device_initcall(pseries, vcpudispatch_stats_procfs_init);
+
+#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
+u64 pseries_paravirt_steal_clock(int cpu)
+{
+	struct lppaca *lppaca = &lppaca_of(cpu);
+
+	return be64_to_cpu(READ_ONCE(lppaca->enqueue_dispatch_tb)) +
+		be64_to_cpu(READ_ONCE(lppaca->ready_enqueue_tb));
+}
+#endif
+
 #endif /* CONFIG_PPC_SPLPAR */
 
 void vpa_init(int cpu)
arch/powerpc/platforms/pseries/setup.c +19 -0
index 489f4c4df468..5e44c65a032c 100644
@@ -80,6 +80,20 @@
 DEFINE_STATIC_KEY_FALSE(shared_processor);
 EXPORT_SYMBOL(shared_processor);
 
+#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
+struct static_key paravirt_steal_enabled;
+struct static_key paravirt_steal_rq_enabled;
+
+static bool steal_acc = true;
+static int __init parse_no_stealacc(char *arg)
+{
+	steal_acc = false;
+	return 0;
+}
+
+early_param("no-steal-acc", parse_no_stealacc);
+#endif
+
 int CMO_PrPSP = -1;
 int CMO_SecPSP = -1;
 unsigned long CMO_PageSize = (ASM_CONST(1) << IOMMU_PAGE_SHIFT_4K);
@@ -834,6 +848,11 @@ static void __init pSeries_setup_arch(void)
 		if (lppaca_shared_proc(get_lppaca())) {
 			static_branch_enable(&shared_processor);
 			pv_spinlocks_init();
+#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
+			static_key_slow_inc(&paravirt_steal_enabled);
+			if (steal_acc)
+				static_key_slow_inc(&paravirt_steal_rq_enabled);
+#endif
 		}
 
 		ppc_md.power_save = pseries_lpar_idle;
Signature · 1 line
2.37.2
author reviewer
#3 lore raw

PATCH v2 3/4 powerpc/64: Remove PPC64 special case for cputime accounting default

In reply to Nicholas Piggin

Distro kernels tend to be moving to VIRT_CPU_ACCOUNTING_GEN, and there is not much reason why PPC64 should be special here. Remove the special case and make the ppc64 and pseries defconfigs use GEN accounting (others will use TICK, as-per Kconfig defaults).

VIRT_CPU_ACCOUNTING_NATIVE does provide scaled vtime and stolen time apportioned between system and user time, and vtime accounting is not unconditionally enabled, and possibly other things. But it would be better at this point to extend GEN to cover important missing features rather than directing users back to a less used option.

---
 arch/powerpc/configs/ppc64_defconfig   | 2 ++
 arch/powerpc/configs/pseries_defconfig | 2 ++
 init/Kconfig                           | 3 +--
 3 files changed, 5 insertions(+), 2 deletions(-)
arch/powerpc/configs/ppc64_defconfig +2 -0
index c8b0e80d613b..6be0c43397b4 100644
@@ -1,7 +1,9 @@
 CONFIG_SYSVIPC=y
 CONFIG_POSIX_MQUEUE=y
+# CONFIG_CONTEXT_TRACKING_USER_FORCE is not set
 CONFIG_NO_HZ=y
 CONFIG_HIGH_RES_TIMERS=y
+CONFIG_VIRT_CPU_ACCOUNTING_GEN=y
 CONFIG_TASKSTATS=y
 CONFIG_TASK_DELAY_ACCT=y
 CONFIG_IKCONFIG=y
arch/powerpc/configs/pseries_defconfig +2 -0
index b571d084c148..4723ede5e10d 100644
@@ -3,8 +3,10 @@ CONFIG_NR_CPUS=2048
 CONFIG_SYSVIPC=y
 CONFIG_POSIX_MQUEUE=y
 CONFIG_AUDIT=y
+# CONFIG_CONTEXT_TRACKING_USER_FORCE is not set
 CONFIG_NO_HZ=y
 CONFIG_HIGH_RES_TIMERS=y
+CONFIG_VIRT_CPU_ACCOUNTING_GEN=y
 CONFIG_TASKSTATS=y
 CONFIG_TASK_DELAY_ACCT=y
 CONFIG_TASK_XACCT=y
init/Kconfig +1 -2
index 532362fcfe31..94ce5a46a802 100644
@@ -461,8 +461,7 @@ config VIRT_CPU_ACCOUNTING
 
 choice
 	prompt "Cputime accounting"
-	default TICK_CPU_ACCOUNTING if !PPC64
-	default VIRT_CPU_ACCOUNTING_NATIVE if PPC64
+	default TICK_CPU_ACCOUNTING
 
 # Kind of a stub config for the pure tick based cputime accounting
 config TICK_CPU_ACCOUNTING
Signature · 1 line
2.37.2
author reviewer
#4 lore raw

PATCH v2 4/4 powerpc/pseries: Move dtl scanning and steal time accounting to pseries platform

In reply to Nicholas Piggin

dtl is the PAPR Dispatch Trace Log, which is entirely a pseries feature. The pseries platform alrady has a file dealing with the dtl, so move scanning for stolen time accounting there from kernel/time.c.

---
 arch/powerpc/include/asm/cputime.h   |  2 +-
 arch/powerpc/include/asm/dtl.h       |  8 ---
 arch/powerpc/include/asm/time.h      |  5 +-
 arch/powerpc/kernel/time.c           | 92 ++--------------------------
 arch/powerpc/platforms/pseries/dtl.c | 81 ++++++++++++++++++++++++
 5 files changed, 90 insertions(+), 98 deletions(-)
arch/powerpc/include/asm/cputime.h +1 -1
index 6d2b27997492..431ae2343022 100644
@@ -95,7 +95,7 @@ static notrace inline void account_stolen_time(void)
 		struct lppaca *lp = local_paca->lppaca_ptr;
 
 		if (unlikely(local_paca->dtl_ridx != be64_to_cpu(lp->dtl_idx)))
-			accumulate_stolen_time();
+			pseries_accumulate_stolen_time();
 	}
 #endif
 }
arch/powerpc/include/asm/dtl.h +0 -8
index 1625888f27ef..4bcb9f9ac764 100644
@@ -37,14 +37,6 @@ struct dtl_entry {
 extern struct kmem_cache *dtl_cache;
 extern rwlock_t dtl_access_lock;
 
-/*
- * When CONFIG_VIRT_CPU_ACCOUNTING_NATIVE = y, the cpu accounting code controls
- * reading from the dispatch trace log.  If other code wants to consume
- * DTL entries, it can set this pointer to a function that will get
- * called once for each DTL entry that gets processed.
- */
-extern void (*dtl_consumer)(struct dtl_entry *entry, u64 index);
-
 extern void register_dtl_buffer(int cpu);
 extern void alloc_dtl_buffers(unsigned long *time_limit);
 extern long hcall_vphn(unsigned long cpu, u64 flags, __be32 *associativity);
arch/powerpc/include/asm/time.h +3 -2
index 1e5643a9b1f2..9f50766c4623 100644
@@ -116,8 +116,9 @@ unsigned long long tb_to_ns(unsigned long long tb_ticks);
 
 void timer_broadcast_interrupt(void);
 
-/* SPLPAR */
-void accumulate_stolen_time(void);
+/* SPLPAR and VIRT_CPU_ACCOUNTING_NATIVE */
+void pseries_accumulate_stolen_time(void);
+u64 pseries_calculate_stolen_time(u64 stop_tb);
 
 #endif /* __KERNEL__ */
 #endif /* __POWERPC_TIME_H */
arch/powerpc/kernel/time.c +5 -87

104 lines not on this page · raw message

arch/powerpc/platforms/pseries/dtl.c +81 -0

93 lines not on this page · raw message

Signature · 1 line
2.37.2
#6 lore raw
In reply to Nicholas Piggin
Quoted 10 lines · Nicholas Piggin

pseries provides stolen time accounting when VIRT_CPU_ACCOUNTING_NATIVE is selected, but not when VIRT_CPU_ACCOUNTING_GEN is. We like GEN because it's less code in arch/powerpc, allows full nohz, and distros have moved to it, so this series adds stolen time accounting for GEN, and moves our pseries configs over to it.

Thanks,
Nick

[...]

Applied to powerpc/next.

[1/4] powerpc/pseries: Add wait interval counter definitions to struct lppaca

      https://git.kernel.org/powerpc/c/a8933c8d55c37f4d5eb617b4bdb72b8888b88681
[2/4] powerpc/pseries: Implement CONFIG_PARAVIRT_TIME_ACCOUNTING
      https://git.kernel.org/powerpc/c/0e8a63132800dd8ae5fcb19113f79bea43ea18d9
[3/4] powerpc/64: Remove PPC64 special case for cputime accounting default
      https://git.kernel.org/powerpc/c/02382aff72357727f9eee5107fd32c6cd070f1d6
[4/4] powerpc/pseries: Move dtl scanning and steal time accounting to pseries platform
      https://git.kernel.org/powerpc/c/6ba5aa541aaa079c0ca888f7fe564b2035d5ca0a

cheers

#5 lore raw

Re: [PATCH v2 4/4] powerpc/pseries: Move dtl scanning and steal time accounting to pseries platform

In reply to Nicholas Piggin
Quoted 5 lines · Nicholas Piggin

dtl is the PAPR Dispatch Trace Log, which is entirely a pseries feature. The pseries platform alrady has a file dealing with the dtl, so move scanning for stolen time accounting there from kernel/time.c.

This patch ties DTL to PPC_SPLPAR without updating configuration dependencies. As result, the following build error may now be seen if CONFIG_PPC_SPLPAR=y and CONFIG_DTL=n.

arch/powerpc/kernel/irq.o: in function `.do_IRQ':
irq.c:(.text+0x2798): undefined reference to `.pseries_accumulate_stolen_time'

I updated my own configurations to avoid the problem, but you might see randconfig failures with this error in the future.

Guenter