| 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 1004 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1015 #if defined(__ANDROID__) | 1015 #if defined(__ANDROID__) |
| 1016 // Android's C library provides gettid(2). | 1016 // Android's C library provides gettid(2). |
| 1017 return gettid(); | 1017 return gettid(); |
| 1018 #else | 1018 #else |
| 1019 // Glibc doesn't provide a wrapper for gettid(2). | 1019 // Glibc doesn't provide a wrapper for gettid(2). |
| 1020 return syscall(SYS_gettid); | 1020 return syscall(SYS_gettid); |
| 1021 #endif | 1021 #endif |
| 1022 } | 1022 } |
| 1023 | 1023 |
| 1024 | 1024 |
| 1025 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { | |
| 1026 USE(info); | |
| 1027 if (signal != SIGPROF) return; | |
| 1028 Isolate* isolate = Isolate::UncheckedCurrent(); | |
| 1029 if (isolate == NULL || !isolate->IsInitialized() || !isolate->IsInUse()) { | |
| 1030 // We require a fully initialized and entered isolate. | |
| 1031 return; | |
| 1032 } | |
| 1033 if (v8::Locker::IsActive() && | |
| 1034 !isolate->thread_manager()->IsLockedByCurrentThread()) { | |
| 1035 return; | |
| 1036 } | |
| 1037 | |
| 1038 Sampler* sampler = isolate->logger()->sampler(); | |
| 1039 if (sampler == NULL || !sampler->IsActive()) return; | |
| 1040 | |
| 1041 TickSample sample_obj; | |
| 1042 TickSample* sample = CpuProfiler::TickSampleEvent(isolate); | |
| 1043 if (sample == NULL) sample = &sample_obj; | |
| 1044 | |
| 1045 // Extracting the sample from the context is extremely machine dependent. | |
| 1046 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); | |
| 1047 mcontext_t& mcontext = ucontext->uc_mcontext; | |
| 1048 sample->state = isolate->current_vm_state(); | |
| 1049 #if V8_HOST_ARCH_IA32 | |
| 1050 sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]); | |
| 1051 sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]); | |
| 1052 sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]); | |
| 1053 #elif V8_HOST_ARCH_X64 | |
| 1054 sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]); | |
| 1055 sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]); | |
| 1056 sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]); | |
| 1057 #elif V8_HOST_ARCH_ARM | |
| 1058 #if defined(__GLIBC__) && !defined(__UCLIBC__) && \ | |
| 1059 (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3)) | |
| 1060 // Old GLibc ARM versions used a gregs[] array to access the register | |
| 1061 // values from mcontext_t. | |
| 1062 sample->pc = reinterpret_cast<Address>(mcontext.gregs[R15]); | |
| 1063 sample->sp = reinterpret_cast<Address>(mcontext.gregs[R13]); | |
| 1064 sample->fp = reinterpret_cast<Address>(mcontext.gregs[R11]); | |
| 1065 #else | |
| 1066 sample->pc = reinterpret_cast<Address>(mcontext.arm_pc); | |
| 1067 sample->sp = reinterpret_cast<Address>(mcontext.arm_sp); | |
| 1068 sample->fp = reinterpret_cast<Address>(mcontext.arm_fp); | |
| 1069 #endif // defined(__GLIBC__) && !defined(__UCLIBC__) && | |
| 1070 // (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3)) | |
| 1071 #elif V8_HOST_ARCH_MIPS | |
| 1072 sample->pc = reinterpret_cast<Address>(mcontext.pc); | |
| 1073 sample->sp = reinterpret_cast<Address>(mcontext.gregs[29]); | |
| 1074 sample->fp = reinterpret_cast<Address>(mcontext.gregs[30]); | |
| 1075 #endif // V8_HOST_ARCH_* | |
| 1076 sampler->SampleStack(sample); | |
| 1077 sampler->Tick(sample); | |
| 1078 } | |
| 1079 | |
| 1080 | |
| 1081 class Sampler::PlatformData : public Malloced { | 1025 class Sampler::PlatformData : public Malloced { |
| 1082 public: | 1026 public: |
| 1083 PlatformData() : vm_tid_(GetThreadID()) {} | 1027 PlatformData() : vm_tid_(GetThreadID()) {} |
| 1084 | 1028 |
| 1085 int vm_tid() const { return vm_tid_; } | 1029 int vm_tid() const { return vm_tid_; } |
| 1086 | 1030 |
| 1087 private: | 1031 private: |
| 1088 const int vm_tid_; | 1032 const int vm_tid_; |
| 1089 }; | 1033 }; |
| 1090 | 1034 |
| 1091 | 1035 |
| 1036 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context); |
| 1037 |
| 1038 |
| 1092 class SignalSender : public Thread { | 1039 class SignalSender : public Thread { |
| 1093 public: | 1040 public: |
| 1094 enum SleepInterval { | 1041 enum SleepInterval { |
| 1095 HALF_INTERVAL, | 1042 HALF_INTERVAL, |
| 1096 FULL_INTERVAL | 1043 FULL_INTERVAL |
| 1097 }; | 1044 }; |
| 1098 | 1045 |
| 1099 static const int kSignalSenderStackSize = 64 * KB; | 1046 static const int kSignalSenderStackSize = 64 * KB; |
| 1100 | 1047 |
| 1101 explicit SignalSender(int interval) | 1048 explicit SignalSender(int interval) |
| 1102 : Thread(Thread::Options("SignalSender", kSignalSenderStackSize)), | 1049 : Thread(Thread::Options("SignalSender", kSignalSenderStackSize)), |
| 1103 vm_tgid_(getpid()), | 1050 vm_tgid_(getpid()), |
| 1104 interval_(interval) {} | 1051 interval_(interval) {} |
| 1105 | 1052 |
| 1106 static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); } | 1053 static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); } |
| 1107 static void TearDown() { delete mutex_; } | 1054 static void TearDown() { delete mutex_; } |
| 1108 | 1055 |
| 1109 static void InstallSignalHandler() { | 1056 static void InstallSignalHandler() { |
| 1110 struct sigaction sa; | 1057 struct sigaction sa; |
| 1111 sa.sa_sigaction = ProfilerSignalHandler; | 1058 sa.sa_sigaction = ProfilerSignalHandler; |
| 1112 sigemptyset(&sa.sa_mask); | 1059 sigemptyset(&sa.sa_mask); |
| 1060 sigset_t signals_to_unblock; |
| 1061 sigemptyset(&signals_to_unblock); |
| 1062 sigaddset(&signals_to_unblock, SIGPROF); |
| 1063 sigprocmask(SIG_UNBLOCK, &signals_to_unblock, &old_signal_mask_); |
| 1113 sa.sa_flags = SA_RESTART | SA_SIGINFO; | 1064 sa.sa_flags = SA_RESTART | SA_SIGINFO; |
| 1114 signal_handler_installed_ = | 1065 signal_handler_installed_ = |
| 1115 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); | 1066 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); |
| 1116 } | 1067 } |
| 1117 | 1068 |
| 1118 static void RestoreSignalHandler() { | 1069 static void RestoreSignalHandler() { |
| 1119 if (signal_handler_installed_) { | 1070 if (signal_handler_installed_) { |
| 1120 sigaction(SIGPROF, &old_signal_handler_, 0); | 1071 sigaction(SIGPROF, &old_signal_handler_, 0); |
| 1072 sigprocmask(SIG_SETMASK, &old_signal_mask_, NULL); |
| 1121 signal_handler_installed_ = false; | 1073 signal_handler_installed_ = false; |
| 1122 } | 1074 } |
| 1123 } | 1075 } |
| 1124 | 1076 |
| 1077 static void CallOldSignalHandler(int signal, siginfo_t* info, void* context) { |
| 1078 // Only invoke the old signal handler if it did not have SIGPROF masked. |
| 1079 if (sigismember(&old_signal_mask_, SIGPROF)) |
| 1080 return; |
| 1081 if (!(old_signal_handler_.sa_flags & SA_SIGINFO)) { |
| 1082 // If the old signal disposition was SIG_IGN, then it is safe to ignore |
| 1083 // the old signal handler. If it is SIG_DFL, then that is pretty strong |
| 1084 // evidence that nobody other than us is using the profiling |
| 1085 // infrastructure. It is probably reasonable to assume that in this case |
| 1086 // we don't need to chain to the old behavior, as that would merely result |
| 1087 // in the application getting terminated. |
| 1088 if (old_signal_handler_.sa_handler == SIG_IGN || |
| 1089 old_signal_handler_.sa_handler == SIG_DFL) { |
| 1090 return; |
| 1091 } |
| 1092 // Sorry, we only support other 3-arg signal handlers (sigaction). Crash |
| 1093 // loudly if someone tries to use a different signal handler. |
| 1094 *(reinterpret_cast<volatile char*>(NULL) + 74) = 0x34; |
| 1095 } |
| 1096 old_signal_handler_.sa_sigaction(signal, info, context); |
| 1097 } |
| 1098 |
| 1125 static void AddActiveSampler(Sampler* sampler) { | 1099 static void AddActiveSampler(Sampler* sampler) { |
| 1126 ScopedLock lock(mutex_); | 1100 ScopedLock lock(mutex_); |
| 1127 SamplerRegistry::AddActiveSampler(sampler); | 1101 SamplerRegistry::AddActiveSampler(sampler); |
| 1128 if (instance_ == NULL) { | 1102 if (instance_ == NULL) { |
| 1129 // Start a thread that will send SIGPROF signal to VM threads, | 1103 // Start a thread that will send SIGPROF signal to VM threads, |
| 1130 // when CPU profiling will be enabled. | 1104 // when CPU profiling will be enabled. |
| 1131 instance_ = new SignalSender(sampler->interval()); | 1105 instance_ = new SignalSender(sampler->interval()); |
| 1132 instance_->Start(); | 1106 instance_->Start(); |
| 1133 } else { | 1107 } else { |
| 1134 ASSERT(instance_->interval_ == sampler->interval()); | 1108 ASSERT(instance_->interval_ == sampler->interval()); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1236 | 1210 |
| 1237 const int vm_tgid_; | 1211 const int vm_tgid_; |
| 1238 const int interval_; | 1212 const int interval_; |
| 1239 RuntimeProfilerRateLimiter rate_limiter_; | 1213 RuntimeProfilerRateLimiter rate_limiter_; |
| 1240 | 1214 |
| 1241 // Protects the process wide state below. | 1215 // Protects the process wide state below. |
| 1242 static Mutex* mutex_; | 1216 static Mutex* mutex_; |
| 1243 static SignalSender* instance_; | 1217 static SignalSender* instance_; |
| 1244 static bool signal_handler_installed_; | 1218 static bool signal_handler_installed_; |
| 1245 static struct sigaction old_signal_handler_; | 1219 static struct sigaction old_signal_handler_; |
| 1220 static sigset_t old_signal_mask_; |
| 1246 | 1221 |
| 1247 private: | 1222 private: |
| 1248 DISALLOW_COPY_AND_ASSIGN(SignalSender); | 1223 DISALLOW_COPY_AND_ASSIGN(SignalSender); |
| 1249 }; | 1224 }; |
| 1250 | 1225 |
| 1251 | 1226 |
| 1252 Mutex* SignalSender::mutex_ = NULL; | 1227 Mutex* SignalSender::mutex_ = NULL; |
| 1253 SignalSender* SignalSender::instance_ = NULL; | 1228 SignalSender* SignalSender::instance_ = NULL; |
| 1254 struct sigaction SignalSender::old_signal_handler_; | 1229 struct sigaction SignalSender::old_signal_handler_; |
| 1255 bool SignalSender::signal_handler_installed_ = false; | 1230 bool SignalSender::signal_handler_installed_ = false; |
| 1231 sigset_t SignalSender::old_signal_mask_; |
| 1232 |
| 1233 |
| 1234 static void ProfilerSignalHandlerInternal( |
| 1235 int signal, siginfo_t* info, void* context) { |
| 1236 USE(info); |
| 1237 if (signal != SIGPROF) return; |
| 1238 SignalSender::CallOldSignalHandler(signal, info, context); |
| 1239 Isolate* isolate = Isolate::UncheckedCurrent(); |
| 1240 if (isolate == NULL || !isolate->IsInitialized() || !isolate->IsInUse()) { |
| 1241 // We require a fully initialized and entered isolate. |
| 1242 return; |
| 1243 } |
| 1244 if (v8::Locker::IsActive() && |
| 1245 !isolate->thread_manager()->IsLockedByCurrentThread()) { |
| 1246 return; |
| 1247 } |
| 1248 |
| 1249 Sampler* sampler = isolate->logger()->sampler(); |
| 1250 if (sampler == NULL || !sampler->IsActive()) return; |
| 1251 |
| 1252 TickSample sample_obj; |
| 1253 TickSample* sample = CpuProfiler::TickSampleEvent(isolate); |
| 1254 if (sample == NULL) sample = &sample_obj; |
| 1255 |
| 1256 // Extracting the sample from the context is extremely machine dependent. |
| 1257 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); |
| 1258 mcontext_t& mcontext = ucontext->uc_mcontext; |
| 1259 sample->state = isolate->current_vm_state(); |
| 1260 #if V8_HOST_ARCH_IA32 |
| 1261 sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]); |
| 1262 sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]); |
| 1263 sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]); |
| 1264 #elif V8_HOST_ARCH_X64 |
| 1265 sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]); |
| 1266 sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]); |
| 1267 sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]); |
| 1268 #elif V8_HOST_ARCH_ARM |
| 1269 #if defined(__GLIBC__) && !defined(__UCLIBC__) && \ |
| 1270 (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3)) |
| 1271 // Old GLibc ARM versions used a gregs[] array to access the register |
| 1272 // values from mcontext_t. |
| 1273 sample->pc = reinterpret_cast<Address>(mcontext.gregs[R15]); |
| 1274 sample->sp = reinterpret_cast<Address>(mcontext.gregs[R13]); |
| 1275 sample->fp = reinterpret_cast<Address>(mcontext.gregs[R11]); |
| 1276 #else |
| 1277 sample->pc = reinterpret_cast<Address>(mcontext.arm_pc); |
| 1278 sample->sp = reinterpret_cast<Address>(mcontext.arm_sp); |
| 1279 sample->fp = reinterpret_cast<Address>(mcontext.arm_fp); |
| 1280 #endif // defined(__GLIBC__) && !defined(__UCLIBC__) && |
| 1281 // (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3)) |
| 1282 #elif V8_HOST_ARCH_MIPS |
| 1283 sample->pc = reinterpret_cast<Address>(mcontext.pc); |
| 1284 sample->sp = reinterpret_cast<Address>(mcontext.gregs[29]); |
| 1285 sample->fp = reinterpret_cast<Address>(mcontext.gregs[30]); |
| 1286 #endif // V8_HOST_ARCH_* |
| 1287 sampler->SampleStack(sample); |
| 1288 sampler->Tick(sample); |
| 1289 } |
| 1290 |
| 1291 |
| 1292 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { |
| 1293 int curr_errno = errno; |
| 1294 ProfilerSignalHandlerInternal(signal, info, context); |
| 1295 errno = curr_errno; |
| 1296 } |
| 1256 | 1297 |
| 1257 | 1298 |
| 1258 void OS::SetUp() { | 1299 void OS::SetUp() { |
| 1259 // Seed the random number generator. We preserve microsecond resolution. | 1300 // Seed the random number generator. We preserve microsecond resolution. |
| 1260 uint64_t seed = Ticks() ^ (getpid() << 16); | 1301 uint64_t seed = Ticks() ^ (getpid() << 16); |
| 1261 srandom(static_cast<unsigned int>(seed)); | 1302 srandom(static_cast<unsigned int>(seed)); |
| 1262 limit_mutex = CreateMutex(); | 1303 limit_mutex = CreateMutex(); |
| 1263 | 1304 |
| 1264 #ifdef __arm__ | 1305 #ifdef __arm__ |
| 1265 // When running on ARM hardware check that the EABI used by V8 and | 1306 // When running on ARM hardware check that the EABI used by V8 and |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1313 | 1354 |
| 1314 | 1355 |
| 1315 void Sampler::Stop() { | 1356 void Sampler::Stop() { |
| 1316 ASSERT(IsActive()); | 1357 ASSERT(IsActive()); |
| 1317 SignalSender::RemoveActiveSampler(this); | 1358 SignalSender::RemoveActiveSampler(this); |
| 1318 SetActive(false); | 1359 SetActive(false); |
| 1319 } | 1360 } |
| 1320 | 1361 |
| 1321 | 1362 |
| 1322 } } // namespace v8::internal | 1363 } } // namespace v8::internal |
| OLD | NEW |