Skip to contentSkip to conversation

PERF: Do not export power_frequency, but power_start event

PATCH In review Thomas Renninger 9 participants 135 messages Oct 19, 2010 – Oct 28, 2010 1 message not in the archive

Subsystems (10)
x86 perf events Tracing Driver core +6 more subsystems
lore
Share & tools
lore
#1 lore raw

PATCH 1/3 PERF: Do not export power_frequency, but power_start event

In reply to a message not in the archive

power_frequency moved to drivers/cpufreq/cpufreq.c which has to be compiled in, no need to export it.

intel_idle can a be module though...

CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Masami Hiramatsu <[email protected]>
CC Frank Eigler <[email protected]>
CC Steven Rostedt <[email protected]>
CC Kevin Hilman <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Jean Pihet <[email protected]>
CC Pierre Tardy <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Tejun Heo <[email protected]>
CC Mathieu Desnoyers <[email protected]>
CC Arjan van de Ven <[email protected]>
CC Ingo Molnar <[email protected]>
---
 drivers/idle/intel_idle.c   |    2 --
 kernel/trace/power-traces.c |    2 +-
 2 files changed, 1 insertions(+), 3 deletions(-)
drivers/idle/intel_idle.c +0 -2
index c37ef64..21ac077 100644
@@ -201,9 +201,7 @@ static int intel_idle(struct cpuidle_device *dev, struct cpuidle_state *state)
 	kt_before = ktime_get_real();
 
 	stop_critical_timings();
-#ifndef MODULE
 	trace_power_start(POWER_CSTATE, (eax >> 4) + 1, cpu);
