Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: src/platform-linux.cc

Issue 10857035: Moving cpu profiling into its own thread. (Closed) Base URL: http://git.chromium.org/external/v8.git@master
Patch Set: added { } Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/platform-freebsd.cc ('k') | src/platform-macos.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 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
1058 class Sampler::PlatformData : public Malloced { 1104 class Sampler::PlatformData : public Malloced {
1059 public: 1105 public:
1060 PlatformData() : vm_tid_(GetThreadID()) {} 1106 PlatformData()
1107 : vm_tgid_(getpid()),
1108 vm_tid_(GetThreadID()) {}
1061 1109
1062 int vm_tid() const { return vm_tid_; } 1110 void SendProfilingSignal() {
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 }
1063 1119
1064 private: 1120 private:
1121 const int vm_tgid_;
1065 const int vm_tid_; 1122 const int vm_tid_;
1066 }; 1123 };
1067 1124
1068 1125
1069 class SignalSender : public Thread { 1126 class SignalSender : public Thread {
1070 public: 1127 public:
1071 enum SleepInterval { 1128 enum SleepInterval {
1072 HALF_INTERVAL, 1129 HALF_INTERVAL,
1073 FULL_INTERVAL 1130 FULL_INTERVAL
1074 }; 1131 };
1075 1132
1076 static const int kSignalSenderStackSize = 64 * KB; 1133 static const int kSignalSenderStackSize = 64 * KB;
1077 1134
1078 explicit SignalSender(int interval) 1135 explicit SignalSender(int interval)
1079 : Thread(Thread::Options("SignalSender", kSignalSenderStackSize)), 1136 : Thread(Thread::Options("SignalSender", kSignalSenderStackSize)),
1080 vm_tgid_(getpid()),
1081 interval_(interval) {} 1137 interval_(interval) {}
1082 1138
1083 static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); } 1139 static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); }
1084 static void TearDown() { delete mutex_; } 1140 static void TearDown() { delete mutex_; }
1085 1141
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) { 1142 static void AddActiveSampler(Sampler* sampler) {
1103 ScopedLock lock(mutex_); 1143 ScopedLock lock(mutex_);
1104 SamplerRegistry::AddActiveSampler(sampler); 1144 SamplerRegistry::AddActiveSampler(sampler);
1105 if (instance_ == NULL) { 1145 if (instance_ == NULL) {
1106 // Start a thread that will send SIGPROF signal to VM threads, 1146 // Start a thread that will send SIGPROF signal to VM threads,
1107 // when CPU profiling will be enabled. 1147 // when CPU profiling will be enabled.
1108 instance_ = new SignalSender(sampler->interval()); 1148 instance_ = new SignalSender(sampler->interval());
1109 instance_->Start(); 1149 instance_->Start();
1110 } else { 1150 } else {
1111 ASSERT(instance_->interval_ == sampler->interval()); 1151 ASSERT(instance_->interval_ == sampler->interval());
1112 } 1152 }
1113 } 1153 }
1114 1154
1115 static void RemoveActiveSampler(Sampler* sampler) { 1155 static void RemoveActiveSampler(Sampler* sampler) {
1116 ScopedLock lock(mutex_); 1156 ScopedLock lock(mutex_);
1117 SamplerRegistry::RemoveActiveSampler(sampler); 1157 SamplerRegistry::RemoveActiveSampler(sampler);
1118 if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) { 1158 if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) {
1119 RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_); 1159 RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_);
1120 delete instance_; 1160 delete instance_;
1121 instance_ = NULL; 1161 instance_ = NULL;
1122 RestoreSignalHandler();
1123 } 1162 }
1124 } 1163 }
1125 1164
1126 // Implement Thread::Run(). 1165 // Implement Thread::Run().
1127 virtual void Run() { 1166 virtual void Run() {
1128 SamplerRegistry::State state; 1167 SamplerRegistry::State state;
1129 while ((state = SamplerRegistry::GetState()) != 1168 while ((state = SamplerRegistry::GetState()) !=
1130 SamplerRegistry::HAS_NO_SAMPLERS) { 1169 SamplerRegistry::HAS_NO_SAMPLERS) {
1131 bool cpu_profiling_enabled = 1170 if (rate_limiter_.SuspendIfNecessary()) continue;
1132 (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS); 1171 if (RuntimeProfiler::IsEnabled()) {
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);
1149 if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, NULL)) { 1172 if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, NULL)) {
1150 return; 1173 return;
1151 } 1174 }
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);
1167 } 1175 }
1176 Sleep(FULL_INTERVAL);
1168 } 1177 }
1169 } 1178 }
1170 1179
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
1177 static void DoRuntimeProfile(Sampler* sampler, void* ignored) { 1180 static void DoRuntimeProfile(Sampler* sampler, void* ignored) {
1178 if (!sampler->isolate()->IsInitialized()) return; 1181 if (!sampler->isolate()->IsInitialized()) return;
1179 sampler->isolate()->runtime_profiler()->NotifyTick(); 1182 sampler->isolate()->runtime_profiler()->NotifyTick();
1180 } 1183 }
1181 1184
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) { 1185 void Sleep(SleepInterval full_or_half) {
1193 // Convert ms to us and subtract 100 us to compensate delays 1186 // Convert ms to us and subtract 100 us to compensate delays
1194 // occuring during signal delivery. 1187 // occuring during signal delivery.
1195 useconds_t interval = interval_ * 1000 - 100; 1188 useconds_t interval = interval_ * 1000 - 100;
1196 if (full_or_half == HALF_INTERVAL) interval /= 2; 1189 if (full_or_half == HALF_INTERVAL) interval /= 2;
1197 #if defined(ANDROID) 1190 #if defined(ANDROID)
1198 usleep(interval); 1191 usleep(interval);
1199 #else 1192 #else
1200 int result = usleep(interval); 1193 int result = usleep(interval);
1201 #ifdef DEBUG 1194 #ifdef DEBUG
1202 if (result != 0 && errno != EINTR) { 1195 if (result != 0 && errno != EINTR) {
1203 fprintf(stderr, 1196 fprintf(stderr,
1204 "SignalSender usleep error; interval = %u, errno = %d\n", 1197 "SignalSender usleep error; interval = %u, errno = %d\n",
1205 interval, 1198 interval,
1206 errno); 1199 errno);
1207 ASSERT(result == 0 || errno == EINTR); 1200 ASSERT(result == 0 || errno == EINTR);
1208 } 1201 }
1209 #endif // DEBUG 1202 #endif // DEBUG
1210 USE(result); 1203 USE(result);
1211 #endif // ANDROID 1204 #endif // ANDROID
1212 } 1205 }
1213 1206
1214 const int vm_tgid_;
1215 const int interval_; 1207 const int interval_;
1216 RuntimeProfilerRateLimiter rate_limiter_; 1208 RuntimeProfilerRateLimiter rate_limiter_;
1217 1209
1218 // Protects the process wide state below. 1210 // Protects the process wide state below.
1219 static Mutex* mutex_; 1211 static Mutex* mutex_;
1220 static SignalSender* instance_; 1212 static SignalSender* instance_;
1221 static bool signal_handler_installed_;
1222 static struct sigaction old_signal_handler_;
1223 1213
1224 private: 1214 private:
1225 DISALLOW_COPY_AND_ASSIGN(SignalSender); 1215 DISALLOW_COPY_AND_ASSIGN(SignalSender);
1226 }; 1216 };
1227 1217
1228 1218
1229 Mutex* SignalSender::mutex_ = NULL; 1219 Mutex* SignalSender::mutex_ = NULL;
1230 SignalSender* SignalSender::instance_ = NULL; 1220 SignalSender* SignalSender::instance_ = NULL;
1231 struct sigaction SignalSender::old_signal_handler_;
1232 bool SignalSender::signal_handler_installed_ = false;
1233 1221
1234 1222
1235 void OS::SetUp() { 1223 void OS::SetUp() {
1236 // Seed the random number generator. We preserve microsecond resolution. 1224 // Seed the random number generator. We preserve microsecond resolution.
1237 uint64_t seed = Ticks() ^ (getpid() << 16); 1225 uint64_t seed = Ticks() ^ (getpid() << 16);
1238 srandom(static_cast<unsigned int>(seed)); 1226 srandom(static_cast<unsigned int>(seed));
1239 limit_mutex = CreateMutex(); 1227 limit_mutex = CreateMutex();
1240 1228
1241 #ifdef __arm__ 1229 #ifdef __arm__
1242 // When running on ARM hardware check that the EABI used by V8 and 1230 // When running on ARM hardware check that the EABI used by V8 and
1243 // by the C code is the same. 1231 // by the C code is the same.
1244 bool hard_float = OS::ArmUsingHardFloat(); 1232 bool hard_float = OS::ArmUsingHardFloat();
1245 if (hard_float) { 1233 if (hard_float) {
1246 #if !USE_EABI_HARDFLOAT 1234 #if !USE_EABI_HARDFLOAT
1247 PrintF("ERROR: Binary compiled with -mfloat-abi=hard but without " 1235 PrintF("ERROR: Binary compiled with -mfloat-abi=hard but without "
1248 "-DUSE_EABI_HARDFLOAT\n"); 1236 "-DUSE_EABI_HARDFLOAT\n");
1249 exit(1); 1237 exit(1);
1250 #endif 1238 #endif
1251 } else { 1239 } else {
1252 #if USE_EABI_HARDFLOAT 1240 #if USE_EABI_HARDFLOAT
1253 PrintF("ERROR: Binary not compiled with -mfloat-abi=hard but with " 1241 PrintF("ERROR: Binary not compiled with -mfloat-abi=hard but with "
1254 "-DUSE_EABI_HARDFLOAT\n"); 1242 "-DUSE_EABI_HARDFLOAT\n");
1255 exit(1); 1243 exit(1);
1256 #endif 1244 #endif
1257 } 1245 }
1258 #endif 1246 #endif
1259 SignalSender::SetUp(); 1247 SignalSender::SetUp();
1248 CpuProfilerSignalHandler::SetUp();
1260 } 1249 }
1261 1250
1262 1251
1263 void OS::TearDown() { 1252 void OS::TearDown() {
1264 SignalSender::TearDown(); 1253 SignalSender::TearDown();
1254 CpuProfilerSignalHandler::TearDown();
1265 delete limit_mutex; 1255 delete limit_mutex;
1266 } 1256 }
1267 1257
1268 1258
1269 Sampler::Sampler(Isolate* isolate, int interval) 1259 Sampler::Sampler(Isolate* isolate, int interval)
1270 : isolate_(isolate), 1260 : isolate_(isolate),
1271 interval_(interval), 1261 interval_(interval),
1272 profiling_(false), 1262 profiling_(false),
1273 active_(false), 1263 active_(false),
1274 samples_taken_(0) { 1264 samples_taken_(0) {
1275 data_ = new PlatformData; 1265 data_ = new PlatformData;
1276 } 1266 }
1277 1267
1278 1268
1279 Sampler::~Sampler() { 1269 Sampler::~Sampler() {
1280 ASSERT(!IsActive()); 1270 ASSERT(!IsActive());
1281 delete data_; 1271 delete data_;
1282 } 1272 }
1283 1273
1284 1274
1275 void Sampler::DoSample() {
1276 platform_data()->SendProfilingSignal();
1277 }
1278
1279
1285 void Sampler::Start() { 1280 void Sampler::Start() {
1286 ASSERT(!IsActive()); 1281 ASSERT(!IsActive());
1282 CpuProfilerSignalHandler::InstallSignalHandler();
1287 SetActive(true); 1283 SetActive(true);
1288 SignalSender::AddActiveSampler(this); 1284 SignalSender::AddActiveSampler(this);
1289 } 1285 }
1290 1286
1291 1287
1292 void Sampler::Stop() { 1288 void Sampler::Stop() {
1293 ASSERT(IsActive()); 1289 ASSERT(IsActive());
1290 CpuProfilerSignalHandler::RestoreSignalHandler();
1294 SignalSender::RemoveActiveSampler(this); 1291 SignalSender::RemoveActiveSampler(this);
1295 SetActive(false); 1292 SetActive(false);
1296 } 1293 }
1297 1294
1298 1295
1299 } } // namespace v8::internal 1296 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/platform-freebsd.cc ('k') | src/platform-macos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698