Skip to contentSkip to conversation

Simple stats for cpu resource controller

RFC In review Balaji Rao 6 participants 24 messages Apr 5, 2008 – May 13, 2008 2 messages not in the archive

Subsystems (3)
lore
Share & tools
lore

Balaji Rao

author
#0 lore raw

RFC 1/2 Simple stats for cpu resource controller

This patch implements some trivial statistics for the CPU controller.

As per our current requirements, we have decided to keep the stats tick based as opposed to using CFS precise accounting.

CC Balbir Singh <[email protected]>
CC Dhaval Giani <[email protected]>
kernel/sched.c +91 -0
index 8206fda..e2acf06 100644
@@ -164,10 +164,38 @@ struct cfs_rq;
 
 static LIST_HEAD(task_groups);
 
+#ifdef CONFIG_CGROUP_SCHED
+enum cpu_cgroup_stat_index {
+	CPU_CGROUP_STAT_UTIME, /* Usertime of the task group */
+	CPU_CGROUP_STAT_STIME, /* Kerneltime of the task group */
+
+	CPU_CGROUP_STAT_NSTATS,
+};
+
+struct cpu_cgroup_stat_cpu {
+	s64 count[CPU_CGROUP_STAT_NSTATS];
+} ____cacheline_aligned_in_smp;
+
+struct cpu_cgroup_stat {
+	struct cpu_cgroup_stat_cpu cpustat[NR_CPUS];
+};
+
+/* Called under irq disable. */
+static void __cpu_cgroup_stat_add(struct cpu_cgroup_stat *stat,
+		enum cpu_cgroup_stat_index idx, int val)
+{
+	int cpu = smp_processor_id();
+
+	BUG_ON(!irqs_disabled());
+	stat->cpustat[cpu].count[idx] += val;
+}
+#endif
+
 /* task group related information */
 struct task_group {
 #ifdef CONFIG_CGROUP_SCHED
 	struct cgroup_subsys_state css;
+	struct cpu_cgroup_stat stat;
 #endif
 
 #ifdef CONFIG_FAIR_GROUP_SCHED
@@ -3733,6 +3761,16 @@ void account_user_time(struct task_struct *p, cputime_t cputime)
 		cpustat->nice = cputime64_add(cpustat->nice, tmp);
 	else
 		cpustat->user = cputime64_add(cpustat->user, tmp);
+
+	/* Charge the task's group */
+#ifdef CONFIG_CGROUP_SCHED
+	{
+	struct task_group *tg;
+	tg = task_group(p);
+	__cpu_cgroup_stat_add(&tg->stat, CPU_CGROUP_STAT_UTIME,
+			cputime_to_msecs(cputime));
+	}
+#endif
 }
 
 /*
@@ -3796,6 +3834,15 @@ void account_system_time(struct task_struct *p, int hardirq_offset,
 		cpustat->idle = cputime64_add(cpustat->idle, tmp);
 	/* Account for system time used */
 	acct_update_integrals(p);
+
+#ifdef CONFIG_CGROUP_SCHED
+	{
+	struct task_group *tg;
+	tg = task_group(p);
+	__cpu_cgroup_stat_add(&tg->stat, CPU_CGROUP_STAT_STIME,
+			cputime_to_msecs(cputime));
+	}
+#endif
 }
 
 /*
@@ -8004,6 +8051,45 @@ static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
 
 	return (u64) tg->shares;
 }
+
+static s64 cpu_cgroup_read_stat(struct cpu_cgroup_stat *stat,
+		enum cpu_cgroup_stat_index idx)
+{
+	int cpu;
+	s64 ret = 0;
+	unsigned long flags;
+
+	local_irq_save(flags);
+	for_each_possible_cpu(cpu)
+		ret += stat->cpustat[cpu].count[idx];
+	local_irq_restore(flags);
+
+	return ret;
+}
+
+static const struct cpu_cgroup_stat_desc {
+	const char *msg;
+	u64 unit;
+} cpu_cgroup_stat_desc[] = {
+	[CPU_CGROUP_STAT_UTIME] = { "utime", 1, },
+	[CPU_CGROUP_STAT_STIME] = { "stime", 1, },
+};
+
+static int cpu_cgroup_stats_show(struct cgroup *cgrp, struct cftype *cft,
+				struct cgroup_map_cb *cb)
+{
+	struct task_group *tg = cgroup_tg(cgrp);
+	struct cpu_cgroup_stat *stat = &tg->stat;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
+		s64 val;
+		val = cpu_cgroup_read_stat(stat, i);
+		val *= cpu_cgroup_stat_desc[i].unit;
+		cb->fill(cb, cpu_cgroup_stat_desc[i].msg, val);
+	}
+	return 0;
+}
 #endif
 
 #ifdef CONFIG_RT_GROUP_SCHED
@@ -8026,6 +8112,11 @@ static struct cftype cpu_files[] = {
 		.read_u64 = cpu_shares_read_u64,
 		.write_u64 = cpu_shares_write_u64,
 	},
+
+	{
+		.name = "stat",
+		.read_map = cpu_cgroup_stats_show,
+	},
 #endif
 #ifdef CONFIG_RT_GROUP_SCHED
 	{
Signature · 4 lines
regards,
Balaji Rao
Dept. of Mechanical Engineering,
National Institute of Technology Karnataka, India
#1 lore raw
Quoted 4 lines · Balaji Rao

This patch implements some trivial statistics for the CPU controller.

As per our current requirements, we have decided to keep the stats tick based as opposed to using CFS precise accounting.

A quick question? Why? Everywhere in the CFS we use precise accounting, (even within the CPU controller). Doesn't make sense using ticks here then.

Will review and respond soon.

Thanks,
Dhaval

Quoted 144 lines
CC Balbir Singh <[email protected]>
CC Dhaval Giani <[email protected]>
kernel/sched.c +91 -0
index 8206fda..e2acf06 100644
@@ -164,10 +164,38 @@ struct cfs_rq;
 
 static LIST_HEAD(task_groups);
 
+#ifdef CONFIG_CGROUP_SCHED
+enum cpu_cgroup_stat_index {
+	CPU_CGROUP_STAT_UTIME, /* Usertime of the task group */
+	CPU_CGROUP_STAT_STIME, /* Kerneltime of the task group */
+
+	CPU_CGROUP_STAT_NSTATS,
+};
+
+struct cpu_cgroup_stat_cpu {
+	s64 count[CPU_CGROUP_STAT_NSTATS];
+} ____cacheline_aligned_in_smp;
+
+struct cpu_cgroup_stat {
+	struct cpu_cgroup_stat_cpu cpustat[NR_CPUS];
+};
+
+/* Called under irq disable. */
+static void __cpu_cgroup_stat_add(struct cpu_cgroup_stat *stat,
+		enum cpu_cgroup_stat_index idx, int val)
+{
+	int cpu = smp_processor_id();
+
+	BUG_ON(!irqs_disabled());
+	stat->cpustat[cpu].count[idx] += val;
+}
+#endif
+
 /* task group related information */
 struct task_group {
 #ifdef CONFIG_CGROUP_SCHED
 	struct cgroup_subsys_state css;
+	struct cpu_cgroup_stat stat;
 #endif
 
 #ifdef CONFIG_FAIR_GROUP_SCHED
@@ -3733,6 +3761,16 @@ void account_user_time(struct task_struct *p, cputime_t cputime)
 		cpustat->nice = cputime64_add(cpustat->nice, tmp);
 	else
 		cpustat->user = cputime64_add(cpustat->user, tmp);
+
+	/* Charge the task's group */
+#ifdef CONFIG_CGROUP_SCHED
+	{
+	struct task_group *tg;
+	tg = task_group(p);
+	__cpu_cgroup_stat_add(&tg->stat, CPU_CGROUP_STAT_UTIME,
+			cputime_to_msecs(cputime));
+	}
+#endif
 }
 
 /*
@@ -3796,6 +3834,15 @@ void account_system_time(struct task_struct *p, int hardirq_offset,
 		cpustat->idle = cputime64_add(cpustat->idle, tmp);
 	/* Account for system time used */
 	acct_update_integrals(p);
