All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Pratik R. Sampat" <psampat@linux.ibm.com>
To: bristot@redhat.com, christian@brauner.io, ebiederm@xmission.com,
	lizefan.x@bytedance.com, tj@kernel.org, hannes@cmpxchg.org,
	mingo@kernel.org, juri.lelli@redhat.com,
	linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	cgroups@vger.kernel.org, containers@lists.linux.dev,
	containers@lists.linux-foundation.org, psampat@linux.ibm.com,
	pratik.r.sampat@gmail.com
Subject: [RFC 5/5] proc/cpuns: Make procfs load stats CPU namespace aware
Date: Sat,  9 Oct 2021 20:42:43 +0530	[thread overview]
Message-ID: <20211009151243.8825-6-psampat@linux.ibm.com> (raw)
In-Reply-To: <20211009151243.8825-1-psampat@linux.ibm.com>

This commit adds support provide a virtualized view to the /proc/stat
load statistics. The load, idle, irq and the rest of the information of
a physical CPU is now displayed for its corresponding virtual CPU
counterpart.
The procfs file only populates the virtualized view for the CPUs based
on the restrictions from cgroupfs set upon it.

Signed-off-by: Pratik R. Sampat <psampat@linux.ibm.com>
---
 fs/proc/stat.c | 50 ++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 38 insertions(+), 12 deletions(-)

diff --git a/fs/proc/stat.c b/fs/proc/stat.c
index 6561a06ef905..3ff39e7362bb 100644
--- a/fs/proc/stat.c
+++ b/fs/proc/stat.c
@@ -14,6 +14,7 @@
 #include <linux/irqnr.h>
 #include <linux/sched/cputime.h>
 #include <linux/tick.h>
+#include <linux/cpu_namespace.h>
 
 #ifndef arch_irq_stat_cpu
 #define arch_irq_stat_cpu(cpu) 0
