Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" |
| 11 #include "src/frames-inl.h" | 11 #include "src/frames-inl.h" |
| 12 #include "src/heap/heap.h" | 12 #include "src/heap/heap.h" |
| 13 #include "src/isolate.h" | 13 #include "src/isolate.h" |
| 14 #include "src/profiler/strings-storage.h" | 14 #include "src/profiler/strings-storage.h" |
| 15 | 15 |
| 16 namespace v8 { | 16 namespace v8 { |
| 17 namespace internal { | 17 namespace internal { |
| 18 | 18 |
| 19 // We sample with a Poisson process, with constant average sampling interval. | |
| 20 // This follows the exponential probability distribution with parameter | |
| 21 // λ = 1/rate where rate is the average number of bytes between samples. | |
| 22 // | |
| 23 // Let u be a uniformly distributed random number between 0 and 1, then | |
| 24 // next_sample = (- ln u) / λ | |
| 25 intptr_t SamplingAllocationObserver::GetNextSampleInterval( | |
| 26 base::RandomNumberGenerator* random, uint64_t rate) { | |
| 27 if (FLAG_sampling_heap_profiler_suppress_randomness) { | |
| 28 return rate; | |
| 29 } | |
| 30 double u = random->NextDouble(); | |
| 31 double next = (-std::log(u)) * rate; | |
| 32 return next < kPointerSize | |
| 33 ? kPointerSize | |
| 34 : (next > INT_MAX ? INT_MAX : static_cast<intptr_t>(next)); | |
| 35 } | |
| 36 | |
| 19 SamplingHeapProfiler::SamplingHeapProfiler(Heap* heap, StringsStorage* names, | 37 SamplingHeapProfiler::SamplingHeapProfiler(Heap* heap, StringsStorage* names, |
| 20 uint64_t rate, int stack_depth) | 38 uint64_t rate, int stack_depth) |
| 21 : InlineAllocationObserver(GetNextSampleInterval( | 39 : isolate_(heap->isolate()), |
| 22 heap->isolate()->random_number_generator(), rate)), | |
| 23 isolate_(heap->isolate()), | |
| 24 heap_(heap), | 40 heap_(heap), |
| 25 random_(isolate_->random_number_generator()), | |
| 26 names_(names), | 41 names_(names), |
| 27 samples_(), | 42 samples_(), |
| 28 rate_(rate), | |
| 29 stack_depth_(stack_depth) { | 43 stack_depth_(stack_depth) { |
| 30 heap->new_space()->AddInlineAllocationObserver(this); | 44 new_space_observer_ = new SamplingAllocationObserver( |
|
ofrobots
2016/02/11 05:51:46
This allocation is never freed so it will leak. Us
mattloring
2016/02/11 14:54:14
Done.
| |
| 45 heap_, rate, rate, this, heap->isolate()->random_number_generator()); | |
| 46 heap->new_space()->AddAllocationObserver(new_space_observer_); | |
| 47 other_spaces_observer_ = new SamplingAllocationObserver( | |
|
ofrobots
2016/02/11 05:51:47
likewise.
mattloring
2016/02/11 14:54:14
Done.
| |
| 48 heap_, rate, rate, this, heap->isolate()->random_number_generator()); | |
| 49 AllSpaces spaces(heap); | |
| 50 for (Space* space = spaces.next(); space != NULL; space = spaces.next()) { | |
| 51 if (space != heap->new_space()) { | |
| 52 space->AddAllocationObserver(other_spaces_observer_); | |
| 53 } | |
| 54 } | |
| 31 } | 55 } |
| 32 | 56 |
| 33 | 57 |
| 34 SamplingHeapProfiler::~SamplingHeapProfiler() { | 58 SamplingHeapProfiler::~SamplingHeapProfiler() { |
| 35 heap_->new_space()->RemoveInlineAllocationObserver(this); | 59 heap_->new_space()->RemoveAllocationObserver(new_space_observer_); |
| 60 AllSpaces spaces(heap_); | |
| 61 for (Space* space = spaces.next(); space != NULL; space = spaces.next()) { | |
| 62 if (space != heap_->new_space()) { | |
| 63 space->RemoveAllocationObserver(other_spaces_observer_); | |
| 64 } | |
| 65 } | |
| 36 | 66 |
| 37 // Clear samples and drop all the weak references we are keeping. | 67 // Clear samples and drop all the weak references we are keeping. |
| 38 std::set<SampledAllocation*>::iterator it; | 68 std::set<SampledAllocation*>::iterator it; |
| 39 for (it = samples_.begin(); it != samples_.end(); ++it) { | 69 for (it = samples_.begin(); it != samples_.end(); ++it) { |
| 40 delete *it; | 70 delete *it; |
| 41 } | 71 } |
| 42 std::set<SampledAllocation*> empty; | 72 std::set<SampledAllocation*> empty; |
| 43 samples_.swap(empty); | 73 samples_.swap(empty); |
| 44 } | 74 } |
| 45 | 75 |
| 46 void SamplingHeapProfiler::Step(int bytes_allocated, Address soon_object, | |
| 47 size_t size) { | |
| 48 DCHECK(heap_->gc_state() == Heap::NOT_IN_GC); | |
| 49 DCHECK(soon_object); | |
| 50 SampleObject(soon_object, size); | |
| 51 } | |
| 52 | |
| 53 | 76 |
| 54 void SamplingHeapProfiler::SampleObject(Address soon_object, size_t size) { | 77 void SamplingHeapProfiler::SampleObject(Address soon_object, size_t size) { |
| 55 DisallowHeapAllocation no_allocation; | 78 DisallowHeapAllocation no_allocation; |
| 56 | 79 |
| 57 HandleScope scope(isolate_); | 80 HandleScope scope(isolate_); |
| 58 HeapObject* heap_object = HeapObject::FromAddress(soon_object); | 81 HeapObject* heap_object = HeapObject::FromAddress(soon_object); |
| 59 Handle<Object> obj(heap_object, isolate_); | 82 Handle<Object> obj(heap_object, isolate_); |
| 60 | 83 |
| 61 // Mark the new block as FreeSpace to make sure the heap is iterable while we | 84 // Mark the new block as FreeSpace to make sure the heap is iterable while we |
| 62 // are taking the sample. | 85 // are taking the sample. |
| 63 heap()->CreateFillerObjectAt(soon_object, static_cast<int>(size)); | 86 heap()->CreateFillerObjectAt(soon_object, static_cast<int>(size)); |
| 64 | 87 |
| 65 Local<v8::Value> loc = v8::Utils::ToLocal(obj); | 88 Local<v8::Value> loc = v8::Utils::ToLocal(obj); |
| 66 | 89 |
| 67 SampledAllocation* sample = | 90 SampledAllocation* sample = |
| 68 new SampledAllocation(this, isolate_, loc, size, stack_depth_); | 91 new SampledAllocation(this, isolate_, loc, size, stack_depth_); |
| 69 samples_.insert(sample); | 92 samples_.insert(sample); |
| 70 } | 93 } |
| 71 | 94 |
| 72 | 95 |
| 73 // We sample with a Poisson process, with constant average sampling interval. | |
| 74 // This follows the exponential probability distribution with parameter | |
| 75 // λ = 1/rate where rate is the average number of bytes between samples. | |
| 76 // | |
| 77 // Let u be a uniformly distributed random number between 0 and 1, then | |
| 78 // next_sample = (- ln u) / λ | |
| 79 intptr_t SamplingHeapProfiler::GetNextSampleInterval( | |
| 80 base::RandomNumberGenerator* random, uint64_t rate) { | |
| 81 if (FLAG_sampling_heap_profiler_suppress_randomness) { | |
| 82 return rate; | |
| 83 } | |
| 84 double u = random->NextDouble(); | |
| 85 double next = (-std::log(u)) * rate; | |
| 86 return next < kPointerSize | |
| 87 ? kPointerSize | |
| 88 : (next > INT_MAX ? INT_MAX : static_cast<intptr_t>(next)); | |
| 89 } | |
| 90 | |
| 91 | |
| 92 void SamplingHeapProfiler::SampledAllocation::OnWeakCallback( | 96 void SamplingHeapProfiler::SampledAllocation::OnWeakCallback( |
| 93 const WeakCallbackInfo<SampledAllocation>& data) { | 97 const WeakCallbackInfo<SampledAllocation>& data) { |
| 94 SampledAllocation* sample = data.GetParameter(); | 98 SampledAllocation* sample = data.GetParameter(); |
| 95 sample->sampling_heap_profiler_->samples_.erase(sample); | 99 sample->sampling_heap_profiler_->samples_.erase(sample); |
| 96 delete sample; | 100 delete sample; |
| 97 } | 101 } |
| 98 | 102 |
| 99 | 103 |
| 100 SamplingHeapProfiler::FunctionInfo::FunctionInfo(SharedFunctionInfo* shared, | 104 SamplingHeapProfiler::FunctionInfo::FunctionInfo(SharedFunctionInfo* shared, |
| 101 StringsStorage* names) | 105 StringsStorage* names) |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 name = "(IDLE)"; | 156 name = "(IDLE)"; |
| 153 break; | 157 break; |
| 154 case JS: | 158 case JS: |
| 155 name = "(JS)"; | 159 name = "(JS)"; |
| 156 break; | 160 break; |
| 157 } | 161 } |
| 158 stack_.push_back(new FunctionInfo(name)); | 162 stack_.push_back(new FunctionInfo(name)); |
| 159 } | 163 } |
| 160 } | 164 } |
| 161 | 165 |
| 162 | 166 v8::AllocationProfile::Node* SamplingHeapProfiler::AllocateNode( |
| 163 SamplingHeapProfiler::Node* SamplingHeapProfiler::AllocateNode( | |
| 164 AllocationProfile* profile, const std::map<int, Script*>& scripts, | 167 AllocationProfile* profile, const std::map<int, Script*>& scripts, |
| 165 FunctionInfo* function_info) { | 168 FunctionInfo* function_info) { |
| 166 DCHECK(function_info->get_name()); | 169 DCHECK(function_info->get_name()); |
| 167 DCHECK(function_info->get_script_name()); | 170 DCHECK(function_info->get_script_name()); |
| 168 | 171 |
| 169 int line = v8::AllocationProfile::kNoLineNumberInfo; | 172 int line = v8::AllocationProfile::kNoLineNumberInfo; |
| 170 int column = v8::AllocationProfile::kNoColumnNumberInfo; | 173 int column = v8::AllocationProfile::kNoColumnNumberInfo; |
| 171 | 174 |
| 172 if (function_info->get_script_id() != v8::UnboundScript::kNoScriptId) { | 175 if (function_info->get_script_id() != v8::UnboundScript::kNoScriptId) { |
| 173 // Cannot use std::map<T>::at because it is not available on android. | 176 // Cannot use std::map<T>::at because it is not available on android. |
| 174 auto non_const_scripts = const_cast<std::map<int, Script*>&>(scripts); | 177 auto non_const_scripts = const_cast<std::map<int, Script*>&>(scripts); |
| 175 Handle<Script> script(non_const_scripts[function_info->get_script_id()]); | 178 Handle<Script> script(non_const_scripts[function_info->get_script_id()]); |
| 176 | 179 |
| 177 line = | 180 line = |
| 178 1 + Script::GetLineNumber(script, function_info->get_start_position()); | 181 1 + Script::GetLineNumber(script, function_info->get_start_position()); |
| 179 column = 1 + Script::GetColumnNumber(script, | 182 column = 1 + Script::GetColumnNumber(script, |
| 180 function_info->get_start_position()); | 183 function_info->get_start_position()); |
| 181 } | 184 } |
| 182 | 185 |
| 183 profile->nodes().push_back( | 186 profile->nodes().push_back(v8::AllocationProfile::Node( |
| 184 Node({ToApiHandle<v8::String>(isolate_->factory()->InternalizeUtf8String( | 187 {ToApiHandle<v8::String>(isolate_->factory()->InternalizeUtf8String( |
| 185 function_info->get_name())), | 188 function_info->get_name())), |
| 186 ToApiHandle<v8::String>(isolate_->factory()->InternalizeUtf8String( | 189 ToApiHandle<v8::String>(isolate_->factory()->InternalizeUtf8String( |
| 187 function_info->get_script_name())), | 190 function_info->get_script_name())), |
| 188 function_info->get_script_id(), function_info->get_start_position(), | 191 function_info->get_script_id(), function_info->get_start_position(), |
| 189 line, column, std::vector<Node*>(), | 192 line, column, std::vector<v8::AllocationProfile::Node*>(), |
| 190 std::vector<v8::AllocationProfile::Allocation>()})); | 193 std::vector<v8::AllocationProfile::Allocation>()})); |
| 191 | 194 |
| 192 return &profile->nodes().back(); | 195 return &profile->nodes().back(); |
| 193 } | 196 } |
| 194 | 197 |
| 195 | 198 v8::AllocationProfile::Node* SamplingHeapProfiler::FindOrAddChildNode( |
| 196 SamplingHeapProfiler::Node* SamplingHeapProfiler::FindOrAddChildNode( | |
| 197 AllocationProfile* profile, const std::map<int, Script*>& scripts, | 199 AllocationProfile* profile, const std::map<int, Script*>& scripts, |
| 198 Node* parent, FunctionInfo* function_info) { | 200 v8::AllocationProfile::Node* parent, FunctionInfo* function_info) { |
| 199 for (Node* child : parent->children) { | 201 for (v8::AllocationProfile::Node* child : parent->children) { |
| 200 if (child->script_id == function_info->get_script_id() && | 202 if (child->script_id == function_info->get_script_id() && |
| 201 child->start_position == function_info->get_start_position()) | 203 child->start_position == function_info->get_start_position()) |
| 202 return child; | 204 return child; |
| 203 } | 205 } |
| 204 Node* child = AllocateNode(profile, scripts, function_info); | 206 v8::AllocationProfile::Node* child = |
| 207 AllocateNode(profile, scripts, function_info); | |
| 205 parent->children.push_back(child); | 208 parent->children.push_back(child); |
| 206 return child; | 209 return child; |
| 207 } | 210 } |
| 208 | 211 |
| 209 | 212 v8::AllocationProfile::Node* SamplingHeapProfiler::AddStack( |
| 210 SamplingHeapProfiler::Node* SamplingHeapProfiler::AddStack( | |
| 211 AllocationProfile* profile, const std::map<int, Script*>& scripts, | 213 AllocationProfile* profile, const std::map<int, Script*>& scripts, |
| 212 const std::vector<FunctionInfo*>& stack) { | 214 const std::vector<FunctionInfo*>& stack) { |
| 213 Node* node = profile->GetRootNode(); | 215 v8::AllocationProfile::Node* node = profile->GetRootNode(); |
| 214 | 216 |
| 215 // We need to process the stack in reverse order as the top of the stack is | 217 // We need to process the stack in reverse order as the top of the stack is |
| 216 // the first element in the list. | 218 // the first element in the list. |
| 217 for (auto it = stack.rbegin(); it != stack.rend(); ++it) { | 219 for (auto it = stack.rbegin(); it != stack.rend(); ++it) { |
| 218 FunctionInfo* function_info = *it; | 220 FunctionInfo* function_info = *it; |
| 219 node = FindOrAddChildNode(profile, scripts, node, function_info); | 221 node = FindOrAddChildNode(profile, scripts, node, function_info); |
| 220 } | 222 } |
| 221 return node; | 223 return node; |
| 222 } | 224 } |
| 223 | 225 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 234 } | 236 } |
| 235 } | 237 } |
| 236 | 238 |
| 237 auto profile = new v8::internal::AllocationProfile(); | 239 auto profile = new v8::internal::AllocationProfile(); |
| 238 | 240 |
| 239 // Create the root node. | 241 // Create the root node. |
| 240 FunctionInfo function_info("(root)"); | 242 FunctionInfo function_info("(root)"); |
| 241 AllocateNode(profile, scripts, &function_info); | 243 AllocateNode(profile, scripts, &function_info); |
| 242 | 244 |
| 243 for (SampledAllocation* allocation : samples_) { | 245 for (SampledAllocation* allocation : samples_) { |
| 244 Node* node = AddStack(profile, scripts, allocation->get_stack()); | 246 v8::AllocationProfile::Node* node = |
| 247 AddStack(profile, scripts, allocation->get_stack()); | |
| 245 node->allocations.push_back({allocation->get_size(), 1}); | 248 node->allocations.push_back({allocation->get_size(), 1}); |
| 246 } | 249 } |
| 247 | 250 |
| 248 return profile; | 251 return profile; |
| 249 } | 252 } |
| 250 | 253 |
| 251 | 254 |
| 252 } // namespace internal | 255 } // namespace internal |
| 253 } // namespace v8 | 256 } // namespace v8 |
| OLD | NEW |