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 | |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 43 | static const int kTickSamplesBufferChunkSize = 64 * KB; |
| 44 | static const int kTickSamplesBufferChunksCount = 16; |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 45 | static const int kProfilerStackSize = 64 * KB; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 46 | |
| 47 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 48 | ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator) |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 49 | : Thread(Thread::Options("v8:ProfEvntProc", kProfilerStackSize)), |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 50 | generator_(generator), |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 51 | running_(true), |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 52 | ticks_buffer_(sizeof(TickSampleEventRecord), |
| 53 | kTickSamplesBufferChunkSize, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 54 | kTickSamplesBufferChunksCount), |
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_); |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 67 | TickSample* sample = &record.sample; |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 68 | sample->state = isolate->current_vm_state(); |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 69 | sample->pc = reinterpret_cast<Address>(sample); // Not NULL. |
vegorov@chromium.org | 74f333b | 2011-04-06 11:17:46 +0000 | [diff] [blame] | 70 | for (StackTraceFrameIterator it(isolate); |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 71 | !it.done() && sample->frames_count < TickSample::kMaxFramesCount; |
| 72 | it.Advance()) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 73 | sample->stack[sample->frames_count++] = it.frame()->pc(); |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 74 | } |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 75 | ticks_from_vm_buffer_.Enqueue(record); |
| 76 | } |
| 77 | |
| 78 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 79 | void ProfilerEventsProcessor::StopSynchronously() { |
| 80 | if (!running_) return; |
| 81 | running_ = false; |
| 82 | Join(); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | bool ProfilerEventsProcessor::ProcessCodeEvent() { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 87 | CodeEventsContainer record; |
| 88 | if (events_buffer_.Dequeue(&record)) { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 89 | switch (record.generic.type) { |
| 90 | #define PROFILER_TYPE_CASE(type, clss) \ |
| 91 | case CodeEventRecord::type: \ |
| 92 | record.clss##_.UpdateCodeMap(generator_->code_map()); \ |
| 93 | break; |
| 94 | |
| 95 | CODE_EVENTS_TYPE_LIST(PROFILER_TYPE_CASE) |
| 96 | |
| 97 | #undef PROFILER_TYPE_CASE |
| 98 | default: return true; // Skip record. |
| 99 | } |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 100 | last_processed_code_event_id_ = record.generic.order; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 101 | return true; |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 107 | bool ProfilerEventsProcessor::ProcessTicks() { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 108 | while (true) { |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 109 | if (!ticks_from_vm_buffer_.IsEmpty() |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 110 | && ticks_from_vm_buffer_.Peek()->order == |
| 111 | last_processed_code_event_id_) { |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 112 | TickSampleEventRecord record; |
| 113 | ticks_from_vm_buffer_.Dequeue(&record); |
| 114 | generator_->RecordTickSample(record.sample); |
| 115 | } |
| 116 | |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 117 | const TickSampleEventRecord* rec = |
| 118 | TickSampleEventRecord::cast(ticks_buffer_.StartDequeue()); |
| 119 | if (rec == NULL) return !ticks_from_vm_buffer_.IsEmpty(); |
| 120 | // Make a local copy of tick sample record to ensure that it won't |
| 121 | // be modified as we are processing it. This is possible as the |
| 122 | // sampler writes w/o any sync to the queue, so if the processor |
| 123 | // will get far behind, a record may be modified right under its |
| 124 | // feet. |
| 125 | TickSampleEventRecord record = *rec; |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 126 | if (record.order != last_processed_code_event_id_) return true; |
| 127 | |
| 128 | // A paranoid check to make sure that we don't get a memory overrun |
| 129 | // in case of frames_count having a wild value. |
| 130 | if (record.sample.frames_count < 0 |
| 131 | || record.sample.frames_count > TickSample::kMaxFramesCount) |
| 132 | record.sample.frames_count = 0; |
| 133 | generator_->RecordTickSample(record.sample); |
| 134 | ticks_buffer_.FinishDequeue(); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | |
| 139 | void ProfilerEventsProcessor::Run() { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 140 | while (running_) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 141 | // Process ticks until we have any. |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 142 | if (ProcessTicks()) { |
| 143 | // All ticks of the current last_processed_code_event_id_ are processed, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 144 | // proceed to the next code event. |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 145 | ProcessCodeEvent(); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 146 | } |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 147 | YieldCPU(); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 148 | } |
| 149 | |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 150 | // Process remaining tick events. |
| 151 | ticks_buffer_.FlushResidualRecords(); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 152 | do { |
| 153 | ProcessTicks(); |
| 154 | } while (ProcessCodeEvent()); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 158 | int CpuProfiler::GetProfilesCount() { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 159 | // The count of profiles doesn't depend on a security token. |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 160 | return profiles_->profiles()->length(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 164 | CpuProfile* CpuProfiler::GetProfile(int index) { |
| 165 | return profiles_->profiles()->at(index); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 169 | TickSample* CpuProfiler::TickSampleEvent() { |
| 170 | if (is_profiling_) return processor_->TickSampleEvent(); |
| 171 | return NULL; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 175 | void CpuProfiler::DeleteAllProfiles() { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 176 | if (is_profiling_) StopProcessor(); |
| 177 | ResetProfiles(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | |
| 181 | void CpuProfiler::DeleteProfile(CpuProfile* profile) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 182 | profiles_->RemoveProfile(profile); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 183 | delete profile; |
| 184 | } |
| 185 | |
| 186 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 187 | static bool FilterOutCodeCreateEvent(Logger::LogEventsAndTags tag) { |
| 188 | return FLAG_prof_browser_mode |
| 189 | && (tag != Logger::CALLBACK_TAG |
| 190 | && tag != Logger::FUNCTION_TAG |
| 191 | && tag != Logger::LAZY_COMPILE_TAG |
| 192 | && tag != Logger::REG_EXP_TAG |
| 193 | && tag != Logger::SCRIPT_TAG); |
| 194 | } |
| 195 | |
| 196 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 197 | void CpuProfiler::CallbackEvent(Name* name, Address entry_point) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 198 | if (FilterOutCodeCreateEvent(Logger::CALLBACK_TAG)) return; |
| 199 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 200 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 201 | rec->start = entry_point; |
| 202 | rec->entry = profiles_->NewCodeEntry( |
| 203 | Logger::CALLBACK_TAG, |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 204 | profiles_->GetName(name)); |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 205 | rec->size = 1; |
| 206 | rec->shared = NULL; |
| 207 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | |
| 211 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 212 | Code* code, |
| 213 | const char* name) { |
| 214 | if (FilterOutCodeCreateEvent(tag)) return; |
| 215 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 216 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 217 | rec->start = code->address(); |
| 218 | rec->entry = profiles_->NewCodeEntry(tag, profiles_->GetFunctionName(name)); |
| 219 | rec->size = code->ExecutableSize(); |
| 220 | rec->shared = NULL; |
| 221 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | |
| 225 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 226 | Code* code, |
| 227 | Name* name) { |
| 228 | if (FilterOutCodeCreateEvent(tag)) return; |
| 229 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 230 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 231 | rec->start = code->address(); |
| 232 | rec->entry = profiles_->NewCodeEntry(tag, profiles_->GetFunctionName(name)); |
| 233 | rec->size = code->ExecutableSize(); |
| 234 | rec->shared = NULL; |
| 235 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | |
| 239 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 240 | Code* code, |
| 241 | SharedFunctionInfo* shared, |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 242 | CompilationInfo* info, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 243 | Name* name) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 244 | if (FilterOutCodeCreateEvent(tag)) return; |
| 245 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 246 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 247 | rec->start = code->address(); |
| 248 | rec->entry = profiles_->NewCodeEntry(tag, profiles_->GetFunctionName(name)); |
| 249 | if (info) { |
| 250 | rec->entry->set_no_frame_ranges(info->ReleaseNoFrameRanges()); |
| 251 | } |
| 252 | if (shared->script()->IsScript()) { |
| 253 | ASSERT(Script::cast(shared->script())); |
| 254 | Script* script = Script::cast(shared->script()); |
| 255 | rec->entry->set_script_id(script->id()->value()); |
| 256 | } |
| 257 | rec->size = code->ExecutableSize(); |
| 258 | rec->shared = shared->address(); |
| 259 | processor_->Enqueue(evt_rec); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
| 263 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 264 | Code* code, |
| 265 | SharedFunctionInfo* shared, |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 266 | CompilationInfo* info, |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 267 | String* source, int line) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 268 | if (FilterOutCodeCreateEvent(tag)) return; |
| 269 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 270 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 271 | rec->start = code->address(); |
| 272 | rec->entry = profiles_->NewCodeEntry( |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 273 | tag, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 274 | profiles_->GetFunctionName(shared->DebugName()), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 275 | CodeEntry::kEmptyNamePrefix, |
| 276 | profiles_->GetName(source), |
| 277 | line); |
| 278 | if (info) { |
| 279 | rec->entry->set_no_frame_ranges(info->ReleaseNoFrameRanges()); |
| 280 | } |
| 281 | ASSERT(Script::cast(shared->script())); |
| 282 | Script* script = Script::cast(shared->script()); |
| 283 | rec->entry->set_script_id(script->id()->value()); |
| 284 | rec->size = code->ExecutableSize(); |
| 285 | rec->shared = shared->address(); |
| 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), |
| 376 | profiles_(new CpuProfilesCollection()), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 377 | next_profile_uid_(1), |
| 378 | generator_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 379 | processor_(NULL), |
| 380 | need_to_stop_sampler_(false), |
| 381 | is_profiling_(false) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 385 | CpuProfiler::CpuProfiler(Isolate* isolate, |
| 386 | CpuProfilesCollection* test_profiles, |
| 387 | ProfileGenerator* test_generator, |
| 388 | ProfilerEventsProcessor* test_processor) |
| 389 | : isolate_(isolate), |
| 390 | profiles_(test_profiles), |
| 391 | next_profile_uid_(1), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 392 | generator_(test_generator), |
| 393 | processor_(test_processor), |
| 394 | need_to_stop_sampler_(false), |
| 395 | is_profiling_(false) { |
| 396 | } |
| 397 | |
| 398 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 399 | CpuProfiler::~CpuProfiler() { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 400 | ASSERT(!is_profiling_); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 401 | delete profiles_; |
| 402 | } |
| 403 | |
| 404 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 405 | void CpuProfiler::ResetProfiles() { |
| 406 | delete profiles_; |
| 407 | profiles_ = new CpuProfilesCollection(); |
| 408 | } |
| 409 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 410 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 411 | void CpuProfiler::StartProfiling(const char* title, bool record_samples) { |
| 412 | if (profiles_->StartProfiling(title, next_profile_uid_++, record_samples)) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 413 | StartProcessorIfNotStarted(); |
| 414 | } |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 415 | processor_->AddCurrentStack(isolate_); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 419 | void CpuProfiler::StartProfiling(String* title, bool record_samples) { |
| 420 | StartProfiling(profiles_->GetName(title), record_samples); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | |
| 424 | void CpuProfiler::StartProcessorIfNotStarted() { |
| 425 | if (processor_ == NULL) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 426 | Logger* logger = isolate_->logger(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 427 | // Disable logging when using the new implementation. |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 428 | saved_logging_nesting_ = logger->logging_nesting_; |
| 429 | logger->logging_nesting_ = 0; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 430 | generator_ = new ProfileGenerator(profiles_); |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 431 | processor_ = new ProfilerEventsProcessor(generator_); |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 432 | is_profiling_ = true; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 433 | processor_->StartSynchronously(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 434 | // Enumerate stuff we already have in the heap. |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 435 | ASSERT(isolate_->heap()->HasBeenSetUp()); |
| 436 | if (!FLAG_prof_browser_mode) { |
| 437 | logger->LogCodeObjects(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 438 | } |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 439 | logger->LogCompiledFunctions(); |
| 440 | logger->LogAccessorCallbacks(); |
| 441 | LogBuiltins(); |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 442 | // Enable stack sampling. |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 443 | Sampler* sampler = logger->sampler(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 444 | sampler->IncreaseProfilingDepth(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 445 | if (!sampler->IsActive()) { |
| 446 | sampler->Start(); |
| 447 | need_to_stop_sampler_ = true; |
| 448 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
| 452 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 453 | CpuProfile* CpuProfiler::StopProfiling(const char* title) { |
| 454 | if (!is_profiling_) return NULL; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 455 | const double actual_sampling_rate = generator_->actual_sampling_rate(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 456 | StopProcessorIfLastProfile(title); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 457 | CpuProfile* result = profiles_->StopProfiling(title, actual_sampling_rate); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 458 | if (result != NULL) { |
| 459 | result->Print(); |
| 460 | } |
| 461 | return result; |
| 462 | } |
| 463 | |
| 464 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 465 | CpuProfile* CpuProfiler::StopProfiling(String* title) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 466 | if (!is_profiling_) return NULL; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 467 | const double actual_sampling_rate = generator_->actual_sampling_rate(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 468 | const char* profile_title = profiles_->GetName(title); |
| 469 | StopProcessorIfLastProfile(profile_title); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 470 | return profiles_->StopProfiling(profile_title, actual_sampling_rate); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 474 | void CpuProfiler::StopProcessorIfLastProfile(const char* title) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 475 | if (profiles_->IsLastProfile(title)) StopProcessor(); |
| 476 | } |
| 477 | |
| 478 | |
| 479 | void CpuProfiler::StopProcessor() { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 480 | Logger* logger = isolate_->logger(); |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 481 | Sampler* sampler = reinterpret_cast<Sampler*>(logger->ticker_); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 482 | sampler->DecreaseProfilingDepth(); |
| 483 | if (need_to_stop_sampler_) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 484 | sampler->Stop(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 485 | need_to_stop_sampler_ = false; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 486 | } |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 487 | is_profiling_ = false; |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 488 | processor_->StopSynchronously(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 489 | delete processor_; |
| 490 | delete generator_; |
| 491 | processor_ = NULL; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 492 | generator_ = NULL; |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 493 | logger->logging_nesting_ = saved_logging_nesting_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 494 | } |
| 495 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 496 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 497 | void CpuProfiler::LogBuiltins() { |
| 498 | Builtins* builtins = isolate_->builtins(); |
| 499 | ASSERT(builtins->is_initialized()); |
| 500 | for (int i = 0; i < Builtins::builtin_count; i++) { |
| 501 | CodeEventsContainer evt_rec(CodeEventRecord::REPORT_BUILTIN); |
| 502 | ReportBuiltinEventRecord* rec = &evt_rec.ReportBuiltinEventRecord_; |
| 503 | Builtins::Name id = static_cast<Builtins::Name>(i); |
| 504 | rec->start = builtins->builtin(id)->address(); |
| 505 | rec->builtin_id = id; |
| 506 | processor_->Enqueue(evt_rec); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 511 | } } // namespace v8::internal |