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