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.
7 #include "src/cpu-profiler-inl.h"
9 #include "src/compiler.h"
10 #include "src/deoptimizer.h"
11 #include "src/frames-inl.h"
12 #include "src/hashmap.h"
13 #include "src/log-inl.h"
14 #include "src/vm-state-inl.h"
16 #include "include/v8-profiler.h"
21 static const int kProfilerStackSize = 64 * KB;
24 ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator,
26 base::TimeDelta period)
27 : Thread(Thread::Options("v8:ProfEvntProc", kProfilerStackSize)),
28 generator_(generator),
32 last_code_event_id_(0),
33 last_processed_code_event_id_(0) {}
36 void ProfilerEventsProcessor::Enqueue(const CodeEventsContainer& event) {
37 event.generic.order = ++last_code_event_id_;
38 events_buffer_.Enqueue(event);
42 void ProfilerEventsProcessor::AddDeoptStack(Isolate* isolate, Address from,
44 TickSampleEventRecord record(last_code_event_id_);
46 Address fp = isolate->c_entry_fp(isolate->thread_local_top());
47 regs.sp = fp - fp_to_sp_delta;
50 record.sample.Init(isolate, regs, TickSample::kSkipCEntryFrame);
51 ticks_from_vm_buffer_.Enqueue(record);
55 void ProfilerEventsProcessor::AddCurrentStack(Isolate* isolate) {
56 TickSampleEventRecord record(last_code_event_id_);
58 StackFrameIterator it(isolate);
60 StackFrame* frame = it.frame();
61 regs.sp = frame->sp();
62 regs.fp = frame->fp();
63 regs.pc = frame->pc();
65 record.sample.Init(isolate, regs, TickSample::kSkipCEntryFrame);
66 ticks_from_vm_buffer_.Enqueue(record);
70 void ProfilerEventsProcessor::StopSynchronously() {
71 if (!base::NoBarrier_AtomicExchange(&running_, 0)) return;
76 bool ProfilerEventsProcessor::ProcessCodeEvent() {
77 CodeEventsContainer record;
78 if (events_buffer_.Dequeue(&record)) {
79 switch (record.generic.type) {
80 #define PROFILER_TYPE_CASE(type, clss) \
81 case CodeEventRecord::type: \
82 record.clss##_.UpdateCodeMap(generator_->code_map()); \
85 CODE_EVENTS_TYPE_LIST(PROFILER_TYPE_CASE)
87 #undef PROFILER_TYPE_CASE
88 default: return true; // Skip record.
90 last_processed_code_event_id_ = record.generic.order;
96 ProfilerEventsProcessor::SampleProcessingResult
97 ProfilerEventsProcessor::ProcessOneSample() {
98 if (!ticks_from_vm_buffer_.IsEmpty()
99 && ticks_from_vm_buffer_.Peek()->order ==
100 last_processed_code_event_id_) {
101 TickSampleEventRecord record;
102 ticks_from_vm_buffer_.Dequeue(&record);
103 generator_->RecordTickSample(record.sample);
104 return OneSampleProcessed;
107 const TickSampleEventRecord* record = ticks_buffer_.Peek();
108 if (record == NULL) {
109 if (ticks_from_vm_buffer_.IsEmpty()) return NoSamplesInQueue;
110 return FoundSampleForNextCodeEvent;
112 if (record->order != last_processed_code_event_id_) {
113 return FoundSampleForNextCodeEvent;
115 generator_->RecordTickSample(record->sample);
116 ticks_buffer_.Remove();
117 return OneSampleProcessed;
121 void ProfilerEventsProcessor::Run() {
122 while (!!base::NoBarrier_Load(&running_)) {
123 base::ElapsedTimer timer;
125 // Keep processing existing events until we need to do next sample.
127 if (FoundSampleForNextCodeEvent == ProcessOneSample()) {
128 // All ticks of the current last_processed_code_event_id_ are
129 // processed, proceed to the next code event.
132 } while (!timer.HasExpired(period_));
134 // Schedule next sample. sampler_ is NULL in tests.
135 if (sampler_) sampler_->DoSample();
138 // Process remaining tick events.
140 SampleProcessingResult result;
142 result = ProcessOneSample();
143 } while (result == OneSampleProcessed);
144 } while (ProcessCodeEvent());
148 void* ProfilerEventsProcessor::operator new(size_t size) {
149 return AlignedAlloc(size, V8_ALIGNOF(ProfilerEventsProcessor));
153 void ProfilerEventsProcessor::operator delete(void* ptr) {
158 int CpuProfiler::GetProfilesCount() {
159 // The count of profiles doesn't depend on a security token.
160 return profiles_->profiles()->length();
164 CpuProfile* CpuProfiler::GetProfile(int index) {
165 return profiles_->profiles()->at(index);
169 void CpuProfiler::DeleteAllProfiles() {
170 if (is_profiling_) StopProcessor();
175 void CpuProfiler::DeleteProfile(CpuProfile* profile) {
176 profiles_->RemoveProfile(profile);
178 if (profiles_->profiles()->is_empty() && !is_profiling_) {
179 // If this was the last profile, clean up all accessory data as well.
185 static bool FilterOutCodeCreateEvent(Logger::LogEventsAndTags tag) {
186 return FLAG_prof_browser_mode
187 && (tag != Logger::CALLBACK_TAG
188 && tag != Logger::FUNCTION_TAG
189 && tag != Logger::LAZY_COMPILE_TAG
190 && tag != Logger::REG_EXP_TAG
191 && tag != Logger::SCRIPT_TAG);
195 void CpuProfiler::CallbackEvent(Name* name, Address entry_point) {
196 if (FilterOutCodeCreateEvent(Logger::CALLBACK_TAG)) return;
197 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
198 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
199 rec->start = entry_point;
200 rec->entry = profiles_->NewCodeEntry(
201 Logger::CALLBACK_TAG,
202 profiles_->GetName(name));
204 processor_->Enqueue(evt_rec);
208 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag,
211 if (FilterOutCodeCreateEvent(tag)) return;
212 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
213 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
214 rec->start = code->address();
215 rec->entry = profiles_->NewCodeEntry(
216 tag, profiles_->GetFunctionName(name), CodeEntry::kEmptyNamePrefix,
217 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo,
218 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start());
219 rec->size = code->ExecutableSize();
220 processor_->Enqueue(evt_rec);
224 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag,
227 if (FilterOutCodeCreateEvent(tag)) return;
228 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
229 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
230 rec->start = code->address();
231 rec->entry = profiles_->NewCodeEntry(
232 tag, profiles_->GetFunctionName(name), CodeEntry::kEmptyNamePrefix,
233 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo,
234 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start());
235 rec->size = code->ExecutableSize();
236 processor_->Enqueue(evt_rec);
240 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, Code* code,
241 SharedFunctionInfo* shared,
242 CompilationInfo* info, Name* script_name) {
243 if (FilterOutCodeCreateEvent(tag)) return;
244 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
245 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
246 rec->start = code->address();
247 rec->entry = profiles_->NewCodeEntry(
248 tag, profiles_->GetFunctionName(shared->DebugName()),
249 CodeEntry::kEmptyNamePrefix, profiles_->GetName(script_name),
250 CpuProfileNode::kNoLineNumberInfo, CpuProfileNode::kNoColumnNumberInfo,
251 NULL, code->instruction_start());
253 rec->entry->set_no_frame_ranges(info->ReleaseNoFrameRanges());
254 rec->entry->set_inlined_function_infos(info->inlined_function_infos());
256 rec->entry->FillFunctionInfo(shared);
257 rec->size = code->ExecutableSize();
258 processor_->Enqueue(evt_rec);
262 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, Code* code,
263 SharedFunctionInfo* shared,
264 CompilationInfo* info, Name* script_name,
265 int line, int column) {
266 if (FilterOutCodeCreateEvent(tag)) return;
267 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
268 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
269 rec->start = code->address();
270 Script* script = Script::cast(shared->script());
271 JITLineInfoTable* line_table = NULL;
273 line_table = new JITLineInfoTable();
274 for (RelocIterator it(code); !it.done(); it.next()) {
275 RelocInfo::Mode mode = it.rinfo()->rmode();
276 if (RelocInfo::IsPosition(mode)) {
277 int position = static_cast<int>(it.rinfo()->data());
279 int pc_offset = static_cast<int>(it.rinfo()->pc() - code->address());
280 int line_number = script->GetLineNumber(position);
281 line_table->SetPosition(pc_offset, line_number + 1);
286 rec->entry = profiles_->NewCodeEntry(
287 tag, profiles_->GetFunctionName(shared->DebugName()),
288 CodeEntry::kEmptyNamePrefix, profiles_->GetName(script_name), line,
289 column, line_table, code->instruction_start());
291 rec->entry->set_no_frame_ranges(info->ReleaseNoFrameRanges());
292 rec->entry->set_inlined_function_infos(info->inlined_function_infos());
294 rec->entry->FillFunctionInfo(shared);
295 rec->size = code->ExecutableSize();
296 processor_->Enqueue(evt_rec);
300 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag,
303 if (FilterOutCodeCreateEvent(tag)) return;
304 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
305 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
306 rec->start = code->address();
307 rec->entry = profiles_->NewCodeEntry(
308 tag, profiles_->GetName(args_count), "args_count: ",
309 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo,
310 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start());
311 rec->size = code->ExecutableSize();
312 processor_->Enqueue(evt_rec);
316 void CpuProfiler::CodeMoveEvent(Address from, Address to) {
317 CodeEventsContainer evt_rec(CodeEventRecord::CODE_MOVE);
318 CodeMoveEventRecord* rec = &evt_rec.CodeMoveEventRecord_;
321 processor_->Enqueue(evt_rec);
325 void CpuProfiler::CodeDisableOptEvent(Code* code, SharedFunctionInfo* shared) {
326 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DISABLE_OPT);
327 CodeDisableOptEventRecord* rec = &evt_rec.CodeDisableOptEventRecord_;
328 rec->start = code->address();
329 rec->bailout_reason = GetBailoutReason(shared->disable_optimization_reason());
330 processor_->Enqueue(evt_rec);
334 void CpuProfiler::CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta) {
335 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DEOPT);
336 CodeDeoptEventRecord* rec = &evt_rec.CodeDeoptEventRecord_;
337 Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(code, pc);
338 rec->start = code->address();
339 rec->deopt_reason = Deoptimizer::GetDeoptReason(info.deopt_reason);
340 rec->position = info.position;
341 rec->pc_offset = pc - code->instruction_start();
342 processor_->Enqueue(evt_rec);
343 processor_->AddDeoptStack(isolate_, pc, fp_to_sp_delta);
347 void CpuProfiler::CodeDeleteEvent(Address from) {
351 void CpuProfiler::GetterCallbackEvent(Name* name, Address entry_point) {
352 if (FilterOutCodeCreateEvent(Logger::CALLBACK_TAG)) return;
353 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
354 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
355 rec->start = entry_point;
356 rec->entry = profiles_->NewCodeEntry(
357 Logger::CALLBACK_TAG,
358 profiles_->GetName(name),
361 processor_->Enqueue(evt_rec);
365 void CpuProfiler::RegExpCodeCreateEvent(Code* code, String* source) {
366 if (FilterOutCodeCreateEvent(Logger::REG_EXP_TAG)) return;
367 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
368 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
369 rec->start = code->address();
370 rec->entry = profiles_->NewCodeEntry(
371 Logger::REG_EXP_TAG, profiles_->GetName(source), "RegExp: ",
372 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo,
373 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start());
374 rec->size = code->ExecutableSize();
375 processor_->Enqueue(evt_rec);
379 void CpuProfiler::SetterCallbackEvent(Name* name, Address entry_point) {
380 if (FilterOutCodeCreateEvent(Logger::CALLBACK_TAG)) return;
381 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
382 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
383 rec->start = entry_point;
384 rec->entry = profiles_->NewCodeEntry(
385 Logger::CALLBACK_TAG,
386 profiles_->GetName(name),
389 processor_->Enqueue(evt_rec);
393 CpuProfiler::CpuProfiler(Isolate* isolate)
395 sampling_interval_(base::TimeDelta::FromMicroseconds(
396 FLAG_cpu_profiler_sampling_interval)),
397 profiles_(new CpuProfilesCollection(isolate->heap())),
400 is_profiling_(false) {
404 CpuProfiler::CpuProfiler(Isolate* isolate,
405 CpuProfilesCollection* test_profiles,
406 ProfileGenerator* test_generator,
407 ProfilerEventsProcessor* test_processor)
409 sampling_interval_(base::TimeDelta::FromMicroseconds(
410 FLAG_cpu_profiler_sampling_interval)),
411 profiles_(test_profiles),
412 generator_(test_generator),
413 processor_(test_processor),
414 is_profiling_(false) {
418 CpuProfiler::~CpuProfiler() {
419 DCHECK(!is_profiling_);
424 void CpuProfiler::set_sampling_interval(base::TimeDelta value) {
425 DCHECK(!is_profiling_);
426 sampling_interval_ = value;
430 void CpuProfiler::ResetProfiles() {
432 profiles_ = new CpuProfilesCollection(isolate()->heap());
436 void CpuProfiler::StartProfiling(const char* title, bool record_samples) {
437 if (profiles_->StartProfiling(title, record_samples)) {
438 StartProcessorIfNotStarted();
443 void CpuProfiler::StartProfiling(String* title, bool record_samples) {
444 StartProfiling(profiles_->GetName(title), record_samples);
448 void CpuProfiler::StartProcessorIfNotStarted() {
449 if (processor_ != NULL) {
450 processor_->AddCurrentStack(isolate_);
453 Logger* logger = isolate_->logger();
454 // Disable logging when using the new implementation.
455 saved_is_logging_ = logger->is_logging_;
456 logger->is_logging_ = false;
457 generator_ = new ProfileGenerator(profiles_);
458 Sampler* sampler = logger->sampler();
459 processor_ = new ProfilerEventsProcessor(
460 generator_, sampler, sampling_interval_);
461 is_profiling_ = true;
462 // Enumerate stuff we already have in the heap.
463 DCHECK(isolate_->heap()->HasBeenSetUp());
464 if (!FLAG_prof_browser_mode) {
465 logger->LogCodeObjects();
467 logger->LogCompiledFunctions();
468 logger->LogAccessorCallbacks();
470 // Enable stack sampling.
471 sampler->SetHasProcessingThread(true);
472 sampler->IncreaseProfilingDepth();
473 processor_->AddCurrentStack(isolate_);
474 processor_->StartSynchronously();
478 CpuProfile* CpuProfiler::StopProfiling(const char* title) {
479 if (!is_profiling_) return NULL;
480 StopProcessorIfLastProfile(title);
481 CpuProfile* result = profiles_->StopProfiling(title);
482 if (result != NULL) {
489 CpuProfile* CpuProfiler::StopProfiling(String* title) {
490 if (!is_profiling_) return NULL;
491 const char* profile_title = profiles_->GetName(title);
492 StopProcessorIfLastProfile(profile_title);
493 return profiles_->StopProfiling(profile_title);
497 void CpuProfiler::StopProcessorIfLastProfile(const char* title) {
498 if (profiles_->IsLastProfile(title)) StopProcessor();
502 void CpuProfiler::StopProcessor() {
503 Logger* logger = isolate_->logger();
504 Sampler* sampler = reinterpret_cast<Sampler*>(logger->ticker_);
505 is_profiling_ = false;
506 processor_->StopSynchronously();
511 sampler->SetHasProcessingThread(false);
512 sampler->DecreaseProfilingDepth();
513 logger->is_logging_ = saved_is_logging_;
517 void CpuProfiler::LogBuiltins() {
518 Builtins* builtins = isolate_->builtins();
519 DCHECK(builtins->is_initialized());
520 for (int i = 0; i < Builtins::builtin_count; i++) {
521 CodeEventsContainer evt_rec(CodeEventRecord::REPORT_BUILTIN);
522 ReportBuiltinEventRecord* rec = &evt_rec.ReportBuiltinEventRecord_;
523 Builtins::Name id = static_cast<Builtins::Name>(i);
524 rec->start = builtins->builtin(id)->address();
525 rec->builtin_id = id;
526 processor_->Enqueue(evt_rec);
531 } } // namespace v8::internal