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 dfccab4e1a893ae7da9c768f0e5c17aef4f9b692..f67941c8de50e0317f6cb2f6c4fa353d969f1c2c 100644 |
--- a/base/profiler/stack_sampling_profiler_unittest.cc |
+++ b/base/profiler/stack_sampling_profiler_unittest.cc |
@@ -84,6 +84,12 @@ void TargetThread::SignalAndWaitUntilSignaled( |
#pragma optimize("", on) |
#endif |
+// Called on the profiler thread when complete, to collect profiles. |
+void SaveProfiles(std::vector<Profile>* profiles, |
+ const std::vector<Profile>& pending_profiles) { |
+ *profiles = pending_profiles; |
+} |
+ |
// Called on the profiler thread when complete. Collects profiles produced by |
// the profiler, and signals an event to allow the main thread to know that that |
// the profiler is done. |
@@ -94,34 +100,86 @@ void SaveProfilesAndSignalEvent(std::vector<Profile>* profiles, |
event->Signal(); |
} |
-// Captures profiles as specified by |params| on the TargetThread, and returns |
-// them in |profiles|. Waits up to |profiler_wait_time| for the profiler to |
-// complete. |
-void CaptureProfiles(const StackSamplingProfiler::SamplingParams& params, |
- std::vector<Profile>* profiles, |
- TimeDelta profiler_wait_time) { |
+// Starts the target thread and ensures it's in SignalAndWaitUntilSignaled(), |
+// runs the provided function passing the thread ID, then stops and joins the |
+// target thread. |
+template <class Function> |
+void WithTargetThread(Function function) { |
TargetThread target_thread; |
PlatformThreadHandle target_thread_handle; |
EXPECT_TRUE(PlatformThread::Create(0, &target_thread, &target_thread_handle)); |
target_thread.WaitForThreadStart(); |
- WaitableEvent sampling_thread_completed(true, false); |
- profiles->clear(); |
- StackSamplingProfiler profiler(target_thread.id(), params); |
- profiler.SetCustomCompletedCallback( |
- Bind(&SaveProfilesAndSignalEvent, Unretained(profiles), |
- Unretained(&sampling_thread_completed))); |
- profiler.Start(); |
- sampling_thread_completed.TimedWait(profiler_wait_time); |
- profiler.Stop(); |
- sampling_thread_completed.Wait(); |
+ function(target_thread.id()); |
target_thread.SignalThreadToFinish(); |
PlatformThread::Join(target_thread_handle); |
} |
+// Captures profiles as specified by |params| on the TargetThread, and returns |
+// them in |profiles|. Waits up to |profiler_wait_time| for the profiler to |
+// complete. |
+void CaptureProfilesWithObjectCallback( |
+ const StackSamplingProfiler::SamplingParams& params, |
+ std::vector<Profile>* profiles, |
+ TimeDelta profiler_wait_time) { |
+ profiles->clear(); |
+ |
+ WithTargetThread([¶ms, profiles, profiler_wait_time]( |
+ PlatformThreadId target_thread_id) { |
+ WaitableEvent sampling_thread_completed(true, false); |
+ const StackSamplingProfiler::CompletedCallback callback = |
+ Bind(&SaveProfilesAndSignalEvent, Unretained(profiles), |
+ Unretained(&sampling_thread_completed)); |
+ StackSamplingProfiler profiler(target_thread_id, params, callback); |
+ profiler.Start(); |
+ sampling_thread_completed.TimedWait(profiler_wait_time); |
+ profiler.Stop(); |
+ sampling_thread_completed.Wait(); |
+ }); |
+} |
+ |
+// Captures profiles as specified by |params| on the TargetThread, and returns |
+// them in |profiles|. Uses the default callback rather than a per-object |
+// callback. |
+void CaptureProfilesWithDefaultCallback( |
+ const StackSamplingProfiler::SamplingParams& params, |
+ std::vector<Profile>* profiles) { |
+ profiles->clear(); |
+ |
+ WithTargetThread([¶ms, profiles](PlatformThreadId target_thread_id) { |
+ WaitableEvent sampling_thread_completed(false, false); |
+ StackSamplingProfiler::SetDefaultCompletedCallback( |
+ Bind(&SaveProfilesAndSignalEvent, Unretained(profiles), |
+ Unretained(&sampling_thread_completed))); |
+ |
+ StackSamplingProfiler profiler(target_thread_id, params); |
+ profiler.Start(); |
+ sampling_thread_completed.Wait(); |
+ |
+ StackSamplingProfiler::SetDefaultCompletedCallback( |
+ StackSamplingProfiler::CompletedCallback()); |
+ }); |
+} |
+ |
+// Runs the profiler with |params| on the TargetThread, with no default or |
+// per-object callback. |
+void RunProfilerWithNoCallback( |
+ const StackSamplingProfiler::SamplingParams& params, |
+ TimeDelta profiler_wait_time) { |
+ WithTargetThread([¶ms, profiler_wait_time]( |
+ PlatformThreadId target_thread_id) { |
+ StackSamplingProfiler profiler(target_thread_id, params); |
+ profiler.Start(); |
+ // Since we don't specify a callback, we don't have a synchronization |
+ // mechanism with the sampling thread. Just sleep instead. |
+ PlatformThread::Sleep(profiler_wait_time); |
+ profiler.Stop(); |
+ }); |
+} |
+ |
// If this executable was linked with /INCREMENTAL (the default for non-official |
// debug and release builds on Windows), function addresses do not correspond to |
// function code itself, but instead to instructions in the Incremental Link |
@@ -198,7 +256,7 @@ TEST(StackSamplingProfilerTest, MAYBE_Basic) { |
params.samples_per_burst = 1; |
std::vector<Profile> profiles; |
- CaptureProfiles(params, &profiles, AVeryLongTimeDelta()); |
+ CaptureProfilesWithObjectCallback(params, &profiles, AVeryLongTimeDelta()); |
// Check that the profile and samples sizes are correct, and the module |
// indices are in range. |
@@ -252,7 +310,7 @@ TEST(StackSamplingProfilerTest, MAYBE_MultipleProfilesAndSamples) { |
params.samples_per_burst = 3; |
std::vector<Profile> profiles; |
- CaptureProfiles(params, &profiles, AVeryLongTimeDelta()); |
+ CaptureProfilesWithObjectCallback(params, &profiles, AVeryLongTimeDelta()); |
ASSERT_EQ(2u, profiles.size()); |
EXPECT_EQ(3u, profiles[0].samples.size()); |
@@ -274,7 +332,8 @@ TEST(StackSamplingProfilerTest, MAYBE_StopDuringInitialDelay) { |
params.bursts = params.samples_per_burst = 1; |
std::vector<Profile> profiles; |
- CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(0)); |
+ CaptureProfilesWithObjectCallback(params, &profiles, |
+ TimeDelta::FromMilliseconds(0)); |
EXPECT_TRUE(profiles.empty()); |
} |
@@ -295,7 +354,8 @@ TEST(StackSamplingProfilerTest, MAYBE_StopDuringInterBurstInterval) { |
params.samples_per_burst = 1; |
std::vector<Profile> profiles; |
- CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(50)); |
+ CaptureProfilesWithObjectCallback(params, &profiles, |
+ TimeDelta::FromMilliseconds(50)); |
ASSERT_EQ(1u, profiles.size()); |
EXPECT_EQ(1u, profiles[0].samples.size()); |
@@ -316,9 +376,87 @@ TEST(StackSamplingProfilerTest, MAYBE_StopDuringInterSampleInterval) { |
params.samples_per_burst = 2; |
std::vector<Profile> profiles; |
- CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(50)); |
+ CaptureProfilesWithObjectCallback(params, &profiles, |
+ TimeDelta::FromMilliseconds(50)); |
+ |
+ EXPECT_TRUE(profiles.empty()); |
+} |
+ |
+// Checks that profiles are captured via the default completed callback. |
+#if defined(_WIN64) |
+#define MAYBE_DefaultCallback DefaultCallback |
+#else |
+#define MAYBE_DefaultCallback DISABLED_DefaultCallback |
+#endif |
+TEST(StackSamplingProfilerTest, MAYBE_DefaultCallback) { |
+ StackSamplingProfiler::SamplingParams params; |
+ params.initial_delay = params.burst_interval = params.sampling_interval = |
+ TimeDelta::FromMilliseconds(0); |
+ params.bursts = 1; |
+ params.samples_per_burst = 1; |
+ |
+ std::vector<Profile> profiles; |
+ CaptureProfilesWithDefaultCallback(params, &profiles); |
+ |
+ EXPECT_EQ(1u, profiles.size()); |
+ EXPECT_EQ(1u, profiles[0].samples.size()); |
+} |
+ |
+// Checks that profiles are queued until a default callback is set, then |
+// delivered. |
+#if defined(_WIN64) |
+#define MAYBE_ProfilesQueuedWithNoCallback ProfilesQueuedWithNoCallback |
+#else |
+#define MAYBE_ProfilesQueuedWithNoCallback DISABLED_ProfilesQueuedWithNoCallback |
+#endif |
+TEST(StackSamplingProfilerTest, MAYBE_ProfilesQueuedWithNoCallback) { |
+ StackSamplingProfiler::SamplingParams params; |
+ params.initial_delay = params.burst_interval = params.sampling_interval = |
+ TimeDelta::FromMilliseconds(0); |
+ params.bursts = 1; |
+ params.samples_per_burst = 1; |
+ |
+ RunProfilerWithNoCallback(params, TimeDelta::FromMilliseconds(50)); |
+ |
+ std::vector<Profile> profiles; |
+ // This should immediately call SaveProfiles on this thread. |
+ StackSamplingProfiler::SetDefaultCompletedCallback( |
+ Bind(&SaveProfiles, Unretained(&profiles))); |
+ EXPECT_EQ(1u, profiles.size()); |
+ EXPECT_EQ(1u, profiles[0].samples.size()); |
+ StackSamplingProfiler::SetDefaultCompletedCallback( |
+ StackSamplingProfiler::CompletedCallback()); |
+} |
+ |
+// Checks that we can destroy the profiler while profiling. |
+#if defined(_WIN64) |
+#define MAYBE_DestroyProfilerWhileProfiling DestroyProfilerWhileProfiling |
+#else |
+#define MAYBE_DestroyProfilerWhileProfiling \ |
+ DISABLED_DestroyProfilerWhileProfiling |
+#endif |
+TEST(StackSamplingProfilerTest, MAYBE_DestroyProfilerWhileProfiling) { |
+ StackSamplingProfiler::SamplingParams params; |
+ params.initial_delay = params.burst_interval = TimeDelta::FromMilliseconds(0); |
+ params.sampling_interval = TimeDelta::FromMilliseconds(10); |
+ params.bursts = 1; |
+ params.samples_per_burst = 300; |
+ |
+ std::vector<Profile> profiles; |
+ WithTargetThread([¶ms, &profiles](PlatformThreadId target_thread_id) { |
+ scoped_ptr<StackSamplingProfiler> profiler; |
+ profiler.reset(new StackSamplingProfiler(target_thread_id, params, |
+ Bind(&SaveProfiles, |
+ Unretained(&profiles)))); |
+ profiler->Start(); |
+ profiler.reset(); |
+ |
+ // Wait longer than a sample interval to catch any use-after-free actions by |
+ // the profiler thread. |
+ PlatformThread::Sleep(TimeDelta::FromMilliseconds(50)); |
+ }); |
EXPECT_TRUE(profiles.empty()); |
} |
-} // namespace tracked_objects |
+} // namespace base |