OSDN Git Service

Merge "Add logging to help debug an ANR" into ics-mr1
[android-x86/external-webkit.git] / Source / WebCore / platform / graphics / android / PerformanceMonitor.cpp
1 /*
2  * Copyright 2011, 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 #include "PerformanceMonitor.h"
27
28 #include <wtf/text/CString.h>
29
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__)
34
35 namespace WebCore {
36
37 PerformanceMonitor::PerformanceMonitor()
38 {
39 }
40
41 PerformanceMonitor::~PerformanceMonitor()
42 {
43 }
44
45 void PerformanceMonitor::start(const String &tag)
46 {
47     if (tag.isEmpty())
48         return;
49     PerfItem *item;
50     if (m_tags.contains(tag))
51         item = m_tags.get(tag);
52     else {
53         item = new PerfItem();
54         m_tags.set(tag, item);
55     }
56     gettimeofday(&(item->start_time), NULL);
57 }
58
59 void PerformanceMonitor::stop(const String &tag)
60 {
61     if (!m_tags.contains(tag))
62         return;
63     PerfItem *item = m_tags.get(tag);
64     struct timeval end;
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;
69
70     float mtime = (seconds * 1000.0) + (useconds/1000.0);
71
72     float avg = 0;
73     if (item->average_ms) {
74         item->average_ms = (item->average_ms + mtime) / 2;
75     } else
76         item->average_ms = mtime;
77 }
78
79 float PerformanceMonitor::getAverageDuration(const String &tag)
80 {
81     if (tag.isEmpty() || !m_tags.contains(tag))
82         return 0;
83     return m_tags.get(tag)->average_ms;
84 }
85
86 void PerformanceMonitor::display(int limit)
87 {
88     bool shown = false;
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) {
93            if (!shown) {
94                 XLOGC("=== DISPLAY MONITOR ====");
95                 shown = true;
96            }
97            XLOGC("item %s took longer than %d ms: %.2f", it->first.latin1().data(), limit, item->average_ms);
98         }
99     }
100     if (shown)
101         XLOGC("=== END DISPLAY MONITOR ====");
102 }
103
104 } // namespace WebCore