@@ -4442,6 +4442,82 @@ TEST(SamplingHeapProfilerLargeInterval) {
44424442 heap_profiler->StopSamplingHeapProfiler ();
44434443}
44444444
4445+ TEST (SamplingHeapProfilerSampleWithoutGCFlags) {
4446+ v8::HandleScope scope (CcTest::isolate ());
4447+ LocalContext env;
4448+ v8::HeapProfiler* heap_profiler = env.isolate ()->GetHeapProfiler ();
4449+
4450+ // Suppress randomness to avoid flakiness in tests.
4451+ i::v8_flags.sampling_heap_profiler_suppress_randomness = true ;
4452+
4453+ heap_profiler->StartSamplingHeapProfiler (1024 );
4454+
4455+ // Allocate objects that will be retained
4456+ CompileRun (
4457+ " var retained = [];\n "
4458+ " for (var i = 0; i < 500; i++) retained.push(new Array(10));\n " );
4459+
4460+ CompileRun (" for (var i = 0; i < 500; i++) new Array(10);\n " );
4461+
4462+ std::unique_ptr<v8::AllocationProfile> profile (
4463+ heap_profiler->GetAllocationProfile ());
4464+ CHECK (profile);
4465+
4466+ const auto & samples = profile->GetSamples ();
4467+ CHECK (!samples.empty ());
4468+
4469+ for (const auto & sample : samples) {
4470+ CHECK (sample.is_live );
4471+ }
4472+
4473+ heap_profiler->StopSamplingHeapProfiler ();
4474+ }
4475+
4476+ TEST (SamplingHeapProfilerSampleIsLive) {
4477+ v8::HandleScope scope (CcTest::isolate ());
4478+ LocalContext env;
4479+ v8::HeapProfiler* heap_profiler = env.isolate ()->GetHeapProfiler ();
4480+
4481+ // Suppress randomness to avoid flakiness in tests.
4482+ i::v8_flags.sampling_heap_profiler_suppress_randomness = true ;
4483+
4484+ heap_profiler->StartSamplingHeapProfiler (
4485+ 64 , 16 ,
4486+ static_cast <v8::HeapProfiler::SamplingFlags>(
4487+ v8::HeapProfiler::kSamplingForceGC |
4488+ v8::HeapProfiler::kSamplingIncludeObjectsCollectedByMajorGC ));
4489+
4490+ // Allocate objects that will be retained
4491+ CompileRun (
4492+ " var retained = [];\n "
4493+ " for (var i = 0; i < 500; i++) retained.push(new Array(10));\n " );
4494+
4495+ CompileRun (" for (var i = 0; i < 500; i++) new Array(10);\n " );
4496+
4497+ std::unique_ptr<v8::AllocationProfile> profile (
4498+ heap_profiler->GetAllocationProfile ());
4499+ CHECK (profile);
4500+
4501+ const auto & samples = profile->GetSamples ();
4502+ CHECK (!samples.empty ());
4503+
4504+ int live_samples = 0 ;
4505+ int dead_samples = 0 ;
4506+ for (const auto & sample : samples) {
4507+ if (sample.is_live ) {
4508+ ++live_samples;
4509+ } else {
4510+ ++dead_samples;
4511+ }
4512+ }
4513+
4514+ // We expect both retained and collected allocations in this profile.
4515+ CHECK_GT (live_samples, 0 );
4516+ CHECK_GT (dead_samples, 0 );
4517+
4518+ heap_profiler->StopSamplingHeapProfiler ();
4519+ }
4520+
44454521TEST (HeapSnapshotPrototypeNotJSReceiver) {
44464522 LocalContext env;
44474523 v8::HandleScope scope (env.isolate ());
0 commit comments