| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 | 38 |
| 39 namespace v8 { | 39 namespace v8 { |
| 40 namespace internal { | 40 namespace internal { |
| 41 | 41 |
| 42 static const int kEventsBufferSize = 256 * KB; | 42 static const int kEventsBufferSize = 256 * KB; |
| 43 static const int kTickSamplesBufferChunkSize = 64 * KB; | 43 static const int kTickSamplesBufferChunkSize = 64 * KB; |
| 44 static const int kTickSamplesBufferChunksCount = 16; | 44 static const int kTickSamplesBufferChunksCount = 16; |
| 45 static const int kProfilerStackSize = 64 * KB; | 45 static const int kProfilerStackSize = 64 * KB; |
| 46 | 46 |
| 47 | 47 |
| 48 ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator) | 48 ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator, |
| 49 Sampler* sampler, |
| 50 int period_in_useconds) |
| 49 : Thread(Thread::Options("v8:ProfEvntProc", kProfilerStackSize)), | 51 : Thread(Thread::Options("v8:ProfEvntProc", kProfilerStackSize)), |
| 50 generator_(generator), | 52 generator_(generator), |
| 53 sampler_(sampler), |
| 51 running_(true), | 54 running_(true), |
| 55 period_in_useconds_(period_in_useconds), |
| 52 ticks_buffer_(sizeof(TickSampleEventRecord), | 56 ticks_buffer_(sizeof(TickSampleEventRecord), |
| 53 kTickSamplesBufferChunkSize, | 57 kTickSamplesBufferChunkSize, |
| 54 kTickSamplesBufferChunksCount), | 58 kTickSamplesBufferChunksCount), |
| 55 enqueue_order_(0) { | 59 enqueue_order_(0) { |
| 56 } | 60 } |
| 57 | 61 |
| 58 | 62 |
| 59 void ProfilerEventsProcessor::CallbackCreateEvent(Logger::LogEventsAndTags tag, | 63 void ProfilerEventsProcessor::CallbackCreateEvent(Logger::LogEventsAndTags tag, |
| 60 const char* prefix, | 64 const char* prefix, |
| 61 String* name, | 65 String* name, |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 #undef PROFILER_TYPE_CASE | 203 #undef PROFILER_TYPE_CASE |
| 200 default: return true; // Skip record. | 204 default: return true; // Skip record. |
| 201 } | 205 } |
| 202 *dequeue_order = record.generic.order; | 206 *dequeue_order = record.generic.order; |
| 203 return true; | 207 return true; |
| 204 } | 208 } |
| 205 return false; | 209 return false; |
| 206 } | 210 } |
| 207 | 211 |
| 208 | 212 |
| 209 bool ProfilerEventsProcessor::ProcessTicks(unsigned dequeue_order) { | 213 bool ProfilerEventsProcessor::ProcessTicks(int64_t stop_time, |
| 210 while (true) { | 214 unsigned dequeue_order) { |
| 215 while (stop_time == -1 || OS::Ticks() < stop_time) { |
| 211 if (!ticks_from_vm_buffer_.IsEmpty() | 216 if (!ticks_from_vm_buffer_.IsEmpty() |
| 212 && ticks_from_vm_buffer_.Peek()->order == dequeue_order) { | 217 && ticks_from_vm_buffer_.Peek()->order == dequeue_order) { |
| 213 TickSampleEventRecord record; | 218 TickSampleEventRecord record; |
| 214 ticks_from_vm_buffer_.Dequeue(&record); | 219 ticks_from_vm_buffer_.Dequeue(&record); |
| 215 generator_->RecordTickSample(record.sample); | 220 generator_->RecordTickSample(record.sample); |
| 216 } | 221 } |
| 217 | 222 |
| 218 const TickSampleEventRecord* rec = | 223 const TickSampleEventRecord* rec = |
| 219 TickSampleEventRecord::cast(ticks_buffer_.StartDequeue()); | 224 TickSampleEventRecord::cast(ticks_buffer_.StartDequeue()); |
| 220 if (rec == NULL) return !ticks_from_vm_buffer_.IsEmpty(); | 225 if (rec == NULL) return !ticks_from_vm_buffer_.IsEmpty(); |
| 221 // Make a local copy of tick sample record to ensure that it won't | 226 // Make a local copy of tick sample record to ensure that it won't |
| 222 // be modified as we are processing it. This is possible as the | 227 // be modified as we are processing it. This is possible as the |
| 223 // sampler writes w/o any sync to the queue, so if the processor | 228 // sampler writes w/o any sync to the queue, so if the processor |
| 224 // will get far behind, a record may be modified right under its | 229 // will get far behind, a record may be modified right under its |
| 225 // feet. | 230 // feet. |
| 226 TickSampleEventRecord record = *rec; | 231 TickSampleEventRecord record = *rec; |
| 227 if (record.order == dequeue_order) { | 232 if (record.order == dequeue_order) { |
| 228 // A paranoid check to make sure that we don't get a memory overrun | 233 // A paranoid check to make sure that we don't get a memory overrun |
| 229 // in case of frames_count having a wild value. | 234 // in case of frames_count having a wild value. |
| 230 if (record.sample.frames_count < 0 | 235 if (record.sample.frames_count < 0 |
| 231 || record.sample.frames_count > TickSample::kMaxFramesCount) | 236 || record.sample.frames_count > TickSample::kMaxFramesCount) |
| 232 record.sample.frames_count = 0; | 237 record.sample.frames_count = 0; |
| 233 generator_->RecordTickSample(record.sample); | 238 generator_->RecordTickSample(record.sample); |
| 234 ticks_buffer_.FinishDequeue(); | 239 ticks_buffer_.FinishDequeue(); |
| 235 } else { | 240 } else { |
| 236 return true; | 241 return true; |
| 237 } | 242 } |
| 238 } | 243 } |
| 244 return false; |
| 245 } |
| 246 |
| 247 |
| 248 void ProfilerEventsProcessor::ProcessEventsQueue(int64_t stop_time, |
| 249 unsigned* dequeue_order) { |
| 250 while (OS::Ticks() < stop_time) { |
| 251 if (ProcessTicks(stop_time, *dequeue_order)) { |
| 252 // All ticks of the current dequeue_order are processed, |
| 253 // proceed to the next code event. |
| 254 ProcessCodeEvent(dequeue_order); |
| 255 } |
| 256 } |
| 239 } | 257 } |
| 240 | 258 |
| 241 | 259 |
| 242 void ProfilerEventsProcessor::Run() { | 260 void ProfilerEventsProcessor::Run() { |
| 243 unsigned dequeue_order = 0; | 261 unsigned dequeue_order = 0; |
| 244 | 262 |
| 245 while (running_) { | 263 while (running_) { |
| 246 // Process ticks until we have any. | 264 int64_t stop_time = OS::Ticks() + period_in_useconds_; |
| 247 if (ProcessTicks(dequeue_order)) { | 265 if (sampler_ != NULL) { |
| 248 // All ticks of the current dequeue_order are processed, | 266 sampler_->DoSample(); |
| 249 // proceed to the next code event. | |
| 250 ProcessCodeEvent(&dequeue_order); | |
| 251 } | 267 } |
| 252 YieldCPU(); | 268 ProcessEventsQueue(stop_time, &dequeue_order); |
| 253 } | 269 } |
| 254 | 270 |
| 255 // Process remaining tick events. | 271 // Process remaining tick events. |
| 256 ticks_buffer_.FlushResidualRecords(); | 272 ticks_buffer_.FlushResidualRecords(); |
| 257 // Perform processing until we have tick events, skip remaining code events. | 273 // Perform processing until we have tick events, skip remaining code events. |
| 258 while (ProcessTicks(dequeue_order) && ProcessCodeEvent(&dequeue_order)) { } | 274 while (ProcessTicks(-1, dequeue_order) && ProcessCodeEvent(&dequeue_order)) { |
| 275 } |
| 259 } | 276 } |
| 260 | 277 |
| 261 | 278 |
| 262 void CpuProfiler::StartProfiling(const char* title) { | 279 void CpuProfiler::StartProfiling(const char* title) { |
| 263 ASSERT(Isolate::Current()->cpu_profiler() != NULL); | 280 ASSERT(Isolate::Current()->cpu_profiler() != NULL); |
| 264 Isolate::Current()->cpu_profiler()->StartCollectingProfile(title); | 281 Isolate::Current()->cpu_profiler()->StartCollectingProfile(title); |
| 265 } | 282 } |
| 266 | 283 |
| 267 | 284 |
| 268 void CpuProfiler::StartProfiling(String* title) { | 285 void CpuProfiler::StartProfiling(String* title) { |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 479 | 496 |
| 480 void CpuProfiler::StartCollectingProfile(String* title) { | 497 void CpuProfiler::StartCollectingProfile(String* title) { |
| 481 StartCollectingProfile(profiles_->GetName(title)); | 498 StartCollectingProfile(profiles_->GetName(title)); |
| 482 } | 499 } |
| 483 | 500 |
| 484 | 501 |
| 485 void CpuProfiler::StartProcessorIfNotStarted() { | 502 void CpuProfiler::StartProcessorIfNotStarted() { |
| 486 if (processor_ == NULL) { | 503 if (processor_ == NULL) { |
| 487 Isolate* isolate = Isolate::Current(); | 504 Isolate* isolate = Isolate::Current(); |
| 488 | 505 |
| 506 Sampler* sampler = isolate->logger()->sampler(); |
| 489 // Disable logging when using the new implementation. | 507 // Disable logging when using the new implementation. |
| 490 saved_logging_nesting_ = isolate->logger()->logging_nesting_; | 508 saved_logging_nesting_ = isolate->logger()->logging_nesting_; |
| 491 isolate->logger()->logging_nesting_ = 0; | 509 isolate->logger()->logging_nesting_ = 0; |
| 492 generator_ = new ProfileGenerator(profiles_); | 510 generator_ = new ProfileGenerator(profiles_); |
| 493 processor_ = new ProfilerEventsProcessor(generator_); | 511 processor_ = new ProfilerEventsProcessor(generator_, |
| 512 sampler, |
| 513 FLAG_cpu_profiler_sampling_period); |
| 494 NoBarrier_Store(&is_profiling_, true); | 514 NoBarrier_Store(&is_profiling_, true); |
| 495 processor_->Start(); | |
| 496 // Enumerate stuff we already have in the heap. | 515 // Enumerate stuff we already have in the heap. |
| 497 if (isolate->heap()->HasBeenSetUp()) { | 516 if (isolate->heap()->HasBeenSetUp()) { |
| 498 if (!FLAG_prof_browser_mode) { | 517 if (!FLAG_prof_browser_mode) { |
| 499 bool saved_log_code_flag = FLAG_log_code; | 518 bool saved_log_code_flag = FLAG_log_code; |
| 500 FLAG_log_code = true; | 519 FLAG_log_code = true; |
| 501 isolate->logger()->LogCodeObjects(); | 520 isolate->logger()->LogCodeObjects(); |
| 502 FLAG_log_code = saved_log_code_flag; | 521 FLAG_log_code = saved_log_code_flag; |
| 503 } | 522 } |
| 504 isolate->logger()->LogCompiledFunctions(); | 523 isolate->logger()->LogCompiledFunctions(); |
| 505 isolate->logger()->LogAccessorCallbacks(); | 524 isolate->logger()->LogAccessorCallbacks(); |
| 506 } | 525 } |
| 507 // Enable stack sampling. | 526 // Enable stack sampling. |
| 508 Sampler* sampler = reinterpret_cast<Sampler*>(isolate->logger()->ticker_); | |
| 509 if (!sampler->IsActive()) { | 527 if (!sampler->IsActive()) { |
| 510 sampler->Start(); | 528 sampler->Start(); |
| 511 need_to_stop_sampler_ = true; | 529 need_to_stop_sampler_ = true; |
| 512 } | 530 } |
| 513 sampler->IncreaseProfilingDepth(); | 531 sampler->IncreaseProfilingDepth(); |
| 532 processor_->Start(); |
| 514 } | 533 } |
| 515 } | 534 } |
| 516 | 535 |
| 517 | 536 |
| 518 CpuProfile* CpuProfiler::StopCollectingProfile(const char* title) { | 537 CpuProfile* CpuProfiler::StopCollectingProfile(const char* title) { |
| 519 const double actual_sampling_rate = generator_->actual_sampling_rate(); | 538 const double actual_sampling_rate = generator_->actual_sampling_rate(); |
| 520 StopProcessorIfLastProfile(title); | 539 StopProcessorIfLastProfile(title); |
| 521 CpuProfile* result = | 540 CpuProfile* result = |
| 522 profiles_->StopProfiling(TokenEnumerator::kNoSecurityToken, | 541 profiles_->StopProfiling(TokenEnumerator::kNoSecurityToken, |
| 523 title, | 542 title, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 538 return profiles_->StopProfiling(token, profile_title, actual_sampling_rate); | 557 return profiles_->StopProfiling(token, profile_title, actual_sampling_rate); |
| 539 } | 558 } |
| 540 | 559 |
| 541 | 560 |
| 542 void CpuProfiler::StopProcessorIfLastProfile(const char* title) { | 561 void CpuProfiler::StopProcessorIfLastProfile(const char* title) { |
| 543 if (profiles_->IsLastProfile(title)) StopProcessor(); | 562 if (profiles_->IsLastProfile(title)) StopProcessor(); |
| 544 } | 563 } |
| 545 | 564 |
| 546 | 565 |
| 547 void CpuProfiler::StopProcessor() { | 566 void CpuProfiler::StopProcessor() { |
| 567 NoBarrier_Store(&is_profiling_, false); |
| 568 processor_->Stop(); |
| 569 processor_->Join(); |
| 548 Logger* logger = Isolate::Current()->logger(); | 570 Logger* logger = Isolate::Current()->logger(); |
| 549 Sampler* sampler = reinterpret_cast<Sampler*>(logger->ticker_); | 571 Sampler* sampler = logger->sampler(); |
| 550 sampler->DecreaseProfilingDepth(); | 572 sampler->DecreaseProfilingDepth(); |
| 551 if (need_to_stop_sampler_) { | 573 if (need_to_stop_sampler_) { |
| 552 sampler->Stop(); | 574 sampler->Stop(); |
| 553 need_to_stop_sampler_ = false; | 575 need_to_stop_sampler_ = false; |
| 554 } | 576 } |
| 555 NoBarrier_Store(&is_profiling_, false); | |
| 556 processor_->Stop(); | |
| 557 processor_->Join(); | |
| 558 delete processor_; | 577 delete processor_; |
| 559 delete generator_; | 578 delete generator_; |
| 560 processor_ = NULL; | 579 processor_ = NULL; |
| 561 generator_ = NULL; | 580 generator_ = NULL; |
| 562 logger->logging_nesting_ = saved_logging_nesting_; | 581 logger->logging_nesting_ = saved_logging_nesting_; |
| 563 } | 582 } |
| 564 | 583 |
| 565 | 584 |
| 566 void CpuProfiler::SetUp() { | 585 void CpuProfiler::SetUp() { |
| 567 Isolate* isolate = Isolate::Current(); | 586 Isolate* isolate = Isolate::Current(); |
| 568 if (isolate->cpu_profiler() == NULL) { | 587 if (isolate->cpu_profiler() == NULL) { |
| 569 isolate->set_cpu_profiler(new CpuProfiler()); | 588 isolate->set_cpu_profiler(new CpuProfiler()); |
| 570 } | 589 } |
| 571 } | 590 } |
| 572 | 591 |
| 573 | 592 |
| 574 void CpuProfiler::TearDown() { | 593 void CpuProfiler::TearDown() { |
| 575 Isolate* isolate = Isolate::Current(); | 594 Isolate* isolate = Isolate::Current(); |
| 576 if (isolate->cpu_profiler() != NULL) { | 595 if (isolate->cpu_profiler() != NULL) { |
| 577 delete isolate->cpu_profiler(); | 596 delete isolate->cpu_profiler(); |
| 578 } | 597 } |
| 579 isolate->set_cpu_profiler(NULL); | 598 isolate->set_cpu_profiler(NULL); |
| 580 } | 599 } |
| 581 | 600 |
| 582 } } // namespace v8::internal | 601 } } // namespace v8::internal |
| OLD | NEW |