sched_fair.c 30 KB

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