+
+#ifdef CONFIG_CGROUP_SCHED
+	{
+	struct task_group *tg;
+	tg = task_group(p);
+	__cpu_cgroup_stat_add(&tg->stat, CPU_CGROUP_STAT_STIME,
+			cputime_to_msecs(cputime));
+	}
+#endif
 }
 
 /*
@@ -8004,6 +8051,45 @@ static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
 
 	return (u64) tg->shares;
 }
+
+static s64 cpu_cgroup_read_stat(struct cpu_cgroup_stat *stat,
+		enum cpu_cgroup_stat_index idx)
+{
+	int cpu;
+	s64 ret = 0;
+	unsigned long flags;
+
+	local_irq_save(flags);
+	for_each_possible_cpu(cpu)
+		ret += stat->cpustat[cpu].count[idx];
+	local_irq_restore(flags);
+
+	return ret;
+}
+
+static const struct cpu_cgroup_stat_desc {
+	const char *msg;
+	u64 unit;
+} cpu_cgroup_stat_desc[] = {
+	[CPU_CGROUP_STAT_UTIME] = { "utime", 1, },
+	[CPU_CGROUP_STAT_STIME] = { "stime", 1, },
+};
+
+static int cpu_cgroup_stats_show(struct cgroup *cgrp, struct cftype *cft,
+				struct cgroup_map_cb *cb)
+{
+	struct task_group *tg = cgroup_tg(cgrp);
+	struct cpu_cgroup_stat *stat = &tg->stat;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
+		s64 val;
+		val = cpu_cgroup_read_stat(stat, i);
+		val *= cpu_cgroup_stat_desc[i].unit;
+		cb->fill(cb, cpu_cgroup_stat_desc[i].msg, val);
+	}
+	return 0;
+}
 #endif
 
 #ifdef CONFIG_RT_GROUP_SCHED
@@ -8026,6 +8112,11 @@ static struct cftype cpu_files[] = {
 		.read_u64 = cpu_shares_read_u64,
 		.write_u64 = cpu_shares_write_u64,
 	},
+
+	{
+		.name = "stat",
+		.read_map = cpu_cgroup_stats_show,
+	},
 #endif
 #ifdef CONFIG_RT_GROUP_SCHED
 	{
Signature · 5 lines
-- 
regards,
Balaji Rao
Dept. of Mechanical Engineering,
National Institute of Technology Karnataka, India
Signature · 2 lines
regards,
Dhaval
author
#2 lore raw
Quoted 4 lines · Dhaval Giani
Quoted 3 lines · Balaji Rao

This patch implements some trivial statistics for the CPU controller.

As per our current requirements, we have decided to keep the stats tick

based as opposed to using CFS precise accounting.

Quoted 6 lines
Quoted 1 line

A quick question? Why? Everywhere in the CFS we use precise accounting, (even within the CPU controller). Doesn't make sense using ticks here then.

Using CFS precise accounting has a cost (see task_utime and task_stime). Currently our requirements don't call for precise statistics.

Signature · 4 lines
regards,
Balaji Rao
Dept. of Mechanical Engineering,
National Institute of Technology Karnataka, India
#3 lore raw
In reply to Balaji Rao
Quoted 13 lines · Balaji Rao

This patch implements some trivial statistics for the CPU controller.

As per our current requirements, we have decided to keep the stats tick based as opposed to using CFS precise accounting.

CC Balbir Singh <[email protected]>
CC Dhaval Giani <[email protected]>
kernel/sched.c +0 -0
index 8206fda..e2acf06 100644

kernel/sched.c struct cfs_rq; · line 172

Quoted 9 lines
kernel/sched.c +6 -0
@@ -164,10 +164,38 @@ struct cfs_rq;
 
 static LIST_HEAD(task_groups);
 
+#ifdef CONFIG_CGROUP_SCHED
+enum cpu_cgroup_stat_index {
+	CPU_CGROUP_STAT_UTIME, /* Usertime of the task group */
+	CPU_CGROUP_STAT_STIME, /* Kerneltime of the task group */
+
+	CPU_CGROUP_STAT_NSTATS,

why the extra space?

kernel/sched.c struct cfs_rq; · line 176

Quoted 4 lines
kernel/sched.c +4 -0
+};
+
+struct cpu_cgroup_stat_cpu {
+	s64 count[CPU_CGROUP_STAT_NSTATS];

u64? time does not go negative :)
count also is not very clear? Can you give a more descriptive name?

Quoted 62 lines
kernel/sched.c +37 -0
+} ____cacheline_aligned_in_smp;
+
+struct cpu_cgroup_stat {
+	struct cpu_cgroup_stat_cpu cpustat[NR_CPUS];
+};
+
+/* Called under irq disable. */
+static void __cpu_cgroup_stat_add(struct cpu_cgroup_stat *stat,
+		enum cpu_cgroup_stat_index idx, int val)
+{
+	int cpu = smp_processor_id();
+
+	BUG_ON(!irqs_disabled());
+	stat->cpustat[cpu].count[idx] += val;
+}
+#endif
+
 /* task group related information */
 struct task_group {
 #ifdef CONFIG_CGROUP_SCHED
 	struct cgroup_subsys_state css;
+	struct cpu_cgroup_stat stat;
 #endif
 
 #ifdef CONFIG_FAIR_GROUP_SCHED
@@ -3733,6 +3761,16 @@ void account_user_time(struct task_struct *p, cputime_t cputime)
 		cpustat->nice = cputime64_add(cpustat->nice, tmp);
 	else
 		cpustat->user = cputime64_add(cpustat->user, tmp);
+
+	/* Charge the task's group */
+#ifdef CONFIG_CGROUP_SCHED
+	{
+	struct task_group *tg;
+	tg = task_group(p);
+	__cpu_cgroup_stat_add(&tg->stat, CPU_CGROUP_STAT_UTIME,
+			cputime_to_msecs(cputime));
+	}
+#endif
 }
 
 /*
@@ -3796,6 +3834,15 @@ void account_system_time(struct task_struct *p, int hardirq_offset,
 		cpustat->idle = cputime64_add(cpustat->idle, tmp);
 	/* Account for system time used */
 	acct_update_integrals(p);
+
+#ifdef CONFIG_CGROUP_SCHED
+	{
+	struct task_group *tg;
+	tg = task_group(p);
+	__cpu_cgroup_stat_add(&tg->stat, CPU_CGROUP_STAT_STIME,
+			cputime_to_msecs(cputime));
+	}
+#endif
 }
 
 /*
@@ -8004,6 +8051,45 @@ static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
 
 	return (u64) tg->shares;
 }

kernel/sched.c static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft) · line 8063

Quoted 10 lines
kernel/sched.c +9 -0
+
+static s64 cpu_cgroup_read_stat(struct cpu_cgroup_stat *stat,
+		enum cpu_cgroup_stat_index idx)
+{
+	int cpu;
+	s64 ret = 0;
+	unsigned long flags;
 
+
+	local_irq_save(flags);

I am just wondering. Is local_irq_save() enough?

Quoted 46 lines
kernel/sched.c +30 -0
+	for_each_possible_cpu(cpu)
+		ret += stat->cpustat[cpu].count[idx];
+	local_irq_restore(flags);
+
+	return ret;
+}
+
+static const struct cpu_cgroup_stat_desc {
+	const char *msg;
+	u64 unit;
+} cpu_cgroup_stat_desc[] = {
+	[CPU_CGROUP_STAT_UTIME] = { "utime", 1, },
+	[CPU_CGROUP_STAT_STIME] = { "stime", 1, },
+};
+
+static int cpu_cgroup_stats_show(struct cgroup *cgrp, struct cftype *cft,
+				struct cgroup_map_cb *cb)
+{
+	struct task_group *tg = cgroup_tg(cgrp);
+	struct cpu_cgroup_stat *stat = &tg->stat;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
+		s64 val;
+		val = cpu_cgroup_read_stat(stat, i);
+		val *= cpu_cgroup_stat_desc[i].unit;
+		cb->fill(cb, cpu_cgroup_stat_desc[i].msg, val);
+	}
+	return 0;
+}
 #endif
 

#ifdef CONFIG_RT_GROUP_SCHED

kernel/sched.c +5 -0
@@ -8026,6 +8112,11 @@ static struct cftype cpu_files[] = {
 		.read_u64 = cpu_shares_read_u64,
 		.write_u64 = cpu_shares_write_u64,
 	},
+
+	{
+		.name = "stat",
+		.read_map = cpu_cgroup_stats_show,
+	},
 #endif
 #ifdef CONFIG_RT_GROUP_SCHED
 	{
Signature · 2 lines
regards,
Dhaval
author
#4 lore raw

On Sunday 06 April 2008 01:10:41 am Dhaval Giani wrote:
<snip>

Quoted 9 lines

kernel/sched.c struct cfs_rq; · line 172

Quoted 7 lines
kernel/sched.c +6 -0
+#ifdef CONFIG_CGROUP_SCHED
+enum cpu_cgroup_stat_index {
+	CPU_CGROUP_STAT_UTIME, /* Usertime of the task group */
+	CPU_CGROUP_STAT_STIME, /* Kerneltime of the task group */
+
+	CPU_CGROUP_STAT_NSTATS,

