Fix content verifier problem with content scripts (reland)
[chromium-blink-merge.git] / gin / v8_isolate_memory_dump_provider.cc
blob28a05ecbcb5a994915ede3968bb496bd5719d029
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.
5 #include "gin/v8_isolate_memory_dump_provider.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "base/trace_event/memory_dump_manager.h"
10 #include "base/trace_event/process_memory_dump.h"
11 #include "gin/public/isolate_holder.h"
12 #include "v8/include/v8.h"
14 namespace gin {
16 V8IsolateMemoryDumpProvider::V8IsolateMemoryDumpProvider(
17 IsolateHolder* isolate_holder)
18 : isolate_holder_(isolate_holder) {
19 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
20 this, base::ThreadTaskRunnerHandle::Get());
23 V8IsolateMemoryDumpProvider::~V8IsolateMemoryDumpProvider() {
24 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
25 this);
28 // Called at trace dump point time. Creates a snapshot with the memory counters
29 // for the current isolate.
30 bool V8IsolateMemoryDumpProvider::OnMemoryDump(
31 base::trace_event::ProcessMemoryDump* process_memory_dump) {
32 if (isolate_holder_->access_mode() == IsolateHolder::kUseLocker) {
33 v8::Locker locked(isolate_holder_->isolate());
34 DumpHeapStatistics(process_memory_dump);
35 } else {
36 DumpHeapStatistics(process_memory_dump);
38 return true;
41 void V8IsolateMemoryDumpProvider::DumpHeapStatistics(
42 base::trace_event::ProcessMemoryDump* process_memory_dump) {
43 std::string dump_base_name =
44 base::StringPrintf("v8/isolate_%p", isolate_holder_->isolate());
46 // Dump statistics of the heap's spaces.
47 std::string space_name_prefix = dump_base_name + "/heap_spaces";
48 v8::HeapStatistics heap_statistics;
49 isolate_holder_->isolate()->GetHeapStatistics(&heap_statistics);
51 size_t known_spaces_used_size = 0;
52 size_t known_spaces_size = 0;
53 size_t number_of_spaces = isolate_holder_->isolate()->NumberOfHeapSpaces();
54 for (size_t space = 0; space < number_of_spaces; space++) {
55 v8::HeapSpaceStatistics space_statistics;
56 isolate_holder_->isolate()->GetHeapSpaceStatistics(&space_statistics,
57 space);
58 const size_t space_size = space_statistics.space_size();
59 const size_t space_used_size = space_statistics.space_used_size();
61 known_spaces_size += space_size;
62 known_spaces_used_size += space_used_size;
64 std::string space_dump_name =
65 space_name_prefix + "/" + space_statistics.space_name();
66 auto space_dump = process_memory_dump->CreateAllocatorDump(space_dump_name);
67 space_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
68 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
69 space_size);
71 auto space_allocated_dump = process_memory_dump->CreateAllocatorDump(
72 space_dump_name + "/allocated_objects");
73 space_allocated_dump->AddScalar(
74 base::trace_event::MemoryAllocatorDump::kNameSize,
75 base::trace_event::MemoryAllocatorDump::kUnitsBytes, space_used_size);
78 // Compute the rest of the memory, not accounted by the spaces above.
79 std::string other_spaces_name = space_name_prefix + "/other_spaces";
80 auto other_dump = process_memory_dump->CreateAllocatorDump(other_spaces_name);
81 other_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
82 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
83 heap_statistics.total_heap_size() - known_spaces_size);
85 auto other_allocated_dump = process_memory_dump->CreateAllocatorDump(
86 other_spaces_name + "/allocated_objects");
87 other_allocated_dump->AddScalar(
88 base::trace_event::MemoryAllocatorDump::kNameSize,
89 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
90 heap_statistics.used_heap_size() - known_spaces_used_size);
92 // Dump statistics of the heap's live objects from last GC.
93 std::string object_name_prefix = dump_base_name + "/heap_objects";
94 bool did_dump_object_stats = false;
95 const size_t object_types =
96 isolate_holder_->isolate()->NumberOfTrackedHeapObjectTypes();
97 for (size_t type_index = 0; type_index < object_types; type_index++) {
98 v8::HeapObjectStatistics object_statistics;
99 if (!isolate_holder_->isolate()->GetHeapObjectStatisticsAtLastGC(
100 &object_statistics, type_index))
101 continue;
103 std::string dump_name =
104 object_name_prefix + "/" + object_statistics.object_type();
105 if (object_statistics.object_sub_type()[0] != '\0')
106 dump_name += std::string("/") + object_statistics.object_sub_type();
107 auto object_dump = process_memory_dump->CreateAllocatorDump(dump_name);
109 object_dump->AddScalar(
110 base::trace_event::MemoryAllocatorDump::kNameObjectsCount,
111 base::trace_event::MemoryAllocatorDump::kUnitsObjects,
112 object_statistics.object_count());
113 object_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
114 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
115 object_statistics.object_size());
116 did_dump_object_stats = true;
119 if (process_memory_dump->GetAllocatorDump(object_name_prefix +
120 "/CODE_TYPE")) {
121 auto code_kind_dump = process_memory_dump->CreateAllocatorDump(
122 object_name_prefix + "/CODE_TYPE/CODE_KIND");
123 auto code_age_dump = process_memory_dump->CreateAllocatorDump(
124 object_name_prefix + "/CODE_TYPE/CODE_AGE");
125 process_memory_dump->AddOwnershipEdge(code_kind_dump->guid(),
126 code_age_dump->guid());
129 if (did_dump_object_stats) {
130 process_memory_dump->AddOwnershipEdge(
131 process_memory_dump->CreateAllocatorDump(object_name_prefix)->guid(),
132 process_memory_dump->CreateAllocatorDump(space_name_prefix)->guid());
136 } // namespace gin