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 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
344 target_thread.SignalThreadToFinish(); | 344 target_thread.SignalThreadToFinish(); |
345 | 345 |
346 PlatformThread::Join(target_thread_handle); | 346 PlatformThread::Join(target_thread_handle); |
347 } | 347 } |
348 | 348 |
349 template <class Function> | 349 template <class Function> |
350 void WithTargetThread(Function function) { | 350 void WithTargetThread(Function function) { |
351 WithTargetThread(function, StackConfiguration(StackConfiguration::NORMAL)); | 351 WithTargetThread(function, StackConfiguration(StackConfiguration::NORMAL)); |
352 } | 352 } |
353 | 353 |
354 // Waits for one of multiple samplings to complete. | |
355 void CreateProfilers( | |
356 PlatformThreadId target_thread_id, | |
357 const SamplingParams* params, | |
358 size_t count, | |
359 std::vector<CallStackProfiles>* profiles, | |
360 std::vector<std::unique_ptr<StackSamplingProfiler>>* profilers, | |
361 std::vector<std::unique_ptr<WaitableEvent>>* completed) { | |
362 CHECK(profiles->empty()); | |
Mike Wittman
2017/03/21 16:50:38
ASSERT_TRUE rather than CHECK in test code (or EXP
bcwhite
2017/03/22 17:48:54
Done.
| |
363 CHECK(profilers->empty()); | |
364 CHECK(completed->empty()); | |
365 | |
366 // Vectors have to be appropriately sized in advance so that the addresses of | |
367 // values don't change. | |
368 profiles->reserve(count); | |
369 profilers->reserve(count); | |
370 completed->reserve(count); | |
371 | |
372 for (size_t i = 0; i < count; ++i) { | |
373 profiles->push_back(CallStackProfiles()); | |
374 completed->push_back( | |
375 MakeUnique<WaitableEvent>(WaitableEvent::ResetPolicy::AUTOMATIC, | |
376 WaitableEvent::InitialState::NOT_SIGNALED)); | |
377 const StackSamplingProfiler::CompletedCallback callback = | |
378 Bind(&SaveProfilesAndSignalEvent, Unretained(&profiles->back()), | |
379 Unretained(completed->back().get())); | |
380 profilers->push_back(MakeUnique<StackSamplingProfiler>( | |
381 target_thread_id, params[i], callback)); | |
382 } | |
383 } | |
384 | |
354 // Captures profiles as specified by |params| on the TargetThread, and returns | 385 // Captures profiles as specified by |params| on the TargetThread, and returns |
355 // them in |profiles|. Waits up to |profiler_wait_time| for the profiler to | 386 // them in |profiles|. Waits up to |profiler_wait_time| for the profiler to |
356 // complete. | 387 // complete. |
357 void CaptureProfiles(const SamplingParams& params, TimeDelta profiler_wait_time, | 388 void CaptureProfiles(const SamplingParams& params, TimeDelta profiler_wait_time, |
358 CallStackProfiles* profiles) { | 389 CallStackProfiles* profiles) { |
359 profiles->clear(); | 390 profiles->clear(); |
360 | 391 |
361 WithTargetThread([¶ms, profiles, | 392 WithTargetThread([¶ms, profiles, |
362 profiler_wait_time](PlatformThreadId target_thread_id) { | 393 profiler_wait_time](PlatformThreadId target_thread_id) { |
363 WaitableEvent sampling_thread_completed( | 394 WaitableEvent sampling_thread_completed( |
364 WaitableEvent::ResetPolicy::MANUAL, | 395 WaitableEvent::ResetPolicy::MANUAL, |
365 WaitableEvent::InitialState::NOT_SIGNALED); | 396 WaitableEvent::InitialState::NOT_SIGNALED); |
366 const StackSamplingProfiler::CompletedCallback callback = | 397 const StackSamplingProfiler::CompletedCallback callback = |
367 Bind(&SaveProfilesAndSignalEvent, Unretained(profiles), | 398 Bind(&SaveProfilesAndSignalEvent, Unretained(profiles), |
368 Unretained(&sampling_thread_completed)); | 399 Unretained(&sampling_thread_completed)); |
369 StackSamplingProfiler profiler(target_thread_id, params, callback); | 400 StackSamplingProfiler profiler(target_thread_id, params, callback); |
370 profiler.Start(); | 401 profiler.Start(); |
371 sampling_thread_completed.TimedWait(profiler_wait_time); | 402 sampling_thread_completed.TimedWait(profiler_wait_time); |
372 profiler.Stop(); | 403 profiler.Stop(); |
373 sampling_thread_completed.Wait(); | 404 sampling_thread_completed.Wait(); |
374 }); | 405 }); |
375 } | 406 } |
376 | 407 |
408 // Waits for one of multiple samplings to complete. | |
409 size_t WaitForSamplingComplete( | |
410 std::vector<std::unique_ptr<WaitableEvent>>* sampling_completed) { | |
411 // Map unique_ptrs to something that WaitMany can accept. | |
412 std::vector<WaitableEvent*> sampling_completed_rawptrs( | |
413 sampling_completed->size()); | |
414 std::transform( | |
415 sampling_completed->begin(), sampling_completed->end(), | |
416 sampling_completed_rawptrs.begin(), | |
417 [](const std::unique_ptr<WaitableEvent>& elem) { return elem.get(); }); | |
418 // Wait for one profiler to finish. | |
419 return WaitableEvent::WaitMany(sampling_completed_rawptrs.data(), | |
420 sampling_completed_rawptrs.size()); | |
421 } | |
422 | |
377 // If this executable was linked with /INCREMENTAL (the default for non-official | 423 // If this executable was linked with /INCREMENTAL (the default for non-official |
378 // debug and release builds on Windows), function addresses do not correspond to | 424 // debug and release builds on Windows), function addresses do not correspond to |
379 // function code itself, but instead to instructions in the Incremental Link | 425 // function code itself, but instead to instructions in the Incremental Link |
380 // Table that jump to the functions. Checks for a jump instruction and if | 426 // Table that jump to the functions. Checks for a jump instruction and if |
381 // present does a little decompilation to find the function's actual starting | 427 // present does a little decompilation to find the function's actual starting |
382 // address. | 428 // address. |
383 const void* MaybeFixupFunctionAddressForILT(const void* function_address) { | 429 const void* MaybeFixupFunctionAddressForILT(const void* function_address) { |
384 #if defined(_WIN64) | 430 #if defined(_WIN64) |
385 const unsigned char* opcode = | 431 const unsigned char* opcode = |
386 reinterpret_cast<const unsigned char*>(function_address); | 432 reinterpret_cast<const unsigned char*>(function_address); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
437 // asynchronous library loading has completed before walking the stack. If | 483 // asynchronous library loading has completed before walking the stack. If |
438 // false, the unloading may still be occurring during the stack walk. | 484 // false, the unloading may still be occurring during the stack walk. |
439 void TestLibraryUnload(bool wait_until_unloaded) { | 485 void TestLibraryUnload(bool wait_until_unloaded) { |
440 // Test delegate that supports intervening between the copying of the stack | 486 // Test delegate that supports intervening between the copying of the stack |
441 // and the walking of the stack. | 487 // and the walking of the stack. |
442 class StackCopiedSignaler : public NativeStackSamplerTestDelegate { | 488 class StackCopiedSignaler : public NativeStackSamplerTestDelegate { |
443 public: | 489 public: |
444 StackCopiedSignaler(WaitableEvent* stack_copied, | 490 StackCopiedSignaler(WaitableEvent* stack_copied, |
445 WaitableEvent* start_stack_walk, | 491 WaitableEvent* start_stack_walk, |
446 bool wait_to_walk_stack) | 492 bool wait_to_walk_stack) |
447 : stack_copied_(stack_copied), start_stack_walk_(start_stack_walk), | 493 : stack_copied_(stack_copied), |
448 wait_to_walk_stack_(wait_to_walk_stack) { | 494 start_stack_walk_(start_stack_walk), |
449 } | 495 wait_to_walk_stack_(wait_to_walk_stack) {} |
450 | 496 |
451 void OnPreStackWalk() override { | 497 void OnPreStackWalk() override { |
452 stack_copied_->Signal(); | 498 stack_copied_->Signal(); |
453 if (wait_to_walk_stack_) | 499 if (wait_to_walk_stack_) |
454 start_stack_walk_->Wait(); | 500 start_stack_walk_->Wait(); |
455 } | 501 } |
456 | 502 |
457 private: | 503 private: |
458 WaitableEvent* const stack_copied_; | 504 WaitableEvent* const stack_copied_; |
459 WaitableEvent* const start_stack_walk_; | 505 WaitableEvent* const start_stack_walk_; |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
713 reinterpret_cast<const void*>(&TargetThread::CallWithAlloca)) | 759 reinterpret_cast<const void*>(&TargetThread::CallWithAlloca)) |
714 << " was not found in stack:\n" | 760 << " was not found in stack:\n" |
715 << FormatSampleForDiagnosticOutput(sample, profile.modules); | 761 << FormatSampleForDiagnosticOutput(sample, profile.modules); |
716 | 762 |
717 // These frames should be adjacent on the stack. | 763 // These frames should be adjacent on the stack. |
718 EXPECT_EQ(1, alloca_frame - end_frame) | 764 EXPECT_EQ(1, alloca_frame - end_frame) |
719 << "Stack:\n" | 765 << "Stack:\n" |
720 << FormatSampleForDiagnosticOutput(sample, profile.modules); | 766 << FormatSampleForDiagnosticOutput(sample, profile.modules); |
721 } | 767 } |
722 | 768 |
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([¶ms, &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 | 769 // Checks that the expected number of profiles and samples are present in the |
753 // call stack profiles produced. | 770 // call stack profiles produced. |
754 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | 771 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) |
755 #define MAYBE_MultipleProfilesAndSamples MultipleProfilesAndSamples | 772 #define MAYBE_MultipleProfilesAndSamples MultipleProfilesAndSamples |
756 #else | 773 #else |
757 #define MAYBE_MultipleProfilesAndSamples DISABLED_MultipleProfilesAndSamples | 774 #define MAYBE_MultipleProfilesAndSamples DISABLED_MultipleProfilesAndSamples |
758 #endif | 775 #endif |
759 TEST(StackSamplingProfilerTest, MAYBE_MultipleProfilesAndSamples) { | 776 TEST(StackSamplingProfilerTest, MAYBE_MultipleProfilesAndSamples) { |
760 SamplingParams params; | 777 SamplingParams params; |
761 params.burst_interval = params.sampling_interval = | 778 params.burst_interval = params.sampling_interval = |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
853 }); | 870 }); |
854 } | 871 } |
855 | 872 |
856 // Checks that the same profiler may be run multiple times. | 873 // Checks that the same profiler may be run multiple times. |
857 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | 874 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) |
858 #define MAYBE_CanRunMultipleTimes CanRunMultipleTimes | 875 #define MAYBE_CanRunMultipleTimes CanRunMultipleTimes |
859 #else | 876 #else |
860 #define MAYBE_CanRunMultipleTimes DISABLED_CanRunMultipleTimes | 877 #define MAYBE_CanRunMultipleTimes DISABLED_CanRunMultipleTimes |
861 #endif | 878 #endif |
862 TEST(StackSamplingProfilerTest, MAYBE_CanRunMultipleTimes) { | 879 TEST(StackSamplingProfilerTest, MAYBE_CanRunMultipleTimes) { |
880 StackSamplingProfiler::TestAPI::DisableIdleShutdown(); | |
881 | |
882 WithTargetThread([](PlatformThreadId target_thread_id) { | |
883 SamplingParams params; | |
884 params.sampling_interval = TimeDelta::FromMilliseconds(0); | |
885 params.samples_per_burst = 1; | |
886 | |
887 CallStackProfiles profiles; | |
888 WaitableEvent sampling_completed(WaitableEvent::ResetPolicy::MANUAL, | |
889 WaitableEvent::InitialState::NOT_SIGNALED); | |
890 const StackSamplingProfiler::CompletedCallback callback = | |
891 Bind(&SaveProfilesAndSignalEvent, Unretained(&profiles), | |
892 Unretained(&sampling_completed)); | |
893 StackSamplingProfiler profiler(target_thread_id, params, callback); | |
894 | |
895 // Just start and stop to execute code paths. | |
896 profiler.Start(); | |
897 profiler.Stop(); | |
898 sampling_completed.Wait(); | |
899 | |
900 // Ensure a second request will run and not block. | |
901 sampling_completed.Reset(); | |
902 profiles.clear(); | |
903 profiler.Start(); | |
904 sampling_completed.Wait(); | |
905 profiler.Stop(); | |
906 ASSERT_EQ(1u, profiles.size()); | |
907 }); | |
908 } | |
909 | |
910 // Checks that the different profilers may be run. | |
911 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | |
912 #define MAYBE_CanRunMultipleProfilers CanRunMultipleProfilers | |
913 #else | |
914 #define MAYBE_CanRunMultipleProfilers DISABLED_CanRunMultipleProfilers | |
915 #endif | |
916 TEST(StackSamplingProfilerTest, MAYBE_CanRunMultipleProfilers) { | |
917 StackSamplingProfiler::TestAPI::DisableIdleShutdown(); | |
918 | |
863 SamplingParams params; | 919 SamplingParams params; |
864 params.sampling_interval = TimeDelta::FromMilliseconds(0); | 920 params.sampling_interval = TimeDelta::FromMilliseconds(0); |
865 params.samples_per_burst = 1; | 921 params.samples_per_burst = 1; |
866 | 922 |
867 std::vector<CallStackProfile> profiles; | 923 std::vector<CallStackProfile> profiles; |
868 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles); | 924 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles); |
869 ASSERT_EQ(1u, profiles.size()); | 925 ASSERT_EQ(1u, profiles.size()); |
870 | 926 |
871 profiles.clear(); | 927 profiles.clear(); |
872 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles); | 928 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles); |
873 ASSERT_EQ(1u, profiles.size()); | 929 ASSERT_EQ(1u, profiles.size()); |
874 } | 930 } |
875 | 931 |
876 // Checks that requests to start profiling while another profile is taking place | 932 // Checks that additional requests will restart a stopped profiler. |
877 // are ignored. | 933 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) |
878 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | 934 #define MAYBE_WillRestartSamplerAfterIdleShutdown \ |
879 #define MAYBE_ConcurrentProfiling ConcurrentProfiling | 935 WillRestartSamplerAfterIdleShutdown |
880 #else | 936 #else |
881 #define MAYBE_ConcurrentProfiling DISABLED_ConcurrentProfiling | 937 #define MAYBE_WillRestartSamplerAfterIdleShutdown \ |
882 #endif | 938 DISABLED_WillRestartSamplerAfterIdleShutdown |
883 TEST(StackSamplingProfilerTest, MAYBE_ConcurrentProfiling) { | 939 #endif |
940 TEST(StackSamplingProfilerTest, MAYBE_WillRestartSamplerAfterIdleShutdown) { | |
941 StackSamplingProfiler::TestAPI::DisableIdleShutdown(); | |
942 | |
943 SamplingParams params; | |
944 params.sampling_interval = TimeDelta::FromMilliseconds(0); | |
945 params.samples_per_burst = 1; | |
946 | |
947 std::vector<CallStackProfile> profiles; | |
948 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles); | |
949 ASSERT_EQ(1u, profiles.size()); | |
950 | |
951 // Capture thread should still be running at this point. | |
952 ASSERT_TRUE(StackSamplingProfiler::TestAPI::IsSamplingThreadRunning()); | |
953 | |
954 // Initiate an "idle" shutdown. The task will be run immediately but on | |
955 // another thread so wait for it to complete. Otherwise, there's no way | |
956 // of knowing below that the thread "restarted". | |
957 StackSamplingProfiler::TestAPI::InitiateSamplingThreadIdleShutdown(); | |
958 while (StackSamplingProfiler::TestAPI::IsSamplingThreadRunning()) | |
959 PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); | |
960 | |
961 // Ensure another capture will start the sampling thread and run. | |
962 profiles.clear(); | |
963 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles); | |
964 ASSERT_EQ(1u, profiles.size()); | |
965 EXPECT_TRUE(StackSamplingProfiler::TestAPI::IsSamplingThreadRunning()); | |
966 } | |
967 | |
968 // Checks that it's safe to stop a task after it's completed and the sampling | |
969 // thread has shut-down for being idle. | |
970 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | |
971 #define MAYBE_StopAfterIdleShutdown StopAfterIdleShutdown | |
972 #else | |
973 #define MAYBE_StopAfterIdleShutdown DISABLED_StopAfterIdleShutdown | |
974 #endif | |
975 TEST(StackSamplingProfilerTest, MAYBE_StopAfterIdleShutdown) { | |
976 StackSamplingProfiler::TestAPI::DisableIdleShutdown(); | |
977 | |
978 WithTargetThread([](PlatformThreadId target_thread_id) { | |
979 SamplingParams params; | |
980 params.sampling_interval = TimeDelta::FromMilliseconds(0); | |
981 params.samples_per_burst = 1; | |
982 | |
983 CallStackProfiles profiles; | |
984 WaitableEvent sampling_completed(WaitableEvent::ResetPolicy::MANUAL, | |
985 WaitableEvent::InitialState::NOT_SIGNALED); | |
986 const StackSamplingProfiler::CompletedCallback callback = | |
987 Bind(&SaveProfilesAndSignalEvent, Unretained(&profiles), | |
988 Unretained(&sampling_completed)); | |
989 StackSamplingProfiler profiler(target_thread_id, params, callback); | |
990 | |
991 // Let it run and then stop due to being idle. | |
992 profiler.Start(); | |
993 sampling_completed.Wait(); | |
994 StackSamplingProfiler::TestAPI::InitiateSamplingThreadIdleShutdown(); | |
995 while (StackSamplingProfiler::TestAPI::IsSamplingThreadRunning()) | |
996 PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); | |
997 | |
998 // Ensure it's still safe to stop. | |
999 profiler.Stop(); | |
1000 EXPECT_FALSE(StackSamplingProfiler::TestAPI::IsSamplingThreadRunning()); | |
1001 }); | |
1002 } | |
1003 | |
1004 // Checks that synchronized multiple sampling requests execute in parallel. | |
1005 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | |
1006 #define MAYBE_ConcurrentProfiling_InSync ConcurrentProfiling_InSync | |
1007 #else | |
1008 #define MAYBE_ConcurrentProfiling_InSync DISABLED_ConcurrentProfiling_InSync | |
1009 #endif | |
1010 TEST(StackSamplingProfilerTest, MAYBE_ConcurrentProfiling_InSync) { | |
1011 StackSamplingProfiler::TestAPI::DisableIdleShutdown(); | |
1012 | |
884 WithTargetThread([](PlatformThreadId target_thread_id) { | 1013 WithTargetThread([](PlatformThreadId target_thread_id) { |
885 SamplingParams params[2]; | 1014 SamplingParams params[2]; |
1015 | |
1016 // Providing an initial delay makes it more likely that both will be | |
1017 // scheduled before either starts to run. Once started, samples will | |
1018 // run ordered by their scheduled, interleaved times regardless of | |
1019 // whatever interval the thread wakes up. Thus, total execution time | |
1020 // will be 10ms (delay) + 10x1ms (sampling) + 1/2 timer minimum interval. | |
886 params[0].initial_delay = TimeDelta::FromMilliseconds(10); | 1021 params[0].initial_delay = TimeDelta::FromMilliseconds(10); |
887 params[0].sampling_interval = TimeDelta::FromMilliseconds(0); | 1022 params[0].sampling_interval = TimeDelta::FromMilliseconds(1); |
1023 params[0].samples_per_burst = 9; | |
1024 | |
1025 params[1].initial_delay = TimeDelta::FromMilliseconds(11); | |
1026 params[1].sampling_interval = TimeDelta::FromMilliseconds(1); | |
1027 params[1].samples_per_burst = 8; | |
1028 | |
1029 std::vector<CallStackProfiles> profiles; | |
1030 std::vector<std::unique_ptr<StackSamplingProfiler>> profilers; | |
1031 std::vector<std::unique_ptr<WaitableEvent>> sampling_completed; | |
1032 CreateProfilers(target_thread_id, params, arraysize(params), &profiles, | |
1033 &profilers, &sampling_completed); | |
1034 | |
1035 profilers[0]->Start(); | |
1036 profilers[1]->Start(); | |
1037 | |
1038 // Wait for one profiler to finish. | |
1039 size_t completed_profiler = WaitForSamplingComplete(&sampling_completed); | |
1040 ASSERT_EQ(1u, profiles[completed_profiler].size()); | |
1041 | |
1042 size_t other_profiler = 1 - completed_profiler; | |
1043 // Wait for the other profiler to finish. | |
1044 sampling_completed[other_profiler]->Wait(); | |
1045 ASSERT_EQ(1u, profiles[other_profiler].size()); | |
1046 | |
1047 // Ensure each got the correct number of samples. | |
1048 EXPECT_EQ(9u, profiles[0][0].samples.size()); | |
1049 EXPECT_EQ(8u, profiles[1][0].samples.size()); | |
1050 }); | |
1051 } | |
1052 | |
1053 // Checks that several mixed sampling requests execute in parallel. | |
1054 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | |
1055 #define MAYBE_ConcurrentProfiling_Mixed ConcurrentProfiling_Mixed | |
1056 #else | |
1057 #define MAYBE_ConcurrentProfiling_Mixed DISABLED_ConcurrentProfiling_Mixed | |
1058 #endif | |
1059 TEST(StackSamplingProfilerTest, MAYBE_ConcurrentProfiling_Mixed) { | |
1060 StackSamplingProfiler::TestAPI::DisableIdleShutdown(); | |
1061 | |
1062 WithTargetThread([](PlatformThreadId target_thread_id) { | |
1063 SamplingParams params[3]; | |
1064 params[0].initial_delay = TimeDelta::FromMilliseconds(8); | |
1065 params[0].sampling_interval = TimeDelta::FromMilliseconds(4); | |
1066 params[0].samples_per_burst = 10; | |
1067 | |
1068 params[1].initial_delay = TimeDelta::FromMilliseconds(9); | |
1069 params[1].sampling_interval = TimeDelta::FromMilliseconds(3); | |
1070 params[1].samples_per_burst = 10; | |
1071 | |
1072 params[2].initial_delay = TimeDelta::FromMilliseconds(10); | |
1073 params[2].sampling_interval = TimeDelta::FromMilliseconds(2); | |
1074 params[2].samples_per_burst = 10; | |
1075 | |
1076 std::vector<CallStackProfiles> profiles; | |
1077 std::vector<std::unique_ptr<StackSamplingProfiler>> profilers; | |
1078 std::vector<std::unique_ptr<WaitableEvent>> sampling_completed; | |
1079 CreateProfilers(target_thread_id, params, arraysize(params), &profiles, | |
1080 &profilers, &sampling_completed); | |
1081 | |
1082 for (size_t i = 0; i < profilers.size(); ++i) | |
1083 profilers[i]->Start(); | |
1084 | |
1085 // Wait for one profiler to finish. | |
1086 size_t completed_profiler = WaitForSamplingComplete(&sampling_completed); | |
1087 EXPECT_EQ(1u, profiles[completed_profiler].size()); | |
1088 // Stop and destroy all profilers, always in the same order. Don't crash. | |
1089 for (size_t i = 0; i < profilers.size(); ++i) | |
1090 profilers[i]->Stop(); | |
1091 for (size_t i = 0; i < profilers.size(); ++i) | |
1092 profilers[i].reset(); | |
1093 }); | |
1094 } | |
1095 | |
1096 // Checks that sampling requests execute in a staggered manner. | |
1097 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | |
1098 #define MAYBE_ConcurrentProfiling_Staggered ConcurrentProfiling_Staggered | |
1099 #else | |
1100 #define MAYBE_ConcurrentProfiling_Staggered \ | |
1101 DISABLED_ConcurrentProfiling_Staggered | |
1102 #endif | |
1103 TEST(StackSamplingProfilerTest, MAYBE_ConcurrentProfiling_Staggered) { | |
1104 StackSamplingProfiler::TestAPI::DisableIdleShutdown(); | |
1105 | |
1106 WithTargetThread([](PlatformThreadId target_thread_id) { | |
1107 SamplingParams params[3]; | |
1108 params[0].initial_delay = TimeDelta::FromMilliseconds(10); | |
1109 params[0].sampling_interval = TimeDelta::FromMilliseconds(10); | |
888 params[0].samples_per_burst = 1; | 1110 params[0].samples_per_burst = 1; |
889 | 1111 |
890 params[1].sampling_interval = TimeDelta::FromMilliseconds(0); | 1112 params[1].initial_delay = TimeDelta::FromMilliseconds(5); |
891 params[1].samples_per_burst = 1; | 1113 params[1].sampling_interval = TimeDelta::FromMilliseconds(10); |
892 | 1114 params[1].samples_per_burst = 2; |
893 CallStackProfiles profiles[2]; | 1115 |
894 std::vector<std::unique_ptr<WaitableEvent>> sampling_completed(2); | 1116 params[2].initial_delay = TimeDelta::FromMilliseconds(0); |
895 std::vector<std::unique_ptr<StackSamplingProfiler>> profiler(2); | 1117 params[2].sampling_interval = TimeDelta::FromMilliseconds(10); |
896 for (int i = 0; i < 2; ++i) { | 1118 params[2].samples_per_burst = 3; |
897 sampling_completed[i] = | 1119 |
898 MakeUnique<WaitableEvent>(WaitableEvent::ResetPolicy::AUTOMATIC, | 1120 std::vector<CallStackProfiles> profiles; |
899 WaitableEvent::InitialState::NOT_SIGNALED); | 1121 std::vector<std::unique_ptr<StackSamplingProfiler>> profilers; |
900 const StackSamplingProfiler::CompletedCallback callback = | 1122 std::vector<std::unique_ptr<WaitableEvent>> sampling_completed; |
901 Bind(&SaveProfilesAndSignalEvent, Unretained(&profiles[i]), | 1123 CreateProfilers(target_thread_id, params, arraysize(params), &profiles, |
902 Unretained(sampling_completed[i].get())); | 1124 &profilers, &sampling_completed); |
903 profiler[i] = MakeUnique<StackSamplingProfiler>(target_thread_id, | 1125 |
904 params[i], callback); | 1126 profilers[0]->Start(); |
905 } | 1127 profilers[1]->Start(); |
906 | 1128 sampling_completed[0]->Wait(); |
907 profiler[0]->Start(); | 1129 profilers[2]->Start(); |
908 profiler[1]->Start(); | 1130 profilers[0]->Stop(); |
909 | 1131 profilers[1]->Stop(); |
910 std::vector<WaitableEvent*> sampling_completed_rawptrs( | 1132 sampling_completed[1]->Wait(); |
911 sampling_completed.size()); | 1133 sampling_completed[2]->Wait(); |
912 std::transform( | 1134 EXPECT_EQ(1u, profiles[0].size()); |
913 sampling_completed.begin(), sampling_completed.end(), | 1135 EXPECT_EQ(1u, profiles[1].size()); |
914 sampling_completed_rawptrs.begin(), | 1136 EXPECT_EQ(1u, profiles[2].size()); |
915 [](const std::unique_ptr<WaitableEvent>& elem) { return elem.get(); }); | 1137 }); |
916 // Wait for one profiler to finish. | 1138 } |
917 size_t completed_profiler = | 1139 |
918 WaitableEvent::WaitMany(sampling_completed_rawptrs.data(), 2); | |
919 EXPECT_EQ(1u, profiles[completed_profiler].size()); | |
920 | |
921 size_t other_profiler = 1 - completed_profiler; | |
922 // Give the other profiler a chance to run and observe that it hasn't. | |
923 EXPECT_FALSE(sampling_completed[other_profiler]->TimedWait( | |
924 TimeDelta::FromMilliseconds(25))); | |
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 }); | |
931 } | |
932 | |
933 // Checks that a stack that runs through another library produces a stack with | 1140 // Checks that a stack that runs through another library produces a stack with |
934 // the expected functions. | 1141 // the expected functions. |
935 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | 1142 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) |
936 #define MAYBE_OtherLibrary OtherLibrary | 1143 #define MAYBE_OtherLibrary OtherLibrary |
937 #else | 1144 #else |
938 #define MAYBE_OtherLibrary DISABLED_OtherLibrary | 1145 #define MAYBE_OtherLibrary DISABLED_OtherLibrary |
939 #endif | 1146 #endif |
940 TEST(StackSamplingProfilerTest, MAYBE_OtherLibrary) { | 1147 TEST(StackSamplingProfilerTest, MAYBE_OtherLibrary) { |
941 SamplingParams params; | 1148 SamplingParams params; |
942 params.sampling_interval = TimeDelta::FromMilliseconds(0); | 1149 params.sampling_interval = TimeDelta::FromMilliseconds(0); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1017 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) | 1224 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) |
1018 #define MAYBE_UnloadedLibrary UnloadedLibrary | 1225 #define MAYBE_UnloadedLibrary UnloadedLibrary |
1019 #else | 1226 #else |
1020 #define MAYBE_UnloadedLibrary DISABLED_UnloadedLibrary | 1227 #define MAYBE_UnloadedLibrary DISABLED_UnloadedLibrary |
1021 #endif | 1228 #endif |
1022 TEST(StackSamplingProfilerTest, MAYBE_UnloadedLibrary) { | 1229 TEST(StackSamplingProfilerTest, MAYBE_UnloadedLibrary) { |
1023 TestLibraryUnload(true); | 1230 TestLibraryUnload(true); |
1024 } | 1231 } |
1025 | 1232 |
1026 } // namespace base | 1233 } // namespace base |
OLD | NEW |