Skip to contentSkip to conversation

sched/core: Introduce set_next_task() helper for better code readability

PATCH In review Muchun Song 2 participants 3 messages Nov 1, 2018 – Nov 2, 2018 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 sched/core: Introduce set_next_task() helper for better code readability

When we pick the next task, we will do the following for the task:
1) p->se.exec_start = rq_clock_task(rq);
2) dequeue_pushable{_dl}_task(rq, p);

When we call set_curr_task_{rt,dl}(), we also need to do the same thing above. In rt.c, the code at 1) is in the _pick_next_task_rt() and the code at 2) is in the pick_next_task_rt(). If we put two operations in one function, maybe better. So, we introduce a new function set_next_task(), which is responsible for doing the above.

By introducing the function we can get rid of calling the dequeue_pushable{_dl}_task() directly(We can call set_next_task()) in pick_next_task_{rt,dl}() and have better code readability and reuse. In set_curr_task_{rt,dl}(), we also can call set_next_task().

Do this things such that we end up with:

static struct task_struct *pick_next_task_{rt,dl}(struct rq *rq,
struct task_struct *prev, struct rq_flags *rf)
{
/* do something else ... */

  	put_prev_task(rq, prev);

/* pick next task p */

  	set_next_task(rq, p);

  	/* do something else ... */
  }

put_prev_task() can match set_next_task(), which can make the code more readable.

---
 kernel/sched/deadline.c | 19 ++++++++++---------
 kernel/sched/rt.c       | 26 ++++++++++++--------------
 2 files changed, 22 insertions(+), 23 deletions(-)
kernel/sched/deadline.c +10 -9
index 91e4202b0634..470ba6b464fe 100644
@@ -1695,6 +1695,14 @@ static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
 }
 #endif
 
+static inline void set_next_task(struct rq *rq, struct task_struct *p)
+{
+	p->se.exec_start = rq_clock_task(rq);
+
+	/* You can't push away the running task */
+	dequeue_pushable_dl_task(rq, p);
+}
+
 static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
 						   struct dl_rq *dl_rq)
 {
@@ -1750,10 +1758,8 @@ pick_next_task_dl(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
 	BUG_ON(!dl_se);
 
 	p = dl_task_of(dl_se);
-	p->se.exec_start = rq_clock_task(rq);
 
-	/* Running task will never be pushed. */
-       dequeue_pushable_dl_task(rq, p);
+	set_next_task(rq, p);
 
 	if (hrtick_enabled(rq))
 		start_hrtick_dl(rq, p);
@@ -1808,12 +1814,7 @@ static void task_fork_dl(struct task_struct *p)
 
 static void set_curr_task_dl(struct rq *rq)
 {
-	struct task_struct *p = rq->curr;
-
-	p->se.exec_start = rq_clock_task(rq);
-
-	/* You can't push away the running task */
-	dequeue_pushable_dl_task(rq, p);
+	set_next_task(rq, rq->curr);
 }
 
 #ifdef CONFIG_SMP
kernel/sched/rt.c +12 -14
index a21ea6021929..481bb7aa25c5 100644
@@ -1498,6 +1498,14 @@ static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flag
 #endif
 }
 
+static inline void set_next_task(struct rq *rq, struct task_struct *p)
+{
+	p->se.exec_start = rq_clock_task(rq);
+
+	/* The running task is never eligible for pushing */
+	dequeue_pushable_task(rq, p);
+}
+
 static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
 						   struct rt_rq *rt_rq)
 {
@@ -1518,8 +1526,7 @@ static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
 static struct task_struct *_pick_next_task_rt(struct rq *rq)
 {
 	struct sched_rt_entity *rt_se;
-	struct task_struct *p;
-	struct rt_rq *rt_rq  = &rq->rt;
+	struct rt_rq *rt_rq = &rq->rt;
 
 	do {
 		rt_se = pick_next_rt_entity(rq, rt_rq);
@@ -1527,10 +1534,7 @@ static struct task_struct *_pick_next_task_rt(struct rq *rq)
 		rt_rq = group_rt_rq(rt_se);
 	} while (rt_rq);
 
-	p = rt_task_of(rt_se);
-	p->se.exec_start = rq_clock_task(rq);
-
-	return p;
+	return rt_task_of(rt_se);
 }
 
 static struct task_struct *
@@ -1573,8 +1577,7 @@ pick_next_task_rt(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
 
 	p = _pick_next_task_rt(rq);
 
-	/* The running task is never eligible for pushing */
-	dequeue_pushable_task(rq, p);
+	set_next_task(rq, p);
 
 	rt_queue_push_tasks(rq);
 
@@ -2355,12 +2358,7 @@ static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
 
 static void set_curr_task_rt(struct rq *rq)
 {
-	struct task_struct *p = rq->curr;
-
-	p->se.exec_start = rq_clock_task(rq);
-
-	/* The running task is never eligible for pushing */
-	dequeue_pushable_task(rq, p);
+	set_next_task(rq, rq->curr);
 }
 
 static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
Signature · 1 line
2.17.1
maintainer
#1 lore raw

What if anything is the difference with v1 (which I found yesterday and have pending testing).

#2 lore raw

Hi, Peter

Thanks for your review.

Just update commit message. So there is no difference between them on the code.

Yours,
Muchun Song

Quoted 5 lines · Peter Zijlstra <[email protected]> 于2018年11月2日周五 上午12:52写道:

What if anything is the difference with v1 (which I found yesterday and have pending testing).