Skip to contentSkip to conversation

tracing/sched: Fix task states in sched_switch event

PATCH In review Carsten Emde 2 participants 6 messages May 16, 2010 – May 17, 2010

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover tracing/sched: Fix task states in sched_switch event

Hi Ingo,

Quoted 6 lines

The whole enumeration there is pointless in that .c file - it tells nothing to the code reader.

If it cannot be expressed in a meaningful way then introduce TASK_STATE_STRINGS_INIT construct that is defined next to the strings (in a .h file or so) - that way it's a coherent whole.

The updated patch
- has all definitions at a common place,
- adapts the related comment,
- makes the string array declaration in fs/proc/array.c a one-liner.

Carsten.

#1 lore raw

PATCH 1/1 fix-task-states-in-sched_switch-event.patch

The sched_switch trace event displays erroneous character codes of task states, after a new task state was added in the scheduler code but omitted to add in the trace event code.

Define character codes of task states individually. In addition, define task state descriptions needed in /proc and in the sched_switch trace event at the same place. This will help to keep the task state bits, characters and descriptions in sync should they ever need to be changed again.

CC: Ingo Molnar <[email protected]>
CC: Steven Rostedt <[email protected]>
CC: Andrew Morton <[email protected]>
CC: Frederic Weisbecker <[email protected]>
CC: Peter Zijlstra <[email protected]>
LKML-Reference: <[email protected]>

---
 fs/proc/array.c              |   19 -------------
 include/linux/sched.h        |   62 ++++++++++++++++++++++++++++++++++++++++---
 include/trace/events/sched.h |   12 ++++++--
 3 files changed, 69 insertions(+), 24 deletions(-)
fs/proc/array.c +1 -18
Index: head/fs/proc/array.c
===================================================================
@@ -125,24 +125,7 @@ static inline void task_name(struct seq_
 	seq_printf(m, "\n");
 }
 
-/*
- * The task state array is a strange "bitmap" of
- * reasons to sleep. Thus "running" is zero, and
- * you can test for combinations of others with
- * simple bit tests.
- */
-static const char *task_state_array[] = {
-	"R (running)",		/*   0 */
-	"S (sleeping)",		/*   1 */
-	"D (disk sleep)",	/*   2 */
-	"T (stopped)",		/*   4 */
-	"t (tracing stop)",	/*   8 */
-	"Z (zombie)",		/*  16 */
-	"X (dead)",		/*  32 */
-	"x (dead)",		/*  64 */
-	"K (wakekill)",		/* 128 */
-	"W (waking)",		/* 256 */
-};
+static const char *task_state_array[] = TASK_STATE_STRINGS_INIT;
 
 static inline const char *get_task_state(struct task_struct *tsk)
 {
include/linux/sched.h +59 -3

93 lines not on this page · raw message

include/trace/events/sched.h +9 -3
Index: head/include/trace/events/sched.h
===================================================================
@@ -149,11 +149,17 @@ TRACE_EVENT(sched_switch,
 		__entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
 		__entry->prev_state ?
 		  __print_flags(__entry->prev_state, "|",
-				{ 1, "S"} , { 2, "D" }, { 4, "T" }, { 8, "t" },
-				{ 16, "Z" }, { 32, "X" }, { 64, "x" },
-				{ 128, "W" }) : "R",
+			{ 1, TASK_STATE_1} , { 2, TASK_STATE_2 },
+			{ 4, TASK_STATE_4 }, { 8, TASK_STATE_8 },
+			{ 16, TASK_STATE_16 }, { 32, TASK_STATE_32 },
+			{ 64, TASK_STATE_64 }, { 128, TASK_STATE_128 },
+			{ 256, TASK_STATE_256 }
+			) : TASK_STATE_0,
 		__entry->next_comm, __entry->next_pid, __entry->next_prio)
 );
+#if TASK_STATE_MAX != 512
+#error "Please add new task state array in __print_flags() above."
+#endif
 
 /*
  * Tracepoint for a task being migrated:
maintainer
#2 lore raw

Re: [PATCH 1/1] fix-task-states-in-sched_switch-event.patch

Quoted 31 lines · Carsten Emde

#define TASK_RUNNING 0

include/linux/sched.h +21 -0
+#define TASK_STATE_0           "R"
+#define TASK_STATE_NAME_0      "running"
+
 #define TASK_INTERRUPTIBLE     1
+#define TASK_STATE_1           "S"
+#define TASK_STATE_NAME_1      "sleeping"
+
 #define TASK_UNINTERRUPTIBLE   2
+#define TASK_STATE_2           "D"
+#define TASK_STATE_NAME_2      "disk sleep"
+
 #define __TASK_STOPPED         4
+#define TASK_STATE_4           "T"
+#define TASK_STATE_NAME_4      "stopped"
+
 #define __TASK_TRACED          8
+#define TASK_STATE_8           "t"
+#define TASK_STATE_NAME_8      "tracing stop"
+
 /* in tsk->exit_state */
 #define EXIT_ZOMBIE            16
