OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <sstream> |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" |
| 9 #include "base/path_service.h" |
| 10 #include "base/profiler/stack_sampling_profiler.h" |
| 11 #include "base/synchronization/waitable_event.h" |
| 12 #include "base/threading/platform_thread.h" |
| 13 #include "base/time/time.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace base { |
| 17 |
| 18 using Frame = StackSamplingProfiler::Frame; |
| 19 using Module = StackSamplingProfiler::Module; |
| 20 using Sample = StackSamplingProfiler::Sample; |
| 21 using Profile = StackSamplingProfiler::Profile; |
| 22 |
| 23 namespace { |
| 24 // A thread to target for profiling, whose stack is guaranteed to contain |
| 25 // SignalAndWaitUntilSignaled() when coordinated with the main thread. |
| 26 class TargetThread : public PlatformThread::Delegate { |
| 27 public: |
| 28 TargetThread(); |
| 29 |
| 30 // Implementation of PlatformThread::Delegate: |
| 31 void ThreadMain() override; |
| 32 |
| 33 // Wait for the thread to have started and be executing in |
| 34 // SignalAndWaitUntilSignaled(). |
| 35 void WaitForThreadStart(); |
| 36 // Allow the thread to return from SignalAndWaitUntilSignaled() and finish |
| 37 // execution. |
| 38 void SignalThreadToFinish(); |
| 39 |
| 40 // This function is guaranteed to be executing between calls to |
| 41 // WaitForThreadStart() and SignalThreadToFinish(). |
| 42 static void SignalAndWaitUntilSignaled(WaitableEvent* thread_started_event, |
| 43 WaitableEvent* finish_event); |
| 44 |
| 45 PlatformThreadId id() const { return id_; } |
| 46 |
| 47 private: |
| 48 WaitableEvent thread_started_event_; |
| 49 WaitableEvent finish_event_; |
| 50 PlatformThreadId id_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(TargetThread); |
| 53 }; |
| 54 |
| 55 TargetThread::TargetThread() |
| 56 : thread_started_event_(false, false), finish_event_(false, false), |
| 57 id_(0) {} |
| 58 |
| 59 void TargetThread::ThreadMain() { |
| 60 id_ = PlatformThread::CurrentId(); |
| 61 SignalAndWaitUntilSignaled(&thread_started_event_, &finish_event_); |
| 62 } |
| 63 |
| 64 void TargetThread::WaitForThreadStart() { |
| 65 thread_started_event_.Wait(); |
| 66 } |
| 67 |
| 68 void TargetThread::SignalThreadToFinish() { |
| 69 finish_event_.Signal(); |
| 70 } |
| 71 |
| 72 // static |
| 73 #if defined(_WIN64) |
| 74 // Disable optimizations for this function so that it gets its own stack frame. |
| 75 #pragma optimize("", off) |
| 76 #endif |
| 77 void TargetThread::SignalAndWaitUntilSignaled( |
| 78 WaitableEvent* thread_started_event, |
| 79 WaitableEvent* finish_event) { |
| 80 thread_started_event->Signal(); |
| 81 finish_event->Wait(); |
| 82 } |
| 83 #if defined(_WIN64) |
| 84 #pragma optimize("", on) |
| 85 #endif |
| 86 |
| 87 // Called on the profiler thread when complete. Collects profiles produced by |
| 88 // the profiler, and signals an event to allow the main thread to know that that |
| 89 // the profiler is done. |
| 90 void SaveProfilesAndSignalEvent(std::vector<Profile>* profiles, |
| 91 WaitableEvent* event, |
| 92 const std::vector<Profile>& pending_profiles) { |
| 93 *profiles = pending_profiles; |
| 94 event->Signal(); |
| 95 } |
| 96 |
| 97 // Captures profiles as specified by |params| on the TargetThread, and returns |
| 98 // them in |profiles|. Waits up to |profiler_wait_time| for the profiler to |
| 99 // complete. |
| 100 void CaptureProfiles(const StackSamplingProfiler::SamplingParams& params, |
| 101 std::vector<Profile>* profiles, |
| 102 TimeDelta profiler_wait_time) { |
| 103 TargetThread target_thread; |
| 104 PlatformThreadHandle target_thread_handle; |
| 105 EXPECT_TRUE(PlatformThread::Create(0, &target_thread, &target_thread_handle)); |
| 106 |
| 107 target_thread.WaitForThreadStart(); |
| 108 |
| 109 WaitableEvent sampling_thread_completed(true, false); |
| 110 profiles->clear(); |
| 111 StackSamplingProfiler profiler(target_thread.id(), params); |
| 112 profiler.SetCustomCompletedCallback( |
| 113 Bind(&SaveProfilesAndSignalEvent, Unretained(profiles), |
| 114 Unretained(&sampling_thread_completed))); |
| 115 profiler.Start(); |
| 116 sampling_thread_completed.TimedWait(profiler_wait_time); |
| 117 profiler.Stop(); |
| 118 sampling_thread_completed.Wait(); |
| 119 |
| 120 target_thread.SignalThreadToFinish(); |
| 121 |
| 122 PlatformThread::Join(target_thread_handle); |
| 123 } |
| 124 |
| 125 // If this executable was linked with /INCREMENTAL (the default for non-official |
| 126 // debug and release builds on Windows), function addresses do not correspond to |
| 127 // function code itself, but instead to instructions in the Incremental Link |
| 128 // Table that jump to the functions. Check for a jump instruction and if present |
| 129 // do a little decompilation to find the function's actual starting address. |
| 130 const void* MaybeFixupFunctionAddressForILT(const void* function_address) { |
| 131 #if defined(_WIN64) |
| 132 const unsigned char* opcode = |
| 133 reinterpret_cast<const unsigned char*>(function_address); |
| 134 if (*opcode == 0xe9) { |
| 135 // This is a relative jump instruction. Assume we're in the ILT and compute |
| 136 // the function start address from the instruction offset. |
| 137 const unsigned char* offset = opcode + 1; |
| 138 const unsigned char* next_instruction = opcode + 5; |
| 139 return next_instruction + |
| 140 static_cast<int64>(*reinterpret_cast<const int32*>(offset)); |
| 141 } |
| 142 #endif |
| 143 return function_address; |
| 144 } |
| 145 |
| 146 // Searches through the frames in |sample|, returning an iterator to the first |
| 147 // frame that has an instruction pointer between |function_address| and |
| 148 // |function_address| + |size|. Returns sample.end() if no such frames are |
| 149 // found. |
| 150 Sample::const_iterator FindFirstFrameWithinFunction( |
| 151 const Sample& sample, |
| 152 const void* function_address, |
| 153 int function_size) { |
| 154 function_address = MaybeFixupFunctionAddressForILT(function_address); |
| 155 for (auto it = sample.begin(); it != sample.end(); ++it) { |
| 156 if ((reinterpret_cast<const unsigned char*>(it->instruction_pointer) >= |
| 157 reinterpret_cast<const unsigned char*>(function_address)) && |
| 158 (reinterpret_cast<const unsigned char*>(it->instruction_pointer) < |
| 159 (reinterpret_cast<const unsigned char*>(function_address) + |
| 160 function_size))) |
| 161 return it; |
| 162 } |
| 163 return sample.end(); |
| 164 } |
| 165 |
| 166 // Formats a sample into a string that can be output for test diagnostics. |
| 167 std::string FormatSampleForDiagnosticOutput( |
| 168 const Sample& sample, |
| 169 const std::vector<Module>& modules) { |
| 170 std::ostringstream stream; |
| 171 for (const Frame& frame: sample) { |
| 172 stream << frame.instruction_pointer << " " |
| 173 << modules[frame.module_index].filename.value() << std::endl; |
| 174 } |
| 175 return stream.str(); |
| 176 } |
| 177 |
| 178 // Returns a duration that is longer than the test timeout. We would use |
| 179 // TimeDelta::Max() but https://crbug.com/465948. |
| 180 TimeDelta AVeryLongTimeDelta() { return TimeDelta::FromDays(1); } |
| 181 } // namespace |
| 182 |
| 183 |
| 184 // The tests below are enabled for Win x64 only, pending implementation of the |
| 185 // tested functionality on other platforms/architectures. |
| 186 |
| 187 // Checks that the basic expected information is present in a sampled profile. |
| 188 #if defined(_WIN64) |
| 189 #define MAYBE_Basic Basic |
| 190 #else |
| 191 #define MAYBE_Basic DISABLED_Basic |
| 192 #endif |
| 193 TEST(StackSamplingProfilerTest, MAYBE_Basic) { |
| 194 StackSamplingProfiler::SamplingParams params; |
| 195 params.initial_delay = params.burst_interval = params.sampling_interval = |
| 196 TimeDelta::FromMilliseconds(0); |
| 197 params.bursts = 1; |
| 198 params.samples_per_burst = 1; |
| 199 |
| 200 std::vector<Profile> profiles; |
| 201 CaptureProfiles(params, &profiles, AVeryLongTimeDelta()); |
| 202 |
| 203 // Check that the profile and samples sizes are correct, and the module |
| 204 // indices are in range. |
| 205 |
| 206 ASSERT_EQ(1u, profiles.size()); |
| 207 const Profile& profile = profiles[0]; |
| 208 ASSERT_EQ(1u, profile.samples.size()); |
| 209 EXPECT_EQ(params.sampling_interval, profile.sampling_period); |
| 210 const Sample& sample = profile.samples[0]; |
| 211 for (const auto& frame : sample) { |
| 212 ASSERT_GE(frame.module_index, 0); |
| 213 ASSERT_LT(frame.module_index, static_cast<int>(profile.modules.size())); |
| 214 } |
| 215 |
| 216 // Check that the stack contains a frame for |
| 217 // TargetThread::SignalAndWaitUntilSignaled() and that the frame has this |
| 218 // executable's module. |
| 219 |
| 220 // Since we don't have a good way to know the function size, use 100 bytes as |
| 221 // a reasonable window to locate the instruction pointer. |
| 222 Sample::const_iterator loc = FindFirstFrameWithinFunction( |
| 223 sample, |
| 224 reinterpret_cast<const void*>(&TargetThread::SignalAndWaitUntilSignaled), |
| 225 100); |
| 226 ASSERT_TRUE(loc != sample.end()) |
| 227 << "Function at " |
| 228 << MaybeFixupFunctionAddressForILT( |
| 229 reinterpret_cast<const void*>( |
| 230 &TargetThread::SignalAndWaitUntilSignaled)) |
| 231 << " was not found in stack:" << std::endl |
| 232 << FormatSampleForDiagnosticOutput(sample, profile.modules); |
| 233 |
| 234 FilePath executable_path; |
| 235 bool got_executable_path = PathService::Get(FILE_EXE, &executable_path); |
| 236 EXPECT_TRUE(got_executable_path); |
| 237 EXPECT_EQ(executable_path, profile.modules[loc->module_index].filename); |
| 238 } |
| 239 |
| 240 // Checks that the expected number of profiles and samples are present in the |
| 241 // profiles produced. |
| 242 #if defined(_WIN64) |
| 243 #define MAYBE_MultipleProfilesAndSamples MultipleProfilesAndSamples |
| 244 #else |
| 245 #define MAYBE_MultipleProfilesAndSamples DISABLED_MultipleProfilesAndSamples |
| 246 #endif |
| 247 TEST(StackSamplingProfilerTest, MAYBE_MultipleProfilesAndSamples) { |
| 248 StackSamplingProfiler::SamplingParams params; |
| 249 params.initial_delay = params.burst_interval = params.sampling_interval = |
| 250 TimeDelta::FromMilliseconds(0); |
| 251 params.bursts = 2; |
| 252 params.samples_per_burst = 3; |
| 253 |
| 254 std::vector<Profile> profiles; |
| 255 CaptureProfiles(params, &profiles, AVeryLongTimeDelta()); |
| 256 |
| 257 ASSERT_EQ(2u, profiles.size()); |
| 258 EXPECT_EQ(3u, profiles[0].samples.size()); |
| 259 EXPECT_EQ(3u, profiles[1].samples.size()); |
| 260 } |
| 261 |
| 262 // Checks that no profiles are captured if the profiling is stopped during the |
| 263 // initial delay. |
| 264 #if defined(_WIN64) |
| 265 #define MAYBE_StopDuringInitialDelay StopDuringInitialDelay |
| 266 #else |
| 267 #define MAYBE_StopDuringInitialDelay DISABLED_StopDuringInitialDelay |
| 268 #endif |
| 269 TEST(StackSamplingProfilerTest, MAYBE_StopDuringInitialDelay) { |
| 270 StackSamplingProfiler::SamplingParams params; |
| 271 params.burst_interval = params.sampling_interval = |
| 272 TimeDelta::FromMilliseconds(0); |
| 273 params.initial_delay = TimeDelta::FromSeconds(60); |
| 274 params.bursts = params.samples_per_burst = 1; |
| 275 |
| 276 std::vector<Profile> profiles; |
| 277 CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(0)); |
| 278 |
| 279 EXPECT_TRUE(profiles.empty()); |
| 280 } |
| 281 |
| 282 // Checks that the single completed profile is captured if the profiling is |
| 283 // stopped between bursts. |
| 284 #if defined(_WIN64) |
| 285 #define MAYBE_StopDuringInterBurstInterval StopDuringInterBurstInterval |
| 286 #else |
| 287 #define MAYBE_StopDuringInterBurstInterval DISABLED_StopDuringInterBurstInterval |
| 288 #endif |
| 289 TEST(StackSamplingProfilerTest, MAYBE_StopDuringInterBurstInterval) { |
| 290 StackSamplingProfiler::SamplingParams params; |
| 291 params.initial_delay = params.sampling_interval = |
| 292 TimeDelta::FromMilliseconds(0); |
| 293 params.burst_interval = TimeDelta::FromSeconds(60); |
| 294 params.bursts = 2; |
| 295 params.samples_per_burst = 1; |
| 296 |
| 297 std::vector<Profile> profiles; |
| 298 CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(50)); |
| 299 |
| 300 ASSERT_EQ(1u, profiles.size()); |
| 301 EXPECT_EQ(1u, profiles[0].samples.size()); |
| 302 } |
| 303 |
| 304 // Checks that only completed profiles are captured. |
| 305 #if defined(_WIN64) |
| 306 #define MAYBE_StopDuringInterSampleInterval StopDuringInterSampleInterval |
| 307 #else |
| 308 #define MAYBE_StopDuringInterSampleInterval \ |
| 309 DISABLED_StopDuringInterSampleInterval |
| 310 #endif |
| 311 TEST(StackSamplingProfilerTest, MAYBE_StopDuringInterSampleInterval) { |
| 312 StackSamplingProfiler::SamplingParams params; |
| 313 params.initial_delay = params.burst_interval = TimeDelta::FromMilliseconds(0); |
| 314 params.sampling_interval = TimeDelta::FromSeconds(60); |
| 315 params.bursts = 1; |
| 316 params.samples_per_burst = 2; |
| 317 |
| 318 std::vector<Profile> profiles; |
| 319 CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(50)); |
| 320 |
| 321 EXPECT_TRUE(profiles.empty()); |
| 322 } |
| 323 |
| 324 } // namespace tracked_objects |
OLD | NEW |