Chromium Code Reviews| 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 1062 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1073 FULL_INTERVAL | 1073 FULL_INTERVAL |
| 1074 }; | 1074 }; |
| 1075 | 1075 |
| 1076 static const int kSignalSenderStackSize = 64 * KB; | 1076 static const int kSignalSenderStackSize = 64 * KB; |
| 1077 | 1077 |
| 1078 explicit SignalSender(int interval) | 1078 explicit SignalSender(int interval) |
| 1079 : Thread(Thread::Options("SignalSender", kSignalSenderStackSize)), | 1079 : Thread(Thread::Options("SignalSender", kSignalSenderStackSize)), |
| 1080 vm_tgid_(getpid()), | 1080 vm_tgid_(getpid()), |
| 1081 interval_(interval) {} | 1081 interval_(interval) {} |
| 1082 | 1082 |
| 1083 static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); } | 1083 static void SetUp() { |
| 1084 static void TearDown() { delete mutex_; } | 1084 if (!mutex_) mutex_ = OS::CreateMutex(); |
| 1085 InstallSignalHandler(); | |
|
caseq
2012/08/16 14:39:22
So SignalSender has nothing to do with CPU profili
| |
| 1086 } | |
| 1087 static void TearDown() { | |
| 1088 RestoreSignalHandler(); | |
| 1089 delete mutex_; | |
| 1090 } | |
| 1085 | 1091 |
| 1086 static void InstallSignalHandler() { | 1092 static void InstallSignalHandler() { |
| 1087 struct sigaction sa; | 1093 struct sigaction sa; |
| 1088 sa.sa_sigaction = ProfilerSignalHandler; | 1094 sa.sa_sigaction = ProfilerSignalHandler; |
| 1089 sigemptyset(&sa.sa_mask); | 1095 sigemptyset(&sa.sa_mask); |
| 1090 sa.sa_flags = SA_RESTART | SA_SIGINFO; | 1096 sa.sa_flags = SA_RESTART | SA_SIGINFO; |
| 1091 signal_handler_installed_ = | 1097 signal_handler_installed_ = |
| 1092 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); | 1098 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); |
| 1093 } | 1099 } |
| 1094 | 1100 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 1121 instance_ = NULL; | 1127 instance_ = NULL; |
| 1122 RestoreSignalHandler(); | 1128 RestoreSignalHandler(); |
| 1123 } | 1129 } |
| 1124 } | 1130 } |
| 1125 | 1131 |
| 1126 // Implement Thread::Run(). | 1132 // Implement Thread::Run(). |
| 1127 virtual void Run() { | 1133 virtual void Run() { |
| 1128 SamplerRegistry::State state; | 1134 SamplerRegistry::State state; |
| 1129 while ((state = SamplerRegistry::GetState()) != | 1135 while ((state = SamplerRegistry::GetState()) != |
| 1130 SamplerRegistry::HAS_NO_SAMPLERS) { | 1136 SamplerRegistry::HAS_NO_SAMPLERS) { |
| 1131 bool cpu_profiling_enabled = | |
| 1132 (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS); | |
| 1133 bool runtime_profiler_enabled = RuntimeProfiler::IsEnabled(); | 1137 bool runtime_profiler_enabled = RuntimeProfiler::IsEnabled(); |
| 1134 if (cpu_profiling_enabled && !signal_handler_installed_) { | |
| 1135 InstallSignalHandler(); | |
| 1136 } else if (!cpu_profiling_enabled && signal_handler_installed_) { | |
| 1137 RestoreSignalHandler(); | |
| 1138 } | |
| 1139 // When CPU profiling is enabled both JavaScript and C++ code is | 1138 // When CPU profiling is enabled both JavaScript and C++ code is |
| 1140 // profiled. We must not suspend. | 1139 // profiled. We must not suspend. |
| 1141 if (!cpu_profiling_enabled) { | 1140 if (rate_limiter_.SuspendIfNecessary()) continue; |
| 1142 if (rate_limiter_.SuspendIfNecessary()) continue; | 1141 if (runtime_profiler_enabled) { |
| 1143 } | |
| 1144 if (cpu_profiling_enabled && runtime_profiler_enabled) { | |
| 1145 if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this)) { | |
| 1146 return; | |
| 1147 } | |
| 1148 Sleep(HALF_INTERVAL); | |
| 1149 if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, NULL)) { | 1142 if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, NULL)) { |
| 1150 return; | 1143 return; |
| 1151 } | 1144 } |
| 1152 Sleep(HALF_INTERVAL); | |
| 1153 } else { | |
| 1154 if (cpu_profiling_enabled) { | |
| 1155 if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, | |
| 1156 this)) { | |
| 1157 return; | |
| 1158 } | |
| 1159 } | |
| 1160 if (runtime_profiler_enabled) { | |
| 1161 if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, | |
| 1162 NULL)) { | |
| 1163 return; | |
| 1164 } | |
| 1165 } | |
| 1166 Sleep(FULL_INTERVAL); | |
| 1167 } | 1145 } |
| 1146 Sleep(FULL_INTERVAL); | |
| 1168 } | 1147 } |
| 1169 } | 1148 } |
| 1170 | 1149 |
| 1171 static void DoCpuProfile(Sampler* sampler, void* raw_sender) { | |
| 1172 if (!sampler->IsProfiling()) return; | |
| 1173 SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender); | |
| 1174 sender->SendProfilingSignal(sampler->platform_data()->vm_tid()); | |
| 1175 } | |
| 1176 | |
| 1177 static void DoRuntimeProfile(Sampler* sampler, void* ignored) { | 1150 static void DoRuntimeProfile(Sampler* sampler, void* ignored) { |
| 1178 if (!sampler->isolate()->IsInitialized()) return; | 1151 if (!sampler->isolate()->IsInitialized()) return; |
| 1179 sampler->isolate()->runtime_profiler()->NotifyTick(); | 1152 sampler->isolate()->runtime_profiler()->NotifyTick(); |
| 1180 } | 1153 } |
| 1181 | 1154 |
| 1182 void SendProfilingSignal(int tid) { | |
| 1183 if (!signal_handler_installed_) return; | |
| 1184 // Glibc doesn't provide a wrapper for tgkill(2). | |
| 1185 #if defined(ANDROID) | |
| 1186 syscall(__NR_tgkill, vm_tgid_, tid, SIGPROF); | |
| 1187 #else | |
| 1188 syscall(SYS_tgkill, vm_tgid_, tid, SIGPROF); | |
| 1189 #endif | |
| 1190 } | |
| 1191 | |
| 1192 void Sleep(SleepInterval full_or_half) { | 1155 void Sleep(SleepInterval full_or_half) { |
| 1193 // Convert ms to us and subtract 100 us to compensate delays | 1156 // Convert ms to us and subtract 100 us to compensate delays |
| 1194 // occuring during signal delivery. | 1157 // occuring during signal delivery. |
| 1195 useconds_t interval = interval_ * 1000 - 100; | 1158 useconds_t interval = interval_ * 1000 - 100; |
| 1196 if (full_or_half == HALF_INTERVAL) interval /= 2; | 1159 if (full_or_half == HALF_INTERVAL) interval /= 2; |
| 1197 #if defined(ANDROID) | 1160 #if defined(ANDROID) |
| 1198 usleep(interval); | 1161 usleep(interval); |
| 1199 #else | 1162 #else |
| 1200 int result = usleep(interval); | 1163 int result = usleep(interval); |
| 1201 #ifdef DEBUG | 1164 #ifdef DEBUG |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1289 } | 1252 } |
| 1290 | 1253 |
| 1291 | 1254 |
| 1292 void Sampler::Stop() { | 1255 void Sampler::Stop() { |
| 1293 ASSERT(IsActive()); | 1256 ASSERT(IsActive()); |
| 1294 SignalSender::RemoveActiveSampler(this); | 1257 SignalSender::RemoveActiveSampler(this); |
| 1295 SetActive(false); | 1258 SetActive(false); |
| 1296 } | 1259 } |
| 1297 | 1260 |
| 1298 | 1261 |
| 1262 CpuProfilerThread::CpuProfilerThread(Sampler* sampler) | |
| 1263 : Thread(Thread::Options("CpuProfiler", kCpuProfilerThreadStackSize)), | |
| 1264 vm_tgid_(getpid()), | |
| 1265 sampler_(sampler) { | |
| 1266 } | |
| 1267 | |
| 1268 | |
| 1269 void CpuProfilerThread::DoCpuProfile() { | |
|
caseq
2012/08/16 14:39:22
DoCpuProfile -> DoSample?
| |
| 1270 int tid = sampler_->platform_data()->vm_tid(); | |
| 1271 if (!SignalSender::signal_handler_installed_) return; | |
| 1272 // Glibc doesn't provide a wrapper for tgkill(2). | |
| 1273 #if defined(ANDROID) | |
| 1274 syscall(__NR_tgkill, vm_tgid_, tid, SIGPROF); | |
| 1275 #else | |
| 1276 syscall(SYS_tgkill, vm_tgid_, tid, SIGPROF); | |
| 1277 #endif | |
| 1278 } | |
| 1279 | |
| 1280 | |
| 1299 } } // namespace v8::internal | 1281 } } // namespace v8::internal |
| OLD | NEW |