sched_fair.c 31 KB

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