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