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 683 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
694 | 694 |
695 pthread_t vm_tid() const { return vm_tid_; } | 695 pthread_t vm_tid() const { return vm_tid_; } |
696 | 696 |
697 private: | 697 private: |
698 pthread_t vm_tid_; | 698 pthread_t vm_tid_; |
699 }; | 699 }; |
700 | 700 |
701 | 701 |
702 class SignalSender : public Thread { | 702 class SignalSender : public Thread { |
703 public: | 703 public: |
704 enum SleepInterval { | |
705 HALF_INTERVAL, | |
706 FULL_INTERVAL | |
707 }; | |
708 | |
709 static const int kSignalSenderStackSize = 64 * KB; | 704 static const int kSignalSenderStackSize = 64 * KB; |
710 | 705 |
711 explicit SignalSender(int interval) | 706 explicit SignalSender(int interval) |
712 : Thread(Thread::Options("SignalSender", kSignalSenderStackSize)), | 707 : Thread(Thread::Options("SignalSender", kSignalSenderStackSize)), |
713 interval_(interval) {} | 708 interval_(interval) {} |
714 | 709 |
715 static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); } | 710 static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); } |
716 static void TearDown() { delete mutex_; } | 711 static void TearDown() { delete mutex_; } |
717 | 712 |
718 static void InstallSignalHandler() { | 713 static void InstallSignalHandler() { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
753 instance_ = NULL; | 748 instance_ = NULL; |
754 RestoreSignalHandler(); | 749 RestoreSignalHandler(); |
755 } | 750 } |
756 } | 751 } |
757 | 752 |
758 // Implement Thread::Run(). | 753 // Implement Thread::Run(). |
759 virtual void Run() { | 754 virtual void Run() { |
760 SamplerRegistry::State state; | 755 SamplerRegistry::State state; |
761 while ((state = SamplerRegistry::GetState()) != | 756 while ((state = SamplerRegistry::GetState()) != |
762 SamplerRegistry::HAS_NO_SAMPLERS) { | 757 SamplerRegistry::HAS_NO_SAMPLERS) { |
763 bool cpu_profiling_enabled = | |
764 (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS); | |
765 bool runtime_profiler_enabled = RuntimeProfiler::IsEnabled(); | |
766 if (cpu_profiling_enabled && !signal_handler_installed_) { | |
767 InstallSignalHandler(); | |
768 } else if (!cpu_profiling_enabled && signal_handler_installed_) { | |
769 RestoreSignalHandler(); | |
770 } | |
771 | |
772 // When CPU profiling is enabled both JavaScript and C++ code is | 758 // When CPU profiling is enabled both JavaScript and C++ code is |
773 // profiled. We must not suspend. | 759 // profiled. We must not suspend. |
774 if (!cpu_profiling_enabled) { | 760 if (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS) { |
| 761 if (!signal_handler_installed_) InstallSignalHandler(); |
| 762 SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this); |
| 763 } else { |
| 764 if (signal_handler_installed_) RestoreSignalHandler(); |
775 if (rate_limiter_.SuspendIfNecessary()) continue; | 765 if (rate_limiter_.SuspendIfNecessary()) continue; |
776 } | 766 } |
777 if (cpu_profiling_enabled && runtime_profiler_enabled) { | 767 Sleep(); // TODO(svenpanne) Figure out if OS:Sleep(interval_) is enough. |
778 if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this)) { | |
779 return; | |
780 } | |
781 Sleep(HALF_INTERVAL); | |
782 if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, NULL)) { | |
783 return; | |
784 } | |
785 Sleep(HALF_INTERVAL); | |
786 } else { | |
787 if (cpu_profiling_enabled) { | |
788 if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, | |
789 this)) { | |
790 return; | |
791 } | |
792 } | |
793 if (runtime_profiler_enabled) { | |
794 if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, | |
795 NULL)) { | |
796 return; | |
797 } | |
798 } | |
799 Sleep(FULL_INTERVAL); | |
800 } | |
801 } | 768 } |
802 } | 769 } |
803 | 770 |
804 static void DoCpuProfile(Sampler* sampler, void* raw_sender) { | 771 static void DoCpuProfile(Sampler* sampler, void* raw_sender) { |
805 if (!sampler->IsProfiling()) return; | 772 if (!sampler->IsProfiling()) return; |
806 SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender); | 773 SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender); |
807 sender->SendProfilingSignal(sampler->platform_data()->vm_tid()); | 774 sender->SendProfilingSignal(sampler->platform_data()->vm_tid()); |
808 } | 775 } |
809 | 776 |
810 static void DoRuntimeProfile(Sampler* sampler, void* ignored) { | |
811 if (!sampler->isolate()->IsInitialized()) return; | |
812 sampler->isolate()->runtime_profiler()->NotifyTick(); | |
813 } | |
814 | |
815 void SendProfilingSignal(pthread_t tid) { | 777 void SendProfilingSignal(pthread_t tid) { |
816 if (!signal_handler_installed_) return; | 778 if (!signal_handler_installed_) return; |
817 pthread_kill(tid, SIGPROF); | 779 pthread_kill(tid, SIGPROF); |
818 } | 780 } |
819 | 781 |
820 void Sleep(SleepInterval full_or_half) { | 782 void Sleep() { |
821 // Convert ms to us and subtract 100 us to compensate delays | 783 // Convert ms to us and subtract 100 us to compensate delays |
822 // occuring during signal delivery. | 784 // occuring during signal delivery. |
823 useconds_t interval = interval_ * 1000 - 100; | 785 useconds_t interval = interval_ * 1000 - 100; |
824 if (full_or_half == HALF_INTERVAL) interval /= 2; | |
825 int result = usleep(interval); | 786 int result = usleep(interval); |
826 #ifdef DEBUG | 787 #ifdef DEBUG |
827 if (result != 0 && errno != EINTR) { | 788 if (result != 0 && errno != EINTR) { |
828 fprintf(stderr, | 789 fprintf(stderr, |
829 "SignalSender usleep error; interval = %u, errno = %d\n", | 790 "SignalSender usleep error; interval = %u, errno = %d\n", |
830 interval, | 791 interval, |
831 errno); | 792 errno); |
832 ASSERT(result == 0 || errno == EINTR); | 793 ASSERT(result == 0 || errno == EINTR); |
833 } | 794 } |
834 #endif | 795 #endif |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
896 } | 857 } |
897 | 858 |
898 | 859 |
899 void Sampler::Stop() { | 860 void Sampler::Stop() { |
900 ASSERT(IsActive()); | 861 ASSERT(IsActive()); |
901 SignalSender::RemoveActiveSampler(this); | 862 SignalSender::RemoveActiveSampler(this); |
902 SetActive(false); | 863 SetActive(false); |
903 } | 864 } |
904 | 865 |
905 } } // namespace v8::internal | 866 } } // namespace v8::internal |
OLD | NEW |