Skip to contentSkip to conversation

perf core: Add PERF_COUNT_SW_CGROUP_SWITCHES event

RFC In review Namhyung Kim 4 participants 14 messages Dec 2, 2020 – Dec 4, 2020 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

RFC 1/2 perf core: Add PERF_COUNT_SW_CGROUP_SWITCHES event

This patch adds a new software event to count context switches involving cgroup switches. So it's counted only if cgroups of previous and next tasks are different.

One can argue that we can do this by using existing sched_switch event with eBPF. But some systems might not have eBPF for some reason so I'd like to add this as a simple way.

---
 include/linux/perf_event.h      | 22 ++++++++++++++++++++++
 include/uapi/linux/perf_event.h |  1 +
 2 files changed, 23 insertions(+)
include/linux/perf_event.h +22 -0
index 9a38f579bc76..d6dec422ba03 100644
@@ -1185,6 +1185,27 @@ perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
 	}
 }
 
+#ifdef CONFIG_CGROUP_PERF
+static inline void
+perf_sw_event_cgroup_switch(struct task_struct *prev, struct task_struct *next)
+{
+	struct cgroup *prev_cgrp, *next_cgrp;
+
+	rcu_read_lock();
+
+	prev_cgrp = task_css_check(prev, perf_event_cgrp_id, 1)->cgroup;
+	next_cgrp = task_css_check(next, perf_event_cgrp_id, 1)->cgroup;
+
+	if (prev_cgrp != next_cgrp)
+		perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);
+
+	rcu_read_unlock();
+}
+#else
+static inline void perf_sw_event_cgroup_switch(struct task_struct *prev,
+					       struct task_struct *next) {}
+#endif  /* CONFIG_CGROUP_PERF */
+
 extern struct static_key_false perf_sched_events;
 
 static __always_inline bool
@@ -1220,6 +1241,7 @@ static inline void perf_event_task_sched_out(struct task_struct *prev,
 					     struct task_struct *next)
 {
 	perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
+	perf_sw_event_cgroup_switch(prev, next);
 
 	if (static_branch_unlikely(&perf_sched_events))
 		__perf_event_task_sched_out(prev, next);
include/uapi/linux/perf_event.h +1 -0
index b15e3447cd9f..16b9538ad89b 100644
@@ -112,6 +112,7 @@ enum perf_sw_ids {
 	PERF_COUNT_SW_EMULATION_FAULTS		= 8,
 	PERF_COUNT_SW_DUMMY			= 9,
 	PERF_COUNT_SW_BPF_OUTPUT		= 10,
+	PERF_COUNT_SW_CGROUP_SWITCHES		= 11,
 
 	PERF_COUNT_SW_MAX,			/* non-ABI */
 };
Signature · 1 line
2.29.2.454.gaff20da3a2-goog
author maintainer
#1 lore raw

RFC 2/2 perf tools: Add 'cgroup-switches' software event

It counts how often cgroups are changed actually during the context switches.

  # perf stat -a -e context-switches,cgroup-switches -a sleep 1

Performance counter stats for 'system wide':

              11,267      context-switches
              10,950      cgroup-switches

         1.015634369 seconds time elapsed
---
 tools/include/uapi/linux/perf_event.h | 1 +
 tools/perf/util/parse-events.c        | 4 ++++
 tools/perf/util/parse-events.l        | 1 +
 3 files changed, 6 insertions(+)
tools/include/uapi/linux/perf_event.h +1 -0
index b95d3c485d27..16559703c49c 100644
@@ -112,6 +112,7 @@ enum perf_sw_ids {
 	PERF_COUNT_SW_EMULATION_FAULTS		= 8,
 	PERF_COUNT_SW_DUMMY			= 9,
 	PERF_COUNT_SW_BPF_OUTPUT		= 10,
+	PERF_COUNT_SW_CGROUP_SWITCHES		= 11,
 
 	PERF_COUNT_SW_MAX,			/* non-ABI */
 };
tools/perf/util/parse-events.c +4 -0
index 3b273580fb84..f6a5a099e143 100644
@@ -145,6 +145,10 @@ struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
 		.symbol = "bpf-output",
 		.alias  = "",
 	},
