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

Side by Side Diff: base/profiler/stack_sampling_profiler_unittest.cc

Issue 2554123002: Support parallel captures from the StackSamplingProfiler. (Closed)
Patch Set: switch to separate thread-state variable Created 3 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium 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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <cstdlib> 8 #include <cstdlib>
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 // asynchronous library loading has completed before walking the stack. If 437 // asynchronous library loading has completed before walking the stack. If
438 // false, the unloading may still be occurring during the stack walk. 438 // false, the unloading may still be occurring during the stack walk.
439 void TestLibraryUnload(bool wait_until_unloaded) { 439 void TestLibraryUnload(bool wait_until_unloaded) {
440 // Test delegate that supports intervening between the copying of the stack 440 // Test delegate that supports intervening between the copying of the stack
441 // and the walking of the stack. 441 // and the walking of the stack.
442 class StackCopiedSignaler : public NativeStackSamplerTestDelegate { 442 class StackCopiedSignaler : public NativeStackSamplerTestDelegate {
443 public: 443 public:
444 StackCopiedSignaler(WaitableEvent* stack_copied, 444 StackCopiedSignaler(WaitableEvent* stack_copied,
445 WaitableEvent* start_stack_walk, 445 WaitableEvent* start_stack_walk,
446 bool wait_to_walk_stack) 446 bool wait_to_walk_stack)
447 : stack_copied_(stack_copied), start_stack_walk_(start_stack_walk), 447 : stack_copied_(stack_copied),
448 wait_to_walk_stack_(wait_to_walk_stack) { 448 start_stack_walk_(start_stack_walk),
449 } 449 wait_to_walk_stack_(wait_to_walk_stack) {}
450 450
451 void OnPreStackWalk() override { 451 void OnPreStackWalk() override {
452 stack_copied_->Signal(); 452 stack_copied_->Signal();
453 if (wait_to_walk_stack_) 453 if (wait_to_walk_stack_)
454 start_stack_walk_->Wait(); 454 start_stack_walk_->Wait();
455 } 455 }
456 456
457 private: 457 private:
458 WaitableEvent* const stack_copied_; 458 WaitableEvent* const stack_copied_;
459 WaitableEvent* const start_stack_walk_; 459 WaitableEvent* const start_stack_walk_;
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 reinterpret_cast<const void*>(&TargetThread::CallWithAlloca)) 713 reinterpret_cast<const void*>(&TargetThread::CallWithAlloca))
714 << " was not found in stack:\n" 714 << " was not found in stack:\n"
715 << FormatSampleForDiagnosticOutput(sample, profile.modules); 715 << FormatSampleForDiagnosticOutput(sample, profile.modules);
716 716
717 // These frames should be adjacent on the stack. 717 // These frames should be adjacent on the stack.
718 EXPECT_EQ(1, alloca_frame - end_frame) 718 EXPECT_EQ(1, alloca_frame - end_frame)
719 << "Stack:\n" 719 << "Stack:\n"
720 << FormatSampleForDiagnosticOutput(sample, profile.modules); 720 << FormatSampleForDiagnosticOutput(sample, profile.modules);
721 } 721 }
722 722
723 // Checks that the fire-and-forget interface works.
724 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED)
725 #define MAYBE_StartAndRunAsync StartAndRunAsync
726 #else
727 #define MAYBE_StartAndRunAsync DISABLED_StartAndRunAsync
728 #endif
729 TEST(StackSamplingProfilerTest, MAYBE_StartAndRunAsync) {
730 // StartAndRunAsync requires the caller to have a message loop.
731 MessageLoop message_loop;
732
733 SamplingParams params;
734 params.samples_per_burst = 1;
735
736 CallStackProfiles profiles;
737 WithTargetThread([&params, &profiles](PlatformThreadId target_thread_id) {
738 WaitableEvent sampling_thread_completed(
739 WaitableEvent::ResetPolicy::AUTOMATIC,
740 WaitableEvent::InitialState::NOT_SIGNALED);
741 const StackSamplingProfiler::CompletedCallback callback =
742 Bind(&SaveProfilesAndSignalEvent, Unretained(&profiles),
743 Unretained(&sampling_thread_completed));
744 StackSamplingProfiler::StartAndRunAsync(target_thread_id, params, callback);
745 RunLoop().RunUntilIdle();
746 sampling_thread_completed.Wait();
747 });
748
749 ASSERT_EQ(1u, profiles.size());
750 }
751
752 // Checks that the expected number of profiles and samples are present in the 723 // Checks that the expected number of profiles and samples are present in the
753 // call stack profiles produced. 724 // call stack profiles produced.
754 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) 725 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED)
755 #define MAYBE_MultipleProfilesAndSamples MultipleProfilesAndSamples 726 #define MAYBE_MultipleProfilesAndSamples MultipleProfilesAndSamples
756 #else 727 #else
757 #define MAYBE_MultipleProfilesAndSamples DISABLED_MultipleProfilesAndSamples 728 #define MAYBE_MultipleProfilesAndSamples DISABLED_MultipleProfilesAndSamples
758 #endif 729 #endif
759 TEST(StackSamplingProfilerTest, MAYBE_MultipleProfilesAndSamples) { 730 TEST(StackSamplingProfilerTest, MAYBE_MultipleProfilesAndSamples) {
760 SamplingParams params; 731 SamplingParams params;
761 params.burst_interval = params.sampling_interval = 732 params.burst_interval = params.sampling_interval =
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
853 }); 824 });
854 } 825 }
855 826
856 // Checks that the same profiler may be run multiple times. 827 // Checks that the same profiler may be run multiple times.
857 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) 828 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED)
858 #define MAYBE_CanRunMultipleTimes CanRunMultipleTimes 829 #define MAYBE_CanRunMultipleTimes CanRunMultipleTimes
859 #else 830 #else
860 #define MAYBE_CanRunMultipleTimes DISABLED_CanRunMultipleTimes 831 #define MAYBE_CanRunMultipleTimes DISABLED_CanRunMultipleTimes
861 #endif 832 #endif
862 TEST(StackSamplingProfilerTest, MAYBE_CanRunMultipleTimes) { 833 TEST(StackSamplingProfilerTest, MAYBE_CanRunMultipleTimes) {
834 StackSamplingProfiler::TestAPI::SetSamplingThreadIdleShutdownTime(0);
835
836 WithTargetThread([](PlatformThreadId target_thread_id) {
837 SamplingParams params;
838 params.sampling_interval = TimeDelta::FromMilliseconds(0);
839 params.samples_per_burst = 1;
840
841 CallStackProfiles profiles;
842 WaitableEvent sampling_completed(WaitableEvent::ResetPolicy::MANUAL,
843 WaitableEvent::InitialState::NOT_SIGNALED);
844 const StackSamplingProfiler::CompletedCallback callback =
845 Bind(&SaveProfilesAndSignalEvent, Unretained(&profiles),
846 Unretained(&sampling_completed));
847 StackSamplingProfiler profiler(target_thread_id, params, callback);
848
849 // Just start and stop to execute code paths.
850 profiler.Start();
851 profiler.Stop();
852 sampling_completed.Wait();
853
854 // Ensure a second request will run and not block.
855 sampling_completed.Reset();
856 profiles.clear();
857 profiler.Start();
858 sampling_completed.TimedWait(AVeryLongTimeDelta());
859 profiler.Stop();
860 sampling_completed.Wait();
861 ASSERT_EQ(1u, profiles.size());
862 });
863 }
864
865 // Checks that the different profilers may be run.
866 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED)
867 #define MAYBE_CanRunMultipleProfilers CanRunMultipleProfilers
868 #else
869 #define MAYBE_CanRunMultipleProfilers DISABLED_CanRunMultipleProfilers
870 #endif
871 TEST(StackSamplingProfilerTest, MAYBE_CanRunMultipleProfilers) {
872 StackSamplingProfiler::TestAPI::SetSamplingThreadIdleShutdownTime(0);
873
863 SamplingParams params; 874 SamplingParams params;
864 params.sampling_interval = TimeDelta::FromMilliseconds(0); 875 params.sampling_interval = TimeDelta::FromMilliseconds(0);
865 params.samples_per_burst = 1; 876 params.samples_per_burst = 1;
866 877
867 std::vector<CallStackProfile> profiles; 878 std::vector<CallStackProfile> profiles;
868 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles); 879 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles);
869 ASSERT_EQ(1u, profiles.size()); 880 ASSERT_EQ(1u, profiles.size());
870 881
871 profiles.clear(); 882 profiles.clear();
872 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles); 883 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles);
873 ASSERT_EQ(1u, profiles.size()); 884 ASSERT_EQ(1u, profiles.size());
874 } 885 }
875 886
876 // Checks that requests to start profiling while another profile is taking place 887 // Checks that additional requests will restart a stopped profiler.
877 // are ignored.
878 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) 888 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED)
879 #define MAYBE_ConcurrentProfiling ConcurrentProfiling 889 #define MAYBE_WillRestartSampler WillRestartSampler
880 #else 890 #else
881 #define MAYBE_ConcurrentProfiling DISABLED_ConcurrentProfiling 891 #define MAYBE_WillRestartSampler DISABLED_WillRestartSampler
882 #endif 892 #endif
883 TEST(StackSamplingProfilerTest, MAYBE_ConcurrentProfiling) { 893 TEST(StackSamplingProfilerTest, MAYBE_WillRestartSampler) {
894 StackSamplingProfiler::TestAPI::SetSamplingThreadIdleShutdownTime(0);
895
896 SamplingParams params;
897 params.sampling_interval = TimeDelta::FromMilliseconds(0);
898 params.samples_per_burst = 1;
899
900 std::vector<CallStackProfile> profiles;
901 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles);
902 ASSERT_EQ(1u, profiles.size());
903
904 // Capture thread should still be running at this point.
905 ASSERT_TRUE(StackSamplingProfiler::TestAPI::IsSamplingThreadRunning());
906
907 // Initiate an "idle" shutdown. The task will be run immediately but on
908 // another thread so wait for it to complete.
909 StackSamplingProfiler::TestAPI::InitiateSamplingThreadIdleShutdown();
910 while (StackSamplingProfiler::TestAPI::IsSamplingThreadRunning())
911 PlatformThread::YieldCurrentThread();
912
913 // Ensure another capture will start the sampling thread and run.
914 profiles.clear();
915 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles);
916 ASSERT_EQ(1u, profiles.size());
917 EXPECT_TRUE(StackSamplingProfiler::TestAPI::IsSamplingThreadRunning());
918 }
919
920 // Checks that multiple sampling requests execute in parallel (#1).
921 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED)
922 #define MAYBE_ConcurrentProfiling1 ConcurrentProfiling1
923 #else
924 #define MAYBE_ConcurrentProfiling1 DISABLED_ConcurrentProfiling1
925 #endif
926 TEST(StackSamplingProfilerTest, MAYBE_ConcurrentProfiling1) {
884 WithTargetThread([](PlatformThreadId target_thread_id) { 927 WithTargetThread([](PlatformThreadId target_thread_id) {
885 SamplingParams params[2]; 928 SamplingParams params[2];
886 params[0].initial_delay = TimeDelta::FromMilliseconds(10); 929 params[0].initial_delay = TimeDelta::FromMilliseconds(10);
887 params[0].sampling_interval = TimeDelta::FromMilliseconds(0); 930 params[0].sampling_interval = TimeDelta::FromMilliseconds(1);
888 params[0].samples_per_burst = 1; 931 params[0].samples_per_burst = 10;
889 932
890 params[1].sampling_interval = TimeDelta::FromMilliseconds(0); 933 params[1].initial_delay = TimeDelta::FromMilliseconds(10);
891 params[1].samples_per_burst = 1; 934 params[1].sampling_interval = TimeDelta::FromMilliseconds(1);
935 params[1].samples_per_burst = 10;
892 936
893 CallStackProfiles profiles[2]; 937 CallStackProfiles profiles[2];
894 std::vector<std::unique_ptr<WaitableEvent>> sampling_completed(2); 938 std::vector<std::unique_ptr<WaitableEvent>> sampling_completed(2);
895 std::vector<std::unique_ptr<StackSamplingProfiler>> profiler(2); 939 std::vector<std::unique_ptr<StackSamplingProfiler>> profiler(2);
896 for (int i = 0; i < 2; ++i) { 940 for (int i = 0; i < 2; ++i) {
897 sampling_completed[i] = 941 sampling_completed[i] =
898 MakeUnique<WaitableEvent>(WaitableEvent::ResetPolicy::AUTOMATIC, 942 MakeUnique<WaitableEvent>(WaitableEvent::ResetPolicy::AUTOMATIC,
899 WaitableEvent::InitialState::NOT_SIGNALED); 943 WaitableEvent::InitialState::NOT_SIGNALED);
900 const StackSamplingProfiler::CompletedCallback callback = 944 const StackSamplingProfiler::CompletedCallback callback =
901 Bind(&SaveProfilesAndSignalEvent, Unretained(&profiles[i]), 945 Bind(&SaveProfilesAndSignalEvent, Unretained(&profiles[i]),
(...skipping 10 matching lines...) Expand all
912 std::transform( 956 std::transform(
913 sampling_completed.begin(), sampling_completed.end(), 957 sampling_completed.begin(), sampling_completed.end(),
914 sampling_completed_rawptrs.begin(), 958 sampling_completed_rawptrs.begin(),
915 [](const std::unique_ptr<WaitableEvent>& elem) { return elem.get(); }); 959 [](const std::unique_ptr<WaitableEvent>& elem) { return elem.get(); });
916 // Wait for one profiler to finish. 960 // Wait for one profiler to finish.
917 size_t completed_profiler = 961 size_t completed_profiler =
918 WaitableEvent::WaitMany(sampling_completed_rawptrs.data(), 2); 962 WaitableEvent::WaitMany(sampling_completed_rawptrs.data(), 2);
919 EXPECT_EQ(1u, profiles[completed_profiler].size()); 963 EXPECT_EQ(1u, profiles[completed_profiler].size());
920 964
921 size_t other_profiler = 1 - completed_profiler; 965 size_t other_profiler = 1 - completed_profiler;
922 // Give the other profiler a chance to run and observe that it hasn't. 966 // Give the other profiler a chance to finish and verify it does so.
923 EXPECT_FALSE(sampling_completed[other_profiler]->TimedWait( 967 EXPECT_TRUE(sampling_completed[other_profiler]->TimedWait(
924 TimeDelta::FromMilliseconds(25))); 968 TimeDelta::FromMilliseconds(250)));
925
926 // Start the other profiler again and it should run.
927 profiler[other_profiler]->Start();
928 sampling_completed[other_profiler]->Wait();
929 EXPECT_EQ(1u, profiles[other_profiler].size());
930 }); 969 });
931 } 970 }
932 971
972 // Checks that multiple sampling requests execute in parallel (#2).
973 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED)
974 #define MAYBE_ConcurrentProfiling2 ConcurrentProfiling2
975 #else
976 #define MAYBE_ConcurrentProfiling2 DISABLED_ConcurrentProfiling2
977 #endif
978 TEST(StackSamplingProfilerTest, MAYBE_ConcurrentProfiling2) {
979 WithTargetThread([](PlatformThreadId target_thread_id) {
980 SamplingParams params[2];
981 params[0].initial_delay = TimeDelta::FromMilliseconds(10);
982 params[0].sampling_interval = TimeDelta::FromMilliseconds(2);
983 params[0].samples_per_burst = 9;
984
985 params[1].initial_delay = TimeDelta::FromMilliseconds(10);
986 params[1].sampling_interval = TimeDelta::FromMilliseconds(3);
987 params[1].samples_per_burst = 6;
988
989 CallStackProfiles profiles[2];
990 std::vector<std::unique_ptr<WaitableEvent>> sampling_completed(2);
991 std::vector<std::unique_ptr<StackSamplingProfiler>> profiler(2);
992 for (int i = 0; i < 2; ++i) {
993 sampling_completed[i] =
994 MakeUnique<WaitableEvent>(WaitableEvent::ResetPolicy::AUTOMATIC,
995 WaitableEvent::InitialState::NOT_SIGNALED);
996 const StackSamplingProfiler::CompletedCallback callback =
997 Bind(&SaveProfilesAndSignalEvent, Unretained(&profiles[i]),
998 Unretained(sampling_completed[i].get()));
999 profiler[i] = MakeUnique<StackSamplingProfiler>(target_thread_id,
1000 params[i], callback);
1001 }
1002
1003 for (size_t i = 0; i < profiler.size(); ++i)
1004 profiler[i]->Start();
1005
1006 std::vector<WaitableEvent*> sampling_completed_rawptrs(
1007 sampling_completed.size());
1008 std::transform(
1009 sampling_completed.begin(), sampling_completed.end(),
1010 sampling_completed_rawptrs.begin(),
1011 [](const std::unique_ptr<WaitableEvent>& elem) { return elem.get(); });
1012 // Wait for one profiler to finish.
1013 size_t completed_profiler =
1014 WaitableEvent::WaitMany(sampling_completed_rawptrs.data(), 2);
1015 EXPECT_EQ(1u, profiles[completed_profiler].size());
1016 // Stop and destroy all the profilers. Don't crash.
1017 for (size_t i = 0; i < profiler.size(); ++i)
1018 profiler[i]->Stop();
1019 for (size_t i = 0; i < profiler.size(); ++i)
1020 profiler[i].reset();
1021 });
1022 }
1023
1024 // Checks that multiple sampling requests execute in parallel (#3).
1025 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED)
1026 #define MAYBE_ConcurrentProfiling3 ConcurrentProfiling3
1027 #else
1028 #define MAYBE_ConcurrentProfiling3 DISABLED_ConcurrentProfiling3
1029 #endif
1030 TEST(StackSamplingProfilerTest, MAYBE_ConcurrentProfiling3) {
1031 WithTargetThread([](PlatformThreadId target_thread_id) {
1032 SamplingParams params[3];
1033 params[0].initial_delay = TimeDelta::FromMilliseconds(8);
1034 params[0].sampling_interval = TimeDelta::FromMilliseconds(4);
1035 params[0].samples_per_burst = 10;
1036
1037 params[1].initial_delay = TimeDelta::FromMilliseconds(9);
1038 params[1].sampling_interval = TimeDelta::FromMilliseconds(3);
1039 params[1].samples_per_burst = 10;
1040
1041 params[2].initial_delay = TimeDelta::FromMilliseconds(10);
1042 params[2].sampling_interval = TimeDelta::FromMilliseconds(2);
1043 params[2].samples_per_burst = 10;
1044
1045 CallStackProfiles profiles[3];
1046 std::vector<std::unique_ptr<WaitableEvent>> sampling_completed(3);
1047 std::vector<std::unique_ptr<StackSamplingProfiler>> profiler(3);
1048 for (int i = 0; i < 3; ++i) {
1049 sampling_completed[i] =
1050 MakeUnique<WaitableEvent>(WaitableEvent::ResetPolicy::AUTOMATIC,
1051 WaitableEvent::InitialState::NOT_SIGNALED);
1052 const StackSamplingProfiler::CompletedCallback callback =
1053 Bind(&SaveProfilesAndSignalEvent, Unretained(&profiles[i]),
1054 Unretained(sampling_completed[i].get()));
1055 profiler[i] = MakeUnique<StackSamplingProfiler>(target_thread_id,
1056 params[i], callback);
1057 }
1058
1059 for (size_t i = 0; i < profiler.size(); ++i)
1060 profiler[i]->Start();
1061
1062 std::vector<WaitableEvent*> sampling_completed_rawptrs(
1063 sampling_completed.size());
1064 std::transform(
1065 sampling_completed.begin(), sampling_completed.end(),
1066 sampling_completed_rawptrs.begin(),
1067 [](const std::unique_ptr<WaitableEvent>& elem) { return elem.get(); });
1068 // Wait for one profiler to finish.
1069 size_t completed_profiler =
1070 WaitableEvent::WaitMany(sampling_completed_rawptrs.data(), 2);
1071 EXPECT_EQ(1u, profiles[completed_profiler].size());
1072 // Destroy all the profilers. Don't crash.
1073 for (size_t i = 0; i < profiler.size(); ++i)
1074 profiler[i].reset();
1075 });
1076 }
1077
933 // Checks that a stack that runs through another library produces a stack with 1078 // Checks that a stack that runs through another library produces a stack with
934 // the expected functions. 1079 // the expected functions.
935 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) 1080 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED)
936 #define MAYBE_OtherLibrary OtherLibrary 1081 #define MAYBE_OtherLibrary OtherLibrary
937 #else 1082 #else
938 #define MAYBE_OtherLibrary DISABLED_OtherLibrary 1083 #define MAYBE_OtherLibrary DISABLED_OtherLibrary
939 #endif 1084 #endif
940 TEST(StackSamplingProfilerTest, MAYBE_OtherLibrary) { 1085 TEST(StackSamplingProfilerTest, MAYBE_OtherLibrary) {
941 SamplingParams params; 1086 SamplingParams params;
942 params.sampling_interval = TimeDelta::FromMilliseconds(0); 1087 params.sampling_interval = TimeDelta::FromMilliseconds(0);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
1017 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) 1162 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED)
1018 #define MAYBE_UnloadedLibrary UnloadedLibrary 1163 #define MAYBE_UnloadedLibrary UnloadedLibrary
1019 #else 1164 #else
1020 #define MAYBE_UnloadedLibrary DISABLED_UnloadedLibrary 1165 #define MAYBE_UnloadedLibrary DISABLED_UnloadedLibrary
1021 #endif 1166 #endif
1022 TEST(StackSamplingProfilerTest, MAYBE_UnloadedLibrary) { 1167 TEST(StackSamplingProfilerTest, MAYBE_UnloadedLibrary) {
1023 TestLibraryUnload(true); 1168 TestLibraryUnload(true);
1024 } 1169 }
1025 1170
1026 } // namespace base 1171 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698