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