OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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/profiler/tick-sample.h" |
6 | 6 |
7 #if V8_OS_POSIX && !V8_OS_CYGWIN | |
8 | |
9 #define USE_SIGNALS | |
10 | |
11 #include <errno.h> | |
12 #include <pthread.h> | |
13 #include <signal.h> | |
14 #include <sys/time.h> | |
15 | |
16 #if !V8_OS_QNX && !V8_OS_NACL && !V8_OS_AIX | |
17 #include <sys/syscall.h> // NOLINT | |
18 #endif | |
19 | |
20 #if V8_OS_MACOSX | |
21 #include <mach/mach.h> | |
22 // OpenBSD doesn't have <ucontext.h>. ucontext_t lives in <signal.h> | |
23 // and is a typedef for struct sigcontext. There is no uc_mcontext. | |
24 #elif(!V8_OS_ANDROID || defined(__BIONIC_HAVE_UCONTEXT_T)) && \ | |
25 !V8_OS_OPENBSD && !V8_OS_NACL | |
26 #include <ucontext.h> | |
27 #endif | |
28 | |
29 #include <unistd.h> | |
30 | |
31 // GLibc on ARM defines mcontext_t has a typedef for 'struct sigcontext'. | |
32 // Old versions of the C library <signal.h> didn't define the type. | |
33 #if V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) && \ | |
34 (defined(__arm__) || defined(__aarch64__)) && \ | |
35 !defined(__BIONIC_HAVE_STRUCT_SIGCONTEXT) | |
36 #include <asm/sigcontext.h> // NOLINT | |
37 #endif | |
38 | |
39 #elif V8_OS_WIN || V8_OS_CYGWIN | |
40 | |
41 #include "src/base/win32-headers.h" | |
42 | |
43 #endif | |
44 | |
45 #include "src/base/atomic-utils.h" | |
46 #include "src/base/platform/platform.h" | |
47 #include "src/flags.h" | |
48 #include "src/frames-inl.h" | 7 #include "src/frames-inl.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" | 8 #include "src/vm-state-inl.h" |
54 | 9 |
55 | 10 |
56 #if V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) | |
57 | |
58 // Not all versions of Android's C library provide ucontext_t. | |
59 // Detect this and provide custom but compatible definitions. Note that these | |
60 // follow the GLibc naming convention to access register values from | |
61 // mcontext_t. | |
62 // | |
63 // See http://code.google.com/p/android/issues/detail?id=34784 | |
64 | |
65 #if defined(__arm__) | |
66 | |
67 typedef struct sigcontext mcontext_t; | |
68 | |
69 typedef struct ucontext { | |
70 uint32_t uc_flags; | |
71 struct ucontext* uc_link; | |
72 stack_t uc_stack; | |
73 mcontext_t uc_mcontext; | |
74 // Other fields are not used by V8, don't define them here. | |
75 } ucontext_t; | |
76 | |
77 #elif defined(__aarch64__) | |
78 | |
79 typedef struct sigcontext mcontext_t; | |
80 | |
81 typedef struct ucontext { | |
82 uint64_t uc_flags; | |
83 struct ucontext *uc_link; | |
84 stack_t uc_stack; | |
85 mcontext_t uc_mcontext; | |
86 // Other fields are not used by V8, don't define them here. | |
87 } ucontext_t; | |
88 | |
89 #elif defined(__mips__) | |
90 // MIPS version of sigcontext, for Android bionic. | |
91 typedef struct { | |
92 uint32_t regmask; | |
93 uint32_t status; | |
94 uint64_t pc; | |
95 uint64_t gregs[32]; | |
96 uint64_t fpregs[32]; | |
97 uint32_t acx; | |
98 uint32_t fpc_csr; | |
99 uint32_t fpc_eir; | |
100 uint32_t used_math; | |
101 uint32_t dsp; | |
102 uint64_t mdhi; | |
103 uint64_t mdlo; | |
104 uint32_t hi1; | |
105 uint32_t lo1; | |
106 uint32_t hi2; | |
107 uint32_t lo2; | |
108 uint32_t hi3; | |
109 uint32_t lo3; | |
110 } mcontext_t; | |
111 | |
112 typedef struct ucontext { | |
113 uint32_t uc_flags; | |
114 struct ucontext* uc_link; | |
115 stack_t uc_stack; | |
116 mcontext_t uc_mcontext; | |
117 // Other fields are not used by V8, don't define them here. | |
118 } ucontext_t; | |
119 | |
120 #elif defined(__i386__) | |
121 // x86 version for Android. | |
122 typedef struct { | |
123 uint32_t gregs[19]; | |
124 void* fpregs; | |
125 uint32_t oldmask; | |
126 uint32_t cr2; | |
127 } mcontext_t; | |
128 | |
129 typedef uint32_t kernel_sigset_t[2]; // x86 kernel uses 64-bit signal masks | |
130 typedef struct ucontext { | |
131 uint32_t uc_flags; | |
132 struct ucontext* uc_link; | |
133 stack_t uc_stack; | |
134 mcontext_t uc_mcontext; | |
135 // Other fields are not used by V8, don't define them here. | |
136 } ucontext_t; | |
137 enum { REG_EBP = 6, REG_ESP = 7, REG_EIP = 14 }; | |
138 | |
139 #elif defined(__x86_64__) | |
140 // x64 version for Android. | |
141 typedef struct { | |
142 uint64_t gregs[23]; | |
143 void* fpregs; | |
144 uint64_t __reserved1[8]; | |
145 } mcontext_t; | |
146 | |
147 typedef struct ucontext { | |
148 uint64_t uc_flags; | |
149 struct ucontext *uc_link; | |
150 stack_t uc_stack; | |
151 mcontext_t uc_mcontext; | |
152 // Other fields are not used by V8, don't define them here. | |
153 } ucontext_t; | |
154 enum { REG_RBP = 10, REG_RSP = 15, REG_RIP = 16 }; | |
155 #endif | |
156 | |
157 #endif // V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) | |
158 | |
159 | |
160 namespace v8 { | 11 namespace v8 { |
161 namespace internal { | 12 namespace internal { |
162 | 13 |
163 namespace { | 14 namespace { |
164 | 15 |
165 class PlatformDataCommon : public Malloced { | |
166 public: | |
167 PlatformDataCommon() : profiled_thread_id_(ThreadId::Current()) {} | |
168 ThreadId profiled_thread_id() { return profiled_thread_id_; } | |
169 | |
170 protected: | |
171 ~PlatformDataCommon() {} | |
172 | |
173 private: | |
174 ThreadId profiled_thread_id_; | |
175 }; | |
176 | |
177 | |
178 bool IsSamePage(byte* ptr1, byte* ptr2) { | 16 bool IsSamePage(byte* ptr1, byte* ptr2) { |
179 const uint32_t kPageSize = 4096; | 17 const uint32_t kPageSize = 4096; |
180 uintptr_t mask = ~static_cast<uintptr_t>(kPageSize - 1); | 18 uintptr_t mask = ~static_cast<uintptr_t>(kPageSize - 1); |
181 return (reinterpret_cast<uintptr_t>(ptr1) & mask) == | 19 return (reinterpret_cast<uintptr_t>(ptr1) & mask) == |
182 (reinterpret_cast<uintptr_t>(ptr2) & mask); | 20 (reinterpret_cast<uintptr_t>(ptr2) & mask); |
183 } | 21 } |
184 | 22 |
185 | 23 |
186 // Check if the code at specified address could potentially be a | 24 // Check if the code at specified address could potentially be a |
187 // frame setup code. | 25 // frame setup code. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 // pessimistically assume it could be the entire pattern match. | 68 // pessimistically assume it could be the entire pattern match. |
231 MSAN_MEMORY_IS_INITIALIZED(pc, pattern->bytes_count - offset); | 69 MSAN_MEMORY_IS_INITIALIZED(pc, pattern->bytes_count - offset); |
232 if (!memcmp(pc, pattern->bytes + offset, pattern->bytes_count - offset)) | 70 if (!memcmp(pc, pattern->bytes + offset, pattern->bytes_count - offset)) |
233 return true; | 71 return true; |
234 } | 72 } |
235 } | 73 } |
236 } | 74 } |
237 return false; | 75 return false; |
238 } | 76 } |
239 | 77 |
240 typedef List<Sampler*> SamplerList; | |
241 | |
242 #if defined(USE_SIGNALS) | |
243 class AtomicGuard { | |
244 public: | |
245 explicit AtomicGuard(base::AtomicValue<int>* atomic, bool is_block = true) | |
246 : atomic_(atomic), | |
247 is_success_(false) { | |
248 do { | |
249 // Use Acquire_Load to gain mutual exclusion. | |
250 USE(atomic_->Value()); | |
251 is_success_ = atomic_->TrySetValue(0, 1); | |
252 } while (is_block && !is_success_); | |
253 } | |
254 | |
255 bool is_success() { return is_success_; } | |
256 | |
257 ~AtomicGuard() { | |
258 if (is_success_) { | |
259 atomic_->SetValue(0); | |
260 } | |
261 atomic_ = NULL; | |
262 } | |
263 | |
264 private: | |
265 base::AtomicValue<int>* atomic_; | |
266 bool is_success_; | |
267 }; | |
268 | |
269 | |
270 // Returns key for hash map. | |
271 void* ThreadKey(pthread_t thread_id) { | |
272 return reinterpret_cast<void*>(thread_id); | |
273 } | |
274 | |
275 | |
276 // Returns hash value for hash map. | |
277 uint32_t ThreadHash(pthread_t thread_id) { | |
278 #if V8_OS_MACOSX | |
279 return static_cast<uint32_t>(reinterpret_cast<intptr_t>(thread_id)); | |
280 #else | |
281 return static_cast<uint32_t>(thread_id); | |
282 #endif | |
283 } | |
284 #endif // USE_SIGNALS | |
285 | |
286 } // namespace | 78 } // namespace |
287 | 79 |
288 #if defined(USE_SIGNALS) | |
289 | |
290 class Sampler::PlatformData : public PlatformDataCommon { | |
291 public: | |
292 PlatformData() : vm_tid_(pthread_self()) {} | |
293 pthread_t vm_tid() const { return vm_tid_; } | |
294 | |
295 private: | |
296 pthread_t vm_tid_; | |
297 }; | |
298 | |
299 #elif V8_OS_WIN || V8_OS_CYGWIN | |
300 | |
301 // ---------------------------------------------------------------------------- | |
302 // Win32 profiler support. On Cygwin we use the same sampler implementation as | |
303 // on Win32. | |
304 | |
305 class Sampler::PlatformData : public PlatformDataCommon { | |
306 public: | |
307 // 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 | |
309 // going to use it in the sampler thread. Using GetThreadHandle() will | |
310 // not work in this case. We're using OpenThread because DuplicateHandle | |
311 // for some reason doesn't work in Chrome's sandbox. | |
312 PlatformData() | |
313 : profiled_thread_(OpenThread(THREAD_GET_CONTEXT | | |
314 THREAD_SUSPEND_RESUME | | |
315 THREAD_QUERY_INFORMATION, | |
316 false, | |
317 GetCurrentThreadId())) {} | |
318 | |
319 ~PlatformData() { | |
320 if (profiled_thread_ != NULL) { | |
321 CloseHandle(profiled_thread_); | |
322 profiled_thread_ = NULL; | |
323 } | |
324 } | |
325 | |
326 HANDLE profiled_thread() { return profiled_thread_; } | |
327 | |
328 private: | |
329 HANDLE profiled_thread_; | |
330 }; | |
331 #endif | |
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 | |
388 | |
389 #if defined(USE_SIGNALS) | |
390 | |
391 class SignalHandler : public AllStatic { | |
392 public: | |
393 static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } | |
394 static void TearDown() { delete mutex_; mutex_ = NULL; } | |
395 | |
396 static void IncreaseSamplerCount() { | |
397 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
398 if (++client_count_ == 1) Install(); | |
399 } | |
400 | |
401 static void DecreaseSamplerCount() { | |
402 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
403 if (--client_count_ == 0) Restore(); | |
404 } | |
405 | |
406 static bool Installed() { | |
407 return signal_handler_installed_; | |
408 } | |
409 | |
410 #if !V8_OS_NACL | |
411 static void CollectSample(void* context, Sampler* sampler); | |
412 #endif | |
413 | |
414 private: | |
415 static void Install() { | |
416 #if !V8_OS_NACL | |
417 struct sigaction sa; | |
418 sa.sa_sigaction = &HandleProfilerSignal; | |
419 sigemptyset(&sa.sa_mask); | |
420 #if V8_OS_QNX | |
421 sa.sa_flags = SA_SIGINFO; | |
422 #else | |
423 sa.sa_flags = SA_RESTART | SA_SIGINFO; | |
424 #endif | |
425 signal_handler_installed_ = | |
426 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); | |
427 #endif | |
428 } | |
429 | |
430 static void Restore() { | |
431 #if !V8_OS_NACL | |
432 if (signal_handler_installed_) { | |
433 sigaction(SIGPROF, &old_signal_handler_, 0); | |
434 signal_handler_installed_ = false; | |
435 } | |
436 #endif | |
437 } | |
438 | |
439 #if !V8_OS_NACL | |
440 static void HandleProfilerSignal(int signal, siginfo_t* info, void* context); | |
441 #endif | |
442 // Protects the process wide state below. | |
443 static base::Mutex* mutex_; | |
444 static int client_count_; | |
445 static bool signal_handler_installed_; | |
446 static struct sigaction old_signal_handler_; | |
447 }; | |
448 | |
449 | |
450 base::Mutex* SignalHandler::mutex_ = NULL; | |
451 int SignalHandler::client_count_ = 0; | |
452 struct sigaction SignalHandler::old_signal_handler_; | |
453 bool SignalHandler::signal_handler_installed_ = false; | |
454 | |
455 | |
456 // As Native Client does not support signal handling, profiling is disabled. | |
457 #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 #if V8_OS_AIX && V8_TARGET_ARCH_PPC64 | |
716 // on AIX64, cannot cast (void *) to pthread_t which is | |
717 // of type unsigned int (4bytes) | |
718 pthread_t thread_id = reinterpret_cast<intptr_t>(p->key); | |
719 #else | |
720 pthread_t thread_id = reinterpret_cast<pthread_t>(p->key); | |
721 #endif | |
722 pthread_kill(thread_id, SIGPROF); | |
723 } | |
724 } | |
725 #else | |
726 if (active_samplers_.is_empty()) break; | |
727 // When CPU profiling is enabled both JavaScript and C++ code is | |
728 // profiled. We must not suspend. | |
729 for (int i = 0; i < active_samplers_.length(); ++i) { | |
730 Sampler* sampler = active_samplers_.at(i); | |
731 if (!sampler->IsProfiling()) continue; | |
732 sampler->DoSample(); | |
733 } | |
734 #endif // USE_SIGNALS | |
735 } | |
736 base::OS::Sleep(base::TimeDelta::FromMilliseconds(interval_)); | |
737 } | |
738 } | |
739 | |
740 private: | |
741 // Protects the process wide state below. | |
742 static base::Mutex* mutex_; | |
743 static SamplerThread* instance_; | |
744 | |
745 const int interval_; | |
746 | |
747 #if defined(USE_SIGNALS) | |
748 struct HashMapCreateTrait { | |
749 static void Construct(HashMap* allocated_ptr) { | |
750 new (allocated_ptr) HashMap(HashMap::PointersMatch); | |
751 } | |
752 }; | |
753 friend class SignalHandler; | |
754 static base::LazyInstance<HashMap, HashMapCreateTrait>::type | |
755 thread_id_to_samplers_; | |
756 static base::AtomicValue<int> sampler_list_access_counter_; | |
757 static void AddSampler(Sampler* sampler) { | |
758 AtomicGuard atomic_guard(&sampler_list_access_counter_); | |
759 // Add sampler into map if needed. | |
760 pthread_t thread_id = sampler->platform_data()->vm_tid(); | |
761 HashMap::Entry *entry = | |
762 thread_id_to_samplers_.Pointer()->LookupOrInsert(ThreadKey(thread_id), | |
763 ThreadHash(thread_id)); | |
764 if (entry->value == NULL) { | |
765 SamplerList* samplers = new SamplerList(); | |
766 samplers->Add(sampler); | |
767 entry->value = samplers; | |
768 } else { | |
769 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); | |
770 if (!samplers->Contains(sampler)) { | |
771 samplers->Add(sampler); | |
772 } | |
773 } | |
774 } | |
775 #else | |
776 SamplerList active_samplers_; | |
777 #endif // USE_SIGNALS | |
778 | |
779 DISALLOW_COPY_AND_ASSIGN(SamplerThread); | |
780 }; | |
781 | |
782 | |
783 base::Mutex* SamplerThread::mutex_ = NULL; | |
784 SamplerThread* SamplerThread::instance_ = NULL; | |
785 #if defined(USE_SIGNALS) | |
786 base::LazyInstance<HashMap, SamplerThread::HashMapCreateTrait>::type | |
787 SamplerThread::thread_id_to_samplers_ = LAZY_INSTANCE_INITIALIZER; | |
788 base::AtomicValue<int> SamplerThread::sampler_list_access_counter_(0); | |
789 | |
790 // As Native Client does not support signal handling, profiling is disabled. | |
791 #if !V8_OS_NACL | |
792 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, | |
793 void* context) { | |
794 USE(info); | |
795 if (signal != SIGPROF) return; | |
796 AtomicGuard atomic_guard(&SamplerThread::sampler_list_access_counter_, false); | |
797 if (!atomic_guard.is_success()) return; | |
798 pthread_t thread_id = pthread_self(); | |
799 HashMap::Entry* entry = | |
800 SamplerThread::thread_id_to_samplers_.Pointer()->Lookup( | |
801 ThreadKey(thread_id), ThreadHash(thread_id)); | |
802 if (entry == NULL) | |
803 return; | |
804 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value); | |
805 for (int i = 0; i < samplers->length(); ++i) { | |
806 Sampler* sampler = samplers->at(i); | |
807 CollectSample(context, sampler); | |
808 } | |
809 } | |
810 #endif // !V8_OS_NACL | |
811 #endif // USE_SIGNALs | |
812 | |
813 | 80 |
814 // | 81 // |
815 // StackTracer implementation | 82 // StackTracer implementation |
816 // | 83 // |
817 DISABLE_ASAN void TickSample::Init(Isolate* isolate, | 84 DISABLE_ASAN void TickSample::Init(Isolate* isolate, |
818 const v8::RegisterState& regs, | 85 const v8::RegisterState& regs, |
819 RecordCEntryFrame record_c_entry_frame, | 86 RecordCEntryFrame record_c_entry_frame, |
820 bool update_stats) { | 87 bool update_stats) { |
821 timestamp = base::TimeTicks::HighResolutionNow(); | 88 timestamp = base::TimeTicks::HighResolutionNow(); |
822 pc = reinterpret_cast<Address>(regs.pc); | 89 pc = reinterpret_cast<Address>(regs.pc); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
901 frame->GetBytecodeOffset(); | 168 frame->GetBytecodeOffset(); |
902 } else { | 169 } else { |
903 frames[i++] = it.frame()->pc(); | 170 frames[i++] = it.frame()->pc(); |
904 } | 171 } |
905 it.Advance(); | 172 it.Advance(); |
906 } | 173 } |
907 sample_info->frames_count = i; | 174 sample_info->frames_count = i; |
908 } | 175 } |
909 | 176 |
910 | 177 |
911 void Sampler::SetUp() { | 178 #if defined(USE_SIMULATOR) |
912 #if defined(USE_SIGNALS) | 179 bool SimulatorHelper::FillRegisters(Isolate* isolate, |
913 SignalHandler::SetUp(); | 180 v8::RegisterState* state) { |
| 181 Simulator *simulator = isolate->thread_local_top()->simulator_; |
| 182 // Check if there is active simulator. |
| 183 if (simulator == NULL) return false; |
| 184 #if V8_TARGET_ARCH_ARM |
| 185 if (!simulator->has_bad_pc()) { |
| 186 state->pc = reinterpret_cast<Address>(simulator->get_pc()); |
| 187 } |
| 188 state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp)); |
| 189 state->fp = reinterpret_cast<Address>(simulator->get_register( |
| 190 Simulator::r11)); |
| 191 #elif V8_TARGET_ARCH_ARM64 |
| 192 state->pc = reinterpret_cast<Address>(simulator->pc()); |
| 193 state->sp = reinterpret_cast<Address>(simulator->sp()); |
| 194 state->fp = reinterpret_cast<Address>(simulator->fp()); |
| 195 #elif V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 |
| 196 if (!simulator->has_bad_pc()) { |
| 197 state->pc = reinterpret_cast<Address>(simulator->get_pc()); |
| 198 } |
| 199 state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp)); |
| 200 state->fp = reinterpret_cast<Address>(simulator->get_register(Simulator::fp)); |
| 201 #elif V8_TARGET_ARCH_PPC |
| 202 if (!simulator->has_bad_pc()) { |
| 203 state->pc = reinterpret_cast<Address>(simulator->get_pc()); |
| 204 } |
| 205 state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp)); |
| 206 state->fp = reinterpret_cast<Address>(simulator->get_register(Simulator::fp)); |
| 207 #elif V8_TARGET_ARCH_S390 |
| 208 if (!simulator->has_bad_pc()) { |
| 209 state->pc = reinterpret_cast<Address>(simulator->get_pc()); |
| 210 } |
| 211 state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp)); |
| 212 state->fp = reinterpret_cast<Address>(simulator->get_register(Simulator::fp)); |
914 #endif | 213 #endif |
915 SamplerThread::SetUp(); | 214 if (state->sp == 0 || state->fp == 0) { |
| 215 // It possible that the simulator is interrupted while it is updating |
| 216 // the sp or fp register. ARM64 simulator does this in two steps: |
| 217 // first setting it to zero and then setting it to the new value. |
| 218 // Bailout if sp/fp doesn't contain the new value. |
| 219 // |
| 220 // FIXME: The above doesn't really solve the issue. |
| 221 // If a 64-bit target is executed on a 32-bit host even the final |
| 222 // write is non-atomic, so it might obtain a half of the result. |
| 223 // Moreover as long as the register set code uses memcpy (as of now), |
| 224 // it is not guaranteed to be atomic even when both host and target |
| 225 // are of same bitness. |
| 226 return false; |
| 227 } |
| 228 return true; |
916 } | 229 } |
917 | |
918 | |
919 void Sampler::TearDown() { | |
920 SamplerThread::TearDown(); | |
921 #if defined(USE_SIGNALS) | |
922 SignalHandler::TearDown(); | |
923 #endif | |
924 } | |
925 | |
926 Sampler::Sampler(Isolate* isolate, int interval) | |
927 : isolate_(isolate), | |
928 interval_(interval), | |
929 profiling_(false), | |
930 has_processing_thread_(false), | |
931 active_(false), | |
932 registered_(false), | |
933 is_counting_samples_(false), | |
934 js_sample_count_(0), | |
935 external_sample_count_(0) { | |
936 data_ = new PlatformData; | |
937 } | |
938 | |
939 Sampler::~Sampler() { | |
940 DCHECK(!IsActive()); | |
941 if (IsRegistered()) { | |
942 SamplerThread::RemoveSampler(this); | |
943 } | |
944 delete data_; | |
945 } | |
946 | |
947 void Sampler::Start() { | |
948 DCHECK(!IsActive()); | |
949 SetActive(true); | |
950 SamplerThread::AddActiveSampler(this); | |
951 } | |
952 | |
953 | |
954 void Sampler::Stop() { | |
955 DCHECK(IsActive()); | |
956 SamplerThread::RemoveSampler(this); | |
957 SetActive(false); | |
958 SetRegistered(false); | |
959 } | |
960 | |
961 | |
962 void Sampler::IncreaseProfilingDepth() { | |
963 base::NoBarrier_AtomicIncrement(&profiling_, 1); | |
964 #if defined(USE_SIGNALS) | |
965 SignalHandler::IncreaseSamplerCount(); | |
966 #endif | |
967 } | |
968 | |
969 | |
970 void Sampler::DecreaseProfilingDepth() { | |
971 #if defined(USE_SIGNALS) | |
972 SignalHandler::DecreaseSamplerCount(); | |
973 #endif | |
974 base::NoBarrier_AtomicIncrement(&profiling_, -1); | |
975 } | |
976 | |
977 | |
978 void Sampler::SampleStack(const v8::RegisterState& state) { | |
979 TickSample* sample = isolate_->cpu_profiler()->StartTickSample(); | |
980 TickSample sample_obj; | |
981 if (sample == NULL) sample = &sample_obj; | |
982 sample->Init(isolate_, state, TickSample::kIncludeCEntryFrame, true); | |
983 if (is_counting_samples_ && !sample->timestamp.IsNull()) { | |
984 if (sample->state == JS) ++js_sample_count_; | |
985 if (sample->state == EXTERNAL) ++external_sample_count_; | |
986 } | |
987 Tick(sample); | |
988 if (sample != &sample_obj) { | |
989 isolate_->cpu_profiler()->FinishTickSample(); | |
990 } | |
991 } | |
992 | |
993 | |
994 #if defined(USE_SIGNALS) | |
995 | |
996 void Sampler::DoSample() { | |
997 if (!SignalHandler::Installed()) return; | |
998 if (!IsActive() && !IsRegistered()) { | |
999 SamplerThread::RegisterSampler(this); | |
1000 SetRegistered(true); | |
1001 } | |
1002 pthread_kill(platform_data()->vm_tid(), SIGPROF); | |
1003 } | |
1004 | |
1005 #elif V8_OS_WIN || V8_OS_CYGWIN | |
1006 | |
1007 void Sampler::DoSample() { | |
1008 HANDLE profiled_thread = platform_data()->profiled_thread(); | |
1009 if (profiled_thread == NULL) return; | |
1010 | |
1011 const DWORD kSuspendFailed = static_cast<DWORD>(-1); | |
1012 if (SuspendThread(profiled_thread) == kSuspendFailed) return; | |
1013 | |
1014 // Context used for sampling the register state of the profiled thread. | |
1015 CONTEXT context; | |
1016 memset(&context, 0, sizeof(context)); | |
1017 context.ContextFlags = CONTEXT_FULL; | |
1018 if (GetThreadContext(profiled_thread, &context) != 0) { | |
1019 v8::RegisterState state; | |
1020 #if defined(USE_SIMULATOR) | |
1021 if (!SimulatorHelper::FillRegisters(isolate(), &state)) { | |
1022 ResumeThread(profiled_thread); | |
1023 return; | |
1024 } | |
1025 #else | |
1026 #if V8_HOST_ARCH_X64 | |
1027 state.pc = reinterpret_cast<Address>(context.Rip); | |
1028 state.sp = reinterpret_cast<Address>(context.Rsp); | |
1029 state.fp = reinterpret_cast<Address>(context.Rbp); | |
1030 #else | |
1031 state.pc = reinterpret_cast<Address>(context.Eip); | |
1032 state.sp = reinterpret_cast<Address>(context.Esp); | |
1033 state.fp = reinterpret_cast<Address>(context.Ebp); | |
1034 #endif | |
1035 #endif // USE_SIMULATOR | 230 #endif // USE_SIMULATOR |
1036 SampleStack(state); | |
1037 } | |
1038 ResumeThread(profiled_thread); | |
1039 } | |
1040 | |
1041 #endif // USE_SIGNALS | |
1042 | |
1043 | 231 |
1044 } // namespace internal | 232 } // namespace internal |
1045 } // namespace v8 | 233 } // namespace v8 |
OLD | NEW |