Skip to content

Commit 1593baa

Browse files
Peter Zijlstraingomolnar
authored andcommitted
sched/debug: Implement consistent task-state printing
Currently get_task_state() and task_state_to_char() report different states, create a number of common helpers and unify the reported state space. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-kernel@vger.kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 770b782 commit 1593baa

2 files changed

Lines changed: 21 additions & 20 deletions

File tree

‎fs/proc/array.c‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,8 @@ static const char * const task_state_array[] = {
130130

131131
static inline const char *get_task_state(struct task_struct *tsk)
132132
{
133-
unsigned int state = (tsk->state | tsk->exit_state) & TASK_REPORT;
134-
135-
/*
136-
* Parked tasks do not run; they sit in __kthread_parkme().
137-
* Without this check, we would report them as running, which is
138-
* clearly wrong, so we report them as sleeping instead.
139-
*/
140-
if (tsk->state == TASK_PARKED)
141-
state = TASK_INTERRUPTIBLE;
142-
143-
BUILD_BUG_ON(1 + ilog2(TASK_REPORT) != ARRAY_SIZE(task_state_array)-1);
144-
145-
return task_state_array[fls(state)];
133+
BUILD_BUG_ON(1 + ilog2(TASK_REPORT) != ARRAY_SIZE(task_state_array) - 1);
134+
return task_state_array[__get_task_state(tsk)];
146135
}
147136

148137
static inline int get_task_umask(struct task_struct *tsk)

‎include/linux/sched.h‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,17 +1243,29 @@ static inline pid_t task_pgrp_nr(struct task_struct *tsk)
12431243
return task_pgrp_nr_ns(tsk, &init_pid_ns);
12441244
}
12451245

1246-
static inline char task_state_to_char(struct task_struct *task)
1246+
static inline unsigned int __get_task_state(struct task_struct *tsk)
12471247
{
1248-
const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
1249-
unsigned long state = task->state;
1248+
unsigned int tsk_state = READ_ONCE(tsk->state);
1249+
unsigned int state = (tsk_state | tsk->exit_state) & TASK_REPORT;
12501250

1251-
state = state ? __ffs(state) + 1 : 0;
1251+
if (tsk_state == TASK_PARKED)
1252+
state = TASK_INTERRUPTIBLE;
12521253

1253-
/* Make sure the string lines up properly with the number of task states: */
1254-
BUILD_BUG_ON(sizeof(TASK_STATE_TO_CHAR_STR)-1 != ilog2(TASK_STATE_MAX)+1);
1254+
return fls(state);
1255+
}
1256+
1257+
static inline char __task_state_to_char(unsigned int state)
1258+
{
1259+
static const char state_char[] = "RSDTtXZ";
1260+
1261+
BUILD_BUG_ON(1 + ilog2(TASK_REPORT) != sizeof(state_char) - 2);
12551262

1256-
return state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?';
1263+
return state_char[state];
1264+
}
1265+
1266+
static inline char task_state_to_char(struct task_struct *tsk)
1267+
{
1268+
return __task_state_to_char(__get_task_state(tsk));
12571269
}
12581270

12591271
/**

0 commit comments

Comments
 (0)