[vm] Fix HeapProfileSampler::SampleOldSpaceAllocation

When Isolate is just starting up and no TLABs are yet present
we were setting interval_to_next_sample_ to -1 which caused
every old space allocation to be sampled.

This CL properly initializes the interval to sampling_interval_
instead.

TEST=vm/cc/DartAPI_HeapSampling_CorrectSamplingIntervalForOldSpaceAllocations

Change-Id: Ieb29234379d3afa4716ebdf3591f9fc0cbcaef71
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/311844
Commit-Queue: Slava Egorov <vegorov@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
Auto-Submit: Slava Egorov <vegorov@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
This commit is contained in:
Vyacheslav Egorov 2023-06-28 12:58:32 +00:00 committed by Commit Queue
parent 8f7cf7724c
commit 86c311607a
2 changed files with 29 additions and 0 deletions

View file

@ -10868,6 +10868,29 @@ TEST_CASE(DartAPI_HeapSampling_NonTrivialSamplingPeriod) {
Dart_EnterIsolate(isolate);
Dart_DisableHeapSampling();
}
// Check that we set correct sampling interval for old space allocations
// during initial isolate startup (before the first TLAB is created).
// This is a regression test for a bug that was causing each old space
// allocation to be sampled.
VM_UNIT_TEST_CASE(
DartAPI_HeapSampling_CorrectSamplingIntervalForOldSpaceAllocations) {
Dart_RegisterHeapSamplingCallback(
[](Dart_Isolate isolate, Dart_IsolateGroup isolate_group,
const char* cls_name, intptr_t allocation_size) -> void* {
// Should not be called because sampling interval is very large.
UNREACHABLE();
return nullptr;
},
[](void* data) {
// Nothing to do.
});
Dart_SetHeapSamplingPeriod(1 * GB);
Dart_EnableHeapSampling();
TestCase::CreateTestIsolate();
Dart_DisableHeapSampling();
Dart_ShutdownIsolate();
}
#endif // !defined(PRODUCT) || defined(FORCE_INCLUDE_SAMPLING_HEAP_PROFILER)
#if defined(DART_ENABLE_HEAP_SNAPSHOT_WRITER)

View file

@ -248,6 +248,12 @@ void HeapProfileSampler::SampleOldSpaceAllocation(intptr_t allocation_size) {
interval_to_next_sample_ = tlab_interval;
}
// If we don't have a TLAB yet simply initialize interval_to_next_sample_
// from the sampling interval.
if (interval_to_next_sample_ == kUninitialized) {
interval_to_next_sample_ = sampling_interval_;
}
// Check the allocation is large enough to trigger a sample. If not, tighten
// the interval.
if (allocation_size < interval_to_next_sample_) {