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

Side by Side Diff: src/inspector/V8ProfilerAgentImpl.cpp

Issue 2338413003: [inspector] change implementation file extension from cpp to cc (Closed)
Patch Set: string16 -> string-16 Created 4 years, 3 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
« no previous file with comments | « src/inspector/V8ProfilerAgentImpl.h ('k') | src/inspector/V8Regex.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/inspector/V8ProfilerAgentImpl.h"
6
7 #include "src/base/atomicops.h"
8 #include "src/inspector/StringUtil.h"
9 #include "src/inspector/V8Debugger.h"
10 #include "src/inspector/V8InspectorImpl.h"
11 #include "src/inspector/V8InspectorSessionImpl.h"
12 #include "src/inspector/V8StackTraceImpl.h"
13 #include "src/inspector/protocol/Protocol.h"
14
15 #include "include/v8-profiler.h"
16
17 #include <vector>
18
19 namespace v8_inspector {
20
21 namespace ProfilerAgentState {
22 static const char samplingInterval[] = "samplingInterval";
23 static const char userInitiatedProfiling[] = "userInitiatedProfiling";
24 static const char profilerEnabled[] = "profilerEnabled";
25 }
26
27 namespace {
28
29 std::unique_ptr<protocol::Array<protocol::Profiler::PositionTickInfo>>
30 buildInspectorObjectForPositionTicks(const v8::CpuProfileNode* node) {
31 unsigned lineCount = node->GetHitLineCount();
32 if (!lineCount) return nullptr;
33 auto array = protocol::Array<protocol::Profiler::PositionTickInfo>::create();
34 std::vector<v8::CpuProfileNode::LineTick> entries(lineCount);
35 if (node->GetLineTicks(&entries[0], lineCount)) {
36 for (unsigned i = 0; i < lineCount; i++) {
37 std::unique_ptr<protocol::Profiler::PositionTickInfo> line =
38 protocol::Profiler::PositionTickInfo::create()
39 .setLine(entries[i].line)
40 .setTicks(entries[i].hit_count)
41 .build();
42 array->addItem(std::move(line));
43 }
44 }
45 return array;
46 }
47
48 std::unique_ptr<protocol::Profiler::ProfileNode> buildInspectorObjectFor(
49 v8::Isolate* isolate, const v8::CpuProfileNode* node) {
50 v8::HandleScope handleScope(isolate);
51 auto callFrame =
52 protocol::Runtime::CallFrame::create()
53 .setFunctionName(toProtocolString(node->GetFunctionName()))
54 .setScriptId(String16::fromInteger(node->GetScriptId()))
55 .setUrl(toProtocolString(node->GetScriptResourceName()))
56 .setLineNumber(node->GetLineNumber() - 1)
57 .setColumnNumber(node->GetColumnNumber() - 1)
58 .build();
59 auto result = protocol::Profiler::ProfileNode::create()
60 .setCallFrame(std::move(callFrame))
61 .setHitCount(node->GetHitCount())
62 .setId(node->GetNodeId())
63 .build();
64
65 const int childrenCount = node->GetChildrenCount();
66 if (childrenCount) {
67 auto children = protocol::Array<int>::create();
68 for (int i = 0; i < childrenCount; i++)
69 children->addItem(node->GetChild(i)->GetNodeId());
70 result->setChildren(std::move(children));
71 }
72
73 const char* deoptReason = node->GetBailoutReason();
74 if (deoptReason && deoptReason[0] && strcmp(deoptReason, "no reason"))
75 result->setDeoptReason(deoptReason);
76
77 auto positionTicks = buildInspectorObjectForPositionTicks(node);
78 if (positionTicks) result->setPositionTicks(std::move(positionTicks));
79
80 return result;
81 }
82
83 std::unique_ptr<protocol::Array<int>> buildInspectorObjectForSamples(
84 v8::CpuProfile* v8profile) {
85 auto array = protocol::Array<int>::create();
86 int count = v8profile->GetSamplesCount();
87 for (int i = 0; i < count; i++)
88 array->addItem(v8profile->GetSample(i)->GetNodeId());
89 return array;
90 }
91
92 std::unique_ptr<protocol::Array<int>> buildInspectorObjectForTimestamps(
93 v8::CpuProfile* v8profile) {
94 auto array = protocol::Array<int>::create();
95 int count = v8profile->GetSamplesCount();
96 uint64_t lastTime = v8profile->GetStartTime();
97 for (int i = 0; i < count; i++) {
98 uint64_t ts = v8profile->GetSampleTimestamp(i);
99 array->addItem(static_cast<int>(ts - lastTime));
100 lastTime = ts;
101 }
102 return array;
103 }
104
105 void flattenNodesTree(v8::Isolate* isolate, const v8::CpuProfileNode* node,
106 protocol::Array<protocol::Profiler::ProfileNode>* list) {
107 list->addItem(buildInspectorObjectFor(isolate, node));
108 const int childrenCount = node->GetChildrenCount();
109 for (int i = 0; i < childrenCount; i++)
110 flattenNodesTree(isolate, node->GetChild(i), list);
111 }
112
113 std::unique_ptr<protocol::Profiler::Profile> createCPUProfile(
114 v8::Isolate* isolate, v8::CpuProfile* v8profile) {
115 auto nodes = protocol::Array<protocol::Profiler::ProfileNode>::create();
116 flattenNodesTree(isolate, v8profile->GetTopDownRoot(), nodes.get());
117 return protocol::Profiler::Profile::create()
118 .setNodes(std::move(nodes))
119 .setStartTime(static_cast<double>(v8profile->GetStartTime()))
120 .setEndTime(static_cast<double>(v8profile->GetEndTime()))
121 .setSamples(buildInspectorObjectForSamples(v8profile))
122 .setTimeDeltas(buildInspectorObjectForTimestamps(v8profile))
123 .build();
124 }
125
126 std::unique_ptr<protocol::Debugger::Location> currentDebugLocation(
127 V8InspectorImpl* inspector) {
128 std::unique_ptr<V8StackTraceImpl> callStack =
129 inspector->debugger()->captureStackTrace(false /* fullStack */);
130 auto location = protocol::Debugger::Location::create()
131 .setScriptId(toString16(callStack->topScriptId()))
132 .setLineNumber(callStack->topLineNumber())
133 .build();
134 location->setColumnNumber(callStack->topColumnNumber());
135 return location;
136 }
137
138 volatile int s_lastProfileId = 0;
139
140 } // namespace
141
142 class V8ProfilerAgentImpl::ProfileDescriptor {
143 public:
144 ProfileDescriptor(const String16& id, const String16& title)
145 : m_id(id), m_title(title) {}
146 String16 m_id;
147 String16 m_title;
148 };
149
150 V8ProfilerAgentImpl::V8ProfilerAgentImpl(
151 V8InspectorSessionImpl* session, protocol::FrontendChannel* frontendChannel,
152 protocol::DictionaryValue* state)
153 : m_session(session),
154 m_isolate(m_session->inspector()->isolate()),
155 m_profiler(nullptr),
156 m_state(state),
157 m_frontend(frontendChannel),
158 m_enabled(false),
159 m_recordingCPUProfile(false) {}
160
161 V8ProfilerAgentImpl::~V8ProfilerAgentImpl() {
162 if (m_profiler) m_profiler->Dispose();
163 }
164
165 void V8ProfilerAgentImpl::consoleProfile(const String16& title) {
166 if (!m_enabled) return;
167 String16 id = nextProfileId();
168 m_startedProfiles.push_back(ProfileDescriptor(id, title));
169 startProfiling(id);
170 m_frontend.consoleProfileStarted(
171 id, currentDebugLocation(m_session->inspector()), title);
172 }
173
174 void V8ProfilerAgentImpl::consoleProfileEnd(const String16& title) {
175 if (!m_enabled) return;
176 String16 id;
177 String16 resolvedTitle;
178 // Take last started profile if no title was passed.
179 if (title.isEmpty()) {
180 if (m_startedProfiles.empty()) return;
181 id = m_startedProfiles.back().m_id;
182 resolvedTitle = m_startedProfiles.back().m_title;
183 m_startedProfiles.pop_back();
184 } else {
185 for (size_t i = 0; i < m_startedProfiles.size(); i++) {
186 if (m_startedProfiles[i].m_title == title) {
187 resolvedTitle = title;
188 id = m_startedProfiles[i].m_id;
189 m_startedProfiles.erase(m_startedProfiles.begin() + i);
190 break;
191 }
192 }
193 if (id.isEmpty()) return;
194 }
195 std::unique_ptr<protocol::Profiler::Profile> profile =
196 stopProfiling(id, true);
197 if (!profile) return;
198 std::unique_ptr<protocol::Debugger::Location> location =
199 currentDebugLocation(m_session->inspector());
200 m_frontend.consoleProfileFinished(id, std::move(location), std::move(profile),
201 resolvedTitle);
202 }
203
204 void V8ProfilerAgentImpl::enable(ErrorString*) {
205 if (m_enabled) return;
206 m_enabled = true;
207 DCHECK(!m_profiler);
208 m_profiler = v8::CpuProfiler::New(m_isolate);
209 m_state->setBoolean(ProfilerAgentState::profilerEnabled, true);
210 }
211
212 void V8ProfilerAgentImpl::disable(ErrorString* errorString) {
213 if (!m_enabled) return;
214 for (size_t i = m_startedProfiles.size(); i > 0; --i)
215 stopProfiling(m_startedProfiles[i - 1].m_id, false);
216 m_startedProfiles.clear();
217 stop(nullptr, nullptr);
218 m_profiler->Dispose();
219 m_profiler = nullptr;
220 m_enabled = false;
221 m_state->setBoolean(ProfilerAgentState::profilerEnabled, false);
222 }
223
224 void V8ProfilerAgentImpl::setSamplingInterval(ErrorString* error,
225 int interval) {
226 if (m_recordingCPUProfile) {
227 *error = "Cannot change sampling interval when profiling.";
228 return;
229 }
230 m_state->setInteger(ProfilerAgentState::samplingInterval, interval);
231 m_profiler->SetSamplingInterval(interval);
232 }
233
234 void V8ProfilerAgentImpl::restore() {
235 DCHECK(!m_enabled);
236 if (!m_state->booleanProperty(ProfilerAgentState::profilerEnabled, false))
237 return;
238 m_enabled = true;
239 DCHECK(!m_profiler);
240 m_profiler = v8::CpuProfiler::New(m_isolate);
241 int interval = 0;
242 m_state->getInteger(ProfilerAgentState::samplingInterval, &interval);
243 if (interval) m_profiler->SetSamplingInterval(interval);
244 if (m_state->booleanProperty(ProfilerAgentState::userInitiatedProfiling,
245 false)) {
246 ErrorString error;
247 start(&error);
248 }
249 }
250
251 void V8ProfilerAgentImpl::start(ErrorString* error) {
252 if (m_recordingCPUProfile) return;
253 if (!m_enabled) {
254 *error = "Profiler is not enabled";
255 return;
256 }
257 m_recordingCPUProfile = true;
258 m_frontendInitiatedProfileId = nextProfileId();
259 startProfiling(m_frontendInitiatedProfileId);
260 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, true);
261 }
262
263 void V8ProfilerAgentImpl::stop(
264 ErrorString* errorString,
265 std::unique_ptr<protocol::Profiler::Profile>* profile) {
266 if (!m_recordingCPUProfile) {
267 if (errorString) *errorString = "No recording profiles found";
268 return;
269 }
270 m_recordingCPUProfile = false;
271 std::unique_ptr<protocol::Profiler::Profile> cpuProfile =
272 stopProfiling(m_frontendInitiatedProfileId, !!profile);
273 if (profile) {
274 *profile = std::move(cpuProfile);
275 if (!profile->get() && errorString) *errorString = "Profile is not found";
276 }
277 m_frontendInitiatedProfileId = String16();
278 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, false);
279 }
280
281 String16 V8ProfilerAgentImpl::nextProfileId() {
282 return String16::fromInteger(
283 v8::base::NoBarrier_AtomicIncrement(&s_lastProfileId, 1));
284 }
285
286 void V8ProfilerAgentImpl::startProfiling(const String16& title) {
287 v8::HandleScope handleScope(m_isolate);
288 m_profiler->StartProfiling(toV8String(m_isolate, title), true);
289 }
290
291 std::unique_ptr<protocol::Profiler::Profile> V8ProfilerAgentImpl::stopProfiling(
292 const String16& title, bool serialize) {
293 v8::HandleScope handleScope(m_isolate);
294 v8::CpuProfile* profile =
295 m_profiler->StopProfiling(toV8String(m_isolate, title));
296 if (!profile) return nullptr;
297 std::unique_ptr<protocol::Profiler::Profile> result;
298 if (serialize) result = createCPUProfile(m_isolate, profile);
299 profile->Delete();
300 return result;
301 }
302
303 bool V8ProfilerAgentImpl::isRecording() const {
304 return m_recordingCPUProfile || !m_startedProfiles.empty();
305 }
306
307 bool V8ProfilerAgentImpl::idleStarted() {
308 if (m_profiler) m_profiler->SetIdle(true);
309 return m_profiler;
310 }
311
312 bool V8ProfilerAgentImpl::idleFinished() {
313 if (m_profiler) m_profiler->SetIdle(false);
314 return m_profiler;
315 }
316
317 void V8ProfilerAgentImpl::collectSample() {
318 if (m_profiler) m_profiler->CollectSample();
319 }
320
321 } // namespace v8_inspector
OLDNEW
« no previous file with comments | « src/inspector/V8ProfilerAgentImpl.h ('k') | src/inspector/V8Regex.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698