OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <limits> |
| 6 |
| 7 #include "src/flags.h" |
| 8 #include "src/heap/memory-reducer.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace v8 { |
| 12 namespace internal { |
| 13 |
| 14 MemoryReducer::State DoneState() { |
| 15 return MemoryReducer::State(MemoryReducer::kDone, 0, 0.0); |
| 16 } |
| 17 |
| 18 |
| 19 MemoryReducer::State WaitState(int started_gcs, double next_gc_start_ms) { |
| 20 return MemoryReducer::State(MemoryReducer::kWait, started_gcs, |
| 21 next_gc_start_ms); |
| 22 } |
| 23 |
| 24 |
| 25 MemoryReducer::State RunState(int started_gcs, double next_gc_start_ms) { |
| 26 return MemoryReducer::State(MemoryReducer::kRun, started_gcs, |
| 27 next_gc_start_ms); |
| 28 } |
| 29 |
| 30 |
| 31 MemoryReducer::Event MarkCompactEvent(double time_ms, |
| 32 bool next_gc_likely_to_collect_more) { |
| 33 MemoryReducer::Event event; |
| 34 event.type = MemoryReducer::kMarkCompact; |
| 35 event.time_ms = time_ms; |
| 36 event.next_gc_likely_to_collect_more = next_gc_likely_to_collect_more; |
| 37 return event; |
| 38 } |
| 39 |
| 40 |
| 41 MemoryReducer::Event MarkCompactEventGarbageLeft(double time_ms) { |
| 42 return MarkCompactEvent(time_ms, true); |
| 43 } |
| 44 |
| 45 |
| 46 MemoryReducer::Event MarkCompactEventNoGarbageLeft(double time_ms) { |
| 47 return MarkCompactEvent(time_ms, false); |
| 48 } |
| 49 |
| 50 |
| 51 MemoryReducer::Event TimerEvent(double time_ms, bool low_allocation_rate, |
| 52 bool high_fragmentation, |
| 53 bool incremental_gc_in_progress) { |
| 54 MemoryReducer::Event event; |
| 55 event.type = MemoryReducer::kTimer; |
| 56 event.time_ms = time_ms; |
| 57 event.low_allocation_rate = low_allocation_rate; |
| 58 event.high_fragmentation = high_fragmentation; |
| 59 event.incremental_gc_in_progress = incremental_gc_in_progress; |
| 60 return event; |
| 61 } |
| 62 |
| 63 |
| 64 MemoryReducer::Event TimerEventLowAllocationRate(double time_ms) { |
| 65 return TimerEvent(time_ms, true, false, false); |
| 66 } |
| 67 |
| 68 |
| 69 MemoryReducer::Event TimerEventHighFragmentation(double time_ms) { |
| 70 return TimerEvent(time_ms, false, true, false); |
| 71 } |
| 72 |
| 73 |
| 74 MemoryReducer::Event TimerEventHighAllocationRateAndLowFragmentation( |
| 75 double time_ms) { |
| 76 return TimerEvent(time_ms, false, false, false); |
| 77 } |
| 78 |
| 79 |
| 80 MemoryReducer::Event TimerEventPendingGC(double time_ms) { |
| 81 return TimerEvent(time_ms, true, true, true); |
| 82 } |
| 83 |
| 84 |
| 85 MemoryReducer::Event ScavengeEvent(double time_ms) { |
| 86 MemoryReducer::Event event; |
| 87 event.type = MemoryReducer::kScavenge; |
| 88 event.time_ms = time_ms; |
| 89 return event; |
| 90 } |
| 91 |
| 92 |
| 93 MemoryReducer::Event ContextDisposedEvent(double time_ms) { |
| 94 MemoryReducer::Event event; |
| 95 event.type = MemoryReducer::kContextDisposed; |
| 96 event.time_ms = time_ms; |
| 97 return event; |
| 98 } |
| 99 |
| 100 |
| 101 TEST(MemoryReducer, FromDoneToDone) { |
| 102 MemoryReducer::State state0(DoneState()), state1(DoneState()); |
| 103 |
| 104 state1 = MemoryReducer::Step(state0, TimerEventLowAllocationRate(0)); |
| 105 EXPECT_EQ(MemoryReducer::kDone, state1.action); |
| 106 |
| 107 state1 = MemoryReducer::Step(state0, TimerEventHighFragmentation(0)); |
| 108 EXPECT_EQ(MemoryReducer::kDone, state1.action); |
| 109 |
| 110 state1 = MemoryReducer::Step( |
| 111 state0, TimerEventHighAllocationRateAndLowFragmentation(0)); |
| 112 EXPECT_EQ(MemoryReducer::kDone, state1.action); |
| 113 |
| 114 state1 = MemoryReducer::Step(state0, TimerEventPendingGC(0)); |
| 115 EXPECT_EQ(MemoryReducer::kDone, state1.action); |
| 116 |
| 117 state1 = MemoryReducer::Step(state0, ScavengeEvent(0)); |
| 118 EXPECT_EQ(MemoryReducer::kDone, state1.action); |
| 119 } |
| 120 |
| 121 |
| 122 TEST(MemoryReducer, FromDoneToWait) { |
| 123 if (!FLAG_incremental_marking) return; |
| 124 |
| 125 MemoryReducer::State state0(DoneState()), state1(DoneState()); |
| 126 |
| 127 state1 = MemoryReducer::Step(state0, MarkCompactEventGarbageLeft(0)); |
| 128 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 129 EXPECT_EQ(MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); |
| 130 EXPECT_EQ(0, state1.started_gcs); |
| 131 |
| 132 state1 = MemoryReducer::Step(state0, MarkCompactEventNoGarbageLeft(0)); |
| 133 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 134 EXPECT_EQ(MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); |
| 135 EXPECT_EQ(0, state1.started_gcs); |
| 136 |
| 137 state1 = MemoryReducer::Step(state0, ContextDisposedEvent(0)); |
| 138 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 139 EXPECT_EQ(MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); |
| 140 EXPECT_EQ(0, state1.started_gcs); |
| 141 } |
| 142 |
| 143 |
| 144 TEST(MemoryReducer, FromWaitToWait) { |
| 145 if (!FLAG_incremental_marking) return; |
| 146 |
| 147 MemoryReducer::State state0(WaitState(2, 1000.0)), state1(DoneState()); |
| 148 |
| 149 state1 = MemoryReducer::Step(state0, ContextDisposedEvent(2000)); |
| 150 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 151 EXPECT_EQ(state0.next_gc_start_ms, state1.next_gc_start_ms); |
| 152 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 153 |
| 154 state1 = MemoryReducer::Step( |
| 155 state0, TimerEventLowAllocationRate(state0.next_gc_start_ms - 1)); |
| 156 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 157 EXPECT_EQ(state0.next_gc_start_ms, state1.next_gc_start_ms); |
| 158 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 159 |
| 160 state1 = MemoryReducer::Step( |
| 161 state0, TimerEventHighFragmentation(state0.next_gc_start_ms - 1)); |
| 162 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 163 EXPECT_EQ(state0.next_gc_start_ms, state1.next_gc_start_ms); |
| 164 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 165 |
| 166 state1 = MemoryReducer::Step( |
| 167 state0, TimerEventHighAllocationRateAndLowFragmentation(2000)); |
| 168 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 169 EXPECT_EQ(2000 + MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); |
| 170 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 171 |
| 172 state1 = MemoryReducer::Step(state0, TimerEventPendingGC(2000)); |
| 173 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 174 EXPECT_EQ(2000 + MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); |
| 175 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 176 |
| 177 state1 = MemoryReducer::Step(state0, ScavengeEvent(2000)); |
| 178 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 179 EXPECT_EQ(2000 + MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); |
| 180 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 181 |
| 182 state1 = MemoryReducer::Step(state0, MarkCompactEventGarbageLeft(2000)); |
| 183 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 184 EXPECT_EQ(2000 + MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); |
| 185 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 186 |
| 187 state1 = MemoryReducer::Step(state0, MarkCompactEventNoGarbageLeft(2000)); |
| 188 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 189 EXPECT_EQ(2000 + MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); |
| 190 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 191 } |
| 192 |
| 193 |
| 194 TEST(MemoryReducer, FromWaitToRun) { |
| 195 if (!FLAG_incremental_marking) return; |
| 196 |
| 197 MemoryReducer::State state0(WaitState(0, 1000.0)), state1(DoneState()); |
| 198 |
| 199 state1 = MemoryReducer::Step( |
| 200 state0, TimerEventLowAllocationRate(state0.next_gc_start_ms + 1)); |
| 201 EXPECT_EQ(MemoryReducer::kRun, state1.action); |
| 202 EXPECT_EQ(0, state1.next_gc_start_ms); |
| 203 EXPECT_EQ(state0.started_gcs + 1, state1.started_gcs); |
| 204 |
| 205 state1 = MemoryReducer::Step( |
| 206 state0, TimerEventHighFragmentation(state0.next_gc_start_ms + 1)); |
| 207 EXPECT_EQ(MemoryReducer::kRun, state1.action); |
| 208 EXPECT_EQ(0, state1.next_gc_start_ms); |
| 209 EXPECT_EQ(state0.started_gcs + 1, state1.started_gcs); |
| 210 } |
| 211 |
| 212 |
| 213 TEST(MemoryReducer, FromRunToRun) { |
| 214 if (!FLAG_incremental_marking) return; |
| 215 |
| 216 MemoryReducer::State state0(RunState(1, 0.0)), state1(DoneState()); |
| 217 |
| 218 state1 = MemoryReducer::Step(state0, TimerEventLowAllocationRate(2000)); |
| 219 EXPECT_EQ(MemoryReducer::kRun, state1.action); |
| 220 EXPECT_EQ(state0.next_gc_start_ms, state1.next_gc_start_ms); |
| 221 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 222 |
| 223 state1 = MemoryReducer::Step(state0, TimerEventHighFragmentation(2000)); |
| 224 EXPECT_EQ(MemoryReducer::kRun, state1.action); |
| 225 EXPECT_EQ(state0.next_gc_start_ms, state1.next_gc_start_ms); |
| 226 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 227 |
| 228 state1 = MemoryReducer::Step( |
| 229 state0, TimerEventHighAllocationRateAndLowFragmentation(2000)); |
| 230 EXPECT_EQ(MemoryReducer::kRun, state1.action); |
| 231 EXPECT_EQ(state0.next_gc_start_ms, state1.next_gc_start_ms); |
| 232 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 233 |
| 234 state1 = MemoryReducer::Step(state0, TimerEventPendingGC(2000)); |
| 235 EXPECT_EQ(MemoryReducer::kRun, state1.action); |
| 236 EXPECT_EQ(state0.next_gc_start_ms, state1.next_gc_start_ms); |
| 237 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 238 |
| 239 state1 = MemoryReducer::Step(state0, ScavengeEvent(2000)); |
| 240 EXPECT_EQ(MemoryReducer::kRun, state1.action); |
| 241 EXPECT_EQ(state0.next_gc_start_ms, state1.next_gc_start_ms); |
| 242 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 243 |
| 244 state1 = MemoryReducer::Step(state0, ContextDisposedEvent(2000)); |
| 245 EXPECT_EQ(MemoryReducer::kRun, state1.action); |
| 246 EXPECT_EQ(state0.next_gc_start_ms, state1.next_gc_start_ms); |
| 247 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 248 } |
| 249 |
| 250 |
| 251 TEST(MemoryReducer, FromRunToDone) { |
| 252 if (!FLAG_incremental_marking) return; |
| 253 |
| 254 MemoryReducer::State state0(RunState(2, 0.0)), state1(DoneState()); |
| 255 |
| 256 state1 = MemoryReducer::Step(state0, MarkCompactEventNoGarbageLeft(2000)); |
| 257 EXPECT_EQ(MemoryReducer::kDone, state1.action); |
| 258 EXPECT_EQ(0, state1.next_gc_start_ms); |
| 259 EXPECT_EQ(0, state1.started_gcs); |
| 260 |
| 261 state0.started_gcs = MemoryReducer::kMaxNumberOfGCs; |
| 262 |
| 263 state1 = MemoryReducer::Step(state0, MarkCompactEventGarbageLeft(2000)); |
| 264 EXPECT_EQ(MemoryReducer::kDone, state1.action); |
| 265 EXPECT_EQ(0, state1.next_gc_start_ms); |
| 266 EXPECT_EQ(0, state1.started_gcs); |
| 267 } |
| 268 |
| 269 |
| 270 TEST(MemoryReducer, FromRunToWait) { |
| 271 if (!FLAG_incremental_marking) return; |
| 272 |
| 273 MemoryReducer::State state0(RunState(2, 0.0)), state1(DoneState()); |
| 274 |
| 275 state1 = MemoryReducer::Step(state0, MarkCompactEventGarbageLeft(2000)); |
| 276 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 277 EXPECT_EQ(2000 + MemoryReducer::kShortDelayMs, state1.next_gc_start_ms); |
| 278 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 279 |
| 280 state0.started_gcs = 1; |
| 281 |
| 282 state1 = MemoryReducer::Step(state0, MarkCompactEventNoGarbageLeft(2000)); |
| 283 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 284 EXPECT_EQ(2000 + MemoryReducer::kShortDelayMs, state1.next_gc_start_ms); |
| 285 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 286 } |
| 287 |
| 288 } // namespace internal |
| 289 } // namespace v8 |
OLD | NEW |