gc-idle-time-handler.cc 2.84 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/flags.h"
8
#include "src/heap/gc-tracer.h"
9
#include "src/utils/utils.h"
10 11 12 13 14

namespace v8 {
namespace internal {

const double GCIdleTimeHandler::kConservativeTimeRatio = 0.9;
15
const double GCIdleTimeHandler::kHighContextDisposalRate = 100;
16

17
void GCIdleTimeHeapState::Print() {
18 19
  PrintF("contexts_disposed=%d ", contexts_disposed);
  PrintF("contexts_disposal_rate=%f ", contexts_disposal_rate);
20
  PrintF("size_of_objects=%zu ", size_of_objects);
21 22 23
  PrintF("incremental_marking_stopped=%d ", incremental_marking_stopped);
}

24
size_t GCIdleTimeHandler::EstimateMarkingStepSize(
25
    double idle_time_in_ms, double marking_speed_in_bytes_per_ms) {
26
  DCHECK_LT(0, idle_time_in_ms);
27 28

  if (marking_speed_in_bytes_per_ms == 0) {
29
    marking_speed_in_bytes_per_ms = kInitialConservativeMarkingSpeed;
30 31
  }

32 33
  double marking_step_size = marking_speed_in_bytes_per_ms * idle_time_in_ms;
  if (marking_step_size >= kMaximumMarkingStepSize) {
34
    return kMaximumMarkingStepSize;
35
  }
36
  return static_cast<size_t>(marking_step_size * kConservativeTimeRatio);
37
}
38

39
bool GCIdleTimeHandler::ShouldDoContextDisposalMarkCompact(
40 41
    int contexts_disposed, double contexts_disposal_rate,
    size_t size_of_objects) {
42
  return contexts_disposed > 0 && contexts_disposal_rate > 0 &&
43 44
         contexts_disposal_rate < kHighContextDisposalRate &&
         size_of_objects <= kMaxHeapSizeForContextDisposalMarkCompact;
45 46
}

47
// The following logic is implemented by the controller:
48 49 50
// (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.
51 52
// (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.
53
// (3) If incremental marking is in progress, we perform a marking step.
54
GCIdleTimeAction GCIdleTimeHandler::Compute(double idle_time_in_ms,
55
                                            GCIdleTimeHeapState heap_state) {
56
  if (static_cast<int>(idle_time_in_ms) <= 0) {
57
    if (heap_state.incremental_marking_stopped) {
58 59 60
      if (ShouldDoContextDisposalMarkCompact(heap_state.contexts_disposed,
                                             heap_state.contexts_disposal_rate,
                                             heap_state.size_of_objects)) {
61
        return GCIdleTimeAction::kFullGC;
62 63
      }
    }
64
    return GCIdleTimeAction::kDone;
65 66
  }

67 68
  if (FLAG_incremental_marking && !heap_state.incremental_marking_stopped) {
    return GCIdleTimeAction::kIncrementalStep;
69
  }
70

71
  return GCIdleTimeAction::kDone;
72 73
}

74
bool GCIdleTimeHandler::Enabled() { return FLAG_incremental_marking; }
75

76 77
}  // namespace internal
}  // namespace v8