sched_ext: Implement core event counters
Subsystems (2)
Changwoo Min
reviewerv4 cover sched_ext: Implement core event counters
The sched_ext core often has to override the BPF scheduler decisions, and some events could be interesting but not easily visible.
This patchset aims to address such a problem in the following manner:
- Introduce an infrastructure to collect such events in a scalable and extensible way and to expose the collected events to the BPF scheduler in a compatible way.
- Define seven events to be collected.
- Modify scx schedulers to demonstrate the usage of the new BPF APIs.
ChangeLog: v3 -> v4
- Do not include the already applied patches (1-4) from the patchset.
- Add a missing scx_agg_event() for SCX_EV_ENQ_SKIP_EXITINGk.
- Move SCX_EV_BYPASS_DISPATCH loggig to select_task_rq_scx() and
do_enqueue_task().
- Move scx_bypass_timestamp inside the scx_ops_bypass function (renamed to bypass_timestamp).
ChangeLog: v2 -> v3
- Rename scx_bpf_event_stats() to scx_bpf_events().
- Add a prefix SCX_EV_ to all event names.
- Change the implementation SCX_EV_SELECT_CPU_FALLBACK such that record the selected cpu (p->scx.selected_cpu) in select_task_rq_scx() and compare it in enqueue_task_scx() to reliably count events.
- Change the implementation of SCX_EV_BYPASS_DISPATCH such that log the event where the condition is initially detected.
- Modify scx_qmap to print the core event counter every second.
- Remove unnecessary white space in patch 6.
ChangeLog: v1 -> v2
- Rename scx_event_stat and scx_bpf_event_stat() to scx_event_stats and
scx_bpf_event_stats().
- Rename event names following the convention of $COMPONENT_$EVENT.
- Rename event_stats to event_stats_cpu.
- Drop the enum scx_event_kind and related macros.
- Revise scx_add_event() to use this_cpu_add().
- Add __scx_add_event() to use __this_cpu_add().
- Move the event counter resetting code to the loading of a BPF scheduler.
- The bypass-related event is further categorized into three events:
BYPASS_ACTIVATE, BYPASS_DISPATCH, and BYPASS_DURATION.
- Revise SELECT_CPU_FALLBACK to capture the case of the chosen CPU is not allowed.
- Move is_cpu_allowed() from core.c to sched.h to use in the SELECT_CPU_FALLBACK code.
Changwoo Min (7):
sched_ext: Add an event, SCX_EV_ENQ_SKIP_EXITING
sched_ext: Add an event, SCX_EV_BYPASS_ACTIVATE
sched_ext: Add an event, SCX_EV_BYPASS_DISPATCH
sched_ext: Add an event, SCX_EV_BYPASS_DURATION
sched_ext: Add scx_bpf_events() and scx_read_event() for BPF
schedulers
sched_ext: Print core event count in scx_central scheduler
sched_ext: Print core event count in scx_qmap scheduler
kernel/sched/ext.c | 51 ++++++++++++++++++++++-- tools/sched_ext/include/scx/common.bpf.h | 4 ++ tools/sched_ext/scx_central.bpf.c | 21 ++++++++++ tools/sched_ext/scx_qmap.bpf.c | 19 +++++++++ 4 files changed, 92 insertions(+), 3 deletions(-)
Signature · 1 line
2.48.1
Changwoo Min
reviewerPATCH v4 1/7 sched_ext: Add an event, SCX_EV_ENQ_SKIP_EXITING
Add a core event, SCX_EV_ENQ_SKIP_EXITING, which represents how many times a task is enqueued to a local DSQ when exiting if SCX_OPS_ENQ_EXITING is not set.
__scx_add_event() is used since the caller holds an rq lock, so the preemption has already been disabled.
--- kernel/sched/ext.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
kernel/sched/ext.c +11 -1
@@ -1461,6 +1461,12 @@ struct scx_event_stats {* continued to run because there were no other tasks on the CPU. */ u64 SCX_EV_DISPATCH_KEEP_LAST ; + +/* +* If SCX_OPS_ENQ_EXITING is not set, the number of times that a task +* is dispatched to a local DSQ when exiting. +*/ +u64 SCX_EV_ENQ_SKIP_EXITING ; };/*
@@ -2068,8 +2074,10 @@ static void do_enqueue_task(struct rq *rq, struct task_struct *p, u64 enq_flags,/* see %SCX_OPS_ENQ_EXITING */ if (!static_branch_unlikely (&scx_ops_enq_exiting) && -unlikely (p->flags &PF_EXITING )) +unlikely (p->flags &PF_EXITING )) { +__scx_add_event (SCX_EV_ENQ_SKIP_EXITING ,1 ); goto local; + }if (!SCX_HAS_OP (enqueue)) goto global;
@@ -4985,6 +4993,7 @@ static void scx_dump_state(struct scx_exit_info *ei, size_t dump_len)scx_dump_event (s, &events,SCX_EV_SELECT_CPU_FALLBACK );scx_dump_event (s, &events,SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE );scx_dump_event (s, &events,SCX_EV_DISPATCH_KEEP_LAST ); +scx_dump_event (s, &events,SCX_EV_ENQ_SKIP_EXITING );if (seq_buf_has_overflowed (&s) && dump_len >=sizeof (trunc_marker)) memcpy(ei->dump + dump_len - sizeof(trunc_marker),
@@ -7121,6 +7130,7 @@ __bpf_kfunc void scx_bpf_events(struct scx_event_stats *events,scx_agg_event (&e_sys, e_cpu,SCX_EV_SELECT_CPU_FALLBACK );scx_agg_event (&e_sys, e_cpu,SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE );scx_agg_event (&e_sys, e_cpu,SCX_EV_DISPATCH_KEEP_LAST ); +scx_agg_event (&e_sys, e_cpu,SCX_EV_ENQ_SKIP_EXITING ); }/*
Signature · 1 line
2.48.1
Changwoo Min
reviewerPATCH v4 2/7 sched_ext: Add an event, SCX_EV_BYPASS_ACTIVATE
In reply to Changwoo MinAdd a core event, SCX_EV_BYPASS_ACTIVATE, which represents how many times the bypass mode has been triggered.
--- kernel/sched/ext.c | 8 ++++++++ 1 file changed, 8 insertions(+)
kernel/sched/ext.c +8 -0
@@ -1467,6 +1467,11 @@ struct scx_event_stats {* is dispatched to a local DSQ when exiting. */ u64 SCX_EV_ENQ_SKIP_EXITING ; + +/* +* The number of times the bypassing mode has been activated. +*/ +u64 SCX_EV_BYPASS_ACTIVATE ; };/*
@@ -4399,6 +4404,7 @@ static void scx_ops_bypass(bool bypass)WARN_ON_ONCE (scx_ops_bypass_depth <=0 );if (scx_ops_bypass_depth !=1 ) goto unlock; +scx_add_event (SCX_EV_BYPASS_ACTIVATE ,1 ); }else { scx_ops_bypass_depth--;WARN_ON_ONCE (scx_ops_bypass_depth <0 );
@@ -4994,6 +5000,7 @@ static void scx_dump_state(struct scx_exit_info *ei, size_t dump_len)scx_dump_event (s, &events,SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE );scx_dump_event (s, &events,SCX_EV_DISPATCH_KEEP_LAST );scx_dump_event (s, &events,SCX_EV_ENQ_SKIP_EXITING ); +scx_dump_event (s, &events,SCX_EV_BYPASS_ACTIVATE );if (seq_buf_has_overflowed (&s) && dump_len >=sizeof (trunc_marker)) memcpy(ei->dump + dump_len - sizeof(trunc_marker),
@@ -7131,6 +7138,7 @@ __bpf_kfunc void scx_bpf_events(struct scx_event_stats *events,scx_agg_event (&e_sys, e_cpu,SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE );scx_agg_event (&e_sys, e_cpu,SCX_EV_DISPATCH_KEEP_LAST );scx_agg_event (&e_sys, e_cpu,SCX_EV_ENQ_SKIP_EXITING ); +scx_agg_event (&e_sys, e_cpu,SCX_EV_BYPASS_ACTIVATE ); }/*
Signature · 1 line
2.48.1
Changwoo Min
reviewerPATCH v4 3/7 sched_ext: Add an event, SCX_EV_BYPASS_DISPATCH
In reply to Changwoo MinAdd a core event, SCX_EV_BYPASS_DISPATCH, which represents how many tasks have been dispatched in the bypass mode.
__scx_add_event() is used since the caller holds an rq lock or p->pi_lock, so the preemption has already been disabled.
--- kernel/sched/ext.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
kernel/sched/ext.c +17 -2
@@ -1468,6 +1468,11 @@ struct scx_event_stats {*/ u64 SCX_EV_ENQ_SKIP_EXITING ; +/* +* The number of tasks dispatched in the bypassing mode. +*/ +u64 SCX_EV_BYPASS_DISPATCH ; +/* * The number of times the bypassing mode has been activated. */
@@ -2071,8 +2076,10 @@ static void do_enqueue_task(struct rq *rq, struct task_struct *p, u64 enq_flags,if (!scx_rq_online (rq)) goto local; -if (scx_rq_bypassing (rq)) +if (scx_rq_bypassing (rq)) { +__scx_add_event (SCX_EV_BYPASS_DISPATCH ,1 ); goto global; + }if (p->scx.ddsp_dsq_id !=SCX_DSQ_INVALID ) goto direct;
@@ -3253,6 +3260,8 @@ bool scx_prio_less(const struct task_struct *a, const struct task_struct *b,static int select_task_rq_scx (struct task_struct *p,int prev_cpu,int wake_flags) { +bool rq_bypass; +/* * sched_exec() calls with %WF_EXEC when @p is about to exec(2) as it * can be a good migration opportunity with low cache and memory
@@ -3266,7 +3275,8 @@ static int select_task_rq_scx(struct task_struct *p, int prev_cpu, int wake_flagif (unlikely (wake_flags &WF_EXEC ))return prev_cpu; -if (SCX_HAS_OP (select_cpu) && !scx_rq_bypassing (task_rq (p))) { + rq_bypass =scx_rq_bypassing (task_rq (p)); +if (SCX_HAS_OP (select_cpu) && !rq_bypass) {s32 cpu;struct task_struct **ddsp_taskp;
@@ -3292,6 +3302,9 @@ static int select_task_rq_scx(struct task_struct *p, int prev_cpu, int wake_flag p->scx.slice =SCX_SLICE_DFL ; p->scx.ddsp_dsq_id =SCX_DSQ_LOCAL ; } + +if (rq_bypass) +__scx_add_event (SCX_EV_BYPASS_DISPATCH ,1 );return cpu; } }
@@ -5000,6 +5013,7 @@ static void scx_dump_state(struct scx_exit_info *ei, size_t dump_len)scx_dump_event (s, &events,SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE );scx_dump_event (s, &events,SCX_EV_DISPATCH_KEEP_LAST );scx_dump_event (s, &events,SCX_EV_ENQ_SKIP_EXITING ); +scx_dump_event (s, &events,SCX_EV_BYPASS_DISPATCH );scx_dump_event (s, &events,SCX_EV_BYPASS_ACTIVATE );if (seq_buf_has_overflowed (&s) && dump_len >=sizeof (trunc_marker))
@@ -7138,6 +7152,7 @@ __bpf_kfunc void scx_bpf_events(struct scx_event_stats *events,scx_agg_event (&e_sys, e_cpu,SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE );scx_agg_event (&e_sys, e_cpu,SCX_EV_DISPATCH_KEEP_LAST );scx_agg_event (&e_sys, e_cpu,SCX_EV_ENQ_SKIP_EXITING ); +scx_agg_event (&e_sys, e_cpu,SCX_EV_BYPASS_DISPATCH );scx_agg_event (&e_sys, e_cpu,SCX_EV_BYPASS_ACTIVATE ); }
Signature · 1 line
2.48.1
Changwoo Min
reviewerPATCH v4 4/7 sched_ext: Add an event, SCX_EV_BYPASS_DURATION
In reply to Changwoo MinAdd a core event, SCX_EV_BYPASS_DURATION, which represents the total duration of bypass modes in nanoseconds.
--- kernel/sched/ext.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
kernel/sched/ext.c +12 -0
@@ -1468,6 +1468,11 @@ struct scx_event_stats {*/ u64 SCX_EV_ENQ_SKIP_EXITING ; +/* +* The total duration of bypass modes in nanoseconds. +*/ +u64 SCX_EV_BYPASS_DURATION ; +/* * The number of tasks dispatched in the bypassing mode. */
@@ -4408,6 +4413,8 @@ static void scx_clear_softlockup(void)static void scx_ops_bypass (bool bypass) {static DEFINE_RAW_SPINLOCK (bypass_lock); +static unsigned long bypass_timestamp; +int cpu;unsigned long flags;
@@ -4417,12 +4424,15 @@ static void scx_ops_bypass(bool bypass)WARN_ON_ONCE (scx_ops_bypass_depth <=0 );if (scx_ops_bypass_depth !=1 ) goto unlock; + bypass_timestamp =ktime_get_ns ();scx_add_event (SCX_EV_BYPASS_ACTIVATE ,1 ); }else { scx_ops_bypass_depth--;WARN_ON_ONCE (scx_ops_bypass_depth <0 );if (scx_ops_bypass_depth !=0 ) goto unlock; +scx_add_event (SCX_EV_BYPASS_DURATION , +ktime_get_ns () - bypass_timestamp); }atomic_inc (&scx_ops_breather_depth);
@@ -5013,6 +5023,7 @@ static void scx_dump_state(struct scx_exit_info *ei, size_t dump_len)scx_dump_event (s, &events,SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE );scx_dump_event (s, &events,SCX_EV_DISPATCH_KEEP_LAST );scx_dump_event (s, &events,SCX_EV_ENQ_SKIP_EXITING ); +scx_dump_event (s, &events,SCX_EV_BYPASS_DURATION );scx_dump_event (s, &events,SCX_EV_BYPASS_DISPATCH );scx_dump_event (s, &events,SCX_EV_BYPASS_ACTIVATE );
@@ -7152,6 +7163,7 @@ __bpf_kfunc void scx_bpf_events(struct scx_event_stats *events,scx_agg_event (&e_sys, e_cpu,SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE );scx_agg_event (&e_sys, e_cpu,SCX_EV_DISPATCH_KEEP_LAST );scx_agg_event (&e_sys, e_cpu,SCX_EV_ENQ_SKIP_EXITING ); +scx_agg_event (&e_sys, e_cpu,SCX_EV_BYPASS_DURATION );scx_agg_event (&e_sys, e_cpu,SCX_EV_BYPASS_DISPATCH );scx_agg_event (&e_sys, e_cpu,SCX_EV_BYPASS_ACTIVATE ); }
Signature · 1 line
2.48.1
Changwoo Min
reviewerPATCH v4 5/7 sched_ext: Add scx_bpf_events() and scx_read_event() for BPF schedulers
In reply to Changwoo Minscx_bpf_events() is added to the header files so the BPF scheduler can use it. Also, scx_read_event() is added to read an event type in a compatible way.
--- tools/sched_ext/include/scx/common.bpf.h | 4 ++++ 1 file changed, 4 insertions(+)
tools/sched_ext/include/scx/common.bpf.h +4 -0
@@ -78,6 +78,10 @@ struct rq *scx_bpf_cpu_rq(s32 cpu) __ksym;struct cgroup *scx_bpf_task_cgroup (struct task_struct *p) __ksym__weak ; u64 scx_bpf_now(void) __ksym __weak; +void scx_bpf_events(struct scx_event_stats *events, size_t events__sz) __ksym __weak; +#define scx_read_event (e, name) \ + (bpf_core_field_exists((e)->name) ? (e)->name : 0) +/* * Use the following as @it__iter when calling scx_bpf_dsq_move[_vtime]() from * within bpf_for_each() loops.
Signature · 1 line
2.48.1
Changwoo Min
reviewerPATCH v4 6/7 sched_ext: Print core event count in scx_central scheduler
In reply to Changwoo MinModify the scx_central scheduler to print the core event counter every second.
--- tools/sched_ext/scx_central.bpf.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
tools/sched_ext/scx_central.bpf.c +21 -0
@@ -256,6 +256,7 @@ static int central_timerfn(void *map, int *key, struct bpf_timer *timer)u64 now =scx_bpf_now ();u64 nr_to_kick = nr_queued;s32 i, curr_cpu; +struct scx_event_stats events; curr_cpu =bpf_get_smp_processor_id ();if (timer_pinned && (curr_cpu != central_cpu)) {
@@ -291,6 +292,26 @@ static int central_timerfn(void *map, int *key, struct bpf_timer *timer)bpf_timer_start (timer,TIMER_INTERVAL_NS ,BPF_F_TIMER_CPU_PIN );__sync_fetch_and_add (&nr_timers,1 ); + +/* print event counters every second */ +if (nr_timers %1000 ==0 ) { +scx_bpf_events (&events,sizeof (events)); + +bpf_printk ("%35s: %llu\n" ,"SCX_EV_SELECT_CPU_FALLBACK" , +scx_read_event (&events,SCX_EV_SELECT_CPU_FALLBACK )); +bpf_printk ("%35s: %llu\n" ,"SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE" , +scx_read_event (&events,SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE )); +bpf_printk ("%35s: %llu\n" ,"SCX_EV_DISPATCH_KEEP_LAST" , +scx_read_event (&events,SCX_EV_DISPATCH_KEEP_LAST )); +bpf_printk ("%35s: %llu\n" ,"SCX_EV_ENQ_SKIP_EXITING" , +scx_read_event (&events,SCX_EV_ENQ_SKIP_EXITING )); +bpf_printk ("%35s: %llu\n" ,"SCX_EV_BYPASS_DURATION" , +scx_read_event (&events,SCX_EV_BYPASS_DURATION )); +bpf_printk ("%35s: %llu\n" ,"SCX_EV_BYPASS_DISPATCH" , +scx_read_event (&events,SCX_EV_BYPASS_DISPATCH )); +bpf_printk ("%35s: %llu\n" ,"SCX_EV_BYPASS_ACTIVATE" , +scx_read_event (&events,SCX_EV_BYPASS_ACTIVATE )); + }return 0 ; }
Signature · 1 line
2.48.1
Changwoo Min
reviewerPATCH v4 7/7 sched_ext: Print core event count in scx_qmap scheduler
In reply to Changwoo MinModify the scx_qmap scheduler to print the core event counter every second.
--- tools/sched_ext/scx_qmap.bpf.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
tools/sched_ext/scx_qmap.bpf.c +19 -0
@@ -763,6 +763,8 @@ static void dump_shared_dsq(void)static int monitor_timerfn (void *map,int *key,struct bpf_timer *timer) { +struct scx_event_stats events; +bpf_rcu_read_lock ();dispatch_highpri (true);bpf_rcu_read_unlock ();
@@ -772,6 +774,23 @@ static int monitor_timerfn(void *map, int *key, struct bpf_timer *timer)if (print_shared_dsq)dump_shared_dsq (); +scx_bpf_events (&events,sizeof (events)); + +bpf_printk ("%35s: %llu\n" ,"SCX_EV_SELECT_CPU_FALLBACK" , +scx_read_event (&events,SCX_EV_SELECT_CPU_FALLBACK )); +bpf_printk ("%35s: %llu\n" ,"SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE" , +scx_read_event (&events,SCX_EV_DISPATCH_LOCAL_DSQ_OFFLINE )); +bpf_printk ("%35s: %llu\n" ,"SCX_EV_DISPATCH_KEEP_LAST" , +scx_read_event (&events,SCX_EV_DISPATCH_KEEP_LAST )); +bpf_printk ("%35s: %llu\n" ,"SCX_EV_ENQ_SKIP_EXITING" , +scx_read_event (&events,SCX_EV_ENQ_SKIP_EXITING )); +bpf_printk ("%35s: %llu\n" ,"SCX_EV_BYPASS_DURATION" , +scx_read_event (&events,SCX_EV_BYPASS_DURATION )); +bpf_printk ("%35s: %llu\n" ,"SCX_EV_BYPASS_DISPATCH" , +scx_read_event (&events,SCX_EV_BYPASS_DISPATCH )); +bpf_printk ("%35s: %llu\n" ,"SCX_EV_BYPASS_ACTIVATE" , +scx_read_event (&events,SCX_EV_BYPASS_ACTIVATE )); +bpf_timer_start (timer,ONE_SEC_IN_NS ,0 );return 0 ; }
Signature · 1 line
2.48.1
Quoted 2 lines · Changwoo Min
The
sched_extcore often has to override the BPF scheduler decisions, and some events could be interesting but not easily visible.
Applied to sched_ext/for-6.15.
Thanks.
Signature · 1 line
tejun