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

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 a flag to switch between old and new versions Created 8 years, 4 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
« src/cpu-profiler.cc ('K') | « src/platform.h ('k') | no next file » | 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 InstallSignalHandler() {
1061 if (signal_handler_installed_) return;
1062 struct sigaction sa;
1063 sa.sa_sigaction = ProfilerSignalHandler;
1064 sigemptyset(&sa.sa_mask);
1065 sa.sa_flags = SA_RESTART | SA_SIGINFO;
1066 signal_handler_installed_ =
1067 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0);
1068 }
1069
1070 static void RestoreSignalHandler() {
1071 if (signal_handler_installed_) {
1072 sigaction(SIGPROF, &old_signal_handler_, 0);
1073 signal_handler_installed_ = false;
1074 }
1075 }
1076
1077 static bool signal_handler_installed() { return signal_handler_installed_; }
1078
1079 private:
1080 static bool signal_handler_installed_;
1081 static struct sigaction old_signal_handler_;
1082 };
1083
1084
1085 bool CpuProfilerSignalHandler::signal_handler_installed_ = false;
1086 struct sigaction CpuProfilerSignalHandler::old_signal_handler_;
1087
1088
1058 class Sampler::PlatformData : public Malloced { 1089 class Sampler::PlatformData : public Malloced {
1059 public: 1090 public:
1060 PlatformData() : vm_tid_(GetThreadID()) {} 1091 PlatformData() : vm_tid_(GetThreadID()) {}
1061 1092
1062 int vm_tid() const { return vm_tid_; } 1093 int vm_tid() const { return vm_tid_; }
1063 1094
1064 private: 1095 private:
1065 const int vm_tid_; 1096 const int vm_tid_;
1066 }; 1097 };
1067 1098
1068 1099
1069 class SignalSender : public Thread { 1100 class SignalSender : public Thread {
1070 public: 1101 public:
1071 enum SleepInterval { 1102 enum SleepInterval {
1072 HALF_INTERVAL, 1103 HALF_INTERVAL,
1073 FULL_INTERVAL 1104 FULL_INTERVAL
1074 }; 1105 };
1075 1106
1076 static const int kSignalSenderStackSize = 64 * KB; 1107 static const int kSignalSenderStackSize = 64 * KB;
1077 1108
1078 explicit SignalSender(int interval) 1109 explicit SignalSender(int interval)
1079 : Thread(Thread::Options("SignalSender", kSignalSenderStackSize)), 1110 : Thread(Thread::Options("SignalSender", kSignalSenderStackSize)),
1080 vm_tgid_(getpid()), 1111 vm_tgid_(getpid()),
1081 interval_(interval) {} 1112 interval_(interval) {}
1082 1113
1083 static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); } 1114 static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); }
1084 static void TearDown() { delete mutex_; } 1115 static void TearDown() { delete mutex_; }
1085 1116
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) { 1117 static void AddActiveSampler(Sampler* sampler) {
1103 ScopedLock lock(mutex_); 1118 ScopedLock lock(mutex_);
1104 SamplerRegistry::AddActiveSampler(sampler); 1119 SamplerRegistry::AddActiveSampler(sampler);
1105 if (instance_ == NULL) { 1120 if (instance_ == NULL) {
1106 // Start a thread that will send SIGPROF signal to VM threads, 1121 // Start a thread that will send SIGPROF signal to VM threads,
1107 // when CPU profiling will be enabled. 1122 // when CPU profiling will be enabled.
1108 instance_ = new SignalSender(sampler->interval()); 1123 instance_ = new SignalSender(sampler->interval());
1109 instance_->Start(); 1124 instance_->Start();
1110 } else { 1125 } else {
1111 ASSERT(instance_->interval_ == sampler->interval()); 1126 ASSERT(instance_->interval_ == sampler->interval());
1112 } 1127 }
1113 } 1128 }
1114 1129
1115 static void RemoveActiveSampler(Sampler* sampler) { 1130 static void RemoveActiveSampler(Sampler* sampler) {
1116 ScopedLock lock(mutex_); 1131 ScopedLock lock(mutex_);
1117 SamplerRegistry::RemoveActiveSampler(sampler); 1132 SamplerRegistry::RemoveActiveSampler(sampler);
1118 if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) { 1133 if (SamplerRegistry::GetState() == SamplerRegistry::HAS_NO_SAMPLERS) {
1119 RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_); 1134 RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_);
1120 delete instance_; 1135 delete instance_;
1121 instance_ = NULL; 1136 instance_ = NULL;
1122 RestoreSignalHandler();
1123 } 1137 }
1124 } 1138 }
1125 1139
1126 // Implement Thread::Run(). 1140 // Implement Thread::Run().
1127 virtual void Run() { 1141 virtual void Run() {
1128 SamplerRegistry::State state; 1142 SamplerRegistry::State state;
1129 while ((state = SamplerRegistry::GetState()) != 1143 while ((state = SamplerRegistry::GetState()) !=
1130 SamplerRegistry::HAS_NO_SAMPLERS) { 1144 SamplerRegistry::HAS_NO_SAMPLERS) {
1131 bool cpu_profiling_enabled = 1145 bool cpu_profiling_enabled =
1132 (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS); 1146 (!FLAG_sample_stack_in_postprocessor_thread && state == SamplerRegistr y::HAS_CPU_PROFILING_SAMPLERS);
1133 bool runtime_profiler_enabled = RuntimeProfiler::IsEnabled(); 1147 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 1148 // When CPU profiling is enabled both JavaScript and C++ code is
1140 // profiled. We must not suspend. 1149 // profiled. We must not suspend.
1141 if (!cpu_profiling_enabled) { 1150 if (!cpu_profiling_enabled) {
1142 if (rate_limiter_.SuspendIfNecessary()) continue; 1151 if (rate_limiter_.SuspendIfNecessary()) continue;
1143 } 1152 }
1144 if (cpu_profiling_enabled && runtime_profiler_enabled) { 1153 if (cpu_profiling_enabled && runtime_profiler_enabled) {
1145 if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this)) { 1154 if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this)) {
1146 return; 1155 return;
1147 } 1156 }
1148 Sleep(HALF_INTERVAL); 1157 Sleep(HALF_INTERVAL);
(...skipping 24 matching lines...) Expand all
1173 SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender); 1182 SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender);
1174 sender->SendProfilingSignal(sampler->platform_data()->vm_tid()); 1183 sender->SendProfilingSignal(sampler->platform_data()->vm_tid());
1175 } 1184 }
1176 1185
1177 static void DoRuntimeProfile(Sampler* sampler, void* ignored) { 1186 static void DoRuntimeProfile(Sampler* sampler, void* ignored) {
1178 if (!sampler->isolate()->IsInitialized()) return; 1187 if (!sampler->isolate()->IsInitialized()) return;
1179 sampler->isolate()->runtime_profiler()->NotifyTick(); 1188 sampler->isolate()->runtime_profiler()->NotifyTick();
1180 } 1189 }
1181 1190
1182 void SendProfilingSignal(int tid) { 1191 void SendProfilingSignal(int tid) {
1183 if (!signal_handler_installed_) return; 1192 if (!CpuProfilerSignalHandler::signal_handler_installed()) return;
1184 // Glibc doesn't provide a wrapper for tgkill(2). 1193 // Glibc doesn't provide a wrapper for tgkill(2).
1185 #if defined(ANDROID) 1194 #if defined(ANDROID)
1186 syscall(__NR_tgkill, vm_tgid_, tid, SIGPROF); 1195 syscall(__NR_tgkill, vm_tgid_, tid, SIGPROF);
1187 #else 1196 #else
1188 syscall(SYS_tgkill, vm_tgid_, tid, SIGPROF); 1197 syscall(SYS_tgkill, vm_tgid_, tid, SIGPROF);
1189 #endif 1198 #endif
1190 } 1199 }
1191 1200
1192 void Sleep(SleepInterval full_or_half) { 1201 void Sleep(SleepInterval full_or_half) {
1193 // Convert ms to us and subtract 100 us to compensate delays 1202 // Convert ms to us and subtract 100 us to compensate delays
(...skipping 17 matching lines...) Expand all
1211 #endif // ANDROID 1220 #endif // ANDROID
1212 } 1221 }
1213 1222
1214 const int vm_tgid_; 1223 const int vm_tgid_;
1215 const int interval_; 1224 const int interval_;
1216 RuntimeProfilerRateLimiter rate_limiter_; 1225 RuntimeProfilerRateLimiter rate_limiter_;
1217 1226
1218 // Protects the process wide state below. 1227 // Protects the process wide state below.
1219 static Mutex* mutex_; 1228 static Mutex* mutex_;
1220 static SignalSender* instance_; 1229 static SignalSender* instance_;
1221 static bool signal_handler_installed_;
1222 static struct sigaction old_signal_handler_;
1223 1230
1224 private: 1231 private:
1225 DISALLOW_COPY_AND_ASSIGN(SignalSender); 1232 DISALLOW_COPY_AND_ASSIGN(SignalSender);
1226 }; 1233 };
1227 1234
1228 1235
1229 Mutex* SignalSender::mutex_ = NULL; 1236 Mutex* SignalSender::mutex_ = NULL;
1230 SignalSender* SignalSender::instance_ = NULL; 1237 SignalSender* SignalSender::instance_ = NULL;
1231 struct sigaction SignalSender::old_signal_handler_;
1232 bool SignalSender::signal_handler_installed_ = false;
1233 1238
1234 1239
1235 void OS::SetUp() { 1240 void OS::SetUp() {
1236 // Seed the random number generator. We preserve microsecond resolution. 1241 // Seed the random number generator. We preserve microsecond resolution.
1237 uint64_t seed = Ticks() ^ (getpid() << 16); 1242 uint64_t seed = Ticks() ^ (getpid() << 16);
1238 srandom(static_cast<unsigned int>(seed)); 1243 srandom(static_cast<unsigned int>(seed));
1239 limit_mutex = CreateMutex(); 1244 limit_mutex = CreateMutex();
1240 1245
1241 #ifdef __arm__ 1246 #ifdef __arm__
1242 // When running on ARM hardware check that the EABI used by V8 and 1247 // When running on ARM hardware check that the EABI used by V8 and
1243 // by the C code is the same. 1248 // by the C code is the same.
1244 bool hard_float = OS::ArmUsingHardFloat(); 1249 bool hard_float = OS::ArmUsingHardFloat();
1245 if (hard_float) { 1250 if (hard_float) {
1246 #if !USE_EABI_HARDFLOAT 1251 #if !USE_EABI_HARDFLOAT
1247 PrintF("ERROR: Binary compiled with -mfloat-abi=hard but without " 1252 PrintF("ERROR: Binary compiled with -mfloat-abi=hard but without "
1248 "-DUSE_EABI_HARDFLOAT\n"); 1253 "-DUSE_EABI_HARDFLOAT\n");
1249 exit(1); 1254 exit(1);
1250 #endif 1255 #endif
1251 } else { 1256 } else {
1252 #if USE_EABI_HARDFLOAT 1257 #if USE_EABI_HARDFLOAT
1253 PrintF("ERROR: Binary not compiled with -mfloat-abi=hard but with " 1258 PrintF("ERROR: Binary not compiled with -mfloat-abi=hard but with "
1254 "-DUSE_EABI_HARDFLOAT\n"); 1259 "-DUSE_EABI_HARDFLOAT\n");
1255 exit(1); 1260 exit(1);
1256 #endif 1261 #endif
1257 } 1262 }
1258 #endif 1263 #endif
1259 SignalSender::SetUp(); 1264 SignalSender::SetUp();
1265 CpuProfilerSignalHandler::InstallSignalHandler();
1260 } 1266 }
1261 1267
1262 1268
1263 void OS::TearDown() { 1269 void OS::TearDown() {
1264 SignalSender::TearDown(); 1270 SignalSender::TearDown();
1271 CpuProfilerSignalHandler::RestoreSignalHandler();
1265 delete limit_mutex; 1272 delete limit_mutex;
1266 } 1273 }
1267 1274
1268 1275
1269 Sampler::Sampler(Isolate* isolate, int interval) 1276 Sampler::Sampler(Isolate* isolate, int interval)
1270 : isolate_(isolate), 1277 : isolate_(isolate),
1271 interval_(interval), 1278 interval_(interval),
1272 profiling_(false), 1279 profiling_(false),
1273 active_(false), 1280 active_(false),
1274 samples_taken_(0) { 1281 samples_taken_(0) {
(...skipping 14 matching lines...) Expand all
1289 } 1296 }
1290 1297
1291 1298
1292 void Sampler::Stop() { 1299 void Sampler::Stop() {
1293 ASSERT(IsActive()); 1300 ASSERT(IsActive());
1294 SignalSender::RemoveActiveSampler(this); 1301 SignalSender::RemoveActiveSampler(this);
1295 SetActive(false); 1302 SetActive(false);
1296 } 1303 }
1297 1304
1298 1305
1306 class CpuProfilerThread::PlatformData : public Malloced {
1307 public:
1308 PlatformData() : vm_tgid_(getpid()) {}
1309
1310 int vm_tgid() const { return vm_tgid_; }
1311
1312 private:
1313 const int vm_tgid_;
1314 };
1315
1316
1317 CpuProfilerThread::CpuProfilerThread(Sampler* sampler)
1318 : Thread(Thread::Options("CpuProfiler", kCpuProfilerThreadStackSize)),
1319 sampler_(sampler) {
1320 data_ = new PlatformData;
1321 }
1322
1323
1324 CpuProfilerThread::~CpuProfilerThread() {
1325 delete data_;
1326 }
1327
1328
1329 void CpuProfilerThread::DoSample() {
1330 int tid = sampler_->platform_data()->vm_tid();
1331 int tgid = data_->vm_tgid();
1332 if (!CpuProfilerSignalHandler::signal_handler_installed()) return;
1333 // Glibc doesn't provide a wrapper for tgkill(2).
1334 #if defined(ANDROID)
1335 syscall(__NR_tgkill, tgid, tid, SIGPROF);
1336 #else
1337 syscall(SYS_tgkill, tgid, tid, SIGPROF);
caseq 2012/08/17 12:56:35 looks like we could either use a tkill() or tgkill
1338 #endif
1339 }
1340
1341
1299 } } // namespace v8::internal 1342 } } // namespace v8::internal
OLDNEW
« src/cpu-profiler.cc ('K') | « src/platform.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698