sched_fair.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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 int
  572. __check_preempt_curr_fair(struct cfs_rq *cfs_rq, struct sched_entity *se,
  573. struct sched_entity *curr, unsigned long granularity)
  574. {
  575. s64 __delta = curr->fair_key - se->fair_key;
  576. /*
  577. * Take scheduling granularity into account - do not
  578. * preempt the current task unless the best task has
  579. * a larger than sched_granularity fairness advantage:
  580. */
  581. if (__delta > niced_granularity(curr, granularity)) {
  582. resched_task(rq_of(cfs_rq)->curr);
  583. return 1;
  584. }
  585. return 0;
  586. }
  587. static inline void
  588. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  589. {
  590. /*
  591. * Any task has to be enqueued before it get to execute on
  592. * a CPU. So account for the time it spent waiting on the
  593. * runqueue. (note, here we rely on pick_next_task() having
  594. * done a put_prev_task_fair() shortly before this, which
  595. * updated rq->fair_clock - used by update_stats_wait_end())
  596. */
  597. update_stats_wait_end(cfs_rq, se);
  598. update_stats_curr_start(cfs_rq, se);
  599. set_cfs_rq_curr(cfs_rq, se);
  600. }
  601. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  602. {
  603. struct sched_entity *se = __pick_next_entity(cfs_rq);
  604. set_next_entity(cfs_rq, se);
  605. return se;
  606. }
  607. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  608. {
  609. /*
  610. * If still on the runqueue then deactivate_task()
  611. * was not called and update_curr() has to be done:
  612. */
  613. if (prev->on_rq)
  614. update_curr(cfs_rq);
  615. update_stats_curr_end(cfs_rq, prev);
  616. if (prev->on_rq)
  617. update_stats_wait_start(cfs_rq, prev);
  618. set_cfs_rq_curr(cfs_rq, NULL);
  619. }
  620. static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  621. {
  622. unsigned long gran, ideal_runtime, delta_exec;
  623. struct sched_entity *next;
  624. /*
  625. * Dequeue and enqueue the task to update its
  626. * position within the tree:
  627. */
  628. dequeue_entity(cfs_rq, curr, 0);
  629. enqueue_entity(cfs_rq, curr, 0);
  630. /*
  631. * Reschedule if another task tops the current one.
  632. */
  633. next = __pick_next_entity(cfs_rq);
  634. if (next == curr)
  635. return;
  636. gran = sched_granularity(cfs_rq);
  637. ideal_runtime = niced_granularity(curr,
  638. max(sysctl_sched_latency / cfs_rq->nr_running,
  639. (unsigned long)sysctl_sched_min_granularity));
  640. /*
  641. * If we executed more than what the latency constraint suggests,
  642. * reduce the rescheduling granularity. This way the total latency
  643. * of how much a task is not scheduled converges to
  644. * sysctl_sched_latency:
  645. */
  646. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  647. if (delta_exec > ideal_runtime)
  648. gran = 0;
  649. if (__check_preempt_curr_fair(cfs_rq, next, curr, gran))
  650. curr->prev_sum_exec_runtime = curr->sum_exec_runtime;
  651. }
  652. /**************************************************
  653. * CFS operations on tasks:
  654. */
  655. #ifdef CONFIG_FAIR_GROUP_SCHED
  656. /* Walk up scheduling entities hierarchy */
  657. #define for_each_sched_entity(se) \
  658. for (; se; se = se->parent)
  659. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  660. {
  661. return p->se.cfs_rq;
  662. }
  663. /* runqueue on which this entity is (to be) queued */
  664. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  665. {
  666. return se->cfs_rq;
  667. }
  668. /* runqueue "owned" by this group */
  669. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  670. {
  671. return grp->my_q;
  672. }
  673. /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
  674. * another cpu ('this_cpu')
  675. */
  676. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  677. {
  678. /* A later patch will take group into account */
  679. return &cpu_rq(this_cpu)->cfs;
  680. }
  681. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  682. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  683. list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  684. /* Do the two (enqueued) tasks belong to the same group ? */
  685. static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
  686. {
  687. if (curr->se.cfs_rq == p->se.cfs_rq)
  688. return 1;
  689. return 0;
  690. }
  691. #else /* CONFIG_FAIR_GROUP_SCHED */
  692. #define for_each_sched_entity(se) \
  693. for (; se; se = NULL)
  694. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  695. {
  696. return &task_rq(p)->cfs;
  697. }
  698. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  699. {
  700. struct task_struct *p = task_of(se);
  701. struct rq *rq = task_rq(p);
  702. return &rq->cfs;
  703. }
  704. /* runqueue "owned" by this group */
  705. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  706. {
  707. return NULL;
  708. }
  709. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  710. {
  711. return &cpu_rq(this_cpu)->cfs;
  712. }
  713. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  714. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  715. static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
  716. {
  717. return 1;
  718. }
  719. #endif /* CONFIG_FAIR_GROUP_SCHED */
  720. /*
  721. * The enqueue_task method is called before nr_running is
  722. * increased. Here we update the fair scheduling stats and
  723. * then put the task into the rbtree:
  724. */
  725. static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
  726. {
  727. struct cfs_rq *cfs_rq;
  728. struct sched_entity *se = &p->se;
  729. for_each_sched_entity(se) {
  730. if (se->on_rq)
  731. break;
  732. cfs_rq = cfs_rq_of(se);
  733. enqueue_entity(cfs_rq, se, wakeup);
  734. }
  735. }
  736. /*
  737. * The dequeue_task method is called before nr_running is
  738. * decreased. We remove the task from the rbtree and
  739. * update the fair scheduling stats:
  740. */
  741. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
  742. {
  743. struct cfs_rq *cfs_rq;
  744. struct sched_entity *se = &p->se;
  745. for_each_sched_entity(se) {
  746. cfs_rq = cfs_rq_of(se);
  747. dequeue_entity(cfs_rq, se, sleep);
  748. /* Don't dequeue parent if it has other entities besides us */
  749. if (cfs_rq->load.weight)
  750. break;
  751. }
  752. }
  753. /*
  754. * sched_yield() support is very simple - we dequeue and enqueue
  755. */
  756. static void yield_task_fair(struct rq *rq, struct task_struct *p)
  757. {
  758. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  759. __update_rq_clock(rq);
  760. /*
  761. * Dequeue and enqueue the task to update its
  762. * position within the tree:
  763. */
  764. dequeue_entity(cfs_rq, &p->se, 0);
  765. enqueue_entity(cfs_rq, &p->se, 0);
  766. }
  767. /*
  768. * Preempt the current task with a newly woken task if needed:
  769. */
  770. static void check_preempt_curr_fair(struct rq *rq, struct task_struct *p)
  771. {
  772. struct task_struct *curr = rq->curr;
  773. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  774. unsigned long gran;
  775. if (unlikely(rt_prio(p->prio))) {
  776. update_rq_clock(rq);
  777. update_curr(cfs_rq);
  778. resched_task(curr);
  779. return;
  780. }
  781. gran = sysctl_sched_wakeup_granularity;
  782. /*
  783. * Batch tasks prefer throughput over latency:
  784. */
  785. if (unlikely(p->policy == SCHED_BATCH))
  786. gran = sysctl_sched_batch_wakeup_granularity;
  787. if (is_same_group(curr, p))
  788. __check_preempt_curr_fair(cfs_rq, &p->se, &curr->se, gran);
  789. }
  790. static struct task_struct *pick_next_task_fair(struct rq *rq)
  791. {
  792. struct cfs_rq *cfs_rq = &rq->cfs;
  793. struct sched_entity *se;
  794. if (unlikely(!cfs_rq->nr_running))
  795. return NULL;
  796. do {
  797. se = pick_next_entity(cfs_rq);
  798. cfs_rq = group_cfs_rq(se);
  799. } while (cfs_rq);
  800. return task_of(se);
  801. }
  802. /*
  803. * Account for a descheduled task:
  804. */
  805. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  806. {
  807. struct sched_entity *se = &prev->se;
  808. struct cfs_rq *cfs_rq;
  809. for_each_sched_entity(se) {
  810. cfs_rq = cfs_rq_of(se);
  811. put_prev_entity(cfs_rq, se);
  812. }
  813. }
  814. /**************************************************
  815. * Fair scheduling class load-balancing methods:
  816. */
  817. /*
  818. * Load-balancing iterator. Note: while the runqueue stays locked
  819. * during the whole iteration, the current task might be
  820. * dequeued so the iterator has to be dequeue-safe. Here we
  821. * achieve that by always pre-iterating before returning
  822. * the current task:
  823. */
  824. static inline struct task_struct *
  825. __load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
  826. {
  827. struct task_struct *p;
  828. if (!curr)
  829. return NULL;
  830. p = rb_entry(curr, struct task_struct, se.run_node);
  831. cfs_rq->rb_load_balance_curr = rb_next(curr);
  832. return p;
  833. }
  834. static struct task_struct *load_balance_start_fair(void *arg)
  835. {
  836. struct cfs_rq *cfs_rq = arg;
  837. return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
  838. }
  839. static struct task_struct *load_balance_next_fair(void *arg)
  840. {
  841. struct cfs_rq *cfs_rq = arg;
  842. return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
  843. }
  844. #ifdef CONFIG_FAIR_GROUP_SCHED
  845. static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
  846. {
  847. struct sched_entity *curr;
  848. struct task_struct *p;
  849. if (!cfs_rq->nr_running)
  850. return MAX_PRIO;
  851. curr = __pick_next_entity(cfs_rq);
  852. p = task_of(curr);
  853. return p->prio;
  854. }
  855. #endif
  856. static unsigned long
  857. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  858. unsigned long max_nr_move, unsigned long max_load_move,
  859. struct sched_domain *sd, enum cpu_idle_type idle,
  860. int *all_pinned, int *this_best_prio)
  861. {
  862. struct cfs_rq *busy_cfs_rq;
  863. unsigned long load_moved, total_nr_moved = 0, nr_moved;
  864. long rem_load_move = max_load_move;
  865. struct rq_iterator cfs_rq_iterator;
  866. cfs_rq_iterator.start = load_balance_start_fair;
  867. cfs_rq_iterator.next = load_balance_next_fair;
  868. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  869. #ifdef CONFIG_FAIR_GROUP_SCHED
  870. struct cfs_rq *this_cfs_rq;
  871. long imbalance;
  872. unsigned long maxload;
  873. this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
  874. imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
  875. /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
  876. if (imbalance <= 0)
  877. continue;
  878. /* Don't pull more than imbalance/2 */
  879. imbalance /= 2;
  880. maxload = min(rem_load_move, imbalance);
  881. *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
  882. #else
  883. # define maxload rem_load_move
  884. #endif
  885. /* pass busy_cfs_rq argument into
  886. * load_balance_[start|next]_fair iterators
  887. */
  888. cfs_rq_iterator.arg = busy_cfs_rq;
  889. nr_moved = balance_tasks(this_rq, this_cpu, busiest,
  890. max_nr_move, maxload, sd, idle, all_pinned,
  891. &load_moved, this_best_prio, &cfs_rq_iterator);
  892. total_nr_moved += nr_moved;
  893. max_nr_move -= nr_moved;
  894. rem_load_move -= load_moved;
  895. if (max_nr_move <= 0 || rem_load_move <= 0)
  896. break;
  897. }
  898. return max_load_move - rem_load_move;
  899. }
  900. /*
  901. * scheduler tick hitting a task of our scheduling class:
  902. */
  903. static void task_tick_fair(struct rq *rq, struct task_struct *curr)
  904. {
  905. struct cfs_rq *cfs_rq;
  906. struct sched_entity *se = &curr->se;
  907. for_each_sched_entity(se) {
  908. cfs_rq = cfs_rq_of(se);
  909. entity_tick(cfs_rq, se);
  910. }
  911. }
  912. /*
  913. * Share the fairness runtime between parent and child, thus the
  914. * total amount of pressure for CPU stays equal - new tasks
  915. * get a chance to run but frequent forkers are not allowed to
  916. * monopolize the CPU. Note: the parent runqueue is locked,
  917. * the child is not running yet.
  918. */
  919. static void task_new_fair(struct rq *rq, struct task_struct *p)
  920. {
  921. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  922. struct sched_entity *se = &p->se, *curr = cfs_rq_curr(cfs_rq);
  923. sched_info_queued(p);
  924. update_curr(cfs_rq);
  925. update_stats_enqueue(cfs_rq, se);
  926. /*
  927. * Child runs first: we let it run before the parent
  928. * until it reschedules once. We set up the key so that
  929. * it will preempt the parent:
  930. */
  931. se->fair_key = curr->fair_key -
  932. niced_granularity(curr, sched_granularity(cfs_rq)) - 1;
  933. /*
  934. * The first wait is dominated by the child-runs-first logic,
  935. * so do not credit it with that waiting time yet:
  936. */
  937. if (sysctl_sched_features & SCHED_FEAT_SKIP_INITIAL)
  938. se->wait_start_fair = 0;
  939. /*
  940. * The statistical average of wait_runtime is about
  941. * -granularity/2, so initialize the task with that:
  942. */
  943. if (sysctl_sched_features & SCHED_FEAT_START_DEBIT)
  944. se->wait_runtime = -(sched_granularity(cfs_rq) / 2);
  945. __enqueue_entity(cfs_rq, se);
  946. }
  947. #ifdef CONFIG_FAIR_GROUP_SCHED
  948. /* Account for a task changing its policy or group.
  949. *
  950. * This routine is mostly called to set cfs_rq->curr field when a task
  951. * migrates between groups/classes.
  952. */
  953. static void set_curr_task_fair(struct rq *rq)
  954. {
  955. struct sched_entity *se = &rq->curr->se;
  956. for_each_sched_entity(se)
  957. set_next_entity(cfs_rq_of(se), se);
  958. }
  959. #else
  960. static void set_curr_task_fair(struct rq *rq)
  961. {
  962. }
  963. #endif
  964. /*
  965. * All the scheduling class methods:
  966. */
  967. struct sched_class fair_sched_class __read_mostly = {
  968. .enqueue_task = enqueue_task_fair,
  969. .dequeue_task = dequeue_task_fair,
  970. .yield_task = yield_task_fair,
  971. .check_preempt_curr = check_preempt_curr_fair,
  972. .pick_next_task = pick_next_task_fair,
  973. .put_prev_task = put_prev_task_fair,
  974. .load_balance = load_balance_fair,
  975. .set_curr_task = set_curr_task_fair,
  976. .task_tick = task_tick_fair,
  977. .task_new = task_new_fair,
  978. };
  979. #ifdef CONFIG_SCHED_DEBUG
  980. static void print_cfs_stats(struct seq_file *m, int cpu)
  981. {
  982. struct cfs_rq *cfs_rq;
  983. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  984. print_cfs_rq(m, cpu, cfs_rq);
  985. }
  986. #endif