why the extra space?

Just to keep things clearly separated. If you've not noticed, CPU_CGROUP_STAT_NSTATS is not a stat.

Quoted 7 lines

kernel/sched.c struct cfs_rq; · line 176

Quoted 5 lines
kernel/sched.c +4 -0
+};
+
+struct cpu_cgroup_stat_cpu {
+	s64 count[CPU_CGROUP_STAT_NSTATS];

u64? time does not go negative :)

Right. But these stats are not only going to measure time. We need the same variables for measuring other stats as well. I'm not sure if we would encounter scheduler stats that would count negative.

Balbir, what do you say ?

Quoted 2 lines

count also is not very clear? Can you give a more descriptive name?

ok. How does 'value' look ?

<snip>

Quoted 7 lines
Quoted 6 lines
kernel/sched.c +6 -0
+static s64 cpu_cgroup_read_stat(struct cpu_cgroup_stat *stat,
+		enum cpu_cgroup_stat_index idx)
+{
+	int cpu;
+	s64 ret = 0;
+	unsigned long flags;
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	local_irq_save(flags);

I am just wondering. Is local_irq_save() enough?

Hmmm.. You are right.This does not prevent concurrent updates on other CPUs from crossing a 32bit boundary. Am not sure how to do this in a safe way. I can only think of using atomic64_t now..

Quoted 7 lines

kernel/sched.c static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft) · line 8069

Quoted 7 lines
kernel/sched.c +7 -0
+	for_each_possible_cpu(cpu)
+		ret += stat->cpustat[cpu].count[idx];
+	local_irq_restore(flags);
+
+	return ret;
+}
+

Thanks for the review.

Signature · 4 lines
regards,
Balaji Rao
Dept. of Mechanical Engineering,
National Institute of Technology Karnataka, India
#5 lore raw
Quoted 7 lines · Balaji Rao
Quoted 6 lines · Dhaval Giani

kernel/sched.c struct cfs_rq; · line 176

Quoted 4 lines
kernel/sched.c +4 -0
+};
+
+struct cpu_cgroup_stat_cpu {
+	s64 count[CPU_CGROUP_STAT_NSTATS];

u64? time does not go negative :)

Quoted 5 lines

Right. But these stats are not only going to measure time. We need the same variables for measuring other stats as well. I'm not sure if we would encounter scheduler stats that would count negative.

Balbir, what do you say ?

I would prefer to keep the stats logically separate. So something like

struct cpu_cgroup_stat_cpu {
	u64 time[];
	s64 some_other_stat;
}

and so on. (I am not sure, is there some advantage gained by using structs?) Makes the code more maintainable imho.

Quoted 14 lines
Quoted 3 lines

count also is not very clear? Can you give a more descriptive name?

ok. How does 'value' look ?

<snip>

Quoted 7 lines
Quoted 6 lines
kernel/sched.c +6 -0
+static s64 cpu_cgroup_read_stat(struct cpu_cgroup_stat *stat,
+		enum cpu_cgroup_stat_index idx)
+{
+	int cpu;
+	s64 ret = 0;
+	unsigned long flags;
Quoted 9 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	local_irq_save(flags);

I am just wondering. Is local_irq_save() enough?

Hmmm.. You are right.This does not prevent concurrent updates on other CPUs from crossing a 32bit boundary. Am not sure how to do this in a safe way. I can only think of using atomic64_t now..

I am going to answer that one when I am awake :-)

Signature · 2 lines
regards,
Dhaval
author
#6 lore raw
Quoted 9 lines · Dhaval Giani
Quoted 8 lines · Balaji Rao
Quoted 6 lines · Dhaval Giani

kernel/sched.c struct cfs_rq; · line 176

Quoted 4 lines
kernel/sched.c +4 -0
+};
+
+struct cpu_cgroup_stat_cpu {
+	s64 count[CPU_CGROUP_STAT_NSTATS];

u64? time does not go negative :)

Right. But these stats are not only going to measure time. We need the

same

Quoted 5 lines
Quoted 4 lines

variables for measuring other stats as well. I'm not sure if we would encounter scheduler stats that would count negative.

Balbir, what do you say ?

Quoted 8 lines

I would prefer to keep the stats logically separate. So something like

struct cpu_cgroup_stat_cpu {
	u64 time[];
	s64 some_other_stat;
}

and so on. (I am not sure, is there some advantage gained by using structs?) Makes the code more maintainable imho.

This would break the generic nature of __cpu_cgroup_stat_add. Its not a nice thing in my opinion.

Signature · 4 lines
regards,
Balaji Rao
Dept. of Mechanical Engineering,
National Institute of Technology Karnataka, India
#7 lore raw
Quoted 22 lines · Balaji Rao
Quoted 8 lines · Dhaval Giani
Quoted 7 lines · Balaji Rao
Quoted 5 lines · Dhaval Giani

kernel/sched.c struct cfs_rq; · line 176

Quoted 4 lines
kernel/sched.c +4 -0
+};
+
+struct cpu_cgroup_stat_cpu {
+	s64 count[CPU_CGROUP_STAT_NSTATS];

u64? time does not go negative :)

Right. But these stats are not only going to measure time. We need the

same

Quoted 12 lines
Quoted 4 lines

variables for measuring other stats as well. I'm not sure if we would encounter scheduler stats that would count negative.

Balbir, what do you say ?

I would prefer to keep the stats logically separate. So something like

struct cpu_cgroup_stat_cpu {
	u64 time[];
	s64 some_other_stat;
}

and so on. (I am not sure, is there some advantage gained by using structs?) Makes the code more maintainable imho.

Quoted 3 lines

This would break the generic nature of __cpu_cgroup_stat_add. Its not a nice thing in my opinion.

I prefer keeping stats in the array as Balaji has done, it makes it easier to do batch processing on the stats.

Signature · 4 lines
	Warm Regards,
	Balbir Singh
	Linux Technology Center
	IBM, ISTL
#8 lore raw
In reply to Balaji Rao
Quoted 16 lines · Balaji Rao
Quoted 7 lines
Quoted 6 lines
kernel/sched.c +6 -0
+static s64 cpu_cgroup_read_stat(struct cpu_cgroup_stat *stat,
+		enum cpu_cgroup_stat_index idx)
+{
+	int cpu;
+	s64 ret = 0;
+	unsigned long flags;
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	local_irq_save(flags);

I am just wondering. Is local_irq_save() enough?

Hmmm.. You are right.This does not prevent concurrent updates on other CPUs from crossing a 32bit boundary. Am not sure how to do this in a safe way. I can only think of using atomic64_t now..

Quoted 7 lines
Quoted 7 lines

kernel/sched.c static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft) · line 8069

Quoted 7 lines
kernel/sched.c +7 -0
+	for_each_possible_cpu(cpu)
+		ret += stat->cpustat[cpu].count[idx];
+	local_irq_restore(flags);
+
+	return ret;
+}
+

So many stats to steal code from,.. but you didn't :-(

Look at mm/vmstat.c, that is a rather complete example.

The trick to solving the above is to use per cpu deltas instead, the deltas can be machine word size and are thus always read in an atomic manner (provided they are also naturally aligned).

author
#9 lore raw
Quoted 9 lines · Peter Zijlstra
Quoted 7 lines · Balaji Rao
Quoted 7 lines
Quoted 6 lines
kernel/sched.c +6 -0
+static s64 cpu_cgroup_read_stat(struct cpu_cgroup_stat *stat,
+		enum cpu_cgroup_stat_index idx)
+{
+	int cpu;
+	s64 ret = 0;
+	unsigned long flags;
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	local_irq_save(flags);

I am just wondering. Is local_irq_save() enough?

Hmmm.. You are right.This does not prevent concurrent updates on other

CPUs

Quoted 1 line
Quoted 1 line

from crossing a 32bit boundary. Am not sure how to do this in a safe way.

I

Quoted 12 lines
Quoted 9 lines

can only think of using atomic64_t now..

Quoted 7 lines

kernel/sched.c static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft) · line 8069

