sched_fair.c 30 KB

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