+	[PERF_COUNT_SW_CGROUP_SWITCHES] = {
+		.symbol = "cgroup-switches",
+		.alias  = "",
+	},
 };
 
 #define __PERF_EVENT_FIELD(config, name) \
tools/perf/util/parse-events.l +1 -0
index 9db5097317f4..88f203bb6fab 100644
@@ -347,6 +347,7 @@ emulation-faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EM
 dummy						{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
 duration_time					{ return tool(yyscanner, PERF_TOOL_DURATION_TIME); }
 bpf-output					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); }
+cgroup-switches					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CGROUP_SWITCHES); }
 
 	/*
 	 * We have to handle the kernel PMU event cycles-ct/cycles-t/mem-loads/mem-stores separately.
Signature · 1 line
2.29.2.454.gaff20da3a2-goog
maintainer
#2 lore raw
In reply to Namhyung Kim
Quoted 25 lines · Namhyung Kim
(quoted diff) +21 -0
+#ifdef CONFIG_CGROUP_PERF
+static inline void
+perf_sw_event_cgroup_switch(struct task_struct *prev, struct task_struct *next)
+{
+	struct cgroup *prev_cgrp, *next_cgrp;
+
+	rcu_read_lock();
+
+	prev_cgrp = task_css_check(prev, perf_event_cgrp_id, 1)->cgroup;
+	next_cgrp = task_css_check(next, perf_event_cgrp_id, 1)->cgroup;
+
+	if (prev_cgrp != next_cgrp)
+		perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);
+
+	rcu_read_unlock();
+}
+#else
+static inline void perf_sw_event_cgroup_switch(struct task_struct *prev,
+					       struct task_struct *next) {}
+#endif  /* CONFIG_CGROUP_PERF */
+
 extern struct static_key_false perf_sched_events;
 
 static __always_inline bool

Review static inline void perf_event_task_sched_out(struct task_struct *prev, · line 1247

Quoted 7 lines
(quoted diff) +1 -0
@@ -1220,6 +1241,7 @@ static inline void perf_event_task_sched_out(struct task_struct *prev,
 					     struct task_struct *next)
 {
 	perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
+	perf_sw_event_cgroup_switch(prev, next);
 
 	if (static_branch_unlikely(&perf_sched_events))
 		__perf_event_task_sched_out(prev, next);

Urgh.. that's horrible, try something like this.

include/linux/perf_event.h +16 -20
index 9a38f579bc76..5eb284819ee5 100644
@@ -1174,25 +1174,19 @@ DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
  * which is guaranteed by us not actually scheduling inside other swevents
  * because those disable preemption.
  */
-static __always_inline void
-perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
+static __always_inline void __perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
 {
-	if (static_key_false(&perf_swevent_enabled[event_id])) {
-		struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
+	struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
 
-		perf_fetch_caller_regs(regs);
-		___perf_sw_event(event_id, nr, regs, addr);
-	}
+	perf_fetch_caller_regs(regs);
+	___perf_sw_event(event_id, nr, regs, addr);
 }
 
 extern struct static_key_false perf_sched_events;
 
-static __always_inline bool
-perf_sw_migrate_enabled(void)
+static __always_inline bool __perf_sw_enabled(int swevt)
 {
-	if (static_key_false(&perf_swevent_enabled[PERF_COUNT_SW_CPU_MIGRATIONS]))
-		return true;
-	return false;
+	return static_key_false(&perf_swevent_enabled[swevt]);
 }
 
 static inline void perf_event_task_migrate(struct task_struct *task)
@@ -1207,11 +1201,9 @@ static inline void perf_event_task_sched_in(struct task_struct *prev,
 	if (static_branch_unlikely(&perf_sched_events))
 		__perf_event_task_sched_in(prev, task);
 
-	if (perf_sw_migrate_enabled() && task->sched_migrated) {
-		struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
-
-		perf_fetch_caller_regs(regs);
-		___perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, regs, 0);
+	if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS) &&
+	    task->sched_migrated) {
+		__perf_sw_event_sched(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 0);
 		task->sched_migrated = 0;
 	}
 }
