2 * Copyright 2011, The Android Open Source Project
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #include "PerformanceMonitor.h"
28 #include <wtf/text/CString.h>
30 #include <wtf/CurrentTime.h>
31 #include <cutils/log.h>
32 #include <wtf/text/CString.h>
33 #define XLOGC(...) android_printLog(ANDROID_LOG_DEBUG, "PerformanceMonitor", __VA_ARGS__)
37 PerformanceMonitor::PerformanceMonitor()
41 PerformanceMonitor::~PerformanceMonitor()
45 void PerformanceMonitor::start(const String &tag)
50 if (m_tags.contains(tag))
51 item = m_tags.get(tag);
53 item = new PerfItem();
54 m_tags.set(tag, item);
56 gettimeofday(&(item->start_time), NULL);
59 void PerformanceMonitor::stop(const String &tag)
61 if (!m_tags.contains(tag))
63 PerfItem *item = m_tags.get(tag);
65 gettimeofday(&end, NULL);
66 long seconds, useconds;
67 seconds = end.tv_sec - item->start_time.tv_sec;
68 useconds = end.tv_usec - item->start_time.tv_usec;
70 float mtime = (seconds * 1000.0) + (useconds/1000.0);
73 if (item->average_ms) {
74 item->average_ms = (item->average_ms + mtime) / 2;
76 item->average_ms = mtime;
79 float PerformanceMonitor::getAverageDuration(const String &tag)
81 if (tag.isEmpty() || !m_tags.contains(tag))
83 return m_tags.get(tag)->average_ms;
86 void PerformanceMonitor::display(int limit)
89 HashMap<String, PerfItem*, StringHash>::iterator end = m_tags.end();
90 for (HashMap<String, PerfItem*, StringHash>::iterator it = m_tags.begin(); it != end; ++it) {
91 PerfItem* item = it->second;
92 if (item->average_ms > limit) {
94 XLOGC("=== DISPLAY MONITOR ====");
97 XLOGC("item %s took longer than %d ms: %.2f", it->first.latin1().data(), limit, item->average_ms);
101 XLOGC("=== END DISPLAY MONITOR ====");
104 } // namespace WebCore