sched_fair.c 27 KB

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