2010-06-25 Yuzo Fujishima <yuzo@google.com>
[WebKit-https.git] / WebKit / mac / Misc / WebCoreStatistics.mm
1 /*
2  * Copyright (C) 2005, 2006, 2008 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  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer. 
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution. 
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission. 
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #import "WebCoreStatistics.h"
30
31 #import "DOMElementInternal.h"
32 #import "WebCache.h"
33 #import "WebFrameInternal.h"
34 #import <runtime/JSLock.h>
35 #import <WebCore/Console.h>
36 #import <WebCore/FontCache.h>
37 #import <WebCore/Frame.h>
38 #import <WebCore/GCController.h>
39 #import <WebCore/GlyphPageTreeNode.h>
40 #import <WebCore/IconDatabase.h>
41 #import <WebCore/JSDOMWindow.h>
42 #import <WebCore/PageCache.h>
43 #import <WebCore/PrintContext.h>
44 #import <WebCore/RenderTreeAsText.h>
45 #import <WebCore/RenderView.h>
46
47 using namespace JSC;
48 using namespace WebCore;
49
50 @implementation WebCoreStatistics
51
52 + (NSArray *)statistics
53 {
54     return [WebCache statistics];
55 }
56
57 + (size_t)javaScriptObjectsCount
58 {
59     JSLock lock(SilenceAssertionsOnly);
60     return JSDOMWindow::commonJSGlobalData()->heap.objectCount();
61 }
62
63 + (size_t)javaScriptGlobalObjectsCount
64 {
65     JSLock lock(SilenceAssertionsOnly);
66     return JSDOMWindow::commonJSGlobalData()->heap.globalObjectCount();
67 }
68
69 + (size_t)javaScriptProtectedObjectsCount
70 {
71     JSLock lock(SilenceAssertionsOnly);
72     return JSDOMWindow::commonJSGlobalData()->heap.protectedObjectCount();
73 }
74
75 + (size_t)javaScriptProtectedGlobalObjectsCount
76 {
77     JSLock lock(SilenceAssertionsOnly);
78     return JSDOMWindow::commonJSGlobalData()->heap.protectedGlobalObjectCount();
79 }
80
81 + (NSCountedSet *)javaScriptProtectedObjectTypeCounts
82 {
83     JSLock lock(SilenceAssertionsOnly);
84     
85     NSCountedSet *result = [NSCountedSet set];
86
87     OwnPtr<HashCountedSet<const char*> > counts(JSDOMWindow::commonJSGlobalData()->heap.protectedObjectTypeCounts());
88     HashCountedSet<const char*>::iterator end = counts->end();
89     for (HashCountedSet<const char*>::iterator it = counts->begin(); it != end; ++it)
90         for (unsigned i = 0; i < it->second; ++i)
91             [result addObject:[NSString stringWithUTF8String:it->first]];
92     
93     return result;
94 }
95
96 + (NSCountedSet *)javaScriptObjectTypeCounts
97 {
98     JSLock lock(SilenceAssertionsOnly);
99     
100     NSCountedSet *result = [NSCountedSet set];
101
102     OwnPtr<HashCountedSet<const char*> > counts(JSDOMWindow::commonJSGlobalData()->heap.objectTypeCounts());
103     HashCountedSet<const char*>::iterator end = counts->end();
104     for (HashCountedSet<const char*>::iterator it = counts->begin(); it != end; ++it)
105         for (unsigned i = 0; i < it->second; ++i)
106             [result addObject:[NSString stringWithUTF8String:it->first]];
107     
108     return result;
109 }
110
111 + (void)garbageCollectJavaScriptObjects
112 {
113     gcController().garbageCollectNow();
114 }
115
116 + (void)garbageCollectJavaScriptObjectsOnAlternateThreadForDebugging:(BOOL)waitUntilDone
117 {
118     gcController().garbageCollectOnAlternateThreadForDebugging(waitUntilDone);
119 }
120
121 + (size_t)iconPageURLMappingCount
122 {
123     return iconDatabase()->pageURLMappingCount();
124 }
125
126 + (size_t)iconRetainedPageURLCount
127 {
128     return iconDatabase()->retainedPageURLCount();
129 }
130
131 + (size_t)iconRecordCount
132 {
133     return iconDatabase()->iconRecordCount();
134 }
135
136 + (size_t)iconsWithDataCount
137 {
138     return iconDatabase()->iconRecordCountWithData();
139 }
140
141 + (size_t)cachedFontDataCount
142 {
143     return fontCache()->fontDataCount();
144 }
145
146 + (size_t)cachedFontDataInactiveCount
147 {
148     return fontCache()->inactiveFontDataCount();
149 }
150
151 + (void)purgeInactiveFontData
152 {
153     fontCache()->purgeInactiveFontData();
154 }
155
156 + (size_t)glyphPageCount
157 {
158     return GlyphPageTreeNode::treeGlyphPageCount();
159 }
160
161 + (BOOL)shouldPrintExceptions
162 {
163     JSLock lock(SilenceAssertionsOnly);
164     return Console::shouldPrintExceptions();
165 }
166
167 + (void)setShouldPrintExceptions:(BOOL)print
168 {
169     JSLock lock(SilenceAssertionsOnly);
170     Console::setShouldPrintExceptions(print);
171 }
172
173 + (void)emptyCache
174 {
175     [WebCache empty];
176 }
177
178 + (void)setCacheDisabled:(BOOL)disabled
179 {
180     [WebCache setDisabled:disabled];
181 }
182
183 + (void)startIgnoringWebCoreNodeLeaks
184 {
185     WebCore::Node::startIgnoringLeaks();
186 }
187
188 + (void)stopIgnoringWebCoreNodeLeaks
189 {
190     WebCore::Node::stopIgnoringLeaks();
191 }
192
193 + (NSDictionary *)memoryStatistics
194 {
195     WTF::FastMallocStatistics fastMallocStatistics = WTF::fastMallocStatistics();
196     JSLock lock(SilenceAssertionsOnly);
197     Heap::Statistics jsHeapStatistics = JSDOMWindow::commonJSGlobalData()->heap.statistics();
198     return [NSDictionary dictionaryWithObjectsAndKeys:
199                 [NSNumber numberWithInt:fastMallocStatistics.reservedVMBytes], @"FastMallocReservedVMBytes",
200                 [NSNumber numberWithInt:fastMallocStatistics.committedVMBytes], @"FastMallocCommittedVMBytes",
201                 [NSNumber numberWithInt:fastMallocStatistics.freeListBytes], @"FastMallocFreeListBytes",
202                 [NSNumber numberWithInt:jsHeapStatistics.size], @"JavaScriptHeapSize",
203                 [NSNumber numberWithInt:jsHeapStatistics.free], @"JavaScriptFreeSize",
204             nil];
205 }
206
207 + (void)returnFreeMemoryToSystem
208 {
209     WTF::releaseFastMallocFreeMemory();
210 }
211
212 + (int)cachedPageCount
213 {
214     return pageCache()->pageCount();
215 }
216
217 + (int)cachedFrameCount
218 {
219     return pageCache()->frameCount();
220 }
221
222 + (int)autoreleasedPageCount
223 {
224     return pageCache()->autoreleasedPageCount();
225 }
226
227 // Deprecated
228 + (size_t)javaScriptNoGCAllowedObjectsCount
229 {
230     return 0;
231 }
232
233 + (size_t)javaScriptReferencedObjectsCount
234 {
235     JSLock lock(SilenceAssertionsOnly);
236     return JSDOMWindow::commonJSGlobalData()->heap.protectedObjectCount();
237 }
238
239 + (NSSet *)javaScriptRootObjectClasses
240 {
241     return [self javaScriptRootObjectTypeCounts];
242 }
243
244 + (size_t)javaScriptInterpretersCount
245 {
246     return [self javaScriptProtectedGlobalObjectsCount];
247 }
248
249 + (NSCountedSet *)javaScriptRootObjectTypeCounts
250 {
251     return [self javaScriptProtectedObjectTypeCounts];
252 }
253
254 @end
255
256 @implementation WebFrame (WebKitDebug)
257
258 - (NSString *)renderTreeAsExternalRepresentationForPrinting:(BOOL)forPrinting
259 {
260     return externalRepresentation(_private->coreFrame, forPrinting ? RenderAsTextPrintingMode : RenderAsTextBehaviorNormal);
261 }
262
263 - (NSString *)counterValueForElement:(DOMElement*)element
264 {
265     return counterValueForElement(core(element));
266 }
267
268 - (int)pageNumberForElement:(DOMElement*)element:(float)pageWidthInPixels:(float)pageHeightInPixels
269 {
270     return PrintContext::pageNumberForElement(core(element), FloatSize(pageWidthInPixels, pageHeightInPixels));
271 }
272
273 - (int)numberOfPages:(float)pageWidthInPixels:(float)pageHeightInPixels
274 {
275     return PrintContext::numberOfPages(_private->coreFrame, FloatSize(pageWidthInPixels, pageHeightInPixels));
276 }
277
278 - (NSString *)pageProperty:(const char *)propertyName:(int)pageNumber
279 {
280     return PrintContext::pageProperty(_private->coreFrame, propertyName, pageNumber);
281 }
282
283 - (bool)isPageBoxVisible:(int)pageNumber
284 {
285     return PrintContext::isPageBoxVisible(_private->coreFrame, pageNumber);
286 }
287
288 - (NSString *)pageSizeAndMarginsInPixels:(int)pageNumber:(int)width:(int)height:(int)marginTop:(int)marginRight:(int)marginBottom:(int)marginLeft
289 {
290     return PrintContext::pageSizeAndMarginsInPixels(_private->coreFrame, pageNumber, width, height, marginTop, marginRight, marginBottom, marginLeft);
291 }
292 @end