| 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 |
| 18 using Frame = StackSamplingProfiler::Frame; | 17 using Frame = StackSamplingProfiler::Frame; |
| 19 using Module = StackSamplingProfiler::Module; | 18 using Module = StackSamplingProfiler::Module; |
| 20 using Sample = StackSamplingProfiler::Sample; | 19 using Sample = StackSamplingProfiler::Sample; |
| 21 using Profile = StackSamplingProfiler::Profile; | 20 using Profile = StackSamplingProfiler::Profile; |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 23 |
| 24 // A thread to target for profiling, whose stack is guaranteed to contain | 24 // A thread to target for profiling, whose stack is guaranteed to contain |
| 25 // SignalAndWaitUntilSignaled() when coordinated with the main thread. | 25 // SignalAndWaitUntilSignaled() when coordinated with the main thread. |
| 26 class TargetThread : public PlatformThread::Delegate { | 26 class TargetThread : public PlatformThread::Delegate { |
| 27 public: | 27 public: |
| 28 TargetThread(); | 28 TargetThread(); |
| 29 | 29 |
| 30 // Implementation of PlatformThread::Delegate: | 30 // PlatformThread::Delegate: |
| 31 void ThreadMain() override; | 31 void ThreadMain() override; |
| 32 | 32 |
| 33 // Wait for the thread to have started and be executing in | 33 // Wait for the thread to have started and be executing in |
| 34 // SignalAndWaitUntilSignaled(). | 34 // SignalAndWaitUntilSignaled(). |
| 35 void WaitForThreadStart(); | 35 void WaitForThreadStart(); |
| 36 |
| 36 // Allow the thread to return from SignalAndWaitUntilSignaled() and finish | 37 // Allow the thread to return from SignalAndWaitUntilSignaled() and finish |
| 37 // execution. | 38 // execution. |
| 38 void SignalThreadToFinish(); | 39 void SignalThreadToFinish(); |
| 39 | 40 |
| 40 // This function is guaranteed to be executing between calls to | 41 // This function is guaranteed to be executing between calls to |
| 41 // WaitForThreadStart() and SignalThreadToFinish(). | 42 // WaitForThreadStart() and SignalThreadToFinish(). This function is static so |
| 43 // that we can get a straightforward address for it, rather than dealing with |
| 44 // the complexity of a member function pointer representation. |
| 42 static void SignalAndWaitUntilSignaled(WaitableEvent* thread_started_event, | 45 static void SignalAndWaitUntilSignaled(WaitableEvent* thread_started_event, |
| 43 WaitableEvent* finish_event); | 46 WaitableEvent* finish_event); |
| 44 | 47 |
| 45 PlatformThreadId id() const { return id_; } | 48 PlatformThreadId id() const { return id_; } |
| 46 | 49 |
| 47 private: | 50 private: |
| 48 WaitableEvent thread_started_event_; | 51 WaitableEvent thread_started_event_; |
| 49 WaitableEvent finish_event_; | 52 WaitableEvent finish_event_; |
| 50 PlatformThreadId id_; | 53 PlatformThreadId id_; |
| 51 | 54 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 TimeDelta profiler_wait_time) { | 105 TimeDelta profiler_wait_time) { |
| 103 TargetThread target_thread; | 106 TargetThread target_thread; |
| 104 PlatformThreadHandle target_thread_handle; | 107 PlatformThreadHandle target_thread_handle; |
| 105 EXPECT_TRUE(PlatformThread::Create(0, &target_thread, &target_thread_handle)); | 108 EXPECT_TRUE(PlatformThread::Create(0, &target_thread, &target_thread_handle)); |
| 106 | 109 |
| 107 target_thread.WaitForThreadStart(); | 110 target_thread.WaitForThreadStart(); |
| 108 | 111 |
| 109 WaitableEvent sampling_thread_completed(true, false); | 112 WaitableEvent sampling_thread_completed(true, false); |
| 110 profiles->clear(); | 113 profiles->clear(); |
| 111 StackSamplingProfiler profiler(target_thread.id(), params); | 114 StackSamplingProfiler profiler(target_thread.id(), params); |
| 112 profiler.SetCustomCompletedCallback( | 115 profiler.set_custom_completed_callback( |
| 113 Bind(&SaveProfilesAndSignalEvent, Unretained(profiles), | 116 Bind(&SaveProfilesAndSignalEvent, Unretained(profiles), |
| 114 Unretained(&sampling_thread_completed))); | 117 Unretained(&sampling_thread_completed))); |
| 115 profiler.Start(); | 118 profiler.Start(); |
| 116 sampling_thread_completed.TimedWait(profiler_wait_time); | 119 sampling_thread_completed.TimedWait(profiler_wait_time); |
| 117 profiler.Stop(); | 120 profiler.Stop(); |
| 118 sampling_thread_completed.Wait(); | 121 sampling_thread_completed.Wait(); |
| 119 | 122 |
| 120 target_thread.SignalThreadToFinish(); | 123 target_thread.SignalThreadToFinish(); |
| 121 | 124 |
| 122 PlatformThread::Join(target_thread_handle); | 125 PlatformThread::Join(target_thread_handle); |
| 123 } | 126 } |
| 124 | 127 |
| 125 // If this executable was linked with /INCREMENTAL (the default for non-official | 128 // 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 | 129 // debug and release builds on Windows), function addresses do not correspond to |
| 127 // function code itself, but instead to instructions in the Incremental Link | 130 // 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 | 131 // Table that jump to the functions. Checks for a jump instruction and if |
| 129 // do a little decompilation to find the function's actual starting address. | 132 // present does a little decompilation to find the function's actual starting |
| 133 // address. |
| 130 const void* MaybeFixupFunctionAddressForILT(const void* function_address) { | 134 const void* MaybeFixupFunctionAddressForILT(const void* function_address) { |
| 131 #if defined(_WIN64) | 135 #if defined(_WIN64) |
| 132 const unsigned char* opcode = | 136 const unsigned char* opcode = |
| 133 reinterpret_cast<const unsigned char*>(function_address); | 137 reinterpret_cast<const unsigned char*>(function_address); |
| 134 if (*opcode == 0xe9) { | 138 if (*opcode == 0xe9) { |
| 135 // This is a relative jump instruction. Assume we're in the ILT and compute | 139 // This is a relative jump instruction. Assume we're in the ILT and compute |
| 136 // the function start address from the instruction offset. | 140 // the function start address from the instruction offset. |
| 137 const unsigned char* offset = opcode + 1; | 141 const int32* offset = reinterpret_cast<const int32*>(opcode + 1); |
| 138 const unsigned char* next_instruction = opcode + 5; | 142 const unsigned char* next_instruction = |
| 139 return next_instruction + | 143 reinterpret_cast<const unsigned char*>(offset + 1); |
| 140 static_cast<int64>(*reinterpret_cast<const int32*>(offset)); | 144 return next_instruction + *offset; |
| 141 } | 145 } |
| 142 #endif | 146 #endif |
| 143 return function_address; | 147 return function_address; |
| 144 } | 148 } |
| 145 | 149 |
| 146 // Searches through the frames in |sample|, returning an iterator to the first | 150 // Searches through the frames in |sample|, returning an iterator to the first |
| 147 // frame that has an instruction pointer between |function_address| and | 151 // frame that has an instruction pointer between |function_address| and |
| 148 // |function_address| + |size|. Returns sample.end() if no such frames are | 152 // |function_address| + |size|. Returns sample.end() if no such frames are |
| 149 // found. | 153 // found. |
| 150 Sample::const_iterator FindFirstFrameWithinFunction( | 154 Sample::const_iterator FindFirstFrameWithinFunction( |
| 151 const Sample& sample, | 155 const Sample& sample, |
| 152 const void* function_address, | 156 const void* function_address, |
| 153 int function_size) { | 157 int function_size) { |
| 154 function_address = MaybeFixupFunctionAddressForILT(function_address); | 158 function_address = MaybeFixupFunctionAddressForILT(function_address); |
| 155 for (auto it = sample.begin(); it != sample.end(); ++it) { | 159 for (auto it = sample.begin(); it != sample.end(); ++it) { |
| 156 if ((reinterpret_cast<const unsigned char*>(it->instruction_pointer) >= | 160 if ((it->instruction_pointer >= function_address) && |
| 157 reinterpret_cast<const unsigned char*>(function_address)) && | 161 (it->instruction_pointer < |
| 158 (reinterpret_cast<const unsigned char*>(it->instruction_pointer) < | |
| 159 (reinterpret_cast<const unsigned char*>(function_address) + | 162 (reinterpret_cast<const unsigned char*>(function_address) + |
| 160 function_size))) | 163 function_size))) |
| 161 return it; | 164 return it; |
| 162 } | 165 } |
| 163 return sample.end(); | 166 return sample.end(); |
| 164 } | 167 } |
| 165 | 168 |
| 166 // Formats a sample into a string that can be output for test diagnostics. | 169 // Formats a sample into a string that can be output for test diagnostics. |
| 167 std::string FormatSampleForDiagnosticOutput( | 170 std::string FormatSampleForDiagnosticOutput( |
| 168 const Sample& sample, | 171 const Sample& sample, |
| 169 const std::vector<Module>& modules) { | 172 const std::vector<Module>& modules) { |
| 170 std::ostringstream stream; | 173 std::string output; |
| 171 for (const Frame& frame: sample) { | 174 for (const Frame& frame: sample) { |
| 172 stream << frame.instruction_pointer << " " | 175 output += StringPrintf( |
| 173 << modules[frame.module_index].filename.value() << std::endl; | 176 "0x%p %s\n", frame.instruction_pointer, |
| 177 modules[frame.module_index].filename.AsUTF8Unsafe().c_str()); |
| 174 } | 178 } |
| 175 return stream.str(); | 179 return output; |
| 176 } | 180 } |
| 177 | 181 |
| 178 // Returns a duration that is longer than the test timeout. We would use | 182 // Returns a duration that is longer than the test timeout. We would use |
| 179 // TimeDelta::Max() but https://crbug.com/465948. | 183 // TimeDelta::Max() but https://crbug.com/465948. |
| 180 TimeDelta AVeryLongTimeDelta() { return TimeDelta::FromDays(1); } | 184 TimeDelta AVeryLongTimeDelta() { return TimeDelta::FromDays(1); } |
| 185 |
| 181 } // namespace | 186 } // namespace |
| 182 | 187 |
| 183 | 188 |
| 184 // The tests below are enabled for Win x64 only, pending implementation of the | 189 // The tests below are enabled for Win x64 only, pending implementation of the |
| 185 // tested functionality on other platforms/architectures. | 190 // tested functionality on other platforms/architectures. |
| 186 | 191 |
| 187 // Checks that the basic expected information is present in a sampled profile. | 192 // Checks that the basic expected information is present in a sampled profile. |
| 188 #if defined(_WIN64) | 193 #if defined(_WIN64) |
| 189 #define MAYBE_Basic Basic | 194 #define MAYBE_Basic Basic |
| 190 #else | 195 #else |
| 191 #define MAYBE_Basic DISABLED_Basic | 196 #define MAYBE_Basic DISABLED_Basic |
| 192 #endif | 197 #endif |
| 193 TEST(StackSamplingProfilerTest, MAYBE_Basic) { | 198 TEST(StackSamplingProfilerTest, MAYBE_Basic) { |
| 194 StackSamplingProfiler::SamplingParams params; | 199 StackSamplingProfiler::SamplingParams params; |
| 195 params.initial_delay = params.burst_interval = params.sampling_interval = | 200 params.initial_delay = params.burst_interval = params.sampling_interval = |
| 196 TimeDelta::FromMilliseconds(0); | 201 TimeDelta::FromMilliseconds(0); |
| 197 params.bursts = 1; | 202 params.bursts = 1; |
| 198 params.samples_per_burst = 1; | 203 params.samples_per_burst = 1; |
| 199 | 204 |
| 200 std::vector<Profile> profiles; | 205 std::vector<Profile> profiles; |
| 201 CaptureProfiles(params, &profiles, AVeryLongTimeDelta()); | 206 CaptureProfiles(params, &profiles, AVeryLongTimeDelta()); |
| 202 | 207 |
| 203 // Check that the profile and samples sizes are correct, and the module | 208 // Check that the profile and samples sizes are correct, and the module |
| 204 // indices are in range. | 209 // indices are in range. |
| 205 | |
| 206 ASSERT_EQ(1u, profiles.size()); | 210 ASSERT_EQ(1u, profiles.size()); |
| 207 const Profile& profile = profiles[0]; | 211 const Profile& profile = profiles[0]; |
| 208 ASSERT_EQ(1u, profile.samples.size()); | 212 ASSERT_EQ(1u, profile.samples.size()); |
| 209 EXPECT_EQ(params.sampling_interval, profile.sampling_period); | 213 EXPECT_EQ(params.sampling_interval, profile.sampling_period); |
| 210 const Sample& sample = profile.samples[0]; | 214 const Sample& sample = profile.samples[0]; |
| 211 for (const auto& frame : sample) { | 215 for (const auto& frame : sample) { |
| 212 ASSERT_GE(frame.module_index, 0); | 216 ASSERT_GE(frame.module_index, 0u); |
| 213 ASSERT_LT(frame.module_index, static_cast<int>(profile.modules.size())); | 217 ASSERT_LT(frame.module_index, profile.modules.size()); |
| 214 } | 218 } |
| 215 | 219 |
| 216 // Check that the stack contains a frame for | 220 // Check that the stack contains a frame for |
| 217 // TargetThread::SignalAndWaitUntilSignaled() and that the frame has this | 221 // TargetThread::SignalAndWaitUntilSignaled() and that the frame has this |
| 218 // executable's module. | 222 // executable's module. |
| 219 | 223 // |
| 220 // Since we don't have a good way to know the function size, use 100 bytes as | 224 // 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. | 225 // a reasonable window to locate the instruction pointer. |
| 222 Sample::const_iterator loc = FindFirstFrameWithinFunction( | 226 Sample::const_iterator loc = FindFirstFrameWithinFunction( |
| 223 sample, | 227 sample, |
| 224 reinterpret_cast<const void*>(&TargetThread::SignalAndWaitUntilSignaled), | 228 reinterpret_cast<const void*>(&TargetThread::SignalAndWaitUntilSignaled), |
| 225 100); | 229 100); |
| 226 ASSERT_TRUE(loc != sample.end()) | 230 ASSERT_TRUE(loc != sample.end()) |
| 227 << "Function at " | 231 << "Function at " |
| 228 << MaybeFixupFunctionAddressForILT( | 232 << MaybeFixupFunctionAddressForILT( |
| 229 reinterpret_cast<const void*>( | 233 reinterpret_cast<const void*>( |
| 230 &TargetThread::SignalAndWaitUntilSignaled)) | 234 &TargetThread::SignalAndWaitUntilSignaled)) |
| 231 << " was not found in stack:" << std::endl | 235 << " was not found in stack:\n" |
| 232 << FormatSampleForDiagnosticOutput(sample, profile.modules); | 236 << FormatSampleForDiagnosticOutput(sample, profile.modules); |
| 233 | |
| 234 FilePath executable_path; | 237 FilePath executable_path; |
| 235 bool got_executable_path = PathService::Get(FILE_EXE, &executable_path); | 238 EXPECT_TRUE(PathService::Get(FILE_EXE, &executable_path)); |
| 236 EXPECT_TRUE(got_executable_path); | |
| 237 EXPECT_EQ(executable_path, profile.modules[loc->module_index].filename); | 239 EXPECT_EQ(executable_path, profile.modules[loc->module_index].filename); |
| 238 } | 240 } |
| 239 | 241 |
| 240 // Checks that the expected number of profiles and samples are present in the | 242 // Checks that the expected number of profiles and samples are present in the |
| 241 // profiles produced. | 243 // profiles produced. |
| 242 #if defined(_WIN64) | 244 #if defined(_WIN64) |
| 243 #define MAYBE_MultipleProfilesAndSamples MultipleProfilesAndSamples | 245 #define MAYBE_MultipleProfilesAndSamples MultipleProfilesAndSamples |
| 244 #else | 246 #else |
| 245 #define MAYBE_MultipleProfilesAndSamples DISABLED_MultipleProfilesAndSamples | 247 #define MAYBE_MultipleProfilesAndSamples DISABLED_MultipleProfilesAndSamples |
| 246 #endif | 248 #endif |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 params.bursts = 1; | 317 params.bursts = 1; |
| 316 params.samples_per_burst = 2; | 318 params.samples_per_burst = 2; |
| 317 | 319 |
| 318 std::vector<Profile> profiles; | 320 std::vector<Profile> profiles; |
| 319 CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(50)); | 321 CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(50)); |
| 320 | 322 |
| 321 EXPECT_TRUE(profiles.empty()); | 323 EXPECT_TRUE(profiles.empty()); |
| 322 } | 324 } |
| 323 | 325 |
| 324 } // namespace tracked_objects | 326 } // namespace tracked_objects |
| 327 |
| OLD | NEW |