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_); |
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) { |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 109 | while (!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 | |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 117 | const TickSampleEventRecord* record = ticks_buffer_.StartDequeue(); |
| 118 | if (record == NULL) return !ticks_from_vm_buffer_.IsEmpty(); |
| 119 | if (record->order != last_processed_code_event_id_) return true; |
| 120 | generator_->RecordTickSample(record->sample); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 121 | ticks_buffer_.FinishDequeue(); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 126 | void ProfilerEventsProcessor::ProcessEventsAndDoSample() { |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame^] | 127 | ElapsedTimer timer; |
| 128 | timer.Start(); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 129 | // Keep processing existing events until we need to do next sample. |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame^] | 130 | while (!timer.HasExpired(period_)) { |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 131 | if (ProcessTicks()) { |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 132 | // All ticks of the current dequeue_order are processed, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 133 | // proceed to the next code event. |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 134 | ProcessCodeEvent(); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 135 | } |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 136 | } |
| 137 | // Schedule next sample. sampler_ is NULL in tests. |
| 138 | if (sampler_) sampler_->DoSample(); |
| 139 | } |
| 140 | |
| 141 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 142 | void ProfilerEventsProcessor::Run() { |
| 143 | while (running_) { |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame^] | 144 | ProcessEventsAndDoSample(); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 145 | } |
| 146 | |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 147 | // Process remaining tick events. |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 148 | do { |
| 149 | ProcessTicks(); |
| 150 | } while (ProcessCodeEvent()); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 154 | int CpuProfiler::GetProfilesCount() { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 155 | // The count of profiles doesn't depend on a security token. |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 156 | return profiles_->profiles()->length(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 160 | CpuProfile* CpuProfiler::GetProfile(int index) { |
| 161 | return profiles_->profiles()->at(index); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 165 | void CpuProfiler::DeleteAllProfiles() { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 166 | if (is_profiling_) StopProcessor(); |
| 167 | ResetProfiles(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | |
| 171 | void CpuProfiler::DeleteProfile(CpuProfile* profile) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 172 | profiles_->RemoveProfile(profile); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 173 | delete profile; |
| 174 | } |
| 175 | |
| 176 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 177 | static bool FilterOutCodeCreateEvent(Logger::LogEventsAndTags tag) { |
| 178 | return FLAG_prof_browser_mode |
| 179 | && (tag != Logger::CALLBACK_TAG |
| 180 | && tag != Logger::FUNCTION_TAG |
| 181 | && tag != Logger::LAZY_COMPILE_TAG |
| 182 | && tag != Logger::REG_EXP_TAG |
| 183 | && tag != Logger::SCRIPT_TAG); |
| 184 | } |
| 185 | |
| 186 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 187 | void CpuProfiler::CallbackEvent(Name* name, Address entry_point) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 188 | if (FilterOutCodeCreateEvent(Logger::CALLBACK_TAG)) return; |
| 189 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 190 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 191 | rec->start = entry_point; |
| 192 | rec->entry = profiles_->NewCodeEntry( |
| 193 | Logger::CALLBACK_TAG, |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 194 | profiles_->GetName(name)); |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 195 | rec->size = 1; |
| 196 | rec->shared = NULL; |
| 197 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | |
| 201 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 202 | Code* code, |
| 203 | const char* name) { |
| 204 | if (FilterOutCodeCreateEvent(tag)) return; |
| 205 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 206 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 207 | rec->start = code->address(); |
| 208 | rec->entry = profiles_->NewCodeEntry(tag, profiles_->GetFunctionName(name)); |
| 209 | rec->size = code->ExecutableSize(); |
| 210 | rec->shared = NULL; |
| 211 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | |
| 215 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 216 | Code* code, |
| 217 | Name* name) { |
| 218 | if (FilterOutCodeCreateEvent(tag)) return; |
| 219 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 220 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 221 | rec->start = code->address(); |
| 222 | rec->entry = profiles_->NewCodeEntry(tag, profiles_->GetFunctionName(name)); |
| 223 | rec->size = code->ExecutableSize(); |
| 224 | rec->shared = NULL; |
| 225 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | |
| 229 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 230 | Code* code, |
| 231 | SharedFunctionInfo* shared, |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 232 | CompilationInfo* info, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 233 | Name* name) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 234 | if (FilterOutCodeCreateEvent(tag)) return; |
| 235 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 236 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 237 | rec->start = code->address(); |
| 238 | rec->entry = profiles_->NewCodeEntry(tag, profiles_->GetFunctionName(name)); |
| 239 | if (info) { |
| 240 | rec->entry->set_no_frame_ranges(info->ReleaseNoFrameRanges()); |
| 241 | } |
| 242 | if (shared->script()->IsScript()) { |
| 243 | ASSERT(Script::cast(shared->script())); |
| 244 | Script* script = Script::cast(shared->script()); |
| 245 | rec->entry->set_script_id(script->id()->value()); |
| 246 | } |
| 247 | rec->size = code->ExecutableSize(); |
| 248 | rec->shared = shared->address(); |
| 249 | processor_->Enqueue(evt_rec); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | |
| 253 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 254 | Code* code, |
| 255 | SharedFunctionInfo* shared, |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 256 | CompilationInfo* info, |
yangguo@chromium.org | c73d55b | 2013-07-24 08:18:28 +0000 | [diff] [blame] | 257 | Name* source, int line) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 258 | if (FilterOutCodeCreateEvent(tag)) return; |
| 259 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 260 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 261 | rec->start = code->address(); |
| 262 | rec->entry = profiles_->NewCodeEntry( |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 263 | tag, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 264 | profiles_->GetFunctionName(shared->DebugName()), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 265 | CodeEntry::kEmptyNamePrefix, |
| 266 | profiles_->GetName(source), |
| 267 | line); |
| 268 | if (info) { |
| 269 | rec->entry->set_no_frame_ranges(info->ReleaseNoFrameRanges()); |
| 270 | } |
| 271 | ASSERT(Script::cast(shared->script())); |
| 272 | Script* script = Script::cast(shared->script()); |
| 273 | rec->entry->set_script_id(script->id()->value()); |
| 274 | rec->size = code->ExecutableSize(); |
| 275 | rec->shared = shared->address(); |
| 276 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | |
| 280 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 281 | Code* code, |
| 282 | int args_count) { |
| 283 | if (FilterOutCodeCreateEvent(tag)) return; |
| 284 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 285 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 286 | rec->start = code->address(); |
| 287 | rec->entry = profiles_->NewCodeEntry( |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 288 | tag, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 289 | profiles_->GetName(args_count), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 290 | "args_count: "); |
| 291 | rec->size = code->ExecutableSize(); |
| 292 | rec->shared = NULL; |
| 293 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | |
| 297 | void CpuProfiler::CodeMoveEvent(Address from, Address to) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 298 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_MOVE); |
| 299 | CodeMoveEventRecord* rec = &evt_rec.CodeMoveEventRecord_; |
| 300 | rec->from = from; |
| 301 | rec->to = to; |
| 302 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | |
| 306 | void CpuProfiler::CodeDeleteEvent(Address from) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 310 | void CpuProfiler::SharedFunctionInfoMoveEvent(Address from, Address to) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 311 | CodeEventsContainer evt_rec(CodeEventRecord::SHARED_FUNC_MOVE); |
| 312 | SharedFunctionInfoMoveEventRecord* rec = |
| 313 | &evt_rec.SharedFunctionInfoMoveEventRecord_; |
| 314 | rec->from = from; |
| 315 | rec->to = to; |
| 316 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 320 | void CpuProfiler::GetterCallbackEvent(Name* name, Address entry_point) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 321 | if (FilterOutCodeCreateEvent(Logger::CALLBACK_TAG)) return; |
| 322 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 323 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 324 | rec->start = entry_point; |
| 325 | rec->entry = profiles_->NewCodeEntry( |
| 326 | Logger::CALLBACK_TAG, |
| 327 | profiles_->GetName(name), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 328 | "get "); |
| 329 | rec->size = 1; |
| 330 | rec->shared = NULL; |
| 331 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | |
| 335 | void CpuProfiler::RegExpCodeCreateEvent(Code* code, String* source) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 336 | if (FilterOutCodeCreateEvent(Logger::REG_EXP_TAG)) return; |
| 337 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 338 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 339 | rec->start = code->address(); |
| 340 | rec->entry = profiles_->NewCodeEntry( |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 341 | Logger::REG_EXP_TAG, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 342 | profiles_->GetName(source), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 343 | "RegExp: "); |
| 344 | rec->size = code->ExecutableSize(); |
| 345 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 349 | void CpuProfiler::SetterCallbackEvent(Name* name, Address entry_point) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 350 | if (FilterOutCodeCreateEvent(Logger::CALLBACK_TAG)) return; |
| 351 | CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 352 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 353 | rec->start = entry_point; |
| 354 | rec->entry = profiles_->NewCodeEntry( |
| 355 | Logger::CALLBACK_TAG, |
| 356 | profiles_->GetName(name), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 357 | "set "); |
| 358 | rec->size = 1; |
| 359 | rec->shared = NULL; |
| 360 | processor_->Enqueue(evt_rec); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 364 | CpuProfiler::CpuProfiler(Isolate* isolate) |
| 365 | : isolate_(isolate), |
| 366 | profiles_(new CpuProfilesCollection()), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 367 | next_profile_uid_(1), |
| 368 | generator_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 369 | processor_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 370 | is_profiling_(false) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 374 | CpuProfiler::CpuProfiler(Isolate* isolate, |
| 375 | CpuProfilesCollection* test_profiles, |
| 376 | ProfileGenerator* test_generator, |
| 377 | ProfilerEventsProcessor* test_processor) |
| 378 | : isolate_(isolate), |
| 379 | profiles_(test_profiles), |
| 380 | next_profile_uid_(1), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 381 | generator_(test_generator), |
| 382 | processor_(test_processor), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 383 | is_profiling_(false) { |
| 384 | } |
| 385 | |
| 386 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 387 | CpuProfiler::~CpuProfiler() { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 388 | ASSERT(!is_profiling_); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 389 | delete profiles_; |
| 390 | } |
| 391 | |
| 392 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 393 | void CpuProfiler::ResetProfiles() { |
| 394 | delete profiles_; |
| 395 | profiles_ = new CpuProfilesCollection(); |
| 396 | } |
| 397 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 398 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 399 | void CpuProfiler::StartProfiling(const char* title, bool record_samples) { |
| 400 | if (profiles_->StartProfiling(title, next_profile_uid_++, record_samples)) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 401 | StartProcessorIfNotStarted(); |
| 402 | } |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 403 | processor_->AddCurrentStack(isolate_); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 407 | void CpuProfiler::StartProfiling(String* title, bool record_samples) { |
| 408 | StartProfiling(profiles_->GetName(title), record_samples); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | |
| 412 | void CpuProfiler::StartProcessorIfNotStarted() { |
| 413 | if (processor_ == NULL) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 414 | Logger* logger = isolate_->logger(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 415 | // Disable logging when using the new implementation. |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame^] | 416 | saved_is_logging_ = logger->is_logging_; |
| 417 | logger->is_logging_ = false; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 418 | generator_ = new ProfileGenerator(profiles_); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 419 | Sampler* sampler = logger->sampler(); |
| 420 | processor_ = new ProfilerEventsProcessor( |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame^] | 421 | generator_, sampler, |
| 422 | TimeDelta::FromMicroseconds(FLAG_cpu_profiler_sampling_interval)); |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 423 | is_profiling_ = true; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 424 | // Enumerate stuff we already have in the heap. |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 425 | ASSERT(isolate_->heap()->HasBeenSetUp()); |
| 426 | if (!FLAG_prof_browser_mode) { |
| 427 | logger->LogCodeObjects(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 428 | } |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 429 | logger->LogCompiledFunctions(); |
| 430 | logger->LogAccessorCallbacks(); |
| 431 | LogBuiltins(); |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 432 | // Enable stack sampling. |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame^] | 433 | sampler->SetHasProcessingThread(true); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 434 | sampler->IncreaseProfilingDepth(); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 435 | processor_->StartSynchronously(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 440 | CpuProfile* CpuProfiler::StopProfiling(const char* title) { |
| 441 | if (!is_profiling_) return NULL; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 442 | StopProcessorIfLastProfile(title); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 443 | CpuProfile* result = profiles_->StopProfiling(title); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 444 | if (result != NULL) { |
| 445 | result->Print(); |
| 446 | } |
| 447 | return result; |
| 448 | } |
| 449 | |
| 450 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 451 | CpuProfile* CpuProfiler::StopProfiling(String* title) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 452 | if (!is_profiling_) return NULL; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 453 | const char* profile_title = profiles_->GetName(title); |
| 454 | StopProcessorIfLastProfile(profile_title); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 455 | return profiles_->StopProfiling(profile_title); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 459 | void CpuProfiler::StopProcessorIfLastProfile(const char* title) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 460 | if (profiles_->IsLastProfile(title)) StopProcessor(); |
| 461 | } |
| 462 | |
| 463 | |
| 464 | void CpuProfiler::StopProcessor() { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 465 | Logger* logger = isolate_->logger(); |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 466 | Sampler* sampler = reinterpret_cast<Sampler*>(logger->ticker_); |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 467 | is_profiling_ = false; |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 468 | processor_->StopSynchronously(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 469 | delete processor_; |
| 470 | delete generator_; |
| 471 | processor_ = NULL; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 472 | generator_ = NULL; |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame^] | 473 | sampler->SetHasProcessingThread(false); |
| 474 | sampler->DecreaseProfilingDepth(); |
| 475 | logger->is_logging_ = saved_is_logging_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 476 | } |
| 477 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 478 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 479 | void CpuProfiler::LogBuiltins() { |
| 480 | Builtins* builtins = isolate_->builtins(); |
| 481 | ASSERT(builtins->is_initialized()); |
| 482 | for (int i = 0; i < Builtins::builtin_count; i++) { |
| 483 | CodeEventsContainer evt_rec(CodeEventRecord::REPORT_BUILTIN); |
| 484 | ReportBuiltinEventRecord* rec = &evt_rec.ReportBuiltinEventRecord_; |
| 485 | Builtins::Name id = static_cast<Builtins::Name>(i); |
| 486 | rec->start = builtins->builtin(id)->address(); |
| 487 | rec->builtin_id = id; |
| 488 | processor_->Enqueue(evt_rec); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 493 | } } // namespace v8::internal |