| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <limits> | 5 #include <limits> |
| 6 | 6 |
| 7 #include "src/flags.h" | 7 #include "src/flags.h" |
| 8 #include "src/heap/memory-reducer.h" | 8 #include "src/heap/memory-reducer.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace v8 { | 11 namespace v8 { |
| 12 namespace internal { | 12 namespace internal { |
| 13 | 13 |
| 14 MemoryReducer::State DoneState() { | 14 MemoryReducer::State DoneState() { |
| 15 return MemoryReducer::State(MemoryReducer::kDone, 0, 0.0, 1.0, 0); | 15 return MemoryReducer::State(MemoryReducer::kDone, 0, 0.0, 1.0); |
| 16 } | 16 } |
| 17 | 17 |
| 18 MemoryReducer::State DoneState(size_t committed_memory) { | |
| 19 return MemoryReducer::State(MemoryReducer::kDone, 0, 0.0, 1.0, | |
| 20 committed_memory); | |
| 21 } | |
| 22 | 18 |
| 23 MemoryReducer::State WaitState(int started_gcs, double next_gc_start_ms) { | 19 MemoryReducer::State WaitState(int started_gcs, double next_gc_start_ms) { |
| 24 return MemoryReducer::State(MemoryReducer::kWait, started_gcs, | 20 return MemoryReducer::State(MemoryReducer::kWait, started_gcs, |
| 25 next_gc_start_ms, 1.0, 0); | 21 next_gc_start_ms, 1.0); |
| 26 } | 22 } |
| 27 | 23 |
| 28 | 24 |
| 29 MemoryReducer::State RunState(int started_gcs, double next_gc_start_ms) { | 25 MemoryReducer::State RunState(int started_gcs, double next_gc_start_ms) { |
| 30 return MemoryReducer::State(MemoryReducer::kRun, started_gcs, | 26 return MemoryReducer::State(MemoryReducer::kRun, started_gcs, |
| 31 next_gc_start_ms, 1.0, 0); | 27 next_gc_start_ms, 1.0); |
| 32 } | 28 } |
| 33 | 29 |
| 34 MemoryReducer::Event MarkCompactEvent(double time_ms, | 30 MemoryReducer::Event MarkCompactEvent(double time_ms, |
| 35 bool next_gc_likely_to_collect_more, | 31 bool next_gc_likely_to_collect_more) { |
| 36 size_t committed_memory) { | |
| 37 MemoryReducer::Event event; | 32 MemoryReducer::Event event; |
| 38 event.type = MemoryReducer::kMarkCompact; | 33 event.type = MemoryReducer::kMarkCompact; |
| 39 event.time_ms = time_ms; | 34 event.time_ms = time_ms; |
| 40 event.next_gc_likely_to_collect_more = next_gc_likely_to_collect_more; | 35 event.next_gc_likely_to_collect_more = next_gc_likely_to_collect_more; |
| 41 event.committed_memory = committed_memory; | |
| 42 return event; | 36 return event; |
| 43 } | 37 } |
| 44 | 38 |
| 45 MemoryReducer::Event MarkCompactEventGarbageLeft(double time_ms, | 39 MemoryReducer::Event MarkCompactEventGarbageLeft(double time_ms) { |
| 46 size_t committed_memory) { | 40 return MarkCompactEvent(time_ms, true); |
| 47 return MarkCompactEvent(time_ms, true, committed_memory); | |
| 48 } | 41 } |
| 49 | 42 |
| 50 MemoryReducer::Event MarkCompactEventNoGarbageLeft(double time_ms, | 43 MemoryReducer::Event MarkCompactEventNoGarbageLeft(double time_ms) { |
| 51 size_t committed_memory) { | 44 return MarkCompactEvent(time_ms, false); |
| 52 return MarkCompactEvent(time_ms, false, committed_memory); | |
| 53 } | 45 } |
| 54 | 46 |
| 55 | 47 |
| 56 MemoryReducer::Event TimerEvent(double time_ms, | 48 MemoryReducer::Event TimerEvent(double time_ms, |
| 57 bool should_start_incremental_gc, | 49 bool should_start_incremental_gc, |
| 58 bool can_start_incremental_gc) { | 50 bool can_start_incremental_gc) { |
| 59 MemoryReducer::Event event; | 51 MemoryReducer::Event event; |
| 60 event.type = MemoryReducer::kTimer; | 52 event.type = MemoryReducer::kTimer; |
| 61 event.time_ms = time_ms; | 53 event.time_ms = time_ms; |
| 62 event.should_start_incremental_gc = should_start_incremental_gc; | 54 event.should_start_incremental_gc = should_start_incremental_gc; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 91 MemoryReducer::State state0(DoneState()), state1(DoneState()); | 83 MemoryReducer::State state0(DoneState()), state1(DoneState()); |
| 92 | 84 |
| 93 state1 = MemoryReducer::Step(state0, TimerEventLowAllocationRate(0)); | 85 state1 = MemoryReducer::Step(state0, TimerEventLowAllocationRate(0)); |
| 94 EXPECT_EQ(MemoryReducer::kDone, state1.action); | 86 EXPECT_EQ(MemoryReducer::kDone, state1.action); |
| 95 | 87 |
| 96 state1 = MemoryReducer::Step(state0, TimerEventHighAllocationRate(0)); | 88 state1 = MemoryReducer::Step(state0, TimerEventHighAllocationRate(0)); |
| 97 EXPECT_EQ(MemoryReducer::kDone, state1.action); | 89 EXPECT_EQ(MemoryReducer::kDone, state1.action); |
| 98 | 90 |
| 99 state1 = MemoryReducer::Step(state0, TimerEventPendingGC(0)); | 91 state1 = MemoryReducer::Step(state0, TimerEventPendingGC(0)); |
| 100 EXPECT_EQ(MemoryReducer::kDone, state1.action); | 92 EXPECT_EQ(MemoryReducer::kDone, state1.action); |
| 101 | |
| 102 state1 = MemoryReducer::Step( | |
| 103 state0, | |
| 104 MarkCompactEventGarbageLeft(0, MemoryReducer::kCommittedMemoryDelta - 1)); | |
| 105 EXPECT_EQ(MemoryReducer::kDone, state1.action); | |
| 106 | |
| 107 state0 = DoneState(1000 * MB); | |
| 108 state1 = MemoryReducer::Step( | |
| 109 state0, MarkCompactEventGarbageLeft( | |
| 110 0, static_cast<size_t>( | |
| 111 1000 * MB * MemoryReducer::kCommittedMemoryFactor) - | |
| 112 1)); | |
| 113 EXPECT_EQ(MemoryReducer::kDone, state1.action); | |
| 114 } | 93 } |
| 115 | 94 |
| 116 | 95 |
| 117 TEST(MemoryReducer, FromDoneToWait) { | 96 TEST(MemoryReducer, FromDoneToWait) { |
| 118 if (!FLAG_incremental_marking) return; | 97 if (!FLAG_incremental_marking) return; |
| 119 | 98 |
| 120 MemoryReducer::State state0(DoneState()), state1(DoneState()); | 99 MemoryReducer::State state0(DoneState()), state1(DoneState()); |
| 121 | 100 |
| 122 state1 = MemoryReducer::Step( | 101 state1 = MemoryReducer::Step(state0, MarkCompactEventGarbageLeft(2)); |
| 123 state0, | |
| 124 MarkCompactEventGarbageLeft(2, MemoryReducer::kCommittedMemoryDelta)); | |
| 125 EXPECT_EQ(MemoryReducer::kWait, state1.action); | 102 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 126 EXPECT_EQ(MemoryReducer::kLongDelayMs + 2, state1.next_gc_start_ms); | 103 EXPECT_EQ(MemoryReducer::kLongDelayMs + 2, state1.next_gc_start_ms); |
| 127 EXPECT_EQ(0, state1.started_gcs); | 104 EXPECT_EQ(0, state1.started_gcs); |
| 128 EXPECT_EQ(2, state1.last_gc_time_ms); | 105 EXPECT_EQ(2, state1.last_gc_time_ms); |
| 129 | 106 |
| 130 state1 = MemoryReducer::Step( | 107 state1 = MemoryReducer::Step(state0, MarkCompactEventNoGarbageLeft(2)); |
| 131 state0, | |
| 132 MarkCompactEventNoGarbageLeft(2, MemoryReducer::kCommittedMemoryDelta)); | |
| 133 EXPECT_EQ(MemoryReducer::kWait, state1.action); | 108 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 134 EXPECT_EQ(MemoryReducer::kLongDelayMs + 2, state1.next_gc_start_ms); | 109 EXPECT_EQ(MemoryReducer::kLongDelayMs + 2, state1.next_gc_start_ms); |
| 135 EXPECT_EQ(0, state1.started_gcs); | 110 EXPECT_EQ(0, state1.started_gcs); |
| 136 EXPECT_EQ(2, state1.last_gc_time_ms); | 111 EXPECT_EQ(2, state1.last_gc_time_ms); |
| 137 | 112 |
| 138 state1 = MemoryReducer::Step(state0, PossibleGarbageEvent(0)); | 113 state1 = MemoryReducer::Step(state0, PossibleGarbageEvent(0)); |
| 139 EXPECT_EQ(MemoryReducer::kWait, state1.action); | 114 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 140 EXPECT_EQ(MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); | 115 EXPECT_EQ(MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); |
| 141 EXPECT_EQ(0, state1.started_gcs); | 116 EXPECT_EQ(0, state1.started_gcs); |
| 142 EXPECT_EQ(state0.last_gc_time_ms, state1.last_gc_time_ms); | 117 EXPECT_EQ(state0.last_gc_time_ms, state1.last_gc_time_ms); |
| 143 | |
| 144 state0 = DoneState(1000 * MB); | |
| 145 state1 = MemoryReducer::Step( | |
| 146 state0, MarkCompactEventGarbageLeft( | |
| 147 2, static_cast<size_t>( | |
| 148 1000 * MB * MemoryReducer::kCommittedMemoryFactor))); | |
| 149 EXPECT_EQ(MemoryReducer::kWait, state1.action); | |
| 150 EXPECT_EQ(MemoryReducer::kLongDelayMs + 2, state1.next_gc_start_ms); | |
| 151 EXPECT_EQ(0, state1.started_gcs); | |
| 152 EXPECT_EQ(2, state1.last_gc_time_ms); | |
| 153 } | 118 } |
| 154 | 119 |
| 155 | 120 |
| 156 TEST(MemoryReducer, FromWaitToWait) { | 121 TEST(MemoryReducer, FromWaitToWait) { |
| 157 if (!FLAG_incremental_marking) return; | 122 if (!FLAG_incremental_marking) return; |
| 158 | 123 |
| 159 MemoryReducer::State state0(WaitState(2, 1000.0)), state1(DoneState()); | 124 MemoryReducer::State state0(WaitState(2, 1000.0)), state1(DoneState()); |
| 160 | 125 |
| 161 state1 = MemoryReducer::Step(state0, PossibleGarbageEvent(2000)); | 126 state1 = MemoryReducer::Step(state0, PossibleGarbageEvent(2000)); |
| 162 EXPECT_EQ(MemoryReducer::kWait, state1.action); | 127 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 163 EXPECT_EQ(state0.next_gc_start_ms, state1.next_gc_start_ms); | 128 EXPECT_EQ(state0.next_gc_start_ms, state1.next_gc_start_ms); |
| 164 EXPECT_EQ(state0.started_gcs, state1.started_gcs); | 129 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 165 | 130 |
| 166 state1 = MemoryReducer::Step( | 131 state1 = MemoryReducer::Step( |
| 167 state0, TimerEventLowAllocationRate(state0.next_gc_start_ms - 1)); | 132 state0, TimerEventLowAllocationRate(state0.next_gc_start_ms - 1)); |
| 168 EXPECT_EQ(MemoryReducer::kWait, state1.action); | 133 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 169 EXPECT_EQ(state0.next_gc_start_ms, state1.next_gc_start_ms); | 134 EXPECT_EQ(state0.next_gc_start_ms, state1.next_gc_start_ms); |
| 170 EXPECT_EQ(state0.started_gcs, state1.started_gcs); | 135 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 171 | 136 |
| 172 state1 = MemoryReducer::Step(state0, TimerEventHighAllocationRate(2000)); | 137 state1 = MemoryReducer::Step(state0, TimerEventHighAllocationRate(2000)); |
| 173 EXPECT_EQ(MemoryReducer::kWait, state1.action); | 138 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 174 EXPECT_EQ(2000 + MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); | 139 EXPECT_EQ(2000 + MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); |
| 175 EXPECT_EQ(state0.started_gcs, state1.started_gcs); | 140 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 176 | 141 |
| 177 state1 = MemoryReducer::Step(state0, TimerEventPendingGC(2000)); | 142 state1 = MemoryReducer::Step(state0, TimerEventPendingGC(2000)); |
| 178 EXPECT_EQ(MemoryReducer::kWait, state1.action); | 143 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 179 EXPECT_EQ(2000 + MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); | 144 EXPECT_EQ(2000 + MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); |
| 180 EXPECT_EQ(state0.started_gcs, state1.started_gcs); | 145 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 181 | 146 |
| 182 state1 = MemoryReducer::Step(state0, MarkCompactEventGarbageLeft(2000, 0)); | 147 state1 = MemoryReducer::Step(state0, MarkCompactEventGarbageLeft(2000)); |
| 183 EXPECT_EQ(MemoryReducer::kWait, state1.action); | 148 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 184 EXPECT_EQ(2000 + MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); | 149 EXPECT_EQ(2000 + MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); |
| 185 EXPECT_EQ(state0.started_gcs, state1.started_gcs); | 150 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 186 EXPECT_EQ(2000, state1.last_gc_time_ms); | 151 EXPECT_EQ(2000, state1.last_gc_time_ms); |
| 187 | 152 |
| 188 state1 = MemoryReducer::Step(state0, MarkCompactEventNoGarbageLeft(2000, 0)); | 153 state1 = MemoryReducer::Step(state0, MarkCompactEventNoGarbageLeft(2000)); |
| 189 EXPECT_EQ(MemoryReducer::kWait, state1.action); | 154 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 190 EXPECT_EQ(2000 + MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); | 155 EXPECT_EQ(2000 + MemoryReducer::kLongDelayMs, state1.next_gc_start_ms); |
| 191 EXPECT_EQ(state0.started_gcs, state1.started_gcs); | 156 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 192 EXPECT_EQ(2000, state1.last_gc_time_ms); | 157 EXPECT_EQ(2000, state1.last_gc_time_ms); |
| 193 | 158 |
| 194 state0.last_gc_time_ms = 0; | 159 state0.last_gc_time_ms = 0; |
| 195 state1 = MemoryReducer::Step( | 160 state1 = MemoryReducer::Step( |
| 196 state0, | 161 state0, |
| 197 TimerEventHighAllocationRate(MemoryReducer::kWatchdogDelayMs + 1)); | 162 TimerEventHighAllocationRate(MemoryReducer::kWatchdogDelayMs + 1)); |
| 198 EXPECT_EQ(MemoryReducer::kWait, state1.action); | 163 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 EXPECT_EQ(state0.started_gcs, state1.started_gcs); | 252 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 288 EXPECT_EQ(state0.last_gc_time_ms, state1.last_gc_time_ms); | 253 EXPECT_EQ(state0.last_gc_time_ms, state1.last_gc_time_ms); |
| 289 } | 254 } |
| 290 | 255 |
| 291 | 256 |
| 292 TEST(MemoryReducer, FromRunToDone) { | 257 TEST(MemoryReducer, FromRunToDone) { |
| 293 if (!FLAG_incremental_marking) return; | 258 if (!FLAG_incremental_marking) return; |
| 294 | 259 |
| 295 MemoryReducer::State state0(RunState(2, 0.0)), state1(DoneState()); | 260 MemoryReducer::State state0(RunState(2, 0.0)), state1(DoneState()); |
| 296 | 261 |
| 297 state1 = MemoryReducer::Step(state0, MarkCompactEventNoGarbageLeft(2000, 0)); | 262 state1 = MemoryReducer::Step(state0, MarkCompactEventNoGarbageLeft(2000)); |
| 298 EXPECT_EQ(MemoryReducer::kDone, state1.action); | 263 EXPECT_EQ(MemoryReducer::kDone, state1.action); |
| 299 EXPECT_EQ(0, state1.next_gc_start_ms); | 264 EXPECT_EQ(0, state1.next_gc_start_ms); |
| 300 EXPECT_EQ(MemoryReducer::kMaxNumberOfGCs, state1.started_gcs); | 265 EXPECT_EQ(MemoryReducer::kMaxNumberOfGCs, state1.started_gcs); |
| 301 EXPECT_EQ(2000, state1.last_gc_time_ms); | 266 EXPECT_EQ(2000, state1.last_gc_time_ms); |
| 302 | 267 |
| 303 state0.started_gcs = MemoryReducer::kMaxNumberOfGCs; | 268 state0.started_gcs = MemoryReducer::kMaxNumberOfGCs; |
| 304 | 269 |
| 305 state1 = MemoryReducer::Step(state0, MarkCompactEventGarbageLeft(2000, 0)); | 270 state1 = MemoryReducer::Step(state0, MarkCompactEventGarbageLeft(2000)); |
| 306 EXPECT_EQ(MemoryReducer::kDone, state1.action); | 271 EXPECT_EQ(MemoryReducer::kDone, state1.action); |
| 307 EXPECT_EQ(0, state1.next_gc_start_ms); | 272 EXPECT_EQ(0, state1.next_gc_start_ms); |
| 308 EXPECT_EQ(2000, state1.last_gc_time_ms); | 273 EXPECT_EQ(2000, state1.last_gc_time_ms); |
| 309 } | 274 } |
| 310 | 275 |
| 311 | 276 |
| 312 TEST(MemoryReducer, FromRunToWait) { | 277 TEST(MemoryReducer, FromRunToWait) { |
| 313 if (!FLAG_incremental_marking) return; | 278 if (!FLAG_incremental_marking) return; |
| 314 | 279 |
| 315 MemoryReducer::State state0(RunState(2, 0.0)), state1(DoneState()); | 280 MemoryReducer::State state0(RunState(2, 0.0)), state1(DoneState()); |
| 316 | 281 |
| 317 state1 = MemoryReducer::Step(state0, MarkCompactEventGarbageLeft(2000, 0)); | 282 state1 = MemoryReducer::Step(state0, MarkCompactEventGarbageLeft(2000)); |
| 318 EXPECT_EQ(MemoryReducer::kWait, state1.action); | 283 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 319 EXPECT_EQ(2000 + MemoryReducer::kShortDelayMs, state1.next_gc_start_ms); | 284 EXPECT_EQ(2000 + MemoryReducer::kShortDelayMs, state1.next_gc_start_ms); |
| 320 EXPECT_EQ(state0.started_gcs, state1.started_gcs); | 285 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 321 EXPECT_EQ(2000, state1.last_gc_time_ms); | 286 EXPECT_EQ(2000, state1.last_gc_time_ms); |
| 322 | 287 |
| 323 state0.started_gcs = 1; | 288 state0.started_gcs = 1; |
| 324 | 289 |
| 325 state1 = MemoryReducer::Step(state0, MarkCompactEventNoGarbageLeft(2000, 0)); | 290 state1 = MemoryReducer::Step(state0, MarkCompactEventNoGarbageLeft(2000)); |
| 326 EXPECT_EQ(MemoryReducer::kWait, state1.action); | 291 EXPECT_EQ(MemoryReducer::kWait, state1.action); |
| 327 EXPECT_EQ(2000 + MemoryReducer::kShortDelayMs, state1.next_gc_start_ms); | 292 EXPECT_EQ(2000 + MemoryReducer::kShortDelayMs, state1.next_gc_start_ms); |
| 328 EXPECT_EQ(state0.started_gcs, state1.started_gcs); | 293 EXPECT_EQ(state0.started_gcs, state1.started_gcs); |
| 329 EXPECT_EQ(2000, state1.last_gc_time_ms); | 294 EXPECT_EQ(2000, state1.last_gc_time_ms); |
| 330 } | 295 } |
| 331 | 296 |
| 332 } // namespace internal | 297 } // namespace internal |
| 333 } // namespace v8 | 298 } // namespace v8 |
| OLD | NEW |