Quoted 7 lines
kernel/sched.c +7 -0
+	for_each_possible_cpu(cpu)
+		ret += stat->cpustat[cpu].count[idx];
+	local_irq_restore(flags);
+
+	return ret;
+}
+

So many stats to steal code from,.. but you didn't :-(

Quoted 7 lines

Look at mm/vmstat.c, that is a rather complete example.

The trick to solving the above is to use per cpu deltas instead, the deltas can be machine word size and are thus always read in an atomic manner (provided they are also naturally aligned).

Hi Peter,

This wont work for time based statistics. At nsec granularity, a word can hold a time value of up to ~4s.

I propose to solve this problem by using a lock to protect the statistics, but only on 32bit architectures.

I'm not sure how good a solution this is, but that's the best I can think of ATM.

Signature · 4 lines
regards,
Balaji Rao
Dept. of Mechanical Engineering,
National Institute of Technology Karnataka, India
#10 lore raw
Quoted 38 lines · Balaji Rao
Quoted 9 lines · Peter Zijlstra
Quoted 7 lines · Balaji Rao
Quoted 7 lines
Quoted 6 lines
kernel/sched.c +6 -0
+static s64 cpu_cgroup_read_stat(struct cpu_cgroup_stat *stat,
+		enum cpu_cgroup_stat_index idx)
+{
+	int cpu;
+	s64 ret = 0;
+	unsigned long flags;
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	local_irq_save(flags);

I am just wondering. Is local_irq_save() enough?

Hmmm.. You are right.This does not prevent concurrent updates on other

CPUs

Quoted 1 line
Quoted 1 line

from crossing a 32bit boundary. Am not sure how to do this in a safe way.

I

Quoted 12 lines
Quoted 9 lines

can only think of using atomic64_t now..

Quoted 7 lines

kernel/sched.c static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft) · line 8069

Quoted 7 lines
kernel/sched.c +7 -0
+	for_each_possible_cpu(cpu)
+		ret += stat->cpustat[cpu].count[idx];
+	local_irq_restore(flags);
+
+	return ret;
+}
+

So many stats to steal code from,.. but you didn't :-(

Quoted 7 lines

Look at mm/vmstat.c, that is a rather complete example.

The trick to solving the above is to use per cpu deltas instead, the deltas can be machine word size and are thus always read in an atomic manner (provided they are also naturally aligned).

Quoted 4 lines

Hi Peter,

This wont work for time based statistics. At nsec granularity, a word can hold a time value of up to ~4s.

4 seconds is plenty for a delta, most increments are in the ms range.

Quoted 5 lines

I propose to solve this problem by using a lock to protect the statistics, but only on 32bit architectures.

I'm not sure how good a solution this is, but that's the best I can think of ATM.

Not needed, keep per cpu word deltas and fold into a global u64 counter while holding a lock.

author
#11 lore raw

RFC 1/1 Simple stats for cpu resource controller v3

This implements a couple of basic statistics for the CPU resource controller.

v2->v3
-------
Proper locking while collecting stats. Thanks to Peter Zijlstra for suggesting the delta approach.

This applies against 2.6.25-mm1
---

kernel/sched.c +109 -1
index bbdc32a..5bda75a 100644
@@ -248,10 +248,51 @@ struct cfs_rq;
 
 static LIST_HEAD(task_groups);
 
+#ifdef CONFIG_CGROUP_SCHED
+
+#define CPU_CGROUP_STAT_THRESHOLD 1 << 30
+
+enum cpu_cgroup_stat_index {
+	CPU_CGROUP_STAT_UTIME, /* Usertime of the task group */
+	CPU_CGROUP_STAT_STIME, /* Kerneltime of the task group */
+
+	CPU_CGROUP_STAT_NSTATS,
+};
+
+struct cpu_cgroup_stat_cpu {
+	s64 count[CPU_CGROUP_STAT_NSTATS];
+	u32 delta[CPU_CGROUP_STAT_NSTATS];
+} ____cacheline_aligned_in_smp;
+
+struct cpu_cgroup_stat {
+	struct cpu_cgroup_stat_cpu cpustat[NR_CPUS];
+	spinlock_t lock;
+};
+
+/* Called under irq disable. */
+static void __cpu_cgroup_stat_add(struct cpu_cgroup_stat *stat,
+		enum cpu_cgroup_stat_index idx, int val)
+{
+	int cpu = smp_processor_id();
+	unsigned long flags;
+
+	BUG_ON(!irqs_disabled());
+	stat->cpustat[cpu].delta[idx] += val;
+
+	if (stat->cpustat[cpu].delta[idx] > CPU_CGROUP_STAT_THRESHOLD) {
+		spin_lock_irqsave(&stat->lock, flags);
+		stat->cpustat[cpu].count[idx] += stat->cpustat[cpu].delta[idx];
+		stat->cpustat[cpu].delta[idx] = 0;
+		spin_unlock_irqrestore(&stat->lock, flags);
+	}
+}
+#endif
+
 /* task group related information */
 struct task_group {
 #ifdef CONFIG_CGROUP_SCHED
 	struct cgroup_subsys_state css;
+	struct cpu_cgroup_stat stat;
 #endif
 
 #ifdef CONFIG_FAIR_GROUP_SCHED
@@ -3837,6 +3878,16 @@ void account_user_time(struct task_struct *p, cputime_t cputime)
 		cpustat->nice = cputime64_add(cpustat->nice, tmp);
 	else
 		cpustat->user = cputime64_add(cpustat->user, tmp);
+
+	/* Charge the task's group */
+#ifdef CONFIG_CGROUP_SCHED
+	{
+		struct task_group *tg;
+		tg = task_group(p);
+		__cpu_cgroup_stat_add(&tg->stat, CPU_CGROUP_STAT_UTIME,
+				cputime_to_msecs(cputime));
+	}
+#endif
 }
 
 /*
@@ -3900,6 +3951,15 @@ void account_system_time(struct task_struct *p, int hardirq_offset,
 		cpustat->idle = cputime64_add(cpustat->idle, tmp);
 	/* Account for system time used */
 	acct_update_integrals(p);
+
+#ifdef CONFIG_CGROUP_SCHED
+	{
+		struct task_group *tg;
+		tg = task_group(p);
+		__cpu_cgroup_stat_add(&tg->stat, CPU_CGROUP_STAT_STIME,
+				cputime_to_msecs(cputime));
+	}
+#endif
 }
 
 /*
@@ -7838,7 +7898,9 @@ struct task_group *sched_create_group(void)
 	}
 	list_add_rcu(&tg->list, &task_groups);
 	spin_unlock_irqrestore(&task_group_lock, flags);
-
+#ifdef CONFIG_CGROUP_SCHED
+	spin_lock_init(&tg->stat.lock);
+#endif
 	return tg;
 
 err:
@@ -8249,6 +8311,48 @@ static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
 
 	return (u64) tg->shares;
 }
+
+static s64 cpu_cgroup_read_stat(struct cpu_cgroup_stat *stat,
+		enum cpu_cgroup_stat_index idx)
+{
+	int cpu;
+	s64 ret = 0;
+	unsigned long flags;
+
+	spin_lock_irqsave(&stat->lock, flags);
+	for_each_possible_cpu(cpu) {
+		stat->cpustat[cpu].count[idx] += stat->cpustat[cpu].delta[idx];
+		stat->cpustat[cpu].delta[idx] = 0;
+		ret += stat->cpustat[cpu].count[idx];
+	}
+	spin_unlock_irqrestore(&stat->lock, flags);
+
+	return ret;
+}
+
+static const struct cpu_cgroup_stat_desc {
+	const char *msg;
+	u64 unit;
+} cpu_cgroup_stat_desc[] = {
+	[CPU_CGROUP_STAT_UTIME] = { "utime", 1, },
+	[CPU_CGROUP_STAT_STIME] = { "stime", 1, },
+};
+
+static int cpu_cgroup_stats_show(struct cgroup *cgrp, struct cftype *cft,
+				struct cgroup_map_cb *cb)
+{
+	struct task_group *tg = cgroup_tg(cgrp);
+	struct cpu_cgroup_stat *stat = &tg->stat;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
+		s64 val;
+		val = cpu_cgroup_read_stat(stat, i);
+		val *= cpu_cgroup_stat_desc[i].unit;
+		cb->fill(cb, cpu_cgroup_stat_desc[i].msg, val);
+	}
+	return 0;
+}
 #endif
 
 #ifdef CONFIG_RT_GROUP_SCHED
@@ -8295,6 +8399,10 @@ static struct cftype cpu_files[] = {
 		.write_u64 = cpu_rt_period_write_uint,
 	},
 #endif
+	{
+		.name = "stat",
+		.read_map = cpu_cgroup_stats_show,
+	},
 };
 
 static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
Signature · 5 lines
Warm Regards,

Balaji Rao
Dept. of Mechanical Engineering
NITK
maintainer
#12 lore raw

Re: [RFC][-mm] Simple stats for cpu resource controller v3

Quoted 17 lines · On Thu, 1 May 2008 23:11:06 +0530 Balaji Rao <[email protected]> wrote:

This implements a couple of basic statistics for the CPU resource controller.

v2->v3
-------
Proper locking while collecting stats. Thanks to Peter Zijlstra for suggesting the delta approach.

This applies against 2.6.25-mm1
---

kernel/sched.c +0 -0
index bbdc32a..5bda75a 100644

kernel/sched.c struct cfs_rq; · line 253

Quoted 6 lines
kernel/sched.c +3 -0
@@ -248,10 +248,51 @@ struct cfs_rq;
 
 static LIST_HEAD(task_groups);
 
+#ifdef CONFIG_CGROUP_SCHED
+
+#define CPU_CGROUP_STAT_THRESHOLD 1 << 30

Needs a comment, and parentheses?

kernel/sched.c struct cfs_rq; · line 265

Quoted 11 lines
kernel/sched.c +11 -0
+enum cpu_cgroup_stat_index {
+	CPU_CGROUP_STAT_UTIME, /* Usertime of the task group */
+	CPU_CGROUP_STAT_STIME, /* Kerneltime of the task group */
+
+	CPU_CGROUP_STAT_NSTATS,
+};
+
+struct cpu_cgroup_stat_cpu {
+	s64 count[CPU_CGROUP_STAT_NSTATS];
+	u32 delta[CPU_CGROUP_STAT_NSTATS];
+} ____cacheline_aligned_in_smp;

