sched_fair.c 27 KB

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