jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "cpu-profiler-inl.h" |
| 31 | |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 32 | #include "compiler.h" |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 33 | #include "frames-inl.h" |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 34 | #include "hashmap.h" |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 35 | #include "log-inl.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 36 | #include "vm-state-inl.h" |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 37 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 38 | #include "../include/v8-profiler.h" |
| 39 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 40 | namespace v8 { |
| 41 | namespace internal { |
| 42 | |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 43 | static const int kProfilerStackSize = 64 * KB; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 44 | |
| 45 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 46 | ProfilerEventsProcessor::ProfilerEventsProcessor( |
| 47 | ProfileGenerator* generator, |
| 48 | Sampler* sampler, |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 49 | TimeDelta period) |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 50 | : Thread(Thread::Options("v8:ProfEvntProc", kProfilerStackSize)), |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 51 | generator_(generator), |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 52 | sampler_(sampler), |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 53 | running_(true), |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 54 | period_(period), |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 55 | last_code_event_id_(0), last_processed_code_event_id_(0) { |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 56 | } |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 57 | |
| 58 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 59 | void ProfilerEventsProcessor::Enqueue(const CodeEventsContainer& event) { |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 60 | event.generic.order = ++last_code_event_id_; |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 61 | events_buffer_.Enqueue(event); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 65 | void ProfilerEventsProcessor::AddCurrentStack(Isolate* isolate) { |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 66 | TickSampleEventRecord record(last_code_event_id_); |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 67 | RegisterState regs; |
| 68 | StackFrameIterator it(isolate); |
| 69 | if (!it.done()) { |
| 70 | StackFrame* frame = it.frame(); |
| 71 | regs.sp = frame->sp(); |
| 72 | regs.fp = frame->fp(); |
| 73 | regs.pc = frame->pc(); |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 74 | } |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 75 | record.sample.Init(isolate, regs); |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 76 | ticks_from_vm_buffer_.Enqueue(record); |
| 77 | } |
| 78 | |
| 79 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 80 | void ProfilerEventsProcessor::StopSynchronously() { |
| 81 | if (!running_) return; |
| 82 | running_ = false; |
| 83 | Join(); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | bool ProfilerEventsProcessor::ProcessCodeEvent() { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 88 | CodeEventsContainer record; |
| 89 | if (events_buffer_.Dequeue(&record)) { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 90 | switch (record.generic.type) { |
| 91 | #define PROFILER_TYPE_CASE(type, clss) \ |
| 92 | case CodeEventRecord::type: \ |
| 93 | record.clss##_.UpdateCodeMap(generator_->code_map()); \ |
| 94 | break; |
| 95 | |
| 96 | CODE_EVENTS_TYPE_LIST(PROFILER_TYPE_CASE) |
| 97 | |
| 98 | #undef PROFILER_TYPE_CASE |
| 99 | default: return true; // Skip record. |
| 100 | } |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 101 | last_processed_code_event_id_ = record.generic.order; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 102 | return true; |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 107 | ProfilerEventsProcessor::SampleProcessingResult |
| 108 | ProfilerEventsProcessor::ProcessOneSample() { |
| 109 | if (!ticks_from_vm_buffer_.IsEmpty() |
| 110 | && ticks_from_vm_buffer_.Peek()->order == |
| 111 | last_processed_code_event_id_) { |
| 112 | TickSampleEventRecord record; |
| 113 | ticks_from_vm_buffer_.Dequeue(&record); |
| 114 | generator_->RecordTickSample(record.sample); |
| 115 | return OneSampleProcessed; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 116 | } |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 117 | |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 118 | const TickSampleEventRecord* record = ticks_buffer_.Peek(); |
| 119 | if (record == NULL) { |
| 120 | if (ticks_from_vm_buffer_.IsEmpty()) return NoSamplesInQueue; |
| 121 | return FoundSampleForNextCodeEvent; |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 122 | } |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 123 | if (record->order != last_processed_code_event_id_) { |
| 124 | return FoundSampleForNextCodeEvent; |
| 125 | } |
| 126 | generator_->RecordTickSample(record->sample); |
| 127 | ticks_buffer_.Remove(); |
| 128 | return OneSampleProcessed; |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 132 | void ProfilerEventsProcessor::Run() { |
| 133 | while (running_) { |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 134 | ElapsedTimer timer; |
| 135 | timer.Start(); |
| 136 | // Keep processing existing events until we need to do next sample. |
| 137 | do { |
| 138 | if (FoundSampleForNextCodeEvent == ProcessOneSample()) { |
| 139 | // All ticks of the current last_processed_code_event_id_ are |
| 140 | // processed, proceed to the next code event. |
| 141 | ProcessCodeEvent(); |
| 142 | } |
| 143 | } while (!timer.HasExpired(period_)); |
| 144 | |
| 145 | // Schedule next sample. sampler_ is NULL in tests. |
| 146 | if (sampler_) sampler_->DoSample(); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 147 | } |
| 148 | |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 149 | // Process remaining tick events. |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 150 | do { |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 151 | SampleProcessingResult result; |
| 152 | do { |
| 153 | result = ProcessOneSample(); |
| 154 | } while (result == OneSampleProcessed); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 155 | } while (ProcessCodeEvent()); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 159 | int CpuProfiler::GetProfilesCount() { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 160 | // The count of profiles doesn't depend on a security token. |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 161 | return profiles_->profiles()->length(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 165 | CpuProfile* CpuProfiler::GetProfile(int index) { |
| 166 | return profiles_->profiles()->at(index); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 170 | void CpuProfiler::DeleteAllProfiles() { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 171 | if (is_profiling_) StopProcessor(); |
| 172 | ResetProfiles(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | |
| 176 | void CpuProfiler::DeleteProfile(CpuProfile* profile) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 177 | profiles_->RemoveProfile(profile); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 178 | delete profile; |
| 179 | } |
| 180 | |
| 181 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 182 | static bool FilterOutCodeCreateEvent(Logger::LogEventsAndTags tag) { |
| 183 | return FLAG_prof_browser_mode |
| 184 | && (tag != Logger::CALLBACK_TAG |
| 185 | && tag != Logger::FUNCTION_TAG |
| 186 | && tag != Logger::LAZY_COMPILE_TAG |
| 187 | && tag != Logger::REG_EXP_TAG |
| 188 | && tag != Logger::SCRIPT_TAG); |
| 189 | } |
| 190 | |
| 191 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 192 | void CpuProfiler::CallbackEvent(Name* name, Address entry_point) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 193 | if (FilterOutCodeCreateEvent(Logger::CALLBACK_TAG)) return; |
| 194 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 195 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 196 | rec->start = entry_point; |
| 197 | rec->entry = profiles_->NewCodeEntry( |
| 198 | Logger::CALLBACK_TAG, |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 199 | profiles_->GetName(name)); |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 200 | rec->size = 1; |
| 201 | rec->shared = NULL; |
| 202 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | |
| 206 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 207 | Code* code, |
| 208 | const char* name) { |
| 209 | if (FilterOutCodeCreateEvent(tag)) return; |
| 210 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 211 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 212 | rec->start = code->address(); |
| 213 | rec->entry = profiles_->NewCodeEntry(tag, profiles_->GetFunctionName(name)); |
| 214 | rec->size = code->ExecutableSize(); |
| 215 | rec->shared = NULL; |
| 216 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | |
| 220 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 221 | Code* code, |
| 222 | Name* name) { |
| 223 | if (FilterOutCodeCreateEvent(tag)) return; |
| 224 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 225 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 226 | rec->start = code->address(); |
| 227 | rec->entry = profiles_->NewCodeEntry(tag, profiles_->GetFunctionName(name)); |
| 228 | rec->size = code->ExecutableSize(); |
| 229 | rec->shared = NULL; |
| 230 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
| 234 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 235 | Code* code, |
| 236 | SharedFunctionInfo* shared, |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 237 | CompilationInfo* info, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 238 | Name* name) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 239 | if (FilterOutCodeCreateEvent(tag)) return; |
| 240 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 241 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 242 | rec->start = code->address(); |
| 243 | rec->entry = profiles_->NewCodeEntry(tag, profiles_->GetFunctionName(name)); |
| 244 | if (info) { |
| 245 | rec->entry->set_no_frame_ranges(info->ReleaseNoFrameRanges()); |
| 246 | } |
| 247 | if (shared->script()->IsScript()) { |
| 248 | ASSERT(Script::cast(shared->script())); |
| 249 | Script* script = Script::cast(shared->script()); |
| 250 | rec->entry->set_script_id(script->id()->value()); |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 251 | rec->entry->set_bailout_reason( |
| 252 | GetBailoutReason(shared->DisableOptimizationReason())); |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 253 | } |
| 254 | rec->size = code->ExecutableSize(); |
| 255 | rec->shared = shared->address(); |
| 256 | processor_->Enqueue(evt_rec); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | |
| 260 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 261 | Code* code, |
| 262 | SharedFunctionInfo* shared, |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 263 | CompilationInfo* info, |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 264 | Name* source, int line, int column) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 265 | if (FilterOutCodeCreateEvent(tag)) return; |
| 266 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 267 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 268 | rec->start = code->address(); |
| 269 | rec->entry = profiles_->NewCodeEntry( |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 270 | tag, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 271 | profiles_->GetFunctionName(shared->DebugName()), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 272 | CodeEntry::kEmptyNamePrefix, |
| 273 | profiles_->GetName(source), |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 274 | line, |
| 275 | column); |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 276 | if (info) { |
| 277 | rec->entry->set_no_frame_ranges(info->ReleaseNoFrameRanges()); |
| 278 | } |
| 279 | ASSERT(Script::cast(shared->script())); |
| 280 | Script* script = Script::cast(shared->script()); |
| 281 | rec->entry->set_script_id(script->id()->value()); |
| 282 | rec->size = code->ExecutableSize(); |
| 283 | rec->shared = shared->address(); |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 284 | rec->entry->set_bailout_reason( |
| 285 | GetBailoutReason(shared->DisableOptimizationReason())); |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 286 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | |
| 290 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 291 | Code* code, |
| 292 | int args_count) { |
| 293 | if (FilterOutCodeCreateEvent(tag)) return; |
| 294 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 295 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 296 | rec->start = code->address(); |
| 297 | rec->entry = profiles_->NewCodeEntry( |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 298 | tag, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 299 | profiles_->GetName(args_count), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 300 | "args_count: "); |
| 301 | rec->size = code->ExecutableSize(); |
| 302 | rec->shared = NULL; |
| 303 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | |
| 307 | void CpuProfiler::CodeMoveEvent(Address from, Address to) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 308 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_MOVE); |
| 309 | CodeMoveEventRecord* rec = &evt_rec.CodeMoveEventRecord_; |
| 310 | rec->from = from; |
| 311 | rec->to = to; |
| 312 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | |
| 316 | void CpuProfiler::CodeDeleteEvent(Address from) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 320 | void CpuProfiler::SharedFunctionInfoMoveEvent(Address from, Address to) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 321 | CodeEventsContainer evt_rec(CodeEventRecord::SHARED_FUNC_MOVE); |
| 322 | SharedFunctionInfoMoveEventRecord* rec = |
| 323 | &evt_rec.SharedFunctionInfoMoveEventRecord_; |
| 324 | rec->from = from; |
| 325 | rec->to = to; |
| 326 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 330 | void CpuProfiler::GetterCallbackEvent(Name* name, Address entry_point) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 331 | if (FilterOutCodeCreateEvent(Logger::CALLBACK_TAG)) return; |
| 332 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 333 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 334 | rec->start = entry_point; |
| 335 | rec->entry = profiles_->NewCodeEntry( |
| 336 | Logger::CALLBACK_TAG, |
| 337 | profiles_->GetName(name), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 338 | "get "); |
| 339 | rec->size = 1; |
| 340 | rec->shared = NULL; |
| 341 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | |
| 345 | void CpuProfiler::RegExpCodeCreateEvent(Code* code, String* source) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 346 | if (FilterOutCodeCreateEvent(Logger::REG_EXP_TAG)) return; |
| 347 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 348 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 349 | rec->start = code->address(); |
| 350 | rec->entry = profiles_->NewCodeEntry( |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 351 | Logger::REG_EXP_TAG, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 352 | profiles_->GetName(source), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 353 | "RegExp: "); |
| 354 | rec->size = code->ExecutableSize(); |
| 355 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 359 | void CpuProfiler::SetterCallbackEvent(Name* name, Address entry_point) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 360 | if (FilterOutCodeCreateEvent(Logger::CALLBACK_TAG)) return; |
| 361 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 362 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 363 | rec->start = entry_point; |
| 364 | rec->entry = profiles_->NewCodeEntry( |
| 365 | Logger::CALLBACK_TAG, |
| 366 | profiles_->GetName(name), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 367 | "set "); |
| 368 | rec->size = 1; |
| 369 | rec->shared = NULL; |
| 370 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 374 | CpuProfiler::CpuProfiler(Isolate* isolate) |
| 375 | : isolate_(isolate), |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 376 | sampling_interval_(TimeDelta::FromMicroseconds( |
| 377 | FLAG_cpu_profiler_sampling_interval)), |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 378 | profiles_(new CpuProfilesCollection(isolate->heap())), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 379 | next_profile_uid_(1), |
| 380 | generator_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 381 | processor_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 382 | is_profiling_(false) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 386 | CpuProfiler::CpuProfiler(Isolate* isolate, |
| 387 | CpuProfilesCollection* test_profiles, |
| 388 | ProfileGenerator* test_generator, |
| 389 | ProfilerEventsProcessor* test_processor) |
| 390 | : isolate_(isolate), |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 391 | sampling_interval_(TimeDelta::FromMicroseconds( |
| 392 | FLAG_cpu_profiler_sampling_interval)), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 393 | profiles_(test_profiles), |
| 394 | next_profile_uid_(1), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 395 | generator_(test_generator), |
| 396 | processor_(test_processor), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 397 | is_profiling_(false) { |
| 398 | } |
| 399 | |
| 400 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 401 | CpuProfiler::~CpuProfiler() { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 402 | ASSERT(!is_profiling_); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 403 | delete profiles_; |
| 404 | } |
| 405 | |
| 406 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 407 | void CpuProfiler::set_sampling_interval(TimeDelta value) { |
| 408 | ASSERT(!is_profiling_); |
| 409 | sampling_interval_ = value; |
| 410 | } |
| 411 | |
| 412 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 413 | void CpuProfiler::ResetProfiles() { |
| 414 | delete profiles_; |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 415 | profiles_ = new CpuProfilesCollection(isolate()->heap()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 416 | } |
| 417 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 418 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 419 | void CpuProfiler::StartProfiling(const char* title, bool record_samples) { |
| 420 | if (profiles_->StartProfiling(title, next_profile_uid_++, record_samples)) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 421 | StartProcessorIfNotStarted(); |
| 422 | } |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 423 | processor_->AddCurrentStack(isolate_); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 427 | void CpuProfiler::StartProfiling(String* title, bool record_samples) { |
| 428 | StartProfiling(profiles_->GetName(title), record_samples); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | |
| 432 | void CpuProfiler::StartProcessorIfNotStarted() { |
| 433 | if (processor_ == NULL) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 434 | Logger* logger = isolate_->logger(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 435 | // Disable logging when using the new implementation. |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 436 | saved_is_logging_ = logger->is_logging_; |
| 437 | logger->is_logging_ = false; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 438 | generator_ = new ProfileGenerator(profiles_); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 439 | Sampler* sampler = logger->sampler(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 440 | #if V8_CC_MSVC && (_MSC_VER >= 1800) |
| 441 | // VS2013 reports "warning C4316: 'v8::internal::ProfilerEventsProcessor' |
| 442 | // : object allocated on the heap may not be aligned 64". We need to |
| 443 | // figure out if this is a legitimate warning or a compiler bug. |
| 444 | #pragma warning(push) |
| 445 | #pragma warning(disable:4316) |
| 446 | #endif |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 447 | processor_ = new ProfilerEventsProcessor( |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 448 | generator_, sampler, sampling_interval_); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 449 | #if V8_CC_MSVC && (_MSC_VER >= 1800) |
| 450 | #pragma warning(pop) |
| 451 | #endif |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 452 | is_profiling_ = true; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 453 | // Enumerate stuff we already have in the heap. |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 454 | ASSERT(isolate_->heap()->HasBeenSetUp()); |
| 455 | if (!FLAG_prof_browser_mode) { |
| 456 | logger->LogCodeObjects(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 457 | } |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 458 | logger->LogCompiledFunctions(); |
| 459 | logger->LogAccessorCallbacks(); |
| 460 | LogBuiltins(); |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 461 | // Enable stack sampling. |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 462 | sampler->SetHasProcessingThread(true); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 463 | sampler->IncreaseProfilingDepth(); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 464 | processor_->StartSynchronously(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 469 | CpuProfile* CpuProfiler::StopProfiling(const char* title) { |
| 470 | if (!is_profiling_) return NULL; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 471 | StopProcessorIfLastProfile(title); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 472 | CpuProfile* result = profiles_->StopProfiling(title); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 473 | if (result != NULL) { |
| 474 | result->Print(); |
| 475 | } |
| 476 | return result; |
| 477 | } |
| 478 | |
| 479 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 480 | CpuProfile* CpuProfiler::StopProfiling(String* title) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 481 | if (!is_profiling_) return NULL; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 482 | const char* profile_title = profiles_->GetName(title); |
| 483 | StopProcessorIfLastProfile(profile_title); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 484 | return profiles_->StopProfiling(profile_title); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 488 | void CpuProfiler::StopProcessorIfLastProfile(const char* title) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 489 | if (profiles_->IsLastProfile(title)) StopProcessor(); |
| 490 | } |
| 491 | |
| 492 | |
| 493 | void CpuProfiler::StopProcessor() { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 494 | Logger* logger = isolate_->logger(); |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 495 | Sampler* sampler = reinterpret_cast<Sampler*>(logger->ticker_); |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 496 | is_profiling_ = false; |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 497 | processor_->StopSynchronously(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 498 | delete processor_; |
| 499 | delete generator_; |
| 500 | processor_ = NULL; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 501 | generator_ = NULL; |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 502 | sampler->SetHasProcessingThread(false); |
| 503 | sampler->DecreaseProfilingDepth(); |
| 504 | logger->is_logging_ = saved_is_logging_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 505 | } |
| 506 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 507 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 508 | void CpuProfiler::LogBuiltins() { |
| 509 | Builtins* builtins = isolate_->builtins(); |
| 510 | ASSERT(builtins->is_initialized()); |
| 511 | for (int i = 0; i < Builtins::builtin_count; i++) { |
| 512 | CodeEventsContainer evt_rec(CodeEventRecord::REPORT_BUILTIN); |
| 513 | ReportBuiltinEventRecord* rec = &evt_rec.ReportBuiltinEventRecord_; |
| 514 | Builtins::Name id = static_cast<Builtins::Name>(i); |
| 515 | rec->start = builtins->builtin(id)->address(); |
| 516 | rec->builtin_id = id; |
| 517 | processor_->Enqueue(evt_rec); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 522 | } } // namespace v8::internal |