whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
| 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 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 32 | #ifdef ENABLE_LOGGING_AND_PROFILING |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 33 | |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 34 | #include "frames-inl.h" |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 35 | #include "hashmap.h" |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 36 | #include "log-inl.h" |
| 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 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 43 | static const int kEventsBufferSize = 256*KB; |
| 44 | static const int kTickSamplesBufferChunkSize = 64*KB; |
| 45 | static const int kTickSamplesBufferChunksCount = 16; |
| 46 | |
| 47 | |
| 48 | ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator) |
| 49 | : generator_(generator), |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 50 | running_(true), |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 51 | ticks_buffer_(sizeof(TickSampleEventRecord), |
| 52 | kTickSamplesBufferChunkSize, |
| 53 | kTickSamplesBufferChunksCount), |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 54 | enqueue_order_(0), |
| 55 | known_functions_(new HashMap(AddressesMatch)) { |
| 56 | } |
| 57 | |
| 58 | |
| 59 | ProfilerEventsProcessor::~ProfilerEventsProcessor() { |
| 60 | delete known_functions_; |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 61 | } |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 62 | |
| 63 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 64 | void ProfilerEventsProcessor::CallbackCreateEvent(Logger::LogEventsAndTags tag, |
| 65 | const char* prefix, |
| 66 | String* name, |
| 67 | Address start) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 68 | if (FilterOutCodeCreateEvent(tag)) return; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 69 | CodeEventsContainer evt_rec; |
| 70 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 71 | rec->type = CodeEventRecord::CODE_CREATION; |
| 72 | rec->order = ++enqueue_order_; |
| 73 | rec->start = start; |
| 74 | rec->entry = generator_->NewCodeEntry(tag, prefix, name); |
| 75 | rec->size = 1; |
| 76 | events_buffer_.Enqueue(evt_rec); |
| 77 | } |
| 78 | |
| 79 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 80 | void ProfilerEventsProcessor::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 81 | String* name, |
| 82 | String* resource_name, |
| 83 | int line_number, |
| 84 | Address start, |
| 85 | unsigned size) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 86 | if (FilterOutCodeCreateEvent(tag)) return; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 87 | CodeEventsContainer evt_rec; |
| 88 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 89 | rec->type = CodeEventRecord::CODE_CREATION; |
| 90 | rec->order = ++enqueue_order_; |
| 91 | rec->start = start; |
| 92 | rec->entry = generator_->NewCodeEntry(tag, name, resource_name, line_number); |
| 93 | rec->size = size; |
| 94 | events_buffer_.Enqueue(evt_rec); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | void ProfilerEventsProcessor::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 99 | const char* name, |
| 100 | Address start, |
| 101 | unsigned size) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 102 | if (FilterOutCodeCreateEvent(tag)) return; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 103 | CodeEventsContainer evt_rec; |
| 104 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 105 | rec->type = CodeEventRecord::CODE_CREATION; |
| 106 | rec->order = ++enqueue_order_; |
| 107 | rec->start = start; |
| 108 | rec->entry = generator_->NewCodeEntry(tag, name); |
| 109 | rec->size = size; |
| 110 | events_buffer_.Enqueue(evt_rec); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | void ProfilerEventsProcessor::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 115 | int args_count, |
| 116 | Address start, |
| 117 | unsigned size) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 118 | if (FilterOutCodeCreateEvent(tag)) return; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 119 | CodeEventsContainer evt_rec; |
| 120 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 121 | rec->type = CodeEventRecord::CODE_CREATION; |
| 122 | rec->order = ++enqueue_order_; |
| 123 | rec->start = start; |
| 124 | rec->entry = generator_->NewCodeEntry(tag, args_count); |
| 125 | rec->size = size; |
| 126 | events_buffer_.Enqueue(evt_rec); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | void ProfilerEventsProcessor::CodeMoveEvent(Address from, Address to) { |
| 131 | CodeEventsContainer evt_rec; |
| 132 | CodeMoveEventRecord* rec = &evt_rec.CodeMoveEventRecord_; |
| 133 | rec->type = CodeEventRecord::CODE_MOVE; |
| 134 | rec->order = ++enqueue_order_; |
| 135 | rec->from = from; |
| 136 | rec->to = to; |
| 137 | events_buffer_.Enqueue(evt_rec); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | void ProfilerEventsProcessor::CodeDeleteEvent(Address from) { |
| 142 | CodeEventsContainer evt_rec; |
| 143 | CodeDeleteEventRecord* rec = &evt_rec.CodeDeleteEventRecord_; |
| 144 | rec->type = CodeEventRecord::CODE_DELETE; |
| 145 | rec->order = ++enqueue_order_; |
| 146 | rec->start = from; |
| 147 | events_buffer_.Enqueue(evt_rec); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | void ProfilerEventsProcessor::FunctionCreateEvent(Address alias, |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 152 | Address start, |
| 153 | int security_token_id) { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 154 | CodeEventsContainer evt_rec; |
| 155 | CodeAliasEventRecord* rec = &evt_rec.CodeAliasEventRecord_; |
| 156 | rec->type = CodeEventRecord::CODE_ALIAS; |
| 157 | rec->order = ++enqueue_order_; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 158 | rec->start = alias; |
| 159 | rec->entry = generator_->NewCodeEntry(security_token_id); |
| 160 | rec->code_start = start; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 161 | events_buffer_.Enqueue(evt_rec); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 162 | |
| 163 | known_functions_->Lookup(alias, AddressHash(alias), true); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | |
| 167 | void ProfilerEventsProcessor::FunctionMoveEvent(Address from, Address to) { |
| 168 | CodeMoveEvent(from, to); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 169 | |
| 170 | if (IsKnownFunction(from)) { |
| 171 | known_functions_->Remove(from, AddressHash(from)); |
| 172 | known_functions_->Lookup(to, AddressHash(to), true); |
| 173 | } |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | |
| 177 | void ProfilerEventsProcessor::FunctionDeleteEvent(Address from) { |
| 178 | CodeDeleteEvent(from); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 179 | |
| 180 | known_functions_->Remove(from, AddressHash(from)); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | bool ProfilerEventsProcessor::IsKnownFunction(Address start) { |
| 185 | HashMap::Entry* entry = |
| 186 | known_functions_->Lookup(start, AddressHash(start), false); |
| 187 | return entry != NULL; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 191 | void ProfilerEventsProcessor::ProcessMovedFunctions() { |
| 192 | for (int i = 0; i < moved_functions_.length(); ++i) { |
| 193 | JSFunction* function = moved_functions_[i]; |
| 194 | CpuProfiler::FunctionCreateEvent(function); |
| 195 | } |
| 196 | moved_functions_.Clear(); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | void ProfilerEventsProcessor::RememberMovedFunction(JSFunction* function) { |
| 201 | moved_functions_.Add(function); |
| 202 | } |
| 203 | |
| 204 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 205 | void ProfilerEventsProcessor::RegExpCodeCreateEvent( |
| 206 | Logger::LogEventsAndTags tag, |
| 207 | const char* prefix, |
| 208 | String* name, |
| 209 | Address start, |
| 210 | unsigned size) { |
| 211 | if (FilterOutCodeCreateEvent(tag)) return; |
| 212 | CodeEventsContainer evt_rec; |
| 213 | CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 214 | rec->type = CodeEventRecord::CODE_CREATION; |
| 215 | rec->order = ++enqueue_order_; |
| 216 | rec->start = start; |
| 217 | rec->entry = generator_->NewCodeEntry(tag, prefix, name); |
| 218 | rec->size = size; |
| 219 | events_buffer_.Enqueue(evt_rec); |
| 220 | } |
| 221 | |
| 222 | |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 223 | void ProfilerEventsProcessor::AddCurrentStack() { |
| 224 | TickSampleEventRecord record; |
| 225 | TickSample* sample = &record.sample; |
| 226 | sample->state = VMState::current_state(); |
| 227 | sample->pc = reinterpret_cast<Address>(sample); // Not NULL. |
| 228 | sample->frames_count = 0; |
| 229 | for (StackTraceFrameIterator it; |
| 230 | !it.done() && sample->frames_count < TickSample::kMaxFramesCount; |
| 231 | it.Advance()) { |
| 232 | JavaScriptFrame* frame = it.frame(); |
| 233 | sample->stack[sample->frames_count++] = |
| 234 | reinterpret_cast<Address>(frame->function()); |
| 235 | } |
| 236 | record.order = enqueue_order_; |
| 237 | ticks_from_vm_buffer_.Enqueue(record); |
| 238 | } |
| 239 | |
| 240 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 241 | bool ProfilerEventsProcessor::ProcessCodeEvent(unsigned* dequeue_order) { |
| 242 | if (!events_buffer_.IsEmpty()) { |
| 243 | CodeEventsContainer record; |
| 244 | events_buffer_.Dequeue(&record); |
| 245 | switch (record.generic.type) { |
| 246 | #define PROFILER_TYPE_CASE(type, clss) \ |
| 247 | case CodeEventRecord::type: \ |
| 248 | record.clss##_.UpdateCodeMap(generator_->code_map()); \ |
| 249 | break; |
| 250 | |
| 251 | CODE_EVENTS_TYPE_LIST(PROFILER_TYPE_CASE) |
| 252 | |
| 253 | #undef PROFILER_TYPE_CASE |
| 254 | default: return true; // Skip record. |
| 255 | } |
| 256 | *dequeue_order = record.generic.order; |
| 257 | return true; |
| 258 | } |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | bool ProfilerEventsProcessor::ProcessTicks(unsigned dequeue_order) { |
| 264 | while (true) { |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 265 | if (!ticks_from_vm_buffer_.IsEmpty() |
| 266 | && ticks_from_vm_buffer_.Peek()->order == dequeue_order) { |
| 267 | TickSampleEventRecord record; |
| 268 | ticks_from_vm_buffer_.Dequeue(&record); |
| 269 | generator_->RecordTickSample(record.sample); |
| 270 | } |
| 271 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 272 | const TickSampleEventRecord* rec = |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 273 | TickSampleEventRecord::cast(ticks_buffer_.StartDequeue()); |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 274 | if (rec == NULL) return !ticks_from_vm_buffer_.IsEmpty(); |
kmillikin@chromium.org | 3cdd9e1 | 2010-09-06 11:39:48 +0000 | [diff] [blame] | 275 | // Make a local copy of tick sample record to ensure that it won't |
| 276 | // be modified as we are processing it. This is possible as the |
| 277 | // sampler writes w/o any sync to the queue, so if the processor |
| 278 | // will get far behind, a record may be modified right under its |
| 279 | // feet. |
| 280 | TickSampleEventRecord record = *rec; |
| 281 | if (record.order == dequeue_order) { |
| 282 | // A paranoid check to make sure that we don't get a memory overrun |
| 283 | // in case of frames_count having a wild value. |
| 284 | if (record.sample.frames_count < 0 |
| 285 | || record.sample.frames_count >= TickSample::kMaxFramesCount) |
| 286 | record.sample.frames_count = 0; |
| 287 | generator_->RecordTickSample(record.sample); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 288 | ticks_buffer_.FinishDequeue(); |
| 289 | } else { |
| 290 | return true; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | |
| 296 | void ProfilerEventsProcessor::Run() { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 297 | unsigned dequeue_order = 0; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 298 | |
| 299 | while (running_) { |
| 300 | // Process ticks until we have any. |
| 301 | if (ProcessTicks(dequeue_order)) { |
| 302 | // All ticks of the current dequeue_order are processed, |
| 303 | // proceed to the next code event. |
| 304 | ProcessCodeEvent(&dequeue_order); |
| 305 | } |
| 306 | YieldCPU(); |
| 307 | } |
| 308 | |
| 309 | // Process remaining tick events. |
| 310 | ticks_buffer_.FlushResidualRecords(); |
| 311 | // Perform processing until we have tick events, skip remaining code events. |
| 312 | while (ProcessTicks(dequeue_order) && ProcessCodeEvent(&dequeue_order)) { } |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 316 | CpuProfiler* CpuProfiler::singleton_ = NULL; |
| 317 | |
| 318 | void CpuProfiler::StartProfiling(const char* title) { |
| 319 | ASSERT(singleton_ != NULL); |
| 320 | singleton_->StartCollectingProfile(title); |
| 321 | } |
| 322 | |
| 323 | |
| 324 | void CpuProfiler::StartProfiling(String* title) { |
| 325 | ASSERT(singleton_ != NULL); |
| 326 | singleton_->StartCollectingProfile(title); |
| 327 | } |
| 328 | |
| 329 | |
| 330 | CpuProfile* CpuProfiler::StopProfiling(const char* title) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 331 | return is_profiling() ? singleton_->StopCollectingProfile(title) : NULL; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 335 | CpuProfile* CpuProfiler::StopProfiling(Object* security_token, String* title) { |
| 336 | return is_profiling() ? |
| 337 | singleton_->StopCollectingProfile(security_token, title) : NULL; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | |
| 341 | int CpuProfiler::GetProfilesCount() { |
| 342 | ASSERT(singleton_ != NULL); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 343 | // The count of profiles doesn't depend on a security token. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 344 | return singleton_->profiles_->Profiles( |
| 345 | TokenEnumerator::kNoSecurityToken)->length(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 349 | CpuProfile* CpuProfiler::GetProfile(Object* security_token, int index) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 350 | ASSERT(singleton_ != NULL); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 351 | const int token = singleton_->token_enumerator_->GetTokenId(security_token); |
| 352 | return singleton_->profiles_->Profiles(token)->at(index); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 356 | CpuProfile* CpuProfiler::FindProfile(Object* security_token, unsigned uid) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 357 | ASSERT(singleton_ != NULL); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 358 | const int token = singleton_->token_enumerator_->GetTokenId(security_token); |
| 359 | return singleton_->profiles_->GetProfile(token, uid); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | |
| 363 | TickSample* CpuProfiler::TickSampleEvent() { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 364 | if (CpuProfiler::is_profiling()) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 365 | return singleton_->processor_->TickSampleEvent(); |
| 366 | } else { |
| 367 | return NULL; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | |
| 372 | void CpuProfiler::CallbackEvent(String* name, Address entry_point) { |
| 373 | singleton_->processor_->CallbackCreateEvent( |
| 374 | Logger::CALLBACK_TAG, CodeEntry::kEmptyNamePrefix, name, entry_point); |
| 375 | } |
| 376 | |
| 377 | |
| 378 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 379 | Code* code, const char* comment) { |
| 380 | singleton_->processor_->CodeCreateEvent( |
| 381 | tag, comment, code->address(), code->ExecutableSize()); |
| 382 | } |
| 383 | |
| 384 | |
| 385 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 386 | Code* code, String* name) { |
| 387 | singleton_->processor_->CodeCreateEvent( |
| 388 | tag, |
| 389 | name, |
| 390 | Heap::empty_string(), |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 391 | v8::CpuProfileNode::kNoLineNumberInfo, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 392 | code->address(), |
| 393 | code->ExecutableSize()); |
| 394 | } |
| 395 | |
| 396 | |
| 397 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 398 | Code* code, String* name, |
| 399 | String* source, int line) { |
| 400 | singleton_->processor_->CodeCreateEvent( |
| 401 | tag, |
| 402 | name, |
| 403 | source, |
| 404 | line, |
| 405 | code->address(), |
| 406 | code->ExecutableSize()); |
| 407 | } |
| 408 | |
| 409 | |
| 410 | void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, |
| 411 | Code* code, int args_count) { |
| 412 | singleton_->processor_->CodeCreateEvent( |
| 413 | tag, |
| 414 | args_count, |
| 415 | code->address(), |
| 416 | code->ExecutableSize()); |
| 417 | } |
| 418 | |
| 419 | |
| 420 | void CpuProfiler::CodeMoveEvent(Address from, Address to) { |
| 421 | singleton_->processor_->CodeMoveEvent(from, to); |
| 422 | } |
| 423 | |
| 424 | |
| 425 | void CpuProfiler::CodeDeleteEvent(Address from) { |
| 426 | singleton_->processor_->CodeDeleteEvent(from); |
| 427 | } |
| 428 | |
| 429 | |
| 430 | void CpuProfiler::FunctionCreateEvent(JSFunction* function) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 431 | int security_token_id = TokenEnumerator::kNoSecurityToken; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 432 | if (function->unchecked_context()->IsContext()) { |
| 433 | security_token_id = singleton_->token_enumerator_->GetTokenId( |
| 434 | function->context()->global_context()->security_token()); |
| 435 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 436 | singleton_->processor_->FunctionCreateEvent( |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 437 | function->address(), |
| 438 | function->code()->address(), |
| 439 | security_token_id); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 443 | void CpuProfiler::ProcessMovedFunctions() { |
| 444 | singleton_->processor_->ProcessMovedFunctions(); |
| 445 | } |
| 446 | |
| 447 | |
| 448 | void CpuProfiler::FunctionCreateEventFromMove(JSFunction* function) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 449 | // This function is called from GC iterators (during Scavenge, |
| 450 | // MC, and MS), so marking bits can be set on objects. That's |
| 451 | // why unchecked accessors are used here. |
| 452 | |
| 453 | // The same function can be reported several times. |
| 454 | if (function->unchecked_code() == Builtins::builtin(Builtins::LazyCompile) |
| 455 | || singleton_->processor_->IsKnownFunction(function->address())) return; |
| 456 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 457 | singleton_->processor_->RememberMovedFunction(function); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 461 | void CpuProfiler::FunctionMoveEvent(Address from, Address to) { |
| 462 | singleton_->processor_->FunctionMoveEvent(from, to); |
| 463 | } |
| 464 | |
| 465 | |
| 466 | void CpuProfiler::FunctionDeleteEvent(Address from) { |
| 467 | singleton_->processor_->FunctionDeleteEvent(from); |
| 468 | } |
| 469 | |
| 470 | |
| 471 | void CpuProfiler::GetterCallbackEvent(String* name, Address entry_point) { |
| 472 | singleton_->processor_->CallbackCreateEvent( |
| 473 | Logger::CALLBACK_TAG, "get ", name, entry_point); |
| 474 | } |
| 475 | |
| 476 | |
| 477 | void CpuProfiler::RegExpCodeCreateEvent(Code* code, String* source) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 478 | singleton_->processor_->RegExpCodeCreateEvent( |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 479 | Logger::REG_EXP_TAG, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 480 | "RegExp: ", |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 481 | source, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 482 | code->address(), |
| 483 | code->ExecutableSize()); |
| 484 | } |
| 485 | |
| 486 | |
| 487 | void CpuProfiler::SetterCallbackEvent(String* name, Address entry_point) { |
| 488 | singleton_->processor_->CallbackCreateEvent( |
| 489 | Logger::CALLBACK_TAG, "set ", name, entry_point); |
| 490 | } |
| 491 | |
| 492 | |
| 493 | CpuProfiler::CpuProfiler() |
| 494 | : profiles_(new CpuProfilesCollection()), |
| 495 | next_profile_uid_(1), |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 496 | token_enumerator_(new TokenEnumerator()), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 497 | generator_(NULL), |
| 498 | processor_(NULL) { |
| 499 | } |
| 500 | |
| 501 | |
| 502 | CpuProfiler::~CpuProfiler() { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 503 | delete token_enumerator_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 504 | delete profiles_; |
| 505 | } |
| 506 | |
| 507 | |
| 508 | void CpuProfiler::StartCollectingProfile(const char* title) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 509 | if (profiles_->StartProfiling(title, next_profile_uid_++)) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 510 | StartProcessorIfNotStarted(); |
| 511 | } |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 512 | processor_->AddCurrentStack(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | |
| 516 | void CpuProfiler::StartCollectingProfile(String* title) { |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 517 | StartCollectingProfile(profiles_->GetName(title)); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | |
| 521 | void CpuProfiler::StartProcessorIfNotStarted() { |
| 522 | if (processor_ == NULL) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 523 | // Disable logging when using the new implementation. |
| 524 | saved_logging_nesting_ = Logger::logging_nesting_; |
| 525 | Logger::logging_nesting_ = 0; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 526 | generator_ = new ProfileGenerator(profiles_); |
| 527 | processor_ = new ProfilerEventsProcessor(generator_); |
| 528 | processor_->Start(); |
| 529 | // Enumerate stuff we already have in the heap. |
| 530 | if (Heap::HasBeenSetup()) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 531 | if (!FLAG_prof_browser_mode) { |
| 532 | bool saved_log_code_flag = FLAG_log_code; |
| 533 | FLAG_log_code = true; |
| 534 | Logger::LogCodeObjects(); |
| 535 | FLAG_log_code = saved_log_code_flag; |
| 536 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 537 | Logger::LogCompiledFunctions(); |
| 538 | Logger::LogFunctionObjects(); |
| 539 | Logger::LogAccessorCallbacks(); |
| 540 | } |
sgjesse@chromium.org | 82dbbab | 2010-06-02 08:57:44 +0000 | [diff] [blame] | 541 | // Enable stack sampling. |
| 542 | reinterpret_cast<Sampler*>(Logger::ticker_)->Start(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
| 546 | |
| 547 | CpuProfile* CpuProfiler::StopCollectingProfile(const char* title) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 548 | const double actual_sampling_rate = generator_->actual_sampling_rate(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 549 | StopProcessorIfLastProfile(title); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 550 | CpuProfile* result = |
| 551 | profiles_->StopProfiling(TokenEnumerator::kNoSecurityToken, |
| 552 | title, |
| 553 | actual_sampling_rate); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 554 | if (result != NULL) { |
| 555 | result->Print(); |
| 556 | } |
| 557 | return result; |
| 558 | } |
| 559 | |
| 560 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 561 | CpuProfile* CpuProfiler::StopCollectingProfile(Object* security_token, |
| 562 | String* title) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 563 | const double actual_sampling_rate = generator_->actual_sampling_rate(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 564 | const char* profile_title = profiles_->GetName(title); |
| 565 | StopProcessorIfLastProfile(profile_title); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 566 | int token = token_enumerator_->GetTokenId(security_token); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 567 | return profiles_->StopProfiling(token, profile_title, actual_sampling_rate); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 571 | void CpuProfiler::StopProcessorIfLastProfile(const char* title) { |
| 572 | if (profiles_->IsLastProfile(title)) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 573 | reinterpret_cast<Sampler*>(Logger::ticker_)->Stop(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 574 | processor_->Stop(); |
| 575 | processor_->Join(); |
| 576 | delete processor_; |
| 577 | delete generator_; |
| 578 | processor_ = NULL; |
| 579 | generator_ = NULL; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 580 | Logger::logging_nesting_ = saved_logging_nesting_; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
| 584 | } } // namespace v8::internal |
| 585 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 586 | #endif // ENABLE_LOGGING_AND_PROFILING |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 587 | |
| 588 | namespace v8 { |
| 589 | namespace internal { |
| 590 | |
| 591 | void CpuProfiler::Setup() { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 592 | #ifdef ENABLE_LOGGING_AND_PROFILING |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 593 | if (singleton_ == NULL) { |
| 594 | singleton_ = new CpuProfiler(); |
| 595 | } |
| 596 | #endif |
| 597 | } |
| 598 | |
| 599 | |
| 600 | void CpuProfiler::TearDown() { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 601 | #ifdef ENABLE_LOGGING_AND_PROFILING |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 602 | if (singleton_ != NULL) { |
| 603 | delete singleton_; |
| 604 | } |
| 605 | singleton_ = NULL; |
| 606 | #endif |
| 607 | } |
| 608 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 609 | } } // namespace v8::internal |