| 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 1037 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1048 #elif V8_HOST_ARCH_MIPS | 1048 #elif V8_HOST_ARCH_MIPS |
| 1049 sample->pc = reinterpret_cast<Address>(mcontext.pc); | 1049 sample->pc = reinterpret_cast<Address>(mcontext.pc); |
| 1050 sample->sp = reinterpret_cast<Address>(mcontext.gregs[29]); | 1050 sample->sp = reinterpret_cast<Address>(mcontext.gregs[29]); |
| 1051 sample->fp = reinterpret_cast<Address>(mcontext.gregs[30]); | 1051 sample->fp = reinterpret_cast<Address>(mcontext.gregs[30]); |
| 1052 #endif // V8_HOST_ARCH_* | 1052 #endif // V8_HOST_ARCH_* |
| 1053 sampler->SampleStack(sample); | 1053 sampler->SampleStack(sample); |
| 1054 sampler->Tick(sample); | 1054 sampler->Tick(sample); |
| 1055 } | 1055 } |
| 1056 | 1056 |
| 1057 | 1057 |
| 1058 class CpuProfilerSignalHandler { |
| 1059 public: |
| 1060 static void InstallSignalHandler() { |
| 1061 if (signal_handler_installed_) return; |
| 1062 struct sigaction sa; |
| 1063 sa.sa_sigaction = ProfilerSignalHandler; |
| 1064 sigemptyset(&sa.sa_mask); |
| 1065 sa.sa_flags = SA_RESTART | SA_SIGINFO; |
| 1066 signal_handler_installed_ = |
| 1067 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); |
| 1068 } |
| 1069 |
| 1070 static void RestoreSignalHandler() { |
| 1071 if (signal_handler_installed_) { |
| 1072 sigaction(SIGPROF, &old_signal_handler_, 0); |
| 1073 signal_handler_installed_ = false; |
| 1074 } |
| 1075 } |
| 1076 |
| 1077 static bool signal_handler_installed() { return signal_handler_installed_; } |
| 1078 |
| 1079 private: |
| 1080 static bool signal_handler_installed_; |
| 1081 static struct sigaction old_signal_handler_; |
| 1082 }; |
| 1083 |
| 1084 |
| 1085 bool CpuProfilerSignalHandler::signal_handler_installed_ = false; |
| 1086 struct sigaction CpuProfilerSignalHandler::old_signal_handler_; |
| 1087 |
| 1088 |
| 1058 class Sampler::PlatformData : public Malloced { | 1089 class Sampler::PlatformData : public Malloced { |
| 1059 public: | 1090 public: |
| 1060 PlatformData() : vm_tid_(GetThreadID()) {} | 1091 PlatformData() |
| 1092 : vm_tgid_(getpid()), |
| 1093 vm_tid_(GetThreadID()) {} |
| 1061 | 1094 |
| 1095 int vm_tgid() const { return vm_tgid_; } |
| 1062 int vm_tid() const { return vm_tid_; } | 1096 int vm_tid() const { return vm_tid_; } |
| 1063 | 1097 |
| 1064 private: | 1098 private: |
| 1099 const int vm_tgid_; |
| 1065 const int vm_tid_; | 1100 const int vm_tid_; |
| 1066 }; | 1101 }; |
| 1067 | 1102 |
| 1068 | 1103 |
| 1069 class SignalSender : public Thread { | 1104 class SignalSender : public Thread { |
| 1070 public: | 1105 public: |
| 1071 enum SleepInterval { | 1106 enum SleepInterval { |
| 1072 HALF_INTERVAL, | 1107 HALF_INTERVAL, |
| 1073 FULL_INTERVAL | 1108 FULL_INTERVAL |
| 1074 }; | 1109 }; |
| 1075 | 1110 |
| 1076 static const int kSignalSenderStackSize = 64 * KB; | 1111 static const int kSignalSenderStackSize = 64 * KB; |
| 1077 | 1112 |
| 1078 explicit SignalSender(int interval) | 1113 explicit SignalSender(int interval) |
| 1079 : Thread(Thread::Options("SignalSender", kSignalSenderStackSize)), | 1114 : Thread(Thread::Options("SignalSender", kSignalSenderStackSize)), |
| 1080 vm_tgid_(getpid()), | |
| 1081 interval_(interval) {} | 1115 interval_(interval) {} |
| 1082 | 1116 |
| 1083 static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); } | 1117 static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); } |
| 1084 static void TearDown() { delete mutex_; } | 1118 static void TearDown() { delete mutex_; } |
| 1085 | 1119 |
| 1086 static void InstallSignalHandler() { | |
| 1087 struct sigaction sa; | |
| 1088 sa.sa_sigaction = ProfilerSignalHandler; | |
| 1089 sigemptyset(&sa.sa_mask); | |
| 1090 sa.sa_flags = SA_RESTART | SA_SIGINFO; | |
| 1091 signal_handler_installed_ = | |
| 1092 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); | |
| 1093 } | |
| 1094 | |
| 1095 static void RestoreSignalHandler() { | |
| 1096 if (signal_handler_installed_) { | |
| 1097 sigaction(SIGPROF, &old_signal_handler_, 0); | |
| 1098 signal_handler_installed_ = false; | |
| 1099 } | |
| 1100 } | |
| 1101 | |
| 1102 static void AddActiveSampler(Sampler* sampler) { | 1120 static void AddActiveSampler(Sampler* sampler) { |
| 1103 ScopedLock lock(mutex_); | 1121 ScopedLock lock(mutex_); |
| 1104 SamplerRegistry::AddActiveSampler(sampler); | 1122 SamplerRegistry::AddActiveSampler(sampler); |
| 1105 if (instance_ == NULL) { | 1123 if (instance_ == NULL) { |
| 1106 // Start a thread that will send SIGPROF signal to VM threads, | 1124 // Start a thread that will send SIGPROF signal to VM threads, |
| 1107 // when CPU profiling will be enabled. | 1125 // when CPU profiling will be enabled. |
| 1108 instance_ = new SignalSender(sampler->interval()); | 1126 instance_ = new SignalSender(sampler->interval()); |
| 1109 instance_->Start(); | 1127 instance_->Start(); |
| 1110 } else { | 1128 } else { |
| 1111 ASSERT(instance_->interval_ == sampler->interval()); | 1129 ASSERT(instance_->interval_ == sampler->interval()); |
| 1112 } | 1130 } |
| 1113 } | 1131 } |
| 1114 | 1132 |
| 1115 static void RemoveActiveSampler(Sampler* sampler) { | 1133 static void RemoveActiveSampler(Sampler* sampler) { |
| 1116 ScopedLock lock(mutex_); | 1134 ScopedLock lock(mutex_); |
| 1117 SamplerRegistry::RemoveActiveSampler(sampler); | 1135 SamplerRegistry::RemoveActiveSampler(sampler); |
| 1118 if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) { | 1136 if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) { |
| 1119 RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_); | 1137 RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_); |
| 1120 delete instance_; | 1138 delete instance_; |
| 1121 instance_ = NULL; | 1139 instance_ = NULL; |
| 1122 RestoreSignalHandler(); | |
| 1123 } | 1140 } |
| 1124 } | 1141 } |
| 1125 | 1142 |
| 1126 // Implement Thread::Run(). | 1143 // Implement Thread::Run(). |
| 1127 virtual void Run() { | 1144 virtual void Run() { |
| 1128 SamplerRegistry::State state; | 1145 SamplerRegistry::State state; |
| 1129 while ((state = SamplerRegistry::GetState()) != | 1146 while ((state = SamplerRegistry::GetState()) != |
| 1130 SamplerRegistry::HAS_NO_SAMPLERS) { | 1147 SamplerRegistry::HAS_NO_SAMPLERS) { |
| 1131 bool cpu_profiling_enabled = | 1148 bool cpu_profiling_enabled = |
| 1132 (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS); | 1149 (!FLAG_sample_stack_in_postprocessor_thread && state == SamplerRegistr
y::HAS_CPU_PROFILING_SAMPLERS); |
| 1133 bool runtime_profiler_enabled = RuntimeProfiler::IsEnabled(); | 1150 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 | 1151 // When CPU profiling is enabled both JavaScript and C++ code is |
| 1140 // profiled. We must not suspend. | 1152 // profiled. We must not suspend. |
| 1141 if (!cpu_profiling_enabled) { | 1153 if (!cpu_profiling_enabled) { |
| 1142 if (rate_limiter_.SuspendIfNecessary()) continue; | 1154 if (rate_limiter_.SuspendIfNecessary()) continue; |
| 1143 } | 1155 } |
| 1144 if (cpu_profiling_enabled && runtime_profiler_enabled) { | 1156 if (cpu_profiling_enabled && runtime_profiler_enabled) { |
| 1145 if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this)) { | 1157 if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this)) { |
| 1146 return; | 1158 return; |
| 1147 } | 1159 } |
| 1148 Sleep(HALF_INTERVAL); | 1160 Sleep(HALF_INTERVAL); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1164 } | 1176 } |
| 1165 } | 1177 } |
| 1166 Sleep(FULL_INTERVAL); | 1178 Sleep(FULL_INTERVAL); |
| 1167 } | 1179 } |
| 1168 } | 1180 } |
| 1169 } | 1181 } |
| 1170 | 1182 |
| 1171 static void DoCpuProfile(Sampler* sampler, void* raw_sender) { | 1183 static void DoCpuProfile(Sampler* sampler, void* raw_sender) { |
| 1172 if (!sampler->IsProfiling()) return; | 1184 if (!sampler->IsProfiling()) return; |
| 1173 SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender); | 1185 SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender); |
| 1174 sender->SendProfilingSignal(sampler->platform_data()->vm_tid()); | 1186 sender->SendProfilingSignal(sampler->platform_data()->vm_tgid(), sampler->pl
atform_data()->vm_tid()); |
| 1175 } | 1187 } |
| 1176 | 1188 |
| 1177 static void DoRuntimeProfile(Sampler* sampler, void* ignored) { | 1189 static void DoRuntimeProfile(Sampler* sampler, void* ignored) { |
| 1178 if (!sampler->isolate()->IsInitialized()) return; | 1190 if (!sampler->isolate()->IsInitialized()) return; |
| 1179 sampler->isolate()->runtime_profiler()->NotifyTick(); | 1191 sampler->isolate()->runtime_profiler()->NotifyTick(); |
| 1180 } | 1192 } |
| 1181 | 1193 |
| 1182 void SendProfilingSignal(int tid) { | 1194 void SendProfilingSignal(int tgid, int tid) { |
| 1183 if (!signal_handler_installed_) return; | 1195 if (!CpuProfilerSignalHandler::signal_handler_installed()) return; |
| 1184 // Glibc doesn't provide a wrapper for tgkill(2). | 1196 // Glibc doesn't provide a wrapper for tgkill(2). |
| 1185 #if defined(ANDROID) | 1197 #if defined(ANDROID) |
| 1186 syscall(__NR_tgkill, vm_tgid_, tid, SIGPROF); | 1198 syscall(__NR_tgkill, tgid, tid, SIGPROF); |
| 1187 #else | 1199 #else |
| 1188 syscall(SYS_tgkill, vm_tgid_, tid, SIGPROF); | 1200 syscall(SYS_tgkill, tgid, tid, SIGPROF); |
| 1189 #endif | 1201 #endif |
| 1190 } | 1202 } |
| 1191 | 1203 |
| 1192 void Sleep(SleepInterval full_or_half) { | 1204 void Sleep(SleepInterval full_or_half) { |
| 1193 // Convert ms to us and subtract 100 us to compensate delays | 1205 // Convert ms to us and subtract 100 us to compensate delays |
| 1194 // occuring during signal delivery. | 1206 // occuring during signal delivery. |
| 1195 useconds_t interval = interval_ * 1000 - 100; | 1207 useconds_t interval = interval_ * 1000 - 100; |
| 1196 if (full_or_half == HALF_INTERVAL) interval /= 2; | 1208 if (full_or_half == HALF_INTERVAL) interval /= 2; |
| 1197 #if defined(ANDROID) | 1209 #if defined(ANDROID) |
| 1198 usleep(interval); | 1210 usleep(interval); |
| 1199 #else | 1211 #else |
| 1200 int result = usleep(interval); | 1212 int result = usleep(interval); |
| 1201 #ifdef DEBUG | 1213 #ifdef DEBUG |
| 1202 if (result != 0 && errno != EINTR) { | 1214 if (result != 0 && errno != EINTR) { |
| 1203 fprintf(stderr, | 1215 fprintf(stderr, |
| 1204 "SignalSender usleep error; interval = %u, errno = %d\n", | 1216 "SignalSender usleep error; interval = %u, errno = %d\n", |
| 1205 interval, | 1217 interval, |
| 1206 errno); | 1218 errno); |
| 1207 ASSERT(result == 0 || errno == EINTR); | 1219 ASSERT(result == 0 || errno == EINTR); |
| 1208 } | 1220 } |
| 1209 #endif // DEBUG | 1221 #endif // DEBUG |
| 1210 USE(result); | 1222 USE(result); |
| 1211 #endif // ANDROID | 1223 #endif // ANDROID |
| 1212 } | 1224 } |
| 1213 | 1225 |
| 1214 const int vm_tgid_; | |
| 1215 const int interval_; | 1226 const int interval_; |
| 1216 RuntimeProfilerRateLimiter rate_limiter_; | 1227 RuntimeProfilerRateLimiter rate_limiter_; |
| 1217 | 1228 |
| 1218 // Protects the process wide state below. | 1229 // Protects the process wide state below. |
| 1219 static Mutex* mutex_; | 1230 static Mutex* mutex_; |
| 1220 static SignalSender* instance_; | 1231 static SignalSender* instance_; |
| 1221 static bool signal_handler_installed_; | |
| 1222 static struct sigaction old_signal_handler_; | |
| 1223 | 1232 |
| 1224 private: | 1233 private: |
| 1225 DISALLOW_COPY_AND_ASSIGN(SignalSender); | 1234 DISALLOW_COPY_AND_ASSIGN(SignalSender); |
| 1226 }; | 1235 }; |
| 1227 | 1236 |
| 1228 | 1237 |
| 1229 Mutex* SignalSender::mutex_ = NULL; | 1238 Mutex* SignalSender::mutex_ = NULL; |
| 1230 SignalSender* SignalSender::instance_ = NULL; | 1239 SignalSender* SignalSender::instance_ = NULL; |
| 1231 struct sigaction SignalSender::old_signal_handler_; | |
| 1232 bool SignalSender::signal_handler_installed_ = false; | |
| 1233 | 1240 |
| 1234 | 1241 |
| 1235 void OS::SetUp() { | 1242 void OS::SetUp() { |
| 1236 // Seed the random number generator. We preserve microsecond resolution. | 1243 // Seed the random number generator. We preserve microsecond resolution. |
| 1237 uint64_t seed = Ticks() ^ (getpid() << 16); | 1244 uint64_t seed = Ticks() ^ (getpid() << 16); |
| 1238 srandom(static_cast<unsigned int>(seed)); | 1245 srandom(static_cast<unsigned int>(seed)); |
| 1239 limit_mutex = CreateMutex(); | 1246 limit_mutex = CreateMutex(); |
| 1240 | 1247 |
| 1241 #ifdef __arm__ | 1248 #ifdef __arm__ |
| 1242 // When running on ARM hardware check that the EABI used by V8 and | 1249 // When running on ARM hardware check that the EABI used by V8 and |
| 1243 // by the C code is the same. | 1250 // by the C code is the same. |
| 1244 bool hard_float = OS::ArmUsingHardFloat(); | 1251 bool hard_float = OS::ArmUsingHardFloat(); |
| 1245 if (hard_float) { | 1252 if (hard_float) { |
| 1246 #if !USE_EABI_HARDFLOAT | 1253 #if !USE_EABI_HARDFLOAT |
| 1247 PrintF("ERROR: Binary compiled with -mfloat-abi=hard but without " | 1254 PrintF("ERROR: Binary compiled with -mfloat-abi=hard but without " |
| 1248 "-DUSE_EABI_HARDFLOAT\n"); | 1255 "-DUSE_EABI_HARDFLOAT\n"); |
| 1249 exit(1); | 1256 exit(1); |
| 1250 #endif | 1257 #endif |
| 1251 } else { | 1258 } else { |
| 1252 #if USE_EABI_HARDFLOAT | 1259 #if USE_EABI_HARDFLOAT |
| 1253 PrintF("ERROR: Binary not compiled with -mfloat-abi=hard but with " | 1260 PrintF("ERROR: Binary not compiled with -mfloat-abi=hard but with " |
| 1254 "-DUSE_EABI_HARDFLOAT\n"); | 1261 "-DUSE_EABI_HARDFLOAT\n"); |
| 1255 exit(1); | 1262 exit(1); |
| 1256 #endif | 1263 #endif |
| 1257 } | 1264 } |
| 1258 #endif | 1265 #endif |
| 1259 SignalSender::SetUp(); | 1266 SignalSender::SetUp(); |
| 1267 CpuProfilerSignalHandler::InstallSignalHandler(); |
| 1260 } | 1268 } |
| 1261 | 1269 |
| 1262 | 1270 |
| 1263 void OS::TearDown() { | 1271 void OS::TearDown() { |
| 1264 SignalSender::TearDown(); | 1272 SignalSender::TearDown(); |
| 1273 CpuProfilerSignalHandler::RestoreSignalHandler(); |
| 1265 delete limit_mutex; | 1274 delete limit_mutex; |
| 1266 } | 1275 } |
| 1267 | 1276 |
| 1268 | 1277 |
| 1269 Sampler::Sampler(Isolate* isolate, int interval) | 1278 Sampler::Sampler(Isolate* isolate, int interval) |
| 1270 : isolate_(isolate), | 1279 : isolate_(isolate), |
| 1271 interval_(interval), | 1280 interval_(interval), |
| 1272 profiling_(false), | 1281 profiling_(false), |
| 1273 active_(false), | 1282 active_(false), |
| 1274 samples_taken_(0) { | 1283 samples_taken_(0) { |
| 1275 data_ = new PlatformData; | 1284 data_ = new PlatformData; |
| 1276 } | 1285 } |
| 1277 | 1286 |
| 1278 | 1287 |
| 1279 Sampler::~Sampler() { | 1288 Sampler::~Sampler() { |
| 1280 ASSERT(!IsActive()); | 1289 ASSERT(!IsActive()); |
| 1281 delete data_; | 1290 delete data_; |
| 1282 } | 1291 } |
| 1283 | 1292 |
| 1284 | 1293 |
| 1294 void Sampler::DoSample() { |
| 1295 int tgid = data_->vm_tgid(); |
| 1296 int tid = data_->vm_tid(); |
| 1297 if (!CpuProfilerSignalHandler::signal_handler_installed()) return; |
| 1298 // Glibc doesn't provide a wrapper for tgkill(2). |
| 1299 #if defined(ANDROID) |
| 1300 syscall(__NR_tgkill, tgid, tid, SIGPROF); |
| 1301 #else |
| 1302 syscall(SYS_tgkill, tgid, tid, SIGPROF); |
| 1303 #endif |
| 1304 } |
| 1305 |
| 1306 |
| 1285 void Sampler::Start() { | 1307 void Sampler::Start() { |
| 1286 ASSERT(!IsActive()); | 1308 ASSERT(!IsActive()); |
| 1287 SetActive(true); | 1309 SetActive(true); |
| 1288 SignalSender::AddActiveSampler(this); | 1310 SignalSender::AddActiveSampler(this); |
| 1289 } | 1311 } |
| 1290 | 1312 |
| 1291 | 1313 |
| 1292 void Sampler::Stop() { | 1314 void Sampler::Stop() { |
| 1293 ASSERT(IsActive()); | 1315 ASSERT(IsActive()); |
| 1294 SignalSender::RemoveActiveSampler(this); | 1316 SignalSender::RemoveActiveSampler(this); |
| 1295 SetActive(false); | 1317 SetActive(false); |
| 1296 } | 1318 } |
| 1297 | 1319 |
| 1298 | 1320 |
| 1299 } } // namespace v8::internal | 1321 } } // namespace v8::internal |
| OLD | NEW |