Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "gin/v8_isolate_memory_dump_provider.h" | |
| 6 | |
| 7 #include "base/message_loop/message_loop.h" | |
| 8 #include "base/strings/stringprintf.h" | |
| 9 #include "base/thread_task_runner_handle.h" | |
| 10 #include "base/trace_event/memory_dump_manager.h" | |
| 11 #include "base/trace_event/process_memory_dump.h" | |
| 12 #include "gin/public/isolate_holder.h" | |
| 13 #include "v8/include/v8.h" | |
| 14 | |
| 15 namespace gin { | |
| 16 | |
| 17 namespace { | |
| 18 const char kRootDumpName[] = "v8"; | |
| 19 const char kIsolateDumpName[] = "isolate"; | |
| 20 const char kAvailableSizeAttribute[] = "available_size_in_bytes"; | |
| 21 } // namespace | |
| 22 | |
| 23 V8IsolateMemoryDumpProvider::V8IsolateMemoryDumpProvider( | |
| 24 IsolateHolder* isolate_holder) | |
| 25 : isolate_holder_(isolate_holder) { | |
| 26 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | |
| 27 this, base::ThreadTaskRunnerHandle::Get()); | |
| 28 } | |
| 29 | |
| 30 V8IsolateMemoryDumpProvider::~V8IsolateMemoryDumpProvider() { | |
| 31 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( | |
| 32 this); | |
| 33 } | |
| 34 | |
| 35 // Called at trace dump point time. Creates a snapshot with the memory counters | |
| 36 // for the current isolate. | |
| 37 bool V8IsolateMemoryDumpProvider::OnMemoryDump( | |
| 38 base::trace_event::ProcessMemoryDump* pmd) { | |
| 39 if (isolate_holder_->access_mode() == IsolateHolder::kUseLocker) { | |
| 40 v8::Locker locked(isolate_holder_->isolate()); | |
| 41 DumpMemoryStatistics_(pmd); | |
| 42 } else { | |
| 43 DumpMemoryStatistics_(pmd); | |
| 44 } | |
| 45 return true; | |
| 46 } | |
| 47 | |
| 48 void V8IsolateMemoryDumpProvider::DumpMemoryStatistics_( | |
| 49 base::trace_event::ProcessMemoryDump* pmd) { | |
| 50 v8::HeapStatistics heap_statistics; | |
| 51 isolate_holder_->isolate()->GetHeapStatistics(&heap_statistics); | |
| 52 | |
| 53 size_t known_spaces_used_size = 0; | |
| 54 size_t known_spaces_size = 0; | |
| 55 size_t number_of_spaces = isolate_holder_->isolate()->NumberOfHeapSpaces(); | |
| 56 for (size_t space = 0; space < number_of_spaces; space++) { | |
| 57 v8::HeapSpaceStatistics space_statistics; | |
| 58 isolate_holder_->isolate()->GetHeapSpaceStatistics(&space_statistics, | |
| 59 space); | |
| 60 size_t space_size = space_statistics.space_size(); | |
|
Primiano Tucci (use gerrit)
2015/05/07 16:25:43
here and below:
s/size_t/const size_t/
ssid
2015/05/08 08:37:40
Done.
| |
| 61 size_t space_used_size = space_statistics.space_used_size(); | |
| 62 | |
| 63 known_spaces_size += space_size; | |
| 64 known_spaces_used_size += space_used_size; | |
| 65 | |
| 66 std::string allocator_name = base::StringPrintf( | |
| 67 "%s/%s_%p/%s", kRootDumpName, kIsolateDumpName, | |
| 68 isolate_holder_->isolate(), space_statistics.space_name()); | |
| 69 base::trace_event::MemoryAllocatorDump* space_dump = | |
| 70 pmd->CreateAllocatorDump(allocator_name.c_str()); | |
|
Primiano Tucci (use gerrit)
2015/05/07 16:25:43
you don't need c_str() here, CreateAllocatorDump t
ssid
2015/05/08 08:37:40
Done.
| |
| 71 space_dump->AddScalar( | |
| 72 base::trace_event::MemoryAllocatorDump::kNameOuterSize, | |
| 73 base::trace_event::MemoryAllocatorDump::kUnitsBytes, space_size); | |
| 74 | |
| 75 // TODO(ssid): Fix crbug.com/481504 to get the objects count of live objects | |
| 76 // after the last GC. | |
| 77 space_dump->AddScalar( | |
| 78 base::trace_event::MemoryAllocatorDump::kNameInnerSize, | |
| 79 base::trace_event::MemoryAllocatorDump::kUnitsBytes, space_used_size); | |
| 80 space_dump->AddScalar(kAvailableSizeAttribute, | |
| 81 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | |
| 82 space_statistics.space_available_size()); | |
| 83 } | |
| 84 // Compute the rest of the memory, not accounted by the spaces above. | |
| 85 std::string allocator_name = | |
| 86 base::StringPrintf("%s/%s_%p/%s", kRootDumpName, kIsolateDumpName, | |
| 87 isolate_holder_->isolate(), "other_spaces"); | |
| 88 base::trace_event::MemoryAllocatorDump* other_spaces_dump = | |
| 89 pmd->CreateAllocatorDump(allocator_name.c_str()); | |
| 90 other_spaces_dump->AddScalar( | |
| 91 base::trace_event::MemoryAllocatorDump::kNameOuterSize, | |
| 92 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | |
| 93 heap_statistics.total_heap_size() - known_spaces_size); | |
| 94 other_spaces_dump->AddScalar( | |
| 95 base::trace_event::MemoryAllocatorDump::kNameInnerSize, | |
| 96 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | |
| 97 heap_statistics.used_heap_size() - known_spaces_used_size); | |
| 98 | |
| 99 // TODO(ssid): Fix crbug.com/481504 to get the total available size of the | |
| 100 // heap. | |
| 101 other_spaces_dump->AddScalar( | |
| 102 kAvailableSizeAttribute, | |
| 103 base::trace_event::MemoryAllocatorDump::kUnitsBytes, 0); | |
| 104 } | |
| 105 | |
| 106 } // namespace gin | |
| OLD | NEW |