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> | 5 #include <sstream> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/profiler/stack_sampling_profiler.h" | 10 #include "base/profiler/stack_sampling_profiler.h" |
11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
12 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 | 17 |
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 Profile = StackSamplingProfiler::Profile; |
22 | 22 |
23 namespace { | 23 namespace { |
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 |
Peter Kasting
2015/03/26 04:29:07
Nit: Blank line above
Mike Wittman
2015/03/27 22:42:04
Done.
| |
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 // Implementation of PlatformThread::Delegate: |
Peter Kasting
2015/03/26 04:29:08
Nit: "Implementation of" is not really necessary
Mike Wittman
2015/03/27 22:42:04
Done.
| |
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 // Allow the thread to return from SignalAndWaitUntilSignaled() and finish | 36 // Allow the thread to return from SignalAndWaitUntilSignaled() and finish |
Peter Kasting
2015/03/26 04:29:07
Nit: Blank line above
Mike Wittman
2015/03/27 22:42:04
Done.
| |
37 // execution. | 37 // execution. |
38 void SignalThreadToFinish(); | 38 void SignalThreadToFinish(); |
39 | 39 |
40 // This function is guaranteed to be executing between calls to | 40 // This function is guaranteed to be executing between calls to |
41 // WaitForThreadStart() and SignalThreadToFinish(). | 41 // WaitForThreadStart() and SignalThreadToFinish(). |
Peter Kasting
2015/03/26 04:29:07
You should probably add to this comment the reason
Mike Wittman
2015/03/27 22:42:04
Done.
| |
42 static void SignalAndWaitUntilSignaled(WaitableEvent* thread_started_event, | 42 static void SignalAndWaitUntilSignaled(WaitableEvent* thread_started_event, |
43 WaitableEvent* finish_event); | 43 WaitableEvent* finish_event); |
44 | 44 |
45 PlatformThreadId id() const { return id_; } | 45 PlatformThreadId id() const { return id_; } |
46 | 46 |
47 private: | 47 private: |
48 WaitableEvent thread_started_event_; | 48 WaitableEvent thread_started_event_; |
49 WaitableEvent finish_event_; | 49 WaitableEvent finish_event_; |
50 PlatformThreadId id_; | 50 PlatformThreadId id_; |
51 | 51 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 target_thread.SignalThreadToFinish(); | 120 target_thread.SignalThreadToFinish(); |
121 | 121 |
122 PlatformThread::Join(target_thread_handle); | 122 PlatformThread::Join(target_thread_handle); |
123 } | 123 } |
124 | 124 |
125 // If this executable was linked with /INCREMENTAL (the default for non-official | 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 | 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 | 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 | 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. | 129 // do a little decompilation to find the function's actual starting address. |
130 const void* MaybeFixupFunctionAddressForILT(const void* function_address) { | 130 const void* MaybeFixupFunctionAddressForILT(const void* function_address) { |
Peter Kasting
2015/03/26 04:29:08
Nit: If you take and return char*s instead of void
Mike Wittman
2015/03/27 22:42:04
I'd somewhat prefer to keep these interfaces using
Peter Kasting
2015/03/27 23:44:46
Fair enough.
| |
131 #if defined(_WIN64) | 131 #if defined(_WIN64) |
132 const unsigned char* opcode = | 132 const unsigned char* opcode = |
133 reinterpret_cast<const unsigned char*>(function_address); | 133 reinterpret_cast<const unsigned char*>(function_address); |
134 if (*opcode == 0xe9) { | 134 if (*opcode == 0xe9) { |
135 // This is a relative jump instruction. Assume we're in the ILT and compute | 135 // This is a relative jump instruction. Assume we're in the ILT and compute |
136 // the function start address from the instruction offset. | 136 // the function start address from the instruction offset. |
137 const unsigned char* offset = opcode + 1; | 137 const unsigned char* offset = opcode + 1; |
Peter Kasting
2015/03/26 04:29:07
Nit: Why not declare this as type const int32* and
Mike Wittman
2015/03/27 22:42:04
Done.
| |
138 const unsigned char* next_instruction = opcode + 5; | 138 const unsigned char* next_instruction = opcode + 5; |
139 return next_instruction + | 139 return next_instruction + |
140 static_cast<int64>(*reinterpret_cast<const int32*>(offset)); | 140 static_cast<int64>(*reinterpret_cast<const int32*>(offset)); |
Peter Kasting
2015/03/26 04:29:08
Why do we need the int64 cast? AFAICT it does not
Mike Wittman
2015/03/27 22:42:04
We don't. I was following the instruction set spec
| |
141 } | 141 } |
142 #endif | 142 #endif |
143 return function_address; | 143 return function_address; |
144 } | 144 } |
145 | 145 |
146 // Searches through the frames in |sample|, returning an iterator to the first | 146 // Searches through the frames in |sample|, returning an iterator to the first |
147 // frame that has an instruction pointer between |function_address| and | 147 // frame that has an instruction pointer between |function_address| and |
148 // |function_address| + |size|. Returns sample.end() if no such frames are | 148 // |function_address| + |size|. Returns sample.end() if no such frames are |
149 // found. | 149 // found. |
150 Sample::const_iterator FindFirstFrameWithinFunction( | 150 Sample::const_iterator FindFirstFrameWithinFunction( |
151 const Sample& sample, | 151 const Sample& sample, |
152 const void* function_address, | 152 const void* function_address, |
153 int function_size) { | 153 int function_size) { |
154 function_address = MaybeFixupFunctionAddressForILT(function_address); | 154 function_address = MaybeFixupFunctionAddressForILT(function_address); |
155 for (auto it = sample.begin(); it != sample.end(); ++it) { | 155 for (auto it = sample.begin(); it != sample.end(); ++it) { |
156 if ((reinterpret_cast<const unsigned char*>(it->instruction_pointer) >= | 156 if ((reinterpret_cast<const unsigned char*>(it->instruction_pointer) >= |
Peter Kasting
2015/03/26 04:29:07
I don't think casting the instruction_pointer to c
Mike Wittman
2015/03/27 22:42:04
That's correct, we only need to cast to support th
Peter Kasting
2015/03/27 23:44:46
My rule-of-thumb is that if static_cast does the r
Mike Wittman
2015/03/30 21:01:13
Sounds reasonable. Changed to static_cast.
| |
157 reinterpret_cast<const unsigned char*>(function_address)) && | 157 reinterpret_cast<const unsigned char*>(function_address)) && |
158 (reinterpret_cast<const unsigned char*>(it->instruction_pointer) < | 158 (reinterpret_cast<const unsigned char*>(it->instruction_pointer) < |
159 (reinterpret_cast<const unsigned char*>(function_address) + | 159 (reinterpret_cast<const unsigned char*>(function_address) + |
160 function_size))) | 160 function_size))) |
161 return it; | 161 return it; |
162 } | 162 } |
163 return sample.end(); | 163 return sample.end(); |
164 } | 164 } |
165 | 165 |
166 // Formats a sample into a string that can be output for test diagnostics. | 166 // Formats a sample into a string that can be output for test diagnostics. |
167 std::string FormatSampleForDiagnosticOutput( | 167 std::string FormatSampleForDiagnosticOutput( |
168 const Sample& sample, | 168 const Sample& sample, |
169 const std::vector<Module>& modules) { | 169 const std::vector<Module>& modules) { |
170 std::ostringstream stream; | 170 std::ostringstream stream; |
Peter Kasting
2015/03/26 04:29:07
ostringstream seems like overkill here.
Something
Mike Wittman
2015/03/27 22:42:04
Done.
| |
171 for (const Frame& frame: sample) { | 171 for (const Frame& frame: sample) { |
172 stream << frame.instruction_pointer << " " | 172 stream << frame.instruction_pointer << " " |
173 << modules[frame.module_index].filename.value() << std::endl; | 173 << modules[frame.module_index].filename.value() << std::endl; |
174 } | 174 } |
175 return stream.str(); | 175 return stream.str(); |
176 } | 176 } |
177 | 177 |
178 // Returns a duration that is longer than the test timeout. We would use | 178 // Returns a duration that is longer than the test timeout. We would use |
179 // TimeDelta::Max() but https://crbug.com/465948. | 179 // TimeDelta::Max() but https://crbug.com/465948. |
180 TimeDelta AVeryLongTimeDelta() { return TimeDelta::FromDays(1); } | 180 TimeDelta AVeryLongTimeDelta() { return TimeDelta::FromDays(1); } |
181 } // namespace | 181 } // namespace |
Peter Kasting
2015/03/26 04:29:08
Nit: Blank line above
Mike Wittman
2015/03/27 22:42:03
Done.
| |
182 | 182 |
183 | 183 |
184 // The tests below are enabled for Win x64 only, pending implementation of the | 184 // The tests below are enabled for Win x64 only, pending implementation of the |
185 // tested functionality on other platforms/architectures. | 185 // tested functionality on other platforms/architectures. |
Peter Kasting
2015/03/26 04:29:08
It might be more readable if you simply bracketed
Mike Wittman
2015/03/27 22:42:04
Probably. But I'd prefer to keep these tests compi
Peter Kasting
2015/03/27 23:44:46
OK. I hope "eventually" is soon :)
| |
186 | 186 |
187 // Checks that the basic expected information is present in a sampled profile. | 187 // Checks that the basic expected information is present in a sampled profile. |
188 #if defined(_WIN64) | 188 #if defined(_WIN64) |
189 #define MAYBE_Basic Basic | 189 #define MAYBE_Basic Basic |
190 #else | 190 #else |
191 #define MAYBE_Basic DISABLED_Basic | 191 #define MAYBE_Basic DISABLED_Basic |
192 #endif | 192 #endif |
193 TEST(StackSamplingProfilerTest, MAYBE_Basic) { | 193 TEST(StackSamplingProfilerTest, MAYBE_Basic) { |
194 StackSamplingProfiler::SamplingParams params; | 194 StackSamplingProfiler::SamplingParams params; |
Peter Kasting
2015/03/26 04:29:07
Enough of these tests want to set most/all of the
Mike Wittman
2015/03/27 22:42:03
I'm setting most of the params intentionally (even
Peter Kasting
2015/03/27 23:44:46
Hmm.
That isn't strictly against the style guide,
Mike Wittman
2015/03/30 21:01:13
Looking through this more closely, many of the val
| |
195 params.initial_delay = params.burst_interval = params.sampling_interval = | 195 params.initial_delay = params.burst_interval = params.sampling_interval = |
196 TimeDelta::FromMilliseconds(0); | 196 TimeDelta::FromMilliseconds(0); |
197 params.bursts = 1; | 197 params.bursts = 1; |
198 params.samples_per_burst = 1; | 198 params.samples_per_burst = 1; |
199 | 199 |
200 std::vector<Profile> profiles; | 200 std::vector<Profile> profiles; |
201 CaptureProfiles(params, &profiles, AVeryLongTimeDelta()); | 201 CaptureProfiles(params, &profiles, AVeryLongTimeDelta()); |
202 | 202 |
203 // Check that the profile and samples sizes are correct, and the module | 203 // Check that the profile and samples sizes are correct, and the module |
204 // indices are in range. | 204 // indices are in range. |
205 | 205 |
Peter Kasting
2015/03/26 04:29:07
Nit: I'd remove this blank line and the one on lin
Mike Wittman
2015/03/27 22:42:03
Done.
| |
206 ASSERT_EQ(1u, profiles.size()); | 206 ASSERT_EQ(1u, profiles.size()); |
207 const Profile& profile = profiles[0]; | 207 const Profile& profile = profiles[0]; |
208 ASSERT_EQ(1u, profile.samples.size()); | 208 ASSERT_EQ(1u, profile.samples.size()); |
209 EXPECT_EQ(params.sampling_interval, profile.sampling_period); | 209 EXPECT_EQ(params.sampling_interval, profile.sampling_period); |
210 const Sample& sample = profile.samples[0]; | 210 const Sample& sample = profile.samples[0]; |
211 for (const auto& frame : sample) { | 211 for (const auto& frame : sample) { |
212 ASSERT_GE(frame.module_index, 0); | 212 ASSERT_GE(frame.module_index, 0); |
213 ASSERT_LT(frame.module_index, static_cast<int>(profile.modules.size())); | 213 ASSERT_LT(frame.module_index, static_cast<int>(profile.modules.size())); |
214 } | 214 } |
215 | 215 |
216 // Check that the stack contains a frame for | 216 // Check that the stack contains a frame for |
217 // TargetThread::SignalAndWaitUntilSignaled() and that the frame has this | 217 // TargetThread::SignalAndWaitUntilSignaled() and that the frame has this |
218 // executable's module. | 218 // executable's module. |
219 | 219 |
220 // Since we don't have a good way to know the function size, use 100 bytes as | 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. | 221 // a reasonable window to locate the instruction pointer. |
222 Sample::const_iterator loc = FindFirstFrameWithinFunction( | 222 Sample::const_iterator loc = FindFirstFrameWithinFunction( |
223 sample, | 223 sample, |
224 reinterpret_cast<const void*>(&TargetThread::SignalAndWaitUntilSignaled), | 224 reinterpret_cast<const void*>(&TargetThread::SignalAndWaitUntilSignaled), |
225 100); | 225 100); |
226 ASSERT_TRUE(loc != sample.end()) | 226 ASSERT_TRUE(loc != sample.end()) |
227 << "Function at " | 227 << "Function at " |
228 << MaybeFixupFunctionAddressForILT( | 228 << MaybeFixupFunctionAddressForILT( |
229 reinterpret_cast<const void*>( | 229 reinterpret_cast<const void*>( |
230 &TargetThread::SignalAndWaitUntilSignaled)) | 230 &TargetThread::SignalAndWaitUntilSignaled)) |
231 << " was not found in stack:" << std::endl | 231 << " was not found in stack:" << std::endl |
Peter Kasting
2015/03/26 04:29:08
Nit: I'd normally use \n over std::endl, since you
Mike Wittman
2015/03/27 22:42:04
Done.
| |
232 << FormatSampleForDiagnosticOutput(sample, profile.modules); | 232 << FormatSampleForDiagnosticOutput(sample, profile.modules); |
233 | 233 |
234 FilePath executable_path; | 234 FilePath executable_path; |
235 bool got_executable_path = PathService::Get(FILE_EXE, &executable_path); | 235 bool got_executable_path = PathService::Get(FILE_EXE, &executable_path); |
Peter Kasting
2015/03/26 04:29:08
Nit: Just inline this into the EXPECT_TRUE below.
Mike Wittman
2015/03/27 22:42:04
Done.
| |
236 EXPECT_TRUE(got_executable_path); | 236 EXPECT_TRUE(got_executable_path); |
237 EXPECT_EQ(executable_path, profile.modules[loc->module_index].filename); | 237 EXPECT_EQ(executable_path, profile.modules[loc->module_index].filename); |
238 } | 238 } |
239 | 239 |
240 // Checks that the expected number of profiles and samples are present in the | 240 // Checks that the expected number of profiles and samples are present in the |
241 // profiles produced. | 241 // profiles produced. |
242 #if defined(_WIN64) | 242 #if defined(_WIN64) |
243 #define MAYBE_MultipleProfilesAndSamples MultipleProfilesAndSamples | 243 #define MAYBE_MultipleProfilesAndSamples MultipleProfilesAndSamples |
244 #else | 244 #else |
245 #define MAYBE_MultipleProfilesAndSamples DISABLED_MultipleProfilesAndSamples | 245 #define MAYBE_MultipleProfilesAndSamples DISABLED_MultipleProfilesAndSamples |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
315 params.bursts = 1; | 315 params.bursts = 1; |
316 params.samples_per_burst = 2; | 316 params.samples_per_burst = 2; |
317 | 317 |
318 std::vector<Profile> profiles; | 318 std::vector<Profile> profiles; |
319 CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(50)); | 319 CaptureProfiles(params, &profiles, TimeDelta::FromMilliseconds(50)); |
320 | 320 |
321 EXPECT_TRUE(profiles.empty()); | 321 EXPECT_TRUE(profiles.empty()); |
322 } | 322 } |
323 | 323 |
324 } // namespace tracked_objects | 324 } // namespace tracked_objects |
325 | |
OLD | NEW |