forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeapCompactTest.cpp
489 lines (400 loc) · 15 KB
/
HeapCompactTest.cpp
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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "platform/heap/HeapCompact.h"
#include "platform/heap/Handle.h"
#include "platform/heap/SparseHeapBitmap.h"
#include "platform/wtf/Deque.h"
#include "platform/wtf/HashMap.h"
#include "platform/wtf/LinkedHashSet.h"
#include "platform/wtf/Vector.h"
#include "testing/gtest/include/gtest/gtest.h"
#include <memory>
namespace {
enum VerifyArenaCompaction {
NoVerify,
VectorsAreCompacted,
HashTablesAreCompacted,
};
class IntWrapper : public blink::GarbageCollectedFinalized<IntWrapper> {
public:
static IntWrapper* Create(int x, VerifyArenaCompaction verify = NoVerify) {
return new IntWrapper(x, verify);
}
virtual ~IntWrapper() {}
DEFINE_INLINE_TRACE() {
// Verify if compaction is indeed activated.
//
// What arenas end up being compacted is dependent on residency,
// so approximate the arena checks to fit.
blink::HeapCompact* compaction = visitor->Heap().Compaction();
switch (verify_) {
case NoVerify:
return;
case HashTablesAreCompacted:
CHECK(compaction->IsCompactingArena(
blink::BlinkGC::kHashTableArenaIndex));
return;
case VectorsAreCompacted:
CHECK(compaction->IsCompactingVectorArenas());
return;
}
}
int Value() const { return x_; }
bool operator==(const IntWrapper& other) const {
return other.Value() == Value();
}
unsigned GetHash() { return IntHash<int>::GetHash(x_); }
IntWrapper(int x, VerifyArenaCompaction verify) : x_(x), verify_(verify) {}
private:
IntWrapper();
int x_;
VerifyArenaCompaction verify_;
};
static_assert(WTF::IsTraceable<IntWrapper>::value,
"IsTraceable<> template failed to recognize trace method.");
} // namespace
using IntVector = blink::HeapVector<blink::Member<IntWrapper>>;
using IntDeque = blink::HeapDeque<blink::Member<IntWrapper>>;
using IntMap = blink::HeapHashMap<blink::Member<IntWrapper>, int>;
// TODO(sof): decide if this ought to be a global trait specialization.
// (i.e., for HeapHash*<T>.)
WTF_ALLOW_CLEAR_UNUSED_SLOTS_WITH_MEM_FUNCTIONS(IntMap);
namespace blink {
#if ENABLE_HEAP_COMPACTION
static const size_t kChunkRange = SparseHeapBitmap::kBitmapChunkRange;
static const size_t kUnitPointer = 0x1u
<< SparseHeapBitmap::kPointerAlignmentInBits;
TEST(HeapCompactTest, SparseBitmapBasic) {
Address base = reinterpret_cast<Address>(0x10000u);
std::unique_ptr<SparseHeapBitmap> bitmap = SparseHeapBitmap::Create(base);
size_t double_chunk = 2 * kChunkRange;
// 101010... starting at |base|.
for (size_t i = 0; i < double_chunk; i += 2 * kUnitPointer)
bitmap->Add(base + i);
// Check that hasRange() returns a bitmap subtree, if any, for a given
// address.
EXPECT_TRUE(!!bitmap->HasRange(base, 1));
EXPECT_TRUE(!!bitmap->HasRange(base + kUnitPointer, 1));
EXPECT_FALSE(!!bitmap->HasRange(base - kUnitPointer, 1));
// Test implementation details.. that each SparseHeapBitmap node maps
// |s_bitmapChunkRange| ranges only.
EXPECT_EQ(bitmap->HasRange(base + kUnitPointer, 1),
bitmap->HasRange(base + 2 * kUnitPointer, 1));
// Second range will be just past the first.
EXPECT_NE(bitmap->HasRange(base, 1), bitmap->HasRange(base + kChunkRange, 1));
// Iterate a range that will encompass more than one 'chunk'.
SparseHeapBitmap* start =
bitmap->HasRange(base + 2 * kUnitPointer, double_chunk);
EXPECT_TRUE(!!start);
for (size_t i = 2 * kUnitPointer; i < double_chunk; i += 2 * kUnitPointer) {
EXPECT_TRUE(start->IsSet(base + i));
EXPECT_FALSE(start->IsSet(base + i + kUnitPointer));
}
}
TEST(HeapCompactTest, SparseBitmapBuild) {
Address base = reinterpret_cast<Address>(0x10000u);
std::unique_ptr<SparseHeapBitmap> bitmap = SparseHeapBitmap::Create(base);
size_t double_chunk = 2 * kChunkRange;
// Create a sparse bitmap containing at least three chunks.
bitmap->Add(base - double_chunk);
bitmap->Add(base + double_chunk);
// This is sanity testing internal implementation details of
// SparseHeapBitmap; probing |isSet()| outside the bitmap
// of the range used in |hasRange()|, is not defined.
//
// Regardless, the testing here verifies that a |hasRange()| that
// straddles multiple internal nodes, returns a bitmap that is
// capable of returning correct |isSet()| results.
SparseHeapBitmap* start = bitmap->HasRange(
base - double_chunk - 2 * kUnitPointer, 4 * kUnitPointer);
EXPECT_TRUE(!!start);
EXPECT_TRUE(start->IsSet(base - double_chunk));
EXPECT_FALSE(start->IsSet(base - double_chunk + kUnitPointer));
EXPECT_FALSE(start->IsSet(base));
EXPECT_FALSE(start->IsSet(base + kUnitPointer));
EXPECT_FALSE(start->IsSet(base + double_chunk));
EXPECT_FALSE(start->IsSet(base + double_chunk + kUnitPointer));
start = bitmap->HasRange(base - double_chunk - 2 * kUnitPointer,
2 * double_chunk + 2 * kUnitPointer);
EXPECT_TRUE(!!start);
EXPECT_TRUE(start->IsSet(base - double_chunk));
EXPECT_FALSE(start->IsSet(base - double_chunk + kUnitPointer));
EXPECT_TRUE(start->IsSet(base));
EXPECT_FALSE(start->IsSet(base + kUnitPointer));
EXPECT_TRUE(start->IsSet(base + double_chunk));
EXPECT_FALSE(start->IsSet(base + double_chunk + kUnitPointer));
start = bitmap->HasRange(base, 20);
EXPECT_TRUE(!!start);
// Probing for values outside of hasRange() should be considered
// undefined, but do it to exercise the (left) tree traversal.
EXPECT_TRUE(start->IsSet(base - double_chunk));
EXPECT_FALSE(start->IsSet(base - double_chunk + kUnitPointer));
EXPECT_TRUE(start->IsSet(base));
EXPECT_FALSE(start->IsSet(base + kUnitPointer));
EXPECT_TRUE(start->IsSet(base + double_chunk));
EXPECT_FALSE(start->IsSet(base + double_chunk + kUnitPointer));
start = bitmap->HasRange(base + kChunkRange + 2 * kUnitPointer, 2048);
EXPECT_TRUE(!!start);
// Probing for values outside of hasRange() should be considered
// undefined, but do it to exercise node traversal.
EXPECT_FALSE(start->IsSet(base - double_chunk));
EXPECT_FALSE(start->IsSet(base - double_chunk + kUnitPointer));
EXPECT_FALSE(start->IsSet(base));
EXPECT_FALSE(start->IsSet(base + kUnitPointer));
EXPECT_FALSE(start->IsSet(base + kChunkRange));
EXPECT_TRUE(start->IsSet(base + double_chunk));
EXPECT_FALSE(start->IsSet(base + double_chunk + kUnitPointer));
}
TEST(HeapCompactTest, SparseBitmapLeftExtension) {
Address base = reinterpret_cast<Address>(0x10000u);
std::unique_ptr<SparseHeapBitmap> bitmap = SparseHeapBitmap::Create(base);
SparseHeapBitmap* start = bitmap->HasRange(base, 1);
EXPECT_TRUE(start);
// Verify that re-adding is a no-op.
bitmap->Add(base);
EXPECT_EQ(start, bitmap->HasRange(base, 1));
// Adding an Address |A| before a single-address SparseHeapBitmap node should
// cause that node to be "left extended" to use |A| as its new base.
bitmap->Add(base - 2 * kUnitPointer);
EXPECT_EQ(bitmap->HasRange(base, 1),
bitmap->HasRange(base - 2 * kUnitPointer, 1));
// Reset.
bitmap = SparseHeapBitmap::Create(base);
// If attempting same as above, but the Address |A| is outside the
// chunk size of a node, a new SparseHeapBitmap node needs to be
// created to the left of |bitmap|.
bitmap->Add(base - kChunkRange);
EXPECT_NE(bitmap->HasRange(base, 1),
bitmap->HasRange(base - 2 * kUnitPointer, 1));
bitmap = SparseHeapBitmap::Create(base);
bitmap->Add(base - kChunkRange + kUnitPointer);
// This address is just inside the horizon and shouldn't create a new chunk.
EXPECT_EQ(bitmap->HasRange(base, 1),
bitmap->HasRange(base - 2 * kUnitPointer, 1));
// ..but this one should, like for the sub-test above.
bitmap->Add(base - kChunkRange);
EXPECT_EQ(bitmap->HasRange(base, 1),
bitmap->HasRange(base - 2 * kUnitPointer, 1));
EXPECT_NE(bitmap->HasRange(base, 1), bitmap->HasRange(base - kChunkRange, 1));
}
static void PreciselyCollectGarbage() {
ThreadState::Current()->CollectGarbage(BlinkGC::kNoHeapPointersOnStack,
BlinkGC::kGCWithSweep,
BlinkGC::kForcedGC);
}
static void PerformHeapCompaction() {
EXPECT_FALSE(HeapCompact::ScheduleCompactionGCForTesting(true));
PreciselyCollectGarbage();
EXPECT_FALSE(HeapCompact::ScheduleCompactionGCForTesting(false));
}
// Do several GCs to make sure that later GCs don't free up old memory from
// previously run tests in this process.
static void ClearOutOldGarbage() {
ThreadHeap& heap = ThreadState::Current()->Heap();
while (true) {
size_t used = heap.ObjectPayloadSizeForTesting();
PreciselyCollectGarbage();
if (heap.ObjectPayloadSizeForTesting() >= used)
break;
}
}
TEST(HeapCompactTest, CompactVector) {
ClearOutOldGarbage();
IntWrapper* val = IntWrapper::Create(1, VectorsAreCompacted);
Persistent<IntVector> vector = new IntVector(10, val);
EXPECT_EQ(10u, vector->size());
for (size_t i = 0; i < vector->size(); ++i)
EXPECT_EQ(val, (*vector)[i]);
PerformHeapCompaction();
for (size_t i = 0; i < vector->size(); ++i)
EXPECT_EQ(val, (*vector)[i]);
}
TEST(HeapCompactTest, CompactHashMap) {
ClearOutOldGarbage();
Persistent<IntMap> int_map = new IntMap();
for (size_t i = 0; i < 100; ++i) {
IntWrapper* val = IntWrapper::Create(i, HashTablesAreCompacted);
int_map->insert(val, 100 - i);
}
EXPECT_EQ(100u, int_map->size());
for (auto k : *int_map)
EXPECT_EQ(k.key->Value(), 100 - k.value);
PerformHeapCompaction();
for (auto k : *int_map)
EXPECT_EQ(k.key->Value(), 100 - k.value);
}
TEST(HeapCompactTest, CompactVectorPartHashMap) {
ClearOutOldGarbage();
using IntMapVector = HeapVector<IntMap>;
Persistent<IntMapVector> int_map_vector = new IntMapVector();
for (size_t i = 0; i < 10; ++i) {
IntMap map;
for (size_t j = 0; j < 10; ++j) {
IntWrapper* val = IntWrapper::Create(j, VectorsAreCompacted);
map.insert(val, 10 - j);
}
int_map_vector->push_back(map);
}
EXPECT_EQ(10u, int_map_vector->size());
for (auto map : *int_map_vector) {
EXPECT_EQ(10u, map.size());
for (auto k : map) {
EXPECT_EQ(k.key->Value(), 10 - k.value);
}
}
PerformHeapCompaction();
EXPECT_EQ(10u, int_map_vector->size());
for (auto map : *int_map_vector) {
EXPECT_EQ(10u, map.size());
for (auto k : map) {
EXPECT_EQ(k.key->Value(), 10 - k.value);
}
}
}
TEST(HeapCompactTest, CompactHashPartVector) {
ClearOutOldGarbage();
using IntVectorMap = HeapHashMap<int, IntVector>;
Persistent<IntVectorMap> int_vector_map = new IntVectorMap();
for (size_t i = 0; i < 10; ++i) {
IntVector vector;
for (size_t j = 0; j < 10; ++j) {
vector.push_back(IntWrapper::Create(j, HashTablesAreCompacted));
}
int_vector_map->insert(1 + i, vector);
}
EXPECT_EQ(10u, int_vector_map->size());
for (const IntVector& int_vector : int_vector_map->Values()) {
EXPECT_EQ(10u, int_vector.size());
for (size_t i = 0; i < int_vector.size(); ++i) {
EXPECT_EQ(static_cast<int>(i), int_vector[i]->Value());
}
}
PerformHeapCompaction();
EXPECT_EQ(10u, int_vector_map->size());
for (const IntVector& int_vector : int_vector_map->Values()) {
EXPECT_EQ(10u, int_vector.size());
for (size_t i = 0; i < int_vector.size(); ++i) {
EXPECT_EQ(static_cast<int>(i), int_vector[i]->Value());
}
}
}
TEST(HeapCompactTest, CompactDeques) {
Persistent<IntDeque> deque = new IntDeque;
for (int i = 0; i < 8; ++i) {
deque->push_front(IntWrapper::Create(i, VectorsAreCompacted));
}
EXPECT_EQ(8u, deque->size());
for (size_t i = 0; i < deque->size(); ++i)
EXPECT_EQ(static_cast<int>(7 - i), deque->at(i)->Value());
PerformHeapCompaction();
for (size_t i = 0; i < deque->size(); ++i)
EXPECT_EQ(static_cast<int>(7 - i), deque->at(i)->Value());
}
TEST(HeapCompactTest, CompactDequeVectors) {
Persistent<HeapDeque<IntVector>> deque = new HeapDeque<IntVector>;
for (int i = 0; i < 8; ++i) {
IntWrapper* value = IntWrapper::Create(i, VectorsAreCompacted);
IntVector vector = IntVector(8, value);
deque->push_front(vector);
}
EXPECT_EQ(8u, deque->size());
for (size_t i = 0; i < deque->size(); ++i)
EXPECT_EQ(static_cast<int>(7 - i), deque->at(i).at(i)->Value());
PerformHeapCompaction();
for (size_t i = 0; i < deque->size(); ++i)
EXPECT_EQ(static_cast<int>(7 - i), deque->at(i).at(i)->Value());
}
TEST(HeapCompactTest, CompactLinkedHashSet) {
using OrderedHashSet = HeapLinkedHashSet<Member<IntWrapper>>;
Persistent<OrderedHashSet> set = new OrderedHashSet;
for (int i = 0; i < 13; ++i) {
IntWrapper* value = IntWrapper::Create(i, HashTablesAreCompacted);
set->insert(value);
}
EXPECT_EQ(13u, set->size());
int expected = 0;
for (IntWrapper* v : *set) {
EXPECT_EQ(expected, v->Value());
expected++;
}
PerformHeapCompaction();
expected = 0;
for (IntWrapper* v : *set) {
EXPECT_EQ(expected, v->Value());
expected++;
}
}
TEST(HeapCompactTest, CompactLinkedHashSetVector) {
using OrderedHashSet = HeapLinkedHashSet<Member<IntVector>>;
Persistent<OrderedHashSet> set = new OrderedHashSet;
for (int i = 0; i < 13; ++i) {
IntWrapper* value = IntWrapper::Create(i);
IntVector* vector = new IntVector(19, value);
set->insert(vector);
}
EXPECT_EQ(13u, set->size());
int expected = 0;
for (IntVector* v : *set) {
EXPECT_EQ(expected, (*v)[0]->Value());
expected++;
}
PerformHeapCompaction();
expected = 0;
for (IntVector* v : *set) {
EXPECT_EQ(expected, (*v)[0]->Value());
expected++;
}
}
TEST(HeapCompactTest, CompactLinkedHashSetMap) {
using Inner = HeapHashSet<Member<IntWrapper>>;
using OrderedHashSet = HeapLinkedHashSet<Member<Inner>>;
Persistent<OrderedHashSet> set = new OrderedHashSet;
for (int i = 0; i < 13; ++i) {
IntWrapper* value = IntWrapper::Create(i);
Inner* inner = new Inner;
inner->insert(value);
set->insert(inner);
}
EXPECT_EQ(13u, set->size());
int expected = 0;
for (const Inner* v : *set) {
EXPECT_EQ(1u, v->size());
EXPECT_EQ(expected, (*v->begin())->Value());
expected++;
}
PerformHeapCompaction();
expected = 0;
for (const Inner* v : *set) {
EXPECT_EQ(1u, v->size());
EXPECT_EQ(expected, (*v->begin())->Value());
expected++;
}
}
TEST(HeapCompactTest, CompactLinkedHashSetNested) {
using Inner = HeapLinkedHashSet<Member<IntWrapper>>;
using OrderedHashSet = HeapLinkedHashSet<Member<Inner>>;
Persistent<OrderedHashSet> set = new OrderedHashSet;
for (int i = 0; i < 13; ++i) {
IntWrapper* value = IntWrapper::Create(i);
Inner* inner = new Inner;
inner->insert(value);
set->insert(inner);
}
EXPECT_EQ(13u, set->size());
int expected = 0;
for (const Inner* v : *set) {
EXPECT_EQ(1u, v->size());
EXPECT_EQ(expected, (*v->begin())->Value());
expected++;
}
PerformHeapCompaction();
expected = 0;
for (const Inner* v : *set) {
EXPECT_EQ(1u, v->size());
EXPECT_EQ(expected, (*v->begin())->Value());
expected++;
}
}
#endif
} // namespace blink