The Pedigree Project 0.1
Scheduler.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef SCHEDULER_H
21#define SCHEDULER_H
22#include "pedigree/kernel/Atomic.h"
23#include "pedigree/kernel/Spinlock.h"
24#include "pedigree/kernel/compiler.h"
25#include "pedigree/kernel/process/CpuAffinity.h"
26#include "pedigree/kernel/process/LoadAverage.h"
27#include "pedigree/kernel/process/Mutex.h"
28#include "pedigree/kernel/process/Process.h"
29#include "pedigree/kernel/process/TerminationDeferral.h"
30#include "pedigree/kernel/process/WaitQueue.h"
31#include "pedigree/kernel/processor/types.h"
32#include "pedigree/kernel/utilities/List.h"
33#include "pedigree/kernel/utilities/Tree.h"
34#include "pedigree/kernel/utilities/new"
35
36#include <config.h>
37
38class Thread;
40
50class EXPORTED_PUBLIC Scheduler {
51 public:
62 class EXPORTED_PUBLIC ProcessLease {
63 public:
65 ProcessLease(ProcessLease&& other) noexcept;
67
68 ProcessLease& operator=(ProcessLease&& other) noexcept;
69
70 Process* get() const {
71 return m_pProcess;
72 }
73
74 Process* operator->() const {
75 return m_pProcess;
76 }
77
78 explicit operator bool() const {
79 return m_pProcess != nullptr;
80 }
81
82 void reset();
83
84 private:
85 friend class Scheduler;
86
87 explicit ProcessLease(Process* process);
88 ProcessLease(const ProcessLease&) = delete;
89 ProcessLease& operator=(const ProcessLease&) = delete;
90
91 Process* m_pProcess;
92 TerminationDeferral m_TerminationDeferral;
93 };
94
96 static Scheduler& instance() {
97 return m_Instance;
98 }
99
101 bool initialise(Process* pKernelProcess);
102
106 void addThread(Thread* pThread, PerProcessorScheduler& PPSched);
108 void removeThread(Thread* pThread);
109
111 bool threadInSchedule(Thread* pThread);
112
114 void addProcess(Process* pProcess);
117 void removeProcess(Process* pProcess);
118
120 void yield();
121
122 static CpuAffinityMask onlineAffinity();
123 static size_t affinityBytes();
124 static PerProcessorScheduler* schedulerForCpu(size_t cpu);
125 void rebindThread(Thread* thread, PerProcessorScheduler& scheduler);
126
128 uint64_t tasks = 0;
129 uint64_t loads[3] = {};
130 uint64_t userNanoseconds = 0;
131 uint64_t kernelNanoseconds = 0;
132 uint64_t idleNanoseconds = 0;
133 };
134 SystemActivity systemActivity();
136 void recordCpuTime(const Thread& thread, CpuTimeMode mode, Time::Timestamp elapsed);
138 void sampleLoadAverage();
140 void requestLoadAverageSample();
141
143 size_t getNumProcesses();
144
149 MUST_USE_RESULT bool acquireProcess(ProcessLease& lease, size_t n);
150
152 MUST_USE_RESULT bool acquireFirstProcessOfType(ProcessLease& lease, Process::ProcessType type);
153
158 MUST_USE_RESULT bool acquireProcessById(ProcessLease& lease, size_t id);
159 MUST_USE_RESULT bool acquireProcessByUserspaceId(ProcessLease& lease, size_t id,
160 const UserspacePidNamespace* space = nullptr);
161 MUST_USE_RESULT bool acquireNextProcess(ProcessLease& lease, size_t afterId);
162 MUST_USE_RESULT bool acquireThreadByUserspaceId(Process::ThreadLease& lease, size_t id,
163 const UserspacePidNamespace* space = nullptr);
164
166 MUST_USE_RESULT bool acquireThreadByTaskId(Process::ThreadLease& lease, size_t id);
167
176 MUST_USE_RESULT bool acquireProcess(ProcessLease& lease, Process* expected);
177
179 void waitUntilProcessRemoved(Process* expected);
180
187 Process* getChildProcess(Process* pParent, size_t n);
188
189 void threadStatusChanged(Thread* pThread);
190
191#if HOSTED && PEDIGREE_HOSTED_SMOKE_TESTS
192 using GenericThreadStatusHook = void (*)(Thread* thread);
193
195 static EXPORTED_PUBLIC void setGenericThreadStatusHook(GenericThreadStatusHook hook);
196 bool runHostedLoadAverageRequestRegression();
197#endif
198
199 Process* getKernelProcess() const {
200 return m_pKernelProcess;
201 }
202
203 PerProcessorScheduler* getBootstrapProcessorScheduler() const {
204 return m_pBspScheduler;
205 }
206
207 private:
208 friend class Process;
209 friend class PerProcessorScheduler;
210 friend class SyscallManager;
211
213 bool hasActiveSyscallLocked(Service_t service) const;
214
215 Scheduler();
216 NOT_COPYABLE_OR_ASSIGNABLE(Scheduler);
217
219 size_t reserveProcessId();
220
222 void releaseProcessLease(Process* process);
223
228 void drainDeferredTimeAccounting();
229
230 class ActivitySample;
231 static void releaseActivityEntry(Process* process, Thread* thread);
232 Mutex m_ActivityLock;
233 uint64_t m_NextActivityAttempt = 0;
234 bool m_ActivitySamplePending = false;
235 LoadAverage m_LoadAverage;
236 Atomic<uint64_t> m_UserNanoseconds;
237 Atomic<uint64_t> m_KernelNanoseconds;
238 Atomic<uint64_t> m_IdleNanoseconds;
239
242
245
248
251
254
257
266
269
271#if THREADS
273#endif
274};
275
276#endif // SCHEDULER_H
Definition List.h:61
Definition Mutex.h:56
ProcessType
Definition Process.h:296
This class manages how processes and threads are scheduled across processors.
Definition Scheduler.h:50
static Scheduler m_Instance
Definition Scheduler.h:241
List< Process *, 0 > m_Processes
Definition Scheduler.h:244
Spinlock m_SchedulerLock
Definition Scheduler.h:268
static Scheduler & instance()
Definition Scheduler.h:96
Atomic< size_t > m_NextPid
Definition Scheduler.h:247
Tree< PerProcessorScheduler *, List< Thread * > * > m_PTMap
Definition Scheduler.h:250
Tree< Thread *, PerProcessorScheduler * > m_TPMap
Definition Scheduler.h:253
WaitQueue m_ProcessRemovalWaiters
Definition Scheduler.h:272
Process * m_pKernelProcess
Definition Scheduler.h:256
PerProcessorScheduler * m_pBspScheduler
Definition Scheduler.h:265
A key/value dictionary.
Definition Tree.h:33