1// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/v8.h"
6
7#include "src/base/platform/platform.h"
8#include "src/counters.h"
9#include "src/isolate.h"
10
11namespace v8 {
12namespace internal {
13
14StatsTable::StatsTable()
15    : lookup_function_(NULL),
16      create_histogram_function_(NULL),
17      add_histogram_sample_function_(NULL) {}
18
19
20int* StatsCounter::FindLocationInStatsTable() const {
21  return isolate_->stats_table()->FindLocation(name_);
22}
23
24
25void Histogram::AddSample(int sample) {
26  if (Enabled()) {
27    isolate()->stats_table()->AddHistogramSample(histogram_, sample);
28  }
29}
30
31void* Histogram::CreateHistogram() const {
32  return isolate()->stats_table()->
33      CreateHistogram(name_, min_, max_, num_buckets_);
34}
35
36
37// Start the timer.
38void HistogramTimer::Start() {
39  if (Enabled()) {
40    timer_.Start();
41  }
42  isolate()->event_logger()(name(), Logger::START);
43}
44
45
46// Stop the timer and record the results.
47void HistogramTimer::Stop() {
48  if (Enabled()) {
49    // Compute the delta between start and stop, in milliseconds.
50    AddSample(static_cast<int>(timer_.Elapsed().InMilliseconds()));
51    timer_.Stop();
52  }
53  isolate()->event_logger()(name(), Logger::END);
54}
55
56
57Counters::Counters(Isolate* isolate) {
58#define HR(name, caption, min, max, num_buckets) \
59  name##_ = Histogram(#caption, min, max, num_buckets, isolate);
60  HISTOGRAM_RANGE_LIST(HR)
61#undef HR
62
63#define HT(name, caption) \
64    name##_ = HistogramTimer(#caption, 0, 10000, 50, isolate);
65    HISTOGRAM_TIMER_LIST(HT)
66#undef HT
67
68#define HP(name, caption) \
69    name##_ = Histogram(#caption, 0, 101, 100, isolate);
70    HISTOGRAM_PERCENTAGE_LIST(HP)
71#undef HP
72
73#define HM(name, caption) \
74    name##_ = Histogram(#caption, 1000, 500000, 50, isolate);
75    HISTOGRAM_MEMORY_LIST(HM)
76#undef HM
77
78#define SC(name, caption) \
79    name##_ = StatsCounter(isolate, "c:" #caption);
80
81    STATS_COUNTER_LIST_1(SC)
82    STATS_COUNTER_LIST_2(SC)
83#undef SC
84
85#define SC(name) \
86    count_of_##name##_ = StatsCounter(isolate, "c:" "V8.CountOf_" #name); \
87    size_of_##name##_ = StatsCounter(isolate, "c:" "V8.SizeOf_" #name);
88    INSTANCE_TYPE_LIST(SC)
89#undef SC
90
91#define SC(name) \
92    count_of_CODE_TYPE_##name##_ = \
93        StatsCounter(isolate, "c:" "V8.CountOf_CODE_TYPE-" #name); \
94    size_of_CODE_TYPE_##name##_ = \
95        StatsCounter(isolate, "c:" "V8.SizeOf_CODE_TYPE-" #name);
96    CODE_KIND_LIST(SC)
97#undef SC
98
99#define SC(name) \
100    count_of_FIXED_ARRAY_##name##_ = \
101        StatsCounter(isolate, "c:" "V8.CountOf_FIXED_ARRAY-" #name); \
102    size_of_FIXED_ARRAY_##name##_ = \
103        StatsCounter(isolate, "c:" "V8.SizeOf_FIXED_ARRAY-" #name);
104    FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(SC)
105#undef SC
106
107#define SC(name) \
108    count_of_CODE_AGE_##name##_ = \
109        StatsCounter(isolate, "c:" "V8.CountOf_CODE_AGE-" #name); \
110    size_of_CODE_AGE_##name##_ = \
111        StatsCounter(isolate, "c:" "V8.SizeOf_CODE_AGE-" #name);
112    CODE_AGE_LIST_COMPLETE(SC)
113#undef SC
114}
115
116
117void Counters::ResetCounters() {
118#define SC(name, caption) name##_.Reset();
119  STATS_COUNTER_LIST_1(SC)
120  STATS_COUNTER_LIST_2(SC)
121#undef SC
122
123#define SC(name)              \
124  count_of_##name##_.Reset(); \
125  size_of_##name##_.Reset();
126  INSTANCE_TYPE_LIST(SC)
127#undef SC
128
129#define SC(name)                        \
130  count_of_CODE_TYPE_##name##_.Reset(); \
131  size_of_CODE_TYPE_##name##_.Reset();
132  CODE_KIND_LIST(SC)
133#undef SC
134
135#define SC(name)                          \
136  count_of_FIXED_ARRAY_##name##_.Reset(); \
137  size_of_FIXED_ARRAY_##name##_.Reset();
138  FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(SC)
139#undef SC
140
141#define SC(name)                       \
142  count_of_CODE_AGE_##name##_.Reset(); \
143  size_of_CODE_AGE_##name##_.Reset();
144  CODE_AGE_LIST_COMPLETE(SC)
145#undef SC
146}
147
148
149void Counters::ResetHistograms() {
150#define HR(name, caption, min, max, num_buckets) name##_.Reset();
151  HISTOGRAM_RANGE_LIST(HR)
152#undef HR
153
154#define HT(name, caption) name##_.Reset();
155    HISTOGRAM_TIMER_LIST(HT)
156#undef HT
157
158#define HP(name, caption) name##_.Reset();
159    HISTOGRAM_PERCENTAGE_LIST(HP)
160#undef HP
161
162#define HM(name, caption) name##_.Reset();
163    HISTOGRAM_MEMORY_LIST(HM)
164#undef HM
165}
166
167} }  // namespace v8::internal
168