• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 Google 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 are
6  * met:
7  *
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.
17  *
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.
29  */
30 
31 #include "config.h"
32 
33 #include "public/web/WebLeakDetector.h"
34 
35 #include "bindings/core/v8/V8Binding.h"
36 #include "bindings/core/v8/V8GCController.h"
37 #include "core/dom/Document.h"
38 #include "core/fetch/MemoryCache.h"
39 #include "core/fetch/ResourceFetcher.h"
40 #include "core/inspector/InspectorCounters.h"
41 #include "core/rendering/RenderObject.h"
42 #include "modules/webaudio/AudioNode.h"
43 #include "platform/Timer.h"
44 #include "public/web/WebDocument.h"
45 #include "public/web/WebLocalFrame.h"
46 #include "web/WebEmbeddedWorkerImpl.h"
47 
48 #include <v8.h>
49 
50 namespace blink {
51 
52 namespace {
53 
54 // FIXME: Oilpan: It may take multiple GC to collect on-heap objects referenced from off-heap objects.
55 // Please see comment in Heap::collectAllGarbage()
56 static const int kNumberOfGCsToClaimChains = 5;
57 
58 class WebLeakDetectorImpl FINAL : public WebLeakDetector {
59 WTF_MAKE_NONCOPYABLE(WebLeakDetectorImpl);
60 public:
WebLeakDetectorImpl(WebLeakDetectorClient * client)61     explicit WebLeakDetectorImpl(WebLeakDetectorClient* client)
62         : m_client(client)
63         , m_delayedGCAndReportTimer(this, &WebLeakDetectorImpl::delayedGCAndReport)
64         , m_delayedReportTimer(this, &WebLeakDetectorImpl::delayedReport)
65         , m_numberOfGCNeeded(0)
66     {
67         ASSERT(m_client);
68     }
69 
~WebLeakDetectorImpl()70     virtual ~WebLeakDetectorImpl() { }
71 
72     virtual void collectGarbageAndGetDOMCounts(WebLocalFrame*) OVERRIDE;
73 
74 private:
75     void delayedGCAndReport(Timer<WebLeakDetectorImpl>*);
76     void delayedReport(Timer<WebLeakDetectorImpl>*);
77 
78     WebLeakDetectorClient* m_client;
79     Timer<WebLeakDetectorImpl> m_delayedGCAndReportTimer;
80     Timer<WebLeakDetectorImpl> m_delayedReportTimer;
81     int m_numberOfGCNeeded;
82 };
83 
collectGarbageAndGetDOMCounts(WebLocalFrame * frame)84 void WebLeakDetectorImpl::collectGarbageAndGetDOMCounts(WebLocalFrame* frame)
85 {
86     WebEmbeddedWorkerImpl::terminateAll();
87     memoryCache()->evictResources();
88 
89     {
90         RefPtrWillBeRawPtr<Document> document = PassRefPtrWillBeRawPtr<Document>(frame->document());
91         if (ResourceFetcher* fetcher = document->fetcher())
92             fetcher->garbageCollectDocumentResources();
93     }
94 
95     // FIXME: HTML5 Notification should be closed because notification affects the result of number of DOM objects.
96 
97     for (int i = 0; i < kNumberOfGCsToClaimChains; ++i)
98         V8GCController::collectGarbage(v8::Isolate::GetCurrent());
99     // Note: Oilpan precise GC is scheduled at the end of the event loop.
100 
101     // Task queue may contain delayed object destruction tasks.
102     // This method is called from navigation hook inside FrameLoader,
103     // so previous document is still held by the loader until the next event loop.
104     // Complete all pending tasks before proceeding to gc.
105     m_numberOfGCNeeded = 2;
106     m_delayedGCAndReportTimer.startOneShot(0, FROM_HERE);
107 }
108 
delayedGCAndReport(Timer<WebLeakDetectorImpl> *)109 void WebLeakDetectorImpl::delayedGCAndReport(Timer<WebLeakDetectorImpl>*)
110 {
111     // We do a second and third GC here to address flakiness
112     // The second GC is necessary as Resource GC may have postponed clean-up tasks to next event loop.
113     // The third GC is necessary for cleaning up Document after worker object died.
114 
115     for (int i = 0; i < kNumberOfGCsToClaimChains; ++i)
116         V8GCController::collectGarbage(V8PerIsolateData::mainThreadIsolate());
117     // Note: Oilpan precise GC is scheduled at the end of the event loop.
118 
119     // Inspect counters on the next event loop.
120     if (--m_numberOfGCNeeded)
121         m_delayedGCAndReportTimer.startOneShot(0, FROM_HERE);
122     else
123         m_delayedReportTimer.startOneShot(0, FROM_HERE);
124 }
125 
delayedReport(Timer<WebLeakDetectorImpl> *)126 void WebLeakDetectorImpl::delayedReport(Timer<WebLeakDetectorImpl>*)
127 {
128     ASSERT(m_client);
129 
130     WebLeakDetectorClient::Result result;
131     result.numberOfLiveAudioNodes = AudioNode::instanceCount();
132     result.numberOfLiveDocuments = InspectorCounters::counterValue(InspectorCounters::DocumentCounter);
133     result.numberOfLiveNodes = InspectorCounters::counterValue(InspectorCounters::NodeCounter);
134     result.numberOfLiveRenderObjects = RenderObject::instanceCount();
135     result.numberOfLiveResources = Resource::instanceCount();
136 
137     m_client->onLeakDetectionComplete(result);
138 
139 #ifndef NDEBUG
140     showLiveDocumentInstances();
141 #endif
142 }
143 
144 } // namespace
145 
create(WebLeakDetectorClient * client)146 WebLeakDetector* WebLeakDetector::create(WebLeakDetectorClient* client)
147 {
148     return new WebLeakDetectorImpl(client);
149 }
150 
151 } // namespace blink
152