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 |
Peter Kasting
2015/03/30 23:07:35
Waits
Mike Wittman
2015/03/31 01:06:37
Done.
| |
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 |
Peter Kasting
2015/03/30 23:07:35
Allows
Mike Wittman
2015/03/31 01:06:37
Done.
| |
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 | |
Peter Kasting
2015/03/30 23:07:35
Nit: "...address for it in one of the tests below,
Mike Wittman
2015/03/31 01:06:37
Done.
| |
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) < | 162 (static_cast<const unsigned char*>(function_address) + function_size))) |
159 (reinterpret_cast<const unsigned char*>(function_address) + | |
160 function_size))) | |
161 return it; | 163 return it; |
162 } | 164 } |
163 return sample.end(); | 165 return sample.end(); |
164 } | 166 } |
165 | 167 |
166 // Formats a sample into a string that can be output for test diagnostics. | 168 // Formats a sample into a string that can be output for test diagnostics. |
167 std::string FormatSampleForDiagnosticOutput( | 169 std::string FormatSampleForDiagnosticOutput( |
168 const Sample& sample, | 170 const Sample& sample, |
169 const std::vector<Module>& modules) { | 171 const std::vector<Module>& modules) { |
170 std::ostringstream stream; | 172 std::string output; |
171 for (const Frame& frame: sample) { | 173 for (const Frame& frame: sample) { |
172 stream << frame.instruction_pointer << " " | 174 output += StringPrintf( |
173 << modules[frame.module_index].filename.value() << std::endl; | 175 "0x%p %s\n", frame.instruction_pointer, |
176 modules[frame.module_index].filename.AsUTF8Unsafe().c_str()); | |
174 } | 177 } |
175 return stream.str(); | 178 return output; |
176 } | 179 } |
177 | 180 |
178 // Returns a duration that is longer than the test timeout. We would use | 181 // Returns a duration that is longer than the test timeout. We would use |
179 // TimeDelta::Max() but https://crbug.com/465948. | 182 // TimeDelta::Max() but https://crbug.com/465948. |
180 TimeDelta AVeryLongTimeDelta() { return TimeDelta::FromDays(1); } | 183 TimeDelta AVeryLongTimeDelta() { return TimeDelta::FromDays(1); } |
184 | |
181 } // namespace | 185 } // namespace |
182 | 186 |
183 | 187 |
184 // The tests below are enabled for Win x64 only, pending implementation of the | 188 // The tests below are enabled for Win x64 only, pending implementation of the |
185 // tested functionality on other platforms/architectures. | 189 // tested functionality on other platforms/architectures. |
186 | 190 |
187 // Checks that the basic expected information is present in a sampled profile. | 191 // Checks that the basic expected information is present in a sampled profile. |
188 #if defined(_WIN64) | 192 #if defined(_WIN64) |
189 #define MAYBE_Basic Basic | 193 #define MAYBE_Basic Basic |
190 #else | 194 #else |
191 #define MAYBE_Basic DISABLED_Basic | 195 #define MAYBE_Basic DISABLED_Basic |
192 #endif | 196 #endif |
193 TEST(StackSamplingProfilerTest, MAYBE_Basic) { | 197 TEST(StackSamplingProfilerTest, MAYBE_Basic) { |
194 StackSamplingProfiler::SamplingParams params; | 198 StackSamplingProfiler::SamplingParams params; |
195 params.initial_delay = params.burst_interval = params.sampling_interval = | 199 params.sampling_interval = TimeDelta::FromMilliseconds(0); |
196 TimeDelta::FromMilliseconds(0); | |
197 params.bursts = 1; | |
198 params.samples_per_burst = 1; | 200 params.samples_per_burst = 1; |
199 | 201 |
200 std::vector<Profile> profiles; | 202 std::vector<Profile> profiles; |
201 CaptureProfiles(params, &profiles, AVeryLongTimeDelta()); | 203 CaptureProfiles(params, &profiles, AVeryLongTimeDelta()); |
202 | 204 |
203 // Check that the profile and samples sizes are correct, and the module | 205 // Check that the profile and samples sizes are correct, and the module |
204 // indices are in range. | 206 // indices are in range. |
205 | |
206 ASSERT_EQ(1u, profiles.size()); | 207 ASSERT_EQ(1u, profiles.size()); |
207 const Profile& profile = profiles[0]; | 208 const Profile& profile = profiles[0]; |
208 ASSERT_EQ(1u, profile.samples.size()); | 209 ASSERT_EQ(1u, profile.samples.size()); |
209 EXPECT_EQ(params.sampling_interval, profile.sampling_period); | 210 EXPECT_EQ(params.sampling_interval, profile.sampling_period); |
210 const Sample& sample = profile.samples[0]; | 211 const Sample& sample = profile.samples[0]; |
211 for (const auto& frame : sample) { | 212 for (const auto& frame : sample) { |
212 ASSERT_GE(frame.module_index, 0); | 213 ASSERT_GE(frame.module_index, 0u); |
213 ASSERT_LT(frame.module_index, static_cast<int>(profile.modules.size())); | 214 ASSERT_LT(frame.module_index, profile.modules.size()); |
214 } | 215 } |
215 | 216 |
216 // Check that the stack contains a frame for | 217 // Check that the stack contains a frame for |
217 // TargetThread::SignalAndWaitUntilSignaled() and that the frame has this | 218 // TargetThread::SignalAndWaitUntilSignaled() and that the frame has this |
218 // executable's module. | 219 // executable's module. |
219 | 220 // |
220 // Since we don't have a good way to know the function size, use 100 bytes as | 221 // 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 // a reasonable window to locate the instruction pointer. |
222 Sample::const_iterator loc = FindFirstFrameWithinFunction( | 223 Sample::const_iterator loc = FindFirstFrameWithinFunction( |
223 sample, | 224 sample, |
224 reinterpret_cast<const void*>(&TargetThread::SignalAndWaitUntilSignaled), | 225 reinterpret_cast<const void*>(&TargetThread::SignalAndWaitUntilSignaled), |
225 100); | 226 100); |
226 ASSERT_TRUE(loc != sample.end()) | 227 ASSERT_TRUE(loc != sample.end()) |
227 << "Function at " | 228 << "Function at " |
228 << MaybeFixupFunctionAddressForILT( | 229 << MaybeFixupFunctionAddressForILT( |
229 reinterpret_cast<const void*>( | 230 reinterpret_cast<const void*>( |
230 &TargetThread::SignalAndWaitUntilSignaled)) | 231 &TargetThread::SignalAndWaitUntilSignaled)) |
231 << " was not found in stack:" << std::endl | 232 << " was not found in stack:\n" |
232 << FormatSampleForDiagnosticOutput(sample, profile.modules); | 233 << FormatSampleForDiagnosticOutput(sample, profile.modules); |
233 | |
234 FilePath executable_path; | 234 FilePath executable_path; |
235 bool got_executable_path = PathService::Get(FILE_EXE, &executable_path); | 235 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); | 236 EXPECT_EQ(executable_path, profile.modules[loc->module_index].filename); |
238 } | 237 } |
239 | 238 |
240 // Checks that the expected number of profiles and samples are present in the | 239 // Checks that the expected number of profiles and samples are present in the |
241 // profiles produced. | 240 // profiles produced. |
242 #if defined(_WIN64) | 241 #if defined(_WIN64) |
243 #define MAYBE_MultipleProfilesAndSamples MultipleProfilesAndSamples | 242 #define MAYBE_MultipleProfilesAndSamples MultipleProfilesAndSamples |
244 #else | 243 #else |
245 #define MAYBE_MultipleProfilesAndSamples DISABLED_MultipleProfilesAndSamples | 244 #define MAYBE_MultipleProfilesAndSamples DISABLED_MultipleProfilesAndSamples |
246 #endif | 245 #endif |
247 TEST(StackSamplingProfilerTest, MAYBE_MultipleProfilesAndSamples) { | 246 TEST(StackSamplingProfilerTest, MAYBE_MultipleProfilesAndSamples) { |
248 StackSamplingProfiler::SamplingParams params; | 247 StackSamplingProfiler::SamplingParams params; |
249 params.initial_delay = params.burst_interval = params.sampling_interval = | 248 params.burst_interval = params.sampling_interval = |
250 TimeDelta::FromMilliseconds(0); | 249 TimeDelta::FromMilliseconds(0); |
251 params.bursts = 2; | 250 params.bursts = 2; |
252 params.samples_per_burst = 3; | 251 params.samples_per_burst = 3; |
253 | 252 |
254 std::vector<Profile> profiles; | 253 std::vector<Profile> profiles; |
255 CaptureProfiles(params, &profiles, AVeryLongTimeDelta()); | 254 CaptureProfiles(params, &profiles, AVeryLongTimeDelta()); |
256 | 255 |
257 ASSERT_EQ(2u, profiles.size()); | 256 ASSERT_EQ(2u, profiles.size()); |
258 EXPECT_EQ(3u, profiles[0].samples.size()); | 257 EXPECT_EQ(3u, profiles[0].samples.size()); |
259 EXPECT_EQ(3u, profiles[1].samples.size()); | 258 EXPECT_EQ(3u, profiles[1].samples.size()); |
260 } | 259 } |
261 | 260 |
262 // Checks that no profiles are captured if the profiling is stopped during the | 261 // Checks that no profiles are captured if the profiling is stopped during the |
263 // initial delay. | 262 // initial delay. |
264 #if defined(_WIN64) | 263 #if defined(_WIN64) |
265 #define MAYBE_StopDuringInitialDelay StopDuringInitialDelay | 264 #define MAYBE_StopDuringInitialDelay StopDuringInitialDelay |
266 #else | 265 #else |
267 #define MAYBE_StopDuringInitialDelay DISABLED_StopDuringInitialDelay | 266 #define MAYBE_StopDuringInitialDelay DISABLED_StopDuringInitialDelay |
268 #endif | 267 #endif |
269 TEST(StackSamplingProfilerTest, MAYBE_StopDuringInitialDelay) { | 268 TEST(StackSamplingProfilerTest, MAYBE_StopDuringInitialDelay) { |
270 StackSamplingProfiler::SamplingParams params; | 269 StackSamplingProfiler::SamplingParams params; |
271 params.burst_interval = params.sampling_interval = | |
272 TimeDelta::FromMilliseconds(0); | |
273 params.initial_delay = TimeDelta::FromSeconds(60); | 270 params.initial_delay = TimeDelta::FromSeconds(60); |
274 params.bursts = params.samples_per_burst = 1; | |
275 | 271 |
276 std::vector<Profile> profiles; | 272 std::vector<Profile> profiles; |
277 CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(0)); | 273 CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(0)); |
278 | 274 |
279 EXPECT_TRUE(profiles.empty()); | 275 EXPECT_TRUE(profiles.empty()); |
280 } | 276 } |
281 | 277 |
282 // Checks that the single completed profile is captured if the profiling is | 278 // Checks that the single completed profile is captured if the profiling is |
283 // stopped between bursts. | 279 // stopped between bursts. |
284 #if defined(_WIN64) | 280 #if defined(_WIN64) |
285 #define MAYBE_StopDuringInterBurstInterval StopDuringInterBurstInterval | 281 #define MAYBE_StopDuringInterBurstInterval StopDuringInterBurstInterval |
286 #else | 282 #else |
287 #define MAYBE_StopDuringInterBurstInterval DISABLED_StopDuringInterBurstInterval | 283 #define MAYBE_StopDuringInterBurstInterval DISABLED_StopDuringInterBurstInterval |
288 #endif | 284 #endif |
289 TEST(StackSamplingProfilerTest, MAYBE_StopDuringInterBurstInterval) { | 285 TEST(StackSamplingProfilerTest, MAYBE_StopDuringInterBurstInterval) { |
290 StackSamplingProfiler::SamplingParams params; | 286 StackSamplingProfiler::SamplingParams params; |
291 params.initial_delay = params.sampling_interval = | 287 params.sampling_interval = TimeDelta::FromMilliseconds(0); |
292 TimeDelta::FromMilliseconds(0); | |
293 params.burst_interval = TimeDelta::FromSeconds(60); | 288 params.burst_interval = TimeDelta::FromSeconds(60); |
294 params.bursts = 2; | 289 params.bursts = 2; |
295 params.samples_per_burst = 1; | 290 params.samples_per_burst = 1; |
296 | 291 |
297 std::vector<Profile> profiles; | 292 std::vector<Profile> profiles; |
298 CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(50)); | 293 CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(50)); |
299 | 294 |
300 ASSERT_EQ(1u, profiles.size()); | 295 ASSERT_EQ(1u, profiles.size()); |
301 EXPECT_EQ(1u, profiles[0].samples.size()); | 296 EXPECT_EQ(1u, profiles[0].samples.size()); |
302 } | 297 } |
303 | 298 |
304 // Checks that only completed profiles are captured. | 299 // Checks that only completed profiles are captured. |
305 #if defined(_WIN64) | 300 #if defined(_WIN64) |
306 #define MAYBE_StopDuringInterSampleInterval StopDuringInterSampleInterval | 301 #define MAYBE_StopDuringInterSampleInterval StopDuringInterSampleInterval |
307 #else | 302 #else |
308 #define MAYBE_StopDuringInterSampleInterval \ | 303 #define MAYBE_StopDuringInterSampleInterval \ |
309 DISABLED_StopDuringInterSampleInterval | 304 DISABLED_StopDuringInterSampleInterval |
310 #endif | 305 #endif |
311 TEST(StackSamplingProfilerTest, MAYBE_StopDuringInterSampleInterval) { | 306 TEST(StackSamplingProfilerTest, MAYBE_StopDuringInterSampleInterval) { |
312 StackSamplingProfiler::SamplingParams params; | 307 StackSamplingProfiler::SamplingParams params; |
313 params.initial_delay = params.burst_interval = TimeDelta::FromMilliseconds(0); | |
314 params.sampling_interval = TimeDelta::FromSeconds(60); | 308 params.sampling_interval = TimeDelta::FromSeconds(60); |
315 params.bursts = 1; | |
316 params.samples_per_burst = 2; | 309 params.samples_per_burst = 2; |
317 | 310 |
318 std::vector<Profile> profiles; | 311 std::vector<Profile> profiles; |
319 CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(50)); | 312 CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(50)); |
320 | 313 |
321 EXPECT_TRUE(profiles.empty()); | 314 EXPECT_TRUE(profiles.empty()); |
322 } | 315 } |
323 | 316 |
324 } // namespace tracked_objects | 317 } // namespace tracked_objects |
318 | |
OLD | NEW |