sched_fair.c 32 KB

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