Chromium Code Reviews| Index: src/allocation-tracker.cc |
| diff --git a/src/allocation-tracker.cc b/src/allocation-tracker.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..222e30289d617fbd1a3b4edef0018c533ba2e851 |
| --- /dev/null |
| +++ b/src/allocation-tracker.cc |
| @@ -0,0 +1,242 @@ |
| +// Copyright 2013 the V8 project authors. All rights reserved. |
| +// Redistribution and use in source and binary forms, with or without |
| +// modification, are permitted provided that the following conditions are |
| +// met: |
| +// |
| +// * Redistributions of source code must retain the above copyright |
| +// notice, this list of conditions and the following disclaimer. |
| +// * Redistributions in binary form must reproduce the above |
| +// copyright notice, this list of conditions and the following |
| +// disclaimer in the documentation and/or other materials provided |
| +// with the distribution. |
| +// * Neither the name of Google Inc. nor the names of its |
| +// contributors may be used to endorse or promote products derived |
| +// from this software without specific prior written permission. |
| +// |
| +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| + |
| +#include "v8.h" |
| + |
| +#include "allocation-tracker.h" |
| + |
| +#include "frames-inl.h" |
| + |
| +namespace v8 { |
| +namespace internal { |
| + |
| +AllocationTraceNode::AllocationTraceNode( |
| + AllocationTraceTree* tree, SnapshotObjectId shared_function_info_id) |
| + : tree_(tree), |
| + function_id_(shared_function_info_id), |
| + total_size_(0), |
| + allocation_count_(0), |
| + id_(tree->next_node_id()) { |
| +} |
| + |
| + |
| +AllocationTraceNode::~AllocationTraceNode() { |
| +} |
| + |
| + |
| +AllocationTraceNode* AllocationTraceNode::FindChild(SnapshotObjectId id) { |
| + for (int i = 0; i < children_.length(); i++) { |
| + AllocationTraceNode* node = children_[i]; |
| + if (node->function_id() == id) return node; |
| + } |
| + return NULL; |
| +} |
| + |
| + |
| +AllocationTraceNode* AllocationTraceNode::FindOrAddChild(SnapshotObjectId id) { |
| + AllocationTraceNode* child = FindChild(id); |
| + if (child == NULL) { |
| + child = new AllocationTraceNode(tree_, id); |
| + children_.Add(child); |
| + } |
| + return child; |
| +} |
| + |
| + |
| +void AllocationTraceNode::AddAllocation(unsigned size) { |
| + total_size_ += size; |
| + ++allocation_count_; |
| +} |
| + |
| + |
| +void AllocationTraceNode::Print(int indent) { |
| + OS::Print("%10u %10u %*c %u #%u", |
| + total_size_, allocation_count_, |
| + indent, ' ', |
| + function_id_, |
| + id_); |
| + OS::Print("\n"); |
| + indent += 2; |
| + for (int i = 0; i < children_.length(); i++) { |
| + children_[i]->Print(indent); |
| + } |
| +} |
| + |
| + |
| +AllocationTraceTree::AllocationTraceTree() |
| + : next_node_id_(1), |
| + root_(this, 0) { |
| +} |
| + |
| + |
| +AllocationTraceTree::~AllocationTraceTree() { |
| +} |
| + |
| + |
| +AllocationTraceNode* AllocationTraceTree::AddPathFromEnd( |
| + const Vector<SnapshotObjectId>& path) { |
| + AllocationTraceNode* node = root(); |
| + for (SnapshotObjectId* entry = path.start() + path.length() - 1; |
| + entry != path.start() - 1; |
| + --entry) { |
| + node = node->FindOrAddChild(*entry); |
| + } |
| + return node; |
| +} |
| + |
| + |
| +void AllocationTraceTree::Print() { |
| + OS::Print("[AllocationTraceTree:]\n"); |
| + OS::Print("Total size | Allocation count | Function id | id\n"); |
| + root()->Print(0); |
| +} |
| + |
| +void AllocationTracker::DeleteUnresolvedLocation( |
| + UnresolvedLocation** location) { |
| + delete *location; |
| +} |
| + |
| + |
| +static bool AddressesMatch(void* key1, void* key2) { |
| + return key1 == key2; |
| +} |
| + |
| + |
| +AllocationTracker::AllocationTracker( |
| + HeapObjectsMap* ids, StringsStorage* names) |
| + : ids_(ids), |
| + names_(names), |
| + id_to_function_info_(AddressesMatch) { |
| +} |
| + |
| + |
| +AllocationTracker::~AllocationTracker() { |
| + unresolved_locations_.Iterate(DeleteUnresolvedLocation); |
| +} |
| + |
| + |
| +void AllocationTracker::PrepareForSerialization() { |
| + List<UnresolvedLocation*> copy(unresolved_locations_.length()); |
| + copy.AddAll(unresolved_locations_); |
| + unresolved_locations_.Clear(); |
| + for (int i = 0; i < copy.length(); i++) { |
| + copy[i]->Resolve(); |
| + delete copy[i]; |
| + } |
| +} |
| + |
| + |
| +void AllocationTracker::NewObjectEvent(Address addr, int size) { |
| + DisallowHeapAllocation no_allocation; |
| + Isolate* isolate = ids_->heap()->isolate(); |
| + int length = 0; |
| + StackTraceFrameIterator it(isolate); |
| + while (!it.done() && length < kMaxAllocationTraceLength) { |
| + JavaScriptFrame* frame = it.frame(); |
| + SharedFunctionInfo* shared = frame->function()->shared(); |
| + SnapshotObjectId id = ids_->FindEntry(shared->address()); |
| + allocation_trace_buffer_[length++] = id; |
| + AddFunctionInfo(shared, id); |
| + it.Advance(); |
| + } |
| + AllocationTraceNode* top_node = allocation_traces_.AddPathFromEnd( |
| + Vector<SnapshotObjectId>(allocation_trace_buffer_, length)); |
| + top_node->AddAllocation(size); |
| +} |
| + |
| + |
| +static uint32_t SnapshotObjectIdHash(SnapshotObjectId id) { |
| + return ComputeIntegerHash(static_cast<uint32_t>(id), |
| + v8::internal::kZeroHashSeed); |
| +} |
| + |
| + |
| +void AllocationTracker::AddFunctionInfo(SharedFunctionInfo* shared, |
| + SnapshotObjectId id) { |
| + HashMap::Entry* entry = id_to_function_info_.Lookup( |
| + reinterpret_cast<void*>(id), SnapshotObjectIdHash(id), true); |
| + if (entry->value == NULL) { |
| + FunctionInfo* info = new FunctionInfo(); |
| + info->name = names_->GetFunctionName(shared->DebugName()); |
| + if (shared->script()->IsScript()) { |
| + Script* script = Script::cast(shared->script()); |
| + if (script->name()->IsName()) { |
| + Name* name = Name::cast(script->name()); |
| + info->script_name = names_->GetFunctionName(name); |
| + } else { |
| + script->name()->PrintLn(); |
| + printf("script->name is not a name\n"); |
|
loislo
2013/10/16 15:14:08
please remove this
yurys
2013/10/17 15:27:35
Done.
|
| + } |
| + info->script_id = script->id()->value(); |
| + // Converting start offset into line and column may cause heap allocations. |
|
loislo
2013/10/16 15:14:08
80 symbols
yurys
2013/10/17 15:27:35
Done.
|
| + unresolved_locations_.Add(new UnresolvedLocation( |
| + script, |
| + shared->start_position(), |
| + info)); |
| + } |
| + entry->value = info; |
| + } |
| +} |
| + |
| + |
| +AllocationTracker::UnresolvedLocation::UnresolvedLocation( |
| + Script* script, int start, FunctionInfo* info) |
| + : start_position_(start), |
| + info_(info) { |
| + script_ = script->GetIsolate()->global_handles()->Create(script); |
| + GlobalHandles::MakeWeak( |
| + reinterpret_cast<Object**>(script_.location()), |
| + this, &HandleWeakScript); |
| +} |
| + |
| + |
| +AllocationTracker::UnresolvedLocation::~UnresolvedLocation() { |
| + if (!script_.is_null()) { |
| + script_->GetIsolate()->global_handles()->Destroy( |
| + reinterpret_cast<Object**>(script_.location())); |
| + } |
| +} |
| + |
| + |
| +void AllocationTracker::UnresolvedLocation::Resolve() { |
| + if (script_.is_null()) return; |
| + info_->line = GetScriptLineNumber(script_, start_position_); |
| + info_->column = GetScriptColumnNumber(script_, start_position_); |
| +} |
| + |
| + |
| +void AllocationTracker::UnresolvedLocation::HandleWeakScript( |
| + v8::Isolate* isolate, |
| + v8::Persistent<v8::Value>* obj, |
| + void* data) { |
| + UnresolvedLocation* location = reinterpret_cast<UnresolvedLocation*>(data); |
| + location->script_ = Handle<Script>::null(); |
| + obj->Dispose(); |
| +} |
| + |
| + |
| +} } // namespace v8::internal |