@@ -1219,7 +1211,13 @@ static inline void perf_event_task_sched_in(struct task_struct *prev,
 static inline void perf_event_task_sched_out(struct task_struct *prev,
 					     struct task_struct *next)
 {
-	perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
+	if (__perf_sw_enabled(PERF_COUNT_SW_CONTEXT_SWITCHES))
+		__perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
+
+	if (__perf_sw_enabled(PERF_COUNT_SW_CGROUP_SWITCHES) &&
+	    (task_css_check(prev, perf_event_cgrp_id, 1)->cgroup !=
+	     task_css_check(next, perf_event_cgrp_id, 1)->cgroup))
+		__perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);
 
 	if (static_branch_unlikely(&perf_sched_events))
 		__perf_event_task_sched_out(prev, next);
@@ -1475,8 +1473,6 @@ static inline int perf_event_refresh(struct perf_event *event, int refresh)
 static inline void
 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)	{ }
 static inline void
-perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)			{ }
-static inline void
 perf_bp_event(struct perf_event *event, void *data)			{ }
 
 static inline int perf_register_guest_info_callbacks
author maintainer
#3 lore raw

Hi Peter,

Quoted 91 lines · Peter Zijlstra
Quoted 25 lines · Namhyung Kim
include/linux/perf_event.h +21 -0
+#ifdef CONFIG_CGROUP_PERF
+static inline void
+perf_sw_event_cgroup_switch(struct task_struct *prev, struct task_struct *next)
+{
+     struct cgroup *prev_cgrp, *next_cgrp;
+
+     rcu_read_lock();
+
+     prev_cgrp = task_css_check(prev, perf_event_cgrp_id, 1)->cgroup;
+     next_cgrp = task_css_check(next, perf_event_cgrp_id, 1)->cgroup;
+
+     if (prev_cgrp != next_cgrp)
+             perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);
+
+     rcu_read_unlock();
+}
+#else
+static inline void perf_sw_event_cgroup_switch(struct task_struct *prev,
+                                            struct task_struct *next) {}
+#endif  /* CONFIG_CGROUP_PERF */
+
 extern struct static_key_false perf_sched_events;
 
 static __always_inline bool

include/linux/perf_event.h static inline void perf_event_task_sched_out(struct task_struct *prev, · line 1247

Quoted 7 lines
include/linux/perf_event.h +1 -0
@@ -1220,6 +1241,7 @@ static inline void perf_event_task_sched_out(struct task_struct *prev,
                                           struct task_struct *next)
 {
      perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
+     perf_sw_event_cgroup_switch(prev, next);
 
      if (static_branch_unlikely(&perf_sched_events))
              __perf_event_task_sched_out(prev, next);

Urgh.. that's horrible, try something like this.

include/linux/perf_event.h +9 -17
index 9a38f579bc76..5eb284819ee5 100644
@@ -1174,25 +1174,19 @@ DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
  * which is guaranteed by us not actually scheduling inside other swevents
  * because those disable preemption.
  */
-static __always_inline void
-perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
+static __always_inline void __perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
 {
-       if (static_key_false(&perf_swevent_enabled[event_id])) {
-               struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
+       struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
 
-               perf_fetch_caller_regs(regs);
-               ___perf_sw_event(event_id, nr, regs, addr);
-       }
+       perf_fetch_caller_regs(regs);
+       ___perf_sw_event(event_id, nr, regs, addr);
 }
 
 extern struct static_key_false perf_sched_events;
 
-static __always_inline bool
-perf_sw_migrate_enabled(void)
+static __always_inline bool __perf_sw_enabled(int swevt)
 {
-       if (static_key_false(&perf_swevent_enabled[PERF_COUNT_SW_CPU_MIGRATIONS]))
-               return true;
-       return false;
+       return static_key_false(&perf_swevent_enabled[swevt]);
 }
 
 static inline void perf_event_task_migrate(struct task_struct *task)
@@ -1207,11 +1201,9 @@ static inline void perf_event_task_sched_in(struct task_struct *prev,
        if (static_branch_unlikely(&perf_sched_events))
                __perf_event_task_sched_in(prev, task);
 
-       if (perf_sw_migrate_enabled() && task->sched_migrated) {
-               struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
-
-               perf_fetch_caller_regs(regs);
-               ___perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, regs, 0);
+       if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS) &&
+           task->sched_migrated) {
+               __perf_sw_event_sched(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 0);
                task->sched_migrated = 0;
        }
 }
