OLD | NEW |
---|---|
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 605 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
616 sample, &TargetThread::SignalAndWaitUntilSignaled); | 616 sample, &TargetThread::SignalAndWaitUntilSignaled); |
617 ASSERT_TRUE(loc != sample.frames.end()) | 617 ASSERT_TRUE(loc != sample.frames.end()) |
618 << "Function at " | 618 << "Function at " |
619 << MaybeFixupFunctionAddressForILT(reinterpret_cast<const void*>( | 619 << MaybeFixupFunctionAddressForILT(reinterpret_cast<const void*>( |
620 &TargetThread::SignalAndWaitUntilSignaled)) | 620 &TargetThread::SignalAndWaitUntilSignaled)) |
621 << " was not found in stack:\n" | 621 << " was not found in stack:\n" |
622 << FormatSampleForDiagnosticOutput(sample, profile.modules); | 622 << FormatSampleForDiagnosticOutput(sample, profile.modules); |
623 FilePath executable_path; | 623 FilePath executable_path; |
624 EXPECT_TRUE(PathService::Get(FILE_EXE, &executable_path)); | 624 EXPECT_TRUE(PathService::Get(FILE_EXE, &executable_path)); |
625 EXPECT_EQ(executable_path, profile.modules[loc->module_index].filename); | 625 EXPECT_EQ(executable_path, profile.modules[loc->module_index].filename); |
626 | |
627 // Ensure the sampling thread is still running at this point. | |
628 ASSERT_TRUE(StackSamplingProfiler::IsSamplingThreadRunningForTesting()); | |
629 | |
630 // Shutdown the sampler as though the process were about to exit. | |
631 StackSamplingProfiler::Shutdown(); | |
632 EXPECT_FALSE(StackSamplingProfiler::IsSamplingThreadRunningForTesting()); | |
633 | |
634 // Shutdown is a permanent thing in general but this needs to be undone so | |
635 // other tests can run. | |
636 StackSamplingProfiler::UndoShutdownForTesting(); | |
637 EXPECT_FALSE(StackSamplingProfiler::IsSamplingThreadRunningForTesting()); | |
626 } | 638 } |
627 | 639 |
628 // Checks that annotations are recorded in samples. | 640 // Checks that annotations are recorded in samples. |
629 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | 641 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) |
630 #define MAYBE_Annotations Annotations | 642 #define MAYBE_Annotations Annotations |
631 #else | 643 #else |
632 #define MAYBE_Annotations DISABLED_Annotations | 644 #define MAYBE_Annotations DISABLED_Annotations |
633 #endif | 645 #endif |
634 TEST(StackSamplingProfilerTest, MAYBE_Annotations) { | 646 TEST(StackSamplingProfilerTest, MAYBE_Annotations) { |
635 StackSamplingProfiler::ResetAnnotationsForTesting(); | 647 StackSamplingProfiler::ResetAnnotationsForTesting(); |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
846 target_thread_id, params, Bind(&SaveProfiles, Unretained(&profiles)))); | 858 target_thread_id, params, Bind(&SaveProfiles, Unretained(&profiles)))); |
847 profiler->Start(); | 859 profiler->Start(); |
848 profiler.reset(); | 860 profiler.reset(); |
849 | 861 |
850 // Wait longer than a sample interval to catch any use-after-free actions by | 862 // Wait longer than a sample interval to catch any use-after-free actions by |
851 // the profiler thread. | 863 // the profiler thread. |
852 PlatformThread::Sleep(TimeDelta::FromMilliseconds(50)); | 864 PlatformThread::Sleep(TimeDelta::FromMilliseconds(50)); |
853 }); | 865 }); |
854 } | 866 } |
855 | 867 |
868 // Checks that we can destroy the thread under test while profiling. | |
869 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | |
870 #define MAYBE_DestroyThreadWhileProfiling DestroyThreadWhileProfiling | |
871 #else | |
872 #define MAYBE_DestroyThreadWhileProfiling DISABLED_DestroyThreadWhileProfiling | |
873 #endif | |
874 TEST(StackSamplingProfilerTest, MAYBE_DestroyThreadWhileProfiling) { | |
875 // Set up a thread for testing. | |
876 StackConfiguration stack_config(StackConfiguration::NORMAL); | |
877 TargetThread target_thread(stack_config); | |
878 PlatformThreadHandle target_thread_handle; | |
879 ASSERT_TRUE(PlatformThread::Create(0, &target_thread, &target_thread_handle)); | |
880 | |
881 // Set up the sampler: 10 samples, 1 second apart | |
882 SamplingParams params; | |
883 params.sampling_interval = TimeDelta::FromSeconds(1); | |
884 params.samples_per_burst = 10; | |
885 std::vector<CallStackProfile> profiles; | |
886 WaitableEvent sampling_thread_completed( | |
887 WaitableEvent::ResetPolicy::MANUAL, | |
888 WaitableEvent::InitialState::NOT_SIGNALED); | |
889 const StackSamplingProfiler::CompletedCallback callback = | |
890 Bind(&SaveProfilesAndSignalEvent, Unretained(&profiles), | |
891 Unretained(&sampling_thread_completed)); | |
892 | |
893 // Start the thread and the sampler and wait a short while. | |
894 target_thread.WaitForThreadStart(); | |
895 StackSamplingProfiler profiler(target_thread.id(), params, callback); | |
896 profiler.Start(); | |
897 | |
898 // Delay a short while so some samples will get collected. | |
899 PlatformThread::Sleep(TimeDelta::FromSeconds(3)); | |
Mike Wittman
2017/01/31 22:14:27
Coordinating threads via sleep will cause this tes
bcwhite
2017/02/01 14:47:29
I'll look at that.
bcwhite
2017/02/01 17:53:10
Done.
| |
900 | |
901 // Stop the target thread. | |
902 target_thread.SignalThreadToFinish(); | |
903 PlatformThread::Join(target_thread_handle); | |
904 | |
905 // Wait a bit longer and ensure that the sampler completes. | |
906 PlatformThread::Sleep(TimeDelta::FromSeconds(2)); | |
907 | |
908 ASSERT_EQ(1U, profiles.size()); | |
909 EXPECT_LT(1U, profiles[0].samples.size()); | |
910 EXPECT_GT(9U, profiles[0].samples.size()); | |
911 } | |
912 | |
856 // Checks that the same profiler may be run multiple times. | 913 // Checks that the same profiler may be run multiple times. |
857 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | 914 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) |
858 #define MAYBE_CanRunMultipleTimes CanRunMultipleTimes | 915 #define MAYBE_CanRunMultipleTimes CanRunMultipleTimes |
859 #else | 916 #else |
860 #define MAYBE_CanRunMultipleTimes DISABLED_CanRunMultipleTimes | 917 #define MAYBE_CanRunMultipleTimes DISABLED_CanRunMultipleTimes |
861 #endif | 918 #endif |
862 TEST(StackSamplingProfilerTest, MAYBE_CanRunMultipleTimes) { | 919 TEST(StackSamplingProfilerTest, MAYBE_CanRunMultipleTimes) { |
920 StackSamplingProfiler::SetSamplingThreadIdleShutdownTimeForTesting(0); | |
921 | |
863 SamplingParams params; | 922 SamplingParams params; |
864 params.sampling_interval = TimeDelta::FromMilliseconds(0); | 923 params.sampling_interval = TimeDelta::FromMilliseconds(0); |
865 params.samples_per_burst = 1; | 924 params.samples_per_burst = 1; |
866 | 925 |
867 std::vector<CallStackProfile> profiles; | 926 std::vector<CallStackProfile> profiles; |
868 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles); | 927 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles); |
869 ASSERT_EQ(1u, profiles.size()); | 928 ASSERT_EQ(1u, profiles.size()); |
870 | 929 |
871 profiles.clear(); | 930 profiles.clear(); |
872 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles); | 931 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles); |
873 ASSERT_EQ(1u, profiles.size()); | 932 ASSERT_EQ(1u, profiles.size()); |
933 | |
934 // Capture thread should still be running at this point. | |
935 ASSERT_TRUE(StackSamplingProfiler::IsSamplingThreadRunningForTesting()); | |
936 | |
937 // Initiate an "idle" shutdown. The task will be run immediately but on | |
938 // another thread so wait for it to complete. | |
939 StackSamplingProfiler::InitiateSamplingThreadIdleShutdownForTesting(); | |
940 while (StackSamplingProfiler::IsSamplingThreadRunningForTesting()) | |
941 PlatformThread::YieldCurrentThread(); | |
942 | |
943 // Ensure another capture will start the sampling thread and run. | |
944 profiles.clear(); | |
945 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles); | |
946 ASSERT_EQ(1u, profiles.size()); | |
947 EXPECT_TRUE(StackSamplingProfiler::IsSamplingThreadRunningForTesting()); | |
874 } | 948 } |
875 | 949 |
876 // Checks that requests to start profiling while another profile is taking place | 950 // Checks that requests to start profiling while another profile is taking place |
877 // are ignored. | 951 // are ignored. |
878 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | 952 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) |
879 #define MAYBE_ConcurrentProfiling ConcurrentProfiling | 953 #define MAYBE_ConcurrentProfiling ConcurrentProfiling |
880 #else | 954 #else |
881 #define MAYBE_ConcurrentProfiling DISABLED_ConcurrentProfiling | 955 #define MAYBE_ConcurrentProfiling DISABLED_ConcurrentProfiling |
882 #endif | 956 #endif |
883 TEST(StackSamplingProfilerTest, MAYBE_ConcurrentProfiling) { | 957 TEST(StackSamplingProfilerTest, MAYBE_ConcurrentProfiling) { |
884 WithTargetThread([](PlatformThreadId target_thread_id) { | 958 WithTargetThread([](PlatformThreadId target_thread_id) { |
885 SamplingParams params[2]; | 959 SamplingParams params[2]; |
886 params[0].initial_delay = TimeDelta::FromMilliseconds(10); | 960 params[0].initial_delay = TimeDelta::FromMilliseconds(10); |
887 params[0].sampling_interval = TimeDelta::FromMilliseconds(0); | 961 params[0].sampling_interval = TimeDelta::FromMilliseconds(1); |
888 params[0].samples_per_burst = 1; | 962 params[0].samples_per_burst = 10; |
889 | 963 |
890 params[1].sampling_interval = TimeDelta::FromMilliseconds(0); | 964 params[0].initial_delay = TimeDelta::FromMilliseconds(10); |
891 params[1].samples_per_burst = 1; | 965 params[1].sampling_interval = TimeDelta::FromMilliseconds(1); |
966 params[1].samples_per_burst = 10; | |
892 | 967 |
893 CallStackProfiles profiles[2]; | 968 CallStackProfiles profiles[2]; |
894 std::vector<std::unique_ptr<WaitableEvent>> sampling_completed(2); | 969 std::vector<std::unique_ptr<WaitableEvent>> sampling_completed(2); |
895 std::vector<std::unique_ptr<StackSamplingProfiler>> profiler(2); | 970 std::vector<std::unique_ptr<StackSamplingProfiler>> profiler(2); |
896 for (int i = 0; i < 2; ++i) { | 971 for (int i = 0; i < 2; ++i) { |
897 sampling_completed[i] = | 972 sampling_completed[i] = |
898 MakeUnique<WaitableEvent>(WaitableEvent::ResetPolicy::AUTOMATIC, | 973 MakeUnique<WaitableEvent>(WaitableEvent::ResetPolicy::AUTOMATIC, |
899 WaitableEvent::InitialState::NOT_SIGNALED); | 974 WaitableEvent::InitialState::NOT_SIGNALED); |
900 const StackSamplingProfiler::CompletedCallback callback = | 975 const StackSamplingProfiler::CompletedCallback callback = |
901 Bind(&SaveProfilesAndSignalEvent, Unretained(&profiles[i]), | 976 Bind(&SaveProfilesAndSignalEvent, Unretained(&profiles[i]), |
(...skipping 10 matching lines...) Expand all Loading... | |
912 std::transform( | 987 std::transform( |
913 sampling_completed.begin(), sampling_completed.end(), | 988 sampling_completed.begin(), sampling_completed.end(), |
914 sampling_completed_rawptrs.begin(), | 989 sampling_completed_rawptrs.begin(), |
915 [](const std::unique_ptr<WaitableEvent>& elem) { return elem.get(); }); | 990 [](const std::unique_ptr<WaitableEvent>& elem) { return elem.get(); }); |
916 // Wait for one profiler to finish. | 991 // Wait for one profiler to finish. |
917 size_t completed_profiler = | 992 size_t completed_profiler = |
918 WaitableEvent::WaitMany(sampling_completed_rawptrs.data(), 2); | 993 WaitableEvent::WaitMany(sampling_completed_rawptrs.data(), 2); |
919 EXPECT_EQ(1u, profiles[completed_profiler].size()); | 994 EXPECT_EQ(1u, profiles[completed_profiler].size()); |
920 | 995 |
921 size_t other_profiler = 1 - completed_profiler; | 996 size_t other_profiler = 1 - completed_profiler; |
922 // Give the other profiler a chance to run and observe that it hasn't. | 997 // Give the other profiler a chance to finish and verify it does no. |
923 EXPECT_FALSE(sampling_completed[other_profiler]->TimedWait( | 998 EXPECT_TRUE(sampling_completed[other_profiler]->TimedWait( |
924 TimeDelta::FromMilliseconds(25))); | 999 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 }); | 1000 }); |
931 } | 1001 } |
932 | 1002 |
933 // Checks that a stack that runs through another library produces a stack with | 1003 // Checks that a stack that runs through another library produces a stack with |
934 // the expected functions. | 1004 // the expected functions. |
935 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | 1005 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) |
936 #define MAYBE_OtherLibrary OtherLibrary | 1006 #define MAYBE_OtherLibrary OtherLibrary |
937 #else | 1007 #else |
938 #define MAYBE_OtherLibrary DISABLED_OtherLibrary | 1008 #define MAYBE_OtherLibrary DISABLED_OtherLibrary |
939 #endif | 1009 #endif |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1017 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | 1087 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) |
1018 #define MAYBE_UnloadedLibrary UnloadedLibrary | 1088 #define MAYBE_UnloadedLibrary UnloadedLibrary |
1019 #else | 1089 #else |
1020 #define MAYBE_UnloadedLibrary DISABLED_UnloadedLibrary | 1090 #define MAYBE_UnloadedLibrary DISABLED_UnloadedLibrary |
1021 #endif | 1091 #endif |
1022 TEST(StackSamplingProfilerTest, MAYBE_UnloadedLibrary) { | 1092 TEST(StackSamplingProfilerTest, MAYBE_UnloadedLibrary) { |
1023 TestLibraryUnload(true); | 1093 TestLibraryUnload(true); |
1024 } | 1094 } |
1025 | 1095 |
1026 } // namespace base | 1096 } // namespace base |
OLD | NEW |