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 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); | |
|
Markus (顧孟勤)
2012/12/13 08:49:45
Our style guide for some reason prefers that we do
Sven Panne
2012/12/13 09:34:54
The Google C++ style guide allows both possibiliti
Markus (顧孟勤)
2012/12/13 09:56:50
Ah, sanity prevails. And yes, I agree that the pre
| |
| 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) |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 1115 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); | 1062 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); |
| 1116 } | 1063 } |
| 1117 | 1064 |
| 1118 static void RestoreSignalHandler() { | 1065 static void RestoreSignalHandler() { |
| 1119 if (signal_handler_installed_) { | 1066 if (signal_handler_installed_) { |
| 1120 sigaction(SIGPROF, &old_signal_handler_, 0); | 1067 sigaction(SIGPROF, &old_signal_handler_, 0); |
| 1121 signal_handler_installed_ = false; | 1068 signal_handler_installed_ = false; |
| 1122 } | 1069 } |
| 1123 } | 1070 } |
| 1124 | 1071 |
| 1072 static void CallOldSignalHandler(int signal, siginfo_t* info, void* context) { | |
| 1073 // Only invoke the old signal handler if it did not have SIGPROF masked. | |
| 1074 if (sigismember(&old_signal_handler_.sa_mask, SIGPROF)) | |
|
Markus (顧孟勤)
2012/12/13 08:49:45
I don't believe this is the correct mask. See belo
| |
| 1075 return; | |
| 1076 if (!(old_signal_handler_.sa_flags & SA_SIGINFO)) { | |
| 1077 // It's safe to ignore the old signal handler if it's one of these values. | |
|
Markus (顧孟勤)
2012/12/13 08:49:45
Your code is arguably the right thing to do ... ev
| |
| 1078 if (old_signal_handler_.sa_handler == SIG_IGN || | |
| 1079 old_signal_handler_.sa_handler == SIG_DFL) { | |
| 1080 return; | |
| 1081 } | |
| 1082 // Sorry, we only support other 3-arg signal handlers (sigaction). Crash | |
| 1083 // loudly if someone tries to use a different signal handler. | |
| 1084 *(reinterpret_cast<volatile char*>(NULL) + 74) = 0x34; | |
| 1085 } | |
| 1086 old_signal_handler_.sa_sigaction(signal, info, context); | |
| 1087 } | |
| 1088 | |
| 1125 static void AddActiveSampler(Sampler* sampler) { | 1089 static void AddActiveSampler(Sampler* sampler) { |
| 1126 ScopedLock lock(mutex_); | 1090 ScopedLock lock(mutex_); |
| 1127 SamplerRegistry::AddActiveSampler(sampler); | 1091 SamplerRegistry::AddActiveSampler(sampler); |
| 1128 if (instance_ == NULL) { | 1092 if (instance_ == NULL) { |
| 1129 // Start a thread that will send SIGPROF signal to VM threads, | 1093 // Start a thread that will send SIGPROF signal to VM threads, |
| 1130 // when CPU profiling will be enabled. | 1094 // when CPU profiling will be enabled. |
| 1131 instance_ = new SignalSender(sampler->interval()); | 1095 instance_ = new SignalSender(sampler->interval()); |
| 1132 instance_->Start(); | 1096 instance_->Start(); |
| 1133 } else { | 1097 } else { |
| 1134 ASSERT(instance_->interval_ == sampler->interval()); | 1098 ASSERT(instance_->interval_ == sampler->interval()); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1248 DISALLOW_COPY_AND_ASSIGN(SignalSender); | 1212 DISALLOW_COPY_AND_ASSIGN(SignalSender); |
| 1249 }; | 1213 }; |
| 1250 | 1214 |
| 1251 | 1215 |
| 1252 Mutex* SignalSender::mutex_ = NULL; | 1216 Mutex* SignalSender::mutex_ = NULL; |
| 1253 SignalSender* SignalSender::instance_ = NULL; | 1217 SignalSender* SignalSender::instance_ = NULL; |
| 1254 struct sigaction SignalSender::old_signal_handler_; | 1218 struct sigaction SignalSender::old_signal_handler_; |
| 1255 bool SignalSender::signal_handler_installed_ = false; | 1219 bool SignalSender::signal_handler_installed_ = false; |
| 1256 | 1220 |
| 1257 | 1221 |
| 1222 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { | |
| 1223 int curr_errno = errno; | |
| 1224 USE(info); | |
| 1225 if (signal != SIGPROF) return; | |
|
Markus (顧孟勤)
2012/12/13 08:49:45
You should probably restore "errno" for each of th
| |
| 1226 SignalSender::CallOldSignalHandler(signal, info, context); | |
| 1227 Isolate* isolate = Isolate::UncheckedCurrent(); | |
| 1228 if (isolate == NULL || !isolate->IsInitialized() || !isolate->IsInUse()) { | |
| 1229 // We require a fully initialized and entered isolate. | |
| 1230 return; | |
| 1231 } | |
| 1232 if (v8::Locker::IsActive() && | |
| 1233 !isolate->thread_manager()->IsLockedByCurrentThread()) { | |
| 1234 return; | |
| 1235 } | |
| 1236 | |
| 1237 Sampler* sampler = isolate->logger()->sampler(); | |
| 1238 if (sampler == NULL || !sampler->IsActive()) return; | |
| 1239 | |
| 1240 TickSample sample_obj; | |
| 1241 TickSample* sample = CpuProfiler::TickSampleEvent(isolate); | |
| 1242 if (sample == NULL) sample = &sample_obj; | |
| 1243 | |
| 1244 // Extracting the sample from the context is extremely machine dependent. | |
| 1245 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); | |
| 1246 mcontext_t& mcontext = ucontext->uc_mcontext; | |
| 1247 sample->state = isolate->current_vm_state(); | |
| 1248 #if V8_HOST_ARCH_IA32 | |
| 1249 sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]); | |
| 1250 sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]); | |
| 1251 sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]); | |
| 1252 #elif V8_HOST_ARCH_X64 | |
| 1253 sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]); | |
| 1254 sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]); | |
| 1255 sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]); | |
| 1256 #elif V8_HOST_ARCH_ARM | |
| 1257 #if defined(__GLIBC__) && !defined(__UCLIBC__) && \ | |
| 1258 (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3)) | |
| 1259 // Old GLibc ARM versions used a gregs[] array to access the register | |
| 1260 // values from mcontext_t. | |
| 1261 sample->pc = reinterpret_cast<Address>(mcontext.gregs[R15]); | |
| 1262 sample->sp = reinterpret_cast<Address>(mcontext.gregs[R13]); | |
| 1263 sample->fp = reinterpret_cast<Address>(mcontext.gregs[R11]); | |
| 1264 #else | |
| 1265 sample->pc = reinterpret_cast<Address>(mcontext.arm_pc); | |
| 1266 sample->sp = reinterpret_cast<Address>(mcontext.arm_sp); | |
| 1267 sample->fp = reinterpret_cast<Address>(mcontext.arm_fp); | |
| 1268 #endif // defined(__GLIBC__) && !defined(__UCLIBC__) && | |
| 1269 // (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3)) | |
| 1270 #elif V8_HOST_ARCH_MIPS | |
| 1271 sample->pc = reinterpret_cast<Address>(mcontext.pc); | |
| 1272 sample->sp = reinterpret_cast<Address>(mcontext.gregs[29]); | |
| 1273 sample->fp = reinterpret_cast<Address>(mcontext.gregs[30]); | |
| 1274 #endif // V8_HOST_ARCH_* | |
| 1275 sampler->SampleStack(sample); | |
| 1276 sampler->Tick(sample); | |
| 1277 errno = curr_errno; | |
| 1278 } | |
| 1279 | |
| 1280 | |
| 1258 void OS::SetUp() { | 1281 void OS::SetUp() { |
| 1259 // Seed the random number generator. We preserve microsecond resolution. | 1282 // Seed the random number generator. We preserve microsecond resolution. |
| 1260 uint64_t seed = Ticks() ^ (getpid() << 16); | 1283 uint64_t seed = Ticks() ^ (getpid() << 16); |
| 1261 srandom(static_cast<unsigned int>(seed)); | 1284 srandom(static_cast<unsigned int>(seed)); |
| 1262 limit_mutex = CreateMutex(); | 1285 limit_mutex = CreateMutex(); |
| 1263 | 1286 |
| 1264 #ifdef __arm__ | 1287 #ifdef __arm__ |
| 1265 // When running on ARM hardware check that the EABI used by V8 and | 1288 // When running on ARM hardware check that the EABI used by V8 and |
| 1266 // by the C code is the same. | 1289 // by the C code is the same. |
| 1267 bool hard_float = OS::ArmUsingHardFloat(); | 1290 bool hard_float = OS::ArmUsingHardFloat(); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1313 | 1336 |
| 1314 | 1337 |
| 1315 void Sampler::Stop() { | 1338 void Sampler::Stop() { |
| 1316 ASSERT(IsActive()); | 1339 ASSERT(IsActive()); |
| 1317 SignalSender::RemoveActiveSampler(this); | 1340 SignalSender::RemoveActiveSampler(this); |
| 1318 SetActive(false); | 1341 SetActive(false); |
| 1319 } | 1342 } |
| 1320 | 1343 |
| 1321 | 1344 |
| 1322 } } // namespace v8::internal | 1345 } } // namespace v8::internal |
| OLD | NEW |