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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
| 8 #include <algorithm> |
8 #include <cstdlib> | 9 #include <cstdlib> |
9 #include <memory> | 10 #include <memory> |
10 #include <utility> | 11 #include <utility> |
11 #include <vector> | 12 #include <vector> |
12 | 13 |
13 #include "base/bind.h" | 14 #include "base/bind.h" |
14 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
15 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
16 #include "base/macros.h" | 17 #include "base/macros.h" |
17 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 PlatformThread::Sleep(TimeDelta::FromMilliseconds(1)); | 317 PlatformThread::Sleep(TimeDelta::FromMilliseconds(1)); |
317 } | 318 } |
318 #elif defined(OS_MACOSX) | 319 #elif defined(OS_MACOSX) |
319 // Unloading a library on the Mac is synchronous. | 320 // Unloading a library on the Mac is synchronous. |
320 #else | 321 #else |
321 NOTIMPLEMENTED(); | 322 NOTIMPLEMENTED(); |
322 #endif | 323 #endif |
323 } | 324 } |
324 | 325 |
325 // Called on the profiler thread when complete, to collect profiles. | 326 // Called on the profiler thread when complete, to collect profiles. |
326 void SaveProfiles(CallStackProfiles* profiles, | 327 Optional<StackSamplingProfiler::SamplingParams> SaveProfiles( |
327 CallStackProfiles pending_profiles) { | 328 CallStackProfiles* profiles, |
| 329 CallStackProfiles pending_profiles) { |
328 *profiles = std::move(pending_profiles); | 330 *profiles = std::move(pending_profiles); |
| 331 return Optional<StackSamplingProfiler::SamplingParams>(); |
329 } | 332 } |
330 | 333 |
331 // Called on the profiler thread when complete. Collects profiles produced by | 334 // Called on the profiler thread when complete. Collects profiles produced by |
332 // the profiler, and signals an event to allow the main thread to know that that | 335 // the profiler, and signals an event to allow the main thread to know that that |
333 // the profiler is done. | 336 // the profiler is done. |
334 void SaveProfilesAndSignalEvent(CallStackProfiles* profiles, | 337 Optional<StackSamplingProfiler::SamplingParams> SaveProfilesAndSignalEvent( |
335 WaitableEvent* event, | 338 CallStackProfiles* profiles, |
336 CallStackProfiles pending_profiles) { | 339 WaitableEvent* event, |
| 340 CallStackProfiles pending_profiles) { |
337 *profiles = std::move(pending_profiles); | 341 *profiles = std::move(pending_profiles); |
338 event->Signal(); | 342 event->Signal(); |
| 343 return Optional<StackSamplingProfiler::SamplingParams>(); |
| 344 } |
| 345 |
| 346 // Similar to SaveProfilesAndSignalEvent(), but will schedule a second |
| 347 // collection after the first call back. |
| 348 Optional<StackSamplingProfiler::SamplingParams> SaveProfilesAndReschedule( |
| 349 std::vector<CallStackProfiles>* profiles, |
| 350 WaitableEvent* event, |
| 351 CallStackProfiles pending_profiles) { |
| 352 profiles->push_back(std::move(pending_profiles)); |
| 353 |
| 354 event->Signal(); |
| 355 |
| 356 if (profiles->size() == 2) |
| 357 return Optional<StackSamplingProfiler::SamplingParams>(); |
| 358 |
| 359 StackSamplingProfiler::SamplingParams sampling_params; |
| 360 sampling_params.initial_delay = base::TimeDelta::FromMilliseconds(100); |
| 361 sampling_params.bursts = 1; |
| 362 sampling_params.samples_per_burst = 1; |
| 363 // Below are unused: |
| 364 sampling_params.burst_interval = base::TimeDelta::FromMilliseconds(0); |
| 365 sampling_params.sampling_interval = base::TimeDelta::FromMilliseconds(0); |
| 366 return sampling_params; |
339 } | 367 } |
340 | 368 |
341 // Executes the function with the target thread running and executing within | 369 // Executes the function with the target thread running and executing within |
342 // SignalAndWaitUntilSignaled(). Performs all necessary target thread startup | 370 // SignalAndWaitUntilSignaled(). Performs all necessary target thread startup |
343 // and shutdown work before and afterward. | 371 // and shutdown work before and afterward. |
344 template <class Function> | 372 template <class Function> |
345 void WithTargetThread(Function function, | 373 void WithTargetThread(Function function, |
346 const StackConfiguration& stack_config) { | 374 const StackConfiguration& stack_config) { |
347 TargetThread target_thread(stack_config); | 375 TargetThread target_thread(stack_config); |
348 PlatformThreadHandle target_thread_handle; | 376 PlatformThreadHandle target_thread_handle; |
(...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1013 // Ensure a second request will run and not block. | 1041 // Ensure a second request will run and not block. |
1014 sampling_completed.Reset(); | 1042 sampling_completed.Reset(); |
1015 profiles.clear(); | 1043 profiles.clear(); |
1016 profiler.Start(); | 1044 profiler.Start(); |
1017 sampling_completed.Wait(); | 1045 sampling_completed.Wait(); |
1018 profiler.Stop(); | 1046 profiler.Stop(); |
1019 ASSERT_EQ(1u, profiles.size()); | 1047 ASSERT_EQ(1u, profiles.size()); |
1020 }); | 1048 }); |
1021 } | 1049 } |
1022 | 1050 |
| 1051 PROFILER_TEST_F(StackSamplingProfilerTest, RescheduledByCallback) { |
| 1052 WithTargetThread([](PlatformThreadId target_thread_id) { |
| 1053 SamplingParams params; |
| 1054 params.sampling_interval = TimeDelta::FromMilliseconds(0); |
| 1055 params.samples_per_burst = 1; |
| 1056 |
| 1057 std::vector<CallStackProfiles> profiles; |
| 1058 WaitableEvent sampling_completed(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 1059 WaitableEvent::InitialState::NOT_SIGNALED); |
| 1060 const StackSamplingProfiler::CompletedCallback callback = |
| 1061 Bind(&SaveProfilesAndReschedule, Unretained(&profiles), |
| 1062 Unretained(&sampling_completed)); |
| 1063 StackSamplingProfiler profiler(target_thread_id, params, callback); |
| 1064 |
| 1065 // Start once and wait for it to be completed. |
| 1066 profiler.Start(); |
| 1067 sampling_completed.Wait(); |
| 1068 ASSERT_EQ(1u, profiles.size()); |
| 1069 ASSERT_EQ(1u, profiles[0].size()); |
| 1070 |
| 1071 // Now, wait for the second callback call. |
| 1072 sampling_completed.Wait(); |
| 1073 profiler.Stop(); |
| 1074 ASSERT_EQ(2u, profiles.size()); |
| 1075 ASSERT_EQ(1u, profiles[1].size()); |
| 1076 }); |
| 1077 } |
| 1078 |
1023 // Checks that the different profilers may be run. | 1079 // Checks that the different profilers may be run. |
1024 PROFILER_TEST_F(StackSamplingProfilerTest, CanRunMultipleProfilers) { | 1080 PROFILER_TEST_F(StackSamplingProfilerTest, CanRunMultipleProfilers) { |
1025 SamplingParams params; | 1081 SamplingParams params; |
1026 params.sampling_interval = TimeDelta::FromMilliseconds(0); | 1082 params.sampling_interval = TimeDelta::FromMilliseconds(0); |
1027 params.samples_per_burst = 1; | 1083 params.samples_per_burst = 1; |
1028 | 1084 |
1029 std::vector<CallStackProfile> profiles; | 1085 std::vector<CallStackProfile> profiles; |
1030 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles); | 1086 CaptureProfiles(params, AVeryLongTimeDelta(), &profiles); |
1031 ASSERT_EQ(1u, profiles.size()); | 1087 ASSERT_EQ(1u, profiles.size()); |
1032 | 1088 |
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1496 EXPECT_EQ(9u, profiler_thread1.profiles()[0].samples.size()); | 1552 EXPECT_EQ(9u, profiler_thread1.profiles()[0].samples.size()); |
1497 ASSERT_EQ(1u, profiler_thread2.profiles().size()); | 1553 ASSERT_EQ(1u, profiler_thread2.profiles().size()); |
1498 EXPECT_EQ(8u, profiler_thread2.profiles()[0].samples.size()); | 1554 EXPECT_EQ(8u, profiler_thread2.profiles()[0].samples.size()); |
1499 | 1555 |
1500 profiler_thread1.Join(); | 1556 profiler_thread1.Join(); |
1501 profiler_thread2.Join(); | 1557 profiler_thread2.Join(); |
1502 }); | 1558 }); |
1503 } | 1559 } |
1504 | 1560 |
1505 } // namespace base | 1561 } // namespace base |
OLD | NEW |