Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/profiler/sampler.h" | 5 #include "src/libsampler/v8-sampler.h" |
| 6 | 6 |
| 7 #if V8_OS_POSIX && !V8_OS_CYGWIN | 7 #if V8_OS_POSIX && !V8_OS_CYGWIN |
| 8 | 8 |
| 9 #define USE_SIGNALS | 9 #define USE_SIGNALS |
| 10 | 10 |
| 11 #include <errno.h> | 11 #include <errno.h> |
| 12 #include <pthread.h> | 12 #include <pthread.h> |
| 13 #include <signal.h> | 13 #include <signal.h> |
| 14 #include <sys/time.h> | 14 #include <sys/time.h> |
| 15 | 15 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 37 #endif | 37 #endif |
| 38 | 38 |
| 39 #elif V8_OS_WIN || V8_OS_CYGWIN | 39 #elif V8_OS_WIN || V8_OS_CYGWIN |
| 40 | 40 |
| 41 #include "src/base/win32-headers.h" | 41 #include "src/base/win32-headers.h" |
| 42 | 42 |
| 43 #endif | 43 #endif |
| 44 | 44 |
| 45 #include "src/atomic-utils.h" | 45 #include "src/atomic-utils.h" |
| 46 #include "src/base/platform/platform.h" | 46 #include "src/base/platform/platform.h" |
| 47 #include "src/flags.h" | 47 #include "src/hashmap.h" |
| 48 #include "src/frames-inl.h" | 48 #include "src/isolate.h" |
| 49 #include "src/log.h" | |
| 50 #include "src/profiler/cpu-profiler-inl.h" | |
| 51 #include "src/simulator.h" | |
| 52 #include "src/v8threads.h" | |
| 53 #include "src/vm-state-inl.h" | |
| 54 | 49 |
| 55 | 50 |
| 56 #if V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) | 51 #if V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) |
| 57 | 52 |
| 58 // Not all versions of Android's C library provide ucontext_t. | 53 // Not all versions of Android's C library provide ucontext_t. |
| 59 // Detect this and provide custom but compatible definitions. Note that these | 54 // Detect this and provide custom but compatible definitions. Note that these |
| 60 // follow the GLibc naming convention to access register values from | 55 // follow the GLibc naming convention to access register values from |
| 61 // mcontext_t. | 56 // mcontext_t. |
| 62 // | 57 // |
| 63 // See http://code.google.com/p/android/issues/detail?id=34784 | 58 // See http://code.google.com/p/android/issues/detail?id=34784 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 mcontext_t uc_mcontext; | 146 mcontext_t uc_mcontext; |
| 152 // Other fields are not used by V8, don't define them here. | 147 // Other fields are not used by V8, don't define them here. |
| 153 } ucontext_t; | 148 } ucontext_t; |
| 154 enum { REG_RBP = 10, REG_RSP = 15, REG_RIP = 16 }; | 149 enum { REG_RBP = 10, REG_RSP = 15, REG_RIP = 16 }; |
| 155 #endif | 150 #endif |
| 156 | 151 |
| 157 #endif // V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) | 152 #endif // V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) |
| 158 | 153 |
| 159 | 154 |
| 160 namespace v8 { | 155 namespace v8 { |
| 161 namespace internal { | |
| 162 | 156 |
| 163 namespace { | 157 namespace { |
| 164 | 158 |
| 165 class PlatformDataCommon : public Malloced { | 159 static const int kMaxFramesCountLog2 = 8; |
| 160 static const unsigned kMaxFramesCount = (1u << kMaxFramesCountLog2) - 1; | |
| 161 | |
| 162 class PlatformDataCommon : public i::Malloced { | |
| 166 public: | 163 public: |
| 167 PlatformDataCommon() : profiled_thread_id_(ThreadId::Current()) {} | 164 PlatformDataCommon() : profiled_thread_id_(i::ThreadId::Current()) {} |
| 168 ThreadId profiled_thread_id() { return profiled_thread_id_; } | 165 i::ThreadId profiled_thread_id() { return profiled_thread_id_; } |
| 169 | 166 |
| 170 protected: | 167 protected: |
| 171 ~PlatformDataCommon() {} | 168 ~PlatformDataCommon() {} |
| 172 | 169 |
| 173 private: | 170 private: |
| 174 ThreadId profiled_thread_id_; | 171 i::ThreadId profiled_thread_id_; |
| 175 }; | 172 }; |
| 176 | 173 |
| 177 | 174 |
| 178 bool IsSamePage(byte* ptr1, byte* ptr2) { | 175 #if defined(USE_SIGNALS) |
| 179 const uint32_t kPageSize = 4096; | 176 typedef internal::List<Sampler*> SamplerList; |
| 180 uintptr_t mask = ~static_cast<uintptr_t>(kPageSize - 1); | |
| 181 return (reinterpret_cast<uintptr_t>(ptr1) & mask) == | |
| 182 (reinterpret_cast<uintptr_t>(ptr2) & mask); | |
| 183 } | |
| 184 | 177 |
| 185 | |
| 186 // Check if the code at specified address could potentially be a | |
| 187 // frame setup code. | |
| 188 bool IsNoFrameRegion(Address address) { | |
| 189 struct Pattern { | |
| 190 int bytes_count; | |
| 191 byte bytes[8]; | |
| 192 int offsets[4]; | |
| 193 }; | |
| 194 byte* pc = reinterpret_cast<byte*>(address); | |
| 195 static Pattern patterns[] = { | |
| 196 #if V8_HOST_ARCH_IA32 | |
| 197 // push %ebp | |
| 198 // mov %esp,%ebp | |
| 199 {3, {0x55, 0x89, 0xe5}, {0, 1, -1}}, | |
| 200 // pop %ebp | |
| 201 // ret N | |
| 202 {2, {0x5d, 0xc2}, {0, 1, -1}}, | |
| 203 // pop %ebp | |
| 204 // ret | |
| 205 {2, {0x5d, 0xc3}, {0, 1, -1}}, | |
| 206 #elif V8_HOST_ARCH_X64 | |
| 207 // pushq %rbp | |
| 208 // movq %rsp,%rbp | |
| 209 {4, {0x55, 0x48, 0x89, 0xe5}, {0, 1, -1}}, | |
| 210 // popq %rbp | |
| 211 // ret N | |
| 212 {2, {0x5d, 0xc2}, {0, 1, -1}}, | |
| 213 // popq %rbp | |
| 214 // ret | |
| 215 {2, {0x5d, 0xc3}, {0, 1, -1}}, | |
| 216 #endif | |
| 217 {0, {}, {}} | |
| 218 }; | |
| 219 for (Pattern* pattern = patterns; pattern->bytes_count; ++pattern) { | |
| 220 for (int* offset_ptr = pattern->offsets; *offset_ptr != -1; ++offset_ptr) { | |
| 221 int offset = *offset_ptr; | |
| 222 if (!offset || IsSamePage(pc, pc - offset)) { | |
| 223 MSAN_MEMORY_IS_INITIALIZED(pc - offset, pattern->bytes_count); | |
| 224 if (!memcmp(pc - offset, pattern->bytes, pattern->bytes_count)) | |
| 225 return true; | |
| 226 } else { | |
| 227 // It is not safe to examine bytes on another page as it might not be | |
| 228 // allocated thus causing a SEGFAULT. | |
| 229 // Check the pattern part that's on the same page and | |
| 230 // pessimistically assume it could be the entire pattern match. | |
| 231 MSAN_MEMORY_IS_INITIALIZED(pc, pattern->bytes_count - offset); | |
| 232 if (!memcmp(pc, pattern->bytes + offset, pattern->bytes_count - offset)) | |
| 233 return true; | |
| 234 } | |
| 235 } | |
| 236 } | |
| 237 return false; | |
| 238 } | |
| 239 | |
| 240 typedef List<Sampler*> SamplerList; | |
| 241 | |
| 242 #if defined(USE_SIGNALS) | |
| 243 class AtomicGuard { | 178 class AtomicGuard { |
| 244 public: | 179 public: |
| 245 explicit AtomicGuard(AtomicValue<int>* atomic, bool is_block = true) | 180 explicit AtomicGuard(i::AtomicValue<int>* atomic, bool is_block = true) |
| 246 : atomic_(atomic), | 181 : atomic_(atomic), |
| 247 is_success_(false) { | 182 is_success_(false) { |
| 248 do { | 183 do { |
| 249 // Use Acquire_Load to gain mutual exclusion. | 184 // Use Acquire_Load to gain mutual exclusion. |
| 250 USE(atomic_->Value()); | 185 USE(atomic_->Value()); |
| 251 is_success_ = atomic_->TrySetValue(0, 1); | 186 is_success_ = atomic_->TrySetValue(0, 1); |
| 252 } while (is_block && !is_success_); | 187 } while (is_block && !is_success_); |
| 253 } | 188 } |
| 254 | 189 |
| 255 bool is_success() { return is_success_; } | 190 bool is_success() { return is_success_; } |
| 256 | 191 |
| 257 ~AtomicGuard() { | 192 ~AtomicGuard() { |
| 258 if (is_success_) { | 193 if (is_success_) { |
| 259 atomic_->SetValue(0); | 194 atomic_->SetValue(0); |
| 260 } | 195 } |
| 261 atomic_ = NULL; | 196 atomic_ = NULL; |
| 262 } | 197 } |
| 263 | 198 |
| 264 private: | 199 private: |
| 265 AtomicValue<int>* atomic_; | 200 i::AtomicValue<int>* atomic_; |
| 266 bool is_success_; | 201 bool is_success_; |
| 267 }; | 202 }; |
| 268 | 203 |
| 269 | 204 |
| 270 // Returns key for hash map. | 205 // Returns key for hash map. |
| 271 void* ThreadKey(pthread_t thread_id) { | 206 void* ThreadKey(pthread_t thread_id) { |
| 272 return reinterpret_cast<void*>(thread_id); | 207 return reinterpret_cast<void*>(thread_id); |
| 273 } | 208 } |
| 274 | 209 |
| 275 | 210 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 289 | 224 |
| 290 class Sampler::PlatformData : public PlatformDataCommon { | 225 class Sampler::PlatformData : public PlatformDataCommon { |
| 291 public: | 226 public: |
| 292 PlatformData() : vm_tid_(pthread_self()) {} | 227 PlatformData() : vm_tid_(pthread_self()) {} |
| 293 pthread_t vm_tid() const { return vm_tid_; } | 228 pthread_t vm_tid() const { return vm_tid_; } |
| 294 | 229 |
| 295 private: | 230 private: |
| 296 pthread_t vm_tid_; | 231 pthread_t vm_tid_; |
| 297 }; | 232 }; |
| 298 | 233 |
| 234 | |
| 235 class SamplerManager { | |
|
fmeawad
2016/05/02 23:14:37
maybe SamplingManager?
| |
| 236 public: | |
| 237 static void AddSampler(Sampler* sampler) { | |
| 238 AtomicGuard atomic_guard(&samplers_access_counter_); | |
| 239 DCHECK(sampler->IsActive()); | |
| 240 // Add sampler into map if needed. | |
| 241 pthread_t thread_id = sampler->platform_data()->vm_tid(); | |
| 242 i::HashMap::Entry *entry = | |
| 243 thread_id_to_samplers_.Pointer()->LookupOrInsert(ThreadKey(thread_id), | |
| 244 ThreadHash(thread_id)); | |
| 245 if (entry->value == NULL) { | |
| 246 SamplerList* samplers = new SamplerList(); | |
| 247 samplers->Add(sampler); | |
| 248 entry->value = samplers; | |
| 249 } else { | |
| 250 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); | |
| 251 if (!samplers->Contains(sampler)) { | |
| 252 samplers->Add(sampler); | |
| 253 } | |
| 254 } | |
| 255 } | |
| 256 | |
| 257 static void RemoveSampler(Sampler* sampler) { | |
| 258 AtomicGuard atomic_guard(&samplers_access_counter_); | |
| 259 DCHECK(sampler->IsActive()); | |
| 260 // Remove sampler from map. | |
| 261 pthread_t thread_id = sampler->platform_data()->vm_tid(); | |
| 262 void* thread_key = ThreadKey(thread_id); | |
| 263 uint32_t thread_hash = ThreadHash(thread_id); | |
| 264 i::HashMap::Entry* entry = | |
| 265 thread_id_to_samplers_.Get().Lookup(thread_key, thread_hash); | |
| 266 DCHECK(entry != NULL); | |
| 267 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); | |
| 268 samplers->RemoveElement(sampler); | |
| 269 if (samplers->is_empty()) { | |
| 270 thread_id_to_samplers_.Pointer()->Remove(thread_key, thread_hash); | |
| 271 delete samplers; | |
| 272 } | |
| 273 } | |
| 274 | |
| 275 private: | |
| 276 struct HashMapCreateTrait { | |
| 277 static void Construct(internal::HashMap* allocated_ptr) { | |
| 278 new (allocated_ptr) internal::HashMap(internal::HashMap::PointersMatch); | |
| 279 } | |
| 280 }; | |
| 281 friend class SignalHandler; | |
| 282 static base::LazyInstance<internal::HashMap, HashMapCreateTrait>::type | |
| 283 thread_id_to_samplers_; | |
| 284 static i::AtomicValue<int> samplers_access_counter_; | |
| 285 }; | |
| 286 | |
| 287 | |
| 288 base::LazyInstance<i::HashMap, SamplerManager::HashMapCreateTrait>::type | |
| 289 SamplerManager::thread_id_to_samplers_ = LAZY_INSTANCE_INITIALIZER; | |
| 290 i::AtomicValue<int> SamplerManager::samplers_access_counter_(0); | |
| 291 | |
| 292 | |
| 299 #elif V8_OS_WIN || V8_OS_CYGWIN | 293 #elif V8_OS_WIN || V8_OS_CYGWIN |
| 300 | 294 |
| 301 // ---------------------------------------------------------------------------- | 295 // ---------------------------------------------------------------------------- |
| 302 // Win32 profiler support. On Cygwin we use the same sampler implementation as | 296 // Win32 profiler support. On Cygwin we use the same sampler implementation as |
| 303 // on Win32. | 297 // on Win32. |
| 304 | 298 |
| 305 class Sampler::PlatformData : public PlatformDataCommon { | 299 class Sampler::PlatformData : public PlatformDataCommon { |
| 306 public: | 300 public: |
| 307 // Get a handle to the calling thread. This is the thread that we are | 301 // Get a handle to the calling thread. This is the thread that we are |
| 308 // going to profile. We need to make a copy of the handle because we are | 302 // going to profile. We need to make a copy of the handle because we are |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 321 CloseHandle(profiled_thread_); | 315 CloseHandle(profiled_thread_); |
| 322 profiled_thread_ = NULL; | 316 profiled_thread_ = NULL; |
| 323 } | 317 } |
| 324 } | 318 } |
| 325 | 319 |
| 326 HANDLE profiled_thread() { return profiled_thread_; } | 320 HANDLE profiled_thread() { return profiled_thread_; } |
| 327 | 321 |
| 328 private: | 322 private: |
| 329 HANDLE profiled_thread_; | 323 HANDLE profiled_thread_; |
| 330 }; | 324 }; |
| 331 #endif | 325 #endif // USE_SIGNALS |
| 332 | |
| 333 | |
| 334 #if defined(USE_SIMULATOR) | |
| 335 bool SimulatorHelper::FillRegisters(Isolate* isolate, | |
| 336 v8::RegisterState* state) { | |
| 337 Simulator *simulator = isolate->thread_local_top()->simulator_; | |
| 338 // Check if there is active simulator. | |
| 339 if (simulator == NULL) return false; | |
| 340 #if V8_TARGET_ARCH_ARM | |
| 341 if (!simulator->has_bad_pc()) { | |
| 342 state->pc = reinterpret_cast<Address>(simulator->get_pc()); | |
| 343 } | |
| 344 state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp)); | |
| 345 state->fp = reinterpret_cast<Address>(simulator->get_register( | |
| 346 Simulator::r11)); | |
| 347 #elif V8_TARGET_ARCH_ARM64 | |
| 348 state->pc = reinterpret_cast<Address>(simulator->pc()); | |
| 349 state->sp = reinterpret_cast<Address>(simulator->sp()); | |
| 350 state->fp = reinterpret_cast<Address>(simulator->fp()); | |
| 351 #elif V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 | |
| 352 if (!simulator->has_bad_pc()) { | |
| 353 state->pc = reinterpret_cast<Address>(simulator->get_pc()); | |
| 354 } | |
| 355 state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp)); | |
| 356 state->fp = reinterpret_cast<Address>(simulator->get_register(Simulator::fp)); | |
| 357 #elif V8_TARGET_ARCH_PPC | |
| 358 if (!simulator->has_bad_pc()) { | |
| 359 state->pc = reinterpret_cast<Address>(simulator->get_pc()); | |
| 360 } | |
| 361 state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp)); | |
| 362 state->fp = reinterpret_cast<Address>(simulator->get_register(Simulator::fp)); | |
| 363 #elif V8_TARGET_ARCH_S390 | |
| 364 if (!simulator->has_bad_pc()) { | |
| 365 state->pc = reinterpret_cast<Address>(simulator->get_pc()); | |
| 366 } | |
| 367 state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp)); | |
| 368 state->fp = reinterpret_cast<Address>(simulator->get_register(Simulator::fp)); | |
| 369 #endif | |
| 370 if (state->sp == 0 || state->fp == 0) { | |
| 371 // It possible that the simulator is interrupted while it is updating | |
| 372 // the sp or fp register. ARM64 simulator does this in two steps: | |
| 373 // first setting it to zero and then setting it to the new value. | |
| 374 // Bailout if sp/fp doesn't contain the new value. | |
| 375 // | |
| 376 // FIXME: The above doesn't really solve the issue. | |
| 377 // If a 64-bit target is executed on a 32-bit host even the final | |
| 378 // write is non-atomic, so it might obtain a half of the result. | |
| 379 // Moreover as long as the register set code uses memcpy (as of now), | |
| 380 // it is not guaranteed to be atomic even when both host and target | |
| 381 // are of same bitness. | |
| 382 return false; | |
| 383 } | |
| 384 return true; | |
| 385 } | |
| 386 #endif // USE_SIMULATOR | |
| 387 | 326 |
| 388 | 327 |
| 389 #if defined(USE_SIGNALS) | 328 #if defined(USE_SIGNALS) |
| 390 | 329 class SignalHandler : public i::AllStatic { |
| 391 class SignalHandler : public AllStatic { | |
| 392 public: | 330 public: |
| 393 static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } | 331 static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } |
| 394 static void TearDown() { delete mutex_; mutex_ = NULL; } | 332 static void TearDown() { delete mutex_; mutex_ = NULL; } |
| 395 | 333 |
| 396 static void IncreaseSamplerCount() { | 334 static void IncreaseSamplerCount() { |
| 397 base::LockGuard<base::Mutex> lock_guard(mutex_); | 335 base::LockGuard<base::Mutex> lock_guard(mutex_); |
| 398 if (++client_count_ == 1) Install(); | 336 if (++client_count_ == 1) Install(); |
| 399 } | 337 } |
| 400 | 338 |
| 401 static void DecreaseSamplerCount() { | 339 static void DecreaseSamplerCount() { |
| 402 base::LockGuard<base::Mutex> lock_guard(mutex_); | 340 base::LockGuard<base::Mutex> lock_guard(mutex_); |
| 403 if (--client_count_ == 0) Restore(); | 341 if (--client_count_ == 0) Restore(); |
| 404 } | 342 } |
| 405 | 343 |
| 406 static bool Installed() { | 344 static bool Installed() { |
| 407 return signal_handler_installed_; | 345 return signal_handler_installed_; |
| 408 } | 346 } |
| 409 | 347 |
| 410 #if !V8_OS_NACL | |
| 411 static void CollectSample(void* context, Sampler* sampler); | |
| 412 #endif | |
| 413 | |
| 414 private: | 348 private: |
| 415 static void Install() { | 349 static void Install() { |
| 416 #if !V8_OS_NACL | 350 #if !V8_OS_NACL |
| 417 struct sigaction sa; | 351 struct sigaction sa; |
| 418 sa.sa_sigaction = &HandleProfilerSignal; | 352 sa.sa_sigaction = &HandleProfilerSignal; |
| 419 sigemptyset(&sa.sa_mask); | 353 sigemptyset(&sa.sa_mask); |
| 420 #if V8_OS_QNX | 354 #if V8_OS_QNX |
| 421 sa.sa_flags = SA_SIGINFO; | 355 sa.sa_flags = SA_SIGINFO; |
| 422 #else | 356 #else |
| 423 sa.sa_flags = SA_RESTART | SA_SIGINFO; | 357 sa.sa_flags = SA_RESTART | SA_SIGINFO; |
| 424 #endif | 358 #endif |
| 425 signal_handler_installed_ = | 359 signal_handler_installed_ = |
| 426 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); | 360 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); |
| 427 #endif | 361 #endif // !V8_OS_NACL |
| 428 } | 362 } |
| 429 | 363 |
| 430 static void Restore() { | 364 static void Restore() { |
| 431 #if !V8_OS_NACL | 365 #if !V8_OS_NACL |
| 432 if (signal_handler_installed_) { | 366 if (signal_handler_installed_) { |
| 433 sigaction(SIGPROF, &old_signal_handler_, 0); | 367 sigaction(SIGPROF, &old_signal_handler_, 0); |
| 434 signal_handler_installed_ = false; | 368 signal_handler_installed_ = false; |
| 435 } | 369 } |
| 436 #endif | 370 #endif |
| 437 } | 371 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 448 | 382 |
| 449 | 383 |
| 450 base::Mutex* SignalHandler::mutex_ = NULL; | 384 base::Mutex* SignalHandler::mutex_ = NULL; |
| 451 int SignalHandler::client_count_ = 0; | 385 int SignalHandler::client_count_ = 0; |
| 452 struct sigaction SignalHandler::old_signal_handler_; | 386 struct sigaction SignalHandler::old_signal_handler_; |
| 453 bool SignalHandler::signal_handler_installed_ = false; | 387 bool SignalHandler::signal_handler_installed_ = false; |
| 454 | 388 |
| 455 | 389 |
| 456 // As Native Client does not support signal handling, profiling is disabled. | 390 // As Native Client does not support signal handling, profiling is disabled. |
| 457 #if !V8_OS_NACL | 391 #if !V8_OS_NACL |
| 458 void SignalHandler::CollectSample(void* context, Sampler* sampler) { | |
| 459 if (sampler == NULL || (!sampler->IsProfiling() && | |
| 460 !sampler->IsRegistered())) { | |
| 461 return; | |
| 462 } | |
| 463 Isolate* isolate = sampler->isolate(); | |
| 464 | |
| 465 // We require a fully initialized and entered isolate. | |
| 466 if (isolate == NULL || !isolate->IsInUse()) return; | |
| 467 | |
| 468 if (v8::Locker::IsActive() && | |
| 469 !isolate->thread_manager()->IsLockedByCurrentThread()) { | |
| 470 return; | |
| 471 } | |
| 472 | |
| 473 v8::RegisterState state; | |
| 474 | |
| 475 #if defined(USE_SIMULATOR) | |
| 476 if (!SimulatorHelper::FillRegisters(isolate, &state)) return; | |
| 477 #else | |
| 478 // Extracting the sample from the context is extremely machine dependent. | |
| 479 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); | |
| 480 #if !(V8_OS_OPENBSD || (V8_OS_LINUX && (V8_HOST_ARCH_PPC || V8_HOST_ARCH_S390))) | |
| 481 mcontext_t& mcontext = ucontext->uc_mcontext; | |
| 482 #endif | |
| 483 #if V8_OS_LINUX | |
| 484 #if V8_HOST_ARCH_IA32 | |
| 485 state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]); | |
| 486 state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]); | |
| 487 state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]); | |
| 488 #elif V8_HOST_ARCH_X64 | |
| 489 state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]); | |
| 490 state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]); | |
| 491 state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]); | |
| 492 #elif V8_HOST_ARCH_ARM | |
| 493 #if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) | |
| 494 // Old GLibc ARM versions used a gregs[] array to access the register | |
| 495 // values from mcontext_t. | |
| 496 state.pc = reinterpret_cast<Address>(mcontext.gregs[R15]); | |
| 497 state.sp = reinterpret_cast<Address>(mcontext.gregs[R13]); | |
| 498 state.fp = reinterpret_cast<Address>(mcontext.gregs[R11]); | |
| 499 #else | |
| 500 state.pc = reinterpret_cast<Address>(mcontext.arm_pc); | |
| 501 state.sp = reinterpret_cast<Address>(mcontext.arm_sp); | |
| 502 state.fp = reinterpret_cast<Address>(mcontext.arm_fp); | |
| 503 #endif // V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) | |
| 504 #elif V8_HOST_ARCH_ARM64 | |
| 505 state.pc = reinterpret_cast<Address>(mcontext.pc); | |
| 506 state.sp = reinterpret_cast<Address>(mcontext.sp); | |
| 507 // FP is an alias for x29. | |
| 508 state.fp = reinterpret_cast<Address>(mcontext.regs[29]); | |
| 509 #elif V8_HOST_ARCH_MIPS | |
| 510 state.pc = reinterpret_cast<Address>(mcontext.pc); | |
| 511 state.sp = reinterpret_cast<Address>(mcontext.gregs[29]); | |
| 512 state.fp = reinterpret_cast<Address>(mcontext.gregs[30]); | |
| 513 #elif V8_HOST_ARCH_MIPS64 | |
| 514 state.pc = reinterpret_cast<Address>(mcontext.pc); | |
| 515 state.sp = reinterpret_cast<Address>(mcontext.gregs[29]); | |
| 516 state.fp = reinterpret_cast<Address>(mcontext.gregs[30]); | |
| 517 #elif V8_HOST_ARCH_PPC | |
| 518 state.pc = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->nip); | |
| 519 state.sp = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->gpr[PT_R1]); | |
| 520 state.fp = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->gpr[PT_R31]); | |
| 521 #elif V8_HOST_ARCH_S390 | |
| 522 #if V8_TARGET_ARCH_32_BIT | |
| 523 // 31-bit target will have bit 0 (MSB) of the PSW set to denote addressing | |
| 524 // mode. This bit needs to be masked out to resolve actual address. | |
| 525 state.pc = | |
| 526 reinterpret_cast<Address>(ucontext->uc_mcontext.psw.addr & 0x7FFFFFFF); | |
| 527 #else | |
| 528 state.pc = reinterpret_cast<Address>(ucontext->uc_mcontext.psw.addr); | |
| 529 #endif // V8_TARGET_ARCH_32_BIT | |
| 530 state.sp = reinterpret_cast<Address>(ucontext->uc_mcontext.gregs[15]); | |
| 531 state.fp = reinterpret_cast<Address>(ucontext->uc_mcontext.gregs[11]); | |
| 532 #endif // V8_HOST_ARCH_* | |
| 533 #elif V8_OS_MACOSX | |
| 534 #if V8_HOST_ARCH_X64 | |
| 535 #if __DARWIN_UNIX03 | |
| 536 state.pc = reinterpret_cast<Address>(mcontext->__ss.__rip); | |
| 537 state.sp = reinterpret_cast<Address>(mcontext->__ss.__rsp); | |
| 538 state.fp = reinterpret_cast<Address>(mcontext->__ss.__rbp); | |
| 539 #else // !__DARWIN_UNIX03 | |
| 540 state.pc = reinterpret_cast<Address>(mcontext->ss.rip); | |
| 541 state.sp = reinterpret_cast<Address>(mcontext->ss.rsp); | |
| 542 state.fp = reinterpret_cast<Address>(mcontext->ss.rbp); | |
| 543 #endif // __DARWIN_UNIX03 | |
| 544 #elif V8_HOST_ARCH_IA32 | |
| 545 #if __DARWIN_UNIX03 | |
| 546 state.pc = reinterpret_cast<Address>(mcontext->__ss.__eip); | |
| 547 state.sp = reinterpret_cast<Address>(mcontext->__ss.__esp); | |
| 548 state.fp = reinterpret_cast<Address>(mcontext->__ss.__ebp); | |
| 549 #else // !__DARWIN_UNIX03 | |
| 550 state.pc = reinterpret_cast<Address>(mcontext->ss.eip); | |
| 551 state.sp = reinterpret_cast<Address>(mcontext->ss.esp); | |
| 552 state.fp = reinterpret_cast<Address>(mcontext->ss.ebp); | |
| 553 #endif // __DARWIN_UNIX03 | |
| 554 #endif // V8_HOST_ARCH_IA32 | |
| 555 #elif V8_OS_FREEBSD | |
| 556 #if V8_HOST_ARCH_IA32 | |
| 557 state.pc = reinterpret_cast<Address>(mcontext.mc_eip); | |
| 558 state.sp = reinterpret_cast<Address>(mcontext.mc_esp); | |
| 559 state.fp = reinterpret_cast<Address>(mcontext.mc_ebp); | |
| 560 #elif V8_HOST_ARCH_X64 | |
| 561 state.pc = reinterpret_cast<Address>(mcontext.mc_rip); | |
| 562 state.sp = reinterpret_cast<Address>(mcontext.mc_rsp); | |
| 563 state.fp = reinterpret_cast<Address>(mcontext.mc_rbp); | |
| 564 #elif V8_HOST_ARCH_ARM | |
| 565 state.pc = reinterpret_cast<Address>(mcontext.mc_r15); | |
| 566 state.sp = reinterpret_cast<Address>(mcontext.mc_r13); | |
| 567 state.fp = reinterpret_cast<Address>(mcontext.mc_r11); | |
| 568 #endif // V8_HOST_ARCH_* | |
| 569 #elif V8_OS_NETBSD | |
| 570 #if V8_HOST_ARCH_IA32 | |
| 571 state.pc = reinterpret_cast<Address>(mcontext.__gregs[_REG_EIP]); | |
| 572 state.sp = reinterpret_cast<Address>(mcontext.__gregs[_REG_ESP]); | |
| 573 state.fp = reinterpret_cast<Address>(mcontext.__gregs[_REG_EBP]); | |
| 574 #elif V8_HOST_ARCH_X64 | |
| 575 state.pc = reinterpret_cast<Address>(mcontext.__gregs[_REG_RIP]); | |
| 576 state.sp = reinterpret_cast<Address>(mcontext.__gregs[_REG_RSP]); | |
| 577 state.fp = reinterpret_cast<Address>(mcontext.__gregs[_REG_RBP]); | |
| 578 #endif // V8_HOST_ARCH_* | |
| 579 #elif V8_OS_OPENBSD | |
| 580 #if V8_HOST_ARCH_IA32 | |
| 581 state.pc = reinterpret_cast<Address>(ucontext->sc_eip); | |
| 582 state.sp = reinterpret_cast<Address>(ucontext->sc_esp); | |
| 583 state.fp = reinterpret_cast<Address>(ucontext->sc_ebp); | |
| 584 #elif V8_HOST_ARCH_X64 | |
| 585 state.pc = reinterpret_cast<Address>(ucontext->sc_rip); | |
| 586 state.sp = reinterpret_cast<Address>(ucontext->sc_rsp); | |
| 587 state.fp = reinterpret_cast<Address>(ucontext->sc_rbp); | |
| 588 #endif // V8_HOST_ARCH_* | |
| 589 #elif V8_OS_SOLARIS | |
| 590 state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_PC]); | |
| 591 state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_SP]); | |
| 592 state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_FP]); | |
| 593 #elif V8_OS_QNX | |
| 594 #if V8_HOST_ARCH_IA32 | |
| 595 state.pc = reinterpret_cast<Address>(mcontext.cpu.eip); | |
| 596 state.sp = reinterpret_cast<Address>(mcontext.cpu.esp); | |
| 597 state.fp = reinterpret_cast<Address>(mcontext.cpu.ebp); | |
| 598 #elif V8_HOST_ARCH_ARM | |
| 599 state.pc = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_PC]); | |
| 600 state.sp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_SP]); | |
| 601 state.fp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_FP]); | |
| 602 #endif // V8_HOST_ARCH_* | |
| 603 #elif V8_OS_AIX | |
| 604 state.pc = reinterpret_cast<Address>(mcontext.jmp_context.iar); | |
| 605 state.sp = reinterpret_cast<Address>(mcontext.jmp_context.gpr[1]); | |
| 606 state.fp = reinterpret_cast<Address>(mcontext.jmp_context.gpr[31]); | |
| 607 #endif // V8_OS_AIX | |
| 608 #endif // USE_SIMULATOR | |
| 609 sampler->SampleStack(state); | |
| 610 } | |
| 611 #endif // V8_OS_NACL | |
| 612 | |
| 613 #endif // USE_SIGNALS | |
| 614 | |
| 615 | |
| 616 class SamplerThread : public base::Thread { | |
| 617 public: | |
| 618 static const int kSamplerThreadStackSize = 64 * KB; | |
| 619 | |
| 620 explicit SamplerThread(int interval) | |
| 621 : Thread(base::Thread::Options("SamplerThread", kSamplerThreadStackSize)), | |
| 622 interval_(interval) {} | |
| 623 | |
| 624 static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } | |
| 625 static void TearDown() { delete mutex_; mutex_ = NULL; } | |
| 626 | |
| 627 static void AddActiveSampler(Sampler* sampler) { | |
| 628 bool need_to_start = false; | |
| 629 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
| 630 if (instance_ == NULL) { | |
| 631 // Start a thread that will send SIGPROF signal to VM threads, | |
| 632 // when CPU profiling will be enabled. | |
| 633 instance_ = new SamplerThread(sampler->interval()); | |
| 634 need_to_start = true; | |
| 635 } | |
| 636 | |
| 637 DCHECK(sampler->IsActive()); | |
| 638 DCHECK(instance_->interval_ == sampler->interval()); | |
| 639 | |
| 640 #if defined(USE_SIGNALS) | |
| 641 AddSampler(sampler); | |
| 642 #else | |
| 643 DCHECK(!instance_->active_samplers_.Contains(sampler)); | |
| 644 instance_->active_samplers_.Add(sampler); | |
| 645 #endif // USE_SIGNALS | |
| 646 | |
| 647 if (need_to_start) instance_->StartSynchronously(); | |
| 648 } | |
| 649 | |
| 650 static void RemoveSampler(Sampler* sampler) { | |
| 651 SamplerThread* instance_to_remove = NULL; | |
| 652 { | |
| 653 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
| 654 | |
| 655 DCHECK(sampler->IsActive() || sampler->IsRegistered()); | |
| 656 #if defined(USE_SIGNALS) | |
| 657 { | |
| 658 AtomicGuard atomic_guard(&sampler_list_access_counter_); | |
| 659 // Remove sampler from map. | |
| 660 pthread_t thread_id = sampler->platform_data()->vm_tid(); | |
| 661 void* thread_key = ThreadKey(thread_id); | |
| 662 uint32_t thread_hash = ThreadHash(thread_id); | |
| 663 HashMap::Entry* entry = | |
| 664 thread_id_to_samplers_.Get().Lookup(thread_key, thread_hash); | |
| 665 DCHECK(entry != NULL); | |
| 666 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); | |
| 667 samplers->RemoveElement(sampler); | |
| 668 if (samplers->is_empty()) { | |
| 669 thread_id_to_samplers_.Pointer()->Remove(thread_key, thread_hash); | |
| 670 delete samplers; | |
| 671 } | |
| 672 if (thread_id_to_samplers_.Get().occupancy() == 0) { | |
| 673 instance_to_remove = instance_; | |
| 674 instance_ = NULL; | |
| 675 } | |
| 676 } | |
| 677 #else | |
| 678 bool removed = instance_->active_samplers_.RemoveElement(sampler); | |
| 679 DCHECK(removed); | |
| 680 USE(removed); | |
| 681 | |
| 682 // We cannot delete the instance immediately as we need to Join() the | |
| 683 // thread but we are holding mutex_ and the thread may try to acquire it. | |
| 684 if (instance_->active_samplers_.is_empty()) { | |
| 685 instance_to_remove = instance_; | |
| 686 instance_ = NULL; | |
| 687 } | |
| 688 #endif // USE_SIGNALS | |
| 689 } | |
| 690 | |
| 691 if (!instance_to_remove) return; | |
| 692 instance_to_remove->Join(); | |
| 693 delete instance_to_remove; | |
| 694 } | |
| 695 | |
| 696 // Unlike AddActiveSampler, this method only adds a sampler, | |
| 697 // but won't start the sampler thread. | |
| 698 static void RegisterSampler(Sampler* sampler) { | |
| 699 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
| 700 #if defined(USE_SIGNALS) | |
| 701 AddSampler(sampler); | |
| 702 #endif // USE_SIGNALS | |
| 703 } | |
| 704 | |
| 705 // Implement Thread::Run(). | |
| 706 virtual void Run() { | |
| 707 while (true) { | |
| 708 { | |
| 709 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
| 710 #if defined(USE_SIGNALS) | |
| 711 if (thread_id_to_samplers_.Get().occupancy() == 0) break; | |
| 712 if (SignalHandler::Installed()) { | |
| 713 for (HashMap::Entry *p = thread_id_to_samplers_.Get().Start(); | |
| 714 p != NULL; p = thread_id_to_samplers_.Get().Next(p)) { | |
| 715 pthread_t thread_id = reinterpret_cast<pthread_t>(p->key); | |
| 716 pthread_kill(thread_id, SIGPROF); | |
| 717 } | |
| 718 } | |
| 719 #else | |
| 720 if (active_samplers_.is_empty()) break; | |
| 721 // When CPU profiling is enabled both JavaScript and C++ code is | |
| 722 // profiled. We must not suspend. | |
| 723 for (int i = 0; i < active_samplers_.length(); ++i) { | |
| 724 Sampler* sampler = active_samplers_.at(i); | |
| 725 if (!sampler->IsProfiling()) continue; | |
| 726 sampler->DoSample(); | |
| 727 } | |
| 728 #endif // USE_SIGNALS | |
| 729 } | |
| 730 base::OS::Sleep(base::TimeDelta::FromMilliseconds(interval_)); | |
| 731 } | |
| 732 } | |
| 733 | |
| 734 private: | |
| 735 // Protects the process wide state below. | |
| 736 static base::Mutex* mutex_; | |
| 737 static SamplerThread* instance_; | |
| 738 | |
| 739 const int interval_; | |
| 740 | |
| 741 #if defined(USE_SIGNALS) | |
| 742 struct HashMapCreateTrait { | |
| 743 static void Construct(HashMap* allocated_ptr) { | |
| 744 new (allocated_ptr) HashMap(HashMap::PointersMatch); | |
| 745 } | |
| 746 }; | |
| 747 friend class SignalHandler; | |
| 748 static base::LazyInstance<HashMap, HashMapCreateTrait>::type | |
| 749 thread_id_to_samplers_; | |
| 750 static AtomicValue<int> sampler_list_access_counter_; | |
| 751 static void AddSampler(Sampler* sampler) { | |
| 752 AtomicGuard atomic_guard(&sampler_list_access_counter_); | |
| 753 // Add sampler into map if needed. | |
| 754 pthread_t thread_id = sampler->platform_data()->vm_tid(); | |
| 755 HashMap::Entry *entry = | |
| 756 thread_id_to_samplers_.Pointer()->LookupOrInsert(ThreadKey(thread_id), | |
| 757 ThreadHash(thread_id)); | |
| 758 if (entry->value == NULL) { | |
| 759 SamplerList* samplers = new SamplerList(); | |
| 760 samplers->Add(sampler); | |
| 761 entry->value = samplers; | |
| 762 } else { | |
| 763 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); | |
| 764 if (!samplers->Contains(sampler)) { | |
| 765 samplers->Add(sampler); | |
| 766 } | |
| 767 } | |
| 768 } | |
| 769 #else | |
| 770 SamplerList active_samplers_; | |
| 771 #endif // USE_SIGNALS | |
| 772 | |
| 773 DISALLOW_COPY_AND_ASSIGN(SamplerThread); | |
| 774 }; | |
| 775 | |
| 776 | |
| 777 base::Mutex* SamplerThread::mutex_ = NULL; | |
| 778 SamplerThread* SamplerThread::instance_ = NULL; | |
| 779 #if defined(USE_SIGNALS) | |
| 780 base::LazyInstance<HashMap, SamplerThread::HashMapCreateTrait>::type | |
| 781 SamplerThread::thread_id_to_samplers_ = LAZY_INSTANCE_INITIALIZER; | |
| 782 AtomicValue<int> SamplerThread::sampler_list_access_counter_(0); | |
| 783 | |
| 784 // As Native Client does not support signal handling, profiling is disabled. | |
| 785 #if !V8_OS_NACL | |
| 786 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, | 392 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, |
| 787 void* context) { | 393 void* context) { |
| 788 USE(info); | 394 USE(info); |
| 789 if (signal != SIGPROF) return; | 395 if (signal != SIGPROF) return; |
| 790 AtomicGuard atomic_guard(&SamplerThread::sampler_list_access_counter_, false); | 396 AtomicGuard atomic_guard(&SamplerManager::samplers_access_counter_, false); |
| 791 if (!atomic_guard.is_success()) return; | 397 if (!atomic_guard.is_success()) return; |
| 792 pthread_t thread_id = pthread_self(); | 398 pthread_t thread_id = pthread_self(); |
| 793 HashMap::Entry* entry = | 399 i::HashMap::Entry* entry = |
| 794 SamplerThread::thread_id_to_samplers_.Pointer()->Lookup( | 400 SamplerManager::thread_id_to_samplers_.Pointer()->Lookup( |
| 795 ThreadKey(thread_id), ThreadHash(thread_id)); | 401 ThreadKey(thread_id), ThreadHash(thread_id)); |
| 796 if (entry == NULL) | 402 if (entry == NULL) |
| 797 return; | 403 return; |
| 798 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); | 404 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); |
| 799 for (int i = 0; i < samplers->length(); ++i) { | 405 for (int i = 0; i < samplers->length(); ++i) { |
| 800 Sampler* sampler = samplers->at(i); | 406 Sampler* sampler = samplers->at(i); |
| 801 CollectSample(context, sampler); | 407 if (sampler == NULL || !sampler->IsProfiling()) { |
| 408 return; | |
| 409 } | |
| 410 Isolate* isolate = sampler->isolate(); | |
| 411 | |
| 412 // We require a fully initialized and entered isolate. | |
| 413 if (isolate == NULL || !isolate->IsInUse()) return; | |
| 414 | |
| 415 if (v8::Locker::IsActive() && !Locker::IsLocked(isolate)) { | |
| 416 return; | |
| 417 } | |
| 418 | |
| 419 v8::RegisterState state; | |
|
alph
2016/05/03 18:48:07
You can move register filling out of the loop. Mor
lpy
2016/05/04 00:09:24
Done.
| |
| 420 | |
| 421 // Extracting the sample from the context is extremely machine dependent. | |
| 422 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); | |
| 423 #if !(V8_OS_OPENBSD || (V8_OS_LINUX && (V8_HOST_ARCH_PPC || V8_HOST_ARCH_S390))) | |
| 424 mcontext_t& mcontext = ucontext->uc_mcontext; | |
| 425 #endif | |
| 426 #if V8_OS_LINUX | |
| 427 #if V8_HOST_ARCH_IA32 | |
| 428 state.pc = reinterpret_cast<void*>(mcontext.gregs[REG_EIP]); | |
| 429 state.sp = reinterpret_cast<void*>(mcontext.gregs[REG_ESP]); | |
| 430 state.fp = reinterpret_cast<void*>(mcontext.gregs[REG_EBP]); | |
| 431 #elif V8_HOST_ARCH_X64 | |
| 432 state.pc = reinterpret_cast<void*>(mcontext.gregs[REG_RIP]); | |
| 433 state.sp = reinterpret_cast<void*>(mcontext.gregs[REG_RSP]); | |
| 434 state.fp = reinterpret_cast<void*>(mcontext.gregs[REG_RBP]); | |
| 435 #elif V8_HOST_ARCH_ARM | |
| 436 #if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) | |
| 437 // Old GLibc ARM versions used a gregs[] array to access the register | |
| 438 // values from mcontext_t. | |
| 439 state.pc = reinterpret_cast<void*>(mcontext.gregs[R15]); | |
| 440 state.sp = reinterpret_cast<void*>(mcontext.gregs[R13]); | |
| 441 state.fp = reinterpret_cast<void*>(mcontext.gregs[R11]); | |
| 442 #else | |
| 443 state.pc = reinterpret_cast<void*>(mcontext.arm_pc); | |
| 444 state.sp = reinterpret_cast<void*>(mcontext.arm_sp); | |
| 445 state.fp = reinterpret_cast<void*>(mcontext.arm_fp); | |
| 446 #endif // V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) | |
| 447 #elif V8_HOST_ARCH_ARM64 | |
| 448 state.pc = reinterpret_cast<void*>(mcontext.pc); | |
| 449 state.sp = reinterpret_cast<void*>(mcontext.sp); | |
| 450 // FP is an alias for x29. | |
| 451 state.fp = reinterpret_cast<void*>(mcontext.regs[29]); | |
| 452 #elif V8_HOST_ARCH_MIPS | |
| 453 state.pc = reinterpret_cast<void*>(mcontext.pc); | |
| 454 state.sp = reinterpret_cast<void*>(mcontext.gregs[29]); | |
| 455 state.fp = reinterpret_cast<void*>(mcontext.gregs[30]); | |
| 456 #elif V8_HOST_ARCH_MIPS64 | |
| 457 state.pc = reinterpret_cast<void*>(mcontext.pc); | |
| 458 state.sp = reinterpret_cast<void*>(mcontext.gregs[29]); | |
| 459 state.fp = reinterpret_cast<void*>(mcontext.gregs[30]); | |
| 460 #elif V8_HOST_ARCH_PPC | |
| 461 state.pc = reinterpret_cast<void*>(ucontext->uc_mcontext.regs->nip); | |
| 462 state.sp = | |
| 463 reinterpret_cast<void*>(ucontext->uc_mcontext.regs->gpr[PT_R1]); | |
| 464 state.fp = | |
| 465 reinterpret_cast<void*>(ucontext->uc_mcontext.regs->gpr[PT_R31]); | |
| 466 #elif V8_HOST_ARCH_S390 | |
| 467 #if V8_TARGET_ARCH_32_BIT | |
| 468 // 31-bit target will have bit 0 (MSB) of the PSW set to denote addressing | |
| 469 // mode. This bit needs to be masked out to resolve actual address. | |
| 470 state.pc = | |
| 471 reinterpret_cast<void*>(ucontext->uc_mcontext.psw.addr & 0x7FFFFFFF); | |
| 472 #else | |
| 473 state.pc = reinterpret_cast<void*>(ucontext->uc_mcontext.psw.addr); | |
| 474 #endif // V8_TARGET_ARCH_32_BIT | |
| 475 state.sp = reinterpret_cast<void*>(ucontext->uc_mcontext.gregs[15]); | |
| 476 state.fp = reinterpret_cast<void*>(ucontext->uc_mcontext.gregs[11]); | |
| 477 #endif // V8_HOST_ARCH_* | |
| 478 #elif V8_OS_MACOSX | |
| 479 #if V8_HOST_ARCH_X64 | |
| 480 #if __DARWIN_UNIX03 | |
| 481 state.pc = reinterpret_cast<void*>(mcontext->__ss.__rip); | |
| 482 state.sp = reinterpret_cast<void*>(mcontext->__ss.__rsp); | |
| 483 state.fp = reinterpret_cast<void*>(mcontext->__ss.__rbp); | |
| 484 #else // !__DARWIN_UNIX03 | |
| 485 state.pc = reinterpret_cast<void*>(mcontext->ss.rip); | |
| 486 state.sp = reinterpret_cast<void*>(mcontext->ss.rsp); | |
| 487 state.fp = reinterpret_cast<void*>(mcontext->ss.rbp); | |
| 488 #endif // __DARWIN_UNIX03 | |
| 489 #elif V8_HOST_ARCH_IA32 | |
| 490 #if __DARWIN_UNIX03 | |
| 491 state.pc = reinterpret_cast<void*>(mcontext->__ss.__eip); | |
| 492 state.sp = reinterpret_cast<void*>(mcontext->__ss.__esp); | |
| 493 state.fp = reinterpret_cast<void*>(mcontext->__ss.__ebp); | |
| 494 #else // !__DARWIN_UNIX03 | |
| 495 state.pc = reinterpret_cast<void*>(mcontext->ss.eip); | |
| 496 state.sp = reinterpret_cast<void*>(mcontext->ss.esp); | |
| 497 state.fp = reinterpret_cast<void*>(mcontext->ss.ebp); | |
| 498 #endif // __DARWIN_UNIX03 | |
| 499 #endif // V8_HOST_ARCH_IA32 | |
| 500 #elif V8_OS_FREEBSD | |
| 501 #if V8_HOST_ARCH_IA32 | |
| 502 state.pc = reinterpret_cast<void*>(mcontext.mc_eip); | |
| 503 state.sp = reinterpret_cast<void*>(mcontext.mc_esp); | |
| 504 state.fp = reinterpret_cast<void*>(mcontext.mc_ebp); | |
| 505 #elif V8_HOST_ARCH_X64 | |
| 506 state.pc = reinterpret_cast<void*>(mcontext.mc_rip); | |
| 507 state.sp = reinterpret_cast<void*>(mcontext.mc_rsp); | |
| 508 state.fp = reinterpret_cast<void*>(mcontext.mc_rbp); | |
| 509 #elif V8_HOST_ARCH_ARM | |
| 510 state.pc = reinterpret_cast<void*>(mcontext.mc_r15); | |
| 511 state.sp = reinterpret_cast<void*>(mcontext.mc_r13); | |
| 512 state.fp = reinterpret_cast<void*>(mcontext.mc_r11); | |
| 513 #endif // V8_HOST_ARCH_* | |
| 514 #elif V8_OS_NETBSD | |
| 515 #if V8_HOST_ARCH_IA32 | |
| 516 state.pc = reinterpret_cast<void*>(mcontext.__gregs[_REG_EIP]); | |
| 517 state.sp = reinterpret_cast<void*>(mcontext.__gregs[_REG_ESP]); | |
| 518 state.fp = reinterpret_cast<void*>(mcontext.__gregs[_REG_EBP]); | |
| 519 #elif V8_HOST_ARCH_X64 | |
| 520 state.pc = reinterpret_cast<void*>(mcontext.__gregs[_REG_RIP]); | |
| 521 state.sp = reinterpret_cast<void*>(mcontext.__gregs[_REG_RSP]); | |
| 522 state.fp = reinterpret_cast<void*>(mcontext.__gregs[_REG_RBP]); | |
| 523 #endif // V8_HOST_ARCH_* | |
| 524 #elif V8_OS_OPENBSD | |
| 525 #if V8_HOST_ARCH_IA32 | |
| 526 state.pc = reinterpret_cast<void*>(ucontext->sc_eip); | |
| 527 state.sp = reinterpret_cast<void*>(ucontext->sc_esp); | |
| 528 state.fp = reinterpret_cast<void*>(ucontext->sc_ebp); | |
| 529 #elif V8_HOST_ARCH_X64 | |
| 530 state.pc = reinterpret_cast<void*>(ucontext->sc_rip); | |
| 531 state.sp = reinterpret_cast<void*>(ucontext->sc_rsp); | |
| 532 state.fp = reinterpret_cast<void*>(ucontext->sc_rbp); | |
| 533 #endif // V8_HOST_ARCH_* | |
| 534 #elif V8_OS_SOLARIS | |
| 535 state.pc = reinterpret_cast<void*>(mcontext.gregs[REG_PC]); | |
| 536 state.sp = reinterpret_cast<void*>(mcontext.gregs[REG_SP]); | |
| 537 state.fp = reinterpret_cast<void*>(mcontext.gregs[REG_FP]); | |
| 538 #elif V8_OS_QNX | |
| 539 #if V8_HOST_ARCH_IA32 | |
| 540 state.pc = reinterpret_cast<void*>(mcontext.cpu.eip); | |
| 541 state.sp = reinterpret_cast<void*>(mcontext.cpu.esp); | |
| 542 state.fp = reinterpret_cast<void*>(mcontext.cpu.ebp); | |
| 543 #elif V8_HOST_ARCH_ARM | |
| 544 state.pc = reinterpret_cast<void*>(mcontext.cpu.gpr[ARM_REG_PC]); | |
| 545 state.sp = reinterpret_cast<void*>(mcontext.cpu.gpr[ARM_REG_SP]); | |
| 546 state.fp = reinterpret_cast<void*>(mcontext.cpu.gpr[ARM_REG_FP]); | |
| 547 #endif // V8_HOST_ARCH_* | |
| 548 #elif V8_OS_AIX | |
| 549 state.pc = reinterpret_cast<void*>(mcontext.jmp_context.iar); | |
| 550 state.sp = reinterpret_cast<void*>(mcontext.jmp_context.gpr[1]); | |
| 551 state.fp = reinterpret_cast<void*>(mcontext.jmp_context.gpr[31]); | |
| 552 #endif // V8_OS_AIX | |
| 553 sampler->SampleStack(state); | |
| 802 } | 554 } |
| 803 } | 555 } |
| 804 #endif // !V8_OS_NACL | 556 #endif // V8_OS_NACL |
| 805 #endif // USE_SIGNALs | |
| 806 | 557 |
| 807 | 558 #endif // USE_SIGNALS |
| 808 // | |
| 809 // StackTracer implementation | |
| 810 // | |
| 811 DISABLE_ASAN void TickSample::Init(Isolate* isolate, | |
| 812 const v8::RegisterState& regs, | |
| 813 RecordCEntryFrame record_c_entry_frame, | |
| 814 bool update_stats) { | |
| 815 timestamp = base::TimeTicks::HighResolutionNow(); | |
| 816 pc = reinterpret_cast<Address>(regs.pc); | |
| 817 state = isolate->current_vm_state(); | |
| 818 this->update_stats = update_stats; | |
| 819 | |
| 820 // Avoid collecting traces while doing GC. | |
| 821 if (state == GC) return; | |
| 822 | |
| 823 Address js_entry_sp = isolate->js_entry_sp(); | |
| 824 if (js_entry_sp == 0) return; // Not executing JS now. | |
| 825 | |
| 826 if (pc && IsNoFrameRegion(pc)) { | |
| 827 // Can't collect stack. Mark the sample as spoiled. | |
| 828 timestamp = base::TimeTicks(); | |
| 829 pc = 0; | |
| 830 return; | |
| 831 } | |
| 832 | |
| 833 ExternalCallbackScope* scope = isolate->external_callback_scope(); | |
| 834 Address handler = Isolate::handler(isolate->thread_local_top()); | |
| 835 // If there is a handler on top of the external callback scope then | |
| 836 // we have already entrered JavaScript again and the external callback | |
| 837 // is not the top function. | |
| 838 if (scope && scope->scope_address() < handler) { | |
| 839 external_callback_entry = *scope->callback_entrypoint_address(); | |
| 840 has_external_callback = true; | |
| 841 } else { | |
| 842 // sp register may point at an arbitrary place in memory, make | |
| 843 // sure MSAN doesn't complain about it. | |
| 844 MSAN_MEMORY_IS_INITIALIZED(regs.sp, sizeof(Address)); | |
| 845 // Sample potential return address value for frameless invocation of | |
| 846 // stubs (we'll figure out later, if this value makes sense). | |
| 847 tos = Memory::Address_at(reinterpret_cast<Address>(regs.sp)); | |
| 848 has_external_callback = false; | |
| 849 } | |
| 850 | |
| 851 SafeStackFrameIterator it(isolate, reinterpret_cast<Address>(regs.fp), | |
| 852 reinterpret_cast<Address>(regs.sp), js_entry_sp); | |
| 853 top_frame_type = it.top_frame_type(); | |
| 854 | |
| 855 SampleInfo info; | |
| 856 GetStackSample(isolate, regs, record_c_entry_frame, | |
| 857 reinterpret_cast<void**>(&stack[0]), kMaxFramesCount, &info); | |
| 858 frames_count = static_cast<unsigned>(info.frames_count); | |
| 859 if (!frames_count) { | |
| 860 // It is executing JS but failed to collect a stack trace. | |
| 861 // Mark the sample as spoiled. | |
| 862 timestamp = base::TimeTicks(); | |
| 863 pc = 0; | |
| 864 } | |
| 865 } | |
| 866 | |
| 867 | |
| 868 void TickSample::GetStackSample(Isolate* isolate, const v8::RegisterState& regs, | |
| 869 RecordCEntryFrame record_c_entry_frame, | |
| 870 void** frames, size_t frames_limit, | |
| 871 v8::SampleInfo* sample_info) { | |
| 872 sample_info->frames_count = 0; | |
| 873 sample_info->vm_state = isolate->current_vm_state(); | |
| 874 if (sample_info->vm_state == GC) return; | |
| 875 | |
| 876 Address js_entry_sp = isolate->js_entry_sp(); | |
| 877 if (js_entry_sp == 0) return; // Not executing JS now. | |
| 878 | |
| 879 SafeStackFrameIterator it(isolate, reinterpret_cast<Address>(regs.fp), | |
| 880 reinterpret_cast<Address>(regs.sp), js_entry_sp); | |
| 881 size_t i = 0; | |
| 882 if (record_c_entry_frame == kIncludeCEntryFrame && !it.done() && | |
| 883 it.top_frame_type() == StackFrame::EXIT) { | |
| 884 frames[i++] = isolate->c_function(); | |
| 885 } | |
| 886 while (!it.done() && i < frames_limit) { | |
| 887 if (it.frame()->is_interpreted()) { | |
| 888 // For interpreted frames use the bytecode array pointer as the pc. | |
| 889 InterpretedFrame* frame = static_cast<InterpretedFrame*>(it.frame()); | |
| 890 // Since the sampler can interrupt execution at any point the | |
| 891 // bytecode_array might be garbage, so don't dereference it. | |
| 892 Address bytecode_array = | |
| 893 reinterpret_cast<Address>(frame->GetBytecodeArray()) - kHeapObjectTag; | |
| 894 frames[i++] = bytecode_array + BytecodeArray::kHeaderSize + | |
| 895 frame->GetBytecodeOffset(); | |
| 896 } else { | |
| 897 frames[i++] = it.frame()->pc(); | |
| 898 } | |
| 899 it.Advance(); | |
| 900 } | |
| 901 sample_info->frames_count = i; | |
| 902 } | |
| 903 | 559 |
| 904 | 560 |
| 905 void Sampler::SetUp() { | 561 void Sampler::SetUp() { |
| 906 #if defined(USE_SIGNALS) | 562 #if defined(USE_SIGNALS) |
| 907 SignalHandler::SetUp(); | 563 SignalHandler::SetUp(); |
| 908 #endif | 564 #endif |
| 909 SamplerThread::SetUp(); | |
| 910 } | 565 } |
| 911 | 566 |
| 912 | 567 |
| 913 void Sampler::TearDown() { | 568 void Sampler::TearDown() { |
| 914 SamplerThread::TearDown(); | |
| 915 #if defined(USE_SIGNALS) | 569 #if defined(USE_SIGNALS) |
| 916 SignalHandler::TearDown(); | 570 SignalHandler::TearDown(); |
| 917 #endif | 571 #endif |
| 918 } | 572 } |
| 919 | 573 |
| 920 Sampler::Sampler(Isolate* isolate, int interval) | 574 Sampler::Sampler(Isolate* isolate) |
| 921 : isolate_(isolate), | 575 : isolate_(isolate), |
| 922 interval_(interval), | |
| 923 profiling_(false), | 576 profiling_(false), |
| 924 has_processing_thread_(false), | |
| 925 active_(false), | 577 active_(false), |
| 926 registered_(false), | |
| 927 is_counting_samples_(false), | 578 is_counting_samples_(false), |
| 928 js_sample_count_(0), | 579 js_sample_count_(0), |
| 929 external_sample_count_(0) { | 580 external_sample_count_(0) { |
| 930 data_ = new PlatformData; | 581 data_ = new PlatformData; |
| 931 } | 582 } |
| 932 | 583 |
| 933 Sampler::~Sampler() { | 584 Sampler::~Sampler() { |
| 934 DCHECK(!IsActive()); | 585 DCHECK(!IsActive()); |
| 935 if (IsRegistered()) { | |
| 936 SamplerThread::RemoveSampler(this); | |
| 937 } | |
| 938 delete data_; | 586 delete data_; |
| 939 } | 587 } |
| 940 | 588 |
| 941 void Sampler::Start() { | 589 void Sampler::Start() { |
| 942 DCHECK(!IsActive()); | 590 DCHECK(!IsActive()); |
| 943 SetActive(true); | 591 SetActive(true); |
| 944 SamplerThread::AddActiveSampler(this); | 592 SamplerManager::AddSampler(this); |
| 945 } | 593 } |
| 946 | 594 |
| 947 | 595 |
| 948 void Sampler::Stop() { | 596 void Sampler::Stop() { |
| 597 SamplerManager::RemoveSampler(this); | |
| 949 DCHECK(IsActive()); | 598 DCHECK(IsActive()); |
| 950 SamplerThread::RemoveSampler(this); | |
| 951 SetActive(false); | 599 SetActive(false); |
| 952 SetRegistered(false); | |
| 953 } | 600 } |
| 954 | 601 |
| 955 | 602 |
| 956 void Sampler::IncreaseProfilingDepth() { | 603 void Sampler::IncreaseProfilingDepth() { |
| 957 base::NoBarrier_AtomicIncrement(&profiling_, 1); | 604 base::NoBarrier_AtomicIncrement(&profiling_, 1); |
| 958 #if defined(USE_SIGNALS) | 605 #if defined(USE_SIGNALS) |
| 959 SignalHandler::IncreaseSamplerCount(); | 606 SignalHandler::IncreaseSamplerCount(); |
| 960 #endif | 607 #endif |
| 961 } | 608 } |
| 962 | 609 |
| 963 | 610 |
| 964 void Sampler::DecreaseProfilingDepth() { | 611 void Sampler::DecreaseProfilingDepth() { |
| 965 #if defined(USE_SIGNALS) | 612 #if defined(USE_SIGNALS) |
| 966 SignalHandler::DecreaseSamplerCount(); | 613 SignalHandler::DecreaseSamplerCount(); |
| 967 #endif | 614 #endif |
| 968 base::NoBarrier_AtomicIncrement(&profiling_, -1); | 615 base::NoBarrier_AtomicIncrement(&profiling_, -1); |
| 969 } | 616 } |
| 970 | 617 |
| 971 | 618 |
| 972 void Sampler::SampleStack(const v8::RegisterState& state) { | 619 void Sampler::SampleStack(const v8::RegisterState& state) { |
| 973 TickSample* sample = isolate_->cpu_profiler()->StartTickSample(); | 620 const void* frames_[kMaxFramesCount]; |
| 974 TickSample sample_obj; | 621 v8::SampleInfo sample_info; |
| 975 if (sample == NULL) sample = &sample_obj; | 622 |
| 976 sample->Init(isolate_, state, TickSample::kIncludeCEntryFrame, true); | 623 isolate_->GetStackSample(state, reinterpret_cast<void**>(frames_), |
| 977 if (is_counting_samples_ && !sample->timestamp.IsNull()) { | 624 kMaxFramesCount, &sample_info); |
| 978 if (sample->state == JS) ++js_sample_count_; | 625 |
| 979 if (sample->state == EXTERNAL) ++external_sample_count_; | 626 if (is_counting_samples_) { |
| 980 } | 627 if (sample_info.vm_state == JS) ++js_sample_count_; |
| 981 Tick(sample); | 628 if (sample_info.vm_state == EXTERNAL) ++external_sample_count_; |
| 982 if (sample != &sample_obj) { | |
| 983 isolate_->cpu_profiler()->FinishTickSample(); | |
| 984 } | 629 } |
| 985 } | 630 } |
| 986 | 631 |
| 987 | 632 |
| 988 #if defined(USE_SIGNALS) | 633 #if defined(USE_SIGNALS) |
| 989 | 634 |
| 990 void Sampler::DoSample() { | 635 void Sampler::DoSample() { |
| 991 if (!SignalHandler::Installed()) return; | 636 if (!SignalHandler::Installed()) return; |
| 992 if (!IsActive() && !IsRegistered()) { | |
| 993 SamplerThread::RegisterSampler(this); | |
| 994 SetRegistered(true); | |
| 995 } | |
| 996 pthread_kill(platform_data()->vm_tid(), SIGPROF); | 637 pthread_kill(platform_data()->vm_tid(), SIGPROF); |
| 997 } | 638 } |
| 998 | 639 |
| 999 #elif V8_OS_WIN || V8_OS_CYGWIN | 640 #elif V8_OS_WIN || V8_OS_CYGWIN |
| 1000 | 641 |
| 1001 void Sampler::DoSample() { | 642 void Sampler::DoSample() { |
| 1002 HANDLE profiled_thread = platform_data()->profiled_thread(); | 643 HANDLE profiled_thread = platform_data()->profiled_thread(); |
| 1003 if (profiled_thread == NULL) return; | 644 if (profiled_thread == NULL) return; |
| 1004 | 645 |
| 1005 const DWORD kSuspendFailed = static_cast<DWORD>(-1); | 646 const DWORD kSuspendFailed = static_cast<DWORD>(-1); |
| 1006 if (SuspendThread(profiled_thread) == kSuspendFailed) return; | 647 if (SuspendThread(profiled_thread) == kSuspendFailed) return; |
| 1007 | 648 |
| 1008 // Context used for sampling the register state of the profiled thread. | 649 // Context used for sampling the register state of the profiled thread. |
| 1009 CONTEXT context; | 650 CONTEXT context; |
| 1010 memset(&context, 0, sizeof(context)); | 651 memset(&context, 0, sizeof(context)); |
| 1011 context.ContextFlags = CONTEXT_FULL; | 652 context.ContextFlags = CONTEXT_FULL; |
| 1012 if (GetThreadContext(profiled_thread, &context) != 0) { | 653 if (GetThreadContext(profiled_thread, &context) != 0) { |
| 1013 v8::RegisterState state; | 654 v8::RegisterState state; |
| 1014 #if defined(USE_SIMULATOR) | 655 #if V8_HOST_ARCH_X64 |
| 1015 if (!SimulatorHelper::FillRegisters(isolate(), &state)) { | 656 state.pc = reinterpret_cast<void*>(context.Rip); |
| 1016 ResumeThread(profiled_thread); | 657 state.sp = reinterpret_cast<void*>(context.Rsp); |
| 1017 return; | 658 state.fp = reinterpret_cast<void*>(context.Rbp); |
| 1018 } | |
| 1019 #else | 659 #else |
| 1020 #if V8_HOST_ARCH_X64 | 660 state.pc = reinterpret_cast<void*>(context.Eip); |
| 1021 state.pc = reinterpret_cast<Address>(context.Rip); | 661 state.sp = reinterpret_cast<void*>(context.Esp); |
| 1022 state.sp = reinterpret_cast<Address>(context.Rsp); | 662 state.fp = reinterpret_cast<void*>(context.Ebp); |
| 1023 state.fp = reinterpret_cast<Address>(context.Rbp); | |
| 1024 #else | |
| 1025 state.pc = reinterpret_cast<Address>(context.Eip); | |
| 1026 state.sp = reinterpret_cast<Address>(context.Esp); | |
| 1027 state.fp = reinterpret_cast<Address>(context.Ebp); | |
| 1028 #endif | 663 #endif |
| 1029 #endif // USE_SIMULATOR | |
| 1030 SampleStack(state); | 664 SampleStack(state); |
| 1031 } | 665 } |
| 1032 ResumeThread(profiled_thread); | 666 ResumeThread(profiled_thread); |
| 1033 } | 667 } |
| 1034 | 668 |
| 1035 #endif // USE_SIGNALS | 669 #endif // USE_SIGNALS |
| 1036 | 670 |
| 1037 | 671 |
| 1038 } // namespace internal | |
| 1039 } // namespace v8 | 672 } // namespace v8 |
| OLD | NEW |