1 /* 2 * Copyright 2009, The Android Open Source Project 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 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * 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 THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER 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 #ifndef TimeCounter_h 27 #define TimeCounter_h 28 29 #include "hardware_legacy/qemu_tracing.h" 30 31 namespace WebCore { 32 33 class KURL; 34 35 } 36 37 namespace android { 38 39 uint32_t getThreadMsec(); 40 41 #ifdef ANDROID_INSTRUMENT 42 43 class TimeCounter { 44 public: 45 enum Type { 46 // function base counters 47 CSSParseTimeCounter, 48 JavaScriptTimeCounter, 49 JavaScriptInitTimeCounter, 50 JavaScriptParseTimeCounter, 51 JavaScriptExecuteTimeCounter, 52 CalculateStyleTimeCounter, 53 JavaCallbackTimeCounter, 54 ParsingTimeCounter, 55 LayoutTimeCounter, 56 // file base counters 57 NativeCallbackTimeCounter, // WebCoreFrameBridge.cpp 58 ResourceTimeCounter, // WebCoreResourceLoader.cpp 59 SharedTimerTimeCounter, // JavaBridge.cpp 60 WebViewCoreBuildNavTimeCounter, 61 WebViewCoreRecordTimeCounter, 62 WebViewCoreTimeCounter, // WebViewCore.cpp 63 WebViewUIDrawTimeCounter, 64 TotalTimeCounterCount 65 }; 66 67 static void record(enum Type type, const char* functionName); 68 static void recordNoCounter(enum Type type, const char* functionName); 69 static void report(const WebCore::KURL& , int live, int dead, size_t arenaSize); 70 static void reportNow(); 71 static void reset(); 72 static void start(enum Type type); 73 private: 74 static uint32_t sStartWebCoreThreadTime; 75 static uint32_t sEndWebCoreThreadTime; 76 static bool sRecordWebCoreTime; 77 static uint32_t sTotalTimeUsed[TotalTimeCounterCount]; 78 static uint32_t sLastTimeUsed[TotalTimeCounterCount]; 79 static uint32_t sCounter[TotalTimeCounterCount]; 80 static uint32_t sLastCounter[TotalTimeCounterCount]; 81 static uint32_t sStartTime[TotalTimeCounterCount]; 82 friend class TimeCounterAuto; 83 }; 84 85 class TimeCounterAuto { 86 public: 87 TimeCounterAuto(TimeCounter::Type type) : 88 m_type(type), m_startTime(getThreadMsec()) {} 89 ~TimeCounterAuto() { 90 uint32_t time = getThreadMsec(); 91 TimeCounter::sEndWebCoreThreadTime = time; 92 TimeCounter::sTotalTimeUsed[m_type] += time - m_startTime; 93 TimeCounter::sCounter[m_type]++; 94 } 95 private: 96 TimeCounter::Type m_type; 97 uint32_t m_startTime; 98 }; 99 100 class QemuTracerAuto { 101 public: 102 QemuTracerAuto() { 103 if (!reentry_count) 104 qemu_start_tracing(); 105 reentry_count++; 106 } 107 108 ~QemuTracerAuto() { 109 reentry_count--; 110 if (!reentry_count) 111 qemu_stop_tracing(); 112 } 113 private: 114 static int reentry_count; 115 }; 116 #endif // ANDROID_INSTRUMENT 117 118 } 119 120 #endif 121