V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
scavenger.h
1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_HEAP_SCAVENGER_H_
6 #define V8_HEAP_SCAVENGER_H_
7 
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 namespace v8 {
15 namespace internal {
16 
17 class OneshotBarrier;
18 
19 enum class CopyAndForwardResult {
20  SUCCESS_YOUNG_GENERATION,
21  SUCCESS_OLD_GENERATION,
22  FAILURE
23 };
24 
25 using ObjectAndSize = std::pair<HeapObject*, int>;
26 using SurvivingNewLargeObjectsMap = std::unordered_map<HeapObject*, Map>;
27 using SurvivingNewLargeObjectMapEntry = std::pair<HeapObject*, Map>;
28 
30  public:
31  static const int kMaxScavengerTasks = 8;
32  static const int kMaxWaitTimeMs = 2;
33 
34  explicit ScavengerCollector(Heap* heap);
35 
36  void CollectGarbage();
37 
38  private:
39  void MergeSurvivingNewLargeObjects(
40  const SurvivingNewLargeObjectsMap& objects);
41 
42  int NumberOfScavengeTasks();
43 
44  void HandleSurvivingNewLargeObjects();
45 
46  Isolate* const isolate_;
47  Heap* const heap_;
48  base::Semaphore parallel_scavenge_semaphore_;
49  SurvivingNewLargeObjectsMap surviving_new_large_objects_;
50 
51  friend class Scavenger;
52 };
53 
54 class Scavenger {
55  public:
57  HeapObject* heap_object;
58  Map map;
59  int size;
60  };
61 
62  class PromotionList {
63  public:
64  class View {
65  public:
66  View(PromotionList* promotion_list, int task_id)
67  : promotion_list_(promotion_list), task_id_(task_id) {}
68 
69  inline void PushRegularObject(HeapObject* object, int size);
70  inline void PushLargeObject(HeapObject* object, Map map, int size);
71  inline bool IsEmpty();
72  inline size_t LocalPushSegmentSize();
73  inline bool Pop(struct PromotionListEntry* entry);
74  inline bool IsGlobalPoolEmpty();
75  inline bool ShouldEagerlyProcessPromotionList();
76 
77  private:
78  PromotionList* promotion_list_;
79  int task_id_;
80  };
81 
82  explicit PromotionList(int num_tasks)
83  : regular_object_promotion_list_(num_tasks),
84  large_object_promotion_list_(num_tasks) {}
85 
86  inline void PushRegularObject(int task_id, HeapObject* object, int size);
87  inline void PushLargeObject(int task_id, HeapObject* object, Map map,
88  int size);
89  inline bool IsEmpty();
90  inline size_t LocalPushSegmentSize(int task_id);
91  inline bool Pop(int task_id, struct PromotionListEntry* entry);
92  inline bool IsGlobalPoolEmpty();
93  inline bool ShouldEagerlyProcessPromotionList(int task_id);
94 
95  private:
96  static const int kRegularObjectPromotionListSegmentSize = 256;
97  static const int kLargeObjectPromotionListSegmentSize = 4;
98 
99  using RegularObjectPromotionList =
101  using LargeObjectPromotionList =
103 
104  RegularObjectPromotionList regular_object_promotion_list_;
105  LargeObjectPromotionList large_object_promotion_list_;
106  };
107 
108  static const int kCopiedListSegmentSize = 256;
109 
111 
112  Scavenger(ScavengerCollector* collector, Heap* heap, bool is_logging,
113  CopiedList* copied_list, PromotionList* promotion_list,
114  int task_id);
115 
116  // Entry point for scavenging an old generation page. For scavenging single
117  // objects see RootScavengingVisitor and ScavengeVisitor below.
118  void ScavengePage(MemoryChunk* page);
119 
120  // Processes remaining work (=objects) after single objects have been
121  // manually scavenged using ScavengeObject or CheckAndScavengeObject.
122  void Process(OneshotBarrier* barrier = nullptr);
123 
124  // Finalize the Scavenger. Needs to be called from the main thread.
125  void Finalize();
126 
127  size_t bytes_copied() const { return copied_size_; }
128  size_t bytes_promoted() const { return promoted_size_; }
129 
130  private:
131  // Number of objects to process before interrupting for potentially waking
132  // up other tasks.
133  static const int kInterruptThreshold = 128;
134  static const int kInitialLocalPretenuringFeedbackCapacity = 256;
135 
136  inline Heap* heap() { return heap_; }
137 
138  inline void PageMemoryFence(MaybeObject object);
139 
140  void AddPageToSweeperIfNecessary(MemoryChunk* page);
141 
142  // Potentially scavenges an object referenced from |slot| if it is
143  // indeed a HeapObject and resides in from space.
144  inline SlotCallbackResult CheckAndScavengeObject(Heap* heap,
145  MaybeObjectSlot slot);
146 
147  // Scavenges an object |object| referenced from slot |p|. |object| is required
148  // to be in from space.
149  inline SlotCallbackResult ScavengeObject(HeapObjectSlot p,
150  HeapObject* object);
151 
152  // Copies |source| to |target| and sets the forwarding pointer in |source|.
153  V8_INLINE bool MigrateObject(Map map, HeapObject* source, HeapObject* target,
154  int size);
155 
156  V8_INLINE SlotCallbackResult
157  RememberedSetEntryNeeded(CopyAndForwardResult result);
158 
159  V8_INLINE CopyAndForwardResult SemiSpaceCopyObject(Map map,
160  HeapObjectSlot slot,
161  HeapObject* object,
162  int object_size);
163 
164  V8_INLINE CopyAndForwardResult PromoteObject(Map map, HeapObjectSlot slot,
165  HeapObject* object,
166  int object_size);
167 
168  V8_INLINE SlotCallbackResult EvacuateObject(HeapObjectSlot slot, Map map,
169  HeapObject* source);
170 
171  V8_INLINE bool HandleLargeObject(Map map, HeapObject* object,
172  int object_size);
173 
174  // Different cases for object evacuation.
175  V8_INLINE SlotCallbackResult EvacuateObjectDefault(Map map,
176  HeapObjectSlot slot,
177  HeapObject* object,
178  int object_size);
179 
180  inline SlotCallbackResult EvacuateThinString(Map map, HeapObjectSlot slot,
181  ThinString object,
182  int object_size);
183 
184  inline SlotCallbackResult EvacuateShortcutCandidate(Map map,
185  HeapObjectSlot slot,
186  ConsString object,
187  int object_size);
188 
189  void IterateAndScavengePromotedObject(HeapObject* target, Map map, int size);
190 
191  static inline bool ContainsOnlyData(VisitorId visitor_id);
192 
193  ScavengerCollector* const collector_;
194  Heap* const heap_;
195  PromotionList::View promotion_list_;
196  CopiedList::View copied_list_;
197  Heap::PretenuringFeedbackMap local_pretenuring_feedback_;
198  size_t copied_size_;
199  size_t promoted_size_;
200  LocalAllocator allocator_;
201  SurvivingNewLargeObjectsMap surviving_new_large_objects_;
202  const bool is_logging_;
203  const bool is_incremental_marking_;
204  const bool is_compacting_;
205 
206  friend class IterateAndScavengePromotedObjectsVisitor;
207  friend class RootScavengeVisitor;
208  friend class ScavengeVisitor;
209 };
210 
211 // Helper class for turning the scavenger into an object visitor that is also
212 // filtering out non-HeapObjects and objects which do not reside in new space.
213 class RootScavengeVisitor final : public RootVisitor {
214  public:
215  explicit RootScavengeVisitor(Scavenger* scavenger);
216 
217  void VisitRootPointer(Root root, const char* description, ObjectSlot p) final;
218  void VisitRootPointers(Root root, const char* description, ObjectSlot start,
219  ObjectSlot end) final;
220 
221  private:
222  void ScavengePointer(ObjectSlot p);
223 
224  Scavenger* const scavenger_;
225 };
226 
227 class ScavengeVisitor final : public NewSpaceVisitor<ScavengeVisitor> {
228  public:
229  explicit ScavengeVisitor(Scavenger* scavenger);
230 
231  V8_INLINE void VisitPointers(HeapObject* host, ObjectSlot start,
232  ObjectSlot end) final;
233  V8_INLINE void VisitPointers(HeapObject* host, MaybeObjectSlot start,
234  MaybeObjectSlot end) final;
235 
236  private:
237  Scavenger* const scavenger_;
238 };
239 
240 } // namespace internal
241 } // namespace v8
242 
243 #endif // V8_HEAP_SCAVENGER_H_
Definition: libplatform.h:13