OLD | NEW |
---|---|
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 961 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
972 enum SleepInterval { | 972 enum SleepInterval { |
973 HALF_INTERVAL, | 973 HALF_INTERVAL, |
974 FULL_INTERVAL | 974 FULL_INTERVAL |
975 }; | 975 }; |
976 | 976 |
977 explicit SignalSender(int interval) | 977 explicit SignalSender(int interval) |
978 : Thread(NULL, "SignalSender"), | 978 : Thread(NULL, "SignalSender"), |
979 vm_tgid_(getpid()), | 979 vm_tgid_(getpid()), |
980 interval_(interval) {} | 980 interval_(interval) {} |
981 | 981 |
982 static void InstallSignalHandler() { | |
983 struct sigaction sa; | |
984 sa.sa_sigaction = ProfilerSignalHandler; | |
985 sigemptyset(&sa.sa_mask); | |
986 sa.sa_flags = SA_RESTART | SA_SIGINFO; | |
987 signal_handler_installed_ = | |
988 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); | |
989 } | |
990 | |
991 static void RestoreSignalHandler() { | |
992 if (signal_handler_installed_) { | |
993 sigaction(SIGPROF, &old_signal_handler_, 0); | |
994 signal_handler_installed_ = false; | |
995 } | |
996 } | |
997 | |
982 static void AddActiveSampler(Sampler* sampler) { | 998 static void AddActiveSampler(Sampler* sampler) { |
983 ScopedLock lock(mutex_); | 999 ScopedLock lock(mutex_); |
984 SamplerRegistry::AddActiveSampler(sampler); | 1000 SamplerRegistry::AddActiveSampler(sampler); |
985 if (instance_ == NULL) { | 1001 if (instance_ == NULL) { |
986 // Install a signal handler. | 1002 // Start a thread that will send SIGPROF signal to VM threads, |
987 struct sigaction sa; | 1003 // when CPU profiling will be enabled. |
988 sa.sa_sigaction = ProfilerSignalHandler; | |
989 sigemptyset(&sa.sa_mask); | |
990 sa.sa_flags = SA_RESTART | SA_SIGINFO; | |
991 signal_handler_installed_ = | |
992 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); | |
993 | |
994 // Start a thread that sends SIGPROF signal to VM threads. | |
995 instance_ = new SignalSender(sampler->interval()); | 1004 instance_ = new SignalSender(sampler->interval()); |
996 instance_->Start(); | 1005 instance_->Start(); |
997 } else { | 1006 } else { |
998 ASSERT(instance_->interval_ == sampler->interval()); | 1007 ASSERT(instance_->interval_ == sampler->interval()); |
999 } | 1008 } |
1000 } | 1009 } |
1001 | 1010 |
1002 static void RemoveActiveSampler(Sampler* sampler) { | 1011 static void RemoveActiveSampler(Sampler* sampler) { |
1003 ScopedLock lock(mutex_); | 1012 ScopedLock lock(mutex_); |
1004 SamplerRegistry::RemoveActiveSampler(sampler); | 1013 SamplerRegistry::RemoveActiveSampler(sampler); |
1005 if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) { | 1014 if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) { |
1006 RuntimeProfiler::WakeUpRuntimeProfilerThreadBeforeShutdown(); | 1015 RuntimeProfiler::WakeUpRuntimeProfilerThreadBeforeShutdown(); |
1007 instance_->Join(); | 1016 instance_->Join(); |
1008 delete instance_; | 1017 delete instance_; |
1009 instance_ = NULL; | 1018 instance_ = NULL; |
1010 | 1019 RestoreSignalHandler(); |
1011 // Restore the old signal handler. | |
1012 if (signal_handler_installed_) { | |
1013 sigaction(SIGPROF, &old_signal_handler_, 0); | |
1014 signal_handler_installed_ = false; | |
1015 } | |
1016 } | 1020 } |
1017 } | 1021 } |
1018 | 1022 |
1019 // Implement Thread::Run(). | 1023 // Implement Thread::Run(). |
1020 virtual void Run() { | 1024 virtual void Run() { |
1021 SamplerRegistry::State state; | 1025 SamplerRegistry::State state; |
1022 while ((state = SamplerRegistry::GetState()) != | 1026 while ((state = SamplerRegistry::GetState()) != |
1023 SamplerRegistry::HAS_NO_SAMPLERS) { | 1027 SamplerRegistry::HAS_NO_SAMPLERS) { |
1024 bool cpu_profiling_enabled = | 1028 bool cpu_profiling_enabled = |
1025 (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS); | 1029 (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS); |
1026 bool runtime_profiler_enabled = RuntimeProfiler::IsEnabled(); | 1030 bool runtime_profiler_enabled = RuntimeProfiler::IsEnabled(); |
1031 if (cpu_profiling_enabled && !signal_handler_installed_) | |
Mads Ager (chromium)
2011/06/02 10:26:55
Drive-by: please use braces around the bodies of i
| |
1032 InstallSignalHandler(); | |
1033 else if (!cpu_profiling_enabled && signal_handler_installed_) | |
1034 RestoreSignalHandler(); | |
1027 // When CPU profiling is enabled both JavaScript and C++ code is | 1035 // When CPU profiling is enabled both JavaScript and C++ code is |
1028 // profiled. We must not suspend. | 1036 // profiled. We must not suspend. |
1029 if (!cpu_profiling_enabled) { | 1037 if (!cpu_profiling_enabled) { |
1030 if (rate_limiter_.SuspendIfNecessary()) continue; | 1038 if (rate_limiter_.SuspendIfNecessary()) continue; |
1031 } | 1039 } |
1032 if (cpu_profiling_enabled && runtime_profiler_enabled) { | 1040 if (cpu_profiling_enabled && runtime_profiler_enabled) { |
1033 if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this)) { | 1041 if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this)) { |
1034 return; | 1042 return; |
1035 } | 1043 } |
1036 Sleep(HALF_INTERVAL); | 1044 Sleep(HALF_INTERVAL); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1140 | 1148 |
1141 void Sampler::Stop() { | 1149 void Sampler::Stop() { |
1142 ASSERT(IsActive()); | 1150 ASSERT(IsActive()); |
1143 SignalSender::RemoveActiveSampler(this); | 1151 SignalSender::RemoveActiveSampler(this); |
1144 SetActive(false); | 1152 SetActive(false); |
1145 } | 1153 } |
1146 | 1154 |
1147 #endif // ENABLE_LOGGING_AND_PROFILING | 1155 #endif // ENABLE_LOGGING_AND_PROFILING |
1148 | 1156 |
1149 } } // namespace v8::internal | 1157 } } // namespace v8::internal |
OLD | NEW |