blob: 8c3ad6a9c0acbff03f85e34cb34db2c5b51f998a [file] [log] [blame]
ssid83aa5be2015-05-08 12:03:261// 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
avi90e658dd2015-12-21 07:16:197#include <stddef.h>
8
ssid83aa5be2015-05-08 12:03:269#include "base/strings/stringprintf.h"
gab54909b722016-05-11 18:34:1110#include "base/threading/thread_task_runner_handle.h"
ssid83aa5be2015-05-08 12:03:2611#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
16namespace gin {
17
ssid83aa5be2015-05-08 12:03:2618V8IsolateMemoryDumpProvider::V8IsolateMemoryDumpProvider(
19 IsolateHolder* isolate_holder)
20 : isolate_holder_(isolate_holder) {
21 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
primiano186d6bfe2015-10-30 13:21:4022 this, "V8Isolate", base::ThreadTaskRunnerHandle::Get());
ssid83aa5be2015-05-08 12:03:2623}
24
25V8IsolateMemoryDumpProvider::~V8IsolateMemoryDumpProvider() {
26 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
27 this);
28}
29
30// Called at trace dump point time. Creates a snapshot with the memory counters
31// for the current isolate.
32bool V8IsolateMemoryDumpProvider::OnMemoryDump(
ssid90694aeec2015-08-06 13:01:3033 const base::trace_event::MemoryDumpArgs& args,
ssidf51216b02015-06-04 19:46:2334 base::trace_event::ProcessMemoryDump* process_memory_dump) {
ssid90694aeec2015-08-06 13:01:3035 // TODO(ssid): Use MemoryDumpArgs to create light dumps when requested
36 // (crbug.com/499731).
37
ssid83aa5be2015-05-08 12:03:2638 if (isolate_holder_->access_mode() == IsolateHolder::kUseLocker) {
39 v8::Locker locked(isolate_holder_->isolate());
ssid2888a242015-08-07 23:08:4240 DumpHeapStatistics(args, process_memory_dump);
ssid83aa5be2015-05-08 12:03:2641 } else {
ssid2888a242015-08-07 23:08:4242 DumpHeapStatistics(args, process_memory_dump);
ssid83aa5be2015-05-08 12:03:2643 }
44 return true;
45}
46
sside36cfaf2015-06-12 16:42:2047void V8IsolateMemoryDumpProvider::DumpHeapStatistics(
ssid2888a242015-08-07 23:08:4248 const base::trace_event::MemoryDumpArgs& args,
sside36cfaf2015-06-12 16:42:2049 base::trace_event::ProcessMemoryDump* process_memory_dump) {
50 std::string dump_base_name =
51 base::StringPrintf("v8/isolate_%p", isolate_holder_->isolate());
52
53 // Dump statistics of the heap's spaces.
54 std::string space_name_prefix = dump_base_name + "/heap_spaces";
ssid83aa5be2015-05-08 12:03:2655 v8::HeapStatistics heap_statistics;
56 isolate_holder_->isolate()->GetHeapStatistics(&heap_statistics);
57
58 size_t known_spaces_used_size = 0;
59 size_t known_spaces_size = 0;
primiano553bea9a2015-09-09 16:37:3560 size_t known_spaces_physical_size = 0;
ssid83aa5be2015-05-08 12:03:2661 size_t number_of_spaces = isolate_holder_->isolate()->NumberOfHeapSpaces();
62 for (size_t space = 0; space < number_of_spaces; space++) {
63 v8::HeapSpaceStatistics space_statistics;
64 isolate_holder_->isolate()->GetHeapSpaceStatistics(&space_statistics,
65 space);
66 const size_t space_size = space_statistics.space_size();
67 const size_t space_used_size = space_statistics.space_used_size();
primiano553bea9a2015-09-09 16:37:3568 const size_t space_physical_size = space_statistics.physical_space_size();
ssid83aa5be2015-05-08 12:03:2669
70 known_spaces_size += space_size;
71 known_spaces_used_size += space_used_size;
primiano553bea9a2015-09-09 16:37:3572 known_spaces_physical_size += space_physical_size;
ssid83aa5be2015-05-08 12:03:2673
sside36cfaf2015-06-12 16:42:2074 std::string space_dump_name =
75 space_name_prefix + "/" + space_statistics.space_name();
76 auto space_dump = process_memory_dump->CreateAllocatorDump(space_dump_name);
77 space_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
ssid83aa5be2015-05-08 12:03:2678 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
primiano553bea9a2015-09-09 16:37:3579 space_physical_size);
80
81 space_dump->AddScalar("virtual_size",
82 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
sside36cfaf2015-06-12 16:42:2083 space_size);
84
primiano553bea9a2015-09-09 16:37:3585 space_dump->AddScalar("allocated_objects_size",
86 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
87 space_used_size);
ssid83aa5be2015-05-08 12:03:2688 }
ssidf51216b02015-06-04 19:46:2389
ssid83aa5be2015-05-08 12:03:2690 // Compute the rest of the memory, not accounted by the spaces above.
sside36cfaf2015-06-12 16:42:2091 std::string other_spaces_name = space_name_prefix + "/other_spaces";
92 auto other_dump = process_memory_dump->CreateAllocatorDump(other_spaces_name);
sside36cfaf2015-06-12 16:42:2093
primiano553bea9a2015-09-09 16:37:3594 other_dump->AddScalar(
sside36cfaf2015-06-12 16:42:2095 base::trace_event::MemoryAllocatorDump::kNameSize,
ssid83aa5be2015-05-08 12:03:2696 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
primiano553bea9a2015-09-09 16:37:3597 heap_statistics.total_physical_size() - known_spaces_physical_size);
98
99 other_dump->AddScalar(
100 "allocated_objects_size",
101 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
ssid83aa5be2015-05-08 12:03:26102 heap_statistics.used_heap_size() - known_spaces_used_size);
103
primiano553bea9a2015-09-09 16:37:35104 other_dump->AddScalar("virtual_size",
105 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
106 heap_statistics.total_heap_size() - known_spaces_size);
107
ssidbce6ee42015-11-05 02:35:39108 // If V8 zaps garbage, all the memory mapped regions become resident,
109 // so we add an extra dump to avoid mismatches w.r.t. the total
110 // resident values.
111 if (heap_statistics.does_zap_garbage()) {
112 auto zap_dump = process_memory_dump->CreateAllocatorDump(
113 dump_base_name + "/zapped_for_debug");
114 zap_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
115 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
116 heap_statistics.total_heap_size() -
117 heap_statistics.total_physical_size());
118 }
119
jochen07eb75792016-04-09 20:34:02120 // Dump statistics about malloced memory.
121 std::string malloc_name = dump_base_name + "/malloc";
122 auto malloc_dump = process_memory_dump->CreateAllocatorDump(malloc_name);
123 malloc_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
124 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
125 heap_statistics.malloced_memory());
126 const char* system_allocator_name =
127 base::trace_event::MemoryDumpManager::GetInstance()
128 ->system_allocator_pool_name();
129 if (system_allocator_name) {
130 process_memory_dump->AddSuballocation(malloc_dump->guid(),
131 system_allocator_name);
132 }
133
ssid2888a242015-08-07 23:08:42134 // If light dump is requested, then object statistics are not dumped
primiano82baa9af2015-09-14 20:44:25135 if (args.level_of_detail == base::trace_event::MemoryDumpLevelOfDetail::LIGHT)
ssid2888a242015-08-07 23:08:42136 return;
137
sside36cfaf2015-06-12 16:42:20138 // Dump statistics of the heap's live objects from last GC.
primiano553bea9a2015-09-09 16:37:35139 // TODO(primiano): these should not be tracked in the same trace event as they
140 // report stats for the last GC (not the current state). See crbug.com/498779.
141 std::string object_name_prefix = dump_base_name + "/heap_objects_at_last_gc";
sside36cfaf2015-06-12 16:42:20142 bool did_dump_object_stats = false;
ssidf51216b02015-06-04 19:46:23143 const size_t object_types =
144 isolate_holder_->isolate()->NumberOfTrackedHeapObjectTypes();
145 for (size_t type_index = 0; type_index < object_types; type_index++) {
146 v8::HeapObjectStatistics object_statistics;
147 if (!isolate_holder_->isolate()->GetHeapObjectStatisticsAtLastGC(
148 &object_statistics, type_index))
149 continue;
150
151 std::string dump_name =
sside36cfaf2015-06-12 16:42:20152 object_name_prefix + "/" + object_statistics.object_type();
ssidf51216b02015-06-04 19:46:23153 if (object_statistics.object_sub_type()[0] != '\0')
154 dump_name += std::string("/") + object_statistics.object_sub_type();
sside36cfaf2015-06-12 16:42:20155 auto object_dump = process_memory_dump->CreateAllocatorDump(dump_name);
ssidf51216b02015-06-04 19:46:23156
157 object_dump->AddScalar(
bratell10f2e3c2015-09-10 16:28:43158 base::trace_event::MemoryAllocatorDump::kNameObjectCount,
ssidf51216b02015-06-04 19:46:23159 base::trace_event::MemoryAllocatorDump::kUnitsObjects,
160 object_statistics.object_count());
sside36cfaf2015-06-12 16:42:20161 object_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
162 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
163 object_statistics.object_size());
164 did_dump_object_stats = true;
ssidf51216b02015-06-04 19:46:23165 }
166
sside36cfaf2015-06-12 16:42:20167 if (process_memory_dump->GetAllocatorDump(object_name_prefix +
168 "/CODE_TYPE")) {
169 auto code_kind_dump = process_memory_dump->CreateAllocatorDump(
170 object_name_prefix + "/CODE_TYPE/CODE_KIND");
171 auto code_age_dump = process_memory_dump->CreateAllocatorDump(
172 object_name_prefix + "/CODE_TYPE/CODE_AGE");
173 process_memory_dump->AddOwnershipEdge(code_kind_dump->guid(),
174 code_age_dump->guid());
175 }
176
177 if (did_dump_object_stats) {
178 process_memory_dump->AddOwnershipEdge(
179 process_memory_dump->CreateAllocatorDump(object_name_prefix)->guid(),
180 process_memory_dump->CreateAllocatorDump(space_name_prefix)->guid());
181 }
ssidf51216b02015-06-04 19:46:23182}
183
ssid83aa5be2015-05-08 12:03:26184} // namespace gin