+#define TASK_STATE_16          "Z"
+#define TASK_STATE_NAME_16     "zombie"
+
 #define EXIT_DEAD              32
+#define TASK_STATE_32          "X"
+#define TASK_STATE_NAME_32     "dead"
+
 /* in tsk->state again */
 #define TASK_DEAD              64

include/linux/sched.h print_cfs_rq(struct seq_file *m, int cpu · line 231

Quoted 10 lines
include/linux/sched.h +8 -0
+#define TASK_STATE_64          "x"
+#define TASK_STATE_NAME_64     "dead"
+
 #define TASK_WAKEKILL          128
+#define TASK_STATE_128         "K"
+#define TASK_STATE_NAME_128    "wakekill"
+
 #define TASK_WAKING            256
+#define TASK_STATE_256         "W"
+#define TASK_STATE_NAME_256    "waking"

Since we all love vile macro magic, is the below any better?

include/linux/task_states.h

TASK_STATE(RUNNING, "R", "running")
TASK_STATE(INTERRUPTIBLE, "S", "sleeping")
...

include/linux/sched.h

enum {
#define TASK_STATE(tstate, tstate_c, tstate_s) __TASK_##tstate, #include <linux/task_states.h>
#undef TASK_STATE
};

enum {

#define TASK_STATE(tstate, tstate_c, tstate_s) \
	TASK_##tstate = 1 << __TASK_##tstate,

#include <linux/task_states.h>
#undef TASK_STATE
};

const char *task_state_to_char =
#define TASK_STATE(tstate, tstate_c, tstate_s) tstate_c #include <linux/task_states.h>
#undef TASK_STATE
;

const char *task_state_to_string[] = {
#define TASK_STATE(tstate, tstate_c, tstate_s) tstate_s, #include <linux/task_states.h>
#undef TASK_STATE
};

#3 lore raw

Re: [PATCH 1/1] fix-task-states-in-sched_switch-event.patch

Hi Peter,

Quoted 44 lines
Quoted 31 lines
  #define TASK_RUNNING           0
(quoted diff) +21 -0
+#define TASK_STATE_0           "R"
+#define TASK_STATE_NAME_0      "running"
+
  #define TASK_INTERRUPTIBLE     1
+#define TASK_STATE_1           "S"
+#define TASK_STATE_NAME_1      "sleeping"
+
  #define TASK_UNINTERRUPTIBLE   2
+#define TASK_STATE_2           "D"
+#define TASK_STATE_NAME_2      "disk sleep"
+
  #define __TASK_STOPPED         4
+#define TASK_STATE_4           "T"
+#define TASK_STATE_NAME_4      "stopped"
+
  #define __TASK_TRACED          8
+#define TASK_STATE_8           "t"
+#define TASK_STATE_NAME_8      "tracing stop"
+
  /* in tsk->exit_state */
  #define EXIT_ZOMBIE            16
+#define TASK_STATE_16          "Z"
+#define TASK_STATE_NAME_16     "zombie"
+
  #define EXIT_DEAD              32
+#define TASK_STATE_32          "X"
+#define TASK_STATE_NAME_32     "dead"
+
  /* in tsk->state again */
  #define TASK_DEAD              64

Review

Quoted 10 lines
(quoted diff) +8 -0
+#define TASK_STATE_64          "x"
+#define TASK_STATE_NAME_64     "dead"
+
  #define TASK_WAKEKILL          128
+#define TASK_STATE_128         "K"
+#define TASK_STATE_NAME_128    "wakekill"
+
  #define TASK_WAKING            256
+#define TASK_STATE_256         "W"
+#define TASK_STATE_NAME_256    "waking"

Since we all love vile macro magic, is the below any better?

Quoted 5 lines

include/linux/task_states.h

TASK_STATE(RUNNING, "R", "running")
TASK_STATE(INTERRUPTIBLE, "S", "sleeping")
...

Well, yes, this looks very nice and is perfectly readable and maintainable.

Quoted 19 lines

enum {
#define TASK_STATE(tstate, tstate_c, tstate_s) __TASK_##tstate, #include<linux/task_states.h>
#undef TASK_STATE
};

enum {

#define TASK_STATE(tstate, tstate_c, tstate_s) \
	TASK_##tstate = 1<<  __TASK_##tstate,

#include<linux/task_states.h>
#undef TASK_STATE
};

const char *task_state_to_char =
#define TASK_STATE(tstate, tstate_c, tstate_s) tstate_c #include<linux/task_states.h>
#undef TASK_STATE
;

Quoted 5 lines

const char *task_state_to_string[] = {
#define TASK_STATE(tstate, tstate_c, tstate_s) tstate_s, #include<linux/task_states.h>
#undef TASK_STATE
};

I find this section less convincing (although certainly indistinguishable from magic).

In addition, we need to take care of the various state name prefixes TASK, __TASK and EXIT and name clashes:
TASK_RUNNING
TASK_INTERRUPTIBLE
TASK_UNINTERRUPTIBLE
__TASK_STOPPED
__TASK_TRACED
EXIT_ZOMBIE
EXIT_DEAD
TASK_DEAD
TASK_WAKEKILL
TASK_WAKING

And we still need to maintain the defines in include/trace/events/ sched.h:
{ 1, TASK_STATE_1 } , { 2, TASK_STATE_2 },
{ 4, TASK_STATE_4 }, { 8, TASK_STATE_8 },
{ 16, TASK_STATE_16 }, { 32, TASK_STATE_32 },
{ 64, TASK_STATE_64 }, { 128, TASK_STATE_128 },
{ 256, TASK_STATE_256 }
) : TASK_STATE_0,

