sched_fair.c 29 KB

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