That's 128 bytes on ia64.

kernel/sched.c struct cfs_rq; · line 270

Quoted 4 lines
kernel/sched.c +4 -0
+struct cpu_cgroup_stat {
+	struct cpu_cgroup_stat_cpu cpustat[NR_CPUS];
+	spinlock_t lock;
+};

And with NR_CPUS=1024, we're starting to talk serious pigginess.

And there's one of these per task_group! Chances are we just won't be able to allocate that much contiguous memory, so that will solve the problem ;)

kernel/sched.c struct cfs_rq; · line 279

Quoted 8 lines
kernel/sched.c +8 -0
+/* Called under irq disable. */
+static void __cpu_cgroup_stat_add(struct cpu_cgroup_stat *stat,
+		enum cpu_cgroup_stat_index idx, int val)
+{
+	int cpu = smp_processor_id();
+	unsigned long flags;
+
+	BUG_ON(!irqs_disabled());

Ok...

kernel/sched.c struct cfs_rq; · line 283

Quoted 4 lines
kernel/sched.c +4 -0
+	stat->cpustat[cpu].delta[idx] += val;
+
+	if (stat->cpustat[cpu].delta[idx] > CPU_CGROUP_STAT_THRESHOLD) {
+		spin_lock_irqsave(&stat->lock, flags);

so we could have used plain old spin_lock() here.

Quoted 71 lines
kernel/sched.c +38 -1
+		stat->cpustat[cpu].count[idx] += stat->cpustat[cpu].delta[idx];
+		stat->cpustat[cpu].delta[idx] = 0;
+		spin_unlock_irqrestore(&stat->lock, flags);
+	}
+}
+#endif
+
 /* task group related information */
 struct task_group {
 #ifdef CONFIG_CGROUP_SCHED
 	struct cgroup_subsys_state css;
+	struct cpu_cgroup_stat stat;
 #endif
 
 #ifdef CONFIG_FAIR_GROUP_SCHED
@@ -3837,6 +3878,16 @@ void account_user_time(struct task_struct *p, cputime_t cputime)
 		cpustat->nice = cputime64_add(cpustat->nice, tmp);
 	else
 		cpustat->user = cputime64_add(cpustat->user, tmp);
+
+	/* Charge the task's group */
+#ifdef CONFIG_CGROUP_SCHED
+	{
+		struct task_group *tg;
+		tg = task_group(p);
+		__cpu_cgroup_stat_add(&tg->stat, CPU_CGROUP_STAT_UTIME,
+				cputime_to_msecs(cputime));
+	}
+#endif
 }
 
 /*
@@ -3900,6 +3951,15 @@ void account_system_time(struct task_struct *p, int hardirq_offset,
 		cpustat->idle = cputime64_add(cpustat->idle, tmp);
 	/* Account for system time used */
 	acct_update_integrals(p);
