Debian Codegraph @

js/src/gc/AtomMarking.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/AtomMarking-inl.h"
8
9 #include <type_traits>
10
11 #include "gc/GCLock.h"
12 #include "gc/PublicIterators.h"
13
14 #include "gc/GC-inl.h"
15 #include "gc/Heap-inl.h"
16 #include "gc/PrivateIterators-inl.h"
17
18 namespace js {
19 namespace gc {
20
21 // [SMDOC] GC Atom Marking
22 //
23 // Things in the atoms zone (which includes atomized strings and other things,
24 // all of which we will refer to as 'atoms' here) may be pointed to freely by
25 // things in other zones. To avoid the need to perform garbage collections of
26 // the entire runtime to collect atoms, we compute a separate atom mark bitmap
27 // for each zone that is always an overapproximation of the atoms that zone is
28 // using. When an atom is not in the mark bitmap for any zone, it can be
29 // destroyed.
30 //
31 // To minimize interference with the rest of the GC, atom marking and sweeping
32 // is done by manipulating the mark bitmaps in the chunks used for the atoms.
33 // When the atoms zone is being collected, the mark bitmaps for the chunk(s)
34 // used by the atoms are updated normally during marking. After marking
35 // finishes, the chunk mark bitmaps are translated to a more efficient atom mark
36 // bitmap (see below) that is stored on the zones which the GC collected
37 // (computeBitmapFromChunkMarkBits). Before sweeping begins, the chunk mark
38 // bitmaps are updated with any atoms that might be referenced by zones which
39 // weren't collected (markAtomsUsedByUncollectedZones). The GC sweeping will
40 // then release all atoms which are not marked by any zone.
41 //
42 // The representation of atom mark bitmaps is as follows:
43 //
44 // Each arena in the atoms zone has an atomBitmapStart() value indicating the
45 // word index into the bitmap of the first thing in the arena. Each arena uses
46 // ArenaBitmapWords of data to store its bitmap, which uses the same
47 // representation as chunk mark bitmaps: one bit is allocated per Cell, with
48 // bits for space between things being unused when things are larger than a
49 // single Cell.
50
51 size_t AtomMarkingRuntime::allocateIndex(GCRuntime* gc) {
52 // We need to find a range of bits from the atoms bitmap for this arena.
53
54 // Try to merge background swept free indexes if necessary.
55 if (freeArenaIndexes.ref().empty()) {
56 mergePendingFreeArenaIndexes(gc);
57 }
58
59 // Look for a free range of bits compatible with this arena.
60 if (!freeArenaIndexes.ref().empty()) {
61 return freeArenaIndexes.ref().popCopy();
62 }
63
64 // Allocate a range of bits from the end for this arena.
65 size_t index = allocatedWords;
66 allocatedWords += ArenaBitmapWords;
67 return index;
68 }
69
70 void AtomMarkingRuntime::freeIndex(size_t index, const AutoLockGC& lock) {
71 MOZ_ASSERT((index % ArenaBitmapWords) == 0);
72 MOZ_ASSERT(index < allocatedWords);
73
74 bool wasEmpty = pendingFreeArenaIndexes.ref().empty();
75 MOZ_ASSERT_IF(wasEmpty, !hasPendingFreeArenaIndexes);
76
77 if (!pendingFreeArenaIndexes.ref().append(index)) {
78 // Leak these atom bits if we run out of memory.
79 return;
80 }
81
82 if (wasEmpty) {
83 hasPendingFreeArenaIndexes = true;
84 }
85 }
86
87 void AtomMarkingRuntime::mergePendingFreeArenaIndexes(GCRuntime* gc) {
88 MOZ_ASSERT(CurrentThreadCanAccessRuntime(gc->rt));
89 if (!hasPendingFreeArenaIndexes) {
90 return;
91 }
92
93 AutoLockGC lock(gc);
94 MOZ_ASSERT(!pendingFreeArenaIndexes.ref().empty());
95
96 hasPendingFreeArenaIndexes = false;
97
98 if (freeArenaIndexes.ref().empty()) {
99 std::swap(freeArenaIndexes.ref(), pendingFreeArenaIndexes.ref());
100 return;
101 }
102
103 // Leak these atom bits if we run out of memory.
104 (void)freeArenaIndexes.ref().appendAll(pendingFreeArenaIndexes.ref());
105 pendingFreeArenaIndexes.ref().clear();
106 }
107
108 void AtomMarkingRuntime::refineZoneBitmapsForCollectedZones(
109 GCRuntime* gc, size_t collectedZones) {
110 // If there is more than one zone to update, copy the chunk mark bits into a
111 // bitmap and AND that into the atom marking bitmap for each zone.
112 DenseBitmap marked;
113 if (collectedZones > 1 && computeBitmapFromChunkMarkBits(gc, marked)) {
114 for (GCZonesIter zone(gc); !zone.done(); zone.next()) {
115 refineZoneBitmapForCollectedZone(zone, marked);
116 }
117 return;
118 }
119
120 // If there's only one zone (or on OOM), AND the mark bits for each arena into
121 // the zones' atom marking bitmaps directly.
122 for (GCZonesIter zone(gc); !zone.done(); zone.next()) {
123 if (zone->isAtomsZone()) {
124 continue;
125 }
126
127 for (auto thingKind : AllAllocKinds()) {
128 for (ArenaIterInGC aiter(gc->atomsZone(), thingKind); !aiter.done();
129 aiter.next()) {
130 Arena* arena = aiter.get();
131 MarkBitmapWord* chunkWords = arena->chunk()->markBits.arenaBits(arena);
132 zone->markedAtoms().bitwiseAndRangeWith(arena->atomBitmapStart(),
133 ArenaBitmapWords, chunkWords);
134 }
135 }
136 }
137 }
138
139 bool AtomMarkingRuntime::computeBitmapFromChunkMarkBits(GCRuntime* gc,
140 DenseBitmap& bitmap) {
141 MOZ_ASSERT(CurrentThreadIsPerformingGC());
142
143 if (!bitmap.ensureSpace(allocatedWords)) {
144 return false;
145 }
146
147 Zone* atomsZone = gc->atomsZone();
148 for (auto thingKind : AllAllocKinds()) {
149 for (ArenaIterInGC aiter(atomsZone, thingKind); !aiter.done();
150 aiter.next()) {
151 Arena* arena = aiter.get();
152 MarkBitmapWord* chunkWords = arena->chunk()->markBits.arenaBits(arena);
153 bitmap.copyBitsFrom(arena->atomBitmapStart(), ArenaBitmapWords,
154 chunkWords);
155 }
156 }
157
158 return true;
159 }
160
161 void AtomMarkingRuntime::refineZoneBitmapForCollectedZone(
162 Zone* zone, const DenseBitmap& bitmap) {
163 MOZ_ASSERT(zone->isCollectingFromAnyThread());
164
165 if (zone->isAtomsZone()) {
166 return;
167 }
168
169 // Take the bitwise and between the two mark bitmaps to get the best new
170 // overapproximation we can. |bitmap| might include bits that are not in
171 // the zone's mark bitmap, if additional zones were collected by the GC.
172 zone->markedAtoms().bitwiseAndWith(bitmap);
173 }
174
175 // Set any bits in the chunk mark bitmaps for atoms which are marked in bitmap.
176 template <typename Bitmap>
177 static void BitwiseOrIntoChunkMarkBits(Zone* atomsZone, Bitmap& bitmap) {
178 // Make sure that by copying the mark bits for one arena in word sizes we
179 // do not affect the mark bits for other arenas.
180 static_assert(ArenaBitmapBits == ArenaBitmapWords * JS_BITS_PER_WORD,
181 "ArenaBitmapWords must evenly divide ArenaBitmapBits");
182
183 for (auto thingKind : AllAllocKinds()) {
184 for (ArenaIterInGC aiter(atomsZone, thingKind); !aiter.done();
185 aiter.next()) {
186 Arena* arena = aiter.get();
187 MarkBitmapWord* chunkWords = arena->chunk()->markBits.arenaBits(arena);
188 bitmap.bitwiseOrRangeInto(arena->atomBitmapStart(), ArenaBitmapWords,
189 chunkWords);
190 }
191 }
192 }
193
194 void AtomMarkingRuntime::markAtomsUsedByUncollectedZones(
195 GCRuntime* gc, size_t uncollectedZones) {
196 MOZ_ASSERT(CurrentThreadIsPerformingGC());
197
198 // If there are no uncollected non-atom zones then there's no work to do.
199 if (uncollectedZones == 0) {
200 return;
201 }
202
203 // If there is more than one zone then try to compute a simple union of the
204 // zone atom bitmaps before updating the chunk mark bitmaps. If there is only
205 // one zone or this allocation fails then update the chunk mark bitmaps
206 // separately for each zone.
207
208 DenseBitmap markedUnion;
209 if (uncollectedZones == 1 || !markedUnion.ensureSpace(allocatedWords)) {
210 for (ZonesIter zone(gc, SkipAtoms); !zone.done(); zone.next()) {
211 if (!zone->isCollecting()) {
212 BitwiseOrIntoChunkMarkBits(gc->atomsZone(), zone->markedAtoms());
213 }
214 }
215 return;
216 }
217
218 for (ZonesIter zone(gc, SkipAtoms); !zone.done(); zone.next()) {
219 // We only need to update the chunk mark bits for zones which were
220 // not collected in the current GC. Atoms which are referenced by
221 // collected zones have already been marked.
222 if (!zone->isCollecting()) {
223 zone->markedAtoms().bitwiseOrInto(markedUnion);
224 }
225 }
226
227 BitwiseOrIntoChunkMarkBits(gc->atomsZone(), markedUnion);
228 }
229
230 template <typename T>
231 void AtomMarkingRuntime::markAtom(JSContext* cx, T* thing) {
232 return inlinedMarkAtom(cx, thing);
233 }
234
235 template void AtomMarkingRuntime::markAtom(JSContext* cx, JSAtom* thing);
236 template void AtomMarkingRuntime::markAtom(JSContext* cx, JS::Symbol* thing);
237
238 void AtomMarkingRuntime::markId(JSContext* cx, jsid id) {
239 if (id.isAtom()) {
240 markAtom(cx, id.toAtom());
241 return;
242 }
243 if (id.isSymbol()) {
244 markAtom(cx, id.toSymbol());
245 return;
246 }
247 MOZ_ASSERT(!id.isGCThing());
248 }
249
250 void AtomMarkingRuntime::markAtomValue(JSContext* cx, const Value& value) {
251 if (value.isString()) {
252 if (value.toString()->isAtom()) {
253 markAtom(cx, &value.toString()->asAtom());
254 }
255 return;
256 }
257 if (value.isSymbol()) {
258 markAtom(cx, value.toSymbol());
259 return;
260 }
261 MOZ_ASSERT_IF(value.isGCThing(), value.isObject() ||
262 value.isPrivateGCThing() ||
263 value.isBigInt());
264 }
265
266 #ifdef DEBUG
267 template <typename T>
268 bool AtomMarkingRuntime::atomIsMarked(Zone* zone, T* thing) {
269 static_assert(std::is_same_v<T, JSAtom> || std::is_same_v<T, JS::Symbol>,
270 "Should only be called with JSAtom* or JS::Symbol* argument");
271
272 MOZ_ASSERT(thing);
273 MOZ_ASSERT(!IsInsideNursery(thing));
274 MOZ_ASSERT(thing->zoneFromAnyThread()->isAtomsZone());
275
276 if (!zone->runtimeFromAnyThread()->permanentAtomsPopulated()) {
277 return true;
278 }
279
280 if (thing->isPermanentAndMayBeShared()) {
281 return true;
282 }
283
284 if constexpr (std::is_same_v<T, JSAtom>) {
285 if (thing->isPinned()) {
286 return true;
287 }
288 }
289
290 size_t bit = GetAtomBit(&thing->asTenured());
291 return zone->markedAtoms().readonlyThreadsafeGetBit(bit);
292 }
293
294 template bool AtomMarkingRuntime::atomIsMarked(Zone* zone, JSAtom* thing);
295 template bool AtomMarkingRuntime::atomIsMarked(Zone* zone, JS::Symbol* thing);
296
297 template <>
298 bool AtomMarkingRuntime::atomIsMarked(Zone* zone, TenuredCell* thing) {
299 if (!thing) {
300 return true;
301 }
302
303 if (thing->is<JSString>()) {
304 JSString* str = thing->as<JSString>();
305 if (!str->isAtom()) {
306 return true;
307 }
308 return atomIsMarked(zone, &str->asAtom());
309 }
310
311 if (thing->is<JS::Symbol>()) {
312 return atomIsMarked(zone, thing->as<JS::Symbol>());
313 }
314
315 return true;
316 }
317
318 bool AtomMarkingRuntime::idIsMarked(Zone* zone, jsid id) {
319 if (id.isAtom()) {
320 return atomIsMarked(zone, id.toAtom());
321 }
322
323 if (id.isSymbol()) {
324 return atomIsMarked(zone, id.toSymbol());
325 }
326
327 MOZ_ASSERT(!id.isGCThing());
328 return true;
329 }
330
331 bool AtomMarkingRuntime::valueIsMarked(Zone* zone, const Value& value) {
332 if (value.isString()) {
333 if (value.toString()->isAtom()) {
334 return atomIsMarked(zone, &value.toString()->asAtom());
335 }
336 return true;
337 }
338
339 if (value.isSymbol()) {
340 return atomIsMarked(zone, value.toSymbol());
341 }
342
343 MOZ_ASSERT_IF(value.isGCThing(), value.isObject() ||
344 value.isPrivateGCThing() ||
345 value.isBigInt());
346 return true;
347 }
348
349 #endif // DEBUG
350
351 } // namespace gc
352
353 #ifdef DEBUG
354
355 bool AtomIsMarked(Zone* zone, JSAtom* atom) {
356 return zone->runtimeFromAnyThread()->gc.atomMarking.atomIsMarked(zone, atom);
357 }
358
359 bool AtomIsMarked(Zone* zone, jsid id) {
360 return zone->runtimeFromAnyThread()->gc.atomMarking.idIsMarked(zone, id);
361 }
362
363 bool AtomIsMarked(Zone* zone, const Value& value) {
364 return zone->runtimeFromAnyThread()->gc.atomMarking.valueIsMarked(zone,
365 value);
366 }
367
368 #endif // DEBUG
369
370 } // namespace js