sched_fair.c 25 KB

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