gc-idle-time-handler.h 5.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// 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.

#ifndef V8_HEAP_GC_IDLE_TIME_HANDLER_H_
#define V8_HEAP_GC_IDLE_TIME_HANDLER_H_

#include "src/globals.h"

namespace v8 {
namespace internal {

13
enum GCIdleTimeActionType {
14
  DONE,
15 16 17
  DO_NOTHING,
  DO_INCREMENTAL_MARKING,
  DO_SCAVENGE,
18 19
  DO_FULL_GC,
  DO_FINALIZE_SWEEPING
20 21 22 23 24
};


class GCIdleTimeAction {
 public:
25 26 27 28 29 30 31
  static GCIdleTimeAction Done() {
    GCIdleTimeAction result;
    result.type = DONE;
    result.parameter = 0;
    return result;
  }

32 33 34 35 36 37
  static GCIdleTimeAction Nothing() {
    GCIdleTimeAction result;
    result.type = DO_NOTHING;
    result.parameter = 0;
    return result;
  }
38

39 40 41 42 43 44
  static GCIdleTimeAction IncrementalMarking(intptr_t step_size) {
    GCIdleTimeAction result;
    result.type = DO_INCREMENTAL_MARKING;
    result.parameter = step_size;
    return result;
  }
45

46 47 48 49 50 51
  static GCIdleTimeAction Scavenge() {
    GCIdleTimeAction result;
    result.type = DO_SCAVENGE;
    result.parameter = 0;
    return result;
  }
52

53 54 55 56 57 58 59
  static GCIdleTimeAction FullGC() {
    GCIdleTimeAction result;
    result.type = DO_FULL_GC;
    result.parameter = 0;
    return result;
  }

60 61 62 63 64 65 66
  static GCIdleTimeAction FinalizeSweeping() {
    GCIdleTimeAction result;
    result.type = DO_FINALIZE_SWEEPING;
    result.parameter = 0;
    return result;
  }

67 68
  void Print();

69 70 71 72
  GCIdleTimeActionType type;
  intptr_t parameter;
};

73

74 75
class GCTracer;

76 77 78 79 80 81
// The idle time handler makes decisions about which garbage collection
// operations are executing during IdleNotification.
class GCIdleTimeHandler {
 public:
  // If we haven't recorded any incremental marking events yet, we carefully
  // mark with a conservative lower bound for the marking speed.
82 83 84 85
  static const size_t kInitialConservativeMarkingSpeed = 100 * KB;

  // Maximum marking step size returned by EstimateMarkingStepSize.
  static const size_t kMaximumMarkingStepSize = 700 * MB;
86 87 88 89 90

  // We have to make sure that we finish the IdleNotification before
  // idle_time_in_ms. Hence, we conservatively prune our workload estimate.
  static const double kConservativeTimeRatio;

91 92 93 94 95
  // If we haven't recorded any mark-compact events yet, we use
  // conservative lower bound for the mark-compact speed.
  static const size_t kInitialConservativeMarkCompactSpeed = 2 * MB;

  // Maximum mark-compact time returned by EstimateMarkCompactTime.
96
  static const size_t kMaxMarkCompactTimeInMs;
97

98 99 100 101
  // Minimum time to finalize sweeping phase. The main thread may wait for
  // sweeper threads.
  static const size_t kMinTimeForFinalizeSweeping;

102 103 104 105 106 107 108
  // Number of idle mark-compact events, after which idle handler will finish
  // idle round.
  static const int kMaxMarkCompactsInIdleRound;

  // Number of scavenges that will trigger start of new idle round.
  static const int kIdleScavengeThreshold;

109 110
  // Heap size threshold below which we prefer mark-compact over incremental
  // step.
111
  static const size_t kSmallHeapSize = 4 * kPointerSize * MB;
112

113 114 115
  // That is the maximum idle time we will have during frame rendering.
  static const size_t kMaxFrameRenderingIdleTime = 16;

116 117 118 119
  // If we haven't recorded any scavenger events yet, we use a conservative
  // lower bound for the scavenger speed.
  static const size_t kInitialConservativeScavengeSpeed = 100 * KB;

120 121 122 123 124
  struct HeapState {
    int contexts_disposed;
    size_t size_of_objects;
    bool incremental_marking_stopped;
    bool can_start_incremental_marking;
125
    bool sweeping_in_progress;
126 127
    size_t mark_compact_speed_in_bytes_per_ms;
    size_t incremental_marking_speed_in_bytes_per_ms;
128
    size_t scavenge_speed_in_bytes_per_ms;
129
    size_t used_new_space_size;
130
    size_t new_space_capacity;
131
    size_t new_space_allocation_throughput_in_bytes_per_ms;
132 133
  };

134 135 136 137
  GCIdleTimeHandler()
      : mark_compacts_since_idle_round_started_(0),
        scavenges_since_last_idle_round_(0) {}

138
  GCIdleTimeAction Compute(size_t idle_time_in_ms, HeapState heap_state);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

  void NotifyIdleMarkCompact() {
    if (mark_compacts_since_idle_round_started_ < kMaxMarkCompactsInIdleRound) {
      ++mark_compacts_since_idle_round_started_;
      if (mark_compacts_since_idle_round_started_ ==
          kMaxMarkCompactsInIdleRound) {
        scavenges_since_last_idle_round_ = 0;
      }
    }
  }

  void NotifyScavenge() { ++scavenges_since_last_idle_round_; }

  static size_t EstimateMarkingStepSize(size_t idle_time_in_ms,
                                        size_t marking_speed_in_bytes_per_ms);

  static size_t EstimateMarkCompactTime(
      size_t size_of_objects, size_t mark_compact_speed_in_bytes_per_ms);

158 159 160 161 162
  static bool ShouldDoMarkCompact(size_t idle_time_in_ms,
                                  size_t size_of_objects,
                                  size_t mark_compact_speed_in_bytes_per_ms);

  static bool ShouldDoScavenge(
163 164
      size_t idle_time_in_ms, size_t new_space_size, size_t used_new_space_size,
      size_t scavenger_speed_in_bytes_per_ms,
165 166
      size_t new_space_allocation_throughput_in_bytes_per_ms);

167
 private:
168
  void StartIdleRound() { mark_compacts_since_idle_round_started_ = 0; }
169
  bool IsMarkCompactIdleRoundFinished() {
170 171 172 173 174 175 176 177 178 179
    return mark_compacts_since_idle_round_started_ ==
           kMaxMarkCompactsInIdleRound;
  }
  bool EnoughGarbageSinceLastIdleRound() {
    return scavenges_since_last_idle_round_ >= kIdleScavengeThreshold;
  }

  int mark_compacts_since_idle_round_started_;
  int scavenges_since_last_idle_round_;

180 181 182 183 184 185 186
  DISALLOW_COPY_AND_ASSIGN(GCIdleTimeHandler);
};

}  // namespace internal
}  // namespace v8

#endif  // V8_HEAP_GC_IDLE_TIME_HANDLER_H_