[WTF] Add makeUnique<T>, which ensures T is fast-allocated, makeUnique / makeUniqueWi...
[WebKit-https.git] / Source / WebCore / inspector / agents / InspectorMemoryAgent.cpp
1 /*
2  * Copyright (C) 2016 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 "InspectorMemoryAgent.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
37 namespace WebCore {
38
39 using namespace Inspector;
40
41 InspectorMemoryAgent::InspectorMemoryAgent(PageAgentContext& context)
42     : InspectorAgentBase("Memory"_s, context)
43     , m_frontendDispatcher(makeUnique<Inspector::MemoryFrontendDispatcher>(context.frontendRouter))
44     , m_backendDispatcher(Inspector::MemoryBackendDispatcher::create(context.backendDispatcher, this))
45 {
46 }
47
48 void InspectorMemoryAgent::didCreateFrontendAndBackend(FrontendRouter*, BackendDispatcher*)
49 {
50 }
51
52 void InspectorMemoryAgent::willDestroyFrontendAndBackend(DisconnectReason)
53 {
54     ErrorString ignored;
55     disable(ignored);
56 }
57
58 void InspectorMemoryAgent::enable(ErrorString& errorString)
59 {
60     if (m_instrumentingAgents.inspectorMemoryAgent() == this) {
61         errorString = "MemoryAgent already enabled"_s;
62         return;
63     }
64
65     m_instrumentingAgents.setInspectorMemoryAgent(this);
66 }
67
68 void InspectorMemoryAgent::disable(ErrorString& errorString)
69 {
70     if (m_instrumentingAgents.inspectorMemoryAgent() != this) {
71         errorString = "MemoryAgent already disabled"_s;
72         return;
73     }
74
75     m_instrumentingAgents.setInspectorMemoryAgent(nullptr);
76
77     m_tracking = false;
78
79     ResourceUsageThread::removeObserver(this);
80 }
81
82 void InspectorMemoryAgent::startTracking(ErrorString&)
83 {
84     if (m_tracking)
85         return;
86
87     ResourceUsageThread::addObserver(this, Memory, [this] (const ResourceUsageData& data) {
88         collectSample(data);
89     });
90
91     m_tracking = true;
92
93     m_frontendDispatcher->trackingStart(m_environment.executionStopwatch()->elapsedTime().seconds());
94 }
95
96 void InspectorMemoryAgent::stopTracking(ErrorString&)
97 {
98     if (!m_tracking)
99         return;
100
101     ResourceUsageThread::removeObserver(this);
102
103     m_tracking = false;
104
105     m_frontendDispatcher->trackingComplete(m_environment.executionStopwatch()->elapsedTime().seconds());
106 }
107
108 void InspectorMemoryAgent::didHandleMemoryPressure(Critical critical)
109 {
110     MemoryFrontendDispatcher::Severity severity = critical == Critical::Yes ? MemoryFrontendDispatcher::Severity::Critical : MemoryFrontendDispatcher::Severity::NonCritical;
111     m_frontendDispatcher->memoryPressure(m_environment.executionStopwatch()->elapsedTime().seconds(), severity);
112 }
113
114 void InspectorMemoryAgent::collectSample(const ResourceUsageData& data)
115 {
116     auto javascriptCategory = Protocol::Memory::CategoryData::create()
117         .setType(Protocol::Memory::CategoryData::Type::JavaScript)
118         .setSize(data.categories[MemoryCategory::GCHeap].totalSize() + data.categories[MemoryCategory::GCOwned].totalSize())
119         .release();
120
121     auto jitCategory = Protocol::Memory::CategoryData::create()
122         .setType(Protocol::Memory::CategoryData::Type::JIT)
123         .setSize(data.categories[MemoryCategory::JSJIT].totalSize())
124         .release();
125
126     auto imagesCategory = Protocol::Memory::CategoryData::create()
127         .setType(Protocol::Memory::CategoryData::Type::Images)
128         .setSize(data.categories[MemoryCategory::Images].totalSize())
129         .release();
130
131     auto layersCategory = Protocol::Memory::CategoryData::create()
132         .setType(Protocol::Memory::CategoryData::Type::Layers)
133         .setSize(data.categories[MemoryCategory::Layers].totalSize())
134         .release();
135
136     auto pageCategory = Protocol::Memory::CategoryData::create()
137         .setType(Protocol::Memory::CategoryData::Type::Page)
138         .setSize(data.categories[MemoryCategory::bmalloc].totalSize() + data.categories[MemoryCategory::LibcMalloc].totalSize())
139         .release();
140
141     auto otherCategory = Protocol::Memory::CategoryData::create()
142         .setType(Protocol::Memory::CategoryData::Type::Other)
143         .setSize(data.categories[MemoryCategory::Other].totalSize())
144         .release();
145
146     auto categories = JSON::ArrayOf<Protocol::Memory::CategoryData>::create();
147     categories->addItem(WTFMove(javascriptCategory));
148     categories->addItem(WTFMove(jitCategory));
149     categories->addItem(WTFMove(imagesCategory));
150     categories->addItem(WTFMove(layersCategory));
151     categories->addItem(WTFMove(pageCategory));
152     categories->addItem(WTFMove(otherCategory));
153
154     auto event = Protocol::Memory::Event::create()
155         .setTimestamp(m_environment.executionStopwatch()->elapsedTimeSince(data.timestamp).seconds())
156         .setCategories(WTFMove(categories))
157         .release();
158
159     m_frontendDispatcher->trackingUpdate(WTFMove(event));
160 }
161
162 } // namespace WebCore
163
164 #endif // ENABLE(RESOURCE_USAGE)