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
36namespace WebCore {
37
38using namespace Inspector;
39
40InspectorCPUProfilerAgent::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
47void InspectorCPUProfilerAgent::didCreateFrontendAndBackend(FrontendRouter*, BackendDispatcher*)
48{
49 m_instrumentingAgents.setInspectorCPUProfilerAgent(this);
50}
51
52void InspectorCPUProfilerAgent::willDestroyFrontendAndBackend(DisconnectReason)
53{
54 ErrorString ignored;
55 stopTracking(ignored);
56
57 m_instrumentingAgents.setInspectorCPUProfilerAgent(nullptr);
58}
59
60void InspectorCPUProfilerAgent::startTracking(ErrorString&)
61{
62 if (m_tracking)
63 return;
64
65 ResourceUsageThread::addObserver(this, CPU, [this] (const ResourceUsageData& data) {
66 collectSample(data);
67 });
68
69 m_tracking = true;
70
71 m_frontendDispatcher->trackingStart(m_environment.executionStopwatch()->elapsedTime().seconds());
72}
73
74void InspectorCPUProfilerAgent::stopTracking(ErrorString&)
75{
76 if (!m_tracking)
77 return;
78
79 ResourceUsageThread::removeObserver(this);
80
81 m_tracking = false;
82
83 m_frontendDispatcher->trackingComplete(m_environment.executionStopwatch()->elapsedTime().seconds());
84}
85
86static Ref<Protocol::CPUProfiler::ThreadInfo> buildThreadInfo(const ThreadCPUInfo& thread)
87{
88 ASSERT(thread.cpu <= 100);
89
90 auto threadInfo = Protocol::CPUProfiler::ThreadInfo::create()
91 .setName(thread.name)
92 .setUsage(thread.cpu)
93 .release();
94
95 if (thread.type == ThreadCPUInfo::Type::Main)
96 threadInfo->setType(Protocol::CPUProfiler::ThreadInfo::Type::Main);
97 else if (thread.type == ThreadCPUInfo::Type::WebKit)
98 threadInfo->setType(Protocol::CPUProfiler::ThreadInfo::Type::WebKit);
99
100 if (!thread.identifier.isEmpty())
101 threadInfo->setTargetId(thread.identifier);
102
103 return threadInfo;
104}
105
106void InspectorCPUProfilerAgent::collectSample(const ResourceUsageData& data)
107{
108 auto event = Protocol::CPUProfiler::Event::create()
109 .setTimestamp(m_environment.executionStopwatch()->elapsedTimeSince(data.timestamp).seconds())
110 .setUsage(data.cpuExcludingDebuggerThreads)
111 .release();
112
113 if (!data.cpuThreads.isEmpty()) {
114 RefPtr<JSON::ArrayOf<Protocol::CPUProfiler::ThreadInfo>> threads = JSON::ArrayOf<Protocol::CPUProfiler::ThreadInfo>::create();
115 for (auto& threadInfo : data.cpuThreads)
116 threads->addItem(buildThreadInfo(threadInfo));
117 event->setThreads(WTFMove(threads));
118 }
119
120 m_frontendDispatcher->trackingUpdate(WTFMove(event));
121}
122
123} // namespace WebCore
124
125#endif // ENABLE(RESOURCE_USAGE)
126