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

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

Issue 1708573003: [WIP]Create a V8 sampler library and tracing controller. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 10 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/v8.cc ('k') | src/v8-tracing-controller.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "include/v8.h"
6 #include "include/v8-sampler.h"
7 #include "src/base/platform/time.h"
8 #include <iostream>
6 9
7 #if V8_OS_POSIX && !V8_OS_CYGWIN 10 #if V8_OS_POSIX && !V8_OS_CYGWIN
8 11
9 #define USE_SIGNALS
10
11 #include <errno.h> 12 #include <errno.h>
12 #include <pthread.h> 13 #include <pthread.h>
13 #include <signal.h> 14 #include <signal.h>
14 #include <sys/time.h> 15 #include <sys/time.h>
15 16
16 #if !V8_OS_QNX && !V8_OS_NACL && !V8_OS_AIX 17 #if !V8_OS_QNX && !V8_OS_NACL && !V8_OS_AIX
17 #include <sys/syscall.h> // NOLINT 18 #include <sys/syscall.h> // NOLINT
18 #endif 19 #endif
19 20
20 #if V8_OS_MACOSX 21 #if V8_OS_MACOSX
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 struct ucontext *uc_link; 149 struct ucontext *uc_link;
149 stack_t uc_stack; 150 stack_t uc_stack;
150 mcontext_t uc_mcontext; 151 mcontext_t uc_mcontext;
151 // Other fields are not used by V8, don't define them here. 152 // Other fields are not used by V8, don't define them here.
152 } ucontext_t; 153 } ucontext_t;
153 enum { REG_RBP = 10, REG_RSP = 15, REG_RIP = 16 }; 154 enum { REG_RBP = 10, REG_RSP = 15, REG_RIP = 16 };
154 #endif 155 #endif
155 156
156 #endif // V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) 157 #endif // V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T)
157 158
158
159 namespace v8 { 159 namespace v8 {
160 namespace internal {
161 160
162 namespace { 161 namespace {
163 162
164 class PlatformDataCommon : public Malloced { 163 class PlatformDataCommon : public i::Malloced {
165 public: 164 public:
166 PlatformDataCommon() : profiled_thread_id_(ThreadId::Current()) {} 165 PlatformDataCommon() : profiled_thread_id_(i::ThreadId::Current()) {}
167 ThreadId profiled_thread_id() { return profiled_thread_id_; } 166 i::ThreadId profiled_thread_id() { return profiled_thread_id_; }
168 167
169 protected: 168 protected:
170 ~PlatformDataCommon() {} 169 ~PlatformDataCommon() {}
171 170
172 private: 171 private:
173 ThreadId profiled_thread_id_; 172 i::ThreadId profiled_thread_id_;
174 }; 173 };
175 174
176
177 bool IsSamePage(byte* ptr1, byte* ptr2) {
178 const uint32_t kPageSize = 4096;
179 uintptr_t mask = ~static_cast<uintptr_t>(kPageSize - 1);
180 return (reinterpret_cast<uintptr_t>(ptr1) & mask) ==
181 (reinterpret_cast<uintptr_t>(ptr2) & mask);
182 }
183
184
185 // Check if the code at specified address could potentially be a
186 // frame setup code.
187 bool IsNoFrameRegion(Address address) {
188 struct Pattern {
189 int bytes_count;
190 byte bytes[8];
191 int offsets[4];
192 };
193 byte* pc = reinterpret_cast<byte*>(address);
194 static Pattern patterns[] = {
195 #if V8_HOST_ARCH_IA32
196 // push %ebp
197 // mov %esp,%ebp
198 {3, {0x55, 0x89, 0xe5}, {0, 1, -1}},
199 // pop %ebp
200 // ret N
201 {2, {0x5d, 0xc2}, {0, 1, -1}},
202 // pop %ebp
203 // ret
204 {2, {0x5d, 0xc3}, {0, 1, -1}},
205 #elif V8_HOST_ARCH_X64
206 // pushq %rbp
207 // movq %rsp,%rbp
208 {4, {0x55, 0x48, 0x89, 0xe5}, {0, 1, -1}},
209 // popq %rbp
210 // ret N
211 {2, {0x5d, 0xc2}, {0, 1, -1}},
212 // popq %rbp
213 // ret
214 {2, {0x5d, 0xc3}, {0, 1, -1}},
215 #endif
216 {0, {}, {}}
217 };
218 for (Pattern* pattern = patterns; pattern->bytes_count; ++pattern) {
219 for (int* offset_ptr = pattern->offsets; *offset_ptr != -1; ++offset_ptr) {
220 int offset = *offset_ptr;
221 if (!offset || IsSamePage(pc, pc - offset)) {
222 MSAN_MEMORY_IS_INITIALIZED(pc - offset, pattern->bytes_count);
223 if (!memcmp(pc - offset, pattern->bytes, pattern->bytes_count))
224 return true;
225 } else {
226 // It is not safe to examine bytes on another page as it might not be
227 // allocated thus causing a SEGFAULT.
228 // Check the pattern part that's on the same page and
229 // pessimistically assume it could be the entire pattern match.
230 MSAN_MEMORY_IS_INITIALIZED(pc, pattern->bytes_count - offset);
231 if (!memcmp(pc, pattern->bytes + offset, pattern->bytes_count - offset))
232 return true;
233 }
234 }
235 }
236 return false;
237 }
238
239 } // namespace 175 } // namespace
240 176
241 #if defined(USE_SIGNALS) 177 #if defined(USE_SIGNALS)
242 178
243 class Sampler::PlatformData : public PlatformDataCommon { 179 class V8Sampler::PlatformData : public PlatformDataCommon {
244 public: 180 public:
245 PlatformData() : vm_tid_(pthread_self()) {} 181 PlatformData() : vm_tid_(pthread_self()) {}
246 pthread_t vm_tid() const { return vm_tid_; } 182 pthread_t vm_tid() const { return vm_tid_; }
247 183
248 private: 184 private:
249 pthread_t vm_tid_; 185 pthread_t vm_tid_;
250 }; 186 };
251 187
252 #elif V8_OS_WIN || V8_OS_CYGWIN 188 #elif V8_OS_WIN || V8_OS_CYGWIN
253 189
254 // ---------------------------------------------------------------------------- 190 // ----------------------------------------------------------------------------
255 // Win32 profiler support. On Cygwin we use the same sampler implementation as 191 // Win32 profiler support. On Cygwin we use the same sampler implementation as
256 // on Win32. 192 // on Win32.
257 193
258 class Sampler::PlatformData : public PlatformDataCommon { 194 class V8Sampler::PlatformData : public PlatformDataCommon {
259 public: 195 public:
260 // Get a handle to the calling thread. This is the thread that we are 196 // 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 197 // 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 198 // going to use it in the sampler thread. Using GetThreadHandle() will
263 // not work in this case. We're using OpenThread because DuplicateHandle 199 // not work in this case. We're using OpenThread because DuplicateHandle
264 // for some reason doesn't work in Chrome's sandbox. 200 // for some reason doesn't work in Chrome's sandbox.
265 PlatformData() 201 PlatformData()
266 : profiled_thread_(OpenThread(THREAD_GET_CONTEXT | 202 : profiled_thread_(OpenThread(THREAD_GET_CONTEXT |
267 THREAD_SUSPEND_RESUME | 203 THREAD_SUSPEND_RESUME |
268 THREAD_QUERY_INFORMATION, 204 THREAD_QUERY_INFORMATION,
(...skipping 11 matching lines...) Expand all
280 216
281 private: 217 private:
282 HANDLE profiled_thread_; 218 HANDLE profiled_thread_;
283 }; 219 };
284 #endif 220 #endif
285 221
286 222
287 #if defined(USE_SIMULATOR) 223 #if defined(USE_SIMULATOR)
288 class SimulatorHelper { 224 class SimulatorHelper {
289 public: 225 public:
290 inline bool Init(Isolate* isolate) { 226 inline bool Init(i::Isolate* isolate) {
291 simulator_ = isolate->thread_local_top()->simulator_; 227 simulator_ = isolate->thread_local_top()->simulator_;
292 // Check if there is active simulator. 228 // Check if there is active simulator.
293 return simulator_ != NULL; 229 return simulator_ != NULL;
294 } 230 }
295 231
296 inline void FillRegisters(v8::RegisterState* state) { 232 inline void FillRegisters(RegisterState* state) {
297 #if V8_TARGET_ARCH_ARM 233 #if V8_TARGET_ARCH_ARM
298 if (!simulator_->has_bad_pc()) { 234 if (!simulator_->has_bad_pc()) {
299 state->pc = reinterpret_cast<Address>(simulator_->get_pc()); 235 state->pc = reinterpret_cast<i::Address>(simulator_->get_pc());
300 } 236 }
301 state->sp = reinterpret_cast<Address>(simulator_->get_register( 237 state->sp = reinterpret_cast<i::Address>(simulator_->get_register(
302 Simulator::sp)); 238 i::Simulator::sp));
303 state->fp = reinterpret_cast<Address>(simulator_->get_register( 239 state->fp = reinterpret_cast<i::Address>(simulator_->get_register(
304 Simulator::r11)); 240 i::Simulator::r11));
305 #elif V8_TARGET_ARCH_ARM64 241 #elif V8_TARGET_ARCH_ARM64
306 if (simulator_->sp() == 0 || simulator_->fp() == 0) { 242 if (simulator_->sp() == 0 || simulator_->fp() == 0) {
307 // It's possible that the simulator is interrupted while it is updating 243 // 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: 244 // 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. 245 // first setting it to zero and then setting it to a new value.
310 // Bailout if sp/fp doesn't contain the new value. 246 // Bailout if sp/fp doesn't contain the new value.
311 // 247 //
312 // FIXME: The above doesn't really solve the issue. 248 // 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 249 // 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. 250 // 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), 251 // 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 252 // it is not guaranteed to be atomic even when both host and target
317 // are of same bitness. 253 // are of same bitness.
318 return; 254 return;
319 } 255 }
320 state->pc = reinterpret_cast<Address>(simulator_->pc()); 256 state->pc = reinterpret_cast<i::Address>(simulator_->pc());
321 state->sp = reinterpret_cast<Address>(simulator_->sp()); 257 state->sp = reinterpret_cast<i::Address>(simulator_->sp());
322 state->fp = reinterpret_cast<Address>(simulator_->fp()); 258 state->fp = reinterpret_cast<i::Address>(simulator_->fp());
323 #elif V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 259 #elif V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64
324 if (!simulator_->has_bad_pc()) { 260 if (!simulator_->has_bad_pc()) {
325 state->pc = reinterpret_cast<Address>(simulator_->get_pc()); 261 state->pc = reinterpret_cast<i::Address>(simulator_->get_pc());
326 } 262 }
327 state->sp = reinterpret_cast<Address>(simulator_->get_register( 263 state->sp = reinterpret_cast<i::Address>(simulator_->get_register(
328 Simulator::sp)); 264 i::Simulator::sp));
329 state->fp = reinterpret_cast<Address>(simulator_->get_register( 265 state->fp = reinterpret_cast<i::Address>(simulator_->get_register(
330 Simulator::fp)); 266 i::Simulator::fp));
331 #elif V8_TARGET_ARCH_PPC 267 #elif V8_TARGET_ARCH_PPC
332 if (!simulator_->has_bad_pc()) { 268 if (!simulator_->has_bad_pc()) {
333 state->pc = reinterpret_cast<Address>(simulator_->get_pc()); 269 state->pc = reinterpret_cast<i::Address>(simulator_->get_pc());
334 } 270 }
335 state->sp = 271 state->sp =
336 reinterpret_cast<Address>(simulator_->get_register(Simulator::sp)); 272 reinterpret_cast<i::Address>(simulator_->get_register(i::Simulator::sp)) ;
337 state->fp = 273 state->fp =
338 reinterpret_cast<Address>(simulator_->get_register(Simulator::fp)); 274 reinterpret_cast<i::Address>(simulator_->get_register(i::Simulator::fp)) ;
339 #endif 275 #endif
340 } 276 }
341 277
342 private: 278 private:
343 Simulator* simulator_; 279 i::Simulator* simulator_;
344 }; 280 };
345 #endif // USE_SIMULATOR 281 #endif // USE_SIMULATOR
346 282
347 283
348 #if defined(USE_SIGNALS) 284 #if defined(USE_SIGNALS)
349 285
350 class SignalHandler : public AllStatic { 286 class SignalHandler {
351 public: 287 public:
352 static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } 288 static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); }
353 static void TearDown() { delete mutex_; mutex_ = NULL; } 289 static void TearDown() { delete mutex_; mutex_ = NULL; }
354 290
355 static void IncreaseSamplerCount() { 291 static void IncreaseSamplerCount() {
356 base::LockGuard<base::Mutex> lock_guard(mutex_); 292 base::LockGuard<base::Mutex> lock_guard(mutex_);
357 if (++client_count_ == 1) Install(); 293 if (++client_count_ == 1) Install();
358 } 294 }
359 295
360 static void DecreaseSamplerCount() { 296 static void DecreaseSamplerCount() {
361 base::LockGuard<base::Mutex> lock_guard(mutex_); 297 base::LockGuard<base::Mutex> lock_guard(mutex_);
362 if (--client_count_ == 0) Restore(); 298 if (--client_count_ == 0) Restore();
363 } 299 }
364 300
365 static bool Installed() { 301 static bool Installed() {
366 return signal_handler_installed_; 302 return signal_handler_installed_;
367 } 303 }
368 304
369 private: 305 private:
370 static void Install() { 306 static void Install() {
371 #if !V8_OS_NACL 307 #if !V8_OS_NACL
372 struct sigaction sa; 308 struct sigaction sa;
373 sa.sa_sigaction = &HandleProfilerSignal; 309 sa.sa_sigaction = &SignalHandler::HandleProfilerSignal;
374 sigemptyset(&sa.sa_mask); 310 sigemptyset(&sa.sa_mask);
375 #if V8_OS_QNX 311 #if V8_OS_QNX
376 sa.sa_flags = SA_SIGINFO; 312 sa.sa_flags = SA_SIGINFO;
377 #else 313 #else
378 sa.sa_flags = SA_RESTART | SA_SIGINFO; 314 sa.sa_flags = SA_RESTART | SA_SIGINFO;
379 #endif 315 #endif
380 signal_handler_installed_ = 316 signal_handler_installed_ =
381 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); 317 (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0);
382 #endif 318 #endif
383 } 319 }
(...skipping 10 matching lines...) Expand all
394 #if !V8_OS_NACL 330 #if !V8_OS_NACL
395 static void HandleProfilerSignal(int signal, siginfo_t* info, void* context); 331 static void HandleProfilerSignal(int signal, siginfo_t* info, void* context);
396 #endif 332 #endif
397 // Protects the process wide state below. 333 // Protects the process wide state below.
398 static base::Mutex* mutex_; 334 static base::Mutex* mutex_;
399 static int client_count_; 335 static int client_count_;
400 static bool signal_handler_installed_; 336 static bool signal_handler_installed_;
401 static struct sigaction old_signal_handler_; 337 static struct sigaction old_signal_handler_;
402 }; 338 };
403 339
404
405 base::Mutex* SignalHandler::mutex_ = NULL; 340 base::Mutex* SignalHandler::mutex_ = NULL;
406 int SignalHandler::client_count_ = 0; 341 int SignalHandler::client_count_ = 0;
407 struct sigaction SignalHandler::old_signal_handler_; 342 struct sigaction SignalHandler::old_signal_handler_;
408 bool SignalHandler::signal_handler_installed_ = false; 343 bool SignalHandler::signal_handler_installed_ = false;
409 344
345 class SamplerThread : public base::Thread {
346 public:
347 static const int kSamplerThreadStackSize = 64 * i::KB;
348
349 explicit SamplerThread()
350 : Thread(base::Thread::Options("V8::SamplerThread",
351 kSamplerThreadStackSize)) {}
352
353 static void SetUp(int interval) {
354 if (!mutex_)
355 mutex_ = new base::Mutex();
356 SamplerThread::interval_ = interval;
357 }
358 static void TearDown() { delete mutex_; mutex_ = NULL; }
359
360 static void SetInterval(int interval) {
361 SamplerThread::interval_ = interval;
362 }
363
364 static void AddActiveSampler(V8Sampler* sampler) {
365 bool need_to_start = false;
366 base::LockGuard<base::Mutex> lock_guard(mutex_);
367 if (instance_ == NULL) {
368 // Start a thread that will send SIGPROF signal to VM threads,
369 // when CPU profiling will be enabled.
370 instance_ = new SamplerThread();
371 need_to_start = true;
372 }
373
374 DCHECK(sampler->IsActive());
375 DCHECK(!instance_->active_samplers_.Contains(sampler));
376 instance_->active_samplers_.Add(sampler);
377
378 if (need_to_start) instance_->StartSynchronously();
379 }
380
381 static void RemoveActiveSampler(V8Sampler* sampler) {
382 SamplerThread* instance_to_remove = NULL;
383 {
384 base::LockGuard<base::Mutex> lock_guard(mutex_);
385
386 DCHECK(sampler->IsActive());
387 bool removed = instance_->active_samplers_.RemoveElement(sampler);
388 DCHECK(removed);
389 USE(removed);
390
391 // We cannot delete the instance immediately as we need to Join() the
392 // thread but we are holding mutex_ and the thread may try to acquire it.
393 if (instance_->active_samplers_.is_empty()) {
394 instance_to_remove = instance_;
395 instance_ = NULL;
396 }
397 }
398
399 if (!instance_to_remove) return;
400 instance_to_remove->Join();
401 delete instance_to_remove;
402 }
403
404 static V8Sampler* FetchActiveSampler(void* isolate) {
405 for (int i = 0; i < instance_->active_samplers_.length(); ++i) {
406 V8Sampler* sampler = instance_->active_samplers_.at(i);
407 if (reinterpret_cast<void*>(sampler->isolate()) == isolate)
408 return sampler;
409 }
410 return NULL;
411 }
412
413 // Implement Thread::Run().
414 void Run() override {
415 while (true) {
416 {
417 base::LockGuard<base::Mutex> lock_guard(mutex_);
418 if (active_samplers_.is_empty()) break;
419 // When CPU profiling is enabled both JavaScript and C++ code is
420 // profiled. We must not suspend.
421 for (int i = 0; i < active_samplers_.length(); ++i) {
422 V8Sampler* sampler = active_samplers_.at(i);
423 if (!sampler->IsProfiling()) continue;
424 sampler->DoSample();
425 }
426 }
427 base::OS::Sleep(
428 base::TimeDelta::FromMilliseconds(SamplerThread::interval_));
429 }
430 }
431
432 private:
433 // Protects the process wide state below.
434 static base::Mutex* mutex_;
435 static SamplerThread* instance_;
436
437 static int interval_;
438 i::List<V8Sampler*> active_samplers_;
439
440 DISALLOW_COPY_AND_ASSIGN(SamplerThread);
441 };
442
443 base::Mutex* SamplerThread::mutex_ = NULL;
444 SamplerThread* SamplerThread::instance_ = NULL;
445 int SamplerThread::interval_ = 0;
446
410 447
411 // As Native Client does not support signal handling, profiling is disabled. 448 // As Native Client does not support signal handling, profiling is disabled.
412 #if !V8_OS_NACL 449 #if !V8_OS_NACL
413 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, 450 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info,
414 void* context) { 451 void* context) {
415 USE(info); 452 USE(info);
416 if (signal != SIGPROF) return; 453 if (signal != SIGPROF) return;
417 Isolate* isolate = Isolate::UnsafeCurrent(); 454 i::Isolate* isolate = i::Isolate::UnsafeCurrent();
418 if (isolate == NULL || !isolate->IsInUse()) { 455 if (isolate == NULL || !isolate->IsInUse()) {
419 // We require a fully initialized and entered isolate. 456 // We require a fully initialized and entered isolate.
420 return; 457 return;
421 } 458 }
422 if (v8::Locker::IsActive() && 459 if (Locker::IsActive() &&
423 !isolate->thread_manager()->IsLockedByCurrentThread()) { 460 !isolate->thread_manager()->IsLockedByCurrentThread()) {
424 return; 461 return;
425 } 462 }
426 463
427 Sampler* sampler = isolate->logger()->sampler(); 464 V8Sampler* sampler = SamplerThread::FetchActiveSampler(
465 reinterpret_cast<void*>(isolate));
428 if (sampler == NULL) return; 466 if (sampler == NULL) return;
429 467
430 v8::RegisterState state; 468 RegisterState state;
431 469
432 #if defined(USE_SIMULATOR) 470 #if defined(USE_SIMULATOR)
433 SimulatorHelper helper; 471 SimulatorHelper helper;
434 if (!helper.Init(isolate)) return; 472 if (!helper.Init(isolate)) return;
435 helper.FillRegisters(&state); 473 helper.FillRegisters(&state);
436 // It possible that the simulator is interrupted while it is updating 474 // It possible that the simulator is interrupted while it is updating
437 // the sp or fp register. ARM64 simulator does this in two steps: 475 // 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. 476 // first setting it to zero and then setting it to the new value.
439 // Bailout if sp/fp doesn't contain the new value. 477 // Bailout if sp/fp doesn't contain the new value.
440 if (state.sp == 0 || state.fp == 0) return; 478 if (state.sp == 0 || state.fp == 0) return;
441 #else 479 #else
442 // Extracting the sample from the context is extremely machine dependent. 480 // Extracting the sample from the context is extremely machine dependent.
443 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); 481 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context);
444 #if !(V8_OS_OPENBSD || (V8_OS_LINUX && V8_HOST_ARCH_PPC)) 482 #if !(V8_OS_OPENBSD || (V8_OS_LINUX && V8_HOST_ARCH_PPC))
445 mcontext_t& mcontext = ucontext->uc_mcontext; 483 mcontext_t& mcontext = ucontext->uc_mcontext;
446 #endif 484 #endif
447 #if V8_OS_LINUX 485 #if V8_OS_LINUX
448 #if V8_HOST_ARCH_IA32 486 #if V8_HOST_ARCH_IA32
449 state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]); 487 state.pc = reinterpret_cast<i::Address>(mcontext.gregs[REG_EIP]);
450 state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]); 488 state.sp = reinterpret_cast<i::Address>(mcontext.gregs[REG_ESP]);
451 state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]); 489 state.fp = reinterpret_cast<i::Address>(mcontext.gregs[REG_EBP]);
452 #elif V8_HOST_ARCH_X64 490 #elif V8_HOST_ARCH_X64
453 state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]); 491 state.pc = reinterpret_cast<i::Address>(mcontext.gregs[REG_RIP]);
454 state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]); 492 state.sp = reinterpret_cast<i::Address>(mcontext.gregs[REG_RSP]);
455 state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]); 493 state.fp = reinterpret_cast<i::Address>(mcontext.gregs[REG_RBP]);
456 #elif V8_HOST_ARCH_ARM 494 #elif V8_HOST_ARCH_ARM
457 #if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) 495 #if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4)
458 // Old GLibc ARM versions used a gregs[] array to access the register 496 // Old GLibc ARM versions used a gregs[] array to access the register
459 // values from mcontext_t. 497 // values from mcontext_t.
460 state.pc = reinterpret_cast<Address>(mcontext.gregs[R15]); 498 state.pc = reinterpret_cast<i::Address>(mcontext.gregs[R15]);
461 state.sp = reinterpret_cast<Address>(mcontext.gregs[R13]); 499 state.sp = reinterpret_cast<i::Address>(mcontext.gregs[R13]);
462 state.fp = reinterpret_cast<Address>(mcontext.gregs[R11]); 500 state.fp = reinterpret_cast<i::Address>(mcontext.gregs[R11]);
463 #else 501 #else
464 state.pc = reinterpret_cast<Address>(mcontext.arm_pc); 502 state.pc = reinterpret_cast<i::Address>(mcontext.arm_pc);
465 state.sp = reinterpret_cast<Address>(mcontext.arm_sp); 503 state.sp = reinterpret_cast<i::Address>(mcontext.arm_sp);
466 state.fp = reinterpret_cast<Address>(mcontext.arm_fp); 504 state.fp = reinterpret_cast<i::Address>(mcontext.arm_fp);
467 #endif // V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) 505 #endif // V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4)
468 #elif V8_HOST_ARCH_ARM64 506 #elif V8_HOST_ARCH_ARM64
469 state.pc = reinterpret_cast<Address>(mcontext.pc); 507 state.pc = reinterpret_cast<i::Address>(mcontext.pc);
470 state.sp = reinterpret_cast<Address>(mcontext.sp); 508 state.sp = reinterpret_cast<i::Address>(mcontext.sp);
471 // FP is an alias for x29. 509 // FP is an alias for x29.
472 state.fp = reinterpret_cast<Address>(mcontext.regs[29]); 510 state.fp = reinterpret_cast<i::Address>(mcontext.regs[29]);
473 #elif V8_HOST_ARCH_MIPS 511 #elif V8_HOST_ARCH_MIPS
474 state.pc = reinterpret_cast<Address>(mcontext.pc); 512 state.pc = reinterpret_cast<i::Address>(mcontext.pc);
475 state.sp = reinterpret_cast<Address>(mcontext.gregs[29]); 513 state.sp = reinterpret_cast<i::Address>(mcontext.gregs[29]);
476 state.fp = reinterpret_cast<Address>(mcontext.gregs[30]); 514 state.fp = reinterpret_cast<i::Address>(mcontext.gregs[30]);
477 #elif V8_HOST_ARCH_MIPS64 515 #elif V8_HOST_ARCH_MIPS64
478 state.pc = reinterpret_cast<Address>(mcontext.pc); 516 state.pc = reinterpret_cast<i::Address>(mcontext.pc);
479 state.sp = reinterpret_cast<Address>(mcontext.gregs[29]); 517 state.sp = reinterpret_cast<i::Address>(mcontext.gregs[29]);
480 state.fp = reinterpret_cast<Address>(mcontext.gregs[30]); 518 state.fp = reinterpret_cast<i::Address>(mcontext.gregs[30]);
481 #elif V8_HOST_ARCH_PPC 519 #elif V8_HOST_ARCH_PPC
482 state.pc = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->nip); 520 state.pc = reinterpret_cast<i::Address>(ucontext->uc_mcontext.regs->nip);
483 state.sp = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->gpr[PT_R1]); 521 state.sp = reinterpret_cast<i::Address>(ucontext->uc_mcontext.regs->gpr[PT_R1] );
484 state.fp = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->gpr[PT_R31]); 522 state.fp = reinterpret_cast<i::Address>(ucontext->uc_mcontext.regs->gpr[PT_R31 ]);
485 #endif // V8_HOST_ARCH_* 523 #endif // V8_HOST_ARCH_*
486 #elif V8_OS_MACOSX 524 #elif V8_OS_MACOSX
487 #if V8_HOST_ARCH_X64 525 #if V8_HOST_ARCH_X64
488 #if __DARWIN_UNIX03 526 #if __DARWIN_UNIX03
489 state.pc = reinterpret_cast<Address>(mcontext->__ss.__rip); 527 state.pc = reinterpret_cast<i::Address>(mcontext->__ss.__rip);
490 state.sp = reinterpret_cast<Address>(mcontext->__ss.__rsp); 528 state.sp = reinterpret_cast<i::Address>(mcontext->__ss.__rsp);
491 state.fp = reinterpret_cast<Address>(mcontext->__ss.__rbp); 529 state.fp = reinterpret_cast<i::Address>(mcontext->__ss.__rbp);
492 #else // !__DARWIN_UNIX03 530 #else // !__DARWIN_UNIX03
493 state.pc = reinterpret_cast<Address>(mcontext->ss.rip); 531 state.pc = reinterpret_cast<i::Address>(mcontext->ss.rip);
494 state.sp = reinterpret_cast<Address>(mcontext->ss.rsp); 532 state.sp = reinterpret_cast<i::Address>(mcontext->ss.rsp);
495 state.fp = reinterpret_cast<Address>(mcontext->ss.rbp); 533 state.fp = reinterpret_cast<i::Address>(mcontext->ss.rbp);
496 #endif // __DARWIN_UNIX03 534 #endif // __DARWIN_UNIX03
497 #elif V8_HOST_ARCH_IA32 535 #elif V8_HOST_ARCH_IA32
498 #if __DARWIN_UNIX03 536 #if __DARWIN_UNIX03
499 state.pc = reinterpret_cast<Address>(mcontext->__ss.__eip); 537 state.pc = reinterpret_cast<i::Address>(mcontext->__ss.__eip);
500 state.sp = reinterpret_cast<Address>(mcontext->__ss.__esp); 538 state.sp = reinterpret_cast<i::Address>(mcontext->__ss.__esp);
501 state.fp = reinterpret_cast<Address>(mcontext->__ss.__ebp); 539 state.fp = reinterpret_cast<i::Address>(mcontext->__ss.__ebp);
502 #else // !__DARWIN_UNIX03 540 #else // !__DARWIN_UNIX03
503 state.pc = reinterpret_cast<Address>(mcontext->ss.eip); 541 state.pc = reinterpret_cast<i::Address>(mcontext->ss.eip);
504 state.sp = reinterpret_cast<Address>(mcontext->ss.esp); 542 state.sp = reinterpret_cast<i::Address>(mcontext->ss.esp);
505 state.fp = reinterpret_cast<Address>(mcontext->ss.ebp); 543 state.fp = reinterpret_cast<i::Address>(mcontext->ss.ebp);
506 #endif // __DARWIN_UNIX03 544 #endif // __DARWIN_UNIX03
507 #endif // V8_HOST_ARCH_IA32 545 #endif // V8_HOST_ARCH_IA32
508 #elif V8_OS_FREEBSD 546 #elif V8_OS_FREEBSD
509 #if V8_HOST_ARCH_IA32 547 #if V8_HOST_ARCH_IA32
510 state.pc = reinterpret_cast<Address>(mcontext.mc_eip); 548 state.pc = reinterpret_cast<i::Address>(mcontext.mc_eip);
511 state.sp = reinterpret_cast<Address>(mcontext.mc_esp); 549 state.sp = reinterpret_cast<i::Address>(mcontext.mc_esp);
512 state.fp = reinterpret_cast<Address>(mcontext.mc_ebp); 550 state.fp = reinterpret_cast<i::Address>(mcontext.mc_ebp);
513 #elif V8_HOST_ARCH_X64 551 #elif V8_HOST_ARCH_X64
514 state.pc = reinterpret_cast<Address>(mcontext.mc_rip); 552 state.pc = reinterpret_cast<i::Address>(mcontext.mc_rip);
515 state.sp = reinterpret_cast<Address>(mcontext.mc_rsp); 553 state.sp = reinterpret_cast<i::Address>(mcontext.mc_rsp);
516 state.fp = reinterpret_cast<Address>(mcontext.mc_rbp); 554 state.fp = reinterpret_cast<i::Address>(mcontext.mc_rbp);
517 #elif V8_HOST_ARCH_ARM 555 #elif V8_HOST_ARCH_ARM
518 state.pc = reinterpret_cast<Address>(mcontext.mc_r15); 556 state.pc = reinterpret_cast<i::Address>(mcontext.mc_r15);
519 state.sp = reinterpret_cast<Address>(mcontext.mc_r13); 557 state.sp = reinterpret_cast<i::Address>(mcontext.mc_r13);
520 state.fp = reinterpret_cast<Address>(mcontext.mc_r11); 558 state.fp = reinterpret_cast<i::Address>(mcontext.mc_r11);
521 #endif // V8_HOST_ARCH_* 559 #endif // V8_HOST_ARCH_*
522 #elif V8_OS_NETBSD 560 #elif V8_OS_NETBSD
523 #if V8_HOST_ARCH_IA32 561 #if V8_HOST_ARCH_IA32
524 state.pc = reinterpret_cast<Address>(mcontext.__gregs[_REG_EIP]); 562 state.pc = reinterpret_cast<i::Address>(mcontext.__gregs[_REG_EIP]);
525 state.sp = reinterpret_cast<Address>(mcontext.__gregs[_REG_ESP]); 563 state.sp = reinterpret_cast<i::Address>(mcontext.__gregs[_REG_ESP]);
526 state.fp = reinterpret_cast<Address>(mcontext.__gregs[_REG_EBP]); 564 state.fp = reinterpret_cast<i::Address>(mcontext.__gregs[_REG_EBP]);
527 #elif V8_HOST_ARCH_X64 565 #elif V8_HOST_ARCH_X64
528 state.pc = reinterpret_cast<Address>(mcontext.__gregs[_REG_RIP]); 566 state.pc = reinterpret_cast<i::Address>(mcontext.__gregs[_REG_RIP]);
529 state.sp = reinterpret_cast<Address>(mcontext.__gregs[_REG_RSP]); 567 state.sp = reinterpret_cast<i::Address>(mcontext.__gregs[_REG_RSP]);
530 state.fp = reinterpret_cast<Address>(mcontext.__gregs[_REG_RBP]); 568 state.fp = reinterpret_cast<i::Address>(mcontext.__gregs[_REG_RBP]);
531 #endif // V8_HOST_ARCH_* 569 #endif // V8_HOST_ARCH_*
532 #elif V8_OS_OPENBSD 570 #elif V8_OS_OPENBSD
533 #if V8_HOST_ARCH_IA32 571 #if V8_HOST_ARCH_IA32
534 state.pc = reinterpret_cast<Address>(ucontext->sc_eip); 572 state.pc = reinterpret_cast<i::Address>(ucontext->sc_eip);
535 state.sp = reinterpret_cast<Address>(ucontext->sc_esp); 573 state.sp = reinterpret_cast<i::Address>(ucontext->sc_esp);
536 state.fp = reinterpret_cast<Address>(ucontext->sc_ebp); 574 state.fp = reinterpret_cast<i::Address>(ucontext->sc_ebp);
537 #elif V8_HOST_ARCH_X64 575 #elif V8_HOST_ARCH_X64
538 state.pc = reinterpret_cast<Address>(ucontext->sc_rip); 576 state.pc = reinterpret_cast<i::Address>(ucontext->sc_rip);
539 state.sp = reinterpret_cast<Address>(ucontext->sc_rsp); 577 state.sp = reinterpret_cast<i::Address>(ucontext->sc_rsp);
540 state.fp = reinterpret_cast<Address>(ucontext->sc_rbp); 578 state.fp = reinterpret_cast<i::Address>(ucontext->sc_rbp);
541 #endif // V8_HOST_ARCH_* 579 #endif // V8_HOST_ARCH_*
542 #elif V8_OS_SOLARIS 580 #elif V8_OS_SOLARIS
543 state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_PC]); 581 state.pc = reinterpret_cast<i::Address>(mcontext.gregs[REG_PC]);
544 state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_SP]); 582 state.sp = reinterpret_cast<i::Address>(mcontext.gregs[REG_SP]);
545 state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_FP]); 583 state.fp = reinterpret_cast<i::Address>(mcontext.gregs[REG_FP]);
546 #elif V8_OS_QNX 584 #elif V8_OS_QNX
547 #if V8_HOST_ARCH_IA32 585 #if V8_HOST_ARCH_IA32
548 state.pc = reinterpret_cast<Address>(mcontext.cpu.eip); 586 state.pc = reinterpret_cast<i::Address>(mcontext.cpu.eip);
549 state.sp = reinterpret_cast<Address>(mcontext.cpu.esp); 587 state.sp = reinterpret_cast<i::Address>(mcontext.cpu.esp);
550 state.fp = reinterpret_cast<Address>(mcontext.cpu.ebp); 588 state.fp = reinterpret_cast<i::Address>(mcontext.cpu.ebp);
551 #elif V8_HOST_ARCH_ARM 589 #elif V8_HOST_ARCH_ARM
552 state.pc = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_PC]); 590 state.pc = reinterpret_cast<i::Address>(mcontext.cpu.gpr[ARM_REG_PC]);
553 state.sp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_SP]); 591 state.sp = reinterpret_cast<i::Address>(mcontext.cpu.gpr[ARM_REG_SP]);
554 state.fp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_FP]); 592 state.fp = reinterpret_cast<i::Address>(mcontext.cpu.gpr[ARM_REG_FP]);
555 #endif // V8_HOST_ARCH_* 593 #endif // V8_HOST_ARCH_*
556 #elif V8_OS_AIX 594 #elif V8_OS_AIX
557 state.pc = reinterpret_cast<Address>(mcontext.jmp_context.iar); 595 state.pc = reinterpret_cast<i::Address>(mcontext.jmp_context.iar);
558 state.sp = reinterpret_cast<Address>(mcontext.jmp_context.gpr[1]); 596 state.sp = reinterpret_cast<i::Address>(mcontext.jmp_context.gpr[1]);
559 state.fp = reinterpret_cast<Address>(mcontext.jmp_context.gpr[31]); 597 state.fp = reinterpret_cast<i::Address>(mcontext.jmp_context.gpr[31]);
560 #endif // V8_OS_AIX 598 #endif // V8_OS_AIX
561 #endif // USE_SIMULATOR 599 #endif // USE_SIMULATOR
562 sampler->SampleStack(state); 600 sampler->SampleStack(state);
563 } 601 }
564 #endif // V8_OS_NACL 602 #endif // V8_OS_NACL
565 603
566 #endif 604 #endif
567 605
568 606
569 class SamplerThread : public base::Thread { 607 void V8Sampler::SetUp() {
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 //
656 // StackTracer implementation
657 //
658 DISABLE_ASAN void TickSample::Init(Isolate* isolate,
659 const v8::RegisterState& regs,
660 RecordCEntryFrame record_c_entry_frame,
661 bool update_stats) {
662 timestamp = base::TimeTicks::HighResolutionNow();
663 pc = reinterpret_cast<Address>(regs.pc);
664 state = isolate->current_vm_state();
665 this->update_stats = update_stats;
666
667 // Avoid collecting traces while doing GC.
668 if (state == GC) return;
669
670 Address js_entry_sp = isolate->js_entry_sp();
671 if (js_entry_sp == 0) return; // Not executing JS now.
672
673 if (pc && IsNoFrameRegion(pc)) {
674 pc = 0;
675 return;
676 }
677
678 ExternalCallbackScope* scope = isolate->external_callback_scope();
679 Address handler = Isolate::handler(isolate->thread_local_top());
680 // If there is a handler on top of the external callback scope then
681 // we have already entrered JavaScript again and the external callback
682 // is not the top function.
683 if (scope && scope->scope_address() < handler) {
684 external_callback = scope->callback();
685 has_external_callback = true;
686 } else {
687 // sp register may point at an arbitrary place in memory, make
688 // sure MSAN doesn't complain about it.
689 MSAN_MEMORY_IS_INITIALIZED(regs.sp, sizeof(Address));
690 // Sample potential return address value for frameless invocation of
691 // stubs (we'll figure out later, if this value makes sense).
692 tos = Memory::Address_at(reinterpret_cast<Address>(regs.sp));
693 has_external_callback = false;
694 }
695
696 SafeStackFrameIterator it(isolate, reinterpret_cast<Address>(regs.fp),
697 reinterpret_cast<Address>(regs.sp), js_entry_sp);
698 top_frame_type = it.top_frame_type();
699
700 SampleInfo info;
701 GetStackSample(isolate, regs, record_c_entry_frame,
702 reinterpret_cast<void**>(&stack[0]), kMaxFramesCount, &info);
703 frames_count = static_cast<unsigned>(info.frames_count);
704 }
705
706
707 void TickSample::GetStackSample(Isolate* isolate, const v8::RegisterState& regs,
708 RecordCEntryFrame record_c_entry_frame,
709 void** frames, size_t frames_limit,
710 v8::SampleInfo* sample_info) {
711 sample_info->frames_count = 0;
712 sample_info->vm_state = isolate->current_vm_state();
713 if (sample_info->vm_state == GC) return;
714
715 Address js_entry_sp = isolate->js_entry_sp();
716 if (js_entry_sp == 0) return; // Not executing JS now.
717
718 SafeStackFrameIterator it(isolate, reinterpret_cast<Address>(regs.fp),
719 reinterpret_cast<Address>(regs.sp), js_entry_sp);
720 size_t i = 0;
721 if (record_c_entry_frame == kIncludeCEntryFrame && !it.done() &&
722 it.top_frame_type() == StackFrame::EXIT) {
723 frames[i++] = isolate->c_function();
724 }
725 while (!it.done() && i < frames_limit) {
726 frames[i++] = it.frame()->pc();
727 it.Advance();
728 }
729 sample_info->frames_count = i;
730 }
731
732
733 void Sampler::SetUp() {
734 #if defined(USE_SIGNALS) 608 #if defined(USE_SIGNALS)
735 SignalHandler::SetUp(); 609 SignalHandler::SetUp();
736 #endif 610 #endif
737 SamplerThread::SetUp(); 611 SamplerThread::SetUp(kSamplingIntervalMs);
738 } 612 }
739 613
740 614 void V8Sampler::TearDown() {
741 void Sampler::TearDown() {
742 SamplerThread::TearDown(); 615 SamplerThread::TearDown();
743 #if defined(USE_SIGNALS) 616 #if defined(USE_SIGNALS)
744 SignalHandler::TearDown(); 617 SignalHandler::TearDown();
745 #endif 618 #endif
746 } 619 }
747 620
748 Sampler::Sampler(Isolate* isolate, int interval) 621 void V8Sampler::SetInterval(int interval) {
622 SamplerThread::SetInterval(interval);
623 }
624
625 void V8Sampler::CollectStackSample(const RegisterState& regs,
626 void** frames, size_t frames_limit,
627 SampleInfo* sample_info) {
628 isolate_->GetStackSample(regs, frames, frames_limit, sample_info);
629 }
630
631 void V8Sampler::SetJitCodeEventHandler(JitCodeEventOptions options,
632 void* data) {
633 JitCodeEventHandler handler =
634 reinterpret_cast<JitCodeEventHandler>(data);
635 isolate_->SetJitCodeEventHandler(options, handler);
636 }
637
638 V8Sampler::V8Sampler(Isolate* isolate)
749 : isolate_(isolate), 639 : isolate_(isolate),
750 interval_(interval),
751 profiling_(false), 640 profiling_(false),
752 has_processing_thread_(false), 641 has_processing_thread_(false),
753 active_(false), 642 active_(false),
754 is_counting_samples_(false), 643 is_counting_samples_(false),
755 js_sample_count_(0), 644 js_sample_count_(0),
756 external_sample_count_(0) { 645 external_sample_count_(0) {
757 data_ = new PlatformData; 646 data_ = new PlatformData;
758 } 647 }
759 648
760 Sampler::~Sampler() { 649 V8Sampler::~V8Sampler() {
761 DCHECK(!IsActive()); 650 DCHECK(!IsActive());
762 delete data_; 651 delete data_;
763 } 652 }
764 653
765 void Sampler::Start() { 654 void V8Sampler::Start() {
766 DCHECK(!IsActive()); 655 DCHECK(!IsActive());
767 SetActive(true); 656 SetActive(true);
768 SamplerThread::AddActiveSampler(this); 657 SamplerThread::AddActiveSampler(this);
769 } 658 }
770 659
771 660 void V8Sampler::Stop() {
772 void Sampler::Stop() {
773 DCHECK(IsActive()); 661 DCHECK(IsActive());
774 SamplerThread::RemoveActiveSampler(this); 662 SamplerThread::RemoveActiveSampler(this);
775 SetActive(false); 663 SetActive(false);
776 } 664 }
777 665
778 666 void V8Sampler::IncreaseProfilingDepth() {
779 void Sampler::IncreaseProfilingDepth() {
780 base::NoBarrier_AtomicIncrement(&profiling_, 1); 667 base::NoBarrier_AtomicIncrement(&profiling_, 1);
781 #if defined(USE_SIGNALS) 668 #if defined(USE_SIGNALS)
782 SignalHandler::IncreaseSamplerCount(); 669 SignalHandler::IncreaseSamplerCount();
783 #endif 670 #endif
784 } 671 }
785 672
786 673 void V8Sampler::DecreaseProfilingDepth() {
787 void Sampler::DecreaseProfilingDepth() {
788 #if defined(USE_SIGNALS) 674 #if defined(USE_SIGNALS)
789 SignalHandler::DecreaseSamplerCount(); 675 SignalHandler::DecreaseSamplerCount();
790 #endif 676 #endif
791 base::NoBarrier_AtomicIncrement(&profiling_, -1); 677 base::NoBarrier_AtomicIncrement(&profiling_, -1);
792 } 678 }
793 679
794 680 void V8Sampler::SampleStack(const RegisterState& state) {
795 void Sampler::SampleStack(const v8::RegisterState& state) { 681 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate_);
796 TickSample* sample = isolate_->cpu_profiler()->StartTickSample(); 682 i::TickSample* sample = i_isolate->cpu_profiler()->StartTickSample();
797 TickSample sample_obj; 683 i::TickSample sample_obj;
798 if (sample == NULL) sample = &sample_obj; 684 if (sample == NULL) sample = &sample_obj;
799 sample->Init(isolate_, state, TickSample::kIncludeCEntryFrame, true); 685 sample->Init(i_isolate, state, i::TickSample::kIncludeCEntryFrame, true);
800 if (is_counting_samples_) { 686 if (is_counting_samples_) {
801 if (sample->state == JS) ++js_sample_count_; 687 if (sample->state == JS) ++js_sample_count_;
802 if (sample->state == EXTERNAL) ++external_sample_count_; 688 if (sample->state == EXTERNAL) ++external_sample_count_;
803 } 689 }
804 Tick(sample); 690 Tick(sample);
805 if (sample != &sample_obj) { 691 if (sample != &sample_obj) {
806 isolate_->cpu_profiler()->FinishTickSample(); 692 i_isolate->cpu_profiler()->FinishTickSample();
807 } 693 }
808 } 694 }
809 695
810
811 #if defined(USE_SIGNALS) 696 #if defined(USE_SIGNALS)
812 697
813 void Sampler::DoSample() { 698 void V8Sampler::DoSample() {
814 if (!SignalHandler::Installed()) return; 699 if (!SignalHandler::Installed()) return;
815 pthread_kill(platform_data()->vm_tid(), SIGPROF); 700 pthread_kill(platform_data()->vm_tid(), SIGPROF);
816 } 701 }
817 702
818 #elif V8_OS_WIN || V8_OS_CYGWIN 703 #elif V8_OS_WIN || V8_OS_CYGWIN
819 704
820 void Sampler::DoSample() { 705 void V8Sampler::DoSample() {
821 HANDLE profiled_thread = platform_data()->profiled_thread(); 706 HANDLE profiled_thread = platform_data()->profiled_thread();
822 if (profiled_thread == NULL) return; 707 if (profiled_thread == NULL) return;
823 708
824 #if defined(USE_SIMULATOR) 709 #if defined(USE_SIMULATOR)
825 SimulatorHelper helper; 710 SimulatorHelper helper;
826 if (!helper.Init(isolate())) return; 711 if (!helper.Init(isolate())) return;
827 #endif 712 #endif
828 713
829 const DWORD kSuspendFailed = static_cast<DWORD>(-1); 714 const DWORD kSuspendFailed = static_cast<DWORD>(-1);
830 if (SuspendThread(profiled_thread) == kSuspendFailed) return; 715 if (SuspendThread(profiled_thread) == kSuspendFailed) return;
831 716
832 // Context used for sampling the register state of the profiled thread. 717 // Context used for sampling the register state of the profiled thread.
833 CONTEXT context; 718 CONTEXT context;
834 memset(&context, 0, sizeof(context)); 719 memset(&context, 0, sizeof(context));
835 context.ContextFlags = CONTEXT_FULL; 720 context.ContextFlags = CONTEXT_FULL;
836 if (GetThreadContext(profiled_thread, &context) != 0) { 721 if (GetThreadContext(profiled_thread, &context) != 0) {
837 v8::RegisterState state; 722 RegisterState state;
838 #if defined(USE_SIMULATOR) 723 #if defined(USE_SIMULATOR)
839 helper.FillRegisters(&state); 724 helper.FillRegisters(&state);
840 #else 725 #else
841 #if V8_HOST_ARCH_X64 726 #if V8_HOST_ARCH_X64
842 state.pc = reinterpret_cast<Address>(context.Rip); 727 state.pc = reinterpret_cast<i::Address>(context.Rip);
843 state.sp = reinterpret_cast<Address>(context.Rsp); 728 state.sp = reinterpret_cast<i::Address>(context.Rsp);
844 state.fp = reinterpret_cast<Address>(context.Rbp); 729 state.fp = reinterpret_cast<i::Address>(context.Rbp);
845 #else 730 #else
846 state.pc = reinterpret_cast<Address>(context.Eip); 731 state.pc = reinterpret_cast<i::Address>(context.Eip);
847 state.sp = reinterpret_cast<Address>(context.Esp); 732 state.sp = reinterpret_cast<i::Address>(context.Esp);
848 state.fp = reinterpret_cast<Address>(context.Ebp); 733 state.fp = reinterpret_cast<i::Address>(context.Ebp);
849 #endif 734 #endif
850 #endif // USE_SIMULATOR 735 #endif // USE_SIMULATOR
851 SampleStack(state); 736 SampleStack(state);
852 } 737 }
853 ResumeThread(profiled_thread); 738 ResumeThread(profiled_thread);
854 } 739 }
855 740
856 #endif // USE_SIGNALS 741 #endif // USE_SIGNALS
857 742
858 743 }
859 } // namespace internal
860 } // namespace v8
OLDNEW
« no previous file with comments | « src/v8.cc ('k') | src/v8-tracing-controller.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698