-#endif
 	if (!need_resched()) {
 
 		__monitor((void *)&current_thread_info()->flags, 0, 0);
kernel/trace/power-traces.c +1 -1
index a22582a..0e0497d 100644
@@ -13,5 +13,5 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/power.h>
 
-EXPORT_TRACEPOINT_SYMBOL_GPL(power_frequency);
+EXPORT_TRACEPOINT_SYMBOL_GPL(power_start);
 
Signature · 1 line
1.6.0.2
#3 lore raw

PATCH 2/3 PERF(kernel): Cleanup power events

In reply to a message not in the archive

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

and
power:power_frequency
is replaced with:
power:processor_frequency

power:machine_suspend
is newly introduced, a first implementation
comes from the ARM side, but it's easy to add these events in X86 as well if needed.

the type= field got removed from both, it was never used and the type is differed by the event type itself.

perf timechart
userspace tool gets adjusted in a separate patch.

CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Masami Hiramatsu <[email protected]>
CC Frank Eigler <[email protected]>
CC Steven Rostedt <[email protected]>
CC Kevin Hilman <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Jean Pihet <[email protected]>
CC Pierre Tardy <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Tejun Heo <[email protected]>
CC Mathieu Desnoyers <[email protected]>
CC Arjan van de Ven <[email protected]>
CC Ingo Molnar <[email protected]>
---
 arch/x86/kernel/process.c    |    5 ++-
 arch/x86/kernel/process_64.c |    1 +
 drivers/cpufreq/cpufreq.c    |    1 +
 drivers/cpuidle/cpuidle.c    |    1 +
 drivers/idle/intel_idle.c    |    1 +
 include/trace/events/power.h |   80 +++++++++++++++++++++++++++++++++++++++++-
 kernel/trace/Kconfig         |   14 +++++++
 kernel/trace/power-traces.c  |    3 ++
 8 files changed, 103 insertions(+), 3 deletions(-)
arch/x86/kernel/process.c +3 -2
index 57d1868..b6b1578 100644
@@ -374,6 +374,7 @@ void default_idle(void)
 {
 	if (hlt_use_halt()) {
 		trace_power_start(POWER_CSTATE, 1, smp_processor_id());
+		trace_processor_idle(1, smp_processor_id());
 		current_thread_info()->status &= ~TS_POLLING;
 		/*
 		 * TS_POLLING-cleared state must be visible before we
@@ -444,6 +445,7 @@ EXPORT_SYMBOL_GPL(cpu_idle_wait);
 void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
 {
 	trace_power_start(POWER_CSTATE, (ax>>4)+1, smp_processor_id());
+	trace_processor_idle((ax>>4)+1, smp_processor_id());
 	if (!need_resched()) {
 		if (cpu_has(&current_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
 			clflush((void *)&current_thread_info()->flags);
@@ -460,6 +462,7 @@ static void mwait_idle(void)
 {
 	if (!need_resched()) {
 		trace_power_start(POWER_CSTATE, 1, smp_processor_id());
+		trace_processor_idle(1, smp_processor_id());
 		if (cpu_has(&current_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
 			clflush((void *)&current_thread_info()->flags);
 
@@ -480,11 +483,9 @@ static void mwait_idle(void)
  */
 static void poll_idle(void)
 {
-	trace_power_start(POWER_CSTATE, 0, smp_processor_id());
 	local_irq_enable();
 	while (!need_resched())
 		cpu_relax();
-	trace_power_end(0);
 }
 
 /*
arch/x86/kernel/process_64.c +1 -0
index 3d9ea53..2c3254c 100644
@@ -142,6 +142,7 @@ void cpu_idle(void)
 			start_critical_timings();
 
 			trace_power_end(smp_processor_id());
+			trace_processor_idle(0, smp_processor_id());
 
 			/* In many cases the interrupt that ended idle
 			   has already called exit_idle. But some idle
drivers/cpufreq/cpufreq.c +1 -0
index 199dcb9..33bdc41 100644
@@ -355,6 +355,7 @@ void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
 		dprintk("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
 			(unsigned long)freqs->cpu);
 		trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
+		trace_processor_frequency(freqs->new, freqs->cpu);
 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
 				CPUFREQ_POSTCHANGE, freqs);
 		if (likely(policy) && likely(policy->cpu == freqs->cpu))
drivers/cpuidle/cpuidle.c +1 -0
index a507108..f79de04 100644
@@ -107,6 +107,7 @@ static void cpuidle_idle_call(void)
 	if (cpuidle_curr_governor->reflect)
 		cpuidle_curr_governor->reflect(dev);
 	trace_power_end(smp_processor_id());
+	trace_processor_idle(0, smp_processor_id());
 }
 
 /**
drivers/idle/intel_idle.c +1 -0
index 21ac077..c78e496 100644
@@ -202,6 +202,7 @@ static int intel_idle(struct cpuidle_device *dev, struct cpuidle_state *state)
 
 	stop_critical_timings();
 	trace_power_start(POWER_CSTATE, (eax >> 4) + 1, cpu);
+	trace_processor_idle((eax >> 4) + 1, smp_processor_id());
 	if (!need_resched()) {
 
 		__monitor((void *)&current_thread_info()->flags, 0, 0);
include/trace/events/power.h +79 -1

93 lines not on this page · raw message

kernel/trace/Kconfig +14 -0
index 538501c..0b5c841 100644
@@ -64,6 +64,20 @@ config EVENT_TRACING
 	select CONTEXT_SWITCH_TRACER
 	bool
 
+config EVENT_POWER_TRACING_DEPRECATED
+	depends on EVENT_TRACING
+	bool
+	help
+	  Provides old power event types:
+	  C-state/idle accounting events:
+	  power:power_start
+	  power:power_end
+	  and old cpufreq accounting event:
+	  power:power_frequency
+	  This is for userspace compatibility
+	  and will vanish after 5 kernel iterations,
+	  namely 2.6.41.
+
 config CONTEXT_SWITCH_TRACER
 	bool
 
kernel/trace/power-traces.c +3 -0
index 0e0497d..6b6da42 100644
@@ -13,5 +13,8 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/power.h>
 
+#ifdef EVENT_POWER_TRACING_DEPRECATED
 EXPORT_TRACEPOINT_SYMBOL_GPL(power_start);
+#endif
+EXPORT_TRACEPOINT_SYMBOL_GPL(processor_idle);
 
Signature · 1 line
1.6.0.2
#132 lore raw

PATCH 3/3 PERF(userspace): Adjust perf timechart to the new power events

In reply to a message not in the archive

The transition was rather smooth, only part I had to fiddle some time was the check whether a tracepoint/event is supported by the running kernel.

builtin-timechart must only pass -e power:xy events which are supported by the running kernel.
For this I added the tiny helper function:
int is_valid_tracepoint(const char *event_string)
to parse-events.[hc]
which could be more generic as an interface and support hardware/software/... events, not only tracepoints, but someone else could extend that if needed...

CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Masami Hiramatsu <[email protected]>
CC Frank Eigler <[email protected]>
CC Steven Rostedt <[email protected]>
CC Kevin Hilman <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Jean Pihet <[email protected]>
CC Pierre Tardy <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Tejun Heo <[email protected]>
CC Mathieu Desnoyers <[email protected]>
CC Arjan van de Ven <[email protected]>
CC Ingo Molnar <[email protected]>
---
 tools/perf/builtin-timechart.c |   87 ++++++++++++++++++++++++++++++++-------
 tools/perf/util/parse-events.c |   43 +++++++++++++++++++-
 tools/perf/util/parse-events.h |    1 +
 3 files changed, 114 insertions(+), 17 deletions(-)
tools/perf/builtin-timechart.c +71 -16
index 9bcc38f..1304c27 100644
@@ -32,6 +32,8 @@
 #include "util/session.h"
 #include "util/svghelper.h"
 
+#define SUPPORT_OLD_POWER_EVENTS 1
+
 static char		const *input_name = "perf.data";
 static char		const *output_name = "output.svg";
 
@@ -298,12 +300,25 @@ struct trace_entry {
 	int			lock_depth;
 };
 
-struct power_entry {
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+struct power_entry_old {
 	struct trace_entry te;
 	u64	type;
 	u64	value;
 	u64	cpu_id;
 };
+#endif
+
+struct power_processor_entry {
+	struct trace_entry te;
+	u64	state;
+	u64	cpu_id;
+};
+
+struct power_suspend_entry {
+	struct trace_entry te;
+	u64	state;
+};
 
 #define TASK_COMM_LEN 16
 struct wakeup_entry {
@@ -489,29 +504,46 @@ static int process_sample_event(event_t *event, struct perf_session *session)
 	te = (void *)data.raw_data;
 	if (session->sample_type & PERF_SAMPLE_RAW && data.raw_size > 0) {
 		char *event_str;
-		struct power_entry *pe;
-
-		pe = (void *)te;
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+		struct power_entry_old *peo;
+		peo = (void *)te;
+#endif
 
 		event_str = perf_header__find_event(te->type);
 
 		if (!event_str)
 			return 0;
 
-		if (strcmp(event_str, "power:power_start") == 0)
-			c_state_start(pe->cpu_id, data.time, pe->value);
-
-		if (strcmp(event_str, "power:power_end") == 0)
-			c_state_end(pe->cpu_id, data.time);
+		if (strcmp(event_str, "power:processor_idle") == 0) {
+			struct power_processor_entry *ppe = (void *)te;
+			if (ppe->state == 0)
+				c_state_end(ppe->cpu_id, data.time);
+			else
+				c_state_start(ppe->cpu_id, data.time,
+					      ppe->state);
+		}
 
-		if (strcmp(event_str, "power:power_frequency") == 0)
-			p_state_change(pe->cpu_id, data.time, pe->value);
+		else if (strcmp(event_str, "power:processor_frequency") == 0) {
+			struct power_processor_entry *ppe = (void *)te;
+			p_state_change(ppe->cpu_id, data.time, ppe->state);
+		}
 
-		if (strcmp(event_str, "sched:sched_wakeup") == 0)
+		else if (strcmp(event_str, "sched:sched_wakeup") == 0)
 			sched_wakeup(data.cpu, data.time, data.pid, te);
 
-		if (strcmp(event_str, "sched:sched_switch") == 0)
+		else if (strcmp(event_str, "sched:sched_switch") == 0)
 			sched_switch(data.cpu, data.time, te);
+
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+		else if (strcmp(event_str, "power:power_start") == 0)
+			c_state_start(peo->cpu_id, data.time, peo->value);
+
+		else if (strcmp(event_str, "power:power_end") == 0)
+			c_state_end(peo->cpu_id, data.time);
+
+		else if (strcmp(event_str, "power:power_frequency") == 0)
+			p_state_change(peo->cpu_id, data.time, peo->value);
+#endif
 	}
 	return 0;
 }
@@ -968,7 +1000,8 @@ static const char * const timechart_usage[] = {
 	NULL
 };
 
-static const char *record_args[] = {
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+static const char *record_old_args[] = {
 	"record",
 	"-a",
 	"-R",
@@ -980,16 +1013,38 @@ static const char *record_args[] = {
 	"-e", "sched:sched_wakeup",
 	"-e", "sched:sched_switch",
 };
+#endif
+
+static const char *record_new_args[] = {
+	"record",
+	"-a",
+	"-R",
+	"-f",
+	"-c", "1",
+	"-e", "power:processor_frequency",
+	"-e", "power:processor_idle",
+	"-e", "sched:sched_wakeup",
+	"-e", "sched:sched_switch",
+};
 
 static int __cmd_record(int argc, const char **argv)
 {
 	unsigned int rec_argc, i, j;
 	const char **rec_argv;
+	const char **record_args = record_new_args;
+	unsigned int record_elems = ARRAY_SIZE(record_new_args);
 
-	rec_argc = ARRAY_SIZE(record_args) + argc - 1;
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+	if (is_valid_tracepoint("power:power_start")) {
+		record_args = record_old_args;
+		record_elems = ARRAY_SIZE(record_old_args);
+	}
+#endif
+	
+	rec_argc = record_elems + argc - 1;
 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
 
-	for (i = 0; i < ARRAY_SIZE(record_args); i++)
+	for (i = 0; i < record_elems; i++)
 		rec_argv[i] = strdup(record_args[i]);
 
 	for (j = 1; j < (unsigned int)argc; j++, i++)
tools/perf/util/parse-events.c +42 -1

55 lines not on this page · raw message

tools/perf/util/parse-events.h +1 -0
index fc4ab3f..7ab4685 100644
@@ -29,6 +29,7 @@ extern int parse_filter(const struct option *opt, const char *str, int unset);
 #define EVENTS_HELP_MAX (128*1024)
 
 extern void print_events(void);
+extern int is_valid_tracepoint(const char *event_string);
 
 extern char debugfs_path[];
 extern int valid_debugfs_mount(const char *debugfs);
Signature · 1 line
1.6.0.2
#4 lore raw

PATCH 2/3 PERF(kernel): Cleanup power events

In reply to a message not in the archive

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

and
power:power_frequency
is replaced with:
power:processor_frequency

power:machine_suspend
is newly introduced, a first implementation
comes from the ARM side, but it's easy to add these events in X86 as well if needed.

the type= field got removed from both, it was never used and the type is differed by the event type itself.

perf timechart
userspace tool gets adjusted in a separate patch.

CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Masami Hiramatsu <[email protected]>
CC Frank Eigler <[email protected]>
CC Steven Rostedt <[email protected]>
CC Kevin Hilman <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Jean Pihet <[email protected]>
CC Pierre Tardy <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Tejun Heo <[email protected]>
CC Mathieu Desnoyers <[email protected]>
CC Arjan van de Ven <[email protected]>
CC Ingo Molnar <[email protected]>
---
 arch/x86/kernel/process.c    |    5 ++-
 arch/x86/kernel/process_64.c |    1 +
 drivers/cpufreq/cpufreq.c    |    1 +
 drivers/cpuidle/cpuidle.c    |    1 +
 drivers/idle/intel_idle.c    |    1 +
 include/trace/events/power.h |   80 +++++++++++++++++++++++++++++++++++++++++-
 kernel/trace/Kconfig         |   14 +++++++
 kernel/trace/power-traces.c  |    3 ++
 8 files changed, 103 insertions(+), 3 deletions(-)
arch/x86/kernel/process.c +3 -2
index 57d1868..b6b1578 100644
@@ -374,6 +374,7 @@ void default_idle(void)
 {
 	if (hlt_use_halt()) {
 		trace_power_start(POWER_CSTATE, 1, smp_processor_id());
+		trace_processor_idle(1, smp_processor_id());
 		current_thread_info()->status &= ~TS_POLLING;
 		/*
 		 * TS_POLLING-cleared state must be visible before we
@@ -444,6 +445,7 @@ EXPORT_SYMBOL_GPL(cpu_idle_wait);
 void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
 {
 	trace_power_start(POWER_CSTATE, (ax>>4)+1, smp_processor_id());
+	trace_processor_idle((ax>>4)+1, smp_processor_id());
 	if (!need_resched()) {
 		if (cpu_has(&current_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
 			clflush((void *)&current_thread_info()->flags);
@@ -460,6 +462,7 @@ static void mwait_idle(void)
 {
 	if (!need_resched()) {
 		trace_power_start(POWER_CSTATE, 1, smp_processor_id());
+		trace_processor_idle(1, smp_processor_id());
 		if (cpu_has(&current_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
 			clflush((void *)&current_thread_info()->flags);
 
@@ -480,11 +483,9 @@ static void mwait_idle(void)
  */
 static void poll_idle(void)
 {
-	trace_power_start(POWER_CSTATE, 0, smp_processor_id());
 	local_irq_enable();
 	while (!need_resched())
 		cpu_relax();
-	trace_power_end(0);
 }
 
 /*
arch/x86/kernel/process_64.c +1 -0
index 3d9ea53..2c3254c 100644
@@ -142,6 +142,7 @@ void cpu_idle(void)
 			start_critical_timings();
 
 			trace_power_end(smp_processor_id());
+			trace_processor_idle(0, smp_processor_id());
 
 			/* In many cases the interrupt that ended idle
 			   has already called exit_idle. But some idle
drivers/cpufreq/cpufreq.c +1 -0
index 199dcb9..33bdc41 100644
@@ -355,6 +355,7 @@ void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
 		dprintk("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
 			(unsigned long)freqs->cpu);
 		trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
+		trace_processor_frequency(freqs->new, freqs->cpu);
 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
 				CPUFREQ_POSTCHANGE, freqs);
 		if (likely(policy) && likely(policy->cpu == freqs->cpu))
drivers/cpuidle/cpuidle.c +1 -0
index a507108..f79de04 100644
@@ -107,6 +107,7 @@ static void cpuidle_idle_call(void)
 	if (cpuidle_curr_governor->reflect)
 		cpuidle_curr_governor->reflect(dev);
 	trace_power_end(smp_processor_id());
+	trace_processor_idle(0, smp_processor_id());
 }
 
 /**
drivers/idle/intel_idle.c +1 -0
index 21ac077..c78e496 100644
@@ -202,6 +202,7 @@ static int intel_idle(struct cpuidle_device *dev, struct cpuidle_state *state)
 
 	stop_critical_timings();
 	trace_power_start(POWER_CSTATE, (eax >> 4) + 1, cpu);
+	trace_processor_idle((eax >> 4) + 1, smp_processor_id());
 	if (!need_resched()) {
 
 		__monitor((void *)&current_thread_info()->flags, 0, 0);
include/trace/events/power.h +79 -1

93 lines not on this page · raw message

kernel/trace/Kconfig +14 -0
index 538501c..0b5c841 100644
@@ -64,6 +64,20 @@ config EVENT_TRACING
 	select CONTEXT_SWITCH_TRACER
 	bool
 
+config EVENT_POWER_TRACING_DEPRECATED
+	depends on EVENT_TRACING
+	bool
+	help
+	  Provides old power event types:
+	  C-state/idle accounting events:
+	  power:power_start
+	  power:power_end
+	  and old cpufreq accounting event:
+	  power:power_frequency
+	  This is for userspace compatibility
+	  and will vanish after 5 kernel iterations,
+	  namely 2.6.41.
+
 config CONTEXT_SWITCH_TRACER
 	bool
 
kernel/trace/power-traces.c +3 -0
index 0e0497d..6b6da42 100644
@@ -13,5 +13,8 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/power.h>
 
+#ifdef EVENT_POWER_TRACING_DEPRECATED
 EXPORT_TRACEPOINT_SYMBOL_GPL(power_start);
+#endif
+EXPORT_TRACEPOINT_SYMBOL_GPL(processor_idle);
 
Signature · 1 line
1.6.0.2
#133 lore raw

PATCH 3/3 PERF(userspace): Adjust perf timechart to the new power events

In reply to a message not in the archive

The transition was rather smooth, only part I had to fiddle some time was the check whether a tracepoint/event is supported by the running kernel.

builtin-timechart must only pass -e power:xy events which are supported by the running kernel.
For this I added the tiny helper function:
int is_valid_tracepoint(const char *event_string)
to parse-events.[hc]
which could be more generic as an interface and support hardware/software/... events, not only tracepoints, but someone else could extend that if needed...

CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Masami Hiramatsu <[email protected]>
CC Frank Eigler <[email protected]>
CC Steven Rostedt <[email protected]>
CC Kevin Hilman <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Jean Pihet <[email protected]>
CC Pierre Tardy <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Tejun Heo <[email protected]>
CC Mathieu Desnoyers <[email protected]>
CC Arjan van de Ven <[email protected]>
CC Ingo Molnar <[email protected]>
---
 tools/perf/builtin-timechart.c |   87 ++++++++++++++++++++++++++++++++-------
 tools/perf/util/parse-events.c |   43 +++++++++++++++++++-
 tools/perf/util/parse-events.h |    1 +
 3 files changed, 114 insertions(+), 17 deletions(-)
tools/perf/builtin-timechart.c +71 -16
index 9bcc38f..1304c27 100644
@@ -32,6 +32,8 @@
 #include "util/session.h"
 #include "util/svghelper.h"
 
+#define SUPPORT_OLD_POWER_EVENTS 1
+
 static char		const *input_name = "perf.data";
 static char		const *output_name = "output.svg";
 
@@ -298,12 +300,25 @@ struct trace_entry {
 	int			lock_depth;
 };
 
-struct power_entry {
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+struct power_entry_old {
 	struct trace_entry te;
 	u64	type;
 	u64	value;
 	u64	cpu_id;
 };
+#endif
+
+struct power_processor_entry {
+	struct trace_entry te;
+	u64	state;
+	u64	cpu_id;
+};
+
+struct power_suspend_entry {
+	struct trace_entry te;
+	u64	state;
+};
 
 #define TASK_COMM_LEN 16
 struct wakeup_entry {
@@ -489,29 +504,46 @@ static int process_sample_event(event_t *event, struct perf_session *session)
 	te = (void *)data.raw_data;
 	if (session->sample_type & PERF_SAMPLE_RAW && data.raw_size > 0) {
 		char *event_str;
-		struct power_entry *pe;
-
-		pe = (void *)te;
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+		struct power_entry_old *peo;
+		peo = (void *)te;
+#endif
 
 		event_str = perf_header__find_event(te->type);
 
 		if (!event_str)
 			return 0;
 
-		if (strcmp(event_str, "power:power_start") == 0)
-			c_state_start(pe->cpu_id, data.time, pe->value);
-
-		if (strcmp(event_str, "power:power_end") == 0)
-			c_state_end(pe->cpu_id, data.time);
+		if (strcmp(event_str, "power:processor_idle") == 0) {
+			struct power_processor_entry *ppe = (void *)te;
+			if (ppe->state == 0)
+				c_state_end(ppe->cpu_id, data.time);
+			else
+				c_state_start(ppe->cpu_id, data.time,
+					      ppe->state);
+		}
 
-		if (strcmp(event_str, "power:power_frequency") == 0)
-			p_state_change(pe->cpu_id, data.time, pe->value);
+		else if (strcmp(event_str, "power:processor_frequency") == 0) {
+			struct power_processor_entry *ppe = (void *)te;
+			p_state_change(ppe->cpu_id, data.time, ppe->state);
+		}
 
-		if (strcmp(event_str, "sched:sched_wakeup") == 0)
+		else if (strcmp(event_str, "sched:sched_wakeup") == 0)
 			sched_wakeup(data.cpu, data.time, data.pid, te);
 
-		if (strcmp(event_str, "sched:sched_switch") == 0)
+		else if (strcmp(event_str, "sched:sched_switch") == 0)
 			sched_switch(data.cpu, data.time, te);
+
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+		else if (strcmp(event_str, "power:power_start") == 0)
+			c_state_start(peo->cpu_id, data.time, peo->value);
+
+		else if (strcmp(event_str, "power:power_end") == 0)
+			c_state_end(peo->cpu_id, data.time);
+
+		else if (strcmp(event_str, "power:power_frequency") == 0)
+			p_state_change(peo->cpu_id, data.time, peo->value);
+#endif
 	}
 	return 0;
 }
@@ -968,7 +1000,8 @@ static const char * const timechart_usage[] = {
 	NULL
 };
 
-static const char *record_args[] = {
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+static const char *record_old_args[] = {
 	"record",
 	"-a",
 	"-R",
@@ -980,16 +1013,38 @@ static const char *record_args[] = {
 	"-e", "sched:sched_wakeup",
 	"-e", "sched:sched_switch",
 };
+#endif
+
+static const char *record_new_args[] = {
+	"record",
+	"-a",
+	"-R",
+	"-f",
+	"-c", "1",
+	"-e", "power:processor_frequency",
+	"-e", "power:processor_idle",
+	"-e", "sched:sched_wakeup",
+	"-e", "sched:sched_switch",
+};
 
 static int __cmd_record(int argc, const char **argv)
 {
 	unsigned int rec_argc, i, j;
 	const char **rec_argv;
+	const char **record_args = record_new_args;
+	unsigned int record_elems = ARRAY_SIZE(record_new_args);
 
-	rec_argc = ARRAY_SIZE(record_args) + argc - 1;
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+	if (is_valid_tracepoint("power:power_start")) {
+		record_args = record_old_args;
+		record_elems = ARRAY_SIZE(record_old_args);
+	}
+#endif
+	
+	rec_argc = record_elems + argc - 1;
 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
 
-	for (i = 0; i < ARRAY_SIZE(record_args); i++)
+	for (i = 0; i < record_elems; i++)
 		rec_argv[i] = strdup(record_args[i]);
 
 	for (j = 1; j < (unsigned int)argc; j++, i++)
tools/perf/util/parse-events.c +42 -1

55 lines not on this page · raw message

tools/perf/util/parse-events.h +1 -0
index fc4ab3f..7ab4685 100644
@@ -29,6 +29,7 @@ extern int parse_filter(const struct option *opt, const char *str, int unset);
 #define EVENTS_HELP_MAX (128*1024)
 
 extern void print_events(void);
+extern int is_valid_tracepoint(const char *event_string);
 
 extern char debugfs_path[];
 extern int valid_debugfs_mount(const char *debugfs);
Signature · 1 line
1.6.0.2
#2 lore raw

PATCH 1/3 PERF: Do not export power_frequency, but power_start event

In reply to a message not in the archive

power_frequency moved to drivers/cpufreq/cpufreq.c which has to be compiled in, no need to export it.

intel_idle can a be module though...

CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Masami Hiramatsu <[email protected]>
CC Frank Eigler <[email protected]>
CC Steven Rostedt <[email protected]>
CC Kevin Hilman <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Jean Pihet <[email protected]>
CC Pierre Tardy <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Tejun Heo <[email protected]>
CC Mathieu Desnoyers <[email protected]>
CC Arjan van de Ven <[email protected]>
CC Ingo Molnar <[email protected]>
---
 drivers/idle/intel_idle.c   |    2 --
 kernel/trace/power-traces.c |    2 +-
 2 files changed, 1 insertions(+), 3 deletions(-)
drivers/idle/intel_idle.c +0 -2
index c37ef64..21ac077 100644
@@ -201,9 +201,7 @@ static int intel_idle(struct cpuidle_device *dev, struct cpuidle_state *state)
 	kt_before = ktime_get_real();
 
 	stop_critical_timings();
-#ifndef MODULE
 	trace_power_start(POWER_CSTATE, (eax >> 4) + 1, cpu);
-#endif
 	if (!need_resched()) {
 
 		__monitor((void *)&current_thread_info()->flags, 0, 0);
kernel/trace/power-traces.c +1 -1
index a22582a..0e0497d 100644
@@ -13,5 +13,5 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/power.h>
 
-EXPORT_TRACEPOINT_SYMBOL_GPL(power_frequency);
+EXPORT_TRACEPOINT_SYMBOL_GPL(power_start);
 
Signature · 1 line
1.6.0.2
#5 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger

arch/x86/kernel/process.c static void mwait_idle(void) · line 489

Quoted 8 lines · Thomas Renninger
  static void poll_idle(void)
  {
arch/x86/kernel/process.c +0 -2
-	trace_power_start(POWER_CSTATE, 0, smp_processor_id());
  	local_irq_enable();
  	while (!need_resched())
  		cpu_relax();
-	trace_power_end(0);
  }

why did you remove the idle tracepoints from this one ???

#6 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger

arch/x86/kernel/process.c static void mwait_idle(void) · line 489

Quoted 8 lines · Thomas Renninger
  static void poll_idle(void)
  {
arch/x86/kernel/process.c +0 -2
-	trace_power_start(POWER_CSTATE, 0, smp_processor_id());
  	local_irq_enable();
  	while (!need_resched())
  		cpu_relax();
-	trace_power_end(0);
  }

why did you remove the idle tracepoints from this one ???

#23 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger
Quoted 6 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

Quoted 6 lines

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

I think you need two trace points for this
one to enter idle
one to exit

because using magic encoding games to encode "exit"is a mistake; as can be seen in this patch.
You're currently trying to use "0" to signal "end of idle", but "0" is also a valid idle state (namely that of polling)

#24 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger
Quoted 6 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

Quoted 6 lines

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

I think you need two trace points for this
one to enter idle
one to exit

because using magic encoding games to encode "exit"is a mistake; as can be seen in this patch. You're currently trying to use "0" to signal "end of idle", but "0" is also a valid idle state (namely that of polling)

#7 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Arjan van de Ven
Quoted 11 lines · On Monday 25 October 2010 08:54:34 Arjan van de Ven wrote:

arch/x86/kernel/process.c static void mwait_idle(void) · line 489

Quoted 8 lines · Thomas Renninger
  static void poll_idle(void)
  {
arch/x86/kernel/process.c +0 -2
-	trace_power_start(POWER_CSTATE, 0, smp_processor_id());
  	local_irq_enable();
  	while (!need_resched())
  		cpu_relax();
-	trace_power_end(0);
  }

why did you remove the idle tracepoints from this one ???

Because no idle/sleep state is entered here.
State 0 does not exist or say, it means the machine is not idle. The new event uses idle state 0 spec conform as "exit sleep state".

If this should still be trackable some kind of dummy sleep state:
#define IDLE_BUSY_LOOP 0xFE
(or similar) must get defined and passed like this:

trace_processor_idle(IDLE_BUSY_LOOP, smp_processor_id());
    cpu_relax()

trace_processor_idle(0, smp_processor_id());

I could imagine this is somewhat worth it to compare idle results to "no idle state at all" is used.
But nobody should ever use idle=poll, comparing deep sleep states with C1 with (idle=halt) should be sufficient?

Thomas

#22 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Arjan van de Ven
Quoted 11 lines · On Monday 25 October 2010 08:54:34 Arjan van de Ven wrote:

arch/x86/kernel/process.c static void mwait_idle(void) · line 489

Quoted 8 lines · Thomas Renninger
  static void poll_idle(void)
  {
arch/x86/kernel/process.c +0 -2
-	trace_power_start(POWER_CSTATE, 0, smp_processor_id());
  	local_irq_enable();
  	while (!need_resched())
  		cpu_relax();
-	trace_power_end(0);
  }

why did you remove the idle tracepoints from this one ???

Because no idle/sleep state is entered here.
State 0 does not exist or say, it means the machine is not idle. The new event uses idle state 0 spec conform as "exit sleep state".

If this should still be trackable some kind of dummy sleep state:
#define IDLE_BUSY_LOOP 0xFE
(or similar) must get defined and passed like this:

trace_processor_idle(IDLE_BUSY_LOOP, smp_processor_id());
    cpu_relax()

trace_processor_idle(0, smp_processor_id());

I could imagine this is somewhat worth it to compare idle results to "no idle state at all" is used.
But nobody should ever use idle=poll, comparing deep sleep states with C1 with (idle=halt) should be sufficient?

Thomas

#25 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger
Quoted 11 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Well, most power saving hw models (and the code implementing them) have this kind of model:

enter power saving mode X
exit power saving mode

Where X is some sort of 'power saving deepness' attribute, right?

Ingo

#44 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger
Quoted 11 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Well, most power saving hw models (and the code implementing them) have this kind of model:

enter power saving mode X
exit power saving mode

Where X is some sort of 'power saving deepness' attribute, right?

Ingo

#26 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Ingo Molnar
Quoted 15 lines · On Monday 25 October 2010 12:04:28 Ingo Molnar wrote:
Quoted 11 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Quoted 7 lines

Well, most power saving hw models (and the code implementing them) have this kind of model:

enter power saving mode X
exit power saving mode

Where X is some sort of 'power saving deepness' attribute, right?

Sure.
But ACPI and afaik this model got picked up for PCI and other (sub-)archs as well, defines state 0 as the non-power saving mode.
Same as done here with machine suspend state (S0 is back from suspend) and this model should get picked up when device sleep states get tracked at some time.
It's consistent and applies to some well known specifications.

Also tracking processor_idle_{start,end} as a separate event makes no sense and there is no need to introduce:
processor_idle_start/processor_idle_end machine_suspend_start/machine_suspend_end device_power_mode_start/device_power_mode_end events.
Using state 0 as "exit/end", is much nicer for kernel/ userspace implementations/code and the user.

Thomas

#43 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Ingo Molnar
Quoted 15 lines · On Monday 25 October 2010 12:04:28 Ingo Molnar wrote:
Quoted 11 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Quoted 7 lines

Well, most power saving hw models (and the code implementing them) have this kind of model:

enter power saving mode X
exit power saving mode

Where X is some sort of 'power saving deepness' attribute, right?

Sure.
But ACPI and afaik this model got picked up for PCI and other (sub-)archs as well, defines state 0 as the non-power saving mode.
Same as done here with machine suspend state (S0 is back from suspend) and this model should get picked up when device sleep states get tracked at some time.
It's consistent and applies to some well known specifications.

Also tracking processor_idle_{start,end} as a separate event makes no sense and there is no need to introduce:
processor_idle_start/processor_idle_end machine_suspend_start/machine_suspend_end device_power_mode_start/device_power_mode_end events.
Using state 0 as "exit/end", is much nicer for kernel/ userspace implementations/code and the user.

Thomas

#27 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger
Quoted 19 lines · Thomas Renninger
Quoted 18 lines · On Monday 25 October 2010 12:04:28 Ingo Molnar wrote:
Quoted 11 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Well, most power saving hw models (and the code implementing them) have this kind of model:

Quoted 6 lines
Quoted 4 lines

enter power saving mode X
exit power saving mode

Where X is some sort of 'power saving deepness' attribute, right?

Sure.

Which is is the 'saner' model?

Quoted 2 lines

But ACPI and afaik this model got picked up for PCI and other (sub-)archs as well, defines state 0 as the non-power saving mode.

But the actual code does not actually deal with any 'state 0', does it? It enters an idle function and then exits it, right?

'power state' might be what is used for devices - but even there, we have:

- enter power state X
- exit power state

right?

Quoted 5 lines

Same as done here with machine suspend state (S0 is back from suspend) and this model should get picked up when device sleep states get tracked at some time.

It's consistent and applies to some well known specifications.

What we want it to be is for it to be the nicest, most understandable, most logical model - not one matching random hardware specifications.

( Hardware specifications only matter in so far that it should be possible to
express all the known hardware state transitions via these events efficiently. )

Quoted 4 lines

Also tracking processor_idle_{start,end} as a separate event makes no sense and there is no need to introduce: processor_idle_start/processor_idle_end machine_suspend_start/machine_suspend_end device_power_mode_start/device_power_mode_end events.

What do you mean by "makes no sense"?

Are they superfluous? Inefficient? Illogical?

Quoted 2 lines

Using state 0 as "exit/end", is much nicer for kernel/ userspace implementations/code and the user.

By that argument we should not have separate fork() and exit() syscalls either, but a set_process_state(1) and set_process_state(0) interface?

Thanks,

Ingo

#38 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger
Quoted 19 lines · Thomas Renninger
Quoted 18 lines · On Monday 25 October 2010 12:04:28 Ingo Molnar wrote:
Quoted 11 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Well, most power saving hw models (and the code implementing them) have this kind of model:

Quoted 6 lines
Quoted 4 lines

enter power saving mode X
exit power saving mode

Where X is some sort of 'power saving deepness' attribute, right?

Sure.

Which is is the 'saner' model?

Quoted 2 lines

But ACPI and afaik this model got picked up for PCI and other (sub-)archs as well, defines state 0 as the non-power saving mode.

But the actual code does not actually deal with any 'state 0', does it? It enters an idle function and then exits it, right?

'power state' might be what is used for devices - but even there, we have:

- enter power state X
- exit power state

right?

Quoted 5 lines

Same as done here with machine suspend state (S0 is back from suspend) and this model should get picked up when device sleep states get tracked at some time.

It's consistent and applies to some well known specifications.

What we want it to be is for it to be the nicest, most understandable, most logical model - not one matching random hardware specifications.

( Hardware specifications only matter in so far that it should be possible to
express all the known hardware state transitions via these events efficiently. )

Quoted 4 lines

Also tracking processor_idle_{start,end} as a separate event makes no sense and there is no need to introduce: processor_idle_start/processor_idle_end machine_suspend_start/machine_suspend_end device_power_mode_start/device_power_mode_end events.

What do you mean by "makes no sense"?

Are they superfluous? Inefficient? Illogical?

Quoted 2 lines

Using state 0 as "exit/end", is much nicer for kernel/ userspace implementations/code and the user.

By that argument we should not have separate fork() and exit() syscalls either, but a set_process_state(1) and set_process_state(0) interface?

Thanks,

Ingo

#28 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Ingo Molnar
Quoted 27 lines · On Monday 25 October 2010 13:55:25 Ingo Molnar wrote:
Quoted 24 lines · Thomas Renninger
Quoted 22 lines · On Monday 25 October 2010 12:04:28 Ingo Molnar wrote:
Quoted 11 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Well, most power saving hw models (and the code implementing them) have this kind of model:

enter power saving mode X
exit power saving mode

Where X is some sort of 'power saving deepness' attribute, right?

Quoted 8 lines
Quoted 1 line

Sure.

Which is is the 'saner' model?

Quoted 2 lines

But ACPI and afaik this model got picked up for PCI and other (sub-)archs as well, defines state 0 as the non-power saving mode.

But the actual code does not actually deal with any 'state 0', does it?

It does. Not being idle is tracked by cpuidle driver as state 0 (arch independent):
/sys/devices/system/cpu/cpu0/cpuidle/state0/
halt/C1 on X86 is:
/sys/devices/system/cpu/cpu0/cpuidle/state1/
...

Quoted 7 lines

It enters an idle function and then exits it, right?
'power state' might be what is used for devices - but even there, we have:

- enter power state X
- exit power state

right?

That is not true for PCI, probably others as well. There you have D0 (being the maximum powered state) up to D3. Same for PCI Bus Power States (B0, B1, B2, and B3).

Look at drivers/pci/pci.c:pci_raw_set_power_state() To "exit" a power state you call:
pci_raw_set_power_state(dev, PCI_D0);

Same for suspend. "Exit" suspend is:
#define PM_SUSPEND_ON ((__force suspend_state_t) 0)
so on resume we enter suspend_state_t 0.

Quoted 12 lines
Quoted 5 lines

Same as done here with machine suspend state (S0 is back from suspend) and this model should get picked up when device sleep states get tracked at some time.

It's consistent and applies to some well known specifications.

What we want it to be is for it to be the nicest, most understandable, most logical model - not one matching random hardware specifications.

( Hardware specifications only matter in so far that it should be possible to
express all the known hardware state transitions via these events efficiently. )

Quoted 8 lines
Quoted 4 lines

Also tracking processor_idle_{start,end} as a separate event makes no sense and there is no need to introduce: processor_idle_start/processor_idle_end machine_suspend_start/machine_suspend_end device_power_mode_start/device_power_mode_end events.

What do you mean by "makes no sense"?

Are they superfluous?

Yes, you do not need two different events to track one thing.

Quoted 1 line

Illogical?

Yes, A user who wants to enable processor idle tracking does want to enable it via:
echo power:processor_idle >/sys/kernel/debug/tracing/events/enable what do you intend to track with a:
power:power_start
event?

Thomas

#33 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Ingo Molnar
Quoted 27 lines · On Monday 25 October 2010 13:55:25 Ingo Molnar wrote:
Quoted 24 lines · Thomas Renninger
Quoted 22 lines · On Monday 25 October 2010 12:04:28 Ingo Molnar wrote:
Quoted 11 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Well, most power saving hw models (and the code implementing them) have this kind of model:

enter power saving mode X
exit power saving mode

Where X is some sort of 'power saving deepness' attribute, right?

Quoted 8 lines
Quoted 1 line

Sure.

Which is is the 'saner' model?

Quoted 2 lines

But ACPI and afaik this model got picked up for PCI and other (sub-)archs as well, defines state 0 as the non-power saving mode.

But the actual code does not actually deal with any 'state 0', does it?

It does. Not being idle is tracked by cpuidle driver as state 0 (arch independent):
/sys/devices/system/cpu/cpu0/cpuidle/state0/
halt/C1 on X86 is:
/sys/devices/system/cpu/cpu0/cpuidle/state1/
...

Quoted 7 lines

It enters an idle function and then exits it, right?
'power state' might be what is used for devices - but even there, we have:

- enter power state X
- exit power state

right?

That is not true for PCI, probably others as well. There you have D0 (being the maximum powered state) up to D3. Same for PCI Bus Power States (B0, B1, B2, and B3).

Look at drivers/pci/pci.c:pci_raw_set_power_state() To "exit" a power state you call:
pci_raw_set_power_state(dev, PCI_D0);

Same for suspend. "Exit" suspend is:
#define PM_SUSPEND_ON ((__force suspend_state_t) 0)
so on resume we enter suspend_state_t 0.

Quoted 12 lines
Quoted 5 lines

Same as done here with machine suspend state (S0 is back from suspend) and this model should get picked up when device sleep states get tracked at some time.

It's consistent and applies to some well known specifications.

What we want it to be is for it to be the nicest, most understandable, most logical model - not one matching random hardware specifications.

( Hardware specifications only matter in so far that it should be possible to
express all the known hardware state transitions via these events efficiently. )

Quoted 8 lines
Quoted 4 lines

Also tracking processor_idle_{start,end} as a separate event makes no sense and there is no need to introduce: processor_idle_start/processor_idle_end machine_suspend_start/machine_suspend_end device_power_mode_start/device_power_mode_end events.

What do you mean by "makes no sense"?

Are they superfluous?

Yes, you do not need two different events to track one thing.

Quoted 1 line

Illogical?

Yes, A user who wants to enable processor idle tracking does want to enable it via:
echo power:processor_idle >/sys/kernel/debug/tracing/events/enable what do you intend to track with a:
power:power_start
event?

Thomas

#34 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Ingo Molnar
Quoted 56 lines · * Ingo Molnar ([email protected]) wrote:
Quoted 19 lines · Thomas Renninger
Quoted 18 lines · On Monday 25 October 2010 12:04:28 Ingo Molnar wrote:
Quoted 11 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Well, most power saving hw models (and the code implementing them) have this kind of model:

Quoted 6 lines
Quoted 4 lines

enter power saving mode X
exit power saving mode

Where X is some sort of 'power saving deepness' attribute, right?

Sure.

Which is is the 'saner' model?

Quoted 2 lines

But ACPI and afaik this model got picked up for PCI and other (sub-)archs as well, defines state 0 as the non-power saving mode.

But the actual code does not actually deal with any 'state 0', does it? It enters an idle function and then exits it, right?

'power state' might be what is used for devices - but even there, we have:

- enter power state X
- exit power state

right?

Quoted 5 lines

Same as done here with machine suspend state (S0 is back from suspend) and this model should get picked up when device sleep states get tracked at some time.

It's consistent and applies to some well known specifications.

What we want it to be is for it to be the nicest, most understandable, most logical model - not one matching random hardware specifications.

( Hardware specifications only matter in so far that it should be possible to
express all the known hardware state transitions via these events efficiently. )

Quoted 8 lines
Quoted 4 lines

Also tracking processor_idle_{start,end} as a separate event makes no sense and there is no need to introduce: processor_idle_start/processor_idle_end machine_suspend_start/machine_suspend_end device_power_mode_start/device_power_mode_end events.

What do you mean by "makes no sense"?

Are they superfluous? Inefficient? Illogical?

I think it would require deep understanding of specific power modes of each architecture to split into this topology. On the bright side, it would bring clear understanding of which HW resource is being put to sleep, which would make automated analysis much easier to do. But maybe it's too much pain compared to the benefit. The related question is also: where is it best to put this logic ? In the kernel code ? In per-arch TRACE_EVENT() handlers or in external trace analysis plugins ?

Quoted 6 lines
Quoted 3 lines

Using state 0 as "exit/end", is much nicer for kernel/ userspace implementations/code and the user.

By that argument we should not have separate fork() and exit() syscalls either, but a set_process_state(1) and set_process_state(0) interface?

I'm by no mean expert on power saving hardware specs, but if it is possible for hardware to switch between two power saving states without passing through power state 0, then using a "set state" rather than an enter/exit would be more appropriate; even if we go for a scheme introducing

processor_idle_start/processor_idle_end,
machine_suspend_start/machine_suspend_end,
device_power_mode_start/device_power_mode_end.

I must defer to you guys to figure out if some hardware actually do that for either of CPU idle, suspend or device power modes.

Thanks,

Mathieu

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#37 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Ingo Molnar
Quoted 56 lines · * Ingo Molnar ([email protected]) wrote:
Quoted 19 lines · Thomas Renninger
Quoted 18 lines · On Monday 25 October 2010 12:04:28 Ingo Molnar wrote:
Quoted 11 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Well, most power saving hw models (and the code implementing them) have this kind of model:

Quoted 6 lines
Quoted 4 lines

enter power saving mode X
exit power saving mode

Where X is some sort of 'power saving deepness' attribute, right?

Sure.

Which is is the 'saner' model?

Quoted 2 lines

But ACPI and afaik this model got picked up for PCI and other (sub-)archs as well, defines state 0 as the non-power saving mode.

But the actual code does not actually deal with any 'state 0', does it? It enters an idle function and then exits it, right?

'power state' might be what is used for devices - but even there, we have:

- enter power state X
- exit power state

right?

Quoted 5 lines

Same as done here with machine suspend state (S0 is back from suspend) and this model should get picked up when device sleep states get tracked at some time.

It's consistent and applies to some well known specifications.

What we want it to be is for it to be the nicest, most understandable, most logical model - not one matching random hardware specifications.

( Hardware specifications only matter in so far that it should be possible to
express all the known hardware state transitions via these events efficiently. )

Quoted 8 lines
Quoted 4 lines

Also tracking processor_idle_{start,end} as a separate event makes no sense and there is no need to introduce: processor_idle_start/processor_idle_end machine_suspend_start/machine_suspend_end device_power_mode_start/device_power_mode_end events.

What do you mean by "makes no sense"?

Are they superfluous? Inefficient? Illogical?

I think it would require deep understanding of specific power modes of each architecture to split into this topology. On the bright side, it would bring clear understanding of which HW resource is being put to sleep, which would make automated analysis much easier to do. But maybe it's too much pain compared to the benefit. The related question is also: where is it best to put this logic ? In the kernel code ? In per-arch TRACE_EVENT() handlers or in external trace analysis plugins ?

Quoted 6 lines
Quoted 3 lines

Using state 0 as "exit/end", is much nicer for kernel/ userspace implementations/code and the user.

By that argument we should not have separate fork() and exit() syscalls either, but a set_process_state(1) and set_process_state(0) interface?

I'm by no mean expert on power saving hardware specs, but if it is possible for hardware to switch between two power saving states without passing through power state 0, then using a "set state" rather than an enter/exit would be more appropriate; even if we go for a scheme introducing

processor_idle_start/processor_idle_end,
machine_suspend_start/machine_suspend_end,
device_power_mode_start/device_power_mode_end.

I must defer to you guys to figure out if some hardware actually do that for either of CPU idle, suspend or device power modes.

Thanks,

Mathieu

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#8 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger
Quoted 18 lines · Thomas Renninger
Quoted 10 lines · On Monday 25 October 2010 08:54:34 Arjan van de Ven wrote:

arch/x86/kernel/process.c static void mwait_idle(void) · line 489

Quoted 8 lines · Thomas Renninger
   static void poll_idle(void)
   {
arch/x86/kernel/process.c +0 -2
-	trace_power_start(POWER_CSTATE, 0, smp_processor_id());
   	local_irq_enable();
   	while (!need_resched())
   		cpu_relax();
-	trace_power_end(0);
   }

why did you remove the idle tracepoints from this one ???

Because no idle/sleep state is entered here.
State 0 does not exist or say, it means the machine is not idle. The new event uses idle state 0 spec conform as "exit sleep state".

If this should still be trackable some kind of dummy sleep state:
#define IDLE_BUSY_LOOP 0xFE
(or similar) must get defined and passed like this:

Quoted 8 lines
trace_processor_idle(IDLE_BUSY_LOOP, smp_processor_id());
     cpu_relax()

trace_processor_idle(0, smp_processor_id());

I could imagine this is somewhat worth it to compare idle results to "no idle state at all" is used.
But nobody should ever use idle=poll, comparing deep sleep states with C1 with (idle=halt) should be sufficient?

this is not idle=poll on the command line only.
this also gets used normally, in two cases
1) during real time operations, for some short periods of time (think wallstreet trading)
2) by the menu governor when the next event is less than a few microseconds away, so short that even C1 is too much

I know that your new API tries to use "0" as exit, but 0 is already taken (in all power terminology at least on x86 it is) for this.

why isn't your "exit" a special define?

also, if you look at many other similar perf events, they ever separate entry/exit points:

process/do_process.cpp:
perf_events->add_event("irq:irq_handler_entry");
process/do_process.cpp:
perf_events->add_event("irq:irq_handler_exit");

process/do_process.cpp:         perf_events->add_event("irq:softirq_entry");
process/do_process.cpp:         perf_events->add_event("irq:softirq_exit");

process/do_process.cpp:
perf_events->add_event("timer:timer_expire_entry"); process/do_process.cpp:
perf_events->add_event("timer:timer_expire_exit"); process/do_process.cpp:
perf_events->add_event("timer:hrtimer_expire_entry"); process/do_process.cpp:
perf_events->add_event("timer:hrtimer_expire_exit");

process/do_process.cpp:         perf_events->add_event("power:power_start");
process/do_process.cpp:         perf_events->add_event("power:power_end");

process/do_process.cpp:
perf_events->add_event("workqueue:workqueue_execute_start"); process/do_process.cpp:
perf_events->add_event("workqueue:workqueue_execute_end");

so there is already an API consistency precedent
(and frankly, trying to multiplex in "exit" via a magic value is asking for trouble API wise)

#9 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger
Quoted 18 lines · Thomas Renninger
Quoted 10 lines · On Monday 25 October 2010 08:54:34 Arjan van de Ven wrote:

arch/x86/kernel/process.c static void mwait_idle(void) · line 489

Quoted 8 lines · Thomas Renninger
   static void poll_idle(void)
   {
arch/x86/kernel/process.c +0 -2
-	trace_power_start(POWER_CSTATE, 0, smp_processor_id());
   	local_irq_enable();
   	while (!need_resched())
   		cpu_relax();
-	trace_power_end(0);
   }

why did you remove the idle tracepoints from this one ???

Because no idle/sleep state is entered here.
State 0 does not exist or say, it means the machine is not idle. The new event uses idle state 0 spec conform as "exit sleep state".

If this should still be trackable some kind of dummy sleep state:
#define IDLE_BUSY_LOOP 0xFE
(or similar) must get defined and passed like this:

Quoted 8 lines
trace_processor_idle(IDLE_BUSY_LOOP, smp_processor_id());
     cpu_relax()

trace_processor_idle(0, smp_processor_id());

I could imagine this is somewhat worth it to compare idle results to "no idle state at all" is used.
But nobody should ever use idle=poll, comparing deep sleep states with C1 with (idle=halt) should be sufficient?

this is not idle=poll on the command line only.
this also gets used normally, in two cases
1) during real time operations, for some short periods of time
(think wallstreet trading)
2) by the menu governor when the next event is less than a few microseconds away, so short that even C1 is too much

I know that your new API tries to use "0" as exit, but 0 is already taken (in all power terminology at least on x86 it is) for this.

why isn't your "exit" a special define?

also, if you look at many other similar perf events, they ever separate entry/exit points:

process/do_process.cpp:         perf_events->add_event("irq:irq_handler_entry");
process/do_process.cpp:         perf_events->add_event("irq:irq_handler_exit");
process/do_process.cpp:         perf_events->add_event("irq:softirq_entry");
process/do_process.cpp:         perf_events->add_event("irq:softirq_exit");
process/do_process.cpp:         perf_events->add_event("timer:timer_expire_entry");
process/do_process.cpp:         perf_events->add_event("timer:timer_expire_exit");
process/do_process.cpp:         perf_events->add_event("timer:hrtimer_expire_entry");
process/do_process.cpp:         perf_events->add_event("timer:hrtimer_expire_exit");
process/do_process.cpp:         perf_events->add_event("power:power_start");
process/do_process.cpp:         perf_events->add_event("power:power_end");
process/do_process.cpp:         perf_events->add_event("workqueue:workqueue_execute_start");
process/do_process.cpp:         perf_events->add_event("workqueue:workqueue_execute_end");

so there is already an API consistency precedent
(and frankly, trying to multiplex in "exit" via a magic value is asking for trouble API wise)

#39 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger
Quoted 17 lines · Thomas Renninger
Quoted 16 lines · On Monday 25 October 2010 12:04:28 Ingo Molnar wrote:
Quoted 11 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Well, most power saving hw models (and the code implementing them) have this kind of model:

Quoted 7 lines
Quoted 4 lines

enter power saving mode X
exit power saving mode

Where X is some sort of 'power saving deepness' attribute, right?

Sure.
But ACPI and afaik this model got picked up for PCI and other (sub-)archs as well, defines state 0 as the non-power saving mode.

correct ,... "C0" is not power efficient... but it's still a valid OS idle state! Also tracking processor_idle_{start,end} as a separate event!

same for "S0"... S0 as standby state is still valid... sure it doesn't save you much power... but that does not mean it's not valid. (as indication, the Intel Moorestown platform, which is currently in production and available to OEMs, has such a S0 standby state)

Quoted 7 lines

makes no sense and there is no need to introduce:
processor_idle_start/processor_idle_end machine_suspend_start/machine_suspend_end device_power_mode_start/device_power_mode_end events.
Using state 0 as "exit/end", is much nicer for kernel/ userspace implementations/code and the user.

actually no; having written a few of these in userspace so far, having a separate end event is easier to deal with; the actions you take on entry and exit are complete separate code paths.

#42 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger
Quoted 17 lines · Thomas Renninger
Quoted 16 lines · On Monday 25 October 2010 12:04:28 Ingo Molnar wrote:
Quoted 11 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Well, most power saving hw models (and the code implementing them) have this kind of model:

Quoted 7 lines
Quoted 4 lines

enter power saving mode X
exit power saving mode

Where X is some sort of 'power saving deepness' attribute, right?

Sure.
But ACPI and afaik this model got picked up for PCI and other (sub-)archs as well, defines state 0 as the non-power saving mode.

correct ,... "C0" is not power efficient... but it's still a valid OS idle state!
Also tracking processor_idle_{start,end} as a separate event!

same for "S0"... S0 as standby state is still valid... sure it doesn't save you much power... but that does not mean it's not valid. (as indication, the Intel Moorestown platform, which is currently in production and available to OEMs, has such a S0 standby state)

Quoted 7 lines

makes no sense and there is no need to introduce:
processor_idle_start/processor_idle_end machine_suspend_start/machine_suspend_end device_power_mode_start/device_power_mode_end events.
Using state 0 as "exit/end", is much nicer for kernel/ userspace implementations/code and the user.

actually no; having written a few of these in userspace so far, having a separate end event is easier to deal with;
the actions you take on entry and exit are complete separate code paths.

#29 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger
Quoted 7 lines · Thomas Renninger
Quoted 1 line

But the actual code does not actually deal with any 'state 0', does it?

It does. Not being idle is tracked by cpuidle driver as state 0 (arch independent):
/sys/devices/system/cpu/cpu0/cpuidle/state0/
halt/C1 on X86 is:
/sys/devices/system/cpu/cpu0/cpuidle/state1/
...

state0 is still OS idle!

the API is just weird for this, from a userspace perspective

if the kernel picks this state 0 for the idle handler, the userspace app gets two events

one for going to state 0 to enter the idle state
one for going to state 0 to exit idle

but they're the exact same event in your API.

rather unpleasant from a userspace program perspective....
now I need to start tracking even more state on top in powertop to be able to make a guess at which of the two meanings a state 0 entry has.

#32 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger
Quoted 7 lines · Thomas Renninger
Quoted 1 line

But the actual code does not actually deal with any 'state 0', does it?

It does. Not being idle is tracked by cpuidle driver as state 0 (arch independent):
/sys/devices/system/cpu/cpu0/cpuidle/state0/
halt/C1 on X86 is:
/sys/devices/system/cpu/cpu0/cpuidle/state1/
...

state0 is still OS idle!

the API is just weird for this, from a userspace perspective

if the kernel picks this state 0 for the idle handler, the userspace app gets
two events

one for going to state 0 to enter the idle state
one for going to state 0 to exit idle

but they're the exact same event in your API.

rather unpleasant from a userspace program perspective.... now I need to start tracking even more state on top in powertop to be able to make a guess at which of the two meanings a state 0 entry has.

#10 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Arjan van de Ven
Quoted 35 lines · On Monday 25 October 2010 15:55:08 Arjan van de Ven wrote:
Quoted 18 lines · Thomas Renninger
Quoted 10 lines · On Monday 25 October 2010 08:54:34 Arjan van de Ven wrote:

arch/x86/kernel/process.c static void mwait_idle(void) · line 489

Quoted 8 lines · Thomas Renninger
   static void poll_idle(void)
   {
arch/x86/kernel/process.c +0 -2
-	trace_power_start(POWER_CSTATE, 0, smp_processor_id());
   	local_irq_enable();
   	while (!need_resched())
   		cpu_relax();
-	trace_power_end(0);
   }

why did you remove the idle tracepoints from this one ???

Because no idle/sleep state is entered here.
State 0 does not exist or say, it means the machine is not idle. The new event uses idle state 0 spec conform as "exit sleep state".

If this should still be trackable some kind of dummy sleep state:
#define IDLE_BUSY_LOOP 0xFE
(or similar) must get defined and passed like this:

Quoted 8 lines
trace_processor_idle(IDLE_BUSY_LOOP, smp_processor_id());
     cpu_relax()

trace_processor_idle(0, smp_processor_id());

I could imagine this is somewhat worth it to compare idle results to "no idle state at all" is used.
But nobody should ever use idle=poll, comparing deep sleep states with C1 with (idle=halt) should be sufficient?

this is not idle=poll on the command line only.
this also gets used normally, in two cases
1) during real time operations, for some short periods of time (think wallstreet trading)
2) by the menu governor when the next event is less than a few microseconds away, so short that even C1 is too much

Quoted 2 lines

I know that your new API tries to use "0" as exit, but 0 is already taken (in all power terminology at least on x86 it is) for this.

cpuidle indeed misuses C0 as "poll idle" state.
That's really bad/misleading, but nothing that can be changed easily.

I agree shifting C0 (cpuidle) <-> POLL_IDLE event
and "not idle" <-> real C0 (executing instructions)
or however this gets mapped makes things even worse.

Damn, it could be that easy and straight forward, but I agree that this kills the approach to trigger state 0 event if C0 is entered (C0 as defined as operational mode executing instructions).

Thomas

#21 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Arjan van de Ven
Quoted 35 lines · On Monday 25 October 2010 15:55:08 Arjan van de Ven wrote:
Quoted 18 lines · Thomas Renninger
Quoted 10 lines · On Monday 25 October 2010 08:54:34 Arjan van de Ven wrote:

arch/x86/kernel/process.c static void mwait_idle(void) · line 489

Quoted 8 lines · Thomas Renninger
   static void poll_idle(void)
   {
arch/x86/kernel/process.c +0 -2
-	trace_power_start(POWER_CSTATE, 0, smp_processor_id());
   	local_irq_enable();
   	while (!need_resched())
   		cpu_relax();
-	trace_power_end(0);
   }

why did you remove the idle tracepoints from this one ???

Because no idle/sleep state is entered here.
State 0 does not exist or say, it means the machine is not idle. The new event uses idle state 0 spec conform as "exit sleep state".

If this should still be trackable some kind of dummy sleep state:
#define IDLE_BUSY_LOOP 0xFE
(or similar) must get defined and passed like this:

Quoted 8 lines
trace_processor_idle(IDLE_BUSY_LOOP, smp_processor_id());
     cpu_relax()

trace_processor_idle(0, smp_processor_id());

I could imagine this is somewhat worth it to compare idle results to "no idle state at all" is used.
But nobody should ever use idle=poll, comparing deep sleep states with C1 with (idle=halt) should be sufficient?

this is not idle=poll on the command line only.
this also gets used normally, in two cases
1) during real time operations, for some short periods of time (think wallstreet trading)
2) by the menu governor when the next event is less than a few microseconds away, so short that even C1 is too much

Quoted 2 lines

I know that your new API tries to use "0" as exit, but 0 is already taken (in all power terminology at least on x86 it is) for this.

cpuidle indeed misuses C0 as "poll idle" state.
That's really bad/misleading, but nothing that can be changed easily.

I agree shifting C0 (cpuidle) <-> POLL_IDLE event
and "not idle" <-> real C0 (executing instructions)
or however this gets mapped makes things even worse.

Damn, it could be that easy and straight forward, but I agree that this kills the approach to trigger state 0 event if C0 is entered (C0 as defined as operational mode executing instructions).

Thomas

#11 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger
Quoted 5 lines · Thomas Renninger
Quoted 2 lines

I know that your new API tries to use "0" as exit, but 0 is already taken (in all power terminology at least on x86 it is) for this.

cpuidle indeed misuses C0 as "poll idle" state.
That's really bad/misleading, but nothing that can be changed easily.

Quoted 7 lines

I agree shifting C0 (cpuidle)<-> POLL_IDLE event
and "not idle"<-> real C0 (executing instructions)
or however this gets mapped makes things even worse.

Damn, it could be that easy and straight forward, but I agree that this kills the approach to trigger state 0 event if C0 is entered (C0 as defined as operational mode executing instructions).

ok so we have

"C0 idle"
and
"C0 no longer idle"

I'd propose using the number 0 for the first one (it makes the most logical sense, it's the least deep idle state etc etc)

we could use "-1" or "INT_MAX" for the later

but as a user of the API I rather like a separate "we're no longer idle" event... but if not, as long as things aren't ambigious I'll find a way to code around it.
basically with a separate event, I demultiplex based on event number between entry and exit.... with a special exit value I would just need a double demultiplex,
one on "idle" and then a second one on the state number to split between entry/exit.

#12 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger
Quoted 5 lines · Thomas Renninger
Quoted 2 lines

I know that your new API tries to use "0" as exit, but 0 is already taken (in all power terminology at least on x86 it is) for this.

cpuidle indeed misuses C0 as "poll idle" state.
That's really bad/misleading, but nothing that can be changed easily.

Quoted 7 lines

I agree shifting C0 (cpuidle)<-> POLL_IDLE event
and "not idle"<-> real C0 (executing instructions)
or however this gets mapped makes things even worse.

Damn, it could be that easy and straight forward, but I agree that this kills the approach to trigger state 0 event if C0 is entered (C0 as defined as operational mode executing instructions).

ok so we have

"C0 idle"
and
"C0 no longer idle"

I'd propose using the number 0 for the first one (it makes the most logical sense, it's the least deep idle state etc etc)

we could use "-1" or "INT_MAX" for the later

but as a user of the API I rather like a separate "we're no longer idle" event... but if not, as long as things aren't ambigious I'll find a way to code around it. basically with a separate event, I demultiplex based on event number between entry and exit.... with a special exit value I would just need a double demultiplex, one on "idle" and then a second one on the state number to split between entry/exit.

#30 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Arjan van de Ven
Quoted 11 lines · On Monday 25 October 2010 16:11:10 Arjan van de Ven wrote:
Quoted 7 lines · Thomas Renninger
Quoted 1 line

But the actual code does not actually deal with any 'state 0', does it?

It does. Not being idle is tracked by cpuidle driver as state 0 (arch independent):
/sys/devices/system/cpu/cpu0/cpuidle/state0/
halt/C1 on X86 is:
/sys/devices/system/cpu/cpu0/cpuidle/state1/
...

state0 is still OS idle!

Yes, I just realized that.
Which is very unfortunate.
The whole cpuidle stuff is based on ACPI C-states and cat /sys/devices/system/cpu/cpu0/cpuidle/state0/name C0
is plain wrong if it's used as "poll idle" time.
C0 is defined as (in the ACPI spec):
----------
2.5 Processor Power State Definitions
C0 Processor Power State
While the processor is in this state, it executes instructions.
----------

Quoted 6 lines

the API is just weird for this, from a userspace perspective

if the kernel picks this state 0 for the idle handler, the userspace app gets
two events

Quoted 6 lines

one for going to state 0 to enter the idle state
one for going to state 0 to exit idle

but they're the exact same event in your API.

rather unpleasant from a userspace program perspective....

Yeah. But the re-definition of C0 being "Linux poll idle" will confuse users as well. Not sure whether this should get touched, though.

Thanks for clarification, I wasn't aware of that...

Thomas

#31 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Arjan van de Ven
Quoted 11 lines · On Monday 25 October 2010 16:11:10 Arjan van de Ven wrote:
Quoted 7 lines · Thomas Renninger
Quoted 1 line

But the actual code does not actually deal with any 'state 0', does it?

It does. Not being idle is tracked by cpuidle driver as state 0 (arch independent):
/sys/devices/system/cpu/cpu0/cpuidle/state0/
halt/C1 on X86 is:
/sys/devices/system/cpu/cpu0/cpuidle/state1/
...

state0 is still OS idle!

Yes, I just realized that.
Which is very unfortunate.
The whole cpuidle stuff is based on ACPI C-states and cat /sys/devices/system/cpu/cpu0/cpuidle/state0/name C0
is plain wrong if it's used as "poll idle" time.
C0 is defined as (in the ACPI spec):
----------
2.5 Processor Power State Definitions
C0 Processor Power State
While the processor is in this state, it executes instructions.
----------

Quoted 6 lines

the API is just weird for this, from a userspace perspective

if the kernel picks this state 0 for the idle handler, the userspace app gets
two events

Quoted 6 lines

one for going to state 0 to enter the idle state
one for going to state 0 to exit idle

but they're the exact same event in your API.

rather unpleasant from a userspace program perspective....

Yeah. But the re-definition of C0 being "Linux poll idle" will confuse users as well. Not sure whether this should get touched, though.

Thanks for clarification, I wasn't aware of that...

Thomas

#13 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Arjan van de Ven
Quoted 23 lines · Arjan van de Ven
Quoted 5 lines · Thomas Renninger
Quoted 2 lines

I know that your new API tries to use "0" as exit, but 0 is already taken (in all power terminology at least on x86 it is) for this.

cpuidle indeed misuses C0 as "poll idle" state.
That's really bad/misleading, but nothing that can be changed easily.

Quoted 7 lines

I agree shifting C0 (cpuidle)<-> POLL_IDLE event
and "not idle"<-> real C0 (executing instructions)
or however this gets mapped makes things even worse.

Damn, it could be that easy and straight forward, but I agree that this kills the approach to trigger state 0 event if C0 is entered (C0 as defined as operational mode executing instructions).

ok so we have

"C0 idle"
and
"C0 no longer idle"

I'd propose using the number 0 for the first one (it makes the most logical sense, it's the least deep idle state etc etc)

Quoted 7 lines

we could use "-1" or "INT_MAX" for the later

but as a user of the API I rather like a separate "we're no longer idle" event... but if not, as long as things aren't ambigious I'll find a way to code around it.

basically with a separate event, I demultiplex based on event number between entry and exit.... with a special exit value I would just need a double demultiplex,

Hm, does not sound particularly smart.

Quoted 2 lines

one on "idle" and then a second one on the state number to split between entry/exit.

The thing is, in terms of CPU idle state, if the old tracepoints give us all the information that the new tracepoints, why dont we simply add the tracepoints to ARM and be done with it? No app needs to be changed in that case, etc.

Plus, lets express the suspend/resume tracepoints as suspend_enter(X)/suspend_exit() events as well, to keep it symmetric and consistent with the other enter/exit events.

The rename alone isnt a strong enough reason really. 'entering idle state X' and 'exiting idle' is pretty much synonymous to 'enter idle state X'.

Thanks,

Ingo

#20 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Arjan van de Ven
Quoted 23 lines · Arjan van de Ven
Quoted 5 lines · Thomas Renninger
Quoted 2 lines

I know that your new API tries to use "0" as exit, but 0 is already taken (in all power terminology at least on x86 it is) for this.

cpuidle indeed misuses C0 as "poll idle" state.
That's really bad/misleading, but nothing that can be changed easily.

Quoted 7 lines

I agree shifting C0 (cpuidle)<-> POLL_IDLE event
and "not idle"<-> real C0 (executing instructions)
or however this gets mapped makes things even worse.

Damn, it could be that easy and straight forward, but I agree that this kills the approach to trigger state 0 event if C0 is entered (C0 as defined as operational mode executing instructions).

ok so we have

"C0 idle"
and
"C0 no longer idle"

I'd propose using the number 0 for the first one (it makes the most logical sense, it's the least deep idle state etc etc)

Quoted 7 lines

we could use "-1" or "INT_MAX" for the later

but as a user of the API I rather like a separate "we're no longer idle" event... but if not, as long as things aren't ambigious I'll find a way to code around it.

basically with a separate event, I demultiplex based on event number between entry and exit.... with a special exit value I would just need a double demultiplex,

Hm, does not sound particularly smart.

Quoted 2 lines

one on "idle" and then a second one on the state number to split between entry/exit.

The thing is, in terms of CPU idle state, if the old tracepoints give us all the information that the new tracepoints, why dont we simply add the tracepoints to ARM and be done with it? No app needs to be changed in that case, etc.

Plus, lets express the suspend/resume tracepoints as suspend_enter(X)/suspend_exit() events as well, to keep it symmetric and consistent with the other enter/exit events.

The rename alone isnt a strong enough reason really. 'entering idle state X' and 'exiting idle' is pretty much synonymous to 'enter idle state X'.

Thanks,

Ingo

#14 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Ingo Molnar
Quoted 6 lines · On Monday 25 October 2010 16:56:04 Ingo Molnar wrote:
Quoted 4 lines · Arjan van de Ven

On 10/25/2010 7:36 AM, Thomas Renninger wrote:
ok so we have

"C0 idle"

Ideally this should not be called C0, but expressed as (#define) POLL_IDLE wherever possible.

In all documentations/specs/white papers about other OSes C0 is refered to as not being idle.
Linux mis-uses it as a self-defined idle state which is really confusing.

Quoted 5 lines
Quoted 5 lines

and
"C0 no longer idle"

I'd propose using the number 0 for the first one (it makes the most logical sense, it's the least deep idle state etc etc)

I would use a special number for the "Linux only" state.

Quoted 16 lines
Quoted 6 lines

we could use "-1" or "INT_MAX" for the later
but as a user of the API I rather like a separate "we're no longer idle" event... but if not, as long as things aren't ambigious I'll find a way to code around it.

basically with a separate event, I demultiplex based on event number between entry and exit.... with a special exit value I would just need a double demultiplex,

Hm, does not sound particularly smart.

Quoted 2 lines

one on "idle" and then a second one on the state number to split between entry/exit.

The thing is, in terms of CPU idle state, if the old tracepoints give us all the information that the new tracepoints, why dont we simply add the tracepoints to ARM and be done with it? No app needs to be changed in that case, etc.

Quoted 6 lines

Plus, lets express the suspend/resume tracepoints as suspend_enter(X)/suspend_exit() events as well, to keep it symmetric and consistent with the other enter/exit events.

The rename alone isnt a strong enough reason really. 'entering idle state X' and 'exiting idle' is pretty much synonymous to 'enter idle state X'.

It's not only that, my patch also:
- eleminates the never ever used type= field
- uses a better name, currently it's power:power_{start,end} How would you name another power event...

Altogether, it should justify the proposed cleanup(s).
But with this C0 clash, I am not sure whether:
1) as Ingo said any clean up
2) a minimal cleanup:
- rename power:power_{start,end} to power:processor_idle{start,end}
- get rid of type= field
3) or a maximum cleanup:
- plus not use start/end events, but use one state transition event.
should be done.
I think best is Jean goes with current definitions.
2. is far less intrusive and if you like to have it, I can still send another patch.

Thomas

#19 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Ingo Molnar
Quoted 6 lines · On Monday 25 October 2010 16:56:04 Ingo Molnar wrote:
Quoted 4 lines · Arjan van de Ven

On 10/25/2010 7:36 AM, Thomas Renninger wrote:
ok so we have

"C0 idle"

Ideally this should not be called C0, but expressed as (#define) POLL_IDLE wherever possible.

In all documentations/specs/white papers about other OSes C0 is refered to as not being idle.
Linux mis-uses it as a self-defined idle state which is really confusing.

Quoted 5 lines
Quoted 5 lines

and
"C0 no longer idle"

I'd propose using the number 0 for the first one (it makes the most logical sense, it's the least deep idle state etc etc)

I would use a special number for the "Linux only" state.

Quoted 16 lines
Quoted 6 lines

we could use "-1" or "INT_MAX" for the later
but as a user of the API I rather like a separate "we're no longer idle" event... but if not, as long as things aren't ambigious I'll find a way to code around it.

basically with a separate event, I demultiplex based on event number between entry and exit.... with a special exit value I would just need a double demultiplex,

Hm, does not sound particularly smart.

Quoted 2 lines

one on "idle" and then a second one on the state number to split between entry/exit.

The thing is, in terms of CPU idle state, if the old tracepoints give us all the information that the new tracepoints, why dont we simply add the tracepoints to ARM and be done with it? No app needs to be changed in that case, etc.

Quoted 6 lines

Plus, lets express the suspend/resume tracepoints as suspend_enter(X)/suspend_exit() events as well, to keep it symmetric and consistent with the other enter/exit events.

The rename alone isnt a strong enough reason really. 'entering idle state X' and 'exiting idle' is pretty much synonymous to 'enter idle state X'.

It's not only that, my patch also:
- eleminates the never ever used type= field
- uses a better name, currently it's power:power_{start,end} How would you name another power event...

Altogether, it should justify the proposed cleanup(s).
But with this C0 clash, I am not sure whether:
1) as Ingo said any clean up
2) a minimal cleanup:
- rename power:power_{start,end} to power:processor_idle{start,end}
- get rid of type= field
3) or a maximum cleanup:
- plus not use start/end events, but use one state transition event.
should be done.
I think best is Jean goes with current definitions.
2. is far less intrusive and if you like to have it, I can still send another patch.

Thomas

#15 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger
Quoted 5 lines · Thomas Renninger
Quoted 4 lines · On Monday 25 October 2010 16:56:04 Ingo Molnar wrote:
Quoted 3 lines · Arjan van de Ven

On 10/25/2010 7:36 AM, Thomas Renninger wrote:
ok so we have

Quoted 8 lines
Quoted 1 line
Quoted 1 line

"C0 idle"

Ideally this should not be called C0, but expressed as (#define) POLL_IDLE wherever possible.

In all documentations/specs/white papers about other OSes C0 is refered to as not being idle.
Linux mis-uses it as a self-defined idle state which is really confusing.

sure naming is one thing

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

and
"C0 no longer idle"

I'd propose using the number 0 for the first one (it makes the most logical sense, it's the least deep idle state etc etc)

I would use a special number for the "Linux only" state.

that special number is 0 though..
it makes sense in ordering, 0 < 1, 1 < 2 etc

0 makes for a really bad special number for the exit marker; not just here, but also for your suspend hook, that one definitely needs to change
(since current commercially available SOCs already reuse 0 for this for standby level states)

#18 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Thomas Renninger
Quoted 5 lines · Thomas Renninger
Quoted 4 lines · On Monday 25 October 2010 16:56:04 Ingo Molnar wrote:
Quoted 3 lines · Arjan van de Ven

On 10/25/2010 7:36 AM, Thomas Renninger wrote:
ok so we have

Quoted 8 lines
Quoted 1 line
Quoted 1 line

"C0 idle"

Ideally this should not be called C0, but expressed as (#define) POLL_IDLE wherever possible.

In all documentations/specs/white papers about other OSes C0 is refered to as not being idle.
Linux mis-uses it as a self-defined idle state which is really confusing.

sure naming is one thing

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

and
"C0 no longer idle"

I'd propose using the number 0 for the first one (it makes the most logical sense, it's the least deep idle state etc etc)

I would use a special number for the "Linux only" state.

that special number is 0 though..
it makes sense in ordering, 0 < 1, 1 < 2 etc

0 makes for a really bad special number for the exit marker; not just here, but also for your suspend hook, that one definitely needs to change (since current commercially available SOCs already reuse 0 for this for standby level states)

#35 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Mathieu Desnoyers
Quoted 86 lines · Mathieu Desnoyers
Quoted 56 lines · * Ingo Molnar ([email protected]) wrote:
Quoted 19 lines · Thomas Renninger
Quoted 18 lines · On Monday 25 October 2010 12:04:28 Ingo Molnar wrote:
Quoted 11 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Well, most power saving hw models (and the code implementing them) have this kind of model:

Quoted 6 lines
Quoted 4 lines

enter power saving mode X
exit power saving mode

Where X is some sort of 'power saving deepness' attribute, right?

Sure.

Which is is the 'saner' model?

Quoted 2 lines

But ACPI and afaik this model got picked up for PCI and other (sub-)archs as well, defines state 0 as the non-power saving mode.

But the actual code does not actually deal with any 'state 0', does it? It enters an idle function and then exits it, right?

'power state' might be what is used for devices - but even there, we have:

- enter power state X
- exit power state

right?

Quoted 5 lines

Same as done here with machine suspend state (S0 is back from suspend) and this model should get picked up when device sleep states get tracked at some time.

It's consistent and applies to some well known specifications.

What we want it to be is for it to be the nicest, most understandable, most logical model - not one matching random hardware specifications.

( Hardware specifications only matter in so far that it should be possible to
express all the known hardware state transitions via these events efficiently. )

Quoted 8 lines
Quoted 4 lines

Also tracking processor_idle_{start,end} as a separate event makes no sense and there is no need to introduce: processor_idle_start/processor_idle_end machine_suspend_start/machine_suspend_end device_power_mode_start/device_power_mode_end events.

What do you mean by "makes no sense"?

Are they superfluous? Inefficient? Illogical?

I think it would require deep understanding of specific power modes of each architecture to split into this topology. On the bright side, it would bring clear understanding of which HW resource is being put to sleep, which would make automated analysis much easier to do. But maybe it's too much pain compared to the benefit. The related question is also: where is it best to put this logic ? In the kernel code ? In per-arch TRACE_EVENT() handlers or in external trace analysis plugins ?

Quoted 6 lines
Quoted 3 lines

Using state 0 as "exit/end", is much nicer for kernel/ userspace implementations/code and the user.

By that argument we should not have separate fork() and exit() syscalls either, but a set_process_state(1) and set_process_state(0) interface?

I'm by no mean expert on power saving hardware specs, but if it is possible for hardware to switch between two power saving states without passing through power state 0, then using a "set state" rather than an enter/exit would be more appropriate; even if we go for a scheme introducing

Quoted 6 lines

processor_idle_start/processor_idle_end,
machine_suspend_start/machine_suspend_end,
device_power_mode_start/device_power_mode_end.

I must defer to you guys to figure out if some hardware actually do that for either of CPU idle, suspend or device power modes.

Yes, you can go directly from PCI_D1 to PCI_D2, for one example.

Apart from this, attempting to put system suspend to the same bag as cpuidle is not going to work in the long run. They are _fundamentally_ different things event though the power state we get into as a result of suspend is approximately the same as we can get into via cpuidle (even in that case the energy savings will generally be different in both cases due to wakeup events).

Thanks,
Rafael

#36 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Mathieu Desnoyers
Quoted 86 lines · Mathieu Desnoyers
Quoted 56 lines · * Ingo Molnar ([email protected]) wrote:
Quoted 19 lines · Thomas Renninger
Quoted 18 lines · On Monday 25 October 2010 12:04:28 Ingo Molnar wrote:
Quoted 11 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Well, most power saving hw models (and the code implementing them) have this kind of model:

Quoted 6 lines
Quoted 4 lines

enter power saving mode X
exit power saving mode

Where X is some sort of 'power saving deepness' attribute, right?

Sure.

Which is is the 'saner' model?

Quoted 2 lines

But ACPI and afaik this model got picked up for PCI and other (sub-)archs as well, defines state 0 as the non-power saving mode.

But the actual code does not actually deal with any 'state 0', does it? It enters an idle function and then exits it, right?

'power state' might be what is used for devices - but even there, we have:

- enter power state X
- exit power state

right?

Quoted 5 lines

Same as done here with machine suspend state (S0 is back from suspend) and this model should get picked up when device sleep states get tracked at some time.

It's consistent and applies to some well known specifications.

What we want it to be is for it to be the nicest, most understandable, most logical model - not one matching random hardware specifications.

( Hardware specifications only matter in so far that it should be possible to
express all the known hardware state transitions via these events efficiently. )

Quoted 8 lines
Quoted 4 lines

Also tracking processor_idle_{start,end} as a separate event makes no sense and there is no need to introduce: processor_idle_start/processor_idle_end machine_suspend_start/machine_suspend_end device_power_mode_start/device_power_mode_end events.

What do you mean by "makes no sense"?

Are they superfluous? Inefficient? Illogical?

I think it would require deep understanding of specific power modes of each architecture to split into this topology. On the bright side, it would bring clear understanding of which HW resource is being put to sleep, which would make automated analysis much easier to do. But maybe it's too much pain compared to the benefit. The related question is also: where is it best to put this logic ? In the kernel code ? In per-arch TRACE_EVENT() handlers or in external trace analysis plugins ?

Quoted 6 lines
Quoted 3 lines

Using state 0 as "exit/end", is much nicer for kernel/ userspace implementations/code and the user.

By that argument we should not have separate fork() and exit() syscalls either, but a set_process_state(1) and set_process_state(0) interface?

I'm by no mean expert on power saving hardware specs, but if it is possible for hardware to switch between two power saving states without passing through power state 0, then using a "set state" rather than an enter/exit would be more appropriate; even if we go for a scheme introducing

Quoted 6 lines

processor_idle_start/processor_idle_end,
machine_suspend_start/machine_suspend_end,
device_power_mode_start/device_power_mode_end.

I must defer to you guys to figure out if some hardware actually do that for either of CPU idle, suspend or device power modes.

Yes, you can go directly from PCI_D1 to PCI_D2, for one example.

Apart from this, attempting to put system suspend to the same bag as cpuidle is not going to work in the long run. They are _fundamentally_ different things event though the power state we get into as a result of suspend is approximately the same as we can get into via cpuidle (even in that case the energy savings will generally be different in both cases due to wakeup events).

Thanks,
Rafael

#40 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Arjan van de Ven
Quoted 26 lines · Arjan van de Ven
Quoted 17 lines · Thomas Renninger
Quoted 16 lines · On Monday 25 October 2010 12:04:28 Ingo Molnar wrote:
Quoted 11 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Well, most power saving hw models (and the code implementing them) have this kind of model:

Quoted 7 lines
Quoted 4 lines

enter power saving mode X
exit power saving mode

Where X is some sort of 'power saving deepness' attribute, right?

Sure.
But ACPI and afaik this model got picked up for PCI and other (sub-)archs as well, defines state 0 as the non-power saving mode.

Quoted 6 lines

correct ,... "C0" is not power efficient... but it's still a valid OS idle state!
Also tracking processor_idle_{start,end} as a separate event!

same for "S0"... S0 as standby state is still valid... sure it doesn't save you much power... but that does not mean it's not valid.

If you mean ACPI S0, it is not a standby state. It actually is the full-power state.

Quoted 2 lines

(as indication, the Intel Moorestown platform, which is currently in production and available to OEMs, has such a S0 standby state)

Another naming confusion. How smart.

Quoted 10 lines
Quoted 7 lines

makes no sense and there is no need to introduce:
processor_idle_start/processor_idle_end machine_suspend_start/machine_suspend_end device_power_mode_start/device_power_mode_end events.
Using state 0 as "exit/end", is much nicer for kernel/ userspace implementations/code and the user.

actually no; having written a few of these in userspace so far, having a separate end event is easier to deal with;
the actions you take on entry and exit are complete separate code paths.

That's correct, unless you go directly from one low-power state to another (which is possible for example for PCI). We don't do that at the moment, but it's possible in principle and we may want to start doing that at one point.

Thanks,
Rafael

#41 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Arjan van de Ven
Quoted 26 lines · Arjan van de Ven
Quoted 17 lines · Thomas Renninger
Quoted 16 lines · On Monday 25 October 2010 12:04:28 Ingo Molnar wrote:
Quoted 11 lines · Thomas Renninger

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Well, most power saving hw models (and the code implementing them) have this kind of model:

Quoted 7 lines
Quoted 4 lines

enter power saving mode X
exit power saving mode

Where X is some sort of 'power saving deepness' attribute, right?

Sure.
But ACPI and afaik this model got picked up for PCI and other (sub-)archs as well, defines state 0 as the non-power saving mode.

Quoted 6 lines

correct ,... "C0" is not power efficient... but it's still a valid OS idle state!
Also tracking processor_idle_{start,end} as a separate event!

same for "S0"... S0 as standby state is still valid... sure it doesn't save you much power... but that does not mean it's not valid.

If you mean ACPI S0, it is not a standby state. It actually is the full-power state.

Quoted 2 lines

(as indication, the Intel Moorestown platform, which is currently in production and available to OEMs, has such a S0 standby state)

Another naming confusion. How smart.

Quoted 10 lines
Quoted 7 lines

makes no sense and there is no need to introduce:
processor_idle_start/processor_idle_end machine_suspend_start/machine_suspend_end device_power_mode_start/device_power_mode_end events.
Using state 0 as "exit/end", is much nicer for kernel/ userspace implementations/code and the user.

actually no; having written a few of these in userspace so far, having a separate end event is easier to deal with;
the actions you take on entry and exit are complete separate code paths.

That's correct, unless you go directly from one low-power state to another (which is possible for example for PCI). We don't do that at the moment, but it's possible in principle and we may want to start doing that at one point.

Thanks,
Rafael

#16 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Arjan van de Ven

@Ingo: Can you queue up 1/3, it's an independent fix.

Quoted 3 lines · Arjan van de Ven

On 10/25/2010 8:48 AM, Thomas Renninger wrote:

sure naming is one thing

Yes it should get renamed to not show:
cat /sys/devices/system/cpu/cpu0/cpuidle/state0/name C0
This is wrong and confusing

Quoted 9 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

and
"C0 no longer idle"

I'd propose using the number 0 for the first one (it makes the most logical sense, it's the least deep idle state etc etc)

I would use a special number for the "Linux only" state.

that special number is 0 though..
it makes sense in ordering, 0 < 1, 1 < 2 etc

As long as it stays a kernel and perf processor_idle internal number it does not hurt.
But userspace tools catching the perf idle event of state 0 should never refer to it as processor idle state 0 (or even worse C0). Instead they should try to get the name/description of:
/sys/../state0/name
or directly refer to it as "poll idle" state.

Processor idle state C0 is not only defined as "not being idle" in the specs, also turbostat and cpufreq-aperf use it correctly and refer to C0 when they show accounted "not idle" time.

Encouraged by your suggestions I send another version.
It's not a big deal to send 0xFFFFFFFF instead of 0 as "non power saving" state. If you can handle compatibility with it in powertop, it doesn't make things more complicated in kernel and perf timechart as I first thought it does.

Thomas

#17 lore raw

Re: [PATCH 2/3] PERF(kernel): Cleanup power events

In reply to Arjan van de Ven

@Ingo: Can you queue up 1/3, it's an independent fix.

Quoted 3 lines · Arjan van de Ven

On 10/25/2010 8:48 AM, Thomas Renninger wrote:

sure naming is one thing

Yes it should get renamed to not show:
cat /sys/devices/system/cpu/cpu0/cpuidle/state0/name C0
This is wrong and confusing

Quoted 9 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

and
"C0 no longer idle"

I'd propose using the number 0 for the first one (it makes the most logical sense, it's the least deep idle state etc etc)

I would use a special number for the "Linux only" state.

that special number is 0 though..
it makes sense in ordering, 0 < 1, 1 < 2 etc

As long as it stays a kernel and perf processor_idle internal number it does not hurt.
But userspace tools catching the perf idle event of state 0 should never refer to it as processor idle state 0 (or even worse C0). Instead they should try to get the name/description of:
/sys/../state0/name
or directly refer to it as "poll idle" state.

Processor idle state C0 is not only defined as "not being idle" in the specs, also turbostat and cpufreq-aperf use it correctly and refer to C0 when they show accounted "not idle" time.

Encouraged by your suggestions I send another version.
It's not a big deal to send 0xFFFFFFFF instead of 0 as "non power saving" state. If you can handle compatibility with it in powertop, it doesn't make things more complicated in kernel and perf timechart as I first thought it does.

Thomas

#45 lore raw

PATCH 1/1 PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger

Changes in V2:
- Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
- Use u32 instead of u64 for cpuid, state which is by far enough

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

and
power:power_frequency
is replaced with:
power:processor_frequency

power:machine_suspend
is newly introduced, a first implementation
comes from the ARM side, but it's easy to add these events in X86 as well if needed.

the type= field got removed from both, it was never used and the type is differed by the event type itself.

perf timechart
userspace tool gets adjusted in a separate patch.

CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Masami Hiramatsu <[email protected]>
CC Frank Eigler <[email protected]>
CC Steven Rostedt <[email protected]>
CC Kevin Hilman <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Jean Pihet <[email protected]>
CC Pierre Tardy <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Tejun Heo <[email protected]>
CC Mathieu Desnoyers <[email protected]>
CC Arjan van de Ven <[email protected]>
CC Ingo Molnar <[email protected]>
---
 arch/x86/kernel/process.c    |    7 +++-
 arch/x86/kernel/process_64.c |    2 +
 drivers/cpufreq/cpufreq.c    |    1 +
 drivers/cpuidle/cpuidle.c    |    1 +
 drivers/idle/intel_idle.c    |    1 +
 include/trace/events/power.h |   81 +++++++++++++++++++++++++++++++++++++++++-
 kernel/trace/Kconfig         |   14 +++++++
 kernel/trace/power-traces.c  |    3 ++
 8 files changed, 108 insertions(+), 2 deletions(-)
arch/x86/kernel/process.c +6 -1
index 57d1868..6a98da3 100644
@@ -374,6 +374,7 @@ void default_idle(void)
 {
 	if (hlt_use_halt()) {
 		trace_power_start(POWER_CSTATE, 1, smp_processor_id());
+		trace_processor_idle(1, smp_processor_id());
 		current_thread_info()->status &= ~TS_POLLING;
 		/*
 		 * TS_POLLING-cleared state must be visible before we
@@ -444,6 +445,7 @@ EXPORT_SYMBOL_GPL(cpu_idle_wait);
 void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
 {
 	trace_power_start(POWER_CSTATE, (ax>>4)+1, smp_processor_id());
+	trace_processor_idle((ax>>4)+1, smp_processor_id());
 	if (!need_resched()) {
 		if (cpu_has(&current_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
 			clflush((void *)&current_thread_info()->flags);
@@ -460,6 +462,7 @@ static void mwait_idle(void)
 {
 	if (!need_resched()) {
 		trace_power_start(POWER_CSTATE, 1, smp_processor_id());
+		trace_processor_idle(1, smp_processor_id());
 		if (cpu_has(&current_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
 			clflush((void *)&current_thread_info()->flags);
 
@@ -481,10 +484,12 @@ static void mwait_idle(void)
 static void poll_idle(void)
 {
 	trace_power_start(POWER_CSTATE, 0, smp_processor_id());
+	trace_processor_idle(1, smp_processor_id());
 	local_irq_enable();
 	while (!need_resched())
 		cpu_relax();
-	trace_power_end(0);
+	trace_power_end(smp_processor_id());
+	trace_processor_idle(PWR_EVENT_EXIT, smp_processor_id());
 }
 
 /*
arch/x86/kernel/process_64.c +2 -0
index 3d9ea53..5f2bb98 100644
@@ -142,6 +142,8 @@ void cpu_idle(void)
 			start_critical_timings();
 
 			trace_power_end(smp_processor_id());
+			trace_processor_idle(PWR_EVENT_EXIT,
+					     smp_processor_id());
 
 			/* In many cases the interrupt that ended idle
 			   has already called exit_idle. But some idle
drivers/cpufreq/cpufreq.c +1 -0
index 199dcb9..33bdc41 100644
@@ -355,6 +355,7 @@ void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
 		dprintk("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
 			(unsigned long)freqs->cpu);
 		trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
+		trace_processor_frequency(freqs->new, freqs->cpu);
 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
 				CPUFREQ_POSTCHANGE, freqs);
 		if (likely(policy) && likely(policy->cpu == freqs->cpu))
drivers/cpuidle/cpuidle.c +1 -0
index a507108..ec703e6 100644
@@ -107,6 +107,7 @@ static void cpuidle_idle_call(void)
 	if (cpuidle_curr_governor->reflect)
 		cpuidle_curr_governor->reflect(dev);
 	trace_power_end(smp_processor_id());
+	trace_processor_idle(PWR_EVENT_EXIT, smp_processor_id());
 }
 
 /**
drivers/idle/intel_idle.c +1 -0
index 21ac077..c78e496 100644
@@ -202,6 +202,7 @@ static int intel_idle(struct cpuidle_device *dev, struct cpuidle_state *state)
 
 	stop_critical_timings();
 	trace_power_start(POWER_CSTATE, (eax >> 4) + 1, cpu);
+	trace_processor_idle((eax >> 4) + 1, smp_processor_id());
 	if (!need_resched()) {
 
 		__monitor((void *)&current_thread_info()->flags, 0, 0);
include/trace/events/power.h +80 -1

94 lines not on this page · raw message

kernel/trace/Kconfig +14 -0
index 538501c..0b5c841 100644
@@ -64,6 +64,20 @@ config EVENT_TRACING
 	select CONTEXT_SWITCH_TRACER
 	bool
 
+config EVENT_POWER_TRACING_DEPRECATED
+	depends on EVENT_TRACING
+	bool
+	help
+	  Provides old power event types:
+	  C-state/idle accounting events:
+	  power:power_start
+	  power:power_end
+	  and old cpufreq accounting event:
+	  power:power_frequency
+	  This is for userspace compatibility
+	  and will vanish after 5 kernel iterations,
+	  namely 2.6.41.
+
 config CONTEXT_SWITCH_TRACER
 	bool
 
kernel/trace/power-traces.c +3 -0
index 0e0497d..6b6da42 100644
@@ -13,5 +13,8 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/power.h>
 
+#ifdef EVENT_POWER_TRACING_DEPRECATED
 EXPORT_TRACEPOINT_SYMBOL_GPL(power_start);
+#endif
+EXPORT_TRACEPOINT_SYMBOL_GPL(processor_idle);
 
Signature · 1 line
1.6.3
#46 lore raw

PATCH 1/1 PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger

Changes in V2:
- Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
- Use u32 instead of u64 for cpuid, state which is by far enough

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

and
power:power_frequency
is replaced with:
power:processor_frequency

power:machine_suspend
is newly introduced, a first implementation
comes from the ARM side, but it's easy to add these events in X86 as well if needed.

the type= field got removed from both, it was never used and the type is differed by the event type itself.

perf timechart
userspace tool gets adjusted in a separate patch.

CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Masami Hiramatsu <[email protected]>
CC Frank Eigler <[email protected]>
CC Steven Rostedt <[email protected]>
CC Kevin Hilman <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Jean Pihet <[email protected]>
CC Pierre Tardy <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Tejun Heo <[email protected]>
CC Mathieu Desnoyers <[email protected]>
CC Arjan van de Ven <[email protected]>
CC Ingo Molnar <[email protected]>
---
 arch/x86/kernel/process.c    |    7 +++-
 arch/x86/kernel/process_64.c |    2 +
 drivers/cpufreq/cpufreq.c    |    1 +
 drivers/cpuidle/cpuidle.c    |    1 +
 drivers/idle/intel_idle.c    |    1 +
 include/trace/events/power.h |   81 +++++++++++++++++++++++++++++++++++++++++-
 kernel/trace/Kconfig         |   14 +++++++
 kernel/trace/power-traces.c  |    3 ++
 8 files changed, 108 insertions(+), 2 deletions(-)
arch/x86/kernel/process.c +6 -1
index 57d1868..6a98da3 100644
@@ -374,6 +374,7 @@ void default_idle(void)
 {
 	if (hlt_use_halt()) {
 		trace_power_start(POWER_CSTATE, 1, smp_processor_id());
+		trace_processor_idle(1, smp_processor_id());
 		current_thread_info()->status &= ~TS_POLLING;
 		/*
 		 * TS_POLLING-cleared state must be visible before we
@@ -444,6 +445,7 @@ EXPORT_SYMBOL_GPL(cpu_idle_wait);
 void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
 {
 	trace_power_start(POWER_CSTATE, (ax>>4)+1, smp_processor_id());
+	trace_processor_idle((ax>>4)+1, smp_processor_id());
 	if (!need_resched()) {
 		if (cpu_has(&current_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
 			clflush((void *)&current_thread_info()->flags);
@@ -460,6 +462,7 @@ static void mwait_idle(void)
 {
 	if (!need_resched()) {
 		trace_power_start(POWER_CSTATE, 1, smp_processor_id());
+		trace_processor_idle(1, smp_processor_id());
 		if (cpu_has(&current_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
 			clflush((void *)&current_thread_info()->flags);
 
@@ -481,10 +484,12 @@ static void mwait_idle(void)
 static void poll_idle(void)
 {
 	trace_power_start(POWER_CSTATE, 0, smp_processor_id());
+	trace_processor_idle(1, smp_processor_id());
 	local_irq_enable();
 	while (!need_resched())
 		cpu_relax();
-	trace_power_end(0);
+	trace_power_end(smp_processor_id());
+	trace_processor_idle(PWR_EVENT_EXIT, smp_processor_id());
 }
 
 /*
arch/x86/kernel/process_64.c +2 -0
index 3d9ea53..5f2bb98 100644
@@ -142,6 +142,8 @@ void cpu_idle(void)
 			start_critical_timings();
 
 			trace_power_end(smp_processor_id());
+			trace_processor_idle(PWR_EVENT_EXIT,
+					     smp_processor_id());
 
 			/* In many cases the interrupt that ended idle
 			   has already called exit_idle. But some idle
drivers/cpufreq/cpufreq.c +1 -0
index 199dcb9..33bdc41 100644
@@ -355,6 +355,7 @@ void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
 		dprintk("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
 			(unsigned long)freqs->cpu);
 		trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
+		trace_processor_frequency(freqs->new, freqs->cpu);
 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
 				CPUFREQ_POSTCHANGE, freqs);
 		if (likely(policy) && likely(policy->cpu == freqs->cpu))
drivers/cpuidle/cpuidle.c +1 -0
index a507108..ec703e6 100644
@@ -107,6 +107,7 @@ static void cpuidle_idle_call(void)
 	if (cpuidle_curr_governor->reflect)
 		cpuidle_curr_governor->reflect(dev);
 	trace_power_end(smp_processor_id());
+	trace_processor_idle(PWR_EVENT_EXIT, smp_processor_id());
 }
 
 /**
drivers/idle/intel_idle.c +1 -0
index 21ac077..c78e496 100644
@@ -202,6 +202,7 @@ static int intel_idle(struct cpuidle_device *dev, struct cpuidle_state *state)
 
 	stop_critical_timings();
 	trace_power_start(POWER_CSTATE, (eax >> 4) + 1, cpu);
+	trace_processor_idle((eax >> 4) + 1, smp_processor_id());
 	if (!need_resched()) {
 
 		__monitor((void *)&current_thread_info()->flags, 0, 0);
include/trace/events/power.h +80 -1

94 lines not on this page · raw message

kernel/trace/Kconfig +14 -0
index 538501c..0b5c841 100644
@@ -64,6 +64,20 @@ config EVENT_TRACING
 	select CONTEXT_SWITCH_TRACER
 	bool
 
+config EVENT_POWER_TRACING_DEPRECATED
+	depends on EVENT_TRACING
+	bool
+	help
+	  Provides old power event types:
+	  C-state/idle accounting events:
+	  power:power_start
+	  power:power_end
+	  and old cpufreq accounting event:
+	  power:power_frequency
+	  This is for userspace compatibility
+	  and will vanish after 5 kernel iterations,
+	  namely 2.6.41.
+
 config CONTEXT_SWITCH_TRACER
 	bool
 
kernel/trace/power-traces.c +3 -0
index 0e0497d..6b6da42 100644
@@ -13,5 +13,8 @@
 #define CREATE_TRACE_POINTS
 #include <trace/events/power.h>
 
+#ifdef EVENT_POWER_TRACING_DEPRECATED
 EXPORT_TRACEPOINT_SYMBOL_GPL(power_start);
+#endif
+EXPORT_TRACEPOINT_SYMBOL_GPL(processor_idle);
 
Signature · 1 line
1.6.3
#134 lore raw

PATCH 1/1 PERF(userspace): Adjust perf timechart to the new power events V2

In reply to Thomas Renninger

Changes in V2:
- Hanlde PWR_EVENT_EXIT instead of 0 to recon non-power state

The transition was rather smooth, only part I had to fiddle some time was the check whether a tracepoint/event is supported by the running kernel.

builtin-timechart must only pass -e power:xy events which are supported by the running kernel.
For this I added the tiny helper function:
int is_valid_tracepoint(const char *event_string)
to parse-events.[hc]
which could be more generic as an interface and support hardware/software/... events, not only tracepoints, but someone else could extend that if needed...

CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Masami Hiramatsu <[email protected]>
CC Frank Eigler <[email protected]>
CC Steven Rostedt <[email protected]>
CC Kevin Hilman <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Jean Pihet <[email protected]>
CC Pierre Tardy <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Tejun Heo <[email protected]>
CC Mathieu Desnoyers <[email protected]>
CC Arjan van de Ven <[email protected]>
CC Ingo Molnar <[email protected]>
---
 tools/perf/builtin-timechart.c |   89 ++++++++++++++++++++++++++++++++-------
 tools/perf/util/parse-events.c |   43 +++++++++++++++++++-
 tools/perf/util/parse-events.h |    1 +
 3 files changed, 116 insertions(+), 17 deletions(-)
tools/perf/builtin-timechart.c +73 -16
index 9bcc38f..7eaa5b5 100644
@@ -32,6 +32,10 @@
 #include "util/session.h"
 #include "util/svghelper.h"
 
+#define SUPPORT_OLD_POWER_EVENTS 1
+#define PWR_EVENT_EXIT 0xFFFFFFFF
+
+
 static char		const *input_name = "perf.data";
 static char		const *output_name = "output.svg";
 
@@ -298,12 +302,25 @@ struct trace_entry {
 	int			lock_depth;
 };
 
-struct power_entry {
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+struct power_entry_old {
 	struct trace_entry te;
 	u64	type;
 	u64	value;
 	u64	cpu_id;
 };
+#endif
+
+struct power_processor_entry {
+	struct trace_entry te;
+	u64	state;
+	u64	cpu_id;
+};
+
+struct power_suspend_entry {
+	struct trace_entry te;
+	u64	state;
+};
 
 #define TASK_COMM_LEN 16
 struct wakeup_entry {
@@ -489,29 +506,46 @@ static int process_sample_event(event_t *event, struct perf_session *session)
 	te = (void *)data.raw_data;
 	if (session->sample_type & PERF_SAMPLE_RAW && data.raw_size > 0) {
 		char *event_str;
-		struct power_entry *pe;
-
-		pe = (void *)te;
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+		struct power_entry_old *peo;
+		peo = (void *)te;
+#endif
 
 		event_str = perf_header__find_event(te->type);
 
 		if (!event_str)
 			return 0;
 
-		if (strcmp(event_str, "power:power_start") == 0)
-			c_state_start(pe->cpu_id, data.time, pe->value);
-
-		if (strcmp(event_str, "power:power_end") == 0)
-			c_state_end(pe->cpu_id, data.time);
+		if (strcmp(event_str, "power:processor_idle") == 0) {
+			struct power_processor_entry *ppe = (void *)te;
+			if (ppe->state == PWR_EVENT_EXIT)
+				c_state_end(ppe->cpu_id, data.time);
+			else
+				c_state_start(ppe->cpu_id, data.time,
+					      ppe->state);
+		}
 
-		if (strcmp(event_str, "power:power_frequency") == 0)
-			p_state_change(pe->cpu_id, data.time, pe->value);
+		else if (strcmp(event_str, "power:processor_frequency") == 0) {
+			struct power_processor_entry *ppe = (void *)te;
+			p_state_change(ppe->cpu_id, data.time, ppe->state);
+		}
 
-		if (strcmp(event_str, "sched:sched_wakeup") == 0)
+		else if (strcmp(event_str, "sched:sched_wakeup") == 0)
 			sched_wakeup(data.cpu, data.time, data.pid, te);
 
-		if (strcmp(event_str, "sched:sched_switch") == 0)
+		else if (strcmp(event_str, "sched:sched_switch") == 0)
 			sched_switch(data.cpu, data.time, te);
+
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+		else if (strcmp(event_str, "power:power_start") == 0)
+			c_state_start(peo->cpu_id, data.time, peo->value);
+
+		else if (strcmp(event_str, "power:power_end") == 0)
+			c_state_end(peo->cpu_id, data.time);
+
+		else if (strcmp(event_str, "power:power_frequency") == 0)
+			p_state_change(peo->cpu_id, data.time, peo->value);
+#endif
 	}
 	return 0;
 }
@@ -968,7 +1002,8 @@ static const char * const timechart_usage[] = {
 	NULL
 };
 
-static const char *record_args[] = {
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+static const char *record_old_args[] = {
 	"record",
 	"-a",
 	"-R",
@@ -980,16 +1015,38 @@ static const char *record_args[] = {
 	"-e", "sched:sched_wakeup",
 	"-e", "sched:sched_switch",
 };
+#endif
+
+static const char *record_new_args[] = {
+	"record",
+	"-a",
+	"-R",
+	"-f",
+	"-c", "1",
+	"-e", "power:processor_frequency",
+	"-e", "power:processor_idle",
+	"-e", "sched:sched_wakeup",
+	"-e", "sched:sched_switch",
+};
 
 static int __cmd_record(int argc, const char **argv)
 {
 	unsigned int rec_argc, i, j;
 	const char **rec_argv;
+	const char **record_args = record_new_args;
+	unsigned int record_elems = ARRAY_SIZE(record_new_args);
 
-	rec_argc = ARRAY_SIZE(record_args) + argc - 1;
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+	if (is_valid_tracepoint("power:power_start")) {
+		record_args = record_old_args;
+		record_elems = ARRAY_SIZE(record_old_args);
+	}
+#endif
+	
+	rec_argc = record_elems + argc - 1;
 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
 
-	for (i = 0; i < ARRAY_SIZE(record_args); i++)
+	for (i = 0; i < record_elems; i++)
 		rec_argv[i] = strdup(record_args[i]);
 
 	for (j = 1; j < (unsigned int)argc; j++, i++)
tools/perf/util/parse-events.c +42 -1

55 lines not on this page · raw message

tools/perf/util/parse-events.h +1 -0
index fc4ab3f..7ab4685 100644
@@ -29,6 +29,7 @@ extern int parse_filter(const struct option *opt, const char *str, int unset);
 #define EVENTS_HELP_MAX (128*1024)
 
 extern void print_events(void);
+extern int is_valid_tracepoint(const char *event_string);
 
 extern char debugfs_path[];
 extern int valid_debugfs_mount(const char *debugfs);
Signature · 1 line
1.6.3
#135 lore raw

PATCH 1/1 PERF(userspace): Adjust perf timechart to the new power events V2

In reply to Thomas Renninger

Changes in V2:
- Hanlde PWR_EVENT_EXIT instead of 0 to recon non-power state

The transition was rather smooth, only part I had to fiddle some time was the check whether a tracepoint/event is supported by the running kernel.

builtin-timechart must only pass -e power:xy events which are supported by the running kernel.
For this I added the tiny helper function:
int is_valid_tracepoint(const char *event_string)
to parse-events.[hc]
which could be more generic as an interface and support hardware/software/... events, not only tracepoints, but someone else could extend that if needed...

CC Linus Torvalds <[email protected]>
CC Andrew Morton <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Masami Hiramatsu <[email protected]>
CC Frank Eigler <[email protected]>
CC Steven Rostedt <[email protected]>
CC Kevin Hilman <[email protected]>
CC Peter Zijlstra <[email protected]>
CC Jean Pihet <[email protected]>
CC Pierre Tardy <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Tejun Heo <[email protected]>
CC Mathieu Desnoyers <[email protected]>
CC Arjan van de Ven <[email protected]>
CC Ingo Molnar <[email protected]>
---
 tools/perf/builtin-timechart.c |   89 ++++++++++++++++++++++++++++++++-------
 tools/perf/util/parse-events.c |   43 +++++++++++++++++++-
 tools/perf/util/parse-events.h |    1 +
 3 files changed, 116 insertions(+), 17 deletions(-)
tools/perf/builtin-timechart.c +73 -16
index 9bcc38f..7eaa5b5 100644
@@ -32,6 +32,10 @@
 #include "util/session.h"
 #include "util/svghelper.h"
 
+#define SUPPORT_OLD_POWER_EVENTS 1
+#define PWR_EVENT_EXIT 0xFFFFFFFF
+
+
 static char		const *input_name = "perf.data";
 static char		const *output_name = "output.svg";
 
@@ -298,12 +302,25 @@ struct trace_entry {
 	int			lock_depth;
 };
 
-struct power_entry {
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+struct power_entry_old {
 	struct trace_entry te;
 	u64	type;
 	u64	value;
 	u64	cpu_id;
 };
+#endif
+
+struct power_processor_entry {
+	struct trace_entry te;
+	u64	state;
+	u64	cpu_id;
+};
+
+struct power_suspend_entry {
+	struct trace_entry te;
+	u64	state;
+};
 
 #define TASK_COMM_LEN 16
 struct wakeup_entry {
@@ -489,29 +506,46 @@ static int process_sample_event(event_t *event, struct perf_session *session)
 	te = (void *)data.raw_data;
 	if (session->sample_type & PERF_SAMPLE_RAW && data.raw_size > 0) {
 		char *event_str;
-		struct power_entry *pe;
-
-		pe = (void *)te;
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+		struct power_entry_old *peo;
+		peo = (void *)te;
+#endif
 
 		event_str = perf_header__find_event(te->type);
 
 		if (!event_str)
 			return 0;
 
-		if (strcmp(event_str, "power:power_start") == 0)
-			c_state_start(pe->cpu_id, data.time, pe->value);
-
-		if (strcmp(event_str, "power:power_end") == 0)
-			c_state_end(pe->cpu_id, data.time);
+		if (strcmp(event_str, "power:processor_idle") == 0) {
+			struct power_processor_entry *ppe = (void *)te;
+			if (ppe->state == PWR_EVENT_EXIT)
+				c_state_end(ppe->cpu_id, data.time);
+			else
+				c_state_start(ppe->cpu_id, data.time,
+					      ppe->state);
+		}
 
-		if (strcmp(event_str, "power:power_frequency") == 0)
-			p_state_change(pe->cpu_id, data.time, pe->value);
+		else if (strcmp(event_str, "power:processor_frequency") == 0) {
+			struct power_processor_entry *ppe = (void *)te;
+			p_state_change(ppe->cpu_id, data.time, ppe->state);
+		}
 
-		if (strcmp(event_str, "sched:sched_wakeup") == 0)
+		else if (strcmp(event_str, "sched:sched_wakeup") == 0)
 			sched_wakeup(data.cpu, data.time, data.pid, te);
 
-		if (strcmp(event_str, "sched:sched_switch") == 0)
+		else if (strcmp(event_str, "sched:sched_switch") == 0)
 			sched_switch(data.cpu, data.time, te);
+
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+		else if (strcmp(event_str, "power:power_start") == 0)
+			c_state_start(peo->cpu_id, data.time, peo->value);
+
+		else if (strcmp(event_str, "power:power_end") == 0)
+			c_state_end(peo->cpu_id, data.time);
+
+		else if (strcmp(event_str, "power:power_frequency") == 0)
+			p_state_change(peo->cpu_id, data.time, peo->value);
+#endif
 	}
 	return 0;
 }
@@ -968,7 +1002,8 @@ static const char * const timechart_usage[] = {
 	NULL
 };
 
-static const char *record_args[] = {
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+static const char *record_old_args[] = {
 	"record",
 	"-a",
 	"-R",
@@ -980,16 +1015,38 @@ static const char *record_args[] = {
 	"-e", "sched:sched_wakeup",
 	"-e", "sched:sched_switch",
 };
+#endif
+
+static const char *record_new_args[] = {
+	"record",
+	"-a",
+	"-R",
+	"-f",
+	"-c", "1",
+	"-e", "power:processor_frequency",
+	"-e", "power:processor_idle",
+	"-e", "sched:sched_wakeup",
+	"-e", "sched:sched_switch",
+};
 
 static int __cmd_record(int argc, const char **argv)
 {
 	unsigned int rec_argc, i, j;
 	const char **rec_argv;
+	const char **record_args = record_new_args;
+	unsigned int record_elems = ARRAY_SIZE(record_new_args);
 
-	rec_argc = ARRAY_SIZE(record_args) + argc - 1;
+#if defined(SUPPORT_OLD_POWER_EVENTS)
+	if (is_valid_tracepoint("power:power_start")) {
+		record_args = record_old_args;
+		record_elems = ARRAY_SIZE(record_old_args);
+	}
+#endif
+	
+	rec_argc = record_elems + argc - 1;
 	rec_argv = calloc(rec_argc + 1, sizeof(char *));
 
-	for (i = 0; i < ARRAY_SIZE(record_args); i++)
+	for (i = 0; i < record_elems; i++)
 		rec_argv[i] = strdup(record_args[i]);
 
 	for (j = 1; j < (unsigned int)argc; j++, i++)
tools/perf/util/parse-events.c +42 -1

55 lines not on this page · raw message

tools/perf/util/parse-events.h +1 -0
index fc4ab3f..7ab4685 100644
@@ -29,6 +29,7 @@ extern int parse_filter(const struct option *opt, const char *str, int unset);
 #define EVENTS_HELP_MAX (128*1024)
 
 extern void print_events(void);
+extern int is_valid_tracepoint(const char *event_string);
 
 extern char debugfs_path[];
 extern int valid_debugfs_mount(const char *debugfs);
Signature · 1 line
1.6.3
Acked-by on v1 1/1
#47 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 26 lines · Thomas Renninger

Changes in V2:
- Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
- Use u32 instead of u64 for cpuid, state which is by far enough

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

and
power:power_frequency
is replaced with:
power:processor_frequency

power:machine_suspend
is newly introduced, a first implementation
comes from the ARM side, but it's easy to add these events in X86 as well if needed.

Quoted 6 lines

the type= field got removed from both, it was never used and the type is differed by the event type itself.

perf timechart
userspace tool gets adjusted in a separate patch.

Acked-by on v1 1/1
#48 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 26 lines · Thomas Renninger

Changes in V2:
- Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
- Use u32 instead of u64 for cpuid, state which is by far enough

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

and
power:power_frequency
is replaced with:
power:processor_frequency

power:machine_suspend
is newly introduced, a first implementation
comes from the ARM side, but it's easy to add these events in X86 as well if needed.

Quoted 6 lines

the type= field got removed from both, it was never used and the type is differed by the event type itself.

perf timechart
userspace tool gets adjusted in a separate patch.

#49 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 16 lines · Thomas Renninger

Changes in V2:
- Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
- Use u32 instead of u64 for cpuid, state which is by far enough

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Quoted 4 lines

and
power:power_frequency
is replaced with:
power:processor_frequency

Could you please name it power:cpu_idle and power:cpu_frequency instead, for shortness? We generally use 'cpu' in the kernel and for events.

Quoted 1 line

power:machine_suspend

How will future PCI (or other device) power saving tracepoints be called?

Might be more consistent to use:

power:cpu_idle
power:machine_idle
power:device_idle

Where machine_idle is the suspend event.

Quoted 4 lines

the type= field got removed from both, it was never used and the type is differed by the event type itself.

+#define PWR_EVENT_EXIT 0xFFFFFFFF

Shouldnt this be part of the POWER_ enum? (and you can write -1 there)

include/trace/events/power.h TRACE_EVENT(power_end, · line 143

Quoted 8 lines
include/trace/events/power.h +8 -0
+#ifndef _TRACE_POWER_ENUM_
+#define _TRACE_POWER_ENUM_
+enum {
+	POWER_NONE = 0,
+	POWER_CSTATE = 1,
+	POWER_PSTATE = 2,
+};
+#endif

Since we are cleaning up all these events, those enum definitions dont really look logical. For example, what is 'POWER_NONE'? Can a CPU have 'no power'?

Plus:

include/trace/events/power.h line 18

Quoted 9 lines
include/trace/events/power.h +9 -0
+DECLARE_EVENT_CLASS(processor,
+
+	TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+	TP_ARGS(state, cpu_id),
+
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+		__field(	u32,		cpu_id		)

Trace entries can carry a cpu_id of the current processor already. Can this cpu_id ever be different from that CPU id?

Quoted 16 lines
include/trace/events/power.h +16 -0
+	),
+
+	TP_fast_assign(
+		__entry->state = state;
+		__entry->cpu_id = cpu_id;
+	),
+
+	TP_printk("state=%lu cpu_id=%lu", (unsigned long)__entry->state,
+		  (unsigned long)__entry->cpu_id)
+);
+
+DEFINE_EVENT(processor, processor_idle,
+
+	TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+	     TP_ARGS(state, cpu_id)

include/trace/events/power.h line 44

Quoted 10 lines
include/trace/events/power.h +10 -0
+);
+
+#define PWR_EVENT_EXIT 0xFFFFFFFF
+
+DEFINE_EVENT(processor, processor_frequency,
+
+	TP_PROTO(unsigned int frequency, unsigned int cpu_id),
+
+	TP_ARGS(frequency, cpu_id)
+);

So, we have a 'state' field in the class, which is used as 'state' by the power::cpu_idle event, and as 'frequency' by the power::cpu_freq event?

Are there any architectures that track frequency in Hz, not in kHz? If yes, might there ever be a need for the frequency value to be larger than 4.29 GHz? If yes, then it wont fit into u32.

Also, might there be a future need to express different types of frequencies? For example, should we decide to track turbo frequencies in Intel CPUs, how would that be expressed via these events? Are there any architectures and CPUs that somehow have some extra attribute to the frequency value?

include/trace/events/power.h line 54

Quoted 9 lines
include/trace/events/power.h +9 -0
+TRACE_EVENT(machine_suspend,
+
+	TP_PROTO(unsigned int state),
+
+	TP_ARGS(state),
+
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+	),

Hm, this event is not used anywhere in the submitted patches. Where is the patch that adds usage, and what are the possible values for 'state'?

Ingo

#127 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 16 lines · Thomas Renninger

Changes in V2:
- Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
- Use u32 instead of u64 for cpuid, state which is by far enough

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Quoted 4 lines

and
power:power_frequency
is replaced with:
power:processor_frequency

Could you please name it power:cpu_idle and power:cpu_frequency instead, for shortness? We generally use 'cpu' in the kernel and for events.

Quoted 1 line

power:machine_suspend

How will future PCI (or other device) power saving tracepoints be called?

Might be more consistent to use:

power:cpu_idle
power:machine_idle
power:device_idle

Where machine_idle is the suspend event.

Quoted 4 lines

the type= field got removed from both, it was never used and the type is differed by the event type itself.

+#define PWR_EVENT_EXIT 0xFFFFFFFF

Shouldnt this be part of the POWER_ enum? (and you can write -1 there)

include/trace/events/power.h TRACE_EVENT(power_end, · line 143

Quoted 8 lines
include/trace/events/power.h +8 -0
+#ifndef _TRACE_POWER_ENUM_
+#define _TRACE_POWER_ENUM_
+enum {
+	POWER_NONE = 0,
+	POWER_CSTATE = 1,
+	POWER_PSTATE = 2,
+};
+#endif

Since we are cleaning up all these events, those enum definitions dont really look logical. For example, what is 'POWER_NONE'? Can a CPU have 'no power'?

Plus:

include/trace/events/power.h line 18

Quoted 9 lines
include/trace/events/power.h +9 -0
+DECLARE_EVENT_CLASS(processor,
+
+	TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+	TP_ARGS(state, cpu_id),
+
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+		__field(	u32,		cpu_id		)

Trace entries can carry a cpu_id of the current processor already. Can this cpu_id ever be different from that CPU id?

Quoted 16 lines
include/trace/events/power.h +16 -0
+	),
+
+	TP_fast_assign(
+		__entry->state = state;
+		__entry->cpu_id = cpu_id;
+	),
+
+	TP_printk("state=%lu cpu_id=%lu", (unsigned long)__entry->state,
+		  (unsigned long)__entry->cpu_id)
+);
+
+DEFINE_EVENT(processor, processor_idle,
+
+	TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+	     TP_ARGS(state, cpu_id)

include/trace/events/power.h line 44

Quoted 10 lines
include/trace/events/power.h +10 -0
+);
+
+#define PWR_EVENT_EXIT 0xFFFFFFFF
+
+DEFINE_EVENT(processor, processor_frequency,
+
+	TP_PROTO(unsigned int frequency, unsigned int cpu_id),
+
+	TP_ARGS(frequency, cpu_id)
+);

So, we have a 'state' field in the class, which is used as 'state' by the power::cpu_idle event, and as 'frequency' by the power::cpu_freq event?

Are there any architectures that track frequency in Hz, not in kHz? If yes, might there ever be a need for the frequency value to be larger than 4.29 GHz? If yes, then it wont fit into u32.

Also, might there be a future need to express different types of frequencies? For example, should we decide to track turbo frequencies in Intel CPUs, how would that be expressed via these events? Are there any architectures and CPUs that somehow have some extra attribute to the frequency value?

include/trace/events/power.h line 54

Quoted 9 lines
include/trace/events/power.h +9 -0
+TRACE_EVENT(machine_suspend,
+
+	TP_PROTO(unsigned int state),
+
+	TP_ARGS(state),
+
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+	),

Hm, this event is not used anywhere in the submitted patches. Where is the patch that adds usage, and what are the possible values for 'state'?

Ingo

#128 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 21 lines · Thomas Renninger

Changes in V2:
 - Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
 - Use u32 instead of u64 for cpuid, state which is by far enough

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
 power:power_start
 power:power_end
are replaced with:
 power:processor_idle

and
 power:power_frequency
is replaced with:
 power:processor_frequency

Quoted 4 lines

power:machine_suspend
is newly introduced, a first implementation
comes from the ARM side, but it's easy to add these events in X86 as well if needed.

This will come as a separate patch, which fits all platforms. Cf.
http://marc.info/?l=linux-omap&m=128620575300682&w=2.

...

arch/x86/kernel/process.c static void mwait_idle(void) · line 487

Quoted 5 lines
arch/x86/kernel/process.c +1 -0
@@ -481,10 +484,12 @@ static void mwait_idle(void)
  static void poll_idle(void)
  {
        trace_power_start(POWER_CSTATE, 0, smp_processor_id());
+       trace_processor_idle(1, smp_processor_id());

Should that be:
+ trace_processor_idle(0, smp_processor_id());
instead?
Since state '0' is for the CPU active in polling mode and PWR_EVENT_EXIT means 'exit from any idle state'.

Quoted 9 lines
arch/x86/kernel/process.c +2 -1
        local_irq_enable();
        while (!need_resched())
                cpu_relax();
-       trace_power_end(0);
+       trace_power_end(smp_processor_id());
+       trace_processor_idle(PWR_EVENT_EXIT, smp_processor_id());
  }
 
  /*

...

include/trace/events/power.h

Quoted 1 line
include/trace/events/power.h +0 -0

...

Regards,
Jean
--
To unsubscribe from this list: send the line "unsubscribe linux-trace-users" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#129 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 21 lines · Thomas Renninger

Changes in V2:
 - Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
 - Use u32 instead of u64 for cpuid, state which is by far enough

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
 power:power_start
 power:power_end
are replaced with:
 power:processor_idle

and
 power:power_frequency
is replaced with:
 power:processor_frequency

Quoted 4 lines

power:machine_suspend
is newly introduced, a first implementation
comes from the ARM side, but it's easy to add these events in X86 as well if needed.

This will come as a separate patch, which fits all platforms. Cf.
http://marc.info/?l=linux-omap&m=128620575300682&w=2.

...

arch/x86/kernel/process.c static void mwait_idle(void) · line 487

Quoted 5 lines
arch/x86/kernel/process.c +1 -0
@@ -481,10 +484,12 @@ static void mwait_idle(void)
  static void poll_idle(void)
  {
        trace_power_start(POWER_CSTATE, 0, smp_processor_id());
+       trace_processor_idle(1, smp_processor_id());

Should that be:
+ trace_processor_idle(0, smp_processor_id());
instead?
Since state '0' is for the CPU active in polling mode and PWR_EVENT_EXIT means 'exit from any idle state'.

Quoted 9 lines
arch/x86/kernel/process.c +2 -1
        local_irq_enable();
        while (!need_resched())
                cpu_relax();
-       trace_power_end(0);
+       trace_power_end(smp_processor_id());
+       trace_processor_idle(PWR_EVENT_EXIT, smp_processor_id());
  }
 
  /*

...

include/trace/events/power.h

Quoted 1 line
include/trace/events/power.h +0 -0

...

Regards,
Jean

#50 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Ingo Molnar

Ingo,

Quoted 6 lines · Ingo Molnar
Quoted 3 lines · Thomas Renninger

Changes in V2:
  - Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
  - Use u32 instead of u64 for cpuid, state which is by far enough

...

Quoted 5 lines
Quoted 2 lines

+#define PWR_EVENT_EXIT 0xFFFFFFFF

Shouldnt this be part of the POWER_ enum? (and you can write -1 there)

Quoted 11 lines

include/trace/events/power.h TRACE_EVENT(power_end, · line 143

Quoted 8 lines
include/trace/events/power.h +8 -0
+#ifndef _TRACE_POWER_ENUM_
+#define _TRACE_POWER_ENUM_
+enum {
+     POWER_NONE = 0,
+     POWER_CSTATE = 1,
+     POWER_PSTATE = 2,
+};
+#endif

Since we are cleaning up all these events, those enum definitions dont really look logical. For example, what is 'POWER_NONE'? Can a CPU have 'no power'?

The enum belongs to the deprecated API so I would rather not touch it. Keeping the deprecated code isolated will make it easier to remove later.

Quoted 15 lines

Plus:

include/trace/events/power.h line 18

Quoted 9 lines
include/trace/events/power.h +9 -0
+DECLARE_EVENT_CLASS(processor,
+
+     TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+     TP_ARGS(state, cpu_id),
+
+     TP_STRUCT__entry(
+             __field(        u32,            state           )
+             __field(        u32,            cpu_id          )

Trace entries can carry a cpu_id of the current processor already. Can this cpu_id ever be different from that CPU id?

I have no evidence of that (powr mgt on SMP ARM is coming real soon...) but one can imagine one of the CPUs being the master for PM decisions.

Quoted 40 lines
Quoted 17 lines

+     ),

include/trace/events/power.h +15 -0
+
+     TP_fast_assign(
+             __entry->state = state;
+             __entry->cpu_id = cpu_id;
+     ),
+
+     TP_printk("state=%lu cpu_id=%lu", (unsigned long)__entry->state,
+               (unsigned long)__entry->cpu_id)
+);
+
+DEFINE_EVENT(processor, processor_idle,
+
+     TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+          TP_ARGS(state, cpu_id)

include/trace/events/power.h line 44

Quoted 10 lines
include/trace/events/power.h +10 -0
+);
+
+#define PWR_EVENT_EXIT 0xFFFFFFFF
+
+DEFINE_EVENT(processor, processor_frequency,
+
+     TP_PROTO(unsigned int frequency, unsigned int cpu_id),
+
+     TP_ARGS(frequency, cpu_id)
+);

So, we have a 'state' field in the class, which is used as 'state' by the power::cpu_idle event, and as 'frequency' by the power::cpu_freq event?

Are there any architectures that track frequency in Hz, not in kHz? If yes, might there ever be a need for the frequency value to be larger than 4.29 GHz? If yes, then it wont fit into u32.

Also, might there be a future need to express different types of frequencies? For example, should we decide to track turbo frequencies in Intel CPUs, how would that be expressed via these events? Are there any architectures and CPUs that somehow have some extra attribute to the frequency value?

Quoted 12 lines

include/trace/events/power.h line 54

Quoted 9 lines
include/trace/events/power.h +9 -0
+TRACE_EVENT(machine_suspend,
+
+     TP_PROTO(unsigned int state),
+
+     TP_ARGS(state),
+
+     TP_STRUCT__entry(
+             __field(        u32,            state           )
+     ),

Hm, this event is not used anywhere in the submitted patches. Where is the patch that adds usage, and what are the possible values for 'state'?

This will come as a separate patch, which fits all platforms. Cf.
http://marc.info/?l=linux-omap&m=128620575300682&w=2.
The state field is of type suspend_state_t, cf. include/linux/suspend.h

Quoted 3 lines

       Ingo

Jean
--
To unsubscribe from this list: send the line "unsubscribe linux-trace-users" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#67 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Ingo Molnar

Ingo,

Quoted 6 lines · Ingo Molnar
Quoted 3 lines · Thomas Renninger

Changes in V2:
  - Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
  - Use u32 instead of u64 for cpuid, state which is by far enough

...

Quoted 5 lines
Quoted 2 lines

+#define PWR_EVENT_EXIT 0xFFFFFFFF

Shouldnt this be part of the POWER_ enum? (and you can write -1 there)

Quoted 11 lines

include/trace/events/power.h TRACE_EVENT(power_end, · line 143

Quoted 8 lines
include/trace/events/power.h +8 -0
+#ifndef _TRACE_POWER_ENUM_
+#define _TRACE_POWER_ENUM_
+enum {
+     POWER_NONE = 0,
+     POWER_CSTATE = 1,
+     POWER_PSTATE = 2,
+};
+#endif

Since we are cleaning up all these events, those enum definitions dont really look logical. For example, what is 'POWER_NONE'? Can a CPU have 'no power'?

The enum belongs to the deprecated API so I would rather not touch it. Keeping the deprecated code isolated will make it easier to remove later.

Quoted 15 lines

Plus:

include/trace/events/power.h line 18

Quoted 9 lines
include/trace/events/power.h +9 -0
+DECLARE_EVENT_CLASS(processor,
+
+     TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+     TP_ARGS(state, cpu_id),
+
+     TP_STRUCT__entry(
+             __field(        u32,            state           )
+             __field(        u32,            cpu_id          )

Trace entries can carry a cpu_id of the current processor already. Can this cpu_id ever be different from that CPU id?

I have no evidence of that (powr mgt on SMP ARM is coming real soon...) but one can imagine one of the CPUs being the master for PM decisions.

Quoted 40 lines
Quoted 17 lines

+     ),

include/trace/events/power.h +15 -0
+
+     TP_fast_assign(
+             __entry->state = state;
+             __entry->cpu_id = cpu_id;
+     ),
+
+     TP_printk("state=%lu cpu_id=%lu", (unsigned long)__entry->state,
+               (unsigned long)__entry->cpu_id)
+);
+
+DEFINE_EVENT(processor, processor_idle,
+
+     TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+          TP_ARGS(state, cpu_id)

include/trace/events/power.h line 44

Quoted 10 lines
include/trace/events/power.h +10 -0
+);
+
+#define PWR_EVENT_EXIT 0xFFFFFFFF
+
+DEFINE_EVENT(processor, processor_frequency,
+
+     TP_PROTO(unsigned int frequency, unsigned int cpu_id),
+
+     TP_ARGS(frequency, cpu_id)
+);

So, we have a 'state' field in the class, which is used as 'state' by the power::cpu_idle event, and as 'frequency' by the power::cpu_freq event?

Are there any architectures that track frequency in Hz, not in kHz? If yes, might there ever be a need for the frequency value to be larger than 4.29 GHz? If yes, then it wont fit into u32.

Also, might there be a future need to express different types of frequencies? For example, should we decide to track turbo frequencies in Intel CPUs, how would that be expressed via these events? Are there any architectures and CPUs that somehow have some extra attribute to the frequency value?

Quoted 12 lines

include/trace/events/power.h line 54

Quoted 9 lines
include/trace/events/power.h +9 -0
+TRACE_EVENT(machine_suspend,
+
+     TP_PROTO(unsigned int state),
+
+     TP_ARGS(state),
+
+     TP_STRUCT__entry(
+             __field(        u32,            state           )
+     ),

Hm, this event is not used anywhere in the submitted patches. Where is the patch that adds usage, and what are the possible values for 'state'?

This will come as a separate patch, which fits all platforms. Cf.
http://marc.info/?l=linux-omap&m=128620575300682&w=2.
The state field is of type suspend_state_t, cf. include/linux/suspend.h

Quoted 3 lines

       Ingo

Jean

#68 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Ingo Molnar
Quoted 6 lines · Ingo Molnar

+

include/trace/events/power.h line 18

Quoted 3 lines
include/trace/events/power.h +3 -0
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+		__field(	u32,		cpu_id		)

Trace entries can carry a cpu_id of the current processor already. Can this cpu_id ever be different from that CPU id?

yes

esp cpu frequency you can change cross cpu....

originally we did not have this in the API but Thomas added it for that reason some time ago.

#71 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Ingo Molnar
Quoted 6 lines · Ingo Molnar

+

include/trace/events/power.h line 18

Quoted 3 lines
include/trace/events/power.h +3 -0
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+		__field(	u32,		cpu_id		)

Trace entries can carry a cpu_id of the current processor already. Can this cpu_id ever be different from that CPU id?

yes

esp cpu frequency you can change cross cpu....

originally we did not have this in the API but Thomas added it for that reason some time ago.

#69 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Arjan van de Ven
Quoted 8 lines · Arjan van de Ven
Quoted 6 lines · Ingo Molnar

+

include/trace/events/power.h line 18

Quoted 3 lines
include/trace/events/power.h +3 -0
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+		__field(	u32,		cpu_id		)

Trace entries can carry a cpu_id of the current processor already. Can this cpu_id ever be different from that CPU id?

Quoted 6 lines

yes

esp cpu frequency you can change cross cpu....

originally we did not have this in the API but Thomas added it for that reason some time ago.

Ok, good! Maybe add this as a comment?

Thanks,

Ingo

#70 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Arjan van de Ven
Quoted 8 lines · Arjan van de Ven
Quoted 6 lines · Ingo Molnar

+

include/trace/events/power.h line 18

Quoted 3 lines
include/trace/events/power.h +3 -0
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+		__field(	u32,		cpu_id		)

Trace entries can carry a cpu_id of the current processor already. Can this cpu_id ever be different from that CPU id?

Quoted 6 lines

yes

esp cpu frequency you can change cross cpu....

originally we did not have this in the API but Thomas added it for that reason some time ago.

Ok, good! Maybe add this as a comment?

Thanks,

Ingo

#72 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Ingo Molnar
Quoted 19 lines · On Tuesday 26 October 2010 09:10:20 Ingo Molnar wrote:
Quoted 16 lines · Thomas Renninger

Changes in V2:
- Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
- Use u32 instead of u64 for cpuid, state which is by far enough

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Quoted 7 lines
Quoted 4 lines

and
power:power_frequency
is replaced with:
power:processor_frequency

Could you please name it power:cpu_idle and power:cpu_frequency instead, for shortness? We generally use 'cpu' in the kernel and for events.

Sure.

Quoted 10 lines
Quoted 2 lines

power:machine_suspend

How will future PCI (or other device) power saving tracepoints be called?

Might be more consistent to use:

power:cpu_idle
power:machine_idle
power:device_idle

device idle is not true. Those may be low power modes like reduced network throughput, reduced wlan range, the device needs not to be idle.
Device power states is probably the most complex area, if such a thing gets introduced, it should makes sense to state the interface experimental for some time until a wider range of devices uses it (in contrast to these new ones
which should not change that soon anymore...).

Also machine_idle may be true, but machine_suspend sounds more familiar and everyone immediately knows what the event is about.

-> *_idle convention is not really worth it.

Quoted 1 line

Where machine_idle is the suspend event.

Here you name it. You talk about machine_idle but you mean the suspend event, better just name it what it is.

Quoted 6 lines
Quoted 4 lines

the type= field got removed from both, it was never used and the type is differed by the event type itself.

+#define PWR_EVENT_EXIT 0xFFFFFFFF

Shouldnt this be part of the POWER_ enum? (and you can write -1 there)

No, below enum will vanish, but -1 is nicer.

...

Quoted 14 lines

Plus:

include/trace/events/power.h line 18

Quoted 9 lines
include/trace/events/power.h +9 -0
+DECLARE_EVENT_CLASS(processor,
+
+	TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+	TP_ARGS(state, cpu_id),
+
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+		__field(	u32,		cpu_id		)

Trace entries can carry a cpu_id of the current processor already. Can this cpu_id ever be different from that CPU id?

Yes. A core's frequency can depend on another one which will get switched as well (by one command/MSR). Compare with commit 6f4f2723d08534fd4e407e1e.

This can theoretically also be the case for sleep states. Afaik such HW does not exist yet, but ACPI spec already provides interfaces to pass these dependency from BIOS to OS. -> We want a stable ABI and should be prepared for such stuff.

Quoted 21 lines
Quoted 21 lines
include/trace/events/power.h +21 -0
+	),
+
+	TP_fast_assign(
+		__entry->state = state;
+		__entry->cpu_id = cpu_id;
+	),
+
+	TP_printk("state=%lu cpu_id=%lu", (unsigned long)__entry->state,
+		  (unsigned long)__entry->cpu_id)
+);
+
+DEFINE_EVENT(processor, processor_idle,
+
+	TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+	     TP_ARGS(state, cpu_id)
+);
+
+#define PWR_EVENT_EXIT 0xFFFFFFFF
+
+DEFINE_EVENT(processor, processor_frequency,
Quoted 8 lines

include/trace/events/power.h line 44

Quoted 5 lines
include/trace/events/power.h +5 -0
+
+	TP_PROTO(unsigned int frequency, unsigned int cpu_id),
+
+	TP_ARGS(frequency, cpu_id)
+);

So, we have a 'state' field in the class, which is used as 'state' by the power::cpu_idle event, and as 'frequency' by the power::cpu_freq event?

Yes, is this a problem?
Definitions are a bit shorter having one power processor class. As "frequency" is stated in frequency event definition everything should be obvious and this one looks like the more elegant way to me.

Quoted 3 lines

Are there any architectures that track frequency in Hz, not in kHz? If yes, might there ever be a need for the frequency value to be larger than 4.29 GHz? If yes, then it wont fit into u32.

drivers/cpufreq subsystem is fixed to unsigned int (cmp. include/linux/cpufreq.h):

        unsigned int            min;    /* in kHz */
        unsigned int            max;    /* in kHz */
        unsigned int            cur;    /* in kHz,
        ...

that should be fine.

Quoted 4 lines

Also, might there be a future need to express different types of frequencies? For example, should we decide to track turbo frequencies in Intel CPUs, how would that be expressed via these events? Are there any architectures and CPUs that somehow have some extra attribute to the frequency value?

I wonder whether this ever can/will work in a sane way. Userspace can compare with:
/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq everything above is turbo. So I do not think it's ever needed. But adding an additional value at the end does not violate userspace compatibility. This has been done with the cpuid as well.

Quoted 4 lines
Quoted 4 lines
include/trace/events/power.h +4 -0
+TRACE_EVENT(machine_suspend,
+
+	TP_PROTO(unsigned int state),
+
Quoted 8 lines

include/trace/events/power.h line 54

Quoted 5 lines
include/trace/events/power.h +5 -0
+	TP_ARGS(state),
+
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+	),

Hm, this event is not used anywhere in the submitted patches. Where is the patch that adds usage, and what are the possible values for 'state'?

Jean wants to make use of it on ARM.
I also had patch for x86, I can have another look at it, Rafael already gave me a comment on it. But on X86 you typically realize when you suspend the machine (could imagine this is more useful on ARM driven mobile phones and similar), still I can add it..

Values probably should be (include/linux/suspend.h):

#define PM_SUSPEND_ON       0
#define PM_SUSPEND_STANDBY  1
#define PM_SUSPEND_MEM      3
#define PM_SUSPEND_MAX      4

How this strange state Arjan talked about is passed is up to these guys. Instead of using 0 and above pre-defined such arch specific special states better should get passed by:

#define X86_MOORESTOWN_STANDBY_S0   0x100
..                                  0x101
#define ARM_WHATEVER_STRANGE_THING  0x200

...

Thomas

#77 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Ingo Molnar
Quoted 19 lines · On Tuesday 26 October 2010 09:10:20 Ingo Molnar wrote:
Quoted 16 lines · Thomas Renninger

Changes in V2:
- Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
- Use u32 instead of u64 for cpuid, state which is by far enough

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Quoted 7 lines
Quoted 4 lines

and
power:power_frequency
is replaced with:
power:processor_frequency

Could you please name it power:cpu_idle and power:cpu_frequency instead, for shortness? We generally use 'cpu' in the kernel and for events.

Sure.

Quoted 10 lines
Quoted 2 lines

power:machine_suspend

How will future PCI (or other device) power saving tracepoints be called?

Might be more consistent to use:

power:cpu_idle
power:machine_idle
power:device_idle

device idle is not true. Those may be low power modes like reduced network throughput, reduced wlan range, the device needs not to be idle.
Device power states is probably the most complex area, if such a thing gets introduced, it should makes sense to state the interface experimental for some time until a wider range of devices uses it (in contrast to these new ones
which should not change that soon anymore...).

Also machine_idle may be true, but machine_suspend sounds more familiar and everyone immediately knows what the event is about.

-> *_idle convention is not really worth it.

Quoted 1 line

Where machine_idle is the suspend event.

Here you name it. You talk about machine_idle but you mean the suspend event, better just name it what it is.

Quoted 6 lines
Quoted 4 lines

the type= field got removed from both, it was never used and the type is differed by the event type itself.

+#define PWR_EVENT_EXIT 0xFFFFFFFF

Shouldnt this be part of the POWER_ enum? (and you can write -1 there)

No, below enum will vanish, but -1 is nicer.

...

Quoted 14 lines

Plus:

include/trace/events/power.h line 18

Quoted 9 lines
include/trace/events/power.h +9 -0
+DECLARE_EVENT_CLASS(processor,
+
+	TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+	TP_ARGS(state, cpu_id),
+
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+		__field(	u32,		cpu_id		)

Trace entries can carry a cpu_id of the current processor already. Can this cpu_id ever be different from that CPU id?

Yes. A core's frequency can depend on another one which will get switched as well (by one command/MSR). Compare with commit 6f4f2723d08534fd4e407e1e.

This can theoretically also be the case for sleep states. Afaik such HW does not exist yet, but ACPI spec already provides interfaces to pass these dependency from BIOS to OS. -> We want a stable ABI and should be prepared for such stuff.

Quoted 21 lines
Quoted 21 lines
include/trace/events/power.h +21 -0
+	),
+
+	TP_fast_assign(
+		__entry->state = state;
+		__entry->cpu_id = cpu_id;
+	),
+
+	TP_printk("state=%lu cpu_id=%lu", (unsigned long)__entry->state,
+		  (unsigned long)__entry->cpu_id)
+);
+
+DEFINE_EVENT(processor, processor_idle,
+
+	TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+	     TP_ARGS(state, cpu_id)
+);
+
+#define PWR_EVENT_EXIT 0xFFFFFFFF
+
+DEFINE_EVENT(processor, processor_frequency,
Quoted 8 lines

include/trace/events/power.h line 44

Quoted 5 lines
include/trace/events/power.h +5 -0
+
+	TP_PROTO(unsigned int frequency, unsigned int cpu_id),
+
+	TP_ARGS(frequency, cpu_id)
+);

So, we have a 'state' field in the class, which is used as 'state' by the power::cpu_idle event, and as 'frequency' by the power::cpu_freq event?

Yes, is this a problem?
Definitions are a bit shorter having one power processor class. As "frequency" is stated in frequency event definition everything should be obvious and this one looks like the more elegant way to me.

Quoted 3 lines

Are there any architectures that track frequency in Hz, not in kHz? If yes, might there ever be a need for the frequency value to be larger than 4.29 GHz? If yes, then it wont fit into u32.

drivers/cpufreq subsystem is fixed to unsigned int (cmp. include/linux/cpufreq.h):

        unsigned int            min;    /* in kHz */
        unsigned int            max;    /* in kHz */
        unsigned int            cur;    /* in kHz,
        ...

that should be fine.

Quoted 4 lines

Also, might there be a future need to express different types of frequencies? For example, should we decide to track turbo frequencies in Intel CPUs, how would that be expressed via these events? Are there any architectures and CPUs that somehow have some extra attribute to the frequency value?

I wonder whether this ever can/will work in a sane way. Userspace can compare with:
/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq everything above is turbo. So I do not think it's ever needed. But adding an additional value at the end does not violate userspace compatibility. This has been done with the cpuid as well.

Quoted 4 lines
Quoted 4 lines
include/trace/events/power.h +4 -0
+TRACE_EVENT(machine_suspend,
+
+	TP_PROTO(unsigned int state),
+
Quoted 8 lines

include/trace/events/power.h line 54

Quoted 5 lines
include/trace/events/power.h +5 -0
+	TP_ARGS(state),
+
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+	),

Hm, this event is not used anywhere in the submitted patches. Where is the patch that adds usage, and what are the possible values for 'state'?

Jean wants to make use of it on ARM.
I also had patch for x86, I can have another look at it, Rafael already gave me a comment on it. But on X86 you typically realize when you suspend the machine (could imagine this is more useful on ARM driven mobile phones and similar), still I can add it..

Values probably should be (include/linux/suspend.h):

#define PM_SUSPEND_ON       0
#define PM_SUSPEND_STANDBY  1
#define PM_SUSPEND_MEM      3
#define PM_SUSPEND_MAX      4

How this strange state Arjan talked about is passed is up to these guys. Instead of using 0 and above pre-defined such arch specific special states better should get passed by:

#define X86_MOORESTOWN_STANDBY_S0   0x100
..                                  0x101
#define ARM_WHATEVER_STRANGE_THING  0x200

...

Thomas

#73 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 39 lines · Thomas Renninger
Quoted 19 lines · On Tuesday 26 October 2010 09:10:20 Ingo Molnar wrote:
Quoted 16 lines · Thomas Renninger

Changes in V2:
- Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
- Use u32 instead of u64 for cpuid, state which is by far enough

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Quoted 7 lines
Quoted 4 lines

and
power:power_frequency
is replaced with:
power:processor_frequency

Could you please name it power:cpu_idle and power:cpu_frequency instead, for shortness? We generally use 'cpu' in the kernel and for events.

Sure.

Quoted 10 lines
Quoted 2 lines

power:machine_suspend

How will future PCI (or other device) power saving tracepoints be called?

Might be more consistent to use:

power:cpu_idle
power:machine_idle
power:device_idle

Quoted 8 lines

device idle is not true. Those may be low power modes like reduced network throughput, reduced wlan range, the device needs not to be idle.
Device power states is probably the most complex area, if such a thing gets introduced, it should makes sense to state the interface experimental for some time until a wider range of devices uses it (in contrast to these new ones
which should not change that soon anymore...).

Ok.

Quoted 2 lines

Also machine_idle may be true, but machine_suspend sounds more familiar and everyone immediately knows what the event is about.

Ok - fair enough.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+#define PWR_EVENT_EXIT 0xFFFFFFFF

Shouldnt this be part of the POWER_ enum? (and you can write -1 there)

No, below enum will vanish, but -1 is nicer.

When it vanishes what will replace it?

Quoted 66 lines

...

Quoted 14 lines

Plus:

include/trace/events/power.h line 18

Quoted 9 lines
include/trace/events/power.h +9 -0
+DECLARE_EVENT_CLASS(processor,
+
+	TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+	TP_ARGS(state, cpu_id),
+
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+		__field(	u32,		cpu_id		)

Trace entries can carry a cpu_id of the current processor already. Can this cpu_id ever be different from that CPU id?

Yes. A core's frequency can depend on another one which will get switched as well (by one command/MSR). Compare with commit 6f4f2723d08534fd4e407e1e.

This can theoretically also be the case for sleep states. Afaik such HW does not exist yet, but ACPI spec already provides interfaces to pass these dependency from BIOS to OS. -> We want a stable ABI and should be prepared for such stuff.

Quoted 21 lines
Quoted 21 lines
include/trace/events/power.h +21 -0
+	),
+
+	TP_fast_assign(
+		__entry->state = state;
+		__entry->cpu_id = cpu_id;
+	),
+
+	TP_printk("state=%lu cpu_id=%lu", (unsigned long)__entry->state,
+		  (unsigned long)__entry->cpu_id)
+);
+
+DEFINE_EVENT(processor, processor_idle,
+
+	TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+	     TP_ARGS(state, cpu_id)
+);
+
+#define PWR_EVENT_EXIT 0xFFFFFFFF
+
+DEFINE_EVENT(processor, processor_frequency,
Quoted 8 lines

include/trace/events/power.h line 44

Quoted 5 lines
include/trace/events/power.h +5 -0
+
+	TP_PROTO(unsigned int frequency, unsigned int cpu_id),
+
+	TP_ARGS(frequency, cpu_id)
+);

So, we have a 'state' field in the class, which is used as 'state' by the power::cpu_idle event, and as 'frequency' by the power::cpu_freq event?

Yes, is this a problem?

Definitions are a bit shorter having one power processor class. As "frequency" is stated in frequency event definition everything should be obvious and this one looks like the more elegant way to me.

Quoted 3 lines

Are there any architectures that track frequency in Hz, not in kHz? If yes, might there ever be a need for the frequency value to be larger than 4.29 GHz? If yes, then it wont fit into u32.

Quoted 6 lines

drivers/cpufreq subsystem is fixed to unsigned int (cmp. include/linux/cpufreq.h):

        unsigned int            min;    /* in kHz */
        unsigned int            max;    /* in kHz */
        unsigned int            cur;    /* in kHz,
        ...

that should be fine.

ok, good - so we should be fine up to 4 THz CPUs.

Quoted 38 lines
Quoted 4 lines

Also, might there be a future need to express different types of frequencies? For example, should we decide to track turbo frequencies in Intel CPUs, how would that be expressed via these events? Are there any architectures and CPUs that somehow have some extra attribute to the frequency value?

I wonder whether this ever can/will work in a sane way. Userspace can compare with:
/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq everything above is turbo. So I do not think it's ever needed. But adding an additional value at the end does not violate userspace compatibility. This has been done with the cpuid as well.

Quoted 4 lines
Quoted 4 lines
include/trace/events/power.h +4 -0
+TRACE_EVENT(machine_suspend,
+
+	TP_PROTO(unsigned int state),
+
Quoted 8 lines

include/trace/events/power.h line 54

Quoted 5 lines
include/trace/events/power.h +5 -0
+	TP_ARGS(state),
+
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+	),

Hm, this event is not used anywhere in the submitted patches. Where is the patch that adds usage, and what are the possible values for 'state'?

Jean wants to make use of it on ARM.
I also had patch for x86, I can have another look at it, Rafael already gave me a comment on it. But on X86 you typically realize when you suspend the machine (could imagine this is more useful on ARM driven mobile phones and similar), still I can add it..

Values probably should be (include/linux/suspend.h):

#define PM_SUSPEND_ON       0
#define PM_SUSPEND_STANDBY  1
#define PM_SUSPEND_MEM      3
#define PM_SUSPEND_MAX      4
Quoted 8 lines

How this strange state Arjan talked about is passed is up to these guys. Instead of using 0 and above pre-defined such arch specific special states better should get passed by:

#define X86_MOORESTOWN_STANDBY_S0   0x100
..                                  0x101
#define ARM_WHATEVER_STRANGE_THING  0x200

...

I'd rather like to see a meaningful name to be given to these states and them being passed, instead of weird platform specific things. Tooling will try to be as generic as possible.

I dont know this stuff, but making a distinction between s2ram and s2disk events would seem meaningful.

Thanks,

Ingo

#76 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 39 lines · Thomas Renninger
Quoted 19 lines · On Tuesday 26 October 2010 09:10:20 Ingo Molnar wrote:
Quoted 16 lines · Thomas Renninger

Changes in V2:
- Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
- Use u32 instead of u64 for cpuid, state which is by far enough

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Quoted 7 lines
Quoted 4 lines

and
power:power_frequency
is replaced with:
power:processor_frequency

Could you please name it power:cpu_idle and power:cpu_frequency instead, for shortness? We generally use 'cpu' in the kernel and for events.

Sure.

Quoted 10 lines
Quoted 2 lines

power:machine_suspend

How will future PCI (or other device) power saving tracepoints be called?

Might be more consistent to use:

power:cpu_idle
power:machine_idle
power:device_idle

Quoted 8 lines

device idle is not true. Those may be low power modes like reduced network throughput, reduced wlan range, the device needs not to be idle.
Device power states is probably the most complex area, if such a thing gets introduced, it should makes sense to state the interface experimental for some time until a wider range of devices uses it (in contrast to these new ones
which should not change that soon anymore...).

Ok.

Quoted 2 lines

Also machine_idle may be true, but machine_suspend sounds more familiar and everyone immediately knows what the event is about.

Ok - fair enough.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+#define PWR_EVENT_EXIT 0xFFFFFFFF

Shouldnt this be part of the POWER_ enum? (and you can write -1 there)

No, below enum will vanish, but -1 is nicer.

When it vanishes what will replace it?

Quoted 66 lines

...

Quoted 14 lines

Plus:

include/trace/events/power.h line 18

Quoted 9 lines
include/trace/events/power.h +9 -0
+DECLARE_EVENT_CLASS(processor,
+
+	TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+	TP_ARGS(state, cpu_id),
+
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+		__field(	u32,		cpu_id		)

Trace entries can carry a cpu_id of the current processor already. Can this cpu_id ever be different from that CPU id?

Yes. A core's frequency can depend on another one which will get switched as well (by one command/MSR). Compare with commit 6f4f2723d08534fd4e407e1e.

This can theoretically also be the case for sleep states. Afaik such HW does not exist yet, but ACPI spec already provides interfaces to pass these dependency from BIOS to OS. -> We want a stable ABI and should be prepared for such stuff.

Quoted 21 lines
Quoted 21 lines
include/trace/events/power.h +21 -0
+	),
+
+	TP_fast_assign(
+		__entry->state = state;
+		__entry->cpu_id = cpu_id;
+	),
+
+	TP_printk("state=%lu cpu_id=%lu", (unsigned long)__entry->state,
+		  (unsigned long)__entry->cpu_id)
+);
+
+DEFINE_EVENT(processor, processor_idle,
+
+	TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+	     TP_ARGS(state, cpu_id)
+);
+
+#define PWR_EVENT_EXIT 0xFFFFFFFF
+
+DEFINE_EVENT(processor, processor_frequency,
Quoted 8 lines

include/trace/events/power.h line 44

Quoted 5 lines
include/trace/events/power.h +5 -0
+
+	TP_PROTO(unsigned int frequency, unsigned int cpu_id),
+
+	TP_ARGS(frequency, cpu_id)
+);

So, we have a 'state' field in the class, which is used as 'state' by the power::cpu_idle event, and as 'frequency' by the power::cpu_freq event?

Yes, is this a problem?

Definitions are a bit shorter having one power processor class. As "frequency" is stated in frequency event definition everything should be obvious and this one looks like the more elegant way to me.

Quoted 3 lines

Are there any architectures that track frequency in Hz, not in kHz? If yes, might there ever be a need for the frequency value to be larger than 4.29 GHz? If yes, then it wont fit into u32.

Quoted 6 lines

drivers/cpufreq subsystem is fixed to unsigned int (cmp. include/linux/cpufreq.h):

        unsigned int            min;    /* in kHz */
        unsigned int            max;    /* in kHz */
        unsigned int            cur;    /* in kHz,
        ...

that should be fine.

ok, good - so we should be fine up to 4 THz CPUs.

Quoted 38 lines
Quoted 4 lines

Also, might there be a future need to express different types of frequencies? For example, should we decide to track turbo frequencies in Intel CPUs, how would that be expressed via these events? Are there any architectures and CPUs that somehow have some extra attribute to the frequency value?

I wonder whether this ever can/will work in a sane way. Userspace can compare with:
/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq everything above is turbo. So I do not think it's ever needed. But adding an additional value at the end does not violate userspace compatibility. This has been done with the cpuid as well.

Quoted 4 lines
Quoted 4 lines
include/trace/events/power.h +4 -0
+TRACE_EVENT(machine_suspend,
+
+	TP_PROTO(unsigned int state),
+
Quoted 8 lines

include/trace/events/power.h line 54

Quoted 5 lines
include/trace/events/power.h +5 -0
+	TP_ARGS(state),
+
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+	),

Hm, this event is not used anywhere in the submitted patches. Where is the patch that adds usage, and what are the possible values for 'state'?

Jean wants to make use of it on ARM.
I also had patch for x86, I can have another look at it, Rafael already gave me a comment on it. But on X86 you typically realize when you suspend the machine (could imagine this is more useful on ARM driven mobile phones and similar), still I can add it..

Values probably should be (include/linux/suspend.h):

#define PM_SUSPEND_ON       0
#define PM_SUSPEND_STANDBY  1
#define PM_SUSPEND_MEM      3
#define PM_SUSPEND_MAX      4
Quoted 8 lines

How this strange state Arjan talked about is passed is up to these guys. Instead of using 0 and above pre-defined such arch specific special states better should get passed by:

#define X86_MOORESTOWN_STANDBY_S0   0x100
..                                  0x101
#define ARM_WHATEVER_STRANGE_THING  0x200

...

I'd rather like to see a meaningful name to be given to these states and them being passed, instead of weird platform specific things. Tooling will try to be as generic as possible.

I dont know this stuff, but making a distinction between s2ram and s2disk events would seem meaningful.

Thanks,

Ingo

#51 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Jean Pihet
Quoted 29 lines · Jean Pihet

Ingo,

Quoted 6 lines · Ingo Molnar
Quoted 3 lines · Thomas Renninger

Changes in V2:
  - Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
  - Use u32 instead of u64 for cpuid, state which is by far enough

...

Quoted 5 lines
Quoted 2 lines

+#define PWR_EVENT_EXIT 0xFFFFFFFF

Shouldnt this be part of the POWER_ enum? (and you can write -1 there)

Quoted 11 lines

include/trace/events/power.h TRACE_EVENT(power_end, · line 143

Quoted 8 lines
include/trace/events/power.h +8 -0
+#ifndef _TRACE_POWER_ENUM_
+#define _TRACE_POWER_ENUM_
+enum {
+     POWER_NONE = 0,
+     POWER_CSTATE = 1,
+     POWER_PSTATE = 2,
+};
+#endif

Since we are cleaning up all these events, those enum definitions dont really look logical. For example, what is 'POWER_NONE'? Can a CPU have 'no power'?

Quoted 3 lines

The enum belongs to the deprecated API so I would rather not touch it. Keeping the deprecated code isolated will make it easier to remove later.

So what will replace it? We still have a state field.

Passing in platform specific codes is a step backwards.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
include/trace/events/power.h +4 -0
+TRACE_EVENT(machine_suspend,
+
+     TP_PROTO(unsigned int state),
+
Quoted 8 lines
Quoted 5 lines

+     TP_ARGS(state),
+
+     TP_STRUCT__entry(
+             __field(        u32,            state           )
+     ),

Hm, this event is not used anywhere in the submitted patches. Where is the patch that adds usage, and what are the possible values for 'state'?

Quoted 3 lines

This will come as a separate patch, which fits all platforms. Cf.
http://marc.info/?l=linux-omap&m=128620575300682&w=2.
The state field is of type suspend_state_t, cf. include/linux/suspend.h

Ok, that's at least generic. Needs the review of Rafael, to determine whether this state value is all we want to know when we enter suspend.

Thanks,

Ingo

#52 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Jean Pihet
Quoted 29 lines · Jean Pihet

Ingo,

Quoted 6 lines · Ingo Molnar
Quoted 3 lines · Thomas Renninger

Changes in V2:
  - Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
  - Use u32 instead of u64 for cpuid, state which is by far enough

...

Quoted 5 lines
Quoted 2 lines

+#define PWR_EVENT_EXIT 0xFFFFFFFF

Shouldnt this be part of the POWER_ enum? (and you can write -1 there)

Quoted 11 lines

include/trace/events/power.h TRACE_EVENT(power_end, · line 143

Quoted 8 lines
include/trace/events/power.h +8 -0
+#ifndef _TRACE_POWER_ENUM_
+#define _TRACE_POWER_ENUM_
+enum {
+     POWER_NONE = 0,
+     POWER_CSTATE = 1,
+     POWER_PSTATE = 2,
+};
+#endif

Since we are cleaning up all these events, those enum definitions dont really look logical. For example, what is 'POWER_NONE'? Can a CPU have 'no power'?

Quoted 3 lines

The enum belongs to the deprecated API so I would rather not touch it. Keeping the deprecated code isolated will make it easier to remove later.

So what will replace it? We still have a state field.

Passing in platform specific codes is a step backwards.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
include/trace/events/power.h +4 -0
+TRACE_EVENT(machine_suspend,
+
+     TP_PROTO(unsigned int state),
+
Quoted 8 lines
Quoted 5 lines

+     TP_ARGS(state),
+
+     TP_STRUCT__entry(
+             __field(        u32,            state           )
+     ),

Hm, this event is not used anywhere in the submitted patches. Where is the patch that adds usage, and what are the possible values for 'state'?

Quoted 3 lines

This will come as a separate patch, which fits all platforms. Cf.
http://marc.info/?l=linux-omap&m=128620575300682&w=2.
The state field is of type suspend_state_t, cf. include/linux/suspend.h

Ok, that's at least generic. Needs the review of Rafael, to determine whether this state value is all we want to know when we enter suspend.

Thanks,

Ingo
--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#53 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

Quoted 2 lines · On Tuesday 26 October 2010 13:21:29 Ingo Molnar wrote:

* Jean Pihet <[email protected]> wrote:

..

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

include/trace/events/power.h TRACE_EVENT(power_end, · line 143

Quoted 8 lines
include/trace/events/power.h +8 -0
+#ifndef _TRACE_POWER_ENUM_
+#define _TRACE_POWER_ENUM_
+enum {
+     POWER_NONE = 0,
+     POWER_CSTATE = 1,
+     POWER_PSTATE = 2,
+};
+#endif

Since we are cleaning up all these events, those enum definitions dont really look logical. For example, what is 'POWER_NONE'? Can a CPU have 'no power'?

Quoted 5 lines
Quoted 3 lines

The enum belongs to the deprecated API so I would rather not touch it. Keeping the deprecated code isolated will make it easier to remove later.

So what will replace it? We still have a state field.

Nothing, this is part of the cleanup.
As you state above: POWER_NONE does not make sense at all. The whole thing (type= attribute that vanishes now) is passed to userspace, but never gets used there because the same info is in the event name:
cpu_frequency <-> frequency_switch <-> PSTATE
cpu_idle <-> power_start/power_end <-> CSTATE

I expect that there was an initial power_start/end which was also used for frequency switching.
Then it got realized that _start/_end does not work out and frequency_switch got introduced.
To stay compatible the whole power_start/end was not renamed to cpu_idle and the type= field was kept.

This is a guess without even looking at the git history. Therefore my partly harsh comments about the sanity of the current power tracing events.

Quoted 15 lines

Passing in platform specific codes is a step backwards.

Quoted 13 lines
Quoted 12 lines

include/trace/events/power.h line 54

Quoted 9 lines
include/trace/events/power.h +9 -0
+TRACE_EVENT(machine_suspend,
+
+     TP_PROTO(unsigned int state),
+
+     TP_ARGS(state),
+
+     TP_STRUCT__entry(
+             __field(        u32,            state           )
+     ),

Hm, this event is not used anywhere in the submitted patches. Where is the patch that adds usage, and what are the possible values for 'state'?

Quoted 6 lines
Quoted 3 lines

This will come as a separate patch, which fits all platforms. Cf.
http://marc.info/?l=linux-omap&m=128620575300682&w=2.
The state field is of type suspend_state_t, cf. include/linux/suspend.h

Ok, that's at least generic. Needs the review of Rafael, to determine whether this state value is all we want to know when we enter suspend.

He already gave an acked-by on this generic one here:
Re: [PATCH 3/4] perf: add calls to suspend trace point
Oh now, that was on the X86 specific part which depends on this one. One should expect that he's fine with the generic part as well then, but I agree that he should definitely have a look at this and sign it off.

So as they got signed-off already, I'll send the X86 suspend events on top, once I find these in a tree...

Thomas

#66 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Ingo Molnar
Quoted 2 lines · On Tuesday 26 October 2010 13:21:29 Ingo Molnar wrote:

* Jean Pihet <[email protected]> wrote:

..

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

include/trace/events/power.h TRACE_EVENT(power_end, · line 143

Quoted 8 lines
include/trace/events/power.h +8 -0
+#ifndef _TRACE_POWER_ENUM_
+#define _TRACE_POWER_ENUM_
+enum {
+     POWER_NONE = 0,
+     POWER_CSTATE = 1,
+     POWER_PSTATE = 2,
+};
+#endif

Since we are cleaning up all these events, those enum definitions dont really look logical. For example, what is 'POWER_NONE'? Can a CPU have 'no power'?

Quoted 5 lines
Quoted 3 lines

The enum belongs to the deprecated API so I would rather not touch it. Keeping the deprecated code isolated will make it easier to remove later.

So what will replace it? We still have a state field.

Nothing, this is part of the cleanup.
As you state above: POWER_NONE does not make sense at all. The whole thing (type= attribute that vanishes now) is passed to userspace, but never gets used there because the same info is in the event name:
cpu_frequency <-> frequency_switch <-> PSTATE
cpu_idle <-> power_start/power_end <-> CSTATE

I expect that there was an initial power_start/end which was also used for frequency switching.
Then it got realized that _start/_end does not work out and frequency_switch got introduced.
To stay compatible the whole power_start/end was not renamed to cpu_idle and the type= field was kept.

This is a guess without even looking at the git history. Therefore my partly harsh comments about the sanity of the current power tracing events.

Quoted 15 lines

Passing in platform specific codes is a step backwards.

Quoted 13 lines
Quoted 12 lines

include/trace/events/power.h line 54

Quoted 9 lines
include/trace/events/power.h +9 -0
+TRACE_EVENT(machine_suspend,
+
+     TP_PROTO(unsigned int state),
+
+     TP_ARGS(state),
+
+     TP_STRUCT__entry(
+             __field(        u32,            state           )
+     ),

Hm, this event is not used anywhere in the submitted patches. Where is the patch that adds usage, and what are the possible values for 'state'?

Quoted 6 lines
Quoted 3 lines

This will come as a separate patch, which fits all platforms. Cf.
http://marc.info/?l=linux-omap&m=128620575300682&w=2.
The state field is of type suspend_state_t, cf. include/linux/suspend.h

Ok, that's at least generic. Needs the review of Rafael, to determine whether this state value is all we want to know when we enter suspend.

He already gave an acked-by on this generic one here:
Re: [PATCH 3/4] perf: add calls to suspend trace point
Oh now, that was on the X86 specific part which depends on this one. One should expect that he's fine with the generic part as well then, but I agree that he should definitely have a look at this and sign it off.

So as they got signed-off already, I'll send the X86 suspend events on top, once I find these in a tree...

Thomas

#54 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 16 lines · Thomas Renninger
Quoted 2 lines · On Tuesday 26 October 2010 13:21:29 Ingo Molnar wrote:

* Jean Pihet <[email protected]> wrote:

..

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

include/trace/events/power.h TRACE_EVENT(power_end, · line 143

Quoted 8 lines
include/trace/events/power.h +8 -0
+#ifndef _TRACE_POWER_ENUM_
+#define _TRACE_POWER_ENUM_
+enum {
+     POWER_NONE = 0,
+     POWER_CSTATE = 1,
+     POWER_PSTATE = 2,
+};
+#endif

Since we are cleaning up all these events, those enum definitions dont really look logical. For example, what is 'POWER_NONE'? Can a CPU have 'no power'?

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

The enum belongs to the deprecated API so I would rather not touch it. Keeping the deprecated code isolated will make it easier to remove later.

So what will replace it? We still have a state field.

Nothing, this is part of the cleanup.

I mean, what will go into the state field of the power:cpu_idle event?

Quoted 6 lines

As you state above: POWER_NONE does not make sense at all. The whole thing (type= attribute that vanishes now) is passed to userspace, but never gets used there because the same info is in the event name:
cpu_frequency <-> frequency_switch <-> PSTATE
cpu_idle <-> power_start/power_end <-> CSTATE

Ah, i see, so this 'state' enum went into the type field.

So my question is, and ignore this particular enum for now, what values go into the state field, which field is still kept in the new events as well.

[ I'd like to avoid us having to define a third set of power events a few years down
the road ;-) ]

Thanks,

Ingo

#59 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 16 lines · Thomas Renninger
Quoted 2 lines · On Tuesday 26 October 2010 13:21:29 Ingo Molnar wrote:

* Jean Pihet <[email protected]> wrote:

..

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

include/trace/events/power.h TRACE_EVENT(power_end, · line 143

Quoted 8 lines
include/trace/events/power.h +8 -0
+#ifndef _TRACE_POWER_ENUM_
+#define _TRACE_POWER_ENUM_
+enum {
+     POWER_NONE = 0,
+     POWER_CSTATE = 1,
+     POWER_PSTATE = 2,
+};
+#endif

Since we are cleaning up all these events, those enum definitions dont really look logical. For example, what is 'POWER_NONE'? Can a CPU have 'no power'?

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

The enum belongs to the deprecated API so I would rather not touch it. Keeping the deprecated code isolated will make it easier to remove later.

So what will replace it? We still have a state field.

Nothing, this is part of the cleanup.

I mean, what will go into the state field of the power:cpu_idle event?

Quoted 6 lines

As you state above: POWER_NONE does not make sense at all. The whole thing (type= attribute that vanishes now) is passed to userspace, but never gets used there because the same info is in the event name:
cpu_frequency <-> frequency_switch <-> PSTATE
cpu_idle <-> power_start/power_end <-> CSTATE

Ah, i see, so this 'state' enum went into the type field.

So my question is, and ignore this particular enum for now, what values go into the state field, which field is still kept in the new events as well.

[ I'd like to avoid us having to define a third set of power events a few years down
the road ;-) ]

Thanks,

Ingo

#55 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Ingo Molnar
Quoted 3 lines · On Tuesday 26 October 2010 13:54:22 Ingo Molnar wrote:

* Thomas Renninger <[email protected]> wrote:

..

Quoted 11 lines

As you state above: POWER_NONE does not make sense at all.

Quoted 5 lines

The whole thing (type= attribute that vanishes now) is passed to userspace, but never gets used there because the same info is in the event name:
cpu_frequency <-> frequency_switch <-> PSTATE
cpu_idle <-> power_start/power_end <-> CSTATE

Ah, i see, so this 'state' enum went into the type field.

So my question is, and ignore this particular enum for now, what values go into the state field, which field is still kept in the new events as well.

Same as before:
cpu_idle:

                trace_power_start(POWER_CSTATE, 1, smp_processor_id());

+ trace_processor_idle(1, smp_processor_id());
(Ooops found a copy and paste bug in my patch where I reverted the poll_idle event, but it should be zero...).

State in cpu_idle is identical with cpuidle registered state. If cpuidle got registered, one should be able to calculate the same C-state residency time and usage via state=X (cpu_idle event) which you can grab via:
cat /sys/devices/system/cpu/cpu0/cpuidle/stateX/{time,usage} The cpu_idle event additionally gives you the timestamps of the state changes.
This is rather nice as userspace can grab additional info from cpuidle sysfs layer like:
/sys/devices/system/cpu/cpu0/cpuidle/stateX/{desc,power,name}

If cpuidle is not registered, the events you get are arch specific. I mean they are arch specific anyway, but with cpuidle you can build up an arch independent userspace framework nicely by looking up name/desc/power/... of an cpu_idle event in cpuidle sysfs as described above.

Thomas

#58 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Ingo Molnar
Quoted 3 lines · On Tuesday 26 October 2010 13:54:22 Ingo Molnar wrote:

* Thomas Renninger <[email protected]> wrote:

..

Quoted 11 lines

As you state above: POWER_NONE does not make sense at all.

Quoted 5 lines

The whole thing (type= attribute that vanishes now) is passed to userspace, but never gets used there because the same info is in the event name:
cpu_frequency <-> frequency_switch <-> PSTATE
cpu_idle <-> power_start/power_end <-> CSTATE

Ah, i see, so this 'state' enum went into the type field.

So my question is, and ignore this particular enum for now, what values go into the state field, which field is still kept in the new events as well.

Same as before:
cpu_idle:

                trace_power_start(POWER_CSTATE, 1, smp_processor_id());

+ trace_processor_idle(1, smp_processor_id());
(Ooops found a copy and paste bug in my patch where I reverted the poll_idle event, but it should be zero...).

State in cpu_idle is identical with cpuidle registered state. If cpuidle got registered, one should be able to calculate the same C-state residency time and usage via state=X (cpu_idle event) which you can grab via:
cat /sys/devices/system/cpu/cpu0/cpuidle/stateX/{time,usage} The cpu_idle event additionally gives you the timestamps of the state changes.
This is rather nice as userspace can grab additional info from cpuidle sysfs layer like:
/sys/devices/system/cpu/cpu0/cpuidle/stateX/{desc,power,name}

If cpuidle is not registered, the events you get are arch specific. I mean they are arch specific anyway, but with cpuidle you can build up an arch independent userspace framework nicely by looking up name/desc/power/... of an cpu_idle event in cpuidle sysfs as described above.

Thomas

#56 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 3 lines · On Tuesday 26 October 2010 15:17:43 Thomas Renninger wrote:
Quoted 2 lines · On Tuesday 26 October 2010 13:54:22 Ingo Molnar wrote:

* Thomas Renninger <[email protected]> wrote:

...

Quoted 5 lines

If cpuidle is not registered, the events you get are arch specific. I mean they are arch specific anyway, but with cpuidle you can build up an arch independent userspace framework nicely by looking up name/desc/power/... of an cpu_idle event in cpuidle sysfs as described above.

About cpuidle and cpu_idle events:
There is some oddness that:
arch specific code which registers for cpuidle has to throw the cpu_idle enter sleep state X event
and the generic cpuidle framework triggers the "exit" event.

So as there are only cpu_idle events in drivers/idle/intel_idle.c, but not in drivers/acpi/processor_idle.c, I expect that processor.ko idle driver is broken and only exit states are sent. Ideally all cpuidle events should be thrown in cpuidle.c like this:

        trace_processor_idle(target_state, smp_processor_id());
        dev->last_residency = target_state->enter(dev, target_state);                                                                     
        trace_processor_idle(PWR_EVENT_EXIT, smp_processor_id());

My patches do not touch this behavior. If, it was broken before. I'll look at it separately.

Thomas

#57 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 3 lines · On Tuesday 26 October 2010 15:17:43 Thomas Renninger wrote:
Quoted 2 lines · On Tuesday 26 October 2010 13:54:22 Ingo Molnar wrote:

* Thomas Renninger <[email protected]> wrote:

...

Quoted 5 lines

If cpuidle is not registered, the events you get are arch specific. I mean they are arch specific anyway, but with cpuidle you can build up an arch independent userspace framework nicely by looking up name/desc/power/... of an cpu_idle event in cpuidle sysfs as described above.

About cpuidle and cpu_idle events:
There is some oddness that:
arch specific code which registers for cpuidle has to throw the cpu_idle enter sleep state X event
and the generic cpuidle framework triggers the "exit" event.

So as there are only cpu_idle events in drivers/idle/intel_idle.c, but not in drivers/acpi/processor_idle.c, I expect that processor.ko idle driver is broken and only exit states are sent. Ideally all cpuidle events should be thrown in cpuidle.c like this:

        trace_processor_idle(target_state, smp_processor_id());
        dev->last_residency = target_state->enter(dev, target_state);                                                                     
        trace_processor_idle(PWR_EVENT_EXIT, smp_processor_id());

My patches do not touch this behavior. If, it was broken before. I'll look at it separately.

Thomas

#78 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Ingo Molnar
Quoted 7 lines · Ingo Molnar

How will future PCI (or other device) power saving tracepoints be called?

Might be more consistent to use:

 power:cpu_idle
 power:machine_idle
 power:device_idle

Agree with this.

FYI, I have a runtime_pm tracepoint currently cooking. Here is preliminary patch.
Can this be a candidate for a "power:device_idle" tracepoint?

Regards,
Pierre
----
From 3d5e03405f590d470ecfa59c8b9759915bf29307 Mon Sep 17 00:00:00 2001
From: Pierre Tardy <[email protected]>
Date: Fri, 22 Oct 2010 03:07:07 -0500
Subject: [PATCH] trace/runtime_pm: add runtime_pm trace event

based on the recent hook from Arjan for powertop statistics we add a tracepoint in order for pytimechart to display the runtime_pm activity over time, and versus other events.

---
 drivers/base/power/runtime.c |    3 ++-
 include/trace/events/power.h |   35 +++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletions(-)
drivers/base/power/runtime.c +2 -1
index b78c401..0f38447 100644
@@ -9,6 +9,7 @@
 #include <linux/sched.h>
 #include <linux/pm_runtime.h>
 #include <linux/jiffies.h>
+#include <trace/events/power.h>
 
 static int __pm_runtime_resume(struct device *dev, bool from_wq);
 static int __pm_request_idle(struct device *dev);
@@ -159,9 +160,9 @@ void update_pm_runtime_accounting(struct device *dev)
 static void __update_runtime_status(struct device *dev, enum rpm_status status)
 {
 	update_pm_runtime_accounting(dev);
+	trace_runtime_pm_status(dev, status);
 	dev->power.runtime_status = status;
 }
-
 /**
  * __pm_runtime_suspend - Carry out run-time suspend of given device.
  * @dev: Device to suspend.
include/trace/events/power.h +31 -0
index 286784d..dd57c29 100644
@@ -6,6 +6,7 @@
 
 #include <linux/ktime.h>
 #include <linux/tracepoint.h>
+#include <linux/device.h>
 
 #ifndef _TRACE_POWER_ENUM_
 #define _TRACE_POWER_ENUM_
@@ -75,6 +76,40 @@ TRACE_EVENT(power_end,
 
 );
 
+#ifdef CONFIG_PM_RUNTIME
+#define rpm_status_name(status) { RPM_##status, #status }
+#define show_rpm_status_name(val)				\
+	__print_symbolic(val,					\
+		rpm_status_name(SUSPENDED),			\
+		rpm_status_name(SUSPENDING),			\
+		rpm_status_name(RESUMING),			\
+		rpm_status_name(ACTIVE)		                \
+		)
+TRACE_EVENT(runtime_pm_status,
+
+	TP_PROTO(struct device *dev, int new_status),
+
+	TP_ARGS(dev, new_status),
+
+	TP_STRUCT__entry(
+		__string(devname,dev_name(dev))
+		__string(drivername,dev_driver_string(dev))
+		__field(u32, prev_status)
+		__field(u32, status)
+	),
+
+	TP_fast_assign(
+		__assign_str(devname, dev_name(dev));
+		__assign_str(drivername, dev_driver_string(dev));
+		__entry->prev_status = (u32)dev->power.runtime_status;
+		__entry->status = (u32)new_status;
+	),
+
+	TP_printk("driver=%s dev=%s prev_status=%s status=%s",

__get_str(drivername),__get_str(devname),
+ show_rpm_status_name(__entry->prev_status),
show_rpm_status_name(__entry->status))
+);
+#endif /* CONFIG_PM_RUNTIME */
+
/*
* The clock events are used for clock enable/disable and for
* clock rate change

Signature · 5 lines
1.7.2.3
--
To unsubscribe from this list: send the line "unsubscribe linux-trace-users" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#126 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Ingo Molnar
Quoted 7 lines · Ingo Molnar

How will future PCI (or other device) power saving tracepoints be called?

Might be more consistent to use:

 power:cpu_idle
 power:machine_idle
 power:device_idle

Agree with this.

FYI, I have a runtime_pm tracepoint currently cooking. Here is preliminary patch.
Can this be a candidate for a "power:device_idle" tracepoint?

Regards,
Pierre
----

Quoted 1 line

From 3d5e03405f590d470ecfa59c8b9759915bf29307 Mon Sep 17 00:00:00 2001

From: Pierre Tardy <[email protected]>
Date: Fri, 22 Oct 2010 03:07:07 -0500
Subject: [PATCH] trace/runtime_pm: add runtime_pm trace event

based on the recent hook from Arjan for powertop statistics we add a tracepoint in order for pytimechart to display the runtime_pm activity over time, and versus other events.

---
 drivers/base/power/runtime.c |    3 ++-
 include/trace/events/power.h |   35 +++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletions(-)
drivers/base/power/runtime.c +2 -1
index b78c401..0f38447 100644
@@ -9,6 +9,7 @@
 #include <linux/sched.h>
 #include <linux/pm_runtime.h>
 #include <linux/jiffies.h>
+#include <trace/events/power.h>
 
 static int __pm_runtime_resume(struct device *dev, bool from_wq);
 static int __pm_request_idle(struct device *dev);
@@ -159,9 +160,9 @@ void update_pm_runtime_accounting(struct device *dev)
 static void __update_runtime_status(struct device *dev, enum rpm_status status)
 {
 	update_pm_runtime_accounting(dev);
+	trace_runtime_pm_status(dev, status);
 	dev->power.runtime_status = status;
 }
-
 /**
  * __pm_runtime_suspend - Carry out run-time suspend of given device.
  * @dev: Device to suspend.
include/trace/events/power.h +31 -0
index 286784d..dd57c29 100644
@@ -6,6 +6,7 @@
 
 #include <linux/ktime.h>
 #include <linux/tracepoint.h>
+#include <linux/device.h>
 
 #ifndef _TRACE_POWER_ENUM_
 #define _TRACE_POWER_ENUM_
@@ -75,6 +76,40 @@ TRACE_EVENT(power_end,
 
 );
 
+#ifdef CONFIG_PM_RUNTIME
+#define rpm_status_name(status) { RPM_##status, #status }
+#define show_rpm_status_name(val)				\
+	__print_symbolic(val,					\
+		rpm_status_name(SUSPENDED),			\
+		rpm_status_name(SUSPENDING),			\
+		rpm_status_name(RESUMING),			\
+		rpm_status_name(ACTIVE)		                \
+		)
+TRACE_EVENT(runtime_pm_status,
+
+	TP_PROTO(struct device *dev, int new_status),
+
+	TP_ARGS(dev, new_status),
+
+	TP_STRUCT__entry(
+		__string(devname,dev_name(dev))
+		__string(drivername,dev_driver_string(dev))
+		__field(u32, prev_status)
+		__field(u32, status)
+	),
+
+	TP_fast_assign(
+		__assign_str(devname, dev_name(dev));
+		__assign_str(drivername, dev_driver_string(dev));
+		__entry->prev_status = (u32)dev->power.runtime_status;
+		__entry->status = (u32)new_status;
+	),
+
+	TP_printk("driver=%s dev=%s prev_status=%s status=%s",

__get_str(drivername),__get_str(devname),
+ show_rpm_status_name(__entry->prev_status),
show_rpm_status_name(__entry->status))
+);
+#endif /* CONFIG_PM_RUNTIME */
+
/*
* The clock events are used for clock enable/disable and for
* clock rate change

Signature · 1 line
1.7.2.3
#79 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Pierre Tardy
Quoted 6 lines · Pierre Tardy
Quoted 4 lines · Ingo Molnar

How will future PCI (or other device) power saving tracepoints be called?

Might be more consistent to use:

Quoted 8 lines
Quoted 3 lines

power:cpu_idle
power:machine_idle
power:device_idle

Agree with this.

FYI, I have a runtime_pm tracepoint currently cooking. Here is preliminary patch.
Can this be a candidate for a "power:device_idle" tracepoint?

I would like to see a slightly more advanced tracepoint do the runtime pm framework; specifically I'd like to see the "comm" of the process that's taking a refcount on a device (that way, powertop can track which process keeps a device busy)

#125 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Pierre Tardy
Quoted 6 lines · Pierre Tardy
Quoted 4 lines · Ingo Molnar

How will future PCI (or other device) power saving tracepoints be called?

Might be more consistent to use:

Quoted 8 lines
Quoted 3 lines

power:cpu_idle
power:machine_idle
power:device_idle

Agree with this.

FYI, I have a runtime_pm tracepoint currently cooking. Here is preliminary patch.
Can this be a candidate for a "power:device_idle" tracepoint?

I would like to see a slightly more advanced tracepoint do the runtime pm framework;
specifically I'd like to see the "comm" of the process that's taking a refcount on a device
(that way, powertop can track which process keeps a device busy)

#80 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Arjan van de Ven
Quoted 7 lines

I would like to see a slightly more advanced tracepoint do the runtime pm framework;
specifically I'd like to see the "comm" of the process that's taking a refcount on a device
(that way, powertop can track which process keeps a device busy)

Yes, the "comm" for this tracepoint should be the runtime_pm workqueue. To track responsabilities, I'm making another tracepoint, that traces the rpm_get/put.

drivers/base/power/runtime.c +1 -0
index 0f38447..54d9911 100644
@@ -792,7 +792,7 @@ EXPORT_SYMBOL_GPL(pm_request_resume);
 int __pm_runtime_get(struct device *dev, bool sync)
 {
        int retval;
 
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

@@ -813,6 +813,7 @@ int __pm_runtime_put(struct device *dev, bool sync)

 {
        int retval = 0;
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)-1);
        if (atomic_dec_and_test(&dev->power.usage_count))
                retval = sync ? pm_runtime_idle(dev) : pm_request_idle(dev);

@@ -1065,6 +1066,7 @@ void pm_runtime_forbid(struct device *dev)

                goto out;

        dev->power.runtime_auto = false;
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        __pm_runtime_resume(dev, false);

@@ -1086,6 +1088,7 @@ void pm_runtime_allow(struct device *dev)

                goto out;

        dev->power.runtime_auto = true;
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)-1);
        if (atomic_dec_and_test(&dev->power.usage_count))
                __pm_runtime_idle(dev);
include/trace/events/power.h +0 -0
index ea514eb..813229c 100644
@@ -100,6 +100,29 @@ TRACE_EVENT(runtime_pm_status,
        TP_printk("driver=%s dev=%s prev_status=%s status=%s",

__get_str(drivername),__get_str(devname),

                  show_rpm_status_name(__entry->prev_status),

show_rpm_status_name(__entry->status))
);
+TRACE_EVENT(runtime_pm_usage,
+
+ TP_PROTO(struct device *dev, int new_usage),
+
+ TP_ARGS(dev, new_usage),
+
+ TP_STRUCT__entry(

+               __string(devname,dev_name(dev))
+               __string(drivername,dev_driver_string(dev))
+               __field(u32, prev_usage)
+               __field(u32, usage)
+       ),
+
+       TP_fast_assign(
+               __assign_str(devname, dev_name(dev));
+               __assign_str(drivername, dev_driver_string(dev));
+               __entry->prev_usage = (u32)atomic_read(&dev->power.usage_count);
+               __entry->usage = (u32)new_usage;

+ ),
+
+ TP_printk("driver=%s dev=%s prev_usage=%d usage=%s",
__get_str(drivername),__get_str(devname),
+ __entry->prev_usage, __entry->usage)
+);

Signature · 1 line
Pierre
#81 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Arjan van de Ven
Quoted 7 lines

I would like to see a slightly more advanced tracepoint do the runtime pm framework;
specifically I'd like to see the "comm" of the process that's taking a refcount on a device
(that way, powertop can track which process keeps a device busy)

Yes, the "comm" for this tracepoint should be the runtime_pm workqueue. To track responsabilities, I'm making another tracepoint, that traces the rpm_get/put.

drivers/base/power/runtime.c +1 -0
index 0f38447..54d9911 100644
@@ -792,7 +792,7 @@ EXPORT_SYMBOL_GPL(pm_request_resume);
 int __pm_runtime_get(struct device *dev, bool sync)
 {
        int retval;
 
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

@@ -813,6 +813,7 @@ int __pm_runtime_put(struct device *dev, bool sync)

 {
        int retval = 0;
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)-1);
        if (atomic_dec_and_test(&dev->power.usage_count))
                retval = sync ? pm_runtime_idle(dev) : pm_request_idle(dev);

@@ -1065,6 +1066,7 @@ void pm_runtime_forbid(struct device *dev)

                goto out;

        dev->power.runtime_auto = false;
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        __pm_runtime_resume(dev, false);

@@ -1086,6 +1088,7 @@ void pm_runtime_allow(struct device *dev)

                goto out;

        dev->power.runtime_auto = true;
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)-1);
        if (atomic_dec_and_test(&dev->power.usage_count))
                __pm_runtime_idle(dev);
include/trace/events/power.h +0 -0
index ea514eb..813229c 100644
@@ -100,6 +100,29 @@ TRACE_EVENT(runtime_pm_status,
        TP_printk("driver=%s dev=%s prev_status=%s status=%s",

__get_str(drivername),__get_str(devname),

                  show_rpm_status_name(__entry->prev_status),

show_rpm_status_name(__entry->status))
);
+TRACE_EVENT(runtime_pm_usage,
+
+ TP_PROTO(struct device *dev, int new_usage),
+
+ TP_ARGS(dev, new_usage),
+
+ TP_STRUCT__entry(

+               __string(devname,dev_name(dev))
+               __string(drivername,dev_driver_string(dev))
+               __field(u32, prev_usage)
+               __field(u32, usage)
+       ),
+
+       TP_fast_assign(
+               __assign_str(devname, dev_name(dev));
+               __assign_str(drivername, dev_driver_string(dev));
+               __entry->prev_usage = (u32)atomic_read(&dev->power.usage_count);
+               __entry->usage = (u32)new_usage;

+ ),
+
+ TP_printk("driver=%s dev=%s prev_usage=%d usage=%s",
__get_str(drivername),__get_str(devname),
+ __entry->prev_usage, __entry->usage)
+);

Signature · 1 line
Pierre
maintainer
#82 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

maintainer
#124 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Pierre Tardy
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

#83 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Peter Zijlstra
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

kref should certainly be used there.

About the instrumentation, well... the only way to have something that's not racy would be to instrument kref directly, and use atomic_add_return() in both the get/put paths. But I fear that the performance impact on many architectures might be significant (turning atomic_add + smp_mb() into a cmpxchg()). Maybe it could be acceptable as a kernel debug option.

Thanks,

Mathieu

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#84 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Peter Zijlstra
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

kref should certainly be used there.

About the instrumentation, well... the only way to have something that's not racy would be to instrument kref directly, and use atomic_add_return() in both the get/put paths. But I fear that the performance impact on many architectures might be significant (turning atomic_add + smp_mb() into a cmpxchg()). Maybe it could be acceptable as a kernel debug option.

Thanks,

Mathieu

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#112 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Peter Zijlstra
Quoted 7 lines · Peter Zijlstra
Quoted 3 lines · Pierre Tardy

+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1); atomic_inc(&dev->power.usage_count);

That's terribly racy..

I know. I'm not proud of this.. As I said, this is preliminary patch. We dont really need to have this prev_usage. This is just for debug. It mayprobably endup with something like:

         atomic_inc(&dev->power.usage_count);

+ trace_power_device_usage(dev);

Signature · 1 line
Pierre
#113 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Peter Zijlstra
Quoted 7 lines · Peter Zijlstra
Quoted 3 lines · Pierre Tardy

+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1); atomic_inc(&dev->power.usage_count);

That's terribly racy..

I know. I'm not proud of this.. As I said, this is preliminary patch. We dont really need to have this prev_usage. This is just for debug. It mayprobably endup with something like:

         atomic_inc(&dev->power.usage_count);

+ trace_power_device_usage(dev);

Signature · 5 lines
Pierre
--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#85 lore raw

Re: [linux-pm] [PATCH] PERF(kernel): Cleanup power events V2

In reply to Mathieu Desnoyers
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

I don't understand. What's the problem? The inc/dec are atomic because they are not protected by spinlocks, but everything else is (aside from the tracepoint, which is new).

Quoted 1 line

kref should certainly be used there.

What for?

Alan Stern

#105 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Mathieu Desnoyers
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

I don't understand. What's the problem? The inc/dec are atomic because they are not protected by spinlocks, but everything else is (aside from the tracepoint, which is new).

Quoted 1 line

kref should certainly be used there.

What for?

Alan Stern

#130 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 21 lines · Thomas Renninger

Changes in V2:
- Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
- Use u32 instead of u64 for cpuid, state which is by far enough

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

and
power:power_frequency
is replaced with:
power:processor_frequency

Quoted 4 lines

power:machine_suspend
is newly introduced, a first implementation
comes from the ARM side, but it's easy to add these events in X86 as well if needed.

As I said already somewhere else, I think this one should be done at the core level rather than in arch-specific code.

Thanks,
Rafael

#131 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 21 lines · Thomas Renninger

Changes in V2:
- Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
- Use u32 instead of u64 for cpuid, state which is by far enough

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

and
power:power_frequency
is replaced with:
power:processor_frequency

Quoted 4 lines

power:machine_suspend
is newly introduced, a first implementation
comes from the ARM side, but it's easy to add these events in X86 as well if needed.

As I said already somewhere else, I think this one should be done at the core level rather than in arch-specific code.

Thanks,
Rafael

#60 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 59 lines · Thomas Renninger
Quoted 2 lines · On Tuesday 26 October 2010 13:21:29 Ingo Molnar wrote:

* Jean Pihet <[email protected]> wrote:

..

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

include/trace/events/power.h TRACE_EVENT(power_end, · line 143

Quoted 8 lines
include/trace/events/power.h +8 -0
+#ifndef _TRACE_POWER_ENUM_
+#define _TRACE_POWER_ENUM_
+enum {
+     POWER_NONE = 0,
+     POWER_CSTATE = 1,
+     POWER_PSTATE = 2,
+};
+#endif

Since we are cleaning up all these events, those enum definitions dont really look logical. For example, what is 'POWER_NONE'? Can a CPU have 'no power'?

Quoted 5 lines
Quoted 3 lines

The enum belongs to the deprecated API so I would rather not touch it. Keeping the deprecated code isolated will make it easier to remove later.

So what will replace it? We still have a state field.

Nothing, this is part of the cleanup.
As you state above: POWER_NONE does not make sense at all. The whole thing (type= attribute that vanishes now) is passed to userspace, but never gets used there because the same info is in the event name:
cpu_frequency <-> frequency_switch <-> PSTATE
cpu_idle <-> power_start/power_end <-> CSTATE

I expect that there was an initial power_start/end which was also used for frequency switching.
Then it got realized that _start/_end does not work out and frequency_switch got introduced.
To stay compatible the whole power_start/end was not renamed to cpu_idle and the type= field was kept.

This is a guess without even looking at the git history. Therefore my partly harsh comments about the sanity of the current power tracing events.

Quoted 19 lines

Passing in platform specific codes is a step backwards.

Quoted 16 lines
Quoted 4 lines
Quoted 4 lines
include/trace/events/power.h +4 -0
+TRACE_EVENT(machine_suspend,
+
+     TP_PROTO(unsigned int state),
+
Quoted 8 lines

include/trace/events/power.h line 54

Quoted 5 lines
include/trace/events/power.h +5 -0
+     TP_ARGS(state),
+
+     TP_STRUCT__entry(
+             __field(        u32,            state           )
+     ),

Hm, this event is not used anywhere in the submitted patches. Where is the patch that adds usage, and what are the possible values for 'state'?

This will come as a separate patch, which fits all platforms. Cf.
http://marc.info/?l=linux-omap&m=128620575300682&w=2.
The state field is of type suspend_state_t, cf. include/linux/suspend.h

Quoted 7 lines
Quoted 2 lines

Ok, that's at least generic. Needs the review of Rafael, to determine whether this state value is all we want to know when we enter suspend.

He already gave an acked-by on this generic one here:
Re: [PATCH 3/4] perf: add calls to suspend trace point
Oh now, that was on the X86 specific part which depends on this one. One should expect that he's fine with the generic part as well then, but I agree that he should definitely have a look at this and sign it off.

What patch exactly do you mean? I'm not quite sure from your comment above.

Rafael

#65 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 59 lines · Thomas Renninger
Quoted 2 lines · On Tuesday 26 October 2010 13:21:29 Ingo Molnar wrote:

* Jean Pihet <[email protected]> wrote:

..

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

include/trace/events/power.h TRACE_EVENT(power_end, · line 143

Quoted 8 lines
include/trace/events/power.h +8 -0
+#ifndef _TRACE_POWER_ENUM_
+#define _TRACE_POWER_ENUM_
+enum {
+     POWER_NONE = 0,
+     POWER_CSTATE = 1,
+     POWER_PSTATE = 2,
+};
+#endif

Since we are cleaning up all these events, those enum definitions dont really look logical. For example, what is 'POWER_NONE'? Can a CPU have 'no power'?

Quoted 5 lines
Quoted 3 lines

The enum belongs to the deprecated API so I would rather not touch it. Keeping the deprecated code isolated will make it easier to remove later.

So what will replace it? We still have a state field.

Nothing, this is part of the cleanup.
As you state above: POWER_NONE does not make sense at all. The whole thing (type= attribute that vanishes now) is passed to userspace, but never gets used there because the same info is in the event name:
cpu_frequency <-> frequency_switch <-> PSTATE
cpu_idle <-> power_start/power_end <-> CSTATE

I expect that there was an initial power_start/end which was also used for frequency switching.
Then it got realized that _start/_end does not work out and frequency_switch got introduced.
To stay compatible the whole power_start/end was not renamed to cpu_idle and the type= field was kept.

This is a guess without even looking at the git history. Therefore my partly harsh comments about the sanity of the current power tracing events.

Quoted 19 lines

Passing in platform specific codes is a step backwards.

Quoted 16 lines
Quoted 4 lines
Quoted 4 lines
include/trace/events/power.h +4 -0
+TRACE_EVENT(machine_suspend,
+
+     TP_PROTO(unsigned int state),
+
Quoted 8 lines

include/trace/events/power.h line 54

Quoted 5 lines
include/trace/events/power.h +5 -0
+     TP_ARGS(state),
+
+     TP_STRUCT__entry(
+             __field(        u32,            state           )
+     ),

Hm, this event is not used anywhere in the submitted patches. Where is the patch that adds usage, and what are the possible values for 'state'?

This will come as a separate patch, which fits all platforms. Cf.
http://marc.info/?l=linux-omap&m=128620575300682&w=2.
The state field is of type suspend_state_t, cf. include/linux/suspend.h

Quoted 7 lines
Quoted 2 lines

Ok, that's at least generic. Needs the review of Rafael, to determine whether this state value is all we want to know when we enter suspend.

He already gave an acked-by on this generic one here:
Re: [PATCH 3/4] perf: add calls to suspend trace point
Oh now, that was on the X86 specific part which depends on this one. One should expect that he's fine with the generic part as well then, but I agree that he should definitely have a look at this and sign it off.

What patch exactly do you mean? I'm not quite sure from your comment above.

Rafael

#74 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Ingo Molnar
Quoted 188 lines · Ingo Molnar
Quoted 39 lines · Thomas Renninger
Quoted 19 lines · On Tuesday 26 October 2010 09:10:20 Ingo Molnar wrote:
Quoted 16 lines · Thomas Renninger

Changes in V2:
- Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
- Use u32 instead of u64 for cpuid, state which is by far enough

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Quoted 7 lines
Quoted 4 lines

and
power:power_frequency
is replaced with:
power:processor_frequency

Could you please name it power:cpu_idle and power:cpu_frequency instead, for shortness? We generally use 'cpu' in the kernel and for events.

Sure.

Quoted 10 lines
Quoted 2 lines

power:machine_suspend

How will future PCI (or other device) power saving tracepoints be called?

Might be more consistent to use:

power:cpu_idle
power:machine_idle
power:device_idle

Quoted 8 lines

device idle is not true. Those may be low power modes like reduced network throughput, reduced wlan range, the device needs not to be idle.
Device power states is probably the most complex area, if such a thing gets introduced, it should makes sense to state the interface experimental for some time until a wider range of devices uses it (in contrast to these new ones
which should not change that soon anymore...).

Ok.

Quoted 2 lines

Also machine_idle may be true, but machine_suspend sounds more familiar and everyone immediately knows what the event is about.

Ok - fair enough.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+#define PWR_EVENT_EXIT 0xFFFFFFFF

Shouldnt this be part of the POWER_ enum? (and you can write -1 there)

No, below enum will vanish, but -1 is nicer.

When it vanishes what will replace it?

Quoted 66 lines

...

Quoted 14 lines

Plus:

include/trace/events/power.h line 18

Quoted 9 lines
include/trace/events/power.h +9 -0
+DECLARE_EVENT_CLASS(processor,
+
+	TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+	TP_ARGS(state, cpu_id),
+
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+		__field(	u32,		cpu_id		)

Trace entries can carry a cpu_id of the current processor already. Can this cpu_id ever be different from that CPU id?

Yes. A core's frequency can depend on another one which will get switched as well (by one command/MSR). Compare with commit 6f4f2723d08534fd4e407e1e.

This can theoretically also be the case for sleep states. Afaik such HW does not exist yet, but ACPI spec already provides interfaces to pass these dependency from BIOS to OS. -> We want a stable ABI and should be prepared for such stuff.

Quoted 21 lines
Quoted 21 lines
include/trace/events/power.h +21 -0
+	),
+
+	TP_fast_assign(
+		__entry->state = state;
+		__entry->cpu_id = cpu_id;
+	),
+
+	TP_printk("state=%lu cpu_id=%lu", (unsigned long)__entry->state,
+		  (unsigned long)__entry->cpu_id)
+);
+
+DEFINE_EVENT(processor, processor_idle,
+
+	TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+	     TP_ARGS(state, cpu_id)
+);
+
+#define PWR_EVENT_EXIT 0xFFFFFFFF
+
+DEFINE_EVENT(processor, processor_frequency,
Quoted 8 lines

include/trace/events/power.h line 44

Quoted 5 lines
include/trace/events/power.h +5 -0
+
+	TP_PROTO(unsigned int frequency, unsigned int cpu_id),
+
+	TP_ARGS(frequency, cpu_id)
+);

So, we have a 'state' field in the class, which is used as 'state' by the power::cpu_idle event, and as 'frequency' by the power::cpu_freq event?

Yes, is this a problem?

Definitions are a bit shorter having one power processor class. As "frequency" is stated in frequency event definition everything should be obvious and this one looks like the more elegant way to me.

Quoted 3 lines

Are there any architectures that track frequency in Hz, not in kHz? If yes, might there ever be a need for the frequency value to be larger than 4.29 GHz? If yes, then it wont fit into u32.

Quoted 6 lines

drivers/cpufreq subsystem is fixed to unsigned int (cmp. include/linux/cpufreq.h):

        unsigned int            min;    /* in kHz */
        unsigned int            max;    /* in kHz */
        unsigned int            cur;    /* in kHz,
        ...

that should be fine.

ok, good - so we should be fine up to 4 THz CPUs.

Quoted 38 lines
Quoted 4 lines

Also, might there be a future need to express different types of frequencies? For example, should we decide to track turbo frequencies in Intel CPUs, how would that be expressed via these events? Are there any architectures and CPUs that somehow have some extra attribute to the frequency value?

I wonder whether this ever can/will work in a sane way. Userspace can compare with:
/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq everything above is turbo. So I do not think it's ever needed. But adding an additional value at the end does not violate userspace compatibility. This has been done with the cpuid as well.

Quoted 4 lines
Quoted 4 lines
include/trace/events/power.h +4 -0
+TRACE_EVENT(machine_suspend,
+
+	TP_PROTO(unsigned int state),
+
Quoted 8 lines

include/trace/events/power.h line 54

Quoted 5 lines
include/trace/events/power.h +5 -0
+	TP_ARGS(state),
+
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+	),

Hm, this event is not used anywhere in the submitted patches. Where is the patch that adds usage, and what are the possible values for 'state'?

Jean wants to make use of it on ARM.
I also had patch for x86, I can have another look at it, Rafael already gave me a comment on it. But on X86 you typically realize when you suspend the machine (could imagine this is more useful on ARM driven mobile phones and similar), still I can add it..

Values probably should be (include/linux/suspend.h):

#define PM_SUSPEND_ON       0
#define PM_SUSPEND_STANDBY  1
#define PM_SUSPEND_MEM      3
#define PM_SUSPEND_MAX      4
Quoted 8 lines

How this strange state Arjan talked about is passed is up to these guys. Instead of using 0 and above pre-defined such arch specific special states better should get passed by:

#define X86_MOORESTOWN_STANDBY_S0   0x100
..                                  0x101
#define ARM_WHATEVER_STRANGE_THING  0x200

...

Quoted 6 lines

I'd rather like to see a meaningful name to be given to these states and them being passed, instead of weird platform specific things. Tooling will try to be as generic as possible.

I dont know this stuff, but making a distinction between s2ram and s2disk events would seem meaningful.

Basically, we have

standby (which is what it says)
mem (s2ram)
disk (s2disk)

These are the transitions the PM core really cares about (if supported). The can be read from /sys/power/state and I think these names should be used by the tracing interfaces too.

Thanks,
Rafael

#75 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Ingo Molnar
Quoted 188 lines · Ingo Molnar
Quoted 39 lines · Thomas Renninger
Quoted 19 lines · On Tuesday 26 October 2010 09:10:20 Ingo Molnar wrote:
Quoted 16 lines · Thomas Renninger

Changes in V2:
- Introduce PWR_EVENT_EXIT instead of 0 to mark non-power state
- Use u32 instead of u64 for cpuid, state which is by far enough

New power trace events:
power:processor_idle
power:processor_frequency
power:machine_suspend

C-state/idle accounting events:
power:power_start
power:power_end
are replaced with:
power:processor_idle

Quoted 7 lines
Quoted 4 lines

and
power:power_frequency
is replaced with:
power:processor_frequency

Could you please name it power:cpu_idle and power:cpu_frequency instead, for shortness? We generally use 'cpu' in the kernel and for events.

Sure.

Quoted 10 lines
Quoted 2 lines

power:machine_suspend

How will future PCI (or other device) power saving tracepoints be called?

Might be more consistent to use:

power:cpu_idle
power:machine_idle
power:device_idle

Quoted 8 lines

device idle is not true. Those may be low power modes like reduced network throughput, reduced wlan range, the device needs not to be idle.
Device power states is probably the most complex area, if such a thing gets introduced, it should makes sense to state the interface experimental for some time until a wider range of devices uses it (in contrast to these new ones
which should not change that soon anymore...).

Ok.

Quoted 2 lines

Also machine_idle may be true, but machine_suspend sounds more familiar and everyone immediately knows what the event is about.

Ok - fair enough.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+#define PWR_EVENT_EXIT 0xFFFFFFFF

Shouldnt this be part of the POWER_ enum? (and you can write -1 there)

No, below enum will vanish, but -1 is nicer.

When it vanishes what will replace it?

Quoted 66 lines

...

Quoted 14 lines

Plus:

include/trace/events/power.h line 18

Quoted 9 lines
include/trace/events/power.h +9 -0
+DECLARE_EVENT_CLASS(processor,
+
+	TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+	TP_ARGS(state, cpu_id),
+
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+		__field(	u32,		cpu_id		)

Trace entries can carry a cpu_id of the current processor already. Can this cpu_id ever be different from that CPU id?

Yes. A core's frequency can depend on another one which will get switched as well (by one command/MSR). Compare with commit 6f4f2723d08534fd4e407e1e.

This can theoretically also be the case for sleep states. Afaik such HW does not exist yet, but ACPI spec already provides interfaces to pass these dependency from BIOS to OS. -> We want a stable ABI and should be prepared for such stuff.

Quoted 21 lines
Quoted 21 lines
include/trace/events/power.h +21 -0
+	),
+
+	TP_fast_assign(
+		__entry->state = state;
+		__entry->cpu_id = cpu_id;
+	),
+
+	TP_printk("state=%lu cpu_id=%lu", (unsigned long)__entry->state,
+		  (unsigned long)__entry->cpu_id)
+);
+
+DEFINE_EVENT(processor, processor_idle,
+
+	TP_PROTO(unsigned int state, unsigned int cpu_id),
+
+	     TP_ARGS(state, cpu_id)
+);
+
+#define PWR_EVENT_EXIT 0xFFFFFFFF
+
+DEFINE_EVENT(processor, processor_frequency,
Quoted 8 lines

include/trace/events/power.h line 44

Quoted 5 lines
include/trace/events/power.h +5 -0
+
+	TP_PROTO(unsigned int frequency, unsigned int cpu_id),
+
+	TP_ARGS(frequency, cpu_id)
+);

So, we have a 'state' field in the class, which is used as 'state' by the power::cpu_idle event, and as 'frequency' by the power::cpu_freq event?

Yes, is this a problem?

Definitions are a bit shorter having one power processor class. As "frequency" is stated in frequency event definition everything should be obvious and this one looks like the more elegant way to me.

Quoted 3 lines

Are there any architectures that track frequency in Hz, not in kHz? If yes, might there ever be a need for the frequency value to be larger than 4.29 GHz? If yes, then it wont fit into u32.

Quoted 6 lines

drivers/cpufreq subsystem is fixed to unsigned int (cmp. include/linux/cpufreq.h):

        unsigned int            min;    /* in kHz */
        unsigned int            max;    /* in kHz */
        unsigned int            cur;    /* in kHz,
        ...

that should be fine.

ok, good - so we should be fine up to 4 THz CPUs.

Quoted 38 lines
Quoted 4 lines

Also, might there be a future need to express different types of frequencies? For example, should we decide to track turbo frequencies in Intel CPUs, how would that be expressed via these events? Are there any architectures and CPUs that somehow have some extra attribute to the frequency value?

I wonder whether this ever can/will work in a sane way. Userspace can compare with:
/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq everything above is turbo. So I do not think it's ever needed. But adding an additional value at the end does not violate userspace compatibility. This has been done with the cpuid as well.

Quoted 4 lines
Quoted 4 lines
include/trace/events/power.h +4 -0
+TRACE_EVENT(machine_suspend,
+
+	TP_PROTO(unsigned int state),
+
Quoted 8 lines

include/trace/events/power.h line 54

Quoted 5 lines
include/trace/events/power.h +5 -0
+	TP_ARGS(state),
+
+	TP_STRUCT__entry(
+		__field(	u32,		state		)
+	),

Hm, this event is not used anywhere in the submitted patches. Where is the patch that adds usage, and what are the possible values for 'state'?

Jean wants to make use of it on ARM.
I also had patch for x86, I can have another look at it, Rafael already gave me a comment on it. But on X86 you typically realize when you suspend the machine (could imagine this is more useful on ARM driven mobile phones and similar), still I can add it..

Values probably should be (include/linux/suspend.h):

#define PM_SUSPEND_ON       0
#define PM_SUSPEND_STANDBY  1
#define PM_SUSPEND_MEM      3
#define PM_SUSPEND_MAX      4
Quoted 8 lines

How this strange state Arjan talked about is passed is up to these guys. Instead of using 0 and above pre-defined such arch specific special states better should get passed by:

#define X86_MOORESTOWN_STANDBY_S0   0x100
..                                  0x101
#define ARM_WHATEVER_STRANGE_THING  0x200

...

Quoted 6 lines

I'd rather like to see a meaningful name to be given to these states and them being passed, instead of weird platform specific things. Tooling will try to be as generic as possible.

I dont know this stuff, but making a distinction between s2ram and s2disk events would seem meaningful.

Basically, we have

standby (which is what it says)
mem (s2ram)
disk (s2disk)

These are the transitions the PM core really cares about (if supported). The can be read from /sys/power/state and I think these names should be used by the tracing interfaces too.

Thanks,
Rafael

#106 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Mathieu Desnoyers
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

No, it isn't.

Quoted 1 line

kref should certainly be used there.

No, it shouldn't.

Please try to understand the code you're commenting on first.

Thanks,
Rafael

#107 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Mathieu Desnoyers
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

No, it isn't.

Quoted 1 line

kref should certainly be used there.

No, it shouldn't.

Please try to understand the code you're commenting on first.

Thanks,
Rafael

#114 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Pierre Tardy
Quoted 6 lines · Pierre Tardy
Quoted 5 lines · Peter Zijlstra
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
Quoted 8 lines
Quoted 2 lines

That's terribly racy..

I know. I'm not proud of this.. As I said, this is preliminary patch. We dont really need to have this prev_usage. This is just for debug. It mayprobably endup with something like:

         atomic_inc(&dev->power.usage_count);

+ trace_power_device_usage(dev);

Well, please tell me what you're trying to achieve.

Thanks,
Rafael

#123 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Pierre Tardy
Quoted 6 lines · Pierre Tardy
Quoted 5 lines · Peter Zijlstra
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
Quoted 8 lines
Quoted 2 lines

That's terribly racy..

I know. I'm not proud of this.. As I said, this is preliminary patch. We dont really need to have this prev_usage. This is just for debug. It mayprobably endup with something like:

         atomic_inc(&dev->power.usage_count);

+ trace_power_device_usage(dev);

Well, please tell me what you're trying to achieve.

Thanks,
Rafael

#115 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Rafael J. Wysocki
Quoted 9 lines · Rafael J. Wysocki
Quoted 8 lines · Pierre Tardy
Quoted 7 lines · Peter Zijlstra
Quoted 3 lines · Pierre Tardy

+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1); atomic_inc(&dev->power.usage_count);

That's terribly racy..

Quoted 8 lines
Quoted 6 lines

I know. I'm not proud of this.. As I said, this is preliminary patch. We dont really need to have this prev_usage. This is just for debug. It mayprobably endup with something like:

         atomic_inc(&dev->power.usage_count);
+       trace_power_device_usage(dev);

Well, please tell me what you're trying to achieve.

Please see attached the kind of pytimechart output I'm trying to achieve (yes, this chart is not coherent, seems I'm still missing some traces)

We basically want to have a trace point eachtime the usage_counter changes, so that I can display nice timecharts, and Arjan can have the comm of the process that eventually generated the rpm_get, in order to pinpoint it in powertop.

What you dont see in the above two lines is that trace_power_device_usage(dev); actually reads the usage_count, as well as the driver and device name.

Regards,

Signature · 1 line
Pierre
#116 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Rafael J. Wysocki
Quoted 9 lines · Rafael J. Wysocki
Quoted 8 lines · Pierre Tardy
Quoted 7 lines · Peter Zijlstra
Quoted 3 lines · Pierre Tardy

+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1); atomic_inc(&dev->power.usage_count);

That's terribly racy..

Quoted 8 lines
Quoted 6 lines

I know. I'm not proud of this.. As I said, this is preliminary patch. We dont really need to have this prev_usage. This is just for debug. It mayprobably endup with something like:

         atomic_inc(&dev->power.usage_count);
+       trace_power_device_usage(dev);

Well, please tell me what you're trying to achieve.

Please see attached the kind of pytimechart output I'm trying to achieve (yes, this chart is not coherent, seems I'm still missing some traces)

We basically want to have a trace point eachtime the usage_counter changes, so that I can display nice timecharts, and Arjan can have the comm of the process that eventually generated the rpm_get, in order to pinpoint it in powertop.

What you dont see in the above two lines is that trace_power_device_usage(dev); actually reads the usage_count, as well as the driver and device name.

Regards,

Signature · 1 line
Pierre
#117 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

Quoted 23 lines · Pierre Tardy
Quoted 9 lines · Rafael J. Wysocki
Quoted 8 lines · Pierre Tardy
Quoted 7 lines · Peter Zijlstra
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);

That's terribly racy..

Quoted 8 lines
Quoted 6 lines

I know. I'm not proud of this.. As I said, this is preliminary patch. We dont really need to have this prev_usage. This is just for debug. It mayprobably endup with something like:

         atomic_inc(&dev->power.usage_count);

+ trace_power_device_usage(dev);

Well, please tell me what you're trying to achieve.

Please see attached the kind of pytimechart output I'm trying to achieve (yes, this chart is not coherent, seems I'm still missing some traces)

Quoted 8 lines

We basically want to have a trace point eachtime the usage_counter changes, so that I can display nice timecharts, and Arjan can have the comm of the process that eventually generated the rpm_get, in order to pinpoint it in powertop.

What you dont see in the above two lines is that trace_power_device_usage(dev); actually reads the usage_count, as well as the driver and device name.

I'm afraid that for this to really work you'd need to put usage_count under a spinlock along with your trace point, which I'm not really sure I like.

Besides, I'm not really sure the manipulations of usage_count are worth tracing.

Thanks,
Rafael

#118 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Pierre Tardy
Quoted 23 lines · Pierre Tardy
Quoted 9 lines · Rafael J. Wysocki
Quoted 8 lines · Pierre Tardy
Quoted 7 lines · Peter Zijlstra
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);

That's terribly racy..

Quoted 8 lines
Quoted 6 lines

I know. I'm not proud of this.. As I said, this is preliminary patch. We dont really need to have this prev_usage. This is just for debug. It mayprobably endup with something like:

         atomic_inc(&dev->power.usage_count);

+ trace_power_device_usage(dev);

Well, please tell me what you're trying to achieve.

Please see attached the kind of pytimechart output I'm trying to achieve (yes, this chart is not coherent, seems I'm still missing some traces)

Quoted 8 lines

We basically want to have a trace point eachtime the usage_counter changes, so that I can display nice timecharts, and Arjan can have the comm of the process that eventually generated the rpm_get, in order to pinpoint it in powertop.

What you dont see in the above two lines is that trace_power_device_usage(dev); actually reads the usage_count, as well as the driver and device name.

I'm afraid that for this to really work you'd need to put usage_count under a spinlock along with your trace point, which I'm not really sure I like.

Besides, I'm not really sure the manipulations of usage_count are worth tracing.

Thanks,
Rafael

#119 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

Quoted 25 lines · Rafael J. Wysocki
Quoted 24 lines · Pierre Tardy
Quoted 7 lines · Rafael J. Wysocki
Quoted 6 lines · Pierre Tardy
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
         atomic_inc(&dev->power.usage_count);

That's terribly racy..

Quoted 7 lines
Quoted 6 lines

I know. I'm not proud of this.. As I said, this is preliminary patch. We dont really need to have this prev_usage. This is just for debug. It mayprobably endup with something like:

          atomic_inc(&dev->power.usage_count);

+ trace_power_device_usage(dev);

Well, please tell me what you're trying to achieve.

Please see attached the kind of pytimechart output I'm trying to achieve (yes, this chart is not coherent, seems I'm still missing some traces)

We basically want to have a trace point eachtime the usage_counter changes, so that I can display nice timecharts, and Arjan can have the comm of the process that eventually generated the rpm_get, in order to pinpoint it in powertop.

Quoted 8 lines
Quoted 3 lines

What you dont see in the above two lines is that trace_power_device_usage(dev); actually reads the usage_count, as well as the driver and device name.

I'm afraid that for this to really work you'd need to put usage_count under a spinlock along with your trace point, which I'm not really sure I like.

Besides, I'm not really sure the manipulations of usage_count are worth tracing.

what's most interesting is the 0->1 and 1->0 transitions.

#120 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Rafael J. Wysocki
Quoted 25 lines · Rafael J. Wysocki
Quoted 24 lines · Pierre Tardy
Quoted 7 lines · Rafael J. Wysocki
Quoted 6 lines · Pierre Tardy
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
         atomic_inc(&dev->power.usage_count);

That's terribly racy..

Quoted 7 lines
Quoted 6 lines

I know. I'm not proud of this.. As I said, this is preliminary patch. We dont really need to have this prev_usage. This is just for debug. It mayprobably endup with something like:

          atomic_inc(&dev->power.usage_count);

+ trace_power_device_usage(dev);

Well, please tell me what you're trying to achieve.

Please see attached the kind of pytimechart output I'm trying to achieve (yes, this chart is not coherent, seems I'm still missing some traces)

We basically want to have a trace point eachtime the usage_counter changes, so that I can display nice timecharts, and Arjan can have the comm of the process that eventually generated the rpm_get, in order to pinpoint it in powertop.

Quoted 8 lines
Quoted 3 lines

What you dont see in the above two lines is that trace_power_device_usage(dev); actually reads the usage_count, as well as the driver and device name.

I'm afraid that for this to really work you'd need to put usage_count under a spinlock along with your trace point, which I'm not really sure I like.

Besides, I'm not really sure the manipulations of usage_count are worth tracing.

what's most interesting is the 0->1 and 1->0 transitions.

#121 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

Quoted 29 lines · Arjan van de Ven
Quoted 28 lines · Rafael J. Wysocki
Quoted 27 lines · Pierre Tardy
Quoted 7 lines · Rafael J. Wysocki
Quoted 6 lines · Pierre Tardy
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
         atomic_inc(&dev->power.usage_count);

That's terribly racy..

Quoted 7 lines
Quoted 6 lines

I know. I'm not proud of this.. As I said, this is preliminary patch. We dont really need to have this prev_usage. This is just for debug. It mayprobably endup with something like:

          atomic_inc(&dev->power.usage_count);

+ trace_power_device_usage(dev);

Well, please tell me what you're trying to achieve.

Please see attached the kind of pytimechart output I'm trying to achieve (yes, this chart is not coherent, seems I'm still missing some traces)

We basically want to have a trace point eachtime the usage_counter changes, so that I can display nice timecharts, and Arjan can have the comm of the process that eventually generated the rpm_get, in order to pinpoint it in powertop.

What you dont see in the above two lines is that trace_power_device_usage(dev); actually reads the usage_count, as well as the driver and device name.

Quoted 7 lines
Quoted 5 lines

I'm afraid that for this to really work you'd need to put usage_count under a spinlock along with your trace point, which I'm not really sure I like.

Besides, I'm not really sure the manipulations of usage_count are worth tracing.

what's most interesting is the 0->1 and 1->0 transitions.

But they are only meaningful in specific situations. For example, if someone does pm_runtime_get_noresume() when the device is active, there may be a device suspend already under way at the same time. So IMO what really is interesting is when rpm_resume() is called with usage_count > 0 and then perhaps when rpm_idle() or rpm_suspend() is called after usage_count drops back to 0.

There are some other interesting cases, but they all need to be checked under ->power.lock and you need to do that cleverly, so that the _functionality_ is not harmed.

Overall, I think that adding tracepoints to the runtime PM core code is really premature at this point, given that we've just reworked it quite a bit recently.

Thanks,
Rafael

#122 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Arjan van de Ven
Quoted 29 lines · Arjan van de Ven
Quoted 28 lines · Rafael J. Wysocki
Quoted 27 lines · Pierre Tardy
Quoted 7 lines · Rafael J. Wysocki
Quoted 6 lines · Pierre Tardy
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
         atomic_inc(&dev->power.usage_count);

That's terribly racy..

Quoted 7 lines
Quoted 6 lines

I know. I'm not proud of this.. As I said, this is preliminary patch. We dont really need to have this prev_usage. This is just for debug. It mayprobably endup with something like:

          atomic_inc(&dev->power.usage_count);

+ trace_power_device_usage(dev);

Well, please tell me what you're trying to achieve.

Please see attached the kind of pytimechart output I'm trying to achieve (yes, this chart is not coherent, seems I'm still missing some traces)

We basically want to have a trace point eachtime the usage_counter changes, so that I can display nice timecharts, and Arjan can have the comm of the process that eventually generated the rpm_get, in order to pinpoint it in powertop.

What you dont see in the above two lines is that trace_power_device_usage(dev); actually reads the usage_count, as well as the driver and device name.

Quoted 7 lines
Quoted 5 lines

I'm afraid that for this to really work you'd need to put usage_count under a spinlock along with your trace point, which I'm not really sure I like.

Besides, I'm not really sure the manipulations of usage_count are worth tracing.

what's most interesting is the 0->1 and 1->0 transitions.

But they are only meaningful in specific situations. For example, if someone does pm_runtime_get_noresume() when the device is active, there may be a device suspend already under way at the same time. So IMO what really is interesting is when rpm_resume() is called with usage_count > 0 and then perhaps when rpm_idle() or rpm_suspend() is called after usage_count drops back to 0.

There are some other interesting cases, but they all need to be checked under ->power.lock and you need to do that cleverly, so that the _functionality_ is not harmed.

Overall, I think that adding tracepoints to the runtime PM core code is really premature at this point, given that we've just reworked it quite a bit recently.

Thanks,
Rafael

#86 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Alan Stern
Quoted 26 lines · * Alan Stern ([email protected]) wrote:
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

Quoted 7 lines

I don't understand. What's the problem? The inc/dec are atomic because they are not protected by spinlocks, but everything else is (aside from the tracepoint, which is new).

Quoted 1 line

kref should certainly be used there.

What for?

kref has the following "get":

        atomic_inc(&kref->refcount);
        smp_mb__after_atomic_inc();

What seems to be missing in __pm_runtime_get() and pm_runtime_get_noresume() is the memory barrier after the atomic increment. The atomic increment is free to be reordered into the following spinlock (within pm_request_resume or pm_request resume execution) because taking a spinlock only acts as a memory barrier with acquire semantic, not a full memory barrier.

So AFAIU, the failure scenario would be as follows (sorry for the 80+ columns):

initial conditions: usage_count = 1

CPU A CPU B
1) __pm_runtime_get() (sync = true)
2) atomic_inc(&usage_count) (not committed to memory yet)
3) pm_runtime_resume()

4)     spin_lock_irqsave(&dev->power.lock, flags);
5)     retval = __pm_request_resume(dev);
6)     (execute the body of __pm_request_resume and return)

7) __pm_runtime_put() (sync = true)
8) if (atomic_dec_and_test(&dev->power.usage_count))
(still see usage_count == 1 before decrement,
thus decrement to 0)
9) pm_runtime_idle()
10) spin_unlock_irqrestore(&dev->power.lock, flags)

11)                                                            spin_lock_irq(&dev->power.lock);
12)                                                            retval = __pm_runtime_idle(dev);
13)                                                            spin_unlock_irq(&dev->power.lock);

So we end up in a situation where CPU A expects the device to be resumed, but the last action performed has been to bring it to idle.

A smp_mb__after_atomic_inc() between lines 2 and 3 would fix this.

Thanks,

Mathieu

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#87 lore raw

Re: [linux-pm] [PATCH] PERF(kernel): Cleanup power events V2

In reply to Alan Stern
Quoted 26 lines · * Alan Stern ([email protected]) wrote:
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

Quoted 7 lines

I don't understand. What's the problem? The inc/dec are atomic because they are not protected by spinlocks, but everything else is (aside from the tracepoint, which is new).

Quoted 1 line

kref should certainly be used there.

What for?

kref has the following "get":

        atomic_inc(&kref->refcount);
        smp_mb__after_atomic_inc();

What seems to be missing in __pm_runtime_get() and pm_runtime_get_noresume() is the memory barrier after the atomic increment. The atomic increment is free to be reordered into the following spinlock (within pm_request_resume or pm_request resume execution) because taking a spinlock only acts as a memory barrier with acquire semantic, not a full memory barrier.

So AFAIU, the failure scenario would be as follows (sorry for the 80+ columns):

initial conditions: usage_count = 1

CPU A CPU B
1) __pm_runtime_get() (sync = true)
2) atomic_inc(&usage_count) (not committed to memory yet)
3) pm_runtime_resume()

4)     spin_lock_irqsave(&dev->power.lock, flags);
5)     retval = __pm_request_resume(dev);
6)     (execute the body of __pm_request_resume and return)

7) __pm_runtime_put() (sync = true)
8) if (atomic_dec_and_test(&dev->power.usage_count))
(still see usage_count == 1 before decrement,
thus decrement to 0)
9) pm_runtime_idle()
10) spin_unlock_irqrestore(&dev->power.lock, flags)

11)                                                            spin_lock_irq(&dev->power.lock);
12)                                                            retval = __pm_runtime_idle(dev);
13)                                                            spin_unlock_irq(&dev->power.lock);

So we end up in a situation where CPU A expects the device to be resumed, but the last action performed has been to bring it to idle.

A smp_mb__after_atomic_inc() between lines 2 and 3 would fix this.

Thanks,

Mathieu

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#108 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Rafael J. Wysocki
Quoted 25 lines · * Rafael J. Wysocki ([email protected]) wrote:
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

Quoted 7 lines

No, it isn't.

Quoted 1 line

kref should certainly be used there.

No, it shouldn't.

Please try to understand the code you're commenting on first.

Please see my reply to Alan Stern:

http://www.spinics.net/lists/linux-omap/msg39382.html

Thanks,

Mathieu

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#109 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Rafael J. Wysocki
Quoted 25 lines · * Rafael J. Wysocki ([email protected]) wrote:
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

Quoted 7 lines

No, it isn't.

Quoted 1 line

kref should certainly be used there.

No, it shouldn't.

Please try to understand the code you're commenting on first.

Please see my reply to Alan Stern:

http://www.spinics.net/lists/linux-omap/msg39382.html

Thanks,

Mathieu

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#88 lore raw

Re: [linux-pm] [PATCH] PERF(kernel): Cleanup power events V2

In reply to Mathieu Desnoyers
Quoted 48 lines · Mathieu Desnoyers
Quoted 26 lines · * Alan Stern ([email protected]) wrote:
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

Quoted 7 lines

I don't understand. What's the problem? The inc/dec are atomic because they are not protected by spinlocks, but everything else is (aside from the tracepoint, which is new).

Quoted 1 line

kref should certainly be used there.

What for?

kref has the following "get":

        atomic_inc(&kref->refcount);
        smp_mb__after_atomic_inc();

What seems to be missing in __pm_runtime_get() and pm_runtime_get_noresume() is the memory barrier after the atomic increment. The atomic increment is free to be reordered into the following spinlock (within pm_request_resume or pm_request resume execution) because taking a spinlock only acts as a memory barrier with acquire semantic, not a full memory barrier.

So AFAIU, the failure scenario would be as follows (sorry for the 80+ columns):

Quoted 8 lines

initial conditions: usage_count = 1

CPU A CPU B
1) __pm_runtime_get() (sync = true)
2) atomic_inc(&usage_count) (not committed to memory yet)
3) pm_runtime_resume()
4) spin_lock_irqsave(&dev->power.lock, flags);
5) retval = __pm_request_resume(dev);

If sync = true this is

           retval = __pm_runtime_resume(dev);

which drops and reacquires the spinlock. In the meantime it sets ->power.runtime_status so that __pm_runtime_idle() will fail if run at this point.

Quoted 9 lines

6) (execute the body of __pm_request_resume and return)
7) __pm_runtime_put() (sync = true)
8) if (atomic_dec_and_test(&dev->power.usage_count))
(still see usage_count == 1 before decrement,
thus decrement to 0)
9) pm_runtime_idle()
10) spin_unlock_irqrestore(&dev->power.lock, flags)
11) spin_lock_irq(&dev->power.lock);
12) retval = __pm_runtime_idle(dev);

Moreover, __pm_runtime_idle() checks ->power.usage_count under the spinlock, so it will see it's been incremented in the meantime and it will back off.

Quoted 6 lines

13) spin_unlock_irq(&dev->power.lock);

So we end up in a situation where CPU A expects the device to be resumed, but the last action performed has been to bring it to idle.

A smp_mb__after_atomic_inc() between lines 2 and 3 would fix this.

I don't think this particular race is possible. However, there is another one that seems to be possible (in a different function) that an explicit barrier will prevent from happening.

It's related to pm_runtime_get_noresume(), but I think it's better to put the barrier where it's necessary rather than into pm_runtime_get_noresume() itself.

Thanks,
Rafael

#104 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Mathieu Desnoyers
Quoted 48 lines · Mathieu Desnoyers
Quoted 26 lines · * Alan Stern ([email protected]) wrote:
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

Quoted 7 lines

I don't understand. What's the problem? The inc/dec are atomic because they are not protected by spinlocks, but everything else is (aside from the tracepoint, which is new).

Quoted 1 line

kref should certainly be used there.

What for?

kref has the following "get":

        atomic_inc(&kref->refcount);
        smp_mb__after_atomic_inc();

What seems to be missing in __pm_runtime_get() and pm_runtime_get_noresume() is the memory barrier after the atomic increment. The atomic increment is free to be reordered into the following spinlock (within pm_request_resume or pm_request resume execution) because taking a spinlock only acts as a memory barrier with acquire semantic, not a full memory barrier.

So AFAIU, the failure scenario would be as follows (sorry for the 80+ columns):

Quoted 8 lines

initial conditions: usage_count = 1

CPU A CPU B
1) __pm_runtime_get() (sync = true)
2) atomic_inc(&usage_count) (not committed to memory yet)
3) pm_runtime_resume()
4) spin_lock_irqsave(&dev->power.lock, flags);
5) retval = __pm_request_resume(dev);

If sync = true this is

           retval = __pm_runtime_resume(dev);

which drops and reacquires the spinlock. In the meantime it sets ->power.runtime_status so that __pm_runtime_idle() will fail if run at this point.

Quoted 9 lines

6) (execute the body of __pm_request_resume and return)
7) __pm_runtime_put() (sync = true)
8) if (atomic_dec_and_test(&dev->power.usage_count))
(still see usage_count == 1 before decrement,
thus decrement to 0)
9) pm_runtime_idle()
10) spin_unlock_irqrestore(&dev->power.lock, flags)
11) spin_lock_irq(&dev->power.lock);
12) retval = __pm_runtime_idle(dev);

Moreover, __pm_runtime_idle() checks ->power.usage_count under the spinlock, so it will see it's been incremented in the meantime and it will back off.

Quoted 6 lines

13) spin_unlock_irq(&dev->power.lock);

So we end up in a situation where CPU A expects the device to be resumed, but the last action performed has been to bring it to idle.

A smp_mb__after_atomic_inc() between lines 2 and 3 would fix this.

I don't think this particular race is possible. However, there is another one that seems to be possible (in a different function) that an explicit barrier will prevent from happening.

It's related to pm_runtime_get_noresume(), but I think it's better to put the barrier where it's necessary rather than into pm_runtime_get_noresume() itself.

Thanks,
Rafael

#110 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Mathieu Desnoyers
Quoted 30 lines · Mathieu Desnoyers
Quoted 29 lines · * Rafael J. Wysocki ([email protected]) wrote:
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

No, it isn't.

Quoted 1 line

kref should certainly be used there.

Quoted 7 lines
Quoted 3 lines

No, it shouldn't.

Please try to understand the code you're commenting on first.

Please see my reply to Alan Stern:

http://www.spinics.net/lists/linux-omap/msg39382.html

I have and I'm still unimpressed. :-)

Please see my reply to that message.

Thanks,
Rafael

#111 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Mathieu Desnoyers
Quoted 30 lines · Mathieu Desnoyers
Quoted 29 lines · * Rafael J. Wysocki ([email protected]) wrote:
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

No, it isn't.

Quoted 1 line

kref should certainly be used there.

Quoted 7 lines
Quoted 3 lines

No, it shouldn't.

Please try to understand the code you're commenting on first.

Please see my reply to Alan Stern:

http://www.spinics.net/lists/linux-omap/msg39382.html

I have and I'm still unimpressed. :-)

Please see my reply to that message.

Thanks,
Rafael

#89 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Rafael J. Wysocki
Quoted 82 lines · Rafael J. Wysocki
Quoted 48 lines · Mathieu Desnoyers
Quoted 26 lines · * Alan Stern ([email protected]) wrote:
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

Quoted 7 lines

I don't understand. What's the problem? The inc/dec are atomic because they are not protected by spinlocks, but everything else is (aside from the tracepoint, which is new).

Quoted 1 line

kref should certainly be used there.

What for?

kref has the following "get":

        atomic_inc(&kref->refcount);
        smp_mb__after_atomic_inc();

What seems to be missing in __pm_runtime_get() and pm_runtime_get_noresume() is the memory barrier after the atomic increment. The atomic increment is free to be reordered into the following spinlock (within pm_request_resume or pm_request resume execution) because taking a spinlock only acts as a memory barrier with acquire semantic, not a full memory barrier.

So AFAIU, the failure scenario would be as follows (sorry for the 80+ columns):

Quoted 8 lines

initial conditions: usage_count = 1

CPU A CPU B
1) __pm_runtime_get() (sync = true)
2) atomic_inc(&usage_count) (not committed to memory yet)
3) pm_runtime_resume()
4) spin_lock_irqsave(&dev->power.lock, flags);
5) retval = __pm_request_resume(dev);

If sync = true this is

           retval = __pm_runtime_resume(dev);

which drops and reacquires the spinlock. In the meantime it sets ->power.runtime_status so that __pm_runtime_idle() will fail if run at this point.

Quoted 9 lines

6) (execute the body of __pm_request_resume and return)
7) __pm_runtime_put() (sync = true)
8) if (atomic_dec_and_test(&dev->power.usage_count))
(still see usage_count == 1 before decrement,
thus decrement to 0)
9) pm_runtime_idle()
10) spin_unlock_irqrestore(&dev->power.lock, flags)
11) spin_lock_irq(&dev->power.lock);
12) retval = __pm_runtime_idle(dev);

Moreover, __pm_runtime_idle() checks ->power.usage_count under the spinlock, so it will see it's been incremented in the meantime and it will back off.

Quoted 5 lines

13) spin_unlock_irq(&dev->power.lock);

So we end up in a situation where CPU A expects the device to be resumed, but the last action performed has been to bring it to idle.

Quoted 8 lines
Quoted 1 line

A smp_mb__after_atomic_inc() between lines 2 and 3 would fix this.

I don't think this particular race is possible. However, there is another one that seems to be possible (in a different function) that an explicit barrier will prevent from happening.

It's related to pm_runtime_get_noresume(), but I think it's better to put the barrier where it's necessary rather than into pm_runtime_get_noresume() itself.

Actually, no. Since rpm_idle() and rpm_suspend() both check usage_count under the spinlock, the race I was thinking about doesn't appear to be possible after all.

Thanks,
Rafael

#90 lore raw

Re: [linux-pm] [PATCH] PERF(kernel): Cleanup power events V2

In reply to Rafael J. Wysocki
Quoted 82 lines · Rafael J. Wysocki
Quoted 48 lines · Mathieu Desnoyers
Quoted 26 lines · * Alan Stern ([email protected]) wrote:
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

Quoted 7 lines

I don't understand. What's the problem? The inc/dec are atomic because they are not protected by spinlocks, but everything else is (aside from the tracepoint, which is new).

Quoted 1 line

kref should certainly be used there.

What for?

kref has the following "get":

        atomic_inc(&kref->refcount);
        smp_mb__after_atomic_inc();

What seems to be missing in __pm_runtime_get() and pm_runtime_get_noresume() is the memory barrier after the atomic increment. The atomic increment is free to be reordered into the following spinlock (within pm_request_resume or pm_request resume execution) because taking a spinlock only acts as a memory barrier with acquire semantic, not a full memory barrier.

So AFAIU, the failure scenario would be as follows (sorry for the 80+ columns):

Quoted 8 lines

initial conditions: usage_count = 1

CPU A CPU B
1) __pm_runtime_get() (sync = true)
2) atomic_inc(&usage_count) (not committed to memory yet)
3) pm_runtime_resume()
4) spin_lock_irqsave(&dev->power.lock, flags);
5) retval = __pm_request_resume(dev);

If sync = true this is

           retval = __pm_runtime_resume(dev);

which drops and reacquires the spinlock. In the meantime it sets ->power.runtime_status so that __pm_runtime_idle() will fail if run at this point.

Quoted 9 lines

6) (execute the body of __pm_request_resume and return)
7) __pm_runtime_put() (sync = true)
8) if (atomic_dec_and_test(&dev->power.usage_count))
(still see usage_count == 1 before decrement,
thus decrement to 0)
9) pm_runtime_idle()
10) spin_unlock_irqrestore(&dev->power.lock, flags)
11) spin_lock_irq(&dev->power.lock);
12) retval = __pm_runtime_idle(dev);

Moreover, __pm_runtime_idle() checks ->power.usage_count under the spinlock, so it will see it's been incremented in the meantime and it will back off.

Quoted 5 lines

13) spin_unlock_irq(&dev->power.lock);

So we end up in a situation where CPU A expects the device to be resumed, but the last action performed has been to bring it to idle.

Quoted 8 lines
Quoted 1 line

A smp_mb__after_atomic_inc() between lines 2 and 3 would fix this.

I don't think this particular race is possible. However, there is another one that seems to be possible (in a different function) that an explicit barrier will prevent from happening.

It's related to pm_runtime_get_noresume(), but I think it's better to put the barrier where it's necessary rather than into pm_runtime_get_noresume() itself.

Actually, no. Since rpm_idle() and rpm_suspend() both check usage_count under the spinlock, the race I was thinking about doesn't appear to be possible after all.

Thanks,
Rafael

author Acked-by Rafael J. Wysocki
#61 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Rafael J. Wysocki
Quoted 11 lines · Rafael J. Wysocki
Quoted 8 lines · Thomas Renninger
Quoted 3 lines

Ok, that's at least generic. Needs the review of Rafael, to determine whether this state value is all we want to know when we enter suspend.

He already gave an acked-by on this generic one here:
Re: [PATCH 3/4] perf: add calls to suspend trace point
Oh now, that was on the X86 specific part which depends on this one. One should expect that he's fine with the generic part as well then, but I agree that he should definitely have a look at this and sign it off.

What patch exactly do you mean? I'm not quite sure from your comment above.

Eh, Jean's patch, sorry about that.
Needs a tiny change to use PWR_EVENT_EXIT instead of 0 with my new patch series:

---
 kernel/power/suspend.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
kernel/power/suspend.c +3 -0
index 7335952..10cad5c 100644
@@ -22,6 +22,7 @@
 #include <linux/mm.h>
 #include <linux/slab.h>
 #include <linux/suspend.h>
+#include <trace/events/power.h>
 
 #include "power.h"
 
@@ -164,7 +165,9 @@ static int suspend_enter(suspend_state_t state)
        error = sysdev_suspend(PMSG_SUSPEND);
        if (!error) {
                if (!suspend_test(TEST_CORE) && pm_check_wakeup_events()) {
+                       trace_machine_suspend(state);
                        error = suspend_ops->enter(state);
+                       trace_machine_suspend(0);
                        events_check_enabled = false;
                }
                sysdev_resume();
author Acked-by Rafael J. Wysocki
#64 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Rafael J. Wysocki
Quoted 11 lines · Rafael J. Wysocki
Quoted 8 lines · Thomas Renninger
Quoted 3 lines

Ok, that's at least generic. Needs the review of Rafael, to determine whether this state value is all we want to know when we enter suspend.

He already gave an acked-by on this generic one here:
Re: [PATCH 3/4] perf: add calls to suspend trace point
Oh now, that was on the X86 specific part which depends on this one. One should expect that he's fine with the generic part as well then, but I agree that he should definitely have a look at this and sign it off.

What patch exactly do you mean? I'm not quite sure from your comment above.

Eh, Jean's patch, sorry about that.
Needs a tiny change to use PWR_EVENT_EXIT instead of 0 with my new patch series:

---
 kernel/power/suspend.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
kernel/power/suspend.c +3 -0
index 7335952..10cad5c 100644
@@ -22,6 +22,7 @@
 #include <linux/mm.h>
 #include <linux/slab.h>
 #include <linux/suspend.h>
+#include <trace/events/power.h>
 
 #include "power.h"
 
@@ -164,7 +165,9 @@ static int suspend_enter(suspend_state_t state)
        error = sysdev_suspend(PMSG_SUSPEND);
        if (!error) {
                if (!suspend_test(TEST_CORE) && pm_check_wakeup_events()) {
+                       trace_machine_suspend(state);
                        error = suspend_ops->enter(state);
+                       trace_machine_suspend(0);
                        events_check_enabled = false;
                }
                sysdev_resume();
#91 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Rafael J. Wysocki
Quoted 58 lines · * Rafael J. Wysocki ([email protected]) wrote:
Quoted 48 lines · Mathieu Desnoyers
Quoted 26 lines · * Alan Stern ([email protected]) wrote:
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

Quoted 7 lines

I don't understand. What's the problem? The inc/dec are atomic because they are not protected by spinlocks, but everything else is (aside from the tracepoint, which is new).

Quoted 1 line

kref should certainly be used there.

What for?

kref has the following "get":

        atomic_inc(&kref->refcount);
        smp_mb__after_atomic_inc();

What seems to be missing in __pm_runtime_get() and pm_runtime_get_noresume() is the memory barrier after the atomic increment. The atomic increment is free to be reordered into the following spinlock (within pm_request_resume or pm_request resume execution) because taking a spinlock only acts as a memory barrier with acquire semantic, not a full memory barrier.

So AFAIU, the failure scenario would be as follows (sorry for the 80+ columns):

Quoted 8 lines

initial conditions: usage_count = 1

CPU A CPU B
1) __pm_runtime_get() (sync = true)
2) atomic_inc(&usage_count) (not committed to memory yet)
3) pm_runtime_resume()
4) spin_lock_irqsave(&dev->power.lock, flags);
5) retval = __pm_request_resume(dev);

Quoted 3 lines

If sync = true this is

           retval = __pm_runtime_resume(dev);

which drops and reacquires the spinlock.

Let's see. Upon entry in __pm_runtime_resume, the following condition holds (remember, the initial condition is that usage_count == 1):

dev->power.runtime_status == RPM_ACTIVE

so retval is set to 1, which goto directly to "out", without setting "parent". So there does not seem to be any spinlock reacquire on this path, or am I misunderstanding how the "runtime_status" works ?

Quoted 3 lines

In the meantime it sets
->power.runtime_status so that __pm_runtime_idle() will fail if run at this point.

runtime_status will be left at "RPM_ACTIVE", which is the appropriate value expected by __pm_runtime_idle.

Quoted 11 lines
Quoted 10 lines

6) (execute the body of __pm_request_resume and return)
7) __pm_runtime_put() (sync = true)
8) if (atomic_dec_and_test(&dev->power.usage_count))
(still see usage_count == 1 before decrement,
thus decrement to 0)
9) pm_runtime_idle()
10) spin_unlock_irqrestore(&dev->power.lock, flags)
11) spin_lock_irq(&dev->power.lock);
12) retval = __pm_runtime_idle(dev);

Quoted 2 lines

Moreover, __pm_runtime_idle() checks ->power.usage_count under the spinlock, so it will see it's been incremented in the meantime and it will back off.

This is a subtle but important point. Yes, my scenario seems to be dealt with by the extra usage_count check while the spinlock is held.

How about adding a comment under this atomic_inc() stating that the memory barriers are implicitely dealt with by the following spinlock release and the extra check while spinlock is held ?

Commenting memory barriers is important, but commenting why memory barriers are not needed due to a subtle corner-case looks even more important.

(hrm, but more below considering pm_runtime_get_noresume())

Quoted 6 lines
Quoted 6 lines

13) spin_unlock_irq(&dev->power.lock);

So we end up in a situation where CPU A expects the device to be resumed, but the last action performed has been to bring it to idle.

Quoted 8 lines
Quoted 1 line

A smp_mb__after_atomic_inc() between lines 2 and 3 would fix this.

I don't think this particular race is possible. However, there is another one that seems to be possible (in a different function) that an explicit barrier will prevent from happening.

It's related to pm_runtime_get_noresume(), but I think it's better to put the barrier where it's necessary rather than into pm_runtime_get_noresume() itself.

Quoting your following mail:

Quoted 3 lines

Actually, no. Since rpm_idle() and rpm_suspend() both check usage_count under the spinlock, the race I was thinking about doesn't appear to be possible after all.

Hrm, for the extra-usage_count-under-spinlock check to work, all pm_runtime_get_noresume() callers should grab and release the dev->power.lock after incrementing the usage_count. This does not seem to be the case though. So you might really have a race there.

So every code path that does:

1) pm_runtime_get_noresume(dev);

2) ...

3) pm_runtime_put_noidle(dev);

expecting that the device state cannot be changed between 1 and 3 might be surprised by a concurrent call to __pm_runtime_idle() that would put a device to idle (or similarly with suspend) due to lack of memory barrier after the atomic increment.

Or am I missing something else ?

Thanks,

Mathieu

Quoted 7 lines

Thanks,
Rafael
--
To unsubscribe from this list: send the line "unsubscribe linux-trace-users" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#92 lore raw

Re: [linux-pm] [PATCH] PERF(kernel): Cleanup power events V2

In reply to Rafael J. Wysocki
Quoted 58 lines · * Rafael J. Wysocki ([email protected]) wrote:
Quoted 48 lines · Mathieu Desnoyers
Quoted 26 lines · * Alan Stern ([email protected]) wrote:
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

Quoted 7 lines

I don't understand. What's the problem? The inc/dec are atomic because they are not protected by spinlocks, but everything else is (aside from the tracepoint, which is new).

Quoted 1 line

kref should certainly be used there.

What for?

kref has the following "get":

        atomic_inc(&kref->refcount);
        smp_mb__after_atomic_inc();

What seems to be missing in __pm_runtime_get() and pm_runtime_get_noresume() is the memory barrier after the atomic increment. The atomic increment is free to be reordered into the following spinlock (within pm_request_resume or pm_request resume execution) because taking a spinlock only acts as a memory barrier with acquire semantic, not a full memory barrier.

So AFAIU, the failure scenario would be as follows (sorry for the 80+ columns):

Quoted 8 lines

initial conditions: usage_count = 1

CPU A CPU B
1) __pm_runtime_get() (sync = true)
2) atomic_inc(&usage_count) (not committed to memory yet)
3) pm_runtime_resume()
4) spin_lock_irqsave(&dev->power.lock, flags);
5) retval = __pm_request_resume(dev);

Quoted 3 lines

If sync = true this is

           retval = __pm_runtime_resume(dev);

which drops and reacquires the spinlock.

Let's see. Upon entry in __pm_runtime_resume, the following condition holds (remember, the initial condition is that usage_count == 1):

dev->power.runtime_status == RPM_ACTIVE

so retval is set to 1, which goto directly to "out", without setting "parent". So there does not seem to be any spinlock reacquire on this path, or am I misunderstanding how the "runtime_status" works ?

Quoted 3 lines

In the meantime it sets
->power.runtime_status so that __pm_runtime_idle() will fail if run at this point.

runtime_status will be left at "RPM_ACTIVE", which is the appropriate value expected by __pm_runtime_idle.

Quoted 11 lines
Quoted 10 lines

6) (execute the body of __pm_request_resume and return)
7) __pm_runtime_put() (sync = true)
8) if (atomic_dec_and_test(&dev->power.usage_count))
(still see usage_count == 1 before decrement,
thus decrement to 0)
9) pm_runtime_idle()
10) spin_unlock_irqrestore(&dev->power.lock, flags)
11) spin_lock_irq(&dev->power.lock);
12) retval = __pm_runtime_idle(dev);

Quoted 2 lines

Moreover, __pm_runtime_idle() checks ->power.usage_count under the spinlock, so it will see it's been incremented in the meantime and it will back off.

This is a subtle but important point. Yes, my scenario seems to be dealt with by the extra usage_count check while the spinlock is held.

How about adding a comment under this atomic_inc() stating that the memory barriers are implicitely dealt with by the following spinlock release and the extra check while spinlock is held ?

Commenting memory barriers is important, but commenting why memory barriers are not needed due to a subtle corner-case looks even more important.

(hrm, but more below considering pm_runtime_get_noresume())

Quoted 6 lines
Quoted 6 lines

13) spin_unlock_irq(&dev->power.lock);

So we end up in a situation where CPU A expects the device to be resumed, but the last action performed has been to bring it to idle.

Quoted 8 lines
Quoted 1 line

A smp_mb__after_atomic_inc() between lines 2 and 3 would fix this.

I don't think this particular race is possible. However, there is another one that seems to be possible (in a different function) that an explicit barrier will prevent from happening.

It's related to pm_runtime_get_noresume(), but I think it's better to put the barrier where it's necessary rather than into pm_runtime_get_noresume() itself.

Quoting your following mail:

Quoted 3 lines

Actually, no. Since rpm_idle() and rpm_suspend() both check usage_count under the spinlock, the race I was thinking about doesn't appear to be possible after all.

Hrm, for the extra-usage_count-under-spinlock check to work, all pm_runtime_get_noresume() callers should grab and release the dev->power.lock after incrementing the usage_count. This does not seem to be the case though. So you might really have a race there.

So every code path that does:

1) pm_runtime_get_noresume(dev);

2) ...

3) pm_runtime_put_noidle(dev);

expecting that the device state cannot be changed between 1 and 3 might be surprised by a concurrent call to __pm_runtime_idle() that would put a device to idle (or similarly with suspend) due to lack of memory barrier after the atomic increment.

Or am I missing something else ?

Thanks,

Mathieu

Quoted 7 lines

Thanks,
Rafael
--
To unsubscribe from this list: send the line "unsubscribe linux-trace-users" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#62 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 11 lines · Thomas Renninger
Quoted 10 lines · Rafael J. Wysocki
Quoted 8 lines · Thomas Renninger
Quoted 3 lines

Ok, that's at least generic. Needs the review of Rafael, to determine whether this state value is all we want to know when we enter suspend.

He already gave an acked-by on this generic one here:
Re: [PATCH 3/4] perf: add calls to suspend trace point
Oh now, that was on the X86 specific part which depends on this one. One should expect that he's fine with the generic part as well then, but I agree that he should definitely have a look at this and sign it off.

Quoted 5 lines
Quoted 1 line

What patch exactly do you mean? I'm not quite sure from your comment above.

Eh, Jean's patch, sorry about that.
Needs a tiny change to use PWR_EVENT_EXIT instead of 0 with my new patch series:

No problem with that as far as I'm concerned.

Thanks,
Rafael

Quoted 32 lines
---
 kernel/power/suspend.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
kernel/power/suspend.c +3 -0
index 7335952..10cad5c 100644
@@ -22,6 +22,7 @@
 #include <linux/mm.h>
 #include <linux/slab.h>
 #include <linux/suspend.h>
+#include <trace/events/power.h>
 
 #include "power.h"
 
@@ -164,7 +165,9 @@ static int suspend_enter(suspend_state_t state)
        error = sysdev_suspend(PMSG_SUSPEND);
        if (!error) {
                if (!suspend_test(TEST_CORE) && pm_check_wakeup_events()) {
+                       trace_machine_suspend(state);
                        error = suspend_ops->enter(state);
+                       trace_machine_suspend(0);
                        events_check_enabled = false;
                }
                sysdev_resume();
#63 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Thomas Renninger
Quoted 11 lines · Thomas Renninger
Quoted 10 lines · Rafael J. Wysocki
Quoted 8 lines · Thomas Renninger
Quoted 3 lines

Ok, that's at least generic. Needs the review of Rafael, to determine whether this state value is all we want to know when we enter suspend.

He already gave an acked-by on this generic one here:
Re: [PATCH 3/4] perf: add calls to suspend trace point
Oh now, that was on the X86 specific part which depends on this one. One should expect that he's fine with the generic part as well then, but I agree that he should definitely have a look at this and sign it off.

Quoted 5 lines
Quoted 1 line

What patch exactly do you mean? I'm not quite sure from your comment above.

Eh, Jean's patch, sorry about that.
Needs a tiny change to use PWR_EVENT_EXIT instead of 0 with my new patch series:

No problem with that as far as I'm concerned.

Thanks,
Rafael

Quoted 32 lines
---
 kernel/power/suspend.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
kernel/power/suspend.c +3 -0
index 7335952..10cad5c 100644
@@ -22,6 +22,7 @@
 #include <linux/mm.h>
 #include <linux/slab.h>
 #include <linux/suspend.h>
+#include <trace/events/power.h>
 
 #include "power.h"
 
@@ -164,7 +165,9 @@ static int suspend_enter(suspend_state_t state)
        error = sysdev_suspend(PMSG_SUSPEND);
        if (!error) {
                if (!suspend_test(TEST_CORE) && pm_check_wakeup_events()) {
+                       trace_machine_suspend(state);
                        error = suspend_ops->enter(state);
+                       trace_machine_suspend(0);
                        events_check_enabled = false;
                }
                sysdev_resume();
#93 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Mathieu Desnoyers
Quoted 63 lines · Mathieu Desnoyers
Quoted 58 lines · * Rafael J. Wysocki ([email protected]) wrote:
Quoted 48 lines · Mathieu Desnoyers
Quoted 26 lines · * Alan Stern ([email protected]) wrote:
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

Quoted 7 lines

I don't understand. What's the problem? The inc/dec are atomic because they are not protected by spinlocks, but everything else is (aside from the tracepoint, which is new).

Quoted 1 line

kref should certainly be used there.

What for?

kref has the following "get":

        atomic_inc(&kref->refcount);
        smp_mb__after_atomic_inc();

What seems to be missing in __pm_runtime_get() and pm_runtime_get_noresume() is the memory barrier after the atomic increment. The atomic increment is free to be reordered into the following spinlock (within pm_request_resume or pm_request resume execution) because taking a spinlock only acts as a memory barrier with acquire semantic, not a full memory barrier.

So AFAIU, the failure scenario would be as follows (sorry for the 80+ columns):

Quoted 8 lines

initial conditions: usage_count = 1

CPU A CPU B
1) __pm_runtime_get() (sync = true)
2) atomic_inc(&usage_count) (not committed to memory yet)
3) pm_runtime_resume()
4) spin_lock_irqsave(&dev->power.lock, flags);
5) retval = __pm_request_resume(dev);

Quoted 3 lines

If sync = true this is

           retval = __pm_runtime_resume(dev);

which drops and reacquires the spinlock.

Quoted 8 lines

Let's see. Upon entry in __pm_runtime_resume, the following condition holds (remember, the initial condition is that usage_count == 1):

dev->power.runtime_status == RPM_ACTIVE

so retval is set to 1, which goto directly to "out", without setting "parent". So there does not seem to be any spinlock reacquire on this path, or am I misunderstanding how the "runtime_status" works ?

No, you're not I think, the above is correct. I was referring to the scenario in which the device was RPM_SUSPENDED initially.

Quoted 24 lines
Quoted 3 lines

In the meantime it sets
->power.runtime_status so that __pm_runtime_idle() will fail if run at this point.

runtime_status will be left at "RPM_ACTIVE", which is the appropriate value expected by __pm_runtime_idle.

Quoted 11 lines
Quoted 10 lines

6) (execute the body of __pm_request_resume and return)
7) __pm_runtime_put() (sync = true)
8) if (atomic_dec_and_test(&dev->power.usage_count))
(still see usage_count == 1 before decrement,
thus decrement to 0)
9) pm_runtime_idle()
10) spin_unlock_irqrestore(&dev->power.lock, flags)
11) spin_lock_irq(&dev->power.lock);
12) retval = __pm_runtime_idle(dev);

Quoted 2 lines

Moreover, __pm_runtime_idle() checks ->power.usage_count under the spinlock, so it will see it's been incremented in the meantime and it will back off.

This is a subtle but important point. Yes, my scenario seems to be dealt with by the extra usage_count check while the spinlock is held.

Quoted 6 lines

How about adding a comment under this atomic_inc() stating that the memory barriers are implicitely dealt with by the following spinlock release and the extra check while spinlock is held ?

Commenting memory barriers is important, but commenting why memory barriers are not needed due to a subtle corner-case looks even more important.

Well, given that this discussion is taking place at all, I admit that it would be good to document this somehow. :-)

I'll take care of that.

Quoted 34 lines

(hrm, but more below considering pm_runtime_get_noresume())

Quoted 6 lines
Quoted 6 lines

13) spin_unlock_irq(&dev->power.lock);

So we end up in a situation where CPU A expects the device to be resumed, but the last action performed has been to bring it to idle.

Quoted 8 lines
Quoted 1 line

A smp_mb__after_atomic_inc() between lines 2 and 3 would fix this.

I don't think this particular race is possible. However, there is another one that seems to be possible (in a different function) that an explicit barrier will prevent from happening.

It's related to pm_runtime_get_noresume(), but I think it's better to put the barrier where it's necessary rather than into pm_runtime_get_noresume() itself.

Quoting your following mail:

Quoted 3 lines

Actually, no. Since rpm_idle() and rpm_suspend() both check usage_count under the spinlock, the race I was thinking about doesn't appear to be possible after all.

Hrm, for the extra-usage_count-under-spinlock check to work, all pm_runtime_get_noresume() callers should grab and release the dev->power.lock after incrementing the usage_count. This does not seem to be the case though. So you might really have a race there.

So every code path that does:

1) pm_runtime_get_noresume(dev);

2) ...

Quoted 8 lines

3) pm_runtime_put_noidle(dev);

expecting that the device state cannot be changed between 1 and 3 might be surprised by a concurrent call to __pm_runtime_idle() that would put a device to idle (or similarly with suspend) due to lack of memory barrier after the atomic increment.

Or am I missing something else ?

First of all, the device can always be resumed regardless of the usage_count value. usage_count is only used to block attempts to suspend the device and execute its driver's ->runtime_idle() callback after it has been resumed. That's why the "normal" pm_runtime_get() queues up a resume request.

IOW, the _get() only becomes meaningful after attempting to resume the device (which is what I tried to tell Arjan in one of the previous messages).

Second, there's no synchronization between pm_runtime_get_noresume() and pm_runtime_suspend/idle() etc., so calling pm_runtime_get_noresume() is certainly insufficient to block pm_runtime_suspend/idle() regardless of memory barriers (there may be one already in progress when _get_noresume() is called). To limit possible status changes from happening one should (at least) run pm_runtime_barrier() (surprise, no? ;-)) after pm_runtime_get_noresume().

So if you don't want to resume the device immediately after increasing its usage_count (in which case it's better to use pm_runtime_get_sync()), you should do something like this:

1) pm_runtime_get_noresume(dev);
1a) pm_runtime_barrier(dev); // That takes care of all pending requests etc.

2) ...

3) pm_runtime_put_noidle(dev);

[The meaning of pm_runtime_barrier() is that all of the runtime PM activity started before the barrier has been completed when it returns.]

There's one place in the PM core where that really is necessary, but I wouldn't recommend anyone doing anything like it in a driver.

Thanks,
Rafael

#94 lore raw

Re: [linux-pm] [PATCH] PERF(kernel): Cleanup power events V2

In reply to Mathieu Desnoyers
Quoted 63 lines · Mathieu Desnoyers
Quoted 58 lines · * Rafael J. Wysocki ([email protected]) wrote:
Quoted 48 lines · Mathieu Desnoyers
Quoted 26 lines · * Alan Stern ([email protected]) wrote:
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

Quoted 7 lines

I don't understand. What's the problem? The inc/dec are atomic because they are not protected by spinlocks, but everything else is (aside from the tracepoint, which is new).

Quoted 1 line

kref should certainly be used there.

What for?

kref has the following "get":

        atomic_inc(&kref->refcount);
        smp_mb__after_atomic_inc();

What seems to be missing in __pm_runtime_get() and pm_runtime_get_noresume() is the memory barrier after the atomic increment. The atomic increment is free to be reordered into the following spinlock (within pm_request_resume or pm_request resume execution) because taking a spinlock only acts as a memory barrier with acquire semantic, not a full memory barrier.

So AFAIU, the failure scenario would be as follows (sorry for the 80+ columns):

Quoted 8 lines

initial conditions: usage_count = 1

CPU A CPU B
1) __pm_runtime_get() (sync = true)
2) atomic_inc(&usage_count) (not committed to memory yet)
3) pm_runtime_resume()
4) spin_lock_irqsave(&dev->power.lock, flags);
5) retval = __pm_request_resume(dev);

Quoted 3 lines

If sync = true this is

           retval = __pm_runtime_resume(dev);

which drops and reacquires the spinlock.

Quoted 8 lines

Let's see. Upon entry in __pm_runtime_resume, the following condition holds (remember, the initial condition is that usage_count == 1):

dev->power.runtime_status == RPM_ACTIVE

so retval is set to 1, which goto directly to "out", without setting "parent". So there does not seem to be any spinlock reacquire on this path, or am I misunderstanding how the "runtime_status" works ?

No, you're not I think, the above is correct. I was referring to the scenario in which the device was RPM_SUSPENDED initially.

Quoted 24 lines
Quoted 3 lines

In the meantime it sets
->power.runtime_status so that __pm_runtime_idle() will fail if run at this point.

runtime_status will be left at "RPM_ACTIVE", which is the appropriate value expected by __pm_runtime_idle.

Quoted 11 lines
Quoted 10 lines

6) (execute the body of __pm_request_resume and return)
7) __pm_runtime_put() (sync = true)
8) if (atomic_dec_and_test(&dev->power.usage_count))
(still see usage_count == 1 before decrement,
thus decrement to 0)
9) pm_runtime_idle()
10) spin_unlock_irqrestore(&dev->power.lock, flags)
11) spin_lock_irq(&dev->power.lock);
12) retval = __pm_runtime_idle(dev);

Quoted 2 lines

Moreover, __pm_runtime_idle() checks ->power.usage_count under the spinlock, so it will see it's been incremented in the meantime and it will back off.

This is a subtle but important point. Yes, my scenario seems to be dealt with by the extra usage_count check while the spinlock is held.

Quoted 6 lines

How about adding a comment under this atomic_inc() stating that the memory barriers are implicitely dealt with by the following spinlock release and the extra check while spinlock is held ?

Commenting memory barriers is important, but commenting why memory barriers are not needed due to a subtle corner-case looks even more important.

Well, given that this discussion is taking place at all, I admit that it would be good to document this somehow. :-)

I'll take care of that.

Quoted 34 lines

(hrm, but more below considering pm_runtime_get_noresume())

Quoted 6 lines
Quoted 6 lines

13) spin_unlock_irq(&dev->power.lock);

So we end up in a situation where CPU A expects the device to be resumed, but the last action performed has been to bring it to idle.

Quoted 8 lines
Quoted 1 line

A smp_mb__after_atomic_inc() between lines 2 and 3 would fix this.

I don't think this particular race is possible. However, there is another one that seems to be possible (in a different function) that an explicit barrier will prevent from happening.

It's related to pm_runtime_get_noresume(), but I think it's better to put the barrier where it's necessary rather than into pm_runtime_get_noresume() itself.

Quoting your following mail:

Quoted 3 lines

Actually, no. Since rpm_idle() and rpm_suspend() both check usage_count under the spinlock, the race I was thinking about doesn't appear to be possible after all.

Hrm, for the extra-usage_count-under-spinlock check to work, all pm_runtime_get_noresume() callers should grab and release the dev->power.lock after incrementing the usage_count. This does not seem to be the case though. So you might really have a race there.

So every code path that does:

1) pm_runtime_get_noresume(dev);

2) ...

Quoted 8 lines

3) pm_runtime_put_noidle(dev);

expecting that the device state cannot be changed between 1 and 3 might be surprised by a concurrent call to __pm_runtime_idle() that would put a device to idle (or similarly with suspend) due to lack of memory barrier after the atomic increment.

Or am I missing something else ?

First of all, the device can always be resumed regardless of the usage_count value. usage_count is only used to block attempts to suspend the device and execute its driver's ->runtime_idle() callback after it has been resumed. That's why the "normal" pm_runtime_get() queues up a resume request.

IOW, the _get() only becomes meaningful after attempting to resume the device (which is what I tried to tell Arjan in one of the previous messages).

Second, there's no synchronization between pm_runtime_get_noresume() and pm_runtime_suspend/idle() etc., so calling pm_runtime_get_noresume() is certainly insufficient to block pm_runtime_suspend/idle() regardless of memory barriers (there may be one already in progress when _get_noresume() is called). To limit possible status changes from happening one should (at least) run pm_runtime_barrier() (surprise, no? ;-)) after pm_runtime_get_noresume().

So if you don't want to resume the device immediately after increasing its usage_count (in which case it's better to use pm_runtime_get_sync()), you should do something like this:

1) pm_runtime_get_noresume(dev);
1a) pm_runtime_barrier(dev); // That takes care of all pending requests etc.

2) ...

3) pm_runtime_put_noidle(dev);

[The meaning of pm_runtime_barrier() is that all of the runtime PM activity started before the barrier has been completed when it returns.]

There's one place in the PM core where that really is necessary, but I wouldn't recommend anyone doing anything like it in a driver.

Thanks,
Rafael

#95 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

Quoted 67 lines · * Rafael J. Wysocki ([email protected]) wrote:
Quoted 66 lines · Mathieu Desnoyers
Quoted 58 lines · * Rafael J. Wysocki ([email protected]) wrote:
Quoted 48 lines · Mathieu Desnoyers
Quoted 26 lines · * Alan Stern ([email protected]) wrote:
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

Quoted 7 lines

I don't understand. What's the problem? The inc/dec are atomic because they are not protected by spinlocks, but everything else is (aside from the tracepoint, which is new).

Quoted 1 line

kref should certainly be used there.

What for?

kref has the following "get":

        atomic_inc(&kref->refcount);
        smp_mb__after_atomic_inc();

What seems to be missing in __pm_runtime_get() and pm_runtime_get_noresume() is the memory barrier after the atomic increment. The atomic increment is free to be reordered into the following spinlock (within pm_request_resume or pm_request resume execution) because taking a spinlock only acts as a memory barrier with acquire semantic, not a full memory barrier.

So AFAIU, the failure scenario would be as follows (sorry for the 80+ columns):

Quoted 8 lines

initial conditions: usage_count = 1

CPU A CPU B
1) __pm_runtime_get() (sync = true)
2) atomic_inc(&usage_count) (not committed to memory yet)
3) pm_runtime_resume()
4) spin_lock_irqsave(&dev->power.lock, flags);
5) retval = __pm_request_resume(dev);

Quoted 3 lines

If sync = true this is

           retval = __pm_runtime_resume(dev);

which drops and reacquires the spinlock.

Let's see. Upon entry in __pm_runtime_resume, the following condition holds (remember, the initial condition is that usage_count == 1):

Quoted 8 lines
Quoted 5 lines

dev->power.runtime_status == RPM_ACTIVE

so retval is set to 1, which goto directly to "out", without setting "parent". So there does not seem to be any spinlock reacquire on this path, or am I misunderstanding how the "runtime_status" works ?

No, you're not I think, the above is correct. I was referring to the scenario in which the device was RPM_SUSPENDED initially.

Good to know I'm not losing it. ;-)

Quoted 29 lines
Quoted 29 lines
Quoted 4 lines

In the meantime it sets
->power.runtime_status so that __pm_runtime_idle() will fail if run at this point.

runtime_status will be left at "RPM_ACTIVE", which is the appropriate value expected by __pm_runtime_idle.

Quoted 11 lines
Quoted 10 lines

6) (execute the body of __pm_request_resume and return)
7) __pm_runtime_put() (sync = true)
8) if (atomic_dec_and_test(&dev->power.usage_count))
(still see usage_count == 1 before decrement,
thus decrement to 0)
9) pm_runtime_idle()
10) spin_unlock_irqrestore(&dev->power.lock, flags)
11) spin_lock_irq(&dev->power.lock);
12) retval = __pm_runtime_idle(dev);

Quoted 2 lines

Moreover, __pm_runtime_idle() checks ->power.usage_count under the spinlock, so it will see it's been incremented in the meantime and it will back off.

This is a subtle but important point. Yes, my scenario seems to be dealt with by the extra usage_count check while the spinlock is held.

How about adding a comment under this atomic_inc() stating that the memory barriers are implicitely dealt with by the following spinlock release and the extra check while spinlock is held ?

Quoted 5 lines
Quoted 2 lines

Commenting memory barriers is important, but commenting why memory barriers are not needed due to a subtle corner-case looks even more important.

Well, given that this discussion is taking place at all, I admit that it would be good to document this somehow. :-)

Yep, it's astonishing how a few comments can end up saving lots of emails from confused reviewers. ;-)

Quoted 46 lines

I'll take care of that.

Quoted 34 lines

(hrm, but more below considering pm_runtime_get_noresume())

Quoted 6 lines
Quoted 6 lines

13) spin_unlock_irq(&dev->power.lock);

So we end up in a situation where CPU A expects the device to be resumed, but the last action performed has been to bring it to idle.

Quoted 8 lines
Quoted 1 line

A smp_mb__after_atomic_inc() between lines 2 and 3 would fix this.

I don't think this particular race is possible. However, there is another one that seems to be possible (in a different function) that an explicit barrier will prevent from happening.

It's related to pm_runtime_get_noresume(), but I think it's better to put the barrier where it's necessary rather than into pm_runtime_get_noresume() itself.

Quoting your following mail:

Quoted 3 lines

Actually, no. Since rpm_idle() and rpm_suspend() both check usage_count under the spinlock, the race I was thinking about doesn't appear to be possible after all.

Hrm, for the extra-usage_count-under-spinlock check to work, all pm_runtime_get_noresume() callers should grab and release the dev->power.lock after incrementing the usage_count. This does not seem to be the case though. So you might really have a race there.

So every code path that does:

1) pm_runtime_get_noresume(dev);

2) ...

Quoted 8 lines

3) pm_runtime_put_noidle(dev);

expecting that the device state cannot be changed between 1 and 3 might be surprised by a concurrent call to __pm_runtime_idle() that would put a device to idle (or similarly with suspend) due to lack of memory barrier after the atomic increment.

Or am I missing something else ?

Quoted 7 lines

First of all, the device can always be resumed regardless of the usage_count value. usage_count is only used to block attempts to suspend the device and execute its driver's ->runtime_idle() callback after it has been resumed. That's why the "normal" pm_runtime_get() queues up a resume request.

IOW, the _get() only becomes meaningful after attempting to resume the device (which is what I tried to tell Arjan in one of the previous messages).

OK

Quoted 5 lines

Second, there's no synchronization between pm_runtime_get_noresume() and pm_runtime_suspend/idle() etc., so calling pm_runtime_get_noresume() is certainly insufficient to block pm_runtime_suspend/idle() regardless of memory barriers (there may be one already in progress when _get_noresume() is called).

Agreed, I was wondering how this was expected to work.

Quoted 2 lines

To limit possible status changes from happening one should (at least) run pm_runtime_barrier() (surprise, no? ;-)) after pm_runtime_get_noresume().

Hrm, then why export pm_runtime_get_noresume() at all ? I don't feel comfortable with some of the pm_runtime_get_noresume() callers.

Quoted 10 lines

So if you don't want to resume the device immediately after increasing its usage_count (in which case it's better to use pm_runtime_get_sync()), you should do something like this:

1) pm_runtime_get_noresume(dev);
1a) pm_runtime_barrier(dev); // That takes care of all pending requests etc.

2) ...

Quoted 7 lines

3) pm_runtime_put_noidle(dev);

[The meaning of pm_runtime_barrier() is that all of the runtime PM activity started before the barrier has been completed when it returns.]

There's one place in the PM core where that really is necessary, but I wouldn't recommend anyone doing anything like it in a driver.

grep -r pm_runtime_get_noresume drivers/ hands out very interesting info.

e.g.:

drivers/usb/core/drivers.c: usb_autopm_get_interface_async()

        pm_runtime_get_noresume(&intf->dev);
        s = ACCESS_ONCE(intf->dev.power.runtime_status);
        if (s == RPM_SUSPENDING || s == RPM_SUSPENDED)
                status = pm_request_resume(&intf->dev);

How is this supposed to work ?

If the ACCESS_ONCE can be reordered before the atomic_inc(), then I fear the device can be suspended even after the check.

My point is that a get/put semantic should imply memory barriers, especially if these are exported APIs.

Thanks,

Mathieu

Quoted 3 lines

Thanks,
Rafael

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#96 lore raw

Re: [linux-pm] [PATCH] PERF(kernel): Cleanup power events V2

In reply to Rafael J. Wysocki
Quoted 67 lines · * Rafael J. Wysocki ([email protected]) wrote:
Quoted 66 lines · Mathieu Desnoyers
Quoted 58 lines · * Rafael J. Wysocki ([email protected]) wrote:
Quoted 48 lines · Mathieu Desnoyers
Quoted 26 lines · * Alan Stern ([email protected]) wrote:
Quoted 15 lines · Mathieu Desnoyers
Quoted 6 lines · * Peter Zijlstra ([email protected]) wrote:
Quoted 3 lines · Pierre Tardy
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count); 

That's terribly racy..

Looking at the original code, it looks racy even without considering the tracepoint:

int __pm_runtime_get(struct device *dev, bool sync)

 {
        int retval;
Quoted 8 lines
+       trace_runtime_pm_usage(dev, atomic_read(&dev->power.usage_count)+1);
        atomic_inc(&dev->power.usage_count);
        retval = sync ? pm_runtime_resume(dev) : pm_request_resume(dev);

There is no implied memory barrier after "atomic_inc". So either all these inc/dec are protected with mutexes or spinlocks, in which case one might wonder why atomic operations are used at all, or it's a racy mess. (I vote for the second option)

Quoted 7 lines

I don't understand. What's the problem? The inc/dec are atomic because they are not protected by spinlocks, but everything else is (aside from the tracepoint, which is new).

Quoted 1 line

kref should certainly be used there.

What for?

kref has the following "get":

        atomic_inc(&kref->refcount);
        smp_mb__after_atomic_inc();

What seems to be missing in __pm_runtime_get() and pm_runtime_get_noresume() is the memory barrier after the atomic increment. The atomic increment is free to be reordered into the following spinlock (within pm_request_resume or pm_request resume execution) because taking a spinlock only acts as a memory barrier with acquire semantic, not a full memory barrier.

So AFAIU, the failure scenario would be as follows (sorry for the 80+ columns):

Quoted 8 lines

initial conditions: usage_count = 1

CPU A CPU B
1) __pm_runtime_get() (sync = true)
2) atomic_inc(&usage_count) (not committed to memory yet)
3) pm_runtime_resume()
4) spin_lock_irqsave(&dev->power.lock, flags);
5) retval = __pm_request_resume(dev);

Quoted 3 lines

If sync = true this is

           retval = __pm_runtime_resume(dev);

which drops and reacquires the spinlock.

Let's see. Upon entry in __pm_runtime_resume, the following condition holds (remember, the initial condition is that usage_count == 1):

Quoted 8 lines
Quoted 5 lines

dev->power.runtime_status == RPM_ACTIVE

so retval is set to 1, which goto directly to "out", without setting "parent". So there does not seem to be any spinlock reacquire on this path, or am I misunderstanding how the "runtime_status" works ?

No, you're not I think, the above is correct. I was referring to the scenario in which the device was RPM_SUSPENDED initially.

Good to know I'm not losing it. ;-)

Quoted 29 lines
Quoted 29 lines
Quoted 4 lines

In the meantime it sets
->power.runtime_status so that __pm_runtime_idle() will fail if run at this point.

runtime_status will be left at "RPM_ACTIVE", which is the appropriate value expected by __pm_runtime_idle.

Quoted 11 lines
Quoted 10 lines

6) (execute the body of __pm_request_resume and return)
7) __pm_runtime_put() (sync = true)
8) if (atomic_dec_and_test(&dev->power.usage_count))
(still see usage_count == 1 before decrement,
thus decrement to 0)
9) pm_runtime_idle()
10) spin_unlock_irqrestore(&dev->power.lock, flags)
11) spin_lock_irq(&dev->power.lock);
12) retval = __pm_runtime_idle(dev);

Quoted 2 lines

Moreover, __pm_runtime_idle() checks ->power.usage_count under the spinlock, so it will see it's been incremented in the meantime and it will back off.

This is a subtle but important point. Yes, my scenario seems to be dealt with by the extra usage_count check while the spinlock is held.

How about adding a comment under this atomic_inc() stating that the memory barriers are implicitely dealt with by the following spinlock release and the extra check while spinlock is held ?

Quoted 5 lines
Quoted 2 lines

Commenting memory barriers is important, but commenting why memory barriers are not needed due to a subtle corner-case looks even more important.

Well, given that this discussion is taking place at all, I admit that it would be good to document this somehow. :-)

Yep, it's astonishing how a few comments can end up saving lots of emails from confused reviewers. ;-)

Quoted 46 lines

I'll take care of that.

Quoted 34 lines

(hrm, but more below considering pm_runtime_get_noresume())

Quoted 6 lines
Quoted 6 lines

13) spin_unlock_irq(&dev->power.lock);

So we end up in a situation where CPU A expects the device to be resumed, but the last action performed has been to bring it to idle.

Quoted 8 lines
Quoted 1 line

A smp_mb__after_atomic_inc() between lines 2 and 3 would fix this.

I don't think this particular race is possible. However, there is another one that seems to be possible (in a different function) that an explicit barrier will prevent from happening.

It's related to pm_runtime_get_noresume(), but I think it's better to put the barrier where it's necessary rather than into pm_runtime_get_noresume() itself.

Quoting your following mail:

Quoted 3 lines

Actually, no. Since rpm_idle() and rpm_suspend() both check usage_count under the spinlock, the race I was thinking about doesn't appear to be possible after all.

Hrm, for the extra-usage_count-under-spinlock check to work, all pm_runtime_get_noresume() callers should grab and release the dev->power.lock after incrementing the usage_count. This does not seem to be the case though. So you might really have a race there.

So every code path that does:

1) pm_runtime_get_noresume(dev);

2) ...

Quoted 8 lines

3) pm_runtime_put_noidle(dev);

expecting that the device state cannot be changed between 1 and 3 might be surprised by a concurrent call to __pm_runtime_idle() that would put a device to idle (or similarly with suspend) due to lack of memory barrier after the atomic increment.

Or am I missing something else ?

Quoted 7 lines

First of all, the device can always be resumed regardless of the usage_count value. usage_count is only used to block attempts to suspend the device and execute its driver's ->runtime_idle() callback after it has been resumed. That's why the "normal" pm_runtime_get() queues up a resume request.

IOW, the _get() only becomes meaningful after attempting to resume the device (which is what I tried to tell Arjan in one of the previous messages).

OK

Quoted 5 lines

Second, there's no synchronization between pm_runtime_get_noresume() and pm_runtime_suspend/idle() etc., so calling pm_runtime_get_noresume() is certainly insufficient to block pm_runtime_suspend/idle() regardless of memory barriers (there may be one already in progress when _get_noresume() is called).

Agreed, I was wondering how this was expected to work.

Quoted 2 lines

To limit possible status changes from happening one should (at least) run pm_runtime_barrier() (surprise, no? ;-)) after pm_runtime_get_noresume().

Hrm, then why export pm_runtime_get_noresume() at all ? I don't feel comfortable with some of the pm_runtime_get_noresume() callers.

Quoted 10 lines

So if you don't want to resume the device immediately after increasing its usage_count (in which case it's better to use pm_runtime_get_sync()), you should do something like this:

1) pm_runtime_get_noresume(dev);
1a) pm_runtime_barrier(dev); // That takes care of all pending requests etc.

2) ...

Quoted 7 lines

3) pm_runtime_put_noidle(dev);

[The meaning of pm_runtime_barrier() is that all of the runtime PM activity started before the barrier has been completed when it returns.]

There's one place in the PM core where that really is necessary, but I wouldn't recommend anyone doing anything like it in a driver.

grep -r pm_runtime_get_noresume drivers/ hands out very interesting info.

e.g.:

drivers/usb/core/drivers.c: usb_autopm_get_interface_async()

        pm_runtime_get_noresume(&intf->dev);
        s = ACCESS_ONCE(intf->dev.power.runtime_status);
        if (s == RPM_SUSPENDING || s == RPM_SUSPENDED)
                status = pm_request_resume(&intf->dev);

How is this supposed to work ?

If the ACCESS_ONCE can be reordered before the atomic_inc(), then I fear the device can be suspended even after the check.

My point is that a get/put semantic should imply memory barriers, especially if these are exported APIs.

Thanks,

Mathieu

Quoted 3 lines

Thanks,
Rafael

Signature · 4 lines
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
#97 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

[CC: list trimmed drastically, on the assumption that most of the people on it aren't very interested in the details of the PM runtime memory barriers.]

Quoted 4 lines · Mathieu Desnoyers

grep -r pm_runtime_get_noresume drivers/ hands out very interesting info.

e.g.:

Quoted 8 lines

drivers/usb/core/drivers.c: usb_autopm_get_interface_async()

        pm_runtime_get_noresume(&intf->dev);
        s = ACCESS_ONCE(intf->dev.power.runtime_status);
        if (s == RPM_SUSPENDING || s == RPM_SUSPENDED)
                status = pm_request_resume(&intf->dev);

How is this supposed to work ?

It's worth pointing out that this code is going to be removed during the 2.6.38 development cycle, due to ongoing changes in the runtime PM core. It would have been removed already if not for the difficulty of coordinating cross-subsystem changes.

But it's legitimate to ask how the code _was_ supposed to work...

Quoted 2 lines

If the ACCESS_ONCE can be reordered before the atomic_inc(), then I fear the device can be suspended even after the check.

You are correct; the code as written may sometimes fail. It was a hack from the beginning; the kind of test it performs should not be done outside the PM core. However at the time it was the easiest way to do what I wanted.

Quoted 2 lines

My point is that a get/put semantic should imply memory barriers, especially if these are exported APIs.

As far as I am aware, apart from the hack above, pm_runtime_get_noresume is called only in places where either:

it is purely advisory (e.g., we know that we will use the device in the near future so we would prefer to prevent it from being suspended, but we don't really care because we're going to call pm_runtime_resume_sync before using it anyway);

or we already know that the usage_count is > 0.

No memory barrier is required for either of these cases.

Alan Stern

--
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/

Geschäftsführender Gesellschafter: Tjark Auerbach
Sitz der Gesellschaft: Tettnang
Handelsregister: Amtsgericht Ulm, HRB 630992
ALLGEMEINE GESCHÄFTSBEDINGUNGEN
Es gelten unsere Allgemeinen Geschäftsbedingungen
(AGB). Sie finden sie in der jeweils gültigen Fassung
im Internet unter http://www.avira.com/de/standard-terms-conditions-business-de
***************************************************
_______________________________________________
linux-pm mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/linux-pm

#98 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Mathieu Desnoyers

[CC: list trimmed drastically, on the assumption that most of the people on it aren't very interested in the details of the PM runtime memory barriers.]

Quoted 4 lines · Mathieu Desnoyers

grep -r pm_runtime_get_noresume drivers/ hands out very interesting info.

e.g.:

Quoted 8 lines

drivers/usb/core/drivers.c: usb_autopm_get_interface_async()

        pm_runtime_get_noresume(&intf->dev);
        s = ACCESS_ONCE(intf->dev.power.runtime_status);
        if (s == RPM_SUSPENDING || s == RPM_SUSPENDED)
                status = pm_request_resume(&intf->dev);

How is this supposed to work ?

It's worth pointing out that this code is going to be removed during the 2.6.38 development cycle, due to ongoing changes in the runtime PM core. It would have been removed already if not for the difficulty of coordinating cross-subsystem changes.

But it's legitimate to ask how the code _was_ supposed to work...

Quoted 2 lines

If the ACCESS_ONCE can be reordered before the atomic_inc(), then I fear the device can be suspended even after the check.

You are correct; the code as written may sometimes fail. It was a hack from the beginning; the kind of test it performs should not be done outside the PM core. However at the time it was the easiest way to do what I wanted.

Quoted 2 lines

My point is that a get/put semantic should imply memory barriers, especially if these are exported APIs.

As far as I am aware, apart from the hack above, pm_runtime_get_noresume is called only in places where either:

it is purely advisory (e.g., we know that we will use the device in the near future so we would prefer to prevent it from being suspended, but we don't really care because we're going to call pm_runtime_resume_sync before using it anyway);

or we already know that the usage_count is > 0.

No memory barrier is required for either of these cases.

Alan Stern

#99 lore raw

Re: [linux-pm] [PATCH] PERF(kernel): Cleanup power events V2

In reply to Mathieu Desnoyers

[CC: list trimmed drastically, on the assumption that most of the people on it aren't very interested in the details of the PM runtime memory barriers.]

Quoted 4 lines · Mathieu Desnoyers

grep -r pm_runtime_get_noresume drivers/ hands out very interesting info.

e.g.:

Quoted 8 lines

drivers/usb/core/drivers.c: usb_autopm_get_interface_async()

        pm_runtime_get_noresume(&intf->dev);
        s = ACCESS_ONCE(intf->dev.power.runtime_status);
        if (s == RPM_SUSPENDING || s == RPM_SUSPENDED)
                status = pm_request_resume(&intf->dev);

How is this supposed to work ?

It's worth pointing out that this code is going to be removed during the 2.6.38 development cycle, due to ongoing changes in the runtime PM core. It would have been removed already if not for the difficulty of coordinating cross-subsystem changes.

But it's legitimate to ask how the code _was_ supposed to work...

Quoted 2 lines

If the ACCESS_ONCE can be reordered before the atomic_inc(), then I fear the device can be suspended even after the check.

You are correct; the code as written may sometimes fail. It was a hack from the beginning; the kind of test it performs should not be done outside the PM core. However at the time it was the easiest way to do what I wanted.

Quoted 2 lines

My point is that a get/put semantic should imply memory barriers, especially if these are exported APIs.

As far as I am aware, apart from the hack above, pm_runtime_get_noresume is called only in places where either:

it is purely advisory (e.g., we know that we will use the device in the near future so we would prefer to prevent it from being suspended, but we don't really care because we're going to call pm_runtime_resume_sync before using it anyway);

or we already know that the usage_count is > 0.

No memory barrier is required for either of these cases.

Alan Stern

#102 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Mathieu Desnoyers
Quoted 1 line · Mathieu Desnoyers

* Rafael J. Wysocki ([email protected]) wrote:

...

Quoted 2 lines

Hrm, then why export pm_runtime_get_noresume() at all ?

Basically, the PM core needs it for some obscure stuff. Beyond that people really should use it with care (preferably avoid using it at all).

Quoted 31 lines

I don't feel comfortable with some of the pm_runtime_get_noresume() callers.

Quoted 10 lines

So if you don't want to resume the device immediately after increasing its usage_count (in which case it's better to use pm_runtime_get_sync()), you should do something like this:

1) pm_runtime_get_noresume(dev);
1a) pm_runtime_barrier(dev); // That takes care of all pending requests etc.

2) ...

Quoted 7 lines

3) pm_runtime_put_noidle(dev);

[The meaning of pm_runtime_barrier() is that all of the runtime PM activity started before the barrier has been completed when it returns.]

There's one place in the PM core where that really is necessary, but I wouldn't recommend anyone doing anything like it in a driver.

grep -r pm_runtime_get_noresume drivers/ hands out very interesting info.

e.g.:

drivers/usb/core/drivers.c: usb_autopm_get_interface_async()

        pm_runtime_get_noresume(&intf->dev);
        s = ACCESS_ONCE(intf->dev.power.runtime_status);
        if (s == RPM_SUSPENDING || s == RPM_SUSPENDED)
                status = pm_request_resume(&intf->dev);
Quoted 7 lines

How is this supposed to work ?

If the ACCESS_ONCE can be reordered before the atomic_inc(), then I fear the device can be suspended even after the check.

My point is that a get/put semantic should imply memory barriers, especially if these are exported APIs.

Well, IMO adding a memory barrier to pm_runtime_get_noresume() wouldn't really change a lot (it still would be racy with respect to some other runtime PM helper funtions). That said I guess we should put a "handle with care" sticker on it.

Thanks,
Rafael

#103 lore raw

Re: [linux-pm] [PATCH] PERF(kernel): Cleanup power events V2

In reply to Mathieu Desnoyers
Quoted 1 line · Mathieu Desnoyers

* Rafael J. Wysocki ([email protected]) wrote:

...

Quoted 2 lines

Hrm, then why export pm_runtime_get_noresume() at all ?

Basically, the PM core needs it for some obscure stuff. Beyond that people really should use it with care (preferably avoid using it at all).

Quoted 31 lines

I don't feel comfortable with some of the pm_runtime_get_noresume() callers.

Quoted 10 lines

So if you don't want to resume the device immediately after increasing its usage_count (in which case it's better to use pm_runtime_get_sync()), you should do something like this:

1) pm_runtime_get_noresume(dev);
1a) pm_runtime_barrier(dev); // That takes care of all pending requests etc.

2) ...

Quoted 7 lines

3) pm_runtime_put_noidle(dev);

[The meaning of pm_runtime_barrier() is that all of the runtime PM activity started before the barrier has been completed when it returns.]

There's one place in the PM core where that really is necessary, but I wouldn't recommend anyone doing anything like it in a driver.

grep -r pm_runtime_get_noresume drivers/ hands out very interesting info.

e.g.:

drivers/usb/core/drivers.c: usb_autopm_get_interface_async()

        pm_runtime_get_noresume(&intf->dev);
        s = ACCESS_ONCE(intf->dev.power.runtime_status);
        if (s == RPM_SUSPENDING || s == RPM_SUSPENDED)
                status = pm_request_resume(&intf->dev);
Quoted 7 lines

How is this supposed to work ?

If the ACCESS_ONCE can be reordered before the atomic_inc(), then I fear the device can be suspended even after the check.

My point is that a get/put semantic should imply memory barriers, especially if these are exported APIs.

Well, IMO adding a memory barrier to pm_runtime_get_noresume() wouldn't really change a lot (it still would be racy with respect to some other runtime PM helper funtions). That said I guess we should put a "handle with care" sticker on it.

Thanks,
Rafael

#100 lore raw

Re: [PATCH] PERF(kernel): Cleanup power events V2

In reply to Alan Stern
Quoted 15 lines · Alan Stern
Quoted 12 lines · Mathieu Desnoyers

grep -r pm_runtime_get_noresume drivers/ hands out very interesting info.

e.g.:

drivers/usb/core/drivers.c: usb_autopm_get_interface_async()

        pm_runtime_get_noresume(&intf->dev);
        s = ACCESS_ONCE(intf->dev.power.runtime_status);
        if (s == RPM_SUSPENDING || s == RPM_SUSPENDED)
                status = pm_request_resume(&intf->dev);

How is this supposed to work ?

Quoted 7 lines
Quoted 2 lines

If the ACCESS_ONCE can be reordered before the atomic_inc(), then I fear the device can be suspended even after the check.

You are correct; the code as written may sometimes fail. It was a hack from the beginning; the kind of test it performs should not be done outside the PM core. However at the time it was the easiest way to do what I wanted.

I forgot to mention one other thing... The fact that this code will sometimes behave unexpectedly isn't a bug. That function is documented as requiring additional locking when a driver uses it. The need for extra locking is unavoidable because I/O requests can arrive at any time, even while a runtime suspend is in progress.

Therefore the fact that usb_autopm_get_interface_async() can race with a runtime suspend doesn't matter. The driver making the call should have sufficient locking to know that the runtime suspend should fail because the driver is busy.

Alan Stern

#101 lore raw

Re: [linux-pm] [PATCH] PERF(kernel): Cleanup power events V2

In reply to Alan Stern
Quoted 15 lines · Alan Stern
Quoted 12 lines · Mathieu Desnoyers

grep -r pm_runtime_get_noresume drivers/ hands out very interesting info.

e.g.:

drivers/usb/core/drivers.c: usb_autopm_get_interface_async()

        pm_runtime_get_noresume(&intf->dev);
        s = ACCESS_ONCE(intf->dev.power.runtime_status);
        if (s == RPM_SUSPENDING || s == RPM_SUSPENDED)
                status = pm_request_resume(&intf->dev);

How is this supposed to work ?

Quoted 7 lines
Quoted 2 lines

If the ACCESS_ONCE can be reordered before the atomic_inc(), then I fear the device can be suspended even after the check.

You are correct; the code as written may sometimes fail. It was a hack from the beginning; the kind of test it performs should not be done outside the PM core. However at the time it was the easiest way to do what I wanted.

I forgot to mention one other thing... The fact that this code will sometimes behave unexpectedly isn't a bug. That function is documented as requiring additional locking when a driver uses it. The need for extra locking is unavoidable because I/O requests can arrive at any time, even while a runtime suspend is in progress.

Therefore the fact that usb_autopm_get_interface_async() can race with a runtime suspend doesn't matter. The driver making the call should have sufficient locking to know that the runtime suspend should fail because the driver is busy.

Alan Stern