| 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 <sstream> | |
| 6 | |
| 7 #include "base/bind.h" | 5 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
| 9 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 10 #include "base/profiler/stack_sampling_profiler.h" | 8 #include "base/profiler/stack_sampling_profiler.h" |
| 9 #include "base/strings/stringprintf.h" |
| 11 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| 12 #include "base/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
| 13 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 14 |
| 16 namespace base { | 15 namespace base { |
| 17 | 16 |
| 17 using SamplingParams = StackSamplingProfiler::SamplingParams; |
| 18 using Frame = StackSamplingProfiler::Frame; | 18 using Frame = StackSamplingProfiler::Frame; |
| 19 using Module = StackSamplingProfiler::Module; | 19 using Module = StackSamplingProfiler::Module; |
| 20 using Sample = StackSamplingProfiler::Sample; | 20 using Sample = StackSamplingProfiler::Sample; |
| 21 using Profile = StackSamplingProfiler::Profile; | 21 using CallStackProfile = StackSamplingProfiler::CallStackProfile; |
| 22 using CallStackProfiles = StackSamplingProfiler::CallStackProfiles; |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 25 |
| 24 // A thread to target for profiling, whose stack is guaranteed to contain | 26 // A thread to target for profiling, whose stack is guaranteed to contain |
| 25 // SignalAndWaitUntilSignaled() when coordinated with the main thread. | 27 // SignalAndWaitUntilSignaled() when coordinated with the main thread. |
| 26 class TargetThread : public PlatformThread::Delegate { | 28 class TargetThread : public PlatformThread::Delegate { |
| 27 public: | 29 public: |
| 28 TargetThread(); | 30 TargetThread(); |
| 29 | 31 |
| 30 // Implementation of PlatformThread::Delegate: | 32 // PlatformThread::Delegate: |
| 31 void ThreadMain() override; | 33 void ThreadMain() override; |
| 32 | 34 |
| 33 // Wait for the thread to have started and be executing in | 35 // Waits for the thread to have started and be executing in |
| 34 // SignalAndWaitUntilSignaled(). | 36 // SignalAndWaitUntilSignaled(). |
| 35 void WaitForThreadStart(); | 37 void WaitForThreadStart(); |
| 36 // Allow the thread to return from SignalAndWaitUntilSignaled() and finish | 38 |
| 39 // Allows the thread to return from SignalAndWaitUntilSignaled() and finish |
| 37 // execution. | 40 // execution. |
| 38 void SignalThreadToFinish(); | 41 void SignalThreadToFinish(); |
| 39 | 42 |
| 40 // This function is guaranteed to be executing between calls to | 43 // This function is guaranteed to be executing between calls to |
| 41 // WaitForThreadStart() and SignalThreadToFinish(). | 44 // WaitForThreadStart() and SignalThreadToFinish(). This function is static so |
| 45 // that we can get a straightforward address for it in one of the tests below, |
| 46 // rather than dealing with the complexity of a member function pointer |
| 47 // representation. |
| 42 static void SignalAndWaitUntilSignaled(WaitableEvent* thread_started_event, | 48 static void SignalAndWaitUntilSignaled(WaitableEvent* thread_started_event, |
| 43 WaitableEvent* finish_event); | 49 WaitableEvent* finish_event); |
| 44 | 50 |
| 45 PlatformThreadId id() const { return id_; } | 51 PlatformThreadId id() const { return id_; } |
| 46 | 52 |
| 47 private: | 53 private: |
| 48 WaitableEvent thread_started_event_; | 54 WaitableEvent thread_started_event_; |
| 49 WaitableEvent finish_event_; | 55 WaitableEvent finish_event_; |
| 50 PlatformThreadId id_; | 56 PlatformThreadId id_; |
| 51 | 57 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 74 NOINLINE void TargetThread::SignalAndWaitUntilSignaled( | 80 NOINLINE void TargetThread::SignalAndWaitUntilSignaled( |
| 75 WaitableEvent* thread_started_event, | 81 WaitableEvent* thread_started_event, |
| 76 WaitableEvent* finish_event) { | 82 WaitableEvent* finish_event) { |
| 77 thread_started_event->Signal(); | 83 thread_started_event->Signal(); |
| 78 volatile int x = 1; | 84 volatile int x = 1; |
| 79 finish_event->Wait(); | 85 finish_event->Wait(); |
| 80 x = 0; // Prevent tail call to WaitableEvent::Wait(). | 86 x = 0; // Prevent tail call to WaitableEvent::Wait(). |
| 81 ALLOW_UNUSED_LOCAL(x); | 87 ALLOW_UNUSED_LOCAL(x); |
| 82 } | 88 } |
| 83 | 89 |
| 90 // Called on the profiler thread when complete, to collect profiles. |
| 91 void SaveProfiles(CallStackProfiles* profiles, |
| 92 const CallStackProfiles& pending_profiles) { |
| 93 *profiles = pending_profiles; |
| 94 } |
| 95 |
| 84 // Called on the profiler thread when complete. Collects profiles produced by | 96 // Called on the profiler thread when complete. Collects profiles produced by |
| 85 // the profiler, and signals an event to allow the main thread to know that that | 97 // the profiler, and signals an event to allow the main thread to know that that |
| 86 // the profiler is done. | 98 // the profiler is done. |
| 87 void SaveProfilesAndSignalEvent(std::vector<Profile>* profiles, | 99 void SaveProfilesAndSignalEvent(CallStackProfiles* profiles, |
| 88 WaitableEvent* event, | 100 WaitableEvent* event, |
| 89 const std::vector<Profile>& pending_profiles) { | 101 const CallStackProfiles& pending_profiles) { |
| 90 *profiles = pending_profiles; | 102 *profiles = pending_profiles; |
| 91 event->Signal(); | 103 event->Signal(); |
| 92 } | 104 } |
| 93 | 105 |
| 94 // Captures profiles as specified by |params| on the TargetThread, and returns | 106 // Executes the function with the target thread running and executing within |
| 95 // them in |profiles|. Waits up to |profiler_wait_time| for the profiler to | 107 // SignalAndWaitUntilSignaled(). Performs all necessary target thread startup |
| 96 // complete. | 108 // and shutdown work before and afterward. |
| 97 void CaptureProfiles(const StackSamplingProfiler::SamplingParams& params, | 109 template <class Function> |
| 98 std::vector<Profile>* profiles, | 110 void WithTargetThread(Function function) { |
| 99 TimeDelta profiler_wait_time) { | |
| 100 TargetThread target_thread; | 111 TargetThread target_thread; |
| 101 PlatformThreadHandle target_thread_handle; | 112 PlatformThreadHandle target_thread_handle; |
| 102 EXPECT_TRUE(PlatformThread::Create(0, &target_thread, &target_thread_handle)); | 113 EXPECT_TRUE(PlatformThread::Create(0, &target_thread, &target_thread_handle)); |
| 103 | 114 |
| 104 target_thread.WaitForThreadStart(); | 115 target_thread.WaitForThreadStart(); |
| 105 | 116 |
| 106 WaitableEvent sampling_thread_completed(true, false); | 117 function(target_thread.id()); |
| 107 profiles->clear(); | |
| 108 StackSamplingProfiler profiler(target_thread.id(), params); | |
| 109 profiler.SetCustomCompletedCallback( | |
| 110 Bind(&SaveProfilesAndSignalEvent, Unretained(profiles), | |
| 111 Unretained(&sampling_thread_completed))); | |
| 112 profiler.Start(); | |
| 113 sampling_thread_completed.TimedWait(profiler_wait_time); | |
| 114 profiler.Stop(); | |
| 115 sampling_thread_completed.Wait(); | |
| 116 | 118 |
| 117 target_thread.SignalThreadToFinish(); | 119 target_thread.SignalThreadToFinish(); |
| 118 | 120 |
| 119 PlatformThread::Join(target_thread_handle); | 121 PlatformThread::Join(target_thread_handle); |
| 120 } | 122 } |
| 121 | 123 |
| 124 // Captures profiles as specified by |params| on the TargetThread, and returns |
| 125 // them in |profiles|. Waits up to |profiler_wait_time| for the profiler to |
| 126 // complete. |
| 127 void CaptureProfilesWithObjectCallback(const SamplingParams& params, |
| 128 CallStackProfiles* profiles, |
| 129 TimeDelta profiler_wait_time) { |
| 130 profiles->clear(); |
| 131 |
| 132 WithTargetThread([¶ms, profiles, profiler_wait_time]( |
| 133 PlatformThreadId target_thread_id) { |
| 134 WaitableEvent sampling_thread_completed(true, false); |
| 135 const StackSamplingProfiler::CompletedCallback callback = |
| 136 Bind(&SaveProfilesAndSignalEvent, Unretained(profiles), |
| 137 Unretained(&sampling_thread_completed)); |
| 138 StackSamplingProfiler profiler(target_thread_id, params, callback); |
| 139 profiler.Start(); |
| 140 sampling_thread_completed.TimedWait(profiler_wait_time); |
| 141 profiler.Stop(); |
| 142 sampling_thread_completed.Wait(); |
| 143 }); |
| 144 } |
| 145 |
| 146 // Captures profiles as specified by |params| on the TargetThread, and returns |
| 147 // them in |profiles|. Uses the default callback rather than a per-object |
| 148 // callback. |
| 149 void CaptureProfilesWithDefaultCallback(const SamplingParams& params, |
| 150 CallStackProfiles* profiles) { |
| 151 profiles->clear(); |
| 152 |
| 153 WithTargetThread([¶ms, profiles](PlatformThreadId target_thread_id) { |
| 154 WaitableEvent sampling_thread_completed(false, false); |
| 155 StackSamplingProfiler::SetDefaultCompletedCallback( |
| 156 Bind(&SaveProfilesAndSignalEvent, Unretained(profiles), |
| 157 Unretained(&sampling_thread_completed))); |
| 158 |
| 159 StackSamplingProfiler profiler(target_thread_id, params); |
| 160 profiler.Start(); |
| 161 sampling_thread_completed.Wait(); |
| 162 |
| 163 StackSamplingProfiler::SetDefaultCompletedCallback( |
| 164 StackSamplingProfiler::CompletedCallback()); |
| 165 }); |
| 166 } |
| 167 |
| 168 // Runs the profiler with |params| on the TargetThread, with no default or |
| 169 // per-object callback. |
| 170 void RunProfilerWithNoCallback(const SamplingParams& params, |
| 171 TimeDelta profiler_wait_time) { |
| 172 WithTargetThread([¶ms, profiler_wait_time]( |
| 173 PlatformThreadId target_thread_id) { |
| 174 StackSamplingProfiler profiler(target_thread_id, params); |
| 175 profiler.Start(); |
| 176 // Since we don't specify a callback, we don't have a synchronization |
| 177 // mechanism with the sampling thread. Just sleep instead. |
| 178 PlatformThread::Sleep(profiler_wait_time); |
| 179 profiler.Stop(); |
| 180 }); |
| 181 } |
| 182 |
| 122 // If this executable was linked with /INCREMENTAL (the default for non-official | 183 // If this executable was linked with /INCREMENTAL (the default for non-official |
| 123 // debug and release builds on Windows), function addresses do not correspond to | 184 // debug and release builds on Windows), function addresses do not correspond to |
| 124 // function code itself, but instead to instructions in the Incremental Link | 185 // function code itself, but instead to instructions in the Incremental Link |
| 125 // Table that jump to the functions. Check for a jump instruction and if present | 186 // Table that jump to the functions. Checks for a jump instruction and if |
| 126 // do a little decompilation to find the function's actual starting address. | 187 // present does a little decompilation to find the function's actual starting |
| 188 // address. |
| 127 const void* MaybeFixupFunctionAddressForILT(const void* function_address) { | 189 const void* MaybeFixupFunctionAddressForILT(const void* function_address) { |
| 128 #if defined(_WIN64) | 190 #if defined(_WIN64) |
| 129 const unsigned char* opcode = | 191 const unsigned char* opcode = |
| 130 reinterpret_cast<const unsigned char*>(function_address); | 192 reinterpret_cast<const unsigned char*>(function_address); |
| 131 if (*opcode == 0xe9) { | 193 if (*opcode == 0xe9) { |
| 132 // This is a relative jump instruction. Assume we're in the ILT and compute | 194 // This is a relative jump instruction. Assume we're in the ILT and compute |
| 133 // the function start address from the instruction offset. | 195 // the function start address from the instruction offset. |
| 134 const unsigned char* offset = opcode + 1; | 196 const int32* offset = reinterpret_cast<const int32*>(opcode + 1); |
| 135 const unsigned char* next_instruction = opcode + 5; | 197 const unsigned char* next_instruction = |
| 136 return next_instruction + | 198 reinterpret_cast<const unsigned char*>(offset + 1); |
| 137 static_cast<int64>(*reinterpret_cast<const int32*>(offset)); | 199 return next_instruction + *offset; |
| 138 } | 200 } |
| 139 #endif | 201 #endif |
| 140 return function_address; | 202 return function_address; |
| 141 } | 203 } |
| 142 | 204 |
| 143 // Searches through the frames in |sample|, returning an iterator to the first | 205 // Searches through the frames in |sample|, returning an iterator to the first |
| 144 // frame that has an instruction pointer between |function_address| and | 206 // frame that has an instruction pointer between |function_address| and |
| 145 // |function_address| + |size|. Returns sample.end() if no such frames are | 207 // |function_address| + |size|. Returns sample.end() if no such frames are |
| 146 // found. | 208 // found. |
| 147 Sample::const_iterator FindFirstFrameWithinFunction( | 209 Sample::const_iterator FindFirstFrameWithinFunction( |
| 148 const Sample& sample, | 210 const Sample& sample, |
| 149 const void* function_address, | 211 const void* function_address, |
| 150 int function_size) { | 212 int function_size) { |
| 151 function_address = MaybeFixupFunctionAddressForILT(function_address); | 213 function_address = MaybeFixupFunctionAddressForILT(function_address); |
| 152 for (auto it = sample.begin(); it != sample.end(); ++it) { | 214 for (auto it = sample.begin(); it != sample.end(); ++it) { |
| 153 if ((reinterpret_cast<const unsigned char*>(it->instruction_pointer) >= | 215 if ((it->instruction_pointer >= function_address) && |
| 154 reinterpret_cast<const unsigned char*>(function_address)) && | 216 (it->instruction_pointer < |
| 155 (reinterpret_cast<const unsigned char*>(it->instruction_pointer) < | 217 (static_cast<const unsigned char*>(function_address) + function_size))) |
| 156 (reinterpret_cast<const unsigned char*>(function_address) + | |
| 157 function_size))) | |
| 158 return it; | 218 return it; |
| 159 } | 219 } |
| 160 return sample.end(); | 220 return sample.end(); |
| 161 } | 221 } |
| 162 | 222 |
| 163 // Formats a sample into a string that can be output for test diagnostics. | 223 // Formats a sample into a string that can be output for test diagnostics. |
| 164 std::string FormatSampleForDiagnosticOutput( | 224 std::string FormatSampleForDiagnosticOutput( |
| 165 const Sample& sample, | 225 const Sample& sample, |
| 166 const std::vector<Module>& modules) { | 226 const std::vector<Module>& modules) { |
| 167 std::ostringstream stream; | 227 std::string output; |
| 168 for (const Frame& frame: sample) { | 228 for (const Frame& frame: sample) { |
| 169 stream << frame.instruction_pointer << " " | 229 output += StringPrintf( |
| 170 << modules[frame.module_index].filename.value() << std::endl; | 230 "0x%p %s\n", frame.instruction_pointer, |
| 231 modules[frame.module_index].filename.AsUTF8Unsafe().c_str()); |
| 171 } | 232 } |
| 172 return stream.str(); | 233 return output; |
| 173 } | 234 } |
| 174 | 235 |
| 175 // Returns a duration that is longer than the test timeout. We would use | 236 // Returns a duration that is longer than the test timeout. We would use |
| 176 // TimeDelta::Max() but https://crbug.com/465948. | 237 // TimeDelta::Max() but https://crbug.com/465948. |
| 177 TimeDelta AVeryLongTimeDelta() { return TimeDelta::FromDays(1); } | 238 TimeDelta AVeryLongTimeDelta() { return TimeDelta::FromDays(1); } |
| 239 |
| 178 } // namespace | 240 } // namespace |
| 179 | 241 |
| 180 | 242 |
| 181 // The tests below are enabled for Win x64 only, pending implementation of the | 243 // The tests below are enabled for Win x64 only, pending implementation of the |
| 182 // tested functionality on other platforms/architectures. | 244 // tested functionality on other platforms/architectures. |
| 183 | 245 |
| 184 // Checks that the basic expected information is present in a sampled profile. | 246 // Checks that the basic expected information is present in a sampled call stack |
| 247 // profile. |
| 185 #if defined(_WIN64) | 248 #if defined(_WIN64) |
| 186 #define MAYBE_Basic Basic | 249 #define MAYBE_Basic Basic |
| 187 #else | 250 #else |
| 188 #define MAYBE_Basic DISABLED_Basic | 251 #define MAYBE_Basic DISABLED_Basic |
| 189 #endif | 252 #endif |
| 190 TEST(StackSamplingProfilerTest, MAYBE_Basic) { | 253 TEST(StackSamplingProfilerTest, MAYBE_Basic) { |
| 191 StackSamplingProfiler::SamplingParams params; | 254 SamplingParams params; |
| 192 params.initial_delay = params.burst_interval = params.sampling_interval = | 255 params.sampling_interval = TimeDelta::FromMilliseconds(0); |
| 193 TimeDelta::FromMilliseconds(0); | |
| 194 params.bursts = 1; | |
| 195 params.samples_per_burst = 1; | 256 params.samples_per_burst = 1; |
| 257 params.user_data = 100; |
| 258 params.preserve_sample_ordering = true; |
| 196 | 259 |
| 197 std::vector<Profile> profiles; | 260 std::vector<CallStackProfile> profiles; |
| 198 CaptureProfiles(params, &profiles, AVeryLongTimeDelta()); | 261 CaptureProfilesWithObjectCallback(params, &profiles, AVeryLongTimeDelta()); |
| 199 | 262 |
| 200 // Check that the profile and samples sizes are correct, and the module | 263 // Check that the profile and samples sizes are correct, and the module |
| 201 // indices are in range. | 264 // indices are in range. |
| 202 | |
| 203 ASSERT_EQ(1u, profiles.size()); | 265 ASSERT_EQ(1u, profiles.size()); |
| 204 const Profile& profile = profiles[0]; | 266 const CallStackProfile& profile = profiles[0]; |
| 205 ASSERT_EQ(1u, profile.samples.size()); | 267 ASSERT_EQ(1u, profile.samples.size()); |
| 206 EXPECT_EQ(params.sampling_interval, profile.sampling_period); | 268 EXPECT_EQ(params.sampling_interval, profile.sampling_period); |
| 207 const Sample& sample = profile.samples[0]; | 269 const Sample& sample = profile.samples[0]; |
| 208 for (const auto& frame : sample) { | 270 for (const auto& frame : sample) { |
| 209 ASSERT_GE(frame.module_index, 0); | 271 ASSERT_GE(frame.module_index, 0u); |
| 210 ASSERT_LT(frame.module_index, static_cast<int>(profile.modules.size())); | 272 ASSERT_LT(frame.module_index, profile.modules.size()); |
| 211 } | 273 } |
| 274 EXPECT_EQ(100u, profile.user_data); |
| 275 EXPECT_EQ(true, profile.preserve_sample_ordering); |
| 212 | 276 |
| 213 // Check that the stack contains a frame for | 277 // Check that the stack contains a frame for |
| 214 // TargetThread::SignalAndWaitUntilSignaled() and that the frame has this | 278 // TargetThread::SignalAndWaitUntilSignaled() and that the frame has this |
| 215 // executable's module. | 279 // executable's module. |
| 216 | 280 // |
| 217 // Since we don't have a good way to know the function size, use 100 bytes as | 281 // Since we don't have a good way to know the function size, use 100 bytes as |
| 218 // a reasonable window to locate the instruction pointer. | 282 // a reasonable window to locate the instruction pointer. |
| 219 Sample::const_iterator loc = FindFirstFrameWithinFunction( | 283 Sample::const_iterator loc = FindFirstFrameWithinFunction( |
| 220 sample, | 284 sample, |
| 221 reinterpret_cast<const void*>(&TargetThread::SignalAndWaitUntilSignaled), | 285 reinterpret_cast<const void*>(&TargetThread::SignalAndWaitUntilSignaled), |
| 222 100); | 286 100); |
| 223 ASSERT_TRUE(loc != sample.end()) | 287 ASSERT_TRUE(loc != sample.end()) |
| 224 << "Function at " | 288 << "Function at " |
| 225 << MaybeFixupFunctionAddressForILT( | 289 << MaybeFixupFunctionAddressForILT( |
| 226 reinterpret_cast<const void*>( | 290 reinterpret_cast<const void*>( |
| 227 &TargetThread::SignalAndWaitUntilSignaled)) | 291 &TargetThread::SignalAndWaitUntilSignaled)) |
| 228 << " was not found in stack:" << std::endl | 292 << " was not found in stack:\n" |
| 229 << FormatSampleForDiagnosticOutput(sample, profile.modules); | 293 << FormatSampleForDiagnosticOutput(sample, profile.modules); |
| 230 | |
| 231 FilePath executable_path; | 294 FilePath executable_path; |
| 232 bool got_executable_path = PathService::Get(FILE_EXE, &executable_path); | 295 EXPECT_TRUE(PathService::Get(FILE_EXE, &executable_path)); |
| 233 EXPECT_TRUE(got_executable_path); | |
| 234 EXPECT_EQ(executable_path, profile.modules[loc->module_index].filename); | 296 EXPECT_EQ(executable_path, profile.modules[loc->module_index].filename); |
| 235 } | 297 } |
| 236 | 298 |
| 237 // Checks that the expected number of profiles and samples are present in the | 299 // Checks that the expected number of profiles and samples are present in the |
| 238 // profiles produced. | 300 // call stack profiles produced. |
| 239 #if defined(_WIN64) | 301 #if defined(_WIN64) |
| 240 #define MAYBE_MultipleProfilesAndSamples MultipleProfilesAndSamples | 302 #define MAYBE_MultipleProfilesAndSamples MultipleProfilesAndSamples |
| 241 #else | 303 #else |
| 242 #define MAYBE_MultipleProfilesAndSamples DISABLED_MultipleProfilesAndSamples | 304 #define MAYBE_MultipleProfilesAndSamples DISABLED_MultipleProfilesAndSamples |
| 243 #endif | 305 #endif |
| 244 TEST(StackSamplingProfilerTest, MAYBE_MultipleProfilesAndSamples) { | 306 TEST(StackSamplingProfilerTest, MAYBE_MultipleProfilesAndSamples) { |
| 245 StackSamplingProfiler::SamplingParams params; | 307 SamplingParams params; |
| 246 params.initial_delay = params.burst_interval = params.sampling_interval = | 308 params.burst_interval = params.sampling_interval = |
| 247 TimeDelta::FromMilliseconds(0); | 309 TimeDelta::FromMilliseconds(0); |
| 248 params.bursts = 2; | 310 params.bursts = 2; |
| 249 params.samples_per_burst = 3; | 311 params.samples_per_burst = 3; |
| 250 | 312 |
| 251 std::vector<Profile> profiles; | 313 std::vector<CallStackProfile> profiles; |
| 252 CaptureProfiles(params, &profiles, AVeryLongTimeDelta()); | 314 CaptureProfilesWithObjectCallback(params, &profiles, AVeryLongTimeDelta()); |
| 253 | 315 |
| 254 ASSERT_EQ(2u, profiles.size()); | 316 ASSERT_EQ(2u, profiles.size()); |
| 255 EXPECT_EQ(3u, profiles[0].samples.size()); | 317 EXPECT_EQ(3u, profiles[0].samples.size()); |
| 256 EXPECT_EQ(3u, profiles[1].samples.size()); | 318 EXPECT_EQ(3u, profiles[1].samples.size()); |
| 257 } | 319 } |
| 258 | 320 |
| 259 // Checks that no profiles are captured if the profiling is stopped during the | 321 // Checks that no call stack profiles are captured if the profiling is stopped |
| 260 // initial delay. | 322 // during the initial delay. |
| 261 #if defined(_WIN64) | 323 #if defined(_WIN64) |
| 262 #define MAYBE_StopDuringInitialDelay StopDuringInitialDelay | 324 #define MAYBE_StopDuringInitialDelay StopDuringInitialDelay |
| 263 #else | 325 #else |
| 264 #define MAYBE_StopDuringInitialDelay DISABLED_StopDuringInitialDelay | 326 #define MAYBE_StopDuringInitialDelay DISABLED_StopDuringInitialDelay |
| 265 #endif | 327 #endif |
| 266 TEST(StackSamplingProfilerTest, MAYBE_StopDuringInitialDelay) { | 328 TEST(StackSamplingProfilerTest, MAYBE_StopDuringInitialDelay) { |
| 267 StackSamplingProfiler::SamplingParams params; | 329 SamplingParams params; |
| 268 params.burst_interval = params.sampling_interval = | |
| 269 TimeDelta::FromMilliseconds(0); | |
| 270 params.initial_delay = TimeDelta::FromSeconds(60); | 330 params.initial_delay = TimeDelta::FromSeconds(60); |
| 271 params.bursts = params.samples_per_burst = 1; | |
| 272 | 331 |
| 273 std::vector<Profile> profiles; | 332 std::vector<CallStackProfile> profiles; |
| 274 CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(0)); | 333 CaptureProfilesWithObjectCallback(params, &profiles, |
| 334 TimeDelta::FromMilliseconds(0)); |
| 275 | 335 |
| 276 EXPECT_TRUE(profiles.empty()); | 336 EXPECT_TRUE(profiles.empty()); |
| 277 } | 337 } |
| 278 | 338 |
| 279 // Checks that the single completed profile is captured if the profiling is | 339 // Checks that the single completed call stack profile is captured if the |
| 280 // stopped between bursts. | 340 // profiling is stopped between bursts. |
| 281 #if defined(_WIN64) | 341 #if defined(_WIN64) |
| 282 #define MAYBE_StopDuringInterBurstInterval StopDuringInterBurstInterval | 342 #define MAYBE_StopDuringInterBurstInterval StopDuringInterBurstInterval |
| 283 #else | 343 #else |
| 284 #define MAYBE_StopDuringInterBurstInterval DISABLED_StopDuringInterBurstInterval | 344 #define MAYBE_StopDuringInterBurstInterval DISABLED_StopDuringInterBurstInterval |
| 285 #endif | 345 #endif |
| 286 TEST(StackSamplingProfilerTest, MAYBE_StopDuringInterBurstInterval) { | 346 TEST(StackSamplingProfilerTest, MAYBE_StopDuringInterBurstInterval) { |
| 287 StackSamplingProfiler::SamplingParams params; | 347 SamplingParams params; |
| 288 params.initial_delay = params.sampling_interval = | 348 params.sampling_interval = TimeDelta::FromMilliseconds(0); |
| 289 TimeDelta::FromMilliseconds(0); | |
| 290 params.burst_interval = TimeDelta::FromSeconds(60); | 349 params.burst_interval = TimeDelta::FromSeconds(60); |
| 291 params.bursts = 2; | 350 params.bursts = 2; |
| 292 params.samples_per_burst = 1; | 351 params.samples_per_burst = 1; |
| 293 | 352 |
| 294 std::vector<Profile> profiles; | 353 std::vector<CallStackProfile> profiles; |
| 295 CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(50)); | 354 CaptureProfilesWithObjectCallback(params, &profiles, |
| 355 TimeDelta::FromMilliseconds(50)); |
| 296 | 356 |
| 297 ASSERT_EQ(1u, profiles.size()); | 357 ASSERT_EQ(1u, profiles.size()); |
| 298 EXPECT_EQ(1u, profiles[0].samples.size()); | 358 EXPECT_EQ(1u, profiles[0].samples.size()); |
| 299 } | 359 } |
| 300 | 360 |
| 301 // Checks that only completed profiles are captured. | 361 // Checks that only completed call stack profiles are captured. |
| 302 #if defined(_WIN64) | 362 #if defined(_WIN64) |
| 303 #define MAYBE_StopDuringInterSampleInterval StopDuringInterSampleInterval | 363 #define MAYBE_StopDuringInterSampleInterval StopDuringInterSampleInterval |
| 304 #else | 364 #else |
| 305 #define MAYBE_StopDuringInterSampleInterval \ | 365 #define MAYBE_StopDuringInterSampleInterval \ |
| 306 DISABLED_StopDuringInterSampleInterval | 366 DISABLED_StopDuringInterSampleInterval |
| 307 #endif | 367 #endif |
| 308 TEST(StackSamplingProfilerTest, MAYBE_StopDuringInterSampleInterval) { | 368 TEST(StackSamplingProfilerTest, MAYBE_StopDuringInterSampleInterval) { |
| 309 StackSamplingProfiler::SamplingParams params; | 369 SamplingParams params; |
| 310 params.initial_delay = params.burst_interval = TimeDelta::FromMilliseconds(0); | |
| 311 params.sampling_interval = TimeDelta::FromSeconds(60); | 370 params.sampling_interval = TimeDelta::FromSeconds(60); |
| 312 params.bursts = 1; | |
| 313 params.samples_per_burst = 2; | 371 params.samples_per_burst = 2; |
| 314 | 372 |
| 315 std::vector<Profile> profiles; | 373 std::vector<CallStackProfile> profiles; |
| 316 CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(50)); | 374 CaptureProfilesWithObjectCallback(params, &profiles, |
| 375 TimeDelta::FromMilliseconds(50)); |
| 317 | 376 |
| 318 EXPECT_TRUE(profiles.empty()); | 377 EXPECT_TRUE(profiles.empty()); |
| 319 } | 378 } |
| 320 | 379 |
| 321 } // namespace tracked_objects | 380 // Checks that profiles are captured via the default completed callback. |
| 381 #if defined(_WIN64) |
| 382 #define MAYBE_DefaultCallback DefaultCallback |
| 383 #else |
| 384 #define MAYBE_DefaultCallback DISABLED_DefaultCallback |
| 385 #endif |
| 386 TEST(StackSamplingProfilerTest, MAYBE_DefaultCallback) { |
| 387 SamplingParams params; |
| 388 params.samples_per_burst = 1; |
| 389 |
| 390 CallStackProfiles profiles; |
| 391 CaptureProfilesWithDefaultCallback(params, &profiles); |
| 392 |
| 393 EXPECT_EQ(1u, profiles.size()); |
| 394 EXPECT_EQ(1u, profiles[0].samples.size()); |
| 395 } |
| 396 |
| 397 // Checks that profiles are queued until a default callback is set, then |
| 398 // delivered. |
| 399 #if defined(_WIN64) |
| 400 #define MAYBE_ProfilesQueuedWithNoCallback ProfilesQueuedWithNoCallback |
| 401 #else |
| 402 #define MAYBE_ProfilesQueuedWithNoCallback DISABLED_ProfilesQueuedWithNoCallback |
| 403 #endif |
| 404 TEST(StackSamplingProfilerTest, MAYBE_ProfilesQueuedWithNoCallback) { |
| 405 SamplingParams params; |
| 406 params.samples_per_burst = 1; |
| 407 |
| 408 RunProfilerWithNoCallback(params, TimeDelta::FromMilliseconds(50)); |
| 409 |
| 410 CallStackProfiles profiles; |
| 411 // This should immediately call SaveProfiles on this thread. |
| 412 StackSamplingProfiler::SetDefaultCompletedCallback( |
| 413 Bind(&SaveProfiles, Unretained(&profiles))); |
| 414 EXPECT_EQ(1u, profiles.size()); |
| 415 EXPECT_EQ(1u, profiles[0].samples.size()); |
| 416 StackSamplingProfiler::SetDefaultCompletedCallback( |
| 417 StackSamplingProfiler::CompletedCallback()); |
| 418 } |
| 419 |
| 420 // Checks that we can destroy the profiler while profiling. |
| 421 #if defined(_WIN64) |
| 422 #define MAYBE_DestroyProfilerWhileProfiling DestroyProfilerWhileProfiling |
| 423 #else |
| 424 #define MAYBE_DestroyProfilerWhileProfiling \ |
| 425 DISABLED_DestroyProfilerWhileProfiling |
| 426 #endif |
| 427 TEST(StackSamplingProfilerTest, MAYBE_DestroyProfilerWhileProfiling) { |
| 428 SamplingParams params; |
| 429 params.sampling_interval = TimeDelta::FromMilliseconds(10); |
| 430 |
| 431 CallStackProfiles profiles; |
| 432 WithTargetThread([¶ms, &profiles](PlatformThreadId target_thread_id) { |
| 433 scoped_ptr<StackSamplingProfiler> profiler; |
| 434 profiler.reset(new StackSamplingProfiler( |
| 435 target_thread_id, params, Bind(&SaveProfiles, Unretained(&profiles)))); |
| 436 profiler->Start(); |
| 437 profiler.reset(); |
| 438 |
| 439 // Wait longer than a sample interval to catch any use-after-free actions by |
| 440 // the profiler thread. |
| 441 PlatformThread::Sleep(TimeDelta::FromMilliseconds(50)); |
| 442 }); |
| 443 } |
| 444 |
| 445 } // namespace base |
| OLD | NEW |