sched_fair.c 28 KB

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