Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #include "src/v8.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/cpu-profiler-inl.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 8 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 9 | #include "src/compiler.h" |
| 10 | #include "src/frames-inl.h" |
| 11 | #include "src/hashmap.h" |
| 12 | #include "src/log-inl.h" |
| 13 | #include "src/vm-state-inl.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 14 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15 | #include "include/v8-profiler.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 16 | |
| 17 | namespace v8 { |
| 18 | namespace internal { |
| 19 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 20 | static const int kProfilerStackSize = 64 * KB; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 21 | |
| 22 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 23 | ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator, |
| 24 | Sampler* sampler, |
| 25 | base::TimeDelta period) |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 26 | : Thread(Thread::Options("v8:ProfEvntProc", kProfilerStackSize)), |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 27 | generator_(generator), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 28 | sampler_(sampler), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 29 | running_(1), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 30 | period_(period), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 31 | last_code_event_id_(0), |
| 32 | last_processed_code_event_id_(0) {} |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 33 | |
| 34 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 35 | void ProfilerEventsProcessor::Enqueue(const CodeEventsContainer& event) { |
| 36 | event.generic.order = ++last_code_event_id_; |
| 37 | events_buffer_.Enqueue(event); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 41 | void ProfilerEventsProcessor::AddCurrentStack(Isolate* isolate) { |
| 42 | TickSampleEventRecord record(last_code_event_id_); |
| 43 | RegisterState regs; |
| 44 | StackFrameIterator it(isolate); |
| 45 | if (!it.done()) { |
| 46 | StackFrame* frame = it.frame(); |
| 47 | regs.sp = frame->sp(); |
| 48 | regs.fp = frame->fp(); |
| 49 | regs.pc = frame->pc(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 50 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 51 | record.sample.Init(isolate, regs, TickSample::kSkipCEntryFrame); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 52 | ticks_from_vm_buffer_.Enqueue(record); |
| 53 | } |
| 54 | |
| 55 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 56 | void ProfilerEventsProcessor::StopSynchronously() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 57 | if (!base::NoBarrier_AtomicExchange(&running_, 0)) return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 58 | Join(); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | bool ProfilerEventsProcessor::ProcessCodeEvent() { |
| 63 | CodeEventsContainer record; |
| 64 | if (events_buffer_.Dequeue(&record)) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 65 | switch (record.generic.type) { |
| 66 | #define PROFILER_TYPE_CASE(type, clss) \ |
| 67 | case CodeEventRecord::type: \ |
| 68 | record.clss##_.UpdateCodeMap(generator_->code_map()); \ |
| 69 | break; |
| 70 | |
| 71 | CODE_EVENTS_TYPE_LIST(PROFILER_TYPE_CASE) |
| 72 | |
| 73 | #undef PROFILER_TYPE_CASE |
| 74 | default: return true; // Skip record. |
| 75 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 76 | last_processed_code_event_id_ = record.generic.order; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 77 | return true; |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 82 | ProfilerEventsProcessor::SampleProcessingResult |
| 83 | ProfilerEventsProcessor::ProcessOneSample() { |
| 84 | if (!ticks_from_vm_buffer_.IsEmpty() |
| 85 | && ticks_from_vm_buffer_.Peek()->order == |
| 86 | last_processed_code_event_id_) { |
| 87 | TickSampleEventRecord record; |
| 88 | ticks_from_vm_buffer_.Dequeue(&record); |
| 89 | generator_->RecordTickSample(record.sample); |
| 90 | return OneSampleProcessed; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 91 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 92 | |
| 93 | const TickSampleEventRecord* record = ticks_buffer_.Peek(); |
| 94 | if (record == NULL) { |
| 95 | if (ticks_from_vm_buffer_.IsEmpty()) return NoSamplesInQueue; |
| 96 | return FoundSampleForNextCodeEvent; |
| 97 | } |
| 98 | if (record->order != last_processed_code_event_id_) { |
| 99 | return FoundSampleForNextCodeEvent; |
| 100 | } |
| 101 | generator_->RecordTickSample(record->sample); |
| 102 | ticks_buffer_.Remove(); |
| 103 | return OneSampleProcessed; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
| 107 | void ProfilerEventsProcessor::Run() { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 108 | while (!!base::NoBarrier_Load(&running_)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 109 | base::ElapsedTimer timer; |
| 110 | timer.Start(); |
| 111 | // Keep processing existing events until we need to do next sample. |
| 112 | do { |
| 113 | if (FoundSampleForNextCodeEvent == ProcessOneSample()) { |
| 114 | // All ticks of the current last_processed_code_event_id_ are |
| 115 | // processed, proceed to the next code event. |
| 116 | ProcessCodeEvent(); |
| 117 | } |
| 118 | } while (!timer.HasExpired(period_)); |
| 119 | |
| 120 | // Schedule next sample. sampler_ is NULL in tests. |
| 121 | if (sampler_) sampler_->DoSample(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // Process remaining tick events. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 125 | do { |
| 126 | SampleProcessingResult result; |
| 127 | do { |
| 128 | result = ProcessOneSample(); |
| 129 | } while (result == OneSampleProcessed); |
| 130 | } while (ProcessCodeEvent()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 134 | void* ProfilerEventsProcessor::operator new(size_t size) { |
| 135 | return AlignedAlloc(size, V8_ALIGNOF(ProfilerEventsProcessor)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 139 | void ProfilerEventsProcessor::operator delete(void* ptr) { |
| 140 | AlignedFree(ptr); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
| 144 | int CpuProfiler::GetProfilesCount() { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 145 | // The count of profiles doesn't depend on a security token. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 146 | return profiles_->profiles()->length(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 150 | CpuProfile* CpuProfiler::GetProfile(int index) { |
| 151 | return profiles_->profiles()->at(index); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 155 | void CpuProfiler::DeleteAllProfiles() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 156 | if (is_profiling_) StopProcessor(); |
| 157 | ResetProfiles(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | |
| 161 | void CpuProfiler::DeleteProfile(CpuProfile* profile) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 162 | profiles_->RemoveProfile(profile); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 163 | delete profile; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 164 | if (profiles_->profiles()->is_empty() && !is_profiling_) { |
| 165 | // If this was the last profile, clean up all accessory data as well. |
| 166 | ResetProfiles(); |
| 167 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 171 | static bool FilterOutCodeCreateEvent(Logger::LogEventsAndTags tag) { |
| 172 | return FLAG_prof_browser_mode |
| 173 | && (tag != Logger::CALLBACK_TAG |
| 174 | && tag != Logger::FUNCTION_TAG |
| 175 | && tag != Logger::LAZY_COMPILE_TAG |
| 176 | && tag != Logger::REG_EXP_TAG |
| 177 | && tag != Logger::SCRIPT_TAG); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 181 | void CpuProfiler::CallbackEvent(Name* name, Address entry_point) { |
| 182 | if (FilterOutCodeCreateEvent(Logger::CALLBACK_TAG)) return; |
| 183 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 184 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 185 | rec->start = entry_point; |
| 186 | rec->entry = profiles_->NewCodeEntry( |
| 187 | Logger::CALLBACK_TAG, |
| 188 | profiles_->GetName(name)); |
| 189 | rec->size = 1; |
| 190 | rec->shared = NULL; |
| 191 | processor_->Enqueue(evt_rec); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | |
| 195 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 196 | Code* code, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 197 | const char* name) { |
| 198 | if (FilterOutCodeCreateEvent(tag)) return; |
| 199 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 200 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 201 | rec->start = code->address(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 202 | rec->entry = profiles_->NewCodeEntry( |
| 203 | tag, profiles_->GetFunctionName(name), CodeEntry::kEmptyNamePrefix, |
| 204 | CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, |
| 205 | CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 206 | rec->size = code->ExecutableSize(); |
| 207 | rec->shared = NULL; |
| 208 | processor_->Enqueue(evt_rec); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | |
| 212 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 213 | Code* code, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 214 | Name* name) { |
| 215 | if (FilterOutCodeCreateEvent(tag)) return; |
| 216 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 217 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 218 | rec->start = code->address(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 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 | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 223 | rec->size = code->ExecutableSize(); |
| 224 | rec->shared = NULL; |
| 225 | processor_->Enqueue(evt_rec); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, Code* code, |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 230 | SharedFunctionInfo* shared, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 231 | CompilationInfo* info, Name* script_name) { |
| 232 | if (FilterOutCodeCreateEvent(tag)) return; |
| 233 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 234 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 235 | rec->start = code->address(); |
| 236 | rec->entry = profiles_->NewCodeEntry( |
| 237 | tag, profiles_->GetFunctionName(shared->DebugName()), |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 238 | CodeEntry::kEmptyNamePrefix, profiles_->GetName(script_name), |
| 239 | CpuProfileNode::kNoLineNumberInfo, CpuProfileNode::kNoColumnNumberInfo, |
| 240 | NULL, code->instruction_start()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 241 | if (info) { |
| 242 | rec->entry->set_no_frame_ranges(info->ReleaseNoFrameRanges()); |
| 243 | } |
| 244 | if (shared->script()->IsScript()) { |
| 245 | DCHECK(Script::cast(shared->script())); |
| 246 | Script* script = Script::cast(shared->script()); |
| 247 | rec->entry->set_script_id(script->id()->value()); |
| 248 | rec->entry->set_bailout_reason( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 249 | GetBailoutReason(shared->disable_optimization_reason())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 250 | } |
| 251 | rec->size = code->ExecutableSize(); |
| 252 | rec->shared = shared->address(); |
| 253 | processor_->Enqueue(evt_rec); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, Code* code, |
| 258 | SharedFunctionInfo* shared, |
| 259 | CompilationInfo* info, Name* script_name, |
| 260 | int line, int column) { |
| 261 | if (FilterOutCodeCreateEvent(tag)) return; |
| 262 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 263 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 264 | rec->start = code->address(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 265 | Script* script = Script::cast(shared->script()); |
| 266 | JITLineInfoTable* line_table = NULL; |
| 267 | if (script) { |
| 268 | line_table = new JITLineInfoTable(); |
| 269 | for (RelocIterator it(code); !it.done(); it.next()) { |
| 270 | RelocInfo::Mode mode = it.rinfo()->rmode(); |
| 271 | if (RelocInfo::IsPosition(mode)) { |
| 272 | int position = static_cast<int>(it.rinfo()->data()); |
| 273 | if (position >= 0) { |
| 274 | int pc_offset = static_cast<int>(it.rinfo()->pc() - code->address()); |
| 275 | int line_number = script->GetLineNumber(position); |
| 276 | line_table->SetPosition(pc_offset, line_number + 1); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 281 | rec->entry = profiles_->NewCodeEntry( |
| 282 | tag, profiles_->GetFunctionName(shared->DebugName()), |
| 283 | CodeEntry::kEmptyNamePrefix, profiles_->GetName(script_name), line, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 284 | column, line_table, code->instruction_start()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 285 | if (info) { |
| 286 | rec->entry->set_no_frame_ranges(info->ReleaseNoFrameRanges()); |
| 287 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 288 | rec->entry->set_script_id(script->id()->value()); |
| 289 | rec->size = code->ExecutableSize(); |
| 290 | rec->shared = shared->address(); |
| 291 | rec->entry->set_bailout_reason( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 292 | GetBailoutReason(shared->disable_optimization_reason())); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 293 | processor_->Enqueue(evt_rec); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | |
| 297 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 298 | Code* code, |
| 299 | int args_count) { |
| 300 | if (FilterOutCodeCreateEvent(tag)) return; |
| 301 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 302 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 303 | rec->start = code->address(); |
| 304 | rec->entry = profiles_->NewCodeEntry( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 305 | tag, profiles_->GetName(args_count), "args_count: ", |
| 306 | CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, |
| 307 | CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 308 | rec->size = code->ExecutableSize(); |
| 309 | rec->shared = NULL; |
| 310 | processor_->Enqueue(evt_rec); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | |
| 314 | void CpuProfiler::CodeMoveEvent(Address from, Address to) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 315 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_MOVE); |
| 316 | CodeMoveEventRecord* rec = &evt_rec.CodeMoveEventRecord_; |
| 317 | rec->from = from; |
| 318 | rec->to = to; |
| 319 | processor_->Enqueue(evt_rec); |
| 320 | } |
| 321 | |
| 322 | |
| 323 | void CpuProfiler::CodeDisableOptEvent(Code* code, SharedFunctionInfo* shared) { |
| 324 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_DISABLE_OPT); |
| 325 | CodeDisableOptEventRecord* rec = &evt_rec.CodeDisableOptEventRecord_; |
| 326 | rec->start = code->address(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 327 | rec->bailout_reason = GetBailoutReason(shared->disable_optimization_reason()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 328 | processor_->Enqueue(evt_rec); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | |
| 332 | void CpuProfiler::CodeDeleteEvent(Address from) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 336 | void CpuProfiler::SharedFunctionInfoMoveEvent(Address from, Address to) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 337 | CodeEventsContainer evt_rec(CodeEventRecord::SHARED_FUNC_MOVE); |
| 338 | SharedFunctionInfoMoveEventRecord* rec = |
| 339 | &evt_rec.SharedFunctionInfoMoveEventRecord_; |
| 340 | rec->from = from; |
| 341 | rec->to = to; |
| 342 | processor_->Enqueue(evt_rec); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 346 | void CpuProfiler::GetterCallbackEvent(Name* name, Address entry_point) { |
| 347 | if (FilterOutCodeCreateEvent(Logger::CALLBACK_TAG)) return; |
| 348 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 349 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 350 | rec->start = entry_point; |
| 351 | rec->entry = profiles_->NewCodeEntry( |
| 352 | Logger::CALLBACK_TAG, |
| 353 | profiles_->GetName(name), |
| 354 | "get "); |
| 355 | rec->size = 1; |
| 356 | rec->shared = NULL; |
| 357 | processor_->Enqueue(evt_rec); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | |
| 361 | void CpuProfiler::RegExpCodeCreateEvent(Code* code, String* source) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 362 | if (FilterOutCodeCreateEvent(Logger::REG_EXP_TAG)) return; |
| 363 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 364 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 365 | rec->start = code->address(); |
| 366 | rec->entry = profiles_->NewCodeEntry( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 367 | Logger::REG_EXP_TAG, profiles_->GetName(source), "RegExp: ", |
| 368 | CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, |
| 369 | CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 370 | rec->size = code->ExecutableSize(); |
| 371 | processor_->Enqueue(evt_rec); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 375 | void CpuProfiler::SetterCallbackEvent(Name* name, Address entry_point) { |
| 376 | if (FilterOutCodeCreateEvent(Logger::CALLBACK_TAG)) return; |
| 377 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 378 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 379 | rec->start = entry_point; |
| 380 | rec->entry = profiles_->NewCodeEntry( |
| 381 | Logger::CALLBACK_TAG, |
| 382 | profiles_->GetName(name), |
| 383 | "set "); |
| 384 | rec->size = 1; |
| 385 | rec->shared = NULL; |
| 386 | processor_->Enqueue(evt_rec); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 390 | CpuProfiler::CpuProfiler(Isolate* isolate) |
| 391 | : isolate_(isolate), |
| 392 | sampling_interval_(base::TimeDelta::FromMicroseconds( |
| 393 | FLAG_cpu_profiler_sampling_interval)), |
| 394 | profiles_(new CpuProfilesCollection(isolate->heap())), |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 395 | generator_(NULL), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 396 | processor_(NULL), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 397 | is_profiling_(false) { |
| 398 | } |
| 399 | |
| 400 | |
| 401 | CpuProfiler::CpuProfiler(Isolate* isolate, |
| 402 | CpuProfilesCollection* test_profiles, |
| 403 | ProfileGenerator* test_generator, |
| 404 | ProfilerEventsProcessor* test_processor) |
| 405 | : isolate_(isolate), |
| 406 | sampling_interval_(base::TimeDelta::FromMicroseconds( |
| 407 | FLAG_cpu_profiler_sampling_interval)), |
| 408 | profiles_(test_profiles), |
| 409 | generator_(test_generator), |
| 410 | processor_(test_processor), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 411 | is_profiling_(false) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | |
| 415 | CpuProfiler::~CpuProfiler() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 416 | DCHECK(!is_profiling_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 417 | delete profiles_; |
| 418 | } |
| 419 | |
| 420 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 421 | void CpuProfiler::set_sampling_interval(base::TimeDelta value) { |
| 422 | DCHECK(!is_profiling_); |
| 423 | sampling_interval_ = value; |
| 424 | } |
| 425 | |
| 426 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 427 | void CpuProfiler::ResetProfiles() { |
| 428 | delete profiles_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 429 | profiles_ = new CpuProfilesCollection(isolate()->heap()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 430 | } |
| 431 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 432 | |
| 433 | void CpuProfiler::StartProfiling(const char* title, bool record_samples) { |
| 434 | if (profiles_->StartProfiling(title, record_samples)) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 435 | StartProcessorIfNotStarted(); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 440 | void CpuProfiler::StartProfiling(String* title, bool record_samples) { |
| 441 | StartProfiling(profiles_->GetName(title), record_samples); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | |
| 445 | void CpuProfiler::StartProcessorIfNotStarted() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 446 | if (processor_ != NULL) { |
| 447 | processor_->AddCurrentStack(isolate_); |
| 448 | return; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 449 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 450 | Logger* logger = isolate_->logger(); |
| 451 | // Disable logging when using the new implementation. |
| 452 | saved_is_logging_ = logger->is_logging_; |
| 453 | logger->is_logging_ = false; |
| 454 | generator_ = new ProfileGenerator(profiles_); |
| 455 | Sampler* sampler = logger->sampler(); |
| 456 | processor_ = new ProfilerEventsProcessor( |
| 457 | generator_, sampler, sampling_interval_); |
| 458 | is_profiling_ = true; |
| 459 | // Enumerate stuff we already have in the heap. |
| 460 | DCHECK(isolate_->heap()->HasBeenSetUp()); |
| 461 | if (!FLAG_prof_browser_mode) { |
| 462 | logger->LogCodeObjects(); |
| 463 | } |
| 464 | logger->LogCompiledFunctions(); |
| 465 | logger->LogAccessorCallbacks(); |
| 466 | LogBuiltins(); |
| 467 | // Enable stack sampling. |
| 468 | sampler->SetHasProcessingThread(true); |
| 469 | sampler->IncreaseProfilingDepth(); |
| 470 | processor_->AddCurrentStack(isolate_); |
| 471 | processor_->StartSynchronously(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 475 | CpuProfile* CpuProfiler::StopProfiling(const char* title) { |
| 476 | if (!is_profiling_) return NULL; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 477 | StopProcessorIfLastProfile(title); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 478 | CpuProfile* result = profiles_->StopProfiling(title); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 479 | if (result != NULL) { |
| 480 | result->Print(); |
| 481 | } |
| 482 | return result; |
| 483 | } |
| 484 | |
| 485 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 486 | CpuProfile* CpuProfiler::StopProfiling(String* title) { |
| 487 | if (!is_profiling_) return NULL; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 488 | const char* profile_title = profiles_->GetName(title); |
| 489 | StopProcessorIfLastProfile(profile_title); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 490 | return profiles_->StopProfiling(profile_title); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 494 | void CpuProfiler::StopProcessorIfLastProfile(const char* title) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 495 | if (profiles_->IsLastProfile(title)) StopProcessor(); |
| 496 | } |
| 497 | |
| 498 | |
| 499 | void CpuProfiler::StopProcessor() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 500 | Logger* logger = isolate_->logger(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 501 | Sampler* sampler = reinterpret_cast<Sampler*>(logger->ticker_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 502 | is_profiling_ = false; |
| 503 | processor_->StopSynchronously(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 504 | delete processor_; |
| 505 | delete generator_; |
| 506 | processor_ = NULL; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 507 | generator_ = NULL; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 508 | sampler->SetHasProcessingThread(false); |
| 509 | sampler->DecreaseProfilingDepth(); |
| 510 | logger->is_logging_ = saved_is_logging_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 511 | } |
| 512 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 513 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 514 | void CpuProfiler::LogBuiltins() { |
| 515 | Builtins* builtins = isolate_->builtins(); |
| 516 | DCHECK(builtins->is_initialized()); |
| 517 | for (int i = 0; i < Builtins::builtin_count; i++) { |
| 518 | CodeEventsContainer evt_rec(CodeEventRecord::REPORT_BUILTIN); |
| 519 | ReportBuiltinEventRecord* rec = &evt_rec.ReportBuiltinEventRecord_; |
| 520 | Builtins::Name id = static_cast<Builtins::Name>(i); |
| 521 | rec->start = builtins->builtin(id)->address(); |
| 522 | rec->builtin_id = id; |
| 523 | processor_->Enqueue(evt_rec); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 524 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 528 | } } // namespace v8::internal |