| 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 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #define ENSURE_V8_VERSION(major, minor) \ |
| 17 (V8_MAJOR_VERSION * 1000 + V8_MINOR_VERSION >= (major) * 1000 + (minor)) |
| 18 |
| 16 namespace blink { | 19 namespace blink { |
| 17 | 20 |
| 18 namespace ProfilerAgentState { | 21 namespace ProfilerAgentState { |
| 19 static const char samplingInterval[] = "samplingInterval"; | 22 static const char samplingInterval[] = "samplingInterval"; |
| 20 static const char userInitiatedProfiling[] = "userInitiatedProfiling"; | 23 static const char userInitiatedProfiling[] = "userInitiatedProfiling"; |
| 21 static const char profilerEnabled[] = "profilerEnabled"; | 24 static const char profilerEnabled[] = "profilerEnabled"; |
| 22 } | 25 } |
| 23 | 26 |
| 24 namespace { | 27 namespace { |
| 25 | 28 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 ProfileDescriptor(const String16& id, const String16& title) | 124 ProfileDescriptor(const String16& id, const String16& title) |
| 122 : m_id(id) | 125 : m_id(id) |
| 123 , m_title(title) { } | 126 , m_title(title) { } |
| 124 String16 m_id; | 127 String16 m_id; |
| 125 String16 m_title; | 128 String16 m_title; |
| 126 }; | 129 }; |
| 127 | 130 |
| 128 V8ProfilerAgentImpl::V8ProfilerAgentImpl(V8InspectorSessionImpl* session, protoc
ol::FrontendChannel* frontendChannel, protocol::DictionaryValue* state) | 131 V8ProfilerAgentImpl::V8ProfilerAgentImpl(V8InspectorSessionImpl* session, protoc
ol::FrontendChannel* frontendChannel, protocol::DictionaryValue* state) |
| 129 : m_session(session) | 132 : m_session(session) |
| 130 , m_isolate(m_session->debugger()->isolate()) | 133 , m_isolate(m_session->debugger()->isolate()) |
| 134 , m_profiler(nullptr) |
| 131 , m_state(state) | 135 , m_state(state) |
| 132 , m_frontend(frontendChannel) | 136 , m_frontend(frontendChannel) |
| 133 , m_enabled(false) | 137 , m_enabled(false) |
| 134 , m_recordingCPUProfile(false) | 138 , m_recordingCPUProfile(false) |
| 135 { | 139 { |
| 136 } | 140 } |
| 137 | 141 |
| 138 V8ProfilerAgentImpl::~V8ProfilerAgentImpl() | 142 V8ProfilerAgentImpl::~V8ProfilerAgentImpl() |
| 139 { | 143 { |
| 144 #if ENSURE_V8_VERSION(5, 4) |
| 145 if (m_profiler) |
| 146 m_profiler->Dispose(); |
| 147 #endif |
| 140 } | 148 } |
| 141 | 149 |
| 142 void V8ProfilerAgentImpl::consoleProfile(const String16& title) | 150 void V8ProfilerAgentImpl::consoleProfile(const String16& title) |
| 143 { | 151 { |
| 144 if (!m_enabled) | 152 if (!m_enabled) |
| 145 return; | 153 return; |
| 146 String16 id = nextProfileId(); | 154 String16 id = nextProfileId(); |
| 147 m_startedProfiles.push_back(ProfileDescriptor(id, title)); | 155 m_startedProfiles.push_back(ProfileDescriptor(id, title)); |
| 148 startProfiling(id); | 156 startProfiling(id); |
| 149 m_frontend.consoleProfileStarted(id, currentDebugLocation(m_session->debugge
r()), title); | 157 m_frontend.consoleProfileStarted(id, currentDebugLocation(m_session->debugge
r()), title); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 179 return; | 187 return; |
| 180 std::unique_ptr<protocol::Debugger::Location> location = currentDebugLocatio
n(m_session->debugger()); | 188 std::unique_ptr<protocol::Debugger::Location> location = currentDebugLocatio
n(m_session->debugger()); |
| 181 m_frontend.consoleProfileFinished(id, std::move(location), std::move(profile
), resolvedTitle); | 189 m_frontend.consoleProfileFinished(id, std::move(location), std::move(profile
), resolvedTitle); |
| 182 } | 190 } |
| 183 | 191 |
| 184 void V8ProfilerAgentImpl::enable(ErrorString*) | 192 void V8ProfilerAgentImpl::enable(ErrorString*) |
| 185 { | 193 { |
| 186 if (m_enabled) | 194 if (m_enabled) |
| 187 return; | 195 return; |
| 188 m_enabled = true; | 196 m_enabled = true; |
| 197 #if ENSURE_V8_VERSION(5, 4) |
| 198 DCHECK(!m_profiler); |
| 199 m_profiler = v8::CpuProfiler::New(m_isolate); |
| 200 #endif |
| 189 m_state->setBoolean(ProfilerAgentState::profilerEnabled, true); | 201 m_state->setBoolean(ProfilerAgentState::profilerEnabled, true); |
| 190 } | 202 } |
| 191 | 203 |
| 192 void V8ProfilerAgentImpl::disable(ErrorString* errorString) | 204 void V8ProfilerAgentImpl::disable(ErrorString* errorString) |
| 193 { | 205 { |
| 194 if (!m_enabled) | 206 if (!m_enabled) |
| 195 return; | 207 return; |
| 196 for (size_t i = m_startedProfiles.size(); i > 0; --i) | 208 for (size_t i = m_startedProfiles.size(); i > 0; --i) |
| 197 stopProfiling(m_startedProfiles[i - 1].m_id, false); | 209 stopProfiling(m_startedProfiles[i - 1].m_id, false); |
| 198 m_startedProfiles.clear(); | 210 m_startedProfiles.clear(); |
| 199 stop(nullptr, nullptr); | 211 stop(nullptr, nullptr); |
| 212 #if ENSURE_V8_VERSION(5, 4) |
| 213 m_profiler->Dispose(); |
| 214 m_profiler = nullptr; |
| 215 #endif |
| 200 m_enabled = false; | 216 m_enabled = false; |
| 201 m_state->setBoolean(ProfilerAgentState::profilerEnabled, false); | 217 m_state->setBoolean(ProfilerAgentState::profilerEnabled, false); |
| 202 } | 218 } |
| 203 | 219 |
| 204 void V8ProfilerAgentImpl::setSamplingInterval(ErrorString* error, int interval) | 220 void V8ProfilerAgentImpl::setSamplingInterval(ErrorString* error, int interval) |
| 205 { | 221 { |
| 206 if (m_recordingCPUProfile) { | 222 if (m_recordingCPUProfile) { |
| 207 *error = "Cannot change sampling interval when profiling."; | 223 *error = "Cannot change sampling interval when profiling."; |
| 208 return; | 224 return; |
| 209 } | 225 } |
| 210 m_state->setInteger(ProfilerAgentState::samplingInterval, interval); | 226 m_state->setInteger(ProfilerAgentState::samplingInterval, interval); |
| 211 m_isolate->GetCpuProfiler()->SetSamplingInterval(interval); | 227 profiler()->SetSamplingInterval(interval); |
| 212 } | 228 } |
| 213 | 229 |
| 214 void V8ProfilerAgentImpl::restore() | 230 void V8ProfilerAgentImpl::restore() |
| 215 { | 231 { |
| 216 DCHECK(!m_enabled); | 232 DCHECK(!m_enabled); |
| 217 if (!m_state->booleanProperty(ProfilerAgentState::profilerEnabled, false)) | 233 if (!m_state->booleanProperty(ProfilerAgentState::profilerEnabled, false)) |
| 218 return; | 234 return; |
| 219 m_enabled = true; | 235 m_enabled = true; |
| 236 #if ENSURE_V8_VERSION(5, 4) |
| 237 DCHECK(!m_profiler); |
| 238 m_profiler = v8::CpuProfiler::New(m_isolate); |
| 239 #endif |
| 220 int interval = 0; | 240 int interval = 0; |
| 221 m_state->getInteger(ProfilerAgentState::samplingInterval, &interval); | 241 m_state->getInteger(ProfilerAgentState::samplingInterval, &interval); |
| 222 if (interval) | 242 if (interval) |
| 223 m_isolate->GetCpuProfiler()->SetSamplingInterval(interval); | 243 profiler()->SetSamplingInterval(interval); |
| 224 if (m_state->booleanProperty(ProfilerAgentState::userInitiatedProfiling, fal
se)) { | 244 if (m_state->booleanProperty(ProfilerAgentState::userInitiatedProfiling, fal
se)) { |
| 225 ErrorString error; | 245 ErrorString error; |
| 226 start(&error); | 246 start(&error); |
| 227 } | 247 } |
| 228 } | 248 } |
| 229 | 249 |
| 230 void V8ProfilerAgentImpl::start(ErrorString* error) | 250 void V8ProfilerAgentImpl::start(ErrorString* error) |
| 231 { | 251 { |
| 232 if (m_recordingCPUProfile) | 252 if (m_recordingCPUProfile) |
| 233 return; | 253 return; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 262 } | 282 } |
| 263 | 283 |
| 264 String16 V8ProfilerAgentImpl::nextProfileId() | 284 String16 V8ProfilerAgentImpl::nextProfileId() |
| 265 { | 285 { |
| 266 return String16::fromInteger(atomicIncrement(&s_lastProfileId)); | 286 return String16::fromInteger(atomicIncrement(&s_lastProfileId)); |
| 267 } | 287 } |
| 268 | 288 |
| 269 void V8ProfilerAgentImpl::startProfiling(const String16& title) | 289 void V8ProfilerAgentImpl::startProfiling(const String16& title) |
| 270 { | 290 { |
| 271 v8::HandleScope handleScope(m_isolate); | 291 v8::HandleScope handleScope(m_isolate); |
| 272 m_isolate->GetCpuProfiler()->StartProfiling(toV8String(m_isolate, title), tr
ue); | 292 profiler()->StartProfiling(toV8String(m_isolate, title), true); |
| 273 } | 293 } |
| 274 | 294 |
| 275 std::unique_ptr<protocol::Profiler::CPUProfile> V8ProfilerAgentImpl::stopProfili
ng(const String16& title, bool serialize) | 295 std::unique_ptr<protocol::Profiler::CPUProfile> V8ProfilerAgentImpl::stopProfili
ng(const String16& title, bool serialize) |
| 276 { | 296 { |
| 277 v8::HandleScope handleScope(m_isolate); | 297 v8::HandleScope handleScope(m_isolate); |
| 278 v8::CpuProfile* profile = m_isolate->GetCpuProfiler()->StopProfiling(toV8Str
ing(m_isolate, title)); | 298 v8::CpuProfile* profile = profiler()->StopProfiling(toV8String(m_isolate, ti
tle)); |
| 279 if (!profile) | 299 if (!profile) |
| 280 return nullptr; | 300 return nullptr; |
| 281 std::unique_ptr<protocol::Profiler::CPUProfile> result; | 301 std::unique_ptr<protocol::Profiler::CPUProfile> result; |
| 282 if (serialize) | 302 if (serialize) |
| 283 result = createCPUProfile(m_isolate, profile); | 303 result = createCPUProfile(m_isolate, profile); |
| 284 profile->Delete(); | 304 profile->Delete(); |
| 285 return result; | 305 return result; |
| 286 } | 306 } |
| 287 | 307 |
| 288 bool V8ProfilerAgentImpl::isRecording() const | 308 bool V8ProfilerAgentImpl::isRecording() const |
| 289 { | 309 { |
| 290 return m_recordingCPUProfile || !m_startedProfiles.empty(); | 310 return m_recordingCPUProfile || !m_startedProfiles.empty(); |
| 291 } | 311 } |
| 292 | 312 |
| 313 v8::CpuProfiler* V8ProfilerAgentImpl::profiler() |
| 314 { |
| 315 #if ENSURE_V8_VERSION(5, 4) |
| 316 return m_profiler; |
| 317 #else |
| 318 return m_isolate->GetCpuProfiler(); |
| 319 #endif |
| 320 } |
| 321 |
| 293 } // namespace blink | 322 } // namespace blink |
| OLD | NEW |