From 257c543caa43aaf3149cc14907f58fe98b7fb066 Mon Sep 17 00:00:00 2001 From: Attila Szegedi Date: Tue, 24 Nov 2026 14:59:23 +0100 Subject: [PATCH] deps: V8: cherry-pick b3a92f0effa5 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Original commit message: [profiler] Move SampleContextExtractor to a CpuProfiler setter The SampleContextExtractor was passed per profile in CpuProfilingOptions, but it is bound to the events processor that all profiles of a CpuProfiler share, taken from the profile that starts the processor. A profile started while another was active therefore silently had its extractor ignored, a profile started without an extractor still received the other profile's sample contexts, and the extractor kept being invoked after the profile that installed it had stopped. None of this matched the documented per-profile contract of CpuProfile::GetSampleContext. Make the API reflect the actual binding instead: remove the extractor from CpuProfilingOptions and install it with CpuProfiler::SetSampleContextExtractor instead which, like SetSamplingInterval and SetUsePreciseSampling, must be called while no profiles are being recorded. Every profile recorded by the CpuProfiler receives its sample contexts, and embedders wanting different extractors can use a separate CpuProfiler for each. This changes the CpuProfilingOptions constructor back to its signature before the extractor was added. Bug: 572878256 Change-Id: I73b0e7c3c2744c7fbaf72b95074a4ae0e8d0f855 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/8579337 Reviewed-by: Camillo Bruni Commit-Queue: Olivier Flückiger Reviewed-by: Leszek Swirski Cr-Commit-Position: refs/heads/main@{#111365} Refs: https://github.com/v8/v8/commit/b3a92f0effa59ccaa0008a07a92d6af2f76b551d Assisted-by: Claude Signed-off-by: Attila Szegedi --- common.gypi | 2 +- deps/v8/include/v8-profiler.h | 33 ++++---- deps/v8/src/api/api.cc | 17 ++-- deps/v8/src/profiler/cpu-profiler.cc | 14 ++-- deps/v8/src/profiler/cpu-profiler.h | 7 +- deps/v8/test/cctest/test-cpu-profiler.cc | 99 +++++++++++++++++++----- 6 files changed, 123 insertions(+), 49 deletions(-) diff --git a/common.gypi b/common.gypi index 5a7ab699e5c2..02ac583bdfb2 100644 --- a/common.gypi +++ b/common.gypi @@ -50,7 +50,7 @@ # Reset this number to 0 on major V8 upgrades. # Increment by one for each non-official patch applied to deps/v8. - 'v8_embedder_string': '-node.4', + 'v8_embedder_string': '-node.5', ##### V8 defaults for Node.js ##### diff --git a/deps/v8/include/v8-profiler.h b/deps/v8/include/v8-profiler.h index ee77b9b6d9f0..4fa97f22674a 100644 --- a/deps/v8/include/v8-profiler.h +++ b/deps/v8/include/v8-profiler.h @@ -32,8 +32,9 @@ using SnapshotObjectId = uint32_t; using ProfilerId = uint32_t; /** - * Embedder-supplied callback invoked as each CPU profile sample is captured. - * The returned pointer is stored on the sample and retrievable via + * Embedder-supplied callback, installed via + * CpuProfiler::SetSampleContextExtractor, invoked as each CPU profile sample is + * captured. The returned pointer is stored on the sample and retrievable via * CpuProfile::GetSampleContext. * * The context in which it runs depends on how the sample was taken. For @@ -308,8 +309,8 @@ class V8_EXPORT CpuProfile { /** * Returns the embedder-supplied sample context for the sample at the given * index. The pointer was produced by the SampleContextExtractor installed - * on the CpuProfilingOptions used to start this profile. If no extractor - * was installed, or the extractor returned nullptr for this sample, returns + * on the CpuProfiler that recorded this profile. If no extractor was + * installed, or the extractor returned nullptr for this sample, returns * nullptr. */ void* GetSampleContext(int index) const; @@ -436,19 +437,12 @@ class V8_EXPORT CpuProfilingOptions { * \param filter_context If specified, profiles will only contain frames * using this context. Other frames will be elided. * \param profile_source Identifies the source of this CPU profile. - * \param sample_context_extractor Optional embedder callback invoked as each - * sample is captured. The returned pointer - * is stored on the sample and retrievable - * via CpuProfile::GetSampleContext. See - * SampleContextExtractor for the - * signal-safety contract. */ CpuProfilingOptions( CpuProfilingMode mode = kLeafNodeLineNumbers, unsigned max_samples = kNoSampleLimit, int sampling_interval_us = 0, MaybeLocal filter_context = MaybeLocal(), - CpuProfileSource profile_source = CpuProfileSource::kUnspecified, - SampleContextExtractor sample_context_extractor = nullptr); + CpuProfileSource profile_source = CpuProfileSource::kUnspecified); CpuProfilingOptions(CpuProfilingOptions&&) = default; CpuProfilingOptions& operator=(CpuProfilingOptions&&) = default; @@ -457,9 +451,6 @@ class V8_EXPORT CpuProfilingOptions { unsigned max_samples() const { return max_samples_; } int sampling_interval_us() const { return sampling_interval_us_; } CpuProfileSource profile_source() const { return profile_source_; } - SampleContextExtractor sample_context_extractor() const { - return sample_context_extractor_; - } private: friend class internal::CpuProfile; @@ -472,7 +463,6 @@ class V8_EXPORT CpuProfilingOptions { int sampling_interval_us_; Global filter_context_; CpuProfileSource profile_source_; - SampleContextExtractor sample_context_extractor_ = nullptr; }; /** @@ -521,6 +511,17 @@ class V8_EXPORT CpuProfiler { */ void SetUsePreciseSampling(bool); + /** + * Installs an embedder callback invoked as each sample is captured while + * this profiler is recording profiles. The pointer it returns is stored on + * the sample, and retrievable via CpuProfile::GetSampleContext from every + * profile recorded by this profiler. Embedders wanting different extractors + * should use a separate CpuProfiler for each. Passing nullptr removes the + * extractor. See SampleContextExtractor for the signal-safety contract. This + * method must be called when there are no profiles being recorded. + */ + void SetSampleContextExtractor(SampleContextExtractor extractor); + /** * Starts collecting a CPU profile. Several profiles may be collected at once. * Generates an anonymous profiler, without a String identifier. diff --git a/deps/v8/src/api/api.cc b/deps/v8/src/api/api.cc index 5dacb03acbfe..bb8f3862ea71 100644 --- a/deps/v8/src/api/api.cc +++ b/deps/v8/src/api/api.cc @@ -11779,15 +11779,15 @@ CpuProfiler* CpuProfiler::New(Isolate* v8_isolate, reinterpret_cast(v8_isolate), naming_mode, logging_mode)); } -CpuProfilingOptions::CpuProfilingOptions( - CpuProfilingMode mode, unsigned max_samples, int sampling_interval_us, - MaybeLocal filter_context, CpuProfileSource profile_source, - SampleContextExtractor sample_context_extractor) +CpuProfilingOptions::CpuProfilingOptions(CpuProfilingMode mode, + unsigned max_samples, + int sampling_interval_us, + MaybeLocal filter_context, + CpuProfileSource profile_source) : mode_(mode), max_samples_(max_samples), sampling_interval_us_(sampling_interval_us), - profile_source_(profile_source), - sample_context_extractor_(sample_context_extractor) { + profile_source_(profile_source) { if (!filter_context.IsEmpty()) { Local local_filter_context = filter_context.ToLocalChecked(); filter_context_.Reset(v8::Isolate::GetCurrent(), local_filter_context); @@ -11824,6 +11824,11 @@ void CpuProfiler::SetUsePreciseSampling(bool use_precise_sampling) { use_precise_sampling); } +void CpuProfiler::SetSampleContextExtractor(SampleContextExtractor extractor) { + reinterpret_cast(this)->set_sample_context_extractor( + extractor); +} + CpuProfilingResult CpuProfiler::Start( CpuProfilingOptions options, std::unique_ptr delegate) { diff --git a/deps/v8/src/profiler/cpu-profiler.cc b/deps/v8/src/profiler/cpu-profiler.cc index 953a91dc2e17..0a666e2e7b37 100644 --- a/deps/v8/src/profiler/cpu-profiler.cc +++ b/deps/v8/src/profiler/cpu-profiler.cc @@ -593,6 +593,12 @@ void CpuProfiler::set_use_precise_sampling(bool value) { use_precise_sampling_ = value; } +void CpuProfiler::set_sample_context_extractor( + ProfilerEventsProcessor::SampleContextExtractor extractor) { + DCHECK(!is_profiling_); + sample_context_extractor_ = extractor; +} + void CpuProfiler::ResetProfiles() { profiles_.reset(new CpuProfilesCollection(isolate_)); profiles_->set_cpu_profiler(this); @@ -662,7 +668,6 @@ CpuProfilingResult CpuProfiler::StartProfiling( CpuProfilingResult CpuProfiler::StartProfiling( const char* title, CpuProfilingOptions options, std::unique_ptr delegate) { - auto sample_context_extractor = options.sample_context_extractor(); CpuProfilingResult result = profiles_->StartProfiling(title, std::move(options), std::move(delegate)); @@ -672,7 +677,7 @@ CpuProfilingResult CpuProfiler::StartProfiling( result.status == CpuProfilingStatus::kAlreadyStarted) { TRACE_EVENT0("v8", "CpuProfiler::StartProfiling"); AdjustSamplingInterval(); - StartProcessorIfNotStarted(sample_context_extractor); + StartProcessorIfNotStarted(); // after StartProcessorIfNotStarted processor_ must exist. DCHECK(processor_); @@ -713,8 +718,7 @@ CpuProfilingResult CpuProfiler::StartProfiling( std::move(delegate)); } -void CpuProfiler::StartProcessorIfNotStarted( - ProfilerEventsProcessor::SampleContextExtractor sample_context_extractor) { +void CpuProfiler::StartProcessorIfNotStarted() { if (processor_) { processor_->AddCurrentStack(); return; @@ -733,7 +737,7 @@ void CpuProfiler::StartProcessorIfNotStarted( base::TimeDelta sampling_interval = ComputeSamplingInterval(); processor_.reset(new SamplingEventsProcessor( isolate_, symbolizer_.get(), code_observer_.get(), profiles_.get(), - sampling_interval, use_precise_sampling_, sample_context_extractor)); + sampling_interval, use_precise_sampling_, sample_context_extractor_)); is_profiling_ = true; // Enable stack sampling. diff --git a/deps/v8/src/profiler/cpu-profiler.h b/deps/v8/src/profiler/cpu-profiler.h index 519fe9662cc4..f0ee680fa2cc 100644 --- a/deps/v8/src/profiler/cpu-profiler.h +++ b/deps/v8/src/profiler/cpu-profiler.h @@ -362,6 +362,8 @@ class V8_EXPORT_PRIVATE CpuProfiler { base::TimeDelta sampling_interval() const { return base_sampling_interval_; } void set_sampling_interval(base::TimeDelta value); void set_use_precise_sampling(bool); + void set_sample_context_extractor( + ProfilerEventsProcessor::SampleContextExtractor extractor); void CollectSample(const std::optional trace_id = std::nullopt); size_t GetEstimatedMemoryUsage() const; CpuProfilingResult StartProfiling( @@ -398,8 +400,7 @@ class V8_EXPORT_PRIVATE CpuProfiler { } private: - void StartProcessorIfNotStarted( - ProfilerEventsProcessor::SampleContextExtractor sample_context_extractor); + void StartProcessorIfNotStarted(); void StopProcessor(); void ResetProfiles(); @@ -416,6 +417,8 @@ class V8_EXPORT_PRIVATE CpuProfiler { const NamingMode naming_mode_; const LoggingMode logging_mode_; bool use_precise_sampling_ = true; + ProfilerEventsProcessor::SampleContextExtractor sample_context_extractor_ = + nullptr; // Sampling interval to which per-profile sampling intervals will be clamped // to a multiple of, or used as the default if unspecified. base::TimeDelta base_sampling_interval_; diff --git a/deps/v8/test/cctest/test-cpu-profiler.cc b/deps/v8/test/cctest/test-cpu-profiler.cc index 92f5d827b1d5..0d0b8089a91e 100644 --- a/deps/v8/test/cctest/test-cpu-profiler.cc +++ b/deps/v8/test/cctest/test-cpu-profiler.cc @@ -627,9 +627,8 @@ v8::CpuProfile* ProfilerHelper::Run( v8::Local profile_name = v8_str("my_profile"); profiler_->SetSamplingInterval(20); - profiler_->StartProfiling(profile_name, {mode, max_samples, 0, context, - v8::CpuProfileSource::kUnspecified, - sample_context_extractor}); + profiler_->SetSampleContextExtractor(sample_context_extractor); + profiler_->StartProfiling(profile_name, {mode, max_samples, 0, context}); v8::internal::CpuProfiler* iprofiler = reinterpret_cast(profiler_); @@ -4352,8 +4351,8 @@ const char* sample_context_extractor_test_source = } // namespace // Tests that an embedder SampleContextExtractor installed via -// CpuProfilingOptions is invoked for each sample and its return value is -// retrievable through CpuProfile::GetSampleContext. +// CpuProfiler::SetSampleContextExtractor is invoked for each sample and its +// return value is retrievable through CpuProfile::GetSampleContext. TEST(SampleContextExtractor) { LocalContext env; v8::HandleScope scope(env.isolate()); @@ -4389,7 +4388,7 @@ TEST(SampleContextExtractor) { profile->Delete(); } -// Tests that when no extractor is installed on CpuProfilingOptions, every +// Tests that when no extractor is installed on the CpuProfiler, every // sample's GetSampleContext() returns nullptr (i.e. the default carries // through, no spurious writes). TEST(SampleContextExtractorAbsent) { @@ -4401,7 +4400,7 @@ TEST(SampleContextExtractorAbsent) { v8::Local function = GetFunction(env.local(), "start"); ProfilerHelper helper(env.local()); - // helper.Run() builds CpuProfilingOptions without an extractor. + // helper.Run() installs no extractor by default. v8::CpuProfile* profile = helper.Run(function, nullptr, 0); CHECK_GE(profile->GetSamplesCount(), kCollectSampleCount); @@ -4436,21 +4435,15 @@ TEST(SampleContextExtractorPerProfilerIsolation) { v8::CpuProfiler* profiler_a = v8::CpuProfiler::New(env.isolate()); v8::CpuProfiler* profiler_b = v8::CpuProfiler::New(env.isolate()); + profiler_a->SetSampleContextExtractor(&ExtractorA); + profiler_b->SetSampleContextExtractor(&ExtractorB); v8::Local name_a = v8_str("profile_a"); v8::Local name_b = v8_str("profile_b"); - v8::CpuProfilingResult result_a = profiler_a->Start( - name_a, - v8::CpuProfilingOptions(v8::CpuProfilingMode::kLeafNodeLineNumbers, - v8::CpuProfilingOptions::kNoSampleLimit, 0, - v8::MaybeLocal(), - v8::CpuProfileSource::kUnspecified, &ExtractorA)); - v8::CpuProfilingResult result_b = profiler_b->Start( - name_b, - v8::CpuProfilingOptions(v8::CpuProfilingMode::kLeafNodeLineNumbers, - v8::CpuProfilingOptions::kNoSampleLimit, 0, - v8::MaybeLocal(), - v8::CpuProfileSource::kUnspecified, &ExtractorB)); + v8::CpuProfilingResult result_a = + profiler_a->Start(name_a, /* record_samples */ true); + v8::CpuProfilingResult result_b = + profiler_b->Start(name_b, /* record_samples */ true); CHECK(result_a.status == v8::CpuProfilingStatus::kStarted); CHECK(result_b.status == v8::CpuProfilingStatus::kStarted); @@ -4481,6 +4474,74 @@ TEST(SampleContextExtractorPerProfilerIsolation) { profiler_b->Dispose(); } +// Tests that the extractor installed on a CpuProfiler is read anew each time +// it starts profiling, so it can be replaced or removed between sessions. +TEST(SampleContextExtractorReplaced) { + LocalContext env; + v8::HandleScope scope(env.isolate()); + + InstallCollectSampleFunction(env.local()); + CompileRun(sample_context_extractor_test_source); + v8::Local function = GetFunction(env.local(), "start"); + + ProfilerHelper helper(env.local()); + struct { + v8::SampleContextExtractor extractor; + void* expected; + } const sessions[] = { + {&ExtractorA, kExtractorASentinel}, + {&ExtractorB, kExtractorBSentinel}, + {nullptr, nullptr}, + }; + for (const auto& session : sessions) { + v8::CpuProfile* profile = helper.Run( + function, nullptr, 0, 0, 0, v8::CpuProfilingMode::kLeafNodeLineNumbers, + v8::CpuProfilingOptions::kNoSampleLimit, v8::Local(), + session.extractor); + CHECK_GE(profile->GetSamplesCount(), kCollectSampleCount); + for (int i = 0; i < profile->GetSamplesCount(); ++i) { + CHECK_EQ(session.expected, profile->GetSampleContext(i)); + } + profile->Delete(); + } +} + +// Tests that the extractor keeps being invoked for as long as the CpuProfiler +// records any profile, including after the profile that was active when it +// started profiling has stopped. +TEST(SampleContextExtractorOutlivesFirstProfile) { + LocalContext env; + v8::HandleScope scope(env.isolate()); + + InstallCollectSampleFunction(env.local()); + CompileRun(sample_context_extractor_test_source); + v8::Local function = GetFunction(env.local(), "start"); + + v8::CpuProfiler* profiler = v8::CpuProfiler::New(env.isolate()); + profiler->SetSampleContextExtractor(&ExtractorA); + + v8::Local first = v8_str("first"); + v8::Local second = v8_str("second"); + CHECK(profiler->Start(first, /* record_samples */ true).status == + v8::CpuProfilingStatus::kStarted); + CHECK(profiler->Start(second, /* record_samples */ true).status == + v8::CpuProfilingStatus::kStarted); + profiler->StopProfiling(first)->Delete(); + + function->Call(env.local(), env.local()->Global(), 0, nullptr) + .ToLocalChecked(); + + // Stopping the last profile drains all samples still queued for it. + v8::CpuProfile* profile = profiler->StopProfiling(second); + CHECK(profile); + CHECK_GE(profile->GetSamplesCount(), kCollectSampleCount); + for (int i = 0; i < profile->GetSamplesCount(); ++i) { + CHECK_EQ(kExtractorASentinel, profile->GetSampleContext(i)); + } + profile->Delete(); + profiler->Dispose(); +} + // Tests that when a native context that's being filtered is moved, we continue // to track its execution. TEST(ContextFilterMovedNativeContext) {