Michal Suchanek 8ab928
From 6909f179ca7a73f243dca7c829facca1cc1d4ff5 Mon Sep 17 00:00:00 2001
Michal Suchanek 8ab928
From: "Gautham R. Shenoy" <ego@linux.vnet.ibm.com>
Michal Suchanek 8ab928
Date: Tue, 7 Apr 2020 14:17:42 +0530
Michal Suchanek 8ab928
Subject: [PATCH] powerpc/sysfs: Show idle_purr and idle_spurr for every CPU
Michal Suchanek 8ab928
Michal Suchanek 8ab928
References: PED-3947 bsc#1210544 ltc#202303
Michal Suchanek 8ab928
Patch-mainline: v5.8-rc1
Michal Suchanek 8ab928
Git-commit: 6909f179ca7a73f243dca7c829facca1cc1d4ff5
Michal Suchanek 8ab928
Michal Suchanek 8ab928
On Pseries LPARs, to calculate utilization, we need to know the
Michal Suchanek 8ab928
[S]PURR ticks when the CPUs were busy or idle.
Michal Suchanek 8ab928
Michal Suchanek 8ab928
The total PURR and SPURR ticks are already exposed via the per-cpu
Michal Suchanek 8ab928
sysfs files "purr" and "spurr". This patch adds support for exposing
Michal Suchanek 8ab928
the idle PURR and SPURR ticks via new per-cpu sysfs files named
Michal Suchanek 8ab928
"idle_purr" and "idle_spurr".
Michal Suchanek 8ab928
Michal Suchanek 8ab928
This patch also adds helper functions to accurately read the values of
Michal Suchanek 8ab928
idle_purr and idle_spurr especially from an interrupt context between
Michal Suchanek 8ab928
when the interrupt has occurred between the pseries_idle_prolog() and
Michal Suchanek 8ab928
pseries_idle_epilog(). This will ensure that the idle purr/spurr
Michal Suchanek 8ab928
values corresponding to the latest idle period is accounted for before
Michal Suchanek 8ab928
these values are read.
Michal Suchanek 8ab928
Michal Suchanek 8ab928
Signed-off-by: Gautham R. Shenoy <ego@linux.vnet.ibm.com>
Michal Suchanek 8ab928
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Michal Suchanek 8ab928
Link: https://lore.kernel.org/r/1586249263-14048-5-git-send-email-ego@linux.vnet.ibm.com
Michal Suchanek 8ab928
Acked-by: Michal Suchanek <msuchanek@suse.de>
Michal Suchanek 8ab928
---
Michal Suchanek 8ab928
 arch/powerpc/include/asm/idle.h | 32 +++++++++++++
Michal Suchanek 8ab928
 arch/powerpc/kernel/sysfs.c     | 82 +++++++++++++++++++++++++++++++--
Michal Suchanek 8ab928
 2 files changed, 111 insertions(+), 3 deletions(-)
Michal Suchanek 8ab928
Michal Suchanek 8ab928
diff --git a/arch/powerpc/include/asm/idle.h b/arch/powerpc/include/asm/idle.h
Michal Suchanek 8ab928
index 0efb25071d87..accd1f50085a 100644
Michal Suchanek 8ab928
--- a/arch/powerpc/include/asm/idle.h
Michal Suchanek 8ab928
+++ b/arch/powerpc/include/asm/idle.h
Michal Suchanek 8ab928
@@ -57,5 +57,37 @@ static inline void pseries_idle_epilog(void)
Michal Suchanek 8ab928
 	ppc64_runlatch_on();
Michal Suchanek 8ab928
 }
Michal Suchanek 8ab928
 
Michal Suchanek 8ab928
+static inline u64 read_this_idle_purr(void)
Michal Suchanek 8ab928
+{
Michal Suchanek 8ab928
+	/*
Michal Suchanek 8ab928
+	 * If we are reading from an idle context, update the
Michal Suchanek 8ab928
+	 * idle-purr cycles corresponding to the last idle period.
Michal Suchanek 8ab928
+	 * Since the idle context is not yet over, take a fresh
Michal Suchanek 8ab928
+	 * snapshot of the idle-purr.
Michal Suchanek 8ab928
+	 */
Michal Suchanek 8ab928
+	if (unlikely(get_lppaca()->idle == 1)) {
Michal Suchanek 8ab928
+		update_idle_purr_accounting();
Michal Suchanek 8ab928
+		snapshot_purr_idle_entry();
Michal Suchanek 8ab928
+	}
Michal Suchanek 8ab928
+
Michal Suchanek 8ab928
+	return be64_to_cpu(get_lppaca()->wait_state_cycles);
Michal Suchanek 8ab928
+}
Michal Suchanek 8ab928
+
Michal Suchanek 8ab928
+static inline u64 read_this_idle_spurr(void)
Michal Suchanek 8ab928
+{
Michal Suchanek 8ab928
+	/*
Michal Suchanek 8ab928
+	 * If we are reading from an idle context, update the
Michal Suchanek 8ab928
+	 * idle-spurr cycles corresponding to the last idle period.
Michal Suchanek 8ab928
+	 * Since the idle context is not yet over, take a fresh
Michal Suchanek 8ab928
+	 * snapshot of the idle-spurr.
Michal Suchanek 8ab928
+	 */
Michal Suchanek 8ab928
+	if (get_lppaca()->idle == 1) {
Michal Suchanek 8ab928
+		update_idle_spurr_accounting();
Michal Suchanek 8ab928
+		snapshot_spurr_idle_entry();
Michal Suchanek 8ab928
+	}
Michal Suchanek 8ab928
+
Michal Suchanek 8ab928
+	return *this_cpu_ptr(&idle_spurr_cycles);
Michal Suchanek 8ab928
+}
Michal Suchanek 8ab928
+
Michal Suchanek 8ab928
 #endif /* CONFIG_PPC_PSERIES */
