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

Side by Side Diff: src/libsampler/v8-sampler.cc

Issue 2000323007: Revert of Create libsampler as V8 sampler library. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/libsampler/v8-sampler.h ('k') | src/log.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "src/libsampler/v8-sampler.h"
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 <algorithm>
46 #include <vector>
47 #include <map>
48
49 #include "src/base/atomic-utils.h"
50 #include "src/base/platform/platform.h"
51 #include "src/libsampler/hashmap.h"
52
53
54 #if V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T)
55
56 // Not all versions of Android's C library provide ucontext_t.
57 // Detect this and provide custom but compatible definitions. Note that these
58 // follow the GLibc naming convention to access register values from
59 // mcontext_t.
60 //
61 // See http://code.google.com/p/android/issues/detail?id=34784
62
63 #if defined(__arm__)
64
65 typedef struct sigcontext mcontext_t;
66
67 typedef struct ucontext {
68 uint32_t uc_flags;
69 struct ucontext* uc_link;
70 stack_t uc_stack;
71 mcontext_t uc_mcontext;
72 // Other fields are not used by V8, don't define them here.
73 } ucontext_t;
74
75 #elif defined(__aarch64__)
76
77 typedef struct sigcontext mcontext_t;
78
79 typedef struct ucontext {
80 uint64_t uc_flags;
81 struct ucontext *uc_link;
82 stack_t uc_stack;
83 mcontext_t uc_mcontext;
84 // Other fields are not used by V8, don't define them here.
85 } ucontext_t;
86
87 #elif defined(__mips__)
88 // MIPS version of sigcontext, for Android bionic.
89 typedef struct {
90 uint32_t regmask;
91 uint32_t status;
92 uint64_t pc;
93 uint64_t gregs[32];
94 uint64_t fpregs[32];
95 uint32_t acx;
96 uint32_t fpc_csr;
97 uint32_t fpc_eir;
98 uint32_t used_math;
99 uint32_t dsp;
100 uint64_t mdhi;
101 uint64_t mdlo;
102 uint32_t hi1;
103 uint32_t lo1;
104 uint32_t hi2;
105 uint32_t lo2;
106 uint32_t hi3;
107 uint32_t lo3;
108 } mcontext_t;
109
110 typedef struct ucontext {
111 uint32_t uc_flags;
112 struct ucontext* uc_link;
113 stack_t uc_stack;
114 mcontext_t uc_mcontext;
115 // Other fields are not used by V8, don't define them here.
116 } ucontext_t;
117
118 #elif defined(__i386__)
119 // x86 version for Android.
120 typedef struct {
121 uint32_t gregs[19];
122 void* fpregs;
123 uint32_t oldmask;
124 uint32_t cr2;
125 } mcontext_t;
126
127 typedef uint32_t kernel_sigset_t[2]; // x86 kernel uses 64-bit signal masks
128 typedef struct ucontext {
129 uint32_t uc_flags;
130 struct ucontext* uc_link;
131 stack_t uc_stack;
132 mcontext_t uc_mcontext;
133 // Other fields are not used by V8, don't define them here.
134 } ucontext_t;
135 enum { REG_EBP = 6, REG_ESP = 7, REG_EIP = 14 };
136
137 #elif defined(__x86_64__)
138 // x64 version for Android.
139 typedef struct {
140 uint64_t gregs[23];
141 void* fpregs;
142 uint64_t __reserved1[8];
143 } mcontext_t;
144
145 typedef struct ucontext {
146 uint64_t uc_flags;
147 struct ucontext *uc_link;
148 stack_t uc_stack;
149 mcontext_t uc_mcontext;
150 // Other fields are not used by V8, don't define them here.
151 } ucontext_t;
152 enum { REG_RBP = 10, REG_RSP = 15, REG_RIP = 16 };
153 #endif
154
155 #endif // V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T)
156
157
158 namespace v8 {
159 namespace sampler {
160
161 namespace {
162
163 #if defined(USE_SIGNALS)
164 typedef std::vector<Sampler*> SamplerList;
165 typedef SamplerList::iterator SamplerListIterator;
166
167 class AtomicGuard {
168 public:
169 explicit AtomicGuard(base::AtomicValue<int>* atomic, bool is_block = true)
170 : atomic_(atomic),
171 is_success_(false) {
172 do {
173 // Use Acquire_Load to gain mutual exclusion.
174 USE(atomic_->Value());
175 is_success_ = atomic_->TrySetValue(0, 1);
176 } while (is_block && !is_success_);
177 }
178
179 bool is_success() { return is_success_; }
180
181 ~AtomicGuard() {
182 if (is_success_) {
183 atomic_->SetValue(0);
184 }
185 atomic_ = NULL;
186 }
187
188 private:
189 base::AtomicValue<int>* atomic_;
190 bool is_success_;
191 };
192
193
194 // Returns key for hash map.
195 void* ThreadKey(pthread_t thread_id) {
196 return reinterpret_cast<void*>(thread_id);
197 }
198
199
200 // Returns hash value for hash map.
201 uint32_t ThreadHash(pthread_t thread_id) {
202 #if V8_OS_MACOSX
203 return static_cast<uint32_t>(reinterpret_cast<intptr_t>(thread_id));
204 #else
205 return static_cast<uint32_t>(thread_id);
206 #endif
207 }
208
209 #endif // USE_SIGNALS
210
211 } // namespace
212
213 #if defined(USE_SIGNALS)
214
215 class Sampler::PlatformData {
216 public:
217 PlatformData() : vm_tid_(pthread_self()) {}
218 pthread_t vm_tid() const { return vm_tid_; }
219
220 private:
221 pthread_t vm_tid_;
222 };
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
296 #elif V8_OS_WIN || V8_OS_CYGWIN
297
298 // ----------------------------------------------------------------------------
299 // Win32 profiler support. On Cygwin we use the same sampler implementation as
300 // on Win32.
301
302 class Sampler::PlatformData {
303 public:
304 // Get a handle to the calling thread. This is the thread that we are
305 // going to profile. We need to make a copy of the handle because we are
306 // going to use it in the sampler thread. Using GetThreadHandle() will
307 // not work in this case. We're using OpenThread because DuplicateHandle
308 // for some reason doesn't work in Chrome's sandbox.
309 PlatformData()
310 : profiled_thread_(OpenThread(THREAD_GET_CONTEXT |
311 THREAD_SUSPEND_RESUME |
312 THREAD_QUERY_INFORMATION,
313 false,
314 GetCurrentThreadId())) {}
315
316 ~PlatformData() {
317 if (profiled_thread_ != NULL) {
318 CloseHandle(profiled_thread_);
319 profiled_thread_ = NULL;
320 }
321 }
322
323 HANDLE profiled_thread() { return profiled_thread_; }
324
325 private:
326 HANDLE profiled_thread_;
327 };
328 #endif // USE_SIGNALS
329
330
331 #if defined(USE_SIGNALS)
332 class SignalHandler {
333 public:
334 static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); }
335 static void TearDown() { delete mutex_; mutex_ = NULL; }
336
337 static void IncreaseSamplerCount() {
338 base::LockGuard<base::Mutex> lock_guard(mutex_);
339 if (++client_count_ == 1) Install();
340 }
341
342 static void DecreaseSamplerCount() {
343 base::LockGuard<base::Mutex> lock_guard(mutex_);
344 if (--client_count_ == 0) Restore();
345 }
346
347 static bool Installed() {
348 return signal_handler_installed_;
349 }
350
351 private:
352 static void Install() {
353 #if !V8_OS_NACL
354 struct sigaction sa;
355 sa.sa_sigaction = &HandleProfilerSignal;
356 sigemptyset(&sa.sa_mask);
357 #if V8_OS_QNX
358 sa.sa_flags = SA_SIGINFO;
359 #else
360 sa.sa_flags = SA_RESTART | SA_SIGINFO;
361 #endif
362 signal_handler_installed_ =
363 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0);
364 #endif // !V8_OS_NACL
365 }
366
367 static void Restore() {
368 #if !V8_OS_NACL
369 if (signal_handler_installed_) {
370 sigaction(SIGPROF, &old_signal_handler_, 0);
371 signal_handler_installed_ = false;
372 }
373 #endif
374 }
375
376 #if !V8_OS_NACL
377 static void FillRegisterState(void* context, RegisterState* regs);
378 static void HandleProfilerSignal(int signal, siginfo_t* info, void* context);
379 #endif
380 // Protects the process wide state below.
381 static base::Mutex* mutex_;
382 static int client_count_;
383 static bool signal_handler_installed_;
384 static struct sigaction old_signal_handler_;
385 };
386
387
388 base::Mutex* SignalHandler::mutex_ = NULL;
389 int SignalHandler::client_count_ = 0;
390 struct sigaction SignalHandler::old_signal_handler_;
391 bool SignalHandler::signal_handler_installed_ = false;
392
393
394 // As Native Client does not support signal handling, profiling is disabled.
395 #if !V8_OS_NACL
396 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info,
397 void* context) {
398 USE(info);
399 if (signal != SIGPROF) return;
400 AtomicGuard atomic_guard(&SamplerManager::samplers_access_counter_, false);
401 if (!atomic_guard.is_success()) return;
402 pthread_t thread_id = pthread_self();
403 HashMap::Entry* entry =
404 SamplerManager::sampler_map_.Pointer()->Lookup(ThreadKey(thread_id),
405 ThreadHash(thread_id));
406 if (entry == NULL) return;
407 SamplerList* samplers = reinterpret_cast<SamplerList*>(entry->value);
408
409 v8::RegisterState state;
410 FillRegisterState(context, &state);
411
412 for (int i = 0; i < samplers->size(); ++i) {
413 Sampler* sampler = (*samplers)[i];
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) {
426 // Extracting the sample from the context is extremely machine dependent.
427 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context);
428 #if !(V8_OS_OPENBSD || (V8_OS_LINUX && (V8_HOST_ARCH_PPC || V8_HOST_ARCH_S390)))
429 mcontext_t& mcontext = ucontext->uc_mcontext;
430 #endif
431 #if V8_OS_LINUX
432 #if V8_HOST_ARCH_IA32
433 state->pc = reinterpret_cast<void*>(mcontext.gregs[REG_EIP]);
434 state->sp = reinterpret_cast<void*>(mcontext.gregs[REG_ESP]);
435 state->fp = reinterpret_cast<void*>(mcontext.gregs[REG_EBP]);
436 #elif V8_HOST_ARCH_X64
437 state->pc = reinterpret_cast<void*>(mcontext.gregs[REG_RIP]);
438 state->sp = reinterpret_cast<void*>(mcontext.gregs[REG_RSP]);
439 state->fp = reinterpret_cast<void*>(mcontext.gregs[REG_RBP]);
440 #elif V8_HOST_ARCH_ARM
441 #if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4)
442 // Old GLibc ARM versions used a gregs[] array to access the register
443 // values from mcontext_t.
444 state->pc = reinterpret_cast<void*>(mcontext.gregs[R15]);
445 state->sp = reinterpret_cast<void*>(mcontext.gregs[R13]);
446 state->fp = reinterpret_cast<void*>(mcontext.gregs[R11]);
447 #else
448 state->pc = reinterpret_cast<void*>(mcontext.arm_pc);
449 state->sp = reinterpret_cast<void*>(mcontext.arm_sp);
450 state->fp = reinterpret_cast<void*>(mcontext.arm_fp);
451 #endif // V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4)
452 #elif V8_HOST_ARCH_ARM64
453 state->pc = reinterpret_cast<void*>(mcontext.pc);
454 state->sp = reinterpret_cast<void*>(mcontext.sp);
455 // FP is an alias for x29.
456 state->fp = reinterpret_cast<void*>(mcontext.regs[29]);
457 #elif V8_HOST_ARCH_MIPS
458 state->pc = reinterpret_cast<void*>(mcontext.pc);
459 state->sp = reinterpret_cast<void*>(mcontext.gregs[29]);
460 state->fp = reinterpret_cast<void*>(mcontext.gregs[30]);
461 #elif V8_HOST_ARCH_MIPS64
462 state->pc = reinterpret_cast<void*>(mcontext.pc);
463 state->sp = reinterpret_cast<void*>(mcontext.gregs[29]);
464 state->fp = reinterpret_cast<void*>(mcontext.gregs[30]);
465 #elif V8_HOST_ARCH_PPC
466 state->pc = reinterpret_cast<void*>(ucontext->uc_mcontext.regs->nip);
467 state->sp =
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]);
471 #elif V8_HOST_ARCH_S390
472 #if V8_TARGET_ARCH_32_BIT
473 // 31-bit target will have bit 0 (MSB) of the PSW set to denote addressing
474 // mode. This bit needs to be masked out to resolve actual address.
475 state->pc =
476 reinterpret_cast<void*>(ucontext->uc_mcontext.psw.addr & 0x7FFFFFFF);
477 #else
478 state->pc = reinterpret_cast<void*>(ucontext->uc_mcontext.psw.addr);
479 #endif // V8_TARGET_ARCH_32_BIT
480 state->sp = reinterpret_cast<void*>(ucontext->uc_mcontext.gregs[15]);
481 state->fp = reinterpret_cast<void*>(ucontext->uc_mcontext.gregs[11]);
482 #endif // V8_HOST_ARCH_*
483 #elif V8_OS_MACOSX
484 #if V8_HOST_ARCH_X64
485 #if __DARWIN_UNIX03
486 state->pc = reinterpret_cast<void*>(mcontext->__ss.__rip);
487 state->sp = reinterpret_cast<void*>(mcontext->__ss.__rsp);
488 state->fp = reinterpret_cast<void*>(mcontext->__ss.__rbp);
489 #else // !__DARWIN_UNIX03
490 state->pc = reinterpret_cast<void*>(mcontext->ss.rip);
491 state->sp = reinterpret_cast<void*>(mcontext->ss.rsp);
492 state->fp = reinterpret_cast<void*>(mcontext->ss.rbp);
493 #endif // __DARWIN_UNIX03
494 #elif V8_HOST_ARCH_IA32
495 #if __DARWIN_UNIX03
496 state->pc = reinterpret_cast<void*>(mcontext->__ss.__eip);
497 state->sp = reinterpret_cast<void*>(mcontext->__ss.__esp);
498 state->fp = reinterpret_cast<void*>(mcontext->__ss.__ebp);
499 #else // !__DARWIN_UNIX03
500 state->pc = reinterpret_cast<void*>(mcontext->ss.eip);
501 state->sp = reinterpret_cast<void*>(mcontext->ss.esp);
502 state->fp = reinterpret_cast<void*>(mcontext->ss.ebp);
503 #endif // __DARWIN_UNIX03
504 #endif // V8_HOST_ARCH_IA32
505 #elif V8_OS_FREEBSD
506 #if V8_HOST_ARCH_IA32
507 state->pc = reinterpret_cast<void*>(mcontext.mc_eip);
508 state->sp = reinterpret_cast<void*>(mcontext.mc_esp);
509 state->fp = reinterpret_cast<void*>(mcontext.mc_ebp);
510 #elif V8_HOST_ARCH_X64
511 state->pc = reinterpret_cast<void*>(mcontext.mc_rip);
512 state->sp = reinterpret_cast<void*>(mcontext.mc_rsp);
513 state->fp = reinterpret_cast<void*>(mcontext.mc_rbp);
514 #elif V8_HOST_ARCH_ARM
515 state->pc = reinterpret_cast<void*>(mcontext.mc_r15);
516 state->sp = reinterpret_cast<void*>(mcontext.mc_r13);
517 state->fp = reinterpret_cast<void*>(mcontext.mc_r11);
518 #endif // V8_HOST_ARCH_*
519 #elif V8_OS_NETBSD
520 #if V8_HOST_ARCH_IA32
521 state->pc = reinterpret_cast<void*>(mcontext.__gregs[_REG_EIP]);
522 state->sp = reinterpret_cast<void*>(mcontext.__gregs[_REG_ESP]);
523 state->fp = reinterpret_cast<void*>(mcontext.__gregs[_REG_EBP]);
524 #elif V8_HOST_ARCH_X64
525 state->pc = reinterpret_cast<void*>(mcontext.__gregs[_REG_RIP]);
526 state->sp = reinterpret_cast<void*>(mcontext.__gregs[_REG_RSP]);
527 state->fp = reinterpret_cast<void*>(mcontext.__gregs[_REG_RBP]);
528 #endif // V8_HOST_ARCH_*
529 #elif V8_OS_OPENBSD
530 #if V8_HOST_ARCH_IA32
531 state->pc = reinterpret_cast<void*>(ucontext->sc_eip);
532 state->sp = reinterpret_cast<void*>(ucontext->sc_esp);
533 state->fp = reinterpret_cast<void*>(ucontext->sc_ebp);
534 #elif V8_HOST_ARCH_X64
535 state->pc = reinterpret_cast<void*>(ucontext->sc_rip);
536 state->sp = reinterpret_cast<void*>(ucontext->sc_rsp);
537 state->fp = reinterpret_cast<void*>(ucontext->sc_rbp);
538 #endif // V8_HOST_ARCH_*
539 #elif V8_OS_SOLARIS
540 state->pc = reinterpret_cast<void*>(mcontext.gregs[REG_PC]);
541 state->sp = reinterpret_cast<void*>(mcontext.gregs[REG_SP]);
542 state->fp = reinterpret_cast<void*>(mcontext.gregs[REG_FP]);
543 #elif V8_OS_QNX
544 #if V8_HOST_ARCH_IA32
545 state->pc = reinterpret_cast<void*>(mcontext.cpu.eip);
546 state->sp = reinterpret_cast<void*>(mcontext.cpu.esp);
547 state->fp = reinterpret_cast<void*>(mcontext.cpu.ebp);
548 #elif V8_HOST_ARCH_ARM
549 state->pc = reinterpret_cast<void*>(mcontext.cpu.gpr[ARM_REG_PC]);
550 state->sp = reinterpret_cast<void*>(mcontext.cpu.gpr[ARM_REG_SP]);
551 state->fp = reinterpret_cast<void*>(mcontext.cpu.gpr[ARM_REG_FP]);
552 #endif // V8_HOST_ARCH_*
553 #elif V8_OS_AIX
554 state->pc = reinterpret_cast<void*>(mcontext.jmp_context.iar);
555 state->sp = reinterpret_cast<void*>(mcontext.jmp_context.gpr[1]);
556 state->fp = reinterpret_cast<void*>(mcontext.jmp_context.gpr[31]);
557 #endif // V8_OS_AIX
558 }
559
560 #endif // !V8_OS_NACL
561
562 #endif // USE_SIGNALS
563
564
565 void Sampler::SetUp() {
566 #if defined(USE_SIGNALS)
567 SignalHandler::SetUp();
568 #endif
569 }
570
571
572 void Sampler::TearDown() {
573 #if defined(USE_SIGNALS)
574 SignalHandler::TearDown();
575 #endif
576 }
577
578 Sampler::Sampler(Isolate* isolate)
579 : is_counting_samples_(false),
580 js_sample_count_(0),
581 external_sample_count_(0),
582 isolate_(isolate),
583 profiling_(false),
584 has_processing_thread_(false),
585 active_(false),
586 registered_(false) {
587 data_ = new PlatformData;
588 }
589
590 Sampler::~Sampler() {
591 DCHECK(!IsActive());
592 #if defined(USE_SIGNALS)
593 if (IsRegistered()) {
594 SamplerManager::RemoveSampler(this);
595 }
596 #endif
597 delete data_;
598 }
599
600 void Sampler::Start() {
601 DCHECK(!IsActive());
602 SetActive(true);
603 #if defined(USE_SIGNALS)
604 SamplerManager::AddSampler(this);
605 #endif
606 }
607
608
609 void Sampler::Stop() {
610 #if defined(USE_SIGNALS)
611 SamplerManager::RemoveSampler(this);
612 #endif
613 DCHECK(IsActive());
614 SetActive(false);
615 SetRegistered(false);
616 }
617
618
619 void Sampler::IncreaseProfilingDepth() {
620 base::NoBarrier_AtomicIncrement(&profiling_, 1);
621 #if defined(USE_SIGNALS)
622 SignalHandler::IncreaseSamplerCount();
623 #endif
624 }
625
626
627 void Sampler::DecreaseProfilingDepth() {
628 #if defined(USE_SIGNALS)
629 SignalHandler::DecreaseSamplerCount();
630 #endif
631 base::NoBarrier_AtomicIncrement(&profiling_, -1);
632 }
633
634
635 #if defined(USE_SIGNALS)
636
637 void Sampler::DoSample() {
638 if (!SignalHandler::Installed()) return;
639 if (!IsActive() && !IsRegistered()) {
640 SamplerManager::AddSampler(this);
641 SetRegistered(true);
642 }
643 pthread_kill(platform_data()->vm_tid(), SIGPROF);
644 }
645
646 #elif V8_OS_WIN || V8_OS_CYGWIN
647
648 void Sampler::DoSample() {
649 HANDLE profiled_thread = platform_data()->profiled_thread();
650 if (profiled_thread == NULL) return;
651
652 const DWORD kSuspendFailed = static_cast<DWORD>(-1);
653 if (SuspendThread(profiled_thread) == kSuspendFailed) return;
654
655 // Context used for sampling the register state of the profiled thread.
656 CONTEXT context;
657 memset(&context, 0, sizeof(context));
658 context.ContextFlags = CONTEXT_FULL;
659 if (GetThreadContext(profiled_thread, &context) != 0) {
660 v8::RegisterState state;
661 #if V8_HOST_ARCH_X64
662 state.pc = reinterpret_cast<void*>(context.Rip);
663 state.sp = reinterpret_cast<void*>(context.Rsp);
664 state.fp = reinterpret_cast<void*>(context.Rbp);
665 #else
666 state.pc = reinterpret_cast<void*>(context.Eip);
667 state.sp = reinterpret_cast<void*>(context.Esp);
668 state.fp = reinterpret_cast<void*>(context.Ebp);
669 #endif
670 SampleStack(state);
671 }
672 ResumeThread(profiled_thread);
673 }
674
675 #endif // USE_SIGNALS
676
677 } // namespace sampler
678 } // namespace v8
OLDNEW
« no previous file with comments | « src/libsampler/v8-sampler.h ('k') | src/log.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698