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 19 matching lines...) Expand all Loading... |
35 !defined(__BIONIC_HAVE_STRUCT_SIGCONTEXT) | 35 !defined(__BIONIC_HAVE_STRUCT_SIGCONTEXT) |
36 #include <asm/sigcontext.h> // NOLINT | 36 #include <asm/sigcontext.h> // NOLINT |
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 <algorithm> |
| 46 #include <vector> |
| 47 #include <map> |
| 48 |
45 #include "src/base/atomic-utils.h" | 49 #include "src/base/atomic-utils.h" |
46 #include "src/base/platform/platform.h" | 50 #include "src/base/platform/platform.h" |
47 #include "src/profiler/cpu-profiler-inl.h" | 51 #include "src/libsampler/hashmap.h" |
48 #include "src/profiler/tick-sample.h" | |
49 #include "src/simulator.h" | |
50 #include "src/v8threads.h" | |
51 | 52 |
52 | 53 |
53 #if V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) | 54 #if V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) |
54 | 55 |
55 // Not all versions of Android's C library provide ucontext_t. | 56 // Not all versions of Android's C library provide ucontext_t. |
56 // Detect this and provide custom but compatible definitions. Note that these | 57 // Detect this and provide custom but compatible definitions. Note that these |
57 // follow the GLibc naming convention to access register values from | 58 // follow the GLibc naming convention to access register values from |
58 // mcontext_t. | 59 // mcontext_t. |
59 // | 60 // |
60 // See http://code.google.com/p/android/issues/detail?id=34784 | 61 // See http://code.google.com/p/android/issues/detail?id=34784 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 mcontext_t uc_mcontext; | 149 mcontext_t uc_mcontext; |
149 // Other fields are not used by V8, don't define them here. | 150 // Other fields are not used by V8, don't define them here. |
150 } ucontext_t; | 151 } ucontext_t; |
151 enum { REG_RBP = 10, REG_RSP = 15, REG_RIP = 16 }; | 152 enum { REG_RBP = 10, REG_RSP = 15, REG_RIP = 16 }; |
152 #endif | 153 #endif |
153 | 154 |
154 #endif // V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) | 155 #endif // V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) |
155 | 156 |
156 | 157 |
157 namespace v8 { | 158 namespace v8 { |
158 namespace internal { | 159 namespace sampler { |
159 | 160 |
160 namespace { | 161 namespace { |
161 | 162 |
162 class PlatformDataCommon : public Malloced { | 163 #if defined(USE_SIGNALS) |
163 public: | 164 typedef std::vector<Sampler*> SamplerList; |
164 PlatformDataCommon() : profiled_thread_id_(ThreadId::Current()) {} | 165 typedef SamplerList::iterator SamplerListIterator; |
165 ThreadId profiled_thread_id() { return profiled_thread_id_; } | |
166 | 166 |
167 protected: | |
168 ~PlatformDataCommon() {} | |
169 | |
170 private: | |
171 ThreadId profiled_thread_id_; | |
172 }; | |
173 | |
174 | |
175 typedef List<Sampler*> SamplerList; | |
176 | |
177 #if defined(USE_SIGNALS) | |
178 class AtomicGuard { | 167 class AtomicGuard { |
179 public: | 168 public: |
180 explicit AtomicGuard(base::AtomicValue<int>* atomic, bool is_block = true) | 169 explicit AtomicGuard(base::AtomicValue<int>* atomic, bool is_block = true) |
181 : atomic_(atomic), | 170 : atomic_(atomic), |
182 is_success_(false) { | 171 is_success_(false) { |
183 do { | 172 do { |
184 // Use Acquire_Load to gain mutual exclusion. | 173 // Use Acquire_Load to gain mutual exclusion. |
185 USE(atomic_->Value()); | 174 USE(atomic_->Value()); |
186 is_success_ = atomic_->TrySetValue(0, 1); | 175 is_success_ = atomic_->TrySetValue(0, 1); |
187 } while (is_block && !is_success_); | 176 } while (is_block && !is_success_); |
(...skipping 21 matching lines...) Expand all Loading... |
209 | 198 |
210 | 199 |
211 // Returns hash value for hash map. | 200 // Returns hash value for hash map. |
212 uint32_t ThreadHash(pthread_t thread_id) { | 201 uint32_t ThreadHash(pthread_t thread_id) { |
213 #if V8_OS_MACOSX | 202 #if V8_OS_MACOSX |
214 return static_cast<uint32_t>(reinterpret_cast<intptr_t>(thread_id)); | 203 return static_cast<uint32_t>(reinterpret_cast<intptr_t>(thread_id)); |
215 #else | 204 #else |
216 return static_cast<uint32_t>(thread_id); | 205 return static_cast<uint32_t>(thread_id); |
217 #endif | 206 #endif |
218 } | 207 } |
| 208 |
219 #endif // USE_SIGNALS | 209 #endif // USE_SIGNALS |
220 | 210 |
221 } // namespace | 211 } // namespace |
222 | 212 |
223 #if defined(USE_SIGNALS) | 213 #if defined(USE_SIGNALS) |
224 | 214 |
225 class Sampler::PlatformData : public PlatformDataCommon { | 215 class Sampler::PlatformData { |
226 public: | 216 public: |
227 PlatformData() : vm_tid_(pthread_self()) {} | 217 PlatformData() : vm_tid_(pthread_self()) {} |
228 pthread_t vm_tid() const { return vm_tid_; } | 218 pthread_t vm_tid() const { return vm_tid_; } |
229 | 219 |
230 private: | 220 private: |
231 pthread_t vm_tid_; | 221 pthread_t vm_tid_; |
232 }; | 222 }; |
233 | 223 |
| 224 |
| 225 class SamplerManager { |
| 226 public: |
| 227 static void AddSampler(Sampler* sampler) { |
| 228 AtomicGuard atomic_guard(&samplers_access_counter_); |
| 229 DCHECK(sampler->IsActive() || !sampler->IsRegistered()); |
| 230 // Add sampler into map if needed. |
| 231 pthread_t thread_id = sampler->platform_data()->vm_tid(); |
| 232 HashMap::Entry* entry = |
| 233 sampler_map_.Pointer()->LookupOrInsert(ThreadKey(thread_id), |
| 234 ThreadHash(thread_id)); |
| 235 DCHECK(entry != NULL); |
| 236 if (entry->value == NULL) { |
| 237 SamplerList* samplers = new SamplerList(); |
| 238 samplers->push_back(sampler); |
| 239 entry->value = samplers; |
| 240 } else { |
| 241 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); |
| 242 bool exists = false; |
| 243 for (SamplerListIterator iter = samplers->begin(); |
| 244 iter != samplers->end(); ++iter) { |
| 245 if (*iter == sampler) { |
| 246 exists = true; |
| 247 break; |
| 248 } |
| 249 } |
| 250 if (!exists) { |
| 251 samplers->push_back(sampler); |
| 252 } |
| 253 } |
| 254 } |
| 255 |
| 256 static void RemoveSampler(Sampler* sampler) { |
| 257 AtomicGuard atomic_guard(&samplers_access_counter_); |
| 258 DCHECK(sampler->IsActive() || sampler->IsRegistered()); |
| 259 // Remove sampler from map. |
| 260 pthread_t thread_id = sampler->platform_data()->vm_tid(); |
| 261 void* thread_key = ThreadKey(thread_id); |
| 262 uint32_t thread_hash = ThreadHash(thread_id); |
| 263 HashMap::Entry* entry = sampler_map_.Get().Lookup(thread_key, thread_hash); |
| 264 DCHECK(entry != NULL); |
| 265 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); |
| 266 for (SamplerListIterator iter = samplers->begin(); iter != samplers->end(); |
| 267 ++iter) { |
| 268 if (*iter == sampler) { |
| 269 samplers->erase(iter); |
| 270 break; |
| 271 } |
| 272 } |
| 273 if (samplers->empty()) { |
| 274 sampler_map_.Pointer()->Remove(thread_key, thread_hash); |
| 275 delete samplers; |
| 276 } |
| 277 } |
| 278 |
| 279 private: |
| 280 struct HashMapCreateTrait { |
| 281 static void Construct(HashMap* allocated_ptr) { |
| 282 new (allocated_ptr) HashMap(HashMap::PointersMatch); |
| 283 } |
| 284 }; |
| 285 friend class SignalHandler; |
| 286 static base::LazyInstance<HashMap, HashMapCreateTrait>::type |
| 287 sampler_map_; |
| 288 static base::AtomicValue<int> samplers_access_counter_; |
| 289 }; |
| 290 |
| 291 base::LazyInstance<HashMap, SamplerManager::HashMapCreateTrait>::type |
| 292 SamplerManager::sampler_map_ = LAZY_INSTANCE_INITIALIZER; |
| 293 base::AtomicValue<int> SamplerManager::samplers_access_counter_(0); |
| 294 |
| 295 |
234 #elif V8_OS_WIN || V8_OS_CYGWIN | 296 #elif V8_OS_WIN || V8_OS_CYGWIN |
235 | 297 |
236 // ---------------------------------------------------------------------------- | 298 // ---------------------------------------------------------------------------- |
237 // Win32 profiler support. On Cygwin we use the same sampler implementation as | 299 // Win32 profiler support. On Cygwin we use the same sampler implementation as |
238 // on Win32. | 300 // on Win32. |
239 | 301 |
240 class Sampler::PlatformData : public PlatformDataCommon { | 302 class Sampler::PlatformData { |
241 public: | 303 public: |
242 // Get a handle to the calling thread. This is the thread that we are | 304 // Get a handle to the calling thread. This is the thread that we are |
243 // going to profile. We need to make a copy of the handle because we are | 305 // going to profile. We need to make a copy of the handle because we are |
244 // going to use it in the sampler thread. Using GetThreadHandle() will | 306 // going to use it in the sampler thread. Using GetThreadHandle() will |
245 // not work in this case. We're using OpenThread because DuplicateHandle | 307 // not work in this case. We're using OpenThread because DuplicateHandle |
246 // for some reason doesn't work in Chrome's sandbox. | 308 // for some reason doesn't work in Chrome's sandbox. |
247 PlatformData() | 309 PlatformData() |
248 : profiled_thread_(OpenThread(THREAD_GET_CONTEXT | | 310 : profiled_thread_(OpenThread(THREAD_GET_CONTEXT | |
249 THREAD_SUSPEND_RESUME | | 311 THREAD_SUSPEND_RESUME | |
250 THREAD_QUERY_INFORMATION, | 312 THREAD_QUERY_INFORMATION, |
251 false, | 313 false, |
252 GetCurrentThreadId())) {} | 314 GetCurrentThreadId())) {} |
253 | 315 |
254 ~PlatformData() { | 316 ~PlatformData() { |
255 if (profiled_thread_ != NULL) { | 317 if (profiled_thread_ != NULL) { |
256 CloseHandle(profiled_thread_); | 318 CloseHandle(profiled_thread_); |
257 profiled_thread_ = NULL; | 319 profiled_thread_ = NULL; |
258 } | 320 } |
259 } | 321 } |
260 | 322 |
261 HANDLE profiled_thread() { return profiled_thread_; } | 323 HANDLE profiled_thread() { return profiled_thread_; } |
262 | 324 |
263 private: | 325 private: |
264 HANDLE profiled_thread_; | 326 HANDLE profiled_thread_; |
265 }; | 327 }; |
266 #endif | 328 #endif // USE_SIGNALS |
267 | 329 |
268 | 330 |
269 #if defined(USE_SIGNALS) | 331 #if defined(USE_SIGNALS) |
270 | 332 class SignalHandler { |
271 class SignalHandler : public AllStatic { | |
272 public: | 333 public: |
273 static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } | 334 static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } |
274 static void TearDown() { delete mutex_; mutex_ = NULL; } | 335 static void TearDown() { delete mutex_; mutex_ = NULL; } |
275 | 336 |
276 static void IncreaseSamplerCount() { | 337 static void IncreaseSamplerCount() { |
277 base::LockGuard<base::Mutex> lock_guard(mutex_); | 338 base::LockGuard<base::Mutex> lock_guard(mutex_); |
278 if (++client_count_ == 1) Install(); | 339 if (++client_count_ == 1) Install(); |
279 } | 340 } |
280 | 341 |
281 static void DecreaseSamplerCount() { | 342 static void DecreaseSamplerCount() { |
282 base::LockGuard<base::Mutex> lock_guard(mutex_); | 343 base::LockGuard<base::Mutex> lock_guard(mutex_); |
283 if (--client_count_ == 0) Restore(); | 344 if (--client_count_ == 0) Restore(); |
284 } | 345 } |
285 | 346 |
286 static bool Installed() { | 347 static bool Installed() { |
287 return signal_handler_installed_; | 348 return signal_handler_installed_; |
288 } | 349 } |
289 | 350 |
290 #if !V8_OS_NACL | |
291 static void CollectSample(void* context, Sampler* sampler); | |
292 #endif | |
293 | |
294 private: | 351 private: |
295 static void Install() { | 352 static void Install() { |
296 #if !V8_OS_NACL | 353 #if !V8_OS_NACL |
297 struct sigaction sa; | 354 struct sigaction sa; |
298 sa.sa_sigaction = &HandleProfilerSignal; | 355 sa.sa_sigaction = &HandleProfilerSignal; |
299 sigemptyset(&sa.sa_mask); | 356 sigemptyset(&sa.sa_mask); |
300 #if V8_OS_QNX | 357 #if V8_OS_QNX |
301 sa.sa_flags = SA_SIGINFO; | 358 sa.sa_flags = SA_SIGINFO; |
302 #else | 359 #else |
303 sa.sa_flags = SA_RESTART | SA_SIGINFO; | 360 sa.sa_flags = SA_RESTART | SA_SIGINFO; |
304 #endif | 361 #endif |
305 signal_handler_installed_ = | 362 signal_handler_installed_ = |
306 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); | 363 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); |
307 #endif | 364 #endif // !V8_OS_NACL |
308 } | 365 } |
309 | 366 |
310 static void Restore() { | 367 static void Restore() { |
311 #if !V8_OS_NACL | 368 #if !V8_OS_NACL |
312 if (signal_handler_installed_) { | 369 if (signal_handler_installed_) { |
313 sigaction(SIGPROF, &old_signal_handler_, 0); | 370 sigaction(SIGPROF, &old_signal_handler_, 0); |
314 signal_handler_installed_ = false; | 371 signal_handler_installed_ = false; |
315 } | 372 } |
316 #endif | 373 #endif |
317 } | 374 } |
318 | 375 |
319 #if !V8_OS_NACL | 376 #if !V8_OS_NACL |
| 377 static void FillRegisterState(void* context, RegisterState* regs); |
320 static void HandleProfilerSignal(int signal, siginfo_t* info, void* context); | 378 static void HandleProfilerSignal(int signal, siginfo_t* info, void* context); |
321 #endif | 379 #endif |
322 // Protects the process wide state below. | 380 // Protects the process wide state below. |
323 static base::Mutex* mutex_; | 381 static base::Mutex* mutex_; |
324 static int client_count_; | 382 static int client_count_; |
325 static bool signal_handler_installed_; | 383 static bool signal_handler_installed_; |
326 static struct sigaction old_signal_handler_; | 384 static struct sigaction old_signal_handler_; |
327 }; | 385 }; |
328 | 386 |
329 | 387 |
330 base::Mutex* SignalHandler::mutex_ = NULL; | 388 base::Mutex* SignalHandler::mutex_ = NULL; |
331 int SignalHandler::client_count_ = 0; | 389 int SignalHandler::client_count_ = 0; |
332 struct sigaction SignalHandler::old_signal_handler_; | 390 struct sigaction SignalHandler::old_signal_handler_; |
333 bool SignalHandler::signal_handler_installed_ = false; | 391 bool SignalHandler::signal_handler_installed_ = false; |
334 | 392 |
335 | 393 |
336 // As Native Client does not support signal handling, profiling is disabled. | 394 // As Native Client does not support signal handling, profiling is disabled. |
337 #if !V8_OS_NACL | 395 #if !V8_OS_NACL |
338 void SignalHandler::CollectSample(void* context, Sampler* sampler) { | 396 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, |
339 if (sampler == NULL || (!sampler->IsProfiling() && | 397 void* context) { |
340 !sampler->IsRegistered())) { | 398 USE(info); |
341 return; | 399 if (signal != SIGPROF) return; |
342 } | 400 AtomicGuard atomic_guard(&SamplerManager::samplers_access_counter_, false); |
343 Isolate* isolate = sampler->isolate(); | 401 if (!atomic_guard.is_success()) return; |
344 | 402 pthread_t thread_id = pthread_self(); |
345 // We require a fully initialized and entered isolate. | 403 HashMap::Entry* entry = |
346 if (isolate == NULL || !isolate->IsInUse()) return; | 404 SamplerManager::sampler_map_.Pointer()->Lookup(ThreadKey(thread_id), |
347 | 405 ThreadHash(thread_id)); |
348 if (v8::Locker::IsActive() && | 406 if (entry == NULL) return; |
349 !isolate->thread_manager()->IsLockedByCurrentThread()) { | 407 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); |
350 return; | |
351 } | |
352 | 408 |
353 v8::RegisterState state; | 409 v8::RegisterState state; |
| 410 FillRegisterState(context, &state); |
354 | 411 |
355 #if defined(USE_SIMULATOR) | 412 for (int i = 0; i < samplers->size(); ++i) { |
356 if (!SimulatorHelper::FillRegisters(isolate, &state)) return; | 413 Sampler* sampler = (*samplers)[i]; |
357 #else | 414 Isolate* isolate = sampler->isolate(); |
| 415 |
| 416 // We require a fully initialized and entered isolate. |
| 417 if (isolate == NULL || !isolate->IsInUse()) return; |
| 418 |
| 419 if (v8::Locker::IsActive() && !Locker::IsLocked(isolate)) return; |
| 420 |
| 421 sampler->SampleStack(state); |
| 422 } |
| 423 } |
| 424 |
| 425 void SignalHandler::FillRegisterState(void* context, RegisterState* state) { |
358 // Extracting the sample from the context is extremely machine dependent. | 426 // Extracting the sample from the context is extremely machine dependent. |
359 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); | 427 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); |
360 #if !(V8_OS_OPENBSD || (V8_OS_LINUX && (V8_HOST_ARCH_PPC || V8_HOST_ARCH_S390))) | 428 #if !(V8_OS_OPENBSD || (V8_OS_LINUX && (V8_HOST_ARCH_PPC || V8_HOST_ARCH_S390))) |
361 mcontext_t& mcontext = ucontext->uc_mcontext; | 429 mcontext_t& mcontext = ucontext->uc_mcontext; |
362 #endif | 430 #endif |
363 #if V8_OS_LINUX | 431 #if V8_OS_LINUX |
364 #if V8_HOST_ARCH_IA32 | 432 #if V8_HOST_ARCH_IA32 |
365 state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]); | 433 state->pc = reinterpret_cast<void*>(mcontext.gregs[REG_EIP]); |
366 state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]); | 434 state->sp = reinterpret_cast<void*>(mcontext.gregs[REG_ESP]); |
367 state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]); | 435 state->fp = reinterpret_cast<void*>(mcontext.gregs[REG_EBP]); |
368 #elif V8_HOST_ARCH_X64 | 436 #elif V8_HOST_ARCH_X64 |
369 state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]); | 437 state->pc = reinterpret_cast<void*>(mcontext.gregs[REG_RIP]); |
370 state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]); | 438 state->sp = reinterpret_cast<void*>(mcontext.gregs[REG_RSP]); |
371 state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]); | 439 state->fp = reinterpret_cast<void*>(mcontext.gregs[REG_RBP]); |
372 #elif V8_HOST_ARCH_ARM | 440 #elif V8_HOST_ARCH_ARM |
373 #if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) | 441 #if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) |
374 // Old GLibc ARM versions used a gregs[] array to access the register | 442 // Old GLibc ARM versions used a gregs[] array to access the register |
375 // values from mcontext_t. | 443 // values from mcontext_t. |
376 state.pc = reinterpret_cast<Address>(mcontext.gregs[R15]); | 444 state->pc = reinterpret_cast<void*>(mcontext.gregs[R15]); |
377 state.sp = reinterpret_cast<Address>(mcontext.gregs[R13]); | 445 state->sp = reinterpret_cast<void*>(mcontext.gregs[R13]); |
378 state.fp = reinterpret_cast<Address>(mcontext.gregs[R11]); | 446 state->fp = reinterpret_cast<void*>(mcontext.gregs[R11]); |
379 #else | 447 #else |
380 state.pc = reinterpret_cast<Address>(mcontext.arm_pc); | 448 state->pc = reinterpret_cast<void*>(mcontext.arm_pc); |
381 state.sp = reinterpret_cast<Address>(mcontext.arm_sp); | 449 state->sp = reinterpret_cast<void*>(mcontext.arm_sp); |
382 state.fp = reinterpret_cast<Address>(mcontext.arm_fp); | 450 state->fp = reinterpret_cast<void*>(mcontext.arm_fp); |
383 #endif // V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) | 451 #endif // V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) |
384 #elif V8_HOST_ARCH_ARM64 | 452 #elif V8_HOST_ARCH_ARM64 |
385 state.pc = reinterpret_cast<Address>(mcontext.pc); | 453 state->pc = reinterpret_cast<void*>(mcontext.pc); |
386 state.sp = reinterpret_cast<Address>(mcontext.sp); | 454 state->sp = reinterpret_cast<void*>(mcontext.sp); |
387 // FP is an alias for x29. | 455 // FP is an alias for x29. |
388 state.fp = reinterpret_cast<Address>(mcontext.regs[29]); | 456 state->fp = reinterpret_cast<void*>(mcontext.regs[29]); |
389 #elif V8_HOST_ARCH_MIPS | 457 #elif V8_HOST_ARCH_MIPS |
390 state.pc = reinterpret_cast<Address>(mcontext.pc); | 458 state->pc = reinterpret_cast<void*>(mcontext.pc); |
391 state.sp = reinterpret_cast<Address>(mcontext.gregs[29]); | 459 state->sp = reinterpret_cast<void*>(mcontext.gregs[29]); |
392 state.fp = reinterpret_cast<Address>(mcontext.gregs[30]); | 460 state->fp = reinterpret_cast<void*>(mcontext.gregs[30]); |
393 #elif V8_HOST_ARCH_MIPS64 | 461 #elif V8_HOST_ARCH_MIPS64 |
394 state.pc = reinterpret_cast<Address>(mcontext.pc); | 462 state->pc = reinterpret_cast<void*>(mcontext.pc); |
395 state.sp = reinterpret_cast<Address>(mcontext.gregs[29]); | 463 state->sp = reinterpret_cast<void*>(mcontext.gregs[29]); |
396 state.fp = reinterpret_cast<Address>(mcontext.gregs[30]); | 464 state->fp = reinterpret_cast<void*>(mcontext.gregs[30]); |
397 #elif V8_HOST_ARCH_PPC | 465 #elif V8_HOST_ARCH_PPC |
398 state.pc = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->nip); | 466 state->pc = reinterpret_cast<void*>(ucontext->uc_mcontext.regs->nip); |
399 state.sp = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->gpr[PT_R1]); | 467 state->sp = |
400 state.fp = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->gpr[PT_R31]); | 468 reinterpret_cast<void*>(ucontext->uc_mcontext.regs->gpr[PT_R1]); |
| 469 state->fp = |
| 470 reinterpret_cast<void*>(ucontext->uc_mcontext.regs->gpr[PT_R31]); |
401 #elif V8_HOST_ARCH_S390 | 471 #elif V8_HOST_ARCH_S390 |
402 #if V8_TARGET_ARCH_32_BIT | 472 #if V8_TARGET_ARCH_32_BIT |
403 // 31-bit target will have bit 0 (MSB) of the PSW set to denote addressing | 473 // 31-bit target will have bit 0 (MSB) of the PSW set to denote addressing |
404 // mode. This bit needs to be masked out to resolve actual address. | 474 // mode. This bit needs to be masked out to resolve actual address. |
405 state.pc = | 475 state->pc = |
406 reinterpret_cast<Address>(ucontext->uc_mcontext.psw.addr & 0x7FFFFFFF); | 476 reinterpret_cast<void*>(ucontext->uc_mcontext.psw.addr & 0x7FFFFFFF); |
407 #else | 477 #else |
408 state.pc = reinterpret_cast<Address>(ucontext->uc_mcontext.psw.addr); | 478 state->pc = reinterpret_cast<void*>(ucontext->uc_mcontext.psw.addr); |
409 #endif // V8_TARGET_ARCH_32_BIT | 479 #endif // V8_TARGET_ARCH_32_BIT |
410 state.sp = reinterpret_cast<Address>(ucontext->uc_mcontext.gregs[15]); | 480 state->sp = reinterpret_cast<void*>(ucontext->uc_mcontext.gregs[15]); |
411 state.fp = reinterpret_cast<Address>(ucontext->uc_mcontext.gregs[11]); | 481 state->fp = reinterpret_cast<void*>(ucontext->uc_mcontext.gregs[11]); |
412 #endif // V8_HOST_ARCH_* | 482 #endif // V8_HOST_ARCH_* |
413 #elif V8_OS_MACOSX | 483 #elif V8_OS_MACOSX |
414 #if V8_HOST_ARCH_X64 | 484 #if V8_HOST_ARCH_X64 |
415 #if __DARWIN_UNIX03 | 485 #if __DARWIN_UNIX03 |
416 state.pc = reinterpret_cast<Address>(mcontext->__ss.__rip); | 486 state->pc = reinterpret_cast<void*>(mcontext->__ss.__rip); |
417 state.sp = reinterpret_cast<Address>(mcontext->__ss.__rsp); | 487 state->sp = reinterpret_cast<void*>(mcontext->__ss.__rsp); |
418 state.fp = reinterpret_cast<Address>(mcontext->__ss.__rbp); | 488 state->fp = reinterpret_cast<void*>(mcontext->__ss.__rbp); |
419 #else // !__DARWIN_UNIX03 | 489 #else // !__DARWIN_UNIX03 |
420 state.pc = reinterpret_cast<Address>(mcontext->ss.rip); | 490 state->pc = reinterpret_cast<void*>(mcontext->ss.rip); |
421 state.sp = reinterpret_cast<Address>(mcontext->ss.rsp); | 491 state->sp = reinterpret_cast<void*>(mcontext->ss.rsp); |
422 state.fp = reinterpret_cast<Address>(mcontext->ss.rbp); | 492 state->fp = reinterpret_cast<void*>(mcontext->ss.rbp); |
423 #endif // __DARWIN_UNIX03 | 493 #endif // __DARWIN_UNIX03 |
424 #elif V8_HOST_ARCH_IA32 | 494 #elif V8_HOST_ARCH_IA32 |
425 #if __DARWIN_UNIX03 | 495 #if __DARWIN_UNIX03 |
426 state.pc = reinterpret_cast<Address>(mcontext->__ss.__eip); | 496 state->pc = reinterpret_cast<void*>(mcontext->__ss.__eip); |
427 state.sp = reinterpret_cast<Address>(mcontext->__ss.__esp); | 497 state->sp = reinterpret_cast<void*>(mcontext->__ss.__esp); |
428 state.fp = reinterpret_cast<Address>(mcontext->__ss.__ebp); | 498 state->fp = reinterpret_cast<void*>(mcontext->__ss.__ebp); |
429 #else // !__DARWIN_UNIX03 | 499 #else // !__DARWIN_UNIX03 |
430 state.pc = reinterpret_cast<Address>(mcontext->ss.eip); | 500 state->pc = reinterpret_cast<void*>(mcontext->ss.eip); |
431 state.sp = reinterpret_cast<Address>(mcontext->ss.esp); | 501 state->sp = reinterpret_cast<void*>(mcontext->ss.esp); |
432 state.fp = reinterpret_cast<Address>(mcontext->ss.ebp); | 502 state->fp = reinterpret_cast<void*>(mcontext->ss.ebp); |
433 #endif // __DARWIN_UNIX03 | 503 #endif // __DARWIN_UNIX03 |
434 #endif // V8_HOST_ARCH_IA32 | 504 #endif // V8_HOST_ARCH_IA32 |
435 #elif V8_OS_FREEBSD | 505 #elif V8_OS_FREEBSD |
436 #if V8_HOST_ARCH_IA32 | 506 #if V8_HOST_ARCH_IA32 |
437 state.pc = reinterpret_cast<Address>(mcontext.mc_eip); | 507 state->pc = reinterpret_cast<void*>(mcontext.mc_eip); |
438 state.sp = reinterpret_cast<Address>(mcontext.mc_esp); | 508 state->sp = reinterpret_cast<void*>(mcontext.mc_esp); |
439 state.fp = reinterpret_cast<Address>(mcontext.mc_ebp); | 509 state->fp = reinterpret_cast<void*>(mcontext.mc_ebp); |
440 #elif V8_HOST_ARCH_X64 | 510 #elif V8_HOST_ARCH_X64 |
441 state.pc = reinterpret_cast<Address>(mcontext.mc_rip); | 511 state->pc = reinterpret_cast<void*>(mcontext.mc_rip); |
442 state.sp = reinterpret_cast<Address>(mcontext.mc_rsp); | 512 state->sp = reinterpret_cast<void*>(mcontext.mc_rsp); |
443 state.fp = reinterpret_cast<Address>(mcontext.mc_rbp); | 513 state->fp = reinterpret_cast<void*>(mcontext.mc_rbp); |
444 #elif V8_HOST_ARCH_ARM | 514 #elif V8_HOST_ARCH_ARM |
445 state.pc = reinterpret_cast<Address>(mcontext.mc_r15); | 515 state->pc = reinterpret_cast<void*>(mcontext.mc_r15); |
446 state.sp = reinterpret_cast<Address>(mcontext.mc_r13); | 516 state->sp = reinterpret_cast<void*>(mcontext.mc_r13); |
447 state.fp = reinterpret_cast<Address>(mcontext.mc_r11); | 517 state->fp = reinterpret_cast<void*>(mcontext.mc_r11); |
448 #endif // V8_HOST_ARCH_* | 518 #endif // V8_HOST_ARCH_* |
449 #elif V8_OS_NETBSD | 519 #elif V8_OS_NETBSD |
450 #if V8_HOST_ARCH_IA32 | 520 #if V8_HOST_ARCH_IA32 |
451 state.pc = reinterpret_cast<Address>(mcontext.__gregs[_REG_EIP]); | 521 state->pc = reinterpret_cast<void*>(mcontext.__gregs[_REG_EIP]); |
452 state.sp = reinterpret_cast<Address>(mcontext.__gregs[_REG_ESP]); | 522 state->sp = reinterpret_cast<void*>(mcontext.__gregs[_REG_ESP]); |
453 state.fp = reinterpret_cast<Address>(mcontext.__gregs[_REG_EBP]); | 523 state->fp = reinterpret_cast<void*>(mcontext.__gregs[_REG_EBP]); |
454 #elif V8_HOST_ARCH_X64 | 524 #elif V8_HOST_ARCH_X64 |
455 state.pc = reinterpret_cast<Address>(mcontext.__gregs[_REG_RIP]); | 525 state->pc = reinterpret_cast<void*>(mcontext.__gregs[_REG_RIP]); |
456 state.sp = reinterpret_cast<Address>(mcontext.__gregs[_REG_RSP]); | 526 state->sp = reinterpret_cast<void*>(mcontext.__gregs[_REG_RSP]); |
457 state.fp = reinterpret_cast<Address>(mcontext.__gregs[_REG_RBP]); | 527 state->fp = reinterpret_cast<void*>(mcontext.__gregs[_REG_RBP]); |
458 #endif // V8_HOST_ARCH_* | 528 #endif // V8_HOST_ARCH_* |
459 #elif V8_OS_OPENBSD | 529 #elif V8_OS_OPENBSD |
460 #if V8_HOST_ARCH_IA32 | 530 #if V8_HOST_ARCH_IA32 |
461 state.pc = reinterpret_cast<Address>(ucontext->sc_eip); | 531 state->pc = reinterpret_cast<void*>(ucontext->sc_eip); |
462 state.sp = reinterpret_cast<Address>(ucontext->sc_esp); | 532 state->sp = reinterpret_cast<void*>(ucontext->sc_esp); |
463 state.fp = reinterpret_cast<Address>(ucontext->sc_ebp); | 533 state->fp = reinterpret_cast<void*>(ucontext->sc_ebp); |
464 #elif V8_HOST_ARCH_X64 | 534 #elif V8_HOST_ARCH_X64 |
465 state.pc = reinterpret_cast<Address>(ucontext->sc_rip); | 535 state->pc = reinterpret_cast<void*>(ucontext->sc_rip); |
466 state.sp = reinterpret_cast<Address>(ucontext->sc_rsp); | 536 state->sp = reinterpret_cast<void*>(ucontext->sc_rsp); |
467 state.fp = reinterpret_cast<Address>(ucontext->sc_rbp); | 537 state->fp = reinterpret_cast<void*>(ucontext->sc_rbp); |
468 #endif // V8_HOST_ARCH_* | 538 #endif // V8_HOST_ARCH_* |
469 #elif V8_OS_SOLARIS | 539 #elif V8_OS_SOLARIS |
470 state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_PC]); | 540 state->pc = reinterpret_cast<void*>(mcontext.gregs[REG_PC]); |
471 state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_SP]); | 541 state->sp = reinterpret_cast<void*>(mcontext.gregs[REG_SP]); |
472 state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_FP]); | 542 state->fp = reinterpret_cast<void*>(mcontext.gregs[REG_FP]); |
473 #elif V8_OS_QNX | 543 #elif V8_OS_QNX |
474 #if V8_HOST_ARCH_IA32 | 544 #if V8_HOST_ARCH_IA32 |
475 state.pc = reinterpret_cast<Address>(mcontext.cpu.eip); | 545 state->pc = reinterpret_cast<void*>(mcontext.cpu.eip); |
476 state.sp = reinterpret_cast<Address>(mcontext.cpu.esp); | 546 state->sp = reinterpret_cast<void*>(mcontext.cpu.esp); |
477 state.fp = reinterpret_cast<Address>(mcontext.cpu.ebp); | 547 state->fp = reinterpret_cast<void*>(mcontext.cpu.ebp); |
478 #elif V8_HOST_ARCH_ARM | 548 #elif V8_HOST_ARCH_ARM |
479 state.pc = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_PC]); | 549 state->pc = reinterpret_cast<void*>(mcontext.cpu.gpr[ARM_REG_PC]); |
480 state.sp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_SP]); | 550 state->sp = reinterpret_cast<void*>(mcontext.cpu.gpr[ARM_REG_SP]); |
481 state.fp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_FP]); | 551 state->fp = reinterpret_cast<void*>(mcontext.cpu.gpr[ARM_REG_FP]); |
482 #endif // V8_HOST_ARCH_* | 552 #endif // V8_HOST_ARCH_* |
483 #elif V8_OS_AIX | 553 #elif V8_OS_AIX |
484 state.pc = reinterpret_cast<Address>(mcontext.jmp_context.iar); | 554 state->pc = reinterpret_cast<void*>(mcontext.jmp_context.iar); |
485 state.sp = reinterpret_cast<Address>(mcontext.jmp_context.gpr[1]); | 555 state->sp = reinterpret_cast<void*>(mcontext.jmp_context.gpr[1]); |
486 state.fp = reinterpret_cast<Address>(mcontext.jmp_context.gpr[31]); | 556 state->fp = reinterpret_cast<void*>(mcontext.jmp_context.gpr[31]); |
487 #endif // V8_OS_AIX | 557 #endif // V8_OS_AIX |
488 #endif // USE_SIMULATOR | |
489 sampler->SampleStack(state); | |
490 } | 558 } |
491 #endif // V8_OS_NACL | 559 |
| 560 #endif // !V8_OS_NACL |
492 | 561 |
493 #endif // USE_SIGNALS | 562 #endif // USE_SIGNALS |
494 | 563 |
495 | 564 |
496 class SamplerThread : public base::Thread { | |
497 public: | |
498 static const int kSamplerThreadStackSize = 64 * KB; | |
499 | |
500 explicit SamplerThread(int interval) | |
501 : Thread(base::Thread::Options("SamplerThread", kSamplerThreadStackSize)), | |
502 interval_(interval) {} | |
503 | |
504 static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } | |
505 static void TearDown() { delete mutex_; mutex_ = NULL; } | |
506 | |
507 static void AddActiveSampler(Sampler* sampler) { | |
508 bool need_to_start = false; | |
509 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
510 if (instance_ == NULL) { | |
511 // Start a thread that will send SIGPROF signal to VM threads, | |
512 // when CPU profiling will be enabled. | |
513 instance_ = new SamplerThread(sampler->interval()); | |
514 need_to_start = true; | |
515 } | |
516 | |
517 DCHECK(sampler->IsActive()); | |
518 DCHECK(instance_->interval_ == sampler->interval()); | |
519 | |
520 #if defined(USE_SIGNALS) | |
521 AddSampler(sampler); | |
522 #else | |
523 DCHECK(!instance_->active_samplers_.Contains(sampler)); | |
524 instance_->active_samplers_.Add(sampler); | |
525 #endif // USE_SIGNALS | |
526 | |
527 if (need_to_start) instance_->StartSynchronously(); | |
528 } | |
529 | |
530 static void RemoveSampler(Sampler* sampler) { | |
531 SamplerThread* instance_to_remove = NULL; | |
532 { | |
533 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
534 | |
535 DCHECK(sampler->IsActive() || sampler->IsRegistered()); | |
536 #if defined(USE_SIGNALS) | |
537 { | |
538 AtomicGuard atomic_guard(&sampler_list_access_counter_); | |
539 // Remove sampler from map. | |
540 pthread_t thread_id = sampler->platform_data()->vm_tid(); | |
541 void* thread_key = ThreadKey(thread_id); | |
542 uint32_t thread_hash = ThreadHash(thread_id); | |
543 HashMap::Entry* entry = | |
544 thread_id_to_samplers_.Get().Lookup(thread_key, thread_hash); | |
545 DCHECK(entry != NULL); | |
546 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); | |
547 samplers->RemoveElement(sampler); | |
548 if (samplers->is_empty()) { | |
549 thread_id_to_samplers_.Pointer()->Remove(thread_key, thread_hash); | |
550 delete samplers; | |
551 } | |
552 if (thread_id_to_samplers_.Get().occupancy() == 0) { | |
553 instance_to_remove = instance_; | |
554 instance_ = NULL; | |
555 } | |
556 } | |
557 #else | |
558 bool removed = instance_->active_samplers_.RemoveElement(sampler); | |
559 DCHECK(removed); | |
560 USE(removed); | |
561 | |
562 // We cannot delete the instance immediately as we need to Join() the | |
563 // thread but we are holding mutex_ and the thread may try to acquire it. | |
564 if (instance_->active_samplers_.is_empty()) { | |
565 instance_to_remove = instance_; | |
566 instance_ = NULL; | |
567 } | |
568 #endif // USE_SIGNALS | |
569 } | |
570 | |
571 if (!instance_to_remove) return; | |
572 instance_to_remove->Join(); | |
573 delete instance_to_remove; | |
574 } | |
575 | |
576 // Unlike AddActiveSampler, this method only adds a sampler, | |
577 // but won't start the sampler thread. | |
578 static void RegisterSampler(Sampler* sampler) { | |
579 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
580 #if defined(USE_SIGNALS) | |
581 AddSampler(sampler); | |
582 #endif // USE_SIGNALS | |
583 } | |
584 | |
585 // Implement Thread::Run(). | |
586 virtual void Run() { | |
587 while (true) { | |
588 { | |
589 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
590 #if defined(USE_SIGNALS) | |
591 if (thread_id_to_samplers_.Get().occupancy() == 0) break; | |
592 if (SignalHandler::Installed()) { | |
593 for (HashMap::Entry *p = thread_id_to_samplers_.Get().Start(); | |
594 p != NULL; p = thread_id_to_samplers_.Get().Next(p)) { | |
595 #if V8_OS_AIX && V8_TARGET_ARCH_PPC64 | |
596 // on AIX64, cannot cast (void *) to pthread_t which is | |
597 // of type unsigned int (4bytes) | |
598 pthread_t thread_id = reinterpret_cast<intptr_t>(p->key); | |
599 #else | |
600 pthread_t thread_id = reinterpret_cast<pthread_t>(p->key); | |
601 #endif | |
602 pthread_kill(thread_id, SIGPROF); | |
603 } | |
604 } | |
605 #else | |
606 if (active_samplers_.is_empty()) break; | |
607 // When CPU profiling is enabled both JavaScript and C++ code is | |
608 // profiled. We must not suspend. | |
609 for (int i = 0; i < active_samplers_.length(); ++i) { | |
610 Sampler* sampler = active_samplers_.at(i); | |
611 if (!sampler->IsProfiling()) continue; | |
612 sampler->DoSample(); | |
613 } | |
614 #endif // USE_SIGNALS | |
615 } | |
616 base::OS::Sleep(base::TimeDelta::FromMilliseconds(interval_)); | |
617 } | |
618 } | |
619 | |
620 private: | |
621 // Protects the process wide state below. | |
622 static base::Mutex* mutex_; | |
623 static SamplerThread* instance_; | |
624 | |
625 const int interval_; | |
626 | |
627 #if defined(USE_SIGNALS) | |
628 struct HashMapCreateTrait { | |
629 static void Construct(HashMap* allocated_ptr) { | |
630 new (allocated_ptr) HashMap(HashMap::PointersMatch); | |
631 } | |
632 }; | |
633 friend class SignalHandler; | |
634 static base::LazyInstance<HashMap, HashMapCreateTrait>::type | |
635 thread_id_to_samplers_; | |
636 static base::AtomicValue<int> sampler_list_access_counter_; | |
637 static void AddSampler(Sampler* sampler) { | |
638 AtomicGuard atomic_guard(&sampler_list_access_counter_); | |
639 // Add sampler into map if needed. | |
640 pthread_t thread_id = sampler->platform_data()->vm_tid(); | |
641 HashMap::Entry *entry = | |
642 thread_id_to_samplers_.Pointer()->LookupOrInsert(ThreadKey(thread_id), | |
643 ThreadHash(thread_id)); | |
644 if (entry->value == NULL) { | |
645 SamplerList* samplers = new SamplerList(); | |
646 samplers->Add(sampler); | |
647 entry->value = samplers; | |
648 } else { | |
649 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); | |
650 if (!samplers->Contains(sampler)) { | |
651 samplers->Add(sampler); | |
652 } | |
653 } | |
654 } | |
655 #else | |
656 SamplerList active_samplers_; | |
657 #endif // USE_SIGNALS | |
658 | |
659 DISALLOW_COPY_AND_ASSIGN(SamplerThread); | |
660 }; | |
661 | |
662 | |
663 base::Mutex* SamplerThread::mutex_ = NULL; | |
664 SamplerThread* SamplerThread::instance_ = NULL; | |
665 #if defined(USE_SIGNALS) | |
666 base::LazyInstance<HashMap, SamplerThread::HashMapCreateTrait>::type | |
667 SamplerThread::thread_id_to_samplers_ = LAZY_INSTANCE_INITIALIZER; | |
668 base::AtomicValue<int> SamplerThread::sampler_list_access_counter_(0); | |
669 | |
670 // As Native Client does not support signal handling, profiling is disabled. | |
671 #if !V8_OS_NACL | |
672 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, | |
673 void* context) { | |
674 USE(info); | |
675 if (signal != SIGPROF) return; | |
676 AtomicGuard atomic_guard(&SamplerThread::sampler_list_access_counter_, false); | |
677 if (!atomic_guard.is_success()) return; | |
678 pthread_t thread_id = pthread_self(); | |
679 HashMap::Entry* entry = | |
680 SamplerThread::thread_id_to_samplers_.Pointer()->Lookup( | |
681 ThreadKey(thread_id), ThreadHash(thread_id)); | |
682 if (entry == NULL) | |
683 return; | |
684 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); | |
685 for (int i = 0; i < samplers->length(); ++i) { | |
686 Sampler* sampler = samplers->at(i); | |
687 CollectSample(context, sampler); | |
688 } | |
689 } | |
690 #endif // !V8_OS_NACL | |
691 #endif // USE_SIGNALs | |
692 | |
693 | |
694 void Sampler::SetUp() { | 565 void Sampler::SetUp() { |
695 #if defined(USE_SIGNALS) | 566 #if defined(USE_SIGNALS) |
696 SignalHandler::SetUp(); | 567 SignalHandler::SetUp(); |
697 #endif | 568 #endif |
698 SamplerThread::SetUp(); | |
699 } | 569 } |
700 | 570 |
701 | 571 |
702 void Sampler::TearDown() { | 572 void Sampler::TearDown() { |
703 SamplerThread::TearDown(); | |
704 #if defined(USE_SIGNALS) | 573 #if defined(USE_SIGNALS) |
705 SignalHandler::TearDown(); | 574 SignalHandler::TearDown(); |
706 #endif | 575 #endif |
707 } | 576 } |
708 | 577 |
709 Sampler::Sampler(Isolate* isolate, int interval) | 578 Sampler::Sampler(Isolate* isolate) |
710 : isolate_(isolate), | 579 : is_counting_samples_(false), |
711 interval_(interval), | 580 js_sample_count_(0), |
| 581 external_sample_count_(0), |
| 582 isolate_(isolate), |
712 profiling_(false), | 583 profiling_(false), |
713 has_processing_thread_(false), | 584 has_processing_thread_(false), |
714 active_(false), | 585 active_(false), |
715 registered_(false), | 586 registered_(false) { |
716 is_counting_samples_(false), | |
717 js_sample_count_(0), | |
718 external_sample_count_(0) { | |
719 data_ = new PlatformData; | 587 data_ = new PlatformData; |
720 } | 588 } |
721 | 589 |
722 Sampler::~Sampler() { | 590 Sampler::~Sampler() { |
723 DCHECK(!IsActive()); | 591 DCHECK(!IsActive()); |
| 592 #if defined(USE_SIGNALS) |
724 if (IsRegistered()) { | 593 if (IsRegistered()) { |
725 SamplerThread::RemoveSampler(this); | 594 SamplerManager::RemoveSampler(this); |
726 } | 595 } |
| 596 #endif |
727 delete data_; | 597 delete data_; |
728 } | 598 } |
729 | 599 |
730 void Sampler::Start() { | 600 void Sampler::Start() { |
731 DCHECK(!IsActive()); | 601 DCHECK(!IsActive()); |
732 SetActive(true); | 602 SetActive(true); |
733 SamplerThread::AddActiveSampler(this); | 603 #if defined(USE_SIGNALS) |
| 604 SamplerManager::AddSampler(this); |
| 605 #endif |
734 } | 606 } |
735 | 607 |
736 | 608 |
737 void Sampler::Stop() { | 609 void Sampler::Stop() { |
| 610 #if defined(USE_SIGNALS) |
| 611 SamplerManager::RemoveSampler(this); |
| 612 #endif |
738 DCHECK(IsActive()); | 613 DCHECK(IsActive()); |
739 SamplerThread::RemoveSampler(this); | |
740 SetActive(false); | 614 SetActive(false); |
741 SetRegistered(false); | 615 SetRegistered(false); |
742 } | 616 } |
743 | 617 |
744 | 618 |
745 void Sampler::IncreaseProfilingDepth() { | 619 void Sampler::IncreaseProfilingDepth() { |
746 base::NoBarrier_AtomicIncrement(&profiling_, 1); | 620 base::NoBarrier_AtomicIncrement(&profiling_, 1); |
747 #if defined(USE_SIGNALS) | 621 #if defined(USE_SIGNALS) |
748 SignalHandler::IncreaseSamplerCount(); | 622 SignalHandler::IncreaseSamplerCount(); |
749 #endif | 623 #endif |
750 } | 624 } |
751 | 625 |
752 | 626 |
753 void Sampler::DecreaseProfilingDepth() { | 627 void Sampler::DecreaseProfilingDepth() { |
754 #if defined(USE_SIGNALS) | 628 #if defined(USE_SIGNALS) |
755 SignalHandler::DecreaseSamplerCount(); | 629 SignalHandler::DecreaseSamplerCount(); |
756 #endif | 630 #endif |
757 base::NoBarrier_AtomicIncrement(&profiling_, -1); | 631 base::NoBarrier_AtomicIncrement(&profiling_, -1); |
758 } | 632 } |
759 | 633 |
760 | 634 |
761 void Sampler::SampleStack(const v8::RegisterState& state) { | |
762 TickSample* sample = isolate_->cpu_profiler()->StartTickSample(); | |
763 TickSample sample_obj; | |
764 if (sample == NULL) sample = &sample_obj; | |
765 sample->Init(isolate_, state, TickSample::kIncludeCEntryFrame, true); | |
766 if (is_counting_samples_ && !sample->timestamp.IsNull()) { | |
767 if (sample->state == JS) ++js_sample_count_; | |
768 if (sample->state == EXTERNAL) ++external_sample_count_; | |
769 } | |
770 Tick(sample); | |
771 if (sample != &sample_obj) { | |
772 isolate_->cpu_profiler()->FinishTickSample(); | |
773 } | |
774 } | |
775 | |
776 | |
777 #if defined(USE_SIGNALS) | 635 #if defined(USE_SIGNALS) |
778 | 636 |
779 void Sampler::DoSample() { | 637 void Sampler::DoSample() { |
780 if (!SignalHandler::Installed()) return; | 638 if (!SignalHandler::Installed()) return; |
781 if (!IsActive() && !IsRegistered()) { | 639 if (!IsActive() && !IsRegistered()) { |
782 SamplerThread::RegisterSampler(this); | 640 SamplerManager::AddSampler(this); |
783 SetRegistered(true); | 641 SetRegistered(true); |
784 } | 642 } |
785 pthread_kill(platform_data()->vm_tid(), SIGPROF); | 643 pthread_kill(platform_data()->vm_tid(), SIGPROF); |
786 } | 644 } |
787 | 645 |
788 #elif V8_OS_WIN || V8_OS_CYGWIN | 646 #elif V8_OS_WIN || V8_OS_CYGWIN |
789 | 647 |
790 void Sampler::DoSample() { | 648 void Sampler::DoSample() { |
791 HANDLE profiled_thread = platform_data()->profiled_thread(); | 649 HANDLE profiled_thread = platform_data()->profiled_thread(); |
792 if (profiled_thread == NULL) return; | 650 if (profiled_thread == NULL) return; |
793 | 651 |
794 const DWORD kSuspendFailed = static_cast<DWORD>(-1); | 652 const DWORD kSuspendFailed = static_cast<DWORD>(-1); |
795 if (SuspendThread(profiled_thread) == kSuspendFailed) return; | 653 if (SuspendThread(profiled_thread) == kSuspendFailed) return; |
796 | 654 |
797 // Context used for sampling the register state of the profiled thread. | 655 // Context used for sampling the register state of the profiled thread. |
798 CONTEXT context; | 656 CONTEXT context; |
799 memset(&context, 0, sizeof(context)); | 657 memset(&context, 0, sizeof(context)); |
800 context.ContextFlags = CONTEXT_FULL; | 658 context.ContextFlags = CONTEXT_FULL; |
801 if (GetThreadContext(profiled_thread, &context) != 0) { | 659 if (GetThreadContext(profiled_thread, &context) != 0) { |
802 v8::RegisterState state; | 660 v8::RegisterState state; |
803 #if defined(USE_SIMULATOR) | 661 #if V8_HOST_ARCH_X64 |
804 if (!SimulatorHelper::FillRegisters(isolate(), &state)) { | 662 state.pc = reinterpret_cast<void*>(context.Rip); |
805 ResumeThread(profiled_thread); | 663 state.sp = reinterpret_cast<void*>(context.Rsp); |
806 return; | 664 state.fp = reinterpret_cast<void*>(context.Rbp); |
807 } | |
808 #else | 665 #else |
809 #if V8_HOST_ARCH_X64 | 666 state.pc = reinterpret_cast<void*>(context.Eip); |
810 state.pc = reinterpret_cast<Address>(context.Rip); | 667 state.sp = reinterpret_cast<void*>(context.Esp); |
811 state.sp = reinterpret_cast<Address>(context.Rsp); | 668 state.fp = reinterpret_cast<void*>(context.Ebp); |
812 state.fp = reinterpret_cast<Address>(context.Rbp); | |
813 #else | |
814 state.pc = reinterpret_cast<Address>(context.Eip); | |
815 state.sp = reinterpret_cast<Address>(context.Esp); | |
816 state.fp = reinterpret_cast<Address>(context.Ebp); | |
817 #endif | 669 #endif |
818 #endif // USE_SIMULATOR | |
819 SampleStack(state); | 670 SampleStack(state); |
820 } | 671 } |
821 ResumeThread(profiled_thread); | 672 ResumeThread(profiled_thread); |
822 } | 673 } |
823 | 674 |
824 #endif // USE_SIGNALS | 675 #endif // USE_SIGNALS |
825 | 676 |
826 | 677 } // namespace sampler |
827 } // namespace internal | |
828 } // namespace v8 | 678 } // namespace v8 |
OLD | NEW |