Web Inspector: Timelines: can't reliably stop/start a recording
[WebKit-https.git] / Source / WebCore / inspector / agents / InspectorCPUProfilerAgent.cpp
1 /*
2  * Copyright (C) 2019 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "InspectorCPUProfilerAgent.h"
28
29 #if ENABLE(RESOURCE_USAGE)
30
31 #include "InstrumentingAgents.h"
32 #include "ResourceUsageThread.h"
33 #include <JavaScriptCore/InspectorEnvironment.h>
34 #include <wtf/Stopwatch.h>
35
36 namespace WebCore {
37
38 using namespace Inspector;
39
40 InspectorCPUProfilerAgent::InspectorCPUProfilerAgent(PageAgentContext& context)
41     : InspectorAgentBase("CPUProfiler"_s, context)
42     , m_frontendDispatcher(std::make_unique<Inspector::CPUProfilerFrontendDispatcher>(context.frontendRouter))
43     , m_backendDispatcher(Inspector::CPUProfilerBackendDispatcher::create(context.backendDispatcher, this))
44 {
45 }
46
47 void InspectorCPUProfilerAgent::didCreateFrontendAndBackend(FrontendRouter*, BackendDispatcher*)
48 {
49     m_instrumentingAgents.setInspectorCPUProfilerAgent(this);
50 }
51
52 void InspectorCPUProfilerAgent::willDestroyFrontendAndBackend(DisconnectReason)
53 {
54     m_instrumentingAgents.setInspectorCPUProfilerAgent(nullptr);
55 }
56
57 void InspectorCPUProfilerAgent::startTracking(ErrorString&)
58 {
59     if (m_tracking)
60         return;
61
62     ResourceUsageThread::addObserver(this, CPU, [this] (const ResourceUsageData& data) {
63         collectSample(data);
64     });
65
66     m_tracking = true;
67
68     m_frontendDispatcher->trackingStart(m_environment.executionStopwatch()->elapsedTime().seconds());
69 }
70
71 void InspectorCPUProfilerAgent::stopTracking(ErrorString&)
72 {
73     if (!m_tracking)
74         return;
75
76     ResourceUsageThread::removeObserver(this);
77
78     m_tracking = false;
79
80     m_frontendDispatcher->trackingComplete(m_environment.executionStopwatch()->elapsedTime().seconds());
81 }
82
83 static Ref<Protocol::CPUProfiler::ThreadInfo> buildThreadInfo(const ThreadCPUInfo& thread)
84 {
85     ASSERT(thread.cpu <= 100);
86
87     auto threadInfo = Protocol::CPUProfiler::ThreadInfo::create()
88         .setName(thread.name)
89         .setUsage(thread.cpu)
90         .release();
91
92     if (thread.type == ThreadCPUInfo::Type::Main)
93         threadInfo->setType(Protocol::CPUProfiler::ThreadInfo::Type::Main);
94     else if (thread.type == ThreadCPUInfo::Type::WebKit)
95         threadInfo->setType(Protocol::CPUProfiler::ThreadInfo::Type::WebKit);
96
97     if (!thread.identifier.isEmpty())
98         threadInfo->setTargetId(thread.identifier);
99
100     return threadInfo;
101 }
102
103 void InspectorCPUProfilerAgent::collectSample(const ResourceUsageData& data)
104 {
105     auto event = Protocol::CPUProfiler::Event::create()
106         .setTimestamp(m_environment.executionStopwatch()->elapsedTimeSince(data.timestamp).seconds())
107         .setUsage(data.cpuExcludingDebuggerThreads)
108         .release();
109
110     if (!data.cpuThreads.isEmpty()) {
111         RefPtr<JSON::ArrayOf<Protocol::CPUProfiler::ThreadInfo>> threads = JSON::ArrayOf<Protocol::CPUProfiler::ThreadInfo>::create();
112         for (auto& threadInfo : data.cpuThreads)
113             threads->addItem(buildThreadInfo(threadInfo));
114         event->setThreads(WTFMove(threads));
115     }
116
117     m_frontendDispatcher->trackingUpdate(WTFMove(event));
118 }
119
120 } // namespace WebCore
121
122 #endif // ENABLE(RESOURCE_USAGE)