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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
|
/*
* Copyright (C) 2012 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include <gtest/gtest.h>
#include <wtf/ArrayBuffer.h>
#include <wtf/HashCountedSet.h>
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/MemoryInstrumentation.h>
#include <wtf/MemoryInstrumentationArrayBufferView.h>
#include <wtf/MemoryInstrumentationHashCountedSet.h>
#include <wtf/MemoryInstrumentationHashMap.h>
#include <wtf/MemoryInstrumentationHashSet.h>
#include <wtf/MemoryInstrumentationString.h>
#include <wtf/MemoryInstrumentationVector.h>
#include <wtf/RefCounted.h>
#include <wtf/Vector.h>
#include <wtf/text/AtomicString.h>
#include <wtf/text/CString.h>
#include <wtf/text/StringBuffer.h>
#include <wtf/text/StringHash.h>
#include <wtf/text/StringImpl.h>
#include <wtf/text/WTFString.h>
namespace {
enum TestEnum { ONE = 1, TWO, THREE, MY_ENUM_MAX };
}
namespace WTF {
template<> struct DefaultHash<TestEnum> {
typedef IntHash<unsigned> Hash;
};
template<> struct HashTraits<TestEnum> : GenericHashTraits<TestEnum> {
static const bool emptyValueIsZero = true;
static const bool needsDestruction = false;
static void constructDeletedValue(TestEnum& slot) { slot = static_cast<TestEnum>(MY_ENUM_MAX + 1); }
static bool isDeletedValue(TestEnum value) { return value == (MY_ENUM_MAX + 1); }
};
}
namespace {
using WTF::MemoryObjectInfo;
using WTF::MemoryClassInfo;
using WTF::MemoryObjectType;
MemoryObjectType TestType = "TestType";
class InstrumentationTestHelper : public WTF::MemoryInstrumentation {
public:
InstrumentationTestHelper()
: MemoryInstrumentation(&m_client)
{ }
virtual void processDeferredInstrumentedPointers();
virtual void deferInstrumentedPointer(PassOwnPtr<InstrumentedPointerBase>);
size_t visitedObjects() const { return m_client.visitedObjects(); }
size_t reportedSizeForAllTypes() const { return m_client.reportedSizeForAllTypes(); }
size_t totalSize(const MemoryObjectType objectType) const { return m_client.totalSize(objectType); }
private:
class Client : public WTF::MemoryInstrumentationClient {
public:
virtual void countObjectSize(const void*, MemoryObjectType objectType, size_t size)
{
TypeToSizeMap::AddResult result = m_totalSizes.add(objectType, size);
if (!result.isNewEntry)
result.iterator->value += size;
}
virtual bool visited(const void* object) { return !m_visitedObjects.add(object).isNewEntry; }
virtual void checkCountedObject(const void*) { }
size_t visitedObjects() const { return m_visitedObjects.size(); }
size_t totalSize(const MemoryObjectType objectType) const
{
TypeToSizeMap::const_iterator i = m_totalSizes.find(objectType);
return i == m_totalSizes.end() ? 0 : i->value;
}
size_t reportedSizeForAllTypes() const
{
size_t size = 0;
for (TypeToSizeMap::const_iterator i = m_totalSizes.begin(); i != m_totalSizes.end(); ++i)
size += i->value;
return size;
}
private:
typedef HashMap<MemoryObjectType, size_t> TypeToSizeMap;
TypeToSizeMap m_totalSizes;
WTF::HashSet<const void*> m_visitedObjects;
};
Client m_client;
Vector<OwnPtr<InstrumentedPointerBase> > m_deferredInstrumentedPointers;
};
void InstrumentationTestHelper::processDeferredInstrumentedPointers()
{
while (!m_deferredInstrumentedPointers.isEmpty()) {
OwnPtr<InstrumentedPointerBase> pointer = m_deferredInstrumentedPointers.last().release();
m_deferredInstrumentedPointers.removeLast();
pointer->process(this);
}
}
void InstrumentationTestHelper::deferInstrumentedPointer(PassOwnPtr<InstrumentedPointerBase> pointer)
{
m_deferredInstrumentedPointers.append(pointer);
}
class NotInstrumented {
public:
NotInstrumented(const char* = 0) { }
char m_data[42];
};
class Instrumented {
public:
Instrumented() : m_notInstrumented(new NotInstrumented) { }
virtual ~Instrumented() { delete m_notInstrumented; }
virtual void reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
{
MemoryClassInfo info(memoryObjectInfo, this, TestType);
info.addMember(m_notInstrumented);
}
NotInstrumented* m_notInstrumented;
};
TEST(MemoryInstrumentationTest, sizeOf)
{
InstrumentationTestHelper helper;
Instrumented instrumented;
helper.addRootObject(instrumented);
EXPECT_EQ(sizeof(NotInstrumented), helper.reportedSizeForAllTypes());
EXPECT_EQ(1u, helper.visitedObjects());
}
TEST(MemoryInstrumentationTest, nullCheck)
{
InstrumentationTestHelper helper;
Instrumented* instrumented = 0;
helper.addRootObject(instrumented);
EXPECT_EQ(0u, helper.reportedSizeForAllTypes());
EXPECT_EQ(0u, helper.visitedObjects());
}
TEST(MemoryInstrumentationTest, ptrVsRef)
{
{
InstrumentationTestHelper helper;
Instrumented instrumented;
helper.addRootObject(&instrumented);
EXPECT_EQ(sizeof(Instrumented) + sizeof(NotInstrumented), helper.reportedSizeForAllTypes());
EXPECT_EQ(2u, helper.visitedObjects());
}
{
InstrumentationTestHelper helper;
Instrumented instrumented;
helper.addRootObject(instrumented);
EXPECT_EQ(sizeof(NotInstrumented), helper.reportedSizeForAllTypes());
EXPECT_EQ(1u, helper.visitedObjects());
}
}
TEST(MemoryInstrumentationTest, ownPtr)
{
InstrumentationTestHelper helper;
OwnPtr<Instrumented> instrumented(adoptPtr(new Instrumented));
helper.addRootObject(instrumented);
EXPECT_EQ(sizeof(Instrumented) + sizeof(NotInstrumented), helper.reportedSizeForAllTypes());
EXPECT_EQ(2u, helper.visitedObjects());
}
class InstrumentedRefPtr : public RefCounted<InstrumentedRefPtr> {
public:
InstrumentedRefPtr() : m_notInstrumented(new NotInstrumented) { }
virtual ~InstrumentedRefPtr() { delete m_notInstrumented; }
static PassRefPtr<InstrumentedRefPtr> create() { return adoptRef(new InstrumentedRefPtr()); }
virtual void reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
{
MemoryClassInfo info(memoryObjectInfo, this, TestType);
info.addMember(m_notInstrumented);
}
NotInstrumented* m_notInstrumented;
};
TEST(MemoryInstrumentationTest, refPtr)
{
InstrumentationTestHelper helper;
RefPtr<InstrumentedRefPtr> instrumentedRefPtr(adoptRef(new InstrumentedRefPtr));
helper.addRootObject(instrumentedRefPtr);
EXPECT_EQ(sizeof(InstrumentedRefPtr) + sizeof(NotInstrumented), helper.reportedSizeForAllTypes());
EXPECT_EQ(2u, helper.visitedObjects());
}
class InstrumentedWithOwnPtr : public Instrumented {
public:
InstrumentedWithOwnPtr() : m_notInstrumentedOwnPtr(adoptPtr(new NotInstrumented)) { }
virtual void reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
{
MemoryClassInfo info(memoryObjectInfo, this, TestType);
Instrumented::reportMemoryUsage(memoryObjectInfo);
info.addMember(m_notInstrumentedOwnPtr);
}
OwnPtr<NotInstrumented> m_notInstrumentedOwnPtr;
};
TEST(MemoryInstrumentationTest, ownPtrNotInstrumented)
{
InstrumentationTestHelper helper;
InstrumentedWithOwnPtr instrumentedWithOwnPtr;
helper.addRootObject(instrumentedWithOwnPtr);
EXPECT_EQ(2u * sizeof(NotInstrumented), helper.reportedSizeForAllTypes());
EXPECT_EQ(2u, helper.visitedObjects());
}
class InstrumentedUndefined {
public:
InstrumentedUndefined() : m_data(0) { }
void reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
{
MemoryClassInfo info(memoryObjectInfo, this);
}
int m_data;
};
class InstrumentedDOM {
public:
InstrumentedDOM() : m_instrumentedUndefined(adoptPtr(new InstrumentedUndefined)) { }
void reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
{
MemoryClassInfo info(memoryObjectInfo, this, TestType);
info.addMember(m_instrumentedUndefined);
}
OwnPtr<InstrumentedUndefined> m_instrumentedUndefined;
};
TEST(MemoryInstrumentationTest, ownerTypePropagation)
{
InstrumentationTestHelper helper;
OwnPtr<InstrumentedDOM> instrumentedDOM(adoptPtr(new InstrumentedDOM));
helper.addRootObject(instrumentedDOM);
EXPECT_EQ(sizeof(InstrumentedDOM) + sizeof(InstrumentedUndefined), helper.reportedSizeForAllTypes());
EXPECT_EQ(sizeof(InstrumentedDOM) + sizeof(InstrumentedUndefined), helper.totalSize(TestType));
EXPECT_EQ(2u, helper.visitedObjects());
}
class NonVirtualInstrumented {
public:
void reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
{
MemoryClassInfo info(memoryObjectInfo, this, TestType);
info.addMember(m_instrumented);
}
Instrumented m_instrumented;
};
TEST(MemoryInstrumentationTest, visitFirstMemberInNonVirtualClass)
{
InstrumentationTestHelper helper;
NonVirtualInstrumented nonVirtualInstrumented;
helper.addRootObject(&nonVirtualInstrumented);
EXPECT_EQ(sizeof(NonVirtualInstrumented) + sizeof(NotInstrumented), helper.reportedSizeForAllTypes());
EXPECT_EQ(2u, helper.visitedObjects());
}
template<typename T>
class InstrumentedOwner {
public:
template<typename V>
InstrumentedOwner(const V& value) : m_value(value) { }
InstrumentedOwner() { }
void reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
{
MemoryClassInfo info(memoryObjectInfo, this, TestType);
info.addMember(m_value);
}
T m_value;
};
TEST(MemoryInstrumentationTest, visitStrings)
{
{ // 8-bit string.
InstrumentationTestHelper helper;
InstrumentedOwner<String> stringInstrumentedOwner("String");
helper.addRootObject(stringInstrumentedOwner);
EXPECT_EQ(sizeof(StringImpl) + stringInstrumentedOwner.m_value.length(), helper.reportedSizeForAllTypes());
EXPECT_EQ(1u, helper.visitedObjects());
}
{ // 8-bit string with 16bit shadow.
InstrumentationTestHelper helper;
InstrumentedOwner<String> stringInstrumentedOwner("String");
stringInstrumentedOwner.m_value.characters();
helper.addRootObject(stringInstrumentedOwner);
EXPECT_EQ(sizeof(StringImpl) + stringInstrumentedOwner.m_value.length() * (sizeof(LChar) + sizeof(UChar)), helper.reportedSizeForAllTypes());
EXPECT_EQ(2u, helper.visitedObjects());
}
{ // 16 bit string.
InstrumentationTestHelper helper;
String string("String");
InstrumentedOwner<String> stringInstrumentedOwner(String(string.characters(), string.length()));
helper.addRootObject(stringInstrumentedOwner);
EXPECT_EQ(sizeof(StringImpl) + stringInstrumentedOwner.m_value.length() * sizeof(UChar), helper.reportedSizeForAllTypes());
EXPECT_EQ(1u, helper.visitedObjects());
}
{ // ASCIILiteral
InstrumentationTestHelper helper;
ASCIILiteral literal("String");
InstrumentedOwner<String> stringInstrumentedOwner(literal);
helper.addRootObject(stringInstrumentedOwner);
EXPECT_EQ(sizeof(StringImpl), helper.reportedSizeForAllTypes());
EXPECT_EQ(1u, helper.visitedObjects());
}
{ // Zero terminated internal buffer.
InstrumentationTestHelper helper;
InstrumentedOwner<String> stringInstrumentedOwner("string");
stringInstrumentedOwner.m_value.charactersWithNullTermination();
helper.addRootObject(stringInstrumentedOwner);
EXPECT_EQ(sizeof(StringImpl) + (stringInstrumentedOwner.m_value.length() + 1) * (sizeof(LChar) + sizeof(UChar)), helper.reportedSizeForAllTypes());
EXPECT_EQ(2u, helper.visitedObjects());
}
{ // Substring
InstrumentationTestHelper helper;
String baseString("String");
baseString.characters(); // Force 16 shadow creation.
InstrumentedOwner<String> stringInstrumentedOwner(baseString.substringSharingImpl(1, 4));
helper.addRootObject(stringInstrumentedOwner);
EXPECT_EQ(sizeof(StringImpl) * 2 + baseString.length() * (sizeof(LChar) + sizeof(UChar)), helper.reportedSizeForAllTypes());
EXPECT_EQ(3u, helper.visitedObjects());
}
{ // Owned buffer.
InstrumentationTestHelper helper;
StringBuffer<LChar> buffer(6);
InstrumentedOwner<String> stringInstrumentedOwner(String::adopt(buffer));
helper.addRootObject(stringInstrumentedOwner);
EXPECT_EQ(sizeof(StringImpl) + stringInstrumentedOwner.m_value.length(), helper.reportedSizeForAllTypes());
EXPECT_EQ(2u, helper.visitedObjects());
}
{
InstrumentationTestHelper helper;
InstrumentedOwner<AtomicString> atomicStringInstrumentedOwner("AtomicString");
atomicStringInstrumentedOwner.m_value.string().characters(); // Force 16bit shadow creation.
helper.addRootObject(atomicStringInstrumentedOwner);
EXPECT_EQ(sizeof(StringImpl) + atomicStringInstrumentedOwner.m_value.length() * (sizeof(LChar) + sizeof(UChar)), helper.reportedSizeForAllTypes());
EXPECT_EQ(2u, helper.visitedObjects());
}
{
InstrumentationTestHelper helper;
InstrumentedOwner<CString> cStringInstrumentedOwner("CString");
helper.addRootObject(cStringInstrumentedOwner);
EXPECT_EQ(sizeof(WTF::CStringBuffer) + cStringInstrumentedOwner.m_value.length(), helper.reportedSizeForAllTypes());
EXPECT_EQ(1u, helper.visitedObjects());
}
}
class TwoPointersToRefPtr {
public:
TwoPointersToRefPtr(const RefPtr<StringImpl>& value) : m_ptr1(&value), m_ptr2(&value) { }
void reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
{
MemoryClassInfo info(memoryObjectInfo, this, TestType);
info.addMember(m_ptr1);
info.addMember(m_ptr2);
}
const RefPtr<StringImpl>* m_ptr1;
const RefPtr<StringImpl>* m_ptr2;
};
TEST(MemoryInstrumentationTest, refPtrPtr)
{
InstrumentationTestHelper helper;
RefPtr<StringImpl> refPtr;
TwoPointersToRefPtr root(refPtr);
helper.addRootObject(root);
EXPECT_EQ(sizeof(RefPtr<StringImpl>), helper.reportedSizeForAllTypes());
EXPECT_EQ(1u, helper.visitedObjects());
}
class TwoPointersToOwnPtr {
public:
TwoPointersToOwnPtr(const OwnPtr<NotInstrumented>& value) : m_ptr1(&value), m_ptr2(&value) { }
void reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
{
MemoryClassInfo info(memoryObjectInfo, this, TestType);
info.addMember(m_ptr1);
info.addMember(m_ptr2);
}
const OwnPtr<NotInstrumented>* m_ptr1;
const OwnPtr<NotInstrumented>* m_ptr2;
};
TEST(MemoryInstrumentationTest, ownPtrPtr)
{
InstrumentationTestHelper helper;
OwnPtr<NotInstrumented> ownPtr;
TwoPointersToOwnPtr root(ownPtr);
helper.addRootObject(root);
EXPECT_EQ(sizeof(OwnPtr<NotInstrumented>), helper.reportedSizeForAllTypes());
EXPECT_EQ(1u, helper.visitedObjects());
}
template<typename T>
class InstrumentedTemplate {
public:
template<typename V>
InstrumentedTemplate(const V& value) : m_value(value) { }
template<typename MemoryObjectInfo>
void reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
{
typename MemoryObjectInfo::ClassInfo info(memoryObjectInfo, this, TestType);
info.addMember(m_value);
}
T m_value;
};
TEST(MemoryInstrumentationTest, detectReportMemoryUsageMethod)
{
{
InstrumentationTestHelper helper;
OwnPtr<InstrumentedTemplate<String> > value(adoptPtr(new InstrumentedTemplate<String>("")));
InstrumentedOwner<InstrumentedTemplate<String>* > root(value.get());
helper.addRootObject(root);
EXPECT_EQ(sizeof(InstrumentedTemplate<String>) + sizeof(StringImpl), helper.reportedSizeForAllTypes());
// FIXME: it is failing on Chromium Canary bots but works fine locally.
// EXPECT_EQ(2, helper.visitedObjects());
}
{
InstrumentationTestHelper helper;
OwnPtr<InstrumentedTemplate<NotInstrumented> > value(adoptPtr(new InstrumentedTemplate<NotInstrumented>("")));
InstrumentedOwner<InstrumentedTemplate<NotInstrumented>* > root(value.get());
helper.addRootObject(root);
EXPECT_EQ(sizeof(InstrumentedTemplate<NotInstrumented>), helper.reportedSizeForAllTypes());
EXPECT_EQ(1u, helper.visitedObjects());
}
}
TEST(MemoryInstrumentationTest, vectorZeroInlineCapacity)
{
InstrumentationTestHelper helper;
InstrumentedOwner<Vector<int> > vectorOwner(16);
helper.addRootObject(vectorOwner);
EXPECT_EQ(16 * sizeof(int), helper.reportedSizeForAllTypes());
EXPECT_EQ(1u, helper.visitedObjects());
}
TEST(MemoryInstrumentationTest, vectorFieldWithInlineCapacity)
{
InstrumentationTestHelper helper;
InstrumentedOwner<Vector<int, 4> > vectorOwner;
helper.addRootObject(vectorOwner);
EXPECT_EQ(static_cast<size_t>(0), helper.reportedSizeForAllTypes());
EXPECT_EQ(0u, helper.visitedObjects());
}
TEST(MemoryInstrumentationTest, vectorFieldWithInlineCapacityResized)
{
InstrumentationTestHelper helper;
InstrumentedOwner<Vector<int, 4> > vectorOwner;
vectorOwner.m_value.reserveCapacity(8);
helper.addRootObject(vectorOwner);
EXPECT_EQ(8u * sizeof(int), helper.reportedSizeForAllTypes());
EXPECT_EQ(1u, helper.visitedObjects());
}
TEST(MemoryInstrumentationTest, heapAllocatedVectorWithInlineCapacity)
{
InstrumentationTestHelper helper;
InstrumentedOwner<OwnPtr<Vector<int, 4> > > vectorOwner;
vectorOwner.m_value = adoptPtr(new Vector<int, 4>());
helper.addRootObject(vectorOwner);
EXPECT_EQ(sizeof(Vector<int, 4>), helper.reportedSizeForAllTypes());
EXPECT_EQ(1u, helper.visitedObjects());
}
TEST(MemoryInstrumentationTest, heapAllocatedVectorWithInlineCapacityResized)
{
InstrumentationTestHelper helper;
InstrumentedOwner<OwnPtr<Vector<int, 4> > > vectorOwner;
vectorOwner.m_value = adoptPtr(new Vector<int, 4>());
vectorOwner.m_value->reserveCapacity(8);
helper.addRootObject(vectorOwner);
EXPECT_EQ(8u * sizeof(int) + sizeof(Vector<int, 4>), helper.reportedSizeForAllTypes());
EXPECT_EQ(2u, helper.visitedObjects());
}
TEST(MemoryInstrumentationTest, vectorWithInstrumentedType)
{
InstrumentationTestHelper helper;
typedef Vector<String> StringVector;
OwnPtr<StringVector> value = adoptPtr(new StringVector());
size_t count = 10;
for (size_t i = 0; i < count; ++i)
value->append("string");
InstrumentedOwner<StringVector* > root(value.get());
helper.addRootObject(root);
EXPECT_EQ(sizeof(StringVector) + sizeof(String) * value->capacity() + (sizeof(StringImpl) + 6) * value->size(), helper.reportedSizeForAllTypes());
EXPECT_EQ(count + 2, (size_t)helper.visitedObjects());
}
TEST(MemoryInstrumentationTest, hashSetWithInstrumentedType)
{
InstrumentationTestHelper helper;
typedef HashSet<String> ValueType;
OwnPtr<ValueType> value = adoptPtr(new ValueType());
size_t count = 10;
for (size_t i = 0; i < count; ++i)
value->add(String::number(i));
InstrumentedOwner<ValueType* > root(value.get());
helper.addRootObject(root);
EXPECT_EQ(sizeof(ValueType) + sizeof(String) * value->capacity() + (sizeof(StringImpl) + 1) * value->size(), helper.reportedSizeForAllTypes());
EXPECT_EQ(count + 1, (size_t)helper.visitedObjects());
}
TEST(MemoryInstrumentationTest, hashMapWithNotInstrumentedKeysAndValues)
{
InstrumentationTestHelper helper;
typedef HashMap<int, int> IntToIntMap;
OwnPtr<IntToIntMap> value = adoptPtr(new IntToIntMap());
size_t count = 10;
for (size_t i = 1; i <= count; ++i)
value->set(i, i);
InstrumentedOwner<IntToIntMap* > root(value.get());
helper.addRootObject(root);
EXPECT_EQ(sizeof(IntToIntMap) + sizeof(IntToIntMap::ValueType) * value->capacity(), helper.reportedSizeForAllTypes());
EXPECT_EQ(1u, helper.visitedObjects());
}
TEST(MemoryInstrumentationTest, hashMapWithInstrumentedKeys)
{
InstrumentationTestHelper helper;
typedef HashMap<String, int> StringToIntMap;
OwnPtr<StringToIntMap> value = adoptPtr(new StringToIntMap());
size_t count = 10;
for (size_t i = 10; i < 10 + count; ++i)
value->set(String::number(i), i);
InstrumentedOwner<StringToIntMap* > root(value.get());
helper.addRootObject(root);
EXPECT_EQ(sizeof(StringToIntMap) + sizeof(StringToIntMap::ValueType) * value->capacity() + (sizeof(StringImpl) + 2) * value->size(), helper.reportedSizeForAllTypes());
EXPECT_EQ(count + 1, helper.visitedObjects());
}
TEST(MemoryInstrumentationTest, hashMapWithInstrumentedValues)
{
InstrumentationTestHelper helper;
typedef HashMap<int, String> IntToStringMap;
OwnPtr<IntToStringMap> value = adoptPtr(new IntToStringMap());
size_t count = 10;
for (size_t i = 10; i < 10 + count; ++i)
value->set(i, String::number(i));
InstrumentedOwner<IntToStringMap* > root(value.get());
helper.addRootObject(root);
EXPECT_EQ(sizeof(IntToStringMap) + sizeof(IntToStringMap::ValueType) * value->capacity() + (sizeof(StringImpl) + 2) * value->size(), helper.reportedSizeForAllTypes());
EXPECT_EQ(count + 1, helper.visitedObjects());
}
TEST(MemoryInstrumentationTest, hashMapWithInstrumentedKeysAndValues)
{
InstrumentationTestHelper helper;
typedef HashMap<String, String> StringToStringMap;
OwnPtr<StringToStringMap> value = adoptPtr(new StringToStringMap());
size_t count = 10;
for (size_t i = 10; i < 10 + count; ++i)
value->set(String::number(count + i), String::number(i));
InstrumentedOwner<StringToStringMap* > root(value.get());
helper.addRootObject(root);
EXPECT_EQ(sizeof(StringToStringMap) + sizeof(StringToStringMap::ValueType) * value->capacity() + 2 * (sizeof(StringImpl) + 2) * value->size(), helper.reportedSizeForAllTypes());
EXPECT_EQ(2u * count + 1, helper.visitedObjects());
}
TEST(MemoryInstrumentationTest, hashMapWithInstrumentedPointerKeysAndPointerValues)
{
InstrumentationTestHelper helper;
typedef HashMap<Instrumented*, Instrumented*> InstrumentedToInstrumentedMap;
OwnPtr<InstrumentedToInstrumentedMap> value(adoptPtr(new InstrumentedToInstrumentedMap()));
Vector<OwnPtr<Instrumented> > valuesVector;
size_t count = 10;
for (size_t i = 0; i < count; ++i) {
valuesVector.append(adoptPtr(new Instrumented()));
valuesVector.append(adoptPtr(new Instrumented()));
value->set(valuesVector[2 * i].get(), valuesVector[2 * i + 1].get());
}
InstrumentedOwner<InstrumentedToInstrumentedMap* > root(value.get());
helper.addRootObject(root);
EXPECT_EQ(sizeof(InstrumentedToInstrumentedMap) + sizeof(InstrumentedToInstrumentedMap::ValueType) * value->capacity() + 2 * (sizeof(Instrumented) + sizeof(NotInstrumented)) * value->size(), helper.reportedSizeForAllTypes());
EXPECT_EQ(2u * 2u * count + 1, helper.visitedObjects());
}
class InstrumentedConvertibleToInt {
public:
InstrumentedConvertibleToInt() : m_notInstrumented(0) { }
virtual ~InstrumentedConvertibleToInt() { }
virtual void reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
{
MemoryClassInfo info(memoryObjectInfo, this, TestType);
info.addMember(m_notInstrumented);
}
operator int() const { return 2012; }
NotInstrumented* m_notInstrumented;
};
// This test checks if reportMemoryUsage method will be called on a class
// that can be implicitly cast to int. Currently objects of such classes are
// treated as integers when they are stored in a HashMap by value and
// reportMemoryUsage will not be called on them. We may fix that later.
TEST(MemoryInstrumentationTest, hashMapWithValuesConvertibleToInt)
{
InstrumentationTestHelper helper;
typedef HashMap<InstrumentedConvertibleToInt*, InstrumentedConvertibleToInt> TestMap;
OwnPtr<TestMap> value(adoptPtr(new TestMap()));
Vector<OwnPtr<InstrumentedConvertibleToInt> > keysVector;
Vector<OwnPtr<NotInstrumented> > valuesVector;
size_t count = 10;
for (size_t i = 0; i < count; ++i) {
keysVector.append(adoptPtr(new InstrumentedConvertibleToInt()));
valuesVector.append(adoptPtr(new NotInstrumented()));
value->set(keysVector[i].get(), InstrumentedConvertibleToInt()).iterator->value.m_notInstrumented = valuesVector[i].get();
}
InstrumentedOwner<TestMap* > root(value.get());
helper.addRootObject(root);
EXPECT_EQ(sizeof(TestMap) + sizeof(TestMap::ValueType) * value->capacity() +
sizeof(InstrumentedConvertibleToInt) * count /* + sizeof(NotInstrumented) * count */, helper.reportedSizeForAllTypes());
EXPECT_EQ(count + 1, helper.visitedObjects());
}
TEST(MemoryInstrumentationTest, hashMapWithEnumKeysAndInstrumentedValues)
{
InstrumentationTestHelper helper;
typedef HashMap<TestEnum, String> EnumToStringMap;
OwnPtr<EnumToStringMap> value(adoptPtr(new EnumToStringMap()));
size_t count = MY_ENUM_MAX;
for (size_t i = ONE; i <= count; ++i)
value->set(static_cast<TestEnum>(i), String::number(i));
InstrumentedOwner<EnumToStringMap* > root(value.get());
helper.addRootObject(root);
EXPECT_EQ(sizeof(EnumToStringMap) + sizeof(EnumToStringMap::ValueType) * value->capacity() + (sizeof(StringImpl) + 1) * value->size(), helper.reportedSizeForAllTypes());
EXPECT_EQ(count + 1, helper.visitedObjects());
}
TEST(MemoryInstrumentationTest, hashCountedSetWithInstrumentedValues)
{
InstrumentationTestHelper helper;
typedef HashCountedSet<Instrumented*> TestSet;
OwnPtr<TestSet> set(adoptPtr(new TestSet()));
Vector<OwnPtr<Instrumented> > keysVector;
size_t count = 10;
for (size_t i = 0; i < count; ++i) {
keysVector.append(adoptPtr(new Instrumented()));
for (size_t j = 0; j <= i; j++)
set->add(keysVector.last().get());
}
InstrumentedOwner<TestSet* > root(set.get());
helper.addRootObject(root);
EXPECT_EQ(sizeof(TestSet) + sizeof(HashMap<Instrumented*, unsigned>::ValueType) * set->capacity() + (sizeof(Instrumented) + sizeof(NotInstrumented)) * set->size(), helper.reportedSizeForAllTypes());
EXPECT_EQ(2u * count + 1, helper.visitedObjects());
}
TEST(MemoryInstrumentationTest, arrayBuffer)
{
InstrumentationTestHelper helper;
typedef InstrumentedTemplate<RefPtr<ArrayBuffer> > ValueType;
ValueType value(ArrayBuffer::create(1000, sizeof(int)));
helper.addRootObject(value);
EXPECT_EQ(sizeof(int) * 1000 + sizeof(ArrayBuffer), helper.reportedSizeForAllTypes());
EXPECT_EQ(2u, helper.visitedObjects());
}
class AncestorWithVirtualMethod {
public:
virtual char* data() { return m_data; }
private:
char m_data[10];
};
class ClassWithTwoAncestors : public AncestorWithVirtualMethod, public Instrumented {
public:
virtual void reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
{
MemoryClassInfo info(memoryObjectInfo, this, TestType);
}
};
TEST(MemoryInstrumentationTest, instrumentedWithMultipleAncestors)
{
InstrumentationTestHelper helper;
OwnPtr<ClassWithTwoAncestors> instance = adoptPtr(new ClassWithTwoAncestors());
ClassWithTwoAncestors* descendantPointer = instance.get();
InstrumentedOwner<ClassWithTwoAncestors*> descendantPointerOwner(descendantPointer);
Instrumented* ancestorPointer = descendantPointer;
InstrumentedOwner<Instrumented*> ancestorPointerOwner(ancestorPointer);
EXPECT_NE(static_cast<void*>(ancestorPointer), static_cast<void*>(descendantPointer));
helper.addRootObject(descendantPointerOwner);
helper.addRootObject(ancestorPointerOwner);
EXPECT_EQ(sizeof(ClassWithTwoAncestors), helper.reportedSizeForAllTypes());
EXPECT_EQ(2u, helper.visitedObjects());
}
} // namespace
|