• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/logging/counters.h"
6 
7 #include "src/base/atomic-utils.h"
8 #include "src/base/platform/elapsed-timer.h"
9 #include "src/base/platform/time.h"
10 #include "src/builtins/builtins-definitions.h"
11 #include "src/execution/isolate.h"
12 #include "src/logging/log-inl.h"
13 #include "src/logging/log.h"
14 
15 namespace v8 {
16 namespace internal {
17 
StatsTable(Counters * counters)18 StatsTable::StatsTable(Counters* counters)
19     : lookup_function_(nullptr),
20       create_histogram_function_(nullptr),
21       add_histogram_sample_function_(nullptr) {}
22 
SetCounterFunction(CounterLookupCallback f)23 void StatsTable::SetCounterFunction(CounterLookupCallback f) {
24   lookup_function_ = f;
25 }
26 
27 namespace {
28 std::atomic<int> unused_counter_dump{0};
29 }
30 
Enabled()31 bool StatsCounter::Enabled() { return GetPtr() != &unused_counter_dump; }
32 
SetupPtrFromStatsTable()33 std::atomic<int>* StatsCounter::SetupPtrFromStatsTable() {
34   // {Init} must have been called.
35   DCHECK_NOT_NULL(counters_);
36   DCHECK_NOT_NULL(name_);
37   int* location = counters_->FindLocation(name_);
38   std::atomic<int>* ptr =
39       location ? base::AsAtomicPtr(location) : &unused_counter_dump;
40 #ifdef DEBUG
41   std::atomic<int>* old_ptr = ptr_.exchange(ptr, std::memory_order_release);
42   DCHECK_IMPLIES(old_ptr, old_ptr == ptr);
43 #else
44   ptr_.store(ptr, std::memory_order_release);
45 #endif
46   return ptr;
47 }
48 
AddSample(int sample)49 void Histogram::AddSample(int sample) {
50   if (Enabled()) {
51     counters_->AddHistogramSample(histogram_, sample);
52   }
53 }
54 
CreateHistogram() const55 V8_EXPORT_PRIVATE void* Histogram::CreateHistogram() const {
56   return counters_->CreateHistogram(name_, min_, max_, num_buckets_);
57 }
58 
Stop(base::ElapsedTimer * timer)59 void TimedHistogram::Stop(base::ElapsedTimer* timer) {
60   DCHECK(Enabled());
61   AddTimedSample(timer->Elapsed());
62   timer->Stop();
63 }
64 
AddTimedSample(base::TimeDelta sample)65 void TimedHistogram::AddTimedSample(base::TimeDelta sample) {
66   if (Enabled()) {
67     int64_t sample_int = resolution_ == TimedHistogramResolution::MICROSECOND
68                              ? sample.InMicroseconds()
69                              : sample.InMilliseconds();
70     AddSample(static_cast<int>(sample_int));
71   }
72 }
73 
RecordAbandon(base::ElapsedTimer * timer,Isolate * isolate)74 void TimedHistogram::RecordAbandon(base::ElapsedTimer* timer,
75                                    Isolate* isolate) {
76   if (Enabled()) {
77     DCHECK(timer->IsStarted());
78     timer->Stop();
79     int64_t sample = resolution_ == TimedHistogramResolution::MICROSECOND
80                          ? base::TimeDelta::Max().InMicroseconds()
81                          : base::TimeDelta::Max().InMilliseconds();
82     AddSample(static_cast<int>(sample));
83   }
84   if (isolate != nullptr) {
85     Logger::CallEventLogger(isolate, name(), v8::LogEventStatus::kEnd, true);
86   }
87 }
88 
89 #ifdef DEBUG
ToggleRunningState(bool expect_to_run) const90 bool TimedHistogram::ToggleRunningState(bool expect_to_run) const {
91   static thread_local base::LazyInstance<
92       std::unordered_map<const TimedHistogram*, bool>>::type active_timer =
93       LAZY_INSTANCE_INITIALIZER;
94   bool is_running = (*active_timer.Pointer())[this];
95   DCHECK_NE(is_running, expect_to_run);
96   (*active_timer.Pointer())[this] = !is_running;
97   return true;
98 }
99 #endif
100 
Counters(Isolate * isolate)101 Counters::Counters(Isolate* isolate)
102     :
103 #ifdef V8_RUNTIME_CALL_STATS
104       runtime_call_stats_(RuntimeCallStats::kMainIsolateThread),
105       worker_thread_runtime_call_stats_(),
106 #endif
107       isolate_(isolate),
108       stats_table_(this) {
109   static const struct {
110     Histogram Counters::*member;
111     const char* caption;
112     int min;
113     int max;
114     int num_buckets;
115   } kHistograms[] = {
116 #define HR(name, caption, min, max, num_buckets) \
117   {&Counters::name##_, #caption, min, max, num_buckets},
118       HISTOGRAM_RANGE_LIST(HR)
119 #undef HR
120   };
121   for (const auto& histogram : kHistograms) {
122     (this->*histogram.member)
123         .Initialize(histogram.caption, histogram.min, histogram.max,
124                     histogram.num_buckets, this);
125   }
126 
127   const int DefaultTimedHistogramNumBuckets = 50;
128 
129   static const struct {
130     NestedTimedHistogram Counters::*member;
131     const char* caption;
132     int max;
133     TimedHistogramResolution res;
134   } kNestedTimedHistograms[] = {
135 #define HT(name, caption, max, res) \
136   {&Counters::name##_, #caption, max, TimedHistogramResolution::res},
137       NESTED_TIMED_HISTOGRAM_LIST(HT) NESTED_TIMED_HISTOGRAM_LIST_SLOW(HT)
138 #undef HT
139   };
140   for (const auto& timer : kNestedTimedHistograms) {
141     (this->*timer.member)
142         .Initialize(timer.caption, 0, timer.max, timer.res,
143                     DefaultTimedHistogramNumBuckets, this);
144   }
145 
146   static const struct {
147     TimedHistogram Counters::*member;
148     const char* caption;
149     int max;
150     TimedHistogramResolution res;
151   } kTimedHistograms[] = {
152 #define HT(name, caption, max, res) \
153   {&Counters::name##_, #caption, max, TimedHistogramResolution::res},
154       TIMED_HISTOGRAM_LIST(HT)
155 #undef HT
156   };
157   for (const auto& timer : kTimedHistograms) {
158     (this->*timer.member)
159         .Initialize(timer.caption, 0, timer.max, timer.res,
160                     DefaultTimedHistogramNumBuckets, this);
161   }
162 
163   static const struct {
164     AggregatableHistogramTimer Counters::*member;
165     const char* caption;
166   } kAggregatableHistogramTimers[] = {
167 #define AHT(name, caption) {&Counters::name##_, #caption},
168       AGGREGATABLE_HISTOGRAM_TIMER_LIST(AHT)
169 #undef AHT
170   };
171   for (const auto& aht : kAggregatableHistogramTimers) {
172     (this->*aht.member)
173         .Initialize(aht.caption, 0, 10000000, DefaultTimedHistogramNumBuckets,
174                     this);
175   }
176 
177   static const struct {
178     Histogram Counters::*member;
179     const char* caption;
180   } kHistogramPercentages[] = {
181 #define HP(name, caption) {&Counters::name##_, #caption},
182       HISTOGRAM_PERCENTAGE_LIST(HP)
183 #undef HP
184   };
185   for (const auto& percentage : kHistogramPercentages) {
186     (this->*percentage.member)
187         .Initialize(percentage.caption, 0, 101, 100, this);
188   }
189 
190   // Exponential histogram assigns bucket limits to points
191   // p[1], p[2], ... p[n] such that p[i+1] / p[i] = constant.
192   // The constant factor is equal to the n-th root of (high / low),
193   // where the n is the number of buckets, the low is the lower limit,
194   // the high is the upper limit.
195   // For n = 50, low = 1000, high = 500000: the factor = 1.13.
196   static const struct {
197     Histogram Counters::*member;
198     const char* caption;
199   } kLegacyMemoryHistograms[] = {
200 #define HM(name, caption) {&Counters::name##_, #caption},
201       HISTOGRAM_LEGACY_MEMORY_LIST(HM)
202 #undef HM
203   };
204   for (const auto& histogram : kLegacyMemoryHistograms) {
205     (this->*histogram.member)
206         .Initialize(histogram.caption, 1000, 500000, 50, this);
207   }
208 
209   static constexpr struct {
210     StatsCounter Counters::*member;
211     const char* caption;
212   } kStatsCounters[] = {
213 #define SC(name, caption) {&Counters::name##_, "c:" caption},
214 #define BARE_SC(name, caption) SC(name, #caption)
215 #define COUNT_AND_SIZE_SC(name)            \
216   SC(count_of_##name, "V8.CountOf_" #name) \
217   SC(size_of_##name, "V8.SizeOf_" #name)
218 #define CODE_KIND_SC(name) COUNT_AND_SIZE_SC(CODE_TYPE_##name)
219 #define FIXED_ARRAY_INSTANCE_TYPE_SC(name) COUNT_AND_SIZE_SC(FIXED_ARRAY_##name)
220 
221       // clang-format off
222   STATS_COUNTER_LIST_1(BARE_SC)
223   STATS_COUNTER_LIST_2(BARE_SC)
224   STATS_COUNTER_NATIVE_CODE_LIST(BARE_SC)
225   INSTANCE_TYPE_LIST(COUNT_AND_SIZE_SC)
226   CODE_KIND_LIST(CODE_KIND_SC)
227   FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(FIXED_ARRAY_INSTANCE_TYPE_SC)
228   // clang-format on
229 
230 #undef FIXED_ARRAY_INSTANCE_TYPE_SC
231 #undef CODE_KIND_SC
232 #undef COUNT_AND_SIZE_SC
233 #undef BARE_SC
234 #undef SC
235   };
236   for (const auto& counter : kStatsCounters) {
237     (this->*counter.member).Init(this, counter.caption);
238   }
239 }
240 
ResetCounterFunction(CounterLookupCallback f)241 void Counters::ResetCounterFunction(CounterLookupCallback f) {
242   stats_table_.SetCounterFunction(f);
243 
244 #define SC(name, caption) name##_.Reset();
245   STATS_COUNTER_LIST_1(SC)
246   STATS_COUNTER_LIST_2(SC)
247   STATS_COUNTER_NATIVE_CODE_LIST(SC)
248 #undef SC
249 
250 #define SC(name)              \
251   count_of_##name##_.Reset(); \
252   size_of_##name##_.Reset();
253   INSTANCE_TYPE_LIST(SC)
254 #undef SC
255 
256 #define SC(name)                        \
257   count_of_CODE_TYPE_##name##_.Reset(); \
258   size_of_CODE_TYPE_##name##_.Reset();
259   CODE_KIND_LIST(SC)
260 #undef SC
261 
262 #define SC(name)                          \
263   count_of_FIXED_ARRAY_##name##_.Reset(); \
264   size_of_FIXED_ARRAY_##name##_.Reset();
265   FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(SC)
266 #undef SC
267 }
268 
ResetCreateHistogramFunction(CreateHistogramCallback f)269 void Counters::ResetCreateHistogramFunction(CreateHistogramCallback f) {
270   stats_table_.SetCreateHistogramFunction(f);
271 
272 #define HR(name, caption, min, max, num_buckets) name##_.Reset();
273   HISTOGRAM_RANGE_LIST(HR)
274 #undef HR
275 
276 #define HT(name, caption, max, res) name##_.Reset();
277   NESTED_TIMED_HISTOGRAM_LIST(HT)
278 #undef HT
279 
280 #define HT(name, caption, max, res) name##_.Reset();
281   NESTED_TIMED_HISTOGRAM_LIST_SLOW(HT)
282 #undef HT
283 
284 #define HT(name, caption, max, res) name##_.Reset();
285   TIMED_HISTOGRAM_LIST(HT)
286 #undef HT
287 
288 #define AHT(name, caption) name##_.Reset();
289   AGGREGATABLE_HISTOGRAM_TIMER_LIST(AHT)
290 #undef AHT
291 
292 #define HP(name, caption) name##_.Reset();
293   HISTOGRAM_PERCENTAGE_LIST(HP)
294 #undef HP
295 
296 #define HM(name, caption) name##_.Reset();
297   HISTOGRAM_LEGACY_MEMORY_LIST(HM)
298 #undef HM
299 }
300 
301 }  // namespace internal
302 }  // namespace v8
303