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