5bc3c24294583af9b70a852ba5a34aa71605bb61
[WebKit-https.git] / Source / JavaScriptCore / inspector / agents / InspectorHeapAgent.cpp
1 /*
2  * Copyright (C) 2015, 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. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "InspectorHeapAgent.h"
28
29 #include "HeapProfiler.h"
30 #include "InspectorEnvironment.h"
31 #include "JSCInlines.h"
32 #include "VM.h"
33 #include <wtf/RunLoop.h>
34 #include <wtf/Stopwatch.h>
35
36 using namespace JSC;
37
38 namespace Inspector {
39
40 InspectorHeapAgent::InspectorHeapAgent(AgentContext& context)
41     : InspectorAgentBase(ASCIILiteral("Heap"))
42     , m_frontendDispatcher(std::make_unique<HeapFrontendDispatcher>(context.frontendRouter))
43     , m_backendDispatcher(HeapBackendDispatcher::create(context.backendDispatcher, this))
44     , m_environment(context.environment)
45 {
46 }
47
48 InspectorHeapAgent::~InspectorHeapAgent()
49 {
50 }
51
52 void InspectorHeapAgent::didCreateFrontendAndBackend(FrontendRouter*, BackendDispatcher*)
53 {
54 }
55
56 void InspectorHeapAgent::willDestroyFrontendAndBackend(DisconnectReason)
57 {
58     ErrorString ignored;
59     disable(ignored);
60 }
61
62 void InspectorHeapAgent::enable(ErrorString&)
63 {
64     if (m_enabled)
65         return;
66
67     m_enabled = true;
68
69     m_environment.vm().heap.addObserver(this);
70 }
71
72 void InspectorHeapAgent::disable(ErrorString&)
73 {
74     if (!m_enabled)
75         return;
76
77     m_enabled = false;
78
79     m_environment.vm().heap.removeObserver(this);
80
81     clearHeapSnapshots();
82 }
83
84 void InspectorHeapAgent::gc(ErrorString&)
85 {
86     VM& vm = m_environment.vm();
87     JSLockHolder lock(vm);
88     sanitizeStackForVM(&vm);
89     vm.heap.collectAllGarbage();
90 }
91
92 void InspectorHeapAgent::snapshot(ErrorString&, double* timestamp, String* snapshotData)
93 {
94     VM& vm = m_environment.vm();
95     JSLockHolder lock(vm);
96
97     HeapSnapshotBuilder snapshotBuilder(vm.ensureHeapProfiler());
98     snapshotBuilder.buildSnapshot();
99
100     *timestamp = m_environment.executionStopwatch()->elapsedTime();
101     *snapshotData = snapshotBuilder.json([&] (const HeapSnapshotNode& node) {
102         if (Structure* structure = node.cell->structure(vm)) {
103             if (JSGlobalObject* globalObject = structure->globalObject()) {
104                 if (!m_environment.canAccessInspectedScriptState(globalObject->globalExec()))
105                     return false;
106             }
107         }
108         return true;
109     });
110 }
111
112 static Inspector::Protocol::Heap::GarbageCollection::Type protocolTypeForHeapOperation(HeapOperation operation)
113 {
114     switch (operation) {
115     case FullCollection:
116         return Inspector::Protocol::Heap::GarbageCollection::Type::Full;
117     case EdenCollection:
118         return Inspector::Protocol::Heap::GarbageCollection::Type::Partial;
119     default:
120         ASSERT_NOT_REACHED();
121         return Inspector::Protocol::Heap::GarbageCollection::Type::Full;
122     }
123 }
124
125 void InspectorHeapAgent::willGarbageCollect()
126 {
127     ASSERT(m_enabled);
128     ASSERT(std::isnan(m_gcStartTime));
129
130     m_gcStartTime = m_environment.executionStopwatch()->elapsedTime();
131 }
132
133 void InspectorHeapAgent::didGarbageCollect(HeapOperation operation)
134 {
135     ASSERT(m_enabled);
136     ASSERT(!std::isnan(m_gcStartTime));
137
138     // FIXME: Include number of bytes freed by collection.
139
140     double startTime = m_gcStartTime;
141     double endTime = m_environment.executionStopwatch()->elapsedTime();
142
143     // Dispatch the event asynchronously because this method may be
144     // called between collection and sweeping and we don't want to
145     // create unexpected JavaScript allocations that the Sweeper does
146     // not expect to encounter. JavaScript allocations could happen
147     // with WebKitLegacy's in process inspector which shares the same
148     // VM as the inspected page.
149
150     RunLoop::current().dispatch([this, startTime, endTime, operation]() {
151         auto collection = Inspector::Protocol::Heap::GarbageCollection::create()
152             .setType(protocolTypeForHeapOperation(operation))
153             .setStartTime(startTime)
154             .setEndTime(endTime)
155             .release();
156
157         m_frontendDispatcher->garbageCollected(WTFMove(collection));
158     });
159
160     m_gcStartTime = NAN;
161 }
162
163 void InspectorHeapAgent::clearHeapSnapshots()
164 {
165     if (HeapProfiler* heapProfiler = m_environment.vm().heapProfiler())
166         heapProfiler->clearSnapshots();
167 }
168
169 } // namespace Inspector