Skip to contentSkip to conversation

cgroup: add cpu.stat_percpu

PATCH In review Josh Don 4 participants 8 messages Jan 7, 2022 – Jan 13, 2022

Subsystems (2)
lore
Share & tools
lore

Josh Don

author
#0 lore raw

PATCH 1/2 cgroup: add cpu.stat_percpu

cpu.stat displays global metrics, such as cgroup usage. It would also be useful to be able to break these down by cpu; to that end, this patch adds a new interface, 'cpu.stat_percpu', to display the percpu values of these stats.

Each line of the output corresponds to a particular metric. The format of each line is the name of the metric, followed by space delimited percpu values. The reason for this approach (vs having each line correspond to a particular cpu) is to make it easier to display extra subsystem-specific percpu fields.

---
 include/linux/cgroup-defs.h     |   5 +
 kernel/cgroup/cgroup-internal.h |   1 +
 kernel/cgroup/cgroup.c          |  10 ++
 kernel/cgroup/rstat.c           | 159 ++++++++++++++++++++++++++++----
 4 files changed, 155 insertions(+), 20 deletions(-)
include/linux/cgroup-defs.h +5 -0
index db2e147e069f..7778a011f457 100644
@@ -461,6 +461,11 @@ struct cgroup {
 	struct cgroup_base_stat bstat;
 	struct prev_cputime prev_cputime;	/* for printing out cputime */
 
+	/* Per-cpu basic resource statistics. These are NULL on root. */
+	struct cgroup_base_stat __percpu *bstat_cpu;
+	struct cgroup_base_stat __percpu *last_bstat_cpu;
+	struct prev_cputime __percpu *prev_cputime_cpu;
+
 	/*
 	 * list of pidlists, up to two for each namespace (one for procs, one
 	 * for tasks); created on demand.
kernel/cgroup/cgroup-internal.h +1 -0
index bfbeabc17a9d..07e932c4f875 100644
@@ -254,6 +254,7 @@ int cgroup_rstat_init(struct cgroup *cgrp);
 void cgroup_rstat_exit(struct cgroup *cgrp);
 void cgroup_rstat_boot(void);
 void cgroup_base_stat_cputime_show(struct seq_file *seq);
+void cgroup_base_stat_percpu_cputime_show(struct seq_file *seq);
 
 /*
  * namespace.c
kernel/cgroup/cgroup.c +10 -0
index 919194de39c8..4f5ddce529eb 100644
@@ -3604,6 +3604,12 @@ static int cpu_stat_show(struct seq_file *seq, void *v)
 	return ret;
 }
 
+static int cpu_stat_percpu_show(struct seq_file *seq, void *v)
+{
+	cgroup_base_stat_percpu_cputime_show(seq);
+	return 0;
+}
+
 #ifdef CONFIG_PSI
 static int cgroup_io_pressure_show(struct seq_file *seq, void *v)
 {
@@ -5014,6 +5020,10 @@ static struct cftype cgroup_base_files[] = {
 		.name = "cpu.stat",
 		.seq_show = cpu_stat_show,
 	},
+	{
+		.name = "cpu.stat_percpu",
+		.seq_show = cpu_stat_percpu_show,
+	},
 #ifdef CONFIG_PSI
 	{
 		.name = "io.pressure",
kernel/cgroup/rstat.c +139 -20

220 lines not on this page · raw message

Signature · 1 line
2.34.1.575.g55b058a8bb-goog
author
#1 lore raw

PATCH 2/2 sched: show percpu throttled stats

Now that we have a cpu.stat_percpu interface, we can expose some statistics percpu. From the cpu controller, CFS bandwidth throttling time is a useful metric to expose percpu.

---
 include/linux/cgroup-defs.h |  3 ++-
 kernel/cgroup/cgroup.c      | 15 +++++++++++----
 kernel/sched/core.c         | 38 +++++++++++++++++++++++++++++++++++--
 kernel/sched/fair.c         |  5 ++++-
 kernel/sched/sched.h        |  1 +
 5 files changed, 54 insertions(+), 8 deletions(-)
include/linux/cgroup-defs.h +2 -1
index 7778a011f457..e6903a6e0a10 100644
@@ -636,7 +636,8 @@ struct cgroup_subsys {
 	void (*css_reset)(struct cgroup_subsys_state *css);
 	void (*css_rstat_flush)(struct cgroup_subsys_state *css, int cpu);
 	int (*css_extra_stat_show)(struct seq_file *seq,
-				   struct cgroup_subsys_state *css);
+				   struct cgroup_subsys_state *css,
+				   bool percpu);
 
 	int (*can_attach)(struct cgroup_taskset *tset);
 	void (*cancel_attach)(struct cgroup_taskset *tset);
kernel/cgroup/cgroup.c +11 -4
index 4f5ddce529eb..05d9aeb3ff48 100644
@@ -3574,7 +3574,8 @@ static int cgroup_stat_show(struct seq_file *seq, void *v)
 }
 
 static int __maybe_unused cgroup_extra_stat_show(struct seq_file *seq,
-						 struct cgroup *cgrp, int ssid)
+						 struct cgroup *cgrp, int ssid,
+						 bool percpu)
 {
 	struct cgroup_subsys *ss = cgroup_subsys[ssid];
 	struct cgroup_subsys_state *css;
@@ -3587,7 +3588,7 @@ static int __maybe_unused cgroup_extra_stat_show(struct seq_file *seq,
 	if (!css)
 		return 0;
 
-	ret = ss->css_extra_stat_show(seq, css);
+	ret = ss->css_extra_stat_show(seq, css, percpu);
 	css_put(css);
 	return ret;
 }
@@ -3599,15 +3600,21 @@ static int cpu_stat_show(struct seq_file *seq, void *v)
 
 	cgroup_base_stat_cputime_show(seq);
 #ifdef CONFIG_CGROUP_SCHED
-	ret = cgroup_extra_stat_show(seq, cgrp, cpu_cgrp_id);
+	ret = cgroup_extra_stat_show(seq, cgrp, cpu_cgrp_id, /*percpu=*/false);
 #endif
 	return ret;
 }
 
 static int cpu_stat_percpu_show(struct seq_file *seq, void *v)
 {
+	struct cgroup __maybe_unused *cgrp = seq_css(seq)->cgroup;
+	int ret = 0;
+
 	cgroup_base_stat_percpu_cputime_show(seq);
-	return 0;
+#ifdef CONFIG_CGROUP_SCHED
+	ret = cgroup_extra_stat_show(seq, cgrp, cpu_cgrp_id, /*percpu=*/true);
+#endif
+	return ret;
 }
 
 #ifdef CONFIG_PSI
