1d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck/*
2d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck * Copyright 2011, The Android Open Source Project
3d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck *
4d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck * Redistribution and use in source and binary forms, with or without
5d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck * modification, are permitted provided that the following conditions
6d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck * are met:
7d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck *  * Redistributions of source code must retain the above copyright
8d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck *    notice, this list of conditions and the following disclaimer.
9d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck *  * Redistributions in binary form must reproduce the above copyright
10d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck *    notice, this list of conditions and the following disclaimer in the
11d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck *    documentation and/or other materials provided with the distribution.
12d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck *
13d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck */
25d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck
26d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck#include "PerformanceMonitor.h"
27d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck
28d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck#include <wtf/text/CString.h>
29d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck
305997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard#include <wtf/CurrentTime.h>
315997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard#include <cutils/log.h>
325997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard#include <wtf/text/CString.h>
335997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard#define XLOGC(...) android_printLog(ANDROID_LOG_DEBUG, "PerformanceMonitor", __VA_ARGS__)
345997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard
35d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Recknamespace WebCore {
36d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck
37d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn ReckPerformanceMonitor::PerformanceMonitor()
38d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck{
39d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck}
40d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck
41d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn ReckPerformanceMonitor::~PerformanceMonitor()
42d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck{
43d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck}
44d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck
45d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reckvoid PerformanceMonitor::start(const String &tag)
46d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck{
47d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    if (tag.isEmpty())
48d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck        return;
49d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    PerfItem *item;
50d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    if (m_tags.contains(tag))
51d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck        item = m_tags.get(tag);
52d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    else {
53d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck        item = new PerfItem();
54d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck        m_tags.set(tag, item);
55d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    }
56d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    gettimeofday(&(item->start_time), NULL);
57d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck}
58d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck
59d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reckvoid PerformanceMonitor::stop(const String &tag)
60d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck{
61d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    if (!m_tags.contains(tag))
62d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck        return;
63d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    PerfItem *item = m_tags.get(tag);
64d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    struct timeval end;
65d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    gettimeofday(&end, NULL);
66d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    long seconds, useconds;
67d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    seconds  = end.tv_sec  - item->start_time.tv_sec;
68d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    useconds = end.tv_usec - item->start_time.tv_usec;
69d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck
70d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    float mtime = (seconds * 1000.0) + (useconds/1000.0);
71d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck
72d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    float avg = 0;
73d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    if (item->average_ms) {
74d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck        item->average_ms = (item->average_ms + mtime) / 2;
75d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    } else
76d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck        item->average_ms = mtime;
77d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck}
78d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck
79d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reckfloat PerformanceMonitor::getAverageDuration(const String &tag)
80d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck{
81d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    if (tag.isEmpty() || !m_tags.contains(tag))
82d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck        return 0;
83d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck    return m_tags.get(tag)->average_ms;
84d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck}
85d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck
865997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roardvoid PerformanceMonitor::display(int limit)
875997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard{
885997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard    bool shown = false;
895997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard    HashMap<String, PerfItem*, StringHash>::iterator end = m_tags.end();
905997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard    for (HashMap<String, PerfItem*, StringHash>::iterator it = m_tags.begin(); it != end; ++it) {
915997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard        PerfItem* item = it->second;
925997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard        if (item->average_ms > limit) {
935997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard           if (!shown) {
945997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard                XLOGC("=== DISPLAY MONITOR ====");
955997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard                shown = true;
965997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard           }
975997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard           XLOGC("item %s took longer than %d ms: %.2f", it->first.latin1().data(), limit, item->average_ms);
985997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard        }
995997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard    }
1005997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard    if (shown)
1015997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard        XLOGC("=== END DISPLAY MONITOR ====");
1025997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard}
1035997528e55d0dc6734718bde61faa8513b1dd54fNicolas Roard
104d05cd8d73ef0b36cd415af17933fe79f9f8f328dJohn Reck} // namespace WebCore
105