If we could use a general approach for all states, I would immediately go for your proposal. But since we anyway need to define the states individually, I would vote for the current version of the patch.

Or would you prefer to simply apply a minimal fix to correct the erroneous output of the sched_switch event and to leave the rest as an exercise for the future?

Carsten.

---
 include/trace/events/sched.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/trace/events/sched.h +1 -1
Index: head/include/trace/events/sched.h
===================================================================
@@ -151,7 +151,7 @@ TRACE_EVENT(sched_switch,
 		  __print_flags(__entry->prev_state, "|",
 				{ 1, "S"} , { 2, "D" }, { 4, "T" }, { 8, "t" },
 				{ 16, "Z" }, { 32, "X" }, { 64, "x" },
-				{ 128, "W" }) : "R",
+				{ 128, "K" }, { 256, "W" }) : "R",
 		__entry->next_comm, __entry->next_pid, __entry->next_prio)
 );
 
maintainer
#4 lore raw

Re: [PATCH 1/1] fix-task-states-in-sched_switch-event.patch

Quoted 37 lines · Carsten Emde
Quoted 7 lines

Since we all love vile macro magic, is the below any better?

include/linux/task_states.h

TASK_STATE(RUNNING, "R", "running")
TASK_STATE(INTERRUPTIBLE, "S", "sleeping")
...

Well, yes, this looks very nice and is perfectly readable and maintainable.

Quoted 19 lines

enum {
#define TASK_STATE(tstate, tstate_c, tstate_s) __TASK_##tstate, #include<linux/task_states.h>
#undef TASK_STATE
};

enum {

#define TASK_STATE(tstate, tstate_c, tstate_s) \
	TASK_##tstate = 1<<  __TASK_##tstate,

#include<linux/task_states.h>
#undef TASK_STATE
};

const char *task_state_to_char =
#define TASK_STATE(tstate, tstate_c, tstate_s) tstate_c #include<linux/task_states.h>
#undef TASK_STATE
;

Quoted 5 lines

const char *task_state_to_string[] = {
#define TASK_STATE(tstate, tstate_c, tstate_s) tstate_s, #include<linux/task_states.h>
#undef TASK_STATE
};

I find this section less convincing (although certainly indistinguishable from magic).

Quoted 12 lines

In addition, we need to take care of the various state name prefixes TASK, __TASK and EXIT and name clashes:
TASK_RUNNING
TASK_INTERRUPTIBLE
TASK_UNINTERRUPTIBLE
__TASK_STOPPED
__TASK_TRACED
EXIT_ZOMBIE
EXIT_DEAD
TASK_DEAD
TASK_WAKEKILL
TASK_WAKING

We could manually add:

#define EXIT_ZOMBIE TASK_ZOMBIE
#define EXIT_DEAD TASK_DEAD

But those two __TASK ones are unfortunate indeed.

