| OLD | NEW |
| 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 #include <ucontext.h> // walkstack(), getcontext() | 38 #include <ucontext.h> // walkstack(), getcontext() |
| 39 #include <dlfcn.h> // dladdr | 39 #include <dlfcn.h> // dladdr |
| 40 #include <pthread.h> | 40 #include <pthread.h> |
| 41 #include <sched.h> // for sched_yield | 41 #include <sched.h> // for sched_yield |
| 42 #include <semaphore.h> | 42 #include <semaphore.h> |
| 43 #include <time.h> | 43 #include <time.h> |
| 44 #include <sys/time.h> // gettimeofday(), timeradd() | 44 #include <sys/time.h> // gettimeofday(), timeradd() |
| 45 #include <errno.h> | 45 #include <errno.h> |
| 46 #include <ieeefp.h> // finite() | 46 #include <ieeefp.h> // finite() |
| 47 #include <signal.h> // sigemptyset(), etc | 47 #include <signal.h> // sigemptyset(), etc |
| 48 #include <sys/kdi_regs.h> | 48 #include <sys/regset.h> |
| 49 | 49 |
| 50 | 50 |
| 51 #undef MAP_TYPE | 51 #undef MAP_TYPE |
| 52 | 52 |
| 53 #include "v8.h" | 53 #include "v8.h" |
| 54 | 54 |
| 55 #include "platform.h" | 55 #include "platform.h" |
| 56 #include "vm-state-inl.h" | 56 #include "vm-state-inl.h" |
| 57 | 57 |
| 58 | 58 |
| (...skipping 551 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 610 return new SolarisSemaphore(count); | 610 return new SolarisSemaphore(count); |
| 611 } | 611 } |
| 612 | 612 |
| 613 | 613 |
| 614 #ifdef ENABLE_LOGGING_AND_PROFILING | 614 #ifdef ENABLE_LOGGING_AND_PROFILING |
| 615 | 615 |
| 616 static Sampler* active_sampler_ = NULL; | 616 static Sampler* active_sampler_ = NULL; |
| 617 static pthread_t vm_tid_ = 0; | 617 static pthread_t vm_tid_ = 0; |
| 618 | 618 |
| 619 | 619 |
| 620 static pthread_t GetThreadID() { |
| 621 return pthread_self(); |
| 622 } |
| 623 |
| 624 |
| 620 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { | 625 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { |
| 621 USE(info); | 626 USE(info); |
| 622 if (signal != SIGPROF) return; | 627 if (signal != SIGPROF) return; |
| 623 if (active_sampler_ == NULL || !active_sampler_->IsActive()) return; | 628 if (active_sampler_ == NULL || !active_sampler_->IsActive()) return; |
| 624 if (vm_tid_ != pthread_self()) return; | 629 if (vm_tid_ != GetThreadID()) return; |
| 625 | 630 |
| 626 TickSample sample_obj; | 631 TickSample sample_obj; |
| 627 TickSample* sample = CpuProfiler::TickSampleEvent(); | 632 TickSample* sample = CpuProfiler::TickSampleEvent(); |
| 628 if (sample == NULL) sample = &sample_obj; | 633 if (sample == NULL) sample = &sample_obj; |
| 629 | 634 |
| 630 // Extracting the sample from the context is extremely machine dependent. | 635 // Extracting the sample from the context is extremely machine dependent. |
| 631 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); | 636 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); |
| 632 mcontext_t& mcontext = ucontext->uc_mcontext; | 637 mcontext_t& mcontext = ucontext->uc_mcontext; |
| 633 sample->state = Top::current_vm_state(); | 638 sample->state = Top::current_vm_state(); |
| 634 | 639 |
| 635 #if V8_HOST_ARCH_IA32 | 640 sample->pc = reinterpret_cast<Address>(mcontext.gregs[REG_PC]); |
| 636 sample->pc = reinterpret_cast<Address>(mcontext.gregs[KDIREG_EIP]); | 641 sample->sp = reinterpret_cast<Address>(mcontext.gregs[REG_SP]); |
| 637 sample->sp = reinterpret_cast<Address>(mcontext.gregs[KDIREG_ESP]); | 642 sample->fp = reinterpret_cast<Address>(mcontext.gregs[REG_FP]); |
| 638 sample->fp = reinterpret_cast<Address>(mcontext.gregs[KDIREG_EBP]); | 643 |
| 639 #elif V8_HOST_ARCH_X64 | |
| 640 sample->pc = reinterpret_cast<Address>(mcontext.gregs[KDIREG_RIP]); | |
| 641 sample->sp = reinterpret_cast<Address>(mcontext.gregs[KDIREG_RSP]); | |
| 642 sample->fp = reinterpret_cast<Address>(mcontext.gregs[KDIREG_RBP]); | |
| 643 #else | |
| 644 UNIMPLEMENTED(); | |
| 645 #endif | |
| 646 active_sampler_->SampleStack(sample); | 644 active_sampler_->SampleStack(sample); |
| 647 active_sampler_->Tick(sample); | 645 active_sampler_->Tick(sample); |
| 648 } | 646 } |
| 649 | 647 |
| 650 | 648 |
| 651 class Sampler::PlatformData : public Malloced { | 649 class Sampler::PlatformData : public Malloced { |
| 652 public: | 650 public: |
| 653 PlatformData() { | 651 enum SleepInterval { |
| 654 signal_handler_installed_ = false; | 652 FULL_INTERVAL, |
| 653 HALF_INTERVAL |
| 654 }; |
| 655 |
| 656 explicit PlatformData(Sampler* sampler) |
| 657 : sampler_(sampler), |
| 658 signal_handler_installed_(false), |
| 659 vm_tgid_(getpid()), |
| 660 signal_sender_launched_(false) { |
| 655 } | 661 } |
| 656 | 662 |
| 663 void SignalSender() { |
| 664 while (sampler_->IsActive()) { |
| 665 if (rate_limiter_.SuspendIfNecessary()) continue; |
| 666 if (sampler_->IsProfiling() && RuntimeProfiler::IsEnabled()) { |
| 667 SendProfilingSignal(); |
| 668 Sleep(HALF_INTERVAL); |
| 669 RuntimeProfiler::NotifyTick(); |
| 670 Sleep(HALF_INTERVAL); |
| 671 } else { |
| 672 if (sampler_->IsProfiling()) SendProfilingSignal(); |
| 673 if (RuntimeProfiler::IsEnabled()) RuntimeProfiler::NotifyTick(); |
| 674 Sleep(FULL_INTERVAL); |
| 675 } |
| 676 } |
| 677 } |
| 678 |
| 679 void SendProfilingSignal() { |
| 680 if (!signal_handler_installed_) return; |
| 681 pthread_kill(vm_tid_, SIGPROF); |
| 682 } |
| 683 |
| 684 void Sleep(SleepInterval full_or_half) { |
| 685 // Convert ms to us and subtract 100 us to compensate delays |
| 686 // occuring during signal delivery. |
| 687 useconds_t interval = sampler_->interval_ * 1000 - 100; |
| 688 if (full_or_half == HALF_INTERVAL) interval /= 2; |
| 689 int result = usleep(interval); |
| 690 #ifdef DEBUG |
| 691 if (result != 0 && errno != EINTR) { |
| 692 fprintf(stderr, |
| 693 "SignalSender usleep error; interval = %u, errno = %d\n", |
| 694 interval, |
| 695 errno); |
| 696 ASSERT(result == 0 || errno == EINTR); |
| 697 } |
| 698 #endif |
| 699 USE(result); |
| 700 } |
| 701 |
| 702 Sampler* sampler_; |
| 657 bool signal_handler_installed_; | 703 bool signal_handler_installed_; |
| 658 struct sigaction old_signal_handler_; | 704 struct sigaction old_signal_handler_; |
| 659 struct itimerval old_timer_value_; | 705 int vm_tgid_; |
| 706 bool signal_sender_launched_; |
| 707 pthread_t signal_sender_thread_; |
| 708 RuntimeProfilerRateLimiter rate_limiter_; |
| 660 }; | 709 }; |
| 661 | 710 |
| 662 | 711 |
| 712 static void* SenderEntry(void* arg) { |
| 713 Sampler::PlatformData* data = |
| 714 reinterpret_cast<Sampler::PlatformData*>(arg); |
| 715 data->SignalSender(); |
| 716 return 0; |
| 717 } |
| 718 |
| 719 |
| 663 Sampler::Sampler(Isolate* isolate, int interval) | 720 Sampler::Sampler(Isolate* isolate, int interval) |
| 664 : isolate_(isolate), | 721 : isolate_(isolate), |
| 665 interval_(interval), | 722 interval_(interval), |
| 666 profiling_(false), | 723 profiling_(false), |
| 667 active_(false), | 724 active_(false), |
| 668 samples_taken_(0) { | 725 samples_taken_(0) { |
| 669 data_ = new PlatformData(); | 726 data_ = new PlatformData(this); |
| 670 } | 727 } |
| 671 | 728 |
| 672 | 729 |
| 673 Sampler::~Sampler() { | 730 Sampler::~Sampler() { |
| 731 ASSERT(!data_->signal_sender_launched_); |
| 674 delete data_; | 732 delete data_; |
| 675 } | 733 } |
| 676 | 734 |
| 677 | 735 |
| 678 void Sampler::Start() { | 736 void Sampler::Start() { |
| 679 // There can only be one active sampler at the time on POSIX | 737 // There can only be one active sampler at the time on POSIX |
| 680 // platforms. | 738 // platforms. |
| 681 if (active_sampler_ != NULL) return; | 739 ASSERT(!IsActive()); |
| 740 vm_tid_ = GetThreadID(); |
| 682 | 741 |
| 683 // Request profiling signals. | 742 // Request profiling signals. |
| 684 struct sigaction sa; | 743 struct sigaction sa; |
| 685 sa.sa_sigaction = ProfilerSignalHandler; | 744 sa.sa_sigaction = ProfilerSignalHandler; |
| 686 sigemptyset(&sa.sa_mask); | 745 sigemptyset(&sa.sa_mask); |
| 687 sa.sa_flags = SA_SIGINFO; | 746 sa.sa_flags = SA_RESTART | SA_SIGINFO; |
| 688 if (sigaction(SIGPROF, &sa, &data_->old_signal_handler_) != 0) return; | 747 data_->signal_handler_installed_ = |
| 689 data_->signal_handler_installed_ = true; | 748 sigaction(SIGPROF, &sa, &data_->old_signal_handler_) == 0; |
| 690 | 749 |
| 691 // Set the itimer to generate a tick for each interval. | 750 // Start a thread that sends SIGPROF signal to VM thread. |
| 692 itimerval itimer; | 751 // Sending the signal ourselves instead of relying on itimer provides |
| 693 itimer.it_interval.tv_sec = interval_ / 1000; | 752 // much better accuracy. |
| 694 itimer.it_interval.tv_usec = (interval_ % 1000) * 1000; | 753 SetActive(true); |
| 695 itimer.it_value.tv_sec = itimer.it_interval.tv_sec; | 754 if (pthread_create( |
| 696 itimer.it_value.tv_usec = itimer.it_interval.tv_usec; | 755 &data_->signal_sender_thread_, NULL, SenderEntry, data_) == 0) { |
| 697 setitimer(ITIMER_PROF, &itimer, &data_->old_timer_value_); | 756 data_->signal_sender_launched_ = true; |
| 757 } |
| 698 | 758 |
| 699 // Set this sampler as the active sampler. | 759 // Set this sampler as the active sampler. |
| 700 active_sampler_ = this; | 760 active_sampler_ = this; |
| 701 active_ = true; | |
| 702 } | 761 } |
| 703 | 762 |
| 704 | 763 |
| 705 void Sampler::Stop() { | 764 void Sampler::Stop() { |
| 765 SetActive(false); |
| 766 |
| 767 // Wait for signal sender termination (it will exit after setting |
| 768 // active_ to false). |
| 769 if (data_->signal_sender_launched_) { |
| 770 Top::WakeUpRuntimeProfilerThreadBeforeShutdown(); |
| 771 pthread_join(data_->signal_sender_thread_, NULL); |
| 772 data_->signal_sender_launched_ = false; |
| 773 } |
| 774 |
| 706 // Restore old signal handler | 775 // Restore old signal handler |
| 707 if (data_->signal_handler_installed_) { | 776 if (data_->signal_handler_installed_) { |
| 708 setitimer(ITIMER_PROF, &data_->old_timer_value_, NULL); | |
| 709 sigaction(SIGPROF, &data_->old_signal_handler_, 0); | 777 sigaction(SIGPROF, &data_->old_signal_handler_, 0); |
| 710 data_->signal_handler_installed_ = false; | 778 data_->signal_handler_installed_ = false; |
| 711 } | 779 } |
| 712 | 780 |
| 713 // This sampler is no longer the active sampler. | 781 // This sampler is no longer the active sampler. |
| 714 active_sampler_ = NULL; | 782 active_sampler_ = NULL; |
| 715 active_ = false; | |
| 716 } | 783 } |
| 717 | 784 |
| 718 #endif // ENABLE_LOGGING_AND_PROFILING | 785 #endif // ENABLE_LOGGING_AND_PROFILING |
| 719 | 786 |
| 720 } } // namespace v8::internal | 787 } } // namespace v8::internal |
| OLD | NEW |