@@ -107,13 +108,14 @@ static void show_all_irqs(struct seq_file *p)
 
 static int show_stat(struct seq_file *p, void *v)
 {
-	int i, j;
+	int i, j, pcpu;
 	u64 user, nice, system, idle, iowait, irq, softirq, steal;
 	u64 guest, guest_nice;
 	u64 sum = 0;
 	u64 sum_softirq = 0;
 	unsigned int per_softirq_sums[NR_SOFTIRQS] = {0};
 	struct timespec64 boottime;
+	cpumask_var_t cpu_mask;
 
 	user = nice = system = idle = iowait =
 		irq = softirq = steal = 0;
@@ -122,27 +124,39 @@ static int show_stat(struct seq_file *p, void *v)
 	/* shift boot timestamp according to the timens offset */
 	timens_sub_boottime(&boottime);
 
-	for_each_possible_cpu(i) {
+#ifdef CONFIG_CPU_NS
+	if (current->nsproxy->cpu_ns == &init_cpu_ns) {
+		cpumask_copy(cpu_mask, cpu_possible_mask);
+	} else {
+		cpumask_copy(cpu_mask,
+			     &current->nsproxy->cpu_ns->v_cpuset_cpus);
+	}
+#else
+	cpumask_copy(cpu_mask, cpu_possible_mask);
+#endif
+
+	for_each_cpu(i, cpu_mask) {
 		struct kernel_cpustat kcpustat;
 		u64 *cpustat = kcpustat.cpustat;
 
-		kcpustat_cpu_fetch(&kcpustat, i);
+		pcpu = get_pcpu_cpuns(current->nsproxy->cpu_ns, i);
+		kcpustat_cpu_fetch(&kcpustat, pcpu);
 
 		user		+= cpustat[CPUTIME_USER];
 		nice		+= cpustat[CPUTIME_NICE];
 		system		+= cpustat[CPUTIME_SYSTEM];
-		idle		+= get_idle_time(&kcpustat, i);
-		iowait		+= get_iowait_time(&kcpustat, i);
+		idle		+= get_idle_time(&kcpustat, pcpu);
+		iowait		+= get_iowait_time(&kcpustat, pcpu);
 		irq		+= cpustat[CPUTIME_IRQ];
 		softirq		+= cpustat[CPUTIME_SOFTIRQ];
 		steal		+= cpustat[CPUTIME_STEAL];
 		guest		+= cpustat[CPUTIME_GUEST];
 		guest_nice	+= cpustat[CPUTIME_GUEST_NICE];
-		sum		+= kstat_cpu_irqs_sum(i);
-		sum		+= arch_irq_stat_cpu(i);
+		sum		+= kstat_cpu_irqs_sum(pcpu);
+		sum		+= arch_irq_stat_cpu(pcpu);
 
 		for (j = 0; j < NR_SOFTIRQS; j++) {
-			unsigned int softirq_stat = kstat_softirqs_cpu(j, i);
+			unsigned int softirq_stat = kstat_softirqs_cpu(j, pcpu);
 
 			per_softirq_sums[j] += softirq_stat;
 			sum_softirq += softirq_stat;
@@ -162,18 +176,30 @@ static int show_stat(struct seq_file *p, void *v)
 	seq_put_decimal_ull(p, " ", nsec_to_clock_t(guest_nice));
 	seq_putc(p, '\n');
 
-	for_each_online_cpu(i) {
+#ifdef CONFIG_CPU_NS
+	if (current->nsproxy->cpu_ns == &init_cpu_ns) {
+		cpumask_copy(cpu_mask, cpu_online_mask);
+	} else {
+		cpumask_copy(cpu_mask,
+			     &current->nsproxy->cpu_ns->v_cpuset_cpus);
+	}
+#else
+	cpumask_copy(cpu_mask, cpu_online_mask);
+#endif
+	for_each_cpu(i, cpu_mask) {
 		struct kernel_cpustat kcpustat;
 		u64 *cpustat = kcpustat.cpustat;
 
-		kcpustat_cpu_fetch(&kcpustat, i);
+		pcpu = get_pcpu_cpuns(current->nsproxy->cpu_ns, i);
+
+		kcpustat_cpu_fetch(&kcpustat, pcpu);
 
 		/* Copy values here to work around gcc-2.95.3, gcc-2.96 */
 		user		= cpustat[CPUTIME_USER];
 		nice		= cpustat[CPUTIME_NICE];
 		system		= cpustat[CPUTIME_SYSTEM];
-		idle		= get_idle_time(&kcpustat, i);
-		iowait		= get_iowait_time(&kcpustat, i);
+		idle		= get_idle_time(&kcpustat, pcpu);
+		iowait		= get_iowait_time(&kcpustat, pcpu);
 		irq		= cpustat[CPUTIME_IRQ];
 		softirq		= cpustat[CPUTIME_SOFTIRQ];
 		steal		= cpustat[CPUTIME_STEAL];
-- 
2.31.1


WARNING: multiple messages have this Message-ID (diff)
From: "Pratik R. Sampat" <psampat-tEXmvtCZX7AybS5Ee8rs3A@public.gmane.org>
To: bristot-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org,
	christian-STijNZzMWpgWenYVfaLwtA@public.gmane.org,
	ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org,
	lizefan.x-EC8Uxl6Npydl57MIdRCFDg@public.gmane.org,
	tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org,
	hannes-druUgvl0LCNAfugRpC6u6w@public.gmane.org,
	mingo-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org,
	juri.lelli-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-fsdevel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	containers-cunTk1MwBs/YUNznpcFYbw@public.gmane.org,
	containers-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org,
	psampat-tEXmvtCZX7AybS5Ee8rs3A@public.gmane.org,
	pratik.r.sampat-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Subject: [RFC 5/5] proc/cpuns: Make procfs load stats CPU namespace aware
Date: Sat,  9 Oct 2021 20:42:43 +0530	[thread overview]
Message-ID: <20211009151243.8825-6-psampat@linux.ibm.com> (raw)
In-Reply-To: <20211009151243.8825-1-psampat-tEXmvtCZX7AybS5Ee8rs3A@public.gmane.org>

This commit adds support provide a virtualized view to the /proc/stat
load statistics. The load, idle, irq and the rest of the information of
a physical CPU is now displayed for its corresponding virtual CPU
counterpart.
The procfs file only populates the virtualized view for the CPUs based
on the restrictions from cgroupfs set upon it.

Signed-off-by: Pratik R. Sampat <psampat-tEXmvtCZX7AybS5Ee8rs3A@public.gmane.org>
---
 fs/proc/stat.c | 50 ++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 38 insertions(+), 12 deletions(-)

diff --git a/fs/proc/stat.c b/fs/proc/stat.c
index 6561a06ef905..3ff39e7362bb 100644
--- a/fs/proc/stat.c
+++ b/fs/proc/stat.c
@@ -14,6 +14,7 @@
 #include <linux/irqnr.h>
 #include <linux/sched/cputime.h>
 #include <linux/tick.h>
+#include <linux/cpu_namespace.h>
 
 #ifndef arch_irq_stat_cpu
 #define arch_irq_stat_cpu(cpu) 0
@@ -107,13 +108,14 @@ static void show_all_irqs(struct seq_file *p)
 
 static int show_stat(struct seq_file *p, void *v)
 {
-	int i, j;
+	int i, j, pcpu;
 	u64 user, nice, system, idle, iowait, irq, softirq, steal;
 	u64 guest, guest_nice;
 	u64 sum = 0;
 	u64 sum_softirq = 0;
 	unsigned int per_softirq_sums[NR_SOFTIRQS] = {0};
 	struct timespec64 boottime;
+	cpumask_var_t cpu_mask;
 
 	user = nice = system = idle = iowait =
 		irq = softirq = steal = 0;
@@ -122,27 +124,39 @@ static int show_stat(struct seq_file *p, void *v)
 	/* shift boot timestamp according to the timens offset */
 	timens_sub_boottime(&boottime);
 
-	for_each_possible_cpu(i) {
+#ifdef CONFIG_CPU_NS
+	if (current->nsproxy->cpu_ns == &init_cpu_ns) {
+		cpumask_copy(cpu_mask, cpu_possible_mask);
+	} else {
+		cpumask_copy(cpu_mask,
+			     &current->nsproxy->cpu_ns->v_cpuset_cpus);
+	}
+#else
+	cpumask_copy(cpu_mask, cpu_possible_mask);
+#endif
+
+	for_each_cpu(i, cpu_mask) {
 		struct kernel_cpustat kcpustat;
 		u64 *cpustat = kcpustat.cpustat;
 
-		kcpustat_cpu_fetch(&kcpustat, i);
+		pcpu = get_pcpu_cpuns(current->nsproxy->cpu_ns, i);
+		kcpustat_cpu_fetch(&kcpustat, pcpu);
 
 		user		+= cpustat[CPUTIME_USER];
 		nice		+= cpustat[CPUTIME_NICE];
 		system		+= cpustat[CPUTIME_SYSTEM];
-		idle		+= get_idle_time(&kcpustat, i);
-		iowait		+= get_iowait_time(&kcpustat, i);
+		idle		+= get_idle_time(&kcpustat, pcpu);
+		iowait		+= get_iowait_time(&kcpustat, pcpu);
 		irq		+= cpustat[CPUTIME_IRQ];
 		softirq		+= cpustat[CPUTIME_SOFTIRQ];
 		steal		+= cpustat[CPUTIME_STEAL];
 		guest		+= cpustat[CPUTIME_GUEST];
 		guest_nice	+= cpustat[CPUTIME_GUEST_NICE];
-		sum		+= kstat_cpu_irqs_sum(i);
-		sum		+= arch_irq_stat_cpu(i);
+		sum		+= kstat_cpu_irqs_sum(pcpu);
+		sum		+= arch_irq_stat_cpu(pcpu);
 
 		for (j = 0; j < NR_SOFTIRQS; j++) {
-			unsigned int softirq_stat = kstat_softirqs_cpu(j, i);
+			unsigned int softirq_stat = kstat_softirqs_cpu(j, pcpu);
 
 			per_softirq_sums[j] += softirq_stat;
 			sum_softirq += softirq_stat;
@@ -162,18 +176,30 @@ static int show_stat(struct seq_file *p, void *v)
 	seq_put_decimal_ull(p, " ", nsec_to_clock_t(guest_nice));
 	seq_putc(p, '\n');
 
-	for_each_online_cpu(i) {
+#ifdef CONFIG_CPU_NS
+	if (current->nsproxy->cpu_ns == &init_cpu_ns) {
+		cpumask_copy(cpu_mask, cpu_online_mask);
+	} else {
+		cpumask_copy(cpu_mask,
+			     &current->nsproxy->cpu_ns->v_cpuset_cpus);
+	}
+#else
+	cpumask_copy(cpu_mask, cpu_online_mask);
+#endif
+	for_each_cpu(i, cpu_mask) {
 		struct kernel_cpustat kcpustat;
 		u64 *cpustat = kcpustat.cpustat;
 
-		kcpustat_cpu_fetch(&kcpustat, i);
+		pcpu = get_pcpu_cpuns(current->nsproxy->cpu_ns, i);
+
+		kcpustat_cpu_fetch(&kcpustat, pcpu);
 
 		/* Copy values here to work around gcc-2.95.3, gcc-2.96 */
 		user		= cpustat[CPUTIME_USER];
 		nice		= cpustat[CPUTIME_NICE];
 		system		= cpustat[CPUTIME_SYSTEM];
-		idle		= get_idle_time(&kcpustat, i);
-		iowait		= get_iowait_time(&kcpustat, i);
+		idle		= get_idle_time(&kcpustat, pcpu);
+		iowait		= get_iowait_time(&kcpustat, pcpu);
 		irq		= cpustat[CPUTIME_IRQ];
 		softirq		= cpustat[CPUTIME_SOFTIRQ];
 		steal		= cpustat[CPUTIME_STEAL];
-- 
2.31.1


  parent reply	other threads:[~2021-10-09 15:13 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-09 15:12 [RFC 0/5] kernel: Introduce CPU Namespace Pratik R. Sampat
2021-10-09 15:12 ` Pratik R. Sampat
2021-10-09 15:12 ` [RFC 1/5] ns: " Pratik R. Sampat
2021-10-09 22:37   ` Peter Zijlstra
2021-10-09 22:37     ` Peter Zijlstra
2021-10-09 15:12 ` [RFC 2/5] ns: Add scrambling functionality to CPU namespace Pratik R. Sampat
2021-10-09 15:12   ` Pratik R. Sampat
2021-10-09 15:12 ` [RFC 3/5] cpuset/cpuns: Make cgroup CPUset CPU namespace aware Pratik R. Sampat
2021-10-09 15:12 ` [RFC 4/5] cpu/cpuns: Make sysfs " Pratik R. Sampat
2021-10-09 15:12   ` Pratik R. Sampat
2021-10-09 15:12 ` Pratik R. Sampat [this message]
2021-10-09 15:12   ` [RFC 5/5] proc/cpuns: Make procfs load stats " Pratik R. Sampat
2021-10-09 22:41 ` [RFC 0/5] kernel: Introduce CPU Namespace Peter Zijlstra
2021-10-11 10:11 ` Christian Brauner
2021-10-11 10:11   ` Christian Brauner
2021-10-11 14:17   ` Michal Koutný
2021-10-11 14:17     ` Michal Koutný
2021-10-11 17:42     ` Tejun Heo
2021-10-12  8:42   ` Pratik Sampat
2021-10-14 22:14     ` Tejun Heo
2021-10-18 15:29       ` Pratik Sampat
2021-10-18 16:29         ` Tejun Heo
2021-10-20 10:44           ` Pratik Sampat
2021-10-20 10:44             ` Pratik Sampat
2021-10-20 16:35             ` Tejun Heo
2021-10-20 16:35               ` Tejun Heo
2021-10-21  7:44               ` Pratik Sampat
2021-10-21  7:44                 ` Pratik Sampat
2021-10-21 17:06                 ` Tejun Heo
2021-10-21 17:06                   ` Tejun Heo
2021-10-21 17:15             ` Eric W. Biederman
2021-10-21 17:15               ` Eric W. Biederman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211009151243.8825-6-psampat@linux.ibm.com \
    --to=psampat@linux.ibm.com \
    --cc=bristot@redhat.com \
    --cc=cgroups@vger.kernel.org \
    --cc=christian@brauner.io \
    --cc=containers@lists.linux-foundation.org \
    --cc=containers@lists.linux.dev \
    --cc=ebiederm@xmission.com \
    --cc=hannes@cmpxchg.org \
    --cc=juri.lelli@redhat.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizefan.x@bytedance.com \
    --cc=mingo@kernel.org \
    --cc=pratik.r.sampat@gmail.com \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.