1/*
2 * Copyright 2010, 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
27#ifdef ANDROID_INSTRUMENT
28
29#define LOG_TAG "WebCore"
30
31#include "config.h"
32#include "V8Counters.h"
33
34#include "NotImplemented.h"
35#include <utils/Log.h>
36#include <wtf/text/CString.h>
37#include <wtf/text/StringHash.h>
38
39#if USE(V8)
40
41namespace WebCore {
42
43V8Counters::Counter::Counter(bool isHistogram)
44    : m_count(0), m_sampleTotal(0), m_isHistogram(isHistogram) { }
45
46void V8Counters::Counter::addSample(int sample)
47{
48  m_count++;
49  m_sampleTotal += sample;
50}
51
52HashMap<String, V8Counters::Counter*> V8Counters::m_counters;
53
54// static
55int* V8Counters::counterForName(const char* name)
56{
57    Counter* counter = m_counters.get(name);
58    if (!counter) {
59        counter = new Counter(false);
60        m_counters.add(name, counter);
61    }
62    return *counter;
63}
64
65// static
66void* V8Counters::createHistogram(const char* name, int min, int max,
67        size_t buckets)
68{
69    Counter* counter = new Counter(true);
70    m_counters.add(name, counter);
71    return counter;
72}
73
74// static
75void V8Counters::addHistogramSample(void* histogram, int sample)
76{
77    Counter* counter = reinterpret_cast<Counter*>(histogram);
78    counter->addSample(sample);
79}
80
81// static
82void V8Counters::initCounters()
83{
84    static bool isInitialized = false;
85    if (!isInitialized) {
86        v8::V8::SetCounterFunction(counterForName);
87        v8::V8::SetCreateHistogramFunction(createHistogram);
88        v8::V8::SetAddHistogramSampleFunction(addHistogramSample);
89        isInitialized = true;
90    }
91}
92
93// static
94void V8Counters::dumpCounters()
95{
96    LOGD("+----------------------------------------+-------------+\n");
97    LOGD("| Name                                   | Value       |\n");
98    LOGD("+----------------------------------------+-------------+\n");
99    typedef HashMap<String, V8Counters::Counter*>::iterator CounterIterator;
100    for (CounterIterator iter = m_counters.begin(); iter != m_counters.end(); ++iter) {
101        Counter* counter = iter->second;
102        if (counter->isHistogram()) {
103            LOGD("| c:%-36s | %11i |\n", iter->first.latin1().data(), counter->count());
104            LOGD("| t:%-36s | %11i |\n", iter->first.latin1().data(), counter->sampleTotal());
105          } else {
106            LOGD("| %-38s | %11i |\n", iter->first.latin1().data(), counter->count());
107          }
108    }
109    LOGD("+----------------------------------------+-------------+\n");
110}
111
112}
113
114#endif // ANDROID_INSTRUMENT
115
116#endif // USE(V8)
117