sched_fair.c 25 KB

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