| 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/V8InspectorImpl.h" | 8 #include "platform/v8_inspector/V8InspectorImpl.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" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 for (unsigned i = 0; i < lineCount; i++) { | 37 for (unsigned i = 0; i < lineCount; i++) { |
| 38 std::unique_ptr<protocol::Profiler::PositionTickInfo> line = protoco
l::Profiler::PositionTickInfo::create() | 38 std::unique_ptr<protocol::Profiler::PositionTickInfo> line = protoco
l::Profiler::PositionTickInfo::create() |
| 39 .setLine(entries[i].line) | 39 .setLine(entries[i].line) |
| 40 .setTicks(entries[i].hit_count).build(); | 40 .setTicks(entries[i].hit_count).build(); |
| 41 array->addItem(std::move(line)); | 41 array->addItem(std::move(line)); |
| 42 } | 42 } |
| 43 } | 43 } |
| 44 return array; | 44 return array; |
| 45 } | 45 } |
| 46 | 46 |
| 47 std::unique_ptr<protocol::Profiler::CPUProfileNode> buildInspectorObjectFor(v8::
Isolate* isolate, const v8::CpuProfileNode* node) | 47 std::unique_ptr<protocol::Profiler::ProfileNode> buildInspectorObjectFor(v8::Iso
late* isolate, const v8::CpuProfileNode* node) |
| 48 { | 48 { |
| 49 v8::HandleScope handleScope(isolate); | 49 v8::HandleScope handleScope(isolate); |
| 50 auto callFrame = protocol::Runtime::CallFrame::create() | 50 auto callFrame = protocol::Runtime::CallFrame::create() |
| 51 .setFunctionName(toProtocolString(node->GetFunctionName())) | 51 .setFunctionName(toProtocolString(node->GetFunctionName())) |
| 52 .setScriptId(String16::fromInteger(node->GetScriptId())) | 52 .setScriptId(String16::fromInteger(node->GetScriptId())) |
| 53 .setUrl(toProtocolString(node->GetScriptResourceName())) | 53 .setUrl(toProtocolString(node->GetScriptResourceName())) |
| 54 .setLineNumber(node->GetLineNumber() - 1) | 54 .setLineNumber(node->GetLineNumber() - 1) |
| 55 .setColumnNumber(node->GetColumnNumber() - 1) | 55 .setColumnNumber(node->GetColumnNumber() - 1) |
| 56 .build(); | 56 .build(); |
| 57 auto result = protocol::Profiler::CPUProfileNode::create() | 57 auto result = protocol::Profiler::ProfileNode::create() |
| 58 .setCallFrame(std::move(callFrame)) | 58 .setCallFrame(std::move(callFrame)) |
| 59 .setHitCount(node->GetHitCount()) | 59 .setHitCount(node->GetHitCount()) |
| 60 .setId(node->GetNodeId()).build(); | 60 .setId(node->GetNodeId()).build(); |
| 61 | 61 |
| 62 const int childrenCount = node->GetChildrenCount(); | 62 const int childrenCount = node->GetChildrenCount(); |
| 63 if (childrenCount) { | 63 if (childrenCount) { |
| 64 auto children = protocol::Array<int>::create(); | 64 auto children = protocol::Array<int>::create(); |
| 65 for (int i = 0; i < childrenCount; i++) | 65 for (int i = 0; i < childrenCount; i++) |
| 66 children->addItem(node->GetChild(i)->GetNodeId()); | 66 children->addItem(node->GetChild(i)->GetNodeId()); |
| 67 result->setChildren(std::move(children)); | 67 result->setChildren(std::move(children)); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 93 int count = v8profile->GetSamplesCount(); | 93 int count = v8profile->GetSamplesCount(); |
| 94 uint64_t lastTime = v8profile->GetStartTime(); | 94 uint64_t lastTime = v8profile->GetStartTime(); |
| 95 for (int i = 0; i < count; i++) { | 95 for (int i = 0; i < count; i++) { |
| 96 uint64_t ts = v8profile->GetSampleTimestamp(i); | 96 uint64_t ts = v8profile->GetSampleTimestamp(i); |
| 97 array->addItem(static_cast<int>(ts - lastTime)); | 97 array->addItem(static_cast<int>(ts - lastTime)); |
| 98 lastTime = ts; | 98 lastTime = ts; |
| 99 } | 99 } |
| 100 return array; | 100 return array; |
| 101 } | 101 } |
| 102 | 102 |
| 103 void flattenNodesTree(v8::Isolate* isolate, const v8::CpuProfileNode* node, prot
ocol::Array<protocol::Profiler::CPUProfileNode>* list) | 103 void flattenNodesTree(v8::Isolate* isolate, const v8::CpuProfileNode* node, prot
ocol::Array<protocol::Profiler::ProfileNode>* list) |
| 104 { | 104 { |
| 105 list->addItem(buildInspectorObjectFor(isolate, node)); | 105 list->addItem(buildInspectorObjectFor(isolate, node)); |
| 106 const int childrenCount = node->GetChildrenCount(); | 106 const int childrenCount = node->GetChildrenCount(); |
| 107 for (int i = 0; i < childrenCount; i++) | 107 for (int i = 0; i < childrenCount; i++) |
| 108 flattenNodesTree(isolate, node->GetChild(i), list); | 108 flattenNodesTree(isolate, node->GetChild(i), list); |
| 109 } | 109 } |
| 110 | 110 |
| 111 std::unique_ptr<protocol::Profiler::CPUProfile> createCPUProfile(v8::Isolate* is
olate, v8::CpuProfile* v8profile) | 111 std::unique_ptr<protocol::Profiler::Profile> createCPUProfile(v8::Isolate* isola
te, v8::CpuProfile* v8profile) |
| 112 { | 112 { |
| 113 auto nodes = protocol::Array<protocol::Profiler::CPUProfileNode>::create(); | 113 auto nodes = protocol::Array<protocol::Profiler::ProfileNode>::create(); |
| 114 flattenNodesTree(isolate, v8profile->GetTopDownRoot(), nodes.get()); | 114 flattenNodesTree(isolate, v8profile->GetTopDownRoot(), nodes.get()); |
| 115 | 115 return protocol::Profiler::Profile::create() |
| 116 auto profile = protocol::Profiler::CPUProfile::create() | |
| 117 .setNodes(std::move(nodes)) | 116 .setNodes(std::move(nodes)) |
| 118 .setStartTime(static_cast<double>(v8profile->GetStartTime())) | 117 .setStartTime(static_cast<double>(v8profile->GetStartTime())) |
| 119 .setEndTime(static_cast<double>(v8profile->GetEndTime())).build(); | 118 .setEndTime(static_cast<double>(v8profile->GetEndTime())) |
| 120 profile->setSamples(buildInspectorObjectForSamples(v8profile)); | 119 .setSamples(buildInspectorObjectForSamples(v8profile)) |
| 121 profile->setTimestampDeltas(buildInspectorObjectForTimestamps(v8profile)); | 120 .setTimeDeltas(buildInspectorObjectForTimestamps(v8profile)).build(); |
| 122 return profile; | |
| 123 } | 121 } |
| 124 | 122 |
| 125 std::unique_ptr<protocol::Debugger::Location> currentDebugLocation(V8InspectorIm
pl* inspector) | 123 std::unique_ptr<protocol::Debugger::Location> currentDebugLocation(V8InspectorIm
pl* inspector) |
| 126 { | 124 { |
| 127 std::unique_ptr<V8StackTrace> callStack = inspector->captureStackTrace(1); | 125 std::unique_ptr<V8StackTrace> callStack = inspector->captureStackTrace(1); |
| 128 auto location = protocol::Debugger::Location::create() | 126 auto location = protocol::Debugger::Location::create() |
| 129 .setScriptId(callStack->topScriptId()) | 127 .setScriptId(callStack->topScriptId()) |
| 130 .setLineNumber(callStack->topLineNumber()).build(); | 128 .setLineNumber(callStack->topLineNumber()).build(); |
| 131 location->setColumnNumber(callStack->topColumnNumber()); | 129 location->setColumnNumber(callStack->topColumnNumber()); |
| 132 return location; | 130 return location; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 if (m_startedProfiles[i].m_title == title) { | 190 if (m_startedProfiles[i].m_title == title) { |
| 193 resolvedTitle = title; | 191 resolvedTitle = title; |
| 194 id = m_startedProfiles[i].m_id; | 192 id = m_startedProfiles[i].m_id; |
| 195 m_startedProfiles.erase(m_startedProfiles.begin() + i); | 193 m_startedProfiles.erase(m_startedProfiles.begin() + i); |
| 196 break; | 194 break; |
| 197 } | 195 } |
| 198 } | 196 } |
| 199 if (id.isEmpty()) | 197 if (id.isEmpty()) |
| 200 return; | 198 return; |
| 201 } | 199 } |
| 202 std::unique_ptr<protocol::Profiler::CPUProfile> profile = stopProfiling(id,
true); | 200 std::unique_ptr<protocol::Profiler::Profile> profile = stopProfiling(id, tru
e); |
| 203 if (!profile) | 201 if (!profile) |
| 204 return; | 202 return; |
| 205 std::unique_ptr<protocol::Debugger::Location> location = currentDebugLocatio
n(m_session->inspector()); | 203 std::unique_ptr<protocol::Debugger::Location> location = currentDebugLocatio
n(m_session->inspector()); |
| 206 m_frontend.consoleProfileFinished(id, std::move(location), std::move(profile
), resolvedTitle); | 204 m_frontend.consoleProfileFinished(id, std::move(location), std::move(profile
), resolvedTitle); |
| 207 } | 205 } |
| 208 | 206 |
| 209 void V8ProfilerAgentImpl::enable(ErrorString*) | 207 void V8ProfilerAgentImpl::enable(ErrorString*) |
| 210 { | 208 { |
| 211 if (m_enabled) | 209 if (m_enabled) |
| 212 return; | 210 return; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 if (!m_enabled) { | 269 if (!m_enabled) { |
| 272 *error = "Profiler is not enabled"; | 270 *error = "Profiler is not enabled"; |
| 273 return; | 271 return; |
| 274 } | 272 } |
| 275 m_recordingCPUProfile = true; | 273 m_recordingCPUProfile = true; |
| 276 m_frontendInitiatedProfileId = nextProfileId(); | 274 m_frontendInitiatedProfileId = nextProfileId(); |
| 277 startProfiling(m_frontendInitiatedProfileId); | 275 startProfiling(m_frontendInitiatedProfileId); |
| 278 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, true); | 276 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, true); |
| 279 } | 277 } |
| 280 | 278 |
| 281 void V8ProfilerAgentImpl::stop(ErrorString* errorString, std::unique_ptr<protoco
l::Profiler::CPUProfile>* profile) | 279 void V8ProfilerAgentImpl::stop(ErrorString* errorString, std::unique_ptr<protoco
l::Profiler::Profile>* profile) |
| 282 { | 280 { |
| 283 if (!m_recordingCPUProfile) { | 281 if (!m_recordingCPUProfile) { |
| 284 if (errorString) | 282 if (errorString) |
| 285 *errorString = "No recording profiles found"; | 283 *errorString = "No recording profiles found"; |
| 286 return; | 284 return; |
| 287 } | 285 } |
| 288 m_recordingCPUProfile = false; | 286 m_recordingCPUProfile = false; |
| 289 std::unique_ptr<protocol::Profiler::CPUProfile> cpuProfile = stopProfiling(m
_frontendInitiatedProfileId, !!profile); | 287 std::unique_ptr<protocol::Profiler::Profile> cpuProfile = stopProfiling(m_fr
ontendInitiatedProfileId, !!profile); |
| 290 if (profile) { | 288 if (profile) { |
| 291 *profile = std::move(cpuProfile); | 289 *profile = std::move(cpuProfile); |
| 292 if (!profile->get() && errorString) | 290 if (!profile->get() && errorString) |
| 293 *errorString = "Profile is not found"; | 291 *errorString = "Profile is not found"; |
| 294 } | 292 } |
| 295 m_frontendInitiatedProfileId = String16(); | 293 m_frontendInitiatedProfileId = String16(); |
| 296 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, false); | 294 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, false); |
| 297 } | 295 } |
| 298 | 296 |
| 299 String16 V8ProfilerAgentImpl::nextProfileId() | 297 String16 V8ProfilerAgentImpl::nextProfileId() |
| 300 { | 298 { |
| 301 return String16::fromInteger(atomicIncrement(&s_lastProfileId)); | 299 return String16::fromInteger(atomicIncrement(&s_lastProfileId)); |
| 302 } | 300 } |
| 303 | 301 |
| 304 void V8ProfilerAgentImpl::startProfiling(const String16& title) | 302 void V8ProfilerAgentImpl::startProfiling(const String16& title) |
| 305 { | 303 { |
| 306 v8::HandleScope handleScope(m_isolate); | 304 v8::HandleScope handleScope(m_isolate); |
| 307 profiler()->StartProfiling(toV8String(m_isolate, title), true); | 305 profiler()->StartProfiling(toV8String(m_isolate, title), true); |
| 308 } | 306 } |
| 309 | 307 |
| 310 std::unique_ptr<protocol::Profiler::CPUProfile> V8ProfilerAgentImpl::stopProfili
ng(const String16& title, bool serialize) | 308 std::unique_ptr<protocol::Profiler::Profile> V8ProfilerAgentImpl::stopProfiling(
const String16& title, bool serialize) |
| 311 { | 309 { |
| 312 v8::HandleScope handleScope(m_isolate); | 310 v8::HandleScope handleScope(m_isolate); |
| 313 v8::CpuProfile* profile = profiler()->StopProfiling(toV8String(m_isolate, ti
tle)); | 311 v8::CpuProfile* profile = profiler()->StopProfiling(toV8String(m_isolate, ti
tle)); |
| 314 if (!profile) | 312 if (!profile) |
| 315 return nullptr; | 313 return nullptr; |
| 316 std::unique_ptr<protocol::Profiler::CPUProfile> result; | 314 std::unique_ptr<protocol::Profiler::Profile> result; |
| 317 if (serialize) | 315 if (serialize) |
| 318 result = createCPUProfile(m_isolate, profile); | 316 result = createCPUProfile(m_isolate, profile); |
| 319 profile->Delete(); | 317 profile->Delete(); |
| 320 return result; | 318 return result; |
| 321 } | 319 } |
| 322 | 320 |
| 323 bool V8ProfilerAgentImpl::isRecording() const | 321 bool V8ProfilerAgentImpl::isRecording() const |
| 324 { | 322 { |
| 325 return m_recordingCPUProfile || !m_startedProfiles.empty(); | 323 return m_recordingCPUProfile || !m_startedProfiles.empty(); |
| 326 } | 324 } |
| 327 | 325 |
| 328 v8::CpuProfiler* V8ProfilerAgentImpl::profiler() | 326 v8::CpuProfiler* V8ProfilerAgentImpl::profiler() |
| 329 { | 327 { |
| 330 #if ENSURE_V8_VERSION(5, 4) | 328 #if ENSURE_V8_VERSION(5, 4) |
| 331 return m_profiler; | 329 return m_profiler; |
| 332 #else | 330 #else |
| 333 return m_isolate->GetCpuProfiler(); | 331 return m_isolate->GetCpuProfiler(); |
| 334 #endif | 332 #endif |
| 335 } | 333 } |
| 336 | 334 |
| 337 } // namespace v8_inspector | 335 } // namespace v8_inspector |
| OLD | NEW |