Index: gin/v8_isolate_memory_dump_provider.cc |
diff --git a/gin/v8_isolate_memory_dump_provider.cc b/gin/v8_isolate_memory_dump_provider.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8bf2d5a700f20b2ecd38409c199c3578a8c4e47b |
--- /dev/null |
+++ b/gin/v8_isolate_memory_dump_provider.cc |
@@ -0,0 +1,104 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "gin/v8_isolate_memory_dump_provider.h" |
+ |
+#include <sstream> |
+ |
+#include "base/message_loop/message_loop.h" |
+#include "base/threading/platform_thread.h" |
+#include "base/trace_event/memory_dump_manager.h" |
+#include "base/trace_event/process_memory_dump.h" |
+#include "gin/public/isolate_holder.h" |
+#include "v8/include/v8.h" |
+ |
+namespace gin { |
+ |
+namespace { |
+const char kDumperFriendlyName[] = "V8IsolateHeap"; |
+const char kRootDumpName[] = "v8"; |
+const char kIsolateDumpName[] = "isolate"; |
+const char kAvailableSizeAttribute[] = "available_size_in_bytes"; |
+const char kPhysicalSizeAttribute[] = "physical_size_in_bytes"; |
+} // namespace |
+ |
+V8IsolateMemoryDumpProvider::V8IsolateMemoryDumpProvider( |
+ IsolateHolder* isolate_holder) |
+ : MemoryDumpProvider(base::MessageLoop::current()->task_runner()) { |
+ DeclareAllocatorAttribute(kRootDumpName, kAvailableSizeAttribute, "bytes"); |
+ DeclareAllocatorAttribute(kRootDumpName, kPhysicalSizeAttribute, "bytes"); |
+ base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
+ this); |
+ isolate_holder_ = isolate_holder; |
+} |
+ |
+V8IsolateMemoryDumpProvider::~V8IsolateMemoryDumpProvider() { |
+ base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( |
+ this); |
+} |
+ |
+// Called at trace dump point time. Creates a snapshot with the memory counters |
+// for the current isolate. |
+bool V8IsolateMemoryDumpProvider::DumpInto( |
+ base::trace_event::ProcessMemoryDump* pmd) { |
+ v8::HeapStatistics heap_statistics; |
+ isolate_holder_->isolate()->GetHeapStatistics(&heap_statistics); |
+ |
+ int thread_id = base::PlatformThread::CurrentId(); |
+ std::stringstream isolate_name; |
+ // TODO(ssid): The thread_id is soon not going to be unique enough if we get |
+ // more isolates per thread. |
+ isolate_name << kIsolateDumpName << '_' << thread_id; |
+ |
+ size_t known_spaces_used_size = 0; |
+ size_t known_spaces_size = 0; |
+ size_t number_of_spaces = isolate_holder_->isolate()->NumberOfHeapSpaces(); |
+ for (size_t space = 0; space < number_of_spaces; space++) { |
+ v8::HeapSpaceStatistics space_statistics; |
+ isolate_holder_->isolate()->GetHeapSpaceStatistics(&space_statistics, |
+ space); |
+ size_t space_size = space_statistics.space_size(); |
+ size_t space_used_size = space_statistics.space_used_size(); |
+ |
+ known_spaces_size += space_size; |
+ known_spaces_used_size += space_used_size; |
+ |
+ std::stringstream allocator_name; |
+ allocator_name << isolate_name.str() << '/' |
+ << space_statistics.space_name(); |
+ base::trace_event::MemoryAllocatorDump* space_dump = |
+ pmd->CreateAllocatorDump(kRootDumpName, allocator_name.str()); |
+ space_dump->set_physical_size_in_bytes(static_cast<int>((space_size))); |
+ space_dump->set_allocated_objects_count(0); |
+ space_dump->set_allocated_objects_size_in_bytes( |
+ static_cast<int>(space_used_size)); |
+ space_dump->SetAttribute( |
+ kAvailableSizeAttribute, |
+ static_cast<int>(space_statistics.space_available_size())); |
+ // TODO(ssid): This should probably be the physical_size_in_bytes. Check |
+ // with V8 owner. |
+ space_dump->SetAttribute( |
Primiano Tucci (use gerrit)
2015/04/21 17:13:39
Had a chat with Ross, just delete this attribute a
ssid
2015/04/21 17:38:23
Done.
|
+ kPhysicalSizeAttribute, |
+ static_cast<int>(space_statistics.physical_space_size())); |
+ } |
+ |
+ std::stringstream allocator_name; |
+ allocator_name << isolate_name.str() << '/' << "other_spaces"; |
+ // Compute the rest of the memory, not accounted by the spaces above. |
+ base::trace_event::MemoryAllocatorDump* space_dump = |
+ pmd->CreateAllocatorDump(kRootDumpName, allocator_name.str()); |
+ space_dump->set_physical_size_in_bytes(heap_statistics.total_heap_size() - |
+ known_spaces_size); |
+ space_dump->set_allocated_objects_count(0); |
+ space_dump->set_allocated_objects_size_in_bytes( |
+ heap_statistics.used_heap_size() - known_spaces_used_size); |
+ |
+ return true; |
+} |
+ |
+const char* V8IsolateMemoryDumpProvider::GetFriendlyName() const { |
+ return kDumperFriendlyName; |
+} |
+ |
+} // namespace gin |