Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -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 #####

Expand Down
33 changes: 17 additions & 16 deletions deps/v8/include/v8-profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<Context> filter_context = MaybeLocal<Context>(),
CpuProfileSource profile_source = CpuProfileSource::kUnspecified,
SampleContextExtractor sample_context_extractor = nullptr);
CpuProfileSource profile_source = CpuProfileSource::kUnspecified);

CpuProfilingOptions(CpuProfilingOptions&&) = default;
CpuProfilingOptions& operator=(CpuProfilingOptions&&) = default;
Expand All @@ -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;
Expand All @@ -472,7 +463,6 @@ class V8_EXPORT CpuProfilingOptions {
int sampling_interval_us_;
Global<Context> filter_context_;
CpuProfileSource profile_source_;
SampleContextExtractor sample_context_extractor_ = nullptr;
};

/**
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 11 additions & 6 deletions deps/v8/src/api/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11779,15 +11779,15 @@ CpuProfiler* CpuProfiler::New(Isolate* v8_isolate,
reinterpret_cast<i::Isolate*>(v8_isolate), naming_mode, logging_mode));
}

CpuProfilingOptions::CpuProfilingOptions(
CpuProfilingMode mode, unsigned max_samples, int sampling_interval_us,
MaybeLocal<Context> filter_context, CpuProfileSource profile_source,
SampleContextExtractor sample_context_extractor)
CpuProfilingOptions::CpuProfilingOptions(CpuProfilingMode mode,
unsigned max_samples,
int sampling_interval_us,
MaybeLocal<Context> 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<Context> local_filter_context = filter_context.ToLocalChecked();
filter_context_.Reset(v8::Isolate::GetCurrent(), local_filter_context);
Expand Down Expand Up @@ -11824,6 +11824,11 @@ void CpuProfiler::SetUsePreciseSampling(bool use_precise_sampling) {
use_precise_sampling);
}

void CpuProfiler::SetSampleContextExtractor(SampleContextExtractor extractor) {
reinterpret_cast<i::CpuProfiler*>(this)->set_sample_context_extractor(
extractor);
}

CpuProfilingResult CpuProfiler::Start(
CpuProfilingOptions options,
std::unique_ptr<DiscardedSamplesDelegate> delegate) {
Expand Down
14 changes: 9 additions & 5 deletions deps/v8/src/profiler/cpu-profiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -662,7 +668,6 @@ CpuProfilingResult CpuProfiler::StartProfiling(
CpuProfilingResult CpuProfiler::StartProfiling(
const char* title, CpuProfilingOptions options,
std::unique_ptr<DiscardedSamplesDelegate> delegate) {
auto sample_context_extractor = options.sample_context_extractor();
CpuProfilingResult result =
profiles_->StartProfiling(title, std::move(options), std::move(delegate));

Expand All @@ -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_);

Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions deps/v8/src/profiler/cpu-profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t> trace_id = std::nullopt);
size_t GetEstimatedMemoryUsage() const;
CpuProfilingResult StartProfiling(
Expand Down Expand Up @@ -398,8 +400,7 @@ class V8_EXPORT_PRIVATE CpuProfiler {
}

private:
void StartProcessorIfNotStarted(
ProfilerEventsProcessor::SampleContextExtractor sample_context_extractor);
void StartProcessorIfNotStarted();
void StopProcessor();
void ResetProfiles();

Expand All @@ -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_;
Expand Down
99 changes: 80 additions & 19 deletions deps/v8/test/cctest/test-cpu-profiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -627,9 +627,8 @@ v8::CpuProfile* ProfilerHelper::Run(
v8::Local<v8::String> 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<v8::internal::CpuProfiler*>(profiler_);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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) {
Expand All @@ -4401,7 +4400,7 @@ TEST(SampleContextExtractorAbsent) {
v8::Local<v8::Function> 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);
Expand Down Expand Up @@ -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<v8::String> name_a = v8_str("profile_a");
v8::Local<v8::String> 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::Context>(),
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::Context>(),
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);

Expand Down Expand Up @@ -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<v8::Function> 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<v8::Context>(),
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<v8::Function> function = GetFunction(env.local(), "start");

v8::CpuProfiler* profiler = v8::CpuProfiler::New(env.isolate());
profiler->SetSampleContextExtractor(&ExtractorA);

v8::Local<v8::String> first = v8_str("first");
v8::Local<v8::String> 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) {
Expand Down