Skip to content

Commit 308d4e2

Browse files
hashseedCommit Bot
authored andcommitted
[cpu-profiler] Move SetIdle() to v8::Isolate
The VM state is a property of the isolate, not the CPU profiler. Having to create a v8::CpuProfiler instance in order to change the property is somewhat inefficient. See nodejs/node#18039 and nodejs/node#18534 for context. Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng;master.tryserver.blink:linux_trusty_blink_rel;master.tryserver.chromium.linux:linux_chromium_rel_ng Change-Id: I70e31deca6529bccc05a0f4ed500ee268fb63cb8 Reviewed-on: https://chromium-review.googlesource.com/900622 Commit-Queue: Yang Guo <yangguo@chromium.org> Reviewed-by: Yang Guo <yangguo@chromium.org> Cr-Commit-Position: refs/heads/master@{#51779}
1 parent e885f8a commit 308d4e2

9 files changed

Lines changed: 30 additions & 43 deletions

File tree

include/v8-profiler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ class V8_EXPORT CpuProfiler {
335335
/**
336336
* Tells the profiler whether the embedder is idle.
337337
*/
338-
void SetIdle(bool is_idle);
338+
V8_DEPRECATED("Use Isolate::SetIdle(bool) instead.",
339+
void SetIdle(bool is_idle));
339340

340341
private:
341342
CpuProfiler();

include/v8.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7283,6 +7283,11 @@ class V8_EXPORT Isolate {
72837283
V8_DEPRECATED("CpuProfiler should be created with CpuProfiler::New call.",
72847284
CpuProfiler* GetCpuProfiler());
72857285

7286+
/**
7287+
* Tells the CPU profiler whether the embedder is idle.
7288+
*/
7289+
void SetIdle(bool is_idle);
7290+
72867291
/** Returns true if this isolate has a current context. */
72877292
bool InContext();
72887293

src/api.cc

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8110,6 +8110,10 @@ CpuProfiler* Isolate::GetCpuProfiler() {
81108110
return reinterpret_cast<CpuProfiler*>(cpu_profiler);
81118111
}
81128112

8113+
void Isolate::SetIdle(bool is_idle) {
8114+
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
8115+
isolate->SetIdle(is_idle);
8116+
}
81138117

81148118
bool Isolate::InContext() {
81158119
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
@@ -10126,15 +10130,7 @@ CpuProfile* CpuProfiler::StopProfiling(Local<String> title) {
1012610130
void CpuProfiler::SetIdle(bool is_idle) {
1012710131
i::CpuProfiler* profiler = reinterpret_cast<i::CpuProfiler*>(this);
1012810132
i::Isolate* isolate = profiler->isolate();
10129-
if (!isolate->is_profiling()) return;
10130-
v8::StateTag state = isolate->current_vm_state();
10131-
DCHECK(state == v8::EXTERNAL || state == v8::IDLE);
10132-
if (isolate->js_entry_sp() != nullptr) return;
10133-
if (is_idle) {
10134-
isolate->set_current_vm_state(v8::IDLE);
10135-
} else if (state == v8::IDLE) {
10136-
isolate->set_current_vm_state(v8::EXTERNAL);
10137-
}
10133+
isolate->SetIdle(is_idle);
1013810134
}
1013910135

1014010136

src/inspector/v8-inspector-impl.cc

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -234,21 +234,9 @@ void V8InspectorImpl::resetContextGroup(int contextGroupId) {
234234
m_debugger->wasmTranslation()->Clear();
235235
}
236236

237-
void V8InspectorImpl::idleStarted() {
238-
for (auto& it : m_sessions) {
239-
for (auto& it2 : it.second) {
240-
if (it2.second->profilerAgent()->idleStarted()) return;
241-
}
242-
}
243-
}
237+
void V8InspectorImpl::idleStarted() { m_isolate->SetIdle(true); }
244238

245-
void V8InspectorImpl::idleFinished() {
246-
for (auto& it : m_sessions) {
247-
for (auto& it2 : it.second) {
248-
if (it2.second->profilerAgent()->idleFinished()) return;
249-
}
250-
}
251-
}
239+
void V8InspectorImpl::idleFinished() { m_isolate->SetIdle(false); }
252240

253241
unsigned V8InspectorImpl::exceptionThrown(
254242
v8::Local<v8::Context> context, const StringView& message,

src/inspector/v8-profiler-agent-impl.cc

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,6 @@ void V8ProfilerAgentImpl::startProfiling(const String16& title) {
476476
if (!m_startedProfilesCount) {
477477
DCHECK(!m_profiler);
478478
m_profiler = v8::CpuProfiler::New(m_isolate);
479-
m_profiler->SetIdle(m_idle);
480479
int interval =
481480
m_state->integerProperty(ProfilerAgentState::samplingInterval, 0);
482481
if (interval) m_profiler->SetSamplingInterval(interval);
@@ -503,16 +502,4 @@ std::unique_ptr<protocol::Profiler::Profile> V8ProfilerAgentImpl::stopProfiling(
503502
return result;
504503
}
505504

506-
bool V8ProfilerAgentImpl::idleStarted() {
507-
m_idle = true;
508-
if (m_profiler) m_profiler->SetIdle(m_idle);
509-
return m_profiler;
510-
}
511-
512-
bool V8ProfilerAgentImpl::idleFinished() {
513-
m_idle = false;
514-
if (m_profiler) m_profiler->SetIdle(m_idle);
515-
return m_profiler;
516-
}
517-
518505
} // namespace v8_inspector

src/inspector/v8-profiler-agent-impl.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ class V8ProfilerAgentImpl : public protocol::Profiler::Backend {
5757
void consoleProfile(const String16& title);
5858
void consoleProfileEnd(const String16& title);
5959

60-
bool idleStarted();
61-
bool idleFinished();
62-
6360
private:
6461
String16 nextProfileId();
6562

@@ -77,7 +74,6 @@ class V8ProfilerAgentImpl : public protocol::Profiler::Backend {
7774
class ProfileDescriptor;
7875
std::vector<ProfileDescriptor> m_startedProfiles;
7976
String16 m_frontendInitiatedProfileId;
80-
bool m_idle = false;
8177
int m_startedProfilesCount = 0;
8278

8379
DISALLOW_COPY_AND_ASSIGN(V8ProfilerAgentImpl);

src/isolate.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4038,6 +4038,18 @@ void Isolate::PrintWithTimestamp(const char* format, ...) {
40384038
va_end(arguments);
40394039
}
40404040

4041+
void Isolate::SetIdle(bool is_idle) {
4042+
if (!is_profiling()) return;
4043+
StateTag state = current_vm_state();
4044+
DCHECK(state == EXTERNAL || state == IDLE);
4045+
if (js_entry_sp() != nullptr) return;
4046+
if (is_idle) {
4047+
set_current_vm_state(IDLE);
4048+
} else if (state == IDLE) {
4049+
set_current_vm_state(EXTERNAL);
4050+
}
4051+
}
4052+
40414053
bool StackLimitCheck::JsHasOverflowed(uintptr_t gap) const {
40424054
StackGuard* stack_guard = isolate_->stack_guard();
40434055
#ifdef USE_SIMULATOR

src/isolate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,8 @@ class Isolate {
13541354
top_backup_incumbent_scope_ = top_backup_incumbent_scope;
13551355
}
13561356

1357+
void SetIdle(bool is_idle);
1358+
13571359
protected:
13581360
explicit Isolate(bool enable_serializer);
13591361
bool IsArrayOrObjectOrStringPrototype(Object* object);

test/cctest/test-cpu-profiler.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,11 +1651,11 @@ TEST(IdleTime) {
16511651
reinterpret_cast<i::CpuProfiler*>(cpu_profiler)->processor();
16521652

16531653
processor->AddCurrentStack(isolate, true);
1654-
cpu_profiler->SetIdle(true);
1654+
isolate->SetIdle(true);
16551655
for (int i = 0; i < 3; i++) {
16561656
processor->AddCurrentStack(isolate, true);
16571657
}
1658-
cpu_profiler->SetIdle(false);
1658+
isolate->SetIdle(false);
16591659
processor->AddCurrentStack(isolate, true);
16601660

16611661
v8::CpuProfile* profile = cpu_profiler->StopProfiling(profile_name);

0 commit comments

Comments
 (0)