gc-idle-time-handler.cc 6.31 KB
Newer Older
1 2 3 4 5
// Copyright 2014 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/gc-idle-time-handler.h"
6 7

#include "src/flags.h"
8 9
#include "src/heap/gc-tracer.h"
#include "src/utils.h"
10 11 12 13 14

namespace v8 {
namespace internal {

const double GCIdleTimeHandler::kConservativeTimeRatio = 0.9;
15
const size_t GCIdleTimeHandler::kMaxMarkCompactTimeInMs = 1000;
16
const size_t GCIdleTimeHandler::kMaxFinalIncrementalMarkCompactTimeInMs = 1000;
17
const double GCIdleTimeHandler::kHighContextDisposalRate = 100;
18
const size_t GCIdleTimeHandler::kMinTimeForOverApproximatingWeakClosureInMs = 1;
19 20


21 22
void GCIdleTimeAction::Print() {
  switch (type) {
23 24 25
    case DONE:
      PrintF("done");
      break;
26 27 28
    case DO_NOTHING:
      PrintF("no action");
      break;
29 30
    case DO_INCREMENTAL_STEP:
      PrintF("incremental step");
31 32 33
      if (additional_work) {
        PrintF("; finalized marking");
      }
34 35 36 37 38 39 40 41
      break;
    case DO_FULL_GC:
      PrintF("full GC");
      break;
  }
}


42
void GCIdleTimeHeapState::Print() {
43 44 45 46 47 48 49
  PrintF("contexts_disposed=%d ", contexts_disposed);
  PrintF("contexts_disposal_rate=%f ", contexts_disposal_rate);
  PrintF("size_of_objects=%" V8_PTR_PREFIX "d ", size_of_objects);
  PrintF("incremental_marking_stopped=%d ", incremental_marking_stopped);
}


50 51
size_t GCIdleTimeHandler::EstimateMarkingStepSize(
    size_t idle_time_in_ms, size_t marking_speed_in_bytes_per_ms) {
52 53 54
  DCHECK(idle_time_in_ms > 0);

  if (marking_speed_in_bytes_per_ms == 0) {
55
    marking_speed_in_bytes_per_ms = kInitialConservativeMarkingSpeed;
56 57
  }

58 59
  size_t marking_step_size = marking_speed_in_bytes_per_ms * idle_time_in_ms;
  if (marking_step_size / marking_speed_in_bytes_per_ms != idle_time_in_ms) {
60
    // In the case of an overflow we return maximum marking step size.
61
    return kMaximumMarkingStepSize;
62 63
  }

64 65 66
  if (marking_step_size > kMaximumMarkingStepSize)
    return kMaximumMarkingStepSize;

67
  return static_cast<size_t>(marking_step_size * kConservativeTimeRatio);
68
}
69 70 71 72


size_t GCIdleTimeHandler::EstimateMarkCompactTime(
    size_t size_of_objects, size_t mark_compact_speed_in_bytes_per_ms) {
73
  // TODO(hpayer): Be more precise about the type of mark-compact event. It
74
  // makes a huge difference if compaction is happening.
75 76 77 78 79 80 81 82
  if (mark_compact_speed_in_bytes_per_ms == 0) {
    mark_compact_speed_in_bytes_per_ms = kInitialConservativeMarkCompactSpeed;
  }
  size_t result = size_of_objects / mark_compact_speed_in_bytes_per_ms;
  return Min(result, kMaxMarkCompactTimeInMs);
}


83 84 85 86 87 88 89 90 91 92 93 94 95
size_t GCIdleTimeHandler::EstimateFinalIncrementalMarkCompactTime(
    size_t size_of_objects,
    size_t final_incremental_mark_compact_speed_in_bytes_per_ms) {
  if (final_incremental_mark_compact_speed_in_bytes_per_ms == 0) {
    final_incremental_mark_compact_speed_in_bytes_per_ms =
        kInitialConservativeFinalIncrementalMarkCompactSpeed;
  }
  size_t result =
      size_of_objects / final_incremental_mark_compact_speed_in_bytes_per_ms;
  return Min(result, kMaxFinalIncrementalMarkCompactTimeInMs);
}


96 97 98
bool GCIdleTimeHandler::ShouldDoMarkCompact(
    size_t idle_time_in_ms, size_t size_of_objects,
    size_t mark_compact_speed_in_bytes_per_ms) {
99 100 101 102
  return idle_time_in_ms >= kMaxScheduledIdleTime &&
         idle_time_in_ms >=
             EstimateMarkCompactTime(size_of_objects,
                                     mark_compact_speed_in_bytes_per_ms);
103 104 105
}


106
bool GCIdleTimeHandler::ShouldDoContextDisposalMarkCompact(
107 108
    int contexts_disposed, double contexts_disposal_rate) {
  return contexts_disposed > 0 && contexts_disposal_rate > 0 &&
109 110 111 112
         contexts_disposal_rate < kHighContextDisposalRate;
}


113 114 115 116 117 118 119 120 121 122
bool GCIdleTimeHandler::ShouldDoFinalIncrementalMarkCompact(
    size_t idle_time_in_ms, size_t size_of_objects,
    size_t final_incremental_mark_compact_speed_in_bytes_per_ms) {
  return idle_time_in_ms >=
         EstimateFinalIncrementalMarkCompactTime(
             size_of_objects,
             final_incremental_mark_compact_speed_in_bytes_per_ms);
}


123 124 125 126 127 128 129
bool GCIdleTimeHandler::ShouldDoOverApproximateWeakClosure(
    size_t idle_time_in_ms) {
  // TODO(jochen): Estimate the time it will take to build the object groups.
  return idle_time_in_ms >= kMinTimeForOverApproximatingWeakClosureInMs;
}


130 131 132 133
GCIdleTimeAction GCIdleTimeHandler::NothingOrDone(double idle_time_in_ms) {
  if (idle_time_in_ms >= kMinBackgroundIdleTime) {
    return GCIdleTimeAction::Nothing();
  }
134
  if (idle_times_which_made_no_progress_ >= kMaxNoProgressIdleTimes) {
135 136
    return GCIdleTimeAction::Done();
  } else {
137
    idle_times_which_made_no_progress_++;
138 139 140 141 142
    return GCIdleTimeAction::Nothing();
  }
}


143
// The following logic is implemented by the controller:
144 145 146
// (1) If we don't have any idle time, do nothing, unless a context was
// disposed, incremental marking is stopped, and the heap is small. Then do
// a full GC.
147 148
// (2) If the context disposal rate is high and we cannot perform a full GC,
// we do nothing until the context disposal rate becomes lower.
149 150
// (3) If the new space is almost full and we can affort a scavenge or if the
// next scavenge will very likely take long, then a scavenge is performed.
151
// (4) If sweeping is in progress and we received a large enough idle time
152
// request, we finalize sweeping here.
153
// (5) If incremental marking is in progress, we perform a marking step. Note,
154
// that this currently may trigger a full garbage collection.
155
GCIdleTimeAction GCIdleTimeHandler::Compute(double idle_time_in_ms,
156
                                            GCIdleTimeHeapState heap_state) {
157
  if (static_cast<int>(idle_time_in_ms) <= 0) {
158
    if (heap_state.incremental_marking_stopped) {
159 160 161
      if (ShouldDoContextDisposalMarkCompact(
              heap_state.contexts_disposed,
              heap_state.contexts_disposal_rate)) {
162
        return GCIdleTimeAction::FullGC();
163 164 165 166 167
      }
    }
    return GCIdleTimeAction::Nothing();
  }

168 169 170 171
  // We are in a context disposal GC scenario. Don't do anything if we do not
  // get the right idle signal.
  if (ShouldDoContextDisposalMarkCompact(heap_state.contexts_disposed,
                                         heap_state.contexts_disposal_rate)) {
172
    return NothingOrDone(idle_time_in_ms);
173 174
  }

175 176
  if (!FLAG_incremental_marking || heap_state.incremental_marking_stopped) {
    return GCIdleTimeAction::Done();
177
  }
178

179
  return GCIdleTimeAction::IncrementalStep();
180 181 182
}


183 184
}  // namespace internal
}  // namespace v8