sched_fair.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /*
  2. * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  5. *
  6. * Interactivity improvements by Mike Galbraith
  7. * (C) 2007 Mike Galbraith <efault@gmx.de>
  8. *
  9. * Various enhancements by Dmitry Adamushko.
  10. * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
  11. *
  12. * Group scheduling enhancements by Srivatsa Vaddagiri
  13. * Copyright IBM Corporation, 2007
  14. * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
  15. *
  16. * Scaled math optimizations by Thomas Gleixner
  17. * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
  18. *
  19. * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
  20. * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  21. */
  22. /*
  23. * Targeted preemption latency for CPU-bound tasks:
  24. * (default: 20ms, units: nanoseconds)
  25. *
  26. * NOTE: this latency value is not the same as the concept of
  27. * 'timeslice length' - timeslices in CFS are of variable length.
  28. * (to see the precise effective timeslice length of your workload,
  29. * run vmstat and monitor the context-switches field)
  30. *
  31. * On SMP systems the value of this is multiplied by the log2 of the
  32. * number of CPUs. (i.e. factor 2x on 2-way systems, 3x on 4-way
  33. * systems, 4x on 8-way systems, 5x on 16-way systems, etc.)
  34. * Targeted preemption latency for CPU-bound tasks:
  35. */
  36. const_debug unsigned int sysctl_sched_latency = 20000000ULL;
  37. /*
  38. * After fork, child runs first. (default) If set to 0 then
  39. * parent will (try to) run first.
  40. */
  41. const_debug unsigned int sysctl_sched_child_runs_first = 1;
  42. /*
  43. * Minimal preemption granularity for CPU-bound tasks:
  44. * (default: 2 msec, units: nanoseconds)
  45. */
  46. unsigned int sysctl_sched_min_granularity __read_mostly = 2000000ULL;
  47. /*
  48. * sys_sched_yield() compat mode
  49. *
  50. * This option switches the agressive yield implementation of the
  51. * old scheduler back on.
  52. */
  53. unsigned int __read_mostly sysctl_sched_compat_yield;
  54. /*
  55. * SCHED_BATCH wake-up granularity.
  56. * (default: 25 msec, units: nanoseconds)
  57. *
  58. * This option delays the preemption effects of decoupled workloads
  59. * and reduces their over-scheduling. Synchronous workloads will still
  60. * have immediate wakeup/sleep latencies.
  61. */
  62. const_debug unsigned int sysctl_sched_batch_wakeup_granularity = 25000000UL;
  63. /*
  64. * SCHED_OTHER wake-up granularity.
  65. * (default: 1 msec, units: nanoseconds)
  66. *
  67. * This option delays the preemption effects of decoupled workloads
  68. * and reduces their over-scheduling. Synchronous workloads will still
  69. * have immediate wakeup/sleep latencies.
  70. */
  71. const_debug unsigned int sysctl_sched_wakeup_granularity = 2000000UL;
  72. extern struct sched_class fair_sched_class;
  73. /**************************************************************
  74. * CFS operations on generic schedulable entities:
  75. */
  76. #ifdef CONFIG_FAIR_GROUP_SCHED
  77. /* cpu runqueue to which this cfs_rq is attached */
  78. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  79. {
  80. return cfs_rq->rq;
  81. }
  82. /* An entity is a task if it doesn't "own" a runqueue */
  83. #define entity_is_task(se) (!se->my_q)
  84. #else /* CONFIG_FAIR_GROUP_SCHED */
  85. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  86. {
  87. return container_of(cfs_rq, struct rq, cfs);
  88. }
  89. #define entity_is_task(se) 1
  90. #endif /* CONFIG_FAIR_GROUP_SCHED */
  91. static inline struct task_struct *task_of(struct sched_entity *se)
  92. {
  93. return container_of(se, struct task_struct, se);
  94. }
  95. /**************************************************************
  96. * Scheduling class tree data structure manipulation methods:
  97. */
  98. static inline u64
  99. max_vruntime(u64 min_vruntime, u64 vruntime)
  100. {
  101. if ((vruntime > min_vruntime) ||
  102. (min_vruntime > (1ULL << 61) && vruntime < (1ULL << 50)))
  103. min_vruntime = vruntime;
  104. return min_vruntime;
  105. }
  106. static inline void
  107. set_leftmost(struct cfs_rq *cfs_rq, struct rb_node *leftmost)
  108. {
  109. struct sched_entity *se;
  110. cfs_rq->rb_leftmost = leftmost;
  111. if (leftmost)
  112. se = rb_entry(leftmost, struct sched_entity, run_node);
  113. }
  114. static inline s64
  115. entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
  116. {
  117. return se->vruntime - cfs_rq->min_vruntime;
  118. }
  119. /*
  120. * Enqueue an entity into the rb-tree:
  121. */
  122. static void
  123. __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  124. {
  125. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  126. struct rb_node *parent = NULL;
  127. struct sched_entity *entry;
  128. s64 key = entity_key(cfs_rq, se);
  129. int leftmost = 1;
  130. /*
  131. * Find the right place in the rbtree:
  132. */
  133. while (*link) {
  134. parent = *link;
  135. entry = rb_entry(parent, struct sched_entity, run_node);
  136. /*
  137. * We dont care about collisions. Nodes with
  138. * the same key stay together.
  139. */
  140. if (key < entity_key(cfs_rq, entry)) {
  141. link = &parent->rb_left;
  142. } else {
  143. link = &parent->rb_right;
  144. leftmost = 0;
  145. }
  146. }
  147. /*
  148. * Maintain a cache of leftmost tree entries (it is frequently
  149. * used):
  150. */
  151. if (leftmost)
  152. set_leftmost(cfs_rq, &se->run_node);
  153. rb_link_node(&se->run_node, parent, link);
  154. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  155. }
  156. static void
  157. __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  158. {
  159. if (cfs_rq->rb_leftmost == &se->run_node)
  160. set_leftmost(cfs_rq, rb_next(&se->run_node));
  161. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  162. }
  163. static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
  164. {
  165. return cfs_rq->rb_leftmost;
  166. }
  167. static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
  168. {
  169. return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
  170. }
  171. static inline struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
  172. {
  173. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  174. struct sched_entity *se = NULL;
  175. struct rb_node *parent;
  176. while (*link) {
  177. parent = *link;
  178. se = rb_entry(parent, struct sched_entity, run_node);
  179. link = &parent->rb_right;
  180. }
  181. return se;
  182. }
  183. /**************************************************************
  184. * Scheduling class statistics methods:
  185. */
  186. static u64 __sched_period(unsigned long nr_running)
  187. {
  188. u64 period = sysctl_sched_latency;
  189. unsigned long nr_latency =
  190. sysctl_sched_latency / sysctl_sched_min_granularity;
  191. if (unlikely(nr_running > nr_latency)) {
  192. period *= nr_running;
  193. do_div(period, nr_latency);
  194. }
  195. return period;
  196. }
  197. static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  198. {
  199. u64 period = __sched_period(cfs_rq->nr_running);
  200. period *= se->load.weight;
  201. do_div(period, cfs_rq->load.weight);
  202. return period;
  203. }
  204. static u64 __sched_vslice(unsigned long nr_running)
  205. {
  206. u64 period = __sched_period(nr_running);
  207. do_div(period, nr_running);
  208. return period;
  209. }
  210. /*
  211. * Update the current task's runtime statistics. Skip current tasks that
  212. * are not in our scheduling class.
  213. */
  214. static inline void
  215. __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
  216. unsigned long delta_exec)
  217. {
  218. unsigned long delta_exec_weighted;
  219. u64 next_vruntime, min_vruntime;
  220. schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
  221. curr->sum_exec_runtime += delta_exec;
  222. schedstat_add(cfs_rq, exec_clock, delta_exec);
  223. delta_exec_weighted = delta_exec;
  224. if (unlikely(curr->load.weight != NICE_0_LOAD)) {
  225. delta_exec_weighted = calc_delta_fair(delta_exec_weighted,
  226. &curr->load);
  227. }
  228. curr->vruntime += delta_exec_weighted;
  229. /*
  230. * maintain cfs_rq->min_vruntime to be a monotonic increasing
  231. * value tracking the leftmost vruntime in the tree.
  232. */
  233. if (first_fair(cfs_rq)) {
  234. next_vruntime = __pick_next_entity(cfs_rq)->vruntime;
  235. /* min_vruntime() := !max_vruntime() */
  236. min_vruntime = max_vruntime(curr->vruntime, next_vruntime);
  237. if (min_vruntime == next_vruntime)
  238. min_vruntime = curr->vruntime;
  239. else
  240. min_vruntime = next_vruntime;
  241. } else
  242. min_vruntime = curr->vruntime;
  243. cfs_rq->min_vruntime =
  244. max_vruntime(cfs_rq->min_vruntime, min_vruntime);
  245. }
  246. static void update_curr(struct cfs_rq *cfs_rq)
  247. {
  248. struct sched_entity *curr = cfs_rq->curr;
  249. u64 now = rq_of(cfs_rq)->clock;
  250. unsigned long delta_exec;
  251. if (unlikely(!curr))
  252. return;
  253. /*
  254. * Get the amount of time the current task was running
  255. * since the last time we changed load (this cannot
  256. * overflow on 32 bits):
  257. */
  258. delta_exec = (unsigned long)(now - curr->exec_start);
  259. __update_curr(cfs_rq, curr, delta_exec);
  260. curr->exec_start = now;
  261. }
  262. static inline void
  263. update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  264. {
  265. schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
  266. }
  267. static inline unsigned long
  268. calc_weighted(unsigned long delta, struct sched_entity *se)
  269. {
  270. unsigned long weight = se->load.weight;
  271. if (unlikely(weight != NICE_0_LOAD))
  272. return (u64)delta * se->load.weight >> NICE_0_SHIFT;
  273. else
  274. return delta;
  275. }
  276. /*
  277. * Task is being enqueued - update stats:
  278. */
  279. static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  280. {
  281. /*
  282. * Are we enqueueing a waiting task? (for current tasks
  283. * a dequeue/enqueue event is a NOP)
  284. */
  285. if (se != cfs_rq->curr)
  286. update_stats_wait_start(cfs_rq, se);
  287. }
  288. static void
  289. update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  290. {
  291. schedstat_set(se->wait_max, max(se->wait_max,
  292. rq_of(cfs_rq)->clock - se->wait_start));
  293. schedstat_set(se->wait_start, 0);
  294. }
  295. static inline void
  296. update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  297. {
  298. update_curr(cfs_rq);
  299. /*
  300. * Mark the end of the wait period if dequeueing a
  301. * waiting task:
  302. */
  303. if (se != cfs_rq->curr)
  304. update_stats_wait_end(cfs_rq, se);
  305. }
  306. /*
  307. * We are picking a new current task - update its stats:
  308. */
  309. static inline void
  310. update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  311. {
  312. /*
  313. * We are starting a new run period:
  314. */
  315. se->exec_start = rq_of(cfs_rq)->clock;
  316. }
  317. /*
  318. * We are descheduling a task - update its stats:
  319. */
  320. static inline void
  321. update_stats_curr_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  322. {
  323. se->exec_start = 0;
  324. }
  325. /**************************************************
  326. * Scheduling class queueing methods:
  327. */
  328. static void
  329. account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  330. {
  331. update_load_add(&cfs_rq->load, se->load.weight);
  332. cfs_rq->nr_running++;
  333. se->on_rq = 1;
  334. }
  335. static void
  336. account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  337. {
  338. update_load_sub(&cfs_rq->load, se->load.weight);
  339. cfs_rq->nr_running--;
  340. se->on_rq = 0;
  341. }
  342. static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  343. {
  344. #ifdef CONFIG_SCHEDSTATS
  345. if (se->sleep_start) {
  346. u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
  347. if ((s64)delta < 0)
  348. delta = 0;
  349. if (unlikely(delta > se->sleep_max))
  350. se->sleep_max = delta;
  351. se->sleep_start = 0;
  352. se->sum_sleep_runtime += delta;
  353. }
  354. if (se->block_start) {
  355. u64 delta = rq_of(cfs_rq)->clock - se->block_start;
  356. if ((s64)delta < 0)
  357. delta = 0;
  358. if (unlikely(delta > se->block_max))
  359. se->block_max = delta;
  360. se->block_start = 0;
  361. se->sum_sleep_runtime += delta;
  362. /*
  363. * Blocking time is in units of nanosecs, so shift by 20 to
  364. * get a milliseconds-range estimation of the amount of
  365. * time that the task spent sleeping:
  366. */
  367. if (unlikely(prof_on == SLEEP_PROFILING)) {
  368. struct task_struct *tsk = task_of(se);
  369. profile_hits(SLEEP_PROFILING, (void *)get_wchan(tsk),
  370. delta >> 20);
  371. }
  372. }
  373. #endif
  374. }
  375. static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
  376. {
  377. #ifdef CONFIG_SCHED_DEBUG
  378. s64 d = se->vruntime - cfs_rq->min_vruntime;
  379. if (d < 0)
  380. d = -d;
  381. if (d > 3*sysctl_sched_latency)
  382. schedstat_inc(cfs_rq, nr_spread_over);
  383. #endif
  384. }
  385. static void
  386. place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
  387. {
  388. u64 vruntime;
  389. vruntime = cfs_rq->min_vruntime;
  390. if (sched_feat(USE_TREE_AVG)) {
  391. struct sched_entity *last = __pick_last_entity(cfs_rq);
  392. if (last) {
  393. vruntime += last->vruntime;
  394. vruntime >>= 1;
  395. }
  396. } else if (sched_feat(APPROX_AVG) && cfs_rq->nr_running)
  397. vruntime += __sched_vslice(cfs_rq->nr_running)/2;
  398. if (initial && sched_feat(START_DEBIT))
  399. vruntime += __sched_vslice(cfs_rq->nr_running + 1);
  400. if (!initial) {
  401. if (sched_feat(NEW_FAIR_SLEEPERS)) {
  402. s64 latency = cfs_rq->min_vruntime - se->vruntime;
  403. if (latency < 0 || !cfs_rq->nr_running)
  404. latency = 0;
  405. else
  406. latency = min_t(s64, latency, sysctl_sched_latency);
  407. vruntime -= latency;
  408. }
  409. vruntime = max(vruntime, se->vruntime);
  410. }
  411. se->vruntime = vruntime;
  412. }
  413. static void
  414. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
  415. {
  416. /*
  417. * Update the fair clock.
  418. */
  419. update_curr(cfs_rq);
  420. if (wakeup) {
  421. /* se->vruntime += cfs_rq->min_vruntime; */
  422. place_entity(cfs_rq, se, 0);
  423. enqueue_sleeper(cfs_rq, se);
  424. }
  425. update_stats_enqueue(cfs_rq, se);
  426. check_spread(cfs_rq, se);
  427. if (se != cfs_rq->curr)
  428. __enqueue_entity(cfs_rq, se);
  429. account_entity_enqueue(cfs_rq, se);
  430. }
  431. static void
  432. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
  433. {
  434. update_stats_dequeue(cfs_rq, se);
  435. if (sleep) {
  436. #ifdef CONFIG_SCHEDSTATS
  437. if (entity_is_task(se)) {
  438. struct task_struct *tsk = task_of(se);
  439. if (tsk->state & TASK_INTERRUPTIBLE)
  440. se->sleep_start = rq_of(cfs_rq)->clock;
  441. if (tsk->state & TASK_UNINTERRUPTIBLE)
  442. se->block_start = rq_of(cfs_rq)->clock;
  443. }
  444. #endif
  445. /* se->vruntime = entity_key(cfs_rq, se); */
  446. se->last_min_vruntime = cfs_rq->min_vruntime;
  447. }
  448. if (se != cfs_rq->curr)
  449. __dequeue_entity(cfs_rq, se);
  450. account_entity_dequeue(cfs_rq, se);
  451. }
  452. /*
  453. * Preempt the current task with a newly woken task if needed:
  454. */
  455. static void
  456. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  457. {
  458. unsigned long ideal_runtime, delta_exec;
  459. ideal_runtime = sched_slice(cfs_rq, curr);
  460. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  461. if (delta_exec > ideal_runtime)
  462. resched_task(rq_of(cfs_rq)->curr);
  463. }
  464. static void
  465. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  466. {
  467. /* 'current' is not kept within the tree. */
  468. if (se->on_rq) {
  469. /*
  470. * Any task has to be enqueued before it get to execute on
  471. * a CPU. So account for the time it spent waiting on the
  472. * runqueue.
  473. */
  474. update_stats_wait_end(cfs_rq, se);
  475. __dequeue_entity(cfs_rq, se);
  476. }
  477. update_stats_curr_start(cfs_rq, se);
  478. cfs_rq->curr = se;
  479. #ifdef CONFIG_SCHEDSTATS
  480. /*
  481. * Track our maximum slice length, if the CPU's load is at
  482. * least twice that of our own weight (i.e. dont track it
  483. * when there are only lesser-weight tasks around):
  484. */
  485. if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
  486. se->slice_max = max(se->slice_max,
  487. se->sum_exec_runtime - se->prev_sum_exec_runtime);
  488. }
  489. #endif
  490. se->prev_sum_exec_runtime = se->sum_exec_runtime;
  491. }
  492. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  493. {
  494. struct sched_entity *se = __pick_next_entity(cfs_rq);
  495. set_next_entity(cfs_rq, se);
  496. return se;
  497. }
  498. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  499. {
  500. /*
  501. * If still on the runqueue then deactivate_task()
  502. * was not called and update_curr() has to be done:
  503. */
  504. if (prev->on_rq)
  505. update_curr(cfs_rq);
  506. update_stats_curr_end(cfs_rq, prev);
  507. check_spread(cfs_rq, prev);
  508. if (prev->on_rq) {
  509. update_stats_wait_start(cfs_rq, prev);
  510. /* Put 'current' back into the tree. */
  511. __enqueue_entity(cfs_rq, prev);
  512. }
  513. cfs_rq->curr = NULL;
  514. }
  515. static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  516. {
  517. /*
  518. * Update run-time statistics of the 'current'.
  519. */
  520. update_curr(cfs_rq);
  521. if (cfs_rq->nr_running > 1)
  522. check_preempt_tick(cfs_rq, curr);
  523. }
  524. /**************************************************
  525. * CFS operations on tasks:
  526. */
  527. #ifdef CONFIG_FAIR_GROUP_SCHED
  528. /* Walk up scheduling entities hierarchy */
  529. #define for_each_sched_entity(se) \
  530. for (; se; se = se->parent)
  531. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  532. {
  533. return p->se.cfs_rq;
  534. }
  535. /* runqueue on which this entity is (to be) queued */
  536. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  537. {
  538. return se->cfs_rq;
  539. }
  540. /* runqueue "owned" by this group */
  541. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  542. {
  543. return grp->my_q;
  544. }
  545. /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
  546. * another cpu ('this_cpu')
  547. */
  548. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  549. {
  550. return cfs_rq->tg->cfs_rq[this_cpu];
  551. }
  552. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  553. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  554. list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  555. /* Do the two (enqueued) tasks belong to the same group ? */
  556. static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
  557. {
  558. if (curr->se.cfs_rq == p->se.cfs_rq)
  559. return 1;
  560. return 0;
  561. }
  562. #else /* CONFIG_FAIR_GROUP_SCHED */
  563. #define for_each_sched_entity(se) \
  564. for (; se; se = NULL)
  565. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  566. {
  567. return &task_rq(p)->cfs;
  568. }
  569. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  570. {
  571. struct task_struct *p = task_of(se);
  572. struct rq *rq = task_rq(p);
  573. return &rq->cfs;
  574. }
  575. /* runqueue "owned" by this group */
  576. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  577. {
  578. return NULL;
  579. }
  580. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  581. {
  582. return &cpu_rq(this_cpu)->cfs;
  583. }
  584. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  585. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  586. static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
  587. {
  588. return 1;
  589. }
  590. #endif /* CONFIG_FAIR_GROUP_SCHED */
  591. /*
  592. * The enqueue_task method is called before nr_running is
  593. * increased. Here we update the fair scheduling stats and
  594. * then put the task into the rbtree:
  595. */
  596. static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
  597. {
  598. struct cfs_rq *cfs_rq;
  599. struct sched_entity *se = &p->se;
  600. for_each_sched_entity(se) {
  601. if (se->on_rq)
  602. break;
  603. cfs_rq = cfs_rq_of(se);
  604. enqueue_entity(cfs_rq, se, wakeup);
  605. }
  606. }
  607. /*
  608. * The dequeue_task method is called before nr_running is
  609. * decreased. We remove the task from the rbtree and
  610. * update the fair scheduling stats:
  611. */
  612. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
  613. {
  614. struct cfs_rq *cfs_rq;
  615. struct sched_entity *se = &p->se;
  616. for_each_sched_entity(se) {
  617. cfs_rq = cfs_rq_of(se);
  618. dequeue_entity(cfs_rq, se, sleep);
  619. /* Don't dequeue parent if it has other entities besides us */
  620. if (cfs_rq->load.weight)
  621. break;
  622. }
  623. }
  624. /*
  625. * sched_yield() support is very simple - we dequeue and enqueue.
  626. *
  627. * If compat_yield is turned on then we requeue to the end of the tree.
  628. */
  629. static void yield_task_fair(struct rq *rq)
  630. {
  631. struct cfs_rq *cfs_rq = task_cfs_rq(rq->curr);
  632. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  633. struct sched_entity *rightmost, *se = &rq->curr->se;
  634. struct rb_node *parent;
  635. /*
  636. * Are we the only task in the tree?
  637. */
  638. if (unlikely(cfs_rq->nr_running == 1))
  639. return;
  640. if (likely(!sysctl_sched_compat_yield)) {
  641. __update_rq_clock(rq);
  642. /*
  643. * Dequeue and enqueue the task to update its
  644. * position within the tree:
  645. */
  646. dequeue_entity(cfs_rq, se, 0);
  647. enqueue_entity(cfs_rq, se, 0);
  648. return;
  649. }
  650. /*
  651. * Find the rightmost entry in the rbtree:
  652. */
  653. do {
  654. parent = *link;
  655. link = &parent->rb_right;
  656. } while (*link);
  657. rightmost = rb_entry(parent, struct sched_entity, run_node);
  658. /*
  659. * Already in the rightmost position?
  660. */
  661. if (unlikely(rightmost == se))
  662. return;
  663. /*
  664. * Minimally necessary key value to be last in the tree:
  665. */
  666. se->vruntime = rightmost->vruntime + 1;
  667. if (cfs_rq->rb_leftmost == &se->run_node)
  668. cfs_rq->rb_leftmost = rb_next(&se->run_node);
  669. /*
  670. * Relink the task to the rightmost position:
  671. */
  672. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  673. rb_link_node(&se->run_node, parent, link);
  674. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  675. }
  676. /*
  677. * Preempt the current task with a newly woken task if needed:
  678. */
  679. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p)
  680. {
  681. struct task_struct *curr = rq->curr;
  682. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  683. if (unlikely(rt_prio(p->prio))) {
  684. update_rq_clock(rq);
  685. update_curr(cfs_rq);
  686. resched_task(curr);
  687. return;
  688. }
  689. if (is_same_group(curr, p)) {
  690. s64 delta = curr->se.vruntime - p->se.vruntime;
  691. if (delta > (s64)sysctl_sched_wakeup_granularity)
  692. resched_task(curr);
  693. }
  694. }
  695. static struct task_struct *pick_next_task_fair(struct rq *rq)
  696. {
  697. struct cfs_rq *cfs_rq = &rq->cfs;
  698. struct sched_entity *se;
  699. if (unlikely(!cfs_rq->nr_running))
  700. return NULL;
  701. do {
  702. se = pick_next_entity(cfs_rq);
  703. cfs_rq = group_cfs_rq(se);
  704. } while (cfs_rq);
  705. return task_of(se);
  706. }
  707. /*
  708. * Account for a descheduled task:
  709. */
  710. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  711. {
  712. struct sched_entity *se = &prev->se;
  713. struct cfs_rq *cfs_rq;
  714. for_each_sched_entity(se) {
  715. cfs_rq = cfs_rq_of(se);
  716. put_prev_entity(cfs_rq, se);
  717. }
  718. }
  719. /**************************************************
  720. * Fair scheduling class load-balancing methods:
  721. */
  722. /*
  723. * Load-balancing iterator. Note: while the runqueue stays locked
  724. * during the whole iteration, the current task might be
  725. * dequeued so the iterator has to be dequeue-safe. Here we
  726. * achieve that by always pre-iterating before returning
  727. * the current task:
  728. */
  729. static inline struct task_struct *
  730. __load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
  731. {
  732. struct task_struct *p;
  733. if (!curr)
  734. return NULL;
  735. p = rb_entry(curr, struct task_struct, se.run_node);
  736. cfs_rq->rb_load_balance_curr = rb_next(curr);
  737. return p;
  738. }
  739. static struct task_struct *load_balance_start_fair(void *arg)
  740. {
  741. struct cfs_rq *cfs_rq = arg;
  742. return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
  743. }
  744. static struct task_struct *load_balance_next_fair(void *arg)
  745. {
  746. struct cfs_rq *cfs_rq = arg;
  747. return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
  748. }
  749. #ifdef CONFIG_FAIR_GROUP_SCHED
  750. static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
  751. {
  752. struct sched_entity *curr;
  753. struct task_struct *p;
  754. if (!cfs_rq->nr_running)
  755. return MAX_PRIO;
  756. curr = cfs_rq->curr;
  757. if (!curr)
  758. curr = __pick_next_entity(cfs_rq);
  759. p = task_of(curr);
  760. return p->prio;
  761. }
  762. #endif
  763. static unsigned long
  764. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  765. unsigned long max_nr_move, unsigned long max_load_move,
  766. struct sched_domain *sd, enum cpu_idle_type idle,
  767. int *all_pinned, int *this_best_prio)
  768. {
  769. struct cfs_rq *busy_cfs_rq;
  770. unsigned long load_moved, total_nr_moved = 0, nr_moved;
  771. long rem_load_move = max_load_move;
  772. struct rq_iterator cfs_rq_iterator;
  773. cfs_rq_iterator.start = load_balance_start_fair;
  774. cfs_rq_iterator.next = load_balance_next_fair;
  775. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  776. #ifdef CONFIG_FAIR_GROUP_SCHED
  777. struct cfs_rq *this_cfs_rq;
  778. long imbalance;
  779. unsigned long maxload;
  780. this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
  781. imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
  782. /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
  783. if (imbalance <= 0)
  784. continue;
  785. /* Don't pull more than imbalance/2 */
  786. imbalance /= 2;
  787. maxload = min(rem_load_move, imbalance);
  788. *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
  789. #else
  790. # define maxload rem_load_move
  791. #endif
  792. /* pass busy_cfs_rq argument into
  793. * load_balance_[start|next]_fair iterators
  794. */
  795. cfs_rq_iterator.arg = busy_cfs_rq;
  796. nr_moved = balance_tasks(this_rq, this_cpu, busiest,
  797. max_nr_move, maxload, sd, idle, all_pinned,
  798. &load_moved, this_best_prio, &cfs_rq_iterator);
  799. total_nr_moved += nr_moved;
  800. max_nr_move -= nr_moved;
  801. rem_load_move -= load_moved;
  802. if (max_nr_move <= 0 || rem_load_move <= 0)
  803. break;
  804. }
  805. return max_load_move - rem_load_move;
  806. }
  807. /*
  808. * scheduler tick hitting a task of our scheduling class:
  809. */
  810. static void task_tick_fair(struct rq *rq, struct task_struct *curr)
  811. {
  812. struct cfs_rq *cfs_rq;
  813. struct sched_entity *se = &curr->se;
  814. for_each_sched_entity(se) {
  815. cfs_rq = cfs_rq_of(se);
  816. entity_tick(cfs_rq, se);
  817. }
  818. }
  819. #define swap(a,b) do { typeof(a) tmp = (a); (a) = (b); (b) = tmp; } while (0)
  820. /*
  821. * Share the fairness runtime between parent and child, thus the
  822. * total amount of pressure for CPU stays equal - new tasks
  823. * get a chance to run but frequent forkers are not allowed to
  824. * monopolize the CPU. Note: the parent runqueue is locked,
  825. * the child is not running yet.
  826. */
  827. static void task_new_fair(struct rq *rq, struct task_struct *p)
  828. {
  829. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  830. struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
  831. sched_info_queued(p);
  832. update_curr(cfs_rq);
  833. place_entity(cfs_rq, se, 1);
  834. if (sysctl_sched_child_runs_first &&
  835. curr->vruntime < se->vruntime) {
  836. /*
  837. * Upon rescheduling, sched_class::put_prev_task() will place
  838. * 'current' within the tree based on its new key value.
  839. */
  840. swap(curr->vruntime, se->vruntime);
  841. }
  842. update_stats_enqueue(cfs_rq, se);
  843. check_spread(cfs_rq, se);
  844. check_spread(cfs_rq, curr);
  845. __enqueue_entity(cfs_rq, se);
  846. account_entity_enqueue(cfs_rq, se);
  847. resched_task(rq->curr);
  848. }
  849. /* Account for a task changing its policy or group.
  850. *
  851. * This routine is mostly called to set cfs_rq->curr field when a task
  852. * migrates between groups/classes.
  853. */
  854. static void set_curr_task_fair(struct rq *rq)
  855. {
  856. struct sched_entity *se = &rq->curr->se;
  857. for_each_sched_entity(se)
  858. set_next_entity(cfs_rq_of(se), se);
  859. }
  860. /*
  861. * All the scheduling class methods:
  862. */
  863. struct sched_class fair_sched_class __read_mostly = {
  864. .enqueue_task = enqueue_task_fair,
  865. .dequeue_task = dequeue_task_fair,
  866. .yield_task = yield_task_fair,
  867. .check_preempt_curr = check_preempt_wakeup,
  868. .pick_next_task = pick_next_task_fair,
  869. .put_prev_task = put_prev_task_fair,
  870. .load_balance = load_balance_fair,
  871. .set_curr_task = set_curr_task_fair,
  872. .task_tick = task_tick_fair,
  873. .task_new = task_new_fair,
  874. };
  875. #ifdef CONFIG_SCHED_DEBUG
  876. static void print_cfs_stats(struct seq_file *m, int cpu)
  877. {
  878. struct cfs_rq *cfs_rq;
  879. #ifdef CONFIG_FAIR_GROUP_SCHED
  880. print_cfs_rq(m, cpu, &cpu_rq(cpu)->cfs);
  881. #endif
  882. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  883. print_cfs_rq(m, cpu, cfs_rq);
  884. }
  885. #endif