This repository was archived by the owner on Jul 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathIdleSchedulerParent.cpp
More file actions
276 lines (244 loc) · 9.48 KB
/
Copy pathIdleSchedulerParent.cpp
File metadata and controls
276 lines (244 loc) · 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/ScopeExit.h"
#include "mozilla/StaticPrefs_page_load.h"
#include "mozilla/Unused.h"
#include "mozilla/ipc/IdleSchedulerParent.h"
#include "nsIPropertyBag2.h"
#include "nsSystemInfo.h"
#include "nsThreadUtils.h"
#include "nsITimer.h"
namespace mozilla {
namespace ipc {
base::SharedMemory* IdleSchedulerParent::sActiveChildCounter = nullptr;
std::bitset<NS_IDLE_SCHEDULER_COUNTER_ARRAY_LENGHT>
IdleSchedulerParent::sInUseChildCounters;
LinkedList<IdleSchedulerParent> IdleSchedulerParent::sDefault;
LinkedList<IdleSchedulerParent> IdleSchedulerParent::sWaitingForIdle;
LinkedList<IdleSchedulerParent> IdleSchedulerParent::sIdle;
AutoTArray<IdleSchedulerParent*, 8>* IdleSchedulerParent::sPrioritized =
nullptr;
Atomic<int32_t> IdleSchedulerParent::sCPUsForChildProcesses(-1);
uint32_t IdleSchedulerParent::sChildProcessesRunningPrioritizedOperation = 0;
nsITimer* IdleSchedulerParent::sStarvationPreventer = nullptr;
IdleSchedulerParent::IdleSchedulerParent() {
sDefault.insertBack(this);
if (sCPUsForChildProcesses == -1) {
// nsISystemInfo can be initialized only on the main thread.
// While waiting for the real logical core count behave as if there was just
// one core.
sCPUsForChildProcesses = 1;
nsCOMPtr<nsIThread> thread = do_GetCurrentThread();
nsCOMPtr<nsIRunnable> runnable =
NS_NewRunnableFunction("cpucount getter", [thread]() {
// Always pretend that there is at least one core for child processes.
// If there are multiple logical cores, reserve one for the parent
// process and for the non-main threads.
nsCOMPtr<nsIPropertyBag2> infoService =
do_GetService(NS_SYSTEMINFO_CONTRACTID);
if (infoService) {
int32_t cpus;
nsresult rv = infoService->GetPropertyAsInt32(
NS_LITERAL_STRING("cpucount"), &cpus);
if (NS_SUCCEEDED(rv) && cpus > 1) {
sCPUsForChildProcesses = cpus - 1;
}
// We have a new cpu count, reschedule idle scheduler.
nsCOMPtr<nsIRunnable> runnable =
NS_NewRunnableFunction("IdleSchedulerParent::Schedule", []() {
if (sActiveChildCounter && sActiveChildCounter->memory()) {
static_cast<Atomic<int32_t>*>(sActiveChildCounter->memory())
[NS_IDLE_SCHEDULER_INDEX_OF_CPU_COUNTER] =
static_cast<int32_t>(sCPUsForChildProcesses);
}
IdleSchedulerParent::Schedule(nullptr);
});
thread->Dispatch(runnable, NS_DISPATCH_NORMAL);
}
});
NS_DispatchToMainThread(runnable);
}
}
IdleSchedulerParent::~IdleSchedulerParent() {
// We can't know if an active process just crashed, so we just always expect
// that is the case.
if (mChildId) {
sInUseChildCounters[mChildId] = false;
if (sActiveChildCounter && sActiveChildCounter->memory() &&
static_cast<Atomic<int32_t>*>(
sActiveChildCounter->memory())[mChildId]) {
--static_cast<Atomic<int32_t>*>(
sActiveChildCounter
->memory())[NS_IDLE_SCHEDULER_INDEX_OF_ACTIVITY_COUNTER];
static_cast<Atomic<int32_t>*>(sActiveChildCounter->memory())[mChildId] =
0;
}
}
if (mRunningPrioritizedOperation) {
--sChildProcessesRunningPrioritizedOperation;
}
if (isInList()) {
remove();
if (sDefault.isEmpty() && sWaitingForIdle.isEmpty() && sIdle.isEmpty()) {
delete sActiveChildCounter;
sActiveChildCounter = nullptr;
if (sStarvationPreventer) {
sStarvationPreventer->Cancel();
NS_RELEASE(sStarvationPreventer);
}
}
}
Schedule(nullptr);
}
IPCResult IdleSchedulerParent::RecvInitForIdleUse(
InitForIdleUseResolver&& aResolve) {
// Create a shared memory object which is shared across all the relevant
// processes. Only first 4 bytes of the allocated are used currently to
// count activity state of child processes
if (!sActiveChildCounter) {
sActiveChildCounter = new base::SharedMemory();
size_t shmemSize = NS_IDLE_SCHEDULER_COUNTER_ARRAY_LENGHT * sizeof(int32_t);
if (sActiveChildCounter->Create(shmemSize) &&
sActiveChildCounter->Map(shmemSize)) {
memset(sActiveChildCounter->memory(), 0, shmemSize);
sInUseChildCounters[NS_IDLE_SCHEDULER_INDEX_OF_ACTIVITY_COUNTER] = true;
sInUseChildCounters[NS_IDLE_SCHEDULER_INDEX_OF_CPU_COUNTER] = true;
static_cast<Atomic<int32_t>*>(
sActiveChildCounter
->memory())[NS_IDLE_SCHEDULER_INDEX_OF_CPU_COUNTER] =
static_cast<int32_t>(sCPUsForChildProcesses);
} else {
delete sActiveChildCounter;
sActiveChildCounter = nullptr;
}
}
Maybe<SharedMemoryHandle> activeCounter;
SharedMemoryHandle handle;
if (sActiveChildCounter &&
sActiveChildCounter->ShareToProcess(OtherPid(), &handle)) {
activeCounter.emplace(handle);
}
uint32_t unusedId = 0;
for (uint32_t i = 0; i < NS_IDLE_SCHEDULER_COUNTER_ARRAY_LENGHT; ++i) {
if (!sInUseChildCounters[i]) {
sInUseChildCounters[i] = true;
unusedId = i;
break;
}
}
// If there wasn't an empty item, we'll fallback to 0.
mChildId = unusedId;
aResolve(Tuple<const mozilla::Maybe<SharedMemoryHandle>&, const uint32_t&>(
activeCounter, mChildId));
return IPC_OK();
}
IPCResult IdleSchedulerParent::RecvRequestIdleTime(uint64_t aId,
TimeDuration aBudget) {
mCurrentRequestId = aId;
mRequestedIdleBudget = aBudget;
remove();
sWaitingForIdle.insertBack(this);
Schedule(this);
return IPC_OK();
}
IPCResult IdleSchedulerParent::RecvIdleTimeUsed(uint64_t aId) {
if (mCurrentRequestId == aId) {
// Ensure the object is back in the default queue.
remove();
sDefault.insertBack(this);
}
Schedule(nullptr);
return IPC_OK();
}
IPCResult IdleSchedulerParent::RecvSchedule() {
Schedule(nullptr);
return IPC_OK();
}
IPCResult IdleSchedulerParent::RecvRunningPrioritizedOperation() {
++mRunningPrioritizedOperation;
if (mRunningPrioritizedOperation == 1) {
++sChildProcessesRunningPrioritizedOperation;
}
return IPC_OK();
}
IPCResult IdleSchedulerParent::RecvPrioritizedOperationDone() {
MOZ_ASSERT(mRunningPrioritizedOperation);
--mRunningPrioritizedOperation;
if (mRunningPrioritizedOperation == 0) {
--sChildProcessesRunningPrioritizedOperation;
Schedule(nullptr);
}
return IPC_OK();
}
int32_t IdleSchedulerParent::ActiveCount() {
if (sActiveChildCounter) {
return (static_cast<Atomic<int32_t>*>(
sActiveChildCounter
->memory())[NS_IDLE_SCHEDULER_INDEX_OF_ACTIVITY_COUNTER]);
}
return 0;
}
void IdleSchedulerParent::Schedule(IdleSchedulerParent* aRequester) {
if (sWaitingForIdle.isEmpty()) {
return;
}
if (!aRequester || !aRequester->mRunningPrioritizedOperation) {
int32_t activeCount = ActiveCount();
// Don't bail out so easily if we're running with very few cores.
if (sCPUsForChildProcesses > 1 && sCPUsForChildProcesses <= activeCount) {
// Too many processes are running, bail out.
EnsureStarvationTimer();
return;
}
if (sChildProcessesRunningPrioritizedOperation > 0 &&
sCPUsForChildProcesses / 2 <= activeCount) {
// We're running a prioritized operation and don't have too many spare
// cores for idle tasks, bail out.
EnsureStarvationTimer();
return;
}
}
// We can run run an idle task. If the requester is prioritized, just let it
// run itself.
RefPtr<IdleSchedulerParent> idleRequester;
if (aRequester && aRequester->mRunningPrioritizedOperation) {
aRequester->remove();
idleRequester = aRequester;
} else {
idleRequester = sWaitingForIdle.popFirst();
}
sIdle.insertBack(idleRequester);
Unused << idleRequester->SendIdleTime(idleRequester->mCurrentRequestId,
idleRequester->mRequestedIdleBudget);
}
void IdleSchedulerParent::EnsureStarvationTimer() {
// Even though idle runnables aren't really guaranteed to get run ever (which
// is why most of them have the timer fallback), try to not let any child
// process' idle handling to starve forever in case other processes are busy
if (!sStarvationPreventer) {
// Reuse StaticPrefs::page_load_deprioritization_period(), since that
// is used on child side when deciding the minimum idle period.
NS_NewTimerWithFuncCallback(
&sStarvationPreventer, StarvationCallback, nullptr,
StaticPrefs::page_load_deprioritization_period(),
nsITimer::TYPE_ONE_SHOT_LOW_PRIORITY, "StarvationCallback");
}
}
void IdleSchedulerParent::StarvationCallback(nsITimer* aTimer, void* aData) {
if (!sWaitingForIdle.isEmpty()) {
RefPtr<IdleSchedulerParent> first = sWaitingForIdle.getFirst();
// Treat the first process waiting for idle time as running prioritized
// operation so that it gets run.
++first->mRunningPrioritizedOperation;
++sChildProcessesRunningPrioritizedOperation;
Schedule(first);
--first->mRunningPrioritizedOperation;
--sChildProcessesRunningPrioritizedOperation;
}
NS_RELEASE(sStarvationPreventer);
}
} // namespace ipc
} // namespace mozilla