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

Side by Side Diff: src/profiler/sampler.cc

Issue 1858143003: Get rid of UnsafeCurrent in Sampler (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Update check-static-initializers.sh Created 4 years, 8 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
OLDNEW
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 7 #if V8_OS_POSIX && !V8_OS_CYGWIN
8 8
9 #define USE_SIGNALS 9 #define USE_SIGNALS
10 10
(...skipping 24 matching lines...) Expand all
35 !defined(__BIONIC_HAVE_STRUCT_SIGCONTEXT) 35 !defined(__BIONIC_HAVE_STRUCT_SIGCONTEXT)
36 #include <asm/sigcontext.h> // NOLINT 36 #include <asm/sigcontext.h> // NOLINT
37 #endif 37 #endif
38 38
39 #elif V8_OS_WIN || V8_OS_CYGWIN 39 #elif V8_OS_WIN || V8_OS_CYGWIN
40 40
41 #include "src/base/win32-headers.h" 41 #include "src/base/win32-headers.h"
42 42
43 #endif 43 #endif
44 44
45 #include "src/atomic-utils.h"
45 #include "src/base/platform/platform.h" 46 #include "src/base/platform/platform.h"
46 #include "src/flags.h" 47 #include "src/flags.h"
47 #include "src/frames-inl.h" 48 #include "src/frames-inl.h"
48 #include "src/log.h" 49 #include "src/log.h"
49 #include "src/profiler/cpu-profiler-inl.h" 50 #include "src/profiler/cpu-profiler-inl.h"
50 #include "src/simulator.h" 51 #include "src/simulator.h"
51 #include "src/v8threads.h" 52 #include "src/v8threads.h"
52 #include "src/vm-state-inl.h" 53 #include "src/vm-state-inl.h"
53 54
54 55
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 // pessimistically assume it could be the entire pattern match. 230 // pessimistically assume it could be the entire pattern match.
230 MSAN_MEMORY_IS_INITIALIZED(pc, pattern->bytes_count - offset); 231 MSAN_MEMORY_IS_INITIALIZED(pc, pattern->bytes_count - offset);
231 if (!memcmp(pc, pattern->bytes + offset, pattern->bytes_count - offset)) 232 if (!memcmp(pc, pattern->bytes + offset, pattern->bytes_count - offset))
232 return true; 233 return true;
233 } 234 }
234 } 235 }
235 } 236 }
236 return false; 237 return false;
237 } 238 }
238 239
240 typedef List<Sampler*> SamplerList;
241
242 #if defined(USE_SIGNALS)
243 class ThreadSamplersPair {
244 public:
245 ThreadSamplersPair(pthread_t thread_id, SamplerList* samplers)
246 : thread_id_(thread_id), samplers_(samplers) {}
247 ~ThreadSamplersPair() {
248 delete samplers_;
249 }
250 pthread_t thread_id() const { return thread_id_; }
251 SamplerList* samplers() const { return samplers_; }
252 bool operator==(const ThreadSamplersPair& rhs) {
253 return thread_id_ == rhs.thread_id();
254 }
255 private:
256 pthread_t thread_id_;
257 SamplerList* samplers_;
258 ThreadSamplersPair() {}
259 };
260
261 typedef List<ThreadSamplersPair*> ThreadSamplersList;
262
263 template <typename T>
fmeawad 2016/04/06 20:07:56 I do not think you need this class, I think you sh
fmeawad 2016/04/06 20:13:34 lpy@ explained offline that it is a scoped context
264 class AtomicGuard {
265 public:
266 explicit AtomicGuard(AtomicValue<T>* atomic, T expected, T desired,
267 bool is_block)
268 : atomic_(atomic),
269 expected_(expected),
270 desired_(desired),
271 is_success_(false) {
272 do {
273 is_success_ = atomic_->TrySetValue(expected, desired);
274 if (is_success_) break;
275 } while (is_block);
276 }
277
278 bool is_success() { return is_success_; }
279
280 ~AtomicGuard() {
281 atomic_->SetValue(expected_);
282 atomic_ = NULL;
fmeawad 2016/04/06 20:13:34 If you failed to set it, you should not reset it.
lpy 2016/04/06 20:19:58 Done.
283 }
284
285 private:
286 AtomicValue<T>* atomic_;
287 T expected_;
288 T desired_;
289 bool is_success_;
290 };
291 #endif // USE_SIGNALS
292
239 } // namespace 293 } // namespace
240 294
241 #if defined(USE_SIGNALS) 295 #if defined(USE_SIGNALS)
242 296
243 class Sampler::PlatformData : public PlatformDataCommon { 297 class Sampler::PlatformData : public PlatformDataCommon {
244 public: 298 public:
245 PlatformData() : vm_tid_(pthread_self()) {} 299 PlatformData() : vm_tid_(pthread_self()) {}
246 pthread_t vm_tid() const { return vm_tid_; } 300 pthread_t vm_tid() const { return vm_tid_; }
247 301
248 private: 302 private:
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 421
368 static void DecreaseSamplerCount() { 422 static void DecreaseSamplerCount() {
369 base::LockGuard<base::Mutex> lock_guard(mutex_); 423 base::LockGuard<base::Mutex> lock_guard(mutex_);
370 if (--client_count_ == 0) Restore(); 424 if (--client_count_ == 0) Restore();
371 } 425 }
372 426
373 static bool Installed() { 427 static bool Installed() {
374 return signal_handler_installed_; 428 return signal_handler_installed_;
375 } 429 }
376 430
431 #if !V8_OS_NACL
432 static void CollectSample(void* context, Sampler* sampler);
433 #endif
434
377 private: 435 private:
378 static void Install() { 436 static void Install() {
379 #if !V8_OS_NACL 437 #if !V8_OS_NACL
380 struct sigaction sa; 438 struct sigaction sa;
381 sa.sa_sigaction = &HandleProfilerSignal; 439 sa.sa_sigaction = &HandleProfilerSignal;
382 sigemptyset(&sa.sa_mask); 440 sigemptyset(&sa.sa_mask);
383 #if V8_OS_QNX 441 #if V8_OS_QNX
384 sa.sa_flags = SA_SIGINFO; 442 sa.sa_flags = SA_SIGINFO;
385 #else 443 #else
386 sa.sa_flags = SA_RESTART | SA_SIGINFO; 444 sa.sa_flags = SA_RESTART | SA_SIGINFO;
(...skipping 24 matching lines...) Expand all
411 469
412 470
413 base::Mutex* SignalHandler::mutex_ = NULL; 471 base::Mutex* SignalHandler::mutex_ = NULL;
414 int SignalHandler::client_count_ = 0; 472 int SignalHandler::client_count_ = 0;
415 struct sigaction SignalHandler::old_signal_handler_; 473 struct sigaction SignalHandler::old_signal_handler_;
416 bool SignalHandler::signal_handler_installed_ = false; 474 bool SignalHandler::signal_handler_installed_ = false;
417 475
418 476
419 // As Native Client does not support signal handling, profiling is disabled. 477 // As Native Client does not support signal handling, profiling is disabled.
420 #if !V8_OS_NACL 478 #if !V8_OS_NACL
421 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, 479 void SignalHandler::CollectSample(void* context, Sampler* sampler) {
422 void* context) { 480 if (sampler == NULL || (!sampler->IsProfiling() && !sampler->IsRegistered()))
423 USE(info);
424 if (signal != SIGPROF) return;
425 Isolate* isolate = Isolate::UnsafeCurrent();
426 if (isolate == NULL || !isolate->IsInUse()) {
427 // We require a fully initialized and entered isolate.
428 return; 481 return;
429 } 482 Isolate* isolate = sampler->isolate();
483
484 // We require a fully initialized and entered isolate.
485 if (isolate == NULL || !isolate->IsInUse()) return;
486
430 if (v8::Locker::IsActive() && 487 if (v8::Locker::IsActive() &&
431 !isolate->thread_manager()->IsLockedByCurrentThread()) { 488 !isolate->thread_manager()->IsLockedByCurrentThread()) {
432 return; 489 return;
433 } 490 }
434 491
435 Sampler* sampler = isolate->logger()->sampler();
436 if (sampler == NULL) return;
437
438 v8::RegisterState state; 492 v8::RegisterState state;
439 493
440 #if defined(USE_SIMULATOR) 494 #if defined(USE_SIMULATOR)
441 SimulatorHelper helper; 495 SimulatorHelper helper;
442 if (!helper.Init(isolate)) return; 496 if (!helper.Init(isolate)) return;
443 helper.FillRegisters(&state); 497 helper.FillRegisters(&state);
444 // It possible that the simulator is interrupted while it is updating 498 // It possible that the simulator is interrupted while it is updating
445 // the sp or fp register. ARM64 simulator does this in two steps: 499 // the sp or fp register. ARM64 simulator does this in two steps:
446 // first setting it to zero and then setting it to the new value. 500 // first setting it to zero and then setting it to the new value.
447 // Bailout if sp/fp doesn't contain the new value. 501 // Bailout if sp/fp doesn't contain the new value.
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
600 bool need_to_start = false; 654 bool need_to_start = false;
601 base::LockGuard<base::Mutex> lock_guard(mutex_); 655 base::LockGuard<base::Mutex> lock_guard(mutex_);
602 if (instance_ == NULL) { 656 if (instance_ == NULL) {
603 // Start a thread that will send SIGPROF signal to VM threads, 657 // Start a thread that will send SIGPROF signal to VM threads,
604 // when CPU profiling will be enabled. 658 // when CPU profiling will be enabled.
605 instance_ = new SamplerThread(sampler->interval()); 659 instance_ = new SamplerThread(sampler->interval());
606 need_to_start = true; 660 need_to_start = true;
607 } 661 }
608 662
609 DCHECK(sampler->IsActive()); 663 DCHECK(sampler->IsActive());
664 DCHECK(instance_->interval_ == sampler->interval());
665
666 #if defined(USE_SIGNALS)
667 AddSampler(sampler);
668 #else
610 DCHECK(!instance_->active_samplers_.Contains(sampler)); 669 DCHECK(!instance_->active_samplers_.Contains(sampler));
611 DCHECK(instance_->interval_ == sampler->interval());
612 instance_->active_samplers_.Add(sampler); 670 instance_->active_samplers_.Add(sampler);
671 #endif // USE_SIGNALS
613 672
614 if (need_to_start) instance_->StartSynchronously(); 673 if (need_to_start) instance_->StartSynchronously();
615 } 674 }
616 675
617 static void RemoveActiveSampler(Sampler* sampler) { 676 static void RemoveSampler(Sampler* sampler) {
618 SamplerThread* instance_to_remove = NULL; 677 SamplerThread* instance_to_remove = NULL;
619 { 678 {
620 base::LockGuard<base::Mutex> lock_guard(mutex_); 679 base::LockGuard<base::Mutex> lock_guard(mutex_);
621 680
622 DCHECK(sampler->IsActive()); 681 DCHECK(sampler->IsActive() || sampler->IsRegistered());
682 #if defined(USE_SIGNALS)
683 {
684 AtomicGuard<int> atomic_guard(&sampler_list_access_counter_,
685 0, 1, true);
686 // Remove sampler from map.
687 pthread_t thread_id = sampler->platform_data()->vm_tid();
688 SamplerList* samplers = NULL;
689 int i = 0;
690 for (; i < thread_id_to_samplers_.length(); ++i) {
691 ThreadSamplersPair* tsp = thread_id_to_samplers_.at(i);
692 if (pthread_equal(tsp->thread_id(), thread_id) != 0) {
693 samplers = tsp->samplers();
694 break;
695 }
696 }
697 if (samplers != NULL) {
698 samplers->RemoveElement(sampler);
699 if (samplers->is_empty()) {
700 ThreadSamplersPair* tsp = thread_id_to_samplers_.Remove(i);
701 delete tsp;
702 }
703 }
704 if (thread_id_to_samplers_.is_empty()) {
705 instance_to_remove = instance_;
706 instance_ = NULL;
707 }
708 }
709 #else
623 bool removed = instance_->active_samplers_.RemoveElement(sampler); 710 bool removed = instance_->active_samplers_.RemoveElement(sampler);
624 DCHECK(removed); 711 DCHECK(removed);
625 USE(removed); 712 USE(removed);
626 713
627 // We cannot delete the instance immediately as we need to Join() the 714 // We cannot delete the instance immediately as we need to Join() the
628 // thread but we are holding mutex_ and the thread may try to acquire it. 715 // thread but we are holding mutex_ and the thread may try to acquire it.
629 if (instance_->active_samplers_.is_empty()) { 716 if (instance_->active_samplers_.is_empty()) {
630 instance_to_remove = instance_; 717 instance_to_remove = instance_;
631 instance_ = NULL; 718 instance_ = NULL;
632 } 719 }
720 #endif // USE_SIGNALS
633 } 721 }
634 722
635 if (!instance_to_remove) return; 723 if (!instance_to_remove) return;
636 instance_to_remove->Join(); 724 instance_to_remove->Join();
637 delete instance_to_remove; 725 delete instance_to_remove;
638 } 726 }
639 727
728 // Unlike AddActiveSampler, this method only adds a sampler,
729 // but won't start the sampler thread.
730 static void RegisterSampler(Sampler* sampler) {
731 base::LockGuard<base::Mutex> lock_guard(mutex_);
732 #if defined(USE_SIGNALS)
733 AddSampler(sampler);
734 #endif // USE_SIGNALS
735 }
736
640 // Implement Thread::Run(). 737 // Implement Thread::Run().
641 virtual void Run() { 738 virtual void Run() {
642 while (true) { 739 while (true) {
643 { 740 {
644 base::LockGuard<base::Mutex> lock_guard(mutex_); 741 base::LockGuard<base::Mutex> lock_guard(mutex_);
742 #if defined(USE_SIGNALS)
743 if (thread_id_to_samplers_.is_empty()) break;
744 if (SignalHandler::Installed()) {
745 for (int i = 0; i < thread_id_to_samplers_.length(); ++i) {
746 pthread_t thread_id = thread_id_to_samplers_.at(i)->thread_id();
747 pthread_kill(thread_id, SIGPROF);
748 }
749 }
750 #else
645 if (active_samplers_.is_empty()) break; 751 if (active_samplers_.is_empty()) break;
646 // When CPU profiling is enabled both JavaScript and C++ code is 752 // When CPU profiling is enabled both JavaScript and C++ code is
647 // profiled. We must not suspend. 753 // profiled. We must not suspend.
648 for (int i = 0; i < active_samplers_.length(); ++i) { 754 for (int i = 0; i < active_samplers_.length(); ++i) {
649 Sampler* sampler = active_samplers_.at(i); 755 Sampler* sampler = active_samplers_.at(i);
650 if (!sampler->IsProfiling()) continue; 756 if (!sampler->IsProfiling()) continue;
651 sampler->DoSample(); 757 sampler->DoSample();
652 } 758 }
759 #endif // USE_SIGNALS
653 } 760 }
654 base::OS::Sleep(base::TimeDelta::FromMilliseconds(interval_)); 761 base::OS::Sleep(base::TimeDelta::FromMilliseconds(interval_));
655 } 762 }
656 } 763 }
657 764
658 private: 765 private:
659 // Protects the process wide state below. 766 // Protects the process wide state below.
660 static base::Mutex* mutex_; 767 static base::Mutex* mutex_;
661 static SamplerThread* instance_; 768 static SamplerThread* instance_;
662 769
663 const int interval_; 770 const int interval_;
664 List<Sampler*> active_samplers_; 771
772 #if defined(USE_SIGNALS)
773 friend class SignalHandler;
774 static ThreadSamplersList thread_id_to_samplers_;
775 static AtomicValue<int> sampler_list_access_counter_;
776 static void AddSampler(Sampler* sampler) {
777 AtomicGuard<int> atomic_guard(&SamplerThread::sampler_list_access_counter_,
778 0, 1, true);
779 // Add sampler into map if needed.
780 pthread_t thread_id = sampler->platform_data()->vm_tid();
781 SamplerList* samplers = NULL;
782 for (int i = 0; i < thread_id_to_samplers_.length(); ++i) {
783 ThreadSamplersPair* tsp = thread_id_to_samplers_.at(i);
784 if (pthread_equal(tsp->thread_id(), thread_id) != 0) {
785 samplers = tsp->samplers();
786 break;
787 }
788 }
789 if (samplers != NULL) {
790 if (!samplers->Contains(sampler))
791 samplers->Add(sampler);
792 } else {
793 samplers = new SamplerList();
794 samplers->Add(sampler);
795 thread_id_to_samplers_.Add(new ThreadSamplersPair(thread_id, samplers));
796 }
797 }
798 #else
799 SamplerList active_samplers_;
800 #endif // USE_SIGNALS
665 801
666 DISALLOW_COPY_AND_ASSIGN(SamplerThread); 802 DISALLOW_COPY_AND_ASSIGN(SamplerThread);
667 }; 803 };
668 804
669 805
670 base::Mutex* SamplerThread::mutex_ = NULL; 806 base::Mutex* SamplerThread::mutex_ = NULL;
671 SamplerThread* SamplerThread::instance_ = NULL; 807 SamplerThread* SamplerThread::instance_ = NULL;
808 #if defined(USE_SIGNALS)
809 ThreadSamplersList SamplerThread::thread_id_to_samplers_;
810 AtomicValue<int> SamplerThread::sampler_list_access_counter_(0);
811 #endif // USE_SIGNALS
812
813 // As Native Client does not support signal handling, profiling is disabled.
814 #if defined(USE_SIGNALS)
815 #if !V8_OS_NACL
816 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info,
817 void* context) {
818 USE(info);
819 if (signal != SIGPROF) return;
820 AtomicGuard<int> atomic_guard(&SamplerThread::sampler_list_access_counter_,
821 0, 1, false);
822 if (!atomic_guard.is_success()) return;
823 pthread_t thread_id = pthread_self();
824 SamplerList* samplers = NULL;
825 for (int i = 0; i < SamplerThread::thread_id_to_samplers_.length(); ++i) {
826 ThreadSamplersPair* tsp = SamplerThread::thread_id_to_samplers_.at(i);
827 if (pthread_equal(tsp->thread_id(), thread_id) != 0) {
828 samplers = tsp->samplers();
829 break;
830 }
831 }
832 DCHECK(samplers != NULL);
833 for (int i = 0; i < samplers->length(); ++i) {
834 Sampler* sampler = samplers->at(i);
835 SignalHandler::CollectSample(context, sampler);
836 }
837 }
838 #endif // !V8_OS_NACL
839 #endif // USE_SIGNALs
672 840
673 841
674 // 842 //
675 // StackTracer implementation 843 // StackTracer implementation
676 // 844 //
677 DISABLE_ASAN void TickSample::Init(Isolate* isolate, 845 DISABLE_ASAN void TickSample::Init(Isolate* isolate,
678 const v8::RegisterState& regs, 846 const v8::RegisterState& regs,
679 RecordCEntryFrame record_c_entry_frame, 847 RecordCEntryFrame record_c_entry_frame,
680 bool update_stats) { 848 bool update_stats) {
681 timestamp = base::TimeTicks::HighResolutionNow(); 849 timestamp = base::TimeTicks::HighResolutionNow();
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
782 SignalHandler::TearDown(); 950 SignalHandler::TearDown();
783 #endif 951 #endif
784 } 952 }
785 953
786 Sampler::Sampler(Isolate* isolate, int interval) 954 Sampler::Sampler(Isolate* isolate, int interval)
787 : isolate_(isolate), 955 : isolate_(isolate),
788 interval_(interval), 956 interval_(interval),
789 profiling_(false), 957 profiling_(false),
790 has_processing_thread_(false), 958 has_processing_thread_(false),
791 active_(false), 959 active_(false),
960 registered_(false),
792 is_counting_samples_(false), 961 is_counting_samples_(false),
793 js_sample_count_(0), 962 js_sample_count_(0),
794 external_sample_count_(0) { 963 external_sample_count_(0) {
795 data_ = new PlatformData; 964 data_ = new PlatformData;
796 } 965 }
797 966
798 Sampler::~Sampler() { 967 Sampler::~Sampler() {
799 DCHECK(!IsActive()); 968 DCHECK(!IsActive());
969 if (IsRegistered())
970 SamplerThread::RemoveSampler(this);
800 delete data_; 971 delete data_;
801 } 972 }
802 973
803 void Sampler::Start() { 974 void Sampler::Start() {
804 DCHECK(!IsActive()); 975 DCHECK(!IsActive());
805 SetActive(true); 976 SetActive(true);
806 SamplerThread::AddActiveSampler(this); 977 SamplerThread::AddActiveSampler(this);
807 } 978 }
808 979
809 980
810 void Sampler::Stop() { 981 void Sampler::Stop() {
811 DCHECK(IsActive()); 982 DCHECK(IsActive());
812 SamplerThread::RemoveActiveSampler(this); 983 SamplerThread::RemoveSampler(this);
813 SetActive(false); 984 SetActive(false);
985 SetRegistered(false);
814 } 986 }
815 987
816 988
817 void Sampler::IncreaseProfilingDepth() { 989 void Sampler::IncreaseProfilingDepth() {
818 base::NoBarrier_AtomicIncrement(&profiling_, 1); 990 base::NoBarrier_AtomicIncrement(&profiling_, 1);
819 #if defined(USE_SIGNALS) 991 #if defined(USE_SIGNALS)
820 SignalHandler::IncreaseSamplerCount(); 992 SignalHandler::IncreaseSamplerCount();
821 #endif 993 #endif
822 } 994 }
823 995
(...skipping 19 matching lines...) Expand all
843 if (sample != &sample_obj) { 1015 if (sample != &sample_obj) {
844 isolate_->cpu_profiler()->FinishTickSample(); 1016 isolate_->cpu_profiler()->FinishTickSample();
845 } 1017 }
846 } 1018 }
847 1019
848 1020
849 #if defined(USE_SIGNALS) 1021 #if defined(USE_SIGNALS)
850 1022
851 void Sampler::DoSample() { 1023 void Sampler::DoSample() {
852 if (!SignalHandler::Installed()) return; 1024 if (!SignalHandler::Installed()) return;
1025 if (!IsActive() && !IsRegistered()) {
1026 SamplerThread::RegisterSampler(this);
1027 SetRegistered(true);
1028 }
853 pthread_kill(platform_data()->vm_tid(), SIGPROF); 1029 pthread_kill(platform_data()->vm_tid(), SIGPROF);
854 } 1030 }
855 1031
856 #elif V8_OS_WIN || V8_OS_CYGWIN 1032 #elif V8_OS_WIN || V8_OS_CYGWIN
857 1033
858 void Sampler::DoSample() { 1034 void Sampler::DoSample() {
859 HANDLE profiled_thread = platform_data()->profiled_thread(); 1035 HANDLE profiled_thread = platform_data()->profiled_thread();
860 if (profiled_thread == NULL) return; 1036 if (profiled_thread == NULL) return;
861 1037
862 #if defined(USE_SIMULATOR) 1038 #if defined(USE_SIMULATOR)
(...skipping 26 matching lines...) Expand all
889 SampleStack(state); 1065 SampleStack(state);
890 } 1066 }
891 ResumeThread(profiled_thread); 1067 ResumeThread(profiled_thread);
892 } 1068 }
893 1069
894 #endif // USE_SIGNALS 1070 #endif // USE_SIGNALS
895 1071
896 1072
897 } // namespace internal 1073 } // namespace internal
898 } // namespace v8 1074 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698