| 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 double u = random->NextDouble(); | |
| 82 double next = (-std::log(u)) * rate; | |
| 83 return next < kPointerSize | |
| 84 ? kPointerSize | |
| 85 : (next > INT_MAX ? INT_MAX : static_cast<intptr_t>(next)); | |
| 86 } | |
| 87 | |
| 88 | |
| 89 void SamplingHeapProfiler::SampledAllocation::OnWeakCallback( | |
| 90 const WeakCallbackInfo<SampledAllocation>& data) { | |
| 91 SampledAllocation* sample = data.GetParameter(); | |
| 92 sample->sampling_heap_profiler_->samples_.erase(sample); | |
| 93 delete sample; | |
| 94 } | |
| 95 | |
| 96 | |
| 97 SamplingHeapProfiler::FunctionInfo::FunctionInfo(SharedFunctionInfo* shared, | |
| 98 StringsStorage* names) | |
| 99 : name_(names->GetFunctionName(shared->DebugName())), | |
| 100 script_name_(""), | |
| 101 script_id_(v8::UnboundScript::kNoScriptId), | |
| 102 start_position_(shared->start_position()) { | |
| 103 if (shared->script()->IsScript()) { | |
| 104 Script* script = Script::cast(shared->script()); | |
| 105 script_id_ = script->id(); | |
| 106 if (script->name()->IsName()) { | |
| 107 Name* name = Name::cast(script->name()); | |
| 108 script_name_ = names->GetName(name); | |
| 109 } | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 | |
| 114 SamplingHeapProfiler::SampledAllocation::SampledAllocation( | |
| 115 SamplingHeapProfiler* sampling_heap_profiler, Isolate* isolate, | |
| 116 Local<Value> local, size_t size, int max_frames) | |
| 117 : sampling_heap_profiler_(sampling_heap_profiler), | |
| 118 global_(reinterpret_cast<v8::Isolate*>(isolate), local), | |
| 119 size_(size) { | |
| 120 global_.SetWeak(this, OnWeakCallback, WeakCallbackType::kParameter); | |
| 121 | |
| 122 StackTraceFrameIterator it(isolate); | |
| 123 int frames_captured = 0; | |
| 124 while (!it.done() && frames_captured < max_frames) { | |
| 125 JavaScriptFrame* frame = it.frame(); | |
| 126 SharedFunctionInfo* shared = frame->function()->shared(); | |
| 127 stack_.push_back(new FunctionInfo(shared, sampling_heap_profiler->names())); | |
| 128 | |
| 129 frames_captured++; | |
| 130 it.Advance(); | |
| 131 } | |
| 132 | |
| 133 if (frames_captured == 0) { | |
| 134 const char* name = nullptr; | |
| 135 switch (isolate->current_vm_state()) { | |
| 136 case GC: | |
| 137 name = "(GC)"; | |
| 138 break; | |
| 139 case COMPILER: | |
| 140 name = "(COMPILER)"; | |
| 141 break; | |
| 142 case OTHER: | |
| 143 name = "(V8 API)"; | |
| 144 break; | |
| 145 case EXTERNAL: | |
| 146 name = "(EXTERNAL)"; | |
| 147 break; | |
| 148 case IDLE: | |
| 149 name = "(IDLE)"; | |
| 150 break; | |
| 151 case JS: | |
| 152 name = "(JS)"; | |
| 153 break; | |
| 154 } | |
| 155 stack_.push_back(new FunctionInfo(name)); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 | |
| 160 SamplingHeapProfiler::Node* SamplingHeapProfiler::AllocateNode( | |
| 161 AllocationProfile* profile, const std::map<int, Script*>& scripts, | |
| 162 FunctionInfo* function_info) { | |
| 163 DCHECK(function_info->get_name()); | |
| 164 DCHECK(function_info->get_script_name()); | |
| 165 | |
| 166 int line = v8::AllocationProfile::kNoLineNumberInfo; | |
| 167 int column = v8::AllocationProfile::kNoColumnNumberInfo; | |
| 168 | |
| 169 if (function_info->get_script_id() != v8::UnboundScript::kNoScriptId) { | |
| 170 // Cannot use std::map<T>::at because it is not available on android. | |
| 171 auto non_const_scripts = const_cast<std::map<int, Script*>&>(scripts); | |
| 172 Handle<Script> script(non_const_scripts[function_info->get_script_id()]); | |
| 173 | |
| 174 line = | |
| 175 1 + Script::GetLineNumber(script, function_info->get_start_position()); | |
| 176 column = 1 + Script::GetColumnNumber(script, | |
| 177 function_info->get_start_position()); | |
| 178 } | |
| 179 | |
| 180 profile->nodes().push_back( | |
| 181 Node({ToApiHandle<v8::String>(isolate_->factory()->InternalizeUtf8String( | |
| 182 function_info->get_name())), | |
| 183 ToApiHandle<v8::String>(isolate_->factory()->InternalizeUtf8String( | |
| 184 function_info->get_script_name())), | |
| 185 function_info->get_script_id(), function_info->get_start_position(), | |
| 186 line, column, std::vector<Node*>(), | |
| 187 std::vector<v8::AllocationProfile::Allocation>()})); | |
| 188 | |
| 189 return &profile->nodes().back(); | |
| 190 } | |
| 191 | |
| 192 | |
| 193 SamplingHeapProfiler::Node* SamplingHeapProfiler::FindOrAddChildNode( | |
| 194 AllocationProfile* profile, const std::map<int, Script*>& scripts, | |
| 195 Node* parent, FunctionInfo* function_info) { | |
| 196 for (Node* child : parent->children) { | |
| 197 if (child->script_id == function_info->get_script_id() && | |
| 198 child->start_position == function_info->get_start_position()) | |
| 199 return child; | |
| 200 } | |
| 201 Node* child = AllocateNode(profile, scripts, function_info); | |
| 202 parent->children.push_back(child); | |
| 203 return child; | |
| 204 } | |
| 205 | |
| 206 | |
| 207 SamplingHeapProfiler::Node* SamplingHeapProfiler::AddStack( | |
| 208 AllocationProfile* profile, const std::map<int, Script*>& scripts, | |
| 209 const std::vector<FunctionInfo*>& stack) { | |
| 210 Node* node = profile->GetRootNode(); | |
| 211 | |
| 212 // We need to process the stack in reverse order as the top of the stack is | |
| 213 // the first element in the list. | |
| 214 for (auto it = stack.rbegin(); it != stack.rend(); ++it) { | |
| 215 FunctionInfo* function_info = *it; | |
| 216 node = FindOrAddChildNode(profile, scripts, node, function_info); | |
| 217 } | |
| 218 return node; | |
| 219 } | |
| 220 | |
| 221 | |
| 222 v8::AllocationProfile* SamplingHeapProfiler::GetAllocationProfile() { | |
| 223 // To resolve positions to line/column numbers, we will need to look up | |
| 224 // scripts. Build a map to allow fast mapping from script id to script. | |
| 225 std::map<int, Script*> scripts; | |
| 226 { | |
| 227 Script::Iterator iterator(isolate_); | |
| 228 Script* script; | |
| 229 while ((script = iterator.Next())) { | |
| 230 scripts[script->id()] = script; | |
| 231 } | |
| 232 } | |
| 233 | |
| 234 auto profile = new v8::internal::AllocationProfile(); | |
| 235 | |
| 236 // Create the root node. | |
| 237 FunctionInfo function_info("(root)"); | |
| 238 AllocateNode(profile, scripts, &function_info); | |
| 239 | |
| 240 for (SampledAllocation* allocation : samples_) { | |
| 241 Node* node = AddStack(profile, scripts, allocation->get_stack()); | |
| 242 node->allocations.push_back({allocation->get_size(), 1}); | |
| 243 } | |
| 244 | |
| 245 return profile; | |
| 246 } | |
| 247 | |
| 248 | |
| 249 } // namespace internal | |
| 250 } // namespace v8 | |
| OLD | NEW |