Index: base/profiler/stack_sampling_profiler_unittest.cc |
diff --git a/base/profiler/stack_sampling_profiler_unittest.cc b/base/profiler/stack_sampling_profiler_unittest.cc |
index 16205ac2b09f22e25975cda252c8306ba6eb918a..260a75eb826ab33a9cf169bbd9082a102f70fffe 100644 |
--- a/base/profiler/stack_sampling_profiler_unittest.cc |
+++ b/base/profiler/stack_sampling_profiler_unittest.cc |
@@ -623,6 +623,18 @@ TEST(StackSamplingProfilerTest, MAYBE_Basic) { |
FilePath executable_path; |
EXPECT_TRUE(PathService::Get(FILE_EXE, &executable_path)); |
EXPECT_EQ(executable_path, profile.modules[loc->module_index].filename); |
+ |
+ // Ensure the sampling thread is still running at this point. |
+ ASSERT_TRUE(StackSamplingProfiler::IsSamplingThreadRunningForTesting()); |
+ |
+ // Shutdown the sampler as though the process were about to exit. |
+ StackSamplingProfiler::Shutdown(); |
+ EXPECT_FALSE(StackSamplingProfiler::IsSamplingThreadRunningForTesting()); |
+ |
+ // Shutdown is a permanent thing in general but this needs to be undone so |
+ // other tests can run. |
+ StackSamplingProfiler::UndoShutdownForTesting(); |
+ EXPECT_FALSE(StackSamplingProfiler::IsSamplingThreadRunningForTesting()); |
} |
// Checks that annotations are recorded in samples. |
@@ -853,6 +865,51 @@ TEST(StackSamplingProfilerTest, MAYBE_DestroyProfilerWhileProfiling) { |
}); |
} |
+// Checks that we can destroy the thread under test while profiling. |
+#if defined(STACK_SAMPLING_PROFILER_SUPPORTED) |
+#define MAYBE_DestroyThreadWhileProfiling DestroyThreadWhileProfiling |
+#else |
+#define MAYBE_DestroyThreadWhileProfiling DISABLED_DestroyThreadWhileProfiling |
+#endif |
+TEST(StackSamplingProfilerTest, MAYBE_DestroyThreadWhileProfiling) { |
+ // Set up a thread for testing. |
+ StackConfiguration stack_config(StackConfiguration::NORMAL); |
+ TargetThread target_thread(stack_config); |
+ PlatformThreadHandle target_thread_handle; |
+ ASSERT_TRUE(PlatformThread::Create(0, &target_thread, &target_thread_handle)); |
+ |
+ // Set up the sampler: 10 samples, 1 second apart |
+ SamplingParams params; |
+ params.sampling_interval = TimeDelta::FromSeconds(1); |
+ params.samples_per_burst = 10; |
+ std::vector<CallStackProfile> profiles; |
+ WaitableEvent sampling_thread_completed( |
+ WaitableEvent::ResetPolicy::MANUAL, |
+ WaitableEvent::InitialState::NOT_SIGNALED); |
+ const StackSamplingProfiler::CompletedCallback callback = |
+ Bind(&SaveProfilesAndSignalEvent, Unretained(&profiles), |
+ Unretained(&sampling_thread_completed)); |
+ |
+ // Start the thread and the sampler and wait a short while. |
+ target_thread.WaitForThreadStart(); |
+ StackSamplingProfiler profiler(target_thread.id(), params, callback); |
+ profiler.Start(); |
+ |
+ // Delay a short while so some samples will get collected. |
+ 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.
|
+ |
+ // Stop the target thread. |
+ target_thread.SignalThreadToFinish(); |
+ PlatformThread::Join(target_thread_handle); |
+ |
+ // Wait a bit longer and ensure that the sampler completes. |
+ PlatformThread::Sleep(TimeDelta::FromSeconds(2)); |
+ |
+ ASSERT_EQ(1U, profiles.size()); |
+ EXPECT_LT(1U, profiles[0].samples.size()); |
+ EXPECT_GT(9U, profiles[0].samples.size()); |
+} |
+ |
// Checks that the same profiler may be run multiple times. |
#if defined(STACK_SAMPLING_PROFILER_SUPPORTED) |
#define MAYBE_CanRunMultipleTimes CanRunMultipleTimes |
@@ -860,6 +917,8 @@ TEST(StackSamplingProfilerTest, MAYBE_DestroyProfilerWhileProfiling) { |
#define MAYBE_CanRunMultipleTimes DISABLED_CanRunMultipleTimes |
#endif |
TEST(StackSamplingProfilerTest, MAYBE_CanRunMultipleTimes) { |
+ StackSamplingProfiler::SetSamplingThreadIdleShutdownTimeForTesting(0); |
+ |
SamplingParams params; |
params.sampling_interval = TimeDelta::FromMilliseconds(0); |
params.samples_per_burst = 1; |
@@ -871,6 +930,21 @@ TEST(StackSamplingProfilerTest, MAYBE_CanRunMultipleTimes) { |
profiles.clear(); |
CaptureProfiles(params, AVeryLongTimeDelta(), &profiles); |
ASSERT_EQ(1u, profiles.size()); |
+ |
+ // Capture thread should still be running at this point. |
+ ASSERT_TRUE(StackSamplingProfiler::IsSamplingThreadRunningForTesting()); |
+ |
+ // Initiate an "idle" shutdown. The task will be run immediately but on |
+ // another thread so wait for it to complete. |
+ StackSamplingProfiler::InitiateSamplingThreadIdleShutdownForTesting(); |
+ while (StackSamplingProfiler::IsSamplingThreadRunningForTesting()) |
+ PlatformThread::YieldCurrentThread(); |
+ |
+ // Ensure another capture will start the sampling thread and run. |
+ profiles.clear(); |
+ CaptureProfiles(params, AVeryLongTimeDelta(), &profiles); |
+ ASSERT_EQ(1u, profiles.size()); |
+ EXPECT_TRUE(StackSamplingProfiler::IsSamplingThreadRunningForTesting()); |
} |
// Checks that requests to start profiling while another profile is taking place |
@@ -884,11 +958,12 @@ TEST(StackSamplingProfilerTest, MAYBE_ConcurrentProfiling) { |
WithTargetThread([](PlatformThreadId target_thread_id) { |
SamplingParams params[2]; |
params[0].initial_delay = TimeDelta::FromMilliseconds(10); |
- params[0].sampling_interval = TimeDelta::FromMilliseconds(0); |
- params[0].samples_per_burst = 1; |
+ params[0].sampling_interval = TimeDelta::FromMilliseconds(1); |
+ params[0].samples_per_burst = 10; |
- params[1].sampling_interval = TimeDelta::FromMilliseconds(0); |
- params[1].samples_per_burst = 1; |
+ params[0].initial_delay = TimeDelta::FromMilliseconds(10); |
+ params[1].sampling_interval = TimeDelta::FromMilliseconds(1); |
+ params[1].samples_per_burst = 10; |
CallStackProfiles profiles[2]; |
std::vector<std::unique_ptr<WaitableEvent>> sampling_completed(2); |
@@ -919,14 +994,9 @@ TEST(StackSamplingProfilerTest, MAYBE_ConcurrentProfiling) { |
EXPECT_EQ(1u, profiles[completed_profiler].size()); |
size_t other_profiler = 1 - completed_profiler; |
- // Give the other profiler a chance to run and observe that it hasn't. |
- EXPECT_FALSE(sampling_completed[other_profiler]->TimedWait( |
- TimeDelta::FromMilliseconds(25))); |
- |
- // Start the other profiler again and it should run. |
- profiler[other_profiler]->Start(); |
- sampling_completed[other_profiler]->Wait(); |
- EXPECT_EQ(1u, profiles[other_profiler].size()); |
+ // Give the other profiler a chance to finish and verify it does no. |
+ EXPECT_TRUE(sampling_completed[other_profiler]->TimedWait( |
+ TimeDelta::FromMilliseconds(250))); |
}); |
} |