linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] perf sched: fix wrong conversion of task state
@ 2016-07-27 16:07 Tomoki Sekiyama
  2016-07-27 16:52 ` David Ahern
  2016-07-27 23:43 ` Masami Hiramatsu
  0 siblings, 2 replies; 4+ messages in thread
From: Tomoki Sekiyama @ 2016-07-27 16:07 UTC (permalink / raw)
  To: linux-kernel
  Cc: ltc-kernel, masumi.moritani.ju, Tomoki Sekiyama, Jiri Olsa,
	David Ahern, Namhyung Kim, Peter Zijlstra, Masami Hiramatsu

sched_out_state() converts the prev_state u64 bitmask to a char in
a wrong way, which may cause wrong results of 'perf sched latency'.
This patch fixes the conversion.
Also, preempted tasks must be considered that they are in the
THREAD_WAIT_CPU state.

Signed-off-by: Tomoki Sekiyama <tomoki.sekiyama.qu@hitachi.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
---
 tools/perf/builtin-sched.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index 0dfe8df..8651c36 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -70,7 +70,8 @@ struct sched_atom {
 	struct task_desc	*wakee;
 };
 
-#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
+/* TASK_STATE_MAX means the task is preempted(R+). Use '+' for it here. */
+#define TASK_STATE_TO_CHAR_STR "RSDTtXZxKWPN+"
 
 enum thread_state {
 	THREAD_SLEEPING = 0,
@@ -897,9 +898,10 @@ static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
 
 static char sched_out_state(u64 prev_state)
 {
-	const char *str = TASK_STATE_TO_CHAR_STR;
+	const char str[] = TASK_STATE_TO_CHAR_STR;
+	unsigned int bit = prev_state ? __ffs(prev_state) + 1 : 0;
 
-	return str[prev_state];
+	return bit < sizeof(str) - 1 ? str[bit] : '?';
 }
 
 static int
@@ -915,7 +917,7 @@ add_sched_out_event(struct work_atoms *atoms,
 
 	atom->sched_out_time = timestamp;
 
-	if (run_state == 'R') {
+	if (run_state == 'R' || run_state == '+') {
 		atom->state = THREAD_WAIT_CPU;
 		atom->wake_up_time = atom->sched_out_time;
 	}
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] perf sched: fix wrong conversion of task state
  2016-07-27 16:07 [PATCH v2] perf sched: fix wrong conversion of task state Tomoki Sekiyama
@ 2016-07-27 16:52 ` David Ahern
  2016-07-27 23:43 ` Masami Hiramatsu
  1 sibling, 0 replies; 4+ messages in thread
From: David Ahern @ 2016-07-27 16:52 UTC (permalink / raw)
  To: Tomoki Sekiyama, linux-kernel
  Cc: ltc-kernel, masumi.moritani.ju, Jiri Olsa, Namhyung Kim,
	Peter Zijlstra, Masami Hiramatsu

On 7/27/16 10:07 AM, Tomoki Sekiyama wrote:
> sched_out_state() converts the prev_state u64 bitmask to a char in
> a wrong way, which may cause wrong results of 'perf sched latency'.
> This patch fixes the conversion.
> Also, preempted tasks must be considered that they are in the
> THREAD_WAIT_CPU state.

you reference fixing the order of XZ as well.

>
> Signed-off-by: Tomoki Sekiyama <tomoki.sekiyama.qu@hitachi.com>
> Cc: Jiri Olsa <jolsa@kernel.org>
> Cc: David Ahern <dsahern@gmail.com>
> Cc: Namhyung Kim <namhyung@kernel.org>
> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
> Cc: Masami Hiramatsu <mhiramat@kernel.org>
> ---
>  tools/perf/builtin-sched.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
> index 0dfe8df..8651c36 100644
> --- a/tools/perf/builtin-sched.c
> +++ b/tools/perf/builtin-sched.c
> @@ -70,7 +70,8 @@ struct sched_atom {
>  	struct task_desc	*wakee;
>  };
>
> -#define TASK_STATE_TO_CHAR_STR "RSDTtZXxKWP"
> +/* TASK_STATE_MAX means the task is preempted(R+). Use '+' for it here. */
> +#define TASK_STATE_TO_CHAR_STR "RSDTtXZxKWPN+"

As I mentioned in a previous reply you can't add the '+' on the end.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] perf sched: fix wrong conversion of task state
  2016-07-27 16:07 [PATCH v2] perf sched: fix wrong conversion of task state Tomoki Sekiyama
  2016-07-27 16:52 ` David Ahern
@ 2016-07-27 23:43 ` Masami Hiramatsu
  2016-07-29  4:00   ` Tomoki Sekiyama
  1 sibling, 1 reply; 4+ messages in thread
From: Masami Hiramatsu @ 2016-07-27 23:43 UTC (permalink / raw)
  To: Tomoki Sekiyama
  Cc: linux-kernel, ltc-kernel, masumi.moritani.ju, Jiri Olsa,
	David Ahern, Namhyung Kim, Peter Zijlstra, Masami Hiramatsu

On Thu, 28 Jul 2016 01:07:40 +0900
Tomoki Sekiyama <tomoki.sekiyama.qu@hitachi.com> wrote:

> sched_out_state() converts the prev_state u64 bitmask to a char in
> a wrong way, which may cause wrong results of 'perf sched latency'.
> This patch fixes the conversion.
> Also, preempted tasks must be considered that they are in the
> THREAD_WAIT_CPU state.

Hmm, this includes several fixes and enhancements.
1. Use first bit of the state instead of state itself (critical bug to avoid crash?)
2. Check the range of the array and return '?' if out (minor bug, it can access data area)
3. Fix TASK_STATE_TO_CHAR_STR to swap X and Z.
4. Add new 'N+' to TASK_STATE_TO_CHAR_STR. (how about 'n'?)
5. Treat a preempted task as THREAD_WAIT_CPU.

so IMHO, it is better to split this patch into atleast 2, #1 and #2 (critical bugfix),
#3, #4, and #5 (minor update).

[..]
> @@ -897,9 +898,10 @@ static int thread_atoms_insert(struct perf_sched *sched, struct thread *thread)
>  
>  static char sched_out_state(u64 prev_state)
>  {
> -	const char *str = TASK_STATE_TO_CHAR_STR;
> +	const char str[] = TASK_STATE_TO_CHAR_STR;
> +	unsigned int bit = prev_state ? __ffs(prev_state) + 1 : 0;
>  
> -	return str[prev_state];
> +	return bit < sizeof(str) - 1 ? str[bit] : '?';

You'd better use ARRAY_SIZE(str) instead of sizeof() for array here.

Thank you,


-- 
Masami Hiramatsu <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [PATCH v2] perf sched: fix wrong conversion of task state
  2016-07-27 23:43 ` Masami Hiramatsu
@ 2016-07-29  4:00   ` Tomoki Sekiyama
  0 siblings, 0 replies; 4+ messages in thread
From: Tomoki Sekiyama @ 2016-07-29  4:00 UTC (permalink / raw)
  To: 'Masami Hiramatsu'
  Cc: linux-kernel, ltc-kernel,
	森谷真寿美 /
	MORITANI,MASUMI, Jiri Olsa, David Ahern, Namhyung Kim,
	Peter Zijlstra

Hi Hiramatsu-san,

On 2016/7/28, 2016 8:43, Masami Hiramatsu wrote:
> Hmm, this includes several fixes and enhancements.
> 1. Use first bit of the state instead of state itself (critical bug to
avoid crash?)
> 2. Check the range of the array and return '?' if out (minor bug, it can
access data area)
> 3. Fix TASK_STATE_TO_CHAR_STR to swap X and Z.
> 4. Add new 'N+' to TASK_STATE_TO_CHAR_STR. (how about 'n'?)
> 5. Treat a preempted task as THREAD_WAIT_CPU.
>
> so IMHO, it is better to split this patch into atleast 2, #1 and #2
(critical bugfix),
> #3, #4, and #5 (minor update).

This time I will fix only the invalid array access and
adapting to the current kernel TASK_STATE, and leave the
preempted task handling for later follow-up.

 [..]
>> @@ -897,9 +898,10 @@ static int thread_atoms_insert(struct perf_sched
*sched, struct thread *thread)
>> +	return bit < sizeof(str) - 1 ? str[bit] : '?';
>
> You'd better use ARRAY_SIZE(str) instead of sizeof() for array here.

OK, will change this to use ARRAY_SIZE on the next update.

Thanks,
Tomoki Sekiyama

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-07-29  4:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-27 16:07 [PATCH v2] perf sched: fix wrong conversion of task state Tomoki Sekiyama
2016-07-27 16:52 ` David Ahern
2016-07-27 23:43 ` Masami Hiramatsu
2016-07-29  4:00   ` Tomoki Sekiyama

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).