OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 the V8 project authors. All rights reserved. | |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | |
28 #include "v8.h" | |
29 | |
30 #include "allocation-tracker.h" | |
31 | |
32 #include "heap-snapshot-generator.h" | |
33 #include "frames-inl.h" | |
34 | |
35 namespace v8 { | |
36 namespace internal { | |
37 | |
38 AllocationTraceNode::AllocationTraceNode( | |
39 AllocationTraceTree* tree, SnapshotObjectId shared_function_info_id) | |
40 : tree_(tree), | |
41 function_id_(shared_function_info_id), | |
42 total_size_(0), | |
43 allocation_count_(0), | |
44 id_(tree->next_node_id()) { | |
45 } | |
46 | |
47 | |
48 AllocationTraceNode::~AllocationTraceNode() { | |
49 } | |
50 | |
51 | |
52 AllocationTraceNode* AllocationTraceNode::FindChild(SnapshotObjectId id) { | |
53 for (int i = 0; i < children_.length(); i++) { | |
54 AllocationTraceNode* node = children_[i]; | |
loislo
2013/10/18 08:30:38
what are you expectations about the average number
yurys
2013/10/18 09:04:32
I'd expect it to be <10, if we see some nodes with
| |
55 if (node->function_id() == id) return node; | |
56 } | |
57 return NULL; | |
58 } | |
59 | |
60 | |
61 AllocationTraceNode* AllocationTraceNode::FindOrAddChild(SnapshotObjectId id) { | |
62 AllocationTraceNode* child = FindChild(id); | |
63 if (child == NULL) { | |
64 child = new AllocationTraceNode(tree_, id); | |
65 children_.Add(child); | |
66 } | |
67 return child; | |
68 } | |
69 | |
70 | |
71 void AllocationTraceNode::AddAllocation(unsigned size) { | |
72 total_size_ += size; | |
73 ++allocation_count_; | |
74 } | |
75 | |
76 | |
77 void AllocationTraceNode::Print(int indent, AllocationTracker* tracker) { | |
78 OS::Print("%10u %10u %*c", | |
79 total_size_, allocation_count_, | |
80 indent, ' '); | |
81 if (tracker != NULL) { | |
82 const char* name = "<unknown function>"; | |
83 if (function_id_ != 0) { | |
84 AllocationTracker::FunctionInfo* info = | |
85 tracker->GetFunctionInfo(function_id_); | |
86 if (info != NULL) { | |
87 name = info->name; | |
88 } | |
89 } | |
90 OS::Print("%s #%u", name, id_); | |
91 } else { | |
92 OS::Print("%u #%u", function_id_, id_); | |
93 } | |
94 OS::Print("\n"); | |
95 indent += 2; | |
96 for (int i = 0; i < children_.length(); i++) { | |
97 children_[i]->Print(indent, tracker); | |
98 } | |
99 } | |
100 | |
101 | |
102 AllocationTraceTree::AllocationTraceTree() | |
103 : next_node_id_(1), | |
104 root_(this, 0) { | |
105 } | |
106 | |
107 | |
108 AllocationTraceTree::~AllocationTraceTree() { | |
109 } | |
110 | |
111 | |
112 AllocationTraceNode* AllocationTraceTree::AddPathFromEnd( | |
113 const Vector<SnapshotObjectId>& path) { | |
114 AllocationTraceNode* node = root(); | |
115 for (SnapshotObjectId* entry = path.start() + path.length() - 1; | |
116 entry != path.start() - 1; | |
117 --entry) { | |
118 node = node->FindOrAddChild(*entry); | |
119 } | |
120 return node; | |
121 } | |
122 | |
123 | |
124 void AllocationTraceTree::Print(AllocationTracker* tracker) { | |
125 OS::Print("[AllocationTraceTree:]\n"); | |
126 OS::Print("Total size | Allocation count | Function id | id\n"); | |
127 root()->Print(0, tracker); | |
128 } | |
129 | |
130 void AllocationTracker::DeleteUnresolvedLocation( | |
131 UnresolvedLocation** location) { | |
132 delete *location; | |
133 } | |
134 | |
135 | |
136 AllocationTracker::FunctionInfo::FunctionInfo() | |
137 : name(""), | |
138 script_name(""), | |
139 script_id(0), | |
140 line(-1), | |
141 column(-1) { | |
142 } | |
143 | |
144 | |
145 static bool AddressesMatch(void* key1, void* key2) { | |
146 return key1 == key2; | |
147 } | |
148 | |
149 | |
150 AllocationTracker::AllocationTracker( | |
151 HeapObjectsMap* ids, StringsStorage* names) | |
152 : ids_(ids), | |
153 names_(names), | |
154 id_to_function_info_(AddressesMatch) { | |
155 } | |
156 | |
157 | |
158 AllocationTracker::~AllocationTracker() { | |
159 unresolved_locations_.Iterate(DeleteUnresolvedLocation); | |
160 } | |
161 | |
162 | |
163 void AllocationTracker::PrepareForSerialization() { | |
164 List<UnresolvedLocation*> copy(unresolved_locations_.length()); | |
165 copy.AddAll(unresolved_locations_); | |
166 unresolved_locations_.Clear(); | |
167 for (int i = 0; i < copy.length(); i++) { | |
168 copy[i]->Resolve(); | |
169 delete copy[i]; | |
170 } | |
171 } | |
172 | |
173 | |
174 void AllocationTracker::NewObjectEvent(Address addr, int size) { | |
175 DisallowHeapAllocation no_allocation; | |
176 Isolate* isolate = ids_->heap()->isolate(); | |
177 int length = 0; | |
178 StackTraceFrameIterator it(isolate); | |
179 while (!it.done() && length < kMaxAllocationTraceLength) { | |
180 JavaScriptFrame* frame = it.frame(); | |
181 SharedFunctionInfo* shared = frame->function()->shared(); | |
182 SnapshotObjectId id = ids_->FindEntry(shared->address()); | |
183 allocation_trace_buffer_[length++] = id; | |
184 AddFunctionInfo(shared, id); | |
185 it.Advance(); | |
186 } | |
187 AllocationTraceNode* top_node = allocation_traces_.AddPathFromEnd( | |
188 Vector<SnapshotObjectId>(allocation_trace_buffer_, length)); | |
189 top_node->AddAllocation(size); | |
190 } | |
191 | |
192 | |
193 static uint32_t SnapshotObjectIdHash(SnapshotObjectId id) { | |
194 return ComputeIntegerHash(static_cast<uint32_t>(id), | |
195 v8::internal::kZeroHashSeed); | |
196 } | |
197 | |
198 | |
199 AllocationTracker::FunctionInfo* AllocationTracker::GetFunctionInfo( | |
200 SnapshotObjectId id) { | |
201 HashMap::Entry* entry = id_to_function_info_.Lookup( | |
202 reinterpret_cast<void*>(id), SnapshotObjectIdHash(id), false); | |
203 if (entry == NULL) { | |
204 return NULL; | |
205 } | |
206 return reinterpret_cast<FunctionInfo*>(entry->value); | |
207 } | |
208 | |
209 | |
210 void AllocationTracker::AddFunctionInfo(SharedFunctionInfo* shared, | |
211 SnapshotObjectId id) { | |
212 HashMap::Entry* entry = id_to_function_info_.Lookup( | |
213 reinterpret_cast<void*>(id), SnapshotObjectIdHash(id), true); | |
214 if (entry->value == NULL) { | |
215 FunctionInfo* info = new FunctionInfo(); | |
216 info->name = names_->GetFunctionName(shared->DebugName()); | |
217 if (shared->script()->IsScript()) { | |
218 Script* script = Script::cast(shared->script()); | |
219 if (script->name()->IsName()) { | |
220 Name* name = Name::cast(script->name()); | |
221 info->script_name = names_->GetName(name); | |
222 } | |
223 info->script_id = script->id()->value(); | |
224 // Converting start offset into line and column may cause heap | |
225 // allocations so we postpone them until snapshot serialization. | |
226 unresolved_locations_.Add(new UnresolvedLocation( | |
227 script, | |
228 shared->start_position(), | |
229 info)); | |
230 } | |
231 entry->value = info; | |
232 } | |
233 } | |
234 | |
235 | |
236 AllocationTracker::UnresolvedLocation::UnresolvedLocation( | |
237 Script* script, int start, FunctionInfo* info) | |
238 : start_position_(start), | |
239 info_(info) { | |
240 script_ = Handle<Script>::cast( | |
241 script->GetIsolate()->global_handles()->Create(script)); | |
242 GlobalHandles::MakeWeak( | |
243 reinterpret_cast<Object**>(script_.location()), | |
244 this, &HandleWeakScript); | |
245 } | |
246 | |
247 | |
248 AllocationTracker::UnresolvedLocation::~UnresolvedLocation() { | |
249 if (!script_.is_null()) { | |
250 script_->GetIsolate()->global_handles()->Destroy( | |
251 reinterpret_cast<Object**>(script_.location())); | |
252 } | |
253 } | |
254 | |
255 | |
256 void AllocationTracker::UnresolvedLocation::Resolve() { | |
257 if (script_.is_null()) return; | |
258 info_->line = GetScriptLineNumber(script_, start_position_); | |
259 info_->column = GetScriptColumnNumber(script_, start_position_); | |
260 } | |
261 | |
262 | |
263 void AllocationTracker::UnresolvedLocation::HandleWeakScript( | |
264 v8::Isolate* isolate, | |
265 v8::Persistent<v8::Value>* obj, | |
266 void* data) { | |
267 UnresolvedLocation* location = reinterpret_cast<UnresolvedLocation*>(data); | |
268 location->script_ = Handle<Script>::null(); | |
269 obj->Dispose(); | |
270 } | |
271 | |
272 | |
273 } } // namespace v8::internal | |
OLD | NEW |