sched_fair.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. /*
  20. * Preemption granularity:
  21. * (default: 10 msec, units: nanoseconds)
  22. *
  23. * NOTE: this granularity value is not the same as the concept of
  24. * 'timeslice length' - timeslices in CFS will typically be somewhat
  25. * larger than this value. (to see the precise effective timeslice
  26. * length of your workload, run vmstat and monitor the context-switches
  27. * field)
  28. *
  29. * On SMP systems the value of this is multiplied by the log2 of the
  30. * number of CPUs. (i.e. factor 2x on 2-way systems, 3x on 4-way
  31. * systems, 4x on 8-way systems, 5x on 16-way systems, etc.)
  32. */
  33. unsigned int sysctl_sched_granularity __read_mostly = 10000000UL;
  34. /*
  35. * SCHED_BATCH wake-up granularity.
  36. * (default: 25 msec, units: nanoseconds)
  37. *
  38. * This option delays the preemption effects of decoupled workloads
  39. * and reduces their over-scheduling. Synchronous workloads will still
  40. * have immediate wakeup/sleep latencies.
  41. */
  42. unsigned int sysctl_sched_batch_wakeup_granularity __read_mostly = 25000000UL;
  43. /*
  44. * SCHED_OTHER wake-up granularity.
  45. * (default: 1 msec, units: nanoseconds)
  46. *
  47. * This option delays the preemption effects of decoupled workloads
  48. * and reduces their over-scheduling. Synchronous workloads will still
  49. * have immediate wakeup/sleep latencies.
  50. */
  51. unsigned int sysctl_sched_wakeup_granularity __read_mostly = 1000000UL;
  52. unsigned int sysctl_sched_stat_granularity __read_mostly;
  53. /*
  54. * Initialized in sched_init_granularity() [to 5 times the base granularity]:
  55. */
  56. unsigned int sysctl_sched_runtime_limit __read_mostly;
  57. /*
  58. * Debugging: various feature bits
  59. */
  60. enum {
  61. SCHED_FEAT_FAIR_SLEEPERS = 1,
  62. SCHED_FEAT_SLEEPER_AVG = 2,
  63. SCHED_FEAT_SLEEPER_LOAD_AVG = 4,
  64. SCHED_FEAT_PRECISE_CPU_LOAD = 8,
  65. SCHED_FEAT_START_DEBIT = 16,
  66. SCHED_FEAT_SKIP_INITIAL = 32,
  67. };
  68. unsigned int sysctl_sched_features __read_mostly =
  69. SCHED_FEAT_FAIR_SLEEPERS *1 |
  70. SCHED_FEAT_SLEEPER_AVG *0 |
  71. SCHED_FEAT_SLEEPER_LOAD_AVG *1 |
  72. SCHED_FEAT_PRECISE_CPU_LOAD *1 |
  73. SCHED_FEAT_START_DEBIT *1 |
  74. SCHED_FEAT_SKIP_INITIAL *0;
  75. extern struct sched_class fair_sched_class;
  76. /**************************************************************
  77. * CFS operations on generic schedulable entities:
  78. */
  79. #ifdef CONFIG_FAIR_GROUP_SCHED
  80. /* cpu runqueue to which this cfs_rq is attached */
  81. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  82. {
  83. return cfs_rq->rq;
  84. }
  85. /* currently running entity (if any) on this cfs_rq */
  86. static inline struct sched_entity *cfs_rq_curr(struct cfs_rq *cfs_rq)
  87. {
  88. return cfs_rq->curr;
  89. }
  90. /* An entity is a task if it doesn't "own" a runqueue */
  91. #define entity_is_task(se) (!se->my_q)
  92. static inline void
  93. set_cfs_rq_curr(struct cfs_rq *cfs_rq, struct sched_entity *se)
  94. {
  95. cfs_rq->curr = se;
  96. }
  97. #else /* CONFIG_FAIR_GROUP_SCHED */
  98. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  99. {
  100. return container_of(cfs_rq, struct rq, cfs);
  101. }
  102. static inline struct sched_entity *cfs_rq_curr(struct cfs_rq *cfs_rq)
  103. {
  104. struct rq *rq = rq_of(cfs_rq);
  105. if (unlikely(rq->curr->sched_class != &fair_sched_class))
  106. return NULL;
  107. return &rq->curr->se;
  108. }
  109. #define entity_is_task(se) 1
  110. static inline void
  111. set_cfs_rq_curr(struct cfs_rq *cfs_rq, struct sched_entity *se) { }
  112. #endif /* CONFIG_FAIR_GROUP_SCHED */
  113. static inline struct task_struct *task_of(struct sched_entity *se)
  114. {
  115. return container_of(se, struct task_struct, se);
  116. }
  117. /**************************************************************
  118. * Scheduling class tree data structure manipulation methods:
  119. */
  120. /*
  121. * Enqueue an entity into the rb-tree:
  122. */
  123. static inline void
  124. __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  125. {
  126. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  127. struct rb_node *parent = NULL;
  128. struct sched_entity *entry;
  129. s64 key = se->fair_key;
  130. int leftmost = 1;
  131. /*
  132. * Find the right place in the rbtree:
  133. */
  134. while (*link) {
  135. parent = *link;
  136. entry = rb_entry(parent, struct sched_entity, run_node);
  137. /*
  138. * We dont care about collisions. Nodes with
  139. * the same key stay together.
  140. */
  141. if (key - entry->fair_key < 0) {
  142. link = &parent->rb_left;
  143. } else {
  144. link = &parent->rb_right;
  145. leftmost = 0;
  146. }
  147. }
  148. /*
  149. * Maintain a cache of leftmost tree entries (it is frequently
  150. * used):
  151. */
  152. if (leftmost)
  153. cfs_rq->rb_leftmost = &se->run_node;
  154. rb_link_node(&se->run_node, parent, link);
  155. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  156. update_load_add(&cfs_rq->load, se->load.weight);
  157. cfs_rq->nr_running++;
  158. se->on_rq = 1;
  159. }
  160. static inline void
  161. __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  162. {
  163. if (cfs_rq->rb_leftmost == &se->run_node)
  164. cfs_rq->rb_leftmost = rb_next(&se->run_node);
  165. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  166. update_load_sub(&cfs_rq->load, se->load.weight);
  167. cfs_rq->nr_running--;
  168. se->on_rq = 0;
  169. }
  170. static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
  171. {
  172. return cfs_rq->rb_leftmost;
  173. }
  174. static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
  175. {
  176. return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
  177. }
  178. /**************************************************************
  179. * Scheduling class statistics methods:
  180. */
  181. /*
  182. * We rescale the rescheduling granularity of tasks according to their
  183. * nice level, but only linearly, not exponentially:
  184. */
  185. static long
  186. niced_granularity(struct sched_entity *curr, unsigned long granularity)
  187. {
  188. u64 tmp;
  189. if (likely(curr->load.weight == NICE_0_LOAD))
  190. return granularity;
  191. /*
  192. * Positive nice levels get the same granularity as nice-0:
  193. */
  194. if (likely(curr->load.weight < NICE_0_LOAD)) {
  195. tmp = curr->load.weight * (u64)granularity;
  196. return (long) (tmp >> NICE_0_SHIFT);
  197. }
  198. /*
  199. * Negative nice level tasks get linearly finer
  200. * granularity:
  201. */
  202. tmp = curr->load.inv_weight * (u64)granularity;
  203. /*
  204. * It will always fit into 'long':
  205. */
  206. return (long) (tmp >> WMULT_SHIFT);
  207. }
  208. static inline void
  209. limit_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se)
  210. {
  211. long limit = sysctl_sched_runtime_limit;
  212. /*
  213. * Niced tasks have the same history dynamic range as
  214. * non-niced tasks:
  215. */
  216. if (unlikely(se->wait_runtime > limit)) {
  217. se->wait_runtime = limit;
  218. schedstat_inc(se, wait_runtime_overruns);
  219. schedstat_inc(cfs_rq, wait_runtime_overruns);
  220. }
  221. if (unlikely(se->wait_runtime < -limit)) {
  222. se->wait_runtime = -limit;
  223. schedstat_inc(se, wait_runtime_underruns);
  224. schedstat_inc(cfs_rq, wait_runtime_underruns);
  225. }
  226. }
  227. static inline void
  228. __add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
  229. {
  230. se->wait_runtime += delta;
  231. schedstat_add(se, sum_wait_runtime, delta);
  232. limit_wait_runtime(cfs_rq, se);
  233. }
  234. static void
  235. add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
  236. {
  237. schedstat_add(cfs_rq, wait_runtime, -se->wait_runtime);
  238. __add_wait_runtime(cfs_rq, se, delta);
  239. schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
  240. }
  241. /*
  242. * Update the current task's runtime statistics. Skip current tasks that
  243. * are not in our scheduling class.
  244. */
  245. static inline void
  246. __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  247. {
  248. unsigned long delta, delta_exec, delta_fair, delta_mine;
  249. struct load_weight *lw = &cfs_rq->load;
  250. unsigned long load = lw->weight;
  251. delta_exec = curr->delta_exec;
  252. schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
  253. curr->sum_exec_runtime += delta_exec;
  254. cfs_rq->exec_clock += delta_exec;
  255. if (unlikely(!load))
  256. return;
  257. delta_fair = calc_delta_fair(delta_exec, lw);
  258. delta_mine = calc_delta_mine(delta_exec, curr->load.weight, lw);
  259. if (cfs_rq->sleeper_bonus > sysctl_sched_granularity) {
  260. delta = min((u64)delta_mine, cfs_rq->sleeper_bonus);
  261. delta = min(delta, (unsigned long)(
  262. (long)sysctl_sched_runtime_limit - curr->wait_runtime));
  263. cfs_rq->sleeper_bonus -= delta;
  264. delta_mine -= delta;
  265. }
  266. cfs_rq->fair_clock += delta_fair;
  267. /*
  268. * We executed delta_exec amount of time on the CPU,
  269. * but we were only entitled to delta_mine amount of
  270. * time during that period (if nr_running == 1 then
  271. * the two values are equal)
  272. * [Note: delta_mine - delta_exec is negative]:
  273. */
  274. add_wait_runtime(cfs_rq, curr, delta_mine - delta_exec);
  275. }
  276. static void update_curr(struct cfs_rq *cfs_rq)
  277. {
  278. struct sched_entity *curr = cfs_rq_curr(cfs_rq);
  279. unsigned long delta_exec;
  280. if (unlikely(!curr))
  281. return;
  282. /*
  283. * Get the amount of time the current task was running
  284. * since the last time we changed load (this cannot
  285. * overflow on 32 bits):
  286. */
  287. delta_exec = (unsigned long)(rq_of(cfs_rq)->clock - curr->exec_start);
  288. curr->delta_exec += delta_exec;
  289. if (unlikely(curr->delta_exec > sysctl_sched_stat_granularity)) {
  290. __update_curr(cfs_rq, curr);
  291. curr->delta_exec = 0;
  292. }
  293. curr->exec_start = rq_of(cfs_rq)->clock;
  294. }
  295. static inline void
  296. update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  297. {
  298. se->wait_start_fair = cfs_rq->fair_clock;
  299. schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
  300. }
  301. /*
  302. * We calculate fair deltas here, so protect against the random effects
  303. * of a multiplication overflow by capping it to the runtime limit:
  304. */
  305. #if BITS_PER_LONG == 32
  306. static inline unsigned long
  307. calc_weighted(unsigned long delta, unsigned long weight, int shift)
  308. {
  309. u64 tmp = (u64)delta * weight >> shift;
  310. if (unlikely(tmp > sysctl_sched_runtime_limit*2))
  311. return sysctl_sched_runtime_limit*2;
  312. return tmp;
  313. }
  314. #else
  315. static inline unsigned long
  316. calc_weighted(unsigned long delta, unsigned long weight, int shift)
  317. {
  318. return delta * weight >> shift;
  319. }
  320. #endif
  321. /*
  322. * Task is being enqueued - update stats:
  323. */
  324. static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  325. {
  326. s64 key;
  327. /*
  328. * Are we enqueueing a waiting task? (for current tasks
  329. * a dequeue/enqueue event is a NOP)
  330. */
  331. if (se != cfs_rq_curr(cfs_rq))
  332. update_stats_wait_start(cfs_rq, se);
  333. /*
  334. * Update the key:
  335. */
  336. key = cfs_rq->fair_clock;
  337. /*
  338. * Optimize the common nice 0 case:
  339. */
  340. if (likely(se->load.weight == NICE_0_LOAD)) {
  341. key -= se->wait_runtime;
  342. } else {
  343. u64 tmp;
  344. if (se->wait_runtime < 0) {
  345. tmp = -se->wait_runtime;
  346. key += (tmp * se->load.inv_weight) >>
  347. (WMULT_SHIFT - NICE_0_SHIFT);
  348. } else {
  349. tmp = se->wait_runtime;
  350. key -= (tmp * se->load.inv_weight) >>
  351. (WMULT_SHIFT - NICE_0_SHIFT);
  352. }
  353. }
  354. se->fair_key = key;
  355. }
  356. /*
  357. * Note: must be called with a freshly updated rq->fair_clock.
  358. */
  359. static inline void
  360. __update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  361. {
  362. unsigned long delta_fair = se->delta_fair_run;
  363. schedstat_set(se->wait_max, max(se->wait_max,
  364. rq_of(cfs_rq)->clock - se->wait_start));
  365. if (unlikely(se->load.weight != NICE_0_LOAD))
  366. delta_fair = calc_weighted(delta_fair, se->load.weight,
  367. NICE_0_SHIFT);
  368. add_wait_runtime(cfs_rq, se, delta_fair);
  369. }
  370. static void
  371. update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  372. {
  373. unsigned long delta_fair;
  374. delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
  375. (u64)(cfs_rq->fair_clock - se->wait_start_fair));
  376. se->delta_fair_run += delta_fair;
  377. if (unlikely(abs(se->delta_fair_run) >=
  378. sysctl_sched_stat_granularity)) {
  379. __update_stats_wait_end(cfs_rq, se);
  380. se->delta_fair_run = 0;
  381. }
  382. se->wait_start_fair = 0;
  383. schedstat_set(se->wait_start, 0);
  384. }
  385. static inline void
  386. update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  387. {
  388. update_curr(cfs_rq);
  389. /*
  390. * Mark the end of the wait period if dequeueing a
  391. * waiting task:
  392. */
  393. if (se != cfs_rq_curr(cfs_rq))
  394. update_stats_wait_end(cfs_rq, se);
  395. }
  396. /*
  397. * We are picking a new current task - update its stats:
  398. */
  399. static inline void
  400. update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  401. {
  402. /*
  403. * We are starting a new run period:
  404. */
  405. se->exec_start = rq_of(cfs_rq)->clock;
  406. }
  407. /*
  408. * We are descheduling a task - update its stats:
  409. */
  410. static inline void
  411. update_stats_curr_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  412. {
  413. se->exec_start = 0;
  414. }
  415. /**************************************************
  416. * Scheduling class queueing methods:
  417. */
  418. static void __enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  419. {
  420. unsigned long load = cfs_rq->load.weight, delta_fair;
  421. long prev_runtime;
  422. /*
  423. * Do not boost sleepers if there's too much bonus 'in flight'
  424. * already:
  425. */
  426. if (unlikely(cfs_rq->sleeper_bonus > sysctl_sched_runtime_limit))
  427. return;
  428. if (sysctl_sched_features & SCHED_FEAT_SLEEPER_LOAD_AVG)
  429. load = rq_of(cfs_rq)->cpu_load[2];
  430. delta_fair = se->delta_fair_sleep;
  431. /*
  432. * Fix up delta_fair with the effect of us running
  433. * during the whole sleep period:
  434. */
  435. if (sysctl_sched_features & SCHED_FEAT_SLEEPER_AVG)
  436. delta_fair = div64_likely32((u64)delta_fair * load,
  437. load + se->load.weight);
  438. if (unlikely(se->load.weight != NICE_0_LOAD))
  439. delta_fair = calc_weighted(delta_fair, se->load.weight,
  440. NICE_0_SHIFT);
  441. prev_runtime = se->wait_runtime;
  442. __add_wait_runtime(cfs_rq, se, delta_fair);
  443. schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
  444. delta_fair = se->wait_runtime - prev_runtime;
  445. /*
  446. * Track the amount of bonus we've given to sleepers:
  447. */
  448. cfs_rq->sleeper_bonus += delta_fair;
  449. }
  450. static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  451. {
  452. struct task_struct *tsk = task_of(se);
  453. unsigned long delta_fair;
  454. if ((entity_is_task(se) && tsk->policy == SCHED_BATCH) ||
  455. !(sysctl_sched_features & SCHED_FEAT_FAIR_SLEEPERS))
  456. return;
  457. delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
  458. (u64)(cfs_rq->fair_clock - se->sleep_start_fair));
  459. se->delta_fair_sleep += delta_fair;
  460. if (unlikely(abs(se->delta_fair_sleep) >=
  461. sysctl_sched_stat_granularity)) {
  462. __enqueue_sleeper(cfs_rq, se);
  463. se->delta_fair_sleep = 0;
  464. }
  465. se->sleep_start_fair = 0;
  466. #ifdef CONFIG_SCHEDSTATS
  467. if (se->sleep_start) {
  468. u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
  469. if ((s64)delta < 0)
  470. delta = 0;
  471. if (unlikely(delta > se->sleep_max))
  472. se->sleep_max = delta;
  473. se->sleep_start = 0;
  474. se->sum_sleep_runtime += delta;
  475. }
  476. if (se->block_start) {
  477. u64 delta = rq_of(cfs_rq)->clock - se->block_start;
  478. if ((s64)delta < 0)
  479. delta = 0;
  480. if (unlikely(delta > se->block_max))
  481. se->block_max = delta;
  482. se->block_start = 0;
  483. se->sum_sleep_runtime += delta;
  484. }
  485. #endif
  486. }
  487. static void
  488. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
  489. {
  490. /*
  491. * Update the fair clock.
  492. */
  493. update_curr(cfs_rq);
  494. if (wakeup)
  495. enqueue_sleeper(cfs_rq, se);
  496. update_stats_enqueue(cfs_rq, se);
  497. __enqueue_entity(cfs_rq, se);
  498. }
  499. static void
  500. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
  501. {
  502. update_stats_dequeue(cfs_rq, se);
  503. if (sleep) {
  504. se->sleep_start_fair = cfs_rq->fair_clock;
  505. #ifdef CONFIG_SCHEDSTATS
  506. if (entity_is_task(se)) {
  507. struct task_struct *tsk = task_of(se);
  508. if (tsk->state & TASK_INTERRUPTIBLE)
  509. se->sleep_start = rq_of(cfs_rq)->clock;
  510. if (tsk->state & TASK_UNINTERRUPTIBLE)
  511. se->block_start = rq_of(cfs_rq)->clock;
  512. }
  513. cfs_rq->wait_runtime -= se->wait_runtime;
  514. #endif
  515. }
  516. __dequeue_entity(cfs_rq, se);
  517. }
  518. /*
  519. * Preempt the current task with a newly woken task if needed:
  520. */
  521. static void
  522. __check_preempt_curr_fair(struct cfs_rq *cfs_rq, struct sched_entity *se,
  523. struct sched_entity *curr, unsigned long granularity)
  524. {
  525. s64 __delta = curr->fair_key - se->fair_key;
  526. /*
  527. * Take scheduling granularity into account - do not
  528. * preempt the current task unless the best task has
  529. * a larger than sched_granularity fairness advantage:
  530. */
  531. if (__delta > niced_granularity(curr, granularity))
  532. resched_task(rq_of(cfs_rq)->curr);
  533. }
  534. static inline void
  535. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  536. {
  537. /*
  538. * Any task has to be enqueued before it get to execute on
  539. * a CPU. So account for the time it spent waiting on the
  540. * runqueue. (note, here we rely on pick_next_task() having
  541. * done a put_prev_task_fair() shortly before this, which
  542. * updated rq->fair_clock - used by update_stats_wait_end())
  543. */
  544. update_stats_wait_end(cfs_rq, se);
  545. update_stats_curr_start(cfs_rq, se);
  546. set_cfs_rq_curr(cfs_rq, se);
  547. }
  548. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  549. {
  550. struct sched_entity *se = __pick_next_entity(cfs_rq);
  551. set_next_entity(cfs_rq, se);
  552. return se;
  553. }
  554. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  555. {
  556. /*
  557. * If still on the runqueue then deactivate_task()
  558. * was not called and update_curr() has to be done:
  559. */
  560. if (prev->on_rq)
  561. update_curr(cfs_rq);
  562. update_stats_curr_end(cfs_rq, prev);
  563. if (prev->on_rq)
  564. update_stats_wait_start(cfs_rq, prev);
  565. set_cfs_rq_curr(cfs_rq, NULL);
  566. }
  567. static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  568. {
  569. struct sched_entity *next;
  570. /*
  571. * Dequeue and enqueue the task to update its
  572. * position within the tree:
  573. */
  574. dequeue_entity(cfs_rq, curr, 0);
  575. enqueue_entity(cfs_rq, curr, 0);
  576. /*
  577. * Reschedule if another task tops the current one.
  578. */
  579. next = __pick_next_entity(cfs_rq);
  580. if (next == curr)
  581. return;
  582. __check_preempt_curr_fair(cfs_rq, next, curr, sysctl_sched_granularity);
  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. static void yield_task_fair(struct rq *rq, struct task_struct *p)
  689. {
  690. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  691. __update_rq_clock(rq);
  692. /*
  693. * Dequeue and enqueue the task to update its
  694. * position within the tree:
  695. */
  696. dequeue_entity(cfs_rq, &p->se, 0);
  697. enqueue_entity(cfs_rq, &p->se, 0);
  698. }
  699. /*
  700. * Preempt the current task with a newly woken task if needed:
  701. */
  702. static void check_preempt_curr_fair(struct rq *rq, struct task_struct *p)
  703. {
  704. struct task_struct *curr = rq->curr;
  705. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  706. unsigned long gran;
  707. if (unlikely(rt_prio(p->prio))) {
  708. update_rq_clock(rq);
  709. update_curr(cfs_rq);
  710. resched_task(curr);
  711. return;
  712. }
  713. gran = sysctl_sched_wakeup_granularity;
  714. /*
  715. * Batch tasks prefer throughput over latency:
  716. */
  717. if (unlikely(p->policy == SCHED_BATCH))
  718. gran = sysctl_sched_batch_wakeup_granularity;
  719. if (is_same_group(curr, p))
  720. __check_preempt_curr_fair(cfs_rq, &p->se, &curr->se, gran);
  721. }
  722. static struct task_struct *pick_next_task_fair(struct rq *rq)
  723. {
  724. struct cfs_rq *cfs_rq = &rq->cfs;
  725. struct sched_entity *se;
  726. if (unlikely(!cfs_rq->nr_running))
  727. return NULL;
  728. do {
  729. se = pick_next_entity(cfs_rq);
  730. cfs_rq = group_cfs_rq(se);
  731. } while (cfs_rq);
  732. return task_of(se);
  733. }
  734. /*
  735. * Account for a descheduled task:
  736. */
  737. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  738. {
  739. struct sched_entity *se = &prev->se;
  740. struct cfs_rq *cfs_rq;
  741. for_each_sched_entity(se) {
  742. cfs_rq = cfs_rq_of(se);
  743. put_prev_entity(cfs_rq, se);
  744. }
  745. }
  746. /**************************************************
  747. * Fair scheduling class load-balancing methods:
  748. */
  749. /*
  750. * Load-balancing iterator. Note: while the runqueue stays locked
  751. * during the whole iteration, the current task might be
  752. * dequeued so the iterator has to be dequeue-safe. Here we
  753. * achieve that by always pre-iterating before returning
  754. * the current task:
  755. */
  756. static inline struct task_struct *
  757. __load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
  758. {
  759. struct task_struct *p;
  760. if (!curr)
  761. return NULL;
  762. p = rb_entry(curr, struct task_struct, se.run_node);
  763. cfs_rq->rb_load_balance_curr = rb_next(curr);
  764. return p;
  765. }
  766. static struct task_struct *load_balance_start_fair(void *arg)
  767. {
  768. struct cfs_rq *cfs_rq = arg;
  769. return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
  770. }
  771. static struct task_struct *load_balance_next_fair(void *arg)
  772. {
  773. struct cfs_rq *cfs_rq = arg;
  774. return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
  775. }
  776. #ifdef CONFIG_FAIR_GROUP_SCHED
  777. static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
  778. {
  779. struct sched_entity *curr;
  780. struct task_struct *p;
  781. if (!cfs_rq->nr_running)
  782. return MAX_PRIO;
  783. curr = __pick_next_entity(cfs_rq);
  784. p = task_of(curr);
  785. return p->prio;
  786. }
  787. #endif
  788. static unsigned long
  789. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  790. unsigned long max_nr_move, unsigned long max_load_move,
  791. struct sched_domain *sd, enum cpu_idle_type idle,
  792. int *all_pinned, int *this_best_prio)
  793. {
  794. struct cfs_rq *busy_cfs_rq;
  795. unsigned long load_moved, total_nr_moved = 0, nr_moved;
  796. long rem_load_move = max_load_move;
  797. struct rq_iterator cfs_rq_iterator;
  798. cfs_rq_iterator.start = load_balance_start_fair;
  799. cfs_rq_iterator.next = load_balance_next_fair;
  800. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  801. #ifdef CONFIG_FAIR_GROUP_SCHED
  802. struct cfs_rq *this_cfs_rq;
  803. long imbalance;
  804. unsigned long maxload;
  805. this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
  806. imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
  807. /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
  808. if (imbalance <= 0)
  809. continue;
  810. /* Don't pull more than imbalance/2 */
  811. imbalance /= 2;
  812. maxload = min(rem_load_move, imbalance);
  813. *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
  814. #else
  815. # define maxload rem_load_move
  816. #endif
  817. /* pass busy_cfs_rq argument into
  818. * load_balance_[start|next]_fair iterators
  819. */
  820. cfs_rq_iterator.arg = busy_cfs_rq;
  821. nr_moved = balance_tasks(this_rq, this_cpu, busiest,
  822. max_nr_move, maxload, sd, idle, all_pinned,
  823. &load_moved, this_best_prio, &cfs_rq_iterator);
  824. total_nr_moved += nr_moved;
  825. max_nr_move -= nr_moved;
  826. rem_load_move -= load_moved;
  827. if (max_nr_move <= 0 || rem_load_move <= 0)
  828. break;
  829. }
  830. return max_load_move - rem_load_move;
  831. }
  832. /*
  833. * scheduler tick hitting a task of our scheduling class:
  834. */
  835. static void task_tick_fair(struct rq *rq, struct task_struct *curr)
  836. {
  837. struct cfs_rq *cfs_rq;
  838. struct sched_entity *se = &curr->se;
  839. for_each_sched_entity(se) {
  840. cfs_rq = cfs_rq_of(se);
  841. entity_tick(cfs_rq, se);
  842. }
  843. }
  844. /*
  845. * Share the fairness runtime between parent and child, thus the
  846. * total amount of pressure for CPU stays equal - new tasks
  847. * get a chance to run but frequent forkers are not allowed to
  848. * monopolize the CPU. Note: the parent runqueue is locked,
  849. * the child is not running yet.
  850. */
  851. static void task_new_fair(struct rq *rq, struct task_struct *p)
  852. {
  853. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  854. struct sched_entity *se = &p->se;
  855. sched_info_queued(p);
  856. update_stats_enqueue(cfs_rq, se);
  857. /*
  858. * Child runs first: we let it run before the parent
  859. * until it reschedules once. We set up the key so that
  860. * it will preempt the parent:
  861. */
  862. p->se.fair_key = current->se.fair_key -
  863. niced_granularity(&rq->curr->se, sysctl_sched_granularity) - 1;
  864. /*
  865. * The first wait is dominated by the child-runs-first logic,
  866. * so do not credit it with that waiting time yet:
  867. */
  868. if (sysctl_sched_features & SCHED_FEAT_SKIP_INITIAL)
  869. p->se.wait_start_fair = 0;
  870. /*
  871. * The statistical average of wait_runtime is about
  872. * -granularity/2, so initialize the task with that:
  873. */
  874. if (sysctl_sched_features & SCHED_FEAT_START_DEBIT)
  875. p->se.wait_runtime = -((long)sysctl_sched_granularity / 2);
  876. __enqueue_entity(cfs_rq, se);
  877. }
  878. #ifdef CONFIG_FAIR_GROUP_SCHED
  879. /* Account for a task changing its policy or group.
  880. *
  881. * This routine is mostly called to set cfs_rq->curr field when a task
  882. * migrates between groups/classes.
  883. */
  884. static void set_curr_task_fair(struct rq *rq)
  885. {
  886. struct sched_entity *se = &rq->curr->se;
  887. for_each_sched_entity(se)
  888. set_next_entity(cfs_rq_of(se), se);
  889. }
  890. #else
  891. static void set_curr_task_fair(struct rq *rq)
  892. {
  893. }
  894. #endif
  895. /*
  896. * All the scheduling class methods:
  897. */
  898. struct sched_class fair_sched_class __read_mostly = {
  899. .enqueue_task = enqueue_task_fair,
  900. .dequeue_task = dequeue_task_fair,
  901. .yield_task = yield_task_fair,
  902. .check_preempt_curr = check_preempt_curr_fair,
  903. .pick_next_task = pick_next_task_fair,
  904. .put_prev_task = put_prev_task_fair,
  905. .load_balance = load_balance_fair,
  906. .set_curr_task = set_curr_task_fair,
  907. .task_tick = task_tick_fair,
  908. .task_new = task_new_fair,
  909. };
  910. #ifdef CONFIG_SCHED_DEBUG
  911. static void print_cfs_stats(struct seq_file *m, int cpu)
  912. {
  913. struct cfs_rq *cfs_rq;
  914. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  915. print_cfs_rq(m, cpu, cfs_rq);
  916. }
  917. #endif