@@ -1219,7 +1211,13 @@ static inline void perf_event_task_sched_in(struct task_struct *prev,
 static inline void perf_event_task_sched_out(struct task_struct *prev,

include/linux/perf_event.h static inline void perf_event_task_sched_in(struct task_struct *prev, · line 1220

Quoted 10 lines
include/linux/perf_event.h +7 -1
                                             struct task_struct *next)
 {
-       perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
+       if (__perf_sw_enabled(PERF_COUNT_SW_CONTEXT_SWITCHES))
+               __perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
+
+       if (__perf_sw_enabled(PERF_COUNT_SW_CGROUP_SWITCHES) &&
+           (task_css_check(prev, perf_event_cgrp_id, 1)->cgroup !=
+            task_css_check(next, perf_event_cgrp_id, 1)->cgroup))
+               __perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);

Right, it needs to check only when the event is enabled.

Thanks,
Namhyung

Quoted 12 lines
include/linux/perf_event.h +0 -2
        if (static_branch_unlikely(&perf_sched_events))
                __perf_event_task_sched_out(prev, next);
@@ -1475,8 +1473,6 @@ static inline int perf_event_refresh(struct perf_event *event, int refresh)
 static inline void
 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)    { }
 static inline void
-perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)                    { }
-static inline void
 perf_bp_event(struct perf_event *event, void *data)                    { }
 
 static inline int perf_register_guest_info_callbacks
author maintainer
#4 lore raw
In reply to Peter Zijlstra

include/linux/perf_event.h DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]); · line 1177

Quoted 11 lines · Peter Zijlstra
include/linux/perf_event.h +1 -2
index 9a38f579bc76..5eb284819ee5 100644
@@ -1174,25 +1174,19 @@ DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
  * which is guaranteed by us not actually scheduling inside other swevents
  * because those disable preemption.
  */
-static __always_inline void
-perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
+static __always_inline void __perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)

It'd be nice to avoid the __ prefix if possible.

Quoted 7 lines
include/linux/perf_event.h +1 -4
 {
-       if (static_key_false(&perf_swevent_enabled[event_id])) {
-               struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
+       struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
 
-               perf_fetch_caller_regs(regs);
-               ___perf_sw_event(event_id, nr, regs, addr);

include/linux/perf_event.h DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]); · line 1187

Quoted 10 lines
include/linux/perf_event.h +3 -3
-       }
+       perf_fetch_caller_regs(regs);
+       ___perf_sw_event(event_id, nr, regs, addr);
 }
 
 extern struct static_key_false perf_sched_events;
 
-static __always_inline bool
-perf_sw_migrate_enabled(void)
+static __always_inline bool __perf_sw_enabled(int swevt)

Ditto.

Quoted 9 lines
include/linux/perf_event.h +1 -3
 {
-       if (static_key_false(&perf_swevent_enabled[PERF_COUNT_SW_CPU_MIGRATIONS]))
-               return true;
-       return false;
+       return static_key_false(&perf_swevent_enabled[swevt]);
 }
 
 static inline void perf_event_task_migrate(struct task_struct *task)

include/linux/perf_event.h static inline void perf_event_task_sched_in(struct task_struct *prev, · line 1205

Quoted 10 lines
include/linux/perf_event.h +2 -5
@@ -1207,11 +1201,9 @@ static inline void perf_event_task_sched_in(struct task_struct *prev,
        if (static_branch_unlikely(&perf_sched_events))
                __perf_event_task_sched_in(prev, task);
 
-       if (perf_sw_migrate_enabled() && task->sched_migrated) {
-               struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
-
-               perf_fetch_caller_regs(regs);
-               ___perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, regs, 0);
+       if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS) &&
+           task->sched_migrated) {

It seems task->sched_migrate is set only if the event is enabled, then can we just check the value here?

Quoted 6 lines
include/linux/perf_event.h +1 -0
+               __perf_sw_event_sched(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 0);
                task->sched_migrated = 0;
        }
 }
