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 <sstream> | |
8 | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/threading/platform_thread.h" | |
11 #include "base/trace_event/memory_dump_manager.h" | |
12 #include "base/trace_event/process_memory_dump.h" | |
13 #include "gin/public/isolate_holder.h" | |
14 #include "v8/include/v8.h" | |
15 | |
16 namespace gin { | |
17 | |
18 namespace { | |
19 const char kDumperFriendlyName[] = "V8IsolateHeap"; | |
20 const char kRootDumpName[] = "v8"; | |
21 const char kIsolateDumpName[] = "isolate"; | |
22 const char kAvailableSizeAttribute[] = "available_size_in_bytes"; | |
23 const char kPhysicalSizeAttribute[] = "physical_size_in_bytes"; | |
24 } // namespace | |
25 | |
26 V8IsolateMemoryDumpProvider::V8IsolateMemoryDumpProvider( | |
27 IsolateHolder* isolate_holder) | |
28 : MemoryDumpProvider(base::MessageLoop::current()->task_runner()) { | |
29 DeclareAllocatorAttribute(kRootDumpName, kAvailableSizeAttribute, "bytes"); | |
30 DeclareAllocatorAttribute(kRootDumpName, kPhysicalSizeAttribute, "bytes"); | |
31 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | |
32 this); | |
33 isolate_holder_ = isolate_holder; | |
34 } | |
35 | |
36 V8IsolateMemoryDumpProvider::~V8IsolateMemoryDumpProvider() { | |
37 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( | |
38 this); | |
39 } | |
40 | |
41 // Called at trace dump point time. Creates a snapshot with the memory counters | |
42 // for the current isolate. | |
43 bool V8IsolateMemoryDumpProvider::DumpInto( | |
44 base::trace_event::ProcessMemoryDump* pmd) { | |
45 v8::HeapStatistics heap_statistics; | |
46 isolate_holder_->isolate()->GetHeapStatistics(&heap_statistics); | |
47 | |
48 int thread_id = base::PlatformThread::CurrentId(); | |
49 std::stringstream isolate_name; | |
50 // TODO(ssid): The thread_id is soon not going to be unique enough if we get | |
51 // more isolates per thread. | |
52 isolate_name << kIsolateDumpName << '_' << thread_id; | |
53 | |
54 size_t known_spaces_used_size = 0; | |
55 size_t known_spaces_size = 0; | |
56 size_t number_of_spaces = isolate_holder_->isolate()->NumberOfHeapSpaces(); | |
57 for (size_t space = 0; space < number_of_spaces; space++) { | |
58 v8::HeapSpaceStatistics space_statistics; | |
59 isolate_holder_->isolate()->GetHeapSpaceStatistics(&space_statistics, | |
60 space); | |
61 size_t space_size = space_statistics.space_size(); | |
62 size_t space_used_size = space_statistics.space_used_size(); | |
63 | |
64 known_spaces_size += space_size; | |
65 known_spaces_used_size += space_used_size; | |
66 | |
67 std::stringstream allocator_name; | |
68 allocator_name << isolate_name.str() << '/' | |
69 << space_statistics.space_name(); | |
70 base::trace_event::MemoryAllocatorDump* space_dump = | |
71 pmd->CreateAllocatorDump(kRootDumpName, allocator_name.str()); | |
72 space_dump->set_physical_size_in_bytes(static_cast<int>((space_size))); | |
73 space_dump->set_allocated_objects_count(0); | |
74 space_dump->set_allocated_objects_size_in_bytes( | |
75 static_cast<int>(space_used_size)); | |
76 space_dump->SetAttribute( | |
77 kAvailableSizeAttribute, | |
78 static_cast<int>(space_statistics.space_available_size())); | |
79 // TODO(ssid): This should probably be the physical_size_in_bytes. Check | |
80 // with V8 owner. | |
81 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.
| |
82 kPhysicalSizeAttribute, | |
83 static_cast<int>(space_statistics.physical_space_size())); | |
84 } | |
85 | |
86 std::stringstream allocator_name; | |
87 allocator_name << isolate_name.str() << '/' << "other_spaces"; | |
88 // Compute the rest of the memory, not accounted by the spaces above. | |
89 base::trace_event::MemoryAllocatorDump* space_dump = | |
90 pmd->CreateAllocatorDump(kRootDumpName, allocator_name.str()); | |
91 space_dump->set_physical_size_in_bytes(heap_statistics.total_heap_size() - | |
92 known_spaces_size); | |
93 space_dump->set_allocated_objects_count(0); | |
94 space_dump->set_allocated_objects_size_in_bytes( | |
95 heap_statistics.used_heap_size() - known_spaces_used_size); | |
96 | |
97 return true; | |
98 } | |
99 | |
100 const char* V8IsolateMemoryDumpProvider::GetFriendlyName() const { | |
101 return kDumperFriendlyName; | |
102 } | |
103 | |
104 } // namespace gin | |
OLD | NEW |