1/*
2 * Copyright (C) 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 "PerformanceLogging.h"
28
29#include "CommonVM.h"
30#include "DOMWindow.h"
31#include "Document.h"
32#include "Frame.h"
33#include "FrameLoader.h"
34#include "FrameLoaderClient.h"
35#include "JSDOMWindow.h"
36#include "Logging.h"
37#include "Page.h"
38#include "PageCache.h"
39
40namespace WebCore {
41
42#if !RELEASE_LOG_DISABLED
43static const char* toString(PerformanceLogging::PointOfInterest poi)
44{
45 switch (poi) {
46 case PerformanceLogging::MainFrameLoadStarted:
47 return "MainFrameLoadStarted";
48 case PerformanceLogging::MainFrameLoadCompleted:
49 return "MainFrameLoadCompleted";
50 }
51}
52#endif
53
54HashMap<const char*, size_t> PerformanceLogging::memoryUsageStatistics(ShouldIncludeExpensiveComputations includeExpensive)
55{
56 HashMap<const char*, size_t> stats;
57
58 auto& vm = commonVM();
59 stats.add("javascript_gc_heap_capacity", vm.heap.capacity());
60 stats.add("javascript_gc_heap_extra_memory_size", vm.heap.extraMemorySize());
61
62 auto& pageCache = PageCache::singleton();
63 stats.add("pagecache_page_count", pageCache.pageCount());
64
65 stats.add("document_count", Document::allDocuments().size());
66
67 if (includeExpensive == ShouldIncludeExpensiveComputations::Yes) {
68 stats.add("javascript_gc_heap_size", vm.heap.size());
69 stats.add("javascript_gc_object_count", vm.heap.objectCount());
70 stats.add("javascript_gc_protected_object_count", vm.heap.protectedObjectCount());
71 stats.add("javascript_gc_global_object_count", vm.heap.globalObjectCount());
72 stats.add("javascript_gc_protected_global_object_count", vm.heap.protectedGlobalObjectCount());
73 }
74
75 return stats;
76}
77
78HashCountedSet<const char*> PerformanceLogging::javaScriptObjectCounts()
79{
80 return WTFMove(*commonVM().heap.objectTypeCounts());
81}
82
83PerformanceLogging::PerformanceLogging(Page& page)
84 : m_page(page)
85{
86}
87
88void PerformanceLogging::didReachPointOfInterest(PointOfInterest poi)
89{
90#if RELEASE_LOG_DISABLED
91 UNUSED_PARAM(poi);
92#else
93 // Ignore synthetic main frames used internally by SVG and web inspector.
94 if (m_page.mainFrame().loader().client().isEmptyFrameLoaderClient())
95 return;
96
97 auto stats = memoryUsageStatistics(ShouldIncludeExpensiveComputations::No);
98 getPlatformMemoryUsageStatistics(stats);
99
100 RELEASE_LOG(PerformanceLogging, "Memory usage info dump at %s:", toString(poi));
101 for (auto& it : stats)
102 RELEASE_LOG(PerformanceLogging, " %s: %zu", it.key, it.value);
103#endif
104}
105
106#if !PLATFORM(COCOA)
107void PerformanceLogging::getPlatformMemoryUsageStatistics(HashMap<const char*, size_t>&) { }
108Optional<uint64_t> PerformanceLogging::physicalFootprint() { return WTF::nullopt; }
109#endif
110
111}
112