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/V8ProfilerAgentImpl.h" | 5 #include "src/inspector/V8ProfilerAgentImpl.h" |
6 | 6 |
7 #include "src/inspector/Atomics.h" | 7 #include "src/inspector/Atomics.h" |
8 #include "src/inspector/StringUtil.h" | 8 #include "src/inspector/StringUtil.h" |
9 #include "src/inspector/V8Debugger.h" | 9 #include "src/inspector/V8Debugger.h" |
10 #include "src/inspector/V8InspectorImpl.h" | 10 #include "src/inspector/V8InspectorImpl.h" |
11 #include "src/inspector/V8InspectorSessionImpl.h" | 11 #include "src/inspector/V8InspectorSessionImpl.h" |
12 #include "src/inspector/V8StackTraceImpl.h" | 12 #include "src/inspector/V8StackTraceImpl.h" |
13 #include "src/inspector/protocol/Protocol.h" | 13 #include "src/inspector/protocol/Protocol.h" |
14 | 14 |
15 #include "include/v8-profiler.h" | 15 #include "include/v8-profiler.h" |
16 | 16 |
17 #include <vector> | 17 #include <vector> |
18 | 18 |
19 #define ENSURE_V8_VERSION(major, minor) \ | |
20 (V8_MAJOR_VERSION * 1000 + V8_MINOR_VERSION >= (major)*1000 + (minor)) | |
21 | |
22 namespace v8_inspector { | 19 namespace v8_inspector { |
23 | 20 |
24 namespace ProfilerAgentState { | 21 namespace ProfilerAgentState { |
25 static const char samplingInterval[] = "samplingInterval"; | 22 static const char samplingInterval[] = "samplingInterval"; |
26 static const char userInitiatedProfiling[] = "userInitiatedProfiling"; | 23 static const char userInitiatedProfiling[] = "userInitiatedProfiling"; |
27 static const char profilerEnabled[] = "profilerEnabled"; | 24 static const char profilerEnabled[] = "profilerEnabled"; |
28 } | 25 } |
29 | 26 |
30 namespace { | 27 namespace { |
31 | 28 |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 protocol::DictionaryValue* state) | 152 protocol::DictionaryValue* state) |
156 : m_session(session), | 153 : m_session(session), |
157 m_isolate(m_session->inspector()->isolate()), | 154 m_isolate(m_session->inspector()->isolate()), |
158 m_profiler(nullptr), | 155 m_profiler(nullptr), |
159 m_state(state), | 156 m_state(state), |
160 m_frontend(frontendChannel), | 157 m_frontend(frontendChannel), |
161 m_enabled(false), | 158 m_enabled(false), |
162 m_recordingCPUProfile(false) {} | 159 m_recordingCPUProfile(false) {} |
163 | 160 |
164 V8ProfilerAgentImpl::~V8ProfilerAgentImpl() { | 161 V8ProfilerAgentImpl::~V8ProfilerAgentImpl() { |
165 #if ENSURE_V8_VERSION(5, 4) | |
166 if (m_profiler) m_profiler->Dispose(); | 162 if (m_profiler) m_profiler->Dispose(); |
167 #endif | |
168 } | 163 } |
169 | 164 |
170 void V8ProfilerAgentImpl::consoleProfile(const String16& title) { | 165 void V8ProfilerAgentImpl::consoleProfile(const String16& title) { |
171 if (!m_enabled) return; | 166 if (!m_enabled) return; |
172 String16 id = nextProfileId(); | 167 String16 id = nextProfileId(); |
173 m_startedProfiles.push_back(ProfileDescriptor(id, title)); | 168 m_startedProfiles.push_back(ProfileDescriptor(id, title)); |
174 startProfiling(id); | 169 startProfiling(id); |
175 m_frontend.consoleProfileStarted( | 170 m_frontend.consoleProfileStarted( |
176 id, currentDebugLocation(m_session->inspector()), title); | 171 id, currentDebugLocation(m_session->inspector()), title); |
177 } | 172 } |
(...skipping 24 matching lines...) Expand all Loading... |
202 if (!profile) return; | 197 if (!profile) return; |
203 std::unique_ptr<protocol::Debugger::Location> location = | 198 std::unique_ptr<protocol::Debugger::Location> location = |
204 currentDebugLocation(m_session->inspector()); | 199 currentDebugLocation(m_session->inspector()); |
205 m_frontend.consoleProfileFinished(id, std::move(location), std::move(profile), | 200 m_frontend.consoleProfileFinished(id, std::move(location), std::move(profile), |
206 resolvedTitle); | 201 resolvedTitle); |
207 } | 202 } |
208 | 203 |
209 void V8ProfilerAgentImpl::enable(ErrorString*) { | 204 void V8ProfilerAgentImpl::enable(ErrorString*) { |
210 if (m_enabled) return; | 205 if (m_enabled) return; |
211 m_enabled = true; | 206 m_enabled = true; |
212 #if ENSURE_V8_VERSION(5, 4) | |
213 DCHECK(!m_profiler); | 207 DCHECK(!m_profiler); |
214 m_profiler = v8::CpuProfiler::New(m_isolate); | 208 m_profiler = v8::CpuProfiler::New(m_isolate); |
215 #endif | |
216 m_state->setBoolean(ProfilerAgentState::profilerEnabled, true); | 209 m_state->setBoolean(ProfilerAgentState::profilerEnabled, true); |
217 } | 210 } |
218 | 211 |
219 void V8ProfilerAgentImpl::disable(ErrorString* errorString) { | 212 void V8ProfilerAgentImpl::disable(ErrorString* errorString) { |
220 if (!m_enabled) return; | 213 if (!m_enabled) return; |
221 for (size_t i = m_startedProfiles.size(); i > 0; --i) | 214 for (size_t i = m_startedProfiles.size(); i > 0; --i) |
222 stopProfiling(m_startedProfiles[i - 1].m_id, false); | 215 stopProfiling(m_startedProfiles[i - 1].m_id, false); |
223 m_startedProfiles.clear(); | 216 m_startedProfiles.clear(); |
224 stop(nullptr, nullptr); | 217 stop(nullptr, nullptr); |
225 #if ENSURE_V8_VERSION(5, 4) | |
226 m_profiler->Dispose(); | 218 m_profiler->Dispose(); |
227 m_profiler = nullptr; | 219 m_profiler = nullptr; |
228 #endif | |
229 m_enabled = false; | 220 m_enabled = false; |
230 m_state->setBoolean(ProfilerAgentState::profilerEnabled, false); | 221 m_state->setBoolean(ProfilerAgentState::profilerEnabled, false); |
231 } | 222 } |
232 | 223 |
233 void V8ProfilerAgentImpl::setSamplingInterval(ErrorString* error, | 224 void V8ProfilerAgentImpl::setSamplingInterval(ErrorString* error, |
234 int interval) { | 225 int interval) { |
235 if (m_recordingCPUProfile) { | 226 if (m_recordingCPUProfile) { |
236 *error = "Cannot change sampling interval when profiling."; | 227 *error = "Cannot change sampling interval when profiling."; |
237 return; | 228 return; |
238 } | 229 } |
239 m_state->setInteger(ProfilerAgentState::samplingInterval, interval); | 230 m_state->setInteger(ProfilerAgentState::samplingInterval, interval); |
240 profiler()->SetSamplingInterval(interval); | 231 m_profiler->SetSamplingInterval(interval); |
241 } | 232 } |
242 | 233 |
243 void V8ProfilerAgentImpl::restore() { | 234 void V8ProfilerAgentImpl::restore() { |
244 DCHECK(!m_enabled); | 235 DCHECK(!m_enabled); |
245 if (!m_state->booleanProperty(ProfilerAgentState::profilerEnabled, false)) | 236 if (!m_state->booleanProperty(ProfilerAgentState::profilerEnabled, false)) |
246 return; | 237 return; |
247 m_enabled = true; | 238 m_enabled = true; |
248 #if ENSURE_V8_VERSION(5, 4) | |
249 DCHECK(!m_profiler); | 239 DCHECK(!m_profiler); |
250 m_profiler = v8::CpuProfiler::New(m_isolate); | 240 m_profiler = v8::CpuProfiler::New(m_isolate); |
251 #endif | |
252 int interval = 0; | 241 int interval = 0; |
253 m_state->getInteger(ProfilerAgentState::samplingInterval, &interval); | 242 m_state->getInteger(ProfilerAgentState::samplingInterval, &interval); |
254 if (interval) profiler()->SetSamplingInterval(interval); | 243 if (interval) m_profiler->SetSamplingInterval(interval); |
255 if (m_state->booleanProperty(ProfilerAgentState::userInitiatedProfiling, | 244 if (m_state->booleanProperty(ProfilerAgentState::userInitiatedProfiling, |
256 false)) { | 245 false)) { |
257 ErrorString error; | 246 ErrorString error; |
258 start(&error); | 247 start(&error); |
259 } | 248 } |
260 } | 249 } |
261 | 250 |
262 void V8ProfilerAgentImpl::start(ErrorString* error) { | 251 void V8ProfilerAgentImpl::start(ErrorString* error) { |
263 if (m_recordingCPUProfile) return; | 252 if (m_recordingCPUProfile) return; |
264 if (!m_enabled) { | 253 if (!m_enabled) { |
(...skipping 23 matching lines...) Expand all Loading... |
288 m_frontendInitiatedProfileId = String16(); | 277 m_frontendInitiatedProfileId = String16(); |
289 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, false); | 278 m_state->setBoolean(ProfilerAgentState::userInitiatedProfiling, false); |
290 } | 279 } |
291 | 280 |
292 String16 V8ProfilerAgentImpl::nextProfileId() { | 281 String16 V8ProfilerAgentImpl::nextProfileId() { |
293 return String16::fromInteger(atomicIncrement(&s_lastProfileId)); | 282 return String16::fromInteger(atomicIncrement(&s_lastProfileId)); |
294 } | 283 } |
295 | 284 |
296 void V8ProfilerAgentImpl::startProfiling(const String16& title) { | 285 void V8ProfilerAgentImpl::startProfiling(const String16& title) { |
297 v8::HandleScope handleScope(m_isolate); | 286 v8::HandleScope handleScope(m_isolate); |
298 profiler()->StartProfiling(toV8String(m_isolate, title), true); | 287 m_profiler->StartProfiling(toV8String(m_isolate, title), true); |
299 } | 288 } |
300 | 289 |
301 std::unique_ptr<protocol::Profiler::Profile> V8ProfilerAgentImpl::stopProfiling( | 290 std::unique_ptr<protocol::Profiler::Profile> V8ProfilerAgentImpl::stopProfiling( |
302 const String16& title, bool serialize) { | 291 const String16& title, bool serialize) { |
303 v8::HandleScope handleScope(m_isolate); | 292 v8::HandleScope handleScope(m_isolate); |
304 v8::CpuProfile* profile = | 293 v8::CpuProfile* profile = |
305 profiler()->StopProfiling(toV8String(m_isolate, title)); | 294 m_profiler->StopProfiling(toV8String(m_isolate, title)); |
306 if (!profile) return nullptr; | 295 if (!profile) return nullptr; |
307 std::unique_ptr<protocol::Profiler::Profile> result; | 296 std::unique_ptr<protocol::Profiler::Profile> result; |
308 if (serialize) result = createCPUProfile(m_isolate, profile); | 297 if (serialize) result = createCPUProfile(m_isolate, profile); |
309 profile->Delete(); | 298 profile->Delete(); |
310 return result; | 299 return result; |
311 } | 300 } |
312 | 301 |
313 bool V8ProfilerAgentImpl::isRecording() const { | 302 bool V8ProfilerAgentImpl::isRecording() const { |
314 return m_recordingCPUProfile || !m_startedProfiles.empty(); | 303 return m_recordingCPUProfile || !m_startedProfiles.empty(); |
315 } | 304 } |
316 | 305 |
317 v8::CpuProfiler* V8ProfilerAgentImpl::profiler() { | |
318 #if ENSURE_V8_VERSION(5, 4) | |
319 return m_profiler; | |
320 #else | |
321 return m_isolate->GetCpuProfiler(); | |
322 #endif | |
323 } | |
324 | |
325 } // namespace v8_inspector | 306 } // namespace v8_inspector |
OLD | NEW |