Skip to content

Commit ca51ae3

Browse files
cnpscNicolas Dubus
authored and
V8 LUCI CQ
committed
[api][profiler] Get StartProfiling, StopProfiling to accept integer ID rather than string
This CL adds support for interacting with CpuProfile with their integer id. A String ID is problematic because it forces an allocation when stopping or cancelling a Profiler which can happen during a GC when this is not allowed. Change-Id: I9a8e754bd67214be0bbc5ca051bcadf52bf71a68 Bug: chromium:1297283 Co-Authored-By: Nicolas Dubus <[email protected]> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3522896 Reviewed-by: Camillo Bruni <[email protected]> Auto-Submit: Corentin Pescheloche <[email protected]> Commit-Queue: Corentin Pescheloche <[email protected]> Cr-Commit-Position: refs/heads/main@{#79835}
1 parent 6cf7330 commit ca51ae3

File tree

8 files changed

+319
-93
lines changed

8 files changed

+319
-93
lines changed

include/v8-profiler.h

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum StateTag : int;
2828

2929
using NativeObject = void*;
3030
using SnapshotObjectId = uint32_t;
31+
using ProfilerId = uint32_t;
3132

3233
struct CpuProfileDeoptFrame {
3334
int script_id;
@@ -273,15 +274,33 @@ enum class CpuProfilingStatus {
273274
kErrorTooManyProfilers
274275
};
275276

277+
/**
278+
* Result from StartProfiling returning the Profiling Status, and
279+
* id of the started profiler, or 0 if profiler is not started
280+
*/
281+
struct CpuProfilingResult {
282+
const ProfilerId id;
283+
const CpuProfilingStatus status;
284+
};
285+
276286
/**
277287
* Delegate for when max samples reached and samples are discarded.
278288
*/
279289
class V8_EXPORT DiscardedSamplesDelegate {
280290
public:
281-
DiscardedSamplesDelegate() {}
291+
DiscardedSamplesDelegate() = default;
282292

283293
virtual ~DiscardedSamplesDelegate() = default;
284294
virtual void Notify() = 0;
295+
296+
ProfilerId GetId() const { return profiler_id_; }
297+
298+
private:
299+
friend internal::CpuProfile;
300+
301+
void SetId(ProfilerId id) { profiler_id_ = id; }
302+
303+
ProfilerId profiler_id_;
285304
};
286305

287306
/**
@@ -371,6 +390,45 @@ class V8_EXPORT CpuProfiler {
371390
*/
372391
void SetUsePreciseSampling(bool);
373392

393+
/**
394+
* Starts collecting a CPU profile. Several profiles may be collected at once.
395+
* Generates an anonymous profiler, without a String identifier.
396+
*/
397+
CpuProfilingResult Start(
398+
CpuProfilingOptions options,
399+
std::unique_ptr<DiscardedSamplesDelegate> delegate = nullptr);
400+
401+
/**
402+
* Starts collecting a CPU profile. Title may be an empty string. Several
403+
* profiles may be collected at once. Attempts to start collecting several
404+
* profiles with the same title are silently ignored.
405+
*/
406+
CpuProfilingResult Start(
407+
Local<String> title, CpuProfilingOptions options,
408+
std::unique_ptr<DiscardedSamplesDelegate> delegate = nullptr);
409+
410+
/**
411+
* Starts profiling with the same semantics as above, except with expanded
412+
* parameters.
413+
*
414+
* |record_samples| parameter controls whether individual samples should
415+
* be recorded in addition to the aggregated tree.
416+
*
417+
* |max_samples| controls the maximum number of samples that should be
418+
* recorded by the profiler. Samples obtained after this limit will be
419+
* discarded.
420+
*/
421+
CpuProfilingResult Start(
422+
Local<String> title, CpuProfilingMode mode, bool record_samples = false,
423+
unsigned max_samples = CpuProfilingOptions::kNoSampleLimit);
424+
425+
/**
426+
* The same as StartProfiling above, but the CpuProfilingMode defaults to
427+
* kLeafNodeLineNumbers mode, which was the previous default behavior of the
428+
* profiler.
429+
*/
430+
CpuProfilingResult Start(Local<String> title, bool record_samples = false);
431+
374432
/**
375433
* Starts collecting a CPU profile. Title may be an empty string. Several
376434
* profiles may be collected at once. Attempts to start collecting several
@@ -394,6 +452,7 @@ class V8_EXPORT CpuProfiler {
394452
CpuProfilingStatus StartProfiling(
395453
Local<String> title, CpuProfilingMode mode, bool record_samples = false,
396454
unsigned max_samples = CpuProfilingOptions::kNoSampleLimit);
455+
397456
/**
398457
* The same as StartProfiling above, but the CpuProfilingMode defaults to
399458
* kLeafNodeLineNumbers mode, which was the previous default behavior of the
@@ -402,6 +461,11 @@ class V8_EXPORT CpuProfiler {
402461
CpuProfilingStatus StartProfiling(Local<String> title,
403462
bool record_samples = false);
404463

464+
/**
465+
* Stops collecting CPU profile with a given id and returns it.
466+
*/
467+
CpuProfile* Stop(ProfilerId id);
468+
405469
/**
406470
* Stops collecting CPU profile with a given title and returns it.
407471
* If the title given is empty, finishes the last profile started.

src/api/api.cc

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9932,29 +9932,54 @@ void CpuProfiler::SetUsePreciseSampling(bool use_precise_sampling) {
99329932
use_precise_sampling);
99339933
}
99349934

9935-
CpuProfilingStatus CpuProfiler::StartProfiling(
9935+
CpuProfilingResult CpuProfiler::Start(
9936+
CpuProfilingOptions options,
9937+
std::unique_ptr<DiscardedSamplesDelegate> delegate) {
9938+
return reinterpret_cast<i::CpuProfiler*>(this)->StartProfiling(
9939+
options, std::move(delegate));
9940+
}
9941+
9942+
CpuProfilingResult CpuProfiler::Start(
99369943
Local<String> title, CpuProfilingOptions options,
99379944
std::unique_ptr<DiscardedSamplesDelegate> delegate) {
99389945
return reinterpret_cast<i::CpuProfiler*>(this)->StartProfiling(
99399946
*Utils::OpenHandle(*title), options, std::move(delegate));
99409947
}
99419948

9942-
CpuProfilingStatus CpuProfiler::StartProfiling(Local<String> title,
9943-
bool record_samples) {
9949+
CpuProfilingResult CpuProfiler::Start(Local<String> title,
9950+
bool record_samples) {
99449951
CpuProfilingOptions options(
99459952
kLeafNodeLineNumbers,
99469953
record_samples ? CpuProfilingOptions::kNoSampleLimit : 0);
99479954
return reinterpret_cast<i::CpuProfiler*>(this)->StartProfiling(
99489955
*Utils::OpenHandle(*title), options);
99499956
}
99509957

9958+
CpuProfilingResult CpuProfiler::Start(Local<String> title,
9959+
CpuProfilingMode mode,
9960+
bool record_samples,
9961+
unsigned max_samples) {
9962+
CpuProfilingOptions options(mode, record_samples ? max_samples : 0);
9963+
return reinterpret_cast<i::CpuProfiler*>(this)->StartProfiling(
9964+
*Utils::OpenHandle(*title), options);
9965+
}
9966+
9967+
CpuProfilingStatus CpuProfiler::StartProfiling(
9968+
Local<String> title, CpuProfilingOptions options,
9969+
std::unique_ptr<DiscardedSamplesDelegate> delegate) {
9970+
return Start(title, options, std::move(delegate)).status;
9971+
}
9972+
9973+
CpuProfilingStatus CpuProfiler::StartProfiling(Local<String> title,
9974+
bool record_samples) {
9975+
return Start(title, record_samples).status;
9976+
}
9977+
99519978
CpuProfilingStatus CpuProfiler::StartProfiling(Local<String> title,
99529979
CpuProfilingMode mode,
99539980
bool record_samples,
99549981
unsigned max_samples) {
9955-
CpuProfilingOptions options(mode, record_samples ? max_samples : 0);
9956-
return reinterpret_cast<i::CpuProfiler*>(this)->StartProfiling(
9957-
*Utils::OpenHandle(*title), options);
9982+
return Start(title, mode, record_samples, max_samples).status;
99589983
}
99599984

99609985
CpuProfile* CpuProfiler::StopProfiling(Local<String> title) {
@@ -9963,6 +9988,11 @@ CpuProfile* CpuProfiler::StopProfiling(Local<String> title) {
99639988
*Utils::OpenHandle(*title)));
99649989
}
99659990

9991+
CpuProfile* CpuProfiler::Stop(ProfilerId id) {
9992+
return reinterpret_cast<CpuProfile*>(
9993+
reinterpret_cast<i::CpuProfiler*>(this)->StopProfiling(id));
9994+
}
9995+
99669996
void CpuProfiler::UseDetailedSourcePositionsForProfiling(Isolate* isolate) {
99679997
reinterpret_cast<i::Isolate*>(isolate)
99689998
->SetDetailedSourcePositionsForProfiling(true);

src/profiler/cpu-profiler.cc

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -599,25 +599,31 @@ size_t CpuProfiler::GetEstimatedMemoryUsage() const {
599599
return code_observer_->GetEstimatedMemoryUsage();
600600
}
601601

602-
CpuProfilingStatus CpuProfiler::StartProfiling(
602+
CpuProfilingResult CpuProfiler::StartProfiling(
603+
CpuProfilingOptions options,
604+
std::unique_ptr<DiscardedSamplesDelegate> delegate) {
605+
return StartProfiling(nullptr, options, std::move(delegate));
606+
}
607+
608+
CpuProfilingResult CpuProfiler::StartProfiling(
603609
const char* title, CpuProfilingOptions options,
604610
std::unique_ptr<DiscardedSamplesDelegate> delegate) {
605-
StartProfilingStatus status =
611+
CpuProfilingResult result =
606612
profiles_->StartProfiling(title, options, std::move(delegate));
607613

608614
// TODO(nicodubus): Revisit logic for if we want to do anything different for
609615
// kAlreadyStarted
610-
if (status == CpuProfilingStatus::kStarted ||
611-
status == CpuProfilingStatus::kAlreadyStarted) {
616+
if (result.status == CpuProfilingStatus::kStarted ||
617+
result.status == CpuProfilingStatus::kAlreadyStarted) {
612618
TRACE_EVENT0("v8", "CpuProfiler::StartProfiling");
613619
AdjustSamplingInterval();
614620
StartProcessorIfNotStarted();
615621
}
616622

617-
return status;
623+
return result;
618624
}
619625

620-
CpuProfilingStatus CpuProfiler::StartProfiling(
626+
CpuProfilingResult CpuProfiler::StartProfiling(
621627
String title, CpuProfilingOptions options,
622628
std::unique_ptr<DiscardedSamplesDelegate> delegate) {
623629
return StartProfiling(profiles_->GetName(title), options,
@@ -651,10 +657,19 @@ void CpuProfiler::StartProcessorIfNotStarted() {
651657
}
652658

653659
CpuProfile* CpuProfiler::StopProfiling(const char* title) {
660+
CpuProfile* profile = profiles_->Lookup(title);
661+
if (profile) {
662+
return StopProfiling(profile->id());
663+
}
664+
return nullptr;
665+
}
666+
667+
CpuProfile* CpuProfiler::StopProfiling(ProfilerId id) {
654668
if (!is_profiling_) return nullptr;
655-
const bool last_profile = profiles_->IsLastProfile(title);
669+
const bool last_profile = profiles_->IsLastProfileLeft(id);
656670
if (last_profile) StopProcessor();
657-
CpuProfile* result = profiles_->StopProfiling(title);
671+
672+
CpuProfile* profile = profiles_->StopProfiling(id);
658673

659674
AdjustSamplingInterval();
660675

@@ -663,7 +678,7 @@ CpuProfile* CpuProfiler::StopProfiling(const char* title) {
663678
DisableLogging();
664679
}
665680

666-
return result;
681+
return profile;
667682
}
668683

669684
CpuProfile* CpuProfiler::StopProfiling(String title) {

src/profiler/cpu-profiler.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ class V8_EXPORT_PRIVATE CpuProfiler {
336336
static size_t GetAllProfilersMemorySize(Isolate* isolate);
337337

338338
using ProfilingMode = v8::CpuProfilingMode;
339+
using CpuProfilingResult = v8::CpuProfilingResult;
339340
using NamingMode = v8::CpuProfilingNamingMode;
340341
using LoggingMode = v8::CpuProfilingLoggingMode;
341342
using StartProfilingStatus = CpuProfilingStatus;
@@ -345,15 +346,20 @@ class V8_EXPORT_PRIVATE CpuProfiler {
345346
void set_use_precise_sampling(bool);
346347
void CollectSample();
347348
size_t GetEstimatedMemoryUsage() const;
348-
StartProfilingStatus StartProfiling(
349+
CpuProfilingResult StartProfiling(
350+
CpuProfilingOptions options = {},
351+
std::unique_ptr<DiscardedSamplesDelegate> delegate = nullptr);
352+
CpuProfilingResult StartProfiling(
349353
const char* title, CpuProfilingOptions options = {},
350354
std::unique_ptr<DiscardedSamplesDelegate> delegate = nullptr);
351-
StartProfilingStatus StartProfiling(
355+
CpuProfilingResult StartProfiling(
352356
String title, CpuProfilingOptions options = {},
353357
std::unique_ptr<DiscardedSamplesDelegate> delegate = nullptr);
354358

355359
CpuProfile* StopProfiling(const char* title);
356360
CpuProfile* StopProfiling(String title);
361+
CpuProfile* StopProfiling(ProfilerId id);
362+
357363
int GetProfilesCount();
358364
CpuProfile* GetProfile(int index);
359365
void DeleteAllProfiles();

0 commit comments

Comments
 (0)