+
+#ifdef CONFIG_CGROUP_SCHED
+	{
+		struct task_group *tg;
+		tg = task_group(p);
+		__cpu_cgroup_stat_add(&tg->stat, CPU_CGROUP_STAT_STIME,
+				cputime_to_msecs(cputime));
+	}
+#endif
 }
 
 /*
@@ -7838,7 +7898,9 @@ struct task_group *sched_create_group(void)
 	}
 	list_add_rcu(&tg->list, &task_groups);
 	spin_unlock_irqrestore(&task_group_lock, flags);
-
+#ifdef CONFIG_CGROUP_SCHED
+	spin_lock_init(&tg->stat.lock);
+#endif
 	return tg;
 
 err:
@@ -8249,6 +8311,48 @@ static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
 
 	return (u64) tg->shares;
 }
+
+static s64 cpu_cgroup_read_stat(struct cpu_cgroup_stat *stat,
+		enum cpu_cgroup_stat_index idx)
+{
+	int cpu;
+	s64 ret = 0;
+	unsigned long flags;
+

kernel/sched.c static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft) · line 8331

Quoted 10 lines
kernel/sched.c +10 -0
+	spin_lock_irqsave(&stat->lock, flags);
+	for_each_possible_cpu(cpu) {
+		stat->cpustat[cpu].count[idx] += stat->cpustat[cpu].delta[idx];
+		stat->cpustat[cpu].delta[idx] = 0;
+		ret += stat->cpustat[cpu].count[idx];
+	}
+	spin_unlock_irqrestore(&stat->lock, flags);
+
+	return ret;
+}

That loop iterates 1024 times, under spin_lock_irqsave(). On a 2-way. Sad, no?

Quoted 27 lines
kernel/sched.c +23 -0
+static const struct cpu_cgroup_stat_desc {
+	const char *msg;
+	u64 unit;
+} cpu_cgroup_stat_desc[] = {
+	[CPU_CGROUP_STAT_UTIME] = { "utime", 1, },
+	[CPU_CGROUP_STAT_STIME] = { "stime", 1, },
+};
+
+static int cpu_cgroup_stats_show(struct cgroup *cgrp, struct cftype *cft,
+				struct cgroup_map_cb *cb)
+{
+	struct task_group *tg = cgroup_tg(cgrp);
+	struct cpu_cgroup_stat *stat = &tg->stat;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
+		s64 val;
+		val = cpu_cgroup_read_stat(stat, i);
+		val *= cpu_cgroup_stat_desc[i].unit;
+		cb->fill(cb, cpu_cgroup_stat_desc[i].msg, val);
+	}
+	return 0;
+}
 #endif
 
 #ifdef CONFIG_RT_GROUP_SCHED

kernel/sched.c static struct cftype cpu_files[] = { · line 8407

Quoted 9 lines
kernel/sched.c +4 -0
@@ -8295,6 +8399,10 @@ static struct cftype cpu_files[] = {
 		.write_u64 = cpu_rt_period_write_uint,
 	},
 #endif
+	{
+		.name = "stat",
+		.read_map = cpu_cgroup_stats_show,
+	},
 };
 

Rethink, please...

There are numerous reasons here for implementing the counters as dynamically-allocated, per-online-cpu things, with a cpu-hotplug notifier. All a bit of a hassle, but that's life.

Anyway, forget all that.

Did you consider using include/linux/percpu_counter.h?

If so, what was wrong with it?

Because it would be much better to fix per-cpu counters than to invent new stuff.

author
#13 lore raw

Re: [RFC][-mm] Simple stats for cpu resource controller v3

On Friday 02 May 2008 02:30:26 am Andrew Morton wrote:
<snip>

Hi Andrew,

Thank you for the review.

Quoted 7 lines

Did you consider using include/linux/percpu_counter.h?

If so, what was wrong with it?

Because it would be much better to fix per-cpu counters than to invent new stuff.

No, I hadn't consider using the percpu_counters infrastructure. But today when I tried using it, I got an early exception.I guess its because I tried calling percpu_counter_init from within sched_init, which I perhaps shouldn't do, because percpu_counter_init expects cpu hotplug code to be initialized by then. Right ? Correct me if I'm wrong.

How about we start collecting statistics at a later stage i.e, after percpu_counter becomes usable ?

Signature · 5 lines
Warm Regards,

Balaji Rao
Dept. of Mechanical Engineering
NITK
maintainer
#14 lore raw

Re: [RFC][-mm] Simple stats for cpu resource controller v3

Quoted 11 lines · On Sat, 3 May 2008 01:10:28 +0530 Balaji Rao…

On Friday 02 May 2008 02:30:26 am Andrew Morton wrote:
<snip>

Hi Andrew,

Thank you for the review.

Quoted 5 lines

Did you consider using include/linux/percpu_counter.h?

If so, what was wrong with it?

Quoted 7 lines
Quoted 2 lines

Because it would be much better to fix per-cpu counters than to invent new stuff.

No, I hadn't consider using the percpu_counters infrastructure. But today when I tried using it, I got an early exception.I guess its because I tried calling percpu_counter_init from within sched_init, which I perhaps shouldn't do, because percpu_counter_init expects cpu hotplug code to be initialized by then. Right ? Correct me if I'm wrong.

I don't see any reason why we cannot run percpu_counter_init() prior to running percpu_counter_startup(). And it is desirable that we be able to start using the percpu-counters quite early.

Can you debug it a bit please? It's probably some silly little thing, perhaps fixable by calling percpu_counter_startup() earlier.

Quoted 2 lines

How about we start collecting statistics at a later stage i.e, after percpu_counter becomes usable ?

It would be better to make the core infrastructure more robust, rather than working around problems it might have.

It's rather nice that percpu_counters internally take care of cpu-hotplugging, and use cpu_online_map. I was amazed at how easily that was added. I still expect it to break somehow..

author
#15 lore raw

Re: [RFC][-mm] Simple stats for cpu resource controller v3

Quoted 23 lines · Andrew Morton

On Sat, 3 May 2008 01:10:28 +0530

Quoted 11 lines · Balaji Rao

On Friday 02 May 2008 02:30:26 am Andrew Morton wrote:
<snip>

Hi Andrew,

Thank you for the review.

Quoted 4 lines

Did you consider using include/linux/percpu_counter.h?

If so, what was wrong with it?

Quoted 8 lines
Quoted 2 lines

Because it would be much better to fix per-cpu counters than to invent new stuff.

No, I hadn't consider using the percpu_counters infrastructure. But today when I tried using it, I got an early exception.I guess its because I tried calling percpu_counter_init from within sched_init, which I perhaps shouldn't do, because percpu_counter_init expects cpu hotplug code to be initialized by then. Right ? Correct me if I'm wrong.

Quoted 7 lines

I don't see any reason why we cannot run percpu_counter_init() prior to running percpu_counter_startup(). And it is desirable that we be able to start using the percpu-counters quite early.

Can you debug it a bit please? It's probably some silly little thing, perhaps fixable by calling percpu_counter_startup() earlier.

percpu_counter_init uses kmalloc to create percpu counters. This raises an early exception as kmem_cache is not initialized that early.

It worked for me if we statically allocate memory for the counters. But its not at all a nice thing to do and I don't see another way to make it fit for early use.

I'm beginning to run out of ideas! Why not do what I earlier suggested - begin collecting statistics once we are able to safely use percpu_counters ? This now seems to be the best alternative IMHO.

Signature · 5 lines
Warm Regards,

Balaji Rao
Dept. of Mechanical Engineering
NITK
maintainer
#16 lore raw

Re: [RFC][-mm] Simple stats for cpu resource controller v3

Quoted 28 lines · On Sat, 3 May 2008 04:17:03 +0530 Balaji Rao <[email protected]> wrote:
Quoted 27 lines · Andrew Morton

On Sat, 3 May 2008 01:10:28 +0530

Quoted 11 lines · Balaji Rao

On Friday 02 May 2008 02:30:26 am Andrew Morton wrote:
<snip>

Hi Andrew,

Thank you for the review.

Quoted 4 lines

Did you consider using include/linux/percpu_counter.h?

If so, what was wrong with it?

Quoted 8 lines
Quoted 2 lines

Because it would be much better to fix per-cpu counters than to invent new stuff.

No, I hadn't consider using the percpu_counters infrastructure. But today when I tried using it, I got an early exception.I guess its because I tried calling percpu_counter_init from within sched_init, which I perhaps shouldn't do, because percpu_counter_init expects cpu hotplug code to be initialized by then. Right ? Correct me if I'm wrong.

I don't see any reason why we cannot run percpu_counter_init() prior to running percpu_counter_startup(). And it is desirable that we be able to start using the percpu-counters quite early.

Quoted 5 lines
Quoted 3 lines

Can you debug it a bit please? It's probably some silly little thing, perhaps fixable by calling percpu_counter_startup() earlier.

percpu_counter_init uses kmalloc to create percpu counters. This raises an early exception as kmem_cache is not initialized that early.

whaa? kmalloc is ready to be used quite early in boot. It's a bit of a concern that the CPU resource controller is doing stuff before even kmalloc is ready to go.

What's the call path here? Via cgroup_init_early()? Does it need to run that early?

Quoted 7 lines

It worked for me if we statically allocate memory for the counters. But its not at all a nice thing to do and I don't see another way to make it fit for early use.

I'm beginning to run out of ideas! Why not do what I earlier suggested - begin collecting statistics once we are able to safely use percpu_counters ? This now seems to be the best alternative IMHO.

I'd need to see the code. If we end up doing

	if (counters_are_ready)
		increment_counter();

all over then place then we need to think harder.

Maybe we need a cgroup_init_late(), which can do memory allocations. If nothing actually needs to touch the counters before cgroup_init_late() runs then that might be OK.

author
#19 lore raw

Re: [RFC][-mm] Simple stats for cpu resource controller v3

In reply to a message not in the archive

On Saturday 03 May 2008 05:11:33 am Andrew Morton wrote:

<snip>

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

percpu_counter_init uses kmalloc to create percpu counters. This raises an early exception as kmem_cache is not initialized that early.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

whaa? kmalloc is ready to be used quite early in boot. It's a bit of a concern that the CPU resource controller is doing stuff before even kmalloc is ready to go.

No it doesn't. I placed the call to percpu_counter_init in sched_init. Hence the problem.

I'm groping in the dark without seeing the diff.

oh! am sorry about that! some cosmetic changes remain. It also contains some modifications done to the percpu_counter core.

<snip>

Quoted 6 lines
Quoted 4 lines

For the group_init_late() which you suggested, we should probably hook into a place where kmalloc is ready and account_user_time/account_system_time is not yet called even once. Does it make sense ?

yes, that would be good.

OK, so when does account_system_time get called for the first time ? after IRQs are set up, is it ? So, where do we place the hook ?

Here's the patch.

include/linux/percpu_counter.h +1 -1
index 9007ccd..8a1b756 100644
@@ -21,7 +21,7 @@ struct percpu_counter {
 #ifdef CONFIG_HOTPLUG_CPU
 	struct list_head list;	/* All percpu_counters are on a list */
 #endif
-	s32 *counters;
+	s32 counters[NR_CPUS];
 };
 
 #if NR_CPUS >= 16
kernel/sched.c +89 -1

158 lines not on this page · raw message

lib/percpu_counter.c +1 -2
index 1191744..a7e84f9 100644
@@ -14,6 +14,7 @@ static LIST_HEAD(percpu_counters);
 static DEFINE_MUTEX(percpu_counters_lock);
 #endif
 