Michal Suchanek 8ab928
 #endif
Michal Suchanek 8ab928
diff --git a/arch/powerpc/kernel/sysfs.c b/arch/powerpc/kernel/sysfs.c
Michal Suchanek 8ab928
--- a/arch/powerpc/kernel/sysfs.c
Michal Suchanek 8ab928
+++ b/arch/powerpc/kernel/sysfs.c
Michal Suchanek 8ab928
@@ -19,6 +19,7 @@
Michal Suchanek 8ab928
 #include <asm/smp.h>
Michal Suchanek 8ab928
 #include <asm/pmc.h>
Michal Suchanek 8ab928
 #include <asm/firmware.h>
Michal Suchanek 8ab928
+#include <asm/idle.h>
Michal Suchanek 8ab928
 
Michal Suchanek 8ab928
 #include "cacheinfo.h"
Michal Suchanek 8ab928
 
Michal Suchanek 8ab928
@@ -760,6 +761,74 @@ static void create_svm_file(void)
Michal Suchanek 8ab928
 #endif /* HAS_PPC_PMC_PA6T */
Michal Suchanek 8ab928
 #endif /* HAS_PPC_PMC_CLASSIC */
Michal Suchanek 8ab928
 
Michal Suchanek 8ab928
+#ifdef CONFIG_PPC_PSERIES
Michal Suchanek 8ab928
+static void read_idle_purr(void *val)
Michal Suchanek 8ab928
+{
Michal Suchanek 8ab928
+	u64 *ret = val;
Michal Suchanek 8ab928
+
Michal Suchanek 8ab928
+	*ret = read_this_idle_purr();
Michal Suchanek 8ab928
+}
Michal Suchanek 8ab928
+
Michal Suchanek 8ab928
+static ssize_t idle_purr_show(struct device *dev,
Michal Suchanek 8ab928
+			      struct device_attribute *attr, char *buf)
Michal Suchanek 8ab928
+{
Michal Suchanek 8ab928
+	struct cpu *cpu = container_of(dev, struct cpu, dev);
Michal Suchanek 8ab928
+	u64 val;
Michal Suchanek 8ab928
+
Michal Suchanek 8ab928
+	smp_call_function_single(cpu->dev.id, read_idle_purr, &val, 1);
Michal Suchanek 8ab928
+	return sprintf(buf, "%llx\n", val);
Michal Suchanek 8ab928
+}
Michal Suchanek 8ab928
+static DEVICE_ATTR(idle_purr, 0400, idle_purr_show, NULL);
Michal Suchanek 8ab928
+
Michal Suchanek 8ab928
+static void create_idle_purr_file(struct device *s)
Michal Suchanek 8ab928
+{
Michal Suchanek 8ab928
+	if (firmware_has_feature(FW_FEATURE_LPAR))
Michal Suchanek 8ab928
+		device_create_file(s, &dev_attr_idle_purr);
Michal Suchanek 8ab928
+}
Michal Suchanek 8ab928
+
Michal Suchanek 8ab928
+static void remove_idle_purr_file(struct device *s)
Michal Suchanek 8ab928
+{
Michal Suchanek 8ab928
+	if (firmware_has_feature(FW_FEATURE_LPAR))
Michal Suchanek 8ab928
+		device_remove_file(s, &dev_attr_idle_purr);
Michal Suchanek 8ab928
+}
Michal Suchanek 8ab928
+
Michal Suchanek 8ab928
+static void read_idle_spurr(void *val)
Michal Suchanek 8ab928
+{
Michal Suchanek 8ab928
+	u64 *ret = val;
Michal Suchanek 8ab928
+
Michal Suchanek 8ab928
+	*ret = read_this_idle_spurr();
Michal Suchanek 8ab928
+}
Michal Suchanek 8ab928
+
Michal Suchanek 8ab928
+static ssize_t idle_spurr_show(struct device *dev,
Michal Suchanek 8ab928
+			       struct device_attribute *attr, char *buf)
Michal Suchanek 8ab928
+{
Michal Suchanek 8ab928
+	struct cpu *cpu = container_of(dev, struct cpu, dev);
Michal Suchanek 8ab928
+	u64 val;
Michal Suchanek 8ab928
+
Michal Suchanek 8ab928
+	smp_call_function_single(cpu->dev.id, read_idle_spurr, &val, 1);
Michal Suchanek 8ab928
+	return sprintf(buf, "%llx\n", val);
Michal Suchanek 8ab928
+}
Michal Suchanek 8ab928
+static DEVICE_ATTR(idle_spurr, 0400, idle_spurr_show, NULL);
Michal Suchanek 8ab928
+
Michal Suchanek 8ab928
+static void create_idle_spurr_file(struct device *s)
Michal Suchanek 8ab928
+{
Michal Suchanek 8ab928
+	if (firmware_has_feature(FW_FEATURE_LPAR))
Michal Suchanek 8ab928
+		device_create_file(s, &dev_attr_idle_spurr);
Michal Suchanek 8ab928
+}
Michal Suchanek 8ab928
+
Michal Suchanek 8ab928
+static void remove_idle_spurr_file(struct device *s)
Michal Suchanek 8ab928
+{
Michal Suchanek 8ab928
+	if (firmware_has_feature(FW_FEATURE_LPAR))
Michal Suchanek 8ab928
+		device_remove_file(s, &dev_attr_idle_spurr);
Michal Suchanek 8ab928
+}
Michal Suchanek 8ab928
+
Michal Suchanek 8ab928
+#else /* CONFIG_PPC_PSERIES */
Michal Suchanek 8ab928
+#define create_idle_purr_file(s)
Michal Suchanek 8ab928
+#define remove_idle_purr_file(s)
Michal Suchanek 8ab928
+#define create_idle_spurr_file(s)
Michal Suchanek 8ab928
+#define remove_idle_spurr_file(s)
Michal Suchanek 8ab928
+#endif /* CONFIG_PPC_PSERIES */
Michal Suchanek 8ab928
+
Michal Suchanek 8ab928
 static int register_cpu_online(unsigned int cpu)
