summaryrefslogtreecommitdiff
path: root/deps/v8/test/unittests/heap/cppgc/stats-collector-unittest.cc
blob: 765456d9df5038e34e937443b2ee02ee58e76d22 (plain)
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
// Copyright 2020 the V8 project 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 "src/heap/cppgc/stats-collector.h"

#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace cppgc {
namespace internal {

namespace {

constexpr size_t kNoMarkedBytes = 0;

constexpr size_t kMinReportedSize = StatsCollector::kAllocationThresholdBytes;

class StatsCollectorTest : public ::testing::Test {
 public:
  static constexpr Platform* kNoPlatform = nullptr;

  StatsCollectorTest() : stats(kNoPlatform) {}

  void FakeAllocate(size_t bytes) {
    stats.NotifyAllocation(bytes);
    stats.NotifySafePointForConservativeCollection();
  }

  void FakeFree(size_t bytes) {
    stats.NotifyExplicitFree(bytes);
    stats.NotifySafePointForConservativeCollection();
  }

  StatsCollector stats;
};

}  // namespace

TEST_F(StatsCollectorTest, NoMarkedBytes) {
  stats.NotifyMarkingStarted(GarbageCollector::Config::CollectionType::kMajor,
                             GarbageCollector::Config::MarkingType::kAtomic,
                             GarbageCollector::Config::IsForcedGC::kNotForced);
  stats.NotifyMarkingCompleted(kNoMarkedBytes);
  stats.NotifySweepingCompleted(
      GarbageCollector::Config::SweepingType::kAtomic);
  auto event = stats.GetPreviousEventForTesting();
  EXPECT_EQ(0u, event.marked_bytes);
}

TEST_F(StatsCollectorTest, EventPrevGCMarkedObjectSize) {
  stats.NotifyMarkingStarted(GarbageCollector::Config::CollectionType::kMajor,
                             GarbageCollector::Config::MarkingType::kAtomic,
                             GarbageCollector::Config::IsForcedGC::kNotForced);
  stats.NotifyMarkingCompleted(1024);
  stats.NotifySweepingCompleted(
      GarbageCollector::Config::SweepingType::kAtomic);
  auto event = stats.GetPreviousEventForTesting();
  EXPECT_EQ(1024u, event.marked_bytes);
}

TEST_F(StatsCollectorTest, AllocationNoReportBelowAllocationThresholdBytes) {
  constexpr size_t kObjectSize = 17;
  EXPECT_LT(kObjectSize, StatsCollector::kAllocationThresholdBytes);
  FakeAllocate(kObjectSize);
  EXPECT_EQ(0u, stats.allocated_object_size());
}

TEST_F(StatsCollectorTest, AlllocationReportAboveAllocationThresholdBytes) {
  constexpr size_t kObjectSize = StatsCollector::kAllocationThresholdBytes;
  EXPECT_GE(kObjectSize, StatsCollector::kAllocationThresholdBytes);
  FakeAllocate(kObjectSize);
  EXPECT_EQ(kObjectSize, stats.allocated_object_size());
}

TEST_F(StatsCollectorTest, InitialAllocatedObjectSize) {
  stats.NotifyMarkingStarted(GarbageCollector::Config::CollectionType::kMajor,
                             GarbageCollector::Config::MarkingType::kAtomic,
                             GarbageCollector::Config::IsForcedGC::kNotForced);
  EXPECT_EQ(0u, stats.allocated_object_size());
  stats.NotifyMarkingCompleted(kNoMarkedBytes);
  EXPECT_EQ(0u, stats.allocated_object_size());
  stats.NotifySweepingCompleted(
      GarbageCollector::Config::SweepingType::kAtomic);
  EXPECT_EQ(0u, stats.allocated_object_size());
}

TEST_F(StatsCollectorTest, AllocatedObjectSize) {
  stats.NotifyMarkingStarted(GarbageCollector::Config::CollectionType::kMajor,
                             GarbageCollector::Config::MarkingType::kAtomic,
                             GarbageCollector::Config::IsForcedGC::kNotForced);
  FakeAllocate(kMinReportedSize);
  EXPECT_EQ(kMinReportedSize, stats.allocated_object_size());
  stats.NotifyMarkingCompleted(kMinReportedSize);
  EXPECT_EQ(kMinReportedSize, stats.allocated_object_size());
  stats.NotifySweepingCompleted(
      GarbageCollector::Config::SweepingType::kAtomic);
  EXPECT_EQ(kMinReportedSize, stats.allocated_object_size());
}

TEST_F(StatsCollectorTest, AllocatedObjectSizeNoMarkedBytes) {
  stats.NotifyMarkingStarted(GarbageCollector::Config::CollectionType::kMajor,
                             GarbageCollector::Config::MarkingType::kAtomic,
                             GarbageCollector::Config::IsForcedGC::kNotForced);
  FakeAllocate(kMinReportedSize);
  EXPECT_EQ(kMinReportedSize, stats.allocated_object_size());
  stats.NotifyMarkingCompleted(kNoMarkedBytes);
  EXPECT_EQ(0u, stats.allocated_object_size());
  stats.NotifySweepingCompleted(
      GarbageCollector::Config::SweepingType::kAtomic);
  EXPECT_EQ(0u, stats.allocated_object_size());
}

TEST_F(StatsCollectorTest, AllocatedObjectSizeAllocateAfterMarking) {
  stats.NotifyMarkingStarted(GarbageCollector::Config::CollectionType::kMajor,
                             GarbageCollector::Config::MarkingType::kAtomic,
                             GarbageCollector::Config::IsForcedGC::kNotForced);
  FakeAllocate(kMinReportedSize);
  EXPECT_EQ(kMinReportedSize, stats.allocated_object_size());
  stats.NotifyMarkingCompleted(kMinReportedSize);
  FakeAllocate(kMinReportedSize);
  EXPECT_EQ(2 * kMinReportedSize, stats.allocated_object_size());
  stats.NotifySweepingCompleted(
      GarbageCollector::Config::SweepingType::kAtomic);
  EXPECT_EQ(2 * kMinReportedSize, stats.allocated_object_size());
}

class MockAllocationObserver : public StatsCollector::AllocationObserver {
 public:
  MOCK_METHOD(void, AllocatedObjectSizeIncreased, (size_t), (override));
  MOCK_METHOD(void, AllocatedObjectSizeDecreased, (size_t), (override));
  MOCK_METHOD(void, ResetAllocatedObjectSize, (size_t), (override));
  MOCK_METHOD(void, AllocatedSizeIncreased, (size_t), (override));
  MOCK_METHOD(void, AllocatedSizeDecreased, (size_t), (override));
};

TEST_F(StatsCollectorTest, RegisterUnregisterObserver) {
  MockAllocationObserver observer;
  stats.RegisterObserver(&observer);
  stats.UnregisterObserver(&observer);
}

TEST_F(StatsCollectorTest, ObserveAllocatedObjectSizeIncreaseAndDecrease) {
  MockAllocationObserver observer;
  stats.RegisterObserver(&observer);
  EXPECT_CALL(observer, AllocatedObjectSizeIncreased(kMinReportedSize));
  FakeAllocate(kMinReportedSize);
  EXPECT_CALL(observer, AllocatedObjectSizeDecreased(kMinReportedSize));
  FakeFree(kMinReportedSize);
  stats.UnregisterObserver(&observer);
}

namespace {

void FakeGC(StatsCollector* stats, size_t marked_bytes) {
  stats->NotifyMarkingStarted(GarbageCollector::Config::CollectionType::kMajor,
                              GarbageCollector::Config::MarkingType::kAtomic,
                              GarbageCollector::Config::IsForcedGC::kNotForced);
  stats->NotifyMarkingCompleted(marked_bytes);
  stats->NotifySweepingCompleted(
      GarbageCollector::Config::SweepingType::kAtomic);
}

}  // namespace

TEST_F(StatsCollectorTest, ObserveResetAllocatedObjectSize) {
  MockAllocationObserver observer;
  stats.RegisterObserver(&observer);
  EXPECT_CALL(observer, AllocatedObjectSizeIncreased(kMinReportedSize));
  FakeAllocate(kMinReportedSize);
  EXPECT_CALL(observer, ResetAllocatedObjectSize(64));
  FakeGC(&stats, 64);
  stats.UnregisterObserver(&observer);
}

TEST_F(StatsCollectorTest, ObserveAllocatedMemoryIncreaseAndDecrease) {
  MockAllocationObserver observer;
  stats.RegisterObserver(&observer);
  static constexpr size_t kAllocatedMemorySize = 4096;
  EXPECT_CALL(observer, AllocatedSizeIncreased(kAllocatedMemorySize));
  stats.NotifyAllocatedMemory(kAllocatedMemorySize);
  static constexpr size_t kFreedMemorySize = 2048;
  EXPECT_CALL(observer, AllocatedSizeDecreased(kFreedMemorySize));
  stats.NotifyFreedMemory(kFreedMemorySize);
  stats.UnregisterObserver(&observer);
}

namespace {

class AllocationObserverTriggeringGC final
    : public StatsCollector::AllocationObserver {
 public:
  AllocationObserverTriggeringGC(StatsCollector* stats, double survival_ratio)
      : stats(stats), survival_ratio_(survival_ratio) {}

  void AllocatedObjectSizeIncreased(size_t bytes) final {
    increase_call_count++;
    increased_size_bytes += bytes;
    if (increase_call_count == 1) {
      FakeGC(stats, bytes * survival_ratio_);
    }
  }

  // // Mock out the rest to trigger warnings if used.
  MOCK_METHOD(void, AllocatedObjectSizeDecreased, (size_t), (override));
  MOCK_METHOD(void, ResetAllocatedObjectSize, (size_t), (override));

  size_t increase_call_count = 0;
  size_t increased_size_bytes = 0;
  StatsCollector* stats;
  double survival_ratio_;
};

}  // namespace

TEST_F(StatsCollectorTest, ObserverTriggersGC) {
  constexpr double kSurvivalRatio = 0.5;
  AllocationObserverTriggeringGC gc_observer(&stats, kSurvivalRatio);
  MockAllocationObserver mock_observer;
  // Internal detail: First registered observer is also notified first.
  stats.RegisterObserver(&gc_observer);
  stats.RegisterObserver(&mock_observer);

  // Both observers see the exact allocated object size byte count.
  EXPECT_CALL(mock_observer,
              ResetAllocatedObjectSize(kMinReportedSize * kSurvivalRatio));
  EXPECT_CALL(gc_observer,
              ResetAllocatedObjectSize(kMinReportedSize * kSurvivalRatio));

  // Since the GC clears counters, mock_observer should see an increase call
  // with a delta of zero bytes. This expectation makes use of the internal
  // detail that first registered observer triggers GC.
  EXPECT_CALL(mock_observer, AllocatedObjectSizeIncreased(0));

  // Trigger scenario.
  FakeAllocate(kMinReportedSize);

  EXPECT_EQ(1u, gc_observer.increase_call_count);
  EXPECT_EQ(kMinReportedSize, gc_observer.increased_size_bytes);

  stats.UnregisterObserver(&gc_observer);
  stats.UnregisterObserver(&mock_observer);
}

TEST_F(StatsCollectorTest, AllocatedMemorySize) {
  EXPECT_EQ(0u, stats.allocated_memory_size());
  stats.NotifyAllocatedMemory(1024);
  EXPECT_EQ(1024u, stats.allocated_memory_size());
  stats.NotifyFreedMemory(1024);
  EXPECT_EQ(0u, stats.allocated_memory_size());
}

TEST_F(StatsCollectorTest, DiscardedMemorySize) {
  EXPECT_EQ(0u, stats.discarded_memory_size());
  stats.IncrementDiscardedMemory(1024);
  EXPECT_EQ(1024u, stats.discarded_memory_size());
  stats.DecrementDiscardedMemory(1024);
  EXPECT_EQ(0u, stats.discarded_memory_size());
}

TEST_F(StatsCollectorTest, ResidentMemorySizeWithoutDiscarded) {
  EXPECT_EQ(0u, stats.resident_memory_size());
  stats.NotifyAllocatedMemory(1024);
  EXPECT_EQ(1024u, stats.resident_memory_size());
  stats.NotifyFreedMemory(1024);
  EXPECT_EQ(0u, stats.resident_memory_size());
}

TEST_F(StatsCollectorTest, ResidentMemorySizeWithDiscarded) {
  EXPECT_EQ(0u, stats.resident_memory_size());
  stats.NotifyAllocatedMemory(8192);
  EXPECT_EQ(8192u, stats.resident_memory_size());
  stats.IncrementDiscardedMemory(4096);
  EXPECT_EQ(4096u, stats.resident_memory_size());
  stats.DecrementDiscardedMemory(4096);
  EXPECT_EQ(8192u, stats.resident_memory_size());
  stats.NotifyFreedMemory(8192);
  EXPECT_EQ(0u, stats.resident_memory_size());
}

}  // namespace internal
}  // namespace cppgc