Simple stats for cpu resource controller
RFC 1/2 Simple stats for cpu resource controller
This patch implements trivial statistics for the cpu controller.
kernel/sched.c +86 -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 , +}; + +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_safe (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
@@ -3670,6 +3698,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_safe (&tg->stat,CPU_CGROUP_STAT_UTIME , +cputime_to_msecs (cputime)); + } +#endif }/*
@@ -3733,6 +3771,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_safe (&tg->stat,CPU_CGROUP_STAT_STIME , +cputime_to_msecs (cputime)); + } +#endif }/*
@@ -7939,6 +7986,40 @@ 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 ; +for_each_possible_cpu (cpu) + ret += stat->cpustat[cpu].count[idx]; +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
@@ -7961,6 +8042,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 {
Quoted 8 lines · Balaji Rao
+
+static s64 cpu_cgroup_read_stat(structcpu_cgroup_stat*stat,
+ enumcpu_cgroup_stat_indexidx)
+{+ int cpu; + s64 ret = 0; + for_each_possible_cpu(cpu)+ ret += stat->cpustat[cpu].count[idx];
On a 32-bit architecture I think this could race with a non-atomic update that crosses a 32-bit boundary and get a corrupted result.
Paul
Quoted 23 lines · Balaji Rao
This patch implements trivial statistics for the cpu controller.
Signed-off-by Balaji Rao <[email protected]>CC Balbir Singh <[email protected]>CC Dhaval Giani <[email protected]>kernel/sched.c +9
-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 , +}; + +struct cpu_cgroup_stat_cpu {
kernel/sched.c struct cfs_rq; · line 185
Quoted 10 lines
kernel/sched.c +10
-0+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_safe (struct cpu_cgroup_stat *stat, +enum cpu_cgroup_stat_index idx,int val)
What is safe about this function?
Quoted 37 lines
kernel/sched.c +19
-0+{ +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 @@ -3670,6 +3698,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_safe (&tg->stat,CPU_CGROUP_STAT_UTIME , +cputime_to_msecs (cputime)); + } +#endif }/* @@ -3733,6 +3771,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);
kernel/sched.c void account_system_time(struct task_struct *p, int hardirq_offset, · line 3783
Quoted 10 lines
kernel/sched.c +9
-0+ +#ifdef CONFIG_CGROUP_SCHED + { +struct task_group *tg; + tg =task_group (p); +__cpu_cgroup_stat_add_safe (&tg->stat,CPU_CGROUP_STAT_STIME , +cputime_to_msecs (cputime)); + } +#endif }
So both of these are tick based? The normal CFS [us]time stats are not.
Quoted 60 lines
kernel/sched.c +39
-1/* @@ -7939,6 +7986,40 @@ 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 ; +for_each_possible_cpu (cpu) + ret += stat->cpustat[cpu].count[idx]; +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 @@ -7961,6 +8042,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 { --To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Quoted 1 line · Peter Zijlstra
On Wed, 2008-03-26 at 23:48 +0530, Balaji Rao wrote:
<snip>
Quoted 6 lines
kernel/sched.c struct cfs_rq; · line 185
Quoted 3 lines
kernel/sched.c +3
-0+/* Called under irq disable. */ +static void __cpu_cgroup_stat_add_safe (struct cpu_cgroup_stat *stat, +enum cpu_cgroup_stat_index idx,int val)What is safe about this function?
That it can be called only from an interrupt context.
Quoted 41 lines
Quoted 41 lines
kernel/sched.c +23
-0+{ +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 @@ -3670,6 +3698,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_safe (&tg->stat,CPU_CGROUP_STAT_UTIME , +cputime_to_msecs (cputime)); + } +#endif }/* @@ -3733,6 +3771,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;
Quoted 9 lines
kernel/sched.c void account_system_time(struct task_struct *p, int hardirq_offset, · line 3783
Quoted 6 lines
kernel/sched.c +5
-0+ tg =task_group (p); +__cpu_cgroup_stat_add_safe (&tg->stat,CPU_CGROUP_STAT_STIME , + cputime_to_msecs(cputime)); + } +#endif }So both of these are tick based? The normal CFS [us]time stats are not.
Hmmm.. Yea, right. So I should use the approach used by task_utime and task_stime when reporting it, right ?
Quoted 6 lines
kernel/sched.c static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft) · line 7989
Quoted 6 lines
kernel/sched.c +1
-0/* @@ -7939,6 +7986,40 @@ static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)return (u64 ) tg->shares; } +
Thanks for the review.
Signature · 2 lines
regards, balaji rao
Quoted 10 lines · Balaji Rao
Quoted 1 line · Peter Zijlstra
On Wed, 2008-03-26 at 23:48 +0530, Balaji Rao wrote:
<snip>
Quoted 6 lines
kernel/sched.c struct cfs_rq; · line 185
Quoted 3 lines
kernel/sched.c +3
-0+/* Called under irq disable. */ +static void __cpu_cgroup_stat_add_safe (struct cpu_cgroup_stat *stat, +enum cpu_cgroup_stat_index idx,int val)What is safe about this function?
That it can be called only from an interrupt context.
It can be called from any context that has hardirqs disabled. And the __ prefix suggests as much, no need to tag _safe to the end as well, we never do that.
Quoted 50 lines
Quoted 41 lines
Quoted 41 lines
kernel/sched.c +23
-0+{ +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 @@ -3670,6 +3698,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_safe (&tg->stat,CPU_CGROUP_STAT_UTIME , +cputime_to_msecs (cputime)); + } +#endif }/* @@ -3733,6 +3771,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;Quoted 9 lines
kernel/sched.c void account_system_time(struct task_struct *p, int hardirq_offset, · line 3783
Quoted 6 lines
kernel/sched.c +5
-0+ tg =task_group (p); +__cpu_cgroup_stat_add_safe (&tg->stat,CPU_CGROUP_STAT_STIME , + cputime_to_msecs(cputime)); + } +#endif }So both of these are tick based? The normal CFS [us]time stats are not.
Quoted 1 line
Hmmm.. Yea, right. So I should use the approach used by
task_utimeandtask_stimewhen reporting it, right ?
Not sure what you want to use this for, but yeah, that makes most sense.
That is, I do know _what_ you want to use it for, just not sure which requirements you put on it. The pure tick based thing might be good enough for most purposes, the runtime proportion thing is just more accurate.
Quoted 8 lines
Quoted 6 lines
kernel/sched.c static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft) · line 7989
Quoted 6 lines
kernel/sched.c +1
-0/* @@ -7939,6 +7986,40 @@ static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)return (u64 ) tg->shares; } +Thanks for the review.