sched: encapsulate priority changes in a sched_set_prio static function
RFC PATCH 2/2 tracing: add sched_set_prio tracepoint
In reply to Julien DesfossezThis tracepoint allows to keep track of all priority changes made by all sites that can change this value. The impacted system calls are sched_setscheduler, sched_setattr, sched_process_fork and set_user_nice. The priority inheritance mechanism from rt_mutex gets also instrumented with this tracepoint even though there is a dedicated tracepoint for it (sched_pi_setprio).
This allows analysis of real-time scheduling delays per thread priority, which cannot be performed accurately if we only trace the priority of the currently scheduled processes.
Here is an example of what is output by ftrace when we change the priority of a running process:sys_sched_setscheduler(pid: 1c52, policy: 2, param: 7ffc22e20980)sched_set_prio: comm=burnP6 pid=7250 oldprio=120 newprio=39 sys_sched_setscheduler -> 0x0sched_switch: prev_comm=chrt prev_pid=7268 prev_prio=120
prev_state=R ==> next_comm=burnP6 next_pid=7250
next_prio=39--- include/trace/events/sched.h | 21 ++++++++++++++++----- kernel/sched/core.c | 1 + 2 files changed, 17 insertions(+), 5 deletions(-)
include/trace/events/sched.h +16 -5
@@ -407,11 +407,7 @@ DEFINE_EVENT(sched_stat_runtime, sched_stat_runtime, TP_PROTO(struct task_struct *tsk, u64 runtime, u64 vruntime), TP_ARGS(tsk, runtime, vruntime)); -/* -* Tracepoint for showing priority inheritance modifying a tasks -* priority. -*/ -TRACE_EVENT(sched_pi_setprio, +DECLARE_EVENT_CLASS(sched_prio_template, TP_PROTO(struct task_struct *tsk, int newprio),
@@ -436,6 +432,21 @@ TRACE_EVENT(sched_pi_setprio, __entry->oldprio, __entry->newprio) ); +/* +* Tracepoint for showing priority inheritance modifying a tasks +* priority. +*/ +DEFINE_EVENT (sched_prio_template, sched_pi_setprio, + TP_PROTO(struct task_struct *tsk, int newprio), +TP_ARGS (tsk, newprio)); + +/* +* Tracepoint for priority changes of a task. +*/ +DEFINE_EVENT (sched_prio_template, sched_set_prio, + TP_PROTO(struct task_struct *tsk, int newprio), +TP_ARGS (tsk, newprio)); +#ifdef CONFIG_DETECT_HUNG_TASK TRACE_EVENT(sched_process_hang, TP_PROTO(struct task_struct *tsk),
kernel/sched/core.c +1 -0
@@ -2232,6 +2232,7 @@ int sysctl_schedstats(struct ctl_table *table, int write,static void sched_set_prio (struct task_struct *p,int prio) { +trace_sched_set_prio (p, prio); p->prio = prio; }
Signature · 1 line
1.9.1
RFC PATCH 1/2 sched: encapsulate priority changes in a sched_set_prio static function
Currently, the priority of tasks is modified directly in the scheduling functions. Encapsulate priority updates to enable instrumentation of priority changes. This will enable analysis of real-time scheduling delays per thread priority, which cannot be performed accurately if we only trace the priority of the currently scheduled processes.
The call sites that modify the priority of a task are mostly system
calls: sched_setscheduler, sched_setattr, sched_process_fork and set_user_nice. Priority can also be dynamically boosted through priority inheritance of rt_mutex by rt_mutex_setprio.
--- include/linux/sched.h | 3 ++- kernel/sched/core.c | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-)
include/linux/sched.h +2 -1
@@ -1409,7 +1409,8 @@ struct task_struct {#endif int on_rq; -int prio, static_prio, normal_prio; +int prio;/* Updated through sched_set_prio() */ +int static_prio, normal_prio;unsigned int rt_priority;const struct sched_class *sched_class;struct sched_entity se;
kernel/sched/core.c +13 -6
@@ -2230,6 +2230,11 @@ int sysctl_schedstats(struct ctl_table *table, int write,#endif #endif +static void sched_set_prio (struct task_struct *p,int prio) +{ + p->prio = prio; +} +/* * fork()/clone()-time setup: */
@@ -2249,7 +2254,7 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p)/* * Make sure we do not leak PI boosting priority to the child. */ - p->prio = current->normal_prio; +sched_set_prio (p, current->normal_prio);/* * Revert to default priority/policy on fork if requested.
@@ -2262,7 +2267,8 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p) }else if (PRIO_TO_NICE (p->static_prio) <0 ) p->static_prio =NICE_TO_PRIO (0 ); - p->prio = p->normal_prio =__normal_prio (p); + p->normal_prio =__normal_prio (p); +sched_set_prio (p, p->normal_prio);set_load_weight (p);/*
@@ -3477,7 +3483,7 @@ void rt_mutex_setprio(struct task_struct *p, int prio) p->sched_class = &fair_sched_class; } - p->prio = prio; +sched_set_prio (p, prio);if (running) p->sched_class->set_curr_task (rq);
@@ -3524,7 +3530,7 @@ void set_user_nice(struct task_struct *p, long nice) p->static_prio =NICE_TO_PRIO (nice);set_load_weight (p); old_prio = p->prio; - p->prio =effective_prio (p); +sched_set_prio (p,effective_prio (p)); delta = p->prio - old_prio;if (queued) {
@@ -3731,9 +3737,10 @@ static void __setscheduler(struct rq *rq, struct task_struct *p,* sched_setscheduler(). */ if (keep_boost) - p->prio =rt_mutex_get_effective_prio (p,normal_prio (p)); +sched_set_prio (p,rt_mutex_get_effective_prio (p, +normal_prio (p)));else - p->prio =normal_prio (p); +sched_set_prio (p,normal_prio (p));if (dl_prio (p->prio)) p->sched_class = &dl_sched_class;
Signature · 1 line
1.9.1
Quoted 6 lines · ----- On May 27, 2016, at 5:16 PM, Julien Desfossez [email protected]…
Currently, the priority of tasks is modified directly in the scheduling functions. Encapsulate priority updates to enable instrumentation of priority changes. This will enable analysis of real-time scheduling delays per thread priority, which cannot be performed accurately if we only trace the priority of the currently scheduled processes.
Quoted 6 lines
The call sites that modify the priority of a task are mostly system
calls:sched_setscheduler,sched_setattr,sched_process_forkandset_user_nice. Priority can also be dynamically boosted through priority inheritance ofrt_mutexbyrt_mutex_setprio.Signed-off-by Julien Desfossez <[email protected]>
Quoted 90 lines
---
include/linux/sched.h| 3 ++-kernel/sched/core.c| 19 +++++++++++++------2 files changed, 15 insertions(+), 7 deletions(-)include/linux/sched.h +2
-1@@ -1409,7 +1409,8 @@ struct task_struct {#endif int on_rq; -int prio, static_prio, normal_prio; +int prio;/* Updated through sched_set_prio() */ +int static_prio, normal_prio;unsigned int rt_priority;const struct sched_class *sched_class;struct sched_entity se;kernel/sched/core.c +6
-1@@ -2230,6 +2230,11 @@ int sysctl_schedstats(struct ctl_table *table, int write,#endif #endif +static void sched_set_prio (struct task_struct *p,int prio) +{ + p->prio = prio; +} +/* * fork()/clone()-time setup: */ @@ -2249,7 +2254,7 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p)/* * Make sure we do not leak PI boosting priority to the child. */ - p->prio = current->normal_prio; + sched_set_prio(p, current->normal_prio);/* * Revert to default priority/policy on fork if requested.
kernel/sched/core.c +2
-1@@ -2262,7 +2267,8 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p) }else if (PRIO_TO_NICE (p->static_prio) <0 ) p->static_prio =NICE_TO_PRIO (0 ); - p->prio = p->normal_prio =__normal_prio (p); + p->normal_prio =__normal_prio (p); +sched_set_prio (p, p->normal_prio);set_load_weight (p);/*kernel/sched/core.c +5
-4@@ -3477,7 +3483,7 @@ void rt_mutex_setprio(struct task_struct *p, int prio) p->sched_class = &fair_sched_class; } - p->prio = prio; +sched_set_prio (p, prio);if (running) p->sched_class->set_curr_task (rq);@@ -3524,7 +3530,7 @@ void set_user_nice(struct task_struct *p, long nice) p->static_prio =NICE_TO_PRIO (nice);set_load_weight (p); old_prio = p->prio; - p->prio =effective_prio (p); +sched_set_prio (p,effective_prio (p)); delta = p->prio - old_prio;if (queued) {@@ -3731,9 +3737,10 @@ static void __setscheduler(struct rq *rq, structtask_struct *p, * sched_setscheduler(). */ if (keep_boost) - p->prio =rt_mutex_get_effective_prio (p,normal_prio (p)); +sched_set_prio (p,rt_mutex_get_effective_prio (p, +normal_prio (p)));else - p->prio =normal_prio (p); +sched_set_prio (p,normal_prio (p));if (dl_prio (p->prio))p->sched_class = &dl_sched_class;--
1.9.1
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Re: [RFC PATCH 2/2] tracing: add sched_set_prio tracepoint
In reply to Julien DesfossezQuoted 17 lines · ----- On May 27, 2016, at 5:16 PM, Julien Desfossez [email protected]…
This tracepoint allows to keep track of all priority changes made by all sites that can change this value. The impacted system calls are
sched_setscheduler,sched_setattr,sched_process_forkandset_user_nice. The priority inheritance mechanism fromrt_mutexgets also instrumented with this tracepoint even though there is a dedicated tracepoint for it (sched_pi_setprio).This allows analysis of real-time scheduling delays per thread priority, which cannot be performed accurately if we only trace the priority of the currently scheduled processes.
Here is an example of what is output by ftrace when we change the priority of a running process:
sys_sched_setscheduler(pid: 1c52, policy: 2, param: 7ffc22e20980)sched_set_prio: comm=burnP6 pid=7250 oldprio=120 newprio=39sys_sched_setscheduler->0x0sched_switch: prev_comm=chrt prev_pid=7268 prev_prio=120
Quoted 4 lines
prev_state=R ==> next_comm=burnP6 next_pid=7250 next_prio=39Signed-off-by Julien Desfossez <[email protected]>
Quoted 58 lines
---
include/trace/events/sched.h| 21 ++++++++++++++++-----kernel/sched/core.c| 1 +2 files changed, 17 insertions(+), 5 deletions(-)include/trace/events/sched.h +16
-5@@ -407,11 +407,7 @@ DEFINE_EVENT(sched_stat_runtime, sched_stat_runtime, TP_PROTO(struct task_struct *tsk, u64 runtime, u64 vruntime), TP_ARGS(tsk, runtime, vruntime)); -/* -* Tracepoint for showing priority inheritance modifying a tasks -* priority. -*/ -TRACE_EVENT(sched_pi_setprio, +DECLARE_EVENT_CLASS(sched_prio_template, TP_PROTO(struct task_struct *tsk, int newprio),@@ -436,6 +432,21 @@ TRACE_EVENT(sched_pi_setprio, __entry->oldprio, __entry->newprio) ); +/* +* Tracepoint for showing priority inheritance modifying a tasks +* priority. +*/ +DEFINE_EVENT (sched_prio_template, sched_pi_setprio, + TP_PROTO(struct task_struct *tsk, int newprio), +TP_ARGS (tsk, newprio)); + +/* +* Tracepoint for priority changes of a task. +*/ +DEFINE_EVENT (sched_prio_template, sched_set_prio, + TP_PROTO(struct task_struct *tsk, int newprio), +TP_ARGS (tsk, newprio)); +#ifdef CONFIG_DETECT_HUNG_TASK TRACE_EVENT(sched_process_hang, TP_PROTO(struct task_struct *tsk),kernel/sched/core.c +1
-1@@ -2232,6 +2232,7 @@ int sysctl_schedstats(struct ctl_table *table, int write,static void sched_set_prio (struct task_struct *p,int prio) { +trace_sched_set_prio (p, prio); p->prio = prio; } --1.9.1
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Mathieu Desnoyers
reviewerRe: [RFC PATCH 2/2] tracing: add sched_set_prio tracepoint
Quoted 23 lines · ----- On May 30, 2016, at 9:18 AM, Mathieu Desnoyers…
----- On May 27, 2016, at 5:16 PM, Julien Desfossez [email protected]
Quoted 20 lines · wrote:
This tracepoint allows to keep track of all priority changes made by all sites that can change this value. The impacted system calls are
sched_setscheduler,sched_setattr,sched_process_forkandset_user_nice. The priority inheritance mechanism fromrt_mutexgets also instrumented with this tracepoint even though there is a dedicated tracepoint for it (sched_pi_setprio).This allows analysis of real-time scheduling delays per thread priority, which cannot be performed accurately if we only trace the priority of the currently scheduled processes.
Here is an example of what is output by ftrace when we change the priority of a running process:
sys_sched_setscheduler(pid: 1c52, policy: 2, param: 7ffc22e20980)sched_set_prio: comm=burnP6 pid=7250 oldprio=120 newprio=39sys_sched_setscheduler->0x0sched_switch: prev_comm=chrt prev_pid=7268 prev_prio=120prev_state=R ==> next_comm=burnP6 next_pid=7250 next_prio=39
Quoted 3 lines
Quoted 1 line
Signed-off-by Julien Desfossez <[email protected]>Reviewed-by Mathieu Desnoyers <[email protected]>
Adding Ingo and Peter in CC, considering that it touches to tracing and the scheduler.
Thanks,
Mathieu
Quoted 64 lines
Quoted 58 lines
---
include/trace/events/sched.h| 21 ++++++++++++++++-----kernel/sched/core.c| 1 +2 files changed, 17 insertions(+), 5 deletions(-)include/trace/events/sched.h +16
-5@@ -407,11 +407,7 @@ DEFINE_EVENT(sched_stat_runtime, sched_stat_runtime, TP_PROTO(struct task_struct *tsk, u64 runtime, u64 vruntime), TP_ARGS(tsk, runtime, vruntime)); -/* -* Tracepoint for showing priority inheritance modifying a tasks -* priority. -*/ -TRACE_EVENT(sched_pi_setprio, +DECLARE_EVENT_CLASS(sched_prio_template, TP_PROTO(struct task_struct *tsk, int newprio),@@ -436,6 +432,21 @@ TRACE_EVENT(sched_pi_setprio, __entry->oldprio, __entry->newprio) ); +/* +* Tracepoint for showing priority inheritance modifying a tasks +* priority. +*/ +DEFINE_EVENT (sched_prio_template, sched_pi_setprio, + TP_PROTO(struct task_struct *tsk, int newprio), +TP_ARGS (tsk, newprio)); + +/* +* Tracepoint for priority changes of a task. +*/ +DEFINE_EVENT (sched_prio_template, sched_set_prio, + TP_PROTO(struct task_struct *tsk, int newprio), +TP_ARGS (tsk, newprio)); +#ifdef CONFIG_DETECT_HUNG_TASK TRACE_EVENT(sched_process_hang, TP_PROTO(struct task_struct *tsk),kernel/sched/core.c +1
-1@@ -2232,6 +2232,7 @@ int sysctl_schedstats(struct ctl_table *table, int write,static void sched_set_prio (struct task_struct *p,int prio) { +trace_sched_set_prio (p, prio); p->prio = prio; } --Quoted 1 line
1.9.1
--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Peter Zijlstra
maintainerRe: [RFC PATCH 2/2] tracing: add sched_set_prio tracepoint
Quoted 5 lines · Mathieu Desnoyers
----- On May 30, 2016, at 9:18 AM, Mathieu Desnoyers [email protected] wrote:
Adding Ingo and Peter in CC, considering that it touches to tracing and the scheduler.
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
include/trace/events/sched.h +7
-0+/* +* Tracepoint for showing priority inheritance modifying a tasks +* priority. +*/ +DEFINE_EVENT (sched_prio_template, sched_pi_setprio, + TP_PROTO(struct task_struct *tsk, int newprio), +TP_ARGS (tsk, newprio));Quoted 8 lines
include/trace/events/sched.h TRACE_EVENT(sched_pi_setprio, · line 449
Quoted 8 lines
include/trace/events/sched.h +8
-0+ +/* +* Tracepoint for priority changes of a task. +*/ +DEFINE_EVENT (sched_prio_template, sched_set_prio, + TP_PROTO(struct task_struct *tsk, int newprio), +TP_ARGS (tsk, newprio)); +
Nak on anything that cannot fundamentally deal with SCHED_DEADLINE.
Mathieu Desnoyers
reviewerQuoted 9 lines · ----- On May 30, 2016, at 9:18 AM, Mathieu Desnoyers…
----- On May 27, 2016, at 5:16 PM, Julien Desfossez [email protected]
Quoted 6 lines · wrote:
Currently, the priority of tasks is modified directly in the scheduling functions. Encapsulate priority updates to enable instrumentation of priority changes. This will enable analysis of real-time scheduling delays per thread priority, which cannot be performed accurately if we only trace the priority of the currently scheduled processes.
Quoted 8 lines
Quoted 6 lines
The call sites that modify the priority of a task are mostly system
calls:sched_setscheduler,sched_setattr,sched_process_forkandset_user_nice. Priority can also be dynamically boosted through priority inheritance ofrt_mutexbyrt_mutex_setprio.Signed-off-by Julien Desfossez <[email protected]>Reviewed-by Mathieu Desnoyers <[email protected]>
CCing Ingo and Peter on the first patch of the series too, so they can let us know if we missed anything fundamental related to sched_deadline.
Thanks,
Mathieu
Quoted 96 lines
Quoted 89 lines
---
include/linux/sched.h| 3 ++-kernel/sched/core.c| 19 +++++++++++++------2 files changed, 15 insertions(+), 7 deletions(-)include/linux/sched.h +2
-1@@ -1409,7 +1409,8 @@ struct task_struct {#endif int on_rq; -int prio, static_prio, normal_prio; +int prio;/* Updated through sched_set_prio() */ +int static_prio, normal_prio;unsigned int rt_priority;const struct sched_class *sched_class;struct sched_entity se;kernel/sched/core.c +6
-1@@ -2230,6 +2230,11 @@ int sysctl_schedstats(struct ctl_table *table, int write,#endif #endif +static void sched_set_prio (struct task_struct *p,int prio) +{ + p->prio = prio; +} +/* * fork()/clone()-time setup: */ @@ -2249,7 +2254,7 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p)/* * Make sure we do not leak PI boosting priority to the child. */ - p->prio = current->normal_prio; + sched_set_prio(p, current->normal_prio);/* * Revert to default priority/policy on fork if requested.
kernel/sched/core.c +2
-1@@ -2262,7 +2267,8 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p) }else if (PRIO_TO_NICE (p->static_prio) <0 ) p->static_prio =NICE_TO_PRIO (0 ); - p->prio = p->normal_prio =__normal_prio (p); + p->normal_prio =__normal_prio (p); +sched_set_prio (p, p->normal_prio);set_load_weight (p);/*kernel/sched/core.c +5
-4@@ -3477,7 +3483,7 @@ void rt_mutex_setprio(struct task_struct *p, int prio) p->sched_class = &fair_sched_class; } - p->prio = prio; +sched_set_prio (p, prio);if (running) p->sched_class->set_curr_task (rq);@@ -3524,7 +3530,7 @@ void set_user_nice(struct task_struct *p, long nice) p->static_prio =NICE_TO_PRIO (nice);set_load_weight (p); old_prio = p->prio; - p->prio =effective_prio (p); +sched_set_prio (p,effective_prio (p)); delta = p->prio - old_prio;if (queued) {@@ -3731,9 +3737,10 @@ static void __setscheduler(struct rq *rq, structtask_struct *p, * sched_setscheduler(). */ if (keep_boost) - p->prio =rt_mutex_get_effective_prio (p,normal_prio (p)); +sched_set_prio (p,rt_mutex_get_effective_prio (p, +normal_prio (p)));else - p->prio =normal_prio (p); +sched_set_prio (p,normal_prio (p));if (dl_prio (p->prio))p->sched_class = &dl_sched_class;Quoted 2 lines
--
1.9.1--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Peter Zijlstra
maintainerQuoted 9 lines · Mathieu Desnoyers
Quoted 9 lines
kernel/sched/core.c int sysctl_schedstats(struct ctl_table *table, int write, · line 2236
Quoted 9 lines
+++
b/kernel/sched/core.ckernel/sched/core.c +4
-0@@ -2230,6 +2230,11 @@ int sysctl_schedstats(struct ctl_table *table, int write,#endif #endif +static void sched_set_prio (struct task_struct *p,int prio) +{ + p->prio = prio; +}
Urgh; so there's a bunch of patches to the PI code that I've been ignoring that'll completely break all this.
Let me dig them up and get this all sorted.
Basically, looking at @prio is broken.
Re: [RFC PATCH 2/2] tracing: add sched_set_prio tracepoint
In reply to Peter ZijlstraQuoted 8 lines · Peter Zijlstra
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
include/trace/events/sched.h TRACE_EVENT(sched_pi_setprio, · line 449
Quoted 7 lines
include/trace/events/sched.h +7
-0+/* +* Tracepoint for priority changes of a task. +*/ +DEFINE_EVENT (sched_prio_template, sched_set_prio, + TP_PROTO(struct task_struct *tsk, int newprio), +TP_ARGS (tsk, newprio)); +Nak on anything that cannot fundamentally deal with
SCHED_DEADLINE.
My 2 cents: I liked the idea... But, I agree with peterz. This cannot fundamentally deal with deadline scheduler.
In the deadline scheduler, the priority is set at every new sporadic activation of the task (it is a fixed job priority scheduler - not a fixed priority scheduler). The priority is the deadline of the new job. The deadline is an u64, that does not fit in the int prio - so it cannot be reused.
-- Daniel