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/v8/V8Binding.h" 36 #include "bindings/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 "platform/Timer.h" 42 #include "public/web/WebDocument.h" 43 #include "public/web/WebLocalFrame.h" 44 45 #include <v8.h> 46 47 using namespace WebCore; 48 49 namespace blink { 50 51 namespace { 52 53 // FIXME: Oilpan: It may take multiple GC to collect on-heap objects referenced from off-heap objects. 54 // Please see comment in Heap::collectAllGarbage() 55 static const int kNumberOfGCsToClaimChains = 5; 56 57 class WebLeakDetectorImpl FINAL : public WebLeakDetector { 58 WTF_MAKE_NONCOPYABLE(WebLeakDetectorImpl); 59 public: 60 explicit WebLeakDetectorImpl(WebLeakDetectorClient* client) 61 : m_client(client) 62 , m_delayedGCAndReportTimer(this, &WebLeakDetectorImpl::delayedGCAndReport) 63 , m_delayedReportTimer(this, &WebLeakDetectorImpl::delayedReport) 64 { 65 ASSERT(m_client); 66 } 67 68 virtual ~WebLeakDetectorImpl() { } 69 70 virtual void collectGarbageAndGetDOMCounts(WebLocalFrame*) OVERRIDE; 71 72 private: 73 void delayedGCAndReport(Timer<WebLeakDetectorImpl>*); 74 void delayedReport(Timer<WebLeakDetectorImpl>*); 75 76 WebLeakDetectorClient* m_client; 77 Timer<WebLeakDetectorImpl> m_delayedGCAndReportTimer; 78 Timer<WebLeakDetectorImpl> m_delayedReportTimer; 79 }; 80 81 void WebLeakDetectorImpl::collectGarbageAndGetDOMCounts(WebLocalFrame* frame) 82 { 83 memoryCache()->evictResources(); 84 85 { 86 RefPtrWillBeRawPtr<Document> document = PassRefPtrWillBeRawPtr<Document>(frame->document()); 87 if (ResourceFetcher* fetcher = document->fetcher()) 88 fetcher->garbageCollectDocumentResources(); 89 } 90 91 // FIXME: HTML5 Notification should be closed because notification affects the result of number of DOM objects. 92 93 for (int i = 0; i < kNumberOfGCsToClaimChains; ++i) 94 V8GCController::collectGarbage(v8::Isolate::GetCurrent()); 95 // Note: Oilpan precise GC is scheduled at the end of the event loop. 96 97 // Task queue may contain delayed object destruction tasks. 98 // This method is called from navigation hook inside FrameLoader, 99 // so previous document is still held by the loader until the next event loop. 100 // Complete all pending tasks before proceeding to gc. 101 m_delayedGCAndReportTimer.startOneShot(0, FROM_HERE); 102 } 103 104 void WebLeakDetectorImpl::delayedGCAndReport(Timer<WebLeakDetectorImpl>*) 105 { 106 // We do a second GC here to address flakiness: Resource GC may have postponed clean-up tasks to next event loop. 107 108 for (int i = 0; i < kNumberOfGCsToClaimChains; ++i) 109 V8GCController::collectGarbage(V8PerIsolateData::mainThreadIsolate()); 110 // Note: Oilpan precise GC is scheduled at the end of the event loop. 111 112 // Inspect counters on the next event loop. 113 m_delayedReportTimer.startOneShot(0, FROM_HERE); 114 } 115 116 void WebLeakDetectorImpl::delayedReport(Timer<WebLeakDetectorImpl>*) 117 { 118 ASSERT(m_client); 119 120 WebLeakDetectorClient::Result result; 121 result.numberOfLiveDocuments = InspectorCounters::counterValue(InspectorCounters::DocumentCounter); 122 result.numberOfLiveNodes = InspectorCounters::counterValue(InspectorCounters::NodeCounter); 123 124 m_client->onLeakDetectionComplete(result); 125 } 126 127 } // namespace 128 129 WebLeakDetector* WebLeakDetector::create(WebLeakDetectorClient* client) 130 { 131 return new WebLeakDetectorImpl(client); 132 } 133 134 } // namespace blink 135