1014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch// Copyright 2015 the V8 project authors. All rights reserved.
2014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch// found in the LICENSE file.
4014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
5014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/heap/memory-reducer.h"
6014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
7014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/flags.h"
8014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/heap/gc-tracer.h"
9014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/heap/heap-inl.h"
10014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/utils.h"
11014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/v8.h"
12014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
13014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochnamespace v8 {
14014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochnamespace internal {
15014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
16014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochconst int MemoryReducer::kLongDelayMs = 8000;
17014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochconst int MemoryReducer::kShortDelayMs = 500;
18014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochconst int MemoryReducer::kWatchdogDelayMs = 100000;
19014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochconst int MemoryReducer::kMaxNumberOfGCs = 3;
20014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
21014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochMemoryReducer::TimerTask::TimerTask(MemoryReducer* memory_reducer)
22014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    : CancelableTask(memory_reducer->heap()->isolate()),
23014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      memory_reducer_(memory_reducer) {}
24014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
25014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
26014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochvoid MemoryReducer::TimerTask::RunInternal() {
27014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  const double kJsCallsPerMsThreshold = 0.5;
28014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Heap* heap = memory_reducer_->heap();
29014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Event event;
30014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  double time_ms = heap->MonotonicallyIncreasingTimeInMs();
31014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  heap->tracer()->SampleAllocation(time_ms, heap->NewSpaceAllocationCounter(),
32014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                   heap->OldGenerationAllocationCounter());
33014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  double js_call_rate = memory_reducer_->SampleAndGetJsCallsPerMs(time_ms);
34014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  bool low_allocation_rate = heap->HasLowAllocationRate();
35014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  bool is_idle = js_call_rate < kJsCallsPerMsThreshold && low_allocation_rate;
36014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  bool optimize_for_memory = heap->ShouldOptimizeForMemoryUsage();
37014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (FLAG_trace_gc_verbose) {
38014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    PrintIsolate(heap->isolate(), "Memory reducer: call rate %.3lf, %s, %s\n",
39014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                 js_call_rate, low_allocation_rate ? "low alloc" : "high alloc",
40014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                 optimize_for_memory ? "background" : "foreground");
41014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
42014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  event.type = kTimer;
43014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  event.time_ms = time_ms;
44014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // The memory reducer will start incremental markig if
45014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // 1) mutator is likely idle: js call rate is low and allocation rate is low.
46014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // 2) mutator is in background: optimize for memory flag is set.
47014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  event.should_start_incremental_gc = is_idle || optimize_for_memory;
48014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  event.can_start_incremental_gc =
49014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      heap->incremental_marking()->IsStopped() &&
50342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch      (heap->incremental_marking()->CanBeActivated() || optimize_for_memory);
51014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  memory_reducer_->NotifyTimer(event);
52014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
53014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
54014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
55014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochdouble MemoryReducer::SampleAndGetJsCallsPerMs(double time_ms) {
56014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  unsigned int counter = heap()->isolate()->js_calls_from_api_counter();
57014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  unsigned int call_delta = counter - js_calls_counter_;
58014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  double time_delta_ms = time_ms - js_calls_sample_time_ms_;
59014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  js_calls_counter_ = counter;
60014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  js_calls_sample_time_ms_ = time_ms;
61014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return time_delta_ms > 0 ? call_delta / time_delta_ms : 0;
62014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
63014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
64014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
65014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochvoid MemoryReducer::NotifyTimer(const Event& event) {
66014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  DCHECK_EQ(kTimer, event.type);
67014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  DCHECK_EQ(kWait, state_.action);
68014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  state_ = Step(state_, event);
69014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (state_.action == kRun) {
70014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    DCHECK(heap()->incremental_marking()->IsStopped());
71014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    DCHECK(FLAG_incremental_marking);
72014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    if (FLAG_trace_gc_verbose) {
73014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      PrintIsolate(heap()->isolate(), "Memory reducer: started GC #%d\n",
74014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                   state_.started_gcs);
75014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    }
76014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    heap()->StartIdleIncrementalMarking();
77014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  } else if (state_.action == kWait) {
78014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    if (!heap()->incremental_marking()->IsStopped() &&
79014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        heap()->ShouldOptimizeForMemoryUsage()) {
80014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      // Make progress with pending incremental marking if memory usage has
81014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      // higher priority than latency. This is important for background tabs
82014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      // that do not send idle notifications.
83014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      const int kIncrementalMarkingDelayMs = 500;
84014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      double deadline = heap()->MonotonicallyIncreasingTimeInMs() +
85014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                        kIncrementalMarkingDelayMs;
86014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      heap()->incremental_marking()->AdvanceIncrementalMarking(
871b268ca467c924004286c97bac133db489cf43d0Ben Murdoch          deadline, i::IncrementalMarking::StepActions(
881b268ca467c924004286c97bac133db489cf43d0Ben Murdoch                        i::IncrementalMarking::NO_GC_VIA_STACK_GUARD,
891b268ca467c924004286c97bac133db489cf43d0Ben Murdoch                        i::IncrementalMarking::FORCE_MARKING,
901b268ca467c924004286c97bac133db489cf43d0Ben Murdoch                        i::IncrementalMarking::FORCE_COMPLETION));
91014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      heap()->FinalizeIncrementalMarkingIfComplete(
92014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch          "Memory reducer: finalize incremental marking");
93014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    }
94014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // Re-schedule the timer.
95014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    ScheduleTimer(event.time_ms, state_.next_gc_start_ms - event.time_ms);
96014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    if (FLAG_trace_gc_verbose) {
97014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      PrintIsolate(heap()->isolate(), "Memory reducer: waiting for %.f ms\n",
98014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                   state_.next_gc_start_ms - event.time_ms);
99014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    }
100014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
101014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
102014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
103014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
104014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochvoid MemoryReducer::NotifyMarkCompact(const Event& event) {
105014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  DCHECK_EQ(kMarkCompact, event.type);
106014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Action old_action = state_.action;
107014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  state_ = Step(state_, event);
108014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (old_action != kWait && state_.action == kWait) {
109014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // If we are transitioning to the WAIT state, start the timer.
110014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    ScheduleTimer(event.time_ms, state_.next_gc_start_ms - event.time_ms);
111014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
112014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (old_action == kRun) {
113014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    if (FLAG_trace_gc_verbose) {
114014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      PrintIsolate(heap()->isolate(), "Memory reducer: finished GC #%d (%s)\n",
115014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                   state_.started_gcs,
116014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                   state_.action == kWait ? "will do more" : "done");
117014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    }
118014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
119014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
120014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
121342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdochvoid MemoryReducer::NotifyPossibleGarbage(const Event& event) {
122342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  DCHECK_EQ(kPossibleGarbage, event.type);
123014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Action old_action = state_.action;
124014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  state_ = Step(state_, event);
125014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (old_action != kWait && state_.action == kWait) {
126014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    // If we are transitioning to the WAIT state, start the timer.
127014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    ScheduleTimer(event.time_ms, state_.next_gc_start_ms - event.time_ms);
128014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
129014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
130014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
131014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
132014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochbool MemoryReducer::WatchdogGC(const State& state, const Event& event) {
133014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return state.last_gc_time_ms != 0 &&
134014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch         event.time_ms > state.last_gc_time_ms + kWatchdogDelayMs;
135014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
136014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
137014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
138014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch// For specification of this function see the comment for MemoryReducer class.
139014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochMemoryReducer::State MemoryReducer::Step(const State& state,
140014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                         const Event& event) {
141014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (!FLAG_incremental_marking || !FLAG_memory_reducer) {
142014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    return State(kDone, 0, 0, state.last_gc_time_ms);
143014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
144014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  switch (state.action) {
145014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case kDone:
146014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      if (event.type == kTimer) {
147014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        return state;
148014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      } else {
149342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch        DCHECK(event.type == kPossibleGarbage || event.type == kMarkCompact);
150014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        return State(
151014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch            kWait, 0, event.time_ms + kLongDelayMs,
152014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch            event.type == kMarkCompact ? event.time_ms : state.last_gc_time_ms);
153014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      }
154014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case kWait:
155014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      switch (event.type) {
156342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch        case kPossibleGarbage:
157014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch          return state;
158014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        case kTimer:
159014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch          if (state.started_gcs >= kMaxNumberOfGCs) {
160014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch            return State(kDone, kMaxNumberOfGCs, 0.0, state.last_gc_time_ms);
161014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch          } else if (event.can_start_incremental_gc &&
162014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                     (event.should_start_incremental_gc ||
163014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                      WatchdogGC(state, event))) {
164014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch            if (state.next_gc_start_ms <= event.time_ms) {
165014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch              return State(kRun, state.started_gcs + 1, 0.0,
166014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                           state.last_gc_time_ms);
167014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch            } else {
168014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch              return state;
169014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch            }
170014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch          } else {
171014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch            return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs,
172014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                         state.last_gc_time_ms);
173014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch          }
174014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        case kMarkCompact:
175014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch          return State(kWait, state.started_gcs, event.time_ms + kLongDelayMs,
176014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                       event.time_ms);
177014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      }
178014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case kRun:
179014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      if (event.type != kMarkCompact) {
180014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        return state;
181014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      } else {
182014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        if (state.started_gcs < kMaxNumberOfGCs &&
183014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch            (event.next_gc_likely_to_collect_more || state.started_gcs == 1)) {
184014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch          return State(kWait, state.started_gcs, event.time_ms + kShortDelayMs,
185014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                       event.time_ms);
186014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        } else {
187014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch          return State(kDone, kMaxNumberOfGCs, 0.0, event.time_ms);
188014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        }
189014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      }
190014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
191014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  UNREACHABLE();
192014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return State(kDone, 0, 0, 0.0);  // Make the compiler happy.
193014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
194014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
195014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
196014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochvoid MemoryReducer::ScheduleTimer(double time_ms, double delay_ms) {
197014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  DCHECK(delay_ms > 0);
198014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // Record the time and the js call counter.
199014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  SampleAndGetJsCallsPerMs(time_ms);
200014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // Leave some room for precision error in task scheduler.
201014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  const double kSlackMs = 100;
202014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  v8::Isolate* isolate = reinterpret_cast<v8::Isolate*>(heap()->isolate());
203014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  auto timer_task = new MemoryReducer::TimerTask(this);
204014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  V8::GetCurrentPlatform()->CallDelayedOnForegroundThread(
205014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      isolate, timer_task, (delay_ms + kSlackMs) / 1000.0);
206014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
207014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
208014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
209014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochvoid MemoryReducer::TearDown() { state_ = State(kDone, 0, 0, 0.0); }
210014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
211014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}  // namespace internal
212014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}  // namespace v8
213