@@ -4324,6 +4324,82 @@ TEST(SamplingHeapProfilerLargeInterval) {
43244324 heap_profiler->StopSamplingHeapProfiler ();
43254325}
43264326
4327+ TEST (SamplingHeapProfilerSampleWithoutGCFlags) {
4328+ v8::HandleScope scope (CcTest::isolate ());
4329+ LocalContext env;
4330+ v8::HeapProfiler* heap_profiler = env.isolate ()->GetHeapProfiler ();
4331+
4332+ // Suppress randomness to avoid flakiness in tests.
4333+ i::v8_flags.sampling_heap_profiler_suppress_randomness = true ;
4334+
4335+ heap_profiler->StartSamplingHeapProfiler (1024 );
4336+
4337+ // Allocate objects that will be retained
4338+ CompileRun (
4339+ " var retained = [];\n "
4340+ " for (var i = 0; i < 500; i++) retained.push(new Array(10));\n " );
4341+
4342+ CompileRun (" for (var i = 0; i < 500; i++) new Array(10);\n " );
4343+
4344+ std::unique_ptr<v8::AllocationProfile> profile (
4345+ heap_profiler->GetAllocationProfile ());
4346+ CHECK (profile);
4347+
4348+ const auto & samples = profile->GetSamples ();
4349+ CHECK (!samples.empty ());
4350+
4351+ for (const auto & sample : samples) {
4352+ CHECK (sample.is_live );
4353+ }
4354+
4355+ heap_profiler->StopSamplingHeapProfiler ();
4356+ }
4357+
4358+ TEST (SamplingHeapProfilerSampleIsLive) {
4359+ v8::HandleScope scope (CcTest::isolate ());
4360+ LocalContext env;
4361+ v8::HeapProfiler* heap_profiler = env.isolate ()->GetHeapProfiler ();
4362+
4363+ // Suppress randomness to avoid flakiness in tests.
4364+ i::v8_flags.sampling_heap_profiler_suppress_randomness = true ;
4365+
4366+ heap_profiler->StartSamplingHeapProfiler (
4367+ 64 , 16 ,
4368+ static_cast <v8::HeapProfiler::SamplingFlags>(
4369+ v8::HeapProfiler::kSamplingForceGC |
4370+ v8::HeapProfiler::kSamplingIncludeObjectsCollectedByMajorGC ));
4371+
4372+ // Allocate objects that will be retained
4373+ CompileRun (
4374+ " var retained = [];\n "
4375+ " for (var i = 0; i < 500; i++) retained.push(new Array(10));\n " );
4376+
4377+ CompileRun (" for (var i = 0; i < 500; i++) new Array(10);\n " );
4378+
4379+ std::unique_ptr<v8::AllocationProfile> profile (
4380+ heap_profiler->GetAllocationProfile ());
4381+ CHECK (profile);
4382+
4383+ const auto & samples = profile->GetSamples ();
4384+ CHECK (!samples.empty ());
4385+
4386+ int live_samples = 0 ;
4387+ int dead_samples = 0 ;
4388+ for (const auto & sample : samples) {
4389+ if (sample.is_live ) {
4390+ ++live_samples;
4391+ } else {
4392+ ++dead_samples;
4393+ }
4394+ }
4395+
4396+ // We expect both retained and collected allocations in this profile.
4397+ CHECK_GT (live_samples, 0 );
4398+ CHECK_GT (dead_samples, 0 );
4399+
4400+ heap_profiler->StopSamplingHeapProfiler ();
4401+ }
4402+
43274403TEST (HeapSnapshotPrototypeNotJSReceiver) {
43284404 LocalContext env;
43294405 v8::HandleScope scope (env.isolate ());
0 commit comments