OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/heap/array-buffer-tracker.h" | 5 #include "src/heap/array-buffer-tracker.h" |
6 #include "src/heap/array-buffer-tracker-inl.h" | |
7 #include "src/heap/heap.h" | 6 #include "src/heap/heap.h" |
8 #include "src/isolate.h" | 7 #include "src/isolate.h" |
| 8 #include "src/objects.h" |
9 #include "src/objects-inl.h" | 9 #include "src/objects-inl.h" |
10 #include "src/objects.h" | |
11 #include "src/v8.h" | 10 #include "src/v8.h" |
12 | 11 |
13 namespace v8 { | 12 namespace v8 { |
14 namespace internal { | 13 namespace internal { |
15 | 14 |
16 LocalArrayBufferTracker::~LocalArrayBufferTracker() { | 15 ArrayBufferTracker::~ArrayBufferTracker() { |
| 16 Isolate* isolate = heap()->isolate(); |
17 size_t freed_memory = 0; | 17 size_t freed_memory = 0; |
18 for (auto& buffer : live_) { | 18 for (auto& buffer : live_array_buffers_) { |
19 heap_->isolate()->array_buffer_allocator()->Free(buffer.second.first, | 19 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); |
20 buffer.second.second); | 20 freed_memory += buffer.second; |
21 freed_memory += buffer.second.second; | |
22 } | 21 } |
| 22 for (auto& buffer : live_array_buffers_for_scavenge_) { |
| 23 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); |
| 24 freed_memory += buffer.second; |
| 25 } |
| 26 live_array_buffers_.clear(); |
| 27 live_array_buffers_for_scavenge_.clear(); |
| 28 not_yet_discovered_array_buffers_.clear(); |
| 29 not_yet_discovered_array_buffers_for_scavenge_.clear(); |
| 30 |
23 if (freed_memory > 0) { | 31 if (freed_memory > 0) { |
24 heap_->update_amount_of_external_allocated_freed_memory( | 32 heap()->update_amount_of_external_allocated_memory( |
25 static_cast<intptr_t>(freed_memory)); | 33 -static_cast<int64_t>(freed_memory)); |
26 } | |
27 live_.clear(); | |
28 not_yet_discovered_.clear(); | |
29 } | |
30 | |
31 void LocalArrayBufferTracker::Add(Key key, const Value& value) { | |
32 live_[key] = value; | |
33 not_yet_discovered_[key] = value; | |
34 } | |
35 | |
36 void LocalArrayBufferTracker::AddLive(Key key, const Value& value) { | |
37 live_[key] = value; | |
38 DCHECK_EQ(not_yet_discovered_.count(key), 0); | |
39 } | |
40 | |
41 void LocalArrayBufferTracker::MarkLive(Key key) { | |
42 DCHECK_EQ(live_.count(key), 1); | |
43 not_yet_discovered_.erase(key); | |
44 } | |
45 | |
46 LocalArrayBufferTracker::Value LocalArrayBufferTracker::Remove(Key key) { | |
47 DCHECK_EQ(live_.count(key), 1); | |
48 Value value = live_[key]; | |
49 live_.erase(key); | |
50 not_yet_discovered_.erase(key); | |
51 return value; | |
52 } | |
53 | |
54 void LocalArrayBufferTracker::FreeDead() { | |
55 size_t freed_memory = 0; | |
56 for (TrackingMap::iterator it = not_yet_discovered_.begin(); | |
57 it != not_yet_discovered_.end();) { | |
58 heap_->isolate()->array_buffer_allocator()->Free(it->second.first, | |
59 it->second.second); | |
60 freed_memory += it->second.second; | |
61 live_.erase(it->first); | |
62 not_yet_discovered_.erase(it++); | |
63 } | |
64 if (freed_memory > 0) { | |
65 heap_->update_amount_of_external_allocated_freed_memory( | |
66 static_cast<intptr_t>(freed_memory)); | |
67 } | |
68 started_ = false; | |
69 } | |
70 | |
71 void LocalArrayBufferTracker::Reset() { | |
72 if (!started_) { | |
73 not_yet_discovered_ = live_; | |
74 started_ = true; | |
75 } | 34 } |
76 } | 35 } |
77 | 36 |
78 bool LocalArrayBufferTracker::IsEmpty() { | |
79 return live_.empty() && not_yet_discovered_.empty(); | |
80 } | |
81 | 37 |
82 void ArrayBufferTracker::RegisterNew(Heap* heap, JSArrayBuffer* buffer) { | 38 void ArrayBufferTracker::RegisterNew(JSArrayBuffer* buffer) { |
83 void* data = buffer->backing_store(); | 39 void* data = buffer->backing_store(); |
84 if (!data) return; | 40 if (!data) return; |
85 | 41 |
86 size_t length = NumberToSize(heap->isolate(), buffer->byte_length()); | 42 bool in_new_space = heap()->InNewSpace(buffer); |
87 Page* page = Page::FromAddress(buffer->address()); | 43 size_t length = NumberToSize(heap()->isolate(), buffer->byte_length()); |
88 LocalArrayBufferTracker* tracker = | 44 if (in_new_space) { |
89 page->local_tracker<Page::kCreateIfNotPresent>(); | 45 live_array_buffers_for_scavenge_[data] = length; |
90 DCHECK_NOT_NULL(tracker); | 46 } else { |
91 { | 47 live_array_buffers_[data] = length; |
92 base::LockGuard<base::Mutex> guard(page->mutex()); | |
93 if (Marking::IsBlack(Marking::MarkBitFrom(buffer))) { | |
94 tracker->AddLive(buffer, std::make_pair(data, length)); | |
95 } else { | |
96 tracker->Add(buffer, std::make_pair(data, length)); | |
97 } | |
98 } | 48 } |
| 49 |
99 // We may go over the limit of externally allocated memory here. We call the | 50 // We may go over the limit of externally allocated memory here. We call the |
100 // api function to trigger a GC in this case. | 51 // api function to trigger a GC in this case. |
101 reinterpret_cast<v8::Isolate*>(heap->isolate()) | 52 reinterpret_cast<v8::Isolate*>(heap()->isolate()) |
102 ->AdjustAmountOfExternalAllocatedMemory(length); | 53 ->AdjustAmountOfExternalAllocatedMemory(length); |
103 } | 54 } |
104 | 55 |
105 void ArrayBufferTracker::Unregister(Heap* heap, JSArrayBuffer* buffer) { | 56 |
| 57 void ArrayBufferTracker::Unregister(JSArrayBuffer* buffer) { |
106 void* data = buffer->backing_store(); | 58 void* data = buffer->backing_store(); |
107 if (!data) return; | 59 if (!data) return; |
108 | 60 |
109 Page* page = Page::FromAddress(buffer->address()); | 61 bool in_new_space = heap()->InNewSpace(buffer); |
110 LocalArrayBufferTracker* tracker = page->local_tracker<Page::kDontCreate>(); | 62 std::map<void*, size_t>* live_buffers = |
111 DCHECK_NOT_NULL(tracker); | 63 in_new_space ? &live_array_buffers_for_scavenge_ : &live_array_buffers_; |
112 size_t length = 0; | 64 std::map<void*, size_t>* not_yet_discovered_buffers = |
113 { | 65 in_new_space ? ¬_yet_discovered_array_buffers_for_scavenge_ |
114 base::LockGuard<base::Mutex> guard(page->mutex()); | 66 : ¬_yet_discovered_array_buffers_; |
115 length = tracker->Remove(buffer).second; | 67 |
116 } | 68 DCHECK(live_buffers->count(data) > 0); |
117 heap->update_amount_of_external_allocated_memory( | 69 |
118 -static_cast<intptr_t>(length)); | 70 size_t length = (*live_buffers)[data]; |
| 71 live_buffers->erase(data); |
| 72 not_yet_discovered_buffers->erase(data); |
| 73 |
| 74 heap()->update_amount_of_external_allocated_memory( |
| 75 -static_cast<int64_t>(length)); |
119 } | 76 } |
120 | 77 |
121 void ArrayBufferTracker::FreeDeadInNewSpace(Heap* heap) { | |
122 NewSpacePageIterator from_it(heap->new_space()->FromSpaceStart(), | |
123 heap->new_space()->FromSpaceEnd()); | |
124 while (from_it.has_next()) { | |
125 ScanAndFreeDeadArrayBuffers<LocalArrayBufferTracker::kForwardingPointer>( | |
126 from_it.next()); | |
127 } | |
128 heap->account_amount_of_external_allocated_freed_memory(); | |
129 } | |
130 | 78 |
131 void ArrayBufferTracker::ResetTrackersInOldSpace(Heap* heap) { | 79 void ArrayBufferTracker::MarkLive(JSArrayBuffer* buffer) { |
132 heap->old_space()->ForAllPages([](Page* p) { | 80 base::LockGuard<base::Mutex> guard(&mutex_); |
133 LocalArrayBufferTracker* tracker = p->local_tracker<Page::kDontCreate>(); | 81 void* data = buffer->backing_store(); |
134 if (tracker != nullptr) { | |
135 tracker->Reset(); | |
136 if (tracker->IsEmpty()) { | |
137 p->ReleaseLocalTracker(); | |
138 } | |
139 } | |
140 }); | |
141 } | |
142 | 82 |
143 void ArrayBufferTracker::MarkLive(Heap* heap, JSArrayBuffer* buffer) { | 83 // ArrayBuffer might be in the middle of being constructed. |
144 if (buffer->is_external()) return; | 84 if (data == heap()->undefined_value()) return; |
145 void* data = buffer->backing_store(); | 85 if (heap()->InNewSpace(buffer)) { |
146 if (data == nullptr) return; | 86 not_yet_discovered_array_buffers_for_scavenge_.erase(data); |
147 if (data == heap->undefined_value()) return; | 87 } else { |
148 | 88 not_yet_discovered_array_buffers_.erase(data); |
149 Page* page = Page::FromAddress(buffer->address()); | |
150 LocalArrayBufferTracker* tracker = | |
151 page->local_tracker<Page::kCreateIfNotPresent>(); | |
152 DCHECK_NOT_NULL(tracker); | |
153 { | |
154 base::LockGuard<base::Mutex> guard(page->mutex()); | |
155 DCHECK(tracker->IsTracked(buffer)); | |
156 tracker->MarkLive((buffer)); | |
157 } | 89 } |
158 } | 90 } |
159 | 91 |
160 void ArrayBufferTracker::FreeDead(Page* page) { | 92 |
161 // Only called from the sweeper, which already holds the page lock. | 93 void ArrayBufferTracker::FreeDead(bool from_scavenge) { |
162 LocalArrayBufferTracker* tracker = page->local_tracker<Page::kDontCreate>(); | 94 size_t freed_memory = 0; |
163 if (tracker != nullptr) { | 95 Isolate* isolate = heap()->isolate(); |
164 tracker->FreeDead(); | 96 for (auto& buffer : not_yet_discovered_array_buffers_for_scavenge_) { |
| 97 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); |
| 98 freed_memory += buffer.second; |
| 99 live_array_buffers_for_scavenge_.erase(buffer.first); |
165 } | 100 } |
| 101 |
| 102 if (!from_scavenge) { |
| 103 for (auto& buffer : not_yet_discovered_array_buffers_) { |
| 104 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); |
| 105 freed_memory += buffer.second; |
| 106 live_array_buffers_.erase(buffer.first); |
| 107 } |
| 108 } |
| 109 |
| 110 not_yet_discovered_array_buffers_for_scavenge_ = |
| 111 live_array_buffers_for_scavenge_; |
| 112 if (!from_scavenge) not_yet_discovered_array_buffers_ = live_array_buffers_; |
| 113 |
| 114 // Do not call through the api as this code is triggered while doing a GC. |
| 115 heap()->update_amount_of_external_allocated_memory( |
| 116 -static_cast<int64_t>(freed_memory)); |
166 } | 117 } |
167 | 118 |
168 template <LocalArrayBufferTracker::LivenessIndicator liveness_indicator> | 119 |
169 void ArrayBufferTracker::ScanAndFreeDeadArrayBuffers(Page* page) { | 120 void ArrayBufferTracker::PrepareDiscoveryInNewSpace() { |
170 LocalArrayBufferTracker* tracker = page->local_tracker<Page::kDontCreate>(); | 121 not_yet_discovered_array_buffers_for_scavenge_ = |
171 if (tracker != nullptr) { | 122 live_array_buffers_for_scavenge_; |
172 base::LockGuard<base::Mutex> guard(page->mutex()); | |
173 tracker->ScanAndFreeDead<liveness_indicator>(); | |
174 } | |
175 } | 123 } |
176 | 124 |
177 template void ArrayBufferTracker::ScanAndFreeDeadArrayBuffers< | |
178 LocalArrayBufferTracker::LivenessIndicator::kForwardingPointer>(Page* page); | |
179 template void ArrayBufferTracker::ScanAndFreeDeadArrayBuffers< | |
180 LocalArrayBufferTracker::LivenessIndicator::kMarkBit>(Page* page); | |
181 template void ArrayBufferTracker::ScanAndFreeDeadArrayBuffers< | |
182 LocalArrayBufferTracker::LivenessIndicator::kForwardingPointerOrMarkBit>( | |
183 Page* page); | |
184 | 125 |
185 bool ArrayBufferTracker::IsTracked(JSArrayBuffer* buffer) { | 126 void ArrayBufferTracker::Promote(JSArrayBuffer* buffer) { |
186 Page* page = Page::FromAddress(buffer->address()); | 127 base::LockGuard<base::Mutex> guard(&mutex_); |
187 LocalArrayBufferTracker* tracker = | 128 |
188 page->local_tracker<Page::kCreateIfNotPresent>(); | 129 if (buffer->is_external()) return; |
189 { | 130 void* data = buffer->backing_store(); |
190 base::LockGuard<base::Mutex> guard(page->mutex()); | 131 if (!data) return; |
191 return tracker->IsTracked(buffer); | 132 // ArrayBuffer might be in the middle of being constructed. |
192 } | 133 if (data == heap()->undefined_value()) return; |
| 134 DCHECK(live_array_buffers_for_scavenge_.count(data) > 0); |
| 135 live_array_buffers_[data] = live_array_buffers_for_scavenge_[data]; |
| 136 live_array_buffers_for_scavenge_.erase(data); |
| 137 not_yet_discovered_array_buffers_for_scavenge_.erase(data); |
193 } | 138 } |
194 | 139 |
195 } // namespace internal | 140 } // namespace internal |
196 } // namespace v8 | 141 } // namespace v8 |
OLD | NEW |