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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
|
/*
* Copyright (C) 2003-2017 Apple Inc. All rights reserved.
* Copyright (C) 2007 Eric Seidel <eric@webkit.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "config.h"
#include "MarkedSpace.h"
#include "FunctionCodeBlock.h"
#include "IncrementalSweeper.h"
#include "JSObject.h"
#include "JSCInlines.h"
#include "MarkedAllocatorInlines.h"
#include "MarkedBlockInlines.h"
#include <wtf/ListDump.h>
namespace JSC {
std::array<size_t, MarkedSpace::numSizeClasses> MarkedSpace::s_sizeClassForSizeStep;
namespace {
const Vector<size_t>& sizeClasses()
{
static Vector<size_t>* result;
static std::once_flag once;
std::call_once(
once,
[] {
result = new Vector<size_t>();
auto add = [&] (size_t sizeClass) {
sizeClass = WTF::roundUpToMultipleOf<MarkedBlock::atomSize>(sizeClass);
if (Options::dumpSizeClasses())
dataLog("Adding JSC MarkedSpace size class: ", sizeClass, "\n");
// Perform some validation as we go.
RELEASE_ASSERT(!(sizeClass % MarkedSpace::sizeStep));
if (result->isEmpty())
RELEASE_ASSERT(sizeClass == MarkedSpace::sizeStep);
result->append(sizeClass);
};
// This is a definition of the size classes in our GC. It must define all of the
// size classes from sizeStep up to largeCutoff.
// Have very precise size classes for the small stuff. This is a loop to make it easy to reduce
// atomSize.
for (size_t size = MarkedSpace::sizeStep; size < MarkedSpace::preciseCutoff; size += MarkedSpace::sizeStep)
add(size);
// We want to make sure that the remaining size classes minimize internal fragmentation (i.e.
// the wasted space at the tail end of a MarkedBlock) while proceeding roughly in an exponential
// way starting at just above the precise size classes to four cells per block.
if (Options::dumpSizeClasses())
dataLog(" Marked block payload size: ", static_cast<size_t>(MarkedSpace::blockPayload), "\n");
for (unsigned i = 0; ; ++i) {
double approximateSize = MarkedSpace::preciseCutoff * pow(Options::sizeClassProgression(), i);
if (Options::dumpSizeClasses())
dataLog(" Next size class as a double: ", approximateSize, "\n");
size_t approximateSizeInBytes = static_cast<size_t>(approximateSize);
if (Options::dumpSizeClasses())
dataLog(" Next size class as bytes: ", approximateSizeInBytes, "\n");
// Make sure that the computer did the math correctly.
RELEASE_ASSERT(approximateSizeInBytes >= MarkedSpace::preciseCutoff);
if (approximateSizeInBytes > MarkedSpace::largeCutoff)
break;
size_t sizeClass =
WTF::roundUpToMultipleOf<MarkedSpace::sizeStep>(approximateSizeInBytes);
if (Options::dumpSizeClasses())
dataLog(" Size class: ", sizeClass, "\n");
// Optimize the size class so that there isn't any slop at the end of the block's
// payload.
unsigned cellsPerBlock = MarkedSpace::blockPayload / sizeClass;
size_t possiblyBetterSizeClass = (MarkedSpace::blockPayload / cellsPerBlock) & ~(MarkedSpace::sizeStep - 1);
if (Options::dumpSizeClasses())
dataLog(" Possibly better size class: ", possiblyBetterSizeClass, "\n");
// The size class we just came up with is better than the other one if it reduces
// total wastage assuming we only allocate cells of that size.
size_t originalWastage = MarkedSpace::blockPayload - cellsPerBlock * sizeClass;
size_t newWastage = (possiblyBetterSizeClass - sizeClass) * cellsPerBlock;
if (Options::dumpSizeClasses())
dataLog(" Original wastage: ", originalWastage, ", new wastage: ", newWastage, "\n");
size_t betterSizeClass;
if (newWastage > originalWastage)
betterSizeClass = sizeClass;
else
betterSizeClass = possiblyBetterSizeClass;
if (Options::dumpSizeClasses())
dataLog(" Choosing size class: ", betterSizeClass, "\n");
if (betterSizeClass == result->last()) {
// Defense for when expStep is small.
continue;
}
// This is usually how we get out of the loop.
if (betterSizeClass > MarkedSpace::largeCutoff
|| betterSizeClass > Options::largeAllocationCutoff())
break;
add(betterSizeClass);
}
// Manually inject size classes for objects we know will be allocated in high volume.
add(sizeof(UnlinkedFunctionExecutable));
add(sizeof(UnlinkedFunctionCodeBlock));
add(sizeof(FunctionExecutable));
add(sizeof(FunctionCodeBlock));
add(sizeof(JSString));
add(sizeof(JSFunction));
add(sizeof(PropertyTable));
add(sizeof(Structure));
{
// Sort and deduplicate.
std::sort(result->begin(), result->end());
auto it = std::unique(result->begin(), result->end());
result->shrinkCapacity(it - result->begin());
}
if (Options::dumpSizeClasses())
dataLog("JSC Heap MarkedSpace size class dump: ", listDump(*result), "\n");
// We have an optimiation in MarkedSpace::optimalSizeFor() that assumes things about
// the size class table. This checks our results against that function's assumptions.
for (size_t size = MarkedSpace::sizeStep, i = 0; size <= MarkedSpace::preciseCutoff; size += MarkedSpace::sizeStep, i++)
RELEASE_ASSERT(result->at(i) == size);
});
return *result;
}
template<typename TableType, typename SizeClassCons, typename DefaultCons>
void buildSizeClassTable(TableType& table, const SizeClassCons& cons, const DefaultCons& defaultCons)
{
size_t nextIndex = 0;
for (size_t sizeClass : sizeClasses()) {
auto entry = cons(sizeClass);
size_t index = MarkedSpace::sizeClassToIndex(sizeClass);
for (size_t i = nextIndex; i <= index; ++i)
table[i] = entry;
nextIndex = index + 1;
}
for (size_t i = nextIndex; i < MarkedSpace::numSizeClasses; ++i)
table[i] = defaultCons(MarkedSpace::indexToSizeClass(i));
}
} // anonymous namespace
void MarkedSpace::initializeSizeClassForStepSize()
{
static std::once_flag flag;
std::call_once(
flag,
[] {
buildSizeClassTable(
s_sizeClassForSizeStep,
[&] (size_t sizeClass) -> size_t {
return sizeClass;
},
[&] (size_t sizeClass) -> size_t {
return sizeClass;
});
});
}
MarkedSpace::MarkedSpace(Heap* heap)
: m_heap(heap)
, m_capacity(0)
, m_isIterating(false)
{
initializeSizeClassForStepSize();
}
MarkedSpace::~MarkedSpace()
{
forEachBlock(
[&] (MarkedBlock::Handle* block) {
freeBlock(block);
});
for (LargeAllocation* allocation : m_largeAllocations)
allocation->destroy();
ASSERT(!m_blocks.set().size());
}
void MarkedSpace::lastChanceToFinalize()
{
forEachAllocator(
[&] (MarkedAllocator& allocator) -> IterationStatus {
allocator.lastChanceToFinalize();
return IterationStatus::Continue;
});
for (LargeAllocation* allocation : m_largeAllocations)
allocation->lastChanceToFinalize();
}
void MarkedSpace::sweep()
{
m_heap->sweeper()->stopSweeping();
forEachAllocator(
[&] (MarkedAllocator& allocator) -> IterationStatus {
allocator.sweep();
return IterationStatus::Continue;
});
}
void MarkedSpace::sweepLargeAllocations()
{
RELEASE_ASSERT(m_largeAllocationsNurseryOffset == m_largeAllocations.size());
unsigned srcIndex = m_largeAllocationsNurseryOffsetForSweep;
unsigned dstIndex = srcIndex;
while (srcIndex < m_largeAllocations.size()) {
LargeAllocation* allocation = m_largeAllocations[srcIndex++];
allocation->sweep();
if (allocation->isEmpty()) {
m_capacity -= allocation->cellSize();
allocation->destroy();
continue;
}
m_largeAllocations[dstIndex++] = allocation;
}
m_largeAllocations.resize(dstIndex);
m_largeAllocationsNurseryOffset = m_largeAllocations.size();
}
void MarkedSpace::prepareForAllocation()
{
forEachAllocator(
[&] (MarkedAllocator& allocator) -> IterationStatus {
allocator.prepareForAllocation();
return IterationStatus::Continue;
});
m_activeWeakSets.takeFrom(m_newActiveWeakSets);
if (m_heap->collectionScope() == CollectionScope::Eden)
m_largeAllocationsNurseryOffsetForSweep = m_largeAllocationsNurseryOffset;
else
m_largeAllocationsNurseryOffsetForSweep = 0;
m_largeAllocationsNurseryOffset = m_largeAllocations.size();
m_allocatorForEmptyAllocation = m_firstAllocator;
}
void MarkedSpace::visitWeakSets(SlotVisitor& visitor)
{
auto visit = [&] (WeakSet* weakSet) {
weakSet->visit(visitor);
};
m_newActiveWeakSets.forEach(visit);
if (m_heap->collectionScope() == CollectionScope::Full)
m_activeWeakSets.forEach(visit);
}
void MarkedSpace::reapWeakSets()
{
auto visit = [&] (WeakSet* weakSet) {
weakSet->reap();
};
m_newActiveWeakSets.forEach(visit);
if (m_heap->collectionScope() == CollectionScope::Full)
m_activeWeakSets.forEach(visit);
}
void MarkedSpace::stopAllocating()
{
ASSERT(!isIterating());
forEachAllocator(
[&] (MarkedAllocator& allocator) -> IterationStatus {
allocator.stopAllocating();
return IterationStatus::Continue;
});
}
void MarkedSpace::prepareForConservativeScan()
{
m_largeAllocationsForThisCollectionBegin = m_largeAllocations.begin() + m_largeAllocationsOffsetForThisCollection;
m_largeAllocationsForThisCollectionSize = m_largeAllocations.size() - m_largeAllocationsOffsetForThisCollection;
m_largeAllocationsForThisCollectionEnd = m_largeAllocations.end();
RELEASE_ASSERT(m_largeAllocationsForThisCollectionEnd == m_largeAllocationsForThisCollectionBegin + m_largeAllocationsForThisCollectionSize);
std::sort(
m_largeAllocationsForThisCollectionBegin, m_largeAllocationsForThisCollectionEnd,
[&] (LargeAllocation* a, LargeAllocation* b) {
return a < b;
});
}
void MarkedSpace::prepareForMarking()
{
if (m_heap->collectionScope() == CollectionScope::Eden)
m_largeAllocationsOffsetForThisCollection = m_largeAllocationsNurseryOffset;
else
m_largeAllocationsOffsetForThisCollection = 0;
}
void MarkedSpace::resumeAllocating()
{
forEachAllocator(
[&] (MarkedAllocator& allocator) -> IterationStatus {
allocator.resumeAllocating();
return IterationStatus::Continue;
});
// Nothing to do for LargeAllocations.
}
bool MarkedSpace::isPagedOut(double deadline)
{
bool result = false;
forEachAllocator(
[&] (MarkedAllocator& allocator) -> IterationStatus {
if (allocator.isPagedOut(deadline)) {
result = true;
return IterationStatus::Done;
}
return IterationStatus::Continue;
});
// FIXME: Consider taking LargeAllocations into account here.
return result;
}
void MarkedSpace::freeBlock(MarkedBlock::Handle* block)
{
block->allocator()->removeBlock(block);
m_capacity -= MarkedBlock::blockSize;
m_blocks.remove(&block->block());
delete block;
}
void MarkedSpace::freeOrShrinkBlock(MarkedBlock::Handle* block)
{
if (!block->isEmpty()) {
block->shrink();
return;
}
freeBlock(block);
}
void MarkedSpace::shrink()
{
forEachAllocator(
[&] (MarkedAllocator& allocator) -> IterationStatus {
allocator.shrink();
return IterationStatus::Continue;
});
}
void MarkedSpace::beginMarking()
{
if (m_heap->collectionScope() == CollectionScope::Full) {
forEachAllocator(
[&] (MarkedAllocator& allocator) -> IterationStatus {
allocator.beginMarkingForFullCollection();
return IterationStatus::Continue;
});
if (UNLIKELY(nextVersion(m_markingVersion) == initialVersion)) {
forEachBlock(
[&] (MarkedBlock::Handle* handle) {
handle->block().resetMarks();
});
}
m_markingVersion = nextVersion(m_markingVersion);
for (LargeAllocation* allocation : m_largeAllocations)
allocation->flip();
}
if (!ASSERT_DISABLED) {
forEachBlock(
[&] (MarkedBlock::Handle* block) {
if (block->areMarksStale())
return;
ASSERT(!block->isFreeListed());
});
}
m_isMarking = true;
}
void MarkedSpace::endMarking()
{
if (UNLIKELY(nextVersion(m_newlyAllocatedVersion) == initialVersion)) {
forEachBlock(
[&] (MarkedBlock::Handle* handle) {
handle->resetAllocated();
});
}
m_newlyAllocatedVersion = nextVersion(m_newlyAllocatedVersion);
for (unsigned i = m_largeAllocationsOffsetForThisCollection; i < m_largeAllocations.size(); ++i)
m_largeAllocations[i]->clearNewlyAllocated();
if (!ASSERT_DISABLED) {
for (LargeAllocation* allocation : m_largeAllocations)
ASSERT_UNUSED(allocation, !allocation->isNewlyAllocated());
}
forEachAllocator(
[&] (MarkedAllocator& allocator) -> IterationStatus {
allocator.endMarking();
return IterationStatus::Continue;
});
m_isMarking = false;
}
void MarkedSpace::willStartIterating()
{
ASSERT(!isIterating());
stopAllocating();
m_isIterating = true;
}
void MarkedSpace::didFinishIterating()
{
ASSERT(isIterating());
resumeAllocating();
m_isIterating = false;
}
size_t MarkedSpace::objectCount()
{
size_t result = 0;
forEachBlock(
[&] (MarkedBlock::Handle* block) {
result += block->markCount();
});
for (LargeAllocation* allocation : m_largeAllocations) {
if (allocation->isMarked())
result++;
}
return result;
}
size_t MarkedSpace::size()
{
size_t result = 0;
forEachBlock(
[&] (MarkedBlock::Handle* block) {
result += block->markCount() * block->cellSize();
});
for (LargeAllocation* allocation : m_largeAllocations) {
if (allocation->isMarked())
result += allocation->cellSize();
}
return result;
}
size_t MarkedSpace::capacity()
{
return m_capacity;
}
void MarkedSpace::addActiveWeakSet(WeakSet* weakSet)
{
// We conservatively assume that the WeakSet should belong in the new set. In fact, some weak
// sets might contain new weak handles even though they are tied to old objects. This slightly
// increases the amount of scanning that an eden collection would have to do, but the effect
// ought to be small.
m_newActiveWeakSets.append(weakSet);
}
void MarkedSpace::didAddBlock(MarkedBlock::Handle* block)
{
// WARNING: This function is called before block is fully initialized. The block will not know
// its cellSize() or attributes(). The latter implies that you can't ask things like
// needsDestruction().
m_capacity += MarkedBlock::blockSize;
m_blocks.add(&block->block());
}
void MarkedSpace::didAllocateInBlock(MarkedBlock::Handle* block)
{
if (block->weakSet().isOnList()) {
block->weakSet().remove();
m_newActiveWeakSets.append(&block->weakSet());
}
}
MarkedBlock::Handle* MarkedSpace::findEmptyBlockToSteal()
{
for (; m_allocatorForEmptyAllocation; m_allocatorForEmptyAllocation = m_allocatorForEmptyAllocation->nextAllocator()) {
if (MarkedBlock::Handle* block = m_allocatorForEmptyAllocation->findEmptyBlockToSteal())
return block;
}
return nullptr;
}
void MarkedSpace::snapshotUnswept()
{
if (m_heap->collectionScope() == CollectionScope::Eden) {
forEachAllocator(
[&] (MarkedAllocator& allocator) -> IterationStatus {
allocator.snapshotUnsweptForEdenCollection();
return IterationStatus::Continue;
});
} else {
forEachAllocator(
[&] (MarkedAllocator& allocator) -> IterationStatus {
allocator.snapshotUnsweptForFullCollection();
return IterationStatus::Continue;
});
}
}
void MarkedSpace::assertNoUnswept()
{
if (ASSERT_DISABLED)
return;
forEachAllocator(
[&] (MarkedAllocator& allocator) -> IterationStatus {
allocator.assertNoUnswept();
return IterationStatus::Continue;
});
}
void MarkedSpace::dumpBits(PrintStream& out)
{
forEachAllocator(
[&] (MarkedAllocator& allocator) -> IterationStatus {
out.print("Bits for ", allocator, ":\n");
allocator.dumpBits(out);
return IterationStatus::Continue;
});
}
MarkedAllocator* MarkedSpace::addMarkedAllocator(
const AbstractLocker&, Subspace* subspace, size_t sizeClass)
{
MarkedAllocator* allocator = m_bagOfAllocators.add(heap(), subspace, sizeClass);
allocator->setNextAllocator(nullptr);
WTF::storeStoreFence();
if (!m_firstAllocator) {
m_firstAllocator = allocator;
m_lastAllocator = allocator;
m_allocatorForEmptyAllocation = allocator;
} else {
m_lastAllocator->setNextAllocator(allocator);
m_lastAllocator = allocator;
}
return allocator;
}
} // namespace JSC
|