Skip to content

Commit 0a7aae2

Browse files
derekxu16Commit Queue
authored andcommitted
[VM/Profiler] Correctly apply the value of --profile-period specified on the CLI
TEST=Verified that pkg/vm_service/test/profile_period_cli_option_test passes with the changes in this CL and fails without them. Change-Id: Ic36014abe682f9b4c829317b59bb664683e12d7e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/412781 Commit-Queue: Derek Xu <[email protected]> Reviewed-by: Siva Annamalai <[email protected]>
1 parent 7179fd5 commit 0a7aae2

File tree

6 files changed

+50
-9
lines changed

6 files changed

+50
-9
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:test/test.dart';
6+
import 'package:vm_service/vm_service.dart';
7+
8+
import 'common/test_helper.dart';
9+
10+
void testeeMain() {
11+
final stopwatch = Stopwatch();
12+
stopwatch.start();
13+
while (stopwatch.elapsedMilliseconds < 5000) {}
14+
stopwatch.stop();
15+
}
16+
17+
final tests = <IsolateTest>[
18+
(VmService service, IsolateRef isolateRef) async {
19+
final cpuSamples = await service.getCpuSamples(isolateRef.id!, -1, -1);
20+
// The default profile period is 1ms, and the testee runs for at least 5000
21+
// ms. So, we confirm that increasing the profile period using the CLI
22+
// option worked by confirming that we received significantly fewer than
23+
// 5000 samples.
24+
expect(cpuSamples.sampleCount, lessThan(2000));
25+
}
26+
];
27+
28+
Future<void> main([args = const <String>[]]) => runIsolateTests(
29+
args,
30+
tests,
31+
'eval_with_resident_compiler_test.dart',
32+
testeeBefore: testeeMain,
33+
pauseOnExit: true,
34+
extraArgs: ['--profile-period=10000'],
35+
);

runtime/vm/profiler.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -580,14 +580,14 @@ void Profiler::Init() {
580580
return;
581581
}
582582
ASSERT(!initialized_);
583-
SetSamplePeriod(FLAG_profile_period);
584583
// The profiler may have been shutdown previously, in which case the sample
585584
// buffer will have already been initialized.
586585
if (sample_block_buffer_ == nullptr) {
587586
intptr_t num_blocks = CalculateSampleBufferCapacity();
588587
sample_block_buffer_ = new SampleBlockBuffer(num_blocks);
589588
}
590-
ThreadInterrupter::Init();
589+
UpdateFlagProfilePeriod(FLAG_profile_period);
590+
ThreadInterrupter::Init(FLAG_profile_period);
591591
ThreadInterrupter::Startup();
592592
SampleBlockProcessor::Init();
593593
SampleBlockProcessor::Startup();
@@ -656,18 +656,18 @@ intptr_t Profiler::CalculateSampleBufferCapacity() {
656656
return (sample_count / SampleBlock::kSamplesPerBlock) + 1;
657657
}
658658

659-
void Profiler::SetSamplePeriod(intptr_t period) {
659+
void Profiler::UpdateFlagProfilePeriod(intptr_t period) {
660660
const int kMinimumProfilePeriod = 50;
661661
if (period < kMinimumProfilePeriod) {
662662
FLAG_profile_period = kMinimumProfilePeriod;
663663
} else {
664664
FLAG_profile_period = period;
665665
}
666-
ThreadInterrupter::SetInterruptPeriod(FLAG_profile_period);
667666
}
668667

669668
void Profiler::UpdateSamplePeriod() {
670-
SetSamplePeriod(FLAG_profile_period);
669+
UpdateFlagProfilePeriod(FLAG_profile_period);
670+
ThreadInterrupter::SetInterruptPeriod(FLAG_profile_period);
671671
}
672672

673673
SampleBlockBuffer::SampleBlockBuffer(intptr_t blocks,

runtime/vm/profiler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class Profiler : public AllStatic {
5656
static void Cleanup();
5757

5858
static void SetSampleDepth(intptr_t depth);
59-
static void SetSamplePeriod(intptr_t period);
59+
// Sets |FLAG_profile_period| to |max(period, 50)|.
60+
static void UpdateFlagProfilePeriod(intptr_t period);
6061
// Restarts sampling with a given profile period. This is called after the
6162
// profile period is changed via the service protocol.
6263
static void UpdateSamplePeriod();

runtime/vm/profiler_test.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2438,7 +2438,8 @@ ISOLATE_UNIT_TEST_CASE(Profiler_ProfileCodeTableTest) {
24382438
// https://github.com/flutter/flutter/issues/134548
24392439
ISOLATE_UNIT_TEST_CASE(Profiler_EnterExitIsolate) {
24402440
EnableProfiler();
2441-
Profiler::SetSamplePeriod(50); // Microseconds.
2441+
Profiler::UpdateFlagProfilePeriod(50); // Microseconds.
2442+
Profiler::UpdateSamplePeriod();
24422443

24432444
const char* kScript = "main() => null;\n";
24442445
const Library& root_library = Library::Handle(LoadTestScript(kScript));

runtime/vm/thread_interrupter.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ Monitor* ThreadInterrupter::monitor_ = nullptr;
5151
intptr_t ThreadInterrupter::interrupt_period_ = 1000;
5252
intptr_t ThreadInterrupter::current_wait_time_ = Monitor::kNoTimeout;
5353

54-
void ThreadInterrupter::Init() {
54+
void ThreadInterrupter::Init(intptr_t period) {
5555
ASSERT(!initialized_);
5656
if (monitor_ == nullptr) {
5757
monitor_ = new Monitor();
5858
}
5959
ASSERT(monitor_ != nullptr);
6060
initialized_ = true;
6161
shutdown_ = false;
62+
interrupt_period_ = period;
6263
}
6364

6465
void ThreadInterrupter::Startup() {

runtime/vm/thread_interrupter.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ struct InterruptedThreadState {
2222

2323
class ThreadInterrupter : public AllStatic {
2424
public:
25-
static void Init();
25+
static void Init(intptr_t period);
2626

2727
static void Startup();
2828
static void Cleanup();
2929

3030
// Delay between interrupts.
31+
//
32+
// Warning: This method will have no effect if
33+
// |ThreadInterrupter::initialized_| is false.
3134
static void SetInterruptPeriod(intptr_t period);
3235

3336
// Wake up the thread interrupter thread.

0 commit comments

Comments
 (0)