LCOV - code coverage report
Current view: top level - src/heap - memory-reducer.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 77 90 85.6 %
Date: 2017-10-20 Functions: 7 9 77.8 %

          Line data    Source code
       1             : // Copyright 2015 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 "src/heap/memory-reducer.h"
       6             : 
       7             : #include "src/flags.h"
       8             : #include "src/heap/gc-tracer.h"
       9             : #include "src/heap/heap-inl.h"
      10             : #include "src/utils.h"
      11             : #include "src/v8.h"
      12             : 
      13             : namespace v8 {
      14             : namespace internal {
      15             : 
      16             : const int MemoryReducer::kLongDelayMs = 8000;
      17             : const int MemoryReducer::kShortDelayMs = 500;
      18             : const int MemoryReducer::kWatchdogDelayMs = 100000;
      19             : const int MemoryReducer::kMaxNumberOfGCs = 3;
      20             : const double MemoryReducer::kCommittedMemoryFactor = 1.1;
      21             : const size_t MemoryReducer::kCommittedMemoryDelta = 10 * MB;
      22             : 
      23         405 : MemoryReducer::TimerTask::TimerTask(MemoryReducer* memory_reducer)
      24             :     : CancelableTask(memory_reducer->heap()->isolate()),
      25         405 :       memory_reducer_(memory_reducer) {}
      26             : 
      27             : 
      28           9 : void MemoryReducer::TimerTask::RunInternal() {
      29          27 :   Heap* heap = memory_reducer_->heap();
      30             :   Event event;
      31           9 :   double time_ms = heap->MonotonicallyIncreasingTimeInMs();
      32             :   heap->tracer()->SampleAllocation(time_ms, heap->NewSpaceAllocationCounter(),
      33           9 :                                    heap->OldGenerationAllocationCounter());
      34           9 :   bool low_allocation_rate = heap->HasLowAllocationRate();
      35           9 :   bool optimize_for_memory = heap->ShouldOptimizeForMemoryUsage();
      36           9 :   if (FLAG_trace_gc_verbose) {
      37             :     heap->isolate()->PrintWithTimestamp(
      38             :         "Memory reducer: %s, %s\n",
      39             :         low_allocation_rate ? "low alloc" : "high alloc",
      40           0 :         optimize_for_memory ? "background" : "foreground");
      41             :   }
      42           9 :   event.type = kTimer;
      43           9 :   event.time_ms = time_ms;
      44             :   // The memory reducer will start incremental markig if
      45             :   // 1) mutator is likely idle: js call rate is low and allocation rate is low.
      46             :   // 2) mutator is in background: optimize for memory flag is set.
      47             :   event.should_start_incremental_gc =
      48           9 :       low_allocation_rate || optimize_for_memory;
      49             :   event.can_start_incremental_gc =
      50          13 :       heap->incremental_marking()->IsStopped() &&
      51           4 :       (heap->incremental_marking()->CanBeActivated() || optimize_for_memory);
      52           9 :   event.committed_memory = heap->CommittedOldGenerationMemory();
      53           9 :   memory_reducer_->NotifyTimer(event);
      54           9 : }
      55             : 
      56             : 
      57          18 : void MemoryReducer::NotifyTimer(const Event& event) {
      58             :   DCHECK_EQ(kTimer, event.type);
      59             :   DCHECK_EQ(kWait, state_.action);
      60           9 :   state_ = Step(state_, event);
      61           9 :   if (state_.action == kRun) {
      62             :     DCHECK(heap()->incremental_marking()->IsStopped());
      63             :     DCHECK(FLAG_incremental_marking);
      64           0 :     if (FLAG_trace_gc_verbose) {
      65             :       heap()->isolate()->PrintWithTimestamp("Memory reducer: started GC #%d\n",
      66           0 :                                             state_.started_gcs);
      67             :     }
      68             :     heap()->StartIdleIncrementalMarking(
      69             :         GarbageCollectionReason::kMemoryReducer,
      70           0 :         kGCCallbackFlagCollectAllExternalMemory);
      71           9 :   } else if (state_.action == kWait) {
      72          23 :     if (!heap()->incremental_marking()->IsStopped() &&
      73           5 :         heap()->ShouldOptimizeForMemoryUsage()) {
      74             :       // Make progress with pending incremental marking if memory usage has
      75             :       // higher priority than latency. This is important for background tabs
      76             :       // that do not send idle notifications.
      77             :       const int kIncrementalMarkingDelayMs = 500;
      78           0 :       double deadline = heap()->MonotonicallyIncreasingTimeInMs() +
      79           0 :                         kIncrementalMarkingDelayMs;
      80             :       heap()->incremental_marking()->AdvanceIncrementalMarking(
      81             :           deadline, IncrementalMarking::NO_GC_VIA_STACK_GUARD,
      82           0 :           StepOrigin::kTask);
      83             :       heap()->FinalizeIncrementalMarkingIfComplete(
      84           0 :           GarbageCollectionReason::kFinalizeMarkingViaTask);
      85             :     }
      86             :     // Re-schedule the timer.
      87           9 :     ScheduleTimer(event.time_ms, state_.next_gc_start_ms - event.time_ms);
      88           9 :     if (FLAG_trace_gc_verbose) {
      89             :       heap()->isolate()->PrintWithTimestamp(
      90             :           "Memory reducer: waiting for %.f ms\n",
      91           0 :           state_.next_gc_start_ms - event.time_ms);
      92             :     }
      93             :   }
      94           9 : }
      95             : 
      96             : 
      97       56800 : void MemoryReducer::NotifyMarkCompact(const Event& event) {
      98             :   DCHECK_EQ(kMarkCompact, event.type);
      99       56800 :   Action old_action = state_.action;
     100       56800 :   state_ = Step(state_, event);
     101       56800 :   if (old_action != kWait && state_.action == kWait) {
     102             :     // If we are transitioning to the WAIT state, start the timer.
     103         217 :     ScheduleTimer(event.time_ms, state_.next_gc_start_ms - event.time_ms);
     104             :   }
     105       56800 :   if (old_action == kRun) {
     106           0 :     if (FLAG_trace_gc_verbose) {
     107             :       heap()->isolate()->PrintWithTimestamp(
     108             :           "Memory reducer: finished GC #%d (%s)\n", state_.started_gcs,
     109           0 :           state_.action == kWait ? "will do more" : "done");
     110             :     }
     111             :   }
     112       56800 : }
     113             : 
     114        1387 : void MemoryReducer::NotifyPossibleGarbage(const Event& event) {
     115             :   DCHECK_EQ(kPossibleGarbage, event.type);
     116        1387 :   Action old_action = state_.action;
     117        1387 :   state_ = Step(state_, event);
     118        1387 :   if (old_action != kWait && state_.action == kWait) {
     119             :     // If we are transitioning to the WAIT state, start the timer.
     120         179 :     ScheduleTimer(event.time_ms, state_.next_gc_start_ms - event.time_ms);
     121             :   }
     122        1387 : }
     123             : 
     124             : 
     125           0 : bool MemoryReducer::WatchdogGC(const State& state, const Event& event) {
     126          15 :   return state.last_gc_time_ms != 0 &&
     127           7 :          event.time_ms > state.last_gc_time_ms + kWatchdogDelayMs;
     128             : }
     129             : 
     130             : 
     131             : // For specification of this function see the comment for MemoryReducer class.
     132       58226 : MemoryReducer::State MemoryReducer::Step(const State& state,
     133             :                                          const Event& event) {
     134       58226 :   if (!FLAG_incremental_marking || !FLAG_memory_reducer) {
     135         328 :     return State(kDone, 0, 0, state.last_gc_time_ms, 0);
     136             :   }
     137       57898 :   switch (state.action) {
     138             :     case kDone:
     139       55652 :       if (event.type == kTimer) {
     140           3 :         return state;
     141       55649 :       } else if (event.type == kMarkCompact) {
     142      110938 :         if (event.committed_memory <
     143       55469 :             Max(static_cast<size_t>(state.committed_memory_at_last_run *
     144             :                                     kCommittedMemoryFactor),
     145      110938 :                 state.committed_memory_at_last_run + kCommittedMemoryDelta)) {
     146       55249 :           return state;
     147             :         } else {
     148             :           return State(kWait, 0, event.time_ms + kLongDelayMs,
     149             :                        event.type == kMarkCompact ? event.time_ms
     150             :                                                   : state.last_gc_time_ms,
     151         220 :                        0);
     152             :         }
     153             :       } else {
     154             :         DCHECK_EQ(kPossibleGarbage, event.type);
     155             :         return State(
     156             :             kWait, 0, event.time_ms + kLongDelayMs,
     157             :             event.type == kMarkCompact ? event.time_ms : state.last_gc_time_ms,
     158         180 :             0);
     159             :       }
     160             :     case kWait:
     161        2238 :       switch (event.type) {
     162             :         case kPossibleGarbage:
     163        1204 :           return state;
     164             :         case kTimer:
     165          19 :           if (state.started_gcs >= kMaxNumberOfGCs) {
     166             :             return State(kDone, kMaxNumberOfGCs, 0.0, state.last_gc_time_ms,
     167           3 :                          event.committed_memory);
     168          32 :           } else if (event.can_start_incremental_gc &&
     169           8 :                      (event.should_start_incremental_gc ||
     170             :                       WatchdogGC(state, event))) {
     171           3 :             if (state.next_gc_start_ms <= event.time_ms) {
     172             :               return State(kRun, state.started_gcs + 1, 0.0,
     173           2 :                            state.last_gc_time_ms, 0);
     174             :             } else {
     175           1 :               return state;
     176             :             }
     177             :           } else {
     178             :             return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs,
     179          13 :                          state.last_gc_time_ms, 0);
     180             :           }
     181             :         case kMarkCompact:
     182             :           return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs,
     183        1015 :                        event.time_ms, 0);
     184             :       }
     185             :     case kRun:
     186           8 :       if (event.type != kMarkCompact) {
     187           4 :         return state;
     188             :       } else {
     189           4 :         if (state.started_gcs < kMaxNumberOfGCs &&
     190           2 :             (event.next_gc_likely_to_collect_more || state.started_gcs == 1)) {
     191             :           return State(kWait, state.started_gcs, event.time_ms + kShortDelayMs,
     192           2 :                        event.time_ms, 0);
     193             :         } else {
     194             :           return State(kDone, kMaxNumberOfGCs, 0.0, event.time_ms,
     195           2 :                        event.committed_memory);
     196             :         }
     197             :       }
     198             :   }
     199           0 :   UNREACHABLE();
     200             : }
     201             : 
     202             : 
     203         405 : void MemoryReducer::ScheduleTimer(double time_ms, double delay_ms) {
     204             :   DCHECK_LT(0, delay_ms);
     205             :   // Leave some room for precision error in task scheduler.
     206             :   const double kSlackMs = 100;
     207             :   v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate());
     208         405 :   auto timer_task = new MemoryReducer::TimerTask(this);
     209         405 :   V8::GetCurrentPlatform()->CallDelayedOnForegroundThread(
     210         405 :       isolate, timer_task, (delay_ms + kSlackMs) / 1000.0);
     211         405 : }
     212             : 
     213       53365 : void MemoryReducer::TearDown() { state_ = State(kDone, 0, 0, 0.0, 0); }
     214             : 
     215             : }  // namespace internal
     216             : }  // namespace v8

Generated by: LCOV version 1.10