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