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 "src/bailout-reason.h" | 10 #include "src/bailout-reason.h" |
11 #include "src/base/platform/platform.h" | 11 #include "src/base/platform/platform.h" |
12 #include "src/bootstrapper.h" | 12 #include "src/bootstrapper.h" |
13 #include "src/code-stubs.h" | 13 #include "src/code-stubs.h" |
14 #include "src/counters.h" | 14 #include "src/counters.h" |
15 #include "src/deoptimizer.h" | 15 #include "src/deoptimizer.h" |
16 #include "src/global-handles.h" | 16 #include "src/global-handles.h" |
17 #include "src/interpreter/bytecodes.h" | 17 #include "src/interpreter/bytecodes.h" |
18 #include "src/interpreter/interpreter.h" | 18 #include "src/interpreter/interpreter.h" |
19 #include "src/libsampler/sampler.h" | 19 #include "src/libsampler/sampler.h" |
20 #include "src/log-inl.h" | 20 #include "src/log-inl.h" |
21 #include "src/log-utils.h" | 21 #include "src/log-utils.h" |
22 #include "src/macro-assembler.h" | 22 #include "src/macro-assembler.h" |
23 #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" | 24 #include "src/profiler/profiler-listener.h" |
26 #include "src/profiler/tick-sample.h" | 25 #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. |
609 base::Atomic32 running_; | 607 base::Atomic32 running_; |
610 | 608 |
611 // Tells whether we are currently recording tick samples. | 609 // Tells whether we are currently recording tick samples. |
612 bool paused_; | 610 bool paused_; |
613 }; | 611 }; |
614 | 612 |
615 | 613 |
616 // | 614 // |
617 // Ticker used to provide ticks to the profiler and the sliding state | 615 // Ticker used to provide ticks to the profiler and the sliding state |
618 // window. | 616 // window. |
619 // | 617 // |
620 class Ticker: public sampler::Sampler { | 618 class Ticker: public sampler::Sampler { |
621 public: | 619 public: |
622 Ticker(Isolate* isolate, int interval): | 620 Ticker(Isolate* isolate, int interval) |
623 sampler::Sampler(reinterpret_cast<v8::Isolate*>(isolate)), | 621 : sampler::Sampler(reinterpret_cast<v8::Isolate*>(isolate)), |
624 profiler_(NULL), | 622 profiler_(nullptr), |
625 sampling_thread_(new SamplingThread(this, interval)) {} | 623 sampling_thread_(new SamplingThread(this, interval)) {} |
626 | 624 |
627 ~Ticker() { | 625 ~Ticker() { |
628 if (IsActive()) Stop(); | 626 if (IsActive()) Stop(); |
629 delete sampling_thread_; | 627 delete sampling_thread_; |
630 } | 628 } |
631 | 629 |
632 void SetProfiler(Profiler* profiler) { | 630 void SetProfiler(Profiler* profiler) { |
633 DCHECK(profiler_ == NULL); | 631 DCHECK(profiler_ == nullptr); |
634 profiler_ = profiler; | 632 profiler_ = profiler; |
635 IncreaseProfilingDepth(); | 633 IncreaseProfilingDepth(); |
636 if (!IsActive()) Start(); | 634 if (!IsActive()) Start(); |
637 sampling_thread_->StartSynchronously(); | 635 sampling_thread_->StartSynchronously(); |
638 } | 636 } |
639 | 637 |
640 void ClearProfiler() { | 638 void ClearProfiler() { |
641 profiler_ = NULL; | 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 v8::Isolate* v8_isolate = isolate(); | |
650 Isolate* i_isolate = reinterpret_cast<Isolate*>(v8_isolate); | |
651 #if defined(USE_SIMULATOR) | 647 #if defined(USE_SIMULATOR) |
652 if (!SimulatorHelper::FillRegisters(i_isolate, | 648 Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate()); |
653 const_cast<v8::RegisterState*>(&state))) | 649 v8::RegisterState regs; |
654 return; | 650 if (!SimulatorHelper::FillRegisters(i_isolate, ®s)) return; |
| 651 #else |
| 652 const v8::RegisterState& regs = state; |
655 #endif | 653 #endif |
656 TickSample sample; | 654 v8::TickSample sample; |
657 sample.Init(i_isolate, state, TickSample::kIncludeCEntryFrame, true); | 655 sample.Init(isolate(), regs, v8::TickSample::kIncludeCEntryFrame, true); |
658 profiler_->Insert(&sample); | 656 profiler_->Insert(&sample); |
659 } | 657 } |
660 | 658 |
661 private: | 659 private: |
662 Profiler* profiler_; | 660 Profiler* profiler_; |
663 SamplingThread* sampling_thread_; | 661 SamplingThread* sampling_thread_; |
664 }; | 662 }; |
665 | 663 |
666 | 664 |
667 // | 665 // |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
707 void Profiler::Disengage() { | 705 void Profiler::Disengage() { |
708 if (!engaged_) return; | 706 if (!engaged_) return; |
709 | 707 |
710 // Stop receiving ticks. | 708 // Stop receiving ticks. |
711 isolate_->logger()->ticker_->ClearProfiler(); | 709 isolate_->logger()->ticker_->ClearProfiler(); |
712 | 710 |
713 // Terminate the worker thread by setting running_ to false, | 711 // Terminate the worker thread by setting running_ to false, |
714 // inserting a fake element in the queue and then wait for | 712 // inserting a fake element in the queue and then wait for |
715 // the thread to terminate. | 713 // the thread to terminate. |
716 base::NoBarrier_Store(&running_, 0); | 714 base::NoBarrier_Store(&running_, 0); |
717 TickSample sample; | 715 v8::TickSample sample; |
718 // Reset 'paused_' flag, otherwise semaphore may not be signalled. | 716 // Reset 'paused_' flag, otherwise semaphore may not be signalled. |
719 resume(); | 717 resume(); |
720 Insert(&sample); | 718 Insert(&sample); |
721 Join(); | 719 Join(); |
722 | 720 |
723 LOG(isolate_, UncheckedStringEvent("profiler", "end")); | 721 LOG(isolate_, UncheckedStringEvent("profiler", "end")); |
724 } | 722 } |
725 | 723 |
726 | 724 |
727 void Profiler::Run() { | 725 void Profiler::Run() { |
728 TickSample sample; | 726 v8::TickSample sample; |
729 bool overflow = Remove(&sample); | 727 bool overflow = Remove(&sample); |
730 while (base::NoBarrier_Load(&running_)) { | 728 while (base::NoBarrier_Load(&running_)) { |
731 LOG(isolate_, TickEvent(&sample, overflow)); | 729 LOG(isolate_, TickEvent(&sample, overflow)); |
732 overflow = Remove(&sample); | 730 overflow = Remove(&sample); |
733 } | 731 } |
734 } | 732 } |
735 | 733 |
736 | 734 |
737 // | 735 // |
738 // Logger class implementation. | 736 // Logger class implementation. |
(...skipping 621 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1360 RuntimeCallTimer* timer = stats->current_timer(); | 1358 RuntimeCallTimer* timer = stats->current_timer(); |
1361 if (timer == nullptr) return; | 1359 if (timer == nullptr) return; |
1362 RuntimeCallCounter* counter = timer->counter(); | 1360 RuntimeCallCounter* counter = timer->counter(); |
1363 if (counter == nullptr) return; | 1361 if (counter == nullptr) return; |
1364 Log::MessageBuilder msg(log_); | 1362 Log::MessageBuilder msg(log_); |
1365 msg.Append("active-runtime-timer,"); | 1363 msg.Append("active-runtime-timer,"); |
1366 msg.AppendDoubleQuotedString(counter->name); | 1364 msg.AppendDoubleQuotedString(counter->name); |
1367 msg.WriteToLogFile(); | 1365 msg.WriteToLogFile(); |
1368 } | 1366 } |
1369 | 1367 |
1370 void Logger::TickEvent(TickSample* sample, bool overflow) { | 1368 void Logger::TickEvent(v8::TickSample* sample, bool overflow) { |
1371 if (!log_->IsEnabled() || !FLAG_prof_cpp) return; | 1369 if (!log_->IsEnabled() || !FLAG_prof_cpp) return; |
1372 if (FLAG_runtime_call_stats) { | 1370 if (FLAG_runtime_call_stats) { |
1373 RuntimeCallTimerEvent(); | 1371 RuntimeCallTimerEvent(); |
1374 } | 1372 } |
1375 Log::MessageBuilder msg(log_); | 1373 Log::MessageBuilder msg(log_); |
1376 msg.Append("%s,", kLogEventsNames[CodeEventListener::TICK_EVENT]); | 1374 msg.Append("%s,", kLogEventsNames[CodeEventListener::TICK_EVENT]); |
1377 msg.AppendAddress(reinterpret_cast<Address>(sample->pc)); | 1375 msg.AppendAddress(reinterpret_cast<Address>(sample->pc)); |
1378 msg.Append(",%d", static_cast<int>(timer_.Elapsed().InMicroseconds())); | 1376 msg.Append(",%d", static_cast<int>(timer_.Elapsed().InMicroseconds())); |
1379 if (sample->has_external_callback) { | 1377 if (sample->has_external_callback) { |
1380 msg.Append(",1,"); | 1378 msg.Append(",1,"); |
(...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1871 | 1869 |
1872 if (profiler_listener_.get() != nullptr) { | 1870 if (profiler_listener_.get() != nullptr) { |
1873 removeCodeEventListener(profiler_listener_.get()); | 1871 removeCodeEventListener(profiler_listener_.get()); |
1874 } | 1872 } |
1875 | 1873 |
1876 return log_->Close(); | 1874 return log_->Close(); |
1877 } | 1875 } |
1878 | 1876 |
1879 } // namespace internal | 1877 } // namespace internal |
1880 } // namespace v8 | 1878 } // namespace v8 |
OLD | NEW |