sched_fair.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026
  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. #include <linux/latencytop.h>
  23. /*
  24. * Targeted preemption latency for CPU-bound tasks:
  25. * (default: 5ms * (1 + ilog(ncpus)), units: nanoseconds)
  26. *
  27. * NOTE: this latency value is not the same as the concept of
  28. * 'timeslice length' - timeslices in CFS are of variable length
  29. * and have no persistent notion like in traditional, time-slice
  30. * based scheduling concepts.
  31. *
  32. * (to see the precise effective timeslice length of your workload,
  33. * run vmstat and monitor the context-switches (cs) field)
  34. */
  35. unsigned int sysctl_sched_latency = 5000000ULL;
  36. /*
  37. * Minimal preemption granularity for CPU-bound tasks:
  38. * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
  39. */
  40. unsigned int sysctl_sched_min_granularity = 1000000ULL;
  41. /*
  42. * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
  43. */
  44. static unsigned int sched_nr_latency = 5;
  45. /*
  46. * After fork, child runs first. If set to 0 (default) then
  47. * parent will (try to) run first.
  48. */
  49. unsigned int sysctl_sched_child_runs_first __read_mostly;
  50. /*
  51. * sys_sched_yield() compat mode
  52. *
  53. * This option switches the agressive yield implementation of the
  54. * old scheduler back on.
  55. */
  56. unsigned int __read_mostly sysctl_sched_compat_yield;
  57. /*
  58. * SCHED_OTHER wake-up granularity.
  59. * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
  60. *
  61. * This option delays the preemption effects of decoupled workloads
  62. * and reduces their over-scheduling. Synchronous workloads will still
  63. * have immediate wakeup/sleep latencies.
  64. */
  65. unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
  66. const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
  67. static const struct sched_class fair_sched_class;
  68. /**************************************************************
  69. * CFS operations on generic schedulable entities:
  70. */
  71. #ifdef CONFIG_FAIR_GROUP_SCHED
  72. /* cpu runqueue to which this cfs_rq is attached */
  73. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  74. {
  75. return cfs_rq->rq;
  76. }
  77. /* An entity is a task if it doesn't "own" a runqueue */
  78. #define entity_is_task(se) (!se->my_q)
  79. static inline struct task_struct *task_of(struct sched_entity *se)
  80. {
  81. #ifdef CONFIG_SCHED_DEBUG
  82. WARN_ON_ONCE(!entity_is_task(se));
  83. #endif
  84. return container_of(se, struct task_struct, se);
  85. }
  86. /* Walk up scheduling entities hierarchy */
  87. #define for_each_sched_entity(se) \
  88. for (; se; se = se->parent)
  89. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  90. {
  91. return p->se.cfs_rq;
  92. }
  93. /* runqueue on which this entity is (to be) queued */
  94. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  95. {
  96. return se->cfs_rq;
  97. }
  98. /* runqueue "owned" by this group */
  99. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  100. {
  101. return grp->my_q;
  102. }
  103. /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
  104. * another cpu ('this_cpu')
  105. */
  106. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  107. {
  108. return cfs_rq->tg->cfs_rq[this_cpu];
  109. }
  110. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  111. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  112. list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  113. /* Do the two (enqueued) entities belong to the same group ? */
  114. static inline int
  115. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  116. {
  117. if (se->cfs_rq == pse->cfs_rq)
  118. return 1;
  119. return 0;
  120. }
  121. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  122. {
  123. return se->parent;
  124. }
  125. /* return depth at which a sched entity is present in the hierarchy */
  126. static inline int depth_se(struct sched_entity *se)
  127. {
  128. int depth = 0;
  129. for_each_sched_entity(se)
  130. depth++;
  131. return depth;
  132. }
  133. static void
  134. find_matching_se(struct sched_entity **se, struct sched_entity **pse)
  135. {
  136. int se_depth, pse_depth;
  137. /*
  138. * preemption test can be made between sibling entities who are in the
  139. * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
  140. * both tasks until we find their ancestors who are siblings of common
  141. * parent.
  142. */
  143. /* First walk up until both entities are at same depth */
  144. se_depth = depth_se(*se);
  145. pse_depth = depth_se(*pse);
  146. while (se_depth > pse_depth) {
  147. se_depth--;
  148. *se = parent_entity(*se);
  149. }
  150. while (pse_depth > se_depth) {
  151. pse_depth--;
  152. *pse = parent_entity(*pse);
  153. }
  154. while (!is_same_group(*se, *pse)) {
  155. *se = parent_entity(*se);
  156. *pse = parent_entity(*pse);
  157. }
  158. }
  159. #else /* !CONFIG_FAIR_GROUP_SCHED */
  160. static inline struct task_struct *task_of(struct sched_entity *se)
  161. {
  162. return container_of(se, struct task_struct, se);
  163. }
  164. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  165. {
  166. return container_of(cfs_rq, struct rq, cfs);
  167. }
  168. #define entity_is_task(se) 1
  169. #define for_each_sched_entity(se) \
  170. for (; se; se = NULL)
  171. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  172. {
  173. return &task_rq(p)->cfs;
  174. }
  175. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  176. {
  177. struct task_struct *p = task_of(se);
  178. struct rq *rq = task_rq(p);
  179. return &rq->cfs;
  180. }
  181. /* runqueue "owned" by this group */
  182. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  183. {
  184. return NULL;
  185. }
  186. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  187. {
  188. return &cpu_rq(this_cpu)->cfs;
  189. }
  190. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  191. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  192. static inline int
  193. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  194. {
  195. return 1;
  196. }
  197. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  198. {
  199. return NULL;
  200. }
  201. static inline void
  202. find_matching_se(struct sched_entity **se, struct sched_entity **pse)
  203. {
  204. }
  205. #endif /* CONFIG_FAIR_GROUP_SCHED */
  206. /**************************************************************
  207. * Scheduling class tree data structure manipulation methods:
  208. */
  209. static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
  210. {
  211. s64 delta = (s64)(vruntime - min_vruntime);
  212. if (delta > 0)
  213. min_vruntime = vruntime;
  214. return min_vruntime;
  215. }
  216. static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
  217. {
  218. s64 delta = (s64)(vruntime - min_vruntime);
  219. if (delta < 0)
  220. min_vruntime = vruntime;
  221. return min_vruntime;
  222. }
  223. static inline int entity_before(struct sched_entity *a,
  224. struct sched_entity *b)
  225. {
  226. return (s64)(a->vruntime - b->vruntime) < 0;
  227. }
  228. static inline s64 entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
  229. {
  230. return se->vruntime - cfs_rq->min_vruntime;
  231. }
  232. static void update_min_vruntime(struct cfs_rq *cfs_rq)
  233. {
  234. u64 vruntime = cfs_rq->min_vruntime;
  235. if (cfs_rq->curr)
  236. vruntime = cfs_rq->curr->vruntime;
  237. if (cfs_rq->rb_leftmost) {
  238. struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
  239. struct sched_entity,
  240. run_node);
  241. if (!cfs_rq->curr)
  242. vruntime = se->vruntime;
  243. else
  244. vruntime = min_vruntime(vruntime, se->vruntime);
  245. }
  246. cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
  247. }
  248. /*
  249. * Enqueue an entity into the rb-tree:
  250. */
  251. static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  252. {
  253. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  254. struct rb_node *parent = NULL;
  255. struct sched_entity *entry;
  256. s64 key = entity_key(cfs_rq, se);
  257. int leftmost = 1;
  258. /*
  259. * Find the right place in the rbtree:
  260. */
  261. while (*link) {
  262. parent = *link;
  263. entry = rb_entry(parent, struct sched_entity, run_node);
  264. /*
  265. * We dont care about collisions. Nodes with
  266. * the same key stay together.
  267. */
  268. if (key < entity_key(cfs_rq, entry)) {
  269. link = &parent->rb_left;
  270. } else {
  271. link = &parent->rb_right;
  272. leftmost = 0;
  273. }
  274. }
  275. /*
  276. * Maintain a cache of leftmost tree entries (it is frequently
  277. * used):
  278. */
  279. if (leftmost)
  280. cfs_rq->rb_leftmost = &se->run_node;
  281. rb_link_node(&se->run_node, parent, link);
  282. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  283. }
  284. static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  285. {
  286. if (cfs_rq->rb_leftmost == &se->run_node) {
  287. struct rb_node *next_node;
  288. next_node = rb_next(&se->run_node);
  289. cfs_rq->rb_leftmost = next_node;
  290. }
  291. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  292. }
  293. static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
  294. {
  295. struct rb_node *left = cfs_rq->rb_leftmost;
  296. if (!left)
  297. return NULL;
  298. return rb_entry(left, struct sched_entity, run_node);
  299. }
  300. static struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
  301. {
  302. struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
  303. if (!last)
  304. return NULL;
  305. return rb_entry(last, struct sched_entity, run_node);
  306. }
  307. /**************************************************************
  308. * Scheduling class statistics methods:
  309. */
  310. #ifdef CONFIG_SCHED_DEBUG
  311. int sched_nr_latency_handler(struct ctl_table *table, int write,
  312. void __user *buffer, size_t *lenp,
  313. loff_t *ppos)
  314. {
  315. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  316. if (ret || !write)
  317. return ret;
  318. sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
  319. sysctl_sched_min_granularity);
  320. return 0;
  321. }
  322. #endif
  323. /*
  324. * delta /= w
  325. */
  326. static inline unsigned long
  327. calc_delta_fair(unsigned long delta, struct sched_entity *se)
  328. {
  329. if (unlikely(se->load.weight != NICE_0_LOAD))
  330. delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
  331. return delta;
  332. }
  333. /*
  334. * The idea is to set a period in which each task runs once.
  335. *
  336. * When there are too many tasks (sysctl_sched_nr_latency) we have to stretch
  337. * this period because otherwise the slices get too small.
  338. *
  339. * p = (nr <= nl) ? l : l*nr/nl
  340. */
  341. static u64 __sched_period(unsigned long nr_running)
  342. {
  343. u64 period = sysctl_sched_latency;
  344. unsigned long nr_latency = sched_nr_latency;
  345. if (unlikely(nr_running > nr_latency)) {
  346. period = sysctl_sched_min_granularity;
  347. period *= nr_running;
  348. }
  349. return period;
  350. }
  351. /*
  352. * We calculate the wall-time slice from the period by taking a part
  353. * proportional to the weight.
  354. *
  355. * s = p*P[w/rw]
  356. */
  357. static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  358. {
  359. u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
  360. for_each_sched_entity(se) {
  361. struct load_weight *load;
  362. struct load_weight lw;
  363. cfs_rq = cfs_rq_of(se);
  364. load = &cfs_rq->load;
  365. if (unlikely(!se->on_rq)) {
  366. lw = cfs_rq->load;
  367. update_load_add(&lw, se->load.weight);
  368. load = &lw;
  369. }
  370. slice = calc_delta_mine(slice, se->load.weight, load);
  371. }
  372. return slice;
  373. }
  374. /*
  375. * We calculate the vruntime slice of a to be inserted task
  376. *
  377. * vs = s/w
  378. */
  379. static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  380. {
  381. return calc_delta_fair(sched_slice(cfs_rq, se), se);
  382. }
  383. /*
  384. * Update the current task's runtime statistics. Skip current tasks that
  385. * are not in our scheduling class.
  386. */
  387. static inline void
  388. __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
  389. unsigned long delta_exec)
  390. {
  391. unsigned long delta_exec_weighted;
  392. schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
  393. curr->sum_exec_runtime += delta_exec;
  394. schedstat_add(cfs_rq, exec_clock, delta_exec);
  395. delta_exec_weighted = calc_delta_fair(delta_exec, curr);
  396. curr->vruntime += delta_exec_weighted;
  397. update_min_vruntime(cfs_rq);
  398. }
  399. static void update_curr(struct cfs_rq *cfs_rq)
  400. {
  401. struct sched_entity *curr = cfs_rq->curr;
  402. u64 now = rq_of(cfs_rq)->clock;
  403. unsigned long delta_exec;
  404. if (unlikely(!curr))
  405. return;
  406. /*
  407. * Get the amount of time the current task was running
  408. * since the last time we changed load (this cannot
  409. * overflow on 32 bits):
  410. */
  411. delta_exec = (unsigned long)(now - curr->exec_start);
  412. if (!delta_exec)
  413. return;
  414. __update_curr(cfs_rq, curr, delta_exec);
  415. curr->exec_start = now;
  416. if (entity_is_task(curr)) {
  417. struct task_struct *curtask = task_of(curr);
  418. trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
  419. cpuacct_charge(curtask, delta_exec);
  420. account_group_exec_runtime(curtask, delta_exec);
  421. }
  422. }
  423. static inline void
  424. update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  425. {
  426. schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
  427. }
  428. /*
  429. * Task is being enqueued - update stats:
  430. */
  431. static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  432. {
  433. /*
  434. * Are we enqueueing a waiting task? (for current tasks
  435. * a dequeue/enqueue event is a NOP)
  436. */
  437. if (se != cfs_rq->curr)
  438. update_stats_wait_start(cfs_rq, se);
  439. }
  440. static void
  441. update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  442. {
  443. schedstat_set(se->wait_max, max(se->wait_max,
  444. rq_of(cfs_rq)->clock - se->wait_start));
  445. schedstat_set(se->wait_count, se->wait_count + 1);
  446. schedstat_set(se->wait_sum, se->wait_sum +
  447. rq_of(cfs_rq)->clock - se->wait_start);
  448. #ifdef CONFIG_SCHEDSTATS
  449. if (entity_is_task(se)) {
  450. trace_sched_stat_wait(task_of(se),
  451. rq_of(cfs_rq)->clock - se->wait_start);
  452. }
  453. #endif
  454. schedstat_set(se->wait_start, 0);
  455. }
  456. static inline void
  457. update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  458. {
  459. /*
  460. * Mark the end of the wait period if dequeueing a
  461. * waiting task:
  462. */
  463. if (se != cfs_rq->curr)
  464. update_stats_wait_end(cfs_rq, se);
  465. }
  466. /*
  467. * We are picking a new current task - update its stats:
  468. */
  469. static inline void
  470. update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  471. {
  472. /*
  473. * We are starting a new run period:
  474. */
  475. se->exec_start = rq_of(cfs_rq)->clock;
  476. }
  477. /**************************************************
  478. * Scheduling class queueing methods:
  479. */
  480. #if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
  481. static void
  482. add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight)
  483. {
  484. cfs_rq->task_weight += weight;
  485. }
  486. #else
  487. static inline void
  488. add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight)
  489. {
  490. }
  491. #endif
  492. static void
  493. account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  494. {
  495. update_load_add(&cfs_rq->load, se->load.weight);
  496. if (!parent_entity(se))
  497. inc_cpu_load(rq_of(cfs_rq), se->load.weight);
  498. if (entity_is_task(se)) {
  499. add_cfs_task_weight(cfs_rq, se->load.weight);
  500. list_add(&se->group_node, &cfs_rq->tasks);
  501. }
  502. cfs_rq->nr_running++;
  503. se->on_rq = 1;
  504. }
  505. static void
  506. account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  507. {
  508. update_load_sub(&cfs_rq->load, se->load.weight);
  509. if (!parent_entity(se))
  510. dec_cpu_load(rq_of(cfs_rq), se->load.weight);
  511. if (entity_is_task(se)) {
  512. add_cfs_task_weight(cfs_rq, -se->load.weight);
  513. list_del_init(&se->group_node);
  514. }
  515. cfs_rq->nr_running--;
  516. se->on_rq = 0;
  517. }
  518. static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  519. {
  520. #ifdef CONFIG_SCHEDSTATS
  521. struct task_struct *tsk = NULL;
  522. if (entity_is_task(se))
  523. tsk = task_of(se);
  524. if (se->sleep_start) {
  525. u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
  526. if ((s64)delta < 0)
  527. delta = 0;
  528. if (unlikely(delta > se->sleep_max))
  529. se->sleep_max = delta;
  530. se->sleep_start = 0;
  531. se->sum_sleep_runtime += delta;
  532. if (tsk) {
  533. account_scheduler_latency(tsk, delta >> 10, 1);
  534. trace_sched_stat_sleep(tsk, delta);
  535. }
  536. }
  537. if (se->block_start) {
  538. u64 delta = rq_of(cfs_rq)->clock - se->block_start;
  539. if ((s64)delta < 0)
  540. delta = 0;
  541. if (unlikely(delta > se->block_max))
  542. se->block_max = delta;
  543. se->block_start = 0;
  544. se->sum_sleep_runtime += delta;
  545. if (tsk) {
  546. if (tsk->in_iowait) {
  547. se->iowait_sum += delta;
  548. se->iowait_count++;
  549. trace_sched_stat_iowait(tsk, delta);
  550. }
  551. /*
  552. * Blocking time is in units of nanosecs, so shift by
  553. * 20 to get a milliseconds-range estimation of the
  554. * amount of time that the task spent sleeping:
  555. */
  556. if (unlikely(prof_on == SLEEP_PROFILING)) {
  557. profile_hits(SLEEP_PROFILING,
  558. (void *)get_wchan(tsk),
  559. delta >> 20);
  560. }
  561. account_scheduler_latency(tsk, delta >> 10, 0);
  562. }
  563. }
  564. #endif
  565. }
  566. static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
  567. {
  568. #ifdef CONFIG_SCHED_DEBUG
  569. s64 d = se->vruntime - cfs_rq->min_vruntime;
  570. if (d < 0)
  571. d = -d;
  572. if (d > 3*sysctl_sched_latency)
  573. schedstat_inc(cfs_rq, nr_spread_over);
  574. #endif
  575. }
  576. static void
  577. place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
  578. {
  579. u64 vruntime = cfs_rq->min_vruntime;
  580. /*
  581. * The 'current' period is already promised to the current tasks,
  582. * however the extra weight of the new task will slow them down a
  583. * little, place the new task so that it fits in the slot that
  584. * stays open at the end.
  585. */
  586. if (initial && sched_feat(START_DEBIT))
  587. vruntime += sched_vslice(cfs_rq, se);
  588. /* sleeps up to a single latency don't count. */
  589. if (!initial && sched_feat(FAIR_SLEEPERS)) {
  590. unsigned long thresh = sysctl_sched_latency;
  591. /*
  592. * Convert the sleeper threshold into virtual time.
  593. * SCHED_IDLE is a special sub-class. We care about
  594. * fairness only relative to other SCHED_IDLE tasks,
  595. * all of which have the same weight.
  596. */
  597. if (sched_feat(NORMALIZED_SLEEPER) && (!entity_is_task(se) ||
  598. task_of(se)->policy != SCHED_IDLE))
  599. thresh = calc_delta_fair(thresh, se);
  600. /*
  601. * Halve their sleep time's effect, to allow
  602. * for a gentler effect of sleepers:
  603. */
  604. if (sched_feat(GENTLE_FAIR_SLEEPERS))
  605. thresh >>= 1;
  606. vruntime -= thresh;
  607. }
  608. /* ensure we never gain time by being placed backwards. */
  609. vruntime = max_vruntime(se->vruntime, vruntime);
  610. se->vruntime = vruntime;
  611. }
  612. static void
  613. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
  614. {
  615. /*
  616. * Update run-time statistics of the 'current'.
  617. */
  618. update_curr(cfs_rq);
  619. account_entity_enqueue(cfs_rq, se);
  620. if (wakeup) {
  621. place_entity(cfs_rq, se, 0);
  622. enqueue_sleeper(cfs_rq, se);
  623. }
  624. update_stats_enqueue(cfs_rq, se);
  625. check_spread(cfs_rq, se);
  626. if (se != cfs_rq->curr)
  627. __enqueue_entity(cfs_rq, se);
  628. }
  629. static void __clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
  630. {
  631. if (!se || cfs_rq->last == se)
  632. cfs_rq->last = NULL;
  633. if (!se || cfs_rq->next == se)
  634. cfs_rq->next = NULL;
  635. }
  636. static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
  637. {
  638. for_each_sched_entity(se)
  639. __clear_buddies(cfs_rq_of(se), se);
  640. }
  641. static void
  642. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
  643. {
  644. /*
  645. * Update run-time statistics of the 'current'.
  646. */
  647. update_curr(cfs_rq);
  648. update_stats_dequeue(cfs_rq, se);
  649. if (sleep) {
  650. #ifdef CONFIG_SCHEDSTATS
  651. if (entity_is_task(se)) {
  652. struct task_struct *tsk = task_of(se);
  653. if (tsk->state & TASK_INTERRUPTIBLE)
  654. se->sleep_start = rq_of(cfs_rq)->clock;
  655. if (tsk->state & TASK_UNINTERRUPTIBLE)
  656. se->block_start = rq_of(cfs_rq)->clock;
  657. }
  658. #endif
  659. }
  660. clear_buddies(cfs_rq, se);
  661. if (se != cfs_rq->curr)
  662. __dequeue_entity(cfs_rq, se);
  663. account_entity_dequeue(cfs_rq, se);
  664. update_min_vruntime(cfs_rq);
  665. }
  666. /*
  667. * Preempt the current task with a newly woken task if needed:
  668. */
  669. static void
  670. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  671. {
  672. unsigned long ideal_runtime, delta_exec;
  673. ideal_runtime = sched_slice(cfs_rq, curr);
  674. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  675. if (delta_exec > ideal_runtime) {
  676. resched_task(rq_of(cfs_rq)->curr);
  677. /*
  678. * The current task ran long enough, ensure it doesn't get
  679. * re-elected due to buddy favours.
  680. */
  681. clear_buddies(cfs_rq, curr);
  682. return;
  683. }
  684. /*
  685. * Ensure that a task that missed wakeup preemption by a
  686. * narrow margin doesn't have to wait for a full slice.
  687. * This also mitigates buddy induced latencies under load.
  688. */
  689. if (!sched_feat(WAKEUP_PREEMPT))
  690. return;
  691. if (delta_exec < sysctl_sched_min_granularity)
  692. return;
  693. if (cfs_rq->nr_running > 1) {
  694. struct sched_entity *se = __pick_next_entity(cfs_rq);
  695. s64 delta = curr->vruntime - se->vruntime;
  696. if (delta > ideal_runtime)
  697. resched_task(rq_of(cfs_rq)->curr);
  698. }
  699. }
  700. static void
  701. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  702. {
  703. /* 'current' is not kept within the tree. */
  704. if (se->on_rq) {
  705. /*
  706. * Any task has to be enqueued before it get to execute on
  707. * a CPU. So account for the time it spent waiting on the
  708. * runqueue.
  709. */
  710. update_stats_wait_end(cfs_rq, se);
  711. __dequeue_entity(cfs_rq, se);
  712. }
  713. update_stats_curr_start(cfs_rq, se);
  714. cfs_rq->curr = se;
  715. #ifdef CONFIG_SCHEDSTATS
  716. /*
  717. * Track our maximum slice length, if the CPU's load is at
  718. * least twice that of our own weight (i.e. dont track it
  719. * when there are only lesser-weight tasks around):
  720. */
  721. if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
  722. se->slice_max = max(se->slice_max,
  723. se->sum_exec_runtime - se->prev_sum_exec_runtime);
  724. }
  725. #endif
  726. se->prev_sum_exec_runtime = se->sum_exec_runtime;
  727. }
  728. static int
  729. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
  730. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  731. {
  732. struct sched_entity *se = __pick_next_entity(cfs_rq);
  733. struct sched_entity *left = se;
  734. if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
  735. se = cfs_rq->next;
  736. /*
  737. * Prefer last buddy, try to return the CPU to a preempted task.
  738. */
  739. if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
  740. se = cfs_rq->last;
  741. clear_buddies(cfs_rq, se);
  742. return se;
  743. }
  744. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  745. {
  746. /*
  747. * If still on the runqueue then deactivate_task()
  748. * was not called and update_curr() has to be done:
  749. */
  750. if (prev->on_rq)
  751. update_curr(cfs_rq);
  752. check_spread(cfs_rq, prev);
  753. if (prev->on_rq) {
  754. update_stats_wait_start(cfs_rq, prev);
  755. /* Put 'current' back into the tree. */
  756. __enqueue_entity(cfs_rq, prev);
  757. }
  758. cfs_rq->curr = NULL;
  759. }
  760. static void
  761. entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
  762. {
  763. /*
  764. * Update run-time statistics of the 'current'.
  765. */
  766. update_curr(cfs_rq);
  767. #ifdef CONFIG_SCHED_HRTICK
  768. /*
  769. * queued ticks are scheduled to match the slice, so don't bother
  770. * validating it and just reschedule.
  771. */
  772. if (queued) {
  773. resched_task(rq_of(cfs_rq)->curr);
  774. return;
  775. }
  776. /*
  777. * don't let the period tick interfere with the hrtick preemption
  778. */
  779. if (!sched_feat(DOUBLE_TICK) &&
  780. hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
  781. return;
  782. #endif
  783. if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT))
  784. check_preempt_tick(cfs_rq, curr);
  785. }
  786. /**************************************************
  787. * CFS operations on tasks:
  788. */
  789. #ifdef CONFIG_SCHED_HRTICK
  790. static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
  791. {
  792. struct sched_entity *se = &p->se;
  793. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  794. WARN_ON(task_rq(p) != rq);
  795. if (hrtick_enabled(rq) && cfs_rq->nr_running > 1) {
  796. u64 slice = sched_slice(cfs_rq, se);
  797. u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
  798. s64 delta = slice - ran;
  799. if (delta < 0) {
  800. if (rq->curr == p)
  801. resched_task(p);
  802. return;
  803. }
  804. /*
  805. * Don't schedule slices shorter than 10000ns, that just
  806. * doesn't make sense. Rely on vruntime for fairness.
  807. */
  808. if (rq->curr != p)
  809. delta = max_t(s64, 10000LL, delta);
  810. hrtick_start(rq, delta);
  811. }
  812. }
  813. /*
  814. * called from enqueue/dequeue and updates the hrtick when the
  815. * current task is from our class and nr_running is low enough
  816. * to matter.
  817. */
  818. static void hrtick_update(struct rq *rq)
  819. {
  820. struct task_struct *curr = rq->curr;
  821. if (curr->sched_class != &fair_sched_class)
  822. return;
  823. if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
  824. hrtick_start_fair(rq, curr);
  825. }
  826. #else /* !CONFIG_SCHED_HRTICK */
  827. static inline void
  828. hrtick_start_fair(struct rq *rq, struct task_struct *p)
  829. {
  830. }
  831. static inline void hrtick_update(struct rq *rq)
  832. {
  833. }
  834. #endif
  835. /*
  836. * The enqueue_task method is called before nr_running is
  837. * increased. Here we update the fair scheduling stats and
  838. * then put the task into the rbtree:
  839. */
  840. static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
  841. {
  842. struct cfs_rq *cfs_rq;
  843. struct sched_entity *se = &p->se;
  844. for_each_sched_entity(se) {
  845. if (se->on_rq)
  846. break;
  847. cfs_rq = cfs_rq_of(se);
  848. enqueue_entity(cfs_rq, se, wakeup);
  849. wakeup = 1;
  850. }
  851. hrtick_update(rq);
  852. }
  853. /*
  854. * The dequeue_task method is called before nr_running is
  855. * decreased. We remove the task from the rbtree and
  856. * update the fair scheduling stats:
  857. */
  858. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
  859. {
  860. struct cfs_rq *cfs_rq;
  861. struct sched_entity *se = &p->se;
  862. for_each_sched_entity(se) {
  863. cfs_rq = cfs_rq_of(se);
  864. dequeue_entity(cfs_rq, se, sleep);
  865. /* Don't dequeue parent if it has other entities besides us */
  866. if (cfs_rq->load.weight)
  867. break;
  868. sleep = 1;
  869. }
  870. hrtick_update(rq);
  871. }
  872. /*
  873. * sched_yield() support is very simple - we dequeue and enqueue.
  874. *
  875. * If compat_yield is turned on then we requeue to the end of the tree.
  876. */
  877. static void yield_task_fair(struct rq *rq)
  878. {
  879. struct task_struct *curr = rq->curr;
  880. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  881. struct sched_entity *rightmost, *se = &curr->se;
  882. /*
  883. * Are we the only task in the tree?
  884. */
  885. if (unlikely(cfs_rq->nr_running == 1))
  886. return;
  887. clear_buddies(cfs_rq, se);
  888. if (likely(!sysctl_sched_compat_yield) && curr->policy != SCHED_BATCH) {
  889. update_rq_clock(rq);
  890. /*
  891. * Update run-time statistics of the 'current'.
  892. */
  893. update_curr(cfs_rq);
  894. return;
  895. }
  896. /*
  897. * Find the rightmost entry in the rbtree:
  898. */
  899. rightmost = __pick_last_entity(cfs_rq);
  900. /*
  901. * Already in the rightmost position?
  902. */
  903. if (unlikely(!rightmost || entity_before(rightmost, se)))
  904. return;
  905. /*
  906. * Minimally necessary key value to be last in the tree:
  907. * Upon rescheduling, sched_class::put_prev_task() will place
  908. * 'current' within the tree based on its new key value.
  909. */
  910. se->vruntime = rightmost->vruntime + 1;
  911. }
  912. #ifdef CONFIG_SMP
  913. #ifdef CONFIG_FAIR_GROUP_SCHED
  914. /*
  915. * effective_load() calculates the load change as seen from the root_task_group
  916. *
  917. * Adding load to a group doesn't make a group heavier, but can cause movement
  918. * of group shares between cpus. Assuming the shares were perfectly aligned one
  919. * can calculate the shift in shares.
  920. *
  921. * The problem is that perfectly aligning the shares is rather expensive, hence
  922. * we try to avoid doing that too often - see update_shares(), which ratelimits
  923. * this change.
  924. *
  925. * We compensate this by not only taking the current delta into account, but
  926. * also considering the delta between when the shares were last adjusted and
  927. * now.
  928. *
  929. * We still saw a performance dip, some tracing learned us that between
  930. * cgroup:/ and cgroup:/foo balancing the number of affine wakeups increased
  931. * significantly. Therefore try to bias the error in direction of failing
  932. * the affine wakeup.
  933. *
  934. */
  935. static long effective_load(struct task_group *tg, int cpu,
  936. long wl, long wg)
  937. {
  938. struct sched_entity *se = tg->se[cpu];
  939. if (!tg->parent)
  940. return wl;
  941. /*
  942. * By not taking the decrease of shares on the other cpu into
  943. * account our error leans towards reducing the affine wakeups.
  944. */
  945. if (!wl && sched_feat(ASYM_EFF_LOAD))
  946. return wl;
  947. for_each_sched_entity(se) {
  948. long S, rw, s, a, b;
  949. long more_w;
  950. /*
  951. * Instead of using this increment, also add the difference
  952. * between when the shares were last updated and now.
  953. */
  954. more_w = se->my_q->load.weight - se->my_q->rq_weight;
  955. wl += more_w;
  956. wg += more_w;
  957. S = se->my_q->tg->shares;
  958. s = se->my_q->shares;
  959. rw = se->my_q->rq_weight;
  960. a = S*(rw + wl);
  961. b = S*rw + s*wg;
  962. wl = s*(a-b);
  963. if (likely(b))
  964. wl /= b;
  965. /*
  966. * Assume the group is already running and will
  967. * thus already be accounted for in the weight.
  968. *
  969. * That is, moving shares between CPUs, does not
  970. * alter the group weight.
  971. */
  972. wg = 0;
  973. }
  974. return wl;
  975. }
  976. #else
  977. static inline unsigned long effective_load(struct task_group *tg, int cpu,
  978. unsigned long wl, unsigned long wg)
  979. {
  980. return wl;
  981. }
  982. #endif
  983. static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
  984. {
  985. struct task_struct *curr = current;
  986. unsigned long this_load, load;
  987. int idx, this_cpu, prev_cpu;
  988. unsigned long tl_per_task;
  989. unsigned int imbalance;
  990. struct task_group *tg;
  991. unsigned long weight;
  992. int balanced;
  993. idx = sd->wake_idx;
  994. this_cpu = smp_processor_id();
  995. prev_cpu = task_cpu(p);
  996. load = source_load(prev_cpu, idx);
  997. this_load = target_load(this_cpu, idx);
  998. if (sync) {
  999. if (sched_feat(SYNC_LESS) &&
  1000. (curr->se.avg_overlap > sysctl_sched_migration_cost ||
  1001. p->se.avg_overlap > sysctl_sched_migration_cost))
  1002. sync = 0;
  1003. } else {
  1004. if (sched_feat(SYNC_MORE) &&
  1005. (curr->se.avg_overlap < sysctl_sched_migration_cost &&
  1006. p->se.avg_overlap < sysctl_sched_migration_cost))
  1007. sync = 1;
  1008. }
  1009. /*
  1010. * If sync wakeup then subtract the (maximum possible)
  1011. * effect of the currently running task from the load
  1012. * of the current CPU:
  1013. */
  1014. if (sync) {
  1015. tg = task_group(current);
  1016. weight = current->se.load.weight;
  1017. this_load += effective_load(tg, this_cpu, -weight, -weight);
  1018. load += effective_load(tg, prev_cpu, 0, -weight);
  1019. }
  1020. tg = task_group(p);
  1021. weight = p->se.load.weight;
  1022. imbalance = 100 + (sd->imbalance_pct - 100) / 2;
  1023. /*
  1024. * In low-load situations, where prev_cpu is idle and this_cpu is idle
  1025. * due to the sync cause above having dropped this_load to 0, we'll
  1026. * always have an imbalance, but there's really nothing you can do
  1027. * about that, so that's good too.
  1028. *
  1029. * Otherwise check if either cpus are near enough in load to allow this
  1030. * task to be woken on this_cpu.
  1031. */
  1032. balanced = !this_load ||
  1033. 100*(this_load + effective_load(tg, this_cpu, weight, weight)) <=
  1034. imbalance*(load + effective_load(tg, prev_cpu, 0, weight));
  1035. /*
  1036. * If the currently running task will sleep within
  1037. * a reasonable amount of time then attract this newly
  1038. * woken task:
  1039. */
  1040. if (sync && balanced)
  1041. return 1;
  1042. schedstat_inc(p, se.nr_wakeups_affine_attempts);
  1043. tl_per_task = cpu_avg_load_per_task(this_cpu);
  1044. if (balanced ||
  1045. (this_load <= load &&
  1046. this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
  1047. /*
  1048. * This domain has SD_WAKE_AFFINE and
  1049. * p is cache cold in this domain, and
  1050. * there is no bad imbalance.
  1051. */
  1052. schedstat_inc(sd, ttwu_move_affine);
  1053. schedstat_inc(p, se.nr_wakeups_affine);
  1054. return 1;
  1055. }
  1056. return 0;
  1057. }
  1058. /*
  1059. * find_idlest_group finds and returns the least busy CPU group within the
  1060. * domain.
  1061. */
  1062. static struct sched_group *
  1063. find_idlest_group(struct sched_domain *sd, struct task_struct *p,
  1064. int this_cpu, int load_idx)
  1065. {
  1066. struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
  1067. unsigned long min_load = ULONG_MAX, this_load = 0;
  1068. int imbalance = 100 + (sd->imbalance_pct-100)/2;
  1069. do {
  1070. unsigned long load, avg_load;
  1071. int local_group;
  1072. int i;
  1073. /* Skip over this group if it has no CPUs allowed */
  1074. if (!cpumask_intersects(sched_group_cpus(group),
  1075. &p->cpus_allowed))
  1076. continue;
  1077. local_group = cpumask_test_cpu(this_cpu,
  1078. sched_group_cpus(group));
  1079. /* Tally up the load of all CPUs in the group */
  1080. avg_load = 0;
  1081. for_each_cpu(i, sched_group_cpus(group)) {
  1082. /* Bias balancing toward cpus of our domain */
  1083. if (local_group)
  1084. load = source_load(i, load_idx);
  1085. else
  1086. load = target_load(i, load_idx);
  1087. avg_load += load;
  1088. }
  1089. /* Adjust by relative CPU power of the group */
  1090. avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power;
  1091. if (local_group) {
  1092. this_load = avg_load;
  1093. this = group;
  1094. } else if (avg_load < min_load) {
  1095. min_load = avg_load;
  1096. idlest = group;
  1097. }
  1098. } while (group = group->next, group != sd->groups);
  1099. if (!idlest || 100*this_load < imbalance*min_load)
  1100. return NULL;
  1101. return idlest;
  1102. }
  1103. /*
  1104. * find_idlest_cpu - find the idlest cpu among the cpus in group.
  1105. */
  1106. static int
  1107. find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
  1108. {
  1109. unsigned long load, min_load = ULONG_MAX;
  1110. int idlest = -1;
  1111. int i;
  1112. /* Traverse only the allowed CPUs */
  1113. for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) {
  1114. load = weighted_cpuload(i);
  1115. if (load < min_load || (load == min_load && i == this_cpu)) {
  1116. min_load = load;
  1117. idlest = i;
  1118. }
  1119. }
  1120. return idlest;
  1121. }
  1122. /*
  1123. * sched_balance_self: balance the current task (running on cpu) in domains
  1124. * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
  1125. * SD_BALANCE_EXEC.
  1126. *
  1127. * Balance, ie. select the least loaded group.
  1128. *
  1129. * Returns the target CPU number, or the same CPU if no balancing is needed.
  1130. *
  1131. * preempt must be disabled.
  1132. */
  1133. static int select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
  1134. {
  1135. struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
  1136. int cpu = smp_processor_id();
  1137. int prev_cpu = task_cpu(p);
  1138. int new_cpu = cpu;
  1139. int want_affine = 0;
  1140. int want_sd = 1;
  1141. int sync = wake_flags & WF_SYNC;
  1142. if (sd_flag & SD_BALANCE_WAKE) {
  1143. if (sched_feat(AFFINE_WAKEUPS) &&
  1144. cpumask_test_cpu(cpu, &p->cpus_allowed))
  1145. want_affine = 1;
  1146. new_cpu = prev_cpu;
  1147. }
  1148. rcu_read_lock();
  1149. for_each_domain(cpu, tmp) {
  1150. /*
  1151. * If power savings logic is enabled for a domain, see if we
  1152. * are not overloaded, if so, don't balance wider.
  1153. */
  1154. if (tmp->flags & (SD_POWERSAVINGS_BALANCE|SD_PREFER_LOCAL)) {
  1155. unsigned long power = 0;
  1156. unsigned long nr_running = 0;
  1157. unsigned long capacity;
  1158. int i;
  1159. for_each_cpu(i, sched_domain_span(tmp)) {
  1160. power += power_of(i);
  1161. nr_running += cpu_rq(i)->cfs.nr_running;
  1162. }
  1163. capacity = DIV_ROUND_CLOSEST(power, SCHED_LOAD_SCALE);
  1164. if (tmp->flags & SD_POWERSAVINGS_BALANCE)
  1165. nr_running /= 2;
  1166. if (nr_running < capacity)
  1167. want_sd = 0;
  1168. }
  1169. if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
  1170. cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
  1171. affine_sd = tmp;
  1172. want_affine = 0;
  1173. }
  1174. if (!want_sd && !want_affine)
  1175. break;
  1176. if (!(tmp->flags & sd_flag))
  1177. continue;
  1178. if (want_sd)
  1179. sd = tmp;
  1180. }
  1181. if (sched_feat(LB_SHARES_UPDATE)) {
  1182. /*
  1183. * Pick the largest domain to update shares over
  1184. */
  1185. tmp = sd;
  1186. if (affine_sd && (!tmp ||
  1187. cpumask_weight(sched_domain_span(affine_sd)) >
  1188. cpumask_weight(sched_domain_span(sd))))
  1189. tmp = affine_sd;
  1190. if (tmp)
  1191. update_shares(tmp);
  1192. }
  1193. if (affine_sd && wake_affine(affine_sd, p, sync)) {
  1194. new_cpu = cpu;
  1195. goto out;
  1196. }
  1197. while (sd) {
  1198. int load_idx = sd->forkexec_idx;
  1199. struct sched_group *group;
  1200. int weight;
  1201. if (!(sd->flags & sd_flag)) {
  1202. sd = sd->child;
  1203. continue;
  1204. }
  1205. if (sd_flag & SD_BALANCE_WAKE)
  1206. load_idx = sd->wake_idx;
  1207. group = find_idlest_group(sd, p, cpu, load_idx);
  1208. if (!group) {
  1209. sd = sd->child;
  1210. continue;
  1211. }
  1212. new_cpu = find_idlest_cpu(group, p, cpu);
  1213. if (new_cpu == -1 || new_cpu == cpu) {
  1214. /* Now try balancing at a lower domain level of cpu */
  1215. sd = sd->child;
  1216. continue;
  1217. }
  1218. /* Now try balancing at a lower domain level of new_cpu */
  1219. cpu = new_cpu;
  1220. weight = cpumask_weight(sched_domain_span(sd));
  1221. sd = NULL;
  1222. for_each_domain(cpu, tmp) {
  1223. if (weight <= cpumask_weight(sched_domain_span(tmp)))
  1224. break;
  1225. if (tmp->flags & sd_flag)
  1226. sd = tmp;
  1227. }
  1228. /* while loop will break here if sd == NULL */
  1229. }
  1230. out:
  1231. rcu_read_unlock();
  1232. return new_cpu;
  1233. }
  1234. #endif /* CONFIG_SMP */
  1235. /*
  1236. * Adaptive granularity
  1237. *
  1238. * se->avg_wakeup gives the average time a task runs until it does a wakeup,
  1239. * with the limit of wakeup_gran -- when it never does a wakeup.
  1240. *
  1241. * So the smaller avg_wakeup is the faster we want this task to preempt,
  1242. * but we don't want to treat the preemptee unfairly and therefore allow it
  1243. * to run for at least the amount of time we'd like to run.
  1244. *
  1245. * NOTE: we use 2*avg_wakeup to increase the probability of actually doing one
  1246. *
  1247. * NOTE: we use *nr_running to scale with load, this nicely matches the
  1248. * degrading latency on load.
  1249. */
  1250. static unsigned long
  1251. adaptive_gran(struct sched_entity *curr, struct sched_entity *se)
  1252. {
  1253. u64 this_run = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  1254. u64 expected_wakeup = 2*se->avg_wakeup * cfs_rq_of(se)->nr_running;
  1255. u64 gran = 0;
  1256. if (this_run < expected_wakeup)
  1257. gran = expected_wakeup - this_run;
  1258. return min_t(s64, gran, sysctl_sched_wakeup_granularity);
  1259. }
  1260. static unsigned long
  1261. wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
  1262. {
  1263. unsigned long gran = sysctl_sched_wakeup_granularity;
  1264. if (cfs_rq_of(curr)->curr && sched_feat(ADAPTIVE_GRAN))
  1265. gran = adaptive_gran(curr, se);
  1266. /*
  1267. * Since its curr running now, convert the gran from real-time
  1268. * to virtual-time in his units.
  1269. */
  1270. if (sched_feat(ASYM_GRAN)) {
  1271. /*
  1272. * By using 'se' instead of 'curr' we penalize light tasks, so
  1273. * they get preempted easier. That is, if 'se' < 'curr' then
  1274. * the resulting gran will be larger, therefore penalizing the
  1275. * lighter, if otoh 'se' > 'curr' then the resulting gran will
  1276. * be smaller, again penalizing the lighter task.
  1277. *
  1278. * This is especially important for buddies when the leftmost
  1279. * task is higher priority than the buddy.
  1280. */
  1281. if (unlikely(se->load.weight != NICE_0_LOAD))
  1282. gran = calc_delta_fair(gran, se);
  1283. } else {
  1284. if (unlikely(curr->load.weight != NICE_0_LOAD))
  1285. gran = calc_delta_fair(gran, curr);
  1286. }
  1287. return gran;
  1288. }
  1289. /*
  1290. * Should 'se' preempt 'curr'.
  1291. *
  1292. * |s1
  1293. * |s2
  1294. * |s3
  1295. * g
  1296. * |<--->|c
  1297. *
  1298. * w(c, s1) = -1
  1299. * w(c, s2) = 0
  1300. * w(c, s3) = 1
  1301. *
  1302. */
  1303. static int
  1304. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
  1305. {
  1306. s64 gran, vdiff = curr->vruntime - se->vruntime;
  1307. if (vdiff <= 0)
  1308. return -1;
  1309. gran = wakeup_gran(curr, se);
  1310. if (vdiff > gran)
  1311. return 1;
  1312. return 0;
  1313. }
  1314. static void set_last_buddy(struct sched_entity *se)
  1315. {
  1316. if (likely(task_of(se)->policy != SCHED_IDLE)) {
  1317. for_each_sched_entity(se)
  1318. cfs_rq_of(se)->last = se;
  1319. }
  1320. }
  1321. static void set_next_buddy(struct sched_entity *se)
  1322. {
  1323. if (likely(task_of(se)->policy != SCHED_IDLE)) {
  1324. for_each_sched_entity(se)
  1325. cfs_rq_of(se)->next = se;
  1326. }
  1327. }
  1328. /*
  1329. * Preempt the current task with a newly woken task if needed:
  1330. */
  1331. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
  1332. {
  1333. struct task_struct *curr = rq->curr;
  1334. struct sched_entity *se = &curr->se, *pse = &p->se;
  1335. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  1336. int sync = wake_flags & WF_SYNC;
  1337. int scale = cfs_rq->nr_running >= sched_nr_latency;
  1338. update_curr(cfs_rq);
  1339. if (unlikely(rt_prio(p->prio))) {
  1340. resched_task(curr);
  1341. return;
  1342. }
  1343. if (unlikely(p->sched_class != &fair_sched_class))
  1344. return;
  1345. if (unlikely(se == pse))
  1346. return;
  1347. if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK))
  1348. set_next_buddy(pse);
  1349. /*
  1350. * We can come here with TIF_NEED_RESCHED already set from new task
  1351. * wake up path.
  1352. */
  1353. if (test_tsk_need_resched(curr))
  1354. return;
  1355. /*
  1356. * Batch and idle tasks do not preempt (their preemption is driven by
  1357. * the tick):
  1358. */
  1359. if (unlikely(p->policy != SCHED_NORMAL))
  1360. return;
  1361. /* Idle tasks are by definition preempted by everybody. */
  1362. if (unlikely(curr->policy == SCHED_IDLE)) {
  1363. resched_task(curr);
  1364. return;
  1365. }
  1366. if ((sched_feat(WAKEUP_SYNC) && sync) ||
  1367. (sched_feat(WAKEUP_OVERLAP) &&
  1368. (se->avg_overlap < sysctl_sched_migration_cost &&
  1369. pse->avg_overlap < sysctl_sched_migration_cost))) {
  1370. resched_task(curr);
  1371. return;
  1372. }
  1373. if (sched_feat(WAKEUP_RUNNING)) {
  1374. if (pse->avg_running < se->avg_running) {
  1375. set_next_buddy(pse);
  1376. resched_task(curr);
  1377. return;
  1378. }
  1379. }
  1380. if (!sched_feat(WAKEUP_PREEMPT))
  1381. return;
  1382. find_matching_se(&se, &pse);
  1383. BUG_ON(!pse);
  1384. if (wakeup_preempt_entity(se, pse) == 1) {
  1385. resched_task(curr);
  1386. /*
  1387. * Only set the backward buddy when the current task is still
  1388. * on the rq. This can happen when a wakeup gets interleaved
  1389. * with schedule on the ->pre_schedule() or idle_balance()
  1390. * point, either of which can * drop the rq lock.
  1391. *
  1392. * Also, during early boot the idle thread is in the fair class,
  1393. * for obvious reasons its a bad idea to schedule back to it.
  1394. */
  1395. if (unlikely(!se->on_rq || curr == rq->idle))
  1396. return;
  1397. if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
  1398. set_last_buddy(se);
  1399. }
  1400. }
  1401. static struct task_struct *pick_next_task_fair(struct rq *rq)
  1402. {
  1403. struct task_struct *p;
  1404. struct cfs_rq *cfs_rq = &rq->cfs;
  1405. struct sched_entity *se;
  1406. if (unlikely(!cfs_rq->nr_running))
  1407. return NULL;
  1408. do {
  1409. se = pick_next_entity(cfs_rq);
  1410. set_next_entity(cfs_rq, se);
  1411. cfs_rq = group_cfs_rq(se);
  1412. } while (cfs_rq);
  1413. p = task_of(se);
  1414. hrtick_start_fair(rq, p);
  1415. return p;
  1416. }
  1417. /*
  1418. * Account for a descheduled task:
  1419. */
  1420. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  1421. {
  1422. struct sched_entity *se = &prev->se;
  1423. struct cfs_rq *cfs_rq;
  1424. for_each_sched_entity(se) {
  1425. cfs_rq = cfs_rq_of(se);
  1426. put_prev_entity(cfs_rq, se);
  1427. }
  1428. }
  1429. #ifdef CONFIG_SMP
  1430. /**************************************************
  1431. * Fair scheduling class load-balancing methods:
  1432. */
  1433. /*
  1434. * Load-balancing iterator. Note: while the runqueue stays locked
  1435. * during the whole iteration, the current task might be
  1436. * dequeued so the iterator has to be dequeue-safe. Here we
  1437. * achieve that by always pre-iterating before returning
  1438. * the current task:
  1439. */
  1440. static struct task_struct *
  1441. __load_balance_iterator(struct cfs_rq *cfs_rq, struct list_head *next)
  1442. {
  1443. struct task_struct *p = NULL;
  1444. struct sched_entity *se;
  1445. if (next == &cfs_rq->tasks)
  1446. return NULL;
  1447. se = list_entry(next, struct sched_entity, group_node);
  1448. p = task_of(se);
  1449. cfs_rq->balance_iterator = next->next;
  1450. return p;
  1451. }
  1452. static struct task_struct *load_balance_start_fair(void *arg)
  1453. {
  1454. struct cfs_rq *cfs_rq = arg;
  1455. return __load_balance_iterator(cfs_rq, cfs_rq->tasks.next);
  1456. }
  1457. static struct task_struct *load_balance_next_fair(void *arg)
  1458. {
  1459. struct cfs_rq *cfs_rq = arg;
  1460. return __load_balance_iterator(cfs_rq, cfs_rq->balance_iterator);
  1461. }
  1462. static unsigned long
  1463. __load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1464. unsigned long max_load_move, struct sched_domain *sd,
  1465. enum cpu_idle_type idle, int *all_pinned, int *this_best_prio,
  1466. struct cfs_rq *cfs_rq)
  1467. {
  1468. struct rq_iterator cfs_rq_iterator;
  1469. cfs_rq_iterator.start = load_balance_start_fair;
  1470. cfs_rq_iterator.next = load_balance_next_fair;
  1471. cfs_rq_iterator.arg = cfs_rq;
  1472. return balance_tasks(this_rq, this_cpu, busiest,
  1473. max_load_move, sd, idle, all_pinned,
  1474. this_best_prio, &cfs_rq_iterator);
  1475. }
  1476. #ifdef CONFIG_FAIR_GROUP_SCHED
  1477. static unsigned long
  1478. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1479. unsigned long max_load_move,
  1480. struct sched_domain *sd, enum cpu_idle_type idle,
  1481. int *all_pinned, int *this_best_prio)
  1482. {
  1483. long rem_load_move = max_load_move;
  1484. int busiest_cpu = cpu_of(busiest);
  1485. struct task_group *tg;
  1486. rcu_read_lock();
  1487. update_h_load(busiest_cpu);
  1488. list_for_each_entry_rcu(tg, &task_groups, list) {
  1489. struct cfs_rq *busiest_cfs_rq = tg->cfs_rq[busiest_cpu];
  1490. unsigned long busiest_h_load = busiest_cfs_rq->h_load;
  1491. unsigned long busiest_weight = busiest_cfs_rq->load.weight;
  1492. u64 rem_load, moved_load;
  1493. /*
  1494. * empty group
  1495. */
  1496. if (!busiest_cfs_rq->task_weight)
  1497. continue;
  1498. rem_load = (u64)rem_load_move * busiest_weight;
  1499. rem_load = div_u64(rem_load, busiest_h_load + 1);
  1500. moved_load = __load_balance_fair(this_rq, this_cpu, busiest,
  1501. rem_load, sd, idle, all_pinned, this_best_prio,
  1502. tg->cfs_rq[busiest_cpu]);
  1503. if (!moved_load)
  1504. continue;
  1505. moved_load *= busiest_h_load;
  1506. moved_load = div_u64(moved_load, busiest_weight + 1);
  1507. rem_load_move -= moved_load;
  1508. if (rem_load_move < 0)
  1509. break;
  1510. }
  1511. rcu_read_unlock();
  1512. return max_load_move - rem_load_move;
  1513. }
  1514. #else
  1515. static unsigned long
  1516. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1517. unsigned long max_load_move,
  1518. struct sched_domain *sd, enum cpu_idle_type idle,
  1519. int *all_pinned, int *this_best_prio)
  1520. {
  1521. return __load_balance_fair(this_rq, this_cpu, busiest,
  1522. max_load_move, sd, idle, all_pinned,
  1523. this_best_prio, &busiest->cfs);
  1524. }
  1525. #endif
  1526. static int
  1527. move_one_task_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1528. struct sched_domain *sd, enum cpu_idle_type idle)
  1529. {
  1530. struct cfs_rq *busy_cfs_rq;
  1531. struct rq_iterator cfs_rq_iterator;
  1532. cfs_rq_iterator.start = load_balance_start_fair;
  1533. cfs_rq_iterator.next = load_balance_next_fair;
  1534. for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
  1535. /*
  1536. * pass busy_cfs_rq argument into
  1537. * load_balance_[start|next]_fair iterators
  1538. */
  1539. cfs_rq_iterator.arg = busy_cfs_rq;
  1540. if (iter_move_one_task(this_rq, this_cpu, busiest, sd, idle,
  1541. &cfs_rq_iterator))
  1542. return 1;
  1543. }
  1544. return 0;
  1545. }
  1546. #endif /* CONFIG_SMP */
  1547. /*
  1548. * scheduler tick hitting a task of our scheduling class:
  1549. */
  1550. static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
  1551. {
  1552. struct cfs_rq *cfs_rq;
  1553. struct sched_entity *se = &curr->se;
  1554. for_each_sched_entity(se) {
  1555. cfs_rq = cfs_rq_of(se);
  1556. entity_tick(cfs_rq, se, queued);
  1557. }
  1558. }
  1559. /*
  1560. * Share the fairness runtime between parent and child, thus the
  1561. * total amount of pressure for CPU stays equal - new tasks
  1562. * get a chance to run but frequent forkers are not allowed to
  1563. * monopolize the CPU. Note: the parent runqueue is locked,
  1564. * the child is not running yet.
  1565. */
  1566. static void task_new_fair(struct rq *rq, struct task_struct *p)
  1567. {
  1568. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  1569. struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
  1570. int this_cpu = smp_processor_id();
  1571. sched_info_queued(p);
  1572. update_curr(cfs_rq);
  1573. if (curr)
  1574. se->vruntime = curr->vruntime;
  1575. place_entity(cfs_rq, se, 1);
  1576. /* 'curr' will be NULL if the child belongs to a different group */
  1577. if (sysctl_sched_child_runs_first && this_cpu == task_cpu(p) &&
  1578. curr && entity_before(curr, se)) {
  1579. /*
  1580. * Upon rescheduling, sched_class::put_prev_task() will place
  1581. * 'current' within the tree based on its new key value.
  1582. */
  1583. swap(curr->vruntime, se->vruntime);
  1584. resched_task(rq->curr);
  1585. }
  1586. enqueue_task_fair(rq, p, 0);
  1587. }
  1588. /*
  1589. * Priority of the task has changed. Check to see if we preempt
  1590. * the current task.
  1591. */
  1592. static void prio_changed_fair(struct rq *rq, struct task_struct *p,
  1593. int oldprio, int running)
  1594. {
  1595. /*
  1596. * Reschedule if we are currently running on this runqueue and
  1597. * our priority decreased, or if we are not currently running on
  1598. * this runqueue and our priority is higher than the current's
  1599. */
  1600. if (running) {
  1601. if (p->prio > oldprio)
  1602. resched_task(rq->curr);
  1603. } else
  1604. check_preempt_curr(rq, p, 0);
  1605. }
  1606. /*
  1607. * We switched to the sched_fair class.
  1608. */
  1609. static void switched_to_fair(struct rq *rq, struct task_struct *p,
  1610. int running)
  1611. {
  1612. /*
  1613. * We were most likely switched from sched_rt, so
  1614. * kick off the schedule if running, otherwise just see
  1615. * if we can still preempt the current task.
  1616. */
  1617. if (running)
  1618. resched_task(rq->curr);
  1619. else
  1620. check_preempt_curr(rq, p, 0);
  1621. }
  1622. /* Account for a task changing its policy or group.
  1623. *
  1624. * This routine is mostly called to set cfs_rq->curr field when a task
  1625. * migrates between groups/classes.
  1626. */
  1627. static void set_curr_task_fair(struct rq *rq)
  1628. {
  1629. struct sched_entity *se = &rq->curr->se;
  1630. for_each_sched_entity(se)
  1631. set_next_entity(cfs_rq_of(se), se);
  1632. }
  1633. #ifdef CONFIG_FAIR_GROUP_SCHED
  1634. static void moved_group_fair(struct task_struct *p)
  1635. {
  1636. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  1637. update_curr(cfs_rq);
  1638. place_entity(cfs_rq, &p->se, 1);
  1639. }
  1640. #endif
  1641. unsigned int get_rr_interval_fair(struct task_struct *task)
  1642. {
  1643. struct sched_entity *se = &task->se;
  1644. unsigned long flags;
  1645. struct rq *rq;
  1646. unsigned int rr_interval = 0;
  1647. /*
  1648. * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
  1649. * idle runqueue:
  1650. */
  1651. rq = task_rq_lock(task, &flags);
  1652. if (rq->cfs.load.weight)
  1653. rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
  1654. task_rq_unlock(rq, &flags);
  1655. return rr_interval;
  1656. }
  1657. /*
  1658. * All the scheduling class methods:
  1659. */
  1660. static const struct sched_class fair_sched_class = {
  1661. .next = &idle_sched_class,
  1662. .enqueue_task = enqueue_task_fair,
  1663. .dequeue_task = dequeue_task_fair,
  1664. .yield_task = yield_task_fair,
  1665. .check_preempt_curr = check_preempt_wakeup,
  1666. .pick_next_task = pick_next_task_fair,
  1667. .put_prev_task = put_prev_task_fair,
  1668. #ifdef CONFIG_SMP
  1669. .select_task_rq = select_task_rq_fair,
  1670. .load_balance = load_balance_fair,
  1671. .move_one_task = move_one_task_fair,
  1672. #endif
  1673. .set_curr_task = set_curr_task_fair,
  1674. .task_tick = task_tick_fair,
  1675. .task_new = task_new_fair,
  1676. .prio_changed = prio_changed_fair,
  1677. .switched_to = switched_to_fair,
  1678. .get_rr_interval = get_rr_interval_fair,
  1679. #ifdef CONFIG_FAIR_GROUP_SCHED
  1680. .moved_group = moved_group_fair,
  1681. #endif
  1682. };
  1683. #ifdef CONFIG_SCHED_DEBUG
  1684. static void print_cfs_stats(struct seq_file *m, int cpu)
  1685. {
  1686. struct cfs_rq *cfs_rq;
  1687. rcu_read_lock();
  1688. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  1689. print_cfs_rq(m, cpu, cfs_rq);
  1690. rcu_read_unlock();
  1691. }
  1692. #endif