66
77#include < vector>
88
9+ #include " include/v8-profiler.h"
910#include " src/base/atomicops.h"
1011#include " src/base/platform/time.h"
1112#include " src/debug/debug-interface.h"
1516#include " src/inspector/v8-inspector-impl.h"
1617#include " src/inspector/v8-inspector-session-impl.h"
1718#include " src/inspector/v8-stack-trace-impl.h"
18-
19- #include " include/v8-profiler.h"
19+ #include " src/logging/tracing-flags.h"
2020
2121namespace v8_inspector {
2222
@@ -30,6 +30,8 @@ static const char preciseCoverageDetailed[] = "preciseCoverageDetailed";
3030static const char preciseCoverageAllowTriggeredUpdates[] =
3131 " preciseCoverageAllowTriggeredUpdates" ;
3232static const char typeProfileStarted[] = " typeProfileStarted" ;
33+ static const char countersEnabled[] = " countersEnabled" ;
34+ static const char runtimeCallStatsEnabled[] = " runtimeCallStatsEnabled" ;
3335} // namespace ProfilerAgentState
3436
3537namespace {
@@ -220,22 +222,36 @@ void V8ProfilerAgentImpl::consoleProfileEnd(const String16& title) {
220222}
221223
222224Response V8ProfilerAgentImpl::enable () {
223- if (m_enabled) return Response::Success ();
224- m_enabled = true ;
225- m_state->setBoolean (ProfilerAgentState::profilerEnabled, true );
225+ if (!m_enabled) {
226+ m_enabled = true ;
227+ m_state->setBoolean (ProfilerAgentState::profilerEnabled, true );
228+ }
229+
226230 return Response::Success ();
227231}
228232
229233Response V8ProfilerAgentImpl::disable () {
230- if (!m_enabled) return Response::Success ();
231- for (size_t i = m_startedProfiles.size (); i > 0 ; --i)
232- stopProfiling (m_startedProfiles[i - 1 ].m_id , false );
233- m_startedProfiles.clear ();
234- stop (nullptr );
235- stopPreciseCoverage ();
236- DCHECK (!m_profiler);
237- m_enabled = false ;
238- m_state->setBoolean (ProfilerAgentState::profilerEnabled, false );
234+ if (m_enabled) {
235+ for (size_t i = m_startedProfiles.size (); i > 0 ; --i)
236+ stopProfiling (m_startedProfiles[i - 1 ].m_id , false );
237+ m_startedProfiles.clear ();
238+ stop (nullptr );
239+ stopPreciseCoverage ();
240+ DCHECK (!m_profiler);
241+ m_enabled = false ;
242+ m_state->setBoolean (ProfilerAgentState::profilerEnabled, false );
243+ }
244+
245+ if (m_counters) {
246+ disableCounters ();
247+ m_state->setBoolean (ProfilerAgentState::countersEnabled, false );
248+ }
249+
250+ if (m_runtime_call_stats_enabled) {
251+ disableRuntimeCallStats ();
252+ m_state->setBoolean (ProfilerAgentState::runtimeCallStatsEnabled, false );
253+ }
254+
239255 return Response::Success ();
240256}
241257
@@ -250,25 +266,34 @@ Response V8ProfilerAgentImpl::setSamplingInterval(int interval) {
250266
251267void V8ProfilerAgentImpl::restore () {
252268 DCHECK (!m_enabled);
253- if (!m_state->booleanProperty (ProfilerAgentState::profilerEnabled, false ))
254- return ;
255- m_enabled = true ;
256- DCHECK (!m_profiler);
257- if (m_state->booleanProperty (ProfilerAgentState::userInitiatedProfiling,
258- false )) {
259- start ();
269+ if (m_state->booleanProperty (ProfilerAgentState::profilerEnabled, false )) {
270+ m_enabled = true ;
271+ DCHECK (!m_profiler);
272+ if (m_state->booleanProperty (ProfilerAgentState::userInitiatedProfiling,
273+ false )) {
274+ start ();
275+ }
276+ if (m_state->booleanProperty (ProfilerAgentState::preciseCoverageStarted,
277+ false )) {
278+ bool callCount = m_state->booleanProperty (
279+ ProfilerAgentState::preciseCoverageCallCount, false );
280+ bool detailed = m_state->booleanProperty (
281+ ProfilerAgentState::preciseCoverageDetailed, false );
282+ bool updatesAllowed = m_state->booleanProperty (
283+ ProfilerAgentState::preciseCoverageAllowTriggeredUpdates, false );
284+ double timestamp;
285+ startPreciseCoverage (Maybe<bool >(callCount), Maybe<bool >(detailed),
286+ Maybe<bool >(updatesAllowed), ×tamp);
287+ }
260288 }
261- if (m_state->booleanProperty (ProfilerAgentState::preciseCoverageStarted,
289+
290+ if (m_state->booleanProperty (ProfilerAgentState::countersEnabled, false )) {
291+ enableCounters ();
292+ }
293+
294+ if (m_state->booleanProperty (ProfilerAgentState::runtimeCallStatsEnabled,
262295 false )) {
263- bool callCount = m_state->booleanProperty (
264- ProfilerAgentState::preciseCoverageCallCount, false );
265- bool detailed = m_state->booleanProperty (
266- ProfilerAgentState::preciseCoverageDetailed, false );
267- bool updatesAllowed = m_state->booleanProperty (
268- ProfilerAgentState::preciseCoverageAllowTriggeredUpdates, false );
269- double timestamp;
270- startPreciseCoverage (Maybe<bool >(callCount), Maybe<bool >(detailed),
271- Maybe<bool >(updatesAllowed), ×tamp);
296+ enableRuntimeCallStats ();
272297 }
273298}
274299
@@ -525,10 +550,9 @@ Response V8ProfilerAgentImpl::takeTypeProfile(
525550 return Response::Success ();
526551}
527552
528- Response V8ProfilerAgentImpl::enableRuntimeCallStats () {
553+ Response V8ProfilerAgentImpl::enableCounters () {
529554 if (m_counters)
530- return Response::ServerError (
531- " RuntimeCallStats collection already enabled." );
555+ return Response::ServerError (" Counters collection already enabled." );
532556
533557 if (V8Inspector* inspector = v8::debug::GetInspector (m_isolate))
534558 m_counters = inspector->enableCounters ();
@@ -538,16 +562,16 @@ Response V8ProfilerAgentImpl::enableRuntimeCallStats() {
538562 return Response::Success ();
539563}
540564
541- Response V8ProfilerAgentImpl::disableRuntimeCallStats () {
565+ Response V8ProfilerAgentImpl::disableCounters () {
542566 if (m_counters) m_counters.reset ();
543567 return Response::Success ();
544568}
545569
546- Response V8ProfilerAgentImpl::getRuntimeCallStats (
570+ Response V8ProfilerAgentImpl::getCounters (
547571 std::unique_ptr<protocol::Array<protocol::Profiler::CounterInfo>>*
548572 out_result) {
549573 if (!m_counters)
550- return Response::ServerError (" RuntimeCallStats collection is not enabled." );
574+ return Response::ServerError (" Counters collection is not enabled." );
551575
552576 *out_result =
553577 std::make_unique<protocol::Array<protocol::Profiler::CounterInfo>>();
@@ -564,6 +588,66 @@ Response V8ProfilerAgentImpl::getRuntimeCallStats(
564588 return Response::Success ();
565589}
566590
591+ Response V8ProfilerAgentImpl::enableRuntimeCallStats () {
592+ if (v8::internal::TracingFlags::runtime_stats.load ()) {
593+ return Response::ServerError (
594+ " Runtime Call Stats collection is already enabled." );
595+ }
596+
597+ v8::internal::TracingFlags::runtime_stats.store (true );
598+ m_runtime_call_stats_enabled = true ;
599+
600+ return Response::Success ();
601+ }
602+
603+ Response V8ProfilerAgentImpl::disableRuntimeCallStats () {
604+ if (!v8::internal::TracingFlags::runtime_stats.load ()) {
605+ return Response::ServerError (
606+ " Runtime Call Stats collection is not enabled." );
607+ }
608+
609+ if (!m_runtime_call_stats_enabled) {
610+ return Response::ServerError (
611+ " Runtime Call Stats collection was not enabled by this session." );
612+ }
613+
614+ v8::internal::TracingFlags::runtime_stats.store (false );
615+ m_runtime_call_stats_enabled = false ;
616+
617+ return Response::Success ();
618+ }
619+
620+ Response V8ProfilerAgentImpl::getRuntimeCallStats (
621+ std::unique_ptr<
622+ protocol::Array<protocol::Profiler::RuntimeCallCounterInfo>>*
623+ out_result) {
624+ if (!m_runtime_call_stats_enabled) {
625+ return Response::ServerError (
626+ " Runtime Call Stats collection is not enabled." );
627+ }
628+
629+ if (!v8::internal::TracingFlags::runtime_stats.load ()) {
630+ return Response::ServerError (
631+ " Runtime Call Stats collection was disabled outside of this session." );
632+ }
633+
634+ *out_result = std::make_unique<
635+ protocol::Array<protocol::Profiler::RuntimeCallCounterInfo>>();
636+
637+ v8::debug::EnumerateRuntimeCallCounters (
638+ m_isolate,
639+ [&](const char * name, int64_t count, v8::base::TimeDelta time) {
640+ (*out_result)
641+ ->emplace_back (protocol::Profiler::RuntimeCallCounterInfo::create ()
642+ .setName (String16 (name))
643+ .setValue (static_cast <double >(count))
644+ .setTime (time.InSecondsF ())
645+ .build ());
646+ });
647+
648+ return Response::Success ();
649+ }
650+
567651String16 V8ProfilerAgentImpl::nextProfileId () {
568652 return String16::fromInteger (
569653 v8::base::Relaxed_AtomicIncrement (&s_lastProfileId, 1 ));
0 commit comments