Skip to content

Commit a72f25e

Browse files
mralephCommit Queue
authored andcommitted
[vm] Fix Profiler::UpdateRunningState
It was calling Profiler::Cleanup() to shut the profiler down but Cleanup did nothing if FLAG_profiler is false. So we need to adjust condition at the start of Cleanup(). TEST=pkg/dds/test/profiler_can_be_stopped_test.dart Change-Id: I9921334d264c5cae879e37148d0d784494cb2d63 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426464 Reviewed-by: Siva Annamalai <[email protected]> Commit-Queue: Slava Egorov <[email protected]>
1 parent a6a99dc commit a72f25e

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

runtime/vm/profiler.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ class SampleBlockCleanupVisitor : public IsolateVisitor {
606606
};
607607

608608
void Profiler::Cleanup() {
609-
if (!FLAG_profiler) {
609+
if (!FLAG_profiler && !initialized_) {
610610
return;
611611
}
612612
ASSERT(initialized_);

runtime/vm/profiler_test.cc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,6 +2452,63 @@ ISOLATE_UNIT_TEST_CASE(Profiler_EnterExitIsolate) {
24522452
}
24532453
}
24542454

2455+
ISOLATE_UNIT_TEST_CASE(Profiler_UpdateRunningState) {
2456+
Isolate* isolate = Isolate::Current();
2457+
SampleFilter filter(isolate->main_port(), Thread::kMutatorTask, -1, -1);
2458+
2459+
EnableProfiler();
2460+
Profiler::UpdateFlagProfilePeriod(50); // Microseconds.
2461+
Profiler::UpdateSamplePeriod();
2462+
2463+
const char* kScript = R"(
2464+
void main() {
2465+
final sw = Stopwatch()..start();
2466+
while (sw.elapsedMilliseconds < 1000) {
2467+
}
2468+
})";
2469+
const Library& root_library = Library::Handle(LoadTestScript(kScript));
2470+
2471+
// Test that profiler collects some samples while main is running if
2472+
// it is enabled.
2473+
Invoke(root_library, "main");
2474+
{
2475+
Profile profile;
2476+
profile.Build(thread, isolate, &filter, Profiler::sample_block_buffer());
2477+
// We expect to have collected at least some samples.
2478+
EXPECT_LT(0, profile.sample_count());
2479+
}
2480+
2481+
// Switch profiler off and clear the sample block buffer to discard all
2482+
// collected samples.
2483+
FLAG_profiler = false;
2484+
Profiler::UpdateRunningState();
2485+
delete Profiler::sample_block_buffer();
2486+
Profiler::set_sample_block_buffer(new SampleBlockBuffer());
2487+
2488+
// Test that no samples are collected if profiler is disabled.
2489+
Invoke(root_library, "main");
2490+
{
2491+
Profile profile;
2492+
profile.Build(thread, isolate, &filter, Profiler::sample_block_buffer());
2493+
// We expect to have collected no samples.
2494+
EXPECT_EQ(0, profile.sample_count());
2495+
}
2496+
2497+
// Enable profiler again.
2498+
FLAG_profiler = true;
2499+
Profiler::UpdateRunningState();
2500+
2501+
// Test again that samples are collected.
2502+
Invoke(root_library, "main");
2503+
{
2504+
Profile profile;
2505+
profile.Build(thread, isolate, &filter, Profiler::sample_block_buffer());
2506+
2507+
// We expect to have collected at least some samples.
2508+
EXPECT_LT(0, profile.sample_count());
2509+
}
2510+
}
2511+
24552512
#endif // !PRODUCT
24562513

24572514
} // namespace dart

0 commit comments

Comments
 (0)