| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 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 "src/inspector/v8-profiler-agent-impl.h" | 5 #include "src/inspector/v8-profiler-agent-impl.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "src/base/atomicops.h" | 9 #include "src/base/atomicops.h" |
| 10 #include "src/inspector/protocol/Protocol.h" | 10 #include "src/inspector/protocol/Protocol.h" |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 } | 194 } |
| 195 std::unique_ptr<protocol::Profiler::Profile> profile = | 195 std::unique_ptr<protocol::Profiler::Profile> profile = |
| 196 stopProfiling(id, true); | 196 stopProfiling(id, true); |
| 197 if (!profile) return; | 197 if (!profile) return; |
| 198 std::unique_ptr<protocol::Debugger::Location> location = | 198 std::unique_ptr<protocol::Debugger::Location> location = |
| 199 currentDebugLocation(m_session->inspector()); | 199 currentDebugLocation(m_session->inspector()); |
| 200 m_frontend.consoleProfileFinished(id, std::move(location), std::move(profile), | 200 m_frontend.consoleProfileFinished(id, std::move(location), std::move(profile), |
| 201 resolvedTitle); | 201 resolvedTitle); |
| 202 } | 202 } |
| 203 | 203 |
| 204 void V8ProfilerAgentImpl::enable(ErrorString*) { | 204 Response V8ProfilerAgentImpl::enable() { |
| 205 if (m_enabled) return; | 205 if (m_enabled) return Response::OK(); |
| 206 m_enabled = true; | 206 m_enabled = true; |
| 207 DCHECK(!m_profiler); | 207 DCHECK(!m_profiler); |
| 208 m_profiler = v8::CpuProfiler::New(m_isolate); | 208 m_profiler = v8::CpuProfiler::New(m_isolate); |
| 209 m_state->setBoolean(ProfilerAgentState::profilerEnabled, true); | 209 m_state->setBoolean(ProfilerAgentState::profilerEnabled, true); |
| 210 return Response::OK(); |
| 210 } | 211 } |
| 211 | 212 |
| 212 void V8ProfilerAgentImpl::disable(ErrorString* errorString) { | 213 Response V8ProfilerAgentImpl::disable() { |
| 213 if (!m_enabled) return; | 214 if (!m_enabled) return Response::OK(); |
| 214 for (size_t i = m_startedProfiles.size(); i > 0; --i) | 215 for (size_t i = m_startedProfiles.size(); i > 0; --i) |
| 215 stopProfiling(m_startedProfiles[i - 1].m_id, false); | 216 stopProfiling(m_startedProfiles[i - 1].m_id, false); |
| 216 m_startedProfiles.clear(); | 217 m_startedProfiles.clear(); |
| 217 stop(nullptr, nullptr); | 218 stop(nullptr); |
| 218 m_profiler->Dispose(); | 219 m_profiler->Dispose(); |
| 219 m_profiler = nullptr; | 220 m_profiler = nullptr; |
| 220 m_enabled = false; | 221 m_enabled = false; |
| 221 m_state->setBoolean(ProfilerAgentState::profilerEnabled, false); | 222 m_state->setBoolean(ProfilerAgentState::profilerEnabled, false); |
| 223 return Response::OK(); |
| 222 } | 224 } |
| 223 | 225 |
| 224 void V8ProfilerAgentImpl::setSamplingInterval(ErrorString* error, | 226 Response V8ProfilerAgentImpl::setSamplingInterval(int interval) { |
| 225 int interval) { | 227 if (m_recordingCPUProfile) |
| 226 if (m_recordingCPUProfile) { | 228 return Response::Error("Cannot change sampling interval when profiling."); |
| 227 *error = "Cannot change sampling interval when profiling."; | |
| 228 return; | |
| 229 } | |
| 230 m_state->setInteger(ProfilerAgentState::samplingInterval, interval); | 229 m_state->setInteger(ProfilerAgentState::samplingInterval, interval); |
| 231 m_profiler->SetSamplingInterval(interval); | 230 m_profiler->SetSamplingInterval(interval); |
| 231 return Response::OK(); |
| 232 } | 232 } |
| 233 | 233 |
| 234 void V8ProfilerAgentImpl::restore() { | 234 void V8ProfilerAgentImpl::restore() { |
| 235 DCHECK(!m_enabled); | 235 DCHECK(!m_enabled); |
| 236 if (!m_state->booleanProperty(ProfilerAgentState::profilerEnabled, false)) | 236 if (!m_state->booleanProperty(ProfilerAgentState::profilerEnabled, false)) |
| 237 return; | 237 return; |
| 238 m_enabled = true; | 238 m_enabled = true; |
| 239 DCHECK(!m_profiler); | 239 DCHECK(!m_profiler); |
| 240 m_profiler = v8::CpuProfiler::New(m_isolate); | 240 m_profiler = v8::CpuProfiler::New(m_isolate); |
| 241 int interval = 0; | 241 int interval = 0; |
| 242 m_state->getInteger(ProfilerAgentState::samplingInterval, &interval); | 242 m_state->getInteger(ProfilerAgentState::samplingInterval, &interval); |
| 243 if (interval) m_profiler->SetSamplingInterval(interval); | 243 if (interval) m_profiler->SetSamplingInterval(interval); |
| 244 if (m_state->booleanProperty(ProfilerAgentState::userInitiatedProfiling, | 244 if (m_state->booleanProperty(ProfilerAgentState::userInitiatedProfiling, |
| 245 false)) { | 245 false)) { |
| 246 ErrorString error; | 246 start(); |
| 247 start(&error); | |
| 248 } | 247 } |
| 249 } | 248 } |
| 250 | 249 |
| 251 void V8ProfilerAgentImpl::start(ErrorString* error) { | 250 Response V8ProfilerAgentImpl::start() { |
| 252 if (m_recordingCPUProfile) return; | 251 if (m_recordingCPUProfile) return Response::OK(); |
| 253 if (!m_enabled) { | 252 if (!m_enabled) return Response::Error("Profiler is not enabled"); |
| 254 *error = "Profiler is not enabled"; | |
| 255 return; | |
| 256 } | |
| 257 m_recordingCPUProfile = true; | 253 m_recordingCPUProfile = true; |
| 258 m_frontendInitiatedProfileId = nextProfileId(); | 254 m_frontendInitiatedProfileId = nextProfileId(); |
| 259 startProfiling(m_frontendInitiatedProfileId); | 255 startProfiling(m_frontendInitiatedProfileId); |
| 260 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, true); | 256 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, true); |
| 257 return Response::OK(); |
| 261 } | 258 } |
| 262 | 259 |
| 263 void V8ProfilerAgentImpl::stop( | 260 Response V8ProfilerAgentImpl::stop( |
| 264 ErrorString* errorString, | |
| 265 std::unique_ptr<protocol::Profiler::Profile>* profile) { | 261 std::unique_ptr<protocol::Profiler::Profile>* profile) { |
| 266 if (!m_recordingCPUProfile) { | 262 if (!m_recordingCPUProfile) |
| 267 if (errorString) *errorString = "No recording profiles found"; | 263 return Response::Error("No recording profiles found"); |
| 268 return; | |
| 269 } | |
| 270 m_recordingCPUProfile = false; | 264 m_recordingCPUProfile = false; |
| 271 std::unique_ptr<protocol::Profiler::Profile> cpuProfile = | 265 std::unique_ptr<protocol::Profiler::Profile> cpuProfile = |
| 272 stopProfiling(m_frontendInitiatedProfileId, !!profile); | 266 stopProfiling(m_frontendInitiatedProfileId, !!profile); |
| 273 if (profile) { | 267 if (profile) { |
| 274 *profile = std::move(cpuProfile); | 268 *profile = std::move(cpuProfile); |
| 275 if (!profile->get() && errorString) *errorString = "Profile is not found"; | 269 if (!profile->get()) return Response::Error("Profile is not found"); |
| 276 } | 270 } |
| 277 m_frontendInitiatedProfileId = String16(); | 271 m_frontendInitiatedProfileId = String16(); |
| 278 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, false); | 272 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, false); |
| 273 return Response::OK(); |
| 279 } | 274 } |
| 280 | 275 |
| 281 String16 V8ProfilerAgentImpl::nextProfileId() { | 276 String16 V8ProfilerAgentImpl::nextProfileId() { |
| 282 return String16::fromInteger( | 277 return String16::fromInteger( |
| 283 v8::base::NoBarrier_AtomicIncrement(&s_lastProfileId, 1)); | 278 v8::base::NoBarrier_AtomicIncrement(&s_lastProfileId, 1)); |
| 284 } | 279 } |
| 285 | 280 |
| 286 void V8ProfilerAgentImpl::startProfiling(const String16& title) { | 281 void V8ProfilerAgentImpl::startProfiling(const String16& title) { |
| 287 v8::HandleScope handleScope(m_isolate); | 282 v8::HandleScope handleScope(m_isolate); |
| 288 m_profiler->StartProfiling(toV8String(m_isolate, title), true); | 283 m_profiler->StartProfiling(toV8String(m_isolate, title), true); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 312 bool V8ProfilerAgentImpl::idleFinished() { | 307 bool V8ProfilerAgentImpl::idleFinished() { |
| 313 if (m_profiler) m_profiler->SetIdle(false); | 308 if (m_profiler) m_profiler->SetIdle(false); |
| 314 return m_profiler; | 309 return m_profiler; |
| 315 } | 310 } |
| 316 | 311 |
| 317 void V8ProfilerAgentImpl::collectSample() { | 312 void V8ProfilerAgentImpl::collectSample() { |
| 318 if (m_profiler) m_profiler->CollectSample(); | 313 if (m_profiler) m_profiler->CollectSample(); |
| 319 } | 314 } |
| 320 | 315 |
| 321 } // namespace v8_inspector | 316 } // namespace v8_inspector |
| OLD | NEW |