sched_fair.c 31 KB

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