| 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 766 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 777 | 777 |
| 778 pthread_t vm_tid() const { return vm_tid_; } | 778 pthread_t vm_tid() const { return vm_tid_; } |
| 779 | 779 |
| 780 private: | 780 private: |
| 781 pthread_t vm_tid_; | 781 pthread_t vm_tid_; |
| 782 }; | 782 }; |
| 783 | 783 |
| 784 | 784 |
| 785 class SignalSender : public Thread { | 785 class SignalSender : public Thread { |
| 786 public: | 786 public: |
| 787 enum SleepInterval { | |
| 788 HALF_INTERVAL, | |
| 789 FULL_INTERVAL | |
| 790 }; | |
| 791 | |
| 792 static const int kSignalSenderStackSize = 64 * KB; | 787 static const int kSignalSenderStackSize = 64 * KB; |
| 793 | 788 |
| 794 explicit SignalSender(int interval) | 789 explicit SignalSender(int interval) |
| 795 : Thread(Thread::Options("SignalSender", kSignalSenderStackSize)), | 790 : Thread(Thread::Options("SignalSender", kSignalSenderStackSize)), |
| 796 vm_tgid_(getpid()), | 791 vm_tgid_(getpid()), |
| 797 interval_(interval) {} | 792 interval_(interval) {} |
| 798 | 793 |
| 799 static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); } | 794 static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); } |
| 800 static void TearDown() { delete mutex_; } | 795 static void TearDown() { delete mutex_; } |
| 801 | 796 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 837 instance_ = NULL; | 832 instance_ = NULL; |
| 838 RestoreSignalHandler(); | 833 RestoreSignalHandler(); |
| 839 } | 834 } |
| 840 } | 835 } |
| 841 | 836 |
| 842 // Implement Thread::Run(). | 837 // Implement Thread::Run(). |
| 843 virtual void Run() { | 838 virtual void Run() { |
| 844 SamplerRegistry::State state; | 839 SamplerRegistry::State state; |
| 845 while ((state = SamplerRegistry::GetState()) != | 840 while ((state = SamplerRegistry::GetState()) != |
| 846 SamplerRegistry::HAS_NO_SAMPLERS) { | 841 SamplerRegistry::HAS_NO_SAMPLERS) { |
| 847 bool cpu_profiling_enabled = | |
| 848 (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS); | |
| 849 bool runtime_profiler_enabled = RuntimeProfiler::IsEnabled(); | |
| 850 if (cpu_profiling_enabled && !signal_handler_installed_) { | |
| 851 InstallSignalHandler(); | |
| 852 } else if (!cpu_profiling_enabled && signal_handler_installed_) { | |
| 853 RestoreSignalHandler(); | |
| 854 } | |
| 855 // When CPU profiling is enabled both JavaScript and C++ code is | 842 // When CPU profiling is enabled both JavaScript and C++ code is |
| 856 // profiled. We must not suspend. | 843 // profiled. We must not suspend. |
| 857 if (!cpu_profiling_enabled) { | 844 if (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS) { |
| 845 if (!signal_handler_installed_) InstallSignalHandler(); |
| 846 SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this); |
| 847 } else { |
| 848 if (signal_handler_installed_) RestoreSignalHandler(); |
| 858 if (rate_limiter_.SuspendIfNecessary()) continue; | 849 if (rate_limiter_.SuspendIfNecessary()) continue; |
| 859 } | 850 } |
| 860 if (cpu_profiling_enabled && runtime_profiler_enabled) { | 851 Sleep(); // TODO(svenpanne) Figure out if OS:Sleep(interval_) is enough. |
| 861 if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this)) { | |
| 862 return; | |
| 863 } | |
| 864 Sleep(HALF_INTERVAL); | |
| 865 if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, NULL)) { | |
| 866 return; | |
| 867 } | |
| 868 Sleep(HALF_INTERVAL); | |
| 869 } else { | |
| 870 if (cpu_profiling_enabled) { | |
| 871 if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, | |
| 872 this)) { | |
| 873 return; | |
| 874 } | |
| 875 } | |
| 876 if (runtime_profiler_enabled) { | |
| 877 if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, | |
| 878 NULL)) { | |
| 879 return; | |
| 880 } | |
| 881 } | |
| 882 Sleep(FULL_INTERVAL); | |
| 883 } | |
| 884 } | 852 } |
| 885 } | 853 } |
| 886 | 854 |
| 887 static void DoCpuProfile(Sampler* sampler, void* raw_sender) { | 855 static void DoCpuProfile(Sampler* sampler, void* raw_sender) { |
| 888 if (!sampler->IsProfiling()) return; | 856 if (!sampler->IsProfiling()) return; |
| 889 SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender); | 857 SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender); |
| 890 sender->SendProfilingSignal(sampler->platform_data()->vm_tid()); | 858 sender->SendProfilingSignal(sampler->platform_data()->vm_tid()); |
| 891 } | 859 } |
| 892 | 860 |
| 893 static void DoRuntimeProfile(Sampler* sampler, void* ignored) { | |
| 894 if (!sampler->isolate()->IsInitialized()) return; | |
| 895 sampler->isolate()->runtime_profiler()->NotifyTick(); | |
| 896 } | |
| 897 | |
| 898 void SendProfilingSignal(pthread_t tid) { | 861 void SendProfilingSignal(pthread_t tid) { |
| 899 if (!signal_handler_installed_) return; | 862 if (!signal_handler_installed_) return; |
| 900 pthread_kill(tid, SIGPROF); | 863 pthread_kill(tid, SIGPROF); |
| 901 } | 864 } |
| 902 | 865 |
| 903 void Sleep(SleepInterval full_or_half) { | 866 void Sleep() { |
| 904 // Convert ms to us and subtract 100 us to compensate delays | 867 // Convert ms to us and subtract 100 us to compensate delays |
| 905 // occuring during signal delivery. | 868 // occuring during signal delivery. |
| 906 useconds_t interval = interval_ * 1000 - 100; | 869 useconds_t interval = interval_ * 1000 - 100; |
| 907 if (full_or_half == HALF_INTERVAL) interval /= 2; | |
| 908 int result = usleep(interval); | 870 int result = usleep(interval); |
| 909 #ifdef DEBUG | 871 #ifdef DEBUG |
| 910 if (result != 0 && errno != EINTR) { | 872 if (result != 0 && errno != EINTR) { |
| 911 fprintf(stderr, | 873 fprintf(stderr, |
| 912 "SignalSender usleep error; interval = %u, errno = %d\n", | 874 "SignalSender usleep error; interval = %u, errno = %d\n", |
| 913 interval, | 875 interval, |
| 914 errno); | 876 errno); |
| 915 ASSERT(result == 0 || errno == EINTR); | 877 ASSERT(result == 0 || errno == EINTR); |
| 916 } | 878 } |
| 917 #endif | 879 #endif |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 978 | 940 |
| 979 | 941 |
| 980 void Sampler::Stop() { | 942 void Sampler::Stop() { |
| 981 ASSERT(IsActive()); | 943 ASSERT(IsActive()); |
| 982 SignalSender::RemoveActiveSampler(this); | 944 SignalSender::RemoveActiveSampler(this); |
| 983 SetActive(false); | 945 SetActive(false); |
| 984 } | 946 } |
| 985 | 947 |
| 986 | 948 |
| 987 } } // namespace v8::internal | 949 } } // namespace v8::internal |
| OLD | NEW |