sched_fair.c 26 KB

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