1 | /* |
2 | * Copyright (C) 2015-2017 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 "WebHeapAgent.h" |
28 | |
29 | #include "InstrumentingAgents.h" |
30 | #include "WebConsoleAgent.h" |
31 | #include <wtf/RunLoop.h> |
32 | |
33 | namespace WebCore { |
34 | |
35 | using namespace Inspector; |
36 | |
37 | struct GarbageCollectionData { |
38 | Inspector::Protocol::Heap::GarbageCollection::Type type; |
39 | Seconds startTime; |
40 | Seconds endTime; |
41 | }; |
42 | |
43 | class SendGarbageCollectionEventsTask { |
44 | public: |
45 | SendGarbageCollectionEventsTask(WebHeapAgent&); |
46 | void addGarbageCollection(GarbageCollectionData&&); |
47 | void reset(); |
48 | private: |
49 | void timerFired(); |
50 | |
51 | WebHeapAgent& m_agent; |
52 | Vector<GarbageCollectionData> m_collections; |
53 | RunLoop::Timer<SendGarbageCollectionEventsTask> m_timer; |
54 | Lock m_mutex; |
55 | }; |
56 | |
57 | SendGarbageCollectionEventsTask::SendGarbageCollectionEventsTask(WebHeapAgent& agent) |
58 | : m_agent(agent) |
59 | , m_timer(RunLoop::main(), this, &SendGarbageCollectionEventsTask::timerFired) |
60 | { |
61 | } |
62 | |
63 | void SendGarbageCollectionEventsTask::addGarbageCollection(GarbageCollectionData&& collection) |
64 | { |
65 | { |
66 | std::lock_guard<Lock> lock(m_mutex); |
67 | m_collections.append(WTFMove(collection)); |
68 | } |
69 | |
70 | if (!m_timer.isActive()) |
71 | m_timer.startOneShot(0_s); |
72 | } |
73 | |
74 | void SendGarbageCollectionEventsTask::reset() |
75 | { |
76 | { |
77 | std::lock_guard<Lock> lock(m_mutex); |
78 | m_collections.clear(); |
79 | } |
80 | |
81 | m_timer.stop(); |
82 | } |
83 | |
84 | void SendGarbageCollectionEventsTask::timerFired() |
85 | { |
86 | Vector<GarbageCollectionData> collectionsToSend; |
87 | |
88 | { |
89 | std::lock_guard<Lock> lock(m_mutex); |
90 | m_collections.swap(collectionsToSend); |
91 | } |
92 | |
93 | m_agent.dispatchGarbageCollectionEventsAfterDelay(WTFMove(collectionsToSend)); |
94 | } |
95 | |
96 | WebHeapAgent::WebHeapAgent(WebAgentContext& context) |
97 | : InspectorHeapAgent(context) |
98 | , m_instrumentingAgents(context.instrumentingAgents) |
99 | , m_sendGarbageCollectionEventsTask(std::make_unique<SendGarbageCollectionEventsTask>(*this)) |
100 | { |
101 | } |
102 | |
103 | WebHeapAgent::~WebHeapAgent() |
104 | { |
105 | m_sendGarbageCollectionEventsTask->reset(); |
106 | } |
107 | |
108 | void WebHeapAgent::enable(ErrorString& errorString) |
109 | { |
110 | InspectorHeapAgent::enable(errorString); |
111 | |
112 | if (auto* consoleAgent = m_instrumentingAgents.webConsoleAgent()) |
113 | consoleAgent->setInspectorHeapAgent(this); |
114 | } |
115 | |
116 | void WebHeapAgent::disable(ErrorString& errorString) |
117 | { |
118 | m_sendGarbageCollectionEventsTask->reset(); |
119 | |
120 | if (auto* consoleAgent = m_instrumentingAgents.webConsoleAgent()) |
121 | consoleAgent->setInspectorHeapAgent(nullptr); |
122 | |
123 | InspectorHeapAgent::disable(errorString); |
124 | } |
125 | |
126 | void WebHeapAgent::dispatchGarbageCollectedEvent(Inspector::Protocol::Heap::GarbageCollection::Type type, Seconds startTime, Seconds endTime) |
127 | { |
128 | // Dispatch the event asynchronously because this method may be |
129 | // called between collection and sweeping and we don't want to |
130 | // create unexpected JavaScript allocations that the Sweeper does |
131 | // not expect to encounter. JavaScript allocations could happen |
132 | // with WebKitLegacy's in process inspector which shares the same |
133 | // VM as the inspected page. |
134 | |
135 | GarbageCollectionData data = {type, startTime, endTime}; |
136 | m_sendGarbageCollectionEventsTask->addGarbageCollection(WTFMove(data)); |
137 | } |
138 | |
139 | void WebHeapAgent::dispatchGarbageCollectionEventsAfterDelay(Vector<GarbageCollectionData>&& collections) |
140 | { |
141 | for (auto& collection : collections) |
142 | InspectorHeapAgent::dispatchGarbageCollectedEvent(collection.type, collection.startTime, collection.endTime); |
143 | } |
144 | |
145 | } // namespace WebCore |
146 | |