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/counters.h"
6 
7 #include "src/base/platform/platform.h"
8 #include "src/isolate.h"
9 #include "src/log-inl.h"
10 
11 namespace v8 {
12 namespace internal {
13 
StatsTable()14 StatsTable::StatsTable()
15     : lookup_function_(NULL),
16       create_histogram_function_(NULL),
17       add_histogram_sample_function_(NULL) {}
18 
19 
FindLocationInStatsTable() const20 int* StatsCounter::FindLocationInStatsTable() const {
21   return isolate_->stats_table()->FindLocation(name_);
22 }
23 
24 
AddSample(int sample)25 void Histogram::AddSample(int sample) {
26   if (Enabled()) {
27     isolate()->stats_table()->AddHistogramSample(histogram_, sample);
28   }
29 }
30 
CreateHistogram() const31 void* Histogram::CreateHistogram() const {
32   return isolate()->stats_table()->
33       CreateHistogram(name_, min_, max_, num_buckets_);
34 }
35 
36 
37 // Start the timer.
Start()38 void HistogramTimer::Start() {
39   if (Enabled()) {
40     timer_.Start();
41   }
42   Logger::CallEventLogger(isolate(), name(), Logger::START, true);
43 }
44 
45 
46 // Stop the timer and record the results.
Stop()47 void HistogramTimer::Stop() {
48   if (Enabled()) {
49     int64_t sample = resolution_ == MICROSECOND
50                          ? timer_.Elapsed().InMicroseconds()
51                          : timer_.Elapsed().InMilliseconds();
52     // Compute the delta between start and stop, in microseconds.
53     AddSample(static_cast<int>(sample));
54     timer_.Stop();
55   }
56   Logger::CallEventLogger(isolate(), name(), Logger::END, true);
57 }
58 
59 
Counters(Isolate * isolate)60 Counters::Counters(Isolate* isolate) {
61 #define HR(name, caption, min, max, num_buckets) \
62   name##_ = Histogram(#caption, min, max, num_buckets, isolate);
63   HISTOGRAM_RANGE_LIST(HR)
64 #undef HR
65 
66 #define HT(name, caption, max, res) \
67   name##_ = HistogramTimer(#caption, 0, max, HistogramTimer::res, 50, isolate);
68     HISTOGRAM_TIMER_LIST(HT)
69 #undef HT
70 
71 #define AHT(name, caption) \
72   name##_ = AggregatableHistogramTimer(#caption, 0, 10000000, 50, isolate);
73     AGGREGATABLE_HISTOGRAM_TIMER_LIST(AHT)
74 #undef AHT
75 
76 #define HP(name, caption) \
77     name##_ = Histogram(#caption, 0, 101, 100, isolate);
78     HISTOGRAM_PERCENTAGE_LIST(HP)
79 #undef HP
80 
81 
82 // Exponential histogram assigns bucket limits to points
83 // p[1], p[2], ... p[n] such that p[i+1] / p[i] = constant.
84 // The constant factor is equal to the n-th root of (high / low),
85 // where the n is the number of buckets, the low is the lower limit,
86 // the high is the upper limit.
87 // For n = 50, low = 1000, high = 500000: the factor = 1.13.
88 #define HM(name, caption) \
89     name##_ = Histogram(#caption, 1000, 500000, 50, isolate);
90   HISTOGRAM_LEGACY_MEMORY_LIST(HM)
91 #undef HM
92 // For n = 100, low = 4000, high = 2000000: the factor = 1.06.
93 #define HM(name, caption) \
94   name##_ = Histogram(#caption, 4000, 2000000, 100, isolate);
95   HISTOGRAM_MEMORY_LIST(HM)
96 #undef HM
97 
98 #define HM(name, caption) \
99   aggregated_##name##_ = AggregatedMemoryHistogram<Histogram>(&name##_);
100     HISTOGRAM_MEMORY_LIST(HM)
101 #undef HM
102 
103 #define SC(name, caption) \
104     name##_ = StatsCounter(isolate, "c:" #caption);
105 
106     STATS_COUNTER_LIST_1(SC)
107     STATS_COUNTER_LIST_2(SC)
108 #undef SC
109 
110 #define SC(name) \
111     count_of_##name##_ = StatsCounter(isolate, "c:" "V8.CountOf_" #name); \
112     size_of_##name##_ = StatsCounter(isolate, "c:" "V8.SizeOf_" #name);
113     INSTANCE_TYPE_LIST(SC)
114 #undef SC
115 
116 #define SC(name) \
117     count_of_CODE_TYPE_##name##_ = \
118         StatsCounter(isolate, "c:" "V8.CountOf_CODE_TYPE-" #name); \
119     size_of_CODE_TYPE_##name##_ = \
120         StatsCounter(isolate, "c:" "V8.SizeOf_CODE_TYPE-" #name);
121     CODE_KIND_LIST(SC)
122 #undef SC
123 
124 #define SC(name) \
125     count_of_FIXED_ARRAY_##name##_ = \
126         StatsCounter(isolate, "c:" "V8.CountOf_FIXED_ARRAY-" #name); \
127     size_of_FIXED_ARRAY_##name##_ = \
128         StatsCounter(isolate, "c:" "V8.SizeOf_FIXED_ARRAY-" #name);
129     FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(SC)
130 #undef SC
131 
132 #define SC(name) \
133     count_of_CODE_AGE_##name##_ = \
134         StatsCounter(isolate, "c:" "V8.CountOf_CODE_AGE-" #name); \
135     size_of_CODE_AGE_##name##_ = \
136         StatsCounter(isolate, "c:" "V8.SizeOf_CODE_AGE-" #name);
137     CODE_AGE_LIST_COMPLETE(SC)
138 #undef SC
139 }
140 
141 
ResetCounters()142 void Counters::ResetCounters() {
143 #define SC(name, caption) name##_.Reset();
144   STATS_COUNTER_LIST_1(SC)
145   STATS_COUNTER_LIST_2(SC)
146 #undef SC
147 
148 #define SC(name)              \
149   count_of_##name##_.Reset(); \
150   size_of_##name##_.Reset();
151   INSTANCE_TYPE_LIST(SC)
152 #undef SC
153 
154 #define SC(name)                        \
155   count_of_CODE_TYPE_##name##_.Reset(); \
156   size_of_CODE_TYPE_##name##_.Reset();
157   CODE_KIND_LIST(SC)
158 #undef SC
159 
160 #define SC(name)                          \
161   count_of_FIXED_ARRAY_##name##_.Reset(); \
162   size_of_FIXED_ARRAY_##name##_.Reset();
163   FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(SC)
164 #undef SC
165 
166 #define SC(name)                       \
167   count_of_CODE_AGE_##name##_.Reset(); \
168   size_of_CODE_AGE_##name##_.Reset();
169   CODE_AGE_LIST_COMPLETE(SC)
170 #undef SC
171 }
172 
173 
ResetHistograms()174 void Counters::ResetHistograms() {
175 #define HR(name, caption, min, max, num_buckets) name##_.Reset();
176   HISTOGRAM_RANGE_LIST(HR)
177 #undef HR
178 
179 #define HT(name, caption, max, res) name##_.Reset();
180     HISTOGRAM_TIMER_LIST(HT)
181 #undef HT
182 
183 #define AHT(name, caption) name##_.Reset();
184     AGGREGATABLE_HISTOGRAM_TIMER_LIST(AHT)
185 #undef AHT
186 
187 #define HP(name, caption) name##_.Reset();
188     HISTOGRAM_PERCENTAGE_LIST(HP)
189 #undef HP
190 
191 #define HM(name, caption) name##_.Reset();
192     HISTOGRAM_LEGACY_MEMORY_LIST(HM)
193 #undef HM
194 }
195 
196 }  // namespace internal
197 }  // namespace v8
198