Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2012 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/cpu-profiler.h" |
| 6 | |
| 7 | #include "src/debug/debug.h" |
| 8 | #include "src/deoptimizer.h" |
| 9 | #include "src/frames-inl.h" |
| 10 | #include "src/locked-queue-inl.h" |
| 11 | #include "src/log-inl.h" |
| 12 | #include "src/profiler/cpu-profiler-inl.h" |
| 13 | #include "src/vm-state-inl.h" |
| 14 | |
| 15 | #include "include/v8-profiler.h" |
| 16 | |
| 17 | namespace v8 { |
| 18 | namespace internal { |
| 19 | |
| 20 | static const int kProfilerStackSize = 64 * KB; |
| 21 | |
| 22 | |
| 23 | ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator, |
| 24 | Sampler* sampler, |
| 25 | base::TimeDelta period) |
| 26 | : Thread(Thread::Options("v8:ProfEvntProc", kProfilerStackSize)), |
| 27 | generator_(generator), |
| 28 | sampler_(sampler), |
| 29 | running_(1), |
| 30 | period_(period), |
| 31 | last_code_event_id_(0), |
| 32 | last_processed_code_event_id_(0) {} |
| 33 | |
| 34 | |
| 35 | ProfilerEventsProcessor::~ProfilerEventsProcessor() {} |
| 36 | |
| 37 | |
| 38 | void ProfilerEventsProcessor::Enqueue(const CodeEventsContainer& event) { |
| 39 | event.generic.order = last_code_event_id_.Increment(1); |
| 40 | events_buffer_.Enqueue(event); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | void ProfilerEventsProcessor::AddDeoptStack(Isolate* isolate, Address from, |
| 45 | int fp_to_sp_delta) { |
| 46 | TickSampleEventRecord record(last_code_event_id_.Value()); |
| 47 | RegisterState regs; |
| 48 | Address fp = isolate->c_entry_fp(isolate->thread_local_top()); |
| 49 | regs.sp = fp - fp_to_sp_delta; |
| 50 | regs.fp = fp; |
| 51 | regs.pc = from; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 52 | record.sample.Init(isolate, regs, TickSample::kSkipCEntryFrame, false); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 53 | ticks_from_vm_buffer_.Enqueue(record); |
| 54 | } |
| 55 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 56 | void ProfilerEventsProcessor::AddCurrentStack(Isolate* isolate, |
| 57 | bool update_stats) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 58 | TickSampleEventRecord record(last_code_event_id_.Value()); |
| 59 | RegisterState regs; |
| 60 | StackFrameIterator it(isolate); |
| 61 | if (!it.done()) { |
| 62 | StackFrame* frame = it.frame(); |
| 63 | regs.sp = frame->sp(); |
| 64 | regs.fp = frame->fp(); |
| 65 | regs.pc = frame->pc(); |
| 66 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 67 | record.sample.Init(isolate, regs, TickSample::kSkipCEntryFrame, update_stats); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 68 | ticks_from_vm_buffer_.Enqueue(record); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | void ProfilerEventsProcessor::StopSynchronously() { |
| 73 | if (!base::NoBarrier_AtomicExchange(&running_, 0)) return; |
| 74 | Join(); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | bool ProfilerEventsProcessor::ProcessCodeEvent() { |
| 79 | CodeEventsContainer record; |
| 80 | if (events_buffer_.Dequeue(&record)) { |
| 81 | switch (record.generic.type) { |
| 82 | #define PROFILER_TYPE_CASE(type, clss) \ |
| 83 | case CodeEventRecord::type: \ |
| 84 | record.clss##_.UpdateCodeMap(generator_->code_map()); \ |
| 85 | break; |
| 86 | |
| 87 | CODE_EVENTS_TYPE_LIST(PROFILER_TYPE_CASE) |
| 88 | |
| 89 | #undef PROFILER_TYPE_CASE |
| 90 | default: return true; // Skip record. |
| 91 | } |
| 92 | last_processed_code_event_id_ = record.generic.order; |
| 93 | return true; |
| 94 | } |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | ProfilerEventsProcessor::SampleProcessingResult |
| 99 | ProfilerEventsProcessor::ProcessOneSample() { |
| 100 | TickSampleEventRecord record1; |
| 101 | if (ticks_from_vm_buffer_.Peek(&record1) && |
| 102 | (record1.order == last_processed_code_event_id_)) { |
| 103 | TickSampleEventRecord record; |
| 104 | ticks_from_vm_buffer_.Dequeue(&record); |
| 105 | generator_->RecordTickSample(record.sample); |
| 106 | return OneSampleProcessed; |
| 107 | } |
| 108 | |
| 109 | const TickSampleEventRecord* record = ticks_buffer_.Peek(); |
| 110 | if (record == NULL) { |
| 111 | if (ticks_from_vm_buffer_.IsEmpty()) return NoSamplesInQueue; |
| 112 | return FoundSampleForNextCodeEvent; |
| 113 | } |
| 114 | if (record->order != last_processed_code_event_id_) { |
| 115 | return FoundSampleForNextCodeEvent; |
| 116 | } |
| 117 | generator_->RecordTickSample(record->sample); |
| 118 | ticks_buffer_.Remove(); |
| 119 | return OneSampleProcessed; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | void ProfilerEventsProcessor::Run() { |
| 124 | while (!!base::NoBarrier_Load(&running_)) { |
| 125 | base::TimeTicks nextSampleTime = |
| 126 | base::TimeTicks::HighResolutionNow() + period_; |
| 127 | base::TimeTicks now; |
| 128 | SampleProcessingResult result; |
| 129 | // Keep processing existing events until we need to do next sample |
| 130 | // or the ticks buffer is empty. |
| 131 | do { |
| 132 | result = ProcessOneSample(); |
| 133 | if (result == FoundSampleForNextCodeEvent) { |
| 134 | // All ticks of the current last_processed_code_event_id_ are |
| 135 | // processed, proceed to the next code event. |
| 136 | ProcessCodeEvent(); |
| 137 | } |
| 138 | now = base::TimeTicks::HighResolutionNow(); |
| 139 | } while (result != NoSamplesInQueue && now < nextSampleTime); |
| 140 | |
| 141 | if (nextSampleTime > now) { |
| 142 | #if V8_OS_WIN |
| 143 | // Do not use Sleep on Windows as it is very imprecise. |
| 144 | // Could be up to 16ms jitter, which is unacceptable for the purpose. |
| 145 | while (base::TimeTicks::HighResolutionNow() < nextSampleTime) { |
| 146 | } |
| 147 | #else |
| 148 | base::OS::Sleep(nextSampleTime - now); |
| 149 | #endif |
| 150 | } |
| 151 | |
| 152 | // Schedule next sample. sampler_ is NULL in tests. |
| 153 | if (sampler_) sampler_->DoSample(); |
| 154 | } |
| 155 | |
| 156 | // Process remaining tick events. |
| 157 | do { |
| 158 | SampleProcessingResult result; |
| 159 | do { |
| 160 | result = ProcessOneSample(); |
| 161 | } while (result == OneSampleProcessed); |
| 162 | } while (ProcessCodeEvent()); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | void* ProfilerEventsProcessor::operator new(size_t size) { |
| 167 | return AlignedAlloc(size, V8_ALIGNOF(ProfilerEventsProcessor)); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | void ProfilerEventsProcessor::operator delete(void* ptr) { |
| 172 | AlignedFree(ptr); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | int CpuProfiler::GetProfilesCount() { |
| 177 | // The count of profiles doesn't depend on a security token. |
| 178 | return profiles_->profiles()->length(); |
| 179 | } |
| 180 | |
| 181 | |
| 182 | CpuProfile* CpuProfiler::GetProfile(int index) { |
| 183 | return profiles_->profiles()->at(index); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | void CpuProfiler::DeleteAllProfiles() { |
| 188 | if (is_profiling_) StopProcessor(); |
| 189 | ResetProfiles(); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | void CpuProfiler::DeleteProfile(CpuProfile* profile) { |
| 194 | profiles_->RemoveProfile(profile); |
| 195 | delete profile; |
| 196 | if (profiles_->profiles()->is_empty() && !is_profiling_) { |
| 197 | // If this was the last profile, clean up all accessory data as well. |
| 198 | ResetProfiles(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | |
| 203 | void CpuProfiler::CallbackEvent(Name* name, Address entry_point) { |
| 204 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 205 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 206 | rec->start = entry_point; |
| 207 | rec->entry = profiles_->NewCodeEntry( |
| 208 | Logger::CALLBACK_TAG, |
| 209 | profiles_->GetName(name)); |
| 210 | rec->size = 1; |
| 211 | processor_->Enqueue(evt_rec); |
| 212 | } |
| 213 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 214 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 215 | AbstractCode* code, const char* name) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 216 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 217 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 218 | rec->start = code->address(); |
| 219 | rec->entry = profiles_->NewCodeEntry( |
| 220 | tag, profiles_->GetFunctionName(name), CodeEntry::kEmptyNamePrefix, |
| 221 | CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, |
| 222 | CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 223 | RecordInliningInfo(rec->entry, code); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 224 | rec->size = code->ExecutableSize(); |
| 225 | processor_->Enqueue(evt_rec); |
| 226 | } |
| 227 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 228 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 229 | AbstractCode* code, Name* name) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 230 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 231 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 232 | rec->start = code->address(); |
| 233 | rec->entry = profiles_->NewCodeEntry( |
| 234 | tag, profiles_->GetFunctionName(name), CodeEntry::kEmptyNamePrefix, |
| 235 | CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, |
| 236 | CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 237 | RecordInliningInfo(rec->entry, code); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 238 | rec->size = code->ExecutableSize(); |
| 239 | processor_->Enqueue(evt_rec); |
| 240 | } |
| 241 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 242 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 243 | AbstractCode* code, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 244 | SharedFunctionInfo* shared, |
| 245 | CompilationInfo* info, Name* script_name) { |
| 246 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 247 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 248 | rec->start = code->address(); |
| 249 | rec->entry = profiles_->NewCodeEntry( |
| 250 | tag, profiles_->GetFunctionName(shared->DebugName()), |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 251 | CodeEntry::kEmptyNamePrefix, |
| 252 | profiles_->GetName(InferScriptName(script_name, shared)), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 253 | CpuProfileNode::kNoLineNumberInfo, CpuProfileNode::kNoColumnNumberInfo, |
| 254 | NULL, code->instruction_start()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 255 | RecordInliningInfo(rec->entry, code); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 256 | if (info) { |
| 257 | rec->entry->set_inlined_function_infos(info->inlined_function_infos()); |
| 258 | } |
| 259 | rec->entry->FillFunctionInfo(shared); |
| 260 | rec->size = code->ExecutableSize(); |
| 261 | processor_->Enqueue(evt_rec); |
| 262 | } |
| 263 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 264 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 265 | AbstractCode* abstract_code, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 266 | SharedFunctionInfo* shared, |
| 267 | CompilationInfo* info, Name* script_name, |
| 268 | int line, int column) { |
| 269 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 270 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 271 | rec->start = abstract_code->address(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 272 | Script* script = Script::cast(shared->script()); |
| 273 | JITLineInfoTable* line_table = NULL; |
| 274 | if (script) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 275 | if (abstract_code->IsCode()) { |
| 276 | Code* code = abstract_code->GetCode(); |
| 277 | int start_position = shared->start_position(); |
| 278 | int end_position = shared->end_position(); |
| 279 | line_table = new JITLineInfoTable(); |
| 280 | for (RelocIterator it(code); !it.done(); it.next()) { |
| 281 | RelocInfo* reloc_info = it.rinfo(); |
| 282 | if (!RelocInfo::IsPosition(reloc_info->rmode())) continue; |
| 283 | int position = static_cast<int>(reloc_info->data()); |
| 284 | // TODO(alph): in case of inlining the position may correspond |
| 285 | // to an inlined function source code. Do not collect positions |
| 286 | // that fall beyond the function source code. There's however a |
| 287 | // chance the inlined function has similar positions but in another |
| 288 | // script. So the proper fix is to store script_id in some form |
| 289 | // along with the inlined function positions. |
| 290 | if (position < start_position || position >= end_position) continue; |
| 291 | int pc_offset = static_cast<int>(reloc_info->pc() - code->address()); |
| 292 | int line_number = script->GetLineNumber(position) + 1; |
| 293 | line_table->SetPosition(pc_offset, line_number); |
| 294 | } |
| 295 | } else { |
| 296 | BytecodeArray* bytecode = abstract_code->GetBytecodeArray(); |
| 297 | line_table = new JITLineInfoTable(); |
| 298 | interpreter::SourcePositionTableIterator it( |
| 299 | bytecode->source_position_table()); |
| 300 | for (; !it.done(); it.Advance()) { |
| 301 | int line_number = script->GetLineNumber(it.source_position()) + 1; |
| 302 | int pc_offset = it.bytecode_offset() + BytecodeArray::kHeaderSize; |
| 303 | line_table->SetPosition(pc_offset, line_number); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | } |
| 307 | rec->entry = profiles_->NewCodeEntry( |
| 308 | tag, profiles_->GetFunctionName(shared->DebugName()), |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 309 | CodeEntry::kEmptyNamePrefix, |
| 310 | profiles_->GetName(InferScriptName(script_name, shared)), line, column, |
| 311 | line_table, abstract_code->instruction_start()); |
| 312 | RecordInliningInfo(rec->entry, abstract_code); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 313 | if (info) { |
| 314 | rec->entry->set_inlined_function_infos(info->inlined_function_infos()); |
| 315 | } |
| 316 | rec->entry->FillFunctionInfo(shared); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 317 | rec->size = abstract_code->ExecutableSize(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 318 | processor_->Enqueue(evt_rec); |
| 319 | } |
| 320 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 321 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 322 | AbstractCode* code, int args_count) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 323 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 324 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 325 | rec->start = code->address(); |
| 326 | rec->entry = profiles_->NewCodeEntry( |
| 327 | tag, profiles_->GetName(args_count), "args_count: ", |
| 328 | CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, |
| 329 | CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 330 | RecordInliningInfo(rec->entry, code); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 331 | rec->size = code->ExecutableSize(); |
| 332 | processor_->Enqueue(evt_rec); |
| 333 | } |
| 334 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 335 | void CpuProfiler::CodeMoveEvent(AbstractCode* from, Address to) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 336 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_MOVE); |
| 337 | CodeMoveEventRecord* rec = &evt_rec.CodeMoveEventRecord_; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 338 | rec->from = from->address(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 339 | rec->to = to; |
| 340 | processor_->Enqueue(evt_rec); |
| 341 | } |
| 342 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 343 | void CpuProfiler::CodeDisableOptEvent(AbstractCode* code, |
| 344 | SharedFunctionInfo* shared) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 345 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_DISABLE_OPT); |
| 346 | CodeDisableOptEventRecord* rec = &evt_rec.CodeDisableOptEventRecord_; |
| 347 | rec->start = code->address(); |
| 348 | rec->bailout_reason = GetBailoutReason(shared->disable_optimization_reason()); |
| 349 | processor_->Enqueue(evt_rec); |
| 350 | } |
| 351 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 352 | void CpuProfiler::CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta) { |
| 353 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_DEOPT); |
| 354 | CodeDeoptEventRecord* rec = &evt_rec.CodeDeoptEventRecord_; |
| 355 | Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(code, pc); |
| 356 | rec->start = code->address(); |
| 357 | rec->deopt_reason = Deoptimizer::GetDeoptReason(info.deopt_reason); |
| 358 | rec->position = info.position; |
| 359 | rec->pc_offset = pc - code->instruction_start(); |
| 360 | processor_->Enqueue(evt_rec); |
| 361 | processor_->AddDeoptStack(isolate_, pc, fp_to_sp_delta); |
| 362 | } |
| 363 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 364 | void CpuProfiler::GetterCallbackEvent(Name* name, Address entry_point) { |
| 365 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 366 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 367 | rec->start = entry_point; |
| 368 | rec->entry = profiles_->NewCodeEntry( |
| 369 | Logger::CALLBACK_TAG, |
| 370 | profiles_->GetName(name), |
| 371 | "get "); |
| 372 | rec->size = 1; |
| 373 | processor_->Enqueue(evt_rec); |
| 374 | } |
| 375 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 376 | void CpuProfiler::RegExpCodeCreateEvent(AbstractCode* code, String* source) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 377 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 378 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 379 | rec->start = code->address(); |
| 380 | rec->entry = profiles_->NewCodeEntry( |
| 381 | Logger::REG_EXP_TAG, profiles_->GetName(source), "RegExp: ", |
| 382 | CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, |
| 383 | CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); |
| 384 | rec->size = code->ExecutableSize(); |
| 385 | processor_->Enqueue(evt_rec); |
| 386 | } |
| 387 | |
| 388 | |
| 389 | void CpuProfiler::SetterCallbackEvent(Name* name, Address entry_point) { |
| 390 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 391 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 392 | rec->start = entry_point; |
| 393 | rec->entry = profiles_->NewCodeEntry( |
| 394 | Logger::CALLBACK_TAG, |
| 395 | profiles_->GetName(name), |
| 396 | "set "); |
| 397 | rec->size = 1; |
| 398 | processor_->Enqueue(evt_rec); |
| 399 | } |
| 400 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 401 | Name* CpuProfiler::InferScriptName(Name* name, SharedFunctionInfo* info) { |
| 402 | if (name->IsString() && String::cast(name)->length()) return name; |
| 403 | if (!info->script()->IsScript()) return name; |
| 404 | Object* source_url = Script::cast(info->script())->source_url(); |
| 405 | return source_url->IsName() ? Name::cast(source_url) : name; |
| 406 | } |
| 407 | |
| 408 | void CpuProfiler::RecordInliningInfo(CodeEntry* entry, |
| 409 | AbstractCode* abstract_code) { |
| 410 | if (!abstract_code->IsCode()) return; |
| 411 | Code* code = abstract_code->GetCode(); |
| 412 | if (code->kind() != Code::OPTIMIZED_FUNCTION) return; |
| 413 | DeoptimizationInputData* deopt_input_data = |
| 414 | DeoptimizationInputData::cast(code->deoptimization_data()); |
| 415 | int deopt_count = deopt_input_data->DeoptCount(); |
| 416 | for (int i = 0; i < deopt_count; i++) { |
| 417 | int pc_offset = deopt_input_data->Pc(i)->value(); |
| 418 | if (pc_offset == -1) continue; |
| 419 | int translation_index = deopt_input_data->TranslationIndex(i)->value(); |
| 420 | TranslationIterator it(deopt_input_data->TranslationByteArray(), |
| 421 | translation_index); |
| 422 | Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next()); |
| 423 | DCHECK_EQ(Translation::BEGIN, opcode); |
| 424 | it.Skip(Translation::NumberOfOperandsFor(opcode)); |
| 425 | int depth = 0; |
| 426 | std::vector<CodeEntry*> inline_stack; |
| 427 | while (it.HasNext() && |
| 428 | Translation::BEGIN != |
| 429 | (opcode = static_cast<Translation::Opcode>(it.Next()))) { |
| 430 | if (opcode != Translation::JS_FRAME && |
| 431 | opcode != Translation::INTERPRETED_FRAME) { |
| 432 | it.Skip(Translation::NumberOfOperandsFor(opcode)); |
| 433 | continue; |
| 434 | } |
| 435 | it.Next(); // Skip ast_id |
| 436 | int shared_info_id = it.Next(); |
| 437 | it.Next(); // Skip height |
| 438 | SharedFunctionInfo* shared_info = SharedFunctionInfo::cast( |
| 439 | deopt_input_data->LiteralArray()->get(shared_info_id)); |
| 440 | if (!depth++) continue; // Skip the current function itself. |
| 441 | CodeEntry* inline_entry = new CodeEntry( |
| 442 | entry->tag(), profiles_->GetFunctionName(shared_info->DebugName()), |
| 443 | CodeEntry::kEmptyNamePrefix, entry->resource_name(), |
| 444 | CpuProfileNode::kNoLineNumberInfo, |
| 445 | CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); |
| 446 | inline_entry->FillFunctionInfo(shared_info); |
| 447 | inline_stack.push_back(inline_entry); |
| 448 | } |
| 449 | if (!inline_stack.empty()) { |
| 450 | entry->AddInlineStack(pc_offset, inline_stack); |
| 451 | DCHECK(inline_stack.empty()); |
| 452 | } |
| 453 | } |
| 454 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 455 | |
| 456 | CpuProfiler::CpuProfiler(Isolate* isolate) |
| 457 | : isolate_(isolate), |
| 458 | sampling_interval_(base::TimeDelta::FromMicroseconds( |
| 459 | FLAG_cpu_profiler_sampling_interval)), |
| 460 | profiles_(new CpuProfilesCollection(isolate->heap())), |
| 461 | generator_(NULL), |
| 462 | processor_(NULL), |
| 463 | is_profiling_(false) { |
| 464 | } |
| 465 | |
| 466 | |
| 467 | CpuProfiler::CpuProfiler(Isolate* isolate, |
| 468 | CpuProfilesCollection* test_profiles, |
| 469 | ProfileGenerator* test_generator, |
| 470 | ProfilerEventsProcessor* test_processor) |
| 471 | : isolate_(isolate), |
| 472 | sampling_interval_(base::TimeDelta::FromMicroseconds( |
| 473 | FLAG_cpu_profiler_sampling_interval)), |
| 474 | profiles_(test_profiles), |
| 475 | generator_(test_generator), |
| 476 | processor_(test_processor), |
| 477 | is_profiling_(false) { |
| 478 | } |
| 479 | |
| 480 | |
| 481 | CpuProfiler::~CpuProfiler() { |
| 482 | DCHECK(!is_profiling_); |
| 483 | delete profiles_; |
| 484 | } |
| 485 | |
| 486 | |
| 487 | void CpuProfiler::set_sampling_interval(base::TimeDelta value) { |
| 488 | DCHECK(!is_profiling_); |
| 489 | sampling_interval_ = value; |
| 490 | } |
| 491 | |
| 492 | |
| 493 | void CpuProfiler::ResetProfiles() { |
| 494 | delete profiles_; |
| 495 | profiles_ = new CpuProfilesCollection(isolate()->heap()); |
| 496 | } |
| 497 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 498 | void CpuProfiler::CollectSample() { |
| 499 | if (processor_ != NULL) { |
| 500 | processor_->AddCurrentStack(isolate_); |
| 501 | } |
| 502 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 503 | |
| 504 | void CpuProfiler::StartProfiling(const char* title, bool record_samples) { |
| 505 | if (profiles_->StartProfiling(title, record_samples)) { |
| 506 | StartProcessorIfNotStarted(); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | |
| 511 | void CpuProfiler::StartProfiling(String* title, bool record_samples) { |
| 512 | StartProfiling(profiles_->GetName(title), record_samples); |
| 513 | isolate_->debug()->feature_tracker()->Track(DebugFeatureTracker::kProfiler); |
| 514 | } |
| 515 | |
| 516 | |
| 517 | void CpuProfiler::StartProcessorIfNotStarted() { |
| 518 | if (processor_ != NULL) { |
| 519 | processor_->AddCurrentStack(isolate_); |
| 520 | return; |
| 521 | } |
| 522 | Logger* logger = isolate_->logger(); |
| 523 | // Disable logging when using the new implementation. |
| 524 | saved_is_logging_ = logger->is_logging_; |
| 525 | logger->is_logging_ = false; |
| 526 | generator_ = new ProfileGenerator(profiles_); |
| 527 | Sampler* sampler = logger->sampler(); |
| 528 | processor_ = new ProfilerEventsProcessor( |
| 529 | generator_, sampler, sampling_interval_); |
| 530 | is_profiling_ = true; |
| 531 | // Enumerate stuff we already have in the heap. |
| 532 | DCHECK(isolate_->heap()->HasBeenSetUp()); |
| 533 | if (!FLAG_prof_browser_mode) { |
| 534 | logger->LogCodeObjects(); |
| 535 | } |
| 536 | logger->LogCompiledFunctions(); |
| 537 | logger->LogAccessorCallbacks(); |
| 538 | LogBuiltins(); |
| 539 | // Enable stack sampling. |
| 540 | sampler->SetHasProcessingThread(true); |
| 541 | sampler->IncreaseProfilingDepth(); |
| 542 | processor_->AddCurrentStack(isolate_); |
| 543 | processor_->StartSynchronously(); |
| 544 | } |
| 545 | |
| 546 | |
| 547 | CpuProfile* CpuProfiler::StopProfiling(const char* title) { |
| 548 | if (!is_profiling_) return NULL; |
| 549 | StopProcessorIfLastProfile(title); |
| 550 | CpuProfile* result = profiles_->StopProfiling(title); |
| 551 | if (result != NULL) { |
| 552 | result->Print(); |
| 553 | } |
| 554 | return result; |
| 555 | } |
| 556 | |
| 557 | |
| 558 | CpuProfile* CpuProfiler::StopProfiling(String* title) { |
| 559 | if (!is_profiling_) return NULL; |
| 560 | const char* profile_title = profiles_->GetName(title); |
| 561 | StopProcessorIfLastProfile(profile_title); |
| 562 | return profiles_->StopProfiling(profile_title); |
| 563 | } |
| 564 | |
| 565 | |
| 566 | void CpuProfiler::StopProcessorIfLastProfile(const char* title) { |
| 567 | if (profiles_->IsLastProfile(title)) StopProcessor(); |
| 568 | } |
| 569 | |
| 570 | |
| 571 | void CpuProfiler::StopProcessor() { |
| 572 | Logger* logger = isolate_->logger(); |
| 573 | Sampler* sampler = reinterpret_cast<Sampler*>(logger->ticker_); |
| 574 | is_profiling_ = false; |
| 575 | processor_->StopSynchronously(); |
| 576 | delete processor_; |
| 577 | delete generator_; |
| 578 | processor_ = NULL; |
| 579 | generator_ = NULL; |
| 580 | sampler->SetHasProcessingThread(false); |
| 581 | sampler->DecreaseProfilingDepth(); |
| 582 | logger->is_logging_ = saved_is_logging_; |
| 583 | } |
| 584 | |
| 585 | |
| 586 | void CpuProfiler::LogBuiltins() { |
| 587 | Builtins* builtins = isolate_->builtins(); |
| 588 | DCHECK(builtins->is_initialized()); |
| 589 | for (int i = 0; i < Builtins::builtin_count; i++) { |
| 590 | CodeEventsContainer evt_rec(CodeEventRecord::REPORT_BUILTIN); |
| 591 | ReportBuiltinEventRecord* rec = &evt_rec.ReportBuiltinEventRecord_; |
| 592 | Builtins::Name id = static_cast<Builtins::Name>(i); |
| 593 | rec->start = builtins->builtin(id)->address(); |
| 594 | rec->builtin_id = id; |
| 595 | processor_->Enqueue(evt_rec); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | |
| 600 | } // namespace internal |
| 601 | } // namespace v8 |