2 * Copyright (C) 2005, 2006, 2008 Apple 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
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.
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.
29 #import "WebCoreStatistics.h"
31 #import "DOMElementInternal.h"
33 #import "WebFrameInternal.h"
34 #import <JavaScriptCore/JSLock.h>
35 #import <JavaScriptCore/MemoryStatistics.h>
36 #import <WebCore/Console.h>
37 #import <WebCore/FontCache.h>
38 #import <WebCore/Frame.h>
39 #import <WebCore/GCController.h>
40 #import <WebCore/GlyphPageTreeNode.h>
41 #import <WebCore/GraphicsContext.h>
42 #import <WebCore/IconDatabase.h>
43 #import <WebCore/JSDOMWindow.h>
44 #import <WebCore/PageCache.h>
45 #import <WebCore/PrintContext.h>
46 #import <WebCore/RenderTreeAsText.h>
47 #import <WebCore/RenderView.h>
50 using namespace WebCore;
52 @implementation WebCoreStatistics
54 + (NSArray *)statistics
56 return [WebCache statistics];
59 + (size_t)javaScriptObjectsCount
61 JSLock lock(SilenceAssertionsOnly);
62 return JSDOMWindow::commonJSGlobalData()->heap.objectCount();
65 + (size_t)javaScriptGlobalObjectsCount
67 JSLock lock(SilenceAssertionsOnly);
68 return JSDOMWindow::commonJSGlobalData()->heap.globalObjectCount();
71 + (size_t)javaScriptProtectedObjectsCount
73 JSLock lock(SilenceAssertionsOnly);
74 return JSDOMWindow::commonJSGlobalData()->heap.protectedObjectCount();
77 + (size_t)javaScriptProtectedGlobalObjectsCount
79 JSLock lock(SilenceAssertionsOnly);
80 return JSDOMWindow::commonJSGlobalData()->heap.protectedGlobalObjectCount();
83 + (NSCountedSet *)javaScriptProtectedObjectTypeCounts
85 JSLock lock(SilenceAssertionsOnly);
87 NSCountedSet *result = [NSCountedSet set];
89 OwnPtr<TypeCountSet> counts(JSDOMWindow::commonJSGlobalData()->heap.protectedObjectTypeCounts());
90 HashCountedSet<const char*>::iterator end = counts->end();
91 for (HashCountedSet<const char*>::iterator it = counts->begin(); it != end; ++it)
92 for (unsigned i = 0; i < it->second; ++i)
93 [result addObject:[NSString stringWithUTF8String:it->first]];
98 + (NSCountedSet *)javaScriptObjectTypeCounts
100 JSLock lock(SilenceAssertionsOnly);
102 NSCountedSet *result = [NSCountedSet set];
104 OwnPtr<TypeCountSet> counts(JSDOMWindow::commonJSGlobalData()->heap.objectTypeCounts());
105 HashCountedSet<const char*>::iterator end = counts->end();
106 for (HashCountedSet<const char*>::iterator it = counts->begin(); it != end; ++it)
107 for (unsigned i = 0; i < it->second; ++i)
108 [result addObject:[NSString stringWithUTF8String:it->first]];
113 + (void)garbageCollectJavaScriptObjects
115 gcController().garbageCollectNow();
118 + (void)garbageCollectJavaScriptObjectsOnAlternateThreadForDebugging:(BOOL)waitUntilDone
120 gcController().garbageCollectOnAlternateThreadForDebugging(waitUntilDone);
123 + (size_t)iconPageURLMappingCount
125 return iconDatabase().pageURLMappingCount();
128 + (size_t)iconRetainedPageURLCount
130 return iconDatabase().retainedPageURLCount();
133 + (size_t)iconRecordCount
135 return iconDatabase().iconRecordCount();
138 + (size_t)iconsWithDataCount
140 return iconDatabase().iconRecordCountWithData();
143 + (size_t)cachedFontDataCount
145 return fontCache()->fontDataCount();
148 + (size_t)cachedFontDataInactiveCount
150 return fontCache()->inactiveFontDataCount();
153 + (void)purgeInactiveFontData
155 fontCache()->purgeInactiveFontData();
158 + (size_t)glyphPageCount
160 return GlyphPageTreeNode::treeGlyphPageCount();
163 + (BOOL)shouldPrintExceptions
165 JSLock lock(SilenceAssertionsOnly);
166 return Console::shouldPrintExceptions();
169 + (void)setShouldPrintExceptions:(BOOL)print
171 JSLock lock(SilenceAssertionsOnly);
172 Console::setShouldPrintExceptions(print);
180 + (void)setCacheDisabled:(BOOL)disabled
182 [WebCache setDisabled:disabled];
185 + (void)startIgnoringWebCoreNodeLeaks
187 WebCore::Node::startIgnoringLeaks();
190 + (void)stopIgnoringWebCoreNodeLeaks
192 WebCore::Node::stopIgnoringLeaks();
195 + (NSDictionary *)memoryStatistics
197 WTF::FastMallocStatistics fastMallocStatistics = WTF::fastMallocStatistics();
199 JSLock lock(SilenceAssertionsOnly);
200 size_t heapSize = JSDOMWindow::commonJSGlobalData()->heap.size();
201 size_t heapFree = JSDOMWindow::commonJSGlobalData()->heap.capacity() - heapSize;
202 GlobalMemoryStatistics globalMemoryStats = globalMemoryStatistics();
204 return [NSDictionary dictionaryWithObjectsAndKeys:
205 [NSNumber numberWithInt:fastMallocStatistics.reservedVMBytes], @"FastMallocReservedVMBytes",
206 [NSNumber numberWithInt:fastMallocStatistics.committedVMBytes], @"FastMallocCommittedVMBytes",
207 [NSNumber numberWithInt:fastMallocStatistics.freeListBytes], @"FastMallocFreeListBytes",
208 [NSNumber numberWithInt:heapSize], @"JavaScriptHeapSize",
209 [NSNumber numberWithInt:heapFree], @"JavaScriptFreeSize",
210 [NSNumber numberWithUnsignedInt:(unsigned int)globalMemoryStats.stackBytes], @"JavaScriptStackSize",
211 [NSNumber numberWithUnsignedInt:(unsigned int)globalMemoryStats.JITBytes], @"JavaScriptJITSize",
215 + (void)returnFreeMemoryToSystem
217 WTF::releaseFastMallocFreeMemory();
220 + (int)cachedPageCount
222 return pageCache()->pageCount();
225 + (int)cachedFrameCount
227 return pageCache()->frameCount();
230 + (int)autoreleasedPageCount
232 return pageCache()->autoreleasedPageCount();
236 + (size_t)javaScriptNoGCAllowedObjectsCount
241 + (size_t)javaScriptReferencedObjectsCount
243 JSLock lock(SilenceAssertionsOnly);
244 return JSDOMWindow::commonJSGlobalData()->heap.protectedObjectCount();
247 + (NSSet *)javaScriptRootObjectClasses
249 return [self javaScriptRootObjectTypeCounts];
252 + (size_t)javaScriptInterpretersCount
254 return [self javaScriptProtectedGlobalObjectsCount];
257 + (NSCountedSet *)javaScriptRootObjectTypeCounts
259 return [self javaScriptProtectedObjectTypeCounts];
264 @implementation WebFrame (WebKitDebug)
266 - (NSString *)renderTreeAsExternalRepresentationForPrinting:(BOOL)forPrinting
268 return externalRepresentation(_private->coreFrame, forPrinting ? RenderAsTextPrintingMode : RenderAsTextBehaviorNormal);
271 - (NSString *)counterValueForElement:(DOMElement*)element
273 return counterValueForElement(core(element));
276 - (int)pageNumberForElement:(DOMElement*)element:(float)pageWidthInPixels:(float)pageHeightInPixels
278 return PrintContext::pageNumberForElement(core(element), FloatSize(pageWidthInPixels, pageHeightInPixels));
281 - (int)numberOfPages:(float)pageWidthInPixels:(float)pageHeightInPixels
283 return PrintContext::numberOfPages(_private->coreFrame, FloatSize(pageWidthInPixels, pageHeightInPixels));
286 - (NSString *)pageProperty:(const char *)propertyName:(int)pageNumber
288 return PrintContext::pageProperty(_private->coreFrame, propertyName, pageNumber);
291 - (bool)isPageBoxVisible:(int)pageNumber
293 return PrintContext::isPageBoxVisible(_private->coreFrame, pageNumber);
296 - (NSString *)pageSizeAndMarginsInPixels:(int)pageNumber:(int)width:(int)height:(int)marginTop:(int)marginRight:(int)marginBottom:(int)marginLeft
298 return PrintContext::pageSizeAndMarginsInPixels(_private->coreFrame, pageNumber, width, height, marginTop, marginRight, marginBottom, marginLeft);
301 - (void)printToCGContext:(CGContextRef)cgContext:(float)pageWidthInPixels:(float)pageHeightInPixels
303 Frame* coreFrame = _private->coreFrame;
307 GraphicsContext graphicsContext(cgContext);
308 PrintContext::spoolAllPagesWithBoundaries(coreFrame, graphicsContext, FloatSize(pageWidthInPixels, pageHeightInPixels));