Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(364)

Side by Side Diff: base/profiler/stack_sampling_profiler_unittest.cc

Issue 2927593002: Make stack sampling profiler sample beyond startup. (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <cstdlib> 8 #include <cstdlib>
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 PlatformThread::Sleep(TimeDelta::FromMilliseconds(1)); 309 PlatformThread::Sleep(TimeDelta::FromMilliseconds(1));
310 } 310 }
311 #elif defined(OS_MACOSX) 311 #elif defined(OS_MACOSX)
312 // Unloading a library on the Mac is synchronous. 312 // Unloading a library on the Mac is synchronous.
313 #else 313 #else
314 NOTIMPLEMENTED(); 314 NOTIMPLEMENTED();
315 #endif 315 #endif
316 } 316 }
317 317
318 // Called on the profiler thread when complete, to collect profiles. 318 // Called on the profiler thread when complete, to collect profiles.
319 void SaveProfiles(CallStackProfiles* profiles, 319 bool SaveProfiles(CallStackProfiles* profiles,
320 CallStackProfiles pending_profiles) { 320 CallStackProfiles pending_profiles,
321 StackSamplingProfiler::SamplingParams* sampling_params) {
321 *profiles = std::move(pending_profiles); 322 *profiles = std::move(pending_profiles);
323 return false;
322 } 324 }
323 325
324 // Called on the profiler thread when complete. Collects profiles produced by 326 // Called on the profiler thread when complete. Collects profiles produced by
325 // the profiler, and signals an event to allow the main thread to know that that 327 // the profiler, and signals an event to allow the main thread to know that that
326 // the profiler is done. 328 // the profiler is done.
327 void SaveProfilesAndSignalEvent(CallStackProfiles* profiles, 329 bool SaveProfilesAndSignalEvent(
328 WaitableEvent* event, 330 CallStackProfiles* profiles,
329 CallStackProfiles pending_profiles) { 331 WaitableEvent* event,
332 CallStackProfiles pending_profiles,
333 StackSamplingProfiler::SamplingParams* sampling_params) {
330 *profiles = std::move(pending_profiles); 334 *profiles = std::move(pending_profiles);
331 event->Signal(); 335 event->Signal();
336 return false;
337 }
338
339 // Similar to SaveProfilesAndSignalEvent(), but will schedule a second
340 // collection after the first call back.
341 bool SaveProfilesAndReschedule(
342 std::vector<CallStackProfiles>* profiles,
343 WaitableEvent* event,
344 CallStackProfiles pending_profiles,
345 StackSamplingProfiler::SamplingParams* sampling_params) {
346 profiles->push_back(std::move(pending_profiles));
347
348 event->Signal();
349 event->Reset();
350
351 if (profiles->size() == 2)
352 return false;
353
354 sampling_params->initial_delay = base::TimeDelta::FromSeconds(1);
355 sampling_params->bursts = 1;
356 sampling_params->samples_per_burst = 1;
357 // Below are unused:
358 sampling_params->burst_interval = base::TimeDelta::FromMilliseconds(0);
359 sampling_params->sampling_interval = base::TimeDelta::FromMilliseconds(0);
360 return true;
332 } 361 }
333 362
334 // Executes the function with the target thread running and executing within 363 // Executes the function with the target thread running and executing within
335 // SignalAndWaitUntilSignaled(). Performs all necessary target thread startup 364 // SignalAndWaitUntilSignaled(). Performs all necessary target thread startup
336 // and shutdown work before and afterward. 365 // and shutdown work before and afterward.
337 template <class Function> 366 template <class Function>
338 void WithTargetThread(Function function, 367 void WithTargetThread(Function function,
339 const StackConfiguration& stack_config) { 368 const StackConfiguration& stack_config) {
340 TargetThread target_thread(stack_config); 369 TargetThread target_thread(stack_config);
341 PlatformThreadHandle target_thread_handle; 370 PlatformThreadHandle target_thread_handle;
(...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after
1055 // Ensure a second request will run and not block. 1084 // Ensure a second request will run and not block.
1056 sampling_completed.Reset(); 1085 sampling_completed.Reset();
1057 profiles.clear(); 1086 profiles.clear();
1058 profiler.Start(); 1087 profiler.Start();
1059 sampling_completed.Wait(); 1088 sampling_completed.Wait();
1060 profiler.Stop(); 1089 profiler.Stop();
1061 ASSERT_EQ(1u, profiles.size()); 1090 ASSERT_EQ(1u, profiles.size());
1062 }); 1091 });
1063 } 1092 }
1064 1093
1094 TEST_F(StackSamplingProfilerTest, RescheduledByCallback) {
Alexei Svitkine (slow) 2017/06/20 20:57:39 Note: I'll need to rebase this on my other CL.
1095 WithTargetThread([](PlatformThreadId target_thread_id) {
1096 SamplingParams params;
1097 params.sampling_interval = TimeDelta::FromMilliseconds(0);
1098 params.samples_per_burst = 1;
1099
1100 std::vector<CallStackProfiles> profiles;
1101 WaitableEvent sampling_completed(WaitableEvent::ResetPolicy::MANUAL,
1102 WaitableEvent::InitialState::NOT_SIGNALED);
1103 const StackSamplingProfiler::CompletedCallback callback =
1104 Bind(&SaveProfilesAndReschedule, Unretained(&profiles),
1105 Unretained(&sampling_completed));
1106 StackSamplingProfiler profiler(target_thread_id, params, callback);
1107
1108 // Start once and wait for it to be completed.
1109 profiler.Start();
1110 sampling_completed.Wait();
1111 ASSERT_EQ(1u, profiles.size());
1112 ASSERT_EQ(1u, profiles[0].size());
1113
1114 // Now, wait for the second callback call.
1115 sampling_completed.Wait();
1116 profiler.Stop();
1117 ASSERT_EQ(2u, profiles.size());
1118 ASSERT_EQ(1u, profiles[1].size());
1119 });
1120 }
1121
1065 // Checks that the different profilers may be run. 1122 // Checks that the different profilers may be run.
1066 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED) 1123 #if defined(STACK_SAMPLING_PROFILER_SUPPORTED)
1067 #define MAYBE_CanRunMultipleProfilers CanRunMultipleProfilers 1124 #define MAYBE_CanRunMultipleProfilers CanRunMultipleProfilers
1068 #else 1125 #else
1069 #define MAYBE_CanRunMultipleProfilers DISABLED_CanRunMultipleProfilers 1126 #define MAYBE_CanRunMultipleProfilers DISABLED_CanRunMultipleProfilers
1070 #endif 1127 #endif
1071 TEST_F(StackSamplingProfilerTest, MAYBE_CanRunMultipleProfilers) { 1128 TEST_F(StackSamplingProfilerTest, MAYBE_CanRunMultipleProfilers) {
1072 SamplingParams params; 1129 SamplingParams params;
1073 params.sampling_interval = TimeDelta::FromMilliseconds(0); 1130 params.sampling_interval = TimeDelta::FromMilliseconds(0);
1074 params.samples_per_burst = 1; 1131 params.samples_per_burst = 1;
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after
1598 EXPECT_EQ(9u, profiler_thread1.profiles()[0].samples.size()); 1655 EXPECT_EQ(9u, profiler_thread1.profiles()[0].samples.size());
1599 ASSERT_EQ(1u, profiler_thread2.profiles().size()); 1656 ASSERT_EQ(1u, profiler_thread2.profiles().size());
1600 EXPECT_EQ(8u, profiler_thread2.profiles()[0].samples.size()); 1657 EXPECT_EQ(8u, profiler_thread2.profiles()[0].samples.size());
1601 1658
1602 profiler_thread1.Join(); 1659 profiler_thread1.Join();
1603 profiler_thread2.Join(); 1660 profiler_thread2.Join();
1604 }); 1661 });
1605 } 1662 }
1606 1663
1607 } // namespace base 1664 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698