OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "platform/v8_inspector/V8ProfilerAgentImpl.h" | 5 #include "platform/v8_inspector/V8ProfilerAgentImpl.h" |
6 | 6 |
7 #include "platform/v8_inspector/Atomics.h" | 7 #include "platform/v8_inspector/Atomics.h" |
8 #include "platform/v8_inspector/V8DebuggerImpl.h" | 8 #include "platform/v8_inspector/V8DebuggerImpl.h" |
9 #include "platform/v8_inspector/V8InspectorSessionImpl.h" | 9 #include "platform/v8_inspector/V8InspectorSessionImpl.h" |
10 #include "platform/v8_inspector/V8StackTraceImpl.h" | 10 #include "platform/v8_inspector/V8StackTraceImpl.h" |
11 #include "platform/v8_inspector/V8StringUtil.h" | 11 #include "platform/v8_inspector/V8StringUtil.h" |
12 #include <v8-profiler.h> | 12 #include <v8-profiler.h> |
13 | 13 |
14 namespace blink { | 14 namespace blink { |
15 | 15 |
16 namespace ProfilerAgentState { | 16 namespace ProfilerAgentState { |
17 static const char samplingInterval[] = "samplingInterval"; | 17 static const char samplingInterval[] = "samplingInterval"; |
18 static const char userInitiatedProfiling[] = "userInitiatedProfiling"; | 18 static const char userInitiatedProfiling[] = "userInitiatedProfiling"; |
19 static const char profilerEnabled[] = "profilerEnabled"; | 19 static const char profilerEnabled[] = "profilerEnabled"; |
20 } | 20 } |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 PassOwnPtr<protocol::Array<protocol::Profiler::PositionTickInfo>> buildInspector
ObjectForPositionTicks(const v8::CpuProfileNode* node) | 24 PassOwnPtr<protocol::Array<protocol::Profiler::PositionTickInfo>> buildInspector
ObjectForPositionTicks(const v8::CpuProfileNode* node) |
25 { | 25 { |
26 OwnPtr<protocol::Array<protocol::Profiler::PositionTickInfo>> array = protoc
ol::Array<protocol::Profiler::PositionTickInfo>::create(); | 26 OwnPtr<protocol::Array<protocol::Profiler::PositionTickInfo>> array = protoc
ol::Array<protocol::Profiler::PositionTickInfo>::create(); |
27 unsigned lineCount = node->GetHitLineCount(); | 27 unsigned lineCount = node->GetHitLineCount(); |
28 if (!lineCount) | 28 if (!lineCount) |
29 return array.release(); | 29 return array; |
30 | 30 |
31 protocol::Vector<v8::CpuProfileNode::LineTick> entries(lineCount); | 31 protocol::Vector<v8::CpuProfileNode::LineTick> entries(lineCount); |
32 if (node->GetLineTicks(&entries[0], lineCount)) { | 32 if (node->GetLineTicks(&entries[0], lineCount)) { |
33 for (unsigned i = 0; i < lineCount; i++) { | 33 for (unsigned i = 0; i < lineCount; i++) { |
34 OwnPtr<protocol::Profiler::PositionTickInfo> line = protocol::Profil
er::PositionTickInfo::create() | 34 OwnPtr<protocol::Profiler::PositionTickInfo> line = protocol::Profil
er::PositionTickInfo::create() |
35 .setLine(entries[i].line) | 35 .setLine(entries[i].line) |
36 .setTicks(entries[i].hit_count).build(); | 36 .setTicks(entries[i].hit_count).build(); |
37 array->addItem(line.release()); | 37 array->addItem(std::move(line)); |
38 } | 38 } |
39 } | 39 } |
40 | 40 |
41 return array.release(); | 41 return array; |
42 } | 42 } |
43 | 43 |
44 PassOwnPtr<protocol::Profiler::CPUProfileNode> buildInspectorObjectFor(v8::Isola
te* isolate, const v8::CpuProfileNode* node) | 44 PassOwnPtr<protocol::Profiler::CPUProfileNode> buildInspectorObjectFor(v8::Isola
te* isolate, const v8::CpuProfileNode* node) |
45 { | 45 { |
46 v8::HandleScope handleScope(isolate); | 46 v8::HandleScope handleScope(isolate); |
47 | 47 |
48 OwnPtr<protocol::Array<protocol::Profiler::CPUProfileNode>> children = proto
col::Array<protocol::Profiler::CPUProfileNode>::create(); | 48 OwnPtr<protocol::Array<protocol::Profiler::CPUProfileNode>> children = proto
col::Array<protocol::Profiler::CPUProfileNode>::create(); |
49 const int childrenCount = node->GetChildrenCount(); | 49 const int childrenCount = node->GetChildrenCount(); |
50 for (int i = 0; i < childrenCount; i++) { | 50 for (int i = 0; i < childrenCount; i++) { |
51 const v8::CpuProfileNode* child = node->GetChild(i); | 51 const v8::CpuProfileNode* child = node->GetChild(i); |
52 children->addItem(buildInspectorObjectFor(isolate, child)); | 52 children->addItem(buildInspectorObjectFor(isolate, child)); |
53 } | 53 } |
54 | 54 |
55 OwnPtr<protocol::Array<protocol::Profiler::PositionTickInfo>> positionTicks
= buildInspectorObjectForPositionTicks(node); | 55 OwnPtr<protocol::Array<protocol::Profiler::PositionTickInfo>> positionTicks
= buildInspectorObjectForPositionTicks(node); |
56 | 56 |
57 OwnPtr<protocol::Profiler::CPUProfileNode> result = protocol::Profiler::CPUP
rofileNode::create() | 57 OwnPtr<protocol::Profiler::CPUProfileNode> result = protocol::Profiler::CPUP
rofileNode::create() |
58 .setFunctionName(toProtocolString(node->GetFunctionName())) | 58 .setFunctionName(toProtocolString(node->GetFunctionName())) |
59 .setScriptId(String16::number(node->GetScriptId())) | 59 .setScriptId(String16::number(node->GetScriptId())) |
60 .setUrl(toProtocolString(node->GetScriptResourceName())) | 60 .setUrl(toProtocolString(node->GetScriptResourceName())) |
61 .setLineNumber(node->GetLineNumber()) | 61 .setLineNumber(node->GetLineNumber()) |
62 .setColumnNumber(node->GetColumnNumber()) | 62 .setColumnNumber(node->GetColumnNumber()) |
63 .setHitCount(node->GetHitCount()) | 63 .setHitCount(node->GetHitCount()) |
64 .setCallUID(node->GetCallUid()) | 64 .setCallUID(node->GetCallUid()) |
65 .setChildren(children.release()) | 65 .setChildren(std::move(children)) |
66 .setPositionTicks(positionTicks.release()) | 66 .setPositionTicks(std::move(positionTicks)) |
67 .setDeoptReason(node->GetBailoutReason()) | 67 .setDeoptReason(node->GetBailoutReason()) |
68 .setId(node->GetNodeId()).build(); | 68 .setId(node->GetNodeId()).build(); |
69 return result.release(); | 69 return result; |
70 } | 70 } |
71 | 71 |
72 PassOwnPtr<protocol::Array<int>> buildInspectorObjectForSamples(v8::CpuProfile*
v8profile) | 72 PassOwnPtr<protocol::Array<int>> buildInspectorObjectForSamples(v8::CpuProfile*
v8profile) |
73 { | 73 { |
74 OwnPtr<protocol::Array<int>> array = protocol::Array<int>::create(); | 74 OwnPtr<protocol::Array<int>> array = protocol::Array<int>::create(); |
75 int count = v8profile->GetSamplesCount(); | 75 int count = v8profile->GetSamplesCount(); |
76 for (int i = 0; i < count; i++) | 76 for (int i = 0; i < count; i++) |
77 array->addItem(v8profile->GetSample(i)->GetNodeId()); | 77 array->addItem(v8profile->GetSample(i)->GetNodeId()); |
78 return array.release(); | 78 return array; |
79 } | 79 } |
80 | 80 |
81 PassOwnPtr<protocol::Array<double>> buildInspectorObjectForTimestamps(v8::CpuPro
file* v8profile) | 81 PassOwnPtr<protocol::Array<double>> buildInspectorObjectForTimestamps(v8::CpuPro
file* v8profile) |
82 { | 82 { |
83 OwnPtr<protocol::Array<double>> array = protocol::Array<double>::create(); | 83 OwnPtr<protocol::Array<double>> array = protocol::Array<double>::create(); |
84 int count = v8profile->GetSamplesCount(); | 84 int count = v8profile->GetSamplesCount(); |
85 for (int i = 0; i < count; i++) | 85 for (int i = 0; i < count; i++) |
86 array->addItem(v8profile->GetSampleTimestamp(i)); | 86 array->addItem(v8profile->GetSampleTimestamp(i)); |
87 return array.release(); | 87 return array; |
88 } | 88 } |
89 | 89 |
90 PassOwnPtr<protocol::Profiler::CPUProfile> createCPUProfile(v8::Isolate* isolate
, v8::CpuProfile* v8profile) | 90 PassOwnPtr<protocol::Profiler::CPUProfile> createCPUProfile(v8::Isolate* isolate
, v8::CpuProfile* v8profile) |
91 { | 91 { |
92 OwnPtr<protocol::Profiler::CPUProfile> profile = protocol::Profiler::CPUProf
ile::create() | 92 OwnPtr<protocol::Profiler::CPUProfile> profile = protocol::Profiler::CPUProf
ile::create() |
93 .setHead(buildInspectorObjectFor(isolate, v8profile->GetTopDownRoot())) | 93 .setHead(buildInspectorObjectFor(isolate, v8profile->GetTopDownRoot())) |
94 .setStartTime(static_cast<double>(v8profile->GetStartTime()) / 1000000) | 94 .setStartTime(static_cast<double>(v8profile->GetStartTime()) / 1000000) |
95 .setEndTime(static_cast<double>(v8profile->GetEndTime()) / 1000000).buil
d(); | 95 .setEndTime(static_cast<double>(v8profile->GetEndTime()) / 1000000).buil
d(); |
96 profile->setSamples(buildInspectorObjectForSamples(v8profile)); | 96 profile->setSamples(buildInspectorObjectForSamples(v8profile)); |
97 profile->setTimestamps(buildInspectorObjectForTimestamps(v8profile)); | 97 profile->setTimestamps(buildInspectorObjectForTimestamps(v8profile)); |
98 return profile.release(); | 98 return profile; |
99 } | 99 } |
100 | 100 |
101 PassOwnPtr<protocol::Debugger::Location> currentDebugLocation(V8DebuggerImpl* de
bugger) | 101 PassOwnPtr<protocol::Debugger::Location> currentDebugLocation(V8DebuggerImpl* de
bugger) |
102 { | 102 { |
103 OwnPtr<V8StackTrace> callStack = debugger->captureStackTrace(1); | 103 OwnPtr<V8StackTrace> callStack = debugger->captureStackTrace(1); |
104 OwnPtr<protocol::Debugger::Location> location = protocol::Debugger::Location
::create() | 104 OwnPtr<protocol::Debugger::Location> location = protocol::Debugger::Location
::create() |
105 .setScriptId(callStack->topScriptId()) | 105 .setScriptId(callStack->topScriptId()) |
106 .setLineNumber(callStack->topLineNumber()).build(); | 106 .setLineNumber(callStack->topLineNumber()).build(); |
107 location->setColumnNumber(callStack->topColumnNumber()); | 107 location->setColumnNumber(callStack->topColumnNumber()); |
108 return location.release(); | 108 return location; |
109 } | 109 } |
110 | 110 |
111 volatile int s_lastProfileId = 0; | 111 volatile int s_lastProfileId = 0; |
112 | 112 |
113 } // namespace | 113 } // namespace |
114 | 114 |
115 class V8ProfilerAgentImpl::ProfileDescriptor { | 115 class V8ProfilerAgentImpl::ProfileDescriptor { |
116 public: | 116 public: |
117 ProfileDescriptor(const String16& id, const String16& title) | 117 ProfileDescriptor(const String16& id, const String16& title) |
118 : m_id(id) | 118 : m_id(id) |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 break; | 169 break; |
170 } | 170 } |
171 } | 171 } |
172 if (id.isEmpty()) | 172 if (id.isEmpty()) |
173 return; | 173 return; |
174 } | 174 } |
175 OwnPtr<protocol::Profiler::CPUProfile> profile = stopProfiling(id, true); | 175 OwnPtr<protocol::Profiler::CPUProfile> profile = stopProfiling(id, true); |
176 if (!profile) | 176 if (!profile) |
177 return; | 177 return; |
178 OwnPtr<protocol::Debugger::Location> location = currentDebugLocation(m_sessi
on->debugger()); | 178 OwnPtr<protocol::Debugger::Location> location = currentDebugLocation(m_sessi
on->debugger()); |
179 m_frontend->consoleProfileFinished(id, location.release(), profile.release()
, resolvedTitle); | 179 m_frontend->consoleProfileFinished(id, std::move(location), std::move(profil
e), resolvedTitle); |
180 } | 180 } |
181 | 181 |
182 void V8ProfilerAgentImpl::enable(ErrorString*) | 182 void V8ProfilerAgentImpl::enable(ErrorString*) |
183 { | 183 { |
184 if (m_enabled) | 184 if (m_enabled) |
185 return; | 185 return; |
186 m_enabled = true; | 186 m_enabled = true; |
187 m_state->setBoolean(ProfilerAgentState::profilerEnabled, true); | 187 m_state->setBoolean(ProfilerAgentState::profilerEnabled, true); |
188 m_session->changeInstrumentationCounter(+1); | 188 m_session->changeInstrumentationCounter(+1); |
189 } | 189 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 void V8ProfilerAgentImpl::stop(ErrorString* errorString, OwnPtr<protocol::Profil
er::CPUProfile>* profile) | 254 void V8ProfilerAgentImpl::stop(ErrorString* errorString, OwnPtr<protocol::Profil
er::CPUProfile>* profile) |
255 { | 255 { |
256 if (!m_recordingCPUProfile) { | 256 if (!m_recordingCPUProfile) { |
257 if (errorString) | 257 if (errorString) |
258 *errorString = "No recording profiles found"; | 258 *errorString = "No recording profiles found"; |
259 return; | 259 return; |
260 } | 260 } |
261 m_recordingCPUProfile = false; | 261 m_recordingCPUProfile = false; |
262 OwnPtr<protocol::Profiler::CPUProfile> cpuProfile = stopProfiling(m_frontend
InitiatedProfileId, !!profile); | 262 OwnPtr<protocol::Profiler::CPUProfile> cpuProfile = stopProfiling(m_frontend
InitiatedProfileId, !!profile); |
263 if (profile) { | 263 if (profile) { |
264 *profile = cpuProfile.release(); | 264 *profile = std::move(cpuProfile); |
265 if (!profile->get() && errorString) | 265 if (!profile->get() && errorString) |
266 *errorString = "Profile is not found"; | 266 *errorString = "Profile is not found"; |
267 } | 267 } |
268 m_frontendInitiatedProfileId = String16(); | 268 m_frontendInitiatedProfileId = String16(); |
269 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, false); | 269 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, false); |
270 m_session->client()->profilingStopped(); | 270 m_session->client()->profilingStopped(); |
271 } | 271 } |
272 | 272 |
273 String16 V8ProfilerAgentImpl::nextProfileId() | 273 String16 V8ProfilerAgentImpl::nextProfileId() |
274 { | 274 { |
275 return String16::number(atomicIncrement(&s_lastProfileId)); | 275 return String16::number(atomicIncrement(&s_lastProfileId)); |
276 } | 276 } |
277 | 277 |
278 void V8ProfilerAgentImpl::startProfiling(const String16& title) | 278 void V8ProfilerAgentImpl::startProfiling(const String16& title) |
279 { | 279 { |
280 v8::HandleScope handleScope(m_isolate); | 280 v8::HandleScope handleScope(m_isolate); |
281 m_isolate->GetCpuProfiler()->StartProfiling(toV8String(m_isolate, title), tr
ue); | 281 m_isolate->GetCpuProfiler()->StartProfiling(toV8String(m_isolate, title), tr
ue); |
282 } | 282 } |
283 | 283 |
284 PassOwnPtr<protocol::Profiler::CPUProfile> V8ProfilerAgentImpl::stopProfiling(co
nst String16& title, bool serialize) | 284 PassOwnPtr<protocol::Profiler::CPUProfile> V8ProfilerAgentImpl::stopProfiling(co
nst String16& title, bool serialize) |
285 { | 285 { |
286 v8::HandleScope handleScope(m_isolate); | 286 v8::HandleScope handleScope(m_isolate); |
287 v8::CpuProfile* profile = m_isolate->GetCpuProfiler()->StopProfiling(toV8Str
ing(m_isolate, title)); | 287 v8::CpuProfile* profile = m_isolate->GetCpuProfiler()->StopProfiling(toV8Str
ing(m_isolate, title)); |
288 if (!profile) | 288 if (!profile) |
289 return nullptr; | 289 return nullptr; |
290 OwnPtr<protocol::Profiler::CPUProfile> result; | 290 OwnPtr<protocol::Profiler::CPUProfile> result; |
291 if (serialize) | 291 if (serialize) |
292 result = createCPUProfile(m_isolate, profile); | 292 result = createCPUProfile(m_isolate, profile); |
293 profile->Delete(); | 293 profile->Delete(); |
294 return result.release(); | 294 return result; |
295 } | 295 } |
296 | 296 |
297 bool V8ProfilerAgentImpl::isRecording() const | 297 bool V8ProfilerAgentImpl::isRecording() const |
298 { | 298 { |
299 return m_recordingCPUProfile || !m_startedProfiles.isEmpty(); | 299 return m_recordingCPUProfile || !m_startedProfiles.isEmpty(); |
300 } | 300 } |
301 | 301 |
302 } // namespace blink | 302 } // namespace blink |
OLD | NEW |