sched_fair.c 31 KB

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