Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: third_party/WebKit/Source/platform/v8_inspector/V8ProfilerAgentImpl.cpp

Issue 1702673002: DevTools: migrate remote debugging protocol generators to jinja2. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/V8DebuggerImpl.h" 7 #include "platform/v8_inspector/V8DebuggerImpl.h"
8 #include "platform/v8_inspector/V8StackTraceImpl.h" 8 #include "platform/v8_inspector/V8StackTraceImpl.h"
9 #include "platform/v8_inspector/V8StringUtil.h" 9 #include "platform/v8_inspector/V8StringUtil.h"
10 #include "wtf/Atomics.h" 10 #include "wtf/Atomics.h"
11 #include <v8-profiler.h> 11 #include <v8-profiler.h>
12 12
13 namespace blink { 13 namespace blink {
14 14
15 namespace ProfilerAgentState { 15 namespace ProfilerAgentState {
16 static const char samplingInterval[] = "samplingInterval"; 16 static const char samplingInterval[] = "samplingInterval";
17 static const char userInitiatedProfiling[] = "userInitiatedProfiling"; 17 static const char userInitiatedProfiling[] = "userInitiatedProfiling";
18 } 18 }
19 19
20 namespace { 20 namespace {
21 21
22 PassRefPtr<protocol::TypeBuilder::Array<protocol::TypeBuilder::Profiler::Positio nTickInfo>> buildInspectorObjectForPositionTicks(const v8::CpuProfileNode* node) 22 PassOwnPtr<protocol::Array<protocol::Profiler::PositionTickInfo>> buildInspector ObjectForPositionTicks(const v8::CpuProfileNode* node)
23 { 23 {
24 RefPtr<protocol::TypeBuilder::Array<protocol::TypeBuilder::Profiler::Positio nTickInfo>> array = protocol::TypeBuilder::Array<protocol::TypeBuilder::Profiler ::PositionTickInfo>::create(); 24 OwnPtr<protocol::Array<protocol::Profiler::PositionTickInfo>> array = protoc ol::Array<protocol::Profiler::PositionTickInfo>::create();
25 unsigned lineCount = node->GetHitLineCount(); 25 unsigned lineCount = node->GetHitLineCount();
26 if (!lineCount) 26 if (!lineCount)
27 return array.release(); 27 return array.release();
28 28
29 Vector<v8::CpuProfileNode::LineTick> entries(lineCount); 29 Vector<v8::CpuProfileNode::LineTick> entries(lineCount);
30 if (node->GetLineTicks(&entries[0], lineCount)) { 30 if (node->GetLineTicks(&entries[0], lineCount)) {
31 for (unsigned i = 0; i < lineCount; i++) { 31 for (unsigned i = 0; i < lineCount; i++) {
32 RefPtr<protocol::TypeBuilder::Profiler::PositionTickInfo> line = pro tocol::TypeBuilder::Profiler::PositionTickInfo::create() 32 OwnPtr<protocol::Profiler::PositionTickInfo> line = protocol::Profil er::PositionTickInfo::create()
33 .setLine(entries[i].line) 33 .setLine(entries[i].line)
34 .setTicks(entries[i].hit_count); 34 .setTicks(entries[i].hit_count).build();
35 array->addItem(line); 35 array->addItem(line.release());
36 } 36 }
37 } 37 }
38 38
39 return array.release(); 39 return array.release();
40 } 40 }
41 41
42 PassRefPtr<protocol::TypeBuilder::Profiler::CPUProfileNode> buildInspectorObject For(v8::Isolate* isolate, const v8::CpuProfileNode* node) 42 PassOwnPtr<protocol::Profiler::CPUProfileNode> buildInspectorObjectFor(v8::Isola te* isolate, const v8::CpuProfileNode* node)
43 { 43 {
44 v8::HandleScope handleScope(isolate); 44 v8::HandleScope handleScope(isolate);
45 45
46 RefPtr<protocol::TypeBuilder::Array<protocol::TypeBuilder::Profiler::CPUProf ileNode>> children = protocol::TypeBuilder::Array<protocol::TypeBuilder::Profile r::CPUProfileNode>::create(); 46 OwnPtr<protocol::Array<protocol::Profiler::CPUProfileNode>> children = proto col::Array<protocol::Profiler::CPUProfileNode>::create();
47 const int childrenCount = node->GetChildrenCount(); 47 const int childrenCount = node->GetChildrenCount();
48 for (int i = 0; i < childrenCount; i++) { 48 for (int i = 0; i < childrenCount; i++) {
49 const v8::CpuProfileNode* child = node->GetChild(i); 49 const v8::CpuProfileNode* child = node->GetChild(i);
50 children->addItem(buildInspectorObjectFor(isolate, child)); 50 children->addItem(buildInspectorObjectFor(isolate, child));
51 } 51 }
52 52
53 RefPtr<protocol::TypeBuilder::Array<protocol::TypeBuilder::Profiler::Positio nTickInfo>> positionTicks = buildInspectorObjectForPositionTicks(node); 53 OwnPtr<protocol::Array<protocol::Profiler::PositionTickInfo>> positionTicks = buildInspectorObjectForPositionTicks(node);
54 54
55 RefPtr<protocol::TypeBuilder::Profiler::CPUProfileNode> result = protocol::T ypeBuilder::Profiler::CPUProfileNode::create() 55 OwnPtr<protocol::Profiler::CPUProfileNode> result = protocol::Profiler::CPUP rofileNode::create()
56 .setFunctionName(toWTFString(node->GetFunctionName())) 56 .setFunctionName(toWTFString(node->GetFunctionName()))
57 .setScriptId(String::number(node->GetScriptId())) 57 .setScriptId(String::number(node->GetScriptId()))
58 .setUrl(toWTFString(node->GetScriptResourceName())) 58 .setUrl(toWTFString(node->GetScriptResourceName()))
59 .setLineNumber(node->GetLineNumber()) 59 .setLineNumber(node->GetLineNumber())
60 .setColumnNumber(node->GetColumnNumber()) 60 .setColumnNumber(node->GetColumnNumber())
61 .setHitCount(node->GetHitCount()) 61 .setHitCount(node->GetHitCount())
62 .setCallUID(node->GetCallUid()) 62 .setCallUID(node->GetCallUid())
63 .setChildren(children.release()) 63 .setChildren(children.release())
64 .setPositionTicks(positionTicks.release()) 64 .setPositionTicks(positionTicks.release())
65 .setDeoptReason(node->GetBailoutReason()) 65 .setDeoptReason(node->GetBailoutReason())
66 .setId(node->GetNodeId()); 66 .setId(node->GetNodeId()).build();
67 return result.release(); 67 return result.release();
68 } 68 }
69 69
70 PassRefPtr<protocol::TypeBuilder::Array<int>> buildInspectorObjectForSamples(v8: :CpuProfile* v8profile) 70 PassOwnPtr<protocol::Array<int>> buildInspectorObjectForSamples(v8::CpuProfile* v8profile)
71 { 71 {
72 RefPtr<protocol::TypeBuilder::Array<int>> array = protocol::TypeBuilder::Arr ay<int>::create(); 72 OwnPtr<protocol::Array<int>> array = protocol::Array<int>::create();
73 int count = v8profile->GetSamplesCount(); 73 int count = v8profile->GetSamplesCount();
74 for (int i = 0; i < count; i++) 74 for (int i = 0; i < count; i++)
75 array->addItem(v8profile->GetSample(i)->GetNodeId()); 75 array->addItem(v8profile->GetSample(i)->GetNodeId());
76 return array.release(); 76 return array.release();
77 } 77 }
78 78
79 PassRefPtr<protocol::TypeBuilder::Array<double>> buildInspectorObjectForTimestam ps(v8::CpuProfile* v8profile) 79 PassOwnPtr<protocol::Array<double>> buildInspectorObjectForTimestamps(v8::CpuPro file* v8profile)
80 { 80 {
81 RefPtr<protocol::TypeBuilder::Array<double>> array = protocol::TypeBuilder:: Array<double>::create(); 81 OwnPtr<protocol::Array<double>> array = protocol::Array<double>::create();
82 int count = v8profile->GetSamplesCount(); 82 int count = v8profile->GetSamplesCount();
83 for (int i = 0; i < count; i++) 83 for (int i = 0; i < count; i++)
84 array->addItem(v8profile->GetSampleTimestamp(i)); 84 array->addItem(v8profile->GetSampleTimestamp(i));
85 return array.release(); 85 return array.release();
86 } 86 }
87 87
88 PassRefPtr<protocol::TypeBuilder::Profiler::CPUProfile> createCPUProfile(v8::Iso late* isolate, v8::CpuProfile* v8profile) 88 PassOwnPtr<protocol::Profiler::CPUProfile> createCPUProfile(v8::Isolate* isolate , v8::CpuProfile* v8profile)
89 { 89 {
90 RefPtr<protocol::TypeBuilder::Profiler::CPUProfile> profile = protocol::Type Builder::Profiler::CPUProfile::create() 90 OwnPtr<protocol::Profiler::CPUProfile> profile = protocol::Profiler::CPUProf ile::create()
91 .setHead(buildInspectorObjectFor(isolate, v8profile->GetTopDownRoot())) 91 .setHead(buildInspectorObjectFor(isolate, v8profile->GetTopDownRoot()))
92 .setStartTime(static_cast<double>(v8profile->GetStartTime()) / 1000000) 92 .setStartTime(static_cast<double>(v8profile->GetStartTime()) / 1000000)
93 .setEndTime(static_cast<double>(v8profile->GetEndTime()) / 1000000); 93 .setEndTime(static_cast<double>(v8profile->GetEndTime()) / 1000000).buil d();
94 profile->setSamples(buildInspectorObjectForSamples(v8profile)); 94 profile->setSamples(buildInspectorObjectForSamples(v8profile));
95 profile->setTimestamps(buildInspectorObjectForTimestamps(v8profile)); 95 profile->setTimestamps(buildInspectorObjectForTimestamps(v8profile));
96 return profile.release(); 96 return profile.release();
97 } 97 }
98 98
99 PassRefPtr<protocol::TypeBuilder::Debugger::Location> currentDebugLocation(V8Deb uggerImpl* debugger) 99 PassOwnPtr<protocol::Debugger::Location> currentDebugLocation(V8DebuggerImpl* de bugger)
100 { 100 {
101 OwnPtr<V8StackTrace> callStack = debugger->captureStackTrace(1); 101 OwnPtr<V8StackTrace> callStack = debugger->captureStackTrace(1);
102 RefPtr<protocol::TypeBuilder::Debugger::Location> location = protocol::TypeB uilder::Debugger::Location::create() 102 OwnPtr<protocol::Debugger::Location> location = protocol::Debugger::Location ::create()
103 .setScriptId(callStack->topScriptId()) 103 .setScriptId(callStack->topScriptId())
104 .setLineNumber(callStack->topLineNumber()); 104 .setLineNumber(callStack->topLineNumber()).build();
105 location->setColumnNumber(callStack->topColumnNumber()); 105 location->setColumnNumber(callStack->topColumnNumber());
106 return location.release(); 106 return location.release();
107 } 107 }
108 108
109 volatile int s_lastProfileId = 0; 109 volatile int s_lastProfileId = 0;
110 110
111 } // namespace 111 } // namespace
112 112
113 class V8ProfilerAgentImpl::ProfileDescriptor { 113 class V8ProfilerAgentImpl::ProfileDescriptor {
114 public: 114 public:
(...skipping 22 matching lines...) Expand all
137 V8ProfilerAgentImpl::~V8ProfilerAgentImpl() 137 V8ProfilerAgentImpl::~V8ProfilerAgentImpl()
138 { 138 {
139 } 139 }
140 140
141 void V8ProfilerAgentImpl::consoleProfile(const String& title) 141 void V8ProfilerAgentImpl::consoleProfile(const String& title)
142 { 142 {
143 ASSERT(m_frontend && m_enabled); 143 ASSERT(m_frontend && m_enabled);
144 String id = nextProfileId(); 144 String id = nextProfileId();
145 m_startedProfiles.append(ProfileDescriptor(id, title)); 145 m_startedProfiles.append(ProfileDescriptor(id, title));
146 startProfiling(id); 146 startProfiling(id);
147 m_frontend->consoleProfileStarted(id, currentDebugLocation(m_debugger), titl e.isNull() ? 0 : &title); 147 m_frontend->consoleProfileStarted(id, currentDebugLocation(m_debugger), titl e);
148 } 148 }
149 149
150 void V8ProfilerAgentImpl::consoleProfileEnd(const String& title) 150 void V8ProfilerAgentImpl::consoleProfileEnd(const String& title)
151 { 151 {
152 ASSERT(m_frontend && m_enabled); 152 ASSERT(m_frontend && m_enabled);
153 String id; 153 String id;
154 String resolvedTitle; 154 String resolvedTitle;
155 // Take last started profile if no title was passed. 155 // Take last started profile if no title was passed.
156 if (title.isNull()) { 156 if (title.isNull()) {
157 if (m_startedProfiles.isEmpty()) 157 if (m_startedProfiles.isEmpty())
158 return; 158 return;
159 id = m_startedProfiles.last().m_id; 159 id = m_startedProfiles.last().m_id;
160 resolvedTitle = m_startedProfiles.last().m_title; 160 resolvedTitle = m_startedProfiles.last().m_title;
161 m_startedProfiles.removeLast(); 161 m_startedProfiles.removeLast();
162 } else { 162 } else {
163 for (size_t i = 0; i < m_startedProfiles.size(); i++) { 163 for (size_t i = 0; i < m_startedProfiles.size(); i++) {
164 if (m_startedProfiles[i].m_title == title) { 164 if (m_startedProfiles[i].m_title == title) {
165 resolvedTitle = title; 165 resolvedTitle = title;
166 id = m_startedProfiles[i].m_id; 166 id = m_startedProfiles[i].m_id;
167 m_startedProfiles.remove(i); 167 m_startedProfiles.remove(i);
168 break; 168 break;
169 } 169 }
170 } 170 }
171 if (id.isEmpty()) 171 if (id.isEmpty())
172 return; 172 return;
173 } 173 }
174 RefPtr<protocol::TypeBuilder::Profiler::CPUProfile> profile = stopProfiling( id, true); 174 OwnPtr<protocol::Profiler::CPUProfile> profile = stopProfiling(id, true);
175 if (!profile) 175 if (!profile)
176 return; 176 return;
177 RefPtr<protocol::TypeBuilder::Debugger::Location> location = currentDebugLoc ation(m_debugger); 177 OwnPtr<protocol::Debugger::Location> location = currentDebugLocation(m_debug ger);
178 m_frontend->consoleProfileFinished(id, location, profile, resolvedTitle.isNu ll() ? 0 : &resolvedTitle); 178 m_frontend->consoleProfileFinished(id, location.release(), profile.release() , resolvedTitle);
179 } 179 }
180 180
181 void V8ProfilerAgentImpl::enable(ErrorString*) 181 void V8ProfilerAgentImpl::enable(ErrorString*)
182 { 182 {
183 m_enabled = true; 183 m_enabled = true;
184 } 184 }
185 185
186 void V8ProfilerAgentImpl::disable(ErrorString*) 186 void V8ProfilerAgentImpl::disable(ErrorString* errorString)
187 { 187 {
188 for (Vector<ProfileDescriptor>::reverse_iterator it = m_startedProfiles.rbeg in(); it != m_startedProfiles.rend(); ++it) 188 for (Vector<ProfileDescriptor>::reverse_iterator it = m_startedProfiles.rbeg in(); it != m_startedProfiles.rend(); ++it)
189 stopProfiling(it->m_id, false); 189 stopProfiling(it->m_id, false);
190 m_startedProfiles.clear(); 190 m_startedProfiles.clear();
191 stop(0, 0); 191 stop(nullptr, nullptr);
192 m_enabled = false; 192 m_enabled = false;
193 } 193 }
194 194
195 void V8ProfilerAgentImpl::setSamplingInterval(ErrorString* error, int interval) 195 void V8ProfilerAgentImpl::setSamplingInterval(ErrorString* error, int interval)
196 { 196 {
197 if (m_recordingCPUProfile) { 197 if (m_recordingCPUProfile) {
198 *error = "Cannot change sampling interval when profiling."; 198 *error = "Cannot change sampling interval when profiling.";
199 return; 199 return;
200 } 200 }
201 m_state->setNumber(ProfilerAgentState::samplingInterval, interval); 201 m_state->setNumber(ProfilerAgentState::samplingInterval, interval);
(...skipping 28 matching lines...) Expand all
230 if (!m_enabled) { 230 if (!m_enabled) {
231 *error = "Profiler is not enabled"; 231 *error = "Profiler is not enabled";
232 return; 232 return;
233 } 233 }
234 m_recordingCPUProfile = true; 234 m_recordingCPUProfile = true;
235 m_frontendInitiatedProfileId = nextProfileId(); 235 m_frontendInitiatedProfileId = nextProfileId();
236 startProfiling(m_frontendInitiatedProfileId); 236 startProfiling(m_frontendInitiatedProfileId);
237 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, true); 237 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, true);
238 } 238 }
239 239
240 void V8ProfilerAgentImpl::stop(ErrorString* errorString, RefPtr<protocol::TypeBu ilder::Profiler::CPUProfile>& profile) 240 void V8ProfilerAgentImpl::stop(ErrorString* errorString, OwnPtr<protocol::Profil er::CPUProfile>* profile)
241 {
242 stop(errorString, &profile);
243 }
244
245 void V8ProfilerAgentImpl::stop(ErrorString* errorString, RefPtr<protocol::TypeBu ilder::Profiler::CPUProfile>* profile)
246 { 241 {
247 if (!m_recordingCPUProfile) { 242 if (!m_recordingCPUProfile) {
248 if (errorString) 243 if (errorString)
249 *errorString = "No recording profiles found"; 244 *errorString = "No recording profiles found";
250 return; 245 return;
251 } 246 }
252 m_recordingCPUProfile = false; 247 m_recordingCPUProfile = false;
253 RefPtr<protocol::TypeBuilder::Profiler::CPUProfile> cpuProfile = stopProfili ng(m_frontendInitiatedProfileId, !!profile); 248 OwnPtr<protocol::Profiler::CPUProfile> cpuProfile = stopProfiling(m_frontend InitiatedProfileId, !!profile);
254 if (profile) { 249 if (profile) {
255 *profile = cpuProfile; 250 *profile = cpuProfile.release();
256 if (!cpuProfile && errorString) 251 if (!profile->get() && errorString)
257 *errorString = "Profile is not found"; 252 *errorString = "Profile is not found";
258 } 253 }
259 m_frontendInitiatedProfileId = String(); 254 m_frontendInitiatedProfileId = String();
260 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, false); 255 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, false);
261 } 256 }
262 257
263 String V8ProfilerAgentImpl::nextProfileId() 258 String V8ProfilerAgentImpl::nextProfileId()
264 { 259 {
265 return String::number(atomicIncrement(&s_lastProfileId)); 260 return String::number(atomicIncrement(&s_lastProfileId));
266 } 261 }
267 262
268 void V8ProfilerAgentImpl::startProfiling(const String& title) 263 void V8ProfilerAgentImpl::startProfiling(const String& title)
269 { 264 {
270 v8::HandleScope handleScope(m_isolate); 265 v8::HandleScope handleScope(m_isolate);
271 m_isolate->GetCpuProfiler()->StartProfiling(toV8String(m_isolate, title), tr ue); 266 m_isolate->GetCpuProfiler()->StartProfiling(toV8String(m_isolate, title), tr ue);
272 } 267 }
273 268
274 PassRefPtr<protocol::TypeBuilder::Profiler::CPUProfile> V8ProfilerAgentImpl::sto pProfiling(const String& title, bool serialize) 269 PassOwnPtr<protocol::Profiler::CPUProfile> V8ProfilerAgentImpl::stopProfiling(co nst String& title, bool serialize)
275 { 270 {
276 v8::HandleScope handleScope(m_isolate); 271 v8::HandleScope handleScope(m_isolate);
277 v8::CpuProfile* profile = m_isolate->GetCpuProfiler()->StopProfiling(toV8Str ing(m_isolate, title)); 272 v8::CpuProfile* profile = m_isolate->GetCpuProfiler()->StopProfiling(toV8Str ing(m_isolate, title));
278 if (!profile) 273 if (!profile)
279 return nullptr; 274 return nullptr;
280 RefPtr<protocol::TypeBuilder::Profiler::CPUProfile> result; 275 OwnPtr<protocol::Profiler::CPUProfile> result;
281 if (serialize) 276 if (serialize)
282 result = createCPUProfile(m_isolate, profile); 277 result = createCPUProfile(m_isolate, profile);
283 profile->Delete(); 278 profile->Delete();
284 return result.release(); 279 return result.release();
285 } 280 }
286 281
287 bool V8ProfilerAgentImpl::isRecording() const 282 bool V8ProfilerAgentImpl::isRecording() const
288 { 283 {
289 return m_recordingCPUProfile || !m_startedProfiles.isEmpty(); 284 return m_recordingCPUProfile || !m_startedProfiles.isEmpty();
290 } 285 }
291 286
292 void V8ProfilerAgentImpl::idleFinished() 287 void V8ProfilerAgentImpl::idleFinished()
293 { 288 {
294 if (!isRecording()) 289 if (!isRecording())
295 return; 290 return;
296 m_isolate->GetCpuProfiler()->SetIdle(false); 291 m_isolate->GetCpuProfiler()->SetIdle(false);
297 } 292 }
298 293
299 void V8ProfilerAgentImpl::idleStarted() 294 void V8ProfilerAgentImpl::idleStarted()
300 { 295 {
301 if (!isRecording()) 296 if (!isRecording())
302 return; 297 return;
303 m_isolate->GetCpuProfiler()->SetIdle(true); 298 m_isolate->GetCpuProfiler()->SetIdle(true);
304 } 299 }
305 300
306 } // namespace blink 301 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698