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..dcefe0e6c2f4cd20fc9b6d1332893f635b4fea18 100644 |
--- a/base/profiler/stack_sampling_profiler_unittest.cc |
+++ b/base/profiler/stack_sampling_profiler_unittest.cc |
@@ -91,15 +91,16 @@ void SaveProfilesAndSignalEvent(std::vector<Profile>* profiles, |
WaitableEvent* event, |
const std::vector<Profile>& pending_profiles) { |
*profiles = pending_profiles; |
- event->Signal(); |
+ if (event) |
+ 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. |
+// complete. Uses a per-object callback unless |use_default_callback| is true. |
void CaptureProfiles(const StackSamplingProfiler::SamplingParams& params, |
std::vector<Profile>* profiles, |
- TimeDelta profiler_wait_time) { |
+ TimeDelta profiler_wait_time, bool use_default_callback) { |
TargetThread target_thread; |
PlatformThreadHandle target_thread_handle; |
EXPECT_TRUE(PlatformThread::Create(0, &target_thread, &target_thread_handle)); |
@@ -108,18 +109,73 @@ void CaptureProfiles(const StackSamplingProfiler::SamplingParams& params, |
WaitableEvent sampling_thread_completed(true, false); |
profiles->clear(); |
- StackSamplingProfiler profiler(target_thread.id(), params); |
- profiler.SetCustomCompletedCallback( |
+ const StackSamplingProfiler::CompletedCallback callback = |
Bind(&SaveProfilesAndSignalEvent, Unretained(profiles), |
- Unretained(&sampling_thread_completed))); |
- profiler.Start(); |
+ Unretained(&sampling_thread_completed)); |
+ scoped_ptr<StackSamplingProfiler> profiler; |
+ if (use_default_callback) { |
+ StackSamplingProfiler::SetDefaultCompletedCallback(callback); |
+ profiler.reset(new StackSamplingProfiler(target_thread.id(), params)); |
+ } else { |
+ profiler.reset(new StackSamplingProfiler(target_thread.id(), params, |
+ callback)); |
+ } |
+ profiler->Start(); |
sampling_thread_completed.TimedWait(profiler_wait_time); |
- profiler.Stop(); |
+ profiler->Stop(); |
sampling_thread_completed.Wait(); |
target_thread.SignalThreadToFinish(); |
PlatformThread::Join(target_thread_handle); |
+ |
+ if (use_default_callback) { |
+ StackSamplingProfiler::SetDefaultCompletedCallback( |
+ StackSamplingProfiler::CompletedCallback()); |
+ } |
+} |
+ |
+// 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) { |
+ CaptureProfiles(params, profiles, profiler_wait_time, false); |
+} |
+ |
+// Captures profiles as specified by |params| on the TargetThread, and returns |
+// them in |profiles|. Uses the default callback rather than a per-object |
+// callback. Waits up to |profiler_wait_time| for the profiler to complete. |
+void CaptureProfilesWithDefaultCallback( |
+ const StackSamplingProfiler::SamplingParams& params, |
+ std::vector<Profile>* profiles, |
+ TimeDelta profiler_wait_time) { |
+ CaptureProfiles(params, profiles, profiler_wait_time, true); |
+} |
+ |
+// 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) { |
+ TargetThread target_thread; |
+ PlatformThreadHandle target_thread_handle; |
+ EXPECT_TRUE(PlatformThread::Create(0, &target_thread, &target_thread_handle)); |
+ |
+ target_thread.WaitForThreadStart(); |
+ |
+ 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(); |
+ |
+ target_thread.SignalThreadToFinish(); |
+ |
+ PlatformThread::Join(target_thread_handle); |
} |
// If this executable was linked with /INCREMENTAL (the default for non-official |
@@ -198,7 +254,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 +308,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 +330,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 +352,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 +374,56 @@ 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()); |
} |
-} // namespace tracked_objects |
+// 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, AVeryLongTimeDelta()); |
+ |
+ 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 SaveProfilesAndSignalEvent on this thread. |
+ StackSamplingProfiler::SetDefaultCompletedCallback( |
+ Bind(&SaveProfilesAndSignalEvent, Unretained(&profiles), nullptr)); |
+ EXPECT_EQ(1u, profiles.size()); |
+ EXPECT_EQ(1u, profiles[0].samples.size()); |
+ StackSamplingProfiler::SetDefaultCompletedCallback( |
+ StackSamplingProfiler::CompletedCallback()); |
+} |
+ |
+} // namespace base |