gc-idle-time-handler.cc 5.21 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::kMaxFinalIncrementalMarkCompactTimeInMs = 1000;
16
const double GCIdleTimeHandler::kHighContextDisposalRate = 100;
17
const size_t GCIdleTimeHandler::kMinTimeForOverApproximatingWeakClosureInMs = 1;
18 19


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


41
void GCIdleTimeHeapState::Print() {
42 43
  PrintF("contexts_disposed=%d ", contexts_disposed);
  PrintF("contexts_disposal_rate=%f ", contexts_disposal_rate);
jfb's avatar
jfb committed
44
  PrintF("size_of_objects=%" PRIuS " ", size_of_objects);
45 46 47
  PrintF("incremental_marking_stopped=%d ", incremental_marking_stopped);
}

48
size_t GCIdleTimeHandler::EstimateMarkingStepSize(
49
    double idle_time_in_ms, double marking_speed_in_bytes_per_ms) {
50 51 52
  DCHECK(idle_time_in_ms > 0);

  if (marking_speed_in_bytes_per_ms == 0) {
53
    marking_speed_in_bytes_per_ms = kInitialConservativeMarkingSpeed;
54 55
  }

56 57
  double marking_step_size = marking_speed_in_bytes_per_ms * idle_time_in_ms;
  if (marking_step_size >= kMaximumMarkingStepSize) {
58
    return kMaximumMarkingStepSize;
59
  }
60
  return static_cast<size_t>(marking_step_size * kConservativeTimeRatio);
61
}
62

63
double GCIdleTimeHandler::EstimateFinalIncrementalMarkCompactTime(
64
    size_t size_of_objects,
65
    double final_incremental_mark_compact_speed_in_bytes_per_ms) {
66 67 68 69
  if (final_incremental_mark_compact_speed_in_bytes_per_ms == 0) {
    final_incremental_mark_compact_speed_in_bytes_per_ms =
        kInitialConservativeFinalIncrementalMarkCompactSpeed;
  }
70
  double result =
71
      size_of_objects / final_incremental_mark_compact_speed_in_bytes_per_ms;
72
  return Min<double>(result, kMaxFinalIncrementalMarkCompactTimeInMs);
73 74
}

75
bool GCIdleTimeHandler::ShouldDoContextDisposalMarkCompact(
76 77
    int contexts_disposed, double contexts_disposal_rate) {
  return contexts_disposed > 0 && contexts_disposal_rate > 0 &&
78 79 80
         contexts_disposal_rate < kHighContextDisposalRate;
}

81
bool GCIdleTimeHandler::ShouldDoFinalIncrementalMarkCompact(
82 83
    double idle_time_in_ms, size_t size_of_objects,
    double final_incremental_mark_compact_speed_in_bytes_per_ms) {
84 85 86 87 88 89
  return idle_time_in_ms >=
         EstimateFinalIncrementalMarkCompactTime(
             size_of_objects,
             final_incremental_mark_compact_speed_in_bytes_per_ms);
}

90
bool GCIdleTimeHandler::ShouldDoOverApproximateWeakClosure(
91
    double idle_time_in_ms) {
92 93 94 95 96
  // TODO(jochen): Estimate the time it will take to build the object groups.
  return idle_time_in_ms >= kMinTimeForOverApproximatingWeakClosureInMs;
}


97 98 99 100
GCIdleTimeAction GCIdleTimeHandler::NothingOrDone(double idle_time_in_ms) {
  if (idle_time_in_ms >= kMinBackgroundIdleTime) {
    return GCIdleTimeAction::Nothing();
  }
101
  if (idle_times_which_made_no_progress_ >= kMaxNoProgressIdleTimes) {
102 103
    return GCIdleTimeAction::Done();
  } else {
104
    idle_times_which_made_no_progress_++;
105 106 107 108 109
    return GCIdleTimeAction::Nothing();
  }
}


110
// The following logic is implemented by the controller:
111 112 113
// (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.
114 115
// (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.
116 117
// (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.
118
// (4) If sweeping is in progress and we received a large enough idle time
119
// request, we finalize sweeping here.
120
// (5) If incremental marking is in progress, we perform a marking step. Note,
121
// that this currently may trigger a full garbage collection.
122
GCIdleTimeAction GCIdleTimeHandler::Compute(double idle_time_in_ms,
123
                                            GCIdleTimeHeapState heap_state) {
124
  if (static_cast<int>(idle_time_in_ms) <= 0) {
125
    if (heap_state.incremental_marking_stopped) {
126 127 128
      if (ShouldDoContextDisposalMarkCompact(
              heap_state.contexts_disposed,
              heap_state.contexts_disposal_rate)) {
129
        return GCIdleTimeAction::FullGC();
130 131 132 133 134
      }
    }
    return GCIdleTimeAction::Nothing();
  }

135 136 137 138
  // 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)) {
139
    return NothingOrDone(idle_time_in_ms);
140 141
  }

142 143
  if (!FLAG_incremental_marking || heap_state.incremental_marking_stopped) {
    return GCIdleTimeAction::Done();
144
  }
145

146
  return GCIdleTimeAction::IncrementalStep();
147 148 149
}


150 151
}  // namespace internal
}  // namespace v8