sched_fair.c 25 KB

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