Skip to content

Commit 120b753

Browse files
a1phCommit bot
authored andcommitted
Introduce v8::CpuProfiler::New and v8::CpuProfiler::Dispose API.
Isolate is not going to retain a CPU profiler. The client will be creating an instance of profiler when needed. Deprectate v8::Isolate::GetCpuProfiler() BUG=v8:4789 Review-Url: https://codereview.chromium.org/2117343006 Cr-Commit-Position: refs/heads/master@{#37613}
1 parent 29b89b4 commit 120b753

9 files changed

Lines changed: 183 additions & 121 deletions

File tree

include/v8-profiler.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,24 @@ class V8_EXPORT CpuProfile {
207207
void Delete();
208208
};
209209

210-
211210
/**
212211
* Interface for controlling CPU profiling. Instance of the
213-
* profiler can be retrieved using v8::Isolate::GetCpuProfiler.
212+
* profiler can be created using v8::CpuProfiler::New method.
214213
*/
215214
class V8_EXPORT CpuProfiler {
216215
public:
216+
/**
217+
* Creates a new CPU profiler for the |isolate|. The isolate must be
218+
* initialized. The profiler object must be disposed after use by calling
219+
* |Dispose| method.
220+
*/
221+
static CpuProfiler* New(Isolate* isolate);
222+
223+
/**
224+
* Disposes the CPU profiler object.
225+
*/
226+
void Dispose();
227+
217228
/**
218229
* Changes default CPU profiler sampling interval to the specified number
219230
* of microseconds. Default interval is 1000us. This method must be called

include/v8.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5932,7 +5932,8 @@ class V8_EXPORT Isolate {
59325932
* is initialized. It is the embedder's responsibility to stop all CPU
59335933
* profiling activities if it has started any.
59345934
*/
5935-
CpuProfiler* GetCpuProfiler();
5935+
V8_DEPRECATE_SOON("CpuProfiler should be created with CpuProfiler::New call.",
5936+
CpuProfiler* GetCpuProfiler());
59365937

59375938
/** Returns true if this isolate has a current context. */
59385939
bool InContext();

src/api.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8496,6 +8496,12 @@ int CpuProfile::GetSamplesCount() const {
84968496
return reinterpret_cast<const i::CpuProfile*>(this)->samples_count();
84978497
}
84988498

8499+
CpuProfiler* CpuProfiler::New(Isolate* isolate) {
8500+
return reinterpret_cast<CpuProfiler*>(
8501+
new i::CpuProfiler(reinterpret_cast<i::Isolate*>(isolate)));
8502+
}
8503+
8504+
void CpuProfiler::Dispose() { delete reinterpret_cast<i::CpuProfiler*>(this); }
84998505

85008506
void CpuProfiler::SetSamplingInterval(int us) {
85018507
DCHECK_GE(us, 0);

src/isolate.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,6 @@ class Isolate {
921921
CodeEventDispatcher* code_event_dispatcher() const {
922922
return code_event_dispatcher_.get();
923923
}
924-
CpuProfiler* cpu_profiler() const { return cpu_profiler_; }
925924
HeapProfiler* heap_profiler() const { return heap_profiler_; }
926925

927926
#ifdef DEBUG
@@ -1264,6 +1263,10 @@ class Isolate {
12641263
return "";
12651264
}
12661265

1266+
// TODO(alph): Remove along with the deprecated GetCpuProfiler().
1267+
friend v8::CpuProfiler* v8::Isolate::GetCpuProfiler();
1268+
CpuProfiler* cpu_profiler() const { return cpu_profiler_; }
1269+
12671270
base::Atomic32 id_;
12681271
EntryStackItem* entry_stack_;
12691272
int stack_trace_nesting_level_;

test/cctest/profiler-extension.cc

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
namespace v8 {
3434
namespace internal {
3535

36-
v8::CpuProfile* ProfilerExtension::last_profile = NULL;
36+
v8::CpuProfiler* ProfilerExtension::profiler_ = nullptr;
37+
v8::CpuProfile* ProfilerExtension::last_profile = nullptr;
3738
const char* ProfilerExtension::kSource =
3839
"native function startProfiling();"
3940
"native function stopProfiling();"
@@ -58,24 +59,22 @@ v8::Local<v8::FunctionTemplate> ProfilerExtension::GetNativeFunctionTemplate(
5859

5960
void ProfilerExtension::StartProfiling(
6061
const v8::FunctionCallbackInfo<v8::Value>& args) {
61-
last_profile = NULL;
62-
v8::CpuProfiler* cpu_profiler = args.GetIsolate()->GetCpuProfiler();
63-
cpu_profiler->StartProfiling((args.Length() > 0)
64-
? args[0].As<v8::String>()
65-
: v8::String::Empty(args.GetIsolate()));
62+
last_profile = nullptr;
63+
profiler_->StartProfiling(args.Length() > 0
64+
? args[0].As<v8::String>()
65+
: v8::String::Empty(args.GetIsolate()));
6666
}
6767

6868
void ProfilerExtension::StopProfiling(
6969
const v8::FunctionCallbackInfo<v8::Value>& args) {
70-
v8::CpuProfiler* cpu_profiler = args.GetIsolate()->GetCpuProfiler();
71-
last_profile = cpu_profiler->StopProfiling((args.Length() > 0)
72-
? args[0].As<v8::String>()
73-
: v8::String::Empty(args.GetIsolate()));
70+
last_profile = profiler_->StopProfiling(
71+
args.Length() > 0 ? args[0].As<v8::String>()
72+
: v8::String::Empty(args.GetIsolate()));
7473
}
7574

7675
void ProfilerExtension::CollectSample(
7776
const v8::FunctionCallbackInfo<v8::Value>& args) {
78-
args.GetIsolate()->GetCpuProfiler()->CollectSample();
77+
profiler_->CollectSample();
7978
}
8079

8180
} // namespace internal

test/cctest/profiler-extension.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,28 @@
3535
namespace v8 {
3636
namespace internal {
3737

38+
class CpuProfiler;
39+
3840
class ProfilerExtension : public v8::Extension {
3941
public:
4042
ProfilerExtension() : v8::Extension("v8/profiler", kSource) { }
43+
4144
virtual v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate(
4245
v8::Isolate* isolate, v8::Local<v8::String> name);
46+
47+
static void set_profiler(v8::CpuProfiler* profiler) { profiler_ = profiler; }
48+
static void set_profiler(CpuProfiler* profiler) {
49+
profiler_ = reinterpret_cast<v8::CpuProfiler*>(profiler);
50+
}
51+
static v8::CpuProfiler* profiler() { return profiler_; }
4352
static v8::CpuProfile* last_profile;
4453

4554
private:
4655
static void StartProfiling(const v8::FunctionCallbackInfo<v8::Value>& args);
4756
static void StopProfiling(const v8::FunctionCallbackInfo<v8::Value>& args);
4857
static void CollectSample(const v8::FunctionCallbackInfo<v8::Value>& args);
4958

59+
static v8::CpuProfiler* profiler_;
5060
static const char* kSource;
5161
};
5262

test/cctest/test-api.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ void RunWithProfiler(void (*test)()) {
9696
LocalContext env;
9797
v8::HandleScope scope(env->GetIsolate());
9898
v8::Local<v8::String> profile_name = v8_str("my_profile1");
99-
v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler();
100-
99+
v8::CpuProfiler* cpu_profiler = v8::CpuProfiler::New(env->GetIsolate());
101100
cpu_profiler->StartProfiling(profile_name);
102101
(*test)();
103102
reinterpret_cast<i::CpuProfiler*>(cpu_profiler)->DeleteAllProfiles();
103+
cpu_profiler->Dispose();
104104
}
105105

106106

0 commit comments

Comments
 (0)