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

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

Issue 1936233002: [heap] Fine-grained JSArrayBuffer tracking (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Addressed comment Created 4 years, 7 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.cc » ('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.h" 8 #include "src/objects.h"
9 #include "src/objects-inl.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 ArrayBufferTracker::~ArrayBufferTracker() {
16 Isolate* isolate = heap()->isolate(); 16 Isolate* isolate = heap()->isolate();
17 size_t freed_memory = 0; 17 size_t freed_memory = 0;
18 for (auto& buffer : live_array_buffers_) { 18 for (auto& buffer : live_old_gen_) {
19 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); 19 isolate->array_buffer_allocator()->Free(buffer.second.first,
20 freed_memory += buffer.second; 20 buffer.second.second);
21 freed_memory += buffer.second.second;
21 } 22 }
22 for (auto& buffer : live_array_buffers_for_scavenge_) { 23 for (auto& buffer : live_young_gen_) {
23 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); 24 isolate->array_buffer_allocator()->Free(buffer.second.first,
24 freed_memory += buffer.second; 25 buffer.second.second);
26 freed_memory += buffer.second.second;
25 } 27 }
26 live_array_buffers_.clear(); 28 live_old_gen_.clear();
27 live_array_buffers_for_scavenge_.clear(); 29 live_young_gen_.clear();
28 not_yet_discovered_array_buffers_.clear(); 30 not_yet_discovered_old_gen_.clear();
29 not_yet_discovered_array_buffers_for_scavenge_.clear(); 31 not_yet_discovered_young_gen_.clear();
30 32
31 if (freed_memory > 0) { 33 if (freed_memory > 0) {
32 heap()->update_amount_of_external_allocated_memory( 34 heap()->update_amount_of_external_allocated_memory(
33 -static_cast<int64_t>(freed_memory)); 35 -static_cast<int64_t>(freed_memory));
34 } 36 }
35 } 37 }
36 38
37 39
38 void ArrayBufferTracker::RegisterNew(JSArrayBuffer* buffer) { 40 void ArrayBufferTracker::RegisterNew(JSArrayBuffer* buffer) {
39 void* data = buffer->backing_store(); 41 void* data = buffer->backing_store();
40 if (!data) return; 42 if (!data) return;
41 43
42 bool in_new_space = heap()->InNewSpace(buffer); 44 bool in_new_space = heap()->InNewSpace(buffer);
43 size_t length = NumberToSize(heap()->isolate(), buffer->byte_length()); 45 size_t length = NumberToSize(heap()->isolate(), buffer->byte_length());
44 if (in_new_space) { 46 if (in_new_space) {
45 live_array_buffers_for_scavenge_[data] = length; 47 live_young_gen_[buffer->address()] = std::make_pair(data, length);
48 not_yet_discovered_young_gen_[buffer->address()] =
49 std::make_pair(data, length);
46 } else { 50 } else {
47 live_array_buffers_[data] = length; 51 live_old_gen_[buffer->address()] = std::make_pair(data, length);
52 not_yet_discovered_old_gen_[buffer->address()] =
53 std::make_pair(data, length);
48 } 54 }
49 55
50 // We may go over the limit of externally allocated memory here. We call the 56 // We may go over the limit of externally allocated memory here. We call the
51 // api function to trigger a GC in this case. 57 // api function to trigger a GC in this case.
52 reinterpret_cast<v8::Isolate*>(heap()->isolate()) 58 reinterpret_cast<v8::Isolate*>(heap()->isolate())
53 ->AdjustAmountOfExternalAllocatedMemory(length); 59 ->AdjustAmountOfExternalAllocatedMemory(length);
54 } 60 }
55 61
56 62
57 void ArrayBufferTracker::Unregister(JSArrayBuffer* buffer) { 63 void ArrayBufferTracker::Unregister(JSArrayBuffer* buffer) {
58 void* data = buffer->backing_store(); 64 void* data = buffer->backing_store();
59 if (!data) return; 65 if (!data) return;
60 66
61 bool in_new_space = heap()->InNewSpace(buffer); 67 bool in_new_space = heap()->InNewSpace(buffer);
62 std::map<void*, size_t>* live_buffers = 68 Key key = buffer->address();
63 in_new_space ? &live_array_buffers_for_scavenge_ : &live_array_buffers_; 69 TrackingMap* live_buffers = in_new_space ? &live_young_gen_ : &live_old_gen_;
64 std::map<void*, size_t>* not_yet_discovered_buffers = 70 TrackingMap* not_yet_discovered_buffers = in_new_space
65 in_new_space ? &not_yet_discovered_array_buffers_for_scavenge_ 71 ? &not_yet_discovered_young_gen_
66 : &not_yet_discovered_array_buffers_; 72 : &not_yet_discovered_old_gen_;
67 73
68 DCHECK(live_buffers->count(data) > 0); 74 DCHECK(live_buffers->count(key) > 0);
69 75
70 size_t length = (*live_buffers)[data]; 76 size_t length = (*live_buffers)[key].second;
71 live_buffers->erase(data); 77 live_buffers->erase(key);
72 not_yet_discovered_buffers->erase(data); 78 not_yet_discovered_buffers->erase(key);
73 79
74 heap()->update_amount_of_external_allocated_memory( 80 heap()->update_amount_of_external_allocated_memory(
75 -static_cast<int64_t>(length)); 81 -static_cast<int64_t>(length));
76 } 82 }
77 83
78
79 void ArrayBufferTracker::MarkLive(JSArrayBuffer* buffer) {
80 base::LockGuard<base::Mutex> guard(&mutex_);
81 void* data = buffer->backing_store();
82
83 // ArrayBuffer might be in the middle of being constructed.
84 if (data == heap()->undefined_value()) return;
85 if (heap()->InNewSpace(buffer)) {
86 not_yet_discovered_array_buffers_for_scavenge_.erase(data);
87 } else {
88 not_yet_discovered_array_buffers_.erase(data);
89 }
90 }
91
92
93 void ArrayBufferTracker::FreeDead(bool from_scavenge) { 84 void ArrayBufferTracker::FreeDead(bool from_scavenge) {
94 size_t freed_memory = 0; 85 size_t freed_memory = 0;
95 Isolate* isolate = heap()->isolate(); 86 Isolate* isolate = heap()->isolate();
96 for (auto& buffer : not_yet_discovered_array_buffers_for_scavenge_) { 87 for (auto& buffer : not_yet_discovered_young_gen_) {
97 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); 88 isolate->array_buffer_allocator()->Free(buffer.second.first,
98 freed_memory += buffer.second; 89 buffer.second.second);
99 live_array_buffers_for_scavenge_.erase(buffer.first); 90 freed_memory += buffer.second.second;
91 live_young_gen_.erase(buffer.first);
100 } 92 }
101 93
102 if (!from_scavenge) { 94 if (!from_scavenge) {
103 for (auto& buffer : not_yet_discovered_array_buffers_) { 95 for (auto& buffer : not_yet_discovered_old_gen_) {
104 isolate->array_buffer_allocator()->Free(buffer.first, buffer.second); 96 isolate->array_buffer_allocator()->Free(buffer.second.first,
105 freed_memory += buffer.second; 97 buffer.second.second);
106 live_array_buffers_.erase(buffer.first); 98 freed_memory += buffer.second.second;
99 live_old_gen_.erase(buffer.first);
107 } 100 }
108 } 101 }
109 102
110 not_yet_discovered_array_buffers_for_scavenge_ = 103 not_yet_discovered_young_gen_ = live_young_gen_;
111 live_array_buffers_for_scavenge_; 104 if (!from_scavenge) not_yet_discovered_old_gen_ = live_old_gen_;
112 if (!from_scavenge) not_yet_discovered_array_buffers_ = live_array_buffers_;
113 105
114 // Do not call through the api as this code is triggered while doing a GC. 106 // Do not call through the api as this code is triggered while doing a GC.
115 heap()->update_amount_of_external_allocated_memory( 107 heap()->update_amount_of_external_allocated_memory(
116 -static_cast<int64_t>(freed_memory)); 108 -static_cast<int64_t>(freed_memory));
117 } 109 }
118 110
111 #define UPDATE_GUARD(buffer, data) \
112 if (buffer->is_external()) return; \
113 data = buffer->backing_store(); \
114 if (data == nullptr) return; \
115 if (data == heap()->undefined_value()) return; \
116 base::LockGuard<base::Mutex> guard(&mutex_);
119 117
120 void ArrayBufferTracker::PrepareDiscoveryInNewSpace() { 118 void ArrayBufferTracker::MarkLive(JSArrayBuffer* buffer) {
121 not_yet_discovered_array_buffers_for_scavenge_ = 119 void* data = nullptr;
122 live_array_buffers_for_scavenge_; 120 UPDATE_GUARD(buffer, data);
121
122 if (heap()->InNewSpace(buffer)) {
123 not_yet_discovered_young_gen_.erase(buffer->address());
124 } else {
125 not_yet_discovered_old_gen_.erase(buffer->address());
126 }
123 } 127 }
124 128
129 void ArrayBufferTracker::Promote(JSArrayBuffer* new_buffer,
130 JSArrayBuffer* old_buffer) {
131 void* data = nullptr;
132 UPDATE_GUARD(new_buffer, data);
125 133
126 void ArrayBufferTracker::Promote(JSArrayBuffer* buffer) { 134 Key new_key = new_buffer->address();
127 base::LockGuard<base::Mutex> guard(&mutex_); 135 Key old_key = old_buffer->address();
136 DCHECK(live_young_gen_.count(old_key) > 0);
137 live_old_gen_[new_key] = live_young_gen_[old_key];
138 live_young_gen_.erase(old_key);
139 not_yet_discovered_young_gen_.erase(old_key);
140 }
128 141
129 if (buffer->is_external()) return; 142 void ArrayBufferTracker::Compact(JSArrayBuffer* new_buffer,
130 void* data = buffer->backing_store(); 143 JSArrayBuffer* old_buffer) {
131 if (!data) return; 144 void* data = nullptr;
132 // ArrayBuffer might be in the middle of being constructed. 145 UPDATE_GUARD(new_buffer, data);
133 if (data == heap()->undefined_value()) return; 146
134 DCHECK(live_array_buffers_for_scavenge_.count(data) > 0); 147 Key new_key = new_buffer->address();
135 live_array_buffers_[data] = live_array_buffers_for_scavenge_[data]; 148 Key old_key = old_buffer->address();
136 live_array_buffers_for_scavenge_.erase(data); 149 DCHECK_NE(new_key, old_key);
137 not_yet_discovered_array_buffers_for_scavenge_.erase(data); 150 DCHECK(live_old_gen_.count(old_key) > 0);
151 live_old_gen_[new_key] = live_old_gen_[old_key];
152 live_old_gen_.erase(old_key);
153 not_yet_discovered_old_gen_.erase(old_key);
154 }
155
156 void ArrayBufferTracker::SemiSpaceCopy(JSArrayBuffer* new_buffer,
157 JSArrayBuffer* old_buffer) {
158 void* data = nullptr;
159 UPDATE_GUARD(new_buffer, data);
160
161 Key new_key = new_buffer->address();
162 Key old_key = old_buffer->address();
163 DCHECK(live_young_gen_.count(old_key) > 0);
164 live_young_gen_[new_key] = live_young_gen_[old_key];
165 live_young_gen_.erase(old_key);
166 not_yet_discovered_young_gen_.erase(old_key);
167 }
168
169 #undef UPDATE_GUARD
170
171 bool ArrayBufferTracker::IsTrackedInOldGenForTesting(JSArrayBuffer* buffer) {
172 return live_old_gen_.find(buffer->address()) != live_old_gen_.end();
173 }
174
175 bool ArrayBufferTracker::IsTrackedInYoungGenForTesting(JSArrayBuffer* buffer) {
176 return live_young_gen_.find(buffer->address()) != live_young_gen_.end();
138 } 177 }
139 178
140 } // namespace internal 179 } // namespace internal
141 } // namespace v8 180 } // namespace v8
OLDNEW
« no previous file with comments | « src/heap/array-buffer-tracker.h ('k') | src/heap/heap.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698