sched.c 22 KB

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