|
The Pedigree Project 0.1
|
This class manages how processes and threads are scheduled across processors. More...
#include <Scheduler.h>
Collaboration diagram for Scheduler:Classes | |
| class | ActivitySample |
| class | ProcessLease |
| struct | SystemActivity |
Static Public Member Functions | |
| static Scheduler & | instance () |
| static CpuAffinityMask | onlineAffinity () |
| static size_t | affinityBytes () |
| static PerProcessorScheduler * | schedulerForCpu (size_t cpu) |
Private Member Functions | |
| bool | hasActiveSyscallLocked (Service_t service) const |
| NOT_COPYABLE_OR_ASSIGNABLE (Scheduler) | |
| size_t | reserveProcessId () |
| void | releaseProcessLease (Process *process) |
| void | drainDeferredTimeAccounting () |
Static Private Member Functions | |
| static void | releaseActivityEntry (Process *process, Thread *thread) |
Private Attributes | |
| Mutex | m_ActivityLock |
| uint64_t | m_NextActivityAttempt = 0 |
| bool | m_ActivitySamplePending = false |
| LoadAverage | m_LoadAverage |
| Atomic< uint64_t > | m_UserNanoseconds |
| Atomic< uint64_t > | m_KernelNanoseconds |
| Atomic< uint64_t > | m_IdleNanoseconds |
| List< Process *, 0 > | m_Processes |
| Atomic< size_t > | m_NextPid |
| Tree< PerProcessorScheduler *, List< Thread * > * > | m_PTMap |
| Tree< Thread *, PerProcessorScheduler * > | m_TPMap |
| Process * | m_pKernelProcess |
| PerProcessorScheduler * | m_pBspScheduler |
| Spinlock | m_SchedulerLock |
| WaitQueue | m_ProcessRemovalWaiters |
Static Private Attributes | |
| static Scheduler | m_Instance |
Friends | |
| class | Process |
| class | PerProcessorScheduler |
| class | SyscallManager |
This class manages how processes and threads are scheduled across processors.
This is the "long term" scheduler - it load balances between processors and provides the interface for adding, listing and removing threads.
The load balancing is "lazy" in that the algorithm only runs on thread addition and removal.
Definition at line 50 of file Scheduler.h.
|
private |
Definition at line 98 of file Scheduler.cc.
| bool Scheduler::acquireFirstProcessOfType | ( | ProcessLease & | lease, |
| Process::ProcessType | type | ||
| ) |
Pins the first enumerated process with the requested concrete type.
Definition at line 301 of file Scheduler.cc.
References Spinlock::acquire(), List< T, nodePoolSize >::begin(), Process::beginExternalLease(), List< T, nodePoolSize >::end(), m_Processes, m_SchedulerLock, and Spinlock::release().
| bool Scheduler::acquireProcess | ( | ProcessLease & | lease, |
| Process * | expected | ||
| ) |
Pins an expected pointer only if it is still in enumeration.
Comparing the pointer under the scheduler lock does not dereference it, so callers can safely validate a parent/candidate pointer which may have been concurrently removed. Any previous lease is released; failure leaves lease empty.
Definition at line 317 of file Scheduler.cc.
References Spinlock::acquire(), List< T, nodePoolSize >::begin(), Process::beginExternalLease(), List< T, nodePoolSize >::end(), m_Processes, m_SchedulerLock, and Spinlock::release().
| bool Scheduler::acquireProcess | ( | ProcessLease & | lease, |
| size_t | n | ||
| ) |
Pins the n'th process currently in operation into lease. Any previous lease is released; failure leaves lease empty.
Definition at line 275 of file Scheduler.cc.
References Spinlock::acquire(), List< T, nodePoolSize >::begin(), Process::beginExternalLease(), List< T, nodePoolSize >::count(), List< T, nodePoolSize >::end(), m_Processes, m_SchedulerLock, and Spinlock::release().
Referenced by Process::beginTermination(), MemoryPressureProcessKiller::compact(), ThreadsCommand::execute(), X64VirtualAddressSpace::mapUnlocked(), Rtc::processElapsedTime(), and Process::publish().
Here is the caller graph for this function:| bool Scheduler::acquireProcessById | ( | ProcessLease & | lease, |
| size_t | id | ||
| ) |
Pins the process with the given scheduler-assigned ID into lease. Any previous lease is released; failure leaves lease empty.
Definition at line 340 of file Scheduler.cc.
References Spinlock::acquire(), List< T, nodePoolSize >::begin(), Process::beginExternalLease(), List< T, nodePoolSize >::end(), Process::getId(), m_Processes, m_SchedulerLock, and Spinlock::release().
| bool Scheduler::acquireProcessByUserspaceId | ( | ProcessLease & | lease, |
| size_t | id | ||
| ) |
Definition at line 359 of file Scheduler.cc.
| bool Scheduler::acquireThreadByTaskId | ( | Process::ThreadLease & | lease, |
| size_t | id | ||
| ) |
Pins a Linux task without nesting scheduler and process enumeration locks.
Definition at line 378 of file Scheduler.cc.
References Spinlock::acquire(), List< T, nodePoolSize >::begin(), Process::beginExternalLease(), List< T, nodePoolSize >::end(), Process::getId(), m_Processes, m_SchedulerLock, and Spinlock::release().
| void Scheduler::addProcess | ( | Process * | pProcess | ) |
Adds a completely constructed process to enumeration.
Definition at line 213 of file Scheduler.cc.
References Spinlock::acquire(), m_Processes, m_SchedulerLock, List< T, nodePoolSize >::pushBack(), and Spinlock::release().
Referenced by Process::publish().
Here is the caller graph for this function:| void Scheduler::addThread | ( | Thread * | pThread, |
| PerProcessorScheduler & | PPSched | ||
| ) |
Adds a thread to be load-balanced and accounted.
| pThread | The new thread. |
| PPSched | The per-processor scheduler the thread will start on. |
Definition at line 140 of file Scheduler.cc.
References Spinlock::acquire(), ProcessorBase::index(), Tree< K, E >::insert(), m_SchedulerLock, m_TPMap, Spinlock::release(), and Thread::setScheduler().
Referenced by ProcessorThreadAllocator::addThread(), ProcessorThreadAllocator::addThread(), Thread::Thread(), and Thread::Thread().
Here is the caller graph for this function:
|
static |
Definition at line 250 of file Scheduler.cc.
|
private |
Drains every currently enumerated Process's timer-accounting batch. The caller is an ordinary per-processor accounting worker.
Definition at line 190 of file Scheduler.cc.
References Spinlock::acquire(), List< T, nodePoolSize >::begin(), Vector< T >::begin(), Process::beginExternalLease(), Vector< T >::count(), Process::drainDeferredTimeAccounting(), List< T, nodePoolSize >::end(), Vector< T >::end(), Process::endExternalLease(), getNumProcesses(), m_Processes, m_SchedulerLock, Vector< T >::pushBack(), Spinlock::release(), and Vector< T >::size().
|
inline |
Definition at line 199 of file Scheduler.h.
Returns the n'th direct child of a process.
The caller must hold the parent's child-state guard so the returned Process pointer remains valid.
Definition at line 435 of file Scheduler.cc.
References Spinlock::acquire(), List< T, nodePoolSize >::begin(), List< T, nodePoolSize >::end(), Process::getParent(), m_Processes, m_SchedulerLock, and Spinlock::release().
Referenced by Process::beginTermination().
Here is the caller graph for this function:
|
inline |
Definition at line 195 of file Scheduler.h.
| size_t Scheduler::getNumProcesses | ( | ) |
Returns the number of processes currently in operation.
Definition at line 268 of file Scheduler.cc.
References Spinlock::acquire(), List< T, nodePoolSize >::count(), m_Processes, m_SchedulerLock, and Spinlock::release().
Referenced by MemoryPressureProcessKiller::compact(), drainDeferredTimeAccounting(), ThreadsCommand::execute(), X64VirtualAddressSpace::mapUnlocked(), and Rtc::processElapsedTime().
Here is the caller graph for this function:
|
private |
Caller holds m_SchedulerLock; never follows stack-owned callback records.
Definition at line 169 of file Scheduler.cc.
References Tree< K, E >::begin(), Tree< K, E >::end(), and m_TPMap.
| bool Scheduler::initialise | ( | Process * | pKernelProcess | ) |
Initialises the scheduler.
Definition at line 113 of file Scheduler.cc.
References Vector< T >::end(), ProcessorBase::getCount(), ProcessorBase::information(), m_pBspScheduler, m_pKernelProcess, ProcessorBase::m_ProcessorInformation, List< T, nodePoolSize >::pushBack(), and ProcessorThreadAllocator::setAlgorithm().
|
inlinestatic |
Get the instance of the scheduler
Definition at line 96 of file Scheduler.h.
Referenced by RequestQueue::addRequest(), Process::addThread(), ProcessorThreadAllocator::addThread(), ProcessorThreadAllocator::addThread(), Process::beginTermination(), MemoryPressureProcessKiller::compact(), File::dataChanged(), IrqHandlerRegistry::dispatchThreaded(), Rtc::drainRemoteAlarmDispatch(), ThreadsCommand::execute(), KernelElf::executeModules(), TextIO::initialise(), Rtc::initialise3(), Ps2Controller::irq(), PosixSubsystem::kill(), InputManager::mainThread(), X64VirtualAddressSpace::mapUnlocked(), Process::prepareForDestruction(), Rtc::processElapsedTime(), Process::publish(), Process::publishTerminationStatus(), Thread::publishTimeAccounting(), UsbInterruptInHandle::reset(), Process::resume(), PosixSubsystem::sendSignal(), Thread::setUnwindState(), IntervalTimer::signal(), Thread::start(), RequestQueue::startWorker(), UsbHub::suppressConnectionChanges(), Process::suspendInternal(), KernelCoreSyscallManager::syscall(), Thread::Thread(), Thread::Thread(), PerProcessorScheduler::timer(), KernelElf::unloadModules(), IrqHandlerRegistry::unregisterHandler(), KernelElf::waitForModulesToLoad(), Process::waitUntilTerminationReapableForTerminalCoordinator(), RequestQueue::work(), and Thread::~Thread().
Here is the caller graph for this function:
|
static |
Definition at line 241 of file Scheduler.cc.
| void Scheduler::rebindThread | ( | Thread * | thread, |
| PerProcessorScheduler & | scheduler | ||
| ) |
Definition at line 260 of file Scheduler.cc.
| void Scheduler::recordCpuTime | ( | const Thread & | thread, |
| CpuTimeMode | mode, | ||
| Time::Timestamp | elapsed | ||
| ) |
Records one interval after Thread has attributed it to its owner.
Definition at line 52 of file Scheduler-system-info.cc.
References Thread::getScheduler().
Referenced by Thread::publishTimeAccounting().
Here is the caller graph for this function:Definition at line 26 of file Scheduler-system-info.cc.
|
private |
Releases a lease through Scheduler's Process friendship.
Definition at line 186 of file Scheduler.cc.
References Process::endExternalLease().
| void Scheduler::removeProcess | ( | Process * | pProcess | ) |
Removes a process.
Definition at line 219 of file Scheduler.cc.
References Spinlock::acquire(), List< T, nodePoolSize >::begin(), Process::closeExternalLeaseAdmission(), List< T, nodePoolSize >::end(), List< T, nodePoolSize >::erase(), m_Processes, m_ProcessRemovalWaiters, m_SchedulerLock, and Spinlock::release().
Referenced by Process::prepareForDestruction().
Here is the caller graph for this function:| void Scheduler::removeThread | ( | Thread * | pThread | ) |
Removes a thread from being load-balanced and accounted.
Definition at line 152 of file Scheduler.cc.
References Spinlock::acquire(), Tree< K, E >::lookup(), m_SchedulerLock, m_TPMap, Spinlock::release(), and Tree< K, E >::remove().
Referenced by Thread::~Thread().
Here is the caller graph for this function:| void Scheduler::requestLoadAverageSample | ( | ) |
Coalesces the next due load sample from a scheduler tick.
Definition at line 63 of file Scheduler-system-info.cc.
References ProcessorBase::information().
Referenced by PerProcessorScheduler::timer().
Here is the caller graph for this function:
|
private |
Reserves an ID without making a partially built Process visible.
Definition at line 178 of file Scheduler.cc.
References m_NextPid.
Referenced by Process::addThread().
Here is the caller graph for this function:| void Scheduler::sampleLoadAverage | ( | ) |
Called by an ordinary accounting worker after its batch is complete.
Definition at line 72 of file Scheduler-system-info.cc.
References Tree< K, E >::begin(), Process::beginExternalLease(), Thread::beginExternalLease(), Tree< K, E >::count(), Tree< K, E >::end(), Thread::getParent(), Thread::getStatus(), ProcessorBase::information(), Thread::m_Lock, m_SchedulerLock, and m_TPMap.
|
static |
Definition at line 255 of file Scheduler.cc.
| Scheduler::SystemActivity Scheduler::systemActivity | ( | ) |
Definition at line 33 of file Scheduler-system-info.cc.
| bool Scheduler::threadInSchedule | ( | Thread * | pThread | ) |
Whether a thread is entered into the scheduler at all.
Definition at line 162 of file Scheduler.cc.
References Spinlock::acquire(), Tree< K, E >::lookup(), m_SchedulerLock, m_TPMap, and Spinlock::release().
| void Scheduler::threadStatusChanged | ( | Thread * | pThread | ) |
Definition at line 453 of file Scheduler.cc.
| void Scheduler::waitUntilProcessRemoved | ( | Process * | expected | ) |
Completion barrier for deferred deletion after enumeration removal.
Definition at line 412 of file Scheduler.cc.
References Spinlock::acquire(), List< T, nodePoolSize >::begin(), List< T, nodePoolSize >::end(), m_Processes, m_ProcessRemovalWaiters, m_SchedulerLock, and Spinlock::release().
| void Scheduler::yield | ( | ) |
Causes a manual reschedule.
Definition at line 236 of file Scheduler.cc.
References ProcessorBase::information().
Referenced by RequestQueue::addRequest(), File::dataChanged(), IrqHandlerRegistry::dispatchThreaded(), Rtc::drainRemoteAlarmDispatch(), KernelElf::executeModules(), Ps2Controller::irq(), PosixSubsystem::kill(), InputManager::mainThread(), UsbInterruptInHandle::reset(), PosixSubsystem::sendSignal(), RequestQueue::startWorker(), UsbHub::suppressConnectionChanges(), KernelCoreSyscallManager::syscall(), KernelElf::unloadModules(), IrqHandlerRegistry::unregisterHandler(), KernelElf::waitForModulesToLoad(), Process::waitUntilTerminationReapableForTerminalCoordinator(), and RequestQueue::work().
Here is the caller graph for this function:
|
friend |
Definition at line 205 of file Scheduler.h.
|
friend |
Definition at line 204 of file Scheduler.h.
|
friend |
Definition at line 206 of file Scheduler.h.
|
private |
Definition at line 228 of file Scheduler.h.
|
private |
Definition at line 230 of file Scheduler.h.
|
private |
Definition at line 234 of file Scheduler.h.
|
staticprivate |
The Scheduler instance.
Definition at line 237 of file Scheduler.h.
|
private |
Definition at line 233 of file Scheduler.h.
|
private |
Definition at line 231 of file Scheduler.h.
|
private |
Definition at line 229 of file Scheduler.h.
|
private |
The next available process ID.
Definition at line 243 of file Scheduler.h.
Referenced by reserveProcessId().
|
private |
Pointer to the BSP's scheduler.
This may be necessary for threads that need to depend on e.g. interrupts that are only coming to the BSP, and having them run on a different CPU means they cannot control things like IRQs being enabled (not good).
Definition at line 261 of file Scheduler.h.
Referenced by initialise().
|
private |
Pointer to the kernel process.
Definition at line 252 of file Scheduler.h.
Referenced by initialise().
All the processes currently in operation, for enumeration purposes.
Definition at line 240 of file Scheduler.h.
Referenced by acquireFirstProcessOfType(), acquireProcess(), acquireProcess(), acquireProcessById(), acquireThreadByTaskId(), addProcess(), drainDeferredTimeAccounting(), getChildProcess(), getNumProcesses(), removeProcess(), and waitUntilProcessRemoved().
|
private |
Wakes lifecycle waiters after a Process leaves enumeration.
Definition at line 268 of file Scheduler.h.
Referenced by removeProcess(), and waitUntilProcessRemoved().
|
private |
Map of processor->thread mappings, for load-balance accounting.
Definition at line 246 of file Scheduler.h.
|
private |
Main scheduler lock for modifying internal structures.
Definition at line 264 of file Scheduler.h.
Referenced by acquireFirstProcessOfType(), acquireProcess(), acquireProcess(), acquireProcessById(), acquireThreadByTaskId(), addProcess(), addThread(), drainDeferredTimeAccounting(), getChildProcess(), getNumProcesses(), removeProcess(), removeThread(), sampleLoadAverage(), threadInSchedule(), and waitUntilProcessRemoved().
|
private |
Map of thread->processor mappings.
Definition at line 249 of file Scheduler.h.
Referenced by addThread(), hasActiveSyscallLocked(), removeThread(), sampleLoadAverage(), and threadInSchedule().
|
private |
Definition at line 232 of file Scheduler.h.