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