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

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 set sampling rate 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 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 =
1132 (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS);
1133 bool runtime_profiler_enabled = RuntimeProfiler::IsEnabled(); 1154 bool runtime_profiler_enabled = RuntimeProfiler::IsEnabled();
1134 if (cpu_profiling_enabled && !signal_handler_installed_) { 1155 if (rate_limiter_.SuspendIfNecessary()) continue;
1135 InstallSignalHandler(); 1156 if (runtime_profiler_enabled) {
1136 } else if (!cpu_profiling_enabled && signal_handler_installed_) { 1157 if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile,
1137 RestoreSignalHandler(); 1158 NULL)) {
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; 1159 return;
1147 } 1160 }
1148 Sleep(HALF_INTERVAL);
1149 if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile, NULL)) {
1150 return;
1151 }
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 } 1161 }
1162 Sleep(FULL_INTERVAL);
1168 } 1163 }
1169 } 1164 }
1170 1165
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) { 1166 static void DoRuntimeProfile(Sampler* sampler, void* ignored) {
1178 if (!sampler->isolate()->IsInitialized()) return; 1167 if (!sampler->isolate()->IsInitialized()) return;
1179 sampler->isolate()->runtime_profiler()->NotifyTick(); 1168 sampler->isolate()->runtime_profiler()->NotifyTick();
1180 } 1169 }
1181 1170
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) { 1171 void Sleep(SleepInterval full_or_half) {
1193 // Convert ms to us and subtract 100 us to compensate delays 1172 // Convert ms to us and subtract 100 us to compensate delays
1194 // occuring during signal delivery. 1173 // occuring during signal delivery.
1195 useconds_t interval = interval_ * 1000 - 100; 1174 useconds_t interval = interval_ * 1000 - 100;
1196 if (full_or_half == HALF_INTERVAL) interval /= 2; 1175 if (full_or_half == HALF_INTERVAL) interval /= 2;
1197 #if defined(ANDROID) 1176 #if defined(ANDROID)
1198 usleep(interval); 1177 usleep(interval);
1199 #else 1178 #else
1200 int result = usleep(interval); 1179 int result = usleep(interval);
1201 #ifdef DEBUG 1180 #ifdef DEBUG
1202 if (result != 0 && errno != EINTR) { 1181 if (result != 0 && errno != EINTR) {
1203 fprintf(stderr, 1182 fprintf(stderr,
1204 "SignalSender usleep error; interval = %u, errno = %d\n", 1183 "SignalSender usleep error; interval = %u, errno = %d\n",
1205 interval, 1184 interval,
1206 errno); 1185 errno);
1207 ASSERT(result == 0 || errno == EINTR); 1186 ASSERT(result == 0 || errno == EINTR);
1208 } 1187 }
1209 #endif // DEBUG 1188 #endif // DEBUG
1210 USE(result); 1189 USE(result);
1211 #endif // ANDROID 1190 #endif // ANDROID
1212 } 1191 }
1213 1192
1214 const int vm_tgid_;
1215 const int interval_; 1193 const int interval_;
1216 RuntimeProfilerRateLimiter rate_limiter_; 1194 RuntimeProfilerRateLimiter rate_limiter_;
1217 1195
1218 // Protects the process wide state below. 1196 // Protects the process wide state below.
1219 static Mutex* mutex_; 1197 static Mutex* mutex_;
1220 static SignalSender* instance_; 1198 static SignalSender* instance_;
1221 static bool signal_handler_installed_;
1222 static struct sigaction old_signal_handler_;
1223 1199
1224 private: 1200 private:
1225 DISALLOW_COPY_AND_ASSIGN(SignalSender); 1201 DISALLOW_COPY_AND_ASSIGN(SignalSender);
1226 }; 1202 };
1227 1203
1228 1204
1229 Mutex* SignalSender::mutex_ = NULL; 1205 Mutex* SignalSender::mutex_ = NULL;
1230 SignalSender* SignalSender::instance_ = NULL; 1206 SignalSender* SignalSender::instance_ = NULL;
1231 struct sigaction SignalSender::old_signal_handler_;
1232 bool SignalSender::signal_handler_installed_ = false;
1233 1207
1234 1208
1235 void OS::SetUp() { 1209 void OS::SetUp() {
1236 // Seed the random number generator. We preserve microsecond resolution. 1210 // Seed the random number generator. We preserve microsecond resolution.
1237 uint64_t seed = Ticks() ^ (getpid() << 16); 1211 uint64_t seed = Ticks() ^ (getpid() << 16);
1238 srandom(static_cast<unsigned int>(seed)); 1212 srandom(static_cast<unsigned int>(seed));
1239 limit_mutex = CreateMutex(); 1213 limit_mutex = CreateMutex();
1240 1214
1241 #ifdef __arm__ 1215 #ifdef __arm__
1242 // When running on ARM hardware check that the EABI used by V8 and 1216 // When running on ARM hardware check that the EABI used by V8 and
1243 // by the C code is the same. 1217 // by the C code is the same.
1244 bool hard_float = OS::ArmUsingHardFloat(); 1218 bool hard_float = OS::ArmUsingHardFloat();
1245 if (hard_float) { 1219 if (hard_float) {
1246 #if !USE_EABI_HARDFLOAT 1220 #if !USE_EABI_HARDFLOAT
1247 PrintF("ERROR: Binary compiled with -mfloat-abi=hard but without " 1221 PrintF("ERROR: Binary compiled with -mfloat-abi=hard but without "
1248 "-DUSE_EABI_HARDFLOAT\n"); 1222 "-DUSE_EABI_HARDFLOAT\n");
1249 exit(1); 1223 exit(1);
1250 #endif 1224 #endif
1251 } else { 1225 } else {
1252 #if USE_EABI_HARDFLOAT 1226 #if USE_EABI_HARDFLOAT
1253 PrintF("ERROR: Binary not compiled with -mfloat-abi=hard but with " 1227 PrintF("ERROR: Binary not compiled with -mfloat-abi=hard but with "
1254 "-DUSE_EABI_HARDFLOAT\n"); 1228 "-DUSE_EABI_HARDFLOAT\n");
1255 exit(1); 1229 exit(1);
1256 #endif 1230 #endif
1257 } 1231 }
1258 #endif 1232 #endif
1259 SignalSender::SetUp(); 1233 SignalSender::SetUp();
1234 CpuProfilerSignalHandler::InstallSignalHandler();
caseq 2012/09/03 11:59:58 Can we do this as necessary -- e.g. when a first p
1260 } 1235 }
1261 1236
1262 1237
1263 void OS::TearDown() { 1238 void OS::TearDown() {
1264 SignalSender::TearDown(); 1239 SignalSender::TearDown();
1240 CpuProfilerSignalHandler::RestoreSignalHandler();
caseq 2012/09/03 11:59:58 ditto
1265 delete limit_mutex; 1241 delete limit_mutex;
1266 } 1242 }
1267 1243
1268 1244
1269 Sampler::Sampler(Isolate* isolate, int interval) 1245 Sampler::Sampler(Isolate* isolate, int interval)
1270 : isolate_(isolate), 1246 : isolate_(isolate),
1271 interval_(interval), 1247 interval_(interval),
1272 profiling_(false), 1248 profiling_(false),
1273 active_(false), 1249 active_(false),
1274 samples_taken_(0) { 1250 samples_taken_(0) {
1275 data_ = new PlatformData; 1251 data_ = new PlatformData;
1276 } 1252 }
1277 1253
1278 1254
1279 Sampler::~Sampler() { 1255 Sampler::~Sampler() {
1280 ASSERT(!IsActive()); 1256 ASSERT(!IsActive());
1281 delete data_; 1257 delete data_;
1282 } 1258 }
1283 1259
1284 1260
1261 void Sampler::DoSample() {
1262 platform_data()->SendProfilingSignal();
1263 }
1264
1265
1285 void Sampler::Start() { 1266 void Sampler::Start() {
1286 ASSERT(!IsActive()); 1267 ASSERT(!IsActive());
1287 SetActive(true); 1268 SetActive(true);
1288 SignalSender::AddActiveSampler(this); 1269 SignalSender::AddActiveSampler(this);
1289 } 1270 }
1290 1271
1291 1272
1292 void Sampler::Stop() { 1273 void Sampler::Stop() {
1293 ASSERT(IsActive()); 1274 ASSERT(IsActive());
1294 SignalSender::RemoveActiveSampler(this); 1275 SignalSender::RemoveActiveSampler(this);
1295 SetActive(false); 1276 SetActive(false);
1296 } 1277 }
1297 1278
1298 1279
1299 } } // namespace v8::internal 1280 } } // 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