sched.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. /* CFS-lite scheduler core: per-CPU state, a weighted run queue, a sorted sleep
  2. * list and the timer entry.
  3. *
  4. * Weight is an integer 1..SCHED_WEIGHT_MAX. Each ready process has a 64-bit
  5. * virtual runtime advanced by (elapsed ticks * stride) while it runs, where
  6. * stride = SCHED_WEIGHT_SCALE / weight. The smallest vruntime gets the CPU,
  7. * so the long-term share is proportional to the weight. A 10 ms slice bounds
  8. * latency; a wake-up gets clamped to the current minimum vruntime so an
  9. * interactive process is scheduled promptly without accumulating unbounded
  10. * credit.
  11. *
  12. * The run queue is a single global list protected by sched_lock; per-CPU
  13. * current lets every CPU pick the next eligible task from it.
  14. */
  15. #include "sched.h"
  16. #include "io.h"
  17. #include "lapic.h"
  18. #include "paging.h"
  19. #include "process.h"
  20. #include "smp.h"
  21. #include "spinlock.h"
  22. #include "syscall.h"
  23. #include "thermal.h"
  24. #include "timer.h"
  25. #include "tss.h"
  26. /* 2^20 / weight, so weight 8 advances 131072 per tick and weight 16 half
  27. * that. The product fits comfortably in the 64-bit vruntime. */
  28. #define SCHED_WEIGHT_SCALE (1u << 20)
  29. struct cpu cpus[MAX_CPUS];
  30. unsigned int cpu_count;
  31. unsigned int cpu_gs_ready;
  32. static struct process *runq_head, *runq_tail;
  33. static struct process *sleep_head, *sleep_tail;
  34. static unsigned int wakeups;
  35. /* Protects the global run/sleep/wait queues and the process-table state that
  36. * the timer path touches. This is a spinlock: it is held with interrupts
  37. * off for short, non-blocking critical sections only. */
  38. static spinlock_t sched_lock;
  39. /* Sleeping big kernel lock. One owner process at a time (recursive); the
  40. * owner is never timer-preempted, and every voluntary block drops it so the
  41. * other CPU can keep making progress. See bkl_lock()/bkl_unlock(). */
  42. static struct {
  43. struct process *owner;
  44. unsigned int depth;
  45. struct wait_queue wait;
  46. } bkl;
  47. unsigned int sched_lock_acquire(void)
  48. {
  49. unsigned int flags = spin_irq_save_disable();
  50. spin_lock(&sched_lock);
  51. return flags;
  52. }
  53. void sched_lock_release(unsigned int flags)
  54. {
  55. spin_unlock(&sched_lock);
  56. spin_irq_restore(flags);
  57. }
  58. void sched_lock_reset(void)
  59. {
  60. sched_lock.locked = 0;
  61. }
  62. /* switch.S reads these two facts directly: current at %gs:0 and esp at
  63. * offset 0 of the process. Keep them true. */
  64. _Static_assert(__builtin_offsetof(struct cpu, current) == 0,
  65. "switch.S reads current from %gs:0");
  66. _Static_assert(__builtin_offsetof(struct process, esp) == 0,
  67. "switch.S saves the interrupted esp at offset 0");
  68. _Static_assert(__builtin_offsetof(struct cpu, need_resched) == 24,
  69. "smp_resched_wake expects need_resched at offset 24");
  70. static inline unsigned long long weight_stride(unsigned int weight)
  71. {
  72. if (weight < 1)
  73. weight = 1;
  74. if (weight > SCHED_WEIGHT_MAX)
  75. weight = SCHED_WEIGHT_MAX;
  76. return (unsigned long long)(SCHED_WEIGHT_SCALE / weight);
  77. }
  78. /* ---- run queue (interrupts off) ---- */
  79. static void runq_add_locked(struct process *p)
  80. {
  81. if (p->on_runq)
  82. return;
  83. p->on_runq = 1;
  84. p->run_prev = runq_tail;
  85. p->run_next = 0;
  86. if (runq_tail)
  87. runq_tail->run_next = p;
  88. else
  89. runq_head = p;
  90. runq_tail = p;
  91. }
  92. static void runq_del_locked(struct process *p)
  93. {
  94. if (!p->on_runq)
  95. return;
  96. if (p->run_prev)
  97. p->run_prev->run_next = p->run_next;
  98. else
  99. runq_head = p->run_next;
  100. if (p->run_next)
  101. p->run_next->run_prev = p->run_prev;
  102. else
  103. runq_tail = p->run_prev;
  104. p->on_runq = 0;
  105. p->run_next = p->run_prev = 0;
  106. }
  107. static void sleepq_add_locked(struct process *p)
  108. {
  109. struct process *it = sleep_head, *prev = 0;
  110. while (it && (int)(it->wake_ms - p->wake_ms) <= 0) {
  111. prev = it;
  112. it = it->sleep_next;
  113. }
  114. p->sleep_prev = prev;
  115. p->sleep_next = it;
  116. if (prev)
  117. prev->sleep_next = p;
  118. else
  119. sleep_head = p;
  120. if (it)
  121. it->sleep_prev = p;
  122. else
  123. sleep_tail = p;
  124. }
  125. static void sleepq_del_locked(struct process *p)
  126. {
  127. if (p->sleep_prev)
  128. p->sleep_prev->sleep_next = p->sleep_next;
  129. else if (sleep_head == p)
  130. sleep_head = p->sleep_next;
  131. if (p->sleep_next)
  132. p->sleep_next->sleep_prev = p->sleep_prev;
  133. else if (sleep_tail == p)
  134. sleep_tail = p->sleep_prev;
  135. p->sleep_next = p->sleep_prev = 0;
  136. }
  137. void wait_queue_init(struct wait_queue *wq)
  138. {
  139. wq->head = wq->tail = 0;
  140. }
  141. static void waitq_add_locked(struct wait_queue *wq, struct process *p)
  142. {
  143. p->waiting_on = wq;
  144. p->wait_next = 0;
  145. p->wait_prev = wq->tail;
  146. if (wq->tail)
  147. wq->tail->wait_next = p;
  148. else
  149. wq->head = p;
  150. wq->tail = p;
  151. }
  152. static void waitq_del_locked(struct process *p)
  153. {
  154. struct wait_queue *wq = p->waiting_on;
  155. if (!wq)
  156. return;
  157. if (p->wait_prev)
  158. p->wait_prev->wait_next = p->wait_next;
  159. else
  160. wq->head = p->wait_next;
  161. if (p->wait_next)
  162. p->wait_next->wait_prev = p->wait_prev;
  163. else
  164. wq->tail = p->wait_prev;
  165. p->wait_next = p->wait_prev = 0;
  166. p->waiting_on = 0;
  167. }
  168. /* Current minimum vruntime among READY processes. If nothing is ready, use
  169. * the running process's vruntime so a freshly woken process does not receive
  170. * an enormous credit relative to a CPU-bound one. */
  171. static unsigned long long min_vruntime_locked(void)
  172. {
  173. struct process *p;
  174. unsigned long long min = 0;
  175. int found = 0;
  176. for (p = runq_head; p; p = p->run_next) {
  177. if (!found || p->vruntime < min) {
  178. min = p->vruntime;
  179. found = 1;
  180. }
  181. }
  182. if (found)
  183. return min;
  184. p = current_proc();
  185. return (p && p != this_cpu()->idle) ? p->vruntime : 0;
  186. }
  187. static void account_locked(struct process *p)
  188. {
  189. unsigned int elapsed;
  190. if (!p || p == this_cpu()->idle || p->state != PROC_RUNNING)
  191. return;
  192. elapsed = get_ticks_ms() - p->slice_start;
  193. if (elapsed) {
  194. p->vruntime += (unsigned long long)elapsed * p->stride;
  195. p->slice_start = get_ticks_ms();
  196. }
  197. }
  198. /* Clamp a woken process to the current minimum and make it runnable. */
  199. static void wake_locked(struct process *p)
  200. {
  201. struct process *cur = current_proc();
  202. struct cpu *c = this_cpu();
  203. if (!p || p->state == PROC_DEAD || p->state == PROC_UNUSED)
  204. return;
  205. if (p->waiting_on)
  206. waitq_del_locked(p);
  207. if (p->state == PROC_SLEEPING)
  208. sleepq_del_locked(p);
  209. p->state = PROC_READY;
  210. if (p->vruntime < min_vruntime_locked())
  211. p->vruntime = min_vruntime_locked();
  212. runq_add_locked(p);
  213. wakeups++;
  214. if (cur && (p->affinity & (1u << c->id)) &&
  215. (cur == c->idle || p->weight >= cur->weight)) {
  216. c->need_resched = 1;
  217. /* The idle timer period is 10 ms; shorten it so the woken
  218. * process runs at the next millisecond instead. */
  219. smp_resched_wake();
  220. }
  221. }
  222. void sched_add_ready(struct process *p)
  223. {
  224. unsigned int flags = sched_lock_acquire();
  225. if (!p->weight) {
  226. p->weight = SCHED_WEIGHT_DEFAULT;
  227. p->stride = weight_stride(p->weight);
  228. p->vruntime = min_vruntime_locked();
  229. }
  230. p->state = PROC_READY;
  231. runq_add_locked(p);
  232. sched_lock_release(flags);
  233. }
  234. void sched_unlink_locked(struct process *p)
  235. {
  236. runq_del_locked(p);
  237. if (p->waiting_on)
  238. waitq_del_locked(p);
  239. if (p->state == PROC_SLEEPING)
  240. sleepq_del_locked(p);
  241. }
  242. void sched_unlink(struct process *p)
  243. {
  244. unsigned int flags = sched_lock_acquire();
  245. sched_unlink_locked(p);
  246. sched_lock_release(flags);
  247. }
  248. void sched_set_weight_locked(struct process *p, unsigned int weight)
  249. {
  250. if (!p)
  251. return;
  252. if (weight < 1)
  253. weight = 1;
  254. if (weight > SCHED_WEIGHT_MAX)
  255. weight = SCHED_WEIGHT_MAX;
  256. if (p->state == PROC_RUNNING)
  257. account_locked(p);
  258. p->weight = weight;
  259. p->stride = weight_stride(weight);
  260. }
  261. /* Add the current process to a wait queue. The caller holds interrupts off
  262. * and must restore the flags and call sched_yield() itself. */
  263. void sched_block_on_locked(struct wait_queue *wq)
  264. {
  265. struct process *p = current_proc();
  266. if (!p)
  267. return;
  268. runq_del_locked(p);
  269. waitq_add_locked(wq, p);
  270. p->state = PROC_BLOCKED;
  271. p->on_runq = 0;
  272. }
  273. void sched_block_on(struct wait_queue *wq)
  274. {
  275. unsigned int flags;
  276. if (this_cpu()->preempt_count)
  277. return;
  278. flags = sched_lock_acquire();
  279. sched_block_on_locked(wq);
  280. sched_lock_release(flags);
  281. sched_yield();
  282. }
  283. /* Block on a wait queue with a deadline. Either a waiter wake or the sleep
  284. * list expiry removes the process from both queues. */
  285. void sched_block_timeout(struct wait_queue *wq, unsigned int ms)
  286. {
  287. struct process *p = current_proc();
  288. unsigned int flags;
  289. if (!p)
  290. return;
  291. if (ms == 0) {
  292. sched_block_on(wq);
  293. return;
  294. }
  295. if (this_cpu()->preempt_count)
  296. return;
  297. flags = sched_lock_acquire();
  298. p->wake_ms = get_ticks_ms() + ms;
  299. p->state = PROC_SLEEPING;
  300. runq_del_locked(p);
  301. waitq_add_locked(wq, p);
  302. sleepq_add_locked(p);
  303. sched_lock_release(flags);
  304. sched_yield();
  305. }
  306. static void wake_one_locked(struct wait_queue *wq)
  307. {
  308. struct process *p = wq->head;
  309. if (!p)
  310. return;
  311. waitq_del_locked(p);
  312. if (p->state == PROC_BLOCKED || p->state == PROC_SLEEPING)
  313. wake_locked(p);
  314. }
  315. void wait_wake_one(struct wait_queue *wq)
  316. {
  317. unsigned int flags = sched_lock_acquire();
  318. wake_one_locked(wq);
  319. sched_lock_release(flags);
  320. }
  321. void wait_wake_all(struct wait_queue *wq)
  322. {
  323. unsigned int flags = sched_lock_acquire();
  324. while (wq->head)
  325. wake_one_locked(wq);
  326. sched_lock_release(flags);
  327. }
  328. /* ---- sleeping big kernel lock ---- */
  329. void bkl_init(void)
  330. {
  331. bkl.owner = 0;
  332. bkl.depth = 0;
  333. wait_queue_init(&bkl.wait);
  334. }
  335. void bkl_lock(void)
  336. {
  337. struct process *p = current_proc();
  338. unsigned int flags;
  339. if (!p)
  340. return;
  341. if (bkl.owner == p) {
  342. bkl.depth++;
  343. return;
  344. }
  345. for (;;) {
  346. flags = sched_lock_acquire();
  347. if (!bkl.owner) {
  348. bkl.owner = p;
  349. bkl.depth = 1;
  350. sched_lock_release(flags);
  351. return;
  352. }
  353. sched_block_on_locked(&bkl.wait);
  354. sched_lock_release(flags);
  355. sched_yield();
  356. }
  357. }
  358. void bkl_unlock(void)
  359. {
  360. struct process *p = current_proc();
  361. unsigned int flags;
  362. if (!p)
  363. return;
  364. flags = sched_lock_acquire();
  365. if (bkl.owner == p) {
  366. if (bkl.depth > 1) {
  367. bkl.depth--;
  368. } else {
  369. bkl.owner = 0;
  370. bkl.depth = 0;
  371. wake_one_locked(&bkl.wait);
  372. }
  373. }
  374. sched_lock_release(flags);
  375. }
  376. int bkl_trylock(void)
  377. {
  378. struct process *p = current_proc();
  379. unsigned int flags;
  380. if (!p)
  381. return 1;
  382. if (bkl.owner == p) {
  383. bkl.depth++;
  384. return 1;
  385. }
  386. flags = sched_lock_acquire();
  387. if (!bkl.owner) {
  388. bkl.owner = p;
  389. bkl.depth = 1;
  390. sched_lock_release(flags);
  391. return 1;
  392. }
  393. sched_lock_release(flags);
  394. return 0;
  395. }
  396. /* Drop the BKL around a voluntary switch and take it back on resume. The
  397. * saved depth lives in the PCB: only the owner can be holding it, and it is
  398. * the current process on this CPU. */
  399. static void bkl_yield_release(void)
  400. {
  401. struct process *p = current_proc();
  402. unsigned int flags;
  403. if (!p)
  404. return;
  405. flags = sched_lock_acquire();
  406. if (bkl.owner == p) {
  407. p->bkl_saved = bkl.depth;
  408. bkl.owner = 0;
  409. bkl.depth = 0;
  410. wake_one_locked(&bkl.wait);
  411. }
  412. /* Not the owner: leave bkl_saved alone. A nested sleep inside a
  413. * bkl_pause() section must not forget that it still has to resume the
  414. * paused ownership. */
  415. sched_lock_release(flags);
  416. }
  417. static void bkl_yield_reacquire(void)
  418. {
  419. struct process *p = current_proc();
  420. unsigned int depth;
  421. if (!p)
  422. return;
  423. depth = p->bkl_saved;
  424. if (!depth)
  425. return;
  426. p->bkl_saved = 0;
  427. bkl_lock();
  428. bkl.depth = depth;
  429. }
  430. void bkl_pause(void)
  431. {
  432. bkl_yield_release();
  433. }
  434. void bkl_resume(void)
  435. {
  436. bkl_yield_reacquire();
  437. }
  438. /* Run once on the new kernel stack: queue the process this CPU just left, so
  439. * its stack/page directory cannot be freed while switch.S still uses them. */
  440. void sched_finish_switch(void)
  441. {
  442. struct cpu *c = this_cpu();
  443. struct process *p;
  444. unsigned int flags;
  445. if (!c->deferred_unlock)
  446. return;
  447. p = c->pending_zombie;
  448. c->pending_zombie = 0;
  449. flags = c->sched_flags;
  450. c->deferred_unlock = 0;
  451. sched_lock_release(flags);
  452. if (p)
  453. process_zombie_add(p);
  454. }
  455. void sched_init(void)
  456. {
  457. unsigned int i;
  458. cpu_count = 1;
  459. runq_head = runq_tail = 0;
  460. sleep_head = sleep_tail = 0;
  461. wakeups = 0;
  462. sched_lock.locked = 0;
  463. bkl_init();
  464. for (i = 0; i < MAX_CPUS; i++) {
  465. cpus[i].current = 0;
  466. cpus[i].idle = 0;
  467. cpus[i].id = i;
  468. cpus[i].lapic_id = 0;
  469. cpus[i].need_resched = 0;
  470. cpus[i].preempt_count = 0;
  471. cpus[i].idle_ticks = 0;
  472. cpus[i].switches = 0;
  473. cpus[i].pending_zombie = 0;
  474. cpus[i].deferred_unlock = 0;
  475. cpus[i].temp_div = 0;
  476. cpus[i].temp_c = -1;
  477. cpus[i].tick_ms = 1;
  478. }
  479. }
  480. static void sched_switch_to(struct process *next)
  481. {
  482. struct cpu *c = this_cpu();
  483. unsigned int cr3 = paging_current_cr3();
  484. if (next->cr3 && next->cr3 != cr3) {
  485. __asm__ volatile ("movl %0, %%cr3"
  486. :
  487. : "r"(next->cr3)
  488. : "memory");
  489. }
  490. if (c->tss && next->kstack_top)
  491. c->tss->esp0 = next->kstack_top;
  492. }
  493. /* Pick the READY process with the smallest vruntime that may run on this
  494. * CPU. Ties go to the lower pid for determinism. */
  495. static struct process *pick_locked(void)
  496. {
  497. struct process *p, *best = 0;
  498. struct cpu *c = this_cpu();
  499. unsigned int mask = 1u << c->id;
  500. for (p = runq_head; p; p = p->run_next) {
  501. if (!(p->affinity & mask))
  502. continue;
  503. /* A process still marked on another CPU is a wake-up race
  504. * victim: it is about to reach its own CPU's schedule(). Let
  505. * that CPU deal with it instead of running it twice. */
  506. if (p->on_cpu != PROC_NO_CPU && p->on_cpu != c->id)
  507. continue;
  508. if (!best || p->vruntime < best->vruntime ||
  509. (p->vruntime == best->vruntime && p->pid < best->pid))
  510. best = p;
  511. }
  512. return best;
  513. }
  514. static void wake_sleepers_locked(void)
  515. {
  516. unsigned int now = get_ticks_ms();
  517. while (sleep_head && (int)(sleep_head->wake_ms - now) <= 0) {
  518. struct process *p = sleep_head;
  519. sleepq_del_locked(p);
  520. p->state = PROC_READY;
  521. p->wake_ms = 0;
  522. wake_locked(p);
  523. }
  524. }
  525. /*
  526. * CFS-lite schedule. Called from the PIT stub (after ESP has been saved) or
  527. * from the int 0x81 gate, always with interrupts disabled.
  528. */
  529. void schedule(void)
  530. {
  531. struct cpu *c = this_cpu();
  532. struct process *cur = c->current;
  533. struct process *next;
  534. unsigned int flags = sched_lock_acquire();
  535. if (cur && cur != c->idle) {
  536. if (cur->state == PROC_RUNNING) {
  537. account_locked(cur);
  538. cur->state = PROC_READY;
  539. runq_add_locked(cur);
  540. } else if (cur->state == PROC_READY) {
  541. /* Woken between setting BLOCKED/SLEEPING and entering
  542. * the gate: already on the run queue. */
  543. } else if (cur->state == PROC_DEAD) {
  544. /* Defer the zombie until switch.S has loaded the new
  545. * stack: its kernel stack and page directory are still
  546. * live until then. */
  547. c->pending_zombie = cur;
  548. }
  549. }
  550. next = pick_locked();
  551. if (!next) {
  552. next = c->idle;
  553. if (!next) {
  554. sched_lock_release(flags);
  555. for (;;)
  556. __asm__ volatile ("cli; hlt");
  557. }
  558. }
  559. if (next != cur) {
  560. if (cur)
  561. cur->on_cpu = PROC_NO_CPU;
  562. c->switches++;
  563. }
  564. runq_del_locked(next);
  565. next->state = PROC_RUNNING;
  566. next->on_cpu = c->id;
  567. next->last_cpu = c->id;
  568. next->slice_start = get_ticks_ms();
  569. c->current = next;
  570. c->need_resched = 0;
  571. /* The BSP PIT and each AP's LAPIC timer tick at 1 kHz while a process
  572. * runs; while a CPU is idle they drop to 100 Hz so MWAIT can hold C2
  573. * for 10 ms. The global millisecond clock stays accurate because the
  574. * PIT stub adds tick_ms (see timer_tick_advance). */
  575. if (c->id == 0) {
  576. timer_set_hz(next == c->idle ? 100 : 1000);
  577. } else {
  578. unsigned int ms = (next == c->idle) ? 10 : 1;
  579. c->tick_ms = ms;
  580. lapic_timer_set_ms(ms);
  581. }
  582. sched_switch_to(next);
  583. /* Keep the lock across the stack switch performed by switch.S; the
  584. * old process must not become runnable on another CPU while this CPU
  585. * is still using its kernel stack. */
  586. c->sched_flags = flags;
  587. c->deferred_unlock = 1;
  588. }
  589. void sched_yield(void)
  590. {
  591. struct cpu *c = this_cpu();
  592. /* Blocking helpers must not be used inside a preempt-disabled section.
  593. * If one is, record the request instead of switching with the count
  594. * still owned by the old process. */
  595. if (c->preempt_count) {
  596. c->need_resched = 1;
  597. return;
  598. }
  599. /* Release the BKL before switching away and take it back after being
  600. * resumed; a process that never resumes (exit) simply never reacquires
  601. * it. Blocking helpers therefore need no explicit BKL handling. */
  602. bkl_yield_release();
  603. __asm__ volatile ("int $0x81" ::: "memory");
  604. bkl_yield_reacquire();
  605. }
  606. void sched_sleep_ms(unsigned int ms)
  607. {
  608. struct process *p = current_proc();
  609. unsigned int flags;
  610. if (!p)
  611. return;
  612. if (ms == 0) {
  613. sched_yield();
  614. return;
  615. }
  616. if (this_cpu()->preempt_count)
  617. return; /* sleeping inside a non-preemptible section is invalid */
  618. flags = sched_lock_acquire();
  619. p->wake_ms = get_ticks_ms() + ms;
  620. p->state = PROC_SLEEPING;
  621. runq_del_locked(p);
  622. sleepq_add_locked(p);
  623. sched_lock_release(flags);
  624. sched_yield();
  625. }
  626. /* Called from the PIT after ticks has been incremented. Returns 1 when the
  627. * assembly should call schedule(). */
  628. /* Common tail of the tick path: with the scheduler lock held, decide whether
  629. * the assembly stub should call schedule(). */
  630. static int tick_decide_locked(struct cpu *c, struct process *p, int ap,
  631. unsigned int ms)
  632. {
  633. if (p) {
  634. p->cpu_ticks += ms;
  635. if (p == c->idle)
  636. c->idle_ticks += ms;
  637. }
  638. /* A process inside the kernel holding the big lock must not be
  639. * preempted: the lock is not preemptible, and its owner may be in the
  640. * middle of a kernel data structure update. Voluntary block points
  641. * release it first. */
  642. if (p && bkl.owner == p && p->state == PROC_RUNNING)
  643. return 0;
  644. if (c->preempt_count) {
  645. if (p && p != c->idle && p->state == PROC_RUNNING &&
  646. (unsigned int)(get_ticks_ms() - p->slice_start) >=
  647. SCHED_SLICE_TICKS)
  648. c->need_resched = 1;
  649. return 0;
  650. }
  651. /* APs only start taking work once the BSP has finished booting. */
  652. if (ap && !smp_sched_enabled)
  653. return 0;
  654. if (!p)
  655. return 0;
  656. if (p == c->idle) {
  657. if (pick_locked()) {
  658. c->need_resched = 1;
  659. return 1;
  660. }
  661. return 0;
  662. }
  663. if (p->state != PROC_RUNNING) {
  664. c->need_resched = 1;
  665. return 1;
  666. }
  667. if ((unsigned int)(get_ticks_ms() - p->slice_start) >= SCHED_SLICE_TICKS)
  668. c->need_resched = 1;
  669. return c->need_resched ? 1 : 0;
  670. }
  671. /* BSP PIT: also wakes the global sleep list (the PIT is the system clock). */
  672. int sched_tick_bsp(void)
  673. {
  674. struct cpu *c = this_cpu();
  675. struct process *p;
  676. unsigned int flags = sched_lock_acquire();
  677. int r;
  678. thermal_tick();
  679. p = c->current;
  680. wake_sleepers_locked();
  681. r = tick_decide_locked(c, p, 0, timer_tick_ms());
  682. sched_lock_release(flags);
  683. return r;
  684. }
  685. /* AP local timer: no global tick counter and no sleeper wake-up (the BSP PIT
  686. * owns both); only this CPU's scheduling decision. */
  687. int sched_tick_ap(void)
  688. {
  689. struct cpu *c = this_cpu();
  690. struct process *p;
  691. unsigned int flags = sched_lock_acquire();
  692. int r;
  693. thermal_tick();
  694. p = c->current;
  695. r = tick_decide_locked(c, p, 1, c->tick_ms);
  696. if (!r && p == c->idle && c->tick_ms != 10) {
  697. /* Entered idle without a schedule(): stretch this CPU's LAPIC
  698. * period so MWAIT can hold C2 for 10 ms. */
  699. c->tick_ms = 10;
  700. lapic_timer_set_ms(10);
  701. }
  702. sched_lock_release(flags);
  703. return r;
  704. }
  705. void preempt_disable(void)
  706. {
  707. this_cpu()->preempt_count++;
  708. }
  709. void preempt_enable(void)
  710. {
  711. struct cpu *c = this_cpu();
  712. if (c->preempt_count)
  713. c->preempt_count--;
  714. }
  715. int sched_snapshot(struct sys_schedinfo *out)
  716. {
  717. unsigned int flags, i;
  718. struct process *p;
  719. if (!out)
  720. return -1;
  721. flags = sched_lock_acquire();
  722. out->ncpus = cpu_count;
  723. out->runnable = 0;
  724. for (p = runq_head; p; p = p->run_next)
  725. out->runnable++;
  726. out->sleeping = 0;
  727. for (p = sleep_head; p; p = p->sleep_next)
  728. out->sleeping++;
  729. out->blocked = 0;
  730. for (i = 0; i < PROC_MAX; i++) {
  731. p = procs[i];
  732. if (p && p->state == PROC_BLOCKED)
  733. out->blocked++;
  734. }
  735. out->zombies = process_zombie_count_locked();
  736. out->switches = 0;
  737. out->idle_ticks = 0;
  738. for (i = 0; i < cpu_count && i < MAX_CPUS; i++) {
  739. out->switches += cpus[i].switches;
  740. out->idle_ticks += cpus[i].idle_ticks;
  741. }
  742. out->wakeups = wakeups;
  743. out->slice_ticks = SCHED_SLICE_TICKS;
  744. sched_lock_release(flags);
  745. return 0;
  746. }
  747. static void sched_put_dec(unsigned int n)
  748. {
  749. char buf[12];
  750. unsigned int i = sizeof(buf) - 1;
  751. buf[i] = '\0';
  752. if (n == 0) {
  753. io_putchar('0');
  754. return;
  755. }
  756. while (n && i > 0) {
  757. buf[--i] = (char)('0' + (n % 10));
  758. n /= 10;
  759. }
  760. io_putstr(buf + i);
  761. }
  762. void sched_print(void)
  763. {
  764. struct sys_schedinfo s;
  765. unsigned int uptime, idle_pm, up_s, total_s, idle_s, idle_frac, ncpu;
  766. if (sched_snapshot(&s) != 0)
  767. return;
  768. uptime = get_ticks_ms();
  769. ncpu = s.ncpus ? s.ncpus : 1;
  770. /* Idle is a share of the total CPU capacity, so 4 idle CPUs at 50%
  771. * each report 50%, not 200%. Avoid 64-bit division: scale to seconds
  772. * first (four CPUs overflow after ~12 days; good enough for the
  773. * display). */
  774. up_s = uptime / 1000u;
  775. total_s = up_s * ncpu;
  776. if (total_s == 0) {
  777. idle_pm = (s.idle_ticks >= uptime * ncpu) ? 1000 : 0;
  778. } else {
  779. idle_s = s.idle_ticks / 1000u;
  780. idle_frac = s.idle_ticks % 1000u;
  781. idle_pm = (idle_s * 1000u) / total_s;
  782. idle_pm += (idle_frac * 1000u / total_s) / 1000u;
  783. if (idle_pm > 1000u)
  784. idle_pm = 1000u;
  785. }
  786. io_putstr("sched: cfs, ");
  787. sched_put_dec(s.ncpus);
  788. io_putstr(" cpu, ");
  789. sched_put_dec(s.slice_ticks);
  790. io_putstr(" ms slice\nrunq ");
  791. sched_put_dec(s.runnable);
  792. io_putstr(" sleep ");
  793. sched_put_dec(s.sleeping);
  794. io_putstr(" block ");
  795. sched_put_dec(s.blocked);
  796. io_putstr(" zombie ");
  797. sched_put_dec(s.zombies);
  798. io_putstr("\nswitches ");
  799. sched_put_dec(s.switches);
  800. io_putstr(" wakeups ");
  801. sched_put_dec(s.wakeups);
  802. io_putstr(" idle ");
  803. sched_put_dec(idle_pm / 10);
  804. io_putchar('.');
  805. io_putchar((char)('0' + idle_pm % 10));
  806. io_putstr("%\n");
  807. }