+#define per_cpu_ptr(var,cpu) &var[cpu]
 void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
 {
 	int cpu;
@@ -74,7 +75,6 @@ int percpu_counter_init(struct percpu_counter *fbc, s64 amount)
 {
 	spin_lock_init(&fbc->lock);
 	fbc->count = amount;
-	fbc->counters = alloc_percpu(s32);
 	if (!fbc->counters)
 		return -ENOMEM;
 #ifdef CONFIG_HOTPLUG_CPU
@@ -102,7 +102,6 @@ void percpu_counter_destroy(struct percpu_counter *fbc)
 		return;
 
 	free_percpu(fbc->counters);
-	fbc->counters = NULL;
 #ifdef CONFIG_HOTPLUG_CPU
 	mutex_lock(&percpu_counters_lock);
 	list_del(&fbc->list);
Signature · 5 lines
Warm Regards,

Balaji Rao
Dept. of Mechanical Engineering
NITK
maintainer
#20 lore raw

Re: [RFC][-mm] Simple stats for cpu resource controller v3

Quoted 3 lines · On Sat, 3 May 2008 05:26:46 +0530 Balaji Rao <[email protected]> wrote:
Quoted 1 line

yes, that would be good.

OK, so when does account_system_time get called for the first time ? after IRQs are set up, is it ? So, where do we place the hook ?

Don't know - I'd need to dive in and work that out, and it's probably better than you do this..

include/linux/percpu_counter.h struct percpu_counter { · line 25

Quoted 13 lines

Here's the patch.

include/linux/percpu_counter.h +1 -1
index 9007ccd..8a1b756 100644
@@ -21,7 +21,7 @@ struct percpu_counter {
 #ifdef CONFIG_HOTPLUG_CPU
 	struct list_head list;	/* All percpu_counters are on a list */
 #endif
-	s32 *counters;
+	s32 counters[NR_CPUS];
 };

Please, no. That's a 4092-byte increase in sizeof(struct percpu_counter). Hence a 12 kbyte increase in sizeof(struct ext3_sb_info). Let's just sort out the cgroup startup ordering.

<looks at __percpu_alloc_mask>
<wanders off-topic>

Eric, is that optimal? alloc_percpu() will pass down cpu_possible_map in mask', and we only need to allocate enough slots to cover the highest-set-bit in cpu_possible_map. However the implementation ignores mask' and does

        size_t sz = roundup(nr_cpu_ids * sizeof(void *), cache_line_size());
        void *pdata = kzalloc(sz, gfp);

Now, if the highest-set-bit in cpu_possible_map is always equal to (1<<nr_cpu_ids) then it doesn't matter. But is that the case?

(If someone calls __percpu_alloc_mask with something that has less bits set than cpu_possible_map then it surely is wasteful, but that sounds unlikely).

author
#21 lore raw

Re: [RFC][-mm] Simple stats for cpu resource controller v4

Hi Andrew,

Here's a version that uses percpu_counters and which actually works. The only evil it contains is the check,

	if (percpu_counter_ready) {
		..
	}

I've also added a callback called 'initalize' to a cgroup_subsys.

The alternate idea I suggested was to add a hook at a point where the first task has not yet started and where kmalloc is usable. Its hacky and not a nice thing to do IMHO.

How does this look ?

Regards,
Balaji Rao

---

include/linux/cgroup.h +1 -0
index e155aa7..60a25cb 100644
@@ -293,6 +293,7 @@ int cgroup_is_descendant(const struct cgroup *cgrp);
 struct cgroup_subsys {
 	struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
 						  struct cgroup *cgrp);
+	void (*initialize)(int early);
 	void (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
 	void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
 	int (*can_attach)(struct cgroup_subsys *ss,
kernel/cgroup.c +6 -0
index 8877a08..be37395 100644
@@ -2553,6 +2553,9 @@ int __init cgroup_init_early(void)
 
 		if (ss->early_init)
 			cgroup_init_subsys(ss);
+
+		if (ss->initialize)
+			ss->initialize(1);
 	}
 	return 0;
 }
@@ -2577,6 +2580,9 @@ int __init cgroup_init(void)
 		struct cgroup_subsys *ss = subsys[i];
 		if (!ss->early_init)
 			cgroup_init_subsys(ss);
+
+		if (ss->initialize)
+			ss->initialize(0);
 	}
 
 	/* Add init_css_set to the hash table */
kernel/sched.c +98 -1

157 lines not on this page · raw message

#22 lore raw

Re: [RFC][-mm] Simple stats for cpu resource controller v4

Quoted 6 lines · Balaji Rao

/*

kernel/sched.c +0 -1
@@ -3892,8 +3923,17 @@ void account_system_time(struct task_struct *p, int hardirq_offset,
 		cpustat->irq = cputime64_add(cpustat->irq, tmp);
 	else if (softirq_count())
 		cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
-	else if (p != rq->idle)

kernel/sched.c void account_system_time(struct task_struct *p, int hardirq_offset, · line 3935

Quoted 10 lines
kernel/sched.c +9 -0
+	else if (p != rq->idle) {
 		cpustat->system = cputime64_add(cpustat->system, tmp);
+#ifdef CONFIG_CGROUP_SCHED
+		{
+			struct task_group *tg;
+			tg = task_group(p);
+			__cpu_cgroup_stat_add(tg->stat, CPU_CGROUP_STAT_STIME,
+					cputime_to_msecs(cputime));
+		}
+	}

You should put this '}' after '#endif'

Quoted 32 lines
kernel/sched.c +17 -0
+#endif
 	else if (atomic_read(&rq->nr_iowait) > 0)
 		cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
 	else
@@ -8179,10 +8219,26 @@ static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
 			    struct task_group, css);
 }
 
+static void cpu_cgroup_initialize(int early)
+{
+	int i;
+	struct cpu_cgroup_stat *stat;
+
+	if (!early) {
+		stat = kmalloc(sizeof(struct cpu_cgroup_stat)
+				, GFP_KERNEL);
+		for (i = 0; i < CPU_CGROUP_STAT_NSTATS; i++)
+			percpu_counter_init(
+					&stat->cpustat[i], 0);
+		init_task_group.stat = stat;
+	}
+}
+
 static struct cgroup_subsys_state *
 cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
 {
 	struct task_group *tg;
+	int i;
 
 	if (!cgrp->parent) {
 		/* This is early initialization for the top cgroup */

kernel/sched.c cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp) · line 8260

Quoted 7 lines
kernel/sched.c +4 -0
@@ -8198,6 +8254,10 @@ cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
 	if (IS_ERR(tg))
 		return ERR_PTR(-ENOMEM);
 
+	tg->stat = kmalloc(sizeof(struct cpu_cgroup_stat), GFP_KERNEL);
+	for (i = 0; i < CPU_CGROUP_STAT_NSTATS; i++)
+		percpu_counter_init(&tg->stat->cpustat[i], 0);
+

I guess you forgot to free those things in cpu_cgroup_destroy().

Quoted 62 lines
kernel/sched.c +37 -1
 	/* Bind the cgroup to task_group object we just created */
 	tg->css.cgroup = cgrp;
 
@@ -8251,6 +8311,38 @@ static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
 }
 #endif
 
+static s64 cpu_cgroup_read_stat(struct cpu_cgroup_stat *stat,
+		enum cpu_cgroup_stat_index idx)
+{
+	if (stat)
+		return percpu_counter_read(&stat->cpustat[idx]);
+
+	return 0;
+}
+
+static const struct cpu_cgroup_stat_desc {
+	const char *msg;
+	u64 unit;
+} cpu_cgroup_stat_desc[] = {
+	[CPU_CGROUP_STAT_UTIME] = { "utime", 1, },
+	[CPU_CGROUP_STAT_STIME] = { "stime", 1, },
+};
+
+static int cpu_cgroup_stats_show(struct cgroup *cgrp, struct cftype *cft,
+		struct cgroup_map_cb *cb)
+{
+	struct task_group *tg = cgroup_tg(cgrp);
+	struct cpu_cgroup_stat *stat = tg->stat;
+	int i;
+	for (i = 0; i < CPU_CGROUP_STAT_NSTATS; i++) {
+		s64 val;
+		val = cpu_cgroup_read_stat(stat, i);
+		val *= cpu_cgroup_stat_desc[i].unit;
+		cb->fill(cb, cpu_cgroup_stat_desc[i].msg, val);
+	}
+	return 0;
+}
+
 #ifdef CONFIG_RT_GROUP_SCHED
 static ssize_t cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
 				s64 val)
@@ -8295,6 +8387,10 @@ static struct cftype cpu_files[] = {
 		.write_u64 = cpu_rt_period_write_uint,
 	},
 #endif
+	{
+		.name = "stat",
+		.read_map = cpu_cgroup_stats_show,
+	},
 };
 
 static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