@@ -1219,7 +1211,13 @@ static inline void perf_event_task_sched_in(struct task_struct *prev,
 static inline void perf_event_task_sched_out(struct task_struct *prev,

include/linux/perf_event.h static inline void perf_event_task_sched_in(struct task_struct *prev, · line 1220

Quoted 10 lines
include/linux/perf_event.h +7 -1
                                             struct task_struct *next)
 {
-       perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
+       if (__perf_sw_enabled(PERF_COUNT_SW_CONTEXT_SWITCHES))
+               __perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
+
+       if (__perf_sw_enabled(PERF_COUNT_SW_CGROUP_SWITCHES) &&
+           (task_css_check(prev, perf_event_cgrp_id, 1)->cgroup !=
+            task_css_check(next, perf_event_cgrp_id, 1)->cgroup))
+               __perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);

I was not clear about the RCU protection here. Is it ok to access the task's css_set directly?

Thanks,
Namhyung

Quoted 12 lines
include/linux/perf_event.h +0 -2
        if (static_branch_unlikely(&perf_sched_events))
                __perf_event_task_sched_out(prev, next);
@@ -1475,8 +1473,6 @@ static inline int perf_event_refresh(struct perf_event *event, int refresh)
 static inline void
 perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)    { }
 static inline void
-perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)                    { }
-static inline void
 perf_bp_event(struct perf_event *event, void *data)                    { }
 
 static inline int perf_register_guest_info_callbacks
maintainer
#5 lore raw
Quoted 15 lines · Namhyung Kim

include/linux/perf_event.h DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]); · line 1177

Quoted 11 lines · Peter Zijlstra
include/linux/perf_event.h +1 -2
index 9a38f579bc76..5eb284819ee5 100644
@@ -1174,25 +1174,19 @@ DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
  * which is guaranteed by us not actually scheduling inside other swevents
  * because those disable preemption.
  */
-static __always_inline void
-perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
+static __always_inline void __perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)

It'd be nice to avoid the __ prefix if possible.

Not having __ would seem to suggest its a function of generic utility. Still, *shrug* ;-)

Quoted 12 lines
Quoted 12 lines
include/linux/perf_event.h +1 -3
 {
-       if (static_key_false(&perf_swevent_enabled[PERF_COUNT_SW_CPU_MIGRATIONS]))
-               return true;
-       return false;
+       return static_key_false(&perf_swevent_enabled[swevt]);
 }
 
 static inline void perf_event_task_migrate(struct task_struct *task)
@@ -1207,11 +1201,9 @@ static inline void perf_event_task_sched_in(struct task_struct *prev,
        if (static_branch_unlikely(&perf_sched_events))
                __perf_event_task_sched_in(prev, task);
 
Quoted 10 lines

include/linux/perf_event.h static inline void perf_event_task_sched_in(struct task_struct *prev, · line 1205

Quoted 7 lines
include/linux/perf_event.h +2 -5
-       if (perf_sw_migrate_enabled() && task->sched_migrated) {
-               struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
-
-               perf_fetch_caller_regs(regs);
-               ___perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, regs, 0);
+       if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS) &&
+           task->sched_migrated) {

It seems task->sched_migrate is set only if the event is enabled, then can we just check the value here?

Why suffer the unconditional load and test? Your L1 too big?

Quoted 11 lines
Quoted 11 lines
include/linux/perf_event.h +3 -1
+               __perf_sw_event_sched(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 0);
                task->sched_migrated = 0;
        }
 }
@@ -1219,7 +1211,13 @@ static inline void perf_event_task_sched_in(struct task_struct *prev,
 static inline void perf_event_task_sched_out(struct task_struct *prev,
                                             struct task_struct *next)
 {
-       perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
+       if (__perf_sw_enabled(PERF_COUNT_SW_CONTEXT_SWITCHES))
+               __perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
Quoted 8 lines

include/linux/perf_event.h static inline void perf_event_task_sched_in(struct task_struct *prev, · line 1220

Quoted 5 lines
include/linux/perf_event.h +5 -0
+
+       if (__perf_sw_enabled(PERF_COUNT_SW_CGROUP_SWITCHES) &&
+           (task_css_check(prev, perf_event_cgrp_id, 1)->cgroup !=
+            task_css_check(next, perf_event_cgrp_id, 1)->cgroup))
+               __perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);

I was not clear about the RCU protection here. Is it ok to access the task's css_set directly?

We're here with preemption and IRQs disabled, good luck trying to get RCU to consider that not a critical section and spirit things away under us.

author maintainer
#6 lore raw
Quoted 18 lines · Peter Zijlstra
Quoted 15 lines · Namhyung Kim

include/linux/perf_event.h DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]); · line 1177

