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]; |
| 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", total_size_, allocation_count_, indent, ' '); |
| 79 if (tracker != NULL) { |
| 80 const char* name = "<unknown function>"; |
| 81 if (function_id_ != 0) { |
| 82 AllocationTracker::FunctionInfo* info = |
| 83 tracker->GetFunctionInfo(function_id_); |
| 84 if (info != NULL) { |
| 85 name = info->name; |
| 86 } |
| 87 } |
| 88 OS::Print("%s #%u", name, id_); |
| 89 } else { |
| 90 OS::Print("%u #%u", function_id_, id_); |
| 91 } |
| 92 OS::Print("\n"); |
| 93 indent += 2; |
| 94 for (int i = 0; i < children_.length(); i++) { |
| 95 children_[i]->Print(indent, tracker); |
| 96 } |
| 97 } |
| 98 |
| 99 |
| 100 AllocationTraceTree::AllocationTraceTree() |
| 101 : next_node_id_(1), |
| 102 root_(this, 0) { |
| 103 } |
| 104 |
| 105 |
| 106 AllocationTraceTree::~AllocationTraceTree() { |
| 107 } |
| 108 |
| 109 |
| 110 AllocationTraceNode* AllocationTraceTree::AddPathFromEnd( |
| 111 const Vector<SnapshotObjectId>& path) { |
| 112 AllocationTraceNode* node = root(); |
| 113 for (SnapshotObjectId* entry = path.start() + path.length() - 1; |
| 114 entry != path.start() - 1; |
| 115 --entry) { |
| 116 node = node->FindOrAddChild(*entry); |
| 117 } |
| 118 return node; |
| 119 } |
| 120 |
| 121 |
| 122 void AllocationTraceTree::Print(AllocationTracker* tracker) { |
| 123 OS::Print("[AllocationTraceTree:]\n"); |
| 124 OS::Print("Total size | Allocation count | Function id | id\n"); |
| 125 root()->Print(0, tracker); |
| 126 } |
| 127 |
| 128 void AllocationTracker::DeleteUnresolvedLocation( |
| 129 UnresolvedLocation** location) { |
| 130 delete *location; |
| 131 } |
| 132 |
| 133 |
| 134 AllocationTracker::FunctionInfo::FunctionInfo() |
| 135 : name(""), |
| 136 script_name(""), |
| 137 script_id(0), |
| 138 line(-1), |
| 139 column(-1) { |
| 140 } |
| 141 |
| 142 |
| 143 static bool AddressesMatch(void* key1, void* key2) { |
| 144 return key1 == key2; |
| 145 } |
| 146 |
| 147 |
| 148 AllocationTracker::AllocationTracker( |
| 149 HeapObjectsMap* ids, StringsStorage* names) |
| 150 : ids_(ids), |
| 151 names_(names), |
| 152 id_to_function_info_(AddressesMatch) { |
| 153 } |
| 154 |
| 155 |
| 156 AllocationTracker::~AllocationTracker() { |
| 157 unresolved_locations_.Iterate(DeleteUnresolvedLocation); |
| 158 } |
| 159 |
| 160 |
| 161 void AllocationTracker::PrepareForSerialization() { |
| 162 List<UnresolvedLocation*> copy(unresolved_locations_.length()); |
| 163 copy.AddAll(unresolved_locations_); |
| 164 unresolved_locations_.Clear(); |
| 165 for (int i = 0; i < copy.length(); i++) { |
| 166 copy[i]->Resolve(); |
| 167 delete copy[i]; |
| 168 } |
| 169 } |
| 170 |
| 171 |
| 172 void AllocationTracker::NewObjectEvent(Address addr, int size) { |
| 173 DisallowHeapAllocation no_allocation; |
| 174 Isolate* isolate = ids_->heap()->isolate(); |
| 175 int length = 0; |
| 176 StackTraceFrameIterator it(isolate); |
| 177 while (!it.done() && length < kMaxAllocationTraceLength) { |
| 178 JavaScriptFrame* frame = it.frame(); |
| 179 SharedFunctionInfo* shared = frame->function()->shared(); |
| 180 SnapshotObjectId id = ids_->FindEntry(shared->address()); |
| 181 allocation_trace_buffer_[length++] = id; |
| 182 AddFunctionInfo(shared, id); |
| 183 it.Advance(); |
| 184 } |
| 185 AllocationTraceNode* top_node = trace_tree_.AddPathFromEnd( |
| 186 Vector<SnapshotObjectId>(allocation_trace_buffer_, length)); |
| 187 top_node->AddAllocation(size); |
| 188 } |
| 189 |
| 190 |
| 191 static uint32_t SnapshotObjectIdHash(SnapshotObjectId id) { |
| 192 return ComputeIntegerHash(static_cast<uint32_t>(id), |
| 193 v8::internal::kZeroHashSeed); |
| 194 } |
| 195 |
| 196 |
| 197 AllocationTracker::FunctionInfo* AllocationTracker::GetFunctionInfo( |
| 198 SnapshotObjectId id) { |
| 199 HashMap::Entry* entry = id_to_function_info_.Lookup( |
| 200 reinterpret_cast<void*>(id), SnapshotObjectIdHash(id), false); |
| 201 if (entry == NULL) { |
| 202 return NULL; |
| 203 } |
| 204 return reinterpret_cast<FunctionInfo*>(entry->value); |
| 205 } |
| 206 |
| 207 |
| 208 void AllocationTracker::AddFunctionInfo(SharedFunctionInfo* shared, |
| 209 SnapshotObjectId id) { |
| 210 HashMap::Entry* entry = id_to_function_info_.Lookup( |
| 211 reinterpret_cast<void*>(id), SnapshotObjectIdHash(id), true); |
| 212 if (entry->value == NULL) { |
| 213 FunctionInfo* info = new FunctionInfo(); |
| 214 info->name = names_->GetFunctionName(shared->DebugName()); |
| 215 if (shared->script()->IsScript()) { |
| 216 Script* script = Script::cast(shared->script()); |
| 217 if (script->name()->IsName()) { |
| 218 Name* name = Name::cast(script->name()); |
| 219 info->script_name = names_->GetName(name); |
| 220 } |
| 221 info->script_id = script->id()->value(); |
| 222 // Converting start offset into line and column may cause heap |
| 223 // allocations so we postpone them until snapshot serialization. |
| 224 unresolved_locations_.Add(new UnresolvedLocation( |
| 225 script, |
| 226 shared->start_position(), |
| 227 info)); |
| 228 } |
| 229 entry->value = info; |
| 230 } |
| 231 } |
| 232 |
| 233 |
| 234 AllocationTracker::UnresolvedLocation::UnresolvedLocation( |
| 235 Script* script, int start, FunctionInfo* info) |
| 236 : start_position_(start), |
| 237 info_(info) { |
| 238 script_ = Handle<Script>::cast( |
| 239 script->GetIsolate()->global_handles()->Create(script)); |
| 240 GlobalHandles::MakeWeak( |
| 241 reinterpret_cast<Object**>(script_.location()), |
| 242 this, &HandleWeakScript); |
| 243 } |
| 244 |
| 245 |
| 246 AllocationTracker::UnresolvedLocation::~UnresolvedLocation() { |
| 247 if (!script_.is_null()) { |
| 248 script_->GetIsolate()->global_handles()->Destroy( |
| 249 reinterpret_cast<Object**>(script_.location())); |
| 250 } |
| 251 } |
| 252 |
| 253 |
| 254 void AllocationTracker::UnresolvedLocation::Resolve() { |
| 255 if (script_.is_null()) return; |
| 256 info_->line = GetScriptLineNumber(script_, start_position_); |
| 257 info_->column = GetScriptColumnNumber(script_, start_position_); |
| 258 } |
| 259 |
| 260 |
| 261 void AllocationTracker::UnresolvedLocation::HandleWeakScript( |
| 262 v8::Isolate* isolate, |
| 263 v8::Persistent<v8::Value>* obj, |
| 264 void* data) { |
| 265 UnresolvedLocation* location = reinterpret_cast<UnresolvedLocation*>(data); |
| 266 location->script_ = Handle<Script>::null(); |
| 267 obj->Dispose(); |
| 268 } |
| 269 |
| 270 |
| 271 } } // namespace v8::internal |
OLD | NEW |