Scheduler.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ScopeGuard.h>
  7. #include <AK/TemporaryChange.h>
  8. #include <AK/Time.h>
  9. #include <Kernel/Debug.h>
  10. #include <Kernel/Panic.h>
  11. #include <Kernel/PerformanceManager.h>
  12. #include <Kernel/Process.h>
  13. #include <Kernel/RTC.h>
  14. #include <Kernel/Scheduler.h>
  15. #include <Kernel/Time/TimeManagement.h>
  16. #include <Kernel/TimerQueue.h>
  17. // Remove this once SMP is stable and can be enabled by default
  18. #define SCHEDULE_ON_ALL_PROCESSORS 0
  19. namespace Kernel {
  20. class SchedulerPerProcessorData {
  21. AK_MAKE_NONCOPYABLE(SchedulerPerProcessorData);
  22. AK_MAKE_NONMOVABLE(SchedulerPerProcessorData);
  23. public:
  24. SchedulerPerProcessorData() = default;
  25. WeakPtr<Thread> m_pending_beneficiary;
  26. const char* m_pending_donate_reason { nullptr };
  27. bool m_in_scheduler { true };
  28. };
  29. RecursiveSpinLock g_scheduler_lock;
  30. static u32 time_slice_for(const Thread& thread)
  31. {
  32. // One time slice unit == 4ms (assuming 250 ticks/second)
  33. if (thread.is_idle_thread())
  34. return 1;
  35. return 2;
  36. }
  37. READONLY_AFTER_INIT Thread* g_finalizer;
  38. READONLY_AFTER_INIT WaitQueue* g_finalizer_wait_queue;
  39. Atomic<bool> g_finalizer_has_work { false };
  40. READONLY_AFTER_INIT static Process* s_colonel_process;
  41. struct ThreadReadyQueue {
  42. IntrusiveList<Thread, RawPtr<Thread>, &Thread::m_ready_queue_node> thread_list;
  43. };
  44. static SpinLock<u8> g_ready_queues_lock;
  45. static u32 g_ready_queues_mask;
  46. static constexpr u32 g_ready_queue_buckets = sizeof(g_ready_queues_mask) * 8;
  47. READONLY_AFTER_INIT static ThreadReadyQueue* g_ready_queues; // g_ready_queue_buckets entries
  48. static void dump_thread_list();
  49. static inline u32 thread_priority_to_priority_index(u32 thread_priority)
  50. {
  51. // Converts the priority in the range of THREAD_PRIORITY_MIN...THREAD_PRIORITY_MAX
  52. // to a index into g_ready_queues where 0 is the highest priority bucket
  53. VERIFY(thread_priority >= THREAD_PRIORITY_MIN && thread_priority <= THREAD_PRIORITY_MAX);
  54. constexpr u32 thread_priority_count = THREAD_PRIORITY_MAX - THREAD_PRIORITY_MIN + 1;
  55. static_assert(thread_priority_count > 0);
  56. auto priority_bucket = ((thread_priority_count - (thread_priority - THREAD_PRIORITY_MIN)) / thread_priority_count) * (g_ready_queue_buckets - 1);
  57. VERIFY(priority_bucket < g_ready_queue_buckets);
  58. return priority_bucket;
  59. }
  60. Thread& Scheduler::pull_next_runnable_thread()
  61. {
  62. auto affinity_mask = 1u << Processor::current().id();
  63. ScopedSpinLock lock(g_ready_queues_lock);
  64. auto priority_mask = g_ready_queues_mask;
  65. while (priority_mask != 0) {
  66. auto priority = __builtin_ffsl(priority_mask);
  67. VERIFY(priority > 0);
  68. auto& ready_queue = g_ready_queues[--priority];
  69. for (auto& thread : ready_queue.thread_list) {
  70. VERIFY(thread.m_runnable_priority == (int)priority);
  71. if (thread.is_active())
  72. continue;
  73. if (!(thread.affinity() & affinity_mask))
  74. continue;
  75. thread.m_runnable_priority = -1;
  76. ready_queue.thread_list.remove(thread);
  77. if (ready_queue.thread_list.is_empty())
  78. g_ready_queues_mask &= ~(1u << priority);
  79. // Mark it as active because we are using this thread. This is similar
  80. // to comparing it with Processor::current_thread, but when there are
  81. // multiple processors there's no easy way to check whether the thread
  82. // is actually still needed. This prevents accidental finalization when
  83. // a thread is no longer in Running state, but running on another core.
  84. // We need to mark it active here so that this thread won't be
  85. // scheduled on another core if it were to be queued before actually
  86. // switching to it.
  87. // FIXME: Figure out a better way maybe?
  88. thread.set_active(true);
  89. return thread;
  90. }
  91. priority_mask &= ~(1u << priority);
  92. }
  93. return *Processor::idle_thread();
  94. }
  95. bool Scheduler::dequeue_runnable_thread(Thread& thread, bool check_affinity)
  96. {
  97. if (thread.is_idle_thread())
  98. return true;
  99. ScopedSpinLock lock(g_ready_queues_lock);
  100. auto priority = thread.m_runnable_priority;
  101. if (priority < 0) {
  102. VERIFY(!thread.m_ready_queue_node.is_in_list());
  103. return false;
  104. }
  105. if (check_affinity && !(thread.affinity() & (1 << Processor::current().id())))
  106. return false;
  107. VERIFY(g_ready_queues_mask & (1u << priority));
  108. auto& ready_queue = g_ready_queues[priority];
  109. thread.m_runnable_priority = -1;
  110. ready_queue.thread_list.remove(thread);
  111. if (ready_queue.thread_list.is_empty())
  112. g_ready_queues_mask &= ~(1u << priority);
  113. return true;
  114. }
  115. void Scheduler::queue_runnable_thread(Thread& thread)
  116. {
  117. VERIFY(g_scheduler_lock.own_lock());
  118. if (thread.is_idle_thread())
  119. return;
  120. auto priority = thread_priority_to_priority_index(thread.priority());
  121. ScopedSpinLock lock(g_ready_queues_lock);
  122. VERIFY(thread.m_runnable_priority < 0);
  123. thread.m_runnable_priority = (int)priority;
  124. VERIFY(!thread.m_ready_queue_node.is_in_list());
  125. auto& ready_queue = g_ready_queues[priority];
  126. bool was_empty = ready_queue.thread_list.is_empty();
  127. ready_queue.thread_list.append(thread);
  128. if (was_empty)
  129. g_ready_queues_mask |= (1u << priority);
  130. }
  131. UNMAP_AFTER_INIT void Scheduler::start()
  132. {
  133. VERIFY_INTERRUPTS_DISABLED();
  134. // We need to acquire our scheduler lock, which will be released
  135. // by the idle thread once control transferred there
  136. g_scheduler_lock.lock();
  137. auto& processor = Processor::current();
  138. processor.set_scheduler_data(*new SchedulerPerProcessorData());
  139. VERIFY(processor.is_initialized());
  140. auto& idle_thread = *Processor::idle_thread();
  141. VERIFY(processor.current_thread() == &idle_thread);
  142. idle_thread.set_ticks_left(time_slice_for(idle_thread));
  143. idle_thread.did_schedule();
  144. idle_thread.set_initialized(true);
  145. processor.init_context(idle_thread, false);
  146. idle_thread.set_state(Thread::Running);
  147. VERIFY(idle_thread.affinity() == (1u << processor.get_id()));
  148. processor.initialize_context_switching(idle_thread);
  149. VERIFY_NOT_REACHED();
  150. }
  151. bool Scheduler::pick_next()
  152. {
  153. VERIFY_INTERRUPTS_DISABLED();
  154. auto current_thread = Thread::current();
  155. // Set the m_in_scheduler flag before acquiring the spinlock. This
  156. // prevents a recursive call into Scheduler::invoke_async upon
  157. // leaving the scheduler lock.
  158. ScopedCritical critical;
  159. auto& scheduler_data = Processor::current().get_scheduler_data();
  160. scheduler_data.m_in_scheduler = true;
  161. ScopeGuard guard(
  162. []() {
  163. // We may be on a different processor after we got switched
  164. // back to this thread!
  165. auto& scheduler_data = Processor::current().get_scheduler_data();
  166. VERIFY(scheduler_data.m_in_scheduler);
  167. scheduler_data.m_in_scheduler = false;
  168. });
  169. ScopedSpinLock lock(g_scheduler_lock);
  170. if (current_thread->should_die() && current_thread->state() == Thread::Running) {
  171. // Rather than immediately killing threads, yanking the kernel stack
  172. // away from them (which can lead to e.g. reference leaks), we always
  173. // allow Thread::wait_on to return. This allows the kernel stack to
  174. // clean up and eventually we'll get here shortly before transitioning
  175. // back to user mode (from Processor::exit_trap). At this point we
  176. // no longer want to schedule this thread. We can't wait until
  177. // Scheduler::enter_current because we don't want to allow it to
  178. // transition back to user mode.
  179. if constexpr (SCHEDULER_DEBUG)
  180. dbgln("Scheduler[{}]: Thread {} is dying", Processor::id(), *current_thread);
  181. current_thread->set_state(Thread::Dying);
  182. }
  183. if constexpr (SCHEDULER_RUNNABLE_DEBUG) {
  184. dump_thread_list();
  185. }
  186. auto pending_beneficiary = scheduler_data.m_pending_beneficiary.strong_ref();
  187. if (pending_beneficiary && dequeue_runnable_thread(*pending_beneficiary, true)) {
  188. // The thread we're supposed to donate to still exists and we can
  189. const char* reason = scheduler_data.m_pending_donate_reason;
  190. scheduler_data.m_pending_beneficiary = nullptr;
  191. scheduler_data.m_pending_donate_reason = nullptr;
  192. // We need to leave our first critical section before switching context,
  193. // but since we're still holding the scheduler lock we're still in a critical section
  194. critical.leave();
  195. dbgln_if(SCHEDULER_DEBUG, "Processing pending donate to {} reason={}", *pending_beneficiary, reason);
  196. return donate_to_and_switch(pending_beneficiary.ptr(), reason);
  197. }
  198. // Either we're not donating or the beneficiary disappeared.
  199. // Either way clear any pending information
  200. scheduler_data.m_pending_beneficiary = nullptr;
  201. scheduler_data.m_pending_donate_reason = nullptr;
  202. auto& thread_to_schedule = pull_next_runnable_thread();
  203. if constexpr (SCHEDULER_DEBUG) {
  204. dbgln("Scheduler[{}]: Switch to {} @ {:04x}:{:08x}",
  205. Processor::id(),
  206. thread_to_schedule,
  207. thread_to_schedule.tss().cs, thread_to_schedule.tss().eip);
  208. }
  209. // We need to leave our first critical section before switching context,
  210. // but since we're still holding the scheduler lock we're still in a critical section
  211. critical.leave();
  212. thread_to_schedule.set_ticks_left(time_slice_for(thread_to_schedule));
  213. return context_switch(&thread_to_schedule);
  214. }
  215. bool Scheduler::yield()
  216. {
  217. InterruptDisabler disabler;
  218. auto& proc = Processor::current();
  219. auto& scheduler_data = proc.get_scheduler_data();
  220. // Clear any pending beneficiary
  221. scheduler_data.m_pending_beneficiary = nullptr;
  222. scheduler_data.m_pending_donate_reason = nullptr;
  223. auto current_thread = Thread::current();
  224. dbgln_if(SCHEDULER_DEBUG, "Scheduler[{}]: yielding thread {} in_irq={}", proc.get_id(), *current_thread, proc.in_irq());
  225. VERIFY(current_thread != nullptr);
  226. if (proc.in_irq() || proc.in_critical()) {
  227. // If we're handling an IRQ we can't switch context, or we're in
  228. // a critical section where we don't want to switch contexts, then
  229. // delay until exiting the trap or critical section
  230. proc.invoke_scheduler_async();
  231. return false;
  232. }
  233. if (!Scheduler::pick_next())
  234. return false;
  235. if constexpr (SCHEDULER_DEBUG)
  236. dbgln("Scheduler[{}]: yield returns to thread {} in_irq={}", Processor::id(), *current_thread, Processor::current().in_irq());
  237. return true;
  238. }
  239. bool Scheduler::donate_to_and_switch(Thread* beneficiary, [[maybe_unused]] const char* reason)
  240. {
  241. VERIFY(g_scheduler_lock.own_lock());
  242. auto& proc = Processor::current();
  243. VERIFY(proc.in_critical() == 1);
  244. unsigned ticks_left = Thread::current()->ticks_left();
  245. if (!beneficiary || beneficiary->state() != Thread::Runnable || ticks_left <= 1)
  246. return Scheduler::yield();
  247. unsigned ticks_to_donate = min(ticks_left - 1, time_slice_for(*beneficiary));
  248. dbgln_if(SCHEDULER_DEBUG, "Scheduler[{}]: Donating {} ticks to {}, reason={}", proc.get_id(), ticks_to_donate, *beneficiary, reason);
  249. beneficiary->set_ticks_left(ticks_to_donate);
  250. return Scheduler::context_switch(beneficiary);
  251. }
  252. bool Scheduler::donate_to(RefPtr<Thread>& beneficiary, const char* reason)
  253. {
  254. VERIFY(beneficiary);
  255. if (beneficiary == Thread::current())
  256. return Scheduler::yield();
  257. // Set the m_in_scheduler flag before acquiring the spinlock. This
  258. // prevents a recursive call into Scheduler::invoke_async upon
  259. // leaving the scheduler lock.
  260. ScopedCritical critical;
  261. auto& proc = Processor::current();
  262. auto& scheduler_data = proc.get_scheduler_data();
  263. scheduler_data.m_in_scheduler = true;
  264. ScopeGuard guard(
  265. []() {
  266. // We may be on a different processor after we got switched
  267. // back to this thread!
  268. auto& scheduler_data = Processor::current().get_scheduler_data();
  269. VERIFY(scheduler_data.m_in_scheduler);
  270. scheduler_data.m_in_scheduler = false;
  271. });
  272. VERIFY(!proc.in_irq());
  273. if (proc.in_critical() > 1) {
  274. scheduler_data.m_pending_beneficiary = beneficiary; // Save the beneficiary
  275. scheduler_data.m_pending_donate_reason = reason;
  276. proc.invoke_scheduler_async();
  277. return false;
  278. }
  279. ScopedSpinLock lock(g_scheduler_lock);
  280. // "Leave" the critical section before switching context. Since we
  281. // still hold the scheduler lock, we're not actually leaving it.
  282. // Processor::switch_context expects Processor::in_critical() to be 1
  283. critical.leave();
  284. donate_to_and_switch(beneficiary, reason);
  285. return false;
  286. }
  287. bool Scheduler::context_switch(Thread* thread)
  288. {
  289. if (s_mm_lock.own_lock()) {
  290. PANIC("In context switch while holding s_mm_lock");
  291. }
  292. thread->did_schedule();
  293. auto from_thread = Thread::current();
  294. if (from_thread == thread)
  295. return false;
  296. if (from_thread) {
  297. // If the last process hasn't blocked (still marked as running),
  298. // mark it as runnable for the next round.
  299. if (from_thread->state() == Thread::Running)
  300. from_thread->set_state(Thread::Runnable);
  301. #ifdef LOG_EVERY_CONTEXT_SWITCH
  302. dbgln("Scheduler[{}]: {} -> {} [prio={}] {:04x}:{:08x}", Processor::id(), from_thread->tid().value(), thread->tid().value(), thread->priority(), thread->tss().cs, thread->tss().eip);
  303. #endif
  304. }
  305. auto& proc = Processor::current();
  306. if (!thread->is_initialized()) {
  307. proc.init_context(*thread, false);
  308. thread->set_initialized(true);
  309. }
  310. thread->set_state(Thread::Running);
  311. PerformanceManager::add_context_switch_perf_event(*from_thread, *thread);
  312. proc.switch_context(from_thread, thread);
  313. // NOTE: from_thread at this point reflects the thread we were
  314. // switched from, and thread reflects Thread::current()
  315. enter_current(*from_thread, false);
  316. VERIFY(thread == Thread::current());
  317. #if ARCH(I386)
  318. if (thread->process().is_user_process()) {
  319. auto iopl = get_iopl_from_eflags(Thread::current()->get_register_dump_from_stack().eflags);
  320. if (iopl != 0) {
  321. PANIC("Switched to thread {} with non-zero IOPL={}", Thread::current()->tid().value(), iopl);
  322. }
  323. }
  324. #endif
  325. return true;
  326. }
  327. void Scheduler::enter_current(Thread& prev_thread, bool is_first)
  328. {
  329. VERIFY(g_scheduler_lock.own_lock());
  330. prev_thread.set_active(false);
  331. if (prev_thread.state() == Thread::Dying) {
  332. // If the thread we switched from is marked as dying, then notify
  333. // the finalizer. Note that as soon as we leave the scheduler lock
  334. // the finalizer may free from_thread!
  335. notify_finalizer();
  336. } else if (!is_first) {
  337. // Check if we have any signals we should deliver (even if we don't
  338. // end up switching to another thread).
  339. auto current_thread = Thread::current();
  340. if (!current_thread->is_in_block() && current_thread->previous_mode() != Thread::PreviousMode::KernelMode) {
  341. ScopedSpinLock lock(current_thread->get_lock());
  342. if (current_thread->state() == Thread::Running && current_thread->pending_signals_for_state()) {
  343. current_thread->dispatch_one_pending_signal();
  344. }
  345. }
  346. }
  347. }
  348. void Scheduler::leave_on_first_switch(u32 flags)
  349. {
  350. // This is called when a thread is switched into for the first time.
  351. // At this point, enter_current has already be called, but because
  352. // Scheduler::context_switch is not in the call stack we need to
  353. // clean up and release locks manually here
  354. g_scheduler_lock.unlock(flags);
  355. auto& scheduler_data = Processor::current().get_scheduler_data();
  356. VERIFY(scheduler_data.m_in_scheduler);
  357. scheduler_data.m_in_scheduler = false;
  358. }
  359. void Scheduler::prepare_after_exec()
  360. {
  361. // This is called after exec() when doing a context "switch" into
  362. // the new process. This is called from Processor::assume_context
  363. VERIFY(g_scheduler_lock.own_lock());
  364. auto& scheduler_data = Processor::current().get_scheduler_data();
  365. VERIFY(!scheduler_data.m_in_scheduler);
  366. scheduler_data.m_in_scheduler = true;
  367. }
  368. void Scheduler::prepare_for_idle_loop()
  369. {
  370. // This is called when the CPU finished setting up the idle loop
  371. // and is about to run it. We need to acquire he scheduler lock
  372. VERIFY(!g_scheduler_lock.own_lock());
  373. g_scheduler_lock.lock();
  374. auto& scheduler_data = Processor::current().get_scheduler_data();
  375. VERIFY(!scheduler_data.m_in_scheduler);
  376. scheduler_data.m_in_scheduler = true;
  377. }
  378. Process* Scheduler::colonel()
  379. {
  380. VERIFY(s_colonel_process);
  381. return s_colonel_process;
  382. }
  383. UNMAP_AFTER_INIT void Scheduler::initialize()
  384. {
  385. VERIFY(&Processor::current() != nullptr); // sanity check
  386. RefPtr<Thread> idle_thread;
  387. g_finalizer_wait_queue = new WaitQueue;
  388. g_ready_queues = new ThreadReadyQueue[g_ready_queue_buckets];
  389. g_finalizer_has_work.store(false, AK::MemoryOrder::memory_order_release);
  390. s_colonel_process = Process::create_kernel_process(idle_thread, "colonel", idle_loop, nullptr, 1).leak_ref();
  391. VERIFY(s_colonel_process);
  392. VERIFY(idle_thread);
  393. idle_thread->set_priority(THREAD_PRIORITY_MIN);
  394. idle_thread->set_name(StringView("idle thread #0"));
  395. set_idle_thread(idle_thread);
  396. }
  397. UNMAP_AFTER_INIT void Scheduler::set_idle_thread(Thread* idle_thread)
  398. {
  399. idle_thread->set_idle_thread();
  400. Processor::current().set_idle_thread(*idle_thread);
  401. Processor::current().set_current_thread(*idle_thread);
  402. }
  403. UNMAP_AFTER_INIT Thread* Scheduler::create_ap_idle_thread(u32 cpu)
  404. {
  405. VERIFY(cpu != 0);
  406. // This function is called on the bsp, but creates an idle thread for another AP
  407. VERIFY(Processor::is_bootstrap_processor());
  408. VERIFY(s_colonel_process);
  409. Thread* idle_thread = s_colonel_process->create_kernel_thread(idle_loop, nullptr, THREAD_PRIORITY_MIN, String::formatted("idle thread #{}", cpu), 1 << cpu, false);
  410. VERIFY(idle_thread);
  411. return idle_thread;
  412. }
  413. void Scheduler::timer_tick(const RegisterState& regs)
  414. {
  415. VERIFY_INTERRUPTS_DISABLED();
  416. VERIFY(Processor::current().in_irq());
  417. auto current_thread = Processor::current_thread();
  418. if (!current_thread)
  419. return;
  420. // Sanity checks
  421. VERIFY(current_thread->current_trap());
  422. VERIFY(current_thread->current_trap()->regs == &regs);
  423. #if !SCHEDULE_ON_ALL_PROCESSORS
  424. if (!Processor::is_bootstrap_processor())
  425. return; // TODO: This prevents scheduling on other CPUs!
  426. #endif
  427. if (current_thread->tick())
  428. return;
  429. VERIFY_INTERRUPTS_DISABLED();
  430. VERIFY(Processor::current().in_irq());
  431. Processor::current().invoke_scheduler_async();
  432. }
  433. void Scheduler::invoke_async()
  434. {
  435. VERIFY_INTERRUPTS_DISABLED();
  436. auto& proc = Processor::current();
  437. VERIFY(!proc.in_irq());
  438. // Since this function is called when leaving critical sections (such
  439. // as a SpinLock), we need to check if we're not already doing this
  440. // to prevent recursion
  441. if (!proc.get_scheduler_data().m_in_scheduler)
  442. pick_next();
  443. }
  444. void Scheduler::yield_from_critical()
  445. {
  446. auto& proc = Processor::current();
  447. VERIFY(proc.in_critical());
  448. VERIFY(!proc.in_irq());
  449. yield(); // Flag a context switch
  450. u32 prev_flags;
  451. u32 prev_crit = Processor::current().clear_critical(prev_flags, false);
  452. // Note, we may now be on a different CPU!
  453. Processor::current().restore_critical(prev_crit, prev_flags);
  454. }
  455. void Scheduler::notify_finalizer()
  456. {
  457. if (g_finalizer_has_work.exchange(true, AK::MemoryOrder::memory_order_acq_rel) == false)
  458. g_finalizer_wait_queue->wake_all();
  459. }
  460. void Scheduler::idle_loop(void*)
  461. {
  462. auto& proc = Processor::current();
  463. dbgln("Scheduler[{}]: idle loop running", proc.get_id());
  464. VERIFY(are_interrupts_enabled());
  465. for (;;) {
  466. proc.idle_begin();
  467. asm("hlt");
  468. proc.idle_end();
  469. VERIFY_INTERRUPTS_ENABLED();
  470. #if SCHEDULE_ON_ALL_PROCESSORS
  471. yield();
  472. #else
  473. if (Processor::current().id() == 0)
  474. yield();
  475. #endif
  476. }
  477. }
  478. void Scheduler::dump_scheduler_state()
  479. {
  480. dump_thread_list();
  481. }
  482. void dump_thread_list()
  483. {
  484. dbgln("Scheduler thread list for processor {}:", Processor::id());
  485. auto get_cs = [](Thread& thread) -> u16 {
  486. if (!thread.current_trap())
  487. return thread.tss().cs;
  488. return thread.get_register_dump_from_stack().cs;
  489. };
  490. auto get_eip = [](Thread& thread) -> u32 {
  491. if (!thread.current_trap())
  492. return thread.tss().eip;
  493. return thread.get_register_dump_from_stack().eip;
  494. };
  495. Thread::for_each([&](Thread& thread) {
  496. switch (thread.state()) {
  497. case Thread::Dying:
  498. dmesgln(" {:14} {:30} @ {:04x}:{:08x} Finalizable: {}, (nsched: {})",
  499. thread.state_string(),
  500. thread,
  501. get_cs(thread),
  502. get_eip(thread),
  503. thread.is_finalizable(),
  504. thread.times_scheduled());
  505. break;
  506. default:
  507. dmesgln(" {:14} Pr:{:2} {:30} @ {:04x}:{:08x} (nsched: {})",
  508. thread.state_string(),
  509. thread.priority(),
  510. thread,
  511. get_cs(thread),
  512. get_eip(thread),
  513. thread.times_scheduled());
  514. break;
  515. }
  516. });
  517. }
  518. }