| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/log.h" | 5 #include "src/log.h" |
| 6 | 6 |
| 7 #include <cstdarg> | 7 #include <cstdarg> |
| 8 #include <sstream> | 8 #include <sstream> |
| 9 | 9 |
| 10 #include "include/v8-profiler.h" | |
| 11 #include "src/bailout-reason.h" | 10 #include "src/bailout-reason.h" |
| 12 #include "src/base/platform/platform.h" | 11 #include "src/base/platform/platform.h" |
| 13 #include "src/bootstrapper.h" | 12 #include "src/bootstrapper.h" |
| 14 #include "src/code-stubs.h" | 13 #include "src/code-stubs.h" |
| 15 #include "src/counters.h" | 14 #include "src/counters.h" |
| 16 #include "src/deoptimizer.h" | 15 #include "src/deoptimizer.h" |
| 17 #include "src/global-handles.h" | 16 #include "src/global-handles.h" |
| 18 #include "src/interpreter/bytecodes.h" | 17 #include "src/interpreter/bytecodes.h" |
| 19 #include "src/interpreter/interpreter.h" | 18 #include "src/interpreter/interpreter.h" |
| 20 #include "src/libsampler/sampler.h" | 19 #include "src/libsampler/sampler.h" |
| 21 #include "src/log-inl.h" | 20 #include "src/log-inl.h" |
| 22 #include "src/log-utils.h" | 21 #include "src/log-utils.h" |
| 23 #include "src/macro-assembler.h" | 22 #include "src/macro-assembler.h" |
| 24 #include "src/perf-jit.h" | 23 #include "src/perf-jit.h" |
| 24 #include "src/profiler/cpu-profiler-inl.h" |
| 25 #include "src/profiler/profiler-listener.h" | 25 #include "src/profiler/profiler-listener.h" |
| 26 #include "src/profiler/tick-sample.h" |
| 26 #include "src/runtime-profiler.h" | 27 #include "src/runtime-profiler.h" |
| 27 #include "src/string-stream.h" | 28 #include "src/string-stream.h" |
| 28 #include "src/vm-state-inl.h" | 29 #include "src/vm-state-inl.h" |
| 29 | 30 |
| 30 namespace v8 { | 31 namespace v8 { |
| 31 namespace internal { | 32 namespace internal { |
| 32 | 33 |
| 34 |
| 33 #define DECLARE_EVENT(ignore1, name) name, | 35 #define DECLARE_EVENT(ignore1, name) name, |
| 34 static const char* kLogEventsNames[CodeEventListener::NUMBER_OF_LOG_EVENTS] = { | 36 static const char* kLogEventsNames[CodeEventListener::NUMBER_OF_LOG_EVENTS] = { |
| 35 LOG_EVENTS_AND_TAGS_LIST(DECLARE_EVENT)}; | 37 LOG_EVENTS_AND_TAGS_LIST(DECLARE_EVENT)}; |
| 36 #undef DECLARE_EVENT | 38 #undef DECLARE_EVENT |
| 37 | 39 |
| 38 static const char* ComputeMarker(SharedFunctionInfo* shared, | 40 static const char* ComputeMarker(SharedFunctionInfo* shared, |
| 39 AbstractCode* code) { | 41 AbstractCode* code) { |
| 40 switch (code->kind()) { | 42 switch (code->kind()) { |
| 41 case AbstractCode::FUNCTION: | 43 case AbstractCode::FUNCTION: |
| 42 case AbstractCode::INTERPRETED_FUNCTION: | 44 case AbstractCode::INTERPRETED_FUNCTION: |
| (...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 // An independent thread removes data and writes it to the log. | 550 // An independent thread removes data and writes it to the log. |
| 549 // This design minimizes the time spent in the sampler. | 551 // This design minimizes the time spent in the sampler. |
| 550 // | 552 // |
| 551 class Profiler: public base::Thread { | 553 class Profiler: public base::Thread { |
| 552 public: | 554 public: |
| 553 explicit Profiler(Isolate* isolate); | 555 explicit Profiler(Isolate* isolate); |
| 554 void Engage(); | 556 void Engage(); |
| 555 void Disengage(); | 557 void Disengage(); |
| 556 | 558 |
| 557 // Inserts collected profiling data into buffer. | 559 // Inserts collected profiling data into buffer. |
| 558 void Insert(v8::TickSample* sample) { | 560 void Insert(TickSample* sample) { |
| 559 if (paused_) | 561 if (paused_) |
| 560 return; | 562 return; |
| 561 | 563 |
| 562 if (Succ(head_) == static_cast<int>(base::NoBarrier_Load(&tail_))) { | 564 if (Succ(head_) == static_cast<int>(base::NoBarrier_Load(&tail_))) { |
| 563 overflow_ = true; | 565 overflow_ = true; |
| 564 } else { | 566 } else { |
| 565 buffer_[head_] = *sample; | 567 buffer_[head_] = *sample; |
| 566 head_ = Succ(head_); | 568 head_ = Succ(head_); |
| 567 buffer_semaphore_.Signal(); // Tell we have an element. | 569 buffer_semaphore_.Signal(); // Tell we have an element. |
| 568 } | 570 } |
| 569 } | 571 } |
| 570 | 572 |
| 571 virtual void Run(); | 573 virtual void Run(); |
| 572 | 574 |
| 573 // Pause and Resume TickSample data collection. | 575 // Pause and Resume TickSample data collection. |
| 574 void pause() { paused_ = true; } | 576 void pause() { paused_ = true; } |
| 575 void resume() { paused_ = false; } | 577 void resume() { paused_ = false; } |
| 576 | 578 |
| 577 private: | 579 private: |
| 578 // Waits for a signal and removes profiling data. | 580 // Waits for a signal and removes profiling data. |
| 579 bool Remove(v8::TickSample* sample) { | 581 bool Remove(TickSample* sample) { |
| 580 buffer_semaphore_.Wait(); // Wait for an element. | 582 buffer_semaphore_.Wait(); // Wait for an element. |
| 581 *sample = buffer_[base::NoBarrier_Load(&tail_)]; | 583 *sample = buffer_[base::NoBarrier_Load(&tail_)]; |
| 582 bool result = overflow_; | 584 bool result = overflow_; |
| 583 base::NoBarrier_Store(&tail_, static_cast<base::Atomic32>( | 585 base::NoBarrier_Store(&tail_, static_cast<base::Atomic32>( |
| 584 Succ(base::NoBarrier_Load(&tail_)))); | 586 Succ(base::NoBarrier_Load(&tail_)))); |
| 585 overflow_ = false; | 587 overflow_ = false; |
| 586 return result; | 588 return result; |
| 587 } | 589 } |
| 588 | 590 |
| 589 // Returns the next index in the cyclic buffer. | 591 // Returns the next index in the cyclic buffer. |
| 590 int Succ(int index) { return (index + 1) % kBufferSize; } | 592 int Succ(int index) { return (index + 1) % kBufferSize; } |
| 591 | 593 |
| 592 Isolate* isolate_; | 594 Isolate* isolate_; |
| 593 // Cyclic buffer for communicating profiling samples | 595 // Cyclic buffer for communicating profiling samples |
| 594 // between the signal handler and the worker thread. | 596 // between the signal handler and the worker thread. |
| 595 static const int kBufferSize = 128; | 597 static const int kBufferSize = 128; |
| 596 v8::TickSample buffer_[kBufferSize]; // Buffer storage. | 598 TickSample buffer_[kBufferSize]; // Buffer storage. |
| 597 int head_; // Index to the buffer head. | 599 int head_; // Index to the buffer head. |
| 598 base::Atomic32 tail_; // Index to the buffer tail. | 600 base::Atomic32 tail_; // Index to the buffer tail. |
| 599 bool overflow_; // Tell whether a buffer overflow has occurred. | 601 bool overflow_; // Tell whether a buffer overflow has occurred. |
| 600 // Sempahore used for buffer synchronization. | 602 // Sempahore used for buffer synchronization. |
| 601 base::Semaphore buffer_semaphore_; | 603 base::Semaphore buffer_semaphore_; |
| 602 | 604 |
| 603 // Tells whether profiler is engaged, that is, processing thread is stated. | 605 // Tells whether profiler is engaged, that is, processing thread is stated. |
| 604 bool engaged_; | 606 bool engaged_; |
| 605 | 607 |
| 606 // Tells whether worker thread should continue running. | 608 // Tells whether worker thread should continue running. |
| 607 base::Atomic32 running_; | 609 base::Atomic32 running_; |
| 608 | 610 |
| 609 // Tells whether we are currently recording tick samples. | 611 // Tells whether we are currently recording tick samples. |
| 610 bool paused_; | 612 bool paused_; |
| 611 }; | 613 }; |
| 612 | 614 |
| 613 | 615 |
| 614 // | 616 // |
| 615 // Ticker used to provide ticks to the profiler and the sliding state | 617 // Ticker used to provide ticks to the profiler and the sliding state |
| 616 // window. | 618 // window. |
| 617 // | 619 // |
| 618 class Ticker: public sampler::Sampler { | 620 class Ticker: public sampler::Sampler { |
| 619 public: | 621 public: |
| 620 Ticker(Isolate* isolate, int interval) | 622 Ticker(Isolate* isolate, int interval): |
| 621 : sampler::Sampler(reinterpret_cast<v8::Isolate*>(isolate)), | 623 sampler::Sampler(reinterpret_cast<v8::Isolate*>(isolate)), |
| 622 profiler_(nullptr), | 624 profiler_(NULL), |
| 623 sampling_thread_(new SamplingThread(this, interval)) {} | 625 sampling_thread_(new SamplingThread(this, interval)) {} |
| 624 | 626 |
| 625 ~Ticker() { | 627 ~Ticker() { |
| 626 if (IsActive()) Stop(); | 628 if (IsActive()) Stop(); |
| 627 delete sampling_thread_; | 629 delete sampling_thread_; |
| 628 } | 630 } |
| 629 | 631 |
| 630 void SetProfiler(Profiler* profiler) { | 632 void SetProfiler(Profiler* profiler) { |
| 631 DCHECK(profiler_ == nullptr); | 633 DCHECK(profiler_ == NULL); |
| 632 profiler_ = profiler; | 634 profiler_ = profiler; |
| 633 IncreaseProfilingDepth(); | 635 IncreaseProfilingDepth(); |
| 634 if (!IsActive()) Start(); | 636 if (!IsActive()) Start(); |
| 635 sampling_thread_->StartSynchronously(); | 637 sampling_thread_->StartSynchronously(); |
| 636 } | 638 } |
| 637 | 639 |
| 638 void ClearProfiler() { | 640 void ClearProfiler() { |
| 639 profiler_ = nullptr; | 641 profiler_ = NULL; |
| 640 if (IsActive()) Stop(); | 642 if (IsActive()) Stop(); |
| 641 DecreaseProfilingDepth(); | 643 DecreaseProfilingDepth(); |
| 642 sampling_thread_->Join(); | 644 sampling_thread_->Join(); |
| 643 } | 645 } |
| 644 | 646 |
| 645 void SampleStack(const v8::RegisterState& state) override { | 647 void SampleStack(const v8::RegisterState& state) override { |
| 646 if (!profiler_) return; | 648 if (!profiler_) return; |
| 647 v8::TickSample sample; | 649 v8::Isolate* v8_isolate = isolate(); |
| 648 sample.Init(isolate(), state, v8::TickSample::kIncludeCEntryFrame, true); | 650 Isolate* i_isolate = reinterpret_cast<Isolate*>(v8_isolate); |
| 651 #if defined(USE_SIMULATOR) |
| 652 if (!SimulatorHelper::FillRegisters(i_isolate, |
| 653 const_cast<v8::RegisterState*>(&state))) |
| 654 return; |
| 655 #endif |
| 656 TickSample sample; |
| 657 sample.Init(i_isolate, state, TickSample::kIncludeCEntryFrame, true); |
| 649 profiler_->Insert(&sample); | 658 profiler_->Insert(&sample); |
| 650 } | 659 } |
| 651 | 660 |
| 652 private: | 661 private: |
| 653 Profiler* profiler_; | 662 Profiler* profiler_; |
| 654 SamplingThread* sampling_thread_; | 663 SamplingThread* sampling_thread_; |
| 655 }; | 664 }; |
| 656 | 665 |
| 657 | 666 |
| 658 // | 667 // |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 698 void Profiler::Disengage() { | 707 void Profiler::Disengage() { |
| 699 if (!engaged_) return; | 708 if (!engaged_) return; |
| 700 | 709 |
| 701 // Stop receiving ticks. | 710 // Stop receiving ticks. |
| 702 isolate_->logger()->ticker_->ClearProfiler(); | 711 isolate_->logger()->ticker_->ClearProfiler(); |
| 703 | 712 |
| 704 // Terminate the worker thread by setting running_ to false, | 713 // Terminate the worker thread by setting running_ to false, |
| 705 // inserting a fake element in the queue and then wait for | 714 // inserting a fake element in the queue and then wait for |
| 706 // the thread to terminate. | 715 // the thread to terminate. |
| 707 base::NoBarrier_Store(&running_, 0); | 716 base::NoBarrier_Store(&running_, 0); |
| 708 v8::TickSample sample; | 717 TickSample sample; |
| 709 // Reset 'paused_' flag, otherwise semaphore may not be signalled. | 718 // Reset 'paused_' flag, otherwise semaphore may not be signalled. |
| 710 resume(); | 719 resume(); |
| 711 Insert(&sample); | 720 Insert(&sample); |
| 712 Join(); | 721 Join(); |
| 713 | 722 |
| 714 LOG(isolate_, UncheckedStringEvent("profiler", "end")); | 723 LOG(isolate_, UncheckedStringEvent("profiler", "end")); |
| 715 } | 724 } |
| 716 | 725 |
| 717 | 726 |
| 718 void Profiler::Run() { | 727 void Profiler::Run() { |
| 719 v8::TickSample sample; | 728 TickSample sample; |
| 720 bool overflow = Remove(&sample); | 729 bool overflow = Remove(&sample); |
| 721 while (base::NoBarrier_Load(&running_)) { | 730 while (base::NoBarrier_Load(&running_)) { |
| 722 LOG(isolate_, TickEvent(&sample, overflow)); | 731 LOG(isolate_, TickEvent(&sample, overflow)); |
| 723 overflow = Remove(&sample); | 732 overflow = Remove(&sample); |
| 724 } | 733 } |
| 725 } | 734 } |
| 726 | 735 |
| 727 | 736 |
| 728 // | 737 // |
| 729 // Logger class implementation. | 738 // Logger class implementation. |
| (...skipping 621 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1351 RuntimeCallTimer* timer = stats->current_timer(); | 1360 RuntimeCallTimer* timer = stats->current_timer(); |
| 1352 if (timer == nullptr) return; | 1361 if (timer == nullptr) return; |
| 1353 RuntimeCallCounter* counter = timer->counter(); | 1362 RuntimeCallCounter* counter = timer->counter(); |
| 1354 if (counter == nullptr) return; | 1363 if (counter == nullptr) return; |
| 1355 Log::MessageBuilder msg(log_); | 1364 Log::MessageBuilder msg(log_); |
| 1356 msg.Append("active-runtime-timer,"); | 1365 msg.Append("active-runtime-timer,"); |
| 1357 msg.AppendDoubleQuotedString(counter->name); | 1366 msg.AppendDoubleQuotedString(counter->name); |
| 1358 msg.WriteToLogFile(); | 1367 msg.WriteToLogFile(); |
| 1359 } | 1368 } |
| 1360 | 1369 |
| 1361 void Logger::TickEvent(v8::TickSample* sample, bool overflow) { | 1370 void Logger::TickEvent(TickSample* sample, bool overflow) { |
| 1362 if (!log_->IsEnabled() || !FLAG_prof_cpp) return; | 1371 if (!log_->IsEnabled() || !FLAG_prof_cpp) return; |
| 1363 if (FLAG_runtime_call_stats) { | 1372 if (FLAG_runtime_call_stats) { |
| 1364 RuntimeCallTimerEvent(); | 1373 RuntimeCallTimerEvent(); |
| 1365 } | 1374 } |
| 1366 Log::MessageBuilder msg(log_); | 1375 Log::MessageBuilder msg(log_); |
| 1367 msg.Append("%s,", kLogEventsNames[CodeEventListener::TICK_EVENT]); | 1376 msg.Append("%s,", kLogEventsNames[CodeEventListener::TICK_EVENT]); |
| 1368 msg.AppendAddress(reinterpret_cast<Address>(sample->pc)); | 1377 msg.AppendAddress(reinterpret_cast<Address>(sample->pc)); |
| 1369 msg.Append(",%d", static_cast<int>(timer_.Elapsed().InMicroseconds())); | 1378 msg.Append(",%d", static_cast<int>(timer_.Elapsed().InMicroseconds())); |
| 1370 if (sample->has_external_callback) { | 1379 if (sample->has_external_callback) { |
| 1371 msg.Append(",1,"); | 1380 msg.Append(",1,"); |
| (...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1862 | 1871 |
| 1863 if (profiler_listener_.get() != nullptr) { | 1872 if (profiler_listener_.get() != nullptr) { |
| 1864 removeCodeEventListener(profiler_listener_.get()); | 1873 removeCodeEventListener(profiler_listener_.get()); |
| 1865 } | 1874 } |
| 1866 | 1875 |
| 1867 return log_->Close(); | 1876 return log_->Close(); |
| 1868 } | 1877 } |
| 1869 | 1878 |
| 1870 } // namespace internal | 1879 } // namespace internal |
| 1871 } // namespace v8 | 1880 } // namespace v8 |
| OLD | NEW |