kernel/sched/core.c +36 -2
index 4604e80fac5e..8b383e58aaa2 100644
@@ -10651,8 +10651,8 @@ static struct cftype cpu_legacy_files[] = {
 	{ }	/* Terminate */
 };
 
-static int cpu_extra_stat_show(struct seq_file *sf,
-			       struct cgroup_subsys_state *css)
+static void __cpu_extra_stat_show(struct seq_file *sf,
+				  struct cgroup_subsys_state *css)
 {
 #ifdef CONFIG_CFS_BANDWIDTH
 	{
@@ -10674,6 +10674,40 @@ static int cpu_extra_stat_show(struct seq_file *sf,
 			   throttled_usec, cfs_b->nr_burst, burst_usec);
 	}
 #endif
+}
+
+static void __cpu_extra_stat_percpu_show(struct seq_file *sf,
+					 struct cgroup_subsys_state *css)
+{
+#ifdef CONFIG_CFS_BANDWIDTH
+	{
+		struct task_group *tg = css_tg(css);
+		struct cfs_rq *cfs_rq;
+		u64 throttled_usec;
+		int cpu;
+
+		seq_puts(sf, "throttled_usec");
+		for_each_possible_cpu(cpu) {
+			cfs_rq = tg->cfs_rq[cpu];
+
+			throttled_usec = READ_ONCE(cfs_rq->throttled_time);
+			do_div(throttled_usec, NSEC_PER_USEC);
+
+			seq_printf(sf, " %llu", throttled_usec);
+		}
+		seq_puts(sf, "\n");
+	}
+#endif
+}
+
+static int cpu_extra_stat_show(struct seq_file *sf,
+			       struct cgroup_subsys_state *css,
+			       bool percpu)
+{
+	if (percpu)
+		__cpu_extra_stat_percpu_show(sf, css);
+	else
+		__cpu_extra_stat_show(sf, css);
 	return 0;
 }
 
kernel/sched/fair.c +4 -1
index d4b463e015cd..2de0ce23ee99 100644
@@ -4878,6 +4878,7 @@ void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
 	struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
 	struct sched_entity *se;
 	long task_delta, idle_task_delta;
+	u64 throttled_time;
 
 	se = cfs_rq->tg->se[cpu_of(rq)];
 
@@ -4886,7 +4887,9 @@ void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
 	update_rq_clock(rq);
 
 	raw_spin_lock(&cfs_b->lock);
-	cfs_b->throttled_time += rq_clock(rq) - cfs_rq->throttled_clock;
+	throttled_time = rq_clock(rq) - cfs_rq->throttled_clock;
+	cfs_b->throttled_time += throttled_time;
+	cfs_rq->throttled_time += throttled_time;
 	list_del_rcu(&cfs_rq->throttled_list);
 	raw_spin_unlock(&cfs_b->lock);
 
kernel/sched/sched.h +1 -0
index de53be905739..c1ac2b8d8dd5 100644
@@ -620,6 +620,7 @@ struct cfs_rq {
 	u64			throttled_clock;
 	u64			throttled_clock_task;
 	u64			throttled_clock_task_time;
+	u64			throttled_time;
 	int			throttled;
 	int			throttle_count;
 	struct list_head	throttled_list;
Signature · 1 line
2.34.1.575.g55b058a8bb-goog
#2 lore raw
In reply to Josh Don
Thank you for the patch! Yet something to improve:

Hi Josh,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on tj-cgroup/for-next]
[also build test ERROR on tip/sched/core tip/master v5.16-rc8 next-20220107] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Josh-Don/cgroup-add-cpu-stat_percpu/20220108-074316
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup.git for-next
config: x86_64-randconfig-a014-20220107 (https://download.01.org/0day-ci/archive/20220108/202201081834.qK6yea4m-lkp(a)intel.com/config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project f3a344d2125fa37e59bae1b0874442c650a19607)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/d70517c43ca623321dcca220ce4c9b93059dc763
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Josh-Don/cgroup-add-cpu-stat_percpu/20220108-074316 git checkout d70517c43ca623321dcca220ce4c9b93059dc763
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

>> kernel/cgroup/rstat.c:519:9: error: '__section__' attribute only applies to functions, global variables, Objective-C methods, and Objective-C properties
           static DEFINE_PER_CPU(struct cgroup_base_stat, cached_percpu_stats);
                  ^
   include/linux/percpu-defs.h:115:2: note: expanded from macro 'DEFINE_PER_CPU'
           DEFINE_PER_CPU_SECTION(type, name, "")
           ^
   include/linux/percpu-defs.h:93:2: note: expanded from macro 'DEFINE_PER_CPU_SECTION'
           __PCPU_DUMMY_ATTRS char __pcpu_unique_##name;                   \
           ^
   include/linux/percpu-defs.h:54:2: note: expanded from macro '__PCPU_DUMMY_ATTRS'
           __section(".discard") __attribute__((unused))
           ^
   include/linux/compiler_attributes.h:276:56: note: expanded from macro '__section'
   #define __section(section)              __attribute__((__section__(section)))
                                                          ^
>> kernel/cgroup/rstat.c:519:9: error: non-extern declaration of '__pcpu_unique_cached_percpu_stats' follows extern declaration
   include/linux/percpu-defs.h:115:2: note: expanded from macro 'DEFINE_PER_CPU'
           DEFINE_PER_CPU_SECTION(type, name, "")
           ^
   include/linux/percpu-defs.h:93:26: note: expanded from macro 'DEFINE_PER_CPU_SECTION'
           __PCPU_DUMMY_ATTRS char __pcpu_unique_##name;                   \
                                   ^
   <scratch space>:66:1: note: expanded from here
   __pcpu_unique_cached_percpu_stats
   ^
   kernel/cgroup/rstat.c:519:9: note: previous declaration is here
   include/linux/percpu-defs.h:115:2: note: expanded from macro 'DEFINE_PER_CPU'
           DEFINE_PER_CPU_SECTION(type, name, "")
           ^
   include/linux/percpu-defs.h:92:33: note: expanded from macro 'DEFINE_PER_CPU_SECTION'
           extern __PCPU_DUMMY_ATTRS char __pcpu_unique_##name;            \
                                          ^
   <scratch space>:65:1: note: expanded from here
   __pcpu_unique_cached_percpu_stats
   ^
>> kernel/cgroup/rstat.c:519:9: error: 'section' attribute only applies to functions, global variables, Objective-C methods, and Objective-C properties
           static DEFINE_PER_CPU(struct cgroup_base_stat, cached_percpu_stats);
                  ^
   include/linux/percpu-defs.h:115:2: note: expanded from macro 'DEFINE_PER_CPU'
           DEFINE_PER_CPU_SECTION(type, name, "")
           ^
   include/linux/percpu-defs.h:95:2: note: expanded from macro 'DEFINE_PER_CPU_SECTION'
           __PCPU_ATTRS(sec) __weak __typeof__(type) name
           ^
   include/linux/percpu-defs.h:50:26: note: expanded from macro '__PCPU_ATTRS'
           __percpu __attribute__((section(PER_CPU_BASE_SECTION sec)))     \
                                   ^
>> kernel/cgroup/rstat.c:519:49: error: non-extern declaration of 'cached_percpu_stats' follows extern declaration
           static DEFINE_PER_CPU(struct cgroup_base_stat, cached_percpu_stats);
                                                          ^
   kernel/cgroup/rstat.c:519:49: note: previous declaration is here
>> kernel/cgroup/rstat.c:519:9: error: weak declaration cannot have internal linkage
           static DEFINE_PER_CPU(struct cgroup_base_stat, cached_percpu_stats);
                  ^
   include/linux/percpu-defs.h:115:2: note: expanded from macro 'DEFINE_PER_CPU'
           DEFINE_PER_CPU_SECTION(type, name, "")
           ^
   include/linux/percpu-defs.h:95:20: note: expanded from macro 'DEFINE_PER_CPU_SECTION'
           __PCPU_ATTRS(sec) __weak __typeof__(type) name
                             ^
   include/linux/compiler_attributes.h:315:56: note: expanded from macro '__weak'
   #define __weak                          __attribute__((__weak__))
                                                          ^

5 errors generated.

vim +/__section__ +519 kernel/cgroup/rstat.c

515
516 void cgroup_base_stat_percpu_cputime_show(struct seq_file *seq)
517 {
518 static DEFINE_MUTEX(mutex);

 > 519		static DEFINE_PER_CPU(struct cgroup_base_stat, cached_percpu_stats);

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

maintainer
#3 lore raw
In reply to Josh Don
Quoted 16 lines · Josh Don
kernel/cgroup/rstat.c +16 -0
+	seq_puts(seq, "usage_usec");
+	for_each_possible_cpu(cpu) {
+		cached_bstat = per_cpu_ptr(&cached_percpu_stats, cpu);
+		val = cached_bstat->cputime.sum_exec_runtime;
+		do_div(val, NSEC_PER_USEC);
+		seq_printf(seq, " %llu", val);
+	}
+	seq_puts(seq, "\n");
+
+	seq_puts(seq, "user_usec");
+	for_each_possible_cpu(cpu) {
+		cached_bstat = per_cpu_ptr(&cached_percpu_stats, cpu);
+		val = cached_bstat->cputime.utime;
+		do_div(val, NSEC_PER_USEC);
+		seq_printf(seq, " %llu", val);
+	}

kernel/cgroup/rstat.c void cgroup_base_stat_cputime_show(struct seq_file *seq) · line 582

Quoted 10 lines
kernel/cgroup/rstat.c +10 -0
+	seq_puts(seq, "\n");
+
+	seq_puts(seq, "system_usec");
+	for_each_possible_cpu(cpu) {
+		cached_bstat = per_cpu_ptr(&cached_percpu_stats, cpu);
+		val = cached_bstat->cputime.stime;
+		do_div(val, NSEC_PER_USEC);
+		seq_printf(seq, " %llu", val);
+	}
+	seq_puts(seq, "\n");

This is an anti-pattern; given enough CPUs (easy) this will trivially overflow the 1 page seq buffer.

People are already struggling to fix existing ABI, lets not make the problem worse.

author
#4 lore raw
Quoted 33 lines · Peter Zijlstra
Quoted 16 lines · Josh Don
kernel/cgroup/rstat.c +16 -0
+     seq_puts(seq, "usage_usec");
+     for_each_possible_cpu(cpu) {
+             cached_bstat = per_cpu_ptr(&cached_percpu_stats, cpu);
+             val = cached_bstat->cputime.sum_exec_runtime;
+             do_div(val, NSEC_PER_USEC);
+             seq_printf(seq, " %llu", val);
+     }
+     seq_puts(seq, "\n");
+
+     seq_puts(seq, "user_usec");
+     for_each_possible_cpu(cpu) {
+             cached_bstat = per_cpu_ptr(&cached_percpu_stats, cpu);
+             val = cached_bstat->cputime.utime;
+             do_div(val, NSEC_PER_USEC);
+             seq_printf(seq, " %llu", val);
+     }

kernel/cgroup/rstat.c void cgroup_base_stat_cputime_show(struct seq_file *seq) · line 582

Quoted 10 lines
kernel/cgroup/rstat.c +10 -0
+     seq_puts(seq, "\n");
+
+     seq_puts(seq, "system_usec");
+     for_each_possible_cpu(cpu) {
+             cached_bstat = per_cpu_ptr(&cached_percpu_stats, cpu);
+             val = cached_bstat->cputime.stime;
+             do_div(val, NSEC_PER_USEC);
+             seq_printf(seq, " %llu", val);
+     }
+     seq_puts(seq, "\n");

This is an anti-pattern; given enough CPUs (easy) this will trivially overflow the 1 page seq buffer.

Quoted 2 lines

People are already struggling to fix existing ABI, lets not make the problem worse.

Is the concern there just the extra overhead from making multiple trips into this handler and re-allocating the buffer until it is large enough to take all the output? In that case, we could pre-allocate with a size of the right order of magnitude, similar to /proc/stat.

Lack of per-cpu stats is a gap between cgroup v1 and v2, for which v2 can easily support this interface given that it already tracks the stats percpu internally. I opted to dump them all in a single file here, to match the consolidation that occurred from cpuacct->cpu.stat.

maintainer
#5 lore raw
Quoted 42 lines · Josh Don
Quoted 33 lines · Peter Zijlstra
Quoted 16 lines · Josh Don
kernel/cgroup/rstat.c +16 -0
+     seq_puts(seq, "usage_usec");
+     for_each_possible_cpu(cpu) {
+             cached_bstat = per_cpu_ptr(&cached_percpu_stats, cpu);
+             val = cached_bstat->cputime.sum_exec_runtime;
+             do_div(val, NSEC_PER_USEC);
+             seq_printf(seq, " %llu", val);
+     }
+     seq_puts(seq, "\n");
+
+     seq_puts(seq, "user_usec");
+     for_each_possible_cpu(cpu) {
+             cached_bstat = per_cpu_ptr(&cached_percpu_stats, cpu);
+             val = cached_bstat->cputime.utime;
+             do_div(val, NSEC_PER_USEC);
+             seq_printf(seq, " %llu", val);
+     }

kernel/cgroup/rstat.c void cgroup_base_stat_cputime_show(struct seq_file *seq) · line 582

Quoted 10 lines
kernel/cgroup/rstat.c +10 -0
+     seq_puts(seq, "\n");
+
+     seq_puts(seq, "system_usec");
+     for_each_possible_cpu(cpu) {
+             cached_bstat = per_cpu_ptr(&cached_percpu_stats, cpu);
+             val = cached_bstat->cputime.stime;
+             do_div(val, NSEC_PER_USEC);
+             seq_printf(seq, " %llu", val);
+     }
+     seq_puts(seq, "\n");

This is an anti-pattern; given enough CPUs (easy) this will trivially overflow the 1 page seq buffer.

Quoted 2 lines

People are already struggling to fix existing ABI, lets not make the problem worse.

Is the concern there just the extra overhead from making multiple trips into this handler and re-allocating the buffer until it is large enough to take all the output? In that case, we could pre-allocate with a size of the right order of magnitude, similar to /proc/stat.

Quoted 4 lines

Lack of per-cpu stats is a gap between cgroup v1 and v2, for which v2 can easily support this interface given that it already tracks the stats percpu internally. I opted to dump them all in a single file here, to match the consolidation that occurred from cpuacct->cpu.stat.

Hmm.. fancy new stuff there :-) Yes, I think that would aleviate the immediate problem. I suppose /proc/interrupts ought to get some of that too.

Still, I'm not sure having so much data in a single file is wise. But I've not really kept up with the discussions around this problem much.

maintainer
#6 lore raw
In reply to Josh Don

Hello,

Quoted 9 lines · Josh Don

Is the concern there just the extra overhead from making multiple trips into this handler and re-allocating the buffer until it is large enough to take all the output? In that case, we could pre-allocate with a size of the right order of magnitude, similar to /proc/stat.

Lack of per-cpu stats is a gap between cgroup v1 and v2, for which v2 can easily support this interface given that it already tracks the stats percpu internally. I opted to dump them all in a single file here, to match the consolidation that occurred from cpuacct->cpu.stat.

Yeah, nack on this. That part was dropped intentionally. These text pseudo files aren't a great medium for this sort of (potentially large) data dump and they scale really badly with new fields which we may want to expose in the future. For detailed introspection, a better route would be using bpf and if that's inconvenient for some reason trying to make them more convenient.

Thanks.

Signature · 1 line
tejun
author
#7 lore raw

Hi Tejun,

Quoted 14 lines · Tejun Heo

Hello,

Quoted 9 lines · Josh Don

Is the concern there just the extra overhead from making multiple trips into this handler and re-allocating the buffer until it is large enough to take all the output? In that case, we could pre-allocate with a size of the right order of magnitude, similar to /proc/stat.

Lack of per-cpu stats is a gap between cgroup v1 and v2, for which v2 can easily support this interface given that it already tracks the stats percpu internally. I opted to dump them all in a single file here, to match the consolidation that occurred from cpuacct->cpu.stat.

Quoted 6 lines

Yeah, nack on this. That part was dropped intentionally. These text pseudo files aren't a great medium for this sort of (potentially large) data dump and they scale really badly with new fields which we may want to expose in the future. For detailed introspection, a better route would be using bpf and if that's inconvenient for some reason trying to make them more convenient.

Fair enough. Apart from needing to expose an rstat flush mechanism to BPF, the main issues that come to mind are conveniently associating with a particular cgroup, and allowing non-root to collect stats independently. Hao's patch series
(https://lkml.kernel.org/r/%3C20220112192547.3054575-1-haoluo-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org%3E)
makes this particularly convenient, but that's a topic for a separate conversation :)