Quoted 11 lines · Peter Zijlstra
include/linux/perf_event.h +1 -2
index 9a38f579bc76..5eb284819ee5 100644
@@ -1174,25 +1174,19 @@ DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
  * which is guaranteed by us not actually scheduling inside other swevents
  * because those disable preemption.
  */
-static __always_inline void
-perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
+static __always_inline void __perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)

It'd be nice to avoid the __ prefix if possible.

Quoted 2 lines

Not having __ would seem to suggest its a function of generic utility. Still, *shrug* ;-)

Ok, noted.

Quoted 24 lines
Quoted 13 lines
Quoted 13 lines
include/linux/perf_event.h +1 -3
 {
-       if (static_key_false(&perf_swevent_enabled[PERF_COUNT_SW_CPU_MIGRATIONS]))
-               return true;
-       return false;
+       return static_key_false(&perf_swevent_enabled[swevt]);
 }
 
 static inline void perf_event_task_migrate(struct task_struct *task)
@@ -1207,11 +1201,9 @@ static inline void perf_event_task_sched_in(struct task_struct *prev,
        if (static_branch_unlikely(&perf_sched_events))
                __perf_event_task_sched_in(prev, task);
 
Quoted 10 lines

include/linux/perf_event.h static inline void perf_event_task_sched_in(struct task_struct *prev, · line 1205

Quoted 7 lines
include/linux/perf_event.h +2 -5
-       if (perf_sw_migrate_enabled() && task->sched_migrated) {
-               struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
-
-               perf_fetch_caller_regs(regs);
-               ___perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, regs, 0);
+       if (__perf_sw_enabled(PERF_COUNT_SW_CPU_MIGRATIONS) &&
+           task->sched_migrated) {

It seems task->sched_migrate is set only if the event is enabled, then can we just check the value here?

Quoted 1 line

Why suffer the unconditional load and test? Your L1 too big?

I just wanted to avoid typing long lines.. ;-p

Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
include/linux/perf_event.h +3 -1
+               __perf_sw_event_sched(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 0);
                task->sched_migrated = 0;
        }
 }
@@ -1219,7 +1211,13 @@ static inline void perf_event_task_sched_in(struct task_struct *prev,
 static inline void perf_event_task_sched_out(struct task_struct *prev,
                                             struct task_struct *next)
 {
-       perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
+       if (__perf_sw_enabled(PERF_COUNT_SW_CONTEXT_SWITCHES))
+               __perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
Quoted 8 lines

include/linux/perf_event.h static inline void perf_event_task_sched_in(struct task_struct *prev, · line 1220

Quoted 5 lines
include/linux/perf_event.h +5 -0
+
+       if (__perf_sw_enabled(PERF_COUNT_SW_CGROUP_SWITCHES) &&
+           (task_css_check(prev, perf_event_cgrp_id, 1)->cgroup !=
+            task_css_check(next, perf_event_cgrp_id, 1)->cgroup))
+               __perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);

I was not clear about the RCU protection here. Is it ok to access the task's css_set directly?

Quoted 3 lines

We're here with preemption and IRQs disabled, good luck trying to get RCU to consider that not a critical section and spirit things away under us.

Ok, someday I'll go reading the RCU code.. :)

Thanks,
Namhyung

#7 lore raw
In reply to Namhyung Kim

include/linux/perf_event.h perf_sw_event_sched(u32 event_id, u64 nr, u64 addr) · line 1200

Quoted 5 lines
include/linux/perf_event.h +5 -0
+	prev_cgrp = task_css_check(prev, perf_event_cgrp_id, 1)->cgroup;
+	next_cgrp = task_css_check(next, perf_event_cgrp_id, 1)->cgroup;
+
+	if (prev_cgrp != next_cgrp)
+		perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);

Seems to be the perf cgroup only, not all cgroups. That's a big difference and needs to be documented properly.

Probably would make sense to have two events for both, one for all cgroups and one for perf only.

-Andi

#8 lore raw
Quoted 10 lines · Andi Kleen

include/linux/perf_event.h perf_sw_event_sched(u32 event_id, u64 nr, u64 addr) · line 1200

Quoted 6 lines
include/linux/perf_event.h +5 -0
+     prev_cgrp = task_css_check(prev, perf_event_cgrp_id, 1)->cgroup;
+     next_cgrp = task_css_check(next, perf_event_cgrp_id, 1)->cgroup;
+
+     if (prev_cgrp != next_cgrp)
+             perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);

Seems to be the perf cgroup only, not all cgroups. That's a big difference and needs to be documented properly.

We care about the all-cgroup case.

Quoted 6 lines

Probably would make sense to have two events for both, one for all cgroups and one for perf only.

-Andi

#9 lore raw
Quoted 8 lines · Stephane Eranian
Quoted 7 lines · Andi Kleen
Quoted 6 lines
include/linux/perf_event.h +5 -0
+     prev_cgrp = task_css_check(prev, perf_event_cgrp_id, 1)->cgroup;
+     next_cgrp = task_css_check(next, perf_event_cgrp_id, 1)->cgroup;
+
+     if (prev_cgrp != next_cgrp)
+             perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);
Quoted 4 lines
Quoted 3 lines

