scavenger.h 5.83 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2015 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_SCAVENGER_H_
#define V8_HEAP_SCAVENGER_H_

8
#include "src/base/platform/condition-variable.h"
9
#include "src/heap/local-allocator.h"
10
#include "src/heap/objects-visiting.h"
11
#include "src/heap/slot-set.h"
12
#include "src/heap/worklist.h"
13 14 15 16

namespace v8 {
namespace internal {

17 18
class OneshotBarrier;

19 20 21 22 23 24
enum class CopyAndForwardResult {
  SUCCESS_YOUNG_GENERATION,
  SUCCESS_OLD_GENERATION,
  FAILURE
};

25
class Scavenger {
26
 public:
27 28 29 30 31 32 33
  static const int kCopiedListSegmentSize = 256;
  static const int kPromotionListSegmentSize = 256;

  using ObjectAndSize = std::pair<HeapObject*, int>;
  using CopiedList = Worklist<ObjectAndSize, kCopiedListSegmentSize>;
  using PromotionList = Worklist<ObjectAndSize, kPromotionListSegmentSize>;

34 35
  Scavenger(Heap* heap, bool is_logging, CopiedList* copied_list,
            PromotionList* promotion_list, int task_id);
36

37 38 39
  // Entry point for scavenging an old generation page. For scavenging single
  // objects see RootScavengingVisitor and ScavengeVisitor below.
  void ScavengePage(MemoryChunk* page);
40 41 42

  // Processes remaining work (=objects) after single objects have been
  // manually scavenged using ScavengeObject or CheckAndScavengeObject.
43
  void Process(OneshotBarrier* barrier = nullptr);
44

45 46 47
  // Finalize the Scavenger. Needs to be called from the main thread.
  void Finalize();

48 49 50
  size_t bytes_copied() const { return copied_size_; }
  size_t bytes_promoted() const { return promoted_size_; }

51
 private:
52 53 54
  // Number of objects to process before interrupting for potentially waking
  // up other tasks.
  static const int kInterruptThreshold = 128;
55 56
  static const int kInitialLocalPretenuringFeedbackCapacity = 256;

57 58
  inline Heap* heap() { return heap_; }

59
  inline void PageMemoryFence(MaybeObject* object);
60

61 62
  void AddPageToSweeperIfNecessary(MemoryChunk* page);

63 64 65 66 67 68 69
  // Potentially scavenges an object referenced from |slot_address| if it is
  // indeed a HeapObject and resides in from space.
  inline SlotCallbackResult CheckAndScavengeObject(Heap* heap,
                                                   Address slot_address);

  // Scavenges an object |object| referenced from slot |p|. |object| is required
  // to be in from space.
70 71
  inline SlotCallbackResult ScavengeObject(HeapObjectReference** p,
                                           HeapObject* object);
72

73
  // Copies |source| to |target| and sets the forwarding pointer in |source|.
74
  V8_INLINE bool MigrateObject(Map* map, HeapObject* source, HeapObject* target,
75
                               int size);
76

77 78
  V8_INLINE SlotCallbackResult
  RememberedSetEntryNeeded(CopyAndForwardResult result);
79

80 81 82 83
  V8_INLINE CopyAndForwardResult SemiSpaceCopyObject(Map* map,
                                                     HeapObjectReference** slot,
                                                     HeapObject* object,
                                                     int object_size);
84

85 86 87 88
  V8_INLINE CopyAndForwardResult PromoteObject(Map* map,
                                               HeapObjectReference** slot,
                                               HeapObject* object,
                                               int object_size);
89

90 91
  V8_INLINE SlotCallbackResult EvacuateObject(HeapObjectReference** slot,
                                              Map* map, HeapObject* source);
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106
  // Different cases for object evacuation.
  V8_INLINE SlotCallbackResult EvacuateObjectDefault(Map* map,
                                                     HeapObjectReference** slot,
                                                     HeapObject* object,
                                                     int object_size);

  inline SlotCallbackResult EvacuateThinString(Map* map, HeapObject** slot,
                                               ThinString* object,
                                               int object_size);

  inline SlotCallbackResult EvacuateShortcutCandidate(Map* map,
                                                      HeapObject** slot,
                                                      ConsString* object,
                                                      int object_size);
107

108 109
  void IterateAndScavengePromotedObject(HeapObject* target, int size);

110 111
  static inline bool ContainsOnlyData(VisitorId visitor_id);

112 113
  Heap* const heap_;
  PromotionList::View promotion_list_;
114
  CopiedList::View copied_list_;
115
  Heap::PretenuringFeedbackMap local_pretenuring_feedback_;
116 117
  size_t copied_size_;
  size_t promoted_size_;
118
  LocalAllocator allocator_;
119 120 121
  const bool is_logging_;
  const bool is_incremental_marking_;
  const bool is_compacting_;
122 123

  friend class IterateAndScavengePromotedObjectsVisitor;
124 125
  friend class RootScavengeVisitor;
  friend class ScavengeVisitor;
126 127 128 129
};

// Helper class for turning the scavenger into an object visitor that is also
// filtering out non-HeapObjects and objects which do not reside in new space.
130
class RootScavengeVisitor final : public RootVisitor {
131
 public:
132
  explicit RootScavengeVisitor(Scavenger* scavenger);
133

134 135 136
  void VisitRootPointer(Root root, const char* description, Object** p) final;
  void VisitRootPointers(Root root, const char* description, Object** start,
                         Object** end) final;
137

138
 private:
139
  void ScavengePointer(Object** p);
140

141
  Scavenger* const scavenger_;
142 143
};

144
class ScavengeVisitor final : public NewSpaceVisitor<ScavengeVisitor> {
145
 public:
146
  explicit ScavengeVisitor(Scavenger* scavenger);
147

148 149
  V8_INLINE void VisitPointers(HeapObject* host, Object** start,
                               Object** end) final;
150 151
  V8_INLINE void VisitPointers(HeapObject* host, MaybeObject** start,
                               MaybeObject** end) final;
152 153

 private:
154
  Scavenger* const scavenger_;
155 156 157 158 159 160
};

}  // namespace internal
}  // namespace v8

#endif  // V8_HEAP_SCAVENGER_H_