OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/profiler/sampling-heap-profiler.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 #include <memory> |
| 9 #include "src/api.h" |
| 10 #include "src/base/utils/random-number-generator.h" |
| 11 #include "src/frames-inl.h" |
| 12 #include "src/heap/heap.h" |
| 13 #include "src/isolate.h" |
| 14 #include "src/profiler/strings-storage.h" |
| 15 |
| 16 namespace v8 { |
| 17 namespace internal { |
| 18 |
| 19 SamplingHeapProfiler::SamplingHeapProfiler(Heap* heap, StringsStorage* names, |
| 20 uint64_t rate, int stack_depth) |
| 21 : InlineAllocationObserver(GetNextSampleInterval( |
| 22 heap->isolate()->random_number_generator(), rate)), |
| 23 isolate_(heap->isolate()), |
| 24 heap_(heap), |
| 25 random_(isolate_->random_number_generator()), |
| 26 names_(names), |
| 27 samples_(), |
| 28 rate_(rate), |
| 29 stack_depth_(stack_depth) { |
| 30 heap->new_space()->AddInlineAllocationObserver(this); |
| 31 } |
| 32 |
| 33 |
| 34 SamplingHeapProfiler::~SamplingHeapProfiler() { |
| 35 heap_->new_space()->RemoveInlineAllocationObserver(this); |
| 36 |
| 37 // Clear samples and drop all the weak references we are keeping. |
| 38 std::set<SampledAllocation*>::iterator it; |
| 39 for (it = samples_.begin(); it != samples_.end(); ++it) { |
| 40 delete *it; |
| 41 } |
| 42 std::set<SampledAllocation*> empty; |
| 43 samples_.swap(empty); |
| 44 } |
| 45 |
| 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 |
| 54 void SamplingHeapProfiler::SampleObject(Address soon_object, size_t size) { |
| 55 DisallowHeapAllocation no_allocation; |
| 56 |
| 57 HandleScope scope(isolate_); |
| 58 HeapObject* heap_object = HeapObject::FromAddress(soon_object); |
| 59 Handle<Object> obj(heap_object, isolate_); |
| 60 |
| 61 // Mark the new block as FreeSpace to make sure the heap is iterable while we |
| 62 // are taking the sample. |
| 63 heap()->CreateFillerObjectAt(soon_object, static_cast<int>(size)); |
| 64 |
| 65 Local<v8::Value> loc = v8::Utils::ToLocal(obj); |
| 66 |
| 67 SampledAllocation* sample = |
| 68 new SampledAllocation(this, isolate_, loc, size, stack_depth_); |
| 69 samples_.insert(sample); |
| 70 } |
| 71 |
| 72 |
| 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( |
| 93 const WeakCallbackInfo<SampledAllocation>& data) { |
| 94 SampledAllocation* sample = data.GetParameter(); |
| 95 sample->sampling_heap_profiler_->samples_.erase(sample); |
| 96 delete sample; |
| 97 } |
| 98 |
| 99 |
| 100 SamplingHeapProfiler::FunctionInfo::FunctionInfo(SharedFunctionInfo* shared, |
| 101 StringsStorage* names) |
| 102 : name_(names->GetFunctionName(shared->DebugName())), |
| 103 script_name_(""), |
| 104 script_id_(v8::UnboundScript::kNoScriptId), |
| 105 start_position_(shared->start_position()) { |
| 106 if (shared->script()->IsScript()) { |
| 107 Script* script = Script::cast(shared->script()); |
| 108 script_id_ = script->id(); |
| 109 if (script->name()->IsName()) { |
| 110 Name* name = Name::cast(script->name()); |
| 111 script_name_ = names->GetName(name); |
| 112 } |
| 113 } |
| 114 } |
| 115 |
| 116 |
| 117 SamplingHeapProfiler::SampledAllocation::SampledAllocation( |
| 118 SamplingHeapProfiler* sampling_heap_profiler, Isolate* isolate, |
| 119 Local<Value> local, size_t size, int max_frames) |
| 120 : sampling_heap_profiler_(sampling_heap_profiler), |
| 121 global_(reinterpret_cast<v8::Isolate*>(isolate), local), |
| 122 size_(size) { |
| 123 global_.SetWeak(this, OnWeakCallback, WeakCallbackType::kParameter); |
| 124 |
| 125 StackTraceFrameIterator it(isolate); |
| 126 int frames_captured = 0; |
| 127 while (!it.done() && frames_captured < max_frames) { |
| 128 JavaScriptFrame* frame = it.frame(); |
| 129 SharedFunctionInfo* shared = frame->function()->shared(); |
| 130 stack_.push_back(new FunctionInfo(shared, sampling_heap_profiler->names())); |
| 131 |
| 132 frames_captured++; |
| 133 it.Advance(); |
| 134 } |
| 135 |
| 136 if (frames_captured == 0) { |
| 137 const char* name = nullptr; |
| 138 switch (isolate->current_vm_state()) { |
| 139 case GC: |
| 140 name = "(GC)"; |
| 141 break; |
| 142 case COMPILER: |
| 143 name = "(COMPILER)"; |
| 144 break; |
| 145 case OTHER: |
| 146 name = "(V8 API)"; |
| 147 break; |
| 148 case EXTERNAL: |
| 149 name = "(EXTERNAL)"; |
| 150 break; |
| 151 case IDLE: |
| 152 name = "(IDLE)"; |
| 153 break; |
| 154 case JS: |
| 155 name = "(JS)"; |
| 156 break; |
| 157 } |
| 158 stack_.push_back(new FunctionInfo(name)); |
| 159 } |
| 160 } |
| 161 |
| 162 |
| 163 SamplingHeapProfiler::Node* SamplingHeapProfiler::AllocateNode( |
| 164 AllocationProfile* profile, const std::map<int, Script*>& scripts, |
| 165 FunctionInfo* function_info) { |
| 166 DCHECK(function_info->get_name()); |
| 167 DCHECK(function_info->get_script_name()); |
| 168 |
| 169 int line = v8::AllocationProfile::kNoLineNumberInfo; |
| 170 int column = v8::AllocationProfile::kNoColumnNumberInfo; |
| 171 |
| 172 if (function_info->get_script_id() != v8::UnboundScript::kNoScriptId) { |
| 173 // 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); |
| 175 Handle<Script> script(non_const_scripts[function_info->get_script_id()]); |
| 176 |
| 177 line = |
| 178 1 + Script::GetLineNumber(script, function_info->get_start_position()); |
| 179 column = 1 + Script::GetColumnNumber(script, |
| 180 function_info->get_start_position()); |
| 181 } |
| 182 |
| 183 profile->nodes().push_back( |
| 184 Node({ToApiHandle<v8::String>(isolate_->factory()->InternalizeUtf8String( |
| 185 function_info->get_name())), |
| 186 ToApiHandle<v8::String>(isolate_->factory()->InternalizeUtf8String( |
| 187 function_info->get_script_name())), |
| 188 function_info->get_script_id(), function_info->get_start_position(), |
| 189 line, column, std::vector<Node*>(), |
| 190 std::vector<v8::AllocationProfile::Allocation>()})); |
| 191 |
| 192 return &profile->nodes().back(); |
| 193 } |
| 194 |
| 195 |
| 196 SamplingHeapProfiler::Node* SamplingHeapProfiler::FindOrAddChildNode( |
| 197 AllocationProfile* profile, const std::map<int, Script*>& scripts, |
| 198 Node* parent, FunctionInfo* function_info) { |
| 199 for (Node* child : parent->children) { |
| 200 if (child->script_id == function_info->get_script_id() && |
| 201 child->start_position == function_info->get_start_position()) |
| 202 return child; |
| 203 } |
| 204 Node* child = AllocateNode(profile, scripts, function_info); |
| 205 parent->children.push_back(child); |
| 206 return child; |
| 207 } |
| 208 |
| 209 |
| 210 SamplingHeapProfiler::Node* SamplingHeapProfiler::AddStack( |
| 211 AllocationProfile* profile, const std::map<int, Script*>& scripts, |
| 212 const std::vector<FunctionInfo*>& stack) { |
| 213 Node* node = profile->GetRootNode(); |
| 214 |
| 215 // We need to process the stack in reverse order as the top of the stack is |
| 216 // the first element in the list. |
| 217 for (auto it = stack.rbegin(); it != stack.rend(); ++it) { |
| 218 FunctionInfo* function_info = *it; |
| 219 node = FindOrAddChildNode(profile, scripts, node, function_info); |
| 220 } |
| 221 return node; |
| 222 } |
| 223 |
| 224 |
| 225 v8::AllocationProfile* SamplingHeapProfiler::GetAllocationProfile() { |
| 226 // To resolve positions to line/column numbers, we will need to look up |
| 227 // scripts. Build a map to allow fast mapping from script id to script. |
| 228 std::map<int, Script*> scripts; |
| 229 { |
| 230 Script::Iterator iterator(isolate_); |
| 231 Script* script; |
| 232 while ((script = iterator.Next())) { |
| 233 scripts[script->id()] = script; |
| 234 } |
| 235 } |
| 236 |
| 237 auto profile = new v8::internal::AllocationProfile(); |
| 238 |
| 239 // Create the root node. |
| 240 FunctionInfo function_info("(root)"); |
| 241 AllocateNode(profile, scripts, &function_info); |
| 242 |
| 243 for (SampledAllocation* allocation : samples_) { |
| 244 Node* node = AddStack(profile, scripts, allocation->get_stack()); |
| 245 node->allocations.push_back({allocation->get_size(), 1}); |
| 246 } |
| 247 |
| 248 return profile; |
| 249 } |
| 250 |
| 251 |
| 252 } // namespace internal |
| 253 } // namespace v8 |
OLD | NEW |