Michal Suchanek 8ab928
 {
Michal Suchanek 8ab928
 	struct cpu *c = &per_cpu(cpu_devices, cpu);
Michal Suchanek 8ab928
@@ -823,10 +892,13 @@ static int register_cpu_online(unsigned int cpu)
Michal Suchanek 8ab928
 		if (!firmware_has_feature(FW_FEATURE_LPAR))
Michal Suchanek 8ab928
 			add_write_permission_dev_attr(&dev_attr_purr);
Michal Suchanek 8ab928
 		device_create_file(s, &dev_attr_purr);
Michal Suchanek 8ab928
+		create_idle_purr_file(s);
Michal Suchanek 8ab928
 	}
Michal Suchanek 8ab928
 
Michal Suchanek 8ab928
-	if (cpu_has_feature(CPU_FTR_SPURR))
Michal Suchanek 8ab928
+	if (cpu_has_feature(CPU_FTR_SPURR)) {
Michal Suchanek 8ab928
 		device_create_file(s, &dev_attr_spurr);
Michal Suchanek 8ab928
+		create_idle_spurr_file(s);
Michal Suchanek 8ab928
+	}
Michal Suchanek 8ab928
 
Michal Suchanek 8ab928
 	if (cpu_has_feature(CPU_FTR_DSCR))
Michal Suchanek 8ab928
 		device_create_file(s, &dev_attr_dscr);
Michal Suchanek 8ab928
@@ -910,11 +982,15 @@ static int unregister_cpu_online(unsigned int cpu)
Michal Suchanek 8ab928
 	if (cpu_has_feature(CPU_FTR_MMCRA))
Michal Suchanek 8ab928
 		device_remove_file(s, &dev_attr_mmcra);
Michal Suchanek 8ab928
 
Michal Suchanek 8ab928
-	if (cpu_has_feature(CPU_FTR_PURR))
Michal Suchanek 8ab928
+	if (cpu_has_feature(CPU_FTR_PURR)) {
Michal Suchanek 8ab928
 		device_remove_file(s, &dev_attr_purr);
Michal Suchanek 8ab928
+		remove_idle_purr_file(s);
Michal Suchanek 8ab928
+	}
Michal Suchanek 8ab928
 
Michal Suchanek 8ab928
-	if (cpu_has_feature(CPU_FTR_SPURR))
Michal Suchanek 8ab928
+	if (cpu_has_feature(CPU_FTR_SPURR)) {
Michal Suchanek 8ab928
 		device_remove_file(s, &dev_attr_spurr);
Michal Suchanek 8ab928
+		remove_idle_spurr_file(s);
Michal Suchanek 8ab928
+	}
Michal Suchanek 8ab928
 
Michal Suchanek 8ab928
 	if (cpu_has_feature(CPU_FTR_DSCR))
Michal Suchanek 8ab928
 		device_remove_file(s, &dev_attr_dscr);
Michal Suchanek 8ab928
-- 
Michal Suchanek 8ab928
2.40.0
Michal Suchanek 8ab928