Quoted 8 lines

And we still need to maintain the defines in include/trace/events/ sched.h:
{ 1, TASK_STATE_1 } , { 2, TASK_STATE_2 },
{ 4, TASK_STATE_4 }, { 8, TASK_STATE_8 },
{ 16, TASK_STATE_16 }, { 32, TASK_STATE_32 },
{ 64, TASK_STATE_64 }, { 128, TASK_STATE_128 },
{ 256, TASK_STATE_256 }
) : TASK_STATE_0,

#define TASK_STATE(tstate, tstate_c, tstate_s) \
	{ __TASK_##tstate, tstate_c },

#include <linux/task_state.h>
#undef TASK_STATE

Should get you mostly there I guess, trick would be making the user deal with { 0, "R" }

Quoted 7 lines

If we could use a general approach for all states, I would immediately go for your proposal. But since we anyway need to define the states individually, I would vote for the current version of the patch.

Or would you prefer to simply apply a minimal fix to correct the erroneous output of the sched_switch event and to leave the rest as an exercise for the future?

Dunno, I guess we can do with your version, just wanted to mention this method.

#5 lore raw

Re: [PATCH 1/1] fix-task-states-in-sched_switch-event.patch

Quoted 80 lines · Peter Zijlstra
Quoted 37 lines · Carsten Emde
Quoted 7 lines

Since we all love vile macro magic, is the below any better?

include/linux/task_states.h

TASK_STATE(RUNNING, "R", "running")
TASK_STATE(INTERRUPTIBLE, "S", "sleeping")
...

Well, yes, this looks very nice and is perfectly readable and maintainable.

Quoted 19 lines

enum {
#define TASK_STATE(tstate, tstate_c, tstate_s) __TASK_##tstate, #include<linux/task_states.h>
#undef TASK_STATE
};

enum {

#define TASK_STATE(tstate, tstate_c, tstate_s) \
	TASK_##tstate = 1<<   __TASK_##tstate,

#include<linux/task_states.h>
#undef TASK_STATE
};

const char *task_state_to_char =
#define TASK_STATE(tstate, tstate_c, tstate_s) tstate_c #include<linux/task_states.h>
#undef TASK_STATE
;

Quoted 5 lines

const char *task_state_to_string[] = {
#define TASK_STATE(tstate, tstate_c, tstate_s) tstate_s, #include<linux/task_states.h>
#undef TASK_STATE
};

I find this section less convincing (although certainly indistinguishable from magic).

Quoted 12 lines

In addition, we need to take care of the various state name prefixes TASK, __TASK and EXIT and name clashes:
TASK_RUNNING
TASK_INTERRUPTIBLE
TASK_UNINTERRUPTIBLE
__TASK_STOPPED
__TASK_TRACED
EXIT_ZOMBIE
EXIT_DEAD
TASK_DEAD
TASK_WAKEKILL
TASK_WAKING

We could manually add:

#define EXIT_ZOMBIE TASK_ZOMBIE
#define EXIT_DEAD TASK_DEAD

But those two __TASK ones are unfortunate indeed.

Quoted 8 lines

And we still need to maintain the defines in include/trace/events/ sched.h:
{ 1, TASK_STATE_1 } , { 2, TASK_STATE_2 },
{ 4, TASK_STATE_4 }, { 8, TASK_STATE_8 },
{ 16, TASK_STATE_16 }, { 32, TASK_STATE_32 },
{ 64, TASK_STATE_64 }, { 128, TASK_STATE_128 },
{ 256, TASK_STATE_256 }
) : TASK_STATE_0,

#define TASK_STATE(tstate, tstate_c, tstate_s) \
	{ __TASK_##tstate, tstate_c },

#include<linux/task_state.h>
#undef TASK_STATE

Should get you mostly there I guess, trick would be making the user deal with { 0, "R" }

Quoted 4 lines

If we could use a general approach for all states, I would immediately go for your proposal. But since we anyway need to define the states individually, I would vote for the current version of the patch.

Quoted 6 lines
Quoted 3 lines

Or would you prefer to simply apply a minimal fix to correct the erroneous output of the sched_switch event and to leave the rest as an exercise for the future?

Dunno, I guess we can do with your version, just wanted to mention this method.

Yes, thanks, your method is great - much better than using intermediate scripts and similar. However, this approach works best, if you start with a new design and you are free to arrange everything to build on it. At least, we should have this approach in mind when making upgrade changes in the related code - maybe, your method will fit better one day.

Carsten.