Seems to be the perf cgroup only, not all cgroups. That's a big difference and needs to be documented properly.

We care about the all-cgroup case.

Then it's not correct I think. You need a different hook point.

-Andi

#10 lore raw
Quoted 10 lines · Andi Kleen
Quoted 8 lines · Stephane Eranian
Quoted 7 lines · Andi Kleen
Quoted 6 lines
include/linux/perf_event.h +5 -0
+     prev_cgrp = task_css_check(prev, perf_event_cgrp_id, 1)->cgroup;
+     next_cgrp = task_css_check(next, perf_event_cgrp_id, 1)->cgroup;
+
+     if (prev_cgrp != next_cgrp)
+             perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

Seems to be the perf cgroup only, not all cgroups. That's a big difference and needs to be documented properly.

We care about the all-cgroup case.

Then it's not correct I think. You need a different hook point.

I realize that ;-(

author maintainer
#11 lore raw

Hi Stephane and Andi,

Quoted 12 lines · Stephane Eranian
Quoted 10 lines · Andi Kleen
Quoted 8 lines · Stephane Eranian
Quoted 7 lines · Andi Kleen
Quoted 6 lines
include/linux/perf_event.h +5 -0
+     prev_cgrp = task_css_check(prev, perf_event_cgrp_id, 1)->cgroup;
+     next_cgrp = task_css_check(next, perf_event_cgrp_id, 1)->cgroup;
+
+     if (prev_cgrp != next_cgrp)
+             perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

Seems to be the perf cgroup only, not all cgroups. That's a big difference and needs to be documented properly.

We care about the all-cgroup case.

Then it's not correct I think. You need a different hook point.

I realize that ;-(

If we want to count any cgroup changes, I think we can compare task->cgroups (css_set) here instead.

Thanks,
Namyung

maintainer
#12 lore raw
In reply to Andi Kleen
Quoted 8 lines · Andi Kleen

include/linux/perf_event.h perf_sw_event_sched(u32 event_id, u64 nr, u64 addr) · line 1200

Quoted 5 lines
include/linux/perf_event.h +5 -0
+	prev_cgrp = task_css_check(prev, perf_event_cgrp_id, 1)->cgroup;
+	next_cgrp = task_css_check(next, perf_event_cgrp_id, 1)->cgroup;
+
+	if (prev_cgrp != next_cgrp)
+		perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);

Seems to be the perf cgroup only, not all cgroups. That's a big difference and needs to be documented properly.

With cgroup-v2 that's all the same, no?

author maintainer
#13 lore raw
Quoted 8 lines · Peter Zijlstra
Quoted 6 lines · Andi Kleen
Quoted 5 lines
include/linux/perf_event.h +5 -0
+   prev_cgrp = task_css_check(prev, perf_event_cgrp_id, 1)->cgroup;
+   next_cgrp = task_css_check(next, perf_event_cgrp_id, 1)->cgroup;
+
+   if (prev_cgrp != next_cgrp)
+           perf_sw_event_sched(PERF_COUNT_SW_CGROUP_SWITCHES, 1, 0);
Quoted 4 lines
Quoted 2 lines

Seems to be the perf cgroup only, not all cgroups. That's a big difference and needs to be documented properly.

With cgroup-v2 that's all the same, no?

Right, but unfortunately it seems still cgroup v1 is used widely.

Thanks,
Namhyung