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/sampler.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/platform/platform.h" | |
46 #include "src/flags.h" | |
47 #include "src/frames-inl.h" | 7 #include "src/frames-inl.h" |
48 #include "src/log.h" | |
49 #include "src/profiler/cpu-profiler-inl.h" | |
50 #include "src/simulator.h" | |
51 #include "src/v8threads.h" | |
52 #include "src/vm-state-inl.h" | 8 #include "src/vm-state-inl.h" |
53 | 9 |
54 | |
55 #if V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) | |
56 | |
57 // Not all versions of Android's C library provide ucontext_t. | |
58 // Detect this and provide custom but compatible definitions. Note that these | |
59 // follow the GLibc naming convention to access register values from | |
60 // mcontext_t. | |
61 // | |
62 // See http://code.google.com/p/android/issues/detail?id=34784 | |
63 | |
64 #if defined(__arm__) | |
65 | |
66 typedef struct sigcontext mcontext_t; | |
67 | |
68 typedef struct ucontext { | |
69 uint32_t uc_flags; | |
70 struct ucontext* uc_link; | |
71 stack_t uc_stack; | |
72 mcontext_t uc_mcontext; | |
73 // Other fields are not used by V8, don't define them here. | |
74 } ucontext_t; | |
75 | |
76 #elif defined(__aarch64__) | |
77 | |
78 typedef struct sigcontext mcontext_t; | |
79 | |
80 typedef struct ucontext { | |
81 uint64_t uc_flags; | |
82 struct ucontext *uc_link; | |
83 stack_t uc_stack; | |
84 mcontext_t uc_mcontext; | |
85 // Other fields are not used by V8, don't define them here. | |
86 } ucontext_t; | |
87 | |
88 #elif defined(__mips__) | |
89 // MIPS version of sigcontext, for Android bionic. | |
90 typedef struct { | |
91 uint32_t regmask; | |
92 uint32_t status; | |
93 uint64_t pc; | |
94 uint64_t gregs[32]; | |
95 uint64_t fpregs[32]; | |
96 uint32_t acx; | |
97 uint32_t fpc_csr; | |
98 uint32_t fpc_eir; | |
99 uint32_t used_math; | |
100 uint32_t dsp; | |
101 uint64_t mdhi; | |
102 uint64_t mdlo; | |
103 uint32_t hi1; | |
104 uint32_t lo1; | |
105 uint32_t hi2; | |
106 uint32_t lo2; | |
107 uint32_t hi3; | |
108 uint32_t lo3; | |
109 } mcontext_t; | |
110 | |
111 typedef struct ucontext { | |
112 uint32_t uc_flags; | |
113 struct ucontext* uc_link; | |
114 stack_t uc_stack; | |
115 mcontext_t uc_mcontext; | |
116 // Other fields are not used by V8, don't define them here. | |
117 } ucontext_t; | |
118 | |
119 #elif defined(__i386__) | |
120 // x86 version for Android. | |
121 typedef struct { | |
122 uint32_t gregs[19]; | |
123 void* fpregs; | |
124 uint32_t oldmask; | |
125 uint32_t cr2; | |
126 } mcontext_t; | |
127 | |
128 typedef uint32_t kernel_sigset_t[2]; // x86 kernel uses 64-bit signal masks | |
129 typedef struct ucontext { | |
130 uint32_t uc_flags; | |
131 struct ucontext* uc_link; | |
132 stack_t uc_stack; | |
133 mcontext_t uc_mcontext; | |
134 // Other fields are not used by V8, don't define them here. | |
135 } ucontext_t; | |
136 enum { REG_EBP = 6, REG_ESP = 7, REG_EIP = 14 }; | |
137 | |
138 #elif defined(__x86_64__) | |
139 // x64 version for Android. | |
140 typedef struct { | |
141 uint64_t gregs[23]; | |
142 void* fpregs; | |
143 uint64_t __reserved1[8]; | |
144 } mcontext_t; | |
145 | |
146 typedef struct ucontext { | |
147 uint64_t uc_flags; | |
148 struct ucontext *uc_link; | |
149 stack_t uc_stack; | |
150 mcontext_t uc_mcontext; | |
151 // Other fields are not used by V8, don't define them here. | |
152 } ucontext_t; | |
153 enum { REG_RBP = 10, REG_RSP = 15, REG_RIP = 16 }; | |
154 #endif | |
155 | |
156 #endif // V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) | |
157 | |
158 | |
159 namespace v8 { | 10 namespace v8 { |
160 namespace internal { | 11 namespace internal { |
161 | 12 |
162 namespace { | 13 namespace { |
163 | 14 |
164 class PlatformDataCommon : public Malloced { | |
165 public: | |
166 PlatformDataCommon() : profiled_thread_id_(ThreadId::Current()) {} | |
167 ThreadId profiled_thread_id() { return profiled_thread_id_; } | |
168 | |
169 protected: | |
170 ~PlatformDataCommon() {} | |
171 | |
172 private: | |
173 ThreadId profiled_thread_id_; | |
174 }; | |
175 | |
176 | |
177 bool IsSamePage(byte* ptr1, byte* ptr2) { | 15 bool IsSamePage(byte* ptr1, byte* ptr2) { |
178 const uint32_t kPageSize = 4096; | 16 const uint32_t kPageSize = 4096; |
179 uintptr_t mask = ~static_cast<uintptr_t>(kPageSize - 1); | 17 uintptr_t mask = ~static_cast<uintptr_t>(kPageSize - 1); |
180 return (reinterpret_cast<uintptr_t>(ptr1) & mask) == | 18 return (reinterpret_cast<uintptr_t>(ptr1) & mask) == |
181 (reinterpret_cast<uintptr_t>(ptr2) & mask); | 19 (reinterpret_cast<uintptr_t>(ptr2) & mask); |
182 } | 20 } |
183 | 21 |
184 | |
185 // Check if the code at specified address could potentially be a | 22 // Check if the code at specified address could potentially be a |
186 // frame setup code. | 23 // frame setup code. |
187 bool IsNoFrameRegion(Address address) { | 24 bool IsNoFrameRegion(Address address) { |
188 struct Pattern { | 25 struct Pattern { |
189 int bytes_count; | 26 int bytes_count; |
190 byte bytes[8]; | 27 byte bytes[8]; |
191 int offsets[4]; | 28 int offsets[4]; |
192 }; | 29 }; |
193 byte* pc = reinterpret_cast<byte*>(address); | 30 byte* pc = reinterpret_cast<byte*>(address); |
194 static Pattern patterns[] = { | 31 static Pattern patterns[] = { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 if (!memcmp(pc, pattern->bytes + offset, pattern->bytes_count - offset)) | 68 if (!memcmp(pc, pattern->bytes + offset, pattern->bytes_count - offset)) |
232 return true; | 69 return true; |
233 } | 70 } |
234 } | 71 } |
235 } | 72 } |
236 return false; | 73 return false; |
237 } | 74 } |
238 | 75 |
239 } // namespace | 76 } // namespace |
240 | 77 |
241 #if defined(USE_SIGNALS) | |
242 | |
243 class Sampler::PlatformData : public PlatformDataCommon { | |
244 public: | |
245 PlatformData() : vm_tid_(pthread_self()) {} | |
246 pthread_t vm_tid() const { return vm_tid_; } | |
247 | |
248 private: | |
249 pthread_t vm_tid_; | |
250 }; | |
251 | |
252 #elif V8_OS_WIN || V8_OS_CYGWIN | |
253 | |
254 // ---------------------------------------------------------------------------- | |
255 // Win32 profiler support. On Cygwin we use the same sampler implementation as | |
256 // on Win32. | |
257 | |
258 class Sampler::PlatformData : public PlatformDataCommon { | |
259 public: | |
260 // Get a handle to the calling thread. This is the thread that we are | |
261 // going to profile. We need to make a copy of the handle because we are | |
262 // going to use it in the sampler thread. Using GetThreadHandle() will | |
263 // not work in this case. We're using OpenThread because DuplicateHandle | |
264 // for some reason doesn't work in Chrome's sandbox. | |
265 PlatformData() | |
266 : profiled_thread_(OpenThread(THREAD_GET_CONTEXT | | |
267 THREAD_SUSPEND_RESUME | | |
268 THREAD_QUERY_INFORMATION, | |
269 false, | |
270 GetCurrentThreadId())) {} | |
271 | |
272 ~PlatformData() { | |
273 if (profiled_thread_ != NULL) { | |
274 CloseHandle(profiled_thread_); | |
275 profiled_thread_ = NULL; | |
276 } | |
277 } | |
278 | |
279 HANDLE profiled_thread() { return profiled_thread_; } | |
280 | |
281 private: | |
282 HANDLE profiled_thread_; | |
283 }; | |
284 #endif | |
285 | |
286 | |
287 #if defined(USE_SIMULATOR) | |
288 class SimulatorHelper { | |
289 public: | |
290 inline bool Init(Isolate* isolate) { | |
291 simulator_ = isolate->thread_local_top()->simulator_; | |
292 // Check if there is active simulator. | |
293 return simulator_ != NULL; | |
294 } | |
295 | |
296 inline void FillRegisters(v8::RegisterState* state) { | |
297 #if V8_TARGET_ARCH_ARM | |
298 if (!simulator_->has_bad_pc()) { | |
299 state->pc = reinterpret_cast<Address>(simulator_->get_pc()); | |
300 } | |
301 state->sp = reinterpret_cast<Address>(simulator_->get_register( | |
302 Simulator::sp)); | |
303 state->fp = reinterpret_cast<Address>(simulator_->get_register( | |
304 Simulator::r11)); | |
305 #elif V8_TARGET_ARCH_ARM64 | |
306 if (simulator_->sp() == 0 || simulator_->fp() == 0) { | |
307 // It's possible that the simulator is interrupted while it is updating | |
308 // the sp or fp register. ARM64 simulator does this in two steps: | |
309 // first setting it to zero and then setting it to a new value. | |
310 // Bailout if sp/fp doesn't contain the new value. | |
311 // | |
312 // FIXME: The above doesn't really solve the issue. | |
313 // If a 64-bit target is executed on a 32-bit host even the final | |
314 // write is non-atomic, so it might obtain a half of the result. | |
315 // Moreover as long as the register set code uses memcpy (as of now), | |
316 // it is not guaranteed to be atomic even when both host and target | |
317 // are of same bitness. | |
318 return; | |
319 } | |
320 state->pc = reinterpret_cast<Address>(simulator_->pc()); | |
321 state->sp = reinterpret_cast<Address>(simulator_->sp()); | |
322 state->fp = reinterpret_cast<Address>(simulator_->fp()); | |
323 #elif V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 | |
324 if (!simulator_->has_bad_pc()) { | |
325 state->pc = reinterpret_cast<Address>(simulator_->get_pc()); | |
326 } | |
327 state->sp = reinterpret_cast<Address>(simulator_->get_register( | |
328 Simulator::sp)); | |
329 state->fp = reinterpret_cast<Address>(simulator_->get_register( | |
330 Simulator::fp)); | |
331 #elif V8_TARGET_ARCH_PPC | |
332 if (!simulator_->has_bad_pc()) { | |
333 state->pc = reinterpret_cast<Address>(simulator_->get_pc()); | |
334 } | |
335 state->sp = | |
336 reinterpret_cast<Address>(simulator_->get_register(Simulator::sp)); | |
337 state->fp = | |
338 reinterpret_cast<Address>(simulator_->get_register(Simulator::fp)); | |
339 #endif | |
340 } | |
341 | |
342 private: | |
343 Simulator* simulator_; | |
344 }; | |
345 #endif // USE_SIMULATOR | |
346 | |
347 | |
348 #if defined(USE_SIGNALS) | |
349 | |
350 class SignalHandler : public AllStatic { | |
351 public: | |
352 static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } | |
353 static void TearDown() { delete mutex_; mutex_ = NULL; } | |
354 | |
355 static void IncreaseSamplerCount() { | |
356 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
357 if (++client_count_ == 1) Install(); | |
358 } | |
359 | |
360 static void DecreaseSamplerCount() { | |
361 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
362 if (--client_count_ == 0) Restore(); | |
363 } | |
364 | |
365 static bool Installed() { | |
366 return signal_handler_installed_; | |
367 } | |
368 | |
369 private: | |
370 static void Install() { | |
371 #if !V8_OS_NACL | |
372 struct sigaction sa; | |
373 sa.sa_sigaction = &HandleProfilerSignal; | |
374 sigemptyset(&sa.sa_mask); | |
375 #if V8_OS_QNX | |
376 sa.sa_flags = SA_SIGINFO; | |
377 #else | |
378 sa.sa_flags = SA_RESTART | SA_SIGINFO; | |
379 #endif | |
380 signal_handler_installed_ = | |
381 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); | |
382 #endif | |
383 } | |
384 | |
385 static void Restore() { | |
386 #if !V8_OS_NACL | |
387 if (signal_handler_installed_) { | |
388 sigaction(SIGPROF, &old_signal_handler_, 0); | |
389 signal_handler_installed_ = false; | |
390 } | |
391 #endif | |
392 } | |
393 | |
394 #if !V8_OS_NACL | |
395 static void HandleProfilerSignal(int signal, siginfo_t* info, void* context); | |
396 #endif | |
397 // Protects the process wide state below. | |
398 static base::Mutex* mutex_; | |
399 static int client_count_; | |
400 static bool signal_handler_installed_; | |
401 static struct sigaction old_signal_handler_; | |
402 }; | |
403 | |
404 | |
405 base::Mutex* SignalHandler::mutex_ = NULL; | |
406 int SignalHandler::client_count_ = 0; | |
407 struct sigaction SignalHandler::old_signal_handler_; | |
408 bool SignalHandler::signal_handler_installed_ = false; | |
409 | |
410 | |
411 // As Native Client does not support signal handling, profiling is disabled. | |
412 #if !V8_OS_NACL | |
413 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, | |
414 void* context) { | |
415 USE(info); | |
416 if (signal != SIGPROF) return; | |
417 Isolate* isolate = Isolate::UnsafeCurrent(); | |
418 if (isolate == NULL || !isolate->IsInUse()) { | |
419 // We require a fully initialized and entered isolate. | |
420 return; | |
421 } | |
422 if (v8::Locker::IsActive() && | |
423 !isolate->thread_manager()->IsLockedByCurrentThread()) { | |
424 return; | |
425 } | |
426 | |
427 Sampler* sampler = isolate->logger()->sampler(); | |
428 if (sampler == NULL) return; | |
429 | |
430 v8::RegisterState state; | |
431 | |
432 #if defined(USE_SIMULATOR) | |
433 SimulatorHelper helper; | |
434 if (!helper.Init(isolate)) return; | |
435 helper.FillRegisters(&state); | |
436 // It possible that the simulator is interrupted while it is updating | |
437 // the sp or fp register. ARM64 simulator does this in two steps: | |
438 // first setting it to zero and then setting it to the new value. | |
439 // Bailout if sp/fp doesn't contain the new value. | |
440 if (state.sp == 0 || state.fp == 0) return; | |
441 #else | |
442 // Extracting the sample from the context is extremely machine dependent. | |
443 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); | |
444 #if !(V8_OS_OPENBSD || (V8_OS_LINUX && V8_HOST_ARCH_PPC)) | |
445 mcontext_t& mcontext = ucontext->uc_mcontext; | |
446 #endif | |
447 #if V8_OS_LINUX | |
448 #if V8_HOST_ARCH_IA32 | |
449 state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]); | |
450 state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]); | |
451 state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]); | |
452 #elif V8_HOST_ARCH_X64 | |
453 state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]); | |
454 state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]); | |
455 state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]); | |
456 #elif V8_HOST_ARCH_ARM | |
457 #if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) | |
458 // Old GLibc ARM versions used a gregs[] array to access the register | |
459 // values from mcontext_t. | |
460 state.pc = reinterpret_cast<Address>(mcontext.gregs[R15]); | |
461 state.sp = reinterpret_cast<Address>(mcontext.gregs[R13]); | |
462 state.fp = reinterpret_cast<Address>(mcontext.gregs[R11]); | |
463 #else | |
464 state.pc = reinterpret_cast<Address>(mcontext.arm_pc); | |
465 state.sp = reinterpret_cast<Address>(mcontext.arm_sp); | |
466 state.fp = reinterpret_cast<Address>(mcontext.arm_fp); | |
467 #endif // V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) | |
468 #elif V8_HOST_ARCH_ARM64 | |
469 state.pc = reinterpret_cast<Address>(mcontext.pc); | |
470 state.sp = reinterpret_cast<Address>(mcontext.sp); | |
471 // FP is an alias for x29. | |
472 state.fp = reinterpret_cast<Address>(mcontext.regs[29]); | |
473 #elif V8_HOST_ARCH_MIPS | |
474 state.pc = reinterpret_cast<Address>(mcontext.pc); | |
475 state.sp = reinterpret_cast<Address>(mcontext.gregs[29]); | |
476 state.fp = reinterpret_cast<Address>(mcontext.gregs[30]); | |
477 #elif V8_HOST_ARCH_MIPS64 | |
478 state.pc = reinterpret_cast<Address>(mcontext.pc); | |
479 state.sp = reinterpret_cast<Address>(mcontext.gregs[29]); | |
480 state.fp = reinterpret_cast<Address>(mcontext.gregs[30]); | |
481 #elif V8_HOST_ARCH_PPC | |
482 state.pc = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->nip); | |
483 state.sp = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->gpr[PT_R1]); | |
484 state.fp = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->gpr[PT_R31]); | |
485 #endif // V8_HOST_ARCH_* | |
486 #elif V8_OS_MACOSX | |
487 #if V8_HOST_ARCH_X64 | |
488 #if __DARWIN_UNIX03 | |
489 state.pc = reinterpret_cast<Address>(mcontext->__ss.__rip); | |
490 state.sp = reinterpret_cast<Address>(mcontext->__ss.__rsp); | |
491 state.fp = reinterpret_cast<Address>(mcontext->__ss.__rbp); | |
492 #else // !__DARWIN_UNIX03 | |
493 state.pc = reinterpret_cast<Address>(mcontext->ss.rip); | |
494 state.sp = reinterpret_cast<Address>(mcontext->ss.rsp); | |
495 state.fp = reinterpret_cast<Address>(mcontext->ss.rbp); | |
496 #endif // __DARWIN_UNIX03 | |
497 #elif V8_HOST_ARCH_IA32 | |
498 #if __DARWIN_UNIX03 | |
499 state.pc = reinterpret_cast<Address>(mcontext->__ss.__eip); | |
500 state.sp = reinterpret_cast<Address>(mcontext->__ss.__esp); | |
501 state.fp = reinterpret_cast<Address>(mcontext->__ss.__ebp); | |
502 #else // !__DARWIN_UNIX03 | |
503 state.pc = reinterpret_cast<Address>(mcontext->ss.eip); | |
504 state.sp = reinterpret_cast<Address>(mcontext->ss.esp); | |
505 state.fp = reinterpret_cast<Address>(mcontext->ss.ebp); | |
506 #endif // __DARWIN_UNIX03 | |
507 #endif // V8_HOST_ARCH_IA32 | |
508 #elif V8_OS_FREEBSD | |
509 #if V8_HOST_ARCH_IA32 | |
510 state.pc = reinterpret_cast<Address>(mcontext.mc_eip); | |
511 state.sp = reinterpret_cast<Address>(mcontext.mc_esp); | |
512 state.fp = reinterpret_cast<Address>(mcontext.mc_ebp); | |
513 #elif V8_HOST_ARCH_X64 | |
514 state.pc = reinterpret_cast<Address>(mcontext.mc_rip); | |
515 state.sp = reinterpret_cast<Address>(mcontext.mc_rsp); | |
516 state.fp = reinterpret_cast<Address>(mcontext.mc_rbp); | |
517 #elif V8_HOST_ARCH_ARM | |
518 state.pc = reinterpret_cast<Address>(mcontext.mc_r15); | |
519 state.sp = reinterpret_cast<Address>(mcontext.mc_r13); | |
520 state.fp = reinterpret_cast<Address>(mcontext.mc_r11); | |
521 #endif // V8_HOST_ARCH_* | |
522 #elif V8_OS_NETBSD | |
523 #if V8_HOST_ARCH_IA32 | |
524 state.pc = reinterpret_cast<Address>(mcontext.__gregs[_REG_EIP]); | |
525 state.sp = reinterpret_cast<Address>(mcontext.__gregs[_REG_ESP]); | |
526 state.fp = reinterpret_cast<Address>(mcontext.__gregs[_REG_EBP]); | |
527 #elif V8_HOST_ARCH_X64 | |
528 state.pc = reinterpret_cast<Address>(mcontext.__gregs[_REG_RIP]); | |
529 state.sp = reinterpret_cast<Address>(mcontext.__gregs[_REG_RSP]); | |
530 state.fp = reinterpret_cast<Address>(mcontext.__gregs[_REG_RBP]); | |
531 #endif // V8_HOST_ARCH_* | |
532 #elif V8_OS_OPENBSD | |
533 #if V8_HOST_ARCH_IA32 | |
534 state.pc = reinterpret_cast<Address>(ucontext->sc_eip); | |
535 state.sp = reinterpret_cast<Address>(ucontext->sc_esp); | |
536 state.fp = reinterpret_cast<Address>(ucontext->sc_ebp); | |
537 #elif V8_HOST_ARCH_X64 | |
538 state.pc = reinterpret_cast<Address>(ucontext->sc_rip); | |
539 state.sp = reinterpret_cast<Address>(ucontext->sc_rsp); | |
540 state.fp = reinterpret_cast<Address>(ucontext->sc_rbp); | |
541 #endif // V8_HOST_ARCH_* | |
542 #elif V8_OS_SOLARIS | |
543 state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_PC]); | |
544 state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_SP]); | |
545 state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_FP]); | |
546 #elif V8_OS_QNX | |
547 #if V8_HOST_ARCH_IA32 | |
548 state.pc = reinterpret_cast<Address>(mcontext.cpu.eip); | |
549 state.sp = reinterpret_cast<Address>(mcontext.cpu.esp); | |
550 state.fp = reinterpret_cast<Address>(mcontext.cpu.ebp); | |
551 #elif V8_HOST_ARCH_ARM | |
552 state.pc = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_PC]); | |
553 state.sp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_SP]); | |
554 state.fp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_FP]); | |
555 #endif // V8_HOST_ARCH_* | |
556 #elif V8_OS_AIX | |
557 state.pc = reinterpret_cast<Address>(mcontext.jmp_context.iar); | |
558 state.sp = reinterpret_cast<Address>(mcontext.jmp_context.gpr[1]); | |
559 state.fp = reinterpret_cast<Address>(mcontext.jmp_context.gpr[31]); | |
560 #endif // V8_OS_AIX | |
561 #endif // USE_SIMULATOR | |
562 sampler->SampleStack(state); | |
563 } | |
564 #endif // V8_OS_NACL | |
565 | |
566 #endif | |
567 | |
568 | |
569 class SamplerThread : public base::Thread { | |
570 public: | |
571 static const int kSamplerThreadStackSize = 64 * KB; | |
572 | |
573 explicit SamplerThread(int interval) | |
574 : Thread(base::Thread::Options("SamplerThread", kSamplerThreadStackSize)), | |
575 interval_(interval) {} | |
576 | |
577 static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } | |
578 static void TearDown() { delete mutex_; mutex_ = NULL; } | |
579 | |
580 static void AddActiveSampler(Sampler* sampler) { | |
581 bool need_to_start = false; | |
582 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
583 if (instance_ == NULL) { | |
584 // Start a thread that will send SIGPROF signal to VM threads, | |
585 // when CPU profiling will be enabled. | |
586 instance_ = new SamplerThread(sampler->interval()); | |
587 need_to_start = true; | |
588 } | |
589 | |
590 DCHECK(sampler->IsActive()); | |
591 DCHECK(!instance_->active_samplers_.Contains(sampler)); | |
592 DCHECK(instance_->interval_ == sampler->interval()); | |
593 instance_->active_samplers_.Add(sampler); | |
594 | |
595 if (need_to_start) instance_->StartSynchronously(); | |
596 } | |
597 | |
598 static void RemoveActiveSampler(Sampler* sampler) { | |
599 SamplerThread* instance_to_remove = NULL; | |
600 { | |
601 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
602 | |
603 DCHECK(sampler->IsActive()); | |
604 bool removed = instance_->active_samplers_.RemoveElement(sampler); | |
605 DCHECK(removed); | |
606 USE(removed); | |
607 | |
608 // We cannot delete the instance immediately as we need to Join() the | |
609 // thread but we are holding mutex_ and the thread may try to acquire it. | |
610 if (instance_->active_samplers_.is_empty()) { | |
611 instance_to_remove = instance_; | |
612 instance_ = NULL; | |
613 } | |
614 } | |
615 | |
616 if (!instance_to_remove) return; | |
617 instance_to_remove->Join(); | |
618 delete instance_to_remove; | |
619 } | |
620 | |
621 // Implement Thread::Run(). | |
622 virtual void Run() { | |
623 while (true) { | |
624 { | |
625 base::LockGuard<base::Mutex> lock_guard(mutex_); | |
626 if (active_samplers_.is_empty()) break; | |
627 // When CPU profiling is enabled both JavaScript and C++ code is | |
628 // profiled. We must not suspend. | |
629 for (int i = 0; i < active_samplers_.length(); ++i) { | |
630 Sampler* sampler = active_samplers_.at(i); | |
631 if (!sampler->IsProfiling()) continue; | |
632 sampler->DoSample(); | |
633 } | |
634 } | |
635 base::OS::Sleep(base::TimeDelta::FromMilliseconds(interval_)); | |
636 } | |
637 } | |
638 | |
639 private: | |
640 // Protects the process wide state below. | |
641 static base::Mutex* mutex_; | |
642 static SamplerThread* instance_; | |
643 | |
644 const int interval_; | |
645 List<Sampler*> active_samplers_; | |
646 | |
647 DISALLOW_COPY_AND_ASSIGN(SamplerThread); | |
648 }; | |
649 | |
650 | |
651 base::Mutex* SamplerThread::mutex_ = NULL; | |
652 SamplerThread* SamplerThread::instance_ = NULL; | |
653 | |
654 | |
655 // | 78 // |
656 // StackTracer implementation | 79 // StackTracer implementation |
657 // | 80 // |
658 DISABLE_ASAN void TickSample::Init(Isolate* isolate, | 81 DISABLE_ASAN void TickSample::Init(Isolate* isolate, |
659 const v8::RegisterState& regs, | 82 const v8::RegisterState& regs, |
660 RecordCEntryFrame record_c_entry_frame, | 83 RecordCEntryFrame record_c_entry_frame, |
661 bool update_stats) { | 84 bool update_stats) { |
662 timestamp = base::TimeTicks::HighResolutionNow(); | 85 timestamp = base::TimeTicks::HighResolutionNow(); |
663 pc = reinterpret_cast<Address>(regs.pc); | 86 pc = reinterpret_cast<Address>(regs.pc); |
664 state = isolate->current_vm_state(); | 87 state = isolate->current_vm_state(); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
722 it.top_frame_type() == StackFrame::EXIT) { | 145 it.top_frame_type() == StackFrame::EXIT) { |
723 frames[i++] = isolate->c_function(); | 146 frames[i++] = isolate->c_function(); |
724 } | 147 } |
725 while (!it.done() && i < frames_limit) { | 148 while (!it.done() && i < frames_limit) { |
726 frames[i++] = it.frame()->pc(); | 149 frames[i++] = it.frame()->pc(); |
727 it.Advance(); | 150 it.Advance(); |
728 } | 151 } |
729 sample_info->frames_count = i; | 152 sample_info->frames_count = i; |
730 } | 153 } |
731 | 154 |
732 | |
733 void Sampler::SetUp() { | |
734 #if defined(USE_SIGNALS) | |
735 SignalHandler::SetUp(); | |
736 #endif | |
737 SamplerThread::SetUp(); | |
738 } | |
739 | |
740 | |
741 void Sampler::TearDown() { | |
742 SamplerThread::TearDown(); | |
743 #if defined(USE_SIGNALS) | |
744 SignalHandler::TearDown(); | |
745 #endif | |
746 } | |
747 | |
748 Sampler::Sampler(Isolate* isolate, int interval) | |
749 : isolate_(isolate), | |
750 interval_(interval), | |
751 profiling_(false), | |
752 has_processing_thread_(false), | |
753 active_(false), | |
754 is_counting_samples_(false), | |
755 js_sample_count_(0), | |
756 external_sample_count_(0) { | |
757 data_ = new PlatformData; | |
758 } | |
759 | |
760 Sampler::~Sampler() { | |
761 DCHECK(!IsActive()); | |
762 delete data_; | |
763 } | |
764 | |
765 void Sampler::Start() { | |
766 DCHECK(!IsActive()); | |
767 SetActive(true); | |
768 SamplerThread::AddActiveSampler(this); | |
769 } | |
770 | |
771 | |
772 void Sampler::Stop() { | |
773 DCHECK(IsActive()); | |
774 SamplerThread::RemoveActiveSampler(this); | |
775 SetActive(false); | |
776 } | |
777 | |
778 | |
779 void Sampler::IncreaseProfilingDepth() { | |
780 base::NoBarrier_AtomicIncrement(&profiling_, 1); | |
781 #if defined(USE_SIGNALS) | |
782 SignalHandler::IncreaseSamplerCount(); | |
783 #endif | |
784 } | |
785 | |
786 | |
787 void Sampler::DecreaseProfilingDepth() { | |
788 #if defined(USE_SIGNALS) | |
789 SignalHandler::DecreaseSamplerCount(); | |
790 #endif | |
791 base::NoBarrier_AtomicIncrement(&profiling_, -1); | |
792 } | |
793 | |
794 | |
795 void Sampler::SampleStack(const v8::RegisterState& state) { | |
796 TickSample* sample = isolate_->cpu_profiler()->StartTickSample(); | |
797 TickSample sample_obj; | |
798 if (sample == NULL) sample = &sample_obj; | |
799 sample->Init(isolate_, state, TickSample::kIncludeCEntryFrame, true); | |
800 if (is_counting_samples_) { | |
801 if (sample->state == JS) ++js_sample_count_; | |
802 if (sample->state == EXTERNAL) ++external_sample_count_; | |
803 } | |
804 Tick(sample); | |
805 if (sample != &sample_obj) { | |
806 isolate_->cpu_profiler()->FinishTickSample(); | |
807 } | |
808 } | |
809 | |
810 | |
811 #if defined(USE_SIGNALS) | |
812 | |
813 void Sampler::DoSample() { | |
814 if (!SignalHandler::Installed()) return; | |
815 pthread_kill(platform_data()->vm_tid(), SIGPROF); | |
816 } | |
817 | |
818 #elif V8_OS_WIN || V8_OS_CYGWIN | |
819 | |
820 void Sampler::DoSample() { | |
821 HANDLE profiled_thread = platform_data()->profiled_thread(); | |
822 if (profiled_thread == NULL) return; | |
823 | |
824 #if defined(USE_SIMULATOR) | |
825 SimulatorHelper helper; | |
826 if (!helper.Init(isolate())) return; | |
827 #endif | |
828 | |
829 const DWORD kSuspendFailed = static_cast<DWORD>(-1); | |
830 if (SuspendThread(profiled_thread) == kSuspendFailed) return; | |
831 | |
832 // Context used for sampling the register state of the profiled thread. | |
833 CONTEXT context; | |
834 memset(&context, 0, sizeof(context)); | |
835 context.ContextFlags = CONTEXT_FULL; | |
836 if (GetThreadContext(profiled_thread, &context) != 0) { | |
837 v8::RegisterState state; | |
838 #if defined(USE_SIMULATOR) | |
839 helper.FillRegisters(&state); | |
840 #else | |
841 #if V8_HOST_ARCH_X64 | |
842 state.pc = reinterpret_cast<Address>(context.Rip); | |
843 state.sp = reinterpret_cast<Address>(context.Rsp); | |
844 state.fp = reinterpret_cast<Address>(context.Rbp); | |
845 #else | |
846 state.pc = reinterpret_cast<Address>(context.Eip); | |
847 state.sp = reinterpret_cast<Address>(context.Esp); | |
848 state.fp = reinterpret_cast<Address>(context.Ebp); | |
849 #endif | |
850 #endif // USE_SIMULATOR | |
851 SampleStack(state); | |
852 } | |
853 ResumeThread(profiled_thread); | |
854 } | |
855 | |
856 #endif // USE_SIGNALS | |
857 | |
858 | |
859 } // namespace internal | 155 } // namespace internal |
860 } // namespace v8 | 156 } // namespace v8 |
OLD | NEW |