V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
store-buffer.h
1 // Copyright 2011 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_STORE_BUFFER_H_
6 #define V8_HEAP_STORE_BUFFER_H_
7 
8 #include "src/allocation.h"
9 #include "src/base/logging.h"
10 #include "src/base/platform/platform.h"
11 #include "src/cancelable-task.h"
12 #include "src/globals.h"
13 #include "src/heap/gc-tracer.h"
14 #include "src/heap/remembered-set.h"
15 #include "src/heap/slot-set.h"
16 
17 namespace v8 {
18 namespace internal {
19 
20 // Intermediate buffer that accumulates old-to-new stores from the generated
21 // code. Moreover, it stores invalid old-to-new slots with two entries.
22 // The first is a tagged address of the start of the invalid range, the second
23 // one is the end address of the invalid range or null if there is just one slot
24 // that needs to be removed from the remembered set. On buffer overflow the
25 // slots are moved to the remembered set.
26 class StoreBuffer {
27  public:
28  enum StoreBufferMode { IN_GC, NOT_IN_GC };
29 
30  static const int kStoreBuffers = 2;
31  static const int kStoreBufferSize =
32  Max(static_cast<int>(kMinExpectedOSPageSize / kStoreBuffers),
33  1 << (11 + kPointerSizeLog2));
34  static const int kStoreBufferMask = kStoreBufferSize - 1;
35  static const intptr_t kDeletionTag = 1;
36 
37  V8_EXPORT_PRIVATE static int StoreBufferOverflow(Isolate* isolate);
38 
39  static void DeleteDuringGarbageCollection(StoreBuffer* store_buffer,
40  Address start, Address end);
41  static void InsertDuringGarbageCollection(StoreBuffer* store_buffer,
42  Address slot);
43 
44  static void DeleteDuringRuntime(StoreBuffer* store_buffer, Address start,
45  Address end);
46  static void InsertDuringRuntime(StoreBuffer* store_buffer, Address slot);
47 
48  explicit StoreBuffer(Heap* heap);
49  void SetUp();
50  void TearDown();
51 
52  // Used to add entries from generated code.
53  inline Address* top_address() { return reinterpret_cast<Address*>(&top_); }
54 
55  // Moves entries from a specific store buffer to the remembered set. This
56  // method takes a lock.
57  void MoveEntriesToRememberedSet(int index);
58 
59  // This method ensures that all used store buffer entries are transferred to
60  // the remembered set.
61  void MoveAllEntriesToRememberedSet();
62 
63  inline bool IsDeletionAddress(Address address) const {
64  return address & kDeletionTag;
65  }
66 
67  inline Address MarkDeletionAddress(Address address) {
68  return address | kDeletionTag;
69  }
70 
71  inline Address UnmarkDeletionAddress(Address address) {
72  return address & ~kDeletionTag;
73  }
74 
75  inline void InsertDeletionIntoStoreBuffer(Address start, Address end);
76  inline void InsertIntoStoreBuffer(Address slot);
77 
78  void InsertEntry(Address slot) {
79  // Insertions coming from the GC are directly inserted into the remembered
80  // set. Insertions coming from the runtime are added to the store buffer to
81  // allow concurrent processing.
82  insertion_callback(this, slot);
83  }
84 
85  // If we only want to delete a single slot, end should be set to null which
86  // will be written into the second field. When processing the store buffer
87  // the more efficient Remove method will be called in this case.
88  void DeleteEntry(Address start, Address end = kNullAddress) {
89  // Deletions coming from the GC are directly deleted from the remembered
90  // set. Deletions coming from the runtime are added to the store buffer
91  // to allow concurrent processing.
92  deletion_callback(this, start, end);
93  }
94 
95  void SetMode(StoreBufferMode mode);
96 
97  // Used by the concurrent processing thread to transfer entries from the
98  // store buffer to the remembered set.
99  void ConcurrentlyProcessStoreBuffer();
100 
101  bool Empty() {
102  for (int i = 0; i < kStoreBuffers; i++) {
103  if (lazy_top_[i]) {
104  return false;
105  }
106  }
107  return top_ == start_[current_];
108  }
109 
110  Heap* heap() { return heap_; }
111 
112  private:
113  // There are two store buffers. If one store buffer fills up, the main thread
114  // publishes the top pointer of the store buffer that needs processing in its
115  // global lazy_top_ field. After that it start the concurrent processing
116  // thread. The concurrent processing thread uses the pointer in lazy_top_.
117  // It will grab the given mutex and transfer its entries to the remembered
118  // set. If the concurrent thread does not make progress, the main thread will
119  // perform the work.
120  // Important: there is an ordering constrained. The store buffer with the
121  // older entries has to be processed first.
122  class Task : public CancelableTask {
123  public:
124  Task(Isolate* isolate, StoreBuffer* store_buffer)
125  : CancelableTask(isolate),
126  store_buffer_(store_buffer),
127  tracer_(isolate->heap()->tracer()) {}
128  ~Task() override = default;
129 
130  private:
131  void RunInternal() override {
132  TRACE_BACKGROUND_GC(tracer_,
133  GCTracer::BackgroundScope::BACKGROUND_STORE_BUFFER);
134  store_buffer_->ConcurrentlyProcessStoreBuffer();
135  }
136  StoreBuffer* store_buffer_;
137  GCTracer* tracer_;
138  DISALLOW_COPY_AND_ASSIGN(Task);
139  };
140 
141  StoreBufferMode mode() const { return mode_; }
142 
143  void FlipStoreBuffers();
144 
145  Heap* heap_;
146 
147  Address* top_;
148 
149  // The start and the limit of the buffer that contains store slots
150  // added from the generated code. We have two chunks of store buffers.
151  // Whenever one fills up, we notify a concurrent processing thread and
152  // use the other empty one in the meantime.
153  Address* start_[kStoreBuffers];
154  Address* limit_[kStoreBuffers];
155 
156  // At most one lazy_top_ pointer is set at any time.
157  Address* lazy_top_[kStoreBuffers];
158  base::Mutex mutex_;
159 
160  // We only want to have at most one concurrent processing tas running.
161  bool task_running_;
162 
163  // Points to the current buffer in use.
164  int current_;
165 
166  // During GC, entries are directly added to the remembered set without
167  // going through the store buffer. This is signaled by a special
168  // IN_GC mode.
169  StoreBufferMode mode_;
170 
171  VirtualMemory virtual_memory_;
172 
173  // Callbacks are more efficient than reading out the gc state for every
174  // store buffer operation.
175  void (*insertion_callback)(StoreBuffer*, Address);
176  void (*deletion_callback)(StoreBuffer*, Address, Address);
177 };
178 
179 } // namespace internal
180 } // namespace v8
181 
182 #endif // V8_HEAP_STORE_BUFFER_H_
Definition: libplatform.h:13