WebCoreStatistics.mm 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #import "WebCoreStatistics.h"
  29. #import "DOMElementInternal.h"
  30. #import "WebCache.h"
  31. #import "WebFrameInternal.h"
  32. #import <JavaScriptCore/JSLock.h>
  33. #import <JavaScriptCore/MemoryStatistics.h>
  34. #import <WebCore/FontCache.h>
  35. #import <WebCore/Frame.h>
  36. #import <WebCore/GCController.h>
  37. #import <WebCore/GlyphPageTreeNode.h>
  38. #import <WebCore/GraphicsContext.h>
  39. #import <WebCore/IconDatabase.h>
  40. #import <WebCore/JSDOMWindow.h>
  41. #import <WebCore/PageCache.h>
  42. #import <WebCore/PageConsole.h>
  43. #import <WebCore/PrintContext.h>
  44. #import <WebCore/RenderTreeAsText.h>
  45. #import <WebCore/RenderView.h>
  46. using namespace JSC;
  47. using namespace WebCore;
  48. @implementation WebCoreStatistics
  49. + (NSArray *)statistics
  50. {
  51. return [WebCache statistics];
  52. }
  53. + (size_t)javaScriptObjectsCount
  54. {
  55. JSLockHolder lock(JSDOMWindow::commonVM());
  56. return JSDOMWindow::commonVM()->heap.objectCount();
  57. }
  58. + (size_t)javaScriptGlobalObjectsCount
  59. {
  60. JSLockHolder lock(JSDOMWindow::commonVM());
  61. return JSDOMWindow::commonVM()->heap.globalObjectCount();
  62. }
  63. + (size_t)javaScriptProtectedObjectsCount
  64. {
  65. JSLockHolder lock(JSDOMWindow::commonVM());
  66. return JSDOMWindow::commonVM()->heap.protectedObjectCount();
  67. }
  68. + (size_t)javaScriptProtectedGlobalObjectsCount
  69. {
  70. JSLockHolder lock(JSDOMWindow::commonVM());
  71. return JSDOMWindow::commonVM()->heap.protectedGlobalObjectCount();
  72. }
  73. + (NSCountedSet *)javaScriptProtectedObjectTypeCounts
  74. {
  75. JSLockHolder lock(JSDOMWindow::commonVM());
  76. NSCountedSet *result = [NSCountedSet set];
  77. OwnPtr<TypeCountSet> counts(JSDOMWindow::commonVM()->heap.protectedObjectTypeCounts());
  78. HashCountedSet<const char*>::iterator end = counts->end();
  79. for (HashCountedSet<const char*>::iterator it = counts->begin(); it != end; ++it)
  80. for (unsigned i = 0; i < it->value; ++i)
  81. [result addObject:[NSString stringWithUTF8String:it->key]];
  82. return result;
  83. }
  84. + (NSCountedSet *)javaScriptObjectTypeCounts
  85. {
  86. JSLockHolder lock(JSDOMWindow::commonVM());
  87. NSCountedSet *result = [NSCountedSet set];
  88. OwnPtr<TypeCountSet> counts(JSDOMWindow::commonVM()->heap.objectTypeCounts());
  89. HashCountedSet<const char*>::iterator end = counts->end();
  90. for (HashCountedSet<const char*>::iterator it = counts->begin(); it != end; ++it)
  91. for (unsigned i = 0; i < it->value; ++i)
  92. [result addObject:[NSString stringWithUTF8String:it->key]];
  93. return result;
  94. }
  95. + (void)garbageCollectJavaScriptObjects
  96. {
  97. gcController().garbageCollectNow();
  98. }
  99. + (void)garbageCollectJavaScriptObjectsOnAlternateThreadForDebugging:(BOOL)waitUntilDone
  100. {
  101. gcController().garbageCollectOnAlternateThreadForDebugging(waitUntilDone);
  102. }
  103. + (void)setJavaScriptGarbageCollectorTimerEnabled:(BOOL)enable
  104. {
  105. gcController().setJavaScriptGarbageCollectorTimerEnabled(enable);
  106. }
  107. + (size_t)iconPageURLMappingCount
  108. {
  109. return iconDatabase().pageURLMappingCount();
  110. }
  111. + (size_t)iconRetainedPageURLCount
  112. {
  113. return iconDatabase().retainedPageURLCount();
  114. }
  115. + (size_t)iconRecordCount
  116. {
  117. return iconDatabase().iconRecordCount();
  118. }
  119. + (size_t)iconsWithDataCount
  120. {
  121. return iconDatabase().iconRecordCountWithData();
  122. }
  123. + (size_t)cachedFontDataCount
  124. {
  125. return fontCache()->fontDataCount();
  126. }
  127. + (size_t)cachedFontDataInactiveCount
  128. {
  129. return fontCache()->inactiveFontDataCount();
  130. }
  131. + (void)purgeInactiveFontData
  132. {
  133. fontCache()->purgeInactiveFontData();
  134. }
  135. + (size_t)glyphPageCount
  136. {
  137. return GlyphPageTreeNode::treeGlyphPageCount();
  138. }
  139. + (BOOL)shouldPrintExceptions
  140. {
  141. JSLockHolder lock(JSDOMWindow::commonVM());
  142. return PageConsole::shouldPrintExceptions();
  143. }
  144. + (void)setShouldPrintExceptions:(BOOL)print
  145. {
  146. JSLockHolder lock(JSDOMWindow::commonVM());
  147. PageConsole::setShouldPrintExceptions(print);
  148. }
  149. + (void)emptyCache
  150. {
  151. [WebCache empty];
  152. }
  153. + (void)setCacheDisabled:(BOOL)disabled
  154. {
  155. [WebCache setDisabled:disabled];
  156. }
  157. + (void)startIgnoringWebCoreNodeLeaks
  158. {
  159. WebCore::Node::startIgnoringLeaks();
  160. }
  161. + (void)stopIgnoringWebCoreNodeLeaks
  162. {
  163. WebCore::Node::stopIgnoringLeaks();
  164. }
  165. + (NSDictionary *)memoryStatistics
  166. {
  167. WTF::FastMallocStatistics fastMallocStatistics = WTF::fastMallocStatistics();
  168. JSLockHolder lock(JSDOMWindow::commonVM());
  169. size_t heapSize = JSDOMWindow::commonVM()->heap.size();
  170. size_t heapFree = JSDOMWindow::commonVM()->heap.capacity() - heapSize;
  171. GlobalMemoryStatistics globalMemoryStats = globalMemoryStatistics();
  172. return [NSDictionary dictionaryWithObjectsAndKeys:
  173. [NSNumber numberWithInt:fastMallocStatistics.reservedVMBytes], @"FastMallocReservedVMBytes",
  174. [NSNumber numberWithInt:fastMallocStatistics.committedVMBytes], @"FastMallocCommittedVMBytes",
  175. [NSNumber numberWithInt:fastMallocStatistics.freeListBytes], @"FastMallocFreeListBytes",
  176. [NSNumber numberWithInt:heapSize], @"JavaScriptHeapSize",
  177. [NSNumber numberWithInt:heapFree], @"JavaScriptFreeSize",
  178. [NSNumber numberWithUnsignedInt:(unsigned int)globalMemoryStats.stackBytes], @"JavaScriptStackSize",
  179. [NSNumber numberWithUnsignedInt:(unsigned int)globalMemoryStats.JITBytes], @"JavaScriptJITSize",
  180. nil];
  181. }
  182. + (void)returnFreeMemoryToSystem
  183. {
  184. WTF::releaseFastMallocFreeMemory();
  185. }
  186. + (int)cachedPageCount
  187. {
  188. return pageCache()->pageCount();
  189. }
  190. + (int)cachedFrameCount
  191. {
  192. return pageCache()->frameCount();
  193. }
  194. // Deprecated
  195. + (int)autoreleasedPageCount
  196. {
  197. return 0;
  198. }
  199. // Deprecated
  200. + (size_t)javaScriptNoGCAllowedObjectsCount
  201. {
  202. return 0;
  203. }
  204. + (size_t)javaScriptReferencedObjectsCount
  205. {
  206. JSLockHolder lock(JSDOMWindow::commonVM());
  207. return JSDOMWindow::commonVM()->heap.protectedObjectCount();
  208. }
  209. + (NSSet *)javaScriptRootObjectClasses
  210. {
  211. return [self javaScriptRootObjectTypeCounts];
  212. }
  213. + (size_t)javaScriptInterpretersCount
  214. {
  215. return [self javaScriptProtectedGlobalObjectsCount];
  216. }
  217. + (NSCountedSet *)javaScriptRootObjectTypeCounts
  218. {
  219. return [self javaScriptProtectedObjectTypeCounts];
  220. }
  221. @end
  222. @implementation WebFrame (WebKitDebug)
  223. - (NSString *)renderTreeAsExternalRepresentationForPrinting:(BOOL)forPrinting
  224. {
  225. return externalRepresentation(_private->coreFrame, forPrinting ? RenderAsTextPrintingMode : RenderAsTextBehaviorNormal);
  226. }
  227. - (int)numberOfPagesWithPageWidth:(float)pageWidthInPixels pageHeight:(float)pageHeightInPixels
  228. {
  229. return PrintContext::numberOfPages(_private->coreFrame, FloatSize(pageWidthInPixels, pageHeightInPixels));
  230. }
  231. - (void)printToCGContext:(CGContextRef)cgContext pageWidth:(float)pageWidthInPixels pageHeight:(float)pageHeightInPixels
  232. {
  233. Frame* coreFrame = _private->coreFrame;
  234. if (!coreFrame)
  235. return;
  236. GraphicsContext graphicsContext(cgContext);
  237. PrintContext::spoolAllPagesWithBoundaries(coreFrame, graphicsContext, FloatSize(pageWidthInPixels, pageHeightInPixels));
  238. }
  239. @end