Home | History | Annotate | Download | only in Misc
      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.heapSize], @"FastMallocHeapSize",
    200                 [NSNumber numberWithInt:fastMallocStatistics.freeSizeInHeap], @"FastMallocFreeSizeInHeap",
    201                 [NSNumber numberWithInt:fastMallocStatistics.freeSizeInCaches], @"FastMallocFreeSizeInCaches",
    202                 [NSNumber numberWithInt:fastMallocStatistics.returnedSize], @"FastMallocReturnedSize",
    203                 [NSNumber numberWithInt:jsHeapStatistics.size], @"JavaScriptHeapSize",
    204                 [NSNumber numberWithInt:jsHeapStatistics.free], @"JavaScriptFreeSize",
    205             nil];
    206 }
    207 
    208 + (void)returnFreeMemoryToSystem
    209 {
    210     WTF::releaseFastMallocFreeMemory();
    211 }
    212 
    213 + (int)cachedPageCount
    214 {
    215     return pageCache()->pageCount();
    216 }
    217 
    218 + (int)cachedFrameCount
    219 {
    220     return pageCache()->frameCount();
    221 }
    222 
    223 + (int)autoreleasedPageCount
    224 {
    225     return pageCache()->autoreleasedPageCount();
    226 }
    227 
    228 // Deprecated
    229 + (size_t)javaScriptNoGCAllowedObjectsCount
    230 {
    231     return 0;
    232 }
    233 
    234 + (size_t)javaScriptReferencedObjectsCount
    235 {
    236     JSLock lock(SilenceAssertionsOnly);
    237     return JSDOMWindow::commonJSGlobalData()->heap.protectedObjectCount();
    238 }
    239 
    240 + (NSSet *)javaScriptRootObjectClasses
    241 {
    242     return [self javaScriptRootObjectTypeCounts];
    243 }
    244 
    245 + (size_t)javaScriptInterpretersCount
    246 {
    247     return [self javaScriptProtectedGlobalObjectsCount];
    248 }
    249 
    250 + (NSCountedSet *)javaScriptRootObjectTypeCounts
    251 {
    252     return [self javaScriptProtectedObjectTypeCounts];
    253 }
    254 
    255 @end
    256 
    257 @implementation WebFrame (WebKitDebug)
    258 
    259 - (NSString *)renderTreeAsExternalRepresentation
    260 {
    261     return externalRepresentation(_private->coreFrame);
    262 }
    263 
    264 - (NSString *)counterValueForElement:(DOMElement*)element
    265 {
    266     return counterValueForElement(core(element));
    267 }
    268 
    269 - (int)pageNumberForElement:(DOMElement*)element:(float)pageWidthInPixels:(float)pageHeightInPixels
    270 {
    271     return PrintContext::pageNumberForElement(core(element), FloatSize(pageWidthInPixels, pageHeightInPixels));
    272 }
    273 
    274 - (int)numberOfPages:(float)pageWidthInPixels:(float)pageHeightInPixels
    275 {
    276     return PrintContext::numberOfPages(_private->coreFrame, FloatSize(pageWidthInPixels, pageHeightInPixels));
    277 }
    278 
    279 @end
    280