Debian Codegraph @

js/src/gc/ParallelMarking.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "gc/ParallelMarking.h"
8
9 #include "gc/GCInternals.h"
10 #include "gc/GCLock.h"
11 #include "gc/ParallelWork.h"
12 #include "vm/GeckoProfiler.h"
13 #include "vm/HelperThreadState.h"
14 #include "vm/Runtime.h"
15
16 using namespace js;
17 using namespace js::gc;
18
19 using mozilla::TimeDuration;
20 using mozilla::TimeStamp;
21
22 using JS::SliceBudget;
23
24 class AutoAddTimeDuration {
25 TimeStamp start_;
26 TimeDuration& result_;
27
28 public:
29 explicit AutoAddTimeDuration(TimeDuration& result)
30 : start_(TimeStamp::Now()), result_(result) {}
31 TimeStamp start() const { return start_; }
32 ~AutoAddTimeDuration() { result_ += TimeSince(start_); }
33 };
34
35 ParallelMarker::ParallelMarker(GCRuntime* gc) : gc(gc) {}
36
37 size_t ParallelMarker::workerCount() const { return gc->markers.length(); }
38
39 bool ParallelMarker::mark(const SliceBudget& sliceBudget) {
40 MOZ_ASSERT(workerCount() <= gc->getMaxParallelThreads());
41
42 if (markOneColor(MarkColor::Black, sliceBudget) == NotFinished) {
43 return false;
44 }
45 MOZ_ASSERT(!hasWork(MarkColor::Black));
46
47 if (markOneColor(MarkColor::Gray, sliceBudget) == NotFinished) {
48 return false;
49 }
50 MOZ_ASSERT(!hasWork(MarkColor::Gray));
51
52 // Handle any delayed marking, which is not performed in parallel.
53 if (gc->hasDelayedMarking()) {
54 gc->markAllDelayedChildren(ReportMarkTime);
55 }
56
57 return true;
58 }
59
60 bool ParallelMarker::markOneColor(MarkColor color,
61 const SliceBudget& sliceBudget) {
62 // Run a marking slice and return whether the stack is now empty.
63
64 if (!hasWork(color)) {
65 return true;
66 }
67
68 gcstats::AutoPhase ap(gc->stats(), gcstats::PhaseKind::PARALLEL_MARK);
69
70 MOZ_ASSERT(workerCount() <= MaxParallelWorkers);
71 mozilla::Maybe<ParallelMarkTask> tasks[MaxParallelWorkers];
72
73 for (size_t i = 0; i < workerCount(); i++) {
74 GCMarker* marker = gc->markers[i].get();
75 tasks[i].emplace(this, marker, color, sliceBudget);
76
77 // Attempt to populate empty mark stacks.
78 //
79 // TODO: When tuning for more than two markers we may need to adopt a more
80 // sophisticated approach.
81 if (!marker->hasEntriesForCurrentColor() && gc->marker().canDonateWork()) {
82 GCMarker::moveWork(marker, &gc->marker(), false);
83 }
84 }
85
86 AutoLockHelperThreadState lock;
87
88 MOZ_ASSERT(activeTasks == 0);
89 for (size_t i = 0; i < workerCount(); i++) {
90 ParallelMarkTask& task = *tasks[i];
91 if (task.hasWork()) {
92 incActiveTasks(&task, lock);
93 }
94 }
95
96 // There should always be enough parallel tasks to run our marking work.
97 MOZ_RELEASE_ASSERT(gc->maxParallelThreads >= workerCount());
98
99 // Run the parallel tasks, using the main thread for the first one.
100 for (size_t i = 1; i < workerCount(); i++) {
101 ParallelMarkTask& task = *tasks[i];
102 gc->startTask(task, lock);
103 }
104 tasks[0]->runFromMainThread(lock);
105 tasks[0]->recordDuration(); // Record stats as if it used a helper thread.
106 for (size_t i = 1; i < workerCount(); i++) {
107 gc->joinTask(*tasks[i], lock);
108 }
109
110 #ifdef DEBUG
111 MOZ_ASSERT(waitingTasks.ref().isEmpty());
112 MOZ_ASSERT(waitingTaskCount == 0);
113 MOZ_ASSERT(activeTasks == 0);
114 #endif
115
116 return !hasWork(color);
117 }
118
119 bool ParallelMarker::hasWork(MarkColor color) const {
120 for (const auto& marker : gc->markers) {
121 if (marker->hasEntries(color)) {
122 return true;
123 }
124 }
125
126 return false;
127 }
128
129 ParallelMarkTask::ParallelMarkTask(ParallelMarker* pm, GCMarker* marker,
130 MarkColor color, const SliceBudget& budget)
131 : GCParallelTask(pm->gc, gcstats::PhaseKind::PARALLEL_MARK, GCUse::Marking),
132 pm(pm),
133 marker(marker),
134 color(*marker, color),
135 budget(budget) {
136 marker->enterParallelMarkingMode(pm);
137 }
138
139 ParallelMarkTask::~ParallelMarkTask() {
140 MOZ_ASSERT(!isWaiting.refNoCheck());
141 marker->leaveParallelMarkingMode();
142 }
143
144 bool ParallelMarkTask::hasWork() const {
145 return marker->hasEntriesForCurrentColor();
146 }
147
148 void ParallelMarkTask::recordDuration() {
149 // Record times separately to avoid double counting when these are summed.
150 gc->stats().recordParallelPhase(gcstats::PhaseKind::PARALLEL_MARK_MARK,
151 markTime.ref());
152 gc->stats().recordParallelPhase(gcstats::PhaseKind::PARALLEL_MARK_WAIT,
153 waitTime.ref());
154 TimeDuration other = duration() - markTime.ref() - waitTime.ref();
155 if (other < TimeDuration::Zero()) {
156 other = TimeDuration::Zero();
157 }
158 gc->stats().recordParallelPhase(gcstats::PhaseKind::PARALLEL_MARK_OTHER,
159 other);
160 }
161
162 void ParallelMarkTask::run(AutoLockHelperThreadState& lock) {
163 AutoUpdateMarkStackRanges updateRanges(*marker);
164
165 for (;;) {
166 if (hasWork()) {
167 if (!tryMarking(lock)) {
168 return;
169 }
170 } else {
171 if (!requestWork(lock)) {
172 return;
173 }
174 }
175 }
176
177 MOZ_ASSERT(!isWaiting);
178 }
179
180 bool ParallelMarkTask::tryMarking(AutoLockHelperThreadState& lock) {
181 MOZ_ASSERT(hasWork());
182 MOZ_ASSERT(marker->isParallelMarking());
183
184 // Mark until budget exceeded or we run out of work.
185 bool finished;
186 {
187 AutoUnlockHelperThreadState unlock(lock);
188
189 AutoAddTimeDuration time(markTime.ref());
190 finished = marker->markCurrentColorInParallel(budget);
191
192 GeckoProfilerRuntime& profiler = gc->rt->geckoProfiler();
193 if (profiler.enabled()) {
194 profiler.markInterval("Parallel marking ran", time.start(), nullptr,
195 JS::ProfilingCategoryPair::GCCC);
196 }
197 }
198
199 MOZ_ASSERT_IF(finished, !hasWork());
200 pm->decActiveTasks(this, lock);
201
202 return finished;
203 }
204
205 bool ParallelMarkTask::requestWork(AutoLockHelperThreadState& lock) {
206 MOZ_ASSERT(!hasWork());
207
208 if (!pm->hasActiveTasks(lock)) {
209 return false; // All other tasks are empty. We're finished.
210 }
211
212 budget.forceCheck();
213 if (budget.isOverBudget()) {
214 return false; // Over budget or interrupted.
215 }
216
217 // Add ourselves to the waiting list and wait for another task to give us
218 // work. The task with work calls ParallelMarker::donateWorkFrom.
219 waitUntilResumed(lock);
220
221 return true;
222 }
223
224 void ParallelMarkTask::waitUntilResumed(AutoLockHelperThreadState& lock) {
225 AutoAddTimeDuration time(waitTime.ref());
226
227 pm->addTaskToWaitingList(this, lock);
228
229 // Set isWaiting flag and wait for another thread to clear it and resume us.
230 MOZ_ASSERT(!isWaiting);
231 isWaiting = true;
232
233 do {
234 MOZ_ASSERT(pm->hasActiveTasks(lock));
235 resumed.wait(lock);
236 } while (isWaiting);
237
238 MOZ_ASSERT(!pm->isTaskInWaitingList(this, lock));
239
240 GeckoProfilerRuntime& profiler = gc->rt->geckoProfiler();
241 if (profiler.enabled()) {
242 char details[32];
243 SprintfLiteral(details, "markers=%zu", pm->workerCount());
244 profiler.markInterval("Parallel marking wait", time.start(), details,
245 JS::ProfilingCategoryPair::GCCC);
246 }
247 }
248
249 void ParallelMarkTask::resume() {
250 {
251 AutoLockHelperThreadState lock;
252 MOZ_ASSERT(isWaiting);
253
254 isWaiting = false;
255
256 // Increment the active task count before donateWorkFrom() returns so this
257 // can't reach zero before the waiting task runs again.
258 if (hasWork()) {
259 pm->incActiveTasks(this, lock);
260 }
261 }
262
263 resumed.notify_all();
264 }
265
266 void ParallelMarkTask::resumeOnFinish(const AutoLockHelperThreadState& lock) {
267 MOZ_ASSERT(isWaiting);
268 MOZ_ASSERT(!hasWork());
269
270 isWaiting = false;
271 resumed.notify_all();
272 }
273
274 void ParallelMarker::addTaskToWaitingList(
275 ParallelMarkTask* task, const AutoLockHelperThreadState& lock) {
276 MOZ_ASSERT(!task->hasWork());
277 MOZ_ASSERT(hasActiveTasks(lock));
278 MOZ_ASSERT(!isTaskInWaitingList(task, lock));
279 MOZ_ASSERT(waitingTaskCount < workerCount() - 1);
280
281 waitingTasks.ref().pushBack(task);
282 waitingTaskCount++;
283 }
284
285 #ifdef DEBUG
286 bool ParallelMarker::isTaskInWaitingList(
287 const ParallelMarkTask* task, const AutoLockHelperThreadState& lock) const {
288 // The const cast is because ElementProbablyInList is not const.
289 return const_cast<ParallelMarkTaskList&>(waitingTasks.ref())
290 .ElementProbablyInList(const_cast<ParallelMarkTask*>(task));
291 }
292 #endif
293
294 void ParallelMarker::incActiveTasks(ParallelMarkTask* task,
295 const AutoLockHelperThreadState& lock) {
296 MOZ_ASSERT(task->hasWork());
297 MOZ_ASSERT(activeTasks < workerCount());
298
299 activeTasks++;
300 }
301
302 void ParallelMarker::decActiveTasks(ParallelMarkTask* task,
303 const AutoLockHelperThreadState& lock) {
304 MOZ_ASSERT(activeTasks != 0);
305
306 activeTasks--;
307
308 if (activeTasks == 0) {
309 while (!waitingTasks.ref().isEmpty()) {
310 ParallelMarkTask* task = waitingTasks.ref().popFront();
311 MOZ_ASSERT(waitingTaskCount != 0);
312 waitingTaskCount--;
313 task->resumeOnFinish(lock);
314 }
315 }
316 }
317
318 void ParallelMarker::donateWorkFrom(GCMarker* src) {
319 GeckoProfilerRuntime& profiler = gc->rt->geckoProfiler();
320
321 if (!gHelperThreadLock.tryLock()) {
322 if (profiler.enabled()) {
323 profiler.markEvent("Parallel marking donate failed", "lock already held");
324 }
325 return;
326 }
327
328 // Check there are tasks waiting for work while holding the lock.
329 if (waitingTaskCount == 0) {
330 gHelperThreadLock.unlock();
331 if (profiler.enabled()) {
332 profiler.markEvent("Parallel marking donate failed", "no tasks waiting");
333 }
334 return;
335 }
336
337 // Take the first waiting task off the list.
338 ParallelMarkTask* waitingTask = waitingTasks.ref().popFront();
339 waitingTaskCount--;
340
341 // |task| is not running so it's safe to move work to it.
342 MOZ_ASSERT(waitingTask->isWaiting);
343
344 gHelperThreadLock.unlock();
345
346 // Move some work from this thread's mark stack to the waiting task.
347 MOZ_ASSERT(!waitingTask->hasWork());
348 size_t wordsMoved = GCMarker::moveWork(waitingTask->marker, src, true);
349
350 gc->stats().count(gcstats::COUNT_PARALLEL_MARK_INTERRUPTIONS);
351
352 if (profiler.enabled()) {
353 char details[32];
354 SprintfLiteral(details, "words=%zu", wordsMoved);
355 profiler.markEvent("Parallel marking donated work", details,
356 JS::ProfilingCategoryPair::GCCC);
357 }
358
359 // Resume waiting task.
360 waitingTask->resume();
361 }