@@ -8304,6 +8400,7 @@ static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
 
 struct cgroup_subsys cpu_cgroup_subsys = {
 	.name		= "cpu",
+	.initialize	= cpu_cgroup_initialize,
 	.create		= cpu_cgroup_create,
 	.destroy	= cpu_cgroup_destroy,
 	.can_attach	= cpu_cgroup_can_attach,
--
author
#23 lore raw

Re: [RFC][-mm] Simple stats for cpu resource controller v4

Quoted 11 lines · Li Zefan
Quoted 10 lines · Balaji Rao

/*

kernel/sched.c +3 -1
@@ -3892,8 +3923,17 @@ void account_system_time(struct task_struct *p,
 int hardirq_offset, cpustat->irq = cputime64_add(cpustat->irq, tmp);
 	else if (softirq_count())
 		cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
-	else if (p != rq->idle)
+	else if (p != rq->idle) {
 		cpustat->system = cputime64_add(cpustat->system, tmp);
+#ifdef CONFIG_CGROUP_SCHED
+		{
Quoted 9 lines

kernel/sched.c void account_system_time(struct task_struct *p, int hardirq_offset, · line 3935

Quoted 6 lines
kernel/sched.c +6 -0
+			struct task_group *tg;
+			tg = task_group(p);
+			__cpu_cgroup_stat_add(tg->stat, CPU_CGROUP_STAT_STIME,
+					cputime_to_msecs(cputime));
+		}
+	}

You should put this '}' after '#endif'

Yes, my mistake. Will fix it.

Quoted 35 lines
Quoted 35 lines
kernel/sched.c +17 -0
+#endif
 	else if (atomic_read(&rq->nr_iowait) > 0)
 		cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
 	else
@@ -8179,10 +8219,26 @@ static inline struct task_group *cgroup_tg(struct
 cgroup *cgrp) struct task_group, css);
 }
 
+static void cpu_cgroup_initialize(int early)
+{
+	int i;
+	struct cpu_cgroup_stat *stat;
+
+	if (!early) {
+		stat = kmalloc(sizeof(struct cpu_cgroup_stat)
+				, GFP_KERNEL);
+		for (i = 0; i < CPU_CGROUP_STAT_NSTATS; i++)
+			percpu_counter_init(
+					&stat->cpustat[i], 0);
+		init_task_group.stat = stat;
+	}
+}
+
 static struct cgroup_subsys_state *
 cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
 {
 	struct task_group *tg;
+	int i;
 
 	if (!cgrp->parent) {
 		/* This is early initialization for the top cgroup */
@@ -8198,6 +8254,10 @@ cpu_cgroup_create(struct cgroup_subsys *ss, struct
 cgroup *cgrp) if (IS_ERR(tg))
 		return ERR_PTR(-ENOMEM);
 
Quoted 6 lines

kernel/sched.c cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp) · line 8260

Quoted 4 lines
kernel/sched.c +4 -0
+	tg->stat = kmalloc(sizeof(struct cpu_cgroup_stat), GFP_KERNEL);
+	for (i = 0; i < CPU_CGROUP_STAT_NSTATS; i++)
+		percpu_counter_init(&tg->stat->cpustat[i], 0);
+

I guess you forgot to free those things in cpu_cgroup_destroy().

Oh. Sorry, I totally missed it.

Quoted 2 lines
Quoted 2 lines
kernel/sched.c +0 -0
 	/* Bind the cgroup to task_group object we just created */
Signature · 5 lines
Warm Regards,

Balaji Rao
Dept. of Mechanical Engineering
NITK
maintainer
#24 lore raw

Re: [RFC][-mm] Simple stats for cpu resource controller v4

In reply to Balaji Rao
Quoted 8 lines · On Mon, 12 May 2008 01:48:37 +0530 Balaji Rao <[email protected]> wrote:

Hi Andrew,

Here's a version that uses percpu_counters and which actually works. The only evil it contains is the check,

	if (percpu_counter_ready) {
		..
	}

There's no instance of "percpu_counter_ready" in the patch so I'm a bit stumped.

Quoted 23 lines
kernel/sched.c +11 -1
@@ -3837,6 +3858,16 @@ void account_user_time(struct task_struct *p, cputime_t cputime)
 		cpustat->nice = cputime64_add(cpustat->nice, tmp);
 	else
 		cpustat->user = cputime64_add(cpustat->user, tmp);
+
+       /* Charge the task's group */
+#ifdef CONFIG_CGROUP_SCHED
+	{
+		struct task_group *tg;
+		tg = task_group(p);
+		__cpu_cgroup_stat_add(tg->stat, CPU_CGROUP_STAT_UTIME,
+				cputime_to_msecs(cputime));
+	}
+#endif
 }
 
 /*
@@ -3892,8 +3923,17 @@ void account_system_time(struct task_struct *p, int hardirq_offset,
 		cpustat->irq = cputime64_add(cpustat->irq, tmp);
 	else if (softirq_count())
 		cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
-	else if (p != rq->idle)
+	else if (p != rq->idle) {

kernel/sched.c void account_system_time(struct task_struct *p, int hardirq_offset, · line 3936

Quoted 10 lines
kernel/sched.c +9 -0
 		cpustat->system = cputime64_add(cpustat->system, tmp);
+#ifdef CONFIG_CGROUP_SCHED
+		{
+			struct task_group *tg;
+			tg = task_group(p);
+			__cpu_cgroup_stat_add(tg->stat, CPU_CGROUP_STAT_STIME,
+					cputime_to_msecs(cputime));
+		}
+	}
+#endif

I'd suggest that the above be turned into calls to a helper function which is a no-op if !CONFIG_CGROUP_SCHED.

author
#25 lore raw

Re: [RFC][-mm] Simple stats for cpu resource controller v4

Quoted 8 lines · Andrew Morton

On Mon, 12 May 2008 01:48:37 +0530

Quoted 5 lines · Balaji Rao

Hi Andrew,

Here's a version that uses percpu_counters and which actually works. The only evil it contains is the check,

Quoted 7 lines
Quoted 3 lines
	if (percpu_counter_ready) {
		..
	}

There's no instance of "percpu_counter_ready" in the patch so I'm a bit stumped.

Here it is. 'stat' can tell us if percpu_counters are ready or not.

+static void __cpu_cgroup_stat_add(struct cpu_cgroup_stat *stat, +               enum cpu_cgroup_stat_index idx, int val) +{
+       if (stat)
+               percpu_counter_add(&stat->cpustat[idx], val); +}
+#endif

It's here as well.

+static s64 cpu_cgroup_read_stat(struct cpu_cgroup_stat *stat, +               enum cpu_cgroup_stat_index idx)
+{
+       if (stat)
+               return percpu_counter_read(&stat->cpustat[idx]); +
+       return 0;
+}
+

Quoted 28 lines
Quoted 28 lines
kernel/sched.c +15 -1
@@ -3837,6 +3858,16 @@ void account_user_time(struct task_struct *p,
 cputime_t cputime) cpustat->nice = cputime64_add(cpustat->nice, tmp);
 	else
 		cpustat->user = cputime64_add(cpustat->user, tmp);
+
+       /* Charge the task's group */
+#ifdef CONFIG_CGROUP_SCHED
+	{
+		struct task_group *tg;
+		tg = task_group(p);
+		__cpu_cgroup_stat_add(tg->stat, CPU_CGROUP_STAT_UTIME,
+				cputime_to_msecs(cputime));
+	}
+#endif
 }
 
 /*
@@ -3892,8 +3923,17 @@ void account_system_time(struct task_struct *p,
 int hardirq_offset, cpustat->irq = cputime64_add(cpustat->irq, tmp);
 	else if (softirq_count())
 		cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
-	else if (p != rq->idle)
+	else if (p != rq->idle) {
 		cpustat->system = cputime64_add(cpustat->system, tmp);
+#ifdef CONFIG_CGROUP_SCHED
+		{
+			struct task_group *tg;
+			tg = task_group(p);
Quoted 8 lines

kernel/sched.c void account_system_time(struct task_struct *p, int hardirq_offset, · line 3936

Quoted 5 lines
kernel/sched.c +5 -0
+			__cpu_cgroup_stat_add(tg->stat, CPU_CGROUP_STAT_STIME,
+					cputime_to_msecs(cputime));
+		}
+	}
+#endif

I'd suggest that the above be turned into calls to a helper function which is a no-op if !CONFIG_CGROUP_SCHED.

OK. Will do.

Signature · 5 lines
Warm Regards,

Balaji Rao
Dept. of Mechanical Engineering
NITK