sched: support schedstats for RT sched class
v3 cover sched: support schedstats for RT sched class
We want to measure the latency of RT tasks in our production environment with schedstats facility, but currently schedstats is only supported for fair sched class. This patch enable it for RT sched class as well.
- Structure
Before we make schedstats available for RT sched class, we should make struct sched_statistics independent of fair sched class first.
The struct sched_statistics is the schedular statistics of a task_struct or a task_group. So we can move it into struct task_struct and struct task_group to achieve the goal.
Below is the detailed explaination of the change in the structs.
The struct before this patch:
struct task_struct { |-> struct sched_entity {
... | ...
struct sched_entity *se; ---| struct sched_statistics statistics; struct sched_rt_entity *rt; ...
... ...
}; };
struct task_group { |--> se[0]->statistics : schedstats of CPU0
... |
#ifdef CONFIG_FAIR_GROUP_SCHED |
struct sched_entity **se; --|--> se[1]->statistics : schedstats of CPU1
|
#endif |
|--> se[N]->statistics : schedstats of CPUn #ifdef CONFIG_FAIR_GROUP_SCHED
struct sched_rt_entity **rt_se; (N/A)
#endif
...
};The '**se' in task_group is allocated in the fair sched class, which is hard to be reused by other sched class.
The struct after this patch:
struct task_struct {
...
struct sched_statistics statistics;
...
struct sched_entity *se;
struct sched_rt_entity *rt;
...
};struct task_group { |---> stats[0] : of CPU0
... |
struct sched_statistics **stats; --|---> stats[1] : of CPU1
... |
|---> stats[n] : of CPUn
#ifdef CONFIG_FAIR_GROUP_SCHED
struct sched_entity **se;
#endif
#ifdef CONFIG_RT_GROUP_SCHED
struct sched_rt_entity **rt_se;
#endif
...
};After the patch it is clearly that both of se or rt_se can easily get the sched_statistics by a task_struct or a task_group.
- Function Interface
The original prototype of the schedstats helpers are
update_stats_wait_*(struct cfs_rq *cfs_rq, struct sched_entity *se)
The cfs_rq in these helpers is used to get the rq_clock, and the se is used to get the struct sched_statistics and the struct task_struct. In order to make these helpers available by all sched classes, we can pass the rq, sched_statistics and task_struct directly.
Then the new helpers are
update_stats_wait_*(struct rq *rq, struct task_struct *p,
struct sched_statistics *stats)
which are independent of fair sched class.
To avoid vmlinux growing too large or introducing ovehead when !schedstat_enabled(), some new helpers after schedstat_enabled() are also introduced, Suggested by Mel. These helpers are in sched/stats.c,
__update_stats_wait_*(struct rq *rq, struct task_struct *p,
struct sched_statistics *stats)
- Implementation
After we make the struct sched_statistics and the helpers of it independent of fair sched class, we can easily use the schedstats facility for RT sched class.
The schedstat usage in RT sched class is similar with fair sched class, for example,
fair RT enqueue update_stats_enqueue_fair update_stats_enqueue_rt dequeue update_stats_dequeue_fair update_stats_dequeue_rt put_prev_task update_stats_wait_start update_stats_wait_start set_next_task update_stats_wait_end update_stats_wait_end
- Usage
The user can get the schedstats information in the same way in fair sched class. For example,
fair RT
task show /proc/[pid]/sched /proc/[pid]/sched
group show cpu.stat in cgroup cpu.stat in cgroup
The sched:sched_stat_{wait, sleep, iowait, blocked} tracepoints can be used to trace RT tasks as well. sched_stat_runtime can also be supported in the future if it is helpful.
Yafang Shao (5):
sched: don't include stats.h in sched.h
sched, fair: use __schedstat_set() in set_next_entity()
sched: make struct sched_statistics independent of fair sched class
sched: make schedstats helpers independent of fair sched class sched, rt: support schedstats for RT sched class
include/linux/sched.h | 3 +- kernel/sched/core.c | 25 +++-- kernel/sched/deadline.c | 5 +- kernel/sched/debug.c | 82 +++++++-------- kernel/sched/fair.c | 209 +++++++++++++++------------------------ kernel/sched/idle.c | 1 + kernel/sched/rt.c | 178 ++++++++++++++++++++++++++++++++- kernel/sched/sched.h | 13 ++- kernel/sched/stats.c | 105 ++++++++++++++++++++ kernel/sched/stats.h | 80 +++++++++++++++ kernel/sched/stop_task.c | 5 +- 11 files changed, 517 insertions(+), 189 deletions(-)
Signature · 1 line
2.18.4
RFC PATCH v3 1/5 sched: don't include stats.h in sched.h
This patch is a preparation of the followup patches. In the followup patches some common helpers will be defined in stats.h, and these common helpers require some definitions in sched.h, so let's move stats.h out of sched.h.
The source files which require stats.h include it specifically.
--- kernel/sched/core.c | 1 + kernel/sched/deadline.c | 1 + kernel/sched/debug.c | 1 + kernel/sched/fair.c | 1 + kernel/sched/idle.c | 1 + kernel/sched/rt.c | 2 +- kernel/sched/sched.h | 6 +++++- kernel/sched/stats.c | 1 + kernel/sched/stats.h | 2 ++ kernel/sched/stop_task.c | 1 + 10 files changed, 15 insertions(+), 2 deletions(-)
kernel/sched/core.c +1 -0
@@ -11,6 +11,7 @@#undef CREATE_TRACE_POINTS#include "sched.h" +#include "stats.h" #include <linux/nospec.h>
kernel/sched/deadline.c +1 -0
@@ -16,6 +16,7 @@* Fabio Checconi <[email protected]> */ #include "sched.h" +#include "stats.h" #include "pelt.h" struct dl_bandwidth def_dl_bandwidth;
kernel/sched/debug.c +1 -0
@@ -7,6 +7,7 @@* Copyright(C) 2007, Red Hat, Inc., Ingo Molnar */ #include "sched.h" +#include "stats.h" static DEFINE_SPINLOCK (sched_debug_lock);
kernel/sched/fair.c +1 -0
@@ -21,6 +21,7 @@* Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra */ #include "sched.h" +#include "stats.h" /* * Targeted preemption latency for CPU-bound tasks:
kernel/sched/idle.c +1 -0
@@ -7,6 +7,7 @@* tasks which are handled in sched/fair.c ) */ #include "sched.h" +#include "stats.h" #include <trace/events/power.h>
kernel/sched/rt.c +1 -1
@@ -4,7 +4,7 @@* policies) */ #include "sched.h" - +#include "stats.h" #include "pelt.h" int sched_rr_timeslice =RR_TIMESLICE ;
kernel/sched/sched.h +5 -1
@@ -2,6 +2,9 @@/* * Scheduler internal types and methods: */ +#ifndef _KERNEL_SCHED_SCHED_H +#define _KERNEL_SCHED_SCHED_H +#include <linux/sched.h> #include <linux/sched/autogroup.h>
@@ -1538,7 +1541,6 @@ extern void flush_smp_call_function_from_idle(void);static inline void flush_smp_call_function_from_idle (void ) { }#endif -#include "stats.h" #include "autogroup.h" #ifdef CONFIG_CGROUP_SCHED
@@ -2633,3 +2635,5 @@ static inline bool is_per_cpu_kthread(struct task_struct *p)void swake_up_all_locked (struct swait_queue_head *q);void __prepare_to_swait (struct swait_queue_head *q,struct swait_queue *wait); + +#endif /* _KERNEL_SCHED_SCHED_H */
kernel/sched/stats.c +1 -0
@@ -3,6 +3,7 @@* /proc/schedstat implementation */ #include "sched.h" +#include "stats.h" /* * Current schedstat API version.
kernel/sched/stats.h +2 -0
@@ -2,6 +2,8 @@#ifdef CONFIG_SCHEDSTATS +#include "sched.h" +/* * Expects runqueue lock to be held for atomicity of update */
kernel/sched/stop_task.c +1 -0
@@ -8,6 +8,7 @@* See kernel/stop_machine.c */ #include "sched.h" +#include "stats.h" #ifdef CONFIG_SMP static int
Signature · 1 line
2.18.4
RFC PATCH v3 2/5 sched, fair: use __schedstat_set() in set_next_entity()
In reply to Yafang Shaoschedstat_enabled() has been already checked, so we can use __schedstat_set() directly.
--- kernel/sched/fair.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/fair.c +1 -1
@@ -4414,7 +4414,7 @@ set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)*/ if (schedstat_enabled () &&rq_of (cfs_rq)->cfs.load.weight >=2 *se->load.weight) { -schedstat_set (se->statistics.slice_max, +__schedstat_set (se->statistics.slice_max,max ((u64 )schedstat_val (se->statistics.slice_max), se->sum_exec_runtime - se->prev_sum_exec_runtime)); }
Signature · 1 line
2.18.4
RFC PATCH v3 3/5 sched: make struct sched_statistics independent of fair sched class
In reply to Yafang ShaoIf we want to use schedstats facility, we should move out of struct sched_statistics from the struct sched_entity or add it into other sctructs of sched entity as well. Obviously the latter one is bad because that requires more spaces. So we should move it into a common struct which can be used by all sched classes.
The struct sched_statistics is the schedular statistics of a task_struct or a task_group. So we can move it into struct task_struct and struct task_group to achieve the goal.
Below is the detailed explaination of the change in the structs.
- Before this patch
struct task_struct { |-> struct sched_entity {
... | ...
struct sched_entity *se; ---| struct sched_statistics statistics; struct sched_rt_entity *rt; ...
... ...
}; };
struct task_group { |--> se[0]->statistics : schedstats of CPU0
... |
#ifdef CONFIG_FAIR_GROUP_SCHED |
struct sched_entity **se; --|--> se[1]->statistics : schedstats of CPU1
|
#endif |
|--> se[N]->statistics : schedstats of CPUn #ifdef CONFIG_FAIR_GROUP_SCHED
struct sched_rt_entity **rt_se; (N/A)
#endif
...
};The '**se' in task_group is allocated in the fair sched class, which is hard to be reused by other sched class.
- After this patch
struct task_struct {
...
struct sched_statistics statistics;
...
struct sched_entity *se;
struct sched_rt_entity *rt;
...
};struct task_group { |---> stats[0] : of CPU0
... |
struct sched_statistics **stats; --|---> stats[1] : of CPU1
... |
|---> stats[n] : of CPUn
#ifdef CONFIG_FAIR_GROUP_SCHED
struct sched_entity **se;
#endif
#ifdef CONFIG_RT_GROUP_SCHED
struct sched_rt_entity **rt_se;
#endif
...
};After the patch it is clearly that both of se or rt_se can easily get the sched_statistics by a task_struct or a task_group.
--- include/linux/sched.h | 3 +- kernel/sched/core.c | 24 ++++--- kernel/sched/deadline.c | 4 +- kernel/sched/debug.c | 81 +++++++++++----------- kernel/sched/fair.c | 142 ++++++++++++++++++++++++++------------- kernel/sched/rt.c | 4 +- kernel/sched/sched.h | 3 + kernel/sched/stats.h | 46 +++++++++++++ kernel/sched/stop_task.c | 4 +- 9 files changed, 206 insertions(+), 105 deletions(-)
include/linux/sched.h +1 -2
@@ -462,8 +462,6 @@ struct sched_entity {u64 nr_migrations; -struct sched_statistics statistics; -#ifdef CONFIG_FAIR_GROUP_SCHED int depth;struct sched_entity *parent;
@@ -681,6 +679,7 @@ struct task_struct {unsigned int rt_priority;const struct sched_class *sched_class; +struct sched_statistics stats;struct sched_entity se;struct sched_rt_entity rt;#ifdef CONFIG_CGROUP_SCHED
kernel/sched/core.c +14 -10
@@ -2427,11 +2427,11 @@ ttwu_stat(struct task_struct *p, int cpu, int wake_flags)#ifdef CONFIG_SMP if (cpu == rq->cpu) {__schedstat_inc (rq->ttwu_local); -__schedstat_inc (p->se.statistics.nr_wakeups_local); +__schedstat_inc (p->stats.nr_wakeups_local); }else {struct sched_domain *sd; -__schedstat_inc (p->se.statistics.nr_wakeups_remote); +__schedstat_inc (p->stats.nr_wakeups_remote);rcu_read_lock ();for_each_domain (rq->cpu, sd) {if (cpumask_test_cpu (cpu,sched_domain_span (sd))) {
@@ -2443,14 +2443,14 @@ ttwu_stat(struct task_struct *p, int cpu, int wake_flags) }if (wake_flags &WF_MIGRATED ) -__schedstat_inc (p->se.statistics.nr_wakeups_migrate); +__schedstat_inc (p->stats.nr_wakeups_migrate);#endif /* CONFIG_SMP */ __schedstat_inc (rq->ttwu_count); -__schedstat_inc (p->se.statistics.nr_wakeups); +__schedstat_inc (p->stats.nr_wakeups);if (wake_flags &WF_SYNC ) -__schedstat_inc (p->se.statistics.nr_wakeups_sync); +__schedstat_inc (p->stats.nr_wakeups_sync); }/*
@@ -3075,7 +3075,7 @@ static void __sched_fork(unsigned long clone_flags, struct task_struct *p)#ifdef CONFIG_SCHEDSTATS /* Even if schedstat is disabled, there should not be garbage */ -memset (&p->se.statistics,0 ,sizeof (p->se.statistics)); +memset (&p->stats,0 ,sizeof (p->stats));#endif RB_CLEAR_NODE (&p->dl.rb_node);
@@ -7347,9 +7347,9 @@ void normalize_rt_tasks(void)continue ; p->se.exec_start =0 ; -schedstat_set (p->se.statistics.wait_start,0 ); -schedstat_set (p->se.statistics.sleep_start,0 ); -schedstat_set (p->se.statistics.block_start,0 ); +schedstat_set (p->stats.wait_start,0 ); +schedstat_set (p->stats.sleep_start,0 ); +schedstat_set (p->stats.block_start,0 );if (!dl_task (p) && !rt_task (p)) {/*
@@ -7439,6 +7439,7 @@ static void sched_free_group(struct task_group *tg) {free_fair_sched_group (tg);free_rt_sched_group (tg); +free_tg_schedstats (tg);autogroup_free (tg);kmem_cache_free (task_group_cache, tg); }
@@ -7458,6 +7459,9 @@ struct task_group *sched_create_group(struct task_group *parent)if (!alloc_rt_sched_group (tg, parent)) goto err; +if (!alloc_tg_schedstats (tg)) + goto err; +alloc_uclamp_sched_group (tg, parent);return tg;
@@ -8144,7 +8148,7 @@ static int cpu_cfs_stat_show(struct seq_file *sf, void *v)int i;for_each_possible_cpu (i) - ws +=schedstat_val (tg->se[i]->statistics.wait_sum); + ws +=schedstat_val (tg->stats[i]->wait_sum);seq_printf (sf,"wait_sum %llu\n" , ws); }
kernel/sched/deadline.c +2 -2
@@ -1245,8 +1245,8 @@ static void update_curr_dl(struct rq *rq)return ; } -schedstat_set (curr->se.statistics.exec_max, -max (curr->se.statistics.exec_max, delta_exec)); +schedstat_set (curr->stats.exec_max, +max (curr->stats.exec_max, delta_exec)); curr->se.sum_exec_runtime += delta_exec;account_group_exec_runtime (curr, delta_exec);
kernel/sched/debug.c +40 -41
105 lines not on this page · raw message
kernel/sched/fair.c +96 -46
290 lines not on this page · raw message
kernel/sched/rt.c +2 -2
@@ -1008,8 +1008,8 @@ static void update_curr_rt(struct rq *rq)if (unlikely ((s64 )delta_exec <=0 ))return ; -schedstat_set (curr->se.statistics.exec_max, -max (curr->se.statistics.exec_max, delta_exec)); +schedstat_set (curr->stats.exec_max, +max (curr->stats.exec_max, delta_exec)); curr->se.sum_exec_runtime += delta_exec;account_group_exec_runtime (curr, delta_exec);
kernel/sched/sched.h +3 -0
@@ -387,6 +387,9 @@ struct cfs_bandwidth {struct task_group {struct cgroup_subsys_state css; +/* schedstats of this group on each CPU */ +struct sched_statistics **stats; +#ifdef CONFIG_FAIR_GROUP_SCHED /* schedulable entities of this group on each CPU */ struct sched_entity **se;
kernel/sched/stats.h +46 -0
58 lines not on this page · raw message
kernel/sched/stop_task.c +2 -2
@@ -70,8 +70,8 @@ static void put_prev_task_stop(struct rq *rq, struct task_struct *prev)if (unlikely ((s64 )delta_exec <0 )) delta_exec =0 ; -schedstat_set (curr->se.statistics.exec_max, -max (curr->se.statistics.exec_max, delta_exec)); +schedstat_set (curr->stats.exec_max, +max (curr->stats.exec_max, delta_exec)); curr->se.sum_exec_runtime += delta_exec;account_group_exec_runtime (curr, delta_exec);
Signature · 1 line
2.18.4
Quoted 142 lines · Yafang Shao
We want to measure the latency of RT tasks in our production environment with schedstats facility, but currently schedstats is only supported for fair sched class. This patch enable it for RT sched class as well.
- Structure
Before we make schedstats available for RT sched class, we should make struct
sched_statisticsindependent of fair sched class first.The struct
sched_statisticsis the schedular statistics of atask_structor atask_group. So we can move it into structtask_structand structtask_groupto achieve the goal.Below is the detailed explaination of the change in the structs.
The struct before this patch:
struct task_struct { |-> struct sched_entity { ... | ... struct sched_entity *se; ---| struct sched_statistics statistics;struct
sched_rt_entity*rt; ...
... ...
}; };struct task_group { |--> se[0]->statistics : schedstats of CPU0 ... | #ifdef CONFIG_FAIR_GROUP_SCHED | struct sched_entity **se; --|--> se[1]->statistics : schedstats of CPU1 | #endif | |--> se[N]->statistics : schedstats of CPUn#ifdef
CONFIG_FAIR_GROUP_SCHED
structsched_rt_entity**rt_se; (N/A)#endif ... };The '**se' in
task_groupis allocated in the fair sched class, which is hard to be reused by other sched class.The struct after this patch:
struct task_struct { ... struct sched_statistics statistics; ... struct sched_entity *se; struct sched_rt_entity *rt; ... };struct task_group { |---> stats[0] : of CPU0 ... | struct sched_statistics **stats; --|---> stats[1] : of CPU1 ... | |---> stats[n] : of CPUn #ifdef CONFIG_FAIR_GROUP_SCHED struct sched_entity **se; #endif #ifdef CONFIG_RT_GROUP_SCHED struct sched_rt_entity **rt_se; #endif ... };After the patch it is clearly that both of se or
rt_secan easily get thesched_statisticsby atask_structor atask_group.- Function Interface
The original prototype of the schedstats helpers are
update_stats_wait_*(struct
cfs_rq*cfs_rq, structsched_entity*se)The
cfs_rqin these helpers is used to get therq_clock, and the se is used to get the structsched_statisticsand the structtask_struct. In order to make these helpers available by all sched classes, we can pass the rq,sched_statisticsandtask_structdirectly.Then the new helpers are
update_stats_wait_*(struct rq *rq, struct
task_struct*p,
structsched_statistics*stats)which are independent of fair sched class.
To avoid vmlinux growing too large or introducing ovehead when !schedstat_enabled(), some new helpers after
schedstat_enabled()are also introduced, Suggested by Mel. These helpers are insched/stats.c,__update_stats_wait_*(struct rq *rq, struct
task_struct*p,
structsched_statistics*stats)- Implementation
After we make the struct
sched_statisticsand the helpers of it independent of fair sched class, we can easily use the schedstats facility for RT sched class.The schedstat usage in RT sched class is similar with fair sched class, for example,
fair RT enqueue update_stats_enqueue_fair update_stats_enqueue_rt dequeue update_stats_dequeue_fair update_stats_dequeue_rt put_prev_task update_stats_wait_start update_stats_wait_start set_next_task update_stats_wait_end update_stats_wait_end- Usage
The user can get the schedstats information in the same way in fair sched class. For example,
fair RT task show /proc/[pid]/sched /proc/[pid]/sched group show cpu.stat in cgroup cpu.stat in cgroupThe sched:sched_stat_{wait, sleep, iowait, blocked} tracepoints can be used to trace RT tasks as well.
sched_stat_runtimecan also be supported in the future if it is helpful.Yafang Shao (5):
sched: don't includestats.hinsched.h
sched, fair: use__schedstat_set()inset_next_entity()
sched: make structsched_statisticsindependent of fair sched class
sched: make schedstats helpers independent of fair sched class sched, rt: support schedstats for RT sched classinclude/linux/sched.h | 3 +- kernel/sched/core.c | 25 +++-- kernel/sched/deadline.c | 5 +- kernel/sched/debug.c | 82 +++++++-------- kernel/sched/fair.c | 209 +++++++++++++++------------------------ kernel/sched/idle.c | 1 + kernel/sched/rt.c | 178 ++++++++++++++++++++++++++++++++- kernel/sched/sched.h | 13 ++- kernel/sched/stats.c | 105 ++++++++++++++++++++ kernel/sched/stats.h | 80 +++++++++++++++ kernel/sched/stop_task.c | 5 +- 11 files changed, 517 insertions(+), 189 deletions(-)
Quoted 3 lines
--
2.18.4
Ah, the patch #5 is missed,pls ignore this patchset. I will resend them.
Signature · 2 lines
Thanks Yafang