sched_fair.c 30 KB

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