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