Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / WebLeakDetector.cpp
blob72b85fac0a9773b54c954dfadc8496f62b99f660
1 /*
2 * Copyright (C) 2014 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "config.h"
33 #include "public/web/WebLeakDetector.h"
35 #include "bindings/core/v8/ScriptPromise.h"
36 #include "bindings/core/v8/V8Binding.h"
37 #include "bindings/core/v8/V8GCController.h"
38 #include "core/dom/ActiveDOMObject.h"
39 #include "core/dom/Document.h"
40 #include "core/fetch/MemoryCache.h"
41 #include "core/fetch/ResourceFetcher.h"
42 #include "core/inspector/InstanceCounters.h"
43 #include "core/layout/LayoutObject.h"
44 #include "core/workers/WorkerThread.h"
45 #include "modules/webaudio/AudioNode.h"
46 #include "platform/Timer.h"
47 #include "public/web/WebDocument.h"
48 #include "public/web/WebLocalFrame.h"
50 namespace blink {
52 namespace {
54 class WebLeakDetectorImpl final : public WebLeakDetector {
55 WTF_MAKE_NONCOPYABLE(WebLeakDetectorImpl);
56 public:
57 explicit WebLeakDetectorImpl(WebLeakDetectorClient* client)
58 : m_client(client)
59 , m_delayedGCAndReportTimer(this, &WebLeakDetectorImpl::delayedGCAndReport)
60 , m_delayedReportTimer(this, &WebLeakDetectorImpl::delayedReport)
61 , m_numberOfGCNeeded(0)
63 ASSERT(m_client);
66 ~WebLeakDetectorImpl() override {}
68 void collectGarbageAndGetDOMCounts(WebLocalFrame*) override;
70 private:
71 void delayedGCAndReport(Timer<WebLeakDetectorImpl>*);
72 void delayedReport(Timer<WebLeakDetectorImpl>*);
74 WebLeakDetectorClient* m_client;
75 Timer<WebLeakDetectorImpl> m_delayedGCAndReportTimer;
76 Timer<WebLeakDetectorImpl> m_delayedReportTimer;
77 int m_numberOfGCNeeded;
80 void WebLeakDetectorImpl::collectGarbageAndGetDOMCounts(WebLocalFrame* frame)
82 v8::Isolate* isolate = v8::Isolate::GetCurrent();
83 v8::HandleScope handleScope(isolate);
85 // For example, calling isValidEmailAddress in EmailInputType.cpp with a
86 // non-empty string creates a static ScriptRegexp value which holds a
87 // V8PerContextData indirectly. This affects the number of V8PerContextData.
88 // To ensure that context data is created, call ensureScriptRegexpContext
89 // here.
90 V8PerIsolateData::from(isolate)->ensureScriptRegexpContext();
92 WorkerThread::terminateAndWaitForAllWorkers();
93 memoryCache()->evictResources();
96 RefPtrWillBeRawPtr<Document> document = PassRefPtrWillBeRawPtr<Document>(frame->document());
97 if (ResourceFetcher* fetcher = document->fetcher())
98 fetcher->garbageCollectDocumentResources();
101 // FIXME: HTML5 Notification should be closed because notification affects the result of number of DOM objects.
103 V8GCController::collectAllGarbageForTesting(isolate);
104 // Note: Oilpan precise GC is scheduled at the end of the event loop.
106 V8PerIsolateData::from(isolate)->clearScriptRegexpContext();
108 // Task queue may contain delayed object destruction tasks.
109 // This method is called from navigation hook inside FrameLoader,
110 // so previous document is still held by the loader until the next event loop.
111 // Complete all pending tasks before proceeding to gc.
112 m_numberOfGCNeeded = 2;
113 m_delayedGCAndReportTimer.startOneShot(0, FROM_HERE);
116 void WebLeakDetectorImpl::delayedGCAndReport(Timer<WebLeakDetectorImpl>*)
118 // We do a second and third GC here to address flakiness
119 // The second GC is necessary as Resource GC may have postponed clean-up tasks to next event loop.
120 // The third GC is necessary for cleaning up Document after worker object died.
122 V8GCController::collectAllGarbageForTesting(V8PerIsolateData::mainThreadIsolate());
123 // Note: Oilpan precise GC is scheduled at the end of the event loop.
125 // Inspect counters on the next event loop.
126 if (--m_numberOfGCNeeded)
127 m_delayedGCAndReportTimer.startOneShot(0, FROM_HERE);
128 else
129 m_delayedReportTimer.startOneShot(0, FROM_HERE);
132 void WebLeakDetectorImpl::delayedReport(Timer<WebLeakDetectorImpl>*)
134 ASSERT(m_client);
136 WebLeakDetectorClient::Result result;
137 result.numberOfLiveAudioNodes = InstanceCounters::counterValue(InstanceCounters::AudioHandlerCounter);
138 result.numberOfLiveDocuments = InstanceCounters::counterValue(InstanceCounters::DocumentCounter);
139 result.numberOfLiveNodes = InstanceCounters::counterValue(InstanceCounters::NodeCounter);
140 result.numberOfLiveLayoutObjects = InstanceCounters::counterValue(InstanceCounters::LayoutObjectCounter);
141 result.numberOfLiveResources = InstanceCounters::counterValue(InstanceCounters::ResourceCounter);
142 result.numberOfLiveActiveDOMObjects = InstanceCounters::counterValue(InstanceCounters::ActiveDOMObjectCounter);
143 result.numberOfLiveScriptPromises = InstanceCounters::counterValue(InstanceCounters::ScriptPromiseCounter);
144 result.numberOfLiveFrames = InstanceCounters::counterValue(InstanceCounters::FrameCounter);
145 result.numberOfLiveV8PerContextData = InstanceCounters::counterValue(InstanceCounters::V8PerContextDataCounter);
147 m_client->onLeakDetectionComplete(result);
149 #ifndef NDEBUG
150 showLiveDocumentInstances();
151 #endif
154 } // namespace
156 WebLeakDetector* WebLeakDetector::create(WebLeakDetectorClient* client)
158 return new WebLeakDetectorImpl(client);
161 } // namespace blink