Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: src/heap/array-buffer-tracker.cc

Issue 2026633003: Reland "[heap] Fine-grained JSArrayBuffer tracking" (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Tests should not assert for promotion decisions but only for tracking Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/heap/array-buffer-tracker.h ('k') | src/heap/heap.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/heap.h" 6 #include "src/heap/heap.h"
7 #include "src/isolate.h" 7 #include "src/isolate.h"
8 #include "src/objects-inl.h"
8 #include "src/objects.h" 9 #include "src/objects.h"
9 #include "src/objects-inl.h"
10 #include "src/v8.h" 10 #include "src/v8.h"
11 11
12 namespace v8 { 12 namespace v8 {
13 namespace internal { 13 namespace internal {
14 14
15 ArrayBufferTracker::~ArrayBufferTracker() { 15 LocalArrayBufferTracker::~LocalArrayBufferTracker() {
16 Isolate* isolate = heap()->isolate();
17 size_t freed_memory = 0; 16 size_t freed_memory = 0;
18 for (auto& buffer : live_array_buffers_) { 17 for (auto& buffer : array_buffers_) {
19 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); 18 heap_->isolate()->array_buffer_allocator()->Free(buffer.second.first,
20 freed_memory += buffer.second; 19 buffer.second.second);
20 freed_memory += buffer.second.second;
21 } 21 }
22 for (auto& buffer : live_array_buffers_for_scavenge_) { 22 if (freed_memory > 0) {
23 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); 23 heap_->update_amount_of_external_allocated_freed_memory(
24 freed_memory += buffer.second; 24 static_cast<intptr_t>(freed_memory));
25 } 25 }
26 live_array_buffers_.clear(); 26 array_buffers_.clear();
27 live_array_buffers_for_scavenge_.clear(); 27 }
28 not_yet_discovered_array_buffers_.clear();
29 not_yet_discovered_array_buffers_for_scavenge_.clear();
30 28
29 void LocalArrayBufferTracker::Add(Key key, const Value& value) {
30 array_buffers_[key] = value;
31 }
32
33 LocalArrayBufferTracker::Value LocalArrayBufferTracker::Remove(Key key) {
34 DCHECK_EQ(array_buffers_.count(key), 1);
35 Value value = array_buffers_[key];
36 array_buffers_.erase(key);
37 return value;
38 }
39
40 void LocalArrayBufferTracker::FreeDead() {
41 size_t freed_memory = 0;
42 for (TrackingMap::iterator it = array_buffers_.begin();
43 it != array_buffers_.end();) {
44 if (Marking::IsWhite(Marking::MarkBitFrom(it->first))) {
45 heap_->isolate()->array_buffer_allocator()->Free(it->second.first,
46 it->second.second);
47 freed_memory += it->second.second;
48 array_buffers_.erase(it++);
49 } else {
50 it++;
51 }
52 }
31 if (freed_memory > 0) { 53 if (freed_memory > 0) {
32 heap()->update_amount_of_external_allocated_memory( 54 heap_->update_amount_of_external_allocated_freed_memory(
33 -static_cast<int64_t>(freed_memory)); 55 static_cast<intptr_t>(freed_memory));
34 } 56 }
35 } 57 }
36 58
59 template <typename Callback>
60 void LocalArrayBufferTracker::Process(Callback callback) {
61 JSArrayBuffer* new_buffer = nullptr;
62 size_t freed_memory = 0;
63 for (TrackingMap::iterator it = array_buffers_.begin();
64 it != array_buffers_.end();) {
65 switch (callback(it->first, &new_buffer)) {
66 case kKeepEntry:
67 it++;
68 break;
69 case kUpdateEntry:
70 DCHECK_NOT_NULL(new_buffer);
71 Page::FromAddress(new_buffer->address())
72 ->local_tracker<Page::kCreateIfNotPresent>()
73 ->Add(new_buffer, it->second);
74 array_buffers_.erase(it++);
75 break;
76 case kRemoveEntry:
77 heap_->isolate()->array_buffer_allocator()->Free(it->second.first,
78 it->second.second);
79 freed_memory += it->second.second;
80 array_buffers_.erase(it++);
81 break;
82 default:
83 UNREACHABLE();
84 }
85 }
86 if (freed_memory > 0) {
87 heap_->update_amount_of_external_allocated_freed_memory(
88 static_cast<intptr_t>(freed_memory));
89 }
90 }
37 91
38 void ArrayBufferTracker::RegisterNew(JSArrayBuffer* buffer) { 92 void ArrayBufferTracker::RegisterNew(Heap* heap, JSArrayBuffer* buffer) {
39 void* data = buffer->backing_store(); 93 void* data = buffer->backing_store();
40 if (!data) return; 94 if (!data) return;
41 95
42 bool in_new_space = heap()->InNewSpace(buffer); 96 size_t length = NumberToSize(heap->isolate(), buffer->byte_length());
43 size_t length = NumberToSize(heap()->isolate(), buffer->byte_length()); 97 Page* page = Page::FromAddress(buffer->address());
44 if (in_new_space) { 98 LocalArrayBufferTracker* tracker =
45 live_array_buffers_for_scavenge_[data] = length; 99 page->local_tracker<Page::kCreateIfNotPresent>();
46 } else { 100 DCHECK_NOT_NULL(tracker);
47 live_array_buffers_[data] = length; 101 {
102 base::LockGuard<base::Mutex> guard(page->mutex());
103 tracker->Add(buffer, std::make_pair(data, length));
48 } 104 }
49
50 // We may go over the limit of externally allocated memory here. We call the 105 // We may go over the limit of externally allocated memory here. We call the
51 // api function to trigger a GC in this case. 106 // api function to trigger a GC in this case.
52 reinterpret_cast<v8::Isolate*>(heap()->isolate()) 107 reinterpret_cast<v8::Isolate*>(heap->isolate())
53 ->AdjustAmountOfExternalAllocatedMemory(length); 108 ->AdjustAmountOfExternalAllocatedMemory(length);
54 } 109 }
55 110
56 111 void ArrayBufferTracker::Unregister(Heap* heap, JSArrayBuffer* buffer) {
57 void ArrayBufferTracker::Unregister(JSArrayBuffer* buffer) {
58 void* data = buffer->backing_store(); 112 void* data = buffer->backing_store();
59 if (!data) return; 113 if (!data) return;
60 114
61 bool in_new_space = heap()->InNewSpace(buffer); 115 Page* page = Page::FromAddress(buffer->address());
62 std::map<void*, size_t>* live_buffers = 116 LocalArrayBufferTracker* tracker = page->local_tracker<Page::kDontCreate>();
63 in_new_space ? &live_array_buffers_for_scavenge_ : &live_array_buffers_; 117 DCHECK_NOT_NULL(tracker);
64 std::map<void*, size_t>* not_yet_discovered_buffers = 118 size_t length = 0;
65 in_new_space ? &not_yet_discovered_array_buffers_for_scavenge_ 119 {
66 : &not_yet_discovered_array_buffers_; 120 base::LockGuard<base::Mutex> guard(page->mutex());
67 121 length = tracker->Remove(buffer).second;
68 DCHECK(live_buffers->count(data) > 0); 122 }
69 123 heap->update_amount_of_external_allocated_memory(
70 size_t length = (*live_buffers)[data]; 124 -static_cast<intptr_t>(length));
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));
76 } 125 }
77 126
127 void ArrayBufferTracker::FreeDeadInNewSpace(Heap* heap) {
128 NewSpacePageIterator from_it(heap->new_space()->FromSpaceStart(),
129 heap->new_space()->FromSpaceEnd());
130 while (from_it.has_next()) {
131 ProcessBuffers(from_it.next(), kUpdateForwardedRemoveOthers);
132 }
133 heap->account_amount_of_external_allocated_freed_memory();
134 }
78 135
79 void ArrayBufferTracker::MarkLive(JSArrayBuffer* buffer) { 136 void ArrayBufferTracker::FreeDead(Page* page) {
80 base::LockGuard<base::Mutex> guard(&mutex_); 137 // Only called from the sweeper, which already holds the page lock.
81 void* data = buffer->backing_store(); 138 LocalArrayBufferTracker* tracker = page->local_tracker<Page::kDontCreate>();
82 139 if (tracker == nullptr) return;
83 // ArrayBuffer might be in the middle of being constructed. 140 DCHECK(!page->SweepingDone());
84 if (data == heap()->undefined_value()) return; 141 tracker->FreeDead();
85 if (heap()->InNewSpace(buffer)) { 142 if (tracker->IsEmpty()) {
86 not_yet_discovered_array_buffers_for_scavenge_.erase(data); 143 page->ReleaseLocalTracker();
87 } else {
88 not_yet_discovered_array_buffers_.erase(data);
89 } 144 }
90 } 145 }
91 146
92 147 void ArrayBufferTracker::ProcessBuffers(Page* page, ProcessingMode mode) {
93 void ArrayBufferTracker::FreeDead(bool from_scavenge) { 148 LocalArrayBufferTracker* tracker = page->local_tracker<Page::kDontCreate>();
94 size_t freed_memory = 0; 149 if (tracker == nullptr) return;
95 Isolate* isolate = heap()->isolate(); 150 {
96 for (auto& buffer : not_yet_discovered_array_buffers_for_scavenge_) { 151 base::LockGuard<base::Mutex> guard(page->mutex());
97 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); 152 tracker->Process(
98 freed_memory += buffer.second; 153 [mode](JSArrayBuffer* old_buffer, JSArrayBuffer** new_buffer) {
99 live_array_buffers_for_scavenge_.erase(buffer.first); 154 MapWord map_word = old_buffer->map_word();
155 if (map_word.IsForwardingAddress()) {
156 *new_buffer = JSArrayBuffer::cast(map_word.ToForwardingAddress());
157 return LocalArrayBufferTracker::kUpdateEntry;
158 }
159 return mode == kUpdateForwardedKeepOthers
160 ? LocalArrayBufferTracker::kKeepEntry
161 : LocalArrayBufferTracker::kRemoveEntry;
162 });
100 } 163 }
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));
117 } 164 }
118 165
119 166 bool ArrayBufferTracker::IsTracked(JSArrayBuffer* buffer) {
120 void ArrayBufferTracker::PrepareDiscoveryInNewSpace() { 167 Page* page = Page::FromAddress(buffer->address());
121 not_yet_discovered_array_buffers_for_scavenge_ = 168 LocalArrayBufferTracker* tracker =
122 live_array_buffers_for_scavenge_; 169 page->local_tracker<Page::kCreateIfNotPresent>();
123 } 170 {
124 171 base::LockGuard<base::Mutex> guard(page->mutex());
125 172 return tracker->IsTracked(buffer);
126 void ArrayBufferTracker::Promote(JSArrayBuffer* buffer) { 173 }
127 base::LockGuard<base::Mutex> guard(&mutex_);
128
129 if (buffer->is_external()) return;
130 void* data = buffer->backing_store();
131 if (!data) return;
132 // ArrayBuffer might be in the middle of being constructed.
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);
138 } 174 }
139 175
140 } // namespace internal 176 } // namespace internal
141 } // namespace v8 177 } // namespace v8
OLDNEW
« no previous file with comments | « src/heap/array-buffer-tracker.h ('k') | src/heap/heap.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698