Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: src/profiler/sampling-heap-profiler.cc

Issue 1697903002: Sampling heap profiler data structure changes (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Remove functioninfo Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/profiler/sampling-heap-profiler.h" 5 #include "src/profiler/sampling-heap-profiler.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <memory> 8 #include <memory>
9 #include "src/api.h" 9 #include "src/api.h"
10 #include "src/base/utils/random-number-generator.h" 10 #include "src/base/utils/random-number-generator.h"
(...skipping 26 matching lines...) Expand all
37 uint64_t rate, int stack_depth) 37 uint64_t rate, int stack_depth)
38 : isolate_(heap->isolate()), 38 : isolate_(heap->isolate()),
39 heap_(heap), 39 heap_(heap),
40 new_space_observer_(new SamplingAllocationObserver( 40 new_space_observer_(new SamplingAllocationObserver(
41 heap_, rate, rate, this, heap->isolate()->random_number_generator())), 41 heap_, rate, rate, this, heap->isolate()->random_number_generator())),
42 other_spaces_observer_(new SamplingAllocationObserver( 42 other_spaces_observer_(new SamplingAllocationObserver(
43 heap_, rate, rate, this, heap->isolate()->random_number_generator())), 43 heap_, rate, rate, this, heap->isolate()->random_number_generator())),
44 names_(names), 44 names_(names),
45 samples_(), 45 samples_(),
46 stack_depth_(stack_depth) { 46 stack_depth_(stack_depth) {
47 profile_root_ =
ofrobots 2016/02/17 17:54:29 The lifetime of this allocation seems to match the
mattloring 2016/02/17 19:09:35 Done.
48 new AllocationNode("(root)", v8::UnboundScript::kNoScriptId, 0);
47 heap->new_space()->AddAllocationObserver(new_space_observer_.get()); 49 heap->new_space()->AddAllocationObserver(new_space_observer_.get());
48 AllSpaces spaces(heap); 50 AllSpaces spaces(heap);
49 for (Space* space = spaces.next(); space != NULL; space = spaces.next()) { 51 for (Space* space = spaces.next(); space != NULL; space = spaces.next()) {
50 if (space != heap->new_space()) { 52 if (space != heap->new_space()) {
51 space->AddAllocationObserver(other_spaces_observer_.get()); 53 space->AddAllocationObserver(other_spaces_observer_.get());
52 } 54 }
53 } 55 }
54 } 56 }
55 57
56 58
57 SamplingHeapProfiler::~SamplingHeapProfiler() { 59 SamplingHeapProfiler::~SamplingHeapProfiler() {
58 heap_->new_space()->RemoveAllocationObserver(new_space_observer_.get()); 60 heap_->new_space()->RemoveAllocationObserver(new_space_observer_.get());
59 AllSpaces spaces(heap_); 61 AllSpaces spaces(heap_);
60 for (Space* space = spaces.next(); space != NULL; space = spaces.next()) { 62 for (Space* space = spaces.next(); space != NULL; space = spaces.next()) {
61 if (space != heap_->new_space()) { 63 if (space != heap_->new_space()) {
62 space->RemoveAllocationObserver(other_spaces_observer_.get()); 64 space->RemoveAllocationObserver(other_spaces_observer_.get());
63 } 65 }
64 } 66 }
65 67
66 // Clear samples and drop all the weak references we are keeping. 68 for (auto it = samples_.begin(); it != samples_.end(); ++it) {
ulan 2016/02/17 11:14:50 We can use shorter version of for to be consistent
mattloring 2016/02/17 17:50:21 Done.
67 std::set<SampledAllocation*>::iterator it; 69 (*it)->global.Reset();
68 for (it = samples_.begin(); it != samples_.end(); ++it) { 70 delete (*it);
69 delete *it;
70 } 71 }
71 std::set<SampledAllocation*> empty; 72 samples_.clear();
72 samples_.swap(empty); 73
74 delete profile_root_;
73 } 75 }
74 76
75 77
76 void SamplingHeapProfiler::SampleObject(Address soon_object, size_t size) { 78 void SamplingHeapProfiler::SampleObject(Address soon_object, size_t size) {
77 DisallowHeapAllocation no_allocation; 79 DisallowHeapAllocation no_allocation;
78 80
79 HandleScope scope(isolate_); 81 HandleScope scope(isolate_);
80 HeapObject* heap_object = HeapObject::FromAddress(soon_object); 82 HeapObject* heap_object = HeapObject::FromAddress(soon_object);
81 Handle<Object> obj(heap_object, isolate_); 83 Handle<Object> obj(heap_object, isolate_);
82 84
83 // Mark the new block as FreeSpace to make sure the heap is iterable while we 85 // Mark the new block as FreeSpace to make sure the heap is iterable while we
84 // are taking the sample. 86 // are taking the sample.
85 heap()->CreateFillerObjectAt(soon_object, static_cast<int>(size)); 87 heap()->CreateFillerObjectAt(soon_object, static_cast<int>(size));
86 88
87 Local<v8::Value> loc = v8::Utils::ToLocal(obj); 89 Local<v8::Value> loc = v8::Utils::ToLocal(obj);
88 90
89 SampledAllocation* sample = 91 AllocationNode* node = AddStack();
90 new SampledAllocation(this, isolate_, loc, size, stack_depth_); 92 node->allocations_[size]++;
93 Sample* sample = new Sample(
94 {size, node, Global<Value>(reinterpret_cast<v8::Isolate*>(isolate_), loc),
95 this});
91 samples_.insert(sample); 96 samples_.insert(sample);
97 sample->global.SetWeak(sample, OnWeakCallback, WeakCallbackType::kParameter);
92 } 98 }
93 99
94 100 void SamplingHeapProfiler::OnWeakCallback(
95 void SamplingHeapProfiler::SampledAllocation::OnWeakCallback( 101 const WeakCallbackInfo<Sample>& data) {
96 const WeakCallbackInfo<SampledAllocation>& data) { 102 Sample* sample = data.GetParameter();
97 SampledAllocation* sample = data.GetParameter(); 103 AllocationNode* node = sample->owner;
98 sample->sampling_heap_profiler_->samples_.erase(sample); 104 node->allocations_[sample->size]--;
105 sample->profiler->samples_.erase(sample);
99 delete sample; 106 delete sample;
100 } 107 }
101 108
102 109 SamplingHeapProfiler::AllocationNode* SamplingHeapProfiler::FindOrAddChildNode(
103 SamplingHeapProfiler::FunctionInfo::FunctionInfo(SharedFunctionInfo* shared, 110 AllocationNode* parent, const char* name, int script_id,
104 StringsStorage* names) 111 int start_position) {
105 : name_(names->GetFunctionName(shared->DebugName())), 112 for (AllocationNode* child : parent->children_) {
106 script_name_(""), 113 if (child->script_id_ == script_id &&
107 script_id_(v8::UnboundScript::kNoScriptId), 114 child->script_position_ == start_position)
ulan 2016/02/17 11:14:50 DCHECK that names match?
mattloring 2016/02/17 17:50:21 Done.
108 start_position_(shared->start_position()) { 115 return child;
109 if (shared->script()->IsScript()) {
110 Script* script = Script::cast(shared->script());
111 script_id_ = script->id();
112 if (script->name()->IsName()) {
113 Name* name = Name::cast(script->name());
114 script_name_ = names->GetName(name);
115 }
116 } 116 }
117 AllocationNode* child = new AllocationNode(name, script_id, start_position);
118 parent->children_.push_back(child);
119 return child;
117 } 120 }
118 121
122 SamplingHeapProfiler::AllocationNode* SamplingHeapProfiler::AddStack() {
123 AllocationNode* node = profile_root_;
119 124
120 SamplingHeapProfiler::SampledAllocation::SampledAllocation( 125 std::vector<SharedFunctionInfo*> stack;
121 SamplingHeapProfiler* sampling_heap_profiler, Isolate* isolate, 126 StackTraceFrameIterator it(isolate_);
122 Local<Value> local, size_t size, int max_frames)
123 : sampling_heap_profiler_(sampling_heap_profiler),
124 global_(reinterpret_cast<v8::Isolate*>(isolate), local),
125 size_(size) {
126 global_.SetWeak(this, OnWeakCallback, WeakCallbackType::kParameter);
127
128 StackTraceFrameIterator it(isolate);
129 int frames_captured = 0; 127 int frames_captured = 0;
130 while (!it.done() && frames_captured < max_frames) { 128 while (!it.done() && frames_captured < stack_depth_) {
131 JavaScriptFrame* frame = it.frame(); 129 JavaScriptFrame* frame = it.frame();
132 SharedFunctionInfo* shared = frame->function()->shared(); 130 SharedFunctionInfo* shared = frame->function()->shared();
133 stack_.push_back(new FunctionInfo(shared, sampling_heap_profiler->names())); 131 stack.push_back(shared);
134 132
135 frames_captured++; 133 frames_captured++;
136 it.Advance(); 134 it.Advance();
137 } 135 }
138 136
139 if (frames_captured == 0) { 137 if (frames_captured == 0) {
140 const char* name = nullptr; 138 const char* name = nullptr;
141 switch (isolate->current_vm_state()) { 139 switch (isolate_->current_vm_state()) {
142 case GC: 140 case GC:
143 name = "(GC)"; 141 name = "(GC)";
144 break; 142 break;
145 case COMPILER: 143 case COMPILER:
146 name = "(COMPILER)"; 144 name = "(COMPILER)";
147 break; 145 break;
148 case OTHER: 146 case OTHER:
149 name = "(V8 API)"; 147 name = "(V8 API)";
150 break; 148 break;
151 case EXTERNAL: 149 case EXTERNAL:
152 name = "(EXTERNAL)"; 150 name = "(EXTERNAL)";
153 break; 151 break;
154 case IDLE: 152 case IDLE:
155 name = "(IDLE)"; 153 name = "(IDLE)";
156 break; 154 break;
157 case JS: 155 case JS:
158 name = "(JS)"; 156 name = "(JS)";
159 break; 157 break;
160 } 158 }
161 stack_.push_back(new FunctionInfo(name)); 159 return FindOrAddChildNode(node, name, v8::UnboundScript::kNoScriptId, 0);
162 } 160 }
163 }
164
165 v8::AllocationProfile::Node* SamplingHeapProfiler::AllocateNode(
166 AllocationProfile* profile, const std::map<int, Script*>& scripts,
167 FunctionInfo* function_info) {
168 DCHECK(function_info->get_name());
169 DCHECK(function_info->get_script_name());
170
171 int line = v8::AllocationProfile::kNoLineNumberInfo;
172 int column = v8::AllocationProfile::kNoColumnNumberInfo;
173
174 if (function_info->get_script_id() != v8::UnboundScript::kNoScriptId) {
175 // Cannot use std::map<T>::at because it is not available on android.
176 auto non_const_scripts = const_cast<std::map<int, Script*>&>(scripts);
177 Handle<Script> script(non_const_scripts[function_info->get_script_id()]);
178
179 line =
180 1 + Script::GetLineNumber(script, function_info->get_start_position());
181 column = 1 + Script::GetColumnNumber(script,
182 function_info->get_start_position());
183 }
184
185 profile->nodes().push_back(v8::AllocationProfile::Node(
186 {ToApiHandle<v8::String>(isolate_->factory()->InternalizeUtf8String(
187 function_info->get_name())),
188 ToApiHandle<v8::String>(isolate_->factory()->InternalizeUtf8String(
189 function_info->get_script_name())),
190 function_info->get_script_id(), function_info->get_start_position(),
191 line, column, std::vector<v8::AllocationProfile::Node*>(),
192 std::vector<v8::AllocationProfile::Allocation>()}));
193
194 return &profile->nodes().back();
195 }
196
197 v8::AllocationProfile::Node* SamplingHeapProfiler::FindOrAddChildNode(
198 AllocationProfile* profile, const std::map<int, Script*>& scripts,
199 v8::AllocationProfile::Node* parent, FunctionInfo* function_info) {
200 for (v8::AllocationProfile::Node* child : parent->children) {
201 if (child->script_id == function_info->get_script_id() &&
202 child->start_position == function_info->get_start_position())
203 return child;
204 }
205 v8::AllocationProfile::Node* child =
206 AllocateNode(profile, scripts, function_info);
207 parent->children.push_back(child);
208 return child;
209 }
210
211 v8::AllocationProfile::Node* SamplingHeapProfiler::AddStack(
212 AllocationProfile* profile, const std::map<int, Script*>& scripts,
213 const std::vector<FunctionInfo*>& stack) {
214 v8::AllocationProfile::Node* node = profile->GetRootNode();
215 161
216 // We need to process the stack in reverse order as the top of the stack is 162 // We need to process the stack in reverse order as the top of the stack is
217 // the first element in the list. 163 // the first element in the list.
218 for (auto it = stack.rbegin(); it != stack.rend(); ++it) { 164 for (auto it = stack.rbegin(); it != stack.rend(); ++it) {
219 FunctionInfo* function_info = *it; 165 SharedFunctionInfo* shared = *it;
220 node = FindOrAddChildNode(profile, scripts, node, function_info); 166 const char* name = this->names()->GetFunctionName(shared->DebugName());
167 int script_id = v8::UnboundScript::kNoScriptId;
168 if (shared->script()->IsScript()) {
169 Script* script = Script::cast(shared->script());
170 script_id = script->id();
171 }
172 node = FindOrAddChildNode(node, name, script_id, shared->start_position());
221 } 173 }
222 return node; 174 return node;
223 } 175 }
224 176
177 v8::AllocationProfile::Node* SamplingHeapProfiler::GenerateProfile(
178 AllocationProfile* profile, SamplingHeapProfiler::AllocationNode* node,
179 const std::map<int, Script*>& scripts) {
180 Local<v8::String> script_name =
181 ToApiHandle<v8::String>(isolate_->factory()->InternalizeUtf8String(""));
182 int line = v8::AllocationProfile::kNoLineNumberInfo;
183 int column = v8::AllocationProfile::kNoColumnNumberInfo;
184 std::vector<v8::AllocationProfile::Allocation> allocations;
185 if (node->script_id_ != v8::UnboundScript::kNoScriptId) {
186 // Cannot use std::map<T>::at because it is not available on android.
187 auto non_const_scripts = const_cast<std::map<int, Script*>&>(scripts);
188 Script* script = non_const_scripts[node->script_id_];
189 if (script->name()->IsName()) {
190 Name* name = Name::cast(script->name());
191 script_name = ToApiHandle<v8::String>(
192 isolate_->factory()->InternalizeUtf8String(names_->GetName(name)));
193 }
194 Handle<Script> script_handle(script);
195
196 line = 1 + Script::GetLineNumber(script_handle, node->script_position_);
197 column = 1 + Script::GetColumnNumber(script_handle, node->script_position_);
198 for (auto alloc : node->allocations_) {
199 allocations.push_back({alloc.first, alloc.second});
200 }
201 }
202
203 profile->nodes().push_back(v8::AllocationProfile::Node(
204 {ToApiHandle<v8::String>(
205 isolate_->factory()->InternalizeUtf8String(node->name_)),
206 script_name, node->script_id_, node->script_position_, line, column,
207 std::vector<v8::AllocationProfile::Node*>(), allocations}));
208 v8::AllocationProfile::Node* current = &profile->nodes().back();
209 for (auto child : node->children_) {
210 current->children.push_back(GenerateProfile(profile, child, scripts));
211 }
212 return current;
213 }
225 214
226 v8::AllocationProfile* SamplingHeapProfiler::GetAllocationProfile() { 215 v8::AllocationProfile* SamplingHeapProfiler::GetAllocationProfile() {
227 // To resolve positions to line/column numbers, we will need to look up 216 // To resolve positions to line/column numbers, we will need to look up
228 // scripts. Build a map to allow fast mapping from script id to script. 217 // scripts. Build a map to allow fast mapping from script id to script.
229 std::map<int, Script*> scripts; 218 std::map<int, Script*> scripts;
230 { 219 {
231 Script::Iterator iterator(isolate_); 220 Script::Iterator iterator(isolate_);
232 Script* script; 221 Script* script;
233 while ((script = iterator.Next())) { 222 while ((script = iterator.Next())) {
234 scripts[script->id()] = script; 223 scripts[script->id()] = script;
235 } 224 }
236 } 225 }
237 226
238 auto profile = new v8::internal::AllocationProfile(); 227 auto profile = new v8::internal::AllocationProfile();
239 228
240 // Create the root node. 229 GenerateProfile(profile, profile_root_, scripts);
241 FunctionInfo function_info("(root)");
242 AllocateNode(profile, scripts, &function_info);
243
244 for (SampledAllocation* allocation : samples_) {
245 v8::AllocationProfile::Node* node =
246 AddStack(profile, scripts, allocation->get_stack());
247 node->allocations.push_back({allocation->get_size(), 1});
248 }
249 230
250 return profile; 231 return profile;
251 } 232 }
252 233
253 234
254 } // namespace internal 235 } // namespace internal
255 } // namespace v8 236 } // namespace v8
OLDNEW
« src/profiler/sampling-heap-profiler.h ('K') | « src/profiler/sampling-heap-profiler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698