Debian Codegraph @

js/src/gc/StoreBuffer.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/StoreBuffer-inl.h"
8
9 #include "mozilla/Assertions.h"
10
11 #include "gc/Statistics.h"
12 #include "vm/MutexIDs.h"
13 #include "vm/Runtime.h"
14
15 using namespace js;
16 using namespace js::gc;
17
18 #ifdef DEBUG
19 void StoreBuffer::checkAccess() const {
20 // The GC runs tasks that may access the storebuffer in parallel and so must
21 // take a lock. The mutator may only access the storebuffer from the main
22 // thread.
23 if (runtime_->heapState() != JS::HeapState::Idle &&
24 runtime_->heapState() != JS::HeapState::MinorCollecting) {
25 MOZ_ASSERT(!CurrentThreadIsGCMarking());
26 runtime_->gc.assertCurrentThreadHasLockedStoreBuffer();
27 } else {
28 MOZ_ASSERT(CurrentThreadCanAccessRuntime(runtime_));
29 }
30 }
31 #endif
32
33 bool StoreBuffer::WholeCellBuffer::init() {
34 MOZ_ASSERT(!sweepHead_);
35 if (!storage_) {
36 storage_ = MakeUnique<LifoAlloc>(LifoAllocBlockSize, js::MallocArena);
37 if (!storage_) {
38 return false;
39 }
40 }
41
42 // This prevents LifoAlloc::Enum from crashing with a release
43 // assertion if we ever allocate one entry larger than
44 // LifoAllocBlockSize.
45 storage_->disableOversize();
46
47 clear();
48 return true;
49 }
50
51 bool StoreBuffer::GenericBuffer::init() {
52 if (!storage_) {
53 storage_ = MakeUnique<LifoAlloc>(LifoAllocBlockSize, js::MallocArena);
54 }
55 clear();
56 return bool(storage_);
57 }
58
59 void StoreBuffer::GenericBuffer::trace(JSTracer* trc, StoreBuffer* owner) {
60 mozilla::ReentrancyGuard g(*owner);
61 MOZ_ASSERT(owner->isEnabled());
62 if (!storage_) {
63 return;
64 }
65
66 for (LifoAlloc::Enum e(*storage_); !e.empty();) {
67 unsigned size = *e.read<unsigned>();
68 BufferableRef* edge = e.read<BufferableRef>(size);
69 edge->trace(trc);
70 }
71 }
72
73 StoreBuffer::StoreBuffer(JSRuntime* rt)
74 : runtime_(rt),
75 nursery_(rt->gc.nursery()),
76 aboutToOverflow_(false),
77 enabled_(false),
78 mayHavePointersToDeadCells_(false)
79 #ifdef DEBUG
80 ,
81 mEntered(false)
82 #endif
83 {
84 }
85
86 StoreBuffer::StoreBuffer(StoreBuffer&& other)
87 : bufferVal(std::move(other.bufferVal)),
88 bufStrCell(std::move(other.bufStrCell)),
89 bufBigIntCell(std::move(other.bufBigIntCell)),
90 bufObjCell(std::move(other.bufObjCell)),
91 bufferSlot(std::move(other.bufferSlot)),
92 bufferWasmAnyRef(std::move(other.bufferWasmAnyRef)),
93 bufferWholeCell(std::move(other.bufferWholeCell)),
94 bufferGeneric(std::move(other.bufferGeneric)),
95 runtime_(other.runtime_),
96 nursery_(other.nursery_),
97 aboutToOverflow_(other.aboutToOverflow_),
98 enabled_(other.enabled_),
99 mayHavePointersToDeadCells_(other.mayHavePointersToDeadCells_)
100 #ifdef DEBUG
101 ,
102 mEntered(other.mEntered)
103 #endif
104 {
105 MOZ_ASSERT(enabled_);
106 MOZ_ASSERT(!mEntered);
107 other.disable();
108 }
109
110 StoreBuffer& StoreBuffer::operator=(StoreBuffer&& other) {
111 if (&other != this) {
112 this->~StoreBuffer();
113 new (this) StoreBuffer(std::move(other));
114 }
115 return *this;
116 }
117
118 void StoreBuffer::checkEmpty() const { MOZ_ASSERT(isEmpty()); }
119
120 bool StoreBuffer::isEmpty() const {
121 return bufferVal.isEmpty() && bufStrCell.isEmpty() &&
122 bufBigIntCell.isEmpty() && bufObjCell.isEmpty() &&
123 bufferSlot.isEmpty() && bufferWasmAnyRef.isEmpty() &&
124 bufferWholeCell.isEmpty() && bufferGeneric.isEmpty();
125 }
126
127 bool StoreBuffer::enable() {
128 if (enabled_) {
129 return true;
130 }
131
132 checkEmpty();
133
134 if (!bufferWholeCell.init() || !bufferGeneric.init()) {
135 return false;
136 }
137
138 enabled_ = true;
139 return true;
140 }
141
142 void StoreBuffer::disable() {
143 checkEmpty();
144
145 if (!enabled_) {
146 return;
147 }
148
149 aboutToOverflow_ = false;
150
151 enabled_ = false;
152 }
153
154 void StoreBuffer::clear() {
155 if (!enabled_) {
156 return;
157 }
158
159 aboutToOverflow_ = false;
160 mayHavePointersToDeadCells_ = false;
161
162 bufferVal.clear();
163 bufStrCell.clear();
164 bufBigIntCell.clear();
165 bufObjCell.clear();
166 bufferSlot.clear();
167 bufferWasmAnyRef.clear();
168 bufferWholeCell.clear();
169 bufferGeneric.clear();
170 }
171
172 void StoreBuffer::setAboutToOverflow(JS::GCReason reason) {
173 if (!aboutToOverflow_) {
174 aboutToOverflow_ = true;
175 runtime_->gc.stats().count(gcstats::COUNT_STOREBUFFER_OVERFLOW);
176 }
177 nursery_.requestMinorGC(reason);
178 }
179
180 void StoreBuffer::addSizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf,
181 JS::GCSizes* sizes) {
182 sizes->storeBufferVals += bufferVal.sizeOfExcludingThis(mallocSizeOf);
183 sizes->storeBufferCells += bufStrCell.sizeOfExcludingThis(mallocSizeOf) +
184 bufBigIntCell.sizeOfExcludingThis(mallocSizeOf) +
185 bufObjCell.sizeOfExcludingThis(mallocSizeOf);
186 sizes->storeBufferSlots += bufferSlot.sizeOfExcludingThis(mallocSizeOf);
187 sizes->storeBufferWasmAnyRefs +=
188 bufferWasmAnyRef.sizeOfExcludingThis(mallocSizeOf);
189 sizes->storeBufferWholeCells +=
190 bufferWholeCell.sizeOfExcludingThis(mallocSizeOf);
191 sizes->storeBufferGenerics += bufferGeneric.sizeOfExcludingThis(mallocSizeOf);
192 }
193
194 ArenaCellSet ArenaCellSet::Empty;
195
196 ArenaCellSet::ArenaCellSet(Arena* arena)
197 : arena(arena)
198 #ifdef DEBUG
199 ,
200 minorGCNumberAtCreation(
201 arena->zone()->runtimeFromMainThread()->gc.minorGCCount())
202 #endif
203 {
204 MOZ_ASSERT(arena);
205 MOZ_ASSERT(bits.isAllClear());
206 }
207
208 ArenaCellSet* StoreBuffer::WholeCellBuffer::allocateCellSet(Arena* arena) {
209 MOZ_ASSERT(arena->bufferedCells() == &ArenaCellSet::Empty);
210
211 Zone* zone = arena->zone();
212 JSRuntime* rt = zone->runtimeFromMainThread();
213 if (!rt->gc.nursery().isEnabled()) {
214 return nullptr;
215 }
216
217 AutoEnterOOMUnsafeRegion oomUnsafe;
218 auto* cells = storage_->new_<ArenaCellSet>(arena);
219 if (!cells) {
220 oomUnsafe.crash("Failed to allocate ArenaCellSet");
221 }
222
223 arena->bufferedCells() = cells;
224
225 if (isAboutToOverflow()) {
226 rt->gc.storeBuffer().setAboutToOverflow(
227 JS::GCReason::FULL_WHOLE_CELL_BUFFER);
228 }
229
230 return cells;
231 }
232
233 void gc::CellHeaderPostWriteBarrier(JSObject** ptr, JSObject* prev,
234 JSObject* next) {
235 InternalBarrierMethods<JSObject*>::postBarrier(ptr, prev, next);
236 }
237
238 void StoreBuffer::WholeCellBuffer::clear() {
239 for (LifoAlloc::Enum e(*storage_); !e.empty();) {
240 ArenaCellSet* cellSet = e.read<ArenaCellSet>();
241 cellSet->arena->bufferedCells() = &ArenaCellSet::Empty;
242 }
243 sweepHead_ = nullptr;
244
245 if (storage_) {
246 storage_->used() ? storage_->releaseAll() : storage_->freeAll();
247 }
248
249 last_ = nullptr;
250 }
251
252 template struct StoreBuffer::MonoTypeBuffer<StoreBuffer::ValueEdge>;
253 template struct StoreBuffer::MonoTypeBuffer<StoreBuffer::SlotsEdge>;
254 template struct StoreBuffer::MonoTypeBuffer<StoreBuffer::WasmAnyRefEdge>;
255
256 void js::gc::PostWriteBarrierCell(Cell* cell, Cell* prev, Cell* next) {
257 if (!next || !cell->isTenured()) {
258 return;
259 }
260
261 StoreBuffer* buffer = next->storeBuffer();
262 if (!buffer || (prev && prev->storeBuffer())) {
263 return;
264 }
265
266